phy.c 20 KB

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