phy.c 28 KB

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