phy.c 36 KB

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