phy.c 37 KB

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