phy.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954
  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. * Copyright (c) 2006 Maciej W. Rozycki
  11. *
  12. * This program is free software; you can redistribute it and/or modify it
  13. * under the terms of the GNU General Public License as published by the
  14. * Free Software Foundation; either version 2 of the License, or (at your
  15. * option) any later version.
  16. *
  17. */
  18. #include <linux/kernel.h>
  19. #include <linux/string.h>
  20. #include <linux/errno.h>
  21. #include <linux/unistd.h>
  22. #include <linux/slab.h>
  23. #include <linux/interrupt.h>
  24. #include <linux/init.h>
  25. #include <linux/delay.h>
  26. #include <linux/netdevice.h>
  27. #include <linux/etherdevice.h>
  28. #include <linux/skbuff.h>
  29. #include <linux/spinlock.h>
  30. #include <linux/mm.h>
  31. #include <linux/module.h>
  32. #include <linux/mii.h>
  33. #include <linux/ethtool.h>
  34. #include <linux/phy.h>
  35. #include <linux/timer.h>
  36. #include <linux/workqueue.h>
  37. #include <asm/io.h>
  38. #include <asm/irq.h>
  39. #include <asm/uaccess.h>
  40. /**
  41. * phy_print_status - Convenience function to print out the current phy status
  42. * @phydev: the phy_device struct
  43. */
  44. void phy_print_status(struct phy_device *phydev)
  45. {
  46. pr_info("PHY: %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. /**
  56. * phy_read - Convenience function for reading a given PHY register
  57. * @phydev: the phy_device struct
  58. * @regnum: register number to read
  59. *
  60. * NOTE: MUST NOT be called from interrupt context,
  61. * because the bus read/write functions may wait for an interrupt
  62. * to conclude the operation.
  63. */
  64. int phy_read(struct phy_device *phydev, u16 regnum)
  65. {
  66. int retval;
  67. struct mii_bus *bus = phydev->bus;
  68. spin_lock_bh(&bus->mdio_lock);
  69. retval = bus->read(bus, phydev->addr, regnum);
  70. spin_unlock_bh(&bus->mdio_lock);
  71. return retval;
  72. }
  73. EXPORT_SYMBOL(phy_read);
  74. /**
  75. * phy_write - Convenience function for writing a given PHY register
  76. * @phydev: the phy_device struct
  77. * @regnum: register number to write
  78. * @val: value to write to @regnum
  79. *
  80. * NOTE: MUST NOT be called from interrupt context,
  81. * because the bus read/write functions may wait for an interrupt
  82. * to conclude the operation.
  83. */
  84. int phy_write(struct phy_device *phydev, u16 regnum, u16 val)
  85. {
  86. int err;
  87. struct mii_bus *bus = phydev->bus;
  88. spin_lock_bh(&bus->mdio_lock);
  89. err = bus->write(bus, phydev->addr, regnum, val);
  90. spin_unlock_bh(&bus->mdio_lock);
  91. return err;
  92. }
  93. EXPORT_SYMBOL(phy_write);
  94. /**
  95. * phy_clear_interrupt - Ack the phy device's interrupt
  96. * @phydev: the phy_device struct
  97. *
  98. * If the @phydev driver has an ack_interrupt function, call it to
  99. * ack and clear the phy device's interrupt.
  100. *
  101. * Returns 0 on success on < 0 on error.
  102. */
  103. int phy_clear_interrupt(struct phy_device *phydev)
  104. {
  105. int err = 0;
  106. if (phydev->drv->ack_interrupt)
  107. err = phydev->drv->ack_interrupt(phydev);
  108. return err;
  109. }
  110. /**
  111. * phy_config_interrupt - configure the PHY device for the requested interrupts
  112. * @phydev: the phy_device struct
  113. * @interrupts: interrupt flags to configure for this @phydev
  114. *
  115. * Returns 0 on success on < 0 on error.
  116. */
  117. int phy_config_interrupt(struct phy_device *phydev, u32 interrupts)
  118. {
  119. int err = 0;
  120. phydev->interrupts = interrupts;
  121. if (phydev->drv->config_intr)
  122. err = phydev->drv->config_intr(phydev);
  123. return err;
  124. }
  125. /**
  126. * phy_aneg_done - return auto-negotiation status
  127. * @phydev: target phy_device struct
  128. *
  129. * Description: Reads the status register and returns 0 either if
  130. * auto-negotiation is incomplete, or if there was an error.
  131. * Returns BMSR_ANEGCOMPLETE if auto-negotiation is done.
  132. */
  133. static inline int phy_aneg_done(struct phy_device *phydev)
  134. {
  135. int retval;
  136. retval = phy_read(phydev, MII_BMSR);
  137. return (retval < 0) ? retval : (retval & BMSR_ANEGCOMPLETE);
  138. }
  139. /* A structure for mapping a particular speed and duplex
  140. * combination to a particular SUPPORTED and ADVERTISED value */
  141. struct phy_setting {
  142. int speed;
  143. int duplex;
  144. u32 setting;
  145. };
  146. /* A mapping of all SUPPORTED settings to speed/duplex */
  147. static const struct phy_setting settings[] = {
  148. {
  149. .speed = 10000,
  150. .duplex = DUPLEX_FULL,
  151. .setting = SUPPORTED_10000baseT_Full,
  152. },
  153. {
  154. .speed = SPEED_1000,
  155. .duplex = DUPLEX_FULL,
  156. .setting = SUPPORTED_1000baseT_Full,
  157. },
  158. {
  159. .speed = SPEED_1000,
  160. .duplex = DUPLEX_HALF,
  161. .setting = SUPPORTED_1000baseT_Half,
  162. },
  163. {
  164. .speed = SPEED_100,
  165. .duplex = DUPLEX_FULL,
  166. .setting = SUPPORTED_100baseT_Full,
  167. },
  168. {
  169. .speed = SPEED_100,
  170. .duplex = DUPLEX_HALF,
  171. .setting = SUPPORTED_100baseT_Half,
  172. },
  173. {
  174. .speed = SPEED_10,
  175. .duplex = DUPLEX_FULL,
  176. .setting = SUPPORTED_10baseT_Full,
  177. },
  178. {
  179. .speed = SPEED_10,
  180. .duplex = DUPLEX_HALF,
  181. .setting = SUPPORTED_10baseT_Half,
  182. },
  183. };
  184. #define MAX_NUM_SETTINGS (sizeof(settings)/sizeof(struct phy_setting))
  185. /**
  186. * phy_find_setting - find a PHY settings array entry that matches speed & duplex
  187. * @speed: speed to match
  188. * @duplex: duplex to match
  189. *
  190. * Description: Searches the settings array for the setting which
  191. * matches the desired speed and duplex, and returns the index
  192. * of that setting. Returns the index of the last setting if
  193. * none of the others match.
  194. */
  195. static inline int phy_find_setting(int speed, int duplex)
  196. {
  197. int idx = 0;
  198. while (idx < ARRAY_SIZE(settings) &&
  199. (settings[idx].speed != speed ||
  200. settings[idx].duplex != duplex))
  201. idx++;
  202. return idx < MAX_NUM_SETTINGS ? idx : MAX_NUM_SETTINGS - 1;
  203. }
  204. /**
  205. * phy_find_valid - find a PHY setting that matches the requested features mask
  206. * @idx: The first index in settings[] to search
  207. * @features: A mask of the valid settings
  208. *
  209. * Description: Returns the index of the first valid setting less
  210. * than or equal to the one pointed to by idx, as determined by
  211. * the mask in features. Returns the index of the last setting
  212. * if nothing else matches.
  213. */
  214. static inline int phy_find_valid(int idx, u32 features)
  215. {
  216. while (idx < MAX_NUM_SETTINGS && !(settings[idx].setting & features))
  217. idx++;
  218. return idx < MAX_NUM_SETTINGS ? idx : MAX_NUM_SETTINGS - 1;
  219. }
  220. /**
  221. * phy_sanitize_settings - make sure the PHY is set to supported speed and duplex
  222. * @phydev: the target phy_device struct
  223. *
  224. * Description: Make sure the PHY is set to supported speeds and
  225. * duplexes. Drop down by one in this order: 1000/FULL,
  226. * 1000/HALF, 100/FULL, 100/HALF, 10/FULL, 10/HALF.
  227. */
  228. void phy_sanitize_settings(struct phy_device *phydev)
  229. {
  230. u32 features = phydev->supported;
  231. int idx;
  232. /* Sanitize settings based on PHY capabilities */
  233. if ((features & SUPPORTED_Autoneg) == 0)
  234. phydev->autoneg = 0;
  235. idx = phy_find_valid(phy_find_setting(phydev->speed, phydev->duplex),
  236. features);
  237. phydev->speed = settings[idx].speed;
  238. phydev->duplex = settings[idx].duplex;
  239. }
  240. EXPORT_SYMBOL(phy_sanitize_settings);
  241. /**
  242. * phy_ethtool_sset - generic ethtool sset function, handles all the details
  243. * @phydev: target phy_device struct
  244. * @cmd: ethtool_cmd
  245. *
  246. * A few notes about parameter checking:
  247. * - We don't set port or transceiver, so we don't care what they
  248. * were set to.
  249. * - phy_start_aneg() will make sure forced settings are sane, and
  250. * choose the next best ones from the ones selected, so we don't
  251. * care if ethtool tries to give us bad values.
  252. */
  253. int phy_ethtool_sset(struct phy_device *phydev, struct ethtool_cmd *cmd)
  254. {
  255. if (cmd->phy_address != phydev->addr)
  256. return -EINVAL;
  257. /* We make sure that we don't pass unsupported
  258. * values in to the PHY */
  259. cmd->advertising &= phydev->supported;
  260. /* Verify the settings we care about. */
  261. if (cmd->autoneg != AUTONEG_ENABLE && cmd->autoneg != AUTONEG_DISABLE)
  262. return -EINVAL;
  263. if (cmd->autoneg == AUTONEG_ENABLE && cmd->advertising == 0)
  264. return -EINVAL;
  265. if (cmd->autoneg == AUTONEG_DISABLE
  266. && ((cmd->speed != SPEED_1000
  267. && cmd->speed != SPEED_100
  268. && cmd->speed != SPEED_10)
  269. || (cmd->duplex != DUPLEX_HALF
  270. && cmd->duplex != DUPLEX_FULL)))
  271. return -EINVAL;
  272. phydev->autoneg = cmd->autoneg;
  273. phydev->speed = cmd->speed;
  274. phydev->advertising = cmd->advertising;
  275. if (AUTONEG_ENABLE == cmd->autoneg)
  276. phydev->advertising |= ADVERTISED_Autoneg;
  277. else
  278. phydev->advertising &= ~ADVERTISED_Autoneg;
  279. phydev->duplex = cmd->duplex;
  280. /* Restart the PHY */
  281. phy_start_aneg(phydev);
  282. return 0;
  283. }
  284. EXPORT_SYMBOL(phy_ethtool_sset);
  285. int phy_ethtool_gset(struct phy_device *phydev, struct ethtool_cmd *cmd)
  286. {
  287. cmd->supported = phydev->supported;
  288. cmd->advertising = phydev->advertising;
  289. cmd->speed = phydev->speed;
  290. cmd->duplex = phydev->duplex;
  291. cmd->port = PORT_MII;
  292. cmd->phy_address = phydev->addr;
  293. cmd->transceiver = XCVR_EXTERNAL;
  294. cmd->autoneg = phydev->autoneg;
  295. return 0;
  296. }
  297. EXPORT_SYMBOL(phy_ethtool_gset);
  298. /**
  299. * phy_mii_ioctl - generic PHY MII ioctl interface
  300. * @phydev: the phy_device struct
  301. * @mii_data: MII ioctl data
  302. * @cmd: ioctl cmd to execute
  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. /**
  352. * phy_start_aneg - start auto-negotiation for this PHY device
  353. * @phydev: the phy_device struct
  354. *
  355. * Description: Sanitizes the settings (if we're not autonegotiating
  356. * them), and then calls the driver's config_aneg function.
  357. * If the PHYCONTROL Layer is operating, we change the state to
  358. * reflect the beginning of Auto-negotiation or forcing.
  359. */
  360. int phy_start_aneg(struct phy_device *phydev)
  361. {
  362. int err;
  363. spin_lock(&phydev->lock);
  364. if (AUTONEG_DISABLE == phydev->autoneg)
  365. phy_sanitize_settings(phydev);
  366. err = phydev->drv->config_aneg(phydev);
  367. if (err < 0)
  368. goto out_unlock;
  369. if (phydev->state != PHY_HALTED) {
  370. if (AUTONEG_ENABLE == phydev->autoneg) {
  371. phydev->state = PHY_AN;
  372. phydev->link_timeout = PHY_AN_TIMEOUT;
  373. } else {
  374. phydev->state = PHY_FORCING;
  375. phydev->link_timeout = PHY_FORCE_TIMEOUT;
  376. }
  377. }
  378. out_unlock:
  379. spin_unlock(&phydev->lock);
  380. return err;
  381. }
  382. EXPORT_SYMBOL(phy_start_aneg);
  383. static void phy_change(struct work_struct *work);
  384. static void phy_timer(unsigned long data);
  385. /**
  386. * phy_start_machine - start PHY state machine tracking
  387. * @phydev: the phy_device struct
  388. * @handler: callback function for state change notifications
  389. *
  390. * Description: The PHY infrastructure can run a state machine
  391. * which tracks whether the PHY is starting up, negotiating,
  392. * etc. This function starts the timer which tracks the state
  393. * of the PHY. If you want to be notified when the state changes,
  394. * pass in the callback @handler, otherwise, pass NULL. If you
  395. * want to maintain your own state machine, do not call this
  396. * function.
  397. */
  398. void phy_start_machine(struct phy_device *phydev,
  399. void (*handler)(struct net_device *))
  400. {
  401. phydev->adjust_state = handler;
  402. init_timer(&phydev->phy_timer);
  403. phydev->phy_timer.function = &phy_timer;
  404. phydev->phy_timer.data = (unsigned long) phydev;
  405. mod_timer(&phydev->phy_timer, jiffies + HZ);
  406. }
  407. /**
  408. * phy_stop_machine - stop the PHY state machine tracking
  409. * @phydev: target phy_device struct
  410. *
  411. * Description: Stops the state machine timer, sets the state to UP
  412. * (unless it wasn't up yet). This function must be called BEFORE
  413. * phy_detach.
  414. */
  415. void phy_stop_machine(struct phy_device *phydev)
  416. {
  417. del_timer_sync(&phydev->phy_timer);
  418. spin_lock(&phydev->lock);
  419. if (phydev->state > PHY_UP)
  420. phydev->state = PHY_UP;
  421. spin_unlock(&phydev->lock);
  422. phydev->adjust_state = NULL;
  423. }
  424. /**
  425. * phy_force_reduction - reduce PHY speed/duplex settings by one step
  426. * @phydev: target phy_device struct
  427. *
  428. * Description: Reduces the speed/duplex settings by one notch,
  429. * in this order--
  430. * 1000/FULL, 1000/HALF, 100/FULL, 100/HALF, 10/FULL, 10/HALF.
  431. * The function bottoms out at 10/HALF.
  432. */
  433. static void phy_force_reduction(struct phy_device *phydev)
  434. {
  435. int idx;
  436. idx = phy_find_setting(phydev->speed, phydev->duplex);
  437. idx++;
  438. idx = phy_find_valid(idx, phydev->supported);
  439. phydev->speed = settings[idx].speed;
  440. phydev->duplex = settings[idx].duplex;
  441. pr_info("Trying %d/%s\n", phydev->speed,
  442. DUPLEX_FULL == phydev->duplex ?
  443. "FULL" : "HALF");
  444. }
  445. /**
  446. * phy_error - enter HALTED state for this PHY device
  447. * @phydev: target phy_device struct
  448. *
  449. * Moves the PHY to the HALTED state in response to a read
  450. * or write error, and tells the controller the link is down.
  451. * Must not be called from interrupt context, or while the
  452. * phydev->lock is held.
  453. */
  454. void phy_error(struct phy_device *phydev)
  455. {
  456. spin_lock(&phydev->lock);
  457. phydev->state = PHY_HALTED;
  458. spin_unlock(&phydev->lock);
  459. }
  460. /**
  461. * phy_interrupt - PHY interrupt handler
  462. * @irq: interrupt line
  463. * @phy_dat: phy_device pointer
  464. *
  465. * Description: When a PHY interrupt occurs, the handler disables
  466. * interrupts, and schedules a work task to clear the interrupt.
  467. */
  468. static irqreturn_t phy_interrupt(int irq, void *phy_dat)
  469. {
  470. struct phy_device *phydev = phy_dat;
  471. if (PHY_HALTED == phydev->state)
  472. return IRQ_NONE; /* It can't be ours. */
  473. /* The MDIO bus is not allowed to be written in interrupt
  474. * context, so we need to disable the irq here. A work
  475. * queue will write the PHY to disable and clear the
  476. * interrupt, and then reenable the irq line. */
  477. disable_irq_nosync(irq);
  478. schedule_work(&phydev->phy_queue);
  479. return IRQ_HANDLED;
  480. }
  481. /**
  482. * phy_enable_interrupts - Enable the interrupts from the PHY side
  483. * @phydev: target phy_device struct
  484. */
  485. int phy_enable_interrupts(struct phy_device *phydev)
  486. {
  487. int err;
  488. err = phy_clear_interrupt(phydev);
  489. if (err < 0)
  490. return err;
  491. err = phy_config_interrupt(phydev, PHY_INTERRUPT_ENABLED);
  492. return err;
  493. }
  494. EXPORT_SYMBOL(phy_enable_interrupts);
  495. /**
  496. * phy_disable_interrupts - Disable the PHY interrupts from the PHY side
  497. * @phydev: target phy_device struct
  498. */
  499. int phy_disable_interrupts(struct phy_device *phydev)
  500. {
  501. int err;
  502. /* Disable PHY interrupts */
  503. err = phy_config_interrupt(phydev, PHY_INTERRUPT_DISABLED);
  504. if (err)
  505. goto phy_err;
  506. /* Clear the interrupt */
  507. err = phy_clear_interrupt(phydev);
  508. if (err)
  509. goto phy_err;
  510. return 0;
  511. phy_err:
  512. phy_error(phydev);
  513. return err;
  514. }
  515. EXPORT_SYMBOL(phy_disable_interrupts);
  516. /**
  517. * phy_start_interrupts - request and enable interrupts for a PHY device
  518. * @phydev: target phy_device struct
  519. *
  520. * Description: Request the interrupt for the given PHY.
  521. * If this fails, then we set irq to PHY_POLL.
  522. * Otherwise, we enable the interrupts in the PHY.
  523. * This should only be called with a valid IRQ number.
  524. * Returns 0 on success or < 0 on error.
  525. */
  526. int phy_start_interrupts(struct phy_device *phydev)
  527. {
  528. int err = 0;
  529. INIT_WORK(&phydev->phy_queue, phy_change);
  530. if (request_irq(phydev->irq, phy_interrupt,
  531. IRQF_SHARED,
  532. "phy_interrupt",
  533. phydev) < 0) {
  534. printk(KERN_WARNING "%s: Can't get IRQ %d (PHY)\n",
  535. phydev->bus->name,
  536. phydev->irq);
  537. phydev->irq = PHY_POLL;
  538. return 0;
  539. }
  540. err = phy_enable_interrupts(phydev);
  541. return err;
  542. }
  543. EXPORT_SYMBOL(phy_start_interrupts);
  544. /**
  545. * phy_stop_interrupts - disable interrupts from a PHY device
  546. * @phydev: target phy_device struct
  547. */
  548. int phy_stop_interrupts(struct phy_device *phydev)
  549. {
  550. int err;
  551. err = phy_disable_interrupts(phydev);
  552. if (err)
  553. phy_error(phydev);
  554. /*
  555. * Finish any pending work; we might have been scheduled
  556. * to be called from keventd ourselves, though.
  557. */
  558. run_scheduled_work(&phydev->phy_queue);
  559. free_irq(phydev->irq, phydev);
  560. return err;
  561. }
  562. EXPORT_SYMBOL(phy_stop_interrupts);
  563. /**
  564. * phy_change - Scheduled by the phy_interrupt/timer to handle PHY changes
  565. * @work: work_struct that describes the work to be done
  566. */
  567. static void phy_change(struct work_struct *work)
  568. {
  569. int err;
  570. struct phy_device *phydev =
  571. container_of(work, struct phy_device, phy_queue);
  572. err = phy_disable_interrupts(phydev);
  573. if (err)
  574. goto phy_err;
  575. spin_lock(&phydev->lock);
  576. if ((PHY_RUNNING == phydev->state) || (PHY_NOLINK == phydev->state))
  577. phydev->state = PHY_CHANGELINK;
  578. spin_unlock(&phydev->lock);
  579. enable_irq(phydev->irq);
  580. /* Reenable interrupts */
  581. if (PHY_HALTED != phydev->state)
  582. err = phy_config_interrupt(phydev, PHY_INTERRUPT_ENABLED);
  583. if (err)
  584. goto irq_enable_err;
  585. return;
  586. irq_enable_err:
  587. disable_irq(phydev->irq);
  588. phy_err:
  589. phy_error(phydev);
  590. }
  591. /**
  592. * phy_stop - Bring down the PHY link, and stop checking the status
  593. * @phydev: target phy_device struct
  594. */
  595. void phy_stop(struct phy_device *phydev)
  596. {
  597. spin_lock(&phydev->lock);
  598. if (PHY_HALTED == phydev->state)
  599. goto out_unlock;
  600. phydev->state = PHY_HALTED;
  601. if (phydev->irq != PHY_POLL) {
  602. /* Disable PHY Interrupts */
  603. phy_config_interrupt(phydev, PHY_INTERRUPT_DISABLED);
  604. /* Clear any pending interrupts */
  605. phy_clear_interrupt(phydev);
  606. }
  607. out_unlock:
  608. spin_unlock(&phydev->lock);
  609. /*
  610. * Cannot call flush_scheduled_work() here as desired because
  611. * of rtnl_lock(), but PHY_HALTED shall guarantee phy_change()
  612. * will not reenable interrupts.
  613. */
  614. }
  615. /**
  616. * phy_start - start or restart a PHY device
  617. * @phydev: target phy_device struct
  618. *
  619. * Description: Indicates the attached device's readiness to
  620. * handle PHY-related work. Used during startup to start the
  621. * PHY, and after a call to phy_stop() to resume operation.
  622. * Also used to indicate the MDIO bus has cleared an error
  623. * condition.
  624. */
  625. void phy_start(struct phy_device *phydev)
  626. {
  627. spin_lock(&phydev->lock);
  628. switch (phydev->state) {
  629. case PHY_STARTING:
  630. phydev->state = PHY_PENDING;
  631. break;
  632. case PHY_READY:
  633. phydev->state = PHY_UP;
  634. break;
  635. case PHY_HALTED:
  636. phydev->state = PHY_RESUMING;
  637. default:
  638. break;
  639. }
  640. spin_unlock(&phydev->lock);
  641. }
  642. EXPORT_SYMBOL(phy_stop);
  643. EXPORT_SYMBOL(phy_start);
  644. /* PHY timer which handles the state machine */
  645. static void phy_timer(unsigned long data)
  646. {
  647. struct phy_device *phydev = (struct phy_device *)data;
  648. int needs_aneg = 0;
  649. int err = 0;
  650. spin_lock(&phydev->lock);
  651. if (phydev->adjust_state)
  652. phydev->adjust_state(phydev->attached_dev);
  653. switch(phydev->state) {
  654. case PHY_DOWN:
  655. case PHY_STARTING:
  656. case PHY_READY:
  657. case PHY_PENDING:
  658. break;
  659. case PHY_UP:
  660. needs_aneg = 1;
  661. phydev->link_timeout = PHY_AN_TIMEOUT;
  662. break;
  663. case PHY_AN:
  664. err = phy_read_status(phydev);
  665. if (err < 0)
  666. break;
  667. /* If the link is down, give up on
  668. * negotiation for now */
  669. if (!phydev->link) {
  670. phydev->state = PHY_NOLINK;
  671. netif_carrier_off(phydev->attached_dev);
  672. phydev->adjust_link(phydev->attached_dev);
  673. break;
  674. }
  675. /* Check if negotiation is done. Break
  676. * if there's an error */
  677. err = phy_aneg_done(phydev);
  678. if (err < 0)
  679. break;
  680. /* If AN is done, we're running */
  681. if (err > 0) {
  682. phydev->state = PHY_RUNNING;
  683. netif_carrier_on(phydev->attached_dev);
  684. phydev->adjust_link(phydev->attached_dev);
  685. } else if (0 == phydev->link_timeout--) {
  686. int idx;
  687. needs_aneg = 1;
  688. /* If we have the magic_aneg bit,
  689. * we try again */
  690. if (phydev->drv->flags & PHY_HAS_MAGICANEG)
  691. break;
  692. /* The timer expired, and we still
  693. * don't have a setting, so we try
  694. * forcing it until we find one that
  695. * works, starting from the fastest speed,
  696. * and working our way down */
  697. idx = phy_find_valid(0, phydev->supported);
  698. phydev->speed = settings[idx].speed;
  699. phydev->duplex = settings[idx].duplex;
  700. phydev->autoneg = AUTONEG_DISABLE;
  701. pr_info("Trying %d/%s\n", phydev->speed,
  702. DUPLEX_FULL ==
  703. phydev->duplex ?
  704. "FULL" : "HALF");
  705. }
  706. break;
  707. case PHY_NOLINK:
  708. err = phy_read_status(phydev);
  709. if (err)
  710. break;
  711. if (phydev->link) {
  712. phydev->state = PHY_RUNNING;
  713. netif_carrier_on(phydev->attached_dev);
  714. phydev->adjust_link(phydev->attached_dev);
  715. }
  716. break;
  717. case PHY_FORCING:
  718. err = genphy_update_link(phydev);
  719. if (err)
  720. break;
  721. if (phydev->link) {
  722. phydev->state = PHY_RUNNING;
  723. netif_carrier_on(phydev->attached_dev);
  724. } else {
  725. if (0 == phydev->link_timeout--) {
  726. phy_force_reduction(phydev);
  727. needs_aneg = 1;
  728. }
  729. }
  730. phydev->adjust_link(phydev->attached_dev);
  731. break;
  732. case PHY_RUNNING:
  733. /* Only register a CHANGE if we are
  734. * polling */
  735. if (PHY_POLL == phydev->irq)
  736. phydev->state = PHY_CHANGELINK;
  737. break;
  738. case PHY_CHANGELINK:
  739. err = phy_read_status(phydev);
  740. if (err)
  741. break;
  742. if (phydev->link) {
  743. phydev->state = PHY_RUNNING;
  744. netif_carrier_on(phydev->attached_dev);
  745. } else {
  746. phydev->state = PHY_NOLINK;
  747. netif_carrier_off(phydev->attached_dev);
  748. }
  749. phydev->adjust_link(phydev->attached_dev);
  750. if (PHY_POLL != phydev->irq)
  751. err = phy_config_interrupt(phydev,
  752. PHY_INTERRUPT_ENABLED);
  753. break;
  754. case PHY_HALTED:
  755. if (phydev->link) {
  756. phydev->link = 0;
  757. netif_carrier_off(phydev->attached_dev);
  758. phydev->adjust_link(phydev->attached_dev);
  759. }
  760. break;
  761. case PHY_RESUMING:
  762. err = phy_clear_interrupt(phydev);
  763. if (err)
  764. break;
  765. err = phy_config_interrupt(phydev,
  766. PHY_INTERRUPT_ENABLED);
  767. if (err)
  768. break;
  769. if (AUTONEG_ENABLE == phydev->autoneg) {
  770. err = phy_aneg_done(phydev);
  771. if (err < 0)
  772. break;
  773. /* err > 0 if AN is done.
  774. * Otherwise, it's 0, and we're
  775. * still waiting for AN */
  776. if (err > 0) {
  777. phydev->state = PHY_RUNNING;
  778. } else {
  779. phydev->state = PHY_AN;
  780. phydev->link_timeout = PHY_AN_TIMEOUT;
  781. }
  782. } else
  783. phydev->state = PHY_RUNNING;
  784. break;
  785. }
  786. spin_unlock(&phydev->lock);
  787. if (needs_aneg)
  788. err = phy_start_aneg(phydev);
  789. if (err < 0)
  790. phy_error(phydev);
  791. mod_timer(&phydev->phy_timer, jiffies + PHY_STATE_TIME * HZ);
  792. }