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