phy.c 30 KB

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