phy.c 22 KB

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