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 != 92,
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, 1000baseKX_Full ),
166 PHY_SETTING( 1000, FULL, 1000baseT_Full ),
167 PHY_SETTING( 1000, HALF, 1000baseT_Half ),
168 PHY_SETTING( 1000, FULL, 1000baseT1_Full ),
169 PHY_SETTING( 1000, FULL, 1000baseX_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 ),
183 * phy_lookup_setting - lookup a PHY setting
184 * @speed: speed to match
185 * @duplex: duplex to match
186 * @mask: allowed link modes
187 * @exact: an exact match is required
189 * Search the settings array for a setting that matches the speed and
190 * duplex, and which is supported.
192 * If @exact is unset, either an exact match or %NULL for no match will
195 * If @exact is set, an exact match, the fastest supported setting at
196 * or below the specified speed, the slowest supported setting, or if
197 * they all fail, %NULL will be returned.
199 const struct phy_setting *
200 phy_lookup_setting(int speed, int duplex, const unsigned long *mask, bool exact)
202 const struct phy_setting *p, *match = NULL, *last = NULL;
205 for (i = 0, p = settings; i < ARRAY_SIZE(settings); i++, p++) {
206 if (p->bit < __ETHTOOL_LINK_MODE_MASK_NBITS &&
207 test_bit(p->bit, mask)) {
209 if (p->speed == speed && p->duplex == duplex) {
210 /* Exact match for speed and duplex */
214 if (!match && p->speed <= speed)
218 if (p->speed < speed)
224 if (!match && !exact)
229 EXPORT_SYMBOL_GPL(phy_lookup_setting);
231 size_t phy_speeds(unsigned int *speeds, size_t size,
237 for (i = 0, count = 0; i < ARRAY_SIZE(settings) && count < size; i++)
238 if (settings[i].bit < __ETHTOOL_LINK_MODE_MASK_NBITS &&
239 test_bit(settings[i].bit, mask) &&
240 (count == 0 || speeds[count - 1] != settings[i].speed))
241 speeds[count++] = settings[i].speed;
246 static int __set_linkmode_max_speed(u32 max_speed, unsigned long *addr)
248 const struct phy_setting *p;
251 for (i = 0, p = settings; i < ARRAY_SIZE(settings); i++, p++) {
252 if (p->speed > max_speed)
253 linkmode_clear_bit(p->bit, addr);
261 static int __set_phy_supported(struct phy_device *phydev, u32 max_speed)
263 return __set_linkmode_max_speed(max_speed, phydev->supported);
267 * phy_set_max_speed - Set the maximum speed the PHY should support
269 * @phydev: The phy_device struct
270 * @max_speed: Maximum speed
272 * The PHY might be more capable than the MAC. For example a Fast Ethernet
273 * is connected to a 1G PHY. This function allows the MAC to indicate its
274 * maximum speed, and so limit what the PHY will advertise.
276 int phy_set_max_speed(struct phy_device *phydev, u32 max_speed)
280 err = __set_phy_supported(phydev, max_speed);
284 phy_advertise_supported(phydev);
288 EXPORT_SYMBOL(phy_set_max_speed);
290 void of_set_phy_supported(struct phy_device *phydev)
292 struct device_node *node = phydev->mdio.dev.of_node;
295 if (!IS_ENABLED(CONFIG_OF_MDIO))
301 if (!of_property_read_u32(node, "max-speed", &max_speed))
302 __set_phy_supported(phydev, max_speed);
305 void of_set_phy_eee_broken(struct phy_device *phydev)
307 struct device_node *node = phydev->mdio.dev.of_node;
310 if (!IS_ENABLED(CONFIG_OF_MDIO))
316 if (of_property_read_bool(node, "eee-broken-100tx"))
317 broken |= MDIO_EEE_100TX;
318 if (of_property_read_bool(node, "eee-broken-1000t"))
319 broken |= MDIO_EEE_1000T;
320 if (of_property_read_bool(node, "eee-broken-10gt"))
321 broken |= MDIO_EEE_10GT;
322 if (of_property_read_bool(node, "eee-broken-1000kx"))
323 broken |= MDIO_EEE_1000KX;
324 if (of_property_read_bool(node, "eee-broken-10gkx4"))
325 broken |= MDIO_EEE_10GKX4;
326 if (of_property_read_bool(node, "eee-broken-10gkr"))
327 broken |= MDIO_EEE_10GKR;
329 phydev->eee_broken_modes = broken;
333 * phy_resolve_aneg_pause - Determine pause autoneg results
335 * @phydev: The phy_device struct
337 * Once autoneg has completed the local pause settings can be
338 * resolved. Determine if pause and asymmetric pause should be used
342 void phy_resolve_aneg_pause(struct phy_device *phydev)
344 if (phydev->duplex == DUPLEX_FULL) {
345 phydev->pause = linkmode_test_bit(ETHTOOL_LINK_MODE_Pause_BIT,
346 phydev->lp_advertising);
347 phydev->asym_pause = linkmode_test_bit(
348 ETHTOOL_LINK_MODE_Asym_Pause_BIT,
349 phydev->lp_advertising);
352 EXPORT_SYMBOL_GPL(phy_resolve_aneg_pause);
355 * phy_resolve_aneg_linkmode - resolve the advertisements into PHY settings
356 * @phydev: The phy_device struct
358 * Resolve our and the link partner advertisements into their corresponding
359 * speed and duplex. If full duplex was negotiated, extract the pause mode
360 * from the link partner mask.
362 void phy_resolve_aneg_linkmode(struct phy_device *phydev)
364 __ETHTOOL_DECLARE_LINK_MODE_MASK(common);
367 linkmode_and(common, phydev->lp_advertising, phydev->advertising);
369 for (i = 0; i < ARRAY_SIZE(settings); i++)
370 if (test_bit(settings[i].bit, common)) {
371 phydev->speed = settings[i].speed;
372 phydev->duplex = settings[i].duplex;
376 phy_resolve_aneg_pause(phydev);
378 EXPORT_SYMBOL_GPL(phy_resolve_aneg_linkmode);
381 * phy_check_downshift - check whether downshift occurred
382 * @phydev: The phy_device struct
384 * Check whether a downshift to a lower speed occurred. If this should be the
385 * case warn the user.
386 * Prerequisite for detecting downshift is that PHY driver implements the
387 * read_status callback and sets phydev->speed to the actual link speed.
389 void phy_check_downshift(struct phy_device *phydev)
391 __ETHTOOL_DECLARE_LINK_MODE_MASK(common);
392 int i, speed = SPEED_UNKNOWN;
394 phydev->downshifted_rate = 0;
396 if (phydev->autoneg == AUTONEG_DISABLE ||
397 phydev->speed == SPEED_UNKNOWN)
400 linkmode_and(common, phydev->lp_advertising, phydev->advertising);
402 for (i = 0; i < ARRAY_SIZE(settings); i++)
403 if (test_bit(settings[i].bit, common)) {
404 speed = settings[i].speed;
408 if (speed == SPEED_UNKNOWN || phydev->speed >= speed)
411 phydev_warn(phydev, "Downshift occurred from negotiated speed %s to actual speed %s, check cabling!\n",
412 phy_speed_to_str(speed), phy_speed_to_str(phydev->speed));
414 phydev->downshifted_rate = 1;
416 EXPORT_SYMBOL_GPL(phy_check_downshift);
418 static int phy_resolve_min_speed(struct phy_device *phydev, bool fdx_only)
420 __ETHTOOL_DECLARE_LINK_MODE_MASK(common);
421 int i = ARRAY_SIZE(settings);
423 linkmode_and(common, phydev->lp_advertising, phydev->advertising);
426 if (test_bit(settings[i].bit, common)) {
427 if (fdx_only && settings[i].duplex != DUPLEX_FULL)
429 return settings[i].speed;
433 return SPEED_UNKNOWN;
436 int phy_speed_down_core(struct phy_device *phydev)
438 int min_common_speed = phy_resolve_min_speed(phydev, true);
440 if (min_common_speed == SPEED_UNKNOWN)
443 return __set_linkmode_max_speed(min_common_speed, phydev->advertising);
446 static void mmd_phy_indirect(struct mii_bus *bus, int phy_addr, int devad,
449 /* Write the desired MMD Devad */
450 __mdiobus_write(bus, phy_addr, MII_MMD_CTRL, devad);
452 /* Write the desired MMD register address */
453 __mdiobus_write(bus, phy_addr, MII_MMD_DATA, regnum);
455 /* Select the Function : DATA with no post increment */
456 __mdiobus_write(bus, phy_addr, MII_MMD_CTRL,
457 devad | MII_MMD_CTRL_NOINCR);
461 * __phy_read_mmd - Convenience function for reading a register
462 * from an MMD on a given PHY.
463 * @phydev: The phy_device struct
464 * @devad: The MMD to read from (0..31)
465 * @regnum: The register on the MMD to read (0..65535)
467 * Same rules as for __phy_read();
469 int __phy_read_mmd(struct phy_device *phydev, int devad, u32 regnum)
473 if (regnum > (u16)~0 || devad > 32)
476 if (phydev->drv && phydev->drv->read_mmd) {
477 val = phydev->drv->read_mmd(phydev, devad, regnum);
478 } else if (phydev->is_c45) {
479 val = __mdiobus_c45_read(phydev->mdio.bus, phydev->mdio.addr,
482 struct mii_bus *bus = phydev->mdio.bus;
483 int phy_addr = phydev->mdio.addr;
485 mmd_phy_indirect(bus, phy_addr, devad, regnum);
487 /* Read the content of the MMD's selected register */
488 val = __mdiobus_read(bus, phy_addr, MII_MMD_DATA);
492 EXPORT_SYMBOL(__phy_read_mmd);
495 * phy_read_mmd - Convenience function for reading a register
496 * from an MMD on a given PHY.
497 * @phydev: The phy_device struct
498 * @devad: The MMD to read from
499 * @regnum: The register on the MMD to read
501 * Same rules as for phy_read();
503 int phy_read_mmd(struct phy_device *phydev, int devad, u32 regnum)
507 phy_lock_mdio_bus(phydev);
508 ret = __phy_read_mmd(phydev, devad, regnum);
509 phy_unlock_mdio_bus(phydev);
513 EXPORT_SYMBOL(phy_read_mmd);
516 * __phy_write_mmd - Convenience function for writing a register
517 * on an MMD on a given PHY.
518 * @phydev: The phy_device struct
519 * @devad: The MMD to read from
520 * @regnum: The register on the MMD to read
521 * @val: value to write to @regnum
523 * Same rules as for __phy_write();
525 int __phy_write_mmd(struct phy_device *phydev, int devad, u32 regnum, u16 val)
529 if (regnum > (u16)~0 || devad > 32)
532 if (phydev->drv && phydev->drv->write_mmd) {
533 ret = phydev->drv->write_mmd(phydev, devad, regnum, val);
534 } else if (phydev->is_c45) {
535 ret = __mdiobus_c45_write(phydev->mdio.bus, phydev->mdio.addr,
538 struct mii_bus *bus = phydev->mdio.bus;
539 int phy_addr = phydev->mdio.addr;
541 mmd_phy_indirect(bus, phy_addr, devad, regnum);
543 /* Write the data into MMD's selected register */
544 __mdiobus_write(bus, phy_addr, MII_MMD_DATA, val);
550 EXPORT_SYMBOL(__phy_write_mmd);
553 * phy_write_mmd - Convenience function for writing a register
554 * on an MMD on a given PHY.
555 * @phydev: The phy_device struct
556 * @devad: The MMD to read from
557 * @regnum: The register on the MMD to read
558 * @val: value to write to @regnum
560 * Same rules as for phy_write();
562 int phy_write_mmd(struct phy_device *phydev, int devad, u32 regnum, u16 val)
566 phy_lock_mdio_bus(phydev);
567 ret = __phy_write_mmd(phydev, devad, regnum, val);
568 phy_unlock_mdio_bus(phydev);
572 EXPORT_SYMBOL(phy_write_mmd);
575 * phy_modify_changed - Function for modifying a PHY register
576 * @phydev: the phy_device struct
577 * @regnum: register number to modify
578 * @mask: bit mask of bits to clear
579 * @set: new value of bits set in mask to write to @regnum
581 * NOTE: MUST NOT be called from interrupt context,
582 * because the bus read/write functions may wait for an interrupt
583 * to conclude the operation.
585 * Returns negative errno, 0 if there was no change, and 1 in case of change
587 int phy_modify_changed(struct phy_device *phydev, u32 regnum, u16 mask, u16 set)
591 phy_lock_mdio_bus(phydev);
592 ret = __phy_modify_changed(phydev, regnum, mask, set);
593 phy_unlock_mdio_bus(phydev);
597 EXPORT_SYMBOL_GPL(phy_modify_changed);
600 * __phy_modify - Convenience function for modifying a PHY register
601 * @phydev: the phy_device struct
602 * @regnum: register number to modify
603 * @mask: bit mask of bits to clear
604 * @set: new value of bits set in mask to write to @regnum
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.
610 int __phy_modify(struct phy_device *phydev, u32 regnum, u16 mask, u16 set)
614 ret = __phy_modify_changed(phydev, regnum, mask, set);
616 return ret < 0 ? ret : 0;
618 EXPORT_SYMBOL_GPL(__phy_modify);
621 * phy_modify - Convenience function for modifying a given PHY register
622 * @phydev: the phy_device struct
623 * @regnum: register number to write
624 * @mask: bit mask of bits to clear
625 * @set: new value of bits set in mask to write to @regnum
627 * NOTE: MUST NOT be called from interrupt context,
628 * because the bus read/write functions may wait for an interrupt
629 * to conclude the operation.
631 int phy_modify(struct phy_device *phydev, u32 regnum, u16 mask, u16 set)
635 phy_lock_mdio_bus(phydev);
636 ret = __phy_modify(phydev, regnum, mask, set);
637 phy_unlock_mdio_bus(phydev);
641 EXPORT_SYMBOL_GPL(phy_modify);
644 * __phy_modify_mmd_changed - Function for modifying a register on MMD
645 * @phydev: the phy_device struct
646 * @devad: the MMD containing register to modify
647 * @regnum: register number to modify
648 * @mask: bit mask of bits to clear
649 * @set: new value of bits set in mask to write to @regnum
651 * Unlocked helper function which allows a MMD register to be modified as
652 * new register value = (old register value & ~mask) | set
654 * Returns negative errno, 0 if there was no change, and 1 in case of change
656 int __phy_modify_mmd_changed(struct phy_device *phydev, int devad, u32 regnum,
661 ret = __phy_read_mmd(phydev, devad, regnum);
665 new = (ret & ~mask) | set;
669 ret = __phy_write_mmd(phydev, devad, regnum, new);
671 return ret < 0 ? ret : 1;
673 EXPORT_SYMBOL_GPL(__phy_modify_mmd_changed);
676 * phy_modify_mmd_changed - Function for modifying a register on MMD
677 * @phydev: the phy_device struct
678 * @devad: the MMD containing register to modify
679 * @regnum: register number to modify
680 * @mask: bit mask of bits to clear
681 * @set: new value of bits set in mask to write to @regnum
683 * NOTE: MUST NOT be called from interrupt context,
684 * because the bus read/write functions may wait for an interrupt
685 * to conclude the operation.
687 * Returns negative errno, 0 if there was no change, and 1 in case of change
689 int phy_modify_mmd_changed(struct phy_device *phydev, int devad, u32 regnum,
694 phy_lock_mdio_bus(phydev);
695 ret = __phy_modify_mmd_changed(phydev, devad, regnum, mask, set);
696 phy_unlock_mdio_bus(phydev);
700 EXPORT_SYMBOL_GPL(phy_modify_mmd_changed);
703 * __phy_modify_mmd - Convenience function for modifying a register on MMD
704 * @phydev: the phy_device struct
705 * @devad: the MMD containing register to modify
706 * @regnum: register number to modify
707 * @mask: bit mask of bits to clear
708 * @set: new value of bits set in mask to write to @regnum
710 * NOTE: MUST NOT be called from interrupt context,
711 * because the bus read/write functions may wait for an interrupt
712 * to conclude the operation.
714 int __phy_modify_mmd(struct phy_device *phydev, int devad, u32 regnum,
719 ret = __phy_modify_mmd_changed(phydev, devad, regnum, mask, set);
721 return ret < 0 ? ret : 0;
723 EXPORT_SYMBOL_GPL(__phy_modify_mmd);
726 * phy_modify_mmd - Convenience function for modifying a register on MMD
727 * @phydev: the phy_device struct
728 * @devad: the MMD containing register to modify
729 * @regnum: register number to modify
730 * @mask: bit mask of bits to clear
731 * @set: new value of bits set in mask to write to @regnum
733 * NOTE: MUST NOT be called from interrupt context,
734 * because the bus read/write functions may wait for an interrupt
735 * to conclude the operation.
737 int phy_modify_mmd(struct phy_device *phydev, int devad, u32 regnum,
742 phy_lock_mdio_bus(phydev);
743 ret = __phy_modify_mmd(phydev, devad, regnum, mask, set);
744 phy_unlock_mdio_bus(phydev);
748 EXPORT_SYMBOL_GPL(phy_modify_mmd);
750 static int __phy_read_page(struct phy_device *phydev)
752 if (WARN_ONCE(!phydev->drv->read_page, "read_page callback not available, PHY driver not loaded?\n"))
755 return phydev->drv->read_page(phydev);
758 static int __phy_write_page(struct phy_device *phydev, int page)
760 if (WARN_ONCE(!phydev->drv->write_page, "write_page callback not available, PHY driver not loaded?\n"))
763 return phydev->drv->write_page(phydev, page);
767 * phy_save_page() - take the bus lock and save the current page
768 * @phydev: a pointer to a &struct phy_device
770 * Take the MDIO bus lock, and return the current page number. On error,
771 * returns a negative errno. phy_restore_page() must always be called
772 * after this, irrespective of success or failure of this call.
774 int phy_save_page(struct phy_device *phydev)
776 phy_lock_mdio_bus(phydev);
777 return __phy_read_page(phydev);
779 EXPORT_SYMBOL_GPL(phy_save_page);
782 * phy_select_page() - take the bus lock, save the current page, and set a page
783 * @phydev: a pointer to a &struct phy_device
784 * @page: desired page
786 * Take the MDIO bus lock to protect against concurrent access, save the
787 * current PHY page, and set the current page. On error, returns a
788 * negative errno, otherwise returns the previous page number.
789 * phy_restore_page() must always be called after this, irrespective
790 * of success or failure of this call.
792 int phy_select_page(struct phy_device *phydev, int page)
796 oldpage = ret = phy_save_page(phydev);
800 if (oldpage != page) {
801 ret = __phy_write_page(phydev, page);
808 EXPORT_SYMBOL_GPL(phy_select_page);
811 * phy_restore_page() - restore the page register and release the bus lock
812 * @phydev: a pointer to a &struct phy_device
813 * @oldpage: the old page, return value from phy_save_page() or phy_select_page()
814 * @ret: operation's return code
816 * Release the MDIO bus lock, restoring @oldpage if it is a valid page.
817 * This function propagates the earliest error code from the group of
821 * @oldpage if it was a negative value, otherwise
822 * @ret if it was a negative errno value, otherwise
823 * phy_write_page()'s negative value if it were in error, otherwise
826 int phy_restore_page(struct phy_device *phydev, int oldpage, int ret)
831 r = __phy_write_page(phydev, oldpage);
833 /* Propagate the operation return code if the page write
836 if (ret >= 0 && r < 0)
839 /* Propagate the phy page selection error code */
843 phy_unlock_mdio_bus(phydev);
847 EXPORT_SYMBOL_GPL(phy_restore_page);
850 * phy_read_paged() - Convenience function for reading a paged register
851 * @phydev: a pointer to a &struct phy_device
852 * @page: the page for the phy
853 * @regnum: register number
855 * Same rules as for phy_read().
857 int phy_read_paged(struct phy_device *phydev, int page, u32 regnum)
859 int ret = 0, oldpage;
861 oldpage = phy_select_page(phydev, page);
863 ret = __phy_read(phydev, regnum);
865 return phy_restore_page(phydev, oldpage, ret);
867 EXPORT_SYMBOL(phy_read_paged);
870 * phy_write_paged() - Convenience function for writing a paged register
871 * @phydev: a pointer to a &struct phy_device
872 * @page: the page for the phy
873 * @regnum: register number
874 * @val: value to write
876 * Same rules as for phy_write().
878 int phy_write_paged(struct phy_device *phydev, int page, u32 regnum, u16 val)
880 int ret = 0, oldpage;
882 oldpage = phy_select_page(phydev, page);
884 ret = __phy_write(phydev, regnum, val);
886 return phy_restore_page(phydev, oldpage, ret);
888 EXPORT_SYMBOL(phy_write_paged);
891 * phy_modify_paged_changed() - Function for modifying a paged register
892 * @phydev: a pointer to a &struct phy_device
893 * @page: the page for the phy
894 * @regnum: register number
895 * @mask: bit mask of bits to clear
896 * @set: bit mask of bits to set
898 * Returns negative errno, 0 if there was no change, and 1 in case of change
900 int phy_modify_paged_changed(struct phy_device *phydev, int page, u32 regnum,
903 int ret = 0, oldpage;
905 oldpage = phy_select_page(phydev, page);
907 ret = __phy_modify_changed(phydev, regnum, mask, set);
909 return phy_restore_page(phydev, oldpage, ret);
911 EXPORT_SYMBOL(phy_modify_paged_changed);
914 * phy_modify_paged() - Convenience function for modifying a paged register
915 * @phydev: a pointer to a &struct phy_device
916 * @page: the page for the phy
917 * @regnum: register number
918 * @mask: bit mask of bits to clear
919 * @set: bit mask of bits to set
921 * Same rules as for phy_read() and phy_write().
923 int phy_modify_paged(struct phy_device *phydev, int page, u32 regnum,
926 int ret = phy_modify_paged_changed(phydev, page, regnum, mask, set);
928 return ret < 0 ? ret : 0;
930 EXPORT_SYMBOL(phy_modify_paged);