phy.c 24 KB

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