spi: sprd: adi: Add a reset reason for watchdog mode
[linux/fpc-iii.git] / drivers / net / phy / phy-core.c
blob16667fbac8bfa232ca19702c601442023fbd6b0e
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 != 69,
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_UNKNOWN:
46 return "Unknown";
47 default:
48 return "Unsupported (update phy-core.c)";
51 EXPORT_SYMBOL_GPL(phy_speed_to_str);
53 const char *phy_duplex_to_str(unsigned int duplex)
55 if (duplex == DUPLEX_HALF)
56 return "Half";
57 if (duplex == DUPLEX_FULL)
58 return "Full";
59 if (duplex == DUPLEX_UNKNOWN)
60 return "Unknown";
61 return "Unsupported (update phy-core.c)";
63 EXPORT_SYMBOL_GPL(phy_duplex_to_str);
65 /* A mapping of all SUPPORTED settings to speed/duplex. This table
66 * must be grouped by speed and sorted in descending match priority
67 * - iow, descending speed. */
69 #define PHY_SETTING(s, d, b) { .speed = SPEED_ ## s, .duplex = DUPLEX_ ## d, \
70 .bit = ETHTOOL_LINK_MODE_ ## b ## _BIT}
72 static const struct phy_setting settings[] = {
73 /* 200G */
74 PHY_SETTING( 200000, FULL, 200000baseCR4_Full ),
75 PHY_SETTING( 200000, FULL, 200000baseKR4_Full ),
76 PHY_SETTING( 200000, FULL, 200000baseLR4_ER4_FR4_Full ),
77 PHY_SETTING( 200000, FULL, 200000baseDR4_Full ),
78 PHY_SETTING( 200000, FULL, 200000baseSR4_Full ),
79 /* 100G */
80 PHY_SETTING( 100000, FULL, 100000baseCR4_Full ),
81 PHY_SETTING( 100000, FULL, 100000baseKR4_Full ),
82 PHY_SETTING( 100000, FULL, 100000baseLR4_ER4_Full ),
83 PHY_SETTING( 100000, FULL, 100000baseSR4_Full ),
84 PHY_SETTING( 100000, FULL, 100000baseCR2_Full ),
85 PHY_SETTING( 100000, FULL, 100000baseKR2_Full ),
86 PHY_SETTING( 100000, FULL, 100000baseLR2_ER2_FR2_Full ),
87 PHY_SETTING( 100000, FULL, 100000baseDR2_Full ),
88 PHY_SETTING( 100000, FULL, 100000baseSR2_Full ),
89 /* 56G */
90 PHY_SETTING( 56000, FULL, 56000baseCR4_Full ),
91 PHY_SETTING( 56000, FULL, 56000baseKR4_Full ),
92 PHY_SETTING( 56000, FULL, 56000baseLR4_Full ),
93 PHY_SETTING( 56000, FULL, 56000baseSR4_Full ),
94 /* 50G */
95 PHY_SETTING( 50000, FULL, 50000baseCR2_Full ),
96 PHY_SETTING( 50000, FULL, 50000baseKR2_Full ),
97 PHY_SETTING( 50000, FULL, 50000baseSR2_Full ),
98 PHY_SETTING( 50000, FULL, 50000baseCR_Full ),
99 PHY_SETTING( 50000, FULL, 50000baseKR_Full ),
100 PHY_SETTING( 50000, FULL, 50000baseLR_ER_FR_Full ),
101 PHY_SETTING( 50000, FULL, 50000baseDR_Full ),
102 PHY_SETTING( 50000, FULL, 50000baseSR_Full ),
103 /* 40G */
104 PHY_SETTING( 40000, FULL, 40000baseCR4_Full ),
105 PHY_SETTING( 40000, FULL, 40000baseKR4_Full ),
106 PHY_SETTING( 40000, FULL, 40000baseLR4_Full ),
107 PHY_SETTING( 40000, FULL, 40000baseSR4_Full ),
108 /* 25G */
109 PHY_SETTING( 25000, FULL, 25000baseCR_Full ),
110 PHY_SETTING( 25000, FULL, 25000baseKR_Full ),
111 PHY_SETTING( 25000, FULL, 25000baseSR_Full ),
112 /* 20G */
113 PHY_SETTING( 20000, FULL, 20000baseKR2_Full ),
114 PHY_SETTING( 20000, FULL, 20000baseMLD2_Full ),
115 /* 10G */
116 PHY_SETTING( 10000, FULL, 10000baseCR_Full ),
117 PHY_SETTING( 10000, FULL, 10000baseER_Full ),
118 PHY_SETTING( 10000, FULL, 10000baseKR_Full ),
119 PHY_SETTING( 10000, FULL, 10000baseKX4_Full ),
120 PHY_SETTING( 10000, FULL, 10000baseLR_Full ),
121 PHY_SETTING( 10000, FULL, 10000baseLRM_Full ),
122 PHY_SETTING( 10000, FULL, 10000baseR_FEC ),
123 PHY_SETTING( 10000, FULL, 10000baseSR_Full ),
124 PHY_SETTING( 10000, FULL, 10000baseT_Full ),
125 /* 5G */
126 PHY_SETTING( 5000, FULL, 5000baseT_Full ),
127 /* 2.5G */
128 PHY_SETTING( 2500, FULL, 2500baseT_Full ),
129 PHY_SETTING( 2500, FULL, 2500baseX_Full ),
130 /* 1G */
131 PHY_SETTING( 1000, FULL, 1000baseKX_Full ),
132 PHY_SETTING( 1000, FULL, 1000baseT_Full ),
133 PHY_SETTING( 1000, HALF, 1000baseT_Half ),
134 PHY_SETTING( 1000, FULL, 1000baseT1_Full ),
135 PHY_SETTING( 1000, FULL, 1000baseX_Full ),
136 /* 100M */
137 PHY_SETTING( 100, FULL, 100baseT_Full ),
138 PHY_SETTING( 100, FULL, 100baseT1_Full ),
139 PHY_SETTING( 100, HALF, 100baseT_Half ),
140 /* 10M */
141 PHY_SETTING( 10, FULL, 10baseT_Full ),
142 PHY_SETTING( 10, HALF, 10baseT_Half ),
144 #undef PHY_SETTING
147 * phy_lookup_setting - lookup a PHY setting
148 * @speed: speed to match
149 * @duplex: duplex to match
150 * @mask: allowed link modes
151 * @exact: an exact match is required
153 * Search the settings array for a setting that matches the speed and
154 * duplex, and which is supported.
156 * If @exact is unset, either an exact match or %NULL for no match will
157 * be returned.
159 * If @exact is set, an exact match, the fastest supported setting at
160 * or below the specified speed, the slowest supported setting, or if
161 * they all fail, %NULL will be returned.
163 const struct phy_setting *
164 phy_lookup_setting(int speed, int duplex, const unsigned long *mask, bool exact)
166 const struct phy_setting *p, *match = NULL, *last = NULL;
167 int i;
169 for (i = 0, p = settings; i < ARRAY_SIZE(settings); i++, p++) {
170 if (p->bit < __ETHTOOL_LINK_MODE_MASK_NBITS &&
171 test_bit(p->bit, mask)) {
172 last = p;
173 if (p->speed == speed && p->duplex == duplex) {
174 /* Exact match for speed and duplex */
175 match = p;
176 break;
177 } else if (!exact) {
178 if (!match && p->speed <= speed)
179 /* Candidate */
180 match = p;
182 if (p->speed < speed)
183 break;
188 if (!match && !exact)
189 match = last;
191 return match;
193 EXPORT_SYMBOL_GPL(phy_lookup_setting);
195 size_t phy_speeds(unsigned int *speeds, size_t size,
196 unsigned long *mask)
198 size_t count;
199 int i;
201 for (i = 0, count = 0; i < ARRAY_SIZE(settings) && count < size; i++)
202 if (settings[i].bit < __ETHTOOL_LINK_MODE_MASK_NBITS &&
203 test_bit(settings[i].bit, mask) &&
204 (count == 0 || speeds[count - 1] != settings[i].speed))
205 speeds[count++] = settings[i].speed;
207 return count;
210 static int __set_phy_supported(struct phy_device *phydev, u32 max_speed)
212 const struct phy_setting *p;
213 int i;
215 for (i = 0, p = settings; i < ARRAY_SIZE(settings); i++, p++) {
216 if (p->speed > max_speed)
217 linkmode_clear_bit(p->bit, phydev->supported);
218 else
219 break;
222 return 0;
225 int phy_set_max_speed(struct phy_device *phydev, u32 max_speed)
227 int err;
229 err = __set_phy_supported(phydev, max_speed);
230 if (err)
231 return err;
233 phy_advertise_supported(phydev);
235 return 0;
237 EXPORT_SYMBOL(phy_set_max_speed);
239 void of_set_phy_supported(struct phy_device *phydev)
241 struct device_node *node = phydev->mdio.dev.of_node;
242 u32 max_speed;
244 if (!IS_ENABLED(CONFIG_OF_MDIO))
245 return;
247 if (!node)
248 return;
250 if (!of_property_read_u32(node, "max-speed", &max_speed))
251 __set_phy_supported(phydev, max_speed);
254 void of_set_phy_eee_broken(struct phy_device *phydev)
256 struct device_node *node = phydev->mdio.dev.of_node;
257 u32 broken = 0;
259 if (!IS_ENABLED(CONFIG_OF_MDIO))
260 return;
262 if (!node)
263 return;
265 if (of_property_read_bool(node, "eee-broken-100tx"))
266 broken |= MDIO_EEE_100TX;
267 if (of_property_read_bool(node, "eee-broken-1000t"))
268 broken |= MDIO_EEE_1000T;
269 if (of_property_read_bool(node, "eee-broken-10gt"))
270 broken |= MDIO_EEE_10GT;
271 if (of_property_read_bool(node, "eee-broken-1000kx"))
272 broken |= MDIO_EEE_1000KX;
273 if (of_property_read_bool(node, "eee-broken-10gkx4"))
274 broken |= MDIO_EEE_10GKX4;
275 if (of_property_read_bool(node, "eee-broken-10gkr"))
276 broken |= MDIO_EEE_10GKR;
278 phydev->eee_broken_modes = broken;
282 * phy_resolve_aneg_linkmode - resolve the advertisements into phy settings
283 * @phydev: The phy_device struct
285 * Resolve our and the link partner advertisements into their corresponding
286 * speed and duplex. If full duplex was negotiated, extract the pause mode
287 * from the link partner mask.
289 void phy_resolve_aneg_linkmode(struct phy_device *phydev)
291 __ETHTOOL_DECLARE_LINK_MODE_MASK(common);
292 int i;
294 linkmode_and(common, phydev->lp_advertising, phydev->advertising);
296 for (i = 0; i < ARRAY_SIZE(settings); i++)
297 if (test_bit(settings[i].bit, common)) {
298 phydev->speed = settings[i].speed;
299 phydev->duplex = settings[i].duplex;
300 break;
303 if (phydev->duplex == DUPLEX_FULL) {
304 phydev->pause = linkmode_test_bit(ETHTOOL_LINK_MODE_Pause_BIT,
305 phydev->lp_advertising);
306 phydev->asym_pause = linkmode_test_bit(
307 ETHTOOL_LINK_MODE_Asym_Pause_BIT,
308 phydev->lp_advertising);
311 EXPORT_SYMBOL_GPL(phy_resolve_aneg_linkmode);
313 static void mmd_phy_indirect(struct mii_bus *bus, int phy_addr, int devad,
314 u16 regnum)
316 /* Write the desired MMD Devad */
317 __mdiobus_write(bus, phy_addr, MII_MMD_CTRL, devad);
319 /* Write the desired MMD register address */
320 __mdiobus_write(bus, phy_addr, MII_MMD_DATA, regnum);
322 /* Select the Function : DATA with no post increment */
323 __mdiobus_write(bus, phy_addr, MII_MMD_CTRL,
324 devad | MII_MMD_CTRL_NOINCR);
328 * __phy_read_mmd - Convenience function for reading a register
329 * from an MMD on a given PHY.
330 * @phydev: The phy_device struct
331 * @devad: The MMD to read from (0..31)
332 * @regnum: The register on the MMD to read (0..65535)
334 * Same rules as for __phy_read();
336 int __phy_read_mmd(struct phy_device *phydev, int devad, u32 regnum)
338 int val;
340 if (regnum > (u16)~0 || devad > 32)
341 return -EINVAL;
343 if (phydev->drv->read_mmd) {
344 val = phydev->drv->read_mmd(phydev, devad, regnum);
345 } else if (phydev->is_c45) {
346 u32 addr = MII_ADDR_C45 | (devad << 16) | (regnum & 0xffff);
348 val = __mdiobus_read(phydev->mdio.bus, phydev->mdio.addr, addr);
349 } else {
350 struct mii_bus *bus = phydev->mdio.bus;
351 int phy_addr = phydev->mdio.addr;
353 mmd_phy_indirect(bus, phy_addr, devad, regnum);
355 /* Read the content of the MMD's selected register */
356 val = __mdiobus_read(bus, phy_addr, MII_MMD_DATA);
358 return val;
360 EXPORT_SYMBOL(__phy_read_mmd);
363 * phy_read_mmd - Convenience function for reading a register
364 * from an MMD on a given PHY.
365 * @phydev: The phy_device struct
366 * @devad: The MMD to read from
367 * @regnum: The register on the MMD to read
369 * Same rules as for phy_read();
371 int phy_read_mmd(struct phy_device *phydev, int devad, u32 regnum)
373 int ret;
375 mutex_lock(&phydev->mdio.bus->mdio_lock);
376 ret = __phy_read_mmd(phydev, devad, regnum);
377 mutex_unlock(&phydev->mdio.bus->mdio_lock);
379 return ret;
381 EXPORT_SYMBOL(phy_read_mmd);
384 * __phy_write_mmd - Convenience function for writing a register
385 * on an MMD on a given PHY.
386 * @phydev: The phy_device struct
387 * @devad: The MMD to read from
388 * @regnum: The register on the MMD to read
389 * @val: value to write to @regnum
391 * Same rules as for __phy_write();
393 int __phy_write_mmd(struct phy_device *phydev, int devad, u32 regnum, u16 val)
395 int ret;
397 if (regnum > (u16)~0 || devad > 32)
398 return -EINVAL;
400 if (phydev->drv->write_mmd) {
401 ret = phydev->drv->write_mmd(phydev, devad, regnum, val);
402 } else if (phydev->is_c45) {
403 u32 addr = MII_ADDR_C45 | (devad << 16) | (regnum & 0xffff);
405 ret = __mdiobus_write(phydev->mdio.bus, phydev->mdio.addr,
406 addr, val);
407 } else {
408 struct mii_bus *bus = phydev->mdio.bus;
409 int phy_addr = phydev->mdio.addr;
411 mmd_phy_indirect(bus, phy_addr, devad, regnum);
413 /* Write the data into MMD's selected register */
414 __mdiobus_write(bus, phy_addr, MII_MMD_DATA, val);
416 ret = 0;
418 return ret;
420 EXPORT_SYMBOL(__phy_write_mmd);
423 * phy_write_mmd - Convenience function for writing a register
424 * on an MMD on a given PHY.
425 * @phydev: The phy_device struct
426 * @devad: The MMD to read from
427 * @regnum: The register on the MMD to read
428 * @val: value to write to @regnum
430 * Same rules as for phy_write();
432 int phy_write_mmd(struct phy_device *phydev, int devad, u32 regnum, u16 val)
434 int ret;
436 mutex_lock(&phydev->mdio.bus->mdio_lock);
437 ret = __phy_write_mmd(phydev, devad, regnum, val);
438 mutex_unlock(&phydev->mdio.bus->mdio_lock);
440 return ret;
442 EXPORT_SYMBOL(phy_write_mmd);
445 * __phy_modify_changed() - Convenience function for modifying a PHY register
446 * @phydev: a pointer to a &struct phy_device
447 * @regnum: register number
448 * @mask: bit mask of bits to clear
449 * @set: bit mask of bits to set
451 * Unlocked helper function which allows a PHY register to be modified as
452 * new register value = (old register value & ~mask) | set
454 * Returns negative errno, 0 if there was no change, and 1 in case of change
456 int __phy_modify_changed(struct phy_device *phydev, u32 regnum, u16 mask,
457 u16 set)
459 int new, ret;
461 ret = __phy_read(phydev, regnum);
462 if (ret < 0)
463 return ret;
465 new = (ret & ~mask) | set;
466 if (new == ret)
467 return 0;
469 ret = __phy_write(phydev, regnum, new);
471 return ret < 0 ? ret : 1;
473 EXPORT_SYMBOL_GPL(__phy_modify_changed);
476 * phy_modify_changed - Function for modifying a PHY register
477 * @phydev: the phy_device struct
478 * @regnum: register number to modify
479 * @mask: bit mask of bits to clear
480 * @set: new value of bits set in mask to write to @regnum
482 * NOTE: MUST NOT be called from interrupt context,
483 * because the bus read/write functions may wait for an interrupt
484 * to conclude the operation.
486 * Returns negative errno, 0 if there was no change, and 1 in case of change
488 int phy_modify_changed(struct phy_device *phydev, u32 regnum, u16 mask, u16 set)
490 int ret;
492 mutex_lock(&phydev->mdio.bus->mdio_lock);
493 ret = __phy_modify_changed(phydev, regnum, mask, set);
494 mutex_unlock(&phydev->mdio.bus->mdio_lock);
496 return ret;
498 EXPORT_SYMBOL_GPL(phy_modify_changed);
501 * __phy_modify - Convenience function for modifying a PHY register
502 * @phydev: the phy_device struct
503 * @regnum: register number to modify
504 * @mask: bit mask of bits to clear
505 * @set: new value of bits set in mask to write to @regnum
507 * NOTE: MUST NOT be called from interrupt context,
508 * because the bus read/write functions may wait for an interrupt
509 * to conclude the operation.
511 int __phy_modify(struct phy_device *phydev, u32 regnum, u16 mask, u16 set)
513 int ret;
515 ret = __phy_modify_changed(phydev, regnum, mask, set);
517 return ret < 0 ? ret : 0;
519 EXPORT_SYMBOL_GPL(__phy_modify);
522 * phy_modify - Convenience function for modifying a given PHY register
523 * @phydev: the phy_device struct
524 * @regnum: register number to write
525 * @mask: bit mask of bits to clear
526 * @set: new value of bits set in mask to write to @regnum
528 * NOTE: MUST NOT be called from interrupt context,
529 * because the bus read/write functions may wait for an interrupt
530 * to conclude the operation.
532 int phy_modify(struct phy_device *phydev, u32 regnum, u16 mask, u16 set)
534 int ret;
536 mutex_lock(&phydev->mdio.bus->mdio_lock);
537 ret = __phy_modify(phydev, regnum, mask, set);
538 mutex_unlock(&phydev->mdio.bus->mdio_lock);
540 return ret;
542 EXPORT_SYMBOL_GPL(phy_modify);
545 * __phy_modify_mmd_changed - Function for modifying a register on MMD
546 * @phydev: the phy_device struct
547 * @devad: the MMD containing register to modify
548 * @regnum: register number to modify
549 * @mask: bit mask of bits to clear
550 * @set: new value of bits set in mask to write to @regnum
552 * Unlocked helper function which allows a MMD register to be modified as
553 * new register value = (old register value & ~mask) | set
555 * Returns negative errno, 0 if there was no change, and 1 in case of change
557 int __phy_modify_mmd_changed(struct phy_device *phydev, int devad, u32 regnum,
558 u16 mask, u16 set)
560 int new, ret;
562 ret = __phy_read_mmd(phydev, devad, regnum);
563 if (ret < 0)
564 return ret;
566 new = (ret & ~mask) | set;
567 if (new == ret)
568 return 0;
570 ret = __phy_write_mmd(phydev, devad, regnum, new);
572 return ret < 0 ? ret : 1;
574 EXPORT_SYMBOL_GPL(__phy_modify_mmd_changed);
577 * phy_modify_mmd_changed - Function for modifying a register on MMD
578 * @phydev: the phy_device struct
579 * @devad: the MMD containing register to modify
580 * @regnum: register number to modify
581 * @mask: bit mask of bits to clear
582 * @set: new value of bits set in mask to write to @regnum
584 * NOTE: MUST NOT be called from interrupt context,
585 * because the bus read/write functions may wait for an interrupt
586 * to conclude the operation.
588 * Returns negative errno, 0 if there was no change, and 1 in case of change
590 int phy_modify_mmd_changed(struct phy_device *phydev, int devad, u32 regnum,
591 u16 mask, u16 set)
593 int ret;
595 mutex_lock(&phydev->mdio.bus->mdio_lock);
596 ret = __phy_modify_mmd_changed(phydev, devad, regnum, mask, set);
597 mutex_unlock(&phydev->mdio.bus->mdio_lock);
599 return ret;
601 EXPORT_SYMBOL_GPL(phy_modify_mmd_changed);
604 * __phy_modify_mmd - Convenience function for modifying a register on MMD
605 * @phydev: the phy_device struct
606 * @devad: the MMD containing register to modify
607 * @regnum: register number to modify
608 * @mask: bit mask of bits to clear
609 * @set: new value of bits set in mask to write to @regnum
611 * NOTE: MUST NOT be called from interrupt context,
612 * because the bus read/write functions may wait for an interrupt
613 * to conclude the operation.
615 int __phy_modify_mmd(struct phy_device *phydev, int devad, u32 regnum,
616 u16 mask, u16 set)
618 int ret;
620 ret = __phy_modify_mmd_changed(phydev, devad, regnum, mask, set);
622 return ret < 0 ? ret : 0;
624 EXPORT_SYMBOL_GPL(__phy_modify_mmd);
627 * phy_modify_mmd - Convenience function for modifying a register on MMD
628 * @phydev: the phy_device struct
629 * @devad: the MMD containing register to modify
630 * @regnum: register number to modify
631 * @mask: bit mask of bits to clear
632 * @set: new value of bits set in mask to write to @regnum
634 * NOTE: MUST NOT be called from interrupt context,
635 * because the bus read/write functions may wait for an interrupt
636 * to conclude the operation.
638 int phy_modify_mmd(struct phy_device *phydev, int devad, u32 regnum,
639 u16 mask, u16 set)
641 int ret;
643 mutex_lock(&phydev->mdio.bus->mdio_lock);
644 ret = __phy_modify_mmd(phydev, devad, regnum, mask, set);
645 mutex_unlock(&phydev->mdio.bus->mdio_lock);
647 return ret;
649 EXPORT_SYMBOL_GPL(phy_modify_mmd);
651 static int __phy_read_page(struct phy_device *phydev)
653 return phydev->drv->read_page(phydev);
656 static int __phy_write_page(struct phy_device *phydev, int page)
658 return phydev->drv->write_page(phydev, page);
662 * phy_save_page() - take the bus lock and save the current page
663 * @phydev: a pointer to a &struct phy_device
665 * Take the MDIO bus lock, and return the current page number. On error,
666 * returns a negative errno. phy_restore_page() must always be called
667 * after this, irrespective of success or failure of this call.
669 int phy_save_page(struct phy_device *phydev)
671 mutex_lock(&phydev->mdio.bus->mdio_lock);
672 return __phy_read_page(phydev);
674 EXPORT_SYMBOL_GPL(phy_save_page);
677 * phy_select_page() - take the bus lock, save the current page, and set a page
678 * @phydev: a pointer to a &struct phy_device
679 * @page: desired page
681 * Take the MDIO bus lock to protect against concurrent access, save the
682 * current PHY page, and set the current page. On error, returns a
683 * negative errno, otherwise returns the previous page number.
684 * phy_restore_page() must always be called after this, irrespective
685 * of success or failure of this call.
687 int phy_select_page(struct phy_device *phydev, int page)
689 int ret, oldpage;
691 oldpage = ret = phy_save_page(phydev);
692 if (ret < 0)
693 return ret;
695 if (oldpage != page) {
696 ret = __phy_write_page(phydev, page);
697 if (ret < 0)
698 return ret;
701 return oldpage;
703 EXPORT_SYMBOL_GPL(phy_select_page);
706 * phy_restore_page() - restore the page register and release the bus lock
707 * @phydev: a pointer to a &struct phy_device
708 * @oldpage: the old page, return value from phy_save_page() or phy_select_page()
709 * @ret: operation's return code
711 * Release the MDIO bus lock, restoring @oldpage if it is a valid page.
712 * This function propagates the earliest error code from the group of
713 * operations.
715 * Returns:
716 * @oldpage if it was a negative value, otherwise
717 * @ret if it was a negative errno value, otherwise
718 * phy_write_page()'s negative value if it were in error, otherwise
719 * @ret.
721 int phy_restore_page(struct phy_device *phydev, int oldpage, int ret)
723 int r;
725 if (oldpage >= 0) {
726 r = __phy_write_page(phydev, oldpage);
728 /* Propagate the operation return code if the page write
729 * was successful.
731 if (ret >= 0 && r < 0)
732 ret = r;
733 } else {
734 /* Propagate the phy page selection error code */
735 ret = oldpage;
738 mutex_unlock(&phydev->mdio.bus->mdio_lock);
740 return ret;
742 EXPORT_SYMBOL_GPL(phy_restore_page);
745 * phy_read_paged() - Convenience function for reading a paged register
746 * @phydev: a pointer to a &struct phy_device
747 * @page: the page for the phy
748 * @regnum: register number
750 * Same rules as for phy_read().
752 int phy_read_paged(struct phy_device *phydev, int page, u32 regnum)
754 int ret = 0, oldpage;
756 oldpage = phy_select_page(phydev, page);
757 if (oldpage >= 0)
758 ret = __phy_read(phydev, regnum);
760 return phy_restore_page(phydev, oldpage, ret);
762 EXPORT_SYMBOL(phy_read_paged);
765 * phy_write_paged() - Convenience function for writing a paged register
766 * @phydev: a pointer to a &struct phy_device
767 * @page: the page for the phy
768 * @regnum: register number
769 * @val: value to write
771 * Same rules as for phy_write().
773 int phy_write_paged(struct phy_device *phydev, int page, u32 regnum, u16 val)
775 int ret = 0, oldpage;
777 oldpage = phy_select_page(phydev, page);
778 if (oldpage >= 0)
779 ret = __phy_write(phydev, regnum, val);
781 return phy_restore_page(phydev, oldpage, ret);
783 EXPORT_SYMBOL(phy_write_paged);
786 * phy_modify_paged() - Convenience function for modifying a paged register
787 * @phydev: a pointer to a &struct phy_device
788 * @page: the page for the phy
789 * @regnum: register number
790 * @mask: bit mask of bits to clear
791 * @set: bit mask of bits to set
793 * Same rules as for phy_read() and phy_write().
795 int phy_modify_paged(struct phy_device *phydev, int page, u32 regnum,
796 u16 mask, u16 set)
798 int ret = 0, oldpage;
800 oldpage = phy_select_page(phydev, page);
801 if (oldpage >= 0)
802 ret = __phy_modify(phydev, regnum, mask, set);
804 return phy_restore_page(phydev, oldpage, ret);
806 EXPORT_SYMBOL(phy_modify_paged);