phy.c 23 KB

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