phy.c 34 KB

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