phy-core.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903
  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/phy_port.h>
  8. #include <linux/of.h>
  9. #include "phylib.h"
  10. #include "phylib-internal.h"
  11. #include "phy-caps.h"
  12. /**
  13. * phy_speed_to_str - Return a string representing the PHY link speed
  14. *
  15. * @speed: Speed of the link
  16. */
  17. const char *phy_speed_to_str(int speed)
  18. {
  19. BUILD_BUG_ON_MSG(__ETHTOOL_LINK_MODE_MASK_NBITS != 125,
  20. "Enum ethtool_link_mode_bit_indices and phylib are out of sync. "
  21. "If a speed or mode has been added please update phy_speed_to_str "
  22. "and the PHY settings array.\n");
  23. switch (speed) {
  24. case SPEED_10:
  25. return "10Mbps";
  26. case SPEED_100:
  27. return "100Mbps";
  28. case SPEED_1000:
  29. return "1Gbps";
  30. case SPEED_2500:
  31. return "2.5Gbps";
  32. case SPEED_5000:
  33. return "5Gbps";
  34. case SPEED_10000:
  35. return "10Gbps";
  36. case SPEED_14000:
  37. return "14Gbps";
  38. case SPEED_20000:
  39. return "20Gbps";
  40. case SPEED_25000:
  41. return "25Gbps";
  42. case SPEED_40000:
  43. return "40Gbps";
  44. case SPEED_50000:
  45. return "50Gbps";
  46. case SPEED_56000:
  47. return "56Gbps";
  48. case SPEED_80000:
  49. return "80Gbps";
  50. case SPEED_100000:
  51. return "100Gbps";
  52. case SPEED_200000:
  53. return "200Gbps";
  54. case SPEED_400000:
  55. return "400Gbps";
  56. case SPEED_800000:
  57. return "800Gbps";
  58. case SPEED_1600000:
  59. return "1600Gbps";
  60. case SPEED_UNKNOWN:
  61. return "Unknown";
  62. default:
  63. return "Unsupported (update phy-core.c)";
  64. }
  65. }
  66. EXPORT_SYMBOL_GPL(phy_speed_to_str);
  67. /**
  68. * phy_duplex_to_str - Return string describing the duplex
  69. *
  70. * @duplex: Duplex setting to describe
  71. */
  72. const char *phy_duplex_to_str(unsigned int duplex)
  73. {
  74. if (duplex == DUPLEX_HALF)
  75. return "Half";
  76. if (duplex == DUPLEX_FULL)
  77. return "Full";
  78. if (duplex == DUPLEX_UNKNOWN)
  79. return "Unknown";
  80. return "Unsupported (update phy-core.c)";
  81. }
  82. EXPORT_SYMBOL_GPL(phy_duplex_to_str);
  83. /**
  84. * phy_rate_matching_to_str - Return a string describing the rate matching
  85. *
  86. * @rate_matching: Type of rate matching to describe
  87. */
  88. const char *phy_rate_matching_to_str(int rate_matching)
  89. {
  90. switch (rate_matching) {
  91. case RATE_MATCH_NONE:
  92. return "none";
  93. case RATE_MATCH_PAUSE:
  94. return "pause";
  95. case RATE_MATCH_CRS:
  96. return "crs";
  97. case RATE_MATCH_OPEN_LOOP:
  98. return "open-loop";
  99. }
  100. return "Unsupported (update phy-core.c)";
  101. }
  102. EXPORT_SYMBOL_GPL(phy_rate_matching_to_str);
  103. /**
  104. * phy_fix_phy_mode_for_mac_delays - Convenience function for fixing PHY
  105. * mode based on whether mac adds internal delay
  106. *
  107. * @interface: The current interface mode of the port
  108. * @mac_txid: True if the mac adds internal tx delay
  109. * @mac_rxid: True if the mac adds internal rx delay
  110. *
  111. * Return: fixed PHY mode, or PHY_INTERFACE_MODE_NA if the interface can
  112. * not apply the internal delay
  113. */
  114. phy_interface_t phy_fix_phy_mode_for_mac_delays(phy_interface_t interface,
  115. bool mac_txid, bool mac_rxid)
  116. {
  117. if (!phy_interface_mode_is_rgmii(interface))
  118. return interface;
  119. if (mac_txid && mac_rxid) {
  120. if (interface == PHY_INTERFACE_MODE_RGMII_ID)
  121. return PHY_INTERFACE_MODE_RGMII;
  122. return PHY_INTERFACE_MODE_NA;
  123. }
  124. if (mac_txid) {
  125. if (interface == PHY_INTERFACE_MODE_RGMII_ID)
  126. return PHY_INTERFACE_MODE_RGMII_RXID;
  127. if (interface == PHY_INTERFACE_MODE_RGMII_TXID)
  128. return PHY_INTERFACE_MODE_RGMII;
  129. return PHY_INTERFACE_MODE_NA;
  130. }
  131. if (mac_rxid) {
  132. if (interface == PHY_INTERFACE_MODE_RGMII_ID)
  133. return PHY_INTERFACE_MODE_RGMII_TXID;
  134. if (interface == PHY_INTERFACE_MODE_RGMII_RXID)
  135. return PHY_INTERFACE_MODE_RGMII;
  136. return PHY_INTERFACE_MODE_NA;
  137. }
  138. return interface;
  139. }
  140. EXPORT_SYMBOL_GPL(phy_fix_phy_mode_for_mac_delays);
  141. /**
  142. * phy_interface_num_ports - Return the number of links that can be carried by
  143. * a given MAC-PHY physical link. Returns 0 if this is
  144. * unknown, the number of links else.
  145. *
  146. * @interface: The interface mode we want to get the number of ports
  147. */
  148. int phy_interface_num_ports(phy_interface_t interface)
  149. {
  150. switch (interface) {
  151. case PHY_INTERFACE_MODE_NA:
  152. return 0;
  153. case PHY_INTERFACE_MODE_INTERNAL:
  154. case PHY_INTERFACE_MODE_MII:
  155. case PHY_INTERFACE_MODE_MIILITE:
  156. case PHY_INTERFACE_MODE_GMII:
  157. case PHY_INTERFACE_MODE_TBI:
  158. case PHY_INTERFACE_MODE_REVMII:
  159. case PHY_INTERFACE_MODE_RMII:
  160. case PHY_INTERFACE_MODE_REVRMII:
  161. case PHY_INTERFACE_MODE_RGMII:
  162. case PHY_INTERFACE_MODE_RGMII_ID:
  163. case PHY_INTERFACE_MODE_RGMII_RXID:
  164. case PHY_INTERFACE_MODE_RGMII_TXID:
  165. case PHY_INTERFACE_MODE_RTBI:
  166. case PHY_INTERFACE_MODE_XGMII:
  167. case PHY_INTERFACE_MODE_XLGMII:
  168. case PHY_INTERFACE_MODE_MOCA:
  169. case PHY_INTERFACE_MODE_TRGMII:
  170. case PHY_INTERFACE_MODE_USXGMII:
  171. case PHY_INTERFACE_MODE_SGMII:
  172. case PHY_INTERFACE_MODE_SMII:
  173. case PHY_INTERFACE_MODE_1000BASEX:
  174. case PHY_INTERFACE_MODE_2500BASEX:
  175. case PHY_INTERFACE_MODE_5GBASER:
  176. case PHY_INTERFACE_MODE_10GBASER:
  177. case PHY_INTERFACE_MODE_25GBASER:
  178. case PHY_INTERFACE_MODE_10GKR:
  179. case PHY_INTERFACE_MODE_100BASEX:
  180. case PHY_INTERFACE_MODE_RXAUI:
  181. case PHY_INTERFACE_MODE_XAUI:
  182. case PHY_INTERFACE_MODE_1000BASEKX:
  183. case PHY_INTERFACE_MODE_50GBASER:
  184. case PHY_INTERFACE_MODE_LAUI:
  185. case PHY_INTERFACE_MODE_100GBASEP:
  186. return 1;
  187. case PHY_INTERFACE_MODE_QSGMII:
  188. case PHY_INTERFACE_MODE_QUSGMII:
  189. case PHY_INTERFACE_MODE_10G_QXGMII:
  190. return 4;
  191. case PHY_INTERFACE_MODE_PSGMII:
  192. return 5;
  193. case PHY_INTERFACE_MODE_MAX:
  194. WARN_ONCE(1, "PHY_INTERFACE_MODE_MAX isn't a valid interface mode");
  195. return 0;
  196. }
  197. return 0;
  198. }
  199. EXPORT_SYMBOL_GPL(phy_interface_num_ports);
  200. static void __set_phy_supported(struct phy_device *phydev, u32 max_speed)
  201. {
  202. struct phy_port *port;
  203. phy_caps_linkmode_max_speed(max_speed, phydev->supported);
  204. phy_for_each_port(phydev, port)
  205. phy_caps_linkmode_max_speed(max_speed, port->supported);
  206. }
  207. /**
  208. * phy_set_max_speed - Set the maximum speed the PHY should support
  209. *
  210. * @phydev: The phy_device struct
  211. * @max_speed: Maximum speed
  212. *
  213. * The PHY might be more capable than the MAC. For example a Fast Ethernet
  214. * is connected to a 1G PHY. This function allows the MAC to indicate its
  215. * maximum speed, and so limit what the PHY will advertise.
  216. */
  217. void phy_set_max_speed(struct phy_device *phydev, u32 max_speed)
  218. {
  219. __set_phy_supported(phydev, max_speed);
  220. phy_advertise_supported(phydev);
  221. }
  222. EXPORT_SYMBOL(phy_set_max_speed);
  223. void of_set_phy_supported(struct phy_device *phydev)
  224. {
  225. struct device_node *node = phydev->mdio.dev.of_node;
  226. u32 max_speed;
  227. if (!IS_ENABLED(CONFIG_OF_MDIO))
  228. return;
  229. if (!node)
  230. return;
  231. if (!of_property_read_u32(node, "max-speed", &max_speed))
  232. __set_phy_supported(phydev, max_speed);
  233. }
  234. void of_set_phy_eee_broken(struct phy_device *phydev)
  235. {
  236. struct device_node *node = phydev->mdio.dev.of_node;
  237. unsigned long *modes = phydev->eee_disabled_modes;
  238. if (!IS_ENABLED(CONFIG_OF_MDIO) || !node)
  239. return;
  240. linkmode_zero(modes);
  241. if (of_property_read_bool(node, "eee-broken-100tx"))
  242. linkmode_set_bit(ETHTOOL_LINK_MODE_100baseT_Full_BIT, modes);
  243. if (of_property_read_bool(node, "eee-broken-1000t"))
  244. linkmode_set_bit(ETHTOOL_LINK_MODE_1000baseT_Full_BIT, modes);
  245. if (of_property_read_bool(node, "eee-broken-10gt"))
  246. linkmode_set_bit(ETHTOOL_LINK_MODE_10000baseT_Full_BIT, modes);
  247. if (of_property_read_bool(node, "eee-broken-1000kx"))
  248. linkmode_set_bit(ETHTOOL_LINK_MODE_1000baseKX_Full_BIT, modes);
  249. if (of_property_read_bool(node, "eee-broken-10gkx4"))
  250. linkmode_set_bit(ETHTOOL_LINK_MODE_10000baseKX4_Full_BIT, modes);
  251. if (of_property_read_bool(node, "eee-broken-10gkr"))
  252. linkmode_set_bit(ETHTOOL_LINK_MODE_10000baseKR_Full_BIT, modes);
  253. }
  254. /**
  255. * of_set_phy_timing_role - Set the master/slave mode of the PHY
  256. *
  257. * @phydev: The phy_device struct
  258. *
  259. * Set master/slave configuration of the PHY based on the device tree.
  260. */
  261. void of_set_phy_timing_role(struct phy_device *phydev)
  262. {
  263. struct device_node *node = phydev->mdio.dev.of_node;
  264. const char *master;
  265. if (!IS_ENABLED(CONFIG_OF_MDIO))
  266. return;
  267. if (!node)
  268. return;
  269. if (of_property_read_string(node, "timing-role", &master))
  270. return;
  271. if (strcmp(master, "forced-master") == 0)
  272. phydev->master_slave_set = MASTER_SLAVE_CFG_MASTER_FORCE;
  273. else if (strcmp(master, "forced-slave") == 0)
  274. phydev->master_slave_set = MASTER_SLAVE_CFG_SLAVE_FORCE;
  275. else if (strcmp(master, "preferred-master") == 0)
  276. phydev->master_slave_set = MASTER_SLAVE_CFG_MASTER_PREFERRED;
  277. else if (strcmp(master, "preferred-slave") == 0)
  278. phydev->master_slave_set = MASTER_SLAVE_CFG_SLAVE_PREFERRED;
  279. else
  280. phydev_warn(phydev, "Unknown master-slave mode %s\n", master);
  281. }
  282. /**
  283. * phy_resolve_aneg_pause - Determine pause autoneg results
  284. *
  285. * @phydev: The phy_device struct
  286. *
  287. * Once autoneg has completed the local pause settings can be
  288. * resolved. Determine if pause and asymmetric pause should be used
  289. * by the MAC.
  290. */
  291. void phy_resolve_aneg_pause(struct phy_device *phydev)
  292. {
  293. if (phydev->duplex == DUPLEX_FULL) {
  294. phydev->pause = linkmode_test_bit(ETHTOOL_LINK_MODE_Pause_BIT,
  295. phydev->lp_advertising);
  296. phydev->asym_pause = linkmode_test_bit(
  297. ETHTOOL_LINK_MODE_Asym_Pause_BIT,
  298. phydev->lp_advertising);
  299. }
  300. }
  301. EXPORT_SYMBOL_GPL(phy_resolve_aneg_pause);
  302. /**
  303. * phy_resolve_aneg_linkmode - resolve the advertisements into PHY settings
  304. * @phydev: The phy_device struct
  305. *
  306. * Resolve our and the link partner advertisements into their corresponding
  307. * speed and duplex. If full duplex was negotiated, extract the pause mode
  308. * from the link partner mask.
  309. */
  310. void phy_resolve_aneg_linkmode(struct phy_device *phydev)
  311. {
  312. __ETHTOOL_DECLARE_LINK_MODE_MASK(common);
  313. const struct link_capabilities *c;
  314. linkmode_and(common, phydev->lp_advertising, phydev->advertising);
  315. c = phy_caps_lookup_by_linkmode(common);
  316. if (c) {
  317. phydev->speed = c->speed;
  318. phydev->duplex = c->duplex;
  319. }
  320. phy_resolve_aneg_pause(phydev);
  321. }
  322. EXPORT_SYMBOL_GPL(phy_resolve_aneg_linkmode);
  323. /**
  324. * phy_check_downshift - check whether downshift occurred
  325. * @phydev: The phy_device struct
  326. *
  327. * Check whether a downshift to a lower speed occurred. If this should be the
  328. * case warn the user.
  329. * Prerequisite for detecting downshift is that PHY driver implements the
  330. * read_status callback and sets phydev->speed to the actual link speed.
  331. */
  332. void phy_check_downshift(struct phy_device *phydev)
  333. {
  334. __ETHTOOL_DECLARE_LINK_MODE_MASK(common);
  335. const struct link_capabilities *c;
  336. int speed = SPEED_UNKNOWN;
  337. phydev->downshifted_rate = 0;
  338. if (phydev->autoneg == AUTONEG_DISABLE ||
  339. phydev->speed == SPEED_UNKNOWN)
  340. return;
  341. linkmode_and(common, phydev->lp_advertising, phydev->advertising);
  342. c = phy_caps_lookup_by_linkmode(common);
  343. if (c)
  344. speed = c->speed;
  345. if (speed == SPEED_UNKNOWN || phydev->speed >= speed)
  346. return;
  347. phydev_warn(phydev, "Downshift occurred from negotiated speed %s to actual speed %s, check cabling!\n",
  348. phy_speed_to_str(speed), phy_speed_to_str(phydev->speed));
  349. phydev->downshifted_rate = 1;
  350. }
  351. static int phy_resolve_min_speed(struct phy_device *phydev, bool fdx_only)
  352. {
  353. __ETHTOOL_DECLARE_LINK_MODE_MASK(common);
  354. const struct link_capabilities *c;
  355. linkmode_and(common, phydev->lp_advertising, phydev->advertising);
  356. c = phy_caps_lookup_by_linkmode_rev(common, fdx_only);
  357. if (c)
  358. return c->speed;
  359. return SPEED_UNKNOWN;
  360. }
  361. int phy_speed_down_core(struct phy_device *phydev)
  362. {
  363. int min_common_speed = phy_resolve_min_speed(phydev, true);
  364. if (min_common_speed == SPEED_UNKNOWN)
  365. return -EINVAL;
  366. phy_caps_linkmode_max_speed(min_common_speed, phydev->advertising);
  367. return 0;
  368. }
  369. static void mmd_phy_indirect(struct mii_bus *bus, int phy_addr, int devad,
  370. u16 regnum)
  371. {
  372. /* Write the desired MMD Devad */
  373. __mdiobus_write(bus, phy_addr, MII_MMD_CTRL, devad);
  374. /* Write the desired MMD register address */
  375. __mdiobus_write(bus, phy_addr, MII_MMD_DATA, regnum);
  376. /* Select the Function : DATA with no post increment */
  377. __mdiobus_write(bus, phy_addr, MII_MMD_CTRL,
  378. devad | MII_MMD_CTRL_NOINCR);
  379. }
  380. int mmd_phy_read(struct mii_bus *bus, int phy_addr, bool is_c45,
  381. int devad, u32 regnum)
  382. {
  383. if (is_c45)
  384. return __mdiobus_c45_read(bus, phy_addr, devad, regnum);
  385. mmd_phy_indirect(bus, phy_addr, devad, regnum);
  386. /* Read the content of the MMD's selected register */
  387. return __mdiobus_read(bus, phy_addr, MII_MMD_DATA);
  388. }
  389. EXPORT_SYMBOL_GPL(mmd_phy_read);
  390. int mmd_phy_write(struct mii_bus *bus, int phy_addr, bool is_c45,
  391. int devad, u32 regnum, u16 val)
  392. {
  393. if (is_c45)
  394. return __mdiobus_c45_write(bus, phy_addr, devad, regnum, val);
  395. mmd_phy_indirect(bus, phy_addr, devad, regnum);
  396. /* Write the data into MMD's selected register */
  397. return __mdiobus_write(bus, phy_addr, MII_MMD_DATA, val);
  398. }
  399. EXPORT_SYMBOL_GPL(mmd_phy_write);
  400. /**
  401. * __phy_read_mmd - Convenience function for reading a register
  402. * from an MMD on a given PHY.
  403. * @phydev: The phy_device struct
  404. * @devad: The MMD to read from (0..31)
  405. * @regnum: The register on the MMD to read (0..65535)
  406. *
  407. * Same rules as for __phy_read();
  408. */
  409. int __phy_read_mmd(struct phy_device *phydev, int devad, u32 regnum)
  410. {
  411. if (regnum > (u16)~0 || devad > 32)
  412. return -EINVAL;
  413. if (phydev->drv && phydev->drv->read_mmd)
  414. return phydev->drv->read_mmd(phydev, devad, regnum);
  415. return mmd_phy_read(phydev->mdio.bus, phydev->mdio.addr,
  416. phydev->is_c45, devad, regnum);
  417. }
  418. EXPORT_SYMBOL(__phy_read_mmd);
  419. /**
  420. * phy_read_mmd - Convenience function for reading a register
  421. * from an MMD on a given PHY.
  422. * @phydev: The phy_device struct
  423. * @devad: The MMD to read from
  424. * @regnum: The register on the MMD to read
  425. *
  426. * Same rules as for phy_read();
  427. */
  428. int phy_read_mmd(struct phy_device *phydev, int devad, u32 regnum)
  429. {
  430. int ret;
  431. phy_lock_mdio_bus(phydev);
  432. ret = __phy_read_mmd(phydev, devad, regnum);
  433. phy_unlock_mdio_bus(phydev);
  434. return ret;
  435. }
  436. EXPORT_SYMBOL(phy_read_mmd);
  437. /**
  438. * __phy_write_mmd - Convenience function for writing a register
  439. * on an MMD on a given PHY.
  440. * @phydev: The phy_device struct
  441. * @devad: The MMD to read from
  442. * @regnum: The register on the MMD to read
  443. * @val: value to write to @regnum
  444. *
  445. * Same rules as for __phy_write();
  446. */
  447. int __phy_write_mmd(struct phy_device *phydev, int devad, u32 regnum, u16 val)
  448. {
  449. if (regnum > (u16)~0 || devad > 32)
  450. return -EINVAL;
  451. if (phydev->drv && phydev->drv->write_mmd)
  452. return phydev->drv->write_mmd(phydev, devad, regnum, val);
  453. return mmd_phy_write(phydev->mdio.bus, phydev->mdio.addr,
  454. phydev->is_c45, devad, regnum, val);
  455. }
  456. EXPORT_SYMBOL(__phy_write_mmd);
  457. /**
  458. * phy_write_mmd - Convenience function for writing a register
  459. * on an MMD on a given PHY.
  460. * @phydev: The phy_device struct
  461. * @devad: The MMD to read from
  462. * @regnum: The register on the MMD to read
  463. * @val: value to write to @regnum
  464. *
  465. * Same rules as for phy_write();
  466. */
  467. int phy_write_mmd(struct phy_device *phydev, int devad, u32 regnum, u16 val)
  468. {
  469. int ret;
  470. phy_lock_mdio_bus(phydev);
  471. ret = __phy_write_mmd(phydev, devad, regnum, val);
  472. phy_unlock_mdio_bus(phydev);
  473. return ret;
  474. }
  475. EXPORT_SYMBOL(phy_write_mmd);
  476. /**
  477. * phy_modify_changed - Function for modifying a PHY register
  478. * @phydev: the phy_device struct
  479. * @regnum: register number to modify
  480. * @mask: bit mask of bits to clear
  481. * @set: new value of bits set in mask to write to @regnum
  482. *
  483. * NOTE: MUST NOT be called from interrupt context,
  484. * because the bus read/write functions may wait for an interrupt
  485. * to conclude the operation.
  486. *
  487. * Returns negative errno, 0 if there was no change, and 1 in case of change
  488. */
  489. int phy_modify_changed(struct phy_device *phydev, u32 regnum, u16 mask, u16 set)
  490. {
  491. int ret;
  492. phy_lock_mdio_bus(phydev);
  493. ret = __phy_modify_changed(phydev, regnum, mask, set);
  494. phy_unlock_mdio_bus(phydev);
  495. return ret;
  496. }
  497. EXPORT_SYMBOL_GPL(phy_modify_changed);
  498. /**
  499. * __phy_modify - Convenience function for modifying a PHY register
  500. * @phydev: the phy_device struct
  501. * @regnum: register number to modify
  502. * @mask: bit mask of bits to clear
  503. * @set: new value of bits set in mask to write to @regnum
  504. *
  505. * NOTE: MUST NOT be called from interrupt context,
  506. * because the bus read/write functions may wait for an interrupt
  507. * to conclude the operation.
  508. */
  509. int __phy_modify(struct phy_device *phydev, u32 regnum, u16 mask, u16 set)
  510. {
  511. int ret;
  512. ret = __phy_modify_changed(phydev, regnum, mask, set);
  513. return ret < 0 ? ret : 0;
  514. }
  515. EXPORT_SYMBOL_GPL(__phy_modify);
  516. /**
  517. * phy_modify - Convenience function for modifying a given PHY register
  518. * @phydev: the phy_device struct
  519. * @regnum: register number to write
  520. * @mask: bit mask of bits to clear
  521. * @set: new value of bits set in mask to write to @regnum
  522. *
  523. * NOTE: MUST NOT be called from interrupt context,
  524. * because the bus read/write functions may wait for an interrupt
  525. * to conclude the operation.
  526. */
  527. int phy_modify(struct phy_device *phydev, u32 regnum, u16 mask, u16 set)
  528. {
  529. int ret;
  530. phy_lock_mdio_bus(phydev);
  531. ret = __phy_modify(phydev, regnum, mask, set);
  532. phy_unlock_mdio_bus(phydev);
  533. return ret;
  534. }
  535. EXPORT_SYMBOL_GPL(phy_modify);
  536. /**
  537. * __phy_modify_mmd_changed - Function for modifying a register on MMD
  538. * @phydev: the phy_device struct
  539. * @devad: the MMD containing register to modify
  540. * @regnum: register number to modify
  541. * @mask: bit mask of bits to clear
  542. * @set: new value of bits set in mask to write to @regnum
  543. *
  544. * Unlocked helper function which allows a MMD register to be modified as
  545. * new register value = (old register value & ~mask) | set
  546. *
  547. * Returns negative errno, 0 if there was no change, and 1 in case of change
  548. */
  549. int __phy_modify_mmd_changed(struct phy_device *phydev, int devad, u32 regnum,
  550. u16 mask, u16 set)
  551. {
  552. int new, ret;
  553. ret = __phy_read_mmd(phydev, devad, regnum);
  554. if (ret < 0)
  555. return ret;
  556. new = (ret & ~mask) | set;
  557. if (new == ret)
  558. return 0;
  559. ret = __phy_write_mmd(phydev, devad, regnum, new);
  560. return ret < 0 ? ret : 1;
  561. }
  562. EXPORT_SYMBOL_GPL(__phy_modify_mmd_changed);
  563. /**
  564. * phy_modify_mmd_changed - Function for modifying a register on MMD
  565. * @phydev: the phy_device struct
  566. * @devad: the MMD containing register to modify
  567. * @regnum: register number to modify
  568. * @mask: bit mask of bits to clear
  569. * @set: new value of bits set in mask to write to @regnum
  570. *
  571. * NOTE: MUST NOT be called from interrupt context,
  572. * because the bus read/write functions may wait for an interrupt
  573. * to conclude the operation.
  574. *
  575. * Returns negative errno, 0 if there was no change, and 1 in case of change
  576. */
  577. int phy_modify_mmd_changed(struct phy_device *phydev, int devad, u32 regnum,
  578. u16 mask, u16 set)
  579. {
  580. int ret;
  581. phy_lock_mdio_bus(phydev);
  582. ret = __phy_modify_mmd_changed(phydev, devad, regnum, mask, set);
  583. phy_unlock_mdio_bus(phydev);
  584. return ret;
  585. }
  586. EXPORT_SYMBOL_GPL(phy_modify_mmd_changed);
  587. /**
  588. * __phy_modify_mmd - Convenience function for modifying a register on MMD
  589. * @phydev: the phy_device struct
  590. * @devad: the MMD containing register to modify
  591. * @regnum: register number to modify
  592. * @mask: bit mask of bits to clear
  593. * @set: new value of bits set in mask to write to @regnum
  594. *
  595. * NOTE: MUST NOT be called from interrupt context,
  596. * because the bus read/write functions may wait for an interrupt
  597. * to conclude the operation.
  598. */
  599. int __phy_modify_mmd(struct phy_device *phydev, int devad, u32 regnum,
  600. u16 mask, u16 set)
  601. {
  602. int ret;
  603. ret = __phy_modify_mmd_changed(phydev, devad, regnum, mask, set);
  604. return ret < 0 ? ret : 0;
  605. }
  606. EXPORT_SYMBOL_GPL(__phy_modify_mmd);
  607. /**
  608. * phy_modify_mmd - Convenience function for modifying a register on MMD
  609. * @phydev: the phy_device struct
  610. * @devad: the MMD containing register to modify
  611. * @regnum: register number to modify
  612. * @mask: bit mask of bits to clear
  613. * @set: new value of bits set in mask to write to @regnum
  614. *
  615. * NOTE: MUST NOT be called from interrupt context,
  616. * because the bus read/write functions may wait for an interrupt
  617. * to conclude the operation.
  618. */
  619. int phy_modify_mmd(struct phy_device *phydev, int devad, u32 regnum,
  620. u16 mask, u16 set)
  621. {
  622. int ret;
  623. phy_lock_mdio_bus(phydev);
  624. ret = __phy_modify_mmd(phydev, devad, regnum, mask, set);
  625. phy_unlock_mdio_bus(phydev);
  626. return ret;
  627. }
  628. EXPORT_SYMBOL_GPL(phy_modify_mmd);
  629. static int __phy_read_page(struct phy_device *phydev)
  630. {
  631. if (WARN_ONCE(!phydev->drv->read_page, "read_page callback not available, PHY driver not loaded?\n"))
  632. return -EOPNOTSUPP;
  633. return phydev->drv->read_page(phydev);
  634. }
  635. static int __phy_write_page(struct phy_device *phydev, int page)
  636. {
  637. if (WARN_ONCE(!phydev->drv->write_page, "write_page callback not available, PHY driver not loaded?\n"))
  638. return -EOPNOTSUPP;
  639. return phydev->drv->write_page(phydev, page);
  640. }
  641. /**
  642. * phy_save_page() - take the bus lock and save the current page
  643. * @phydev: a pointer to a &struct phy_device
  644. *
  645. * Take the MDIO bus lock, and return the current page number. On error,
  646. * returns a negative errno. phy_restore_page() must always be called
  647. * after this, irrespective of success or failure of this call.
  648. */
  649. int phy_save_page(struct phy_device *phydev)
  650. {
  651. phy_lock_mdio_bus(phydev);
  652. return __phy_read_page(phydev);
  653. }
  654. EXPORT_SYMBOL_GPL(phy_save_page);
  655. /**
  656. * phy_select_page() - take the bus lock, save the current page, and set a page
  657. * @phydev: a pointer to a &struct phy_device
  658. * @page: desired page
  659. *
  660. * Take the MDIO bus lock to protect against concurrent access, save the
  661. * current PHY page, and set the current page. On error, returns a
  662. * negative errno, otherwise returns the previous page number.
  663. * phy_restore_page() must always be called after this, irrespective
  664. * of success or failure of this call.
  665. */
  666. int phy_select_page(struct phy_device *phydev, int page)
  667. {
  668. int ret, oldpage;
  669. oldpage = ret = phy_save_page(phydev);
  670. if (ret < 0)
  671. return ret;
  672. if (oldpage != page) {
  673. ret = __phy_write_page(phydev, page);
  674. if (ret < 0)
  675. return ret;
  676. }
  677. return oldpage;
  678. }
  679. EXPORT_SYMBOL_GPL(phy_select_page);
  680. /**
  681. * phy_restore_page() - restore the page register and release the bus lock
  682. * @phydev: a pointer to a &struct phy_device
  683. * @oldpage: the old page, return value from phy_save_page() or phy_select_page()
  684. * @ret: operation's return code
  685. *
  686. * Release the MDIO bus lock, restoring @oldpage if it is a valid page.
  687. * This function propagates the earliest error code from the group of
  688. * operations.
  689. *
  690. * Returns:
  691. * @oldpage if it was a negative value, otherwise
  692. * @ret if it was a negative errno value, otherwise
  693. * phy_write_page()'s negative value if it were in error, otherwise
  694. * @ret.
  695. */
  696. int phy_restore_page(struct phy_device *phydev, int oldpage, int ret)
  697. {
  698. int r;
  699. if (oldpage >= 0) {
  700. r = __phy_write_page(phydev, oldpage);
  701. /* Propagate the operation return code if the page write
  702. * was successful.
  703. */
  704. if (ret >= 0 && r < 0)
  705. ret = r;
  706. } else {
  707. /* Propagate the phy page selection error code */
  708. ret = oldpage;
  709. }
  710. phy_unlock_mdio_bus(phydev);
  711. return ret;
  712. }
  713. EXPORT_SYMBOL_GPL(phy_restore_page);
  714. /**
  715. * phy_read_paged() - Convenience function for reading a paged register
  716. * @phydev: a pointer to a &struct phy_device
  717. * @page: the page for the phy
  718. * @regnum: register number
  719. *
  720. * Same rules as for phy_read().
  721. */
  722. int phy_read_paged(struct phy_device *phydev, int page, u32 regnum)
  723. {
  724. int ret = 0, oldpage;
  725. oldpage = phy_select_page(phydev, page);
  726. if (oldpage >= 0)
  727. ret = __phy_read(phydev, regnum);
  728. return phy_restore_page(phydev, oldpage, ret);
  729. }
  730. EXPORT_SYMBOL(phy_read_paged);
  731. /**
  732. * phy_write_paged() - Convenience function for writing a paged register
  733. * @phydev: a pointer to a &struct phy_device
  734. * @page: the page for the phy
  735. * @regnum: register number
  736. * @val: value to write
  737. *
  738. * Same rules as for phy_write().
  739. */
  740. int phy_write_paged(struct phy_device *phydev, int page, u32 regnum, u16 val)
  741. {
  742. int ret = 0, oldpage;
  743. oldpage = phy_select_page(phydev, page);
  744. if (oldpage >= 0)
  745. ret = __phy_write(phydev, regnum, val);
  746. return phy_restore_page(phydev, oldpage, ret);
  747. }
  748. EXPORT_SYMBOL(phy_write_paged);
  749. /**
  750. * phy_modify_paged_changed() - Function for modifying a paged register
  751. * @phydev: a pointer to a &struct phy_device
  752. * @page: the page for the phy
  753. * @regnum: register number
  754. * @mask: bit mask of bits to clear
  755. * @set: bit mask of bits to set
  756. *
  757. * Returns negative errno, 0 if there was no change, and 1 in case of change
  758. */
  759. int phy_modify_paged_changed(struct phy_device *phydev, int page, u32 regnum,
  760. u16 mask, u16 set)
  761. {
  762. int ret = 0, oldpage;
  763. oldpage = phy_select_page(phydev, page);
  764. if (oldpage >= 0)
  765. ret = __phy_modify_changed(phydev, regnum, mask, set);
  766. return phy_restore_page(phydev, oldpage, ret);
  767. }
  768. EXPORT_SYMBOL(phy_modify_paged_changed);
  769. /**
  770. * phy_modify_paged() - Convenience function for modifying a paged register
  771. * @phydev: a pointer to a &struct phy_device
  772. * @page: the page for the phy
  773. * @regnum: register number
  774. * @mask: bit mask of bits to clear
  775. * @set: bit mask of bits to set
  776. *
  777. * Same rules as for phy_read() and phy_write().
  778. */
  779. int phy_modify_paged(struct phy_device *phydev, int page, u32 regnum,
  780. u16 mask, u16 set)
  781. {
  782. int ret = phy_modify_paged_changed(phydev, page, regnum, mask, set);
  783. return ret < 0 ? ret : 0;
  784. }
  785. EXPORT_SYMBOL(phy_modify_paged);