phy.c 24 KB

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