phy-core.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * Core PHY library, taken from phy.c
  4. */
  5. #include <linux/export.h>
  6. #include <linux/phy.h>
  7. #include <linux/of.h>
  8. const char *phy_speed_to_str(int speed)
  9. {
  10. BUILD_BUG_ON_MSG(__ETHTOOL_LINK_MODE_MASK_NBITS != 69,
  11. "Enum ethtool_link_mode_bit_indices and phylib are out of sync. "
  12. "If a speed or mode has been added please update phy_speed_to_str "
  13. "and the PHY settings array.\n");
  14. switch (speed) {
  15. case SPEED_10:
  16. return "10Mbps";
  17. case SPEED_100:
  18. return "100Mbps";
  19. case SPEED_1000:
  20. return "1Gbps";
  21. case SPEED_2500:
  22. return "2.5Gbps";
  23. case SPEED_5000:
  24. return "5Gbps";
  25. case SPEED_10000:
  26. return "10Gbps";
  27. case SPEED_14000:
  28. return "14Gbps";
  29. case SPEED_20000:
  30. return "20Gbps";
  31. case SPEED_25000:
  32. return "25Gbps";
  33. case SPEED_40000:
  34. return "40Gbps";
  35. case SPEED_50000:
  36. return "50Gbps";
  37. case SPEED_56000:
  38. return "56Gbps";
  39. case SPEED_100000:
  40. return "100Gbps";
  41. case SPEED_200000:
  42. return "200Gbps";
  43. case SPEED_UNKNOWN:
  44. return "Unknown";
  45. default:
  46. return "Unsupported (update phy-core.c)";
  47. }
  48. }
  49. EXPORT_SYMBOL_GPL(phy_speed_to_str);
  50. const char *phy_duplex_to_str(unsigned int duplex)
  51. {
  52. if (duplex == DUPLEX_HALF)
  53. return "Half";
  54. if (duplex == DUPLEX_FULL)
  55. return "Full";
  56. if (duplex == DUPLEX_UNKNOWN)
  57. return "Unknown";
  58. return "Unsupported (update phy-core.c)";
  59. }
  60. EXPORT_SYMBOL_GPL(phy_duplex_to_str);
  61. /* A mapping of all SUPPORTED settings to speed/duplex. This table
  62. * must be grouped by speed and sorted in descending match priority
  63. * - iow, descending speed. */
  64. #define PHY_SETTING(s, d, b) { .speed = SPEED_ ## s, .duplex = DUPLEX_ ## d, \
  65. .bit = ETHTOOL_LINK_MODE_ ## b ## _BIT}
  66. static const struct phy_setting settings[] = {
  67. /* 200G */
  68. PHY_SETTING( 200000, FULL, 200000baseCR4_Full ),
  69. PHY_SETTING( 200000, FULL, 200000baseKR4_Full ),
  70. PHY_SETTING( 200000, FULL, 200000baseLR4_ER4_FR4_Full ),
  71. PHY_SETTING( 200000, FULL, 200000baseDR4_Full ),
  72. PHY_SETTING( 200000, FULL, 200000baseSR4_Full ),
  73. /* 100G */
  74. PHY_SETTING( 100000, FULL, 100000baseCR4_Full ),
  75. PHY_SETTING( 100000, FULL, 100000baseKR4_Full ),
  76. PHY_SETTING( 100000, FULL, 100000baseLR4_ER4_Full ),
  77. PHY_SETTING( 100000, FULL, 100000baseSR4_Full ),
  78. PHY_SETTING( 100000, FULL, 100000baseCR2_Full ),
  79. PHY_SETTING( 100000, FULL, 100000baseKR2_Full ),
  80. PHY_SETTING( 100000, FULL, 100000baseLR2_ER2_FR2_Full ),
  81. PHY_SETTING( 100000, FULL, 100000baseDR2_Full ),
  82. PHY_SETTING( 100000, FULL, 100000baseSR2_Full ),
  83. /* 56G */
  84. PHY_SETTING( 56000, FULL, 56000baseCR4_Full ),
  85. PHY_SETTING( 56000, FULL, 56000baseKR4_Full ),
  86. PHY_SETTING( 56000, FULL, 56000baseLR4_Full ),
  87. PHY_SETTING( 56000, FULL, 56000baseSR4_Full ),
  88. /* 50G */
  89. PHY_SETTING( 50000, FULL, 50000baseCR2_Full ),
  90. PHY_SETTING( 50000, FULL, 50000baseKR2_Full ),
  91. PHY_SETTING( 50000, FULL, 50000baseSR2_Full ),
  92. PHY_SETTING( 50000, FULL, 50000baseCR_Full ),
  93. PHY_SETTING( 50000, FULL, 50000baseKR_Full ),
  94. PHY_SETTING( 50000, FULL, 50000baseLR_ER_FR_Full ),
  95. PHY_SETTING( 50000, FULL, 50000baseDR_Full ),
  96. PHY_SETTING( 50000, FULL, 50000baseSR_Full ),
  97. /* 40G */
  98. PHY_SETTING( 40000, FULL, 40000baseCR4_Full ),
  99. PHY_SETTING( 40000, FULL, 40000baseKR4_Full ),
  100. PHY_SETTING( 40000, FULL, 40000baseLR4_Full ),
  101. PHY_SETTING( 40000, FULL, 40000baseSR4_Full ),
  102. /* 25G */
  103. PHY_SETTING( 25000, FULL, 25000baseCR_Full ),
  104. PHY_SETTING( 25000, FULL, 25000baseKR_Full ),
  105. PHY_SETTING( 25000, FULL, 25000baseSR_Full ),
  106. /* 20G */
  107. PHY_SETTING( 20000, FULL, 20000baseKR2_Full ),
  108. PHY_SETTING( 20000, FULL, 20000baseMLD2_Full ),
  109. /* 10G */
  110. PHY_SETTING( 10000, FULL, 10000baseCR_Full ),
  111. PHY_SETTING( 10000, FULL, 10000baseER_Full ),
  112. PHY_SETTING( 10000, FULL, 10000baseKR_Full ),
  113. PHY_SETTING( 10000, FULL, 10000baseKX4_Full ),
  114. PHY_SETTING( 10000, FULL, 10000baseLR_Full ),
  115. PHY_SETTING( 10000, FULL, 10000baseLRM_Full ),
  116. PHY_SETTING( 10000, FULL, 10000baseR_FEC ),
  117. PHY_SETTING( 10000, FULL, 10000baseSR_Full ),
  118. PHY_SETTING( 10000, FULL, 10000baseT_Full ),
  119. /* 5G */
  120. PHY_SETTING( 5000, FULL, 5000baseT_Full ),
  121. /* 2.5G */
  122. PHY_SETTING( 2500, FULL, 2500baseT_Full ),
  123. PHY_SETTING( 2500, FULL, 2500baseX_Full ),
  124. /* 1G */
  125. PHY_SETTING( 1000, FULL, 1000baseKX_Full ),
  126. PHY_SETTING( 1000, FULL, 1000baseT_Full ),
  127. PHY_SETTING( 1000, HALF, 1000baseT_Half ),
  128. PHY_SETTING( 1000, FULL, 1000baseT1_Full ),
  129. PHY_SETTING( 1000, FULL, 1000baseX_Full ),
  130. /* 100M */
  131. PHY_SETTING( 100, FULL, 100baseT_Full ),
  132. PHY_SETTING( 100, FULL, 100baseT1_Full ),
  133. PHY_SETTING( 100, HALF, 100baseT_Half ),
  134. /* 10M */
  135. PHY_SETTING( 10, FULL, 10baseT_Full ),
  136. PHY_SETTING( 10, HALF, 10baseT_Half ),
  137. };
  138. #undef PHY_SETTING
  139. /**
  140. * phy_lookup_setting - lookup a PHY setting
  141. * @speed: speed to match
  142. * @duplex: duplex to match
  143. * @mask: allowed link modes
  144. * @exact: an exact match is required
  145. *
  146. * Search the settings array for a setting that matches the speed and
  147. * duplex, and which is supported.
  148. *
  149. * If @exact is unset, either an exact match or %NULL for no match will
  150. * be returned.
  151. *
  152. * If @exact is set, an exact match, the fastest supported setting at
  153. * or below the specified speed, the slowest supported setting, or if
  154. * they all fail, %NULL will be returned.
  155. */
  156. const struct phy_setting *
  157. phy_lookup_setting(int speed, int duplex, const unsigned long *mask, bool exact)
  158. {
  159. const struct phy_setting *p, *match = NULL, *last = NULL;
  160. int i;
  161. for (i = 0, p = settings; i < ARRAY_SIZE(settings); i++, p++) {
  162. if (p->bit < __ETHTOOL_LINK_MODE_MASK_NBITS &&
  163. test_bit(p->bit, mask)) {
  164. last = p;
  165. if (p->speed == speed && p->duplex == duplex) {
  166. /* Exact match for speed and duplex */
  167. match = p;
  168. break;
  169. } else if (!exact) {
  170. if (!match && p->speed <= speed)
  171. /* Candidate */
  172. match = p;
  173. if (p->speed < speed)
  174. break;
  175. }
  176. }
  177. }
  178. if (!match && !exact)
  179. match = last;
  180. return match;
  181. }
  182. EXPORT_SYMBOL_GPL(phy_lookup_setting);
  183. size_t phy_speeds(unsigned int *speeds, size_t size,
  184. unsigned long *mask)
  185. {
  186. size_t count;
  187. int i;
  188. for (i = 0, count = 0; i < ARRAY_SIZE(settings) && count < size; i++)
  189. if (settings[i].bit < __ETHTOOL_LINK_MODE_MASK_NBITS &&
  190. test_bit(settings[i].bit, mask) &&
  191. (count == 0 || speeds[count - 1] != settings[i].speed))
  192. speeds[count++] = settings[i].speed;
  193. return count;
  194. }
  195. static int __set_linkmode_max_speed(u32 max_speed, unsigned long *addr)
  196. {
  197. const struct phy_setting *p;
  198. int i;
  199. for (i = 0, p = settings; i < ARRAY_SIZE(settings); i++, p++) {
  200. if (p->speed > max_speed)
  201. linkmode_clear_bit(p->bit, addr);
  202. else
  203. break;
  204. }
  205. return 0;
  206. }
  207. static int __set_phy_supported(struct phy_device *phydev, u32 max_speed)
  208. {
  209. return __set_linkmode_max_speed(max_speed, phydev->supported);
  210. }
  211. int phy_set_max_speed(struct phy_device *phydev, u32 max_speed)
  212. {
  213. int err;
  214. err = __set_phy_supported(phydev, max_speed);
  215. if (err)
  216. return err;
  217. phy_advertise_supported(phydev);
  218. return 0;
  219. }
  220. EXPORT_SYMBOL(phy_set_max_speed);
  221. void of_set_phy_supported(struct phy_device *phydev)
  222. {
  223. struct device_node *node = phydev->mdio.dev.of_node;
  224. u32 max_speed;
  225. if (!IS_ENABLED(CONFIG_OF_MDIO))
  226. return;
  227. if (!node)
  228. return;
  229. if (!of_property_read_u32(node, "max-speed", &max_speed))
  230. __set_phy_supported(phydev, max_speed);
  231. }
  232. void of_set_phy_eee_broken(struct phy_device *phydev)
  233. {
  234. struct device_node *node = phydev->mdio.dev.of_node;
  235. u32 broken = 0;
  236. if (!IS_ENABLED(CONFIG_OF_MDIO))
  237. return;
  238. if (!node)
  239. return;
  240. if (of_property_read_bool(node, "eee-broken-100tx"))
  241. broken |= MDIO_EEE_100TX;
  242. if (of_property_read_bool(node, "eee-broken-1000t"))
  243. broken |= MDIO_EEE_1000T;
  244. if (of_property_read_bool(node, "eee-broken-10gt"))
  245. broken |= MDIO_EEE_10GT;
  246. if (of_property_read_bool(node, "eee-broken-1000kx"))
  247. broken |= MDIO_EEE_1000KX;
  248. if (of_property_read_bool(node, "eee-broken-10gkx4"))
  249. broken |= MDIO_EEE_10GKX4;
  250. if (of_property_read_bool(node, "eee-broken-10gkr"))
  251. broken |= MDIO_EEE_10GKR;
  252. phydev->eee_broken_modes = broken;
  253. }
  254. void phy_resolve_aneg_pause(struct phy_device *phydev)
  255. {
  256. if (phydev->duplex == DUPLEX_FULL) {
  257. phydev->pause = linkmode_test_bit(ETHTOOL_LINK_MODE_Pause_BIT,
  258. phydev->lp_advertising);
  259. phydev->asym_pause = linkmode_test_bit(
  260. ETHTOOL_LINK_MODE_Asym_Pause_BIT,
  261. phydev->lp_advertising);
  262. }
  263. }
  264. EXPORT_SYMBOL_GPL(phy_resolve_aneg_pause);
  265. /**
  266. * phy_resolve_aneg_linkmode - resolve the advertisements into phy settings
  267. * @phydev: The phy_device struct
  268. *
  269. * Resolve our and the link partner advertisements into their corresponding
  270. * speed and duplex. If full duplex was negotiated, extract the pause mode
  271. * from the link partner mask.
  272. */
  273. void phy_resolve_aneg_linkmode(struct phy_device *phydev)
  274. {
  275. __ETHTOOL_DECLARE_LINK_MODE_MASK(common);
  276. int i;
  277. linkmode_and(common, phydev->lp_advertising, phydev->advertising);
  278. for (i = 0; i < ARRAY_SIZE(settings); i++)
  279. if (test_bit(settings[i].bit, common)) {
  280. phydev->speed = settings[i].speed;
  281. phydev->duplex = settings[i].duplex;
  282. break;
  283. }
  284. phy_resolve_aneg_pause(phydev);
  285. }
  286. EXPORT_SYMBOL_GPL(phy_resolve_aneg_linkmode);
  287. static int phy_resolve_min_speed(struct phy_device *phydev, bool fdx_only)
  288. {
  289. __ETHTOOL_DECLARE_LINK_MODE_MASK(common);
  290. int i = ARRAY_SIZE(settings);
  291. linkmode_and(common, phydev->lp_advertising, phydev->advertising);
  292. while (--i >= 0) {
  293. if (test_bit(settings[i].bit, common)) {
  294. if (fdx_only && settings[i].duplex != DUPLEX_FULL)
  295. continue;
  296. return settings[i].speed;
  297. }
  298. }
  299. return SPEED_UNKNOWN;
  300. }
  301. int phy_speed_down_core(struct phy_device *phydev)
  302. {
  303. int min_common_speed = phy_resolve_min_speed(phydev, true);
  304. if (min_common_speed == SPEED_UNKNOWN)
  305. return -EINVAL;
  306. return __set_linkmode_max_speed(min_common_speed, phydev->advertising);
  307. }
  308. static void mmd_phy_indirect(struct mii_bus *bus, int phy_addr, int devad,
  309. u16 regnum)
  310. {
  311. /* Write the desired MMD Devad */
  312. __mdiobus_write(bus, phy_addr, MII_MMD_CTRL, devad);
  313. /* Write the desired MMD register address */
  314. __mdiobus_write(bus, phy_addr, MII_MMD_DATA, regnum);
  315. /* Select the Function : DATA with no post increment */
  316. __mdiobus_write(bus, phy_addr, MII_MMD_CTRL,
  317. devad | MII_MMD_CTRL_NOINCR);
  318. }
  319. /**
  320. * __phy_read_mmd - Convenience function for reading a register
  321. * from an MMD on a given PHY.
  322. * @phydev: The phy_device struct
  323. * @devad: The MMD to read from (0..31)
  324. * @regnum: The register on the MMD to read (0..65535)
  325. *
  326. * Same rules as for __phy_read();
  327. */
  328. int __phy_read_mmd(struct phy_device *phydev, int devad, u32 regnum)
  329. {
  330. int val;
  331. if (regnum > (u16)~0 || devad > 32)
  332. return -EINVAL;
  333. if (phydev->drv->read_mmd) {
  334. val = phydev->drv->read_mmd(phydev, devad, regnum);
  335. } else if (phydev->is_c45) {
  336. u32 addr = MII_ADDR_C45 | (devad << 16) | (regnum & 0xffff);
  337. val = __mdiobus_read(phydev->mdio.bus, phydev->mdio.addr, addr);
  338. } else {
  339. struct mii_bus *bus = phydev->mdio.bus;
  340. int phy_addr = phydev->mdio.addr;
  341. mmd_phy_indirect(bus, phy_addr, devad, regnum);
  342. /* Read the content of the MMD's selected register */
  343. val = __mdiobus_read(bus, phy_addr, MII_MMD_DATA);
  344. }
  345. return val;
  346. }
  347. EXPORT_SYMBOL(__phy_read_mmd);
  348. /**
  349. * phy_read_mmd - Convenience function for reading a register
  350. * from an MMD on a given PHY.
  351. * @phydev: The phy_device struct
  352. * @devad: The MMD to read from
  353. * @regnum: The register on the MMD to read
  354. *
  355. * Same rules as for phy_read();
  356. */
  357. int phy_read_mmd(struct phy_device *phydev, int devad, u32 regnum)
  358. {
  359. int ret;
  360. mutex_lock(&phydev->mdio.bus->mdio_lock);
  361. ret = __phy_read_mmd(phydev, devad, regnum);
  362. mutex_unlock(&phydev->mdio.bus->mdio_lock);
  363. return ret;
  364. }
  365. EXPORT_SYMBOL(phy_read_mmd);
  366. /**
  367. * __phy_write_mmd - Convenience function for writing a register
  368. * on an MMD on a given PHY.
  369. * @phydev: The phy_device struct
  370. * @devad: The MMD to read from
  371. * @regnum: The register on the MMD to read
  372. * @val: value to write to @regnum
  373. *
  374. * Same rules as for __phy_write();
  375. */
  376. int __phy_write_mmd(struct phy_device *phydev, int devad, u32 regnum, u16 val)
  377. {
  378. int ret;
  379. if (regnum > (u16)~0 || devad > 32)
  380. return -EINVAL;
  381. if (phydev->drv->write_mmd) {
  382. ret = phydev->drv->write_mmd(phydev, devad, regnum, val);
  383. } else if (phydev->is_c45) {
  384. u32 addr = MII_ADDR_C45 | (devad << 16) | (regnum & 0xffff);
  385. ret = __mdiobus_write(phydev->mdio.bus, phydev->mdio.addr,
  386. addr, val);
  387. } else {
  388. struct mii_bus *bus = phydev->mdio.bus;
  389. int phy_addr = phydev->mdio.addr;
  390. mmd_phy_indirect(bus, phy_addr, devad, regnum);
  391. /* Write the data into MMD's selected register */
  392. __mdiobus_write(bus, phy_addr, MII_MMD_DATA, val);
  393. ret = 0;
  394. }
  395. return ret;
  396. }
  397. EXPORT_SYMBOL(__phy_write_mmd);
  398. /**
  399. * phy_write_mmd - Convenience function for writing a register
  400. * on an MMD on a given PHY.
  401. * @phydev: The phy_device struct
  402. * @devad: The MMD to read from
  403. * @regnum: The register on the MMD to read
  404. * @val: value to write to @regnum
  405. *
  406. * Same rules as for phy_write();
  407. */
  408. int phy_write_mmd(struct phy_device *phydev, int devad, u32 regnum, u16 val)
  409. {
  410. int ret;
  411. mutex_lock(&phydev->mdio.bus->mdio_lock);
  412. ret = __phy_write_mmd(phydev, devad, regnum, val);
  413. mutex_unlock(&phydev->mdio.bus->mdio_lock);
  414. return ret;
  415. }
  416. EXPORT_SYMBOL(phy_write_mmd);
  417. /**
  418. * __phy_modify_changed() - Convenience function for modifying a PHY register
  419. * @phydev: a pointer to a &struct phy_device
  420. * @regnum: register number
  421. * @mask: bit mask of bits to clear
  422. * @set: bit mask of bits to set
  423. *
  424. * Unlocked helper function which allows a PHY register to be modified as
  425. * new register value = (old register value & ~mask) | set
  426. *
  427. * Returns negative errno, 0 if there was no change, and 1 in case of change
  428. */
  429. int __phy_modify_changed(struct phy_device *phydev, u32 regnum, u16 mask,
  430. u16 set)
  431. {
  432. int new, ret;
  433. ret = __phy_read(phydev, regnum);
  434. if (ret < 0)
  435. return ret;
  436. new = (ret & ~mask) | set;
  437. if (new == ret)
  438. return 0;
  439. ret = __phy_write(phydev, regnum, new);
  440. return ret < 0 ? ret : 1;
  441. }
  442. EXPORT_SYMBOL_GPL(__phy_modify_changed);
  443. /**
  444. * phy_modify_changed - Function for modifying a PHY register
  445. * @phydev: the phy_device struct
  446. * @regnum: register number to modify
  447. * @mask: bit mask of bits to clear
  448. * @set: new value of bits set in mask to write to @regnum
  449. *
  450. * NOTE: MUST NOT be called from interrupt context,
  451. * because the bus read/write functions may wait for an interrupt
  452. * to conclude the operation.
  453. *
  454. * Returns negative errno, 0 if there was no change, and 1 in case of change
  455. */
  456. int phy_modify_changed(struct phy_device *phydev, u32 regnum, u16 mask, u16 set)
  457. {
  458. int ret;
  459. mutex_lock(&phydev->mdio.bus->mdio_lock);
  460. ret = __phy_modify_changed(phydev, regnum, mask, set);
  461. mutex_unlock(&phydev->mdio.bus->mdio_lock);
  462. return ret;
  463. }
  464. EXPORT_SYMBOL_GPL(phy_modify_changed);
  465. /**
  466. * __phy_modify - Convenience function for modifying a PHY register
  467. * @phydev: the phy_device struct
  468. * @regnum: register number to modify
  469. * @mask: bit mask of bits to clear
  470. * @set: new value of bits set in mask to write to @regnum
  471. *
  472. * NOTE: MUST NOT be called from interrupt context,
  473. * because the bus read/write functions may wait for an interrupt
  474. * to conclude the operation.
  475. */
  476. int __phy_modify(struct phy_device *phydev, u32 regnum, u16 mask, u16 set)
  477. {
  478. int ret;
  479. ret = __phy_modify_changed(phydev, regnum, mask, set);
  480. return ret < 0 ? ret : 0;
  481. }
  482. EXPORT_SYMBOL_GPL(__phy_modify);
  483. /**
  484. * phy_modify - Convenience function for modifying a given PHY register
  485. * @phydev: the phy_device struct
  486. * @regnum: register number to write
  487. * @mask: bit mask of bits to clear
  488. * @set: new value of bits set in mask to write to @regnum
  489. *
  490. * NOTE: MUST NOT be called from interrupt context,
  491. * because the bus read/write functions may wait for an interrupt
  492. * to conclude the operation.
  493. */
  494. int phy_modify(struct phy_device *phydev, u32 regnum, u16 mask, u16 set)
  495. {
  496. int ret;
  497. mutex_lock(&phydev->mdio.bus->mdio_lock);
  498. ret = __phy_modify(phydev, regnum, mask, set);
  499. mutex_unlock(&phydev->mdio.bus->mdio_lock);
  500. return ret;
  501. }
  502. EXPORT_SYMBOL_GPL(phy_modify);
  503. /**
  504. * __phy_modify_mmd_changed - Function for modifying a register on MMD
  505. * @phydev: the phy_device struct
  506. * @devad: the MMD containing register to modify
  507. * @regnum: register number to modify
  508. * @mask: bit mask of bits to clear
  509. * @set: new value of bits set in mask to write to @regnum
  510. *
  511. * Unlocked helper function which allows a MMD register to be modified as
  512. * new register value = (old register value & ~mask) | set
  513. *
  514. * Returns negative errno, 0 if there was no change, and 1 in case of change
  515. */
  516. int __phy_modify_mmd_changed(struct phy_device *phydev, int devad, u32 regnum,
  517. u16 mask, u16 set)
  518. {
  519. int new, ret;
  520. ret = __phy_read_mmd(phydev, devad, regnum);
  521. if (ret < 0)
  522. return ret;
  523. new = (ret & ~mask) | set;
  524. if (new == ret)
  525. return 0;
  526. ret = __phy_write_mmd(phydev, devad, regnum, new);
  527. return ret < 0 ? ret : 1;
  528. }
  529. EXPORT_SYMBOL_GPL(__phy_modify_mmd_changed);
  530. /**
  531. * phy_modify_mmd_changed - Function for modifying a register on MMD
  532. * @phydev: the phy_device struct
  533. * @devad: the MMD containing register to modify
  534. * @regnum: register number to modify
  535. * @mask: bit mask of bits to clear
  536. * @set: new value of bits set in mask to write to @regnum
  537. *
  538. * NOTE: MUST NOT be called from interrupt context,
  539. * because the bus read/write functions may wait for an interrupt
  540. * to conclude the operation.
  541. *
  542. * Returns negative errno, 0 if there was no change, and 1 in case of change
  543. */
  544. int phy_modify_mmd_changed(struct phy_device *phydev, int devad, u32 regnum,
  545. u16 mask, u16 set)
  546. {
  547. int ret;
  548. mutex_lock(&phydev->mdio.bus->mdio_lock);
  549. ret = __phy_modify_mmd_changed(phydev, devad, regnum, mask, set);
  550. mutex_unlock(&phydev->mdio.bus->mdio_lock);
  551. return ret;
  552. }
  553. EXPORT_SYMBOL_GPL(phy_modify_mmd_changed);
  554. /**
  555. * __phy_modify_mmd - Convenience function for modifying a register on MMD
  556. * @phydev: the phy_device struct
  557. * @devad: the MMD containing register to modify
  558. * @regnum: register number to modify
  559. * @mask: bit mask of bits to clear
  560. * @set: new value of bits set in mask to write to @regnum
  561. *
  562. * NOTE: MUST NOT be called from interrupt context,
  563. * because the bus read/write functions may wait for an interrupt
  564. * to conclude the operation.
  565. */
  566. int __phy_modify_mmd(struct phy_device *phydev, int devad, u32 regnum,
  567. u16 mask, u16 set)
  568. {
  569. int ret;
  570. ret = __phy_modify_mmd_changed(phydev, devad, regnum, mask, set);
  571. return ret < 0 ? ret : 0;
  572. }
  573. EXPORT_SYMBOL_GPL(__phy_modify_mmd);
  574. /**
  575. * phy_modify_mmd - Convenience function for modifying a register on MMD
  576. * @phydev: the phy_device struct
  577. * @devad: the MMD containing register to modify
  578. * @regnum: register number to modify
  579. * @mask: bit mask of bits to clear
  580. * @set: new value of bits set in mask to write to @regnum
  581. *
  582. * NOTE: MUST NOT be called from interrupt context,
  583. * because the bus read/write functions may wait for an interrupt
  584. * to conclude the operation.
  585. */
  586. int phy_modify_mmd(struct phy_device *phydev, int devad, u32 regnum,
  587. u16 mask, u16 set)
  588. {
  589. int ret;
  590. mutex_lock(&phydev->mdio.bus->mdio_lock);
  591. ret = __phy_modify_mmd(phydev, devad, regnum, mask, set);
  592. mutex_unlock(&phydev->mdio.bus->mdio_lock);
  593. return ret;
  594. }
  595. EXPORT_SYMBOL_GPL(phy_modify_mmd);
  596. static int __phy_read_page(struct phy_device *phydev)
  597. {
  598. return phydev->drv->read_page(phydev);
  599. }
  600. static int __phy_write_page(struct phy_device *phydev, int page)
  601. {
  602. return phydev->drv->write_page(phydev, page);
  603. }
  604. /**
  605. * phy_save_page() - take the bus lock and save the current page
  606. * @phydev: a pointer to a &struct phy_device
  607. *
  608. * Take the MDIO bus lock, and return the current page number. On error,
  609. * returns a negative errno. phy_restore_page() must always be called
  610. * after this, irrespective of success or failure of this call.
  611. */
  612. int phy_save_page(struct phy_device *phydev)
  613. {
  614. mutex_lock(&phydev->mdio.bus->mdio_lock);
  615. return __phy_read_page(phydev);
  616. }
  617. EXPORT_SYMBOL_GPL(phy_save_page);
  618. /**
  619. * phy_select_page() - take the bus lock, save the current page, and set a page
  620. * @phydev: a pointer to a &struct phy_device
  621. * @page: desired page
  622. *
  623. * Take the MDIO bus lock to protect against concurrent access, save the
  624. * current PHY page, and set the current page. On error, returns a
  625. * negative errno, otherwise returns the previous page number.
  626. * phy_restore_page() must always be called after this, irrespective
  627. * of success or failure of this call.
  628. */
  629. int phy_select_page(struct phy_device *phydev, int page)
  630. {
  631. int ret, oldpage;
  632. oldpage = ret = phy_save_page(phydev);
  633. if (ret < 0)
  634. return ret;
  635. if (oldpage != page) {
  636. ret = __phy_write_page(phydev, page);
  637. if (ret < 0)
  638. return ret;
  639. }
  640. return oldpage;
  641. }
  642. EXPORT_SYMBOL_GPL(phy_select_page);
  643. /**
  644. * phy_restore_page() - restore the page register and release the bus lock
  645. * @phydev: a pointer to a &struct phy_device
  646. * @oldpage: the old page, return value from phy_save_page() or phy_select_page()
  647. * @ret: operation's return code
  648. *
  649. * Release the MDIO bus lock, restoring @oldpage if it is a valid page.
  650. * This function propagates the earliest error code from the group of
  651. * operations.
  652. *
  653. * Returns:
  654. * @oldpage if it was a negative value, otherwise
  655. * @ret if it was a negative errno value, otherwise
  656. * phy_write_page()'s negative value if it were in error, otherwise
  657. * @ret.
  658. */
  659. int phy_restore_page(struct phy_device *phydev, int oldpage, int ret)
  660. {
  661. int r;
  662. if (oldpage >= 0) {
  663. r = __phy_write_page(phydev, oldpage);
  664. /* Propagate the operation return code if the page write
  665. * was successful.
  666. */
  667. if (ret >= 0 && r < 0)
  668. ret = r;
  669. } else {
  670. /* Propagate the phy page selection error code */
  671. ret = oldpage;
  672. }
  673. mutex_unlock(&phydev->mdio.bus->mdio_lock);
  674. return ret;
  675. }
  676. EXPORT_SYMBOL_GPL(phy_restore_page);
  677. /**
  678. * phy_read_paged() - Convenience function for reading a paged register
  679. * @phydev: a pointer to a &struct phy_device
  680. * @page: the page for the phy
  681. * @regnum: register number
  682. *
  683. * Same rules as for phy_read().
  684. */
  685. int phy_read_paged(struct phy_device *phydev, int page, u32 regnum)
  686. {
  687. int ret = 0, oldpage;
  688. oldpage = phy_select_page(phydev, page);
  689. if (oldpage >= 0)
  690. ret = __phy_read(phydev, regnum);
  691. return phy_restore_page(phydev, oldpage, ret);
  692. }
  693. EXPORT_SYMBOL(phy_read_paged);
  694. /**
  695. * phy_write_paged() - Convenience function for writing a paged register
  696. * @phydev: a pointer to a &struct phy_device
  697. * @page: the page for the phy
  698. * @regnum: register number
  699. * @val: value to write
  700. *
  701. * Same rules as for phy_write().
  702. */
  703. int phy_write_paged(struct phy_device *phydev, int page, u32 regnum, u16 val)
  704. {
  705. int ret = 0, oldpage;
  706. oldpage = phy_select_page(phydev, page);
  707. if (oldpage >= 0)
  708. ret = __phy_write(phydev, regnum, val);
  709. return phy_restore_page(phydev, oldpage, ret);
  710. }
  711. EXPORT_SYMBOL(phy_write_paged);
  712. /**
  713. * phy_modify_paged_changed() - Function for modifying a paged register
  714. * @phydev: a pointer to a &struct phy_device
  715. * @page: the page for the phy
  716. * @regnum: register number
  717. * @mask: bit mask of bits to clear
  718. * @set: bit mask of bits to set
  719. *
  720. * Returns negative errno, 0 if there was no change, and 1 in case of change
  721. */
  722. int phy_modify_paged_changed(struct phy_device *phydev, int page, u32 regnum,
  723. u16 mask, u16 set)
  724. {
  725. int ret = 0, oldpage;
  726. oldpage = phy_select_page(phydev, page);
  727. if (oldpage >= 0)
  728. ret = __phy_modify_changed(phydev, regnum, mask, set);
  729. return phy_restore_page(phydev, oldpage, ret);
  730. }
  731. EXPORT_SYMBOL(phy_modify_paged_changed);
  732. /**
  733. * phy_modify_paged() - Convenience function for modifying a paged register
  734. * @phydev: a pointer to a &struct phy_device
  735. * @page: the page for the phy
  736. * @regnum: register number
  737. * @mask: bit mask of bits to clear
  738. * @set: bit mask of bits to set
  739. *
  740. * Same rules as for phy_read() and phy_write().
  741. */
  742. int phy_modify_paged(struct phy_device *phydev, int page, u32 regnum,
  743. u16 mask, u16 set)
  744. {
  745. int ret = phy_modify_paged_changed(phydev, page, regnum, mask, set);
  746. return ret < 0 ? ret : 0;
  747. }
  748. EXPORT_SYMBOL(phy_modify_paged);