phy.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961
  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 ARRAY_SIZE(settings)
  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 = AUTONEG_DISABLE;
  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)) == 0)
  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. if ((!phydev->autoneg) &&
  334. (val & BMCR_SPEED1000))
  335. phydev->speed = SPEED_1000;
  336. else if ((!phydev->autoneg) &&
  337. (val & BMCR_SPEED100))
  338. phydev->speed = SPEED_100;
  339. break;
  340. case MII_ADVERTISE:
  341. phydev->advertising = val;
  342. break;
  343. default:
  344. /* do nothing */
  345. break;
  346. }
  347. }
  348. phy_write(phydev, mii_data->reg_num, val);
  349. if (mii_data->reg_num == MII_BMCR
  350. && val & BMCR_RESET
  351. && phydev->drv->config_init)
  352. phydev->drv->config_init(phydev);
  353. break;
  354. }
  355. return 0;
  356. }
  357. EXPORT_SYMBOL(phy_mii_ioctl);
  358. /**
  359. * phy_start_aneg - start auto-negotiation for this PHY device
  360. * @phydev: the phy_device struct
  361. *
  362. * Description: Sanitizes the settings (if we're not autonegotiating
  363. * them), and then calls the driver's config_aneg function.
  364. * If the PHYCONTROL Layer is operating, we change the state to
  365. * reflect the beginning of Auto-negotiation or forcing.
  366. */
  367. int phy_start_aneg(struct phy_device *phydev)
  368. {
  369. int err;
  370. spin_lock_bh(&phydev->lock);
  371. if (AUTONEG_DISABLE == phydev->autoneg)
  372. phy_sanitize_settings(phydev);
  373. err = phydev->drv->config_aneg(phydev);
  374. if (err < 0)
  375. goto out_unlock;
  376. if (phydev->state != PHY_HALTED) {
  377. if (AUTONEG_ENABLE == phydev->autoneg) {
  378. phydev->state = PHY_AN;
  379. phydev->link_timeout = PHY_AN_TIMEOUT;
  380. } else {
  381. phydev->state = PHY_FORCING;
  382. phydev->link_timeout = PHY_FORCE_TIMEOUT;
  383. }
  384. }
  385. out_unlock:
  386. spin_unlock_bh(&phydev->lock);
  387. return err;
  388. }
  389. EXPORT_SYMBOL(phy_start_aneg);
  390. static void phy_change(struct work_struct *work);
  391. static void phy_timer(unsigned long data);
  392. /**
  393. * phy_start_machine - start PHY state machine tracking
  394. * @phydev: the phy_device struct
  395. * @handler: callback function for state change notifications
  396. *
  397. * Description: The PHY infrastructure can run a state machine
  398. * which tracks whether the PHY is starting up, negotiating,
  399. * etc. This function starts the timer which tracks the state
  400. * of the PHY. If you want to be notified when the state changes,
  401. * pass in the callback @handler, otherwise, pass NULL. If you
  402. * want to maintain your own state machine, do not call this
  403. * function.
  404. */
  405. void phy_start_machine(struct phy_device *phydev,
  406. void (*handler)(struct net_device *))
  407. {
  408. phydev->adjust_state = handler;
  409. init_timer(&phydev->phy_timer);
  410. phydev->phy_timer.function = &phy_timer;
  411. phydev->phy_timer.data = (unsigned long) phydev;
  412. mod_timer(&phydev->phy_timer, jiffies + HZ);
  413. }
  414. /**
  415. * phy_stop_machine - stop the PHY state machine tracking
  416. * @phydev: target phy_device struct
  417. *
  418. * Description: Stops the state machine timer, sets the state to UP
  419. * (unless it wasn't up yet). This function must be called BEFORE
  420. * phy_detach.
  421. */
  422. void phy_stop_machine(struct phy_device *phydev)
  423. {
  424. del_timer_sync(&phydev->phy_timer);
  425. spin_lock_bh(&phydev->lock);
  426. if (phydev->state > PHY_UP)
  427. phydev->state = PHY_UP;
  428. spin_unlock_bh(&phydev->lock);
  429. phydev->adjust_state = NULL;
  430. }
  431. /**
  432. * phy_force_reduction - reduce PHY speed/duplex settings by one step
  433. * @phydev: target phy_device struct
  434. *
  435. * Description: Reduces the speed/duplex settings by one notch,
  436. * in this order--
  437. * 1000/FULL, 1000/HALF, 100/FULL, 100/HALF, 10/FULL, 10/HALF.
  438. * The function bottoms out at 10/HALF.
  439. */
  440. static void phy_force_reduction(struct phy_device *phydev)
  441. {
  442. int idx;
  443. idx = phy_find_setting(phydev->speed, phydev->duplex);
  444. idx++;
  445. idx = phy_find_valid(idx, phydev->supported);
  446. phydev->speed = settings[idx].speed;
  447. phydev->duplex = settings[idx].duplex;
  448. pr_info("Trying %d/%s\n", phydev->speed,
  449. DUPLEX_FULL == phydev->duplex ?
  450. "FULL" : "HALF");
  451. }
  452. /**
  453. * phy_error - enter HALTED state for this PHY device
  454. * @phydev: target phy_device struct
  455. *
  456. * Moves the PHY to the HALTED state in response to a read
  457. * or write error, and tells the controller the link is down.
  458. * Must not be called from interrupt context, or while the
  459. * phydev->lock is held.
  460. */
  461. void phy_error(struct phy_device *phydev)
  462. {
  463. spin_lock_bh(&phydev->lock);
  464. phydev->state = PHY_HALTED;
  465. spin_unlock_bh(&phydev->lock);
  466. }
  467. /**
  468. * phy_interrupt - PHY interrupt handler
  469. * @irq: interrupt line
  470. * @phy_dat: phy_device pointer
  471. *
  472. * Description: When a PHY interrupt occurs, the handler disables
  473. * interrupts, and schedules a work task to clear the interrupt.
  474. */
  475. static irqreturn_t phy_interrupt(int irq, void *phy_dat)
  476. {
  477. struct phy_device *phydev = phy_dat;
  478. if (PHY_HALTED == phydev->state)
  479. return IRQ_NONE; /* It can't be ours. */
  480. /* The MDIO bus is not allowed to be written in interrupt
  481. * context, so we need to disable the irq here. A work
  482. * queue will write the PHY to disable and clear the
  483. * interrupt, and then reenable the irq line. */
  484. disable_irq_nosync(irq);
  485. schedule_work(&phydev->phy_queue);
  486. return IRQ_HANDLED;
  487. }
  488. /**
  489. * phy_enable_interrupts - Enable the interrupts from the PHY side
  490. * @phydev: target phy_device struct
  491. */
  492. int phy_enable_interrupts(struct phy_device *phydev)
  493. {
  494. int err;
  495. err = phy_clear_interrupt(phydev);
  496. if (err < 0)
  497. return err;
  498. err = phy_config_interrupt(phydev, PHY_INTERRUPT_ENABLED);
  499. return err;
  500. }
  501. EXPORT_SYMBOL(phy_enable_interrupts);
  502. /**
  503. * phy_disable_interrupts - Disable the PHY interrupts from the PHY side
  504. * @phydev: target phy_device struct
  505. */
  506. int phy_disable_interrupts(struct phy_device *phydev)
  507. {
  508. int err;
  509. /* Disable PHY interrupts */
  510. err = phy_config_interrupt(phydev, PHY_INTERRUPT_DISABLED);
  511. if (err)
  512. goto phy_err;
  513. /* Clear the interrupt */
  514. err = phy_clear_interrupt(phydev);
  515. if (err)
  516. goto phy_err;
  517. return 0;
  518. phy_err:
  519. phy_error(phydev);
  520. return err;
  521. }
  522. EXPORT_SYMBOL(phy_disable_interrupts);
  523. /**
  524. * phy_start_interrupts - request and enable interrupts for a PHY device
  525. * @phydev: target phy_device struct
  526. *
  527. * Description: Request the interrupt for the given PHY.
  528. * If this fails, then we set irq to PHY_POLL.
  529. * Otherwise, we enable the interrupts in the PHY.
  530. * This should only be called with a valid IRQ number.
  531. * Returns 0 on success or < 0 on error.
  532. */
  533. int phy_start_interrupts(struct phy_device *phydev)
  534. {
  535. int err = 0;
  536. INIT_WORK(&phydev->phy_queue, phy_change);
  537. if (request_irq(phydev->irq, phy_interrupt,
  538. IRQF_SHARED,
  539. "phy_interrupt",
  540. phydev) < 0) {
  541. printk(KERN_WARNING "%s: Can't get IRQ %d (PHY)\n",
  542. phydev->bus->name,
  543. phydev->irq);
  544. phydev->irq = PHY_POLL;
  545. return 0;
  546. }
  547. err = phy_enable_interrupts(phydev);
  548. return err;
  549. }
  550. EXPORT_SYMBOL(phy_start_interrupts);
  551. /**
  552. * phy_stop_interrupts - disable interrupts from a PHY device
  553. * @phydev: target phy_device struct
  554. */
  555. int phy_stop_interrupts(struct phy_device *phydev)
  556. {
  557. int err;
  558. err = phy_disable_interrupts(phydev);
  559. if (err)
  560. phy_error(phydev);
  561. /*
  562. * Finish any pending work; we might have been scheduled to be called
  563. * from keventd ourselves, but cancel_work_sync() handles that.
  564. */
  565. cancel_work_sync(&phydev->phy_queue);
  566. free_irq(phydev->irq, phydev);
  567. return err;
  568. }
  569. EXPORT_SYMBOL(phy_stop_interrupts);
  570. /**
  571. * phy_change - Scheduled by the phy_interrupt/timer to handle PHY changes
  572. * @work: work_struct that describes the work to be done
  573. */
  574. static void phy_change(struct work_struct *work)
  575. {
  576. int err;
  577. struct phy_device *phydev =
  578. container_of(work, struct phy_device, phy_queue);
  579. err = phy_disable_interrupts(phydev);
  580. if (err)
  581. goto phy_err;
  582. spin_lock_bh(&phydev->lock);
  583. if ((PHY_RUNNING == phydev->state) || (PHY_NOLINK == phydev->state))
  584. phydev->state = PHY_CHANGELINK;
  585. spin_unlock_bh(&phydev->lock);
  586. enable_irq(phydev->irq);
  587. /* Reenable interrupts */
  588. if (PHY_HALTED != phydev->state)
  589. err = phy_config_interrupt(phydev, PHY_INTERRUPT_ENABLED);
  590. if (err)
  591. goto irq_enable_err;
  592. return;
  593. irq_enable_err:
  594. disable_irq(phydev->irq);
  595. phy_err:
  596. phy_error(phydev);
  597. }
  598. /**
  599. * phy_stop - Bring down the PHY link, and stop checking the status
  600. * @phydev: target phy_device struct
  601. */
  602. void phy_stop(struct phy_device *phydev)
  603. {
  604. spin_lock_bh(&phydev->lock);
  605. if (PHY_HALTED == phydev->state)
  606. goto out_unlock;
  607. phydev->state = PHY_HALTED;
  608. if (phydev->irq != PHY_POLL) {
  609. /* Disable PHY Interrupts */
  610. phy_config_interrupt(phydev, PHY_INTERRUPT_DISABLED);
  611. /* Clear any pending interrupts */
  612. phy_clear_interrupt(phydev);
  613. }
  614. out_unlock:
  615. spin_unlock_bh(&phydev->lock);
  616. /*
  617. * Cannot call flush_scheduled_work() here as desired because
  618. * of rtnl_lock(), but PHY_HALTED shall guarantee phy_change()
  619. * will not reenable interrupts.
  620. */
  621. }
  622. /**
  623. * phy_start - start or restart a PHY device
  624. * @phydev: target phy_device struct
  625. *
  626. * Description: Indicates the attached device's readiness to
  627. * handle PHY-related work. Used during startup to start the
  628. * PHY, and after a call to phy_stop() to resume operation.
  629. * Also used to indicate the MDIO bus has cleared an error
  630. * condition.
  631. */
  632. void phy_start(struct phy_device *phydev)
  633. {
  634. spin_lock_bh(&phydev->lock);
  635. switch (phydev->state) {
  636. case PHY_STARTING:
  637. phydev->state = PHY_PENDING;
  638. break;
  639. case PHY_READY:
  640. phydev->state = PHY_UP;
  641. break;
  642. case PHY_HALTED:
  643. phydev->state = PHY_RESUMING;
  644. default:
  645. break;
  646. }
  647. spin_unlock_bh(&phydev->lock);
  648. }
  649. EXPORT_SYMBOL(phy_stop);
  650. EXPORT_SYMBOL(phy_start);
  651. /* PHY timer which handles the state machine */
  652. static void phy_timer(unsigned long data)
  653. {
  654. struct phy_device *phydev = (struct phy_device *)data;
  655. int needs_aneg = 0;
  656. int err = 0;
  657. spin_lock_bh(&phydev->lock);
  658. if (phydev->adjust_state)
  659. phydev->adjust_state(phydev->attached_dev);
  660. switch(phydev->state) {
  661. case PHY_DOWN:
  662. case PHY_STARTING:
  663. case PHY_READY:
  664. case PHY_PENDING:
  665. break;
  666. case PHY_UP:
  667. needs_aneg = 1;
  668. phydev->link_timeout = PHY_AN_TIMEOUT;
  669. break;
  670. case PHY_AN:
  671. err = phy_read_status(phydev);
  672. if (err < 0)
  673. break;
  674. /* If the link is down, give up on
  675. * negotiation for now */
  676. if (!phydev->link) {
  677. phydev->state = PHY_NOLINK;
  678. netif_carrier_off(phydev->attached_dev);
  679. phydev->adjust_link(phydev->attached_dev);
  680. break;
  681. }
  682. /* Check if negotiation is done. Break
  683. * if there's an error */
  684. err = phy_aneg_done(phydev);
  685. if (err < 0)
  686. break;
  687. /* If AN is done, we're running */
  688. if (err > 0) {
  689. phydev->state = PHY_RUNNING;
  690. netif_carrier_on(phydev->attached_dev);
  691. phydev->adjust_link(phydev->attached_dev);
  692. } else if (0 == phydev->link_timeout--) {
  693. int idx;
  694. needs_aneg = 1;
  695. /* If we have the magic_aneg bit,
  696. * we try again */
  697. if (phydev->drv->flags & PHY_HAS_MAGICANEG)
  698. break;
  699. /* The timer expired, and we still
  700. * don't have a setting, so we try
  701. * forcing it until we find one that
  702. * works, starting from the fastest speed,
  703. * and working our way down */
  704. idx = phy_find_valid(0, phydev->supported);
  705. phydev->speed = settings[idx].speed;
  706. phydev->duplex = settings[idx].duplex;
  707. phydev->autoneg = AUTONEG_DISABLE;
  708. pr_info("Trying %d/%s\n", phydev->speed,
  709. DUPLEX_FULL ==
  710. phydev->duplex ?
  711. "FULL" : "HALF");
  712. }
  713. break;
  714. case PHY_NOLINK:
  715. err = phy_read_status(phydev);
  716. if (err)
  717. break;
  718. if (phydev->link) {
  719. phydev->state = PHY_RUNNING;
  720. netif_carrier_on(phydev->attached_dev);
  721. phydev->adjust_link(phydev->attached_dev);
  722. }
  723. break;
  724. case PHY_FORCING:
  725. err = genphy_update_link(phydev);
  726. if (err)
  727. break;
  728. if (phydev->link) {
  729. phydev->state = PHY_RUNNING;
  730. netif_carrier_on(phydev->attached_dev);
  731. } else {
  732. if (0 == phydev->link_timeout--) {
  733. phy_force_reduction(phydev);
  734. needs_aneg = 1;
  735. }
  736. }
  737. phydev->adjust_link(phydev->attached_dev);
  738. break;
  739. case PHY_RUNNING:
  740. /* Only register a CHANGE if we are
  741. * polling */
  742. if (PHY_POLL == phydev->irq)
  743. phydev->state = PHY_CHANGELINK;
  744. break;
  745. case PHY_CHANGELINK:
  746. err = phy_read_status(phydev);
  747. if (err)
  748. break;
  749. if (phydev->link) {
  750. phydev->state = PHY_RUNNING;
  751. netif_carrier_on(phydev->attached_dev);
  752. } else {
  753. phydev->state = PHY_NOLINK;
  754. netif_carrier_off(phydev->attached_dev);
  755. }
  756. phydev->adjust_link(phydev->attached_dev);
  757. if (PHY_POLL != phydev->irq)
  758. err = phy_config_interrupt(phydev,
  759. PHY_INTERRUPT_ENABLED);
  760. break;
  761. case PHY_HALTED:
  762. if (phydev->link) {
  763. phydev->link = 0;
  764. netif_carrier_off(phydev->attached_dev);
  765. phydev->adjust_link(phydev->attached_dev);
  766. }
  767. break;
  768. case PHY_RESUMING:
  769. err = phy_clear_interrupt(phydev);
  770. if (err)
  771. break;
  772. err = phy_config_interrupt(phydev,
  773. PHY_INTERRUPT_ENABLED);
  774. if (err)
  775. break;
  776. if (AUTONEG_ENABLE == phydev->autoneg) {
  777. err = phy_aneg_done(phydev);
  778. if (err < 0)
  779. break;
  780. /* err > 0 if AN is done.
  781. * Otherwise, it's 0, and we're
  782. * still waiting for AN */
  783. if (err > 0) {
  784. phydev->state = PHY_RUNNING;
  785. } else {
  786. phydev->state = PHY_AN;
  787. phydev->link_timeout = PHY_AN_TIMEOUT;
  788. }
  789. } else
  790. phydev->state = PHY_RUNNING;
  791. break;
  792. }
  793. spin_unlock_bh(&phydev->lock);
  794. if (needs_aneg)
  795. err = phy_start_aneg(phydev);
  796. if (err < 0)
  797. phy_error(phydev);
  798. mod_timer(&phydev->phy_timer, jiffies + PHY_STATE_TIME * HZ);
  799. }