1 // SPDX-License-Identifier: GPL-2.0+
3 * Core PHY library, taken from phy.c
5 #include <linux/export.h>
10 * phy_speed_to_str - Return a string representing the PHY link speed
12 * @speed: Speed of the link
14 const char *phy_speed_to_str(int speed)
16 BUILD_BUG_ON_MSG(__ETHTOOL_LINK_MODE_MASK_NBITS != 93,
17 "Enum ethtool_link_mode_bit_indices and phylib are out of sync. "
18 "If a speed or mode has been added please update phy_speed_to_str "
19 "and the PHY settings array.\n");
55 return "Unsupported (update phy-core.c)";
58 EXPORT_SYMBOL_GPL(phy_speed_to_str);
61 * phy_duplex_to_str - Return string describing the duplex
63 * @duplex: Duplex setting to describe
65 const char *phy_duplex_to_str(unsigned int duplex)
67 if (duplex == DUPLEX_HALF)
69 if (duplex == DUPLEX_FULL)
71 if (duplex == DUPLEX_UNKNOWN)
73 return "Unsupported (update phy-core.c)";
75 EXPORT_SYMBOL_GPL(phy_duplex_to_str);
77 /* A mapping of all SUPPORTED settings to speed/duplex. This table
78 * must be grouped by speed and sorted in descending match priority
79 * - iow, descending speed.
82 #define PHY_SETTING(s, d, b) { .speed = SPEED_ ## s, .duplex = DUPLEX_ ## d, \
83 .bit = ETHTOOL_LINK_MODE_ ## b ## _BIT}
85 static const struct phy_setting settings[] = {
87 PHY_SETTING( 400000, FULL, 400000baseCR8_Full ),
88 PHY_SETTING( 400000, FULL, 400000baseKR8_Full ),
89 PHY_SETTING( 400000, FULL, 400000baseLR8_ER8_FR8_Full ),
90 PHY_SETTING( 400000, FULL, 400000baseDR8_Full ),
91 PHY_SETTING( 400000, FULL, 400000baseSR8_Full ),
92 PHY_SETTING( 400000, FULL, 400000baseCR4_Full ),
93 PHY_SETTING( 400000, FULL, 400000baseKR4_Full ),
94 PHY_SETTING( 400000, FULL, 400000baseLR4_ER4_FR4_Full ),
95 PHY_SETTING( 400000, FULL, 400000baseDR4_Full ),
96 PHY_SETTING( 400000, FULL, 400000baseSR4_Full ),
98 PHY_SETTING( 200000, FULL, 200000baseCR4_Full ),
99 PHY_SETTING( 200000, FULL, 200000baseKR4_Full ),
100 PHY_SETTING( 200000, FULL, 200000baseLR4_ER4_FR4_Full ),
101 PHY_SETTING( 200000, FULL, 200000baseDR4_Full ),
102 PHY_SETTING( 200000, FULL, 200000baseSR4_Full ),
103 PHY_SETTING( 200000, FULL, 200000baseCR2_Full ),
104 PHY_SETTING( 200000, FULL, 200000baseKR2_Full ),
105 PHY_SETTING( 200000, FULL, 200000baseLR2_ER2_FR2_Full ),
106 PHY_SETTING( 200000, FULL, 200000baseDR2_Full ),
107 PHY_SETTING( 200000, FULL, 200000baseSR2_Full ),
109 PHY_SETTING( 100000, FULL, 100000baseCR4_Full ),
110 PHY_SETTING( 100000, FULL, 100000baseKR4_Full ),
111 PHY_SETTING( 100000, FULL, 100000baseLR4_ER4_Full ),
112 PHY_SETTING( 100000, FULL, 100000baseSR4_Full ),
113 PHY_SETTING( 100000, FULL, 100000baseCR2_Full ),
114 PHY_SETTING( 100000, FULL, 100000baseKR2_Full ),
115 PHY_SETTING( 100000, FULL, 100000baseLR2_ER2_FR2_Full ),
116 PHY_SETTING( 100000, FULL, 100000baseDR2_Full ),
117 PHY_SETTING( 100000, FULL, 100000baseSR2_Full ),
118 PHY_SETTING( 100000, FULL, 100000baseCR_Full ),
119 PHY_SETTING( 100000, FULL, 100000baseKR_Full ),
120 PHY_SETTING( 100000, FULL, 100000baseLR_ER_FR_Full ),
121 PHY_SETTING( 100000, FULL, 100000baseDR_Full ),
122 PHY_SETTING( 100000, FULL, 100000baseSR_Full ),
124 PHY_SETTING( 56000, FULL, 56000baseCR4_Full ),
125 PHY_SETTING( 56000, FULL, 56000baseKR4_Full ),
126 PHY_SETTING( 56000, FULL, 56000baseLR4_Full ),
127 PHY_SETTING( 56000, FULL, 56000baseSR4_Full ),
129 PHY_SETTING( 50000, FULL, 50000baseCR2_Full ),
130 PHY_SETTING( 50000, FULL, 50000baseKR2_Full ),
131 PHY_SETTING( 50000, FULL, 50000baseSR2_Full ),
132 PHY_SETTING( 50000, FULL, 50000baseCR_Full ),
133 PHY_SETTING( 50000, FULL, 50000baseKR_Full ),
134 PHY_SETTING( 50000, FULL, 50000baseLR_ER_FR_Full ),
135 PHY_SETTING( 50000, FULL, 50000baseDR_Full ),
136 PHY_SETTING( 50000, FULL, 50000baseSR_Full ),
138 PHY_SETTING( 40000, FULL, 40000baseCR4_Full ),
139 PHY_SETTING( 40000, FULL, 40000baseKR4_Full ),
140 PHY_SETTING( 40000, FULL, 40000baseLR4_Full ),
141 PHY_SETTING( 40000, FULL, 40000baseSR4_Full ),
143 PHY_SETTING( 25000, FULL, 25000baseCR_Full ),
144 PHY_SETTING( 25000, FULL, 25000baseKR_Full ),
145 PHY_SETTING( 25000, FULL, 25000baseSR_Full ),
147 PHY_SETTING( 20000, FULL, 20000baseKR2_Full ),
148 PHY_SETTING( 20000, FULL, 20000baseMLD2_Full ),
150 PHY_SETTING( 10000, FULL, 10000baseCR_Full ),
151 PHY_SETTING( 10000, FULL, 10000baseER_Full ),
152 PHY_SETTING( 10000, FULL, 10000baseKR_Full ),
153 PHY_SETTING( 10000, FULL, 10000baseKX4_Full ),
154 PHY_SETTING( 10000, FULL, 10000baseLR_Full ),
155 PHY_SETTING( 10000, FULL, 10000baseLRM_Full ),
156 PHY_SETTING( 10000, FULL, 10000baseR_FEC ),
157 PHY_SETTING( 10000, FULL, 10000baseSR_Full ),
158 PHY_SETTING( 10000, FULL, 10000baseT_Full ),
160 PHY_SETTING( 5000, FULL, 5000baseT_Full ),
162 PHY_SETTING( 2500, FULL, 2500baseT_Full ),
163 PHY_SETTING( 2500, FULL, 2500baseX_Full ),
165 PHY_SETTING( 1000, FULL, 1000baseT_Full ),
166 PHY_SETTING( 1000, HALF, 1000baseT_Half ),
167 PHY_SETTING( 1000, FULL, 1000baseT1_Full ),
168 PHY_SETTING( 1000, FULL, 1000baseX_Full ),
169 PHY_SETTING( 1000, FULL, 1000baseKX_Full ),
171 PHY_SETTING( 100, FULL, 100baseT_Full ),
172 PHY_SETTING( 100, FULL, 100baseT1_Full ),
173 PHY_SETTING( 100, HALF, 100baseT_Half ),
174 PHY_SETTING( 100, HALF, 100baseFX_Half ),
175 PHY_SETTING( 100, FULL, 100baseFX_Full ),
177 PHY_SETTING( 10, FULL, 10baseT_Full ),
178 PHY_SETTING( 10, HALF, 10baseT_Half ),
179 PHY_SETTING( 10, FULL, 10baseT1L_Full ),
184 * phy_lookup_setting - lookup a PHY setting
185 * @speed: speed to match
186 * @duplex: duplex to match
187 * @mask: allowed link modes
188 * @exact: an exact match is required
190 * Search the settings array for a setting that matches the speed and
191 * duplex, and which is supported.
193 * If @exact is unset, either an exact match or %NULL for no match will
196 * If @exact is set, an exact match, the fastest supported setting at
197 * or below the specified speed, the slowest supported setting, or if
198 * they all fail, %NULL will be returned.
200 const struct phy_setting *
201 phy_lookup_setting(int speed, int duplex, const unsigned long *mask, bool exact)
203 const struct phy_setting *p, *match = NULL, *last = NULL;
206 for (i = 0, p = settings; i < ARRAY_SIZE(settings); i++, p++) {
207 if (p->bit < __ETHTOOL_LINK_MODE_MASK_NBITS &&
208 test_bit(p->bit, mask)) {
210 if (p->speed == speed && p->duplex == duplex) {
211 /* Exact match for speed and duplex */
215 if (!match && p->speed <= speed)
219 if (p->speed < speed)
225 if (!match && !exact)
230 EXPORT_SYMBOL_GPL(phy_lookup_setting);
232 size_t phy_speeds(unsigned int *speeds, size_t size,
238 for (i = 0, count = 0; i < ARRAY_SIZE(settings) && count < size; i++)
239 if (settings[i].bit < __ETHTOOL_LINK_MODE_MASK_NBITS &&
240 test_bit(settings[i].bit, mask) &&
241 (count == 0 || speeds[count - 1] != settings[i].speed))
242 speeds[count++] = settings[i].speed;
247 static void __set_linkmode_max_speed(u32 max_speed, unsigned long *addr)
249 const struct phy_setting *p;
252 for (i = 0, p = settings; i < ARRAY_SIZE(settings); i++, p++) {
253 if (p->speed > max_speed)
254 linkmode_clear_bit(p->bit, addr);
260 static void __set_phy_supported(struct phy_device *phydev, u32 max_speed)
262 __set_linkmode_max_speed(max_speed, phydev->supported);
266 * phy_set_max_speed - Set the maximum speed the PHY should support
268 * @phydev: The phy_device struct
269 * @max_speed: Maximum speed
271 * The PHY might be more capable than the MAC. For example a Fast Ethernet
272 * is connected to a 1G PHY. This function allows the MAC to indicate its
273 * maximum speed, and so limit what the PHY will advertise.
275 void phy_set_max_speed(struct phy_device *phydev, u32 max_speed)
277 __set_phy_supported(phydev, max_speed);
279 phy_advertise_supported(phydev);
281 EXPORT_SYMBOL(phy_set_max_speed);
283 void of_set_phy_supported(struct phy_device *phydev)
285 struct device_node *node = phydev->mdio.dev.of_node;
288 if (!IS_ENABLED(CONFIG_OF_MDIO))
294 if (!of_property_read_u32(node, "max-speed", &max_speed))
295 __set_phy_supported(phydev, max_speed);
298 void of_set_phy_eee_broken(struct phy_device *phydev)
300 struct device_node *node = phydev->mdio.dev.of_node;
303 if (!IS_ENABLED(CONFIG_OF_MDIO))
309 if (of_property_read_bool(node, "eee-broken-100tx"))
310 broken |= MDIO_EEE_100TX;
311 if (of_property_read_bool(node, "eee-broken-1000t"))
312 broken |= MDIO_EEE_1000T;
313 if (of_property_read_bool(node, "eee-broken-10gt"))
314 broken |= MDIO_EEE_10GT;
315 if (of_property_read_bool(node, "eee-broken-1000kx"))
316 broken |= MDIO_EEE_1000KX;
317 if (of_property_read_bool(node, "eee-broken-10gkx4"))
318 broken |= MDIO_EEE_10GKX4;
319 if (of_property_read_bool(node, "eee-broken-10gkr"))
320 broken |= MDIO_EEE_10GKR;
322 phydev->eee_broken_modes = broken;
326 * phy_resolve_aneg_pause - Determine pause autoneg results
328 * @phydev: The phy_device struct
330 * Once autoneg has completed the local pause settings can be
331 * resolved. Determine if pause and asymmetric pause should be used
335 void phy_resolve_aneg_pause(struct phy_device *phydev)
337 if (phydev->duplex == DUPLEX_FULL) {
338 phydev->pause = linkmode_test_bit(ETHTOOL_LINK_MODE_Pause_BIT,
339 phydev->lp_advertising);
340 phydev->asym_pause = linkmode_test_bit(
341 ETHTOOL_LINK_MODE_Asym_Pause_BIT,
342 phydev->lp_advertising);
345 EXPORT_SYMBOL_GPL(phy_resolve_aneg_pause);
348 * phy_resolve_aneg_linkmode - resolve the advertisements into PHY settings
349 * @phydev: The phy_device struct
351 * Resolve our and the link partner advertisements into their corresponding
352 * speed and duplex. If full duplex was negotiated, extract the pause mode
353 * from the link partner mask.
355 void phy_resolve_aneg_linkmode(struct phy_device *phydev)
357 __ETHTOOL_DECLARE_LINK_MODE_MASK(common);
360 linkmode_and(common, phydev->lp_advertising, phydev->advertising);
362 for (i = 0; i < ARRAY_SIZE(settings); i++)
363 if (test_bit(settings[i].bit, common)) {
364 phydev->speed = settings[i].speed;
365 phydev->duplex = settings[i].duplex;
369 phy_resolve_aneg_pause(phydev);
371 EXPORT_SYMBOL_GPL(phy_resolve_aneg_linkmode);
374 * phy_check_downshift - check whether downshift occurred
375 * @phydev: The phy_device struct
377 * Check whether a downshift to a lower speed occurred. If this should be the
378 * case warn the user.
379 * Prerequisite for detecting downshift is that PHY driver implements the
380 * read_status callback and sets phydev->speed to the actual link speed.
382 void phy_check_downshift(struct phy_device *phydev)
384 __ETHTOOL_DECLARE_LINK_MODE_MASK(common);
385 int i, speed = SPEED_UNKNOWN;
387 phydev->downshifted_rate = 0;
389 if (phydev->autoneg == AUTONEG_DISABLE ||
390 phydev->speed == SPEED_UNKNOWN)
393 linkmode_and(common, phydev->lp_advertising, phydev->advertising);
395 for (i = 0; i < ARRAY_SIZE(settings); i++)
396 if (test_bit(settings[i].bit, common)) {
397 speed = settings[i].speed;
401 if (speed == SPEED_UNKNOWN || phydev->speed >= speed)
404 phydev_warn(phydev, "Downshift occurred from negotiated speed %s to actual speed %s, check cabling!\n",
405 phy_speed_to_str(speed), phy_speed_to_str(phydev->speed));
407 phydev->downshifted_rate = 1;
409 EXPORT_SYMBOL_GPL(phy_check_downshift);
411 static int phy_resolve_min_speed(struct phy_device *phydev, bool fdx_only)
413 __ETHTOOL_DECLARE_LINK_MODE_MASK(common);
414 int i = ARRAY_SIZE(settings);
416 linkmode_and(common, phydev->lp_advertising, phydev->advertising);
419 if (test_bit(settings[i].bit, common)) {
420 if (fdx_only && settings[i].duplex != DUPLEX_FULL)
422 return settings[i].speed;
426 return SPEED_UNKNOWN;
429 int phy_speed_down_core(struct phy_device *phydev)
431 int min_common_speed = phy_resolve_min_speed(phydev, true);
433 if (min_common_speed == SPEED_UNKNOWN)
436 __set_linkmode_max_speed(min_common_speed, phydev->advertising);
441 static void mmd_phy_indirect(struct mii_bus *bus, int phy_addr, int devad,
444 /* Write the desired MMD Devad */
445 __mdiobus_write(bus, phy_addr, MII_MMD_CTRL, devad);
447 /* Write the desired MMD register address */
448 __mdiobus_write(bus, phy_addr, MII_MMD_DATA, regnum);
450 /* Select the Function : DATA with no post increment */
451 __mdiobus_write(bus, phy_addr, MII_MMD_CTRL,
452 devad | MII_MMD_CTRL_NOINCR);
456 * __phy_read_mmd - Convenience function for reading a register
457 * from an MMD on a given PHY.
458 * @phydev: The phy_device struct
459 * @devad: The MMD to read from (0..31)
460 * @regnum: The register on the MMD to read (0..65535)
462 * Same rules as for __phy_read();
464 int __phy_read_mmd(struct phy_device *phydev, int devad, u32 regnum)
468 if (regnum > (u16)~0 || devad > 32)
471 if (phydev->drv && phydev->drv->read_mmd) {
472 val = phydev->drv->read_mmd(phydev, devad, regnum);
473 } else if (phydev->is_c45) {
474 val = __mdiobus_c45_read(phydev->mdio.bus, phydev->mdio.addr,
477 struct mii_bus *bus = phydev->mdio.bus;
478 int phy_addr = phydev->mdio.addr;
480 mmd_phy_indirect(bus, phy_addr, devad, regnum);
482 /* Read the content of the MMD's selected register */
483 val = __mdiobus_read(bus, phy_addr, MII_MMD_DATA);
487 EXPORT_SYMBOL(__phy_read_mmd);
490 * phy_read_mmd - Convenience function for reading a register
491 * from an MMD on a given PHY.
492 * @phydev: The phy_device struct
493 * @devad: The MMD to read from
494 * @regnum: The register on the MMD to read
496 * Same rules as for phy_read();
498 int phy_read_mmd(struct phy_device *phydev, int devad, u32 regnum)
502 phy_lock_mdio_bus(phydev);
503 ret = __phy_read_mmd(phydev, devad, regnum);
504 phy_unlock_mdio_bus(phydev);
508 EXPORT_SYMBOL(phy_read_mmd);
511 * __phy_write_mmd - Convenience function for writing a register
512 * on an MMD on a given PHY.
513 * @phydev: The phy_device struct
514 * @devad: The MMD to read from
515 * @regnum: The register on the MMD to read
516 * @val: value to write to @regnum
518 * Same rules as for __phy_write();
520 int __phy_write_mmd(struct phy_device *phydev, int devad, u32 regnum, u16 val)
524 if (regnum > (u16)~0 || devad > 32)
527 if (phydev->drv && phydev->drv->write_mmd) {
528 ret = phydev->drv->write_mmd(phydev, devad, regnum, val);
529 } else if (phydev->is_c45) {
530 ret = __mdiobus_c45_write(phydev->mdio.bus, phydev->mdio.addr,
533 struct mii_bus *bus = phydev->mdio.bus;
534 int phy_addr = phydev->mdio.addr;
536 mmd_phy_indirect(bus, phy_addr, devad, regnum);
538 /* Write the data into MMD's selected register */
539 __mdiobus_write(bus, phy_addr, MII_MMD_DATA, val);
545 EXPORT_SYMBOL(__phy_write_mmd);
548 * phy_write_mmd - Convenience function for writing a register
549 * on an MMD on a given PHY.
550 * @phydev: The phy_device struct
551 * @devad: The MMD to read from
552 * @regnum: The register on the MMD to read
553 * @val: value to write to @regnum
555 * Same rules as for phy_write();
557 int phy_write_mmd(struct phy_device *phydev, int devad, u32 regnum, u16 val)
561 phy_lock_mdio_bus(phydev);
562 ret = __phy_write_mmd(phydev, devad, regnum, val);
563 phy_unlock_mdio_bus(phydev);
567 EXPORT_SYMBOL(phy_write_mmd);
570 * phy_modify_changed - Function for modifying a PHY register
571 * @phydev: the phy_device struct
572 * @regnum: register number to modify
573 * @mask: bit mask of bits to clear
574 * @set: new value of bits set in mask to write to @regnum
576 * NOTE: MUST NOT be called from interrupt context,
577 * because the bus read/write functions may wait for an interrupt
578 * to conclude the operation.
580 * Returns negative errno, 0 if there was no change, and 1 in case of change
582 int phy_modify_changed(struct phy_device *phydev, u32 regnum, u16 mask, u16 set)
586 phy_lock_mdio_bus(phydev);
587 ret = __phy_modify_changed(phydev, regnum, mask, set);
588 phy_unlock_mdio_bus(phydev);
592 EXPORT_SYMBOL_GPL(phy_modify_changed);
595 * __phy_modify - Convenience function for modifying a PHY register
596 * @phydev: the phy_device struct
597 * @regnum: register number to modify
598 * @mask: bit mask of bits to clear
599 * @set: new value of bits set in mask to write to @regnum
601 * NOTE: MUST NOT be called from interrupt context,
602 * because the bus read/write functions may wait for an interrupt
603 * to conclude the operation.
605 int __phy_modify(struct phy_device *phydev, u32 regnum, u16 mask, u16 set)
609 ret = __phy_modify_changed(phydev, regnum, mask, set);
611 return ret < 0 ? ret : 0;
613 EXPORT_SYMBOL_GPL(__phy_modify);
616 * phy_modify - Convenience function for modifying a given PHY register
617 * @phydev: the phy_device struct
618 * @regnum: register number to write
619 * @mask: bit mask of bits to clear
620 * @set: new value of bits set in mask to write to @regnum
622 * NOTE: MUST NOT be called from interrupt context,
623 * because the bus read/write functions may wait for an interrupt
624 * to conclude the operation.
626 int phy_modify(struct phy_device *phydev, u32 regnum, u16 mask, u16 set)
630 phy_lock_mdio_bus(phydev);
631 ret = __phy_modify(phydev, regnum, mask, set);
632 phy_unlock_mdio_bus(phydev);
636 EXPORT_SYMBOL_GPL(phy_modify);
639 * __phy_modify_mmd_changed - Function for modifying a register on MMD
640 * @phydev: the phy_device struct
641 * @devad: the MMD containing register to modify
642 * @regnum: register number to modify
643 * @mask: bit mask of bits to clear
644 * @set: new value of bits set in mask to write to @regnum
646 * Unlocked helper function which allows a MMD register to be modified as
647 * new register value = (old register value & ~mask) | set
649 * Returns negative errno, 0 if there was no change, and 1 in case of change
651 int __phy_modify_mmd_changed(struct phy_device *phydev, int devad, u32 regnum,
656 ret = __phy_read_mmd(phydev, devad, regnum);
660 new = (ret & ~mask) | set;
664 ret = __phy_write_mmd(phydev, devad, regnum, new);
666 return ret < 0 ? ret : 1;
668 EXPORT_SYMBOL_GPL(__phy_modify_mmd_changed);
671 * phy_modify_mmd_changed - Function for modifying a register on MMD
672 * @phydev: the phy_device struct
673 * @devad: the MMD containing register to modify
674 * @regnum: register number to modify
675 * @mask: bit mask of bits to clear
676 * @set: new value of bits set in mask to write to @regnum
678 * NOTE: MUST NOT be called from interrupt context,
679 * because the bus read/write functions may wait for an interrupt
680 * to conclude the operation.
682 * Returns negative errno, 0 if there was no change, and 1 in case of change
684 int phy_modify_mmd_changed(struct phy_device *phydev, int devad, u32 regnum,
689 phy_lock_mdio_bus(phydev);
690 ret = __phy_modify_mmd_changed(phydev, devad, regnum, mask, set);
691 phy_unlock_mdio_bus(phydev);
695 EXPORT_SYMBOL_GPL(phy_modify_mmd_changed);
698 * __phy_modify_mmd - Convenience function for modifying a register on MMD
699 * @phydev: the phy_device struct
700 * @devad: the MMD containing register to modify
701 * @regnum: register number to modify
702 * @mask: bit mask of bits to clear
703 * @set: new value of bits set in mask to write to @regnum
705 * NOTE: MUST NOT be called from interrupt context,
706 * because the bus read/write functions may wait for an interrupt
707 * to conclude the operation.
709 int __phy_modify_mmd(struct phy_device *phydev, int devad, u32 regnum,
714 ret = __phy_modify_mmd_changed(phydev, devad, regnum, mask, set);
716 return ret < 0 ? ret : 0;
718 EXPORT_SYMBOL_GPL(__phy_modify_mmd);
721 * phy_modify_mmd - Convenience function for modifying a register on MMD
722 * @phydev: the phy_device struct
723 * @devad: the MMD containing register to modify
724 * @regnum: register number to modify
725 * @mask: bit mask of bits to clear
726 * @set: new value of bits set in mask to write to @regnum
728 * NOTE: MUST NOT be called from interrupt context,
729 * because the bus read/write functions may wait for an interrupt
730 * to conclude the operation.
732 int phy_modify_mmd(struct phy_device *phydev, int devad, u32 regnum,
737 phy_lock_mdio_bus(phydev);
738 ret = __phy_modify_mmd(phydev, devad, regnum, mask, set);
739 phy_unlock_mdio_bus(phydev);
743 EXPORT_SYMBOL_GPL(phy_modify_mmd);
745 static int __phy_read_page(struct phy_device *phydev)
747 if (WARN_ONCE(!phydev->drv->read_page, "read_page callback not available, PHY driver not loaded?\n"))
750 return phydev->drv->read_page(phydev);
753 static int __phy_write_page(struct phy_device *phydev, int page)
755 if (WARN_ONCE(!phydev->drv->write_page, "write_page callback not available, PHY driver not loaded?\n"))
758 return phydev->drv->write_page(phydev, page);
762 * phy_save_page() - take the bus lock and save the current page
763 * @phydev: a pointer to a &struct phy_device
765 * Take the MDIO bus lock, and return the current page number. On error,
766 * returns a negative errno. phy_restore_page() must always be called
767 * after this, irrespective of success or failure of this call.
769 int phy_save_page(struct phy_device *phydev)
771 phy_lock_mdio_bus(phydev);
772 return __phy_read_page(phydev);
774 EXPORT_SYMBOL_GPL(phy_save_page);
777 * phy_select_page() - take the bus lock, save the current page, and set a page
778 * @phydev: a pointer to a &struct phy_device
779 * @page: desired page
781 * Take the MDIO bus lock to protect against concurrent access, save the
782 * current PHY page, and set the current page. On error, returns a
783 * negative errno, otherwise returns the previous page number.
784 * phy_restore_page() must always be called after this, irrespective
785 * of success or failure of this call.
787 int phy_select_page(struct phy_device *phydev, int page)
791 oldpage = ret = phy_save_page(phydev);
795 if (oldpage != page) {
796 ret = __phy_write_page(phydev, page);
803 EXPORT_SYMBOL_GPL(phy_select_page);
806 * phy_restore_page() - restore the page register and release the bus lock
807 * @phydev: a pointer to a &struct phy_device
808 * @oldpage: the old page, return value from phy_save_page() or phy_select_page()
809 * @ret: operation's return code
811 * Release the MDIO bus lock, restoring @oldpage if it is a valid page.
812 * This function propagates the earliest error code from the group of
816 * @oldpage if it was a negative value, otherwise
817 * @ret if it was a negative errno value, otherwise
818 * phy_write_page()'s negative value if it were in error, otherwise
821 int phy_restore_page(struct phy_device *phydev, int oldpage, int ret)
826 r = __phy_write_page(phydev, oldpage);
828 /* Propagate the operation return code if the page write
831 if (ret >= 0 && r < 0)
834 /* Propagate the phy page selection error code */
838 phy_unlock_mdio_bus(phydev);
842 EXPORT_SYMBOL_GPL(phy_restore_page);
845 * phy_read_paged() - Convenience function for reading a paged register
846 * @phydev: a pointer to a &struct phy_device
847 * @page: the page for the phy
848 * @regnum: register number
850 * Same rules as for phy_read().
852 int phy_read_paged(struct phy_device *phydev, int page, u32 regnum)
854 int ret = 0, oldpage;
856 oldpage = phy_select_page(phydev, page);
858 ret = __phy_read(phydev, regnum);
860 return phy_restore_page(phydev, oldpage, ret);
862 EXPORT_SYMBOL(phy_read_paged);
865 * phy_write_paged() - Convenience function for writing a paged register
866 * @phydev: a pointer to a &struct phy_device
867 * @page: the page for the phy
868 * @regnum: register number
869 * @val: value to write
871 * Same rules as for phy_write().
873 int phy_write_paged(struct phy_device *phydev, int page, u32 regnum, u16 val)
875 int ret = 0, oldpage;
877 oldpage = phy_select_page(phydev, page);
879 ret = __phy_write(phydev, regnum, val);
881 return phy_restore_page(phydev, oldpage, ret);
883 EXPORT_SYMBOL(phy_write_paged);
886 * phy_modify_paged_changed() - Function for modifying a paged register
887 * @phydev: a pointer to a &struct phy_device
888 * @page: the page for the phy
889 * @regnum: register number
890 * @mask: bit mask of bits to clear
891 * @set: bit mask of bits to set
893 * Returns negative errno, 0 if there was no change, and 1 in case of change
895 int phy_modify_paged_changed(struct phy_device *phydev, int page, u32 regnum,
898 int ret = 0, oldpage;
900 oldpage = phy_select_page(phydev, page);
902 ret = __phy_modify_changed(phydev, regnum, mask, set);
904 return phy_restore_page(phydev, oldpage, ret);
906 EXPORT_SYMBOL(phy_modify_paged_changed);
909 * phy_modify_paged() - Convenience function for modifying a paged register
910 * @phydev: a pointer to a &struct phy_device
911 * @page: the page for the phy
912 * @regnum: register number
913 * @mask: bit mask of bits to clear
914 * @set: bit mask of bits to set
916 * Same rules as for phy_read() and phy_write().
918 int phy_modify_paged(struct phy_device *phydev, int page, u32 regnum,
921 int ret = phy_modify_paged_changed(phydev, page, regnum, mask, set);
923 return ret < 0 ? ret : 0;
925 EXPORT_SYMBOL(phy_modify_paged);