phy.c 19 KB

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