treewide: remove redundant IS_ERR() before error code check
[linux/fpc-iii.git] / drivers / net / phy / phy-core.c
bloba4d2d59fceca51f5d02aaea219695a934d48194d
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>
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");
16 switch (speed) {
17 case SPEED_10:
18 return "10Mbps";
19 case SPEED_100:
20 return "100Mbps";
21 case SPEED_1000:
22 return "1Gbps";
23 case SPEED_2500:
24 return "2.5Gbps";
25 case SPEED_5000:
26 return "5Gbps";
27 case SPEED_10000:
28 return "10Gbps";
29 case SPEED_14000:
30 return "14Gbps";
31 case SPEED_20000:
32 return "20Gbps";
33 case SPEED_25000:
34 return "25Gbps";
35 case SPEED_40000:
36 return "40Gbps";
37 case SPEED_50000:
38 return "50Gbps";
39 case SPEED_56000:
40 return "56Gbps";
41 case SPEED_100000:
42 return "100Gbps";
43 case SPEED_200000:
44 return "200Gbps";
45 case SPEED_400000:
46 return "400Gbps";
47 case SPEED_UNKNOWN:
48 return "Unknown";
49 default:
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)
58 return "Half";
59 if (duplex == DUPLEX_FULL)
60 return "Full";
61 if (duplex == DUPLEX_UNKNOWN)
62 return "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[] = {
75 /* 400G */
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 ),
81 /* 200G */
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 ),
87 /* 100G */
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 ),
97 /* 56G */
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 ),
102 /* 50G */
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 ),
111 /* 40G */
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 ),
116 /* 25G */
117 PHY_SETTING( 25000, FULL, 25000baseCR_Full ),
118 PHY_SETTING( 25000, FULL, 25000baseKR_Full ),
119 PHY_SETTING( 25000, FULL, 25000baseSR_Full ),
120 /* 20G */
121 PHY_SETTING( 20000, FULL, 20000baseKR2_Full ),
122 PHY_SETTING( 20000, FULL, 20000baseMLD2_Full ),
123 /* 10G */
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 ),
133 /* 5G */
134 PHY_SETTING( 5000, FULL, 5000baseT_Full ),
135 /* 2.5G */
136 PHY_SETTING( 2500, FULL, 2500baseT_Full ),
137 PHY_SETTING( 2500, FULL, 2500baseX_Full ),
138 /* 1G */
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 ),
144 /* 100M */
145 PHY_SETTING( 100, FULL, 100baseT_Full ),
146 PHY_SETTING( 100, FULL, 100baseT1_Full ),
147 PHY_SETTING( 100, HALF, 100baseT_Half ),
148 /* 10M */
149 PHY_SETTING( 10, FULL, 10baseT_Full ),
150 PHY_SETTING( 10, HALF, 10baseT_Half ),
152 #undef PHY_SETTING
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
165 * be returned.
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;
175 int i;
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)) {
180 last = p;
181 if (p->speed == speed && p->duplex == duplex) {
182 /* Exact match for speed and duplex */
183 match = p;
184 break;
185 } else if (!exact) {
186 if (!match && p->speed <= speed)
187 /* Candidate */
188 match = p;
190 if (p->speed < speed)
191 break;
196 if (!match && !exact)
197 match = last;
199 return match;
201 EXPORT_SYMBOL_GPL(phy_lookup_setting);
203 size_t phy_speeds(unsigned int *speeds, size_t size,
204 unsigned long *mask)
206 size_t count;
207 int i;
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;
215 return count;
218 static int __set_linkmode_max_speed(u32 max_speed, unsigned long *addr)
220 const struct phy_setting *p;
221 int i;
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);
226 else
227 break;
230 return 0;
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)
240 int err;
242 err = __set_phy_supported(phydev, max_speed);
243 if (err)
244 return err;
246 phy_advertise_supported(phydev);
248 return 0;
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;
255 u32 max_speed;
257 if (!IS_ENABLED(CONFIG_OF_MDIO))
258 return;
260 if (!node)
261 return;
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;
270 u32 broken = 0;
272 if (!IS_ENABLED(CONFIG_OF_MDIO))
273 return;
275 if (!node)
276 return;
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);
317 int i;
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;
325 break;
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);
339 while (--i >= 0) {
340 if (test_bit(settings[i].bit, common)) {
341 if (fdx_only && settings[i].duplex != DUPLEX_FULL)
342 continue;
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)
355 return -EINVAL;
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,
361 u16 regnum)
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)
385 int val;
387 if (regnum > (u16)~0 || devad > 32)
388 return -EINVAL;
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);
396 } else {
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);
405 return val;
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)
420 int ret;
422 phy_lock_mdio_bus(phydev);
423 ret = __phy_read_mmd(phydev, devad, regnum);
424 phy_unlock_mdio_bus(phydev);
426 return ret;
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)
442 int ret;
444 if (regnum > (u16)~0 || devad > 32)
445 return -EINVAL;
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,
453 addr, val);
454 } else {
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);
463 ret = 0;
465 return ret;
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)
481 int ret;
483 phy_lock_mdio_bus(phydev);
484 ret = __phy_write_mmd(phydev, devad, regnum, val);
485 phy_unlock_mdio_bus(phydev);
487 return ret;
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,
504 u16 set)
506 int new, ret;
508 ret = __phy_read(phydev, regnum);
509 if (ret < 0)
510 return ret;
512 new = (ret & ~mask) | set;
513 if (new == ret)
514 return 0;
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)
537 int ret;
539 phy_lock_mdio_bus(phydev);
540 ret = __phy_modify_changed(phydev, regnum, mask, set);
541 phy_unlock_mdio_bus(phydev);
543 return ret;
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)
560 int ret;
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)
581 int ret;
583 phy_lock_mdio_bus(phydev);
584 ret = __phy_modify(phydev, regnum, mask, set);
585 phy_unlock_mdio_bus(phydev);
587 return ret;
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,
605 u16 mask, u16 set)
607 int new, ret;
609 ret = __phy_read_mmd(phydev, devad, regnum);
610 if (ret < 0)
611 return ret;
613 new = (ret & ~mask) | set;
614 if (new == ret)
615 return 0;
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,
638 u16 mask, u16 set)
640 int ret;
642 phy_lock_mdio_bus(phydev);
643 ret = __phy_modify_mmd_changed(phydev, devad, regnum, mask, set);
644 phy_unlock_mdio_bus(phydev);
646 return ret;
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,
663 u16 mask, u16 set)
665 int ret;
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,
686 u16 mask, u16 set)
688 int ret;
690 phy_lock_mdio_bus(phydev);
691 ret = __phy_modify_mmd(phydev, devad, regnum, mask, set);
692 phy_unlock_mdio_bus(phydev);
694 return ret;
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"))
701 return -EOPNOTSUPP;
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"))
709 return -EOPNOTSUPP;
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)
742 int ret, oldpage;
744 oldpage = ret = phy_save_page(phydev);
745 if (ret < 0)
746 return ret;
748 if (oldpage != page) {
749 ret = __phy_write_page(phydev, page);
750 if (ret < 0)
751 return ret;
754 return oldpage;
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
766 * operations.
768 * Returns:
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
772 * @ret.
774 int phy_restore_page(struct phy_device *phydev, int oldpage, int ret)
776 int r;
778 if (oldpage >= 0) {
779 r = __phy_write_page(phydev, oldpage);
781 /* Propagate the operation return code if the page write
782 * was successful.
784 if (ret >= 0 && r < 0)
785 ret = r;
786 } else {
787 /* Propagate the phy page selection error code */
788 ret = oldpage;
791 phy_unlock_mdio_bus(phydev);
793 return ret;
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);
810 if (oldpage >= 0)
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);
831 if (oldpage >= 0)
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,
849 u16 mask, u16 set)
851 int ret = 0, oldpage;
853 oldpage = phy_select_page(phydev, page);
854 if (oldpage >= 0)
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,
872 u16 mask, u16 set)
874 int ret = phy_modify_paged_changed(phydev, page, regnum, mask, set);
876 return ret < 0 ? ret : 0;
878 EXPORT_SYMBOL(phy_modify_paged);