phy.c 23 KB

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