1 // SPDX-License-Identifier: GPL-2.0+
3 * Core PHY library, taken from phy.c
5 #include <linux/export.h>
9 const char *phy_speed_to_str(int speed
)
11 BUILD_BUG_ON_MSG(__ETHTOOL_LINK_MODE_MASK_NBITS
!= 74,
12 "Enum ethtool_link_mode_bit_indices and phylib are out of sync. "
13 "If a speed or mode has been added please update phy_speed_to_str "
14 "and the PHY settings array.\n");
50 return "Unsupported (update phy-core.c)";
53 EXPORT_SYMBOL_GPL(phy_speed_to_str
);
55 const char *phy_duplex_to_str(unsigned int duplex
)
57 if (duplex
== DUPLEX_HALF
)
59 if (duplex
== DUPLEX_FULL
)
61 if (duplex
== DUPLEX_UNKNOWN
)
63 return "Unsupported (update phy-core.c)";
65 EXPORT_SYMBOL_GPL(phy_duplex_to_str
);
67 /* A mapping of all SUPPORTED settings to speed/duplex. This table
68 * must be grouped by speed and sorted in descending match priority
69 * - iow, descending speed. */
71 #define PHY_SETTING(s, d, b) { .speed = SPEED_ ## s, .duplex = DUPLEX_ ## d, \
72 .bit = ETHTOOL_LINK_MODE_ ## b ## _BIT}
74 static const struct phy_setting settings
[] = {
76 PHY_SETTING( 400000, FULL
, 400000baseCR8_Full
),
77 PHY_SETTING( 400000, FULL
, 400000baseKR8_Full
),
78 PHY_SETTING( 400000, FULL
, 400000baseLR8_ER8_FR8_Full
),
79 PHY_SETTING( 400000, FULL
, 400000baseDR8_Full
),
80 PHY_SETTING( 400000, FULL
, 400000baseSR8_Full
),
82 PHY_SETTING( 200000, FULL
, 200000baseCR4_Full
),
83 PHY_SETTING( 200000, FULL
, 200000baseKR4_Full
),
84 PHY_SETTING( 200000, FULL
, 200000baseLR4_ER4_FR4_Full
),
85 PHY_SETTING( 200000, FULL
, 200000baseDR4_Full
),
86 PHY_SETTING( 200000, FULL
, 200000baseSR4_Full
),
88 PHY_SETTING( 100000, FULL
, 100000baseCR4_Full
),
89 PHY_SETTING( 100000, FULL
, 100000baseKR4_Full
),
90 PHY_SETTING( 100000, FULL
, 100000baseLR4_ER4_Full
),
91 PHY_SETTING( 100000, FULL
, 100000baseSR4_Full
),
92 PHY_SETTING( 100000, FULL
, 100000baseCR2_Full
),
93 PHY_SETTING( 100000, FULL
, 100000baseKR2_Full
),
94 PHY_SETTING( 100000, FULL
, 100000baseLR2_ER2_FR2_Full
),
95 PHY_SETTING( 100000, FULL
, 100000baseDR2_Full
),
96 PHY_SETTING( 100000, FULL
, 100000baseSR2_Full
),
98 PHY_SETTING( 56000, FULL
, 56000baseCR4_Full
),
99 PHY_SETTING( 56000, FULL
, 56000baseKR4_Full
),
100 PHY_SETTING( 56000, FULL
, 56000baseLR4_Full
),
101 PHY_SETTING( 56000, FULL
, 56000baseSR4_Full
),
103 PHY_SETTING( 50000, FULL
, 50000baseCR2_Full
),
104 PHY_SETTING( 50000, FULL
, 50000baseKR2_Full
),
105 PHY_SETTING( 50000, FULL
, 50000baseSR2_Full
),
106 PHY_SETTING( 50000, FULL
, 50000baseCR_Full
),
107 PHY_SETTING( 50000, FULL
, 50000baseKR_Full
),
108 PHY_SETTING( 50000, FULL
, 50000baseLR_ER_FR_Full
),
109 PHY_SETTING( 50000, FULL
, 50000baseDR_Full
),
110 PHY_SETTING( 50000, FULL
, 50000baseSR_Full
),
112 PHY_SETTING( 40000, FULL
, 40000baseCR4_Full
),
113 PHY_SETTING( 40000, FULL
, 40000baseKR4_Full
),
114 PHY_SETTING( 40000, FULL
, 40000baseLR4_Full
),
115 PHY_SETTING( 40000, FULL
, 40000baseSR4_Full
),
117 PHY_SETTING( 25000, FULL
, 25000baseCR_Full
),
118 PHY_SETTING( 25000, FULL
, 25000baseKR_Full
),
119 PHY_SETTING( 25000, FULL
, 25000baseSR_Full
),
121 PHY_SETTING( 20000, FULL
, 20000baseKR2_Full
),
122 PHY_SETTING( 20000, FULL
, 20000baseMLD2_Full
),
124 PHY_SETTING( 10000, FULL
, 10000baseCR_Full
),
125 PHY_SETTING( 10000, FULL
, 10000baseER_Full
),
126 PHY_SETTING( 10000, FULL
, 10000baseKR_Full
),
127 PHY_SETTING( 10000, FULL
, 10000baseKX4_Full
),
128 PHY_SETTING( 10000, FULL
, 10000baseLR_Full
),
129 PHY_SETTING( 10000, FULL
, 10000baseLRM_Full
),
130 PHY_SETTING( 10000, FULL
, 10000baseR_FEC
),
131 PHY_SETTING( 10000, FULL
, 10000baseSR_Full
),
132 PHY_SETTING( 10000, FULL
, 10000baseT_Full
),
134 PHY_SETTING( 5000, FULL
, 5000baseT_Full
),
136 PHY_SETTING( 2500, FULL
, 2500baseT_Full
),
137 PHY_SETTING( 2500, FULL
, 2500baseX_Full
),
139 PHY_SETTING( 1000, FULL
, 1000baseKX_Full
),
140 PHY_SETTING( 1000, FULL
, 1000baseT_Full
),
141 PHY_SETTING( 1000, HALF
, 1000baseT_Half
),
142 PHY_SETTING( 1000, FULL
, 1000baseT1_Full
),
143 PHY_SETTING( 1000, FULL
, 1000baseX_Full
),
145 PHY_SETTING( 100, FULL
, 100baseT_Full
),
146 PHY_SETTING( 100, FULL
, 100baseT1_Full
),
147 PHY_SETTING( 100, HALF
, 100baseT_Half
),
149 PHY_SETTING( 10, FULL
, 10baseT_Full
),
150 PHY_SETTING( 10, HALF
, 10baseT_Half
),
155 * phy_lookup_setting - lookup a PHY setting
156 * @speed: speed to match
157 * @duplex: duplex to match
158 * @mask: allowed link modes
159 * @exact: an exact match is required
161 * Search the settings array for a setting that matches the speed and
162 * duplex, and which is supported.
164 * If @exact is unset, either an exact match or %NULL for no match will
167 * If @exact is set, an exact match, the fastest supported setting at
168 * or below the specified speed, the slowest supported setting, or if
169 * they all fail, %NULL will be returned.
171 const struct phy_setting
*
172 phy_lookup_setting(int speed
, int duplex
, const unsigned long *mask
, bool exact
)
174 const struct phy_setting
*p
, *match
= NULL
, *last
= NULL
;
177 for (i
= 0, p
= settings
; i
< ARRAY_SIZE(settings
); i
++, p
++) {
178 if (p
->bit
< __ETHTOOL_LINK_MODE_MASK_NBITS
&&
179 test_bit(p
->bit
, mask
)) {
181 if (p
->speed
== speed
&& p
->duplex
== duplex
) {
182 /* Exact match for speed and duplex */
186 if (!match
&& p
->speed
<= speed
)
190 if (p
->speed
< speed
)
196 if (!match
&& !exact
)
201 EXPORT_SYMBOL_GPL(phy_lookup_setting
);
203 size_t phy_speeds(unsigned int *speeds
, size_t size
,
209 for (i
= 0, count
= 0; i
< ARRAY_SIZE(settings
) && count
< size
; i
++)
210 if (settings
[i
].bit
< __ETHTOOL_LINK_MODE_MASK_NBITS
&&
211 test_bit(settings
[i
].bit
, mask
) &&
212 (count
== 0 || speeds
[count
- 1] != settings
[i
].speed
))
213 speeds
[count
++] = settings
[i
].speed
;
218 static int __set_linkmode_max_speed(u32 max_speed
, unsigned long *addr
)
220 const struct phy_setting
*p
;
223 for (i
= 0, p
= settings
; i
< ARRAY_SIZE(settings
); i
++, p
++) {
224 if (p
->speed
> max_speed
)
225 linkmode_clear_bit(p
->bit
, addr
);
233 static int __set_phy_supported(struct phy_device
*phydev
, u32 max_speed
)
235 return __set_linkmode_max_speed(max_speed
, phydev
->supported
);
238 int phy_set_max_speed(struct phy_device
*phydev
, u32 max_speed
)
242 err
= __set_phy_supported(phydev
, max_speed
);
246 phy_advertise_supported(phydev
);
250 EXPORT_SYMBOL(phy_set_max_speed
);
252 void of_set_phy_supported(struct phy_device
*phydev
)
254 struct device_node
*node
= phydev
->mdio
.dev
.of_node
;
257 if (!IS_ENABLED(CONFIG_OF_MDIO
))
263 if (!of_property_read_u32(node
, "max-speed", &max_speed
))
264 __set_phy_supported(phydev
, max_speed
);
267 void of_set_phy_eee_broken(struct phy_device
*phydev
)
269 struct device_node
*node
= phydev
->mdio
.dev
.of_node
;
272 if (!IS_ENABLED(CONFIG_OF_MDIO
))
278 if (of_property_read_bool(node
, "eee-broken-100tx"))
279 broken
|= MDIO_EEE_100TX
;
280 if (of_property_read_bool(node
, "eee-broken-1000t"))
281 broken
|= MDIO_EEE_1000T
;
282 if (of_property_read_bool(node
, "eee-broken-10gt"))
283 broken
|= MDIO_EEE_10GT
;
284 if (of_property_read_bool(node
, "eee-broken-1000kx"))
285 broken
|= MDIO_EEE_1000KX
;
286 if (of_property_read_bool(node
, "eee-broken-10gkx4"))
287 broken
|= MDIO_EEE_10GKX4
;
288 if (of_property_read_bool(node
, "eee-broken-10gkr"))
289 broken
|= MDIO_EEE_10GKR
;
291 phydev
->eee_broken_modes
= broken
;
294 void phy_resolve_aneg_pause(struct phy_device
*phydev
)
296 if (phydev
->duplex
== DUPLEX_FULL
) {
297 phydev
->pause
= linkmode_test_bit(ETHTOOL_LINK_MODE_Pause_BIT
,
298 phydev
->lp_advertising
);
299 phydev
->asym_pause
= linkmode_test_bit(
300 ETHTOOL_LINK_MODE_Asym_Pause_BIT
,
301 phydev
->lp_advertising
);
304 EXPORT_SYMBOL_GPL(phy_resolve_aneg_pause
);
307 * phy_resolve_aneg_linkmode - resolve the advertisements into phy settings
308 * @phydev: The phy_device struct
310 * Resolve our and the link partner advertisements into their corresponding
311 * speed and duplex. If full duplex was negotiated, extract the pause mode
312 * from the link partner mask.
314 void phy_resolve_aneg_linkmode(struct phy_device
*phydev
)
316 __ETHTOOL_DECLARE_LINK_MODE_MASK(common
);
319 linkmode_and(common
, phydev
->lp_advertising
, phydev
->advertising
);
321 for (i
= 0; i
< ARRAY_SIZE(settings
); i
++)
322 if (test_bit(settings
[i
].bit
, common
)) {
323 phydev
->speed
= settings
[i
].speed
;
324 phydev
->duplex
= settings
[i
].duplex
;
328 phy_resolve_aneg_pause(phydev
);
330 EXPORT_SYMBOL_GPL(phy_resolve_aneg_linkmode
);
332 static int phy_resolve_min_speed(struct phy_device
*phydev
, bool fdx_only
)
334 __ETHTOOL_DECLARE_LINK_MODE_MASK(common
);
335 int i
= ARRAY_SIZE(settings
);
337 linkmode_and(common
, phydev
->lp_advertising
, phydev
->advertising
);
340 if (test_bit(settings
[i
].bit
, common
)) {
341 if (fdx_only
&& settings
[i
].duplex
!= DUPLEX_FULL
)
343 return settings
[i
].speed
;
347 return SPEED_UNKNOWN
;
350 int phy_speed_down_core(struct phy_device
*phydev
)
352 int min_common_speed
= phy_resolve_min_speed(phydev
, true);
354 if (min_common_speed
== SPEED_UNKNOWN
)
357 return __set_linkmode_max_speed(min_common_speed
, phydev
->advertising
);
360 static void mmd_phy_indirect(struct mii_bus
*bus
, int phy_addr
, int devad
,
363 /* Write the desired MMD Devad */
364 __mdiobus_write(bus
, phy_addr
, MII_MMD_CTRL
, devad
);
366 /* Write the desired MMD register address */
367 __mdiobus_write(bus
, phy_addr
, MII_MMD_DATA
, regnum
);
369 /* Select the Function : DATA with no post increment */
370 __mdiobus_write(bus
, phy_addr
, MII_MMD_CTRL
,
371 devad
| MII_MMD_CTRL_NOINCR
);
375 * __phy_read_mmd - Convenience function for reading a register
376 * from an MMD on a given PHY.
377 * @phydev: The phy_device struct
378 * @devad: The MMD to read from (0..31)
379 * @regnum: The register on the MMD to read (0..65535)
381 * Same rules as for __phy_read();
383 int __phy_read_mmd(struct phy_device
*phydev
, int devad
, u32 regnum
)
387 if (regnum
> (u16
)~0 || devad
> 32)
390 if (phydev
->drv
&& phydev
->drv
->read_mmd
) {
391 val
= phydev
->drv
->read_mmd(phydev
, devad
, regnum
);
392 } else if (phydev
->is_c45
) {
393 u32 addr
= MII_ADDR_C45
| (devad
<< 16) | (regnum
& 0xffff);
395 val
= __mdiobus_read(phydev
->mdio
.bus
, phydev
->mdio
.addr
, addr
);
397 struct mii_bus
*bus
= phydev
->mdio
.bus
;
398 int phy_addr
= phydev
->mdio
.addr
;
400 mmd_phy_indirect(bus
, phy_addr
, devad
, regnum
);
402 /* Read the content of the MMD's selected register */
403 val
= __mdiobus_read(bus
, phy_addr
, MII_MMD_DATA
);
407 EXPORT_SYMBOL(__phy_read_mmd
);
410 * phy_read_mmd - Convenience function for reading a register
411 * from an MMD on a given PHY.
412 * @phydev: The phy_device struct
413 * @devad: The MMD to read from
414 * @regnum: The register on the MMD to read
416 * Same rules as for phy_read();
418 int phy_read_mmd(struct phy_device
*phydev
, int devad
, u32 regnum
)
422 phy_lock_mdio_bus(phydev
);
423 ret
= __phy_read_mmd(phydev
, devad
, regnum
);
424 phy_unlock_mdio_bus(phydev
);
428 EXPORT_SYMBOL(phy_read_mmd
);
431 * __phy_write_mmd - Convenience function for writing a register
432 * on an MMD on a given PHY.
433 * @phydev: The phy_device struct
434 * @devad: The MMD to read from
435 * @regnum: The register on the MMD to read
436 * @val: value to write to @regnum
438 * Same rules as for __phy_write();
440 int __phy_write_mmd(struct phy_device
*phydev
, int devad
, u32 regnum
, u16 val
)
444 if (regnum
> (u16
)~0 || devad
> 32)
447 if (phydev
->drv
&& phydev
->drv
->write_mmd
) {
448 ret
= phydev
->drv
->write_mmd(phydev
, devad
, regnum
, val
);
449 } else if (phydev
->is_c45
) {
450 u32 addr
= MII_ADDR_C45
| (devad
<< 16) | (regnum
& 0xffff);
452 ret
= __mdiobus_write(phydev
->mdio
.bus
, phydev
->mdio
.addr
,
455 struct mii_bus
*bus
= phydev
->mdio
.bus
;
456 int phy_addr
= phydev
->mdio
.addr
;
458 mmd_phy_indirect(bus
, phy_addr
, devad
, regnum
);
460 /* Write the data into MMD's selected register */
461 __mdiobus_write(bus
, phy_addr
, MII_MMD_DATA
, val
);
467 EXPORT_SYMBOL(__phy_write_mmd
);
470 * phy_write_mmd - Convenience function for writing a register
471 * on an MMD on a given PHY.
472 * @phydev: The phy_device struct
473 * @devad: The MMD to read from
474 * @regnum: The register on the MMD to read
475 * @val: value to write to @regnum
477 * Same rules as for phy_write();
479 int phy_write_mmd(struct phy_device
*phydev
, int devad
, u32 regnum
, u16 val
)
483 phy_lock_mdio_bus(phydev
);
484 ret
= __phy_write_mmd(phydev
, devad
, regnum
, val
);
485 phy_unlock_mdio_bus(phydev
);
489 EXPORT_SYMBOL(phy_write_mmd
);
492 * __phy_modify_changed() - Convenience function for modifying a PHY register
493 * @phydev: a pointer to a &struct phy_device
494 * @regnum: register number
495 * @mask: bit mask of bits to clear
496 * @set: bit mask of bits to set
498 * Unlocked helper function which allows a PHY register to be modified as
499 * new register value = (old register value & ~mask) | set
501 * Returns negative errno, 0 if there was no change, and 1 in case of change
503 int __phy_modify_changed(struct phy_device
*phydev
, u32 regnum
, u16 mask
,
508 ret
= __phy_read(phydev
, regnum
);
512 new = (ret
& ~mask
) | set
;
516 ret
= __phy_write(phydev
, regnum
, new);
518 return ret
< 0 ? ret
: 1;
520 EXPORT_SYMBOL_GPL(__phy_modify_changed
);
523 * phy_modify_changed - Function for modifying a PHY register
524 * @phydev: the phy_device struct
525 * @regnum: register number to modify
526 * @mask: bit mask of bits to clear
527 * @set: new value of bits set in mask to write to @regnum
529 * NOTE: MUST NOT be called from interrupt context,
530 * because the bus read/write functions may wait for an interrupt
531 * to conclude the operation.
533 * Returns negative errno, 0 if there was no change, and 1 in case of change
535 int phy_modify_changed(struct phy_device
*phydev
, u32 regnum
, u16 mask
, u16 set
)
539 phy_lock_mdio_bus(phydev
);
540 ret
= __phy_modify_changed(phydev
, regnum
, mask
, set
);
541 phy_unlock_mdio_bus(phydev
);
545 EXPORT_SYMBOL_GPL(phy_modify_changed
);
548 * __phy_modify - Convenience function for modifying a PHY register
549 * @phydev: the phy_device struct
550 * @regnum: register number to modify
551 * @mask: bit mask of bits to clear
552 * @set: new value of bits set in mask to write to @regnum
554 * NOTE: MUST NOT be called from interrupt context,
555 * because the bus read/write functions may wait for an interrupt
556 * to conclude the operation.
558 int __phy_modify(struct phy_device
*phydev
, u32 regnum
, u16 mask
, u16 set
)
562 ret
= __phy_modify_changed(phydev
, regnum
, mask
, set
);
564 return ret
< 0 ? ret
: 0;
566 EXPORT_SYMBOL_GPL(__phy_modify
);
569 * phy_modify - Convenience function for modifying a given PHY register
570 * @phydev: the phy_device struct
571 * @regnum: register number to write
572 * @mask: bit mask of bits to clear
573 * @set: new value of bits set in mask to write to @regnum
575 * NOTE: MUST NOT be called from interrupt context,
576 * because the bus read/write functions may wait for an interrupt
577 * to conclude the operation.
579 int phy_modify(struct phy_device
*phydev
, u32 regnum
, u16 mask
, u16 set
)
583 phy_lock_mdio_bus(phydev
);
584 ret
= __phy_modify(phydev
, regnum
, mask
, set
);
585 phy_unlock_mdio_bus(phydev
);
589 EXPORT_SYMBOL_GPL(phy_modify
);
592 * __phy_modify_mmd_changed - Function for modifying a register on MMD
593 * @phydev: the phy_device struct
594 * @devad: the MMD containing register to modify
595 * @regnum: register number to modify
596 * @mask: bit mask of bits to clear
597 * @set: new value of bits set in mask to write to @regnum
599 * Unlocked helper function which allows a MMD register to be modified as
600 * new register value = (old register value & ~mask) | set
602 * Returns negative errno, 0 if there was no change, and 1 in case of change
604 int __phy_modify_mmd_changed(struct phy_device
*phydev
, int devad
, u32 regnum
,
609 ret
= __phy_read_mmd(phydev
, devad
, regnum
);
613 new = (ret
& ~mask
) | set
;
617 ret
= __phy_write_mmd(phydev
, devad
, regnum
, new);
619 return ret
< 0 ? ret
: 1;
621 EXPORT_SYMBOL_GPL(__phy_modify_mmd_changed
);
624 * phy_modify_mmd_changed - Function for modifying a register on MMD
625 * @phydev: the phy_device struct
626 * @devad: the MMD containing register to modify
627 * @regnum: register number to modify
628 * @mask: bit mask of bits to clear
629 * @set: new value of bits set in mask to write to @regnum
631 * NOTE: MUST NOT be called from interrupt context,
632 * because the bus read/write functions may wait for an interrupt
633 * to conclude the operation.
635 * Returns negative errno, 0 if there was no change, and 1 in case of change
637 int phy_modify_mmd_changed(struct phy_device
*phydev
, int devad
, u32 regnum
,
642 phy_lock_mdio_bus(phydev
);
643 ret
= __phy_modify_mmd_changed(phydev
, devad
, regnum
, mask
, set
);
644 phy_unlock_mdio_bus(phydev
);
648 EXPORT_SYMBOL_GPL(phy_modify_mmd_changed
);
651 * __phy_modify_mmd - Convenience function for modifying a register on MMD
652 * @phydev: the phy_device struct
653 * @devad: the MMD containing register to modify
654 * @regnum: register number to modify
655 * @mask: bit mask of bits to clear
656 * @set: new value of bits set in mask to write to @regnum
658 * NOTE: MUST NOT be called from interrupt context,
659 * because the bus read/write functions may wait for an interrupt
660 * to conclude the operation.
662 int __phy_modify_mmd(struct phy_device
*phydev
, int devad
, u32 regnum
,
667 ret
= __phy_modify_mmd_changed(phydev
, devad
, regnum
, mask
, set
);
669 return ret
< 0 ? ret
: 0;
671 EXPORT_SYMBOL_GPL(__phy_modify_mmd
);
674 * phy_modify_mmd - Convenience function for modifying a register on MMD
675 * @phydev: the phy_device struct
676 * @devad: the MMD containing register to modify
677 * @regnum: register number to modify
678 * @mask: bit mask of bits to clear
679 * @set: new value of bits set in mask to write to @regnum
681 * NOTE: MUST NOT be called from interrupt context,
682 * because the bus read/write functions may wait for an interrupt
683 * to conclude the operation.
685 int phy_modify_mmd(struct phy_device
*phydev
, int devad
, u32 regnum
,
690 phy_lock_mdio_bus(phydev
);
691 ret
= __phy_modify_mmd(phydev
, devad
, regnum
, mask
, set
);
692 phy_unlock_mdio_bus(phydev
);
696 EXPORT_SYMBOL_GPL(phy_modify_mmd
);
698 static int __phy_read_page(struct phy_device
*phydev
)
700 if (WARN_ONCE(!phydev
->drv
->read_page
, "read_page callback not available, PHY driver not loaded?\n"))
703 return phydev
->drv
->read_page(phydev
);
706 static int __phy_write_page(struct phy_device
*phydev
, int page
)
708 if (WARN_ONCE(!phydev
->drv
->write_page
, "write_page callback not available, PHY driver not loaded?\n"))
711 return phydev
->drv
->write_page(phydev
, page
);
715 * phy_save_page() - take the bus lock and save the current page
716 * @phydev: a pointer to a &struct phy_device
718 * Take the MDIO bus lock, and return the current page number. On error,
719 * returns a negative errno. phy_restore_page() must always be called
720 * after this, irrespective of success or failure of this call.
722 int phy_save_page(struct phy_device
*phydev
)
724 phy_lock_mdio_bus(phydev
);
725 return __phy_read_page(phydev
);
727 EXPORT_SYMBOL_GPL(phy_save_page
);
730 * phy_select_page() - take the bus lock, save the current page, and set a page
731 * @phydev: a pointer to a &struct phy_device
732 * @page: desired page
734 * Take the MDIO bus lock to protect against concurrent access, save the
735 * current PHY page, and set the current page. On error, returns a
736 * negative errno, otherwise returns the previous page number.
737 * phy_restore_page() must always be called after this, irrespective
738 * of success or failure of this call.
740 int phy_select_page(struct phy_device
*phydev
, int page
)
744 oldpage
= ret
= phy_save_page(phydev
);
748 if (oldpage
!= page
) {
749 ret
= __phy_write_page(phydev
, page
);
756 EXPORT_SYMBOL_GPL(phy_select_page
);
759 * phy_restore_page() - restore the page register and release the bus lock
760 * @phydev: a pointer to a &struct phy_device
761 * @oldpage: the old page, return value from phy_save_page() or phy_select_page()
762 * @ret: operation's return code
764 * Release the MDIO bus lock, restoring @oldpage if it is a valid page.
765 * This function propagates the earliest error code from the group of
769 * @oldpage if it was a negative value, otherwise
770 * @ret if it was a negative errno value, otherwise
771 * phy_write_page()'s negative value if it were in error, otherwise
774 int phy_restore_page(struct phy_device
*phydev
, int oldpage
, int ret
)
779 r
= __phy_write_page(phydev
, oldpage
);
781 /* Propagate the operation return code if the page write
784 if (ret
>= 0 && r
< 0)
787 /* Propagate the phy page selection error code */
791 phy_unlock_mdio_bus(phydev
);
795 EXPORT_SYMBOL_GPL(phy_restore_page
);
798 * phy_read_paged() - Convenience function for reading a paged register
799 * @phydev: a pointer to a &struct phy_device
800 * @page: the page for the phy
801 * @regnum: register number
803 * Same rules as for phy_read().
805 int phy_read_paged(struct phy_device
*phydev
, int page
, u32 regnum
)
807 int ret
= 0, oldpage
;
809 oldpage
= phy_select_page(phydev
, page
);
811 ret
= __phy_read(phydev
, regnum
);
813 return phy_restore_page(phydev
, oldpage
, ret
);
815 EXPORT_SYMBOL(phy_read_paged
);
818 * phy_write_paged() - Convenience function for writing a paged register
819 * @phydev: a pointer to a &struct phy_device
820 * @page: the page for the phy
821 * @regnum: register number
822 * @val: value to write
824 * Same rules as for phy_write().
826 int phy_write_paged(struct phy_device
*phydev
, int page
, u32 regnum
, u16 val
)
828 int ret
= 0, oldpage
;
830 oldpage
= phy_select_page(phydev
, page
);
832 ret
= __phy_write(phydev
, regnum
, val
);
834 return phy_restore_page(phydev
, oldpage
, ret
);
836 EXPORT_SYMBOL(phy_write_paged
);
839 * phy_modify_paged_changed() - Function for modifying a paged register
840 * @phydev: a pointer to a &struct phy_device
841 * @page: the page for the phy
842 * @regnum: register number
843 * @mask: bit mask of bits to clear
844 * @set: bit mask of bits to set
846 * Returns negative errno, 0 if there was no change, and 1 in case of change
848 int phy_modify_paged_changed(struct phy_device
*phydev
, int page
, u32 regnum
,
851 int ret
= 0, oldpage
;
853 oldpage
= phy_select_page(phydev
, page
);
855 ret
= __phy_modify_changed(phydev
, regnum
, mask
, set
);
857 return phy_restore_page(phydev
, oldpage
, ret
);
859 EXPORT_SYMBOL(phy_modify_paged_changed
);
862 * phy_modify_paged() - Convenience function for modifying a paged register
863 * @phydev: a pointer to a &struct phy_device
864 * @page: the page for the phy
865 * @regnum: register number
866 * @mask: bit mask of bits to clear
867 * @set: bit mask of bits to set
869 * Same rules as for phy_read() and phy_write().
871 int phy_modify_paged(struct phy_device
*phydev
, int page
, u32 regnum
,
874 int ret
= phy_modify_paged_changed(phydev
, page
, regnum
, mask
, set
);
876 return ret
< 0 ? ret
: 0;
878 EXPORT_SYMBOL(phy_modify_paged
);