phy-core.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529
  1. /*
  2. * Core PHY library, taken from phy.c
  3. *
  4. * This program is free software; you can redistribute it and/or modify it
  5. * under the terms of the GNU General Public License as published by the
  6. * Free Software Foundation; either version 2 of the License, or (at your
  7. * option) any later version.
  8. */
  9. #include <linux/export.h>
  10. #include <linux/phy.h>
  11. const char *phy_speed_to_str(int speed)
  12. {
  13. switch (speed) {
  14. case SPEED_10:
  15. return "10Mbps";
  16. case SPEED_100:
  17. return "100Mbps";
  18. case SPEED_1000:
  19. return "1Gbps";
  20. case SPEED_2500:
  21. return "2.5Gbps";
  22. case SPEED_5000:
  23. return "5Gbps";
  24. case SPEED_10000:
  25. return "10Gbps";
  26. case SPEED_14000:
  27. return "14Gbps";
  28. case SPEED_20000:
  29. return "20Gbps";
  30. case SPEED_25000:
  31. return "25Gbps";
  32. case SPEED_40000:
  33. return "40Gbps";
  34. case SPEED_50000:
  35. return "50Gbps";
  36. case SPEED_56000:
  37. return "56Gbps";
  38. case SPEED_100000:
  39. return "100Gbps";
  40. case SPEED_UNKNOWN:
  41. return "Unknown";
  42. default:
  43. return "Unsupported (update phy-core.c)";
  44. }
  45. }
  46. EXPORT_SYMBOL_GPL(phy_speed_to_str);
  47. const char *phy_duplex_to_str(unsigned int duplex)
  48. {
  49. if (duplex == DUPLEX_HALF)
  50. return "Half";
  51. if (duplex == DUPLEX_FULL)
  52. return "Full";
  53. if (duplex == DUPLEX_UNKNOWN)
  54. return "Unknown";
  55. return "Unsupported (update phy-core.c)";
  56. }
  57. EXPORT_SYMBOL_GPL(phy_duplex_to_str);
  58. /* A mapping of all SUPPORTED settings to speed/duplex. This table
  59. * must be grouped by speed and sorted in descending match priority
  60. * - iow, descending speed. */
  61. static const struct phy_setting settings[] = {
  62. {
  63. .speed = SPEED_10000,
  64. .duplex = DUPLEX_FULL,
  65. .bit = ETHTOOL_LINK_MODE_10000baseKR_Full_BIT,
  66. },
  67. {
  68. .speed = SPEED_10000,
  69. .duplex = DUPLEX_FULL,
  70. .bit = ETHTOOL_LINK_MODE_10000baseKX4_Full_BIT,
  71. },
  72. {
  73. .speed = SPEED_10000,
  74. .duplex = DUPLEX_FULL,
  75. .bit = ETHTOOL_LINK_MODE_10000baseT_Full_BIT,
  76. },
  77. {
  78. .speed = SPEED_2500,
  79. .duplex = DUPLEX_FULL,
  80. .bit = ETHTOOL_LINK_MODE_2500baseX_Full_BIT,
  81. },
  82. {
  83. .speed = SPEED_1000,
  84. .duplex = DUPLEX_FULL,
  85. .bit = ETHTOOL_LINK_MODE_1000baseKX_Full_BIT,
  86. },
  87. {
  88. .speed = SPEED_1000,
  89. .duplex = DUPLEX_FULL,
  90. .bit = ETHTOOL_LINK_MODE_1000baseX_Full_BIT,
  91. },
  92. {
  93. .speed = SPEED_1000,
  94. .duplex = DUPLEX_FULL,
  95. .bit = ETHTOOL_LINK_MODE_1000baseT_Full_BIT,
  96. },
  97. {
  98. .speed = SPEED_1000,
  99. .duplex = DUPLEX_HALF,
  100. .bit = ETHTOOL_LINK_MODE_1000baseT_Half_BIT,
  101. },
  102. {
  103. .speed = SPEED_100,
  104. .duplex = DUPLEX_FULL,
  105. .bit = ETHTOOL_LINK_MODE_100baseT_Full_BIT,
  106. },
  107. {
  108. .speed = SPEED_100,
  109. .duplex = DUPLEX_HALF,
  110. .bit = ETHTOOL_LINK_MODE_100baseT_Half_BIT,
  111. },
  112. {
  113. .speed = SPEED_10,
  114. .duplex = DUPLEX_FULL,
  115. .bit = ETHTOOL_LINK_MODE_10baseT_Full_BIT,
  116. },
  117. {
  118. .speed = SPEED_10,
  119. .duplex = DUPLEX_HALF,
  120. .bit = ETHTOOL_LINK_MODE_10baseT_Half_BIT,
  121. },
  122. };
  123. /**
  124. * phy_lookup_setting - lookup a PHY setting
  125. * @speed: speed to match
  126. * @duplex: duplex to match
  127. * @mask: allowed link modes
  128. * @maxbit: bit size of link modes
  129. * @exact: an exact match is required
  130. *
  131. * Search the settings array for a setting that matches the speed and
  132. * duplex, and which is supported.
  133. *
  134. * If @exact is unset, either an exact match or %NULL for no match will
  135. * be returned.
  136. *
  137. * If @exact is set, an exact match, the fastest supported setting at
  138. * or below the specified speed, the slowest supported setting, or if
  139. * they all fail, %NULL will be returned.
  140. */
  141. const struct phy_setting *
  142. phy_lookup_setting(int speed, int duplex, const unsigned long *mask,
  143. size_t maxbit, bool exact)
  144. {
  145. const struct phy_setting *p, *match = NULL, *last = NULL;
  146. int i;
  147. for (i = 0, p = settings; i < ARRAY_SIZE(settings); i++, p++) {
  148. if (p->bit < maxbit && test_bit(p->bit, mask)) {
  149. last = p;
  150. if (p->speed == speed && p->duplex == duplex) {
  151. /* Exact match for speed and duplex */
  152. match = p;
  153. break;
  154. } else if (!exact) {
  155. if (!match && p->speed <= speed)
  156. /* Candidate */
  157. match = p;
  158. if (p->speed < speed)
  159. break;
  160. }
  161. }
  162. }
  163. if (!match && !exact)
  164. match = last;
  165. return match;
  166. }
  167. EXPORT_SYMBOL_GPL(phy_lookup_setting);
  168. size_t phy_speeds(unsigned int *speeds, size_t size,
  169. unsigned long *mask, size_t maxbit)
  170. {
  171. size_t count;
  172. int i;
  173. for (i = 0, count = 0; i < ARRAY_SIZE(settings) && count < size; i++)
  174. if (settings[i].bit < maxbit &&
  175. test_bit(settings[i].bit, mask) &&
  176. (count == 0 || speeds[count - 1] != settings[i].speed))
  177. speeds[count++] = settings[i].speed;
  178. return count;
  179. }
  180. /**
  181. * phy_resolve_aneg_linkmode - resolve the advertisements into phy settings
  182. * @phydev: The phy_device struct
  183. *
  184. * Resolve our and the link partner advertisements into their corresponding
  185. * speed and duplex. If full duplex was negotiated, extract the pause mode
  186. * from the link partner mask.
  187. */
  188. void phy_resolve_aneg_linkmode(struct phy_device *phydev)
  189. {
  190. u32 common = phydev->lp_advertising & phydev->advertising;
  191. if (common & ADVERTISED_10000baseT_Full) {
  192. phydev->speed = SPEED_10000;
  193. phydev->duplex = DUPLEX_FULL;
  194. } else if (common & ADVERTISED_1000baseT_Full) {
  195. phydev->speed = SPEED_1000;
  196. phydev->duplex = DUPLEX_FULL;
  197. } else if (common & ADVERTISED_1000baseT_Half) {
  198. phydev->speed = SPEED_1000;
  199. phydev->duplex = DUPLEX_HALF;
  200. } else if (common & ADVERTISED_100baseT_Full) {
  201. phydev->speed = SPEED_100;
  202. phydev->duplex = DUPLEX_FULL;
  203. } else if (common & ADVERTISED_100baseT_Half) {
  204. phydev->speed = SPEED_100;
  205. phydev->duplex = DUPLEX_HALF;
  206. } else if (common & ADVERTISED_10baseT_Full) {
  207. phydev->speed = SPEED_10;
  208. phydev->duplex = DUPLEX_FULL;
  209. } else if (common & ADVERTISED_10baseT_Half) {
  210. phydev->speed = SPEED_10;
  211. phydev->duplex = DUPLEX_HALF;
  212. }
  213. if (phydev->duplex == DUPLEX_FULL) {
  214. phydev->pause = !!(phydev->lp_advertising & ADVERTISED_Pause);
  215. phydev->asym_pause = !!(phydev->lp_advertising &
  216. ADVERTISED_Asym_Pause);
  217. }
  218. }
  219. EXPORT_SYMBOL_GPL(phy_resolve_aneg_linkmode);
  220. static void mmd_phy_indirect(struct mii_bus *bus, int phy_addr, int devad,
  221. u16 regnum)
  222. {
  223. /* Write the desired MMD Devad */
  224. __mdiobus_write(bus, phy_addr, MII_MMD_CTRL, devad);
  225. /* Write the desired MMD register address */
  226. __mdiobus_write(bus, phy_addr, MII_MMD_DATA, regnum);
  227. /* Select the Function : DATA with no post increment */
  228. __mdiobus_write(bus, phy_addr, MII_MMD_CTRL,
  229. devad | MII_MMD_CTRL_NOINCR);
  230. }
  231. /**
  232. * phy_read_mmd - Convenience function for reading a register
  233. * from an MMD on a given PHY.
  234. * @phydev: The phy_device struct
  235. * @devad: The MMD to read from (0..31)
  236. * @regnum: The register on the MMD to read (0..65535)
  237. *
  238. * Same rules as for phy_read();
  239. */
  240. int phy_read_mmd(struct phy_device *phydev, int devad, u32 regnum)
  241. {
  242. int val;
  243. if (regnum > (u16)~0 || devad > 32)
  244. return -EINVAL;
  245. if (phydev->drv->read_mmd) {
  246. val = phydev->drv->read_mmd(phydev, devad, regnum);
  247. } else if (phydev->is_c45) {
  248. u32 addr = MII_ADDR_C45 | (devad << 16) | (regnum & 0xffff);
  249. val = mdiobus_read(phydev->mdio.bus, phydev->mdio.addr, addr);
  250. } else {
  251. struct mii_bus *bus = phydev->mdio.bus;
  252. int phy_addr = phydev->mdio.addr;
  253. mutex_lock(&bus->mdio_lock);
  254. mmd_phy_indirect(bus, phy_addr, devad, regnum);
  255. /* Read the content of the MMD's selected register */
  256. val = __mdiobus_read(bus, phy_addr, MII_MMD_DATA);
  257. mutex_unlock(&bus->mdio_lock);
  258. }
  259. return val;
  260. }
  261. EXPORT_SYMBOL(phy_read_mmd);
  262. /**
  263. * phy_write_mmd - Convenience function for writing a register
  264. * on an MMD on a given PHY.
  265. * @phydev: The phy_device struct
  266. * @devad: The MMD to read from
  267. * @regnum: The register on the MMD to read
  268. * @val: value to write to @regnum
  269. *
  270. * Same rules as for phy_write();
  271. */
  272. int phy_write_mmd(struct phy_device *phydev, int devad, u32 regnum, u16 val)
  273. {
  274. int ret;
  275. if (regnum > (u16)~0 || devad > 32)
  276. return -EINVAL;
  277. if (phydev->drv->write_mmd) {
  278. ret = phydev->drv->write_mmd(phydev, devad, regnum, val);
  279. } else if (phydev->is_c45) {
  280. u32 addr = MII_ADDR_C45 | (devad << 16) | (regnum & 0xffff);
  281. ret = mdiobus_write(phydev->mdio.bus, phydev->mdio.addr,
  282. addr, val);
  283. } else {
  284. struct mii_bus *bus = phydev->mdio.bus;
  285. int phy_addr = phydev->mdio.addr;
  286. mutex_lock(&bus->mdio_lock);
  287. mmd_phy_indirect(bus, phy_addr, devad, regnum);
  288. /* Write the data into MMD's selected register */
  289. __mdiobus_write(bus, phy_addr, MII_MMD_DATA, val);
  290. mutex_unlock(&bus->mdio_lock);
  291. ret = 0;
  292. }
  293. return ret;
  294. }
  295. EXPORT_SYMBOL(phy_write_mmd);
  296. /**
  297. * __phy_modify() - Convenience function for modifying a PHY register
  298. * @phydev: a pointer to a &struct phy_device
  299. * @regnum: register number
  300. * @mask: bit mask of bits to clear
  301. * @set: bit mask of bits to set
  302. *
  303. * Unlocked helper function which allows a PHY register to be modified as
  304. * new register value = (old register value & ~mask) | set
  305. */
  306. int __phy_modify(struct phy_device *phydev, u32 regnum, u16 mask, u16 set)
  307. {
  308. int ret;
  309. ret = __phy_read(phydev, regnum);
  310. if (ret < 0)
  311. return ret;
  312. ret = __phy_write(phydev, regnum, (ret & ~mask) | set);
  313. return ret < 0 ? ret : 0;
  314. }
  315. EXPORT_SYMBOL_GPL(__phy_modify);
  316. /**
  317. * phy_modify - Convenience function for modifying a given PHY register
  318. * @phydev: the phy_device struct
  319. * @regnum: register number to write
  320. * @mask: bit mask of bits to clear
  321. * @set: new value of bits set in mask to write to @regnum
  322. *
  323. * NOTE: MUST NOT be called from interrupt context,
  324. * because the bus read/write functions may wait for an interrupt
  325. * to conclude the operation.
  326. */
  327. int phy_modify(struct phy_device *phydev, u32 regnum, u16 mask, u16 set)
  328. {
  329. int ret;
  330. mutex_lock(&phydev->mdio.bus->mdio_lock);
  331. ret = __phy_modify(phydev, regnum, mask, set);
  332. mutex_unlock(&phydev->mdio.bus->mdio_lock);
  333. return ret;
  334. }
  335. EXPORT_SYMBOL_GPL(phy_modify);
  336. static int __phy_read_page(struct phy_device *phydev)
  337. {
  338. return phydev->drv->read_page(phydev);
  339. }
  340. static int __phy_write_page(struct phy_device *phydev, int page)
  341. {
  342. return phydev->drv->write_page(phydev, page);
  343. }
  344. /**
  345. * phy_save_page() - take the bus lock and save the current page
  346. * @phydev: a pointer to a &struct phy_device
  347. *
  348. * Take the MDIO bus lock, and return the current page number. On error,
  349. * returns a negative errno. phy_restore_page() must always be called
  350. * after this, irrespective of success or failure of this call.
  351. */
  352. int phy_save_page(struct phy_device *phydev)
  353. {
  354. mutex_lock(&phydev->mdio.bus->mdio_lock);
  355. return __phy_read_page(phydev);
  356. }
  357. EXPORT_SYMBOL_GPL(phy_save_page);
  358. /**
  359. * phy_select_page() - take the bus lock, save the current page, and set a page
  360. * @phydev: a pointer to a &struct phy_device
  361. * @page: desired page
  362. *
  363. * Take the MDIO bus lock to protect against concurrent access, save the
  364. * current PHY page, and set the current page. On error, returns a
  365. * negative errno, otherwise returns the previous page number.
  366. * phy_restore_page() must always be called after this, irrespective
  367. * of success or failure of this call.
  368. */
  369. int phy_select_page(struct phy_device *phydev, int page)
  370. {
  371. int ret, oldpage;
  372. oldpage = ret = phy_save_page(phydev);
  373. if (ret < 0)
  374. return ret;
  375. if (oldpage != page) {
  376. ret = __phy_write_page(phydev, page);
  377. if (ret < 0)
  378. return ret;
  379. }
  380. return oldpage;
  381. }
  382. EXPORT_SYMBOL_GPL(phy_select_page);
  383. /**
  384. * phy_restore_page() - restore the page register and release the bus lock
  385. * @phydev: a pointer to a &struct phy_device
  386. * @oldpage: the old page, return value from phy_save_page() or phy_select_page()
  387. * @ret: operation's return code
  388. *
  389. * Release the MDIO bus lock, restoring @oldpage if it is a valid page.
  390. * This function propagates the earliest error code from the group of
  391. * operations.
  392. *
  393. * Returns:
  394. * @oldpage if it was a negative value, otherwise
  395. * @ret if it was a negative errno value, otherwise
  396. * phy_write_page()'s negative value if it were in error, otherwise
  397. * @ret.
  398. */
  399. int phy_restore_page(struct phy_device *phydev, int oldpage, int ret)
  400. {
  401. int r;
  402. if (oldpage >= 0) {
  403. r = __phy_write_page(phydev, oldpage);
  404. /* Propagate the operation return code if the page write
  405. * was successful.
  406. */
  407. if (ret >= 0 && r < 0)
  408. ret = r;
  409. } else {
  410. /* Propagate the phy page selection error code */
  411. ret = oldpage;
  412. }
  413. mutex_unlock(&phydev->mdio.bus->mdio_lock);
  414. return ret;
  415. }
  416. EXPORT_SYMBOL_GPL(phy_restore_page);
  417. /**
  418. * phy_read_paged() - Convenience function for reading a paged register
  419. * @phydev: a pointer to a &struct phy_device
  420. * @page: the page for the phy
  421. * @regnum: register number
  422. *
  423. * Same rules as for phy_read().
  424. */
  425. int phy_read_paged(struct phy_device *phydev, int page, u32 regnum)
  426. {
  427. int ret = 0, oldpage;
  428. oldpage = phy_select_page(phydev, page);
  429. if (oldpage >= 0)
  430. ret = __phy_read(phydev, regnum);
  431. return phy_restore_page(phydev, oldpage, ret);
  432. }
  433. EXPORT_SYMBOL(phy_read_paged);
  434. /**
  435. * phy_write_paged() - Convenience function for writing a paged register
  436. * @phydev: a pointer to a &struct phy_device
  437. * @page: the page for the phy
  438. * @regnum: register number
  439. * @val: value to write
  440. *
  441. * Same rules as for phy_write().
  442. */
  443. int phy_write_paged(struct phy_device *phydev, int page, u32 regnum, u16 val)
  444. {
  445. int ret = 0, oldpage;
  446. oldpage = phy_select_page(phydev, page);
  447. if (oldpage >= 0)
  448. ret = __phy_write(phydev, regnum, val);
  449. return phy_restore_page(phydev, oldpage, ret);
  450. }
  451. EXPORT_SYMBOL(phy_write_paged);
  452. /**
  453. * phy_modify_paged() - Convenience function for modifying a paged register
  454. * @phydev: a pointer to a &struct phy_device
  455. * @page: the page for the phy
  456. * @regnum: register number
  457. * @mask: bit mask of bits to clear
  458. * @set: bit mask of bits to set
  459. *
  460. * Same rules as for phy_read() and phy_write().
  461. */
  462. int phy_modify_paged(struct phy_device *phydev, int page, u32 regnum,
  463. u16 mask, u16 set)
  464. {
  465. int ret = 0, oldpage;
  466. oldpage = phy_select_page(phydev, page);
  467. if (oldpage >= 0)
  468. ret = __phy_modify(phydev, regnum, mask, set);
  469. return phy_restore_page(phydev, oldpage, ret);
  470. }
  471. EXPORT_SYMBOL(phy_modify_paged);