phy.c.orig 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860
  1. /*
  2. * drivers/net/phy/phy.c
  3. *
  4. * Framework for configuring and reading PHY devices
  5. * Based on code in sungem_phy.c and gianfar_phy.c
  6. *
  7. * Author: Andy Fleming
  8. *
  9. * Copyright (c) 2004 Freescale Semiconductor, Inc.
  10. *
  11. * This program is free software; you can redistribute it and/or modify it
  12. * under the terms of the GNU General Public License as published by the
  13. * Free Software Foundation; either version 2 of the License, or (at your
  14. * option) any later version.
  15. *
  16. */
  17. #include <linux/config.h>
  18. #include <linux/kernel.h>
  19. #include <linux/sched.h>
  20. #include <linux/string.h>
  21. #include <linux/errno.h>
  22. #include <linux/unistd.h>
  23. #include <linux/slab.h>
  24. #include <linux/interrupt.h>
  25. #include <linux/init.h>
  26. #include <linux/delay.h>
  27. #include <linux/netdevice.h>
  28. #include <linux/etherdevice.h>
  29. #include <linux/skbuff.h>
  30. #include <linux/spinlock.h>
  31. #include <linux/mm.h>
  32. #include <linux/module.h>
  33. #include <linux/version.h>
  34. #include <linux/mii.h>
  35. #include <linux/ethtool.h>
  36. #include <linux/phy.h>
  37. #include <asm/io.h>
  38. #include <asm/irq.h>
  39. #include <asm/uaccess.h>
  40. static void phy_change(void *data);
  41. static void phy_timer(unsigned long data);
  42. /* Convenience function to print out the current phy status
  43. */
  44. void phy_print_status(struct phy_device *phydev)
  45. {
  46. pr_info("%s: Link is %s", phydev->dev.bus_id,
  47. phydev->link ? "Up" : "Down");
  48. if (phydev->link)
  49. printk(" - %d/%s", phydev->speed,
  50. DUPLEX_FULL == phydev->duplex ?
  51. "Full" : "Half");
  52. printk("\n");
  53. }
  54. EXPORT_SYMBOL(phy_print_status);
  55. /* Convenience functions for reading/writing a given PHY
  56. * register. They MUST NOT be called from interrupt context,
  57. * because the bus read/write functions may wait for an interrupt
  58. * to conclude the operation. */
  59. int phy_read(struct phy_device *phydev, u16 regnum)
  60. {
  61. int retval;
  62. struct mii_bus *bus = phydev->bus;
  63. spin_lock_bh(&bus->mdio_lock);
  64. retval = bus->read(bus, phydev->addr, regnum);
  65. spin_unlock_bh(&bus->mdio_lock);
  66. return retval;
  67. }
  68. EXPORT_SYMBOL(phy_read);
  69. int phy_write(struct phy_device *phydev, u16 regnum, u16 val)
  70. {
  71. int err;
  72. struct mii_bus *bus = phydev->bus;
  73. spin_lock_bh(&bus->mdio_lock);
  74. err = bus->write(bus, phydev->addr, regnum, val);
  75. spin_unlock_bh(&bus->mdio_lock);
  76. return err;
  77. }
  78. EXPORT_SYMBOL(phy_write);
  79. int phy_clear_interrupt(struct phy_device *phydev)
  80. {
  81. int err = 0;
  82. if (phydev->drv->ack_interrupt)
  83. err = phydev->drv->ack_interrupt(phydev);
  84. return err;
  85. }
  86. int phy_config_interrupt(struct phy_device *phydev, u32 interrupts)
  87. {
  88. int err = 0;
  89. phydev->interrupts = interrupts;
  90. if (phydev->drv->config_intr)
  91. err = phydev->drv->config_intr(phydev);
  92. return err;
  93. }
  94. /* phy_aneg_done
  95. *
  96. * description: Reads the status register and returns 0 either if
  97. * auto-negotiation is incomplete, or if there was an error.
  98. * Returns BMSR_ANEGCOMPLETE if auto-negotiation is done.
  99. */
  100. static inline int phy_aneg_done(struct phy_device *phydev)
  101. {
  102. int retval;
  103. retval = phy_read(phydev, MII_BMSR);
  104. return (retval < 0) ? retval : (retval & BMSR_ANEGCOMPLETE);
  105. }
  106. /* phy_start_aneg
  107. *
  108. * description: Calls the PHY driver's config_aneg, and then
  109. * sets the PHY state to PHY_AN if auto-negotiation is enabled,
  110. * and to PHY_FORCING if auto-negotiation is disabled. Unless
  111. * the PHY is currently HALTED.
  112. */
  113. int phy_start_aneg(struct phy_device *phydev)
  114. {
  115. int err;
  116. spin_lock(&phydev->lock);
  117. if (AUTONEG_DISABLE == phydev->autoneg)
  118. phy_sanitize_settings(phydev);
  119. err = phydev->drv->config_aneg(phydev);
  120. if (err < 0)
  121. goto out_unlock;
  122. if (phydev->state != PHY_HALTED) {
  123. if (AUTONEG_ENABLE == phydev->autoneg) {
  124. phydev->state = PHY_AN;
  125. phydev->link_timeout = PHY_AN_TIMEOUT;
  126. } else {
  127. phydev->state = PHY_FORCING;
  128. phydev->link_timeout = PHY_FORCE_TIMEOUT;
  129. }
  130. }
  131. out_unlock:
  132. spin_unlock(&phydev->lock);
  133. return err;
  134. }
  135. EXPORT_SYMBOL(phy_start_aneg);
  136. /* A structure for mapping a particular speed and duplex
  137. * combination to a particular SUPPORTED and ADVERTISED value */
  138. struct phy_setting {
  139. int speed;
  140. int duplex;
  141. u32 setting;
  142. };
  143. /* A mapping of all SUPPORTED settings to speed/duplex */
  144. static struct phy_setting settings[] = {
  145. {
  146. .speed = 10000,
  147. .duplex = DUPLEX_FULL,
  148. .setting = SUPPORTED_10000baseT_Full,
  149. },
  150. {
  151. .speed = SPEED_1000,
  152. .duplex = DUPLEX_FULL,
  153. .setting = SUPPORTED_1000baseT_Full,
  154. },
  155. {
  156. .speed = SPEED_1000,
  157. .duplex = DUPLEX_HALF,
  158. .setting = SUPPORTED_1000baseT_Half,
  159. },
  160. {
  161. .speed = SPEED_100,
  162. .duplex = DUPLEX_FULL,
  163. .setting = SUPPORTED_100baseT_Full,
  164. },
  165. {
  166. .speed = SPEED_100,
  167. .duplex = DUPLEX_HALF,
  168. .setting = SUPPORTED_100baseT_Half,
  169. },
  170. {
  171. .speed = SPEED_10,
  172. .duplex = DUPLEX_FULL,
  173. .setting = SUPPORTED_10baseT_Full,
  174. },
  175. {
  176. .speed = SPEED_10,
  177. .duplex = DUPLEX_HALF,
  178. .setting = SUPPORTED_10baseT_Half,
  179. },
  180. };
  181. #define MAX_NUM_SETTINGS (sizeof(settings)/sizeof(struct phy_setting))
  182. /* phy_find_setting
  183. *
  184. * description: Searches the settings array for the setting which
  185. * matches the desired speed and duplex, and returns the index
  186. * of that setting. Returns the index of the last setting if
  187. * none of the others match.
  188. */
  189. static inline int phy_find_setting(int speed, int duplex)
  190. {
  191. int idx = 0;
  192. while (idx < ARRAY_SIZE(settings) &&
  193. (settings[idx].speed != speed ||
  194. settings[idx].duplex != duplex))
  195. idx++;
  196. return idx < MAX_NUM_SETTINGS ? idx : MAX_NUM_SETTINGS - 1;
  197. }
  198. /* phy_find_valid
  199. * idx: The first index in settings[] to search
  200. * features: A mask of the valid settings
  201. *
  202. * description: Returns the index of the first valid setting less
  203. * than or equal to the one pointed to by idx, as determined by
  204. * the mask in features. Returns the index of the last setting
  205. * if nothing else matches.
  206. */
  207. static inline int phy_find_valid(int idx, u32 features)
  208. {
  209. while (idx < MAX_NUM_SETTINGS && !(settings[idx].setting & features))
  210. idx++;
  211. return idx < MAX_NUM_SETTINGS ? idx : MAX_NUM_SETTINGS - 1;
  212. }
  213. /* phy_sanitize_settings
  214. *
  215. * description: Make sure the PHY is set to supported speeds and
  216. * duplexes. Drop down by one in this order: 1000/FULL,
  217. * 1000/HALF, 100/FULL, 100/HALF, 10/FULL, 10/HALF
  218. */
  219. void phy_sanitize_settings(struct phy_device *phydev)
  220. {
  221. u32 features = phydev->supported;
  222. int idx;
  223. /* Sanitize settings based on PHY capabilities */
  224. if ((features & SUPPORTED_Autoneg) == 0)
  225. phydev->autoneg = 0;
  226. idx = phy_find_valid(phy_find_setting(phydev->speed, phydev->duplex),
  227. features);
  228. phydev->speed = settings[idx].speed;
  229. phydev->duplex = settings[idx].duplex;
  230. }
  231. EXPORT_SYMBOL(phy_sanitize_settings);
  232. /* phy_force_reduction
  233. *
  234. * description: Reduces the speed/duplex settings by
  235. * one notch. The order is so:
  236. * 1000/FULL, 1000/HALF, 100/FULL, 100/HALF,
  237. * 10/FULL, 10/HALF. The function bottoms out at 10/HALF.
  238. */
  239. static void phy_force_reduction(struct phy_device *phydev)
  240. {
  241. int idx;
  242. idx = phy_find_setting(phydev->speed, phydev->duplex);
  243. idx++;
  244. idx = phy_find_valid(idx, phydev->supported);
  245. phydev->speed = settings[idx].speed;
  246. phydev->duplex = settings[idx].duplex;
  247. pr_info("Trying %d/%s\n", phydev->speed,
  248. DUPLEX_FULL == phydev->duplex ?
  249. "FULL" : "HALF");
  250. }
  251. /* phy_ethtool_sset:
  252. * A generic ethtool sset function. Handles all the details
  253. *
  254. * A few notes about parameter checking:
  255. * - We don't set port or transceiver, so we don't care what they
  256. * were set to.
  257. * - phy_start_aneg() will make sure forced settings are sane, and
  258. * choose the next best ones from the ones selected, so we don't
  259. * care if ethtool tries to give us bad values
  260. */
  261. int phy_ethtool_sset(struct phy_device *phydev, struct ethtool_cmd *cmd)
  262. {
  263. if (cmd->phy_address != phydev->addr)
  264. return -EINVAL;
  265. /* We make sure that we don't pass unsupported
  266. * values in to the PHY */
  267. cmd->advertising &= phydev->supported;
  268. /* Verify the settings we care about. */
  269. if (cmd->autoneg != AUTONEG_ENABLE && cmd->autoneg != AUTONEG_DISABLE)
  270. return -EINVAL;
  271. if (cmd->autoneg == AUTONEG_ENABLE && cmd->advertising == 0)
  272. return -EINVAL;
  273. if (cmd->autoneg == AUTONEG_DISABLE
  274. && ((cmd->speed != SPEED_1000
  275. && cmd->speed != SPEED_100
  276. && cmd->speed != SPEED_10)
  277. || (cmd->duplex != DUPLEX_HALF
  278. && cmd->duplex != DUPLEX_FULL)))
  279. return -EINVAL;
  280. phydev->autoneg = cmd->autoneg;
  281. phydev->speed = cmd->speed;
  282. phydev->advertising = cmd->advertising;
  283. if (AUTONEG_ENABLE == cmd->autoneg)
  284. phydev->advertising |= ADVERTISED_Autoneg;
  285. else
  286. phydev->advertising &= ~ADVERTISED_Autoneg;
  287. phydev->duplex = cmd->duplex;
  288. /* Restart the PHY */
  289. phy_start_aneg(phydev);
  290. return 0;
  291. }
  292. int phy_ethtool_gset(struct phy_device *phydev, struct ethtool_cmd *cmd)
  293. {
  294. cmd->supported = phydev->supported;
  295. cmd->advertising = phydev->advertising;
  296. cmd->speed = phydev->speed;
  297. cmd->duplex = phydev->duplex;
  298. cmd->port = PORT_MII;
  299. cmd->phy_address = phydev->addr;
  300. cmd->transceiver = XCVR_EXTERNAL;
  301. cmd->autoneg = phydev->autoneg;
  302. return 0;
  303. }
  304. /* Note that this function is currently incompatible with the
  305. * PHYCONTROL layer. It changes registers without regard to
  306. * current state. Use at own risk
  307. */
  308. int phy_mii_ioctl(struct phy_device *phydev,
  309. struct mii_ioctl_data *mii_data, int cmd)
  310. {
  311. u16 val = mii_data->val_in;
  312. switch (cmd) {
  313. case SIOCGMIIPHY:
  314. mii_data->phy_id = phydev->addr;
  315. break;
  316. case SIOCGMIIREG:
  317. mii_data->val_out = phy_read(phydev, mii_data->reg_num);
  318. break;
  319. case SIOCSMIIREG:
  320. if (!capable(CAP_NET_ADMIN))
  321. return -EPERM;
  322. if (mii_data->phy_id == phydev->addr) {
  323. switch(mii_data->reg_num) {
  324. case MII_BMCR:
  325. if (val & (BMCR_RESET|BMCR_ANENABLE))
  326. phydev->autoneg = AUTONEG_DISABLE;
  327. else
  328. phydev->autoneg = AUTONEG_ENABLE;
  329. if ((!phydev->autoneg) && (val & BMCR_FULLDPLX))
  330. phydev->duplex = DUPLEX_FULL;
  331. else
  332. phydev->duplex = DUPLEX_HALF;
  333. break;
  334. case MII_ADVERTISE:
  335. phydev->advertising = val;
  336. break;
  337. default:
  338. /* do nothing */
  339. break;
  340. }
  341. }
  342. phy_write(phydev, mii_data->reg_num, val);
  343. if (mii_data->reg_num == MII_BMCR
  344. && val & BMCR_RESET
  345. && phydev->drv->config_init)
  346. phydev->drv->config_init(phydev);
  347. break;
  348. }
  349. return 0;
  350. }
  351. /* phy_start_machine:
  352. *
  353. * description: The PHY infrastructure can run a state machine
  354. * which tracks whether the PHY is starting up, negotiating,
  355. * etc. This function starts the timer which tracks the state
  356. * of the PHY. If you want to be notified when the state
  357. * changes, pass in the callback, otherwise, pass NULL. If you
  358. * want to maintain your own state machine, do not call this
  359. * function. */
  360. void phy_start_machine(struct phy_device *phydev,
  361. void (*handler)(struct net_device *))
  362. {
  363. phydev->adjust_state = handler;
  364. init_timer(&phydev->phy_timer);
  365. phydev->phy_timer.function = &phy_timer;
  366. phydev->phy_timer.data = (unsigned long) phydev;
  367. mod_timer(&phydev->phy_timer, jiffies + HZ);
  368. }
  369. /* phy_stop_machine
  370. *
  371. * description: Stops the state machine timer, sets the state to
  372. * UP (unless it wasn't up yet), and then frees the interrupt,
  373. * if it is in use. This function must be called BEFORE
  374. * phy_detach.
  375. */
  376. void phy_stop_machine(struct phy_device *phydev)
  377. {
  378. del_timer_sync(&phydev->phy_timer);
  379. spin_lock(&phydev->lock);
  380. if (phydev->state > PHY_UP)
  381. phydev->state = PHY_UP;
  382. spin_unlock(&phydev->lock);
  383. if (phydev->irq != PHY_POLL)
  384. phy_stop_interrupts(phydev);
  385. phydev->adjust_state = NULL;
  386. }
  387. #ifdef CONFIG_PHYCONTROL
  388. /* phy_error:
  389. *
  390. * Moves the PHY to the HALTED state in response to a read
  391. * or write error, and tells the controller the link is down.
  392. * Must not be called from interrupt context, or while the
  393. * phydev->lock is held.
  394. */
  395. void phy_error(struct phy_device *phydev)
  396. {
  397. spin_lock(&phydev->lock);
  398. phydev->state = PHY_HALTED;
  399. spin_unlock(&phydev->lock);
  400. }
  401. /* phy_interrupt
  402. *
  403. * description: When a PHY interrupt occurs, the handler disables
  404. * interrupts, and schedules a work task to clear the interrupt.
  405. */
  406. static irqreturn_t phy_interrupt(int irq, void *phy_dat, struct pt_regs *regs)
  407. {
  408. struct phy_device *phydev = phy_dat;
  409. /* The MDIO bus is not allowed to be written in interrupt
  410. * context, so we need to disable the irq here. A work
  411. * queue will write the PHY to disable and clear the
  412. * interrupt, and then reenable the irq line. */
  413. disable_irq_nosync(irq);
  414. schedule_work(&phydev->phy_queue);
  415. return IRQ_HANDLED;
  416. }
  417. /* Enable the interrupts from the PHY side */
  418. int phy_enable_interrupts(struct phy_device *phydev)
  419. {
  420. int err;
  421. err = phy_clear_interrupt(phydev);
  422. if (err < 0)
  423. return err;
  424. err = phy_config_interrupt(phydev, PHY_INTERRUPT_ENABLED);
  425. return err;
  426. }
  427. /* Disable the PHY interrupts from the PHY side */
  428. int phy_disable_interrupts(struct phy_device *phydev)
  429. {
  430. int err;
  431. /* Disable PHY interrupts */
  432. err = phy_config_interrupt(phydev, PHY_INTERRUPT_DISABLED);
  433. if (err)
  434. goto phy_err;
  435. /* Clear the interrupt */
  436. err = phy_clear_interrupt(phydev);
  437. if (err)
  438. goto phy_err;
  439. return 0;
  440. phy_err:
  441. phy_error(phydev);
  442. return err;
  443. }
  444. /* phy_start_interrupts
  445. *
  446. * description: Request the interrupt for the given PHY. If
  447. * this fails, then we set irq to PHY_POLL.
  448. * Otherwise, we enable the interrupts in the PHY.
  449. * Returns 0 on success.
  450. * This should only be called with a valid IRQ number.
  451. */
  452. int phy_start_interrupts(struct phy_device *phydev)
  453. {
  454. int err = 0;
  455. INIT_WORK(&phydev->phy_queue, phy_change, phydev);
  456. if (request_irq(phydev->irq, phy_interrupt,
  457. SA_SHIRQ,
  458. "phy_interrupt",
  459. phydev) < 0) {
  460. printk(KERN_WARNING "%s: Can't get IRQ %d (PHY)\n",
  461. phydev->bus->name,
  462. phydev->irq);
  463. phydev->irq = PHY_POLL;
  464. return 0;
  465. }
  466. err = phy_enable_interrupts(phydev);
  467. return err;
  468. }
  469. EXPORT_SYMBOL(phy_start_interrupts);
  470. int phy_stop_interrupts(struct phy_device *phydev)
  471. {
  472. int err;
  473. err = phy_disable_interrupts(phydev);
  474. if (err)
  475. phy_error(phydev);
  476. free_irq(phydev->irq, phydev);
  477. return err;
  478. }
  479. EXPORT_SYMBOL(phy_stop_interrupts);
  480. /* Scheduled by the phy_interrupt/timer to handle PHY changes */
  481. static void phy_change(void *data)
  482. {
  483. int err;
  484. struct phy_device *phydev = data;
  485. err = phy_disable_interrupts(phydev);
  486. if (err)
  487. goto phy_err;
  488. spin_lock(&phydev->lock);
  489. if ((PHY_RUNNING == phydev->state) || (PHY_NOLINK == phydev->state))
  490. phydev->state = PHY_CHANGELINK;
  491. spin_unlock(&phydev->lock);
  492. enable_irq(phydev->irq);
  493. /* Reenable interrupts */
  494. err = phy_config_interrupt(phydev, PHY_INTERRUPT_ENABLED);
  495. if (err)
  496. goto irq_enable_err;
  497. return;
  498. irq_enable_err:
  499. disable_irq(phydev->irq);
  500. phy_err:
  501. phy_error(phydev);
  502. }
  503. /* Bring down the PHY link, and stop checking the status. */
  504. void phy_stop(struct phy_device *phydev)
  505. {
  506. spin_lock(&phydev->lock);
  507. if (PHY_HALTED == phydev->state)
  508. goto out_unlock;
  509. if (phydev->irq != PHY_POLL) {
  510. /* Clear any pending interrupts */
  511. phy_clear_interrupt(phydev);
  512. /* Disable PHY Interrupts */
  513. phy_config_interrupt(phydev, PHY_INTERRUPT_DISABLED);
  514. }
  515. phydev->state = PHY_HALTED;
  516. out_unlock:
  517. spin_unlock(&phydev->lock);
  518. }
  519. /* phy_start
  520. *
  521. * description: Indicates the attached device's readiness to
  522. * handle PHY-related work. Used during startup to start the
  523. * PHY, and after a call to phy_stop() to resume operation.
  524. * Also used to indicate the MDIO bus has cleared an error
  525. * condition.
  526. */
  527. void phy_start(struct phy_device *phydev)
  528. {
  529. spin_lock(&phydev->lock);
  530. switch (phydev->state) {
  531. case PHY_STARTING:
  532. phydev->state = PHY_PENDING;
  533. break;
  534. case PHY_READY:
  535. phydev->state = PHY_UP;
  536. break;
  537. case PHY_HALTED:
  538. phydev->state = PHY_RESUMING;
  539. default:
  540. break;
  541. }
  542. spin_unlock(&phydev->lock);
  543. }
  544. EXPORT_SYMBOL(phy_stop);
  545. EXPORT_SYMBOL(phy_start);
  546. /* PHY timer which handles the state machine */
  547. static void phy_timer(unsigned long data)
  548. {
  549. struct phy_device *phydev = (struct phy_device *)data;
  550. int needs_aneg = 0;
  551. int err = 0;
  552. spin_lock(&phydev->lock);
  553. if (phydev->adjust_state)
  554. phydev->adjust_state(phydev->attached_dev);
  555. switch(phydev->state) {
  556. case PHY_DOWN:
  557. case PHY_STARTING:
  558. case PHY_READY:
  559. case PHY_PENDING:
  560. break;
  561. case PHY_UP:
  562. needs_aneg = 1;
  563. phydev->link_timeout = PHY_AN_TIMEOUT;
  564. break;
  565. case PHY_AN:
  566. /* Check if negotiation is done. Break
  567. * if there's an error */
  568. err = phy_aneg_done(phydev);
  569. if (err < 0)
  570. break;
  571. /* If auto-negotiation is done, we change to
  572. * either RUNNING, or NOLINK */
  573. if (err > 0) {
  574. err = phy_read_status(phydev);
  575. if (err)
  576. break;
  577. if (phydev->link) {
  578. phydev->state = PHY_RUNNING;
  579. netif_carrier_on(phydev->attached_dev);
  580. } else {
  581. phydev->state = PHY_NOLINK;
  582. netif_carrier_off(phydev->attached_dev);
  583. }
  584. phydev->adjust_link(phydev->attached_dev);
  585. } else if (0 == phydev->link_timeout--) {
  586. /* The counter expired, so either we
  587. * switch to forced mode, or the
  588. * magic_aneg bit exists, and we try aneg
  589. * again */
  590. if (!(phydev->drv->flags & PHY_HAS_MAGICANEG)) {
  591. int idx;
  592. /* We'll start from the
  593. * fastest speed, and work
  594. * our way down */
  595. idx = phy_find_valid(0,
  596. phydev->supported);
  597. phydev->speed = settings[idx].speed;
  598. phydev->duplex = settings[idx].duplex;
  599. phydev->autoneg = AUTONEG_DISABLE;
  600. phydev->state = PHY_FORCING;
  601. phydev->link_timeout =
  602. PHY_FORCE_TIMEOUT;
  603. pr_info("Trying %d/%s\n",
  604. phydev->speed,
  605. DUPLEX_FULL ==
  606. phydev->duplex ?
  607. "FULL" : "HALF");
  608. }
  609. needs_aneg = 1;
  610. }
  611. break;
  612. case PHY_NOLINK:
  613. err = phy_read_status(phydev);
  614. if (err)
  615. break;
  616. if (phydev->link) {
  617. phydev->state = PHY_RUNNING;
  618. netif_carrier_on(phydev->attached_dev);
  619. phydev->adjust_link(phydev->attached_dev);
  620. }
  621. break;
  622. case PHY_FORCING:
  623. err = phy_read_status(phydev);
  624. if (err)
  625. break;
  626. if (phydev->link) {
  627. phydev->state = PHY_RUNNING;
  628. netif_carrier_on(phydev->attached_dev);
  629. } else {
  630. if (0 == phydev->link_timeout--) {
  631. phy_force_reduction(phydev);
  632. needs_aneg = 1;
  633. }
  634. }
  635. phydev->adjust_link(phydev->attached_dev);
  636. break;
  637. case PHY_RUNNING:
  638. /* Only register a CHANGE if we are
  639. * polling */
  640. if (PHY_POLL == phydev->irq)
  641. phydev->state = PHY_CHANGELINK;
  642. break;
  643. case PHY_CHANGELINK:
  644. err = phy_read_status(phydev);
  645. if (err)
  646. break;
  647. if (phydev->link) {
  648. phydev->state = PHY_RUNNING;
  649. netif_carrier_on(phydev->attached_dev);
  650. } else {
  651. phydev->state = PHY_NOLINK;
  652. netif_carrier_off(phydev->attached_dev);
  653. }
  654. phydev->adjust_link(phydev->attached_dev);
  655. if (PHY_POLL != phydev->irq)
  656. err = phy_config_interrupt(phydev,
  657. PHY_INTERRUPT_ENABLED);
  658. break;
  659. case PHY_HALTED:
  660. if (phydev->link) {
  661. phydev->link = 0;
  662. netif_carrier_off(phydev->attached_dev);
  663. phydev->adjust_link(phydev->attached_dev);
  664. }
  665. break;
  666. case PHY_RESUMING:
  667. err = phy_clear_interrupt(phydev);
  668. if (err)
  669. break;
  670. err = phy_config_interrupt(phydev,
  671. PHY_INTERRUPT_ENABLED);
  672. if (err)
  673. break;
  674. if (AUTONEG_ENABLE == phydev->autoneg) {
  675. err = phy_aneg_done(phydev);
  676. if (err < 0)
  677. break;
  678. /* err > 0 if AN is done.
  679. * Otherwise, it's 0, and we're
  680. * still waiting for AN */
  681. if (err > 0) {
  682. phydev->state = PHY_RUNNING;
  683. } else {
  684. phydev->state = PHY_AN;
  685. phydev->link_timeout = PHY_AN_TIMEOUT;
  686. }
  687. } else
  688. phydev->state = PHY_RUNNING;
  689. break;
  690. }
  691. spin_unlock(&phydev->lock);
  692. if (needs_aneg)
  693. err = phy_start_aneg(phydev);
  694. if (err < 0)
  695. phy_error(phydev);
  696. mod_timer(&phydev->phy_timer, jiffies + PHY_STATE_TIME * HZ);
  697. }
  698. #endif /* CONFIG_PHYCONTROL */