phy.c 28 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099
  1. /* Framework for configuring and reading PHY devices
  2. * Based on code in sungem_phy.c and gianfar_phy.c
  3. *
  4. * Author: Andy Fleming
  5. *
  6. * Copyright (c) 2004 Freescale Semiconductor, Inc.
  7. * Copyright (c) 2006, 2007 Maciej W. Rozycki
  8. *
  9. * This program is free software; you can redistribute it and/or modify it
  10. * under the terms of the GNU General Public License as published by the
  11. * Free Software Foundation; either version 2 of the License, or (at your
  12. * option) any later version.
  13. *
  14. */
  15. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  16. #include <linux/kernel.h>
  17. #include <linux/string.h>
  18. #include <linux/errno.h>
  19. #include <linux/unistd.h>
  20. #include <linux/interrupt.h>
  21. #include <linux/delay.h>
  22. #include <linux/netdevice.h>
  23. #include <linux/etherdevice.h>
  24. #include <linux/skbuff.h>
  25. #include <linux/mm.h>
  26. #include <linux/module.h>
  27. #include <linux/mii.h>
  28. #include <linux/ethtool.h>
  29. #include <linux/phy.h>
  30. #include <linux/timer.h>
  31. #include <linux/workqueue.h>
  32. #include <linux/mdio.h>
  33. #include <linux/io.h>
  34. #include <linux/uaccess.h>
  35. #include <linux/atomic.h>
  36. #include <asm/irq.h>
  37. /**
  38. * phy_print_status - Convenience function to print out the current phy status
  39. * @phydev: the phy_device struct
  40. */
  41. void phy_print_status(struct phy_device *phydev)
  42. {
  43. if (phydev->link) {
  44. pr_info("%s - Link is Up - %d/%s\n",
  45. dev_name(&phydev->dev),
  46. phydev->speed,
  47. DUPLEX_FULL == phydev->duplex ? "Full" : "Half");
  48. } else {
  49. pr_info("%s - Link is Down\n", dev_name(&phydev->dev));
  50. }
  51. }
  52. EXPORT_SYMBOL(phy_print_status);
  53. /**
  54. * phy_clear_interrupt - Ack the phy device's interrupt
  55. * @phydev: the phy_device struct
  56. *
  57. * If the @phydev driver has an ack_interrupt function, call it to
  58. * ack and clear the phy device's interrupt.
  59. *
  60. * Returns 0 on success on < 0 on error.
  61. */
  62. static int phy_clear_interrupt(struct phy_device *phydev)
  63. {
  64. if (phydev->drv->ack_interrupt)
  65. return phydev->drv->ack_interrupt(phydev);
  66. return 0;
  67. }
  68. /**
  69. * phy_config_interrupt - configure the PHY device for the requested interrupts
  70. * @phydev: the phy_device struct
  71. * @interrupts: interrupt flags to configure for this @phydev
  72. *
  73. * Returns 0 on success on < 0 on error.
  74. */
  75. static int phy_config_interrupt(struct phy_device *phydev, u32 interrupts)
  76. {
  77. phydev->interrupts = interrupts;
  78. if (phydev->drv->config_intr)
  79. return phydev->drv->config_intr(phydev);
  80. return 0;
  81. }
  82. /**
  83. * phy_aneg_done - return auto-negotiation status
  84. * @phydev: target phy_device struct
  85. *
  86. * Description: Reads the status register and returns 0 either if
  87. * auto-negotiation is incomplete, or if there was an error.
  88. * Returns BMSR_ANEGCOMPLETE if auto-negotiation is done.
  89. */
  90. static inline int phy_aneg_done(struct phy_device *phydev)
  91. {
  92. int retval = phy_read(phydev, MII_BMSR);
  93. return (retval < 0) ? retval : (retval & BMSR_ANEGCOMPLETE);
  94. }
  95. /* A structure for mapping a particular speed and duplex
  96. * combination to a particular SUPPORTED and ADVERTISED value
  97. */
  98. struct phy_setting {
  99. int speed;
  100. int duplex;
  101. u32 setting;
  102. };
  103. /* A mapping of all SUPPORTED settings to speed/duplex */
  104. static const struct phy_setting settings[] = {
  105. {
  106. .speed = 10000,
  107. .duplex = DUPLEX_FULL,
  108. .setting = SUPPORTED_10000baseT_Full,
  109. },
  110. {
  111. .speed = SPEED_1000,
  112. .duplex = DUPLEX_FULL,
  113. .setting = SUPPORTED_1000baseT_Full,
  114. },
  115. {
  116. .speed = SPEED_1000,
  117. .duplex = DUPLEX_HALF,
  118. .setting = SUPPORTED_1000baseT_Half,
  119. },
  120. {
  121. .speed = SPEED_100,
  122. .duplex = DUPLEX_FULL,
  123. .setting = SUPPORTED_100baseT_Full,
  124. },
  125. {
  126. .speed = SPEED_100,
  127. .duplex = DUPLEX_HALF,
  128. .setting = SUPPORTED_100baseT_Half,
  129. },
  130. {
  131. .speed = SPEED_10,
  132. .duplex = DUPLEX_FULL,
  133. .setting = SUPPORTED_10baseT_Full,
  134. },
  135. {
  136. .speed = SPEED_10,
  137. .duplex = DUPLEX_HALF,
  138. .setting = SUPPORTED_10baseT_Half,
  139. },
  140. };
  141. #define MAX_NUM_SETTINGS ARRAY_SIZE(settings)
  142. /**
  143. * phy_find_setting - find a PHY settings array entry that matches speed & duplex
  144. * @speed: speed to match
  145. * @duplex: duplex to match
  146. *
  147. * Description: Searches the settings array for the setting which
  148. * matches the desired speed and duplex, and returns the index
  149. * of that setting. Returns the index of the last setting if
  150. * none of the others match.
  151. */
  152. static inline unsigned int phy_find_setting(int speed, int duplex)
  153. {
  154. unsigned int idx = 0;
  155. while (idx < ARRAY_SIZE(settings) &&
  156. (settings[idx].speed != speed || settings[idx].duplex != duplex))
  157. idx++;
  158. return idx < MAX_NUM_SETTINGS ? idx : MAX_NUM_SETTINGS - 1;
  159. }
  160. /**
  161. * phy_find_valid - find a PHY setting that matches the requested features mask
  162. * @idx: The first index in settings[] to search
  163. * @features: A mask of the valid settings
  164. *
  165. * Description: Returns the index of the first valid setting less
  166. * than or equal to the one pointed to by idx, as determined by
  167. * the mask in features. Returns the index of the last setting
  168. * if nothing else matches.
  169. */
  170. static inline unsigned int phy_find_valid(unsigned int idx, u32 features)
  171. {
  172. while (idx < MAX_NUM_SETTINGS && !(settings[idx].setting & features))
  173. idx++;
  174. return idx < MAX_NUM_SETTINGS ? idx : MAX_NUM_SETTINGS - 1;
  175. }
  176. /**
  177. * phy_sanitize_settings - make sure the PHY is set to supported speed and duplex
  178. * @phydev: the target phy_device struct
  179. *
  180. * Description: Make sure the PHY is set to supported speeds and
  181. * duplexes. Drop down by one in this order: 1000/FULL,
  182. * 1000/HALF, 100/FULL, 100/HALF, 10/FULL, 10/HALF.
  183. */
  184. static void phy_sanitize_settings(struct phy_device *phydev)
  185. {
  186. u32 features = phydev->supported;
  187. unsigned int idx;
  188. /* Sanitize settings based on PHY capabilities */
  189. if ((features & SUPPORTED_Autoneg) == 0)
  190. phydev->autoneg = AUTONEG_DISABLE;
  191. idx = phy_find_valid(phy_find_setting(phydev->speed, phydev->duplex),
  192. features);
  193. phydev->speed = settings[idx].speed;
  194. phydev->duplex = settings[idx].duplex;
  195. }
  196. /**
  197. * phy_ethtool_sset - generic ethtool sset function, handles all the details
  198. * @phydev: target phy_device struct
  199. * @cmd: ethtool_cmd
  200. *
  201. * A few notes about parameter checking:
  202. * - We don't set port or transceiver, so we don't care what they
  203. * were set to.
  204. * - phy_start_aneg() will make sure forced settings are sane, and
  205. * choose the next best ones from the ones selected, so we don't
  206. * care if ethtool tries to give us bad values.
  207. */
  208. int phy_ethtool_sset(struct phy_device *phydev, struct ethtool_cmd *cmd)
  209. {
  210. u32 speed = ethtool_cmd_speed(cmd);
  211. if (cmd->phy_address != phydev->addr)
  212. return -EINVAL;
  213. /* We make sure that we don't pass unsupported values in to the PHY */
  214. cmd->advertising &= phydev->supported;
  215. /* Verify the settings we care about. */
  216. if (cmd->autoneg != AUTONEG_ENABLE && cmd->autoneg != AUTONEG_DISABLE)
  217. return -EINVAL;
  218. if (cmd->autoneg == AUTONEG_ENABLE && cmd->advertising == 0)
  219. return -EINVAL;
  220. if (cmd->autoneg == AUTONEG_DISABLE &&
  221. ((speed != SPEED_1000 &&
  222. speed != SPEED_100 &&
  223. speed != SPEED_10) ||
  224. (cmd->duplex != DUPLEX_HALF &&
  225. cmd->duplex != DUPLEX_FULL)))
  226. return -EINVAL;
  227. phydev->autoneg = cmd->autoneg;
  228. phydev->speed = speed;
  229. phydev->advertising = cmd->advertising;
  230. if (AUTONEG_ENABLE == cmd->autoneg)
  231. phydev->advertising |= ADVERTISED_Autoneg;
  232. else
  233. phydev->advertising &= ~ADVERTISED_Autoneg;
  234. phydev->duplex = cmd->duplex;
  235. /* Restart the PHY */
  236. phy_start_aneg(phydev);
  237. return 0;
  238. }
  239. EXPORT_SYMBOL(phy_ethtool_sset);
  240. int phy_ethtool_gset(struct phy_device *phydev, struct ethtool_cmd *cmd)
  241. {
  242. cmd->supported = phydev->supported;
  243. cmd->advertising = phydev->advertising;
  244. cmd->lp_advertising = phydev->lp_advertising;
  245. ethtool_cmd_speed_set(cmd, phydev->speed);
  246. cmd->duplex = phydev->duplex;
  247. cmd->port = PORT_MII;
  248. cmd->phy_address = phydev->addr;
  249. cmd->transceiver = phy_is_internal(phydev) ?
  250. XCVR_INTERNAL : XCVR_EXTERNAL;
  251. cmd->autoneg = phydev->autoneg;
  252. return 0;
  253. }
  254. EXPORT_SYMBOL(phy_ethtool_gset);
  255. /**
  256. * phy_mii_ioctl - generic PHY MII ioctl interface
  257. * @phydev: the phy_device struct
  258. * @ifr: &struct ifreq for socket ioctl's
  259. * @cmd: ioctl cmd to execute
  260. *
  261. * Note that this function is currently incompatible with the
  262. * PHYCONTROL layer. It changes registers without regard to
  263. * current state. Use at own risk.
  264. */
  265. int phy_mii_ioctl(struct phy_device *phydev, struct ifreq *ifr, int cmd)
  266. {
  267. struct mii_ioctl_data *mii_data = if_mii(ifr);
  268. u16 val = mii_data->val_in;
  269. switch (cmd) {
  270. case SIOCGMIIPHY:
  271. mii_data->phy_id = phydev->addr;
  272. /* fall through */
  273. case SIOCGMIIREG:
  274. mii_data->val_out = mdiobus_read(phydev->bus, mii_data->phy_id,
  275. mii_data->reg_num);
  276. return 0;
  277. case SIOCSMIIREG:
  278. if (mii_data->phy_id == phydev->addr) {
  279. switch (mii_data->reg_num) {
  280. case MII_BMCR:
  281. if ((val & (BMCR_RESET | BMCR_ANENABLE)) == 0)
  282. phydev->autoneg = AUTONEG_DISABLE;
  283. else
  284. phydev->autoneg = AUTONEG_ENABLE;
  285. if (!phydev->autoneg && (val & BMCR_FULLDPLX))
  286. phydev->duplex = DUPLEX_FULL;
  287. else
  288. phydev->duplex = DUPLEX_HALF;
  289. if (!phydev->autoneg && (val & BMCR_SPEED1000))
  290. phydev->speed = SPEED_1000;
  291. else if (!phydev->autoneg &&
  292. (val & BMCR_SPEED100))
  293. phydev->speed = SPEED_100;
  294. break;
  295. case MII_ADVERTISE:
  296. phydev->advertising = val;
  297. break;
  298. default:
  299. /* do nothing */
  300. break;
  301. }
  302. }
  303. mdiobus_write(phydev->bus, mii_data->phy_id,
  304. mii_data->reg_num, val);
  305. if (mii_data->reg_num == MII_BMCR &&
  306. val & BMCR_RESET)
  307. return phy_init_hw(phydev);
  308. return 0;
  309. case SIOCSHWTSTAMP:
  310. if (phydev->drv->hwtstamp)
  311. return phydev->drv->hwtstamp(phydev, ifr);
  312. /* fall through */
  313. default:
  314. return -EOPNOTSUPP;
  315. }
  316. }
  317. EXPORT_SYMBOL(phy_mii_ioctl);
  318. /**
  319. * phy_start_aneg - start auto-negotiation for this PHY device
  320. * @phydev: the phy_device struct
  321. *
  322. * Description: Sanitizes the settings (if we're not autonegotiating
  323. * them), and then calls the driver's config_aneg function.
  324. * If the PHYCONTROL Layer is operating, we change the state to
  325. * reflect the beginning of Auto-negotiation or forcing.
  326. */
  327. int phy_start_aneg(struct phy_device *phydev)
  328. {
  329. int err;
  330. mutex_lock(&phydev->lock);
  331. if (AUTONEG_DISABLE == phydev->autoneg)
  332. phy_sanitize_settings(phydev);
  333. err = phydev->drv->config_aneg(phydev);
  334. if (err < 0)
  335. goto out_unlock;
  336. if (phydev->state != PHY_HALTED) {
  337. if (AUTONEG_ENABLE == phydev->autoneg) {
  338. phydev->state = PHY_AN;
  339. phydev->link_timeout = PHY_AN_TIMEOUT;
  340. } else {
  341. phydev->state = PHY_FORCING;
  342. phydev->link_timeout = PHY_FORCE_TIMEOUT;
  343. }
  344. }
  345. out_unlock:
  346. mutex_unlock(&phydev->lock);
  347. return err;
  348. }
  349. EXPORT_SYMBOL(phy_start_aneg);
  350. /**
  351. * phy_start_machine - start PHY state machine tracking
  352. * @phydev: the phy_device struct
  353. *
  354. * Description: The PHY infrastructure can run a state machine
  355. * which tracks whether the PHY is starting up, negotiating,
  356. * etc. This function starts the timer which tracks the state
  357. * of the PHY. If you want to maintain your own state machine,
  358. * do not call this function.
  359. */
  360. void phy_start_machine(struct phy_device *phydev)
  361. {
  362. queue_delayed_work(system_power_efficient_wq, &phydev->state_queue, HZ);
  363. }
  364. /**
  365. * phy_stop_machine - stop the PHY state machine tracking
  366. * @phydev: target phy_device struct
  367. *
  368. * Description: Stops the state machine timer, sets the state to UP
  369. * (unless it wasn't up yet). This function must be called BEFORE
  370. * phy_detach.
  371. */
  372. void phy_stop_machine(struct phy_device *phydev)
  373. {
  374. cancel_delayed_work_sync(&phydev->state_queue);
  375. mutex_lock(&phydev->lock);
  376. if (phydev->state > PHY_UP)
  377. phydev->state = PHY_UP;
  378. mutex_unlock(&phydev->lock);
  379. }
  380. /**
  381. * phy_error - enter HALTED state for this PHY device
  382. * @phydev: target phy_device struct
  383. *
  384. * Moves the PHY to the HALTED state in response to a read
  385. * or write error, and tells the controller the link is down.
  386. * Must not be called from interrupt context, or while the
  387. * phydev->lock is held.
  388. */
  389. static void phy_error(struct phy_device *phydev)
  390. {
  391. mutex_lock(&phydev->lock);
  392. phydev->state = PHY_HALTED;
  393. mutex_unlock(&phydev->lock);
  394. }
  395. /**
  396. * phy_interrupt - PHY interrupt handler
  397. * @irq: interrupt line
  398. * @phy_dat: phy_device pointer
  399. *
  400. * Description: When a PHY interrupt occurs, the handler disables
  401. * interrupts, and schedules a work task to clear the interrupt.
  402. */
  403. static irqreturn_t phy_interrupt(int irq, void *phy_dat)
  404. {
  405. struct phy_device *phydev = phy_dat;
  406. if (PHY_HALTED == phydev->state)
  407. return IRQ_NONE; /* It can't be ours. */
  408. /* The MDIO bus is not allowed to be written in interrupt
  409. * context, so we need to disable the irq here. A work
  410. * queue will write the PHY to disable and clear the
  411. * interrupt, and then reenable the irq line.
  412. */
  413. disable_irq_nosync(irq);
  414. atomic_inc(&phydev->irq_disable);
  415. queue_work(system_power_efficient_wq, &phydev->phy_queue);
  416. return IRQ_HANDLED;
  417. }
  418. /**
  419. * phy_enable_interrupts - Enable the interrupts from the PHY side
  420. * @phydev: target phy_device struct
  421. */
  422. static int phy_enable_interrupts(struct phy_device *phydev)
  423. {
  424. int err = phy_clear_interrupt(phydev);
  425. if (err < 0)
  426. return err;
  427. return phy_config_interrupt(phydev, PHY_INTERRUPT_ENABLED);
  428. }
  429. /**
  430. * phy_disable_interrupts - Disable the PHY interrupts from the PHY side
  431. * @phydev: target phy_device struct
  432. */
  433. static int phy_disable_interrupts(struct phy_device *phydev)
  434. {
  435. int err;
  436. /* Disable PHY interrupts */
  437. err = phy_config_interrupt(phydev, PHY_INTERRUPT_DISABLED);
  438. if (err)
  439. goto phy_err;
  440. /* Clear the interrupt */
  441. err = phy_clear_interrupt(phydev);
  442. if (err)
  443. goto phy_err;
  444. return 0;
  445. phy_err:
  446. phy_error(phydev);
  447. return err;
  448. }
  449. /**
  450. * phy_start_interrupts - request and enable interrupts for a PHY device
  451. * @phydev: target phy_device struct
  452. *
  453. * Description: Request the interrupt for the given PHY.
  454. * If this fails, then we set irq to PHY_POLL.
  455. * Otherwise, we enable the interrupts in the PHY.
  456. * This should only be called with a valid IRQ number.
  457. * Returns 0 on success or < 0 on error.
  458. */
  459. int phy_start_interrupts(struct phy_device *phydev)
  460. {
  461. atomic_set(&phydev->irq_disable, 0);
  462. if (request_irq(phydev->irq, phy_interrupt, 0, "phy_interrupt",
  463. phydev) < 0) {
  464. pr_warn("%s: Can't get IRQ %d (PHY)\n",
  465. phydev->bus->name, phydev->irq);
  466. phydev->irq = PHY_POLL;
  467. return 0;
  468. }
  469. return phy_enable_interrupts(phydev);
  470. }
  471. EXPORT_SYMBOL(phy_start_interrupts);
  472. /**
  473. * phy_stop_interrupts - disable interrupts from a PHY device
  474. * @phydev: target phy_device struct
  475. */
  476. int phy_stop_interrupts(struct phy_device *phydev)
  477. {
  478. int err = phy_disable_interrupts(phydev);
  479. if (err)
  480. phy_error(phydev);
  481. free_irq(phydev->irq, phydev);
  482. /* Cannot call flush_scheduled_work() here as desired because
  483. * of rtnl_lock(), but we do not really care about what would
  484. * be done, except from enable_irq(), so cancel any work
  485. * possibly pending and take care of the matter below.
  486. */
  487. cancel_work_sync(&phydev->phy_queue);
  488. /* If work indeed has been cancelled, disable_irq() will have
  489. * been left unbalanced from phy_interrupt() and enable_irq()
  490. * has to be called so that other devices on the line work.
  491. */
  492. while (atomic_dec_return(&phydev->irq_disable) >= 0)
  493. enable_irq(phydev->irq);
  494. return err;
  495. }
  496. EXPORT_SYMBOL(phy_stop_interrupts);
  497. /**
  498. * phy_change - Scheduled by the phy_interrupt/timer to handle PHY changes
  499. * @work: work_struct that describes the work to be done
  500. */
  501. void phy_change(struct work_struct *work)
  502. {
  503. struct phy_device *phydev =
  504. container_of(work, struct phy_device, phy_queue);
  505. if (phydev->drv->did_interrupt &&
  506. !phydev->drv->did_interrupt(phydev))
  507. goto ignore;
  508. if (phy_disable_interrupts(phydev))
  509. goto phy_err;
  510. mutex_lock(&phydev->lock);
  511. if ((PHY_RUNNING == phydev->state) || (PHY_NOLINK == phydev->state))
  512. phydev->state = PHY_CHANGELINK;
  513. mutex_unlock(&phydev->lock);
  514. atomic_dec(&phydev->irq_disable);
  515. enable_irq(phydev->irq);
  516. /* Reenable interrupts */
  517. if (PHY_HALTED != phydev->state &&
  518. phy_config_interrupt(phydev, PHY_INTERRUPT_ENABLED))
  519. goto irq_enable_err;
  520. /* reschedule state queue work to run as soon as possible */
  521. cancel_delayed_work_sync(&phydev->state_queue);
  522. queue_delayed_work(system_power_efficient_wq, &phydev->state_queue, 0);
  523. return;
  524. ignore:
  525. atomic_dec(&phydev->irq_disable);
  526. enable_irq(phydev->irq);
  527. return;
  528. irq_enable_err:
  529. disable_irq(phydev->irq);
  530. atomic_inc(&phydev->irq_disable);
  531. phy_err:
  532. phy_error(phydev);
  533. }
  534. /**
  535. * phy_stop - Bring down the PHY link, and stop checking the status
  536. * @phydev: target phy_device struct
  537. */
  538. void phy_stop(struct phy_device *phydev)
  539. {
  540. mutex_lock(&phydev->lock);
  541. if (PHY_HALTED == phydev->state)
  542. goto out_unlock;
  543. if (phy_interrupt_is_valid(phydev)) {
  544. /* Disable PHY Interrupts */
  545. phy_config_interrupt(phydev, PHY_INTERRUPT_DISABLED);
  546. /* Clear any pending interrupts */
  547. phy_clear_interrupt(phydev);
  548. }
  549. phydev->state = PHY_HALTED;
  550. out_unlock:
  551. mutex_unlock(&phydev->lock);
  552. /* Cannot call flush_scheduled_work() here as desired because
  553. * of rtnl_lock(), but PHY_HALTED shall guarantee phy_change()
  554. * will not reenable interrupts.
  555. */
  556. }
  557. EXPORT_SYMBOL(phy_stop);
  558. /**
  559. * phy_start - start or restart a PHY device
  560. * @phydev: target phy_device struct
  561. *
  562. * Description: Indicates the attached device's readiness to
  563. * handle PHY-related work. Used during startup to start the
  564. * PHY, and after a call to phy_stop() to resume operation.
  565. * Also used to indicate the MDIO bus has cleared an error
  566. * condition.
  567. */
  568. void phy_start(struct phy_device *phydev)
  569. {
  570. mutex_lock(&phydev->lock);
  571. switch (phydev->state) {
  572. case PHY_STARTING:
  573. phydev->state = PHY_PENDING;
  574. break;
  575. case PHY_READY:
  576. phydev->state = PHY_UP;
  577. break;
  578. case PHY_HALTED:
  579. phydev->state = PHY_RESUMING;
  580. default:
  581. break;
  582. }
  583. mutex_unlock(&phydev->lock);
  584. }
  585. EXPORT_SYMBOL(phy_start);
  586. /**
  587. * phy_state_machine - Handle the state machine
  588. * @work: work_struct that describes the work to be done
  589. */
  590. void phy_state_machine(struct work_struct *work)
  591. {
  592. struct delayed_work *dwork = to_delayed_work(work);
  593. struct phy_device *phydev =
  594. container_of(dwork, struct phy_device, state_queue);
  595. int needs_aneg = 0, do_suspend = 0;
  596. int err = 0;
  597. mutex_lock(&phydev->lock);
  598. switch (phydev->state) {
  599. case PHY_DOWN:
  600. case PHY_STARTING:
  601. case PHY_READY:
  602. case PHY_PENDING:
  603. break;
  604. case PHY_UP:
  605. needs_aneg = 1;
  606. phydev->link_timeout = PHY_AN_TIMEOUT;
  607. break;
  608. case PHY_AN:
  609. err = phy_read_status(phydev);
  610. if (err < 0)
  611. break;
  612. /* If the link is down, give up on negotiation for now */
  613. if (!phydev->link) {
  614. phydev->state = PHY_NOLINK;
  615. netif_carrier_off(phydev->attached_dev);
  616. phydev->adjust_link(phydev->attached_dev);
  617. break;
  618. }
  619. /* Check if negotiation is done. Break if there's an error */
  620. err = phy_aneg_done(phydev);
  621. if (err < 0)
  622. break;
  623. /* If AN is done, we're running */
  624. if (err > 0) {
  625. phydev->state = PHY_RUNNING;
  626. netif_carrier_on(phydev->attached_dev);
  627. phydev->adjust_link(phydev->attached_dev);
  628. } else if (0 == phydev->link_timeout--) {
  629. needs_aneg = 1;
  630. /* If we have the magic_aneg bit, we try again */
  631. if (phydev->drv->flags & PHY_HAS_MAGICANEG)
  632. break;
  633. }
  634. break;
  635. case PHY_NOLINK:
  636. err = phy_read_status(phydev);
  637. if (err)
  638. break;
  639. if (phydev->link) {
  640. phydev->state = PHY_RUNNING;
  641. netif_carrier_on(phydev->attached_dev);
  642. phydev->adjust_link(phydev->attached_dev);
  643. }
  644. break;
  645. case PHY_FORCING:
  646. err = genphy_update_link(phydev);
  647. if (err)
  648. break;
  649. if (phydev->link) {
  650. phydev->state = PHY_RUNNING;
  651. netif_carrier_on(phydev->attached_dev);
  652. } else {
  653. if (0 == phydev->link_timeout--)
  654. needs_aneg = 1;
  655. }
  656. phydev->adjust_link(phydev->attached_dev);
  657. break;
  658. case PHY_RUNNING:
  659. /* Only register a CHANGE if we are
  660. * polling or ignoring interrupts
  661. */
  662. if (!phy_interrupt_is_valid(phydev))
  663. phydev->state = PHY_CHANGELINK;
  664. break;
  665. case PHY_CHANGELINK:
  666. err = phy_read_status(phydev);
  667. if (err)
  668. break;
  669. if (phydev->link) {
  670. phydev->state = PHY_RUNNING;
  671. netif_carrier_on(phydev->attached_dev);
  672. } else {
  673. phydev->state = PHY_NOLINK;
  674. netif_carrier_off(phydev->attached_dev);
  675. }
  676. phydev->adjust_link(phydev->attached_dev);
  677. if (phy_interrupt_is_valid(phydev))
  678. err = phy_config_interrupt(phydev,
  679. PHY_INTERRUPT_ENABLED);
  680. break;
  681. case PHY_HALTED:
  682. if (phydev->link) {
  683. phydev->link = 0;
  684. netif_carrier_off(phydev->attached_dev);
  685. phydev->adjust_link(phydev->attached_dev);
  686. do_suspend = 1;
  687. }
  688. break;
  689. case PHY_RESUMING:
  690. err = phy_clear_interrupt(phydev);
  691. if (err)
  692. break;
  693. err = phy_config_interrupt(phydev, PHY_INTERRUPT_ENABLED);
  694. if (err)
  695. break;
  696. if (AUTONEG_ENABLE == phydev->autoneg) {
  697. err = phy_aneg_done(phydev);
  698. if (err < 0)
  699. break;
  700. /* err > 0 if AN is done.
  701. * Otherwise, it's 0, and we're still waiting for AN
  702. */
  703. if (err > 0) {
  704. err = phy_read_status(phydev);
  705. if (err)
  706. break;
  707. if (phydev->link) {
  708. phydev->state = PHY_RUNNING;
  709. netif_carrier_on(phydev->attached_dev);
  710. } else {
  711. phydev->state = PHY_NOLINK;
  712. }
  713. phydev->adjust_link(phydev->attached_dev);
  714. } else {
  715. phydev->state = PHY_AN;
  716. phydev->link_timeout = PHY_AN_TIMEOUT;
  717. }
  718. } else {
  719. err = phy_read_status(phydev);
  720. if (err)
  721. break;
  722. if (phydev->link) {
  723. phydev->state = PHY_RUNNING;
  724. netif_carrier_on(phydev->attached_dev);
  725. } else {
  726. phydev->state = PHY_NOLINK;
  727. }
  728. phydev->adjust_link(phydev->attached_dev);
  729. }
  730. break;
  731. }
  732. mutex_unlock(&phydev->lock);
  733. if (needs_aneg)
  734. err = phy_start_aneg(phydev);
  735. if (do_suspend)
  736. phy_suspend(phydev);
  737. if (err < 0)
  738. phy_error(phydev);
  739. queue_delayed_work(system_power_efficient_wq, &phydev->state_queue,
  740. PHY_STATE_TIME * HZ);
  741. }
  742. void phy_mac_interrupt(struct phy_device *phydev, int new_link)
  743. {
  744. cancel_work_sync(&phydev->phy_queue);
  745. phydev->link = new_link;
  746. schedule_work(&phydev->phy_queue);
  747. }
  748. EXPORT_SYMBOL(phy_mac_interrupt);
  749. static inline void mmd_phy_indirect(struct mii_bus *bus, int prtad, int devad,
  750. int addr)
  751. {
  752. /* Write the desired MMD Devad */
  753. bus->write(bus, addr, MII_MMD_CTRL, devad);
  754. /* Write the desired MMD register address */
  755. bus->write(bus, addr, MII_MMD_DATA, prtad);
  756. /* Select the Function : DATA with no post increment */
  757. bus->write(bus, addr, MII_MMD_CTRL, (devad | MII_MMD_CTRL_NOINCR));
  758. }
  759. /**
  760. * phy_read_mmd_indirect - reads data from the MMD registers
  761. * @bus: the target MII bus
  762. * @prtad: MMD Address
  763. * @devad: MMD DEVAD
  764. * @addr: PHY address on the MII bus
  765. *
  766. * Description: it reads data from the MMD registers (clause 22 to access to
  767. * clause 45) of the specified phy address.
  768. * To read these register we have:
  769. * 1) Write reg 13 // DEVAD
  770. * 2) Write reg 14 // MMD Address
  771. * 3) Write reg 13 // MMD Data Command for MMD DEVAD
  772. * 3) Read reg 14 // Read MMD data
  773. */
  774. static int phy_read_mmd_indirect(struct mii_bus *bus, int prtad, int devad,
  775. int addr)
  776. {
  777. mmd_phy_indirect(bus, prtad, devad, addr);
  778. /* Read the content of the MMD's selected register */
  779. return bus->read(bus, addr, MII_MMD_DATA);
  780. }
  781. /**
  782. * phy_write_mmd_indirect - writes data to the MMD registers
  783. * @bus: the target MII bus
  784. * @prtad: MMD Address
  785. * @devad: MMD DEVAD
  786. * @addr: PHY address on the MII bus
  787. * @data: data to write in the MMD register
  788. *
  789. * Description: Write data from the MMD registers of the specified
  790. * phy address.
  791. * To write these register we have:
  792. * 1) Write reg 13 // DEVAD
  793. * 2) Write reg 14 // MMD Address
  794. * 3) Write reg 13 // MMD Data Command for MMD DEVAD
  795. * 3) Write reg 14 // Write MMD data
  796. */
  797. static void phy_write_mmd_indirect(struct mii_bus *bus, int prtad, int devad,
  798. int addr, u32 data)
  799. {
  800. mmd_phy_indirect(bus, prtad, devad, addr);
  801. /* Write the data into MMD's selected register */
  802. bus->write(bus, addr, MII_MMD_DATA, data);
  803. }
  804. /**
  805. * phy_init_eee - init and check the EEE feature
  806. * @phydev: target phy_device struct
  807. * @clk_stop_enable: PHY may stop the clock during LPI
  808. *
  809. * Description: it checks if the Energy-Efficient Ethernet (EEE)
  810. * is supported by looking at the MMD registers 3.20 and 7.60/61
  811. * and it programs the MMD register 3.0 setting the "Clock stop enable"
  812. * bit if required.
  813. */
  814. int phy_init_eee(struct phy_device *phydev, bool clk_stop_enable)
  815. {
  816. /* According to 802.3az,the EEE is supported only in full duplex-mode.
  817. * Also EEE feature is active when core is operating with MII, GMII
  818. * or RGMII.
  819. */
  820. if ((phydev->duplex == DUPLEX_FULL) &&
  821. ((phydev->interface == PHY_INTERFACE_MODE_MII) ||
  822. (phydev->interface == PHY_INTERFACE_MODE_GMII) ||
  823. (phydev->interface == PHY_INTERFACE_MODE_RGMII))) {
  824. int eee_lp, eee_cap, eee_adv;
  825. u32 lp, cap, adv;
  826. int status;
  827. unsigned int idx;
  828. /* Read phy status to properly get the right settings */
  829. status = phy_read_status(phydev);
  830. if (status)
  831. return status;
  832. /* First check if the EEE ability is supported */
  833. eee_cap = phy_read_mmd_indirect(phydev->bus, MDIO_PCS_EEE_ABLE,
  834. MDIO_MMD_PCS, phydev->addr);
  835. if (eee_cap < 0)
  836. return eee_cap;
  837. cap = mmd_eee_cap_to_ethtool_sup_t(eee_cap);
  838. if (!cap)
  839. return -EPROTONOSUPPORT;
  840. /* Check which link settings negotiated and verify it in
  841. * the EEE advertising registers.
  842. */
  843. eee_lp = phy_read_mmd_indirect(phydev->bus, MDIO_AN_EEE_LPABLE,
  844. MDIO_MMD_AN, phydev->addr);
  845. if (eee_lp < 0)
  846. return eee_lp;
  847. eee_adv = phy_read_mmd_indirect(phydev->bus, MDIO_AN_EEE_ADV,
  848. MDIO_MMD_AN, phydev->addr);
  849. if (eee_adv < 0)
  850. return eee_adv;
  851. adv = mmd_eee_adv_to_ethtool_adv_t(eee_adv);
  852. lp = mmd_eee_adv_to_ethtool_adv_t(eee_lp);
  853. idx = phy_find_setting(phydev->speed, phydev->duplex);
  854. if (!(lp & adv & settings[idx].setting))
  855. return -EPROTONOSUPPORT;
  856. if (clk_stop_enable) {
  857. /* Configure the PHY to stop receiving xMII
  858. * clock while it is signaling LPI.
  859. */
  860. int val = phy_read_mmd_indirect(phydev->bus, MDIO_CTRL1,
  861. MDIO_MMD_PCS,
  862. phydev->addr);
  863. if (val < 0)
  864. return val;
  865. val |= MDIO_PCS_CTRL1_CLKSTOP_EN;
  866. phy_write_mmd_indirect(phydev->bus, MDIO_CTRL1,
  867. MDIO_MMD_PCS, phydev->addr, val);
  868. }
  869. return 0; /* EEE supported */
  870. }
  871. return -EPROTONOSUPPORT;
  872. }
  873. EXPORT_SYMBOL(phy_init_eee);
  874. /**
  875. * phy_get_eee_err - report the EEE wake error count
  876. * @phydev: target phy_device struct
  877. *
  878. * Description: it is to report the number of time where the PHY
  879. * failed to complete its normal wake sequence.
  880. */
  881. int phy_get_eee_err(struct phy_device *phydev)
  882. {
  883. return phy_read_mmd_indirect(phydev->bus, MDIO_PCS_EEE_WK_ERR,
  884. MDIO_MMD_PCS, phydev->addr);
  885. }
  886. EXPORT_SYMBOL(phy_get_eee_err);
  887. /**
  888. * phy_ethtool_get_eee - get EEE supported and status
  889. * @phydev: target phy_device struct
  890. * @data: ethtool_eee data
  891. *
  892. * Description: it reportes the Supported/Advertisement/LP Advertisement
  893. * capabilities.
  894. */
  895. int phy_ethtool_get_eee(struct phy_device *phydev, struct ethtool_eee *data)
  896. {
  897. int val;
  898. /* Get Supported EEE */
  899. val = phy_read_mmd_indirect(phydev->bus, MDIO_PCS_EEE_ABLE,
  900. MDIO_MMD_PCS, phydev->addr);
  901. if (val < 0)
  902. return val;
  903. data->supported = mmd_eee_cap_to_ethtool_sup_t(val);
  904. /* Get advertisement EEE */
  905. val = phy_read_mmd_indirect(phydev->bus, MDIO_AN_EEE_ADV,
  906. MDIO_MMD_AN, phydev->addr);
  907. if (val < 0)
  908. return val;
  909. data->advertised = mmd_eee_adv_to_ethtool_adv_t(val);
  910. /* Get LP advertisement EEE */
  911. val = phy_read_mmd_indirect(phydev->bus, MDIO_AN_EEE_LPABLE,
  912. MDIO_MMD_AN, phydev->addr);
  913. if (val < 0)
  914. return val;
  915. data->lp_advertised = mmd_eee_adv_to_ethtool_adv_t(val);
  916. return 0;
  917. }
  918. EXPORT_SYMBOL(phy_ethtool_get_eee);
  919. /**
  920. * phy_ethtool_set_eee - set EEE supported and status
  921. * @phydev: target phy_device struct
  922. * @data: ethtool_eee data
  923. *
  924. * Description: it is to program the Advertisement EEE register.
  925. */
  926. int phy_ethtool_set_eee(struct phy_device *phydev, struct ethtool_eee *data)
  927. {
  928. int val = ethtool_adv_to_mmd_eee_adv_t(data->advertised);
  929. phy_write_mmd_indirect(phydev->bus, MDIO_AN_EEE_ADV, MDIO_MMD_AN,
  930. phydev->addr, val);
  931. return 0;
  932. }
  933. EXPORT_SYMBOL(phy_ethtool_set_eee);
  934. int phy_ethtool_set_wol(struct phy_device *phydev, struct ethtool_wolinfo *wol)
  935. {
  936. if (phydev->drv->set_wol)
  937. return phydev->drv->set_wol(phydev, wol);
  938. return -EOPNOTSUPP;
  939. }
  940. EXPORT_SYMBOL(phy_ethtool_set_wol);
  941. void phy_ethtool_get_wol(struct phy_device *phydev, struct ethtool_wolinfo *wol)
  942. {
  943. if (phydev->drv->get_wol)
  944. phydev->drv->get_wol(phydev, wol);
  945. }
  946. EXPORT_SYMBOL(phy_ethtool_get_wol);