dmaengine: imx-sdma: Let the core do the device node validation
[linux/fpc-iii.git] / drivers / net / phy / phy-core.c
blob3daf0214a242fef77909852a7b9676d1fe287eb3
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 != 67,
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, 1000baseX_Full ),
135 /* 100M */
136 PHY_SETTING( 100, FULL, 100baseT_Full ),
137 PHY_SETTING( 100, HALF, 100baseT_Half ),
138 /* 10M */
139 PHY_SETTING( 10, FULL, 10baseT_Full ),
140 PHY_SETTING( 10, HALF, 10baseT_Half ),
142 #undef PHY_SETTING
145 * phy_lookup_setting - lookup a PHY setting
146 * @speed: speed to match
147 * @duplex: duplex to match
148 * @mask: allowed link modes
149 * @exact: an exact match is required
151 * Search the settings array for a setting that matches the speed and
152 * duplex, and which is supported.
154 * If @exact is unset, either an exact match or %NULL for no match will
155 * be returned.
157 * If @exact is set, an exact match, the fastest supported setting at
158 * or below the specified speed, the slowest supported setting, or if
159 * they all fail, %NULL will be returned.
161 const struct phy_setting *
162 phy_lookup_setting(int speed, int duplex, const unsigned long *mask, bool exact)
164 const struct phy_setting *p, *match = NULL, *last = NULL;
165 int i;
167 for (i = 0, p = settings; i < ARRAY_SIZE(settings); i++, p++) {
168 if (p->bit < __ETHTOOL_LINK_MODE_MASK_NBITS &&
169 test_bit(p->bit, mask)) {
170 last = p;
171 if (p->speed == speed && p->duplex == duplex) {
172 /* Exact match for speed and duplex */
173 match = p;
174 break;
175 } else if (!exact) {
176 if (!match && p->speed <= speed)
177 /* Candidate */
178 match = p;
180 if (p->speed < speed)
181 break;
186 if (!match && !exact)
187 match = last;
189 return match;
191 EXPORT_SYMBOL_GPL(phy_lookup_setting);
193 size_t phy_speeds(unsigned int *speeds, size_t size,
194 unsigned long *mask)
196 size_t count;
197 int i;
199 for (i = 0, count = 0; i < ARRAY_SIZE(settings) && count < size; i++)
200 if (settings[i].bit < __ETHTOOL_LINK_MODE_MASK_NBITS &&
201 test_bit(settings[i].bit, mask) &&
202 (count == 0 || speeds[count - 1] != settings[i].speed))
203 speeds[count++] = settings[i].speed;
205 return count;
208 static int __set_phy_supported(struct phy_device *phydev, u32 max_speed)
210 const struct phy_setting *p;
211 int i;
213 for (i = 0, p = settings; i < ARRAY_SIZE(settings); i++, p++) {
214 if (p->speed > max_speed)
215 linkmode_clear_bit(p->bit, phydev->supported);
216 else
217 break;
220 return 0;
223 int phy_set_max_speed(struct phy_device *phydev, u32 max_speed)
225 int err;
227 err = __set_phy_supported(phydev, max_speed);
228 if (err)
229 return err;
231 phy_advertise_supported(phydev);
233 return 0;
235 EXPORT_SYMBOL(phy_set_max_speed);
237 void of_set_phy_supported(struct phy_device *phydev)
239 struct device_node *node = phydev->mdio.dev.of_node;
240 u32 max_speed;
242 if (!IS_ENABLED(CONFIG_OF_MDIO))
243 return;
245 if (!node)
246 return;
248 if (!of_property_read_u32(node, "max-speed", &max_speed))
249 __set_phy_supported(phydev, max_speed);
252 void of_set_phy_eee_broken(struct phy_device *phydev)
254 struct device_node *node = phydev->mdio.dev.of_node;
255 u32 broken = 0;
257 if (!IS_ENABLED(CONFIG_OF_MDIO))
258 return;
260 if (!node)
261 return;
263 if (of_property_read_bool(node, "eee-broken-100tx"))
264 broken |= MDIO_EEE_100TX;
265 if (of_property_read_bool(node, "eee-broken-1000t"))
266 broken |= MDIO_EEE_1000T;
267 if (of_property_read_bool(node, "eee-broken-10gt"))
268 broken |= MDIO_EEE_10GT;
269 if (of_property_read_bool(node, "eee-broken-1000kx"))
270 broken |= MDIO_EEE_1000KX;
271 if (of_property_read_bool(node, "eee-broken-10gkx4"))
272 broken |= MDIO_EEE_10GKX4;
273 if (of_property_read_bool(node, "eee-broken-10gkr"))
274 broken |= MDIO_EEE_10GKR;
276 phydev->eee_broken_modes = broken;
280 * phy_resolve_aneg_linkmode - resolve the advertisements into phy settings
281 * @phydev: The phy_device struct
283 * Resolve our and the link partner advertisements into their corresponding
284 * speed and duplex. If full duplex was negotiated, extract the pause mode
285 * from the link partner mask.
287 void phy_resolve_aneg_linkmode(struct phy_device *phydev)
289 __ETHTOOL_DECLARE_LINK_MODE_MASK(common);
290 int i;
292 linkmode_and(common, phydev->lp_advertising, phydev->advertising);
294 for (i = 0; i < ARRAY_SIZE(settings); i++)
295 if (test_bit(settings[i].bit, common)) {
296 phydev->speed = settings[i].speed;
297 phydev->duplex = settings[i].duplex;
298 break;
301 if (phydev->duplex == DUPLEX_FULL) {
302 phydev->pause = linkmode_test_bit(ETHTOOL_LINK_MODE_Pause_BIT,
303 phydev->lp_advertising);
304 phydev->asym_pause = linkmode_test_bit(
305 ETHTOOL_LINK_MODE_Asym_Pause_BIT,
306 phydev->lp_advertising);
309 EXPORT_SYMBOL_GPL(phy_resolve_aneg_linkmode);
311 static void mmd_phy_indirect(struct mii_bus *bus, int phy_addr, int devad,
312 u16 regnum)
314 /* Write the desired MMD Devad */
315 __mdiobus_write(bus, phy_addr, MII_MMD_CTRL, devad);
317 /* Write the desired MMD register address */
318 __mdiobus_write(bus, phy_addr, MII_MMD_DATA, regnum);
320 /* Select the Function : DATA with no post increment */
321 __mdiobus_write(bus, phy_addr, MII_MMD_CTRL,
322 devad | MII_MMD_CTRL_NOINCR);
326 * __phy_read_mmd - Convenience function for reading a register
327 * from an MMD on a given PHY.
328 * @phydev: The phy_device struct
329 * @devad: The MMD to read from (0..31)
330 * @regnum: The register on the MMD to read (0..65535)
332 * Same rules as for __phy_read();
334 int __phy_read_mmd(struct phy_device *phydev, int devad, u32 regnum)
336 int val;
338 if (regnum > (u16)~0 || devad > 32)
339 return -EINVAL;
341 if (phydev->drv->read_mmd) {
342 val = phydev->drv->read_mmd(phydev, devad, regnum);
343 } else if (phydev->is_c45) {
344 u32 addr = MII_ADDR_C45 | (devad << 16) | (regnum & 0xffff);
346 val = __mdiobus_read(phydev->mdio.bus, phydev->mdio.addr, addr);
347 } else {
348 struct mii_bus *bus = phydev->mdio.bus;
349 int phy_addr = phydev->mdio.addr;
351 mmd_phy_indirect(bus, phy_addr, devad, regnum);
353 /* Read the content of the MMD's selected register */
354 val = __mdiobus_read(bus, phy_addr, MII_MMD_DATA);
356 return val;
358 EXPORT_SYMBOL(__phy_read_mmd);
361 * phy_read_mmd - Convenience function for reading a register
362 * from an MMD on a given PHY.
363 * @phydev: The phy_device struct
364 * @devad: The MMD to read from
365 * @regnum: The register on the MMD to read
367 * Same rules as for phy_read();
369 int phy_read_mmd(struct phy_device *phydev, int devad, u32 regnum)
371 int ret;
373 mutex_lock(&phydev->mdio.bus->mdio_lock);
374 ret = __phy_read_mmd(phydev, devad, regnum);
375 mutex_unlock(&phydev->mdio.bus->mdio_lock);
377 return ret;
379 EXPORT_SYMBOL(phy_read_mmd);
382 * __phy_write_mmd - Convenience function for writing a register
383 * on an MMD on a given PHY.
384 * @phydev: The phy_device struct
385 * @devad: The MMD to read from
386 * @regnum: The register on the MMD to read
387 * @val: value to write to @regnum
389 * Same rules as for __phy_write();
391 int __phy_write_mmd(struct phy_device *phydev, int devad, u32 regnum, u16 val)
393 int ret;
395 if (regnum > (u16)~0 || devad > 32)
396 return -EINVAL;
398 if (phydev->drv->write_mmd) {
399 ret = phydev->drv->write_mmd(phydev, devad, regnum, val);
400 } else if (phydev->is_c45) {
401 u32 addr = MII_ADDR_C45 | (devad << 16) | (regnum & 0xffff);
403 ret = __mdiobus_write(phydev->mdio.bus, phydev->mdio.addr,
404 addr, val);
405 } else {
406 struct mii_bus *bus = phydev->mdio.bus;
407 int phy_addr = phydev->mdio.addr;
409 mmd_phy_indirect(bus, phy_addr, devad, regnum);
411 /* Write the data into MMD's selected register */
412 __mdiobus_write(bus, phy_addr, MII_MMD_DATA, val);
414 ret = 0;
416 return ret;
418 EXPORT_SYMBOL(__phy_write_mmd);
421 * phy_write_mmd - Convenience function for writing a register
422 * on an MMD on a given PHY.
423 * @phydev: The phy_device struct
424 * @devad: The MMD to read from
425 * @regnum: The register on the MMD to read
426 * @val: value to write to @regnum
428 * Same rules as for phy_write();
430 int phy_write_mmd(struct phy_device *phydev, int devad, u32 regnum, u16 val)
432 int ret;
434 mutex_lock(&phydev->mdio.bus->mdio_lock);
435 ret = __phy_write_mmd(phydev, devad, regnum, val);
436 mutex_unlock(&phydev->mdio.bus->mdio_lock);
438 return ret;
440 EXPORT_SYMBOL(phy_write_mmd);
443 * __phy_modify_changed() - Convenience function for modifying a PHY register
444 * @phydev: a pointer to a &struct phy_device
445 * @regnum: register number
446 * @mask: bit mask of bits to clear
447 * @set: bit mask of bits to set
449 * Unlocked helper function which allows a PHY register to be modified as
450 * new register value = (old register value & ~mask) | set
452 * Returns negative errno, 0 if there was no change, and 1 in case of change
454 int __phy_modify_changed(struct phy_device *phydev, u32 regnum, u16 mask,
455 u16 set)
457 int new, ret;
459 ret = __phy_read(phydev, regnum);
460 if (ret < 0)
461 return ret;
463 new = (ret & ~mask) | set;
464 if (new == ret)
465 return 0;
467 ret = __phy_write(phydev, regnum, new);
469 return ret < 0 ? ret : 1;
471 EXPORT_SYMBOL_GPL(__phy_modify_changed);
474 * phy_modify_changed - Function for modifying a PHY register
475 * @phydev: the phy_device struct
476 * @regnum: register number to modify
477 * @mask: bit mask of bits to clear
478 * @set: new value of bits set in mask to write to @regnum
480 * NOTE: MUST NOT be called from interrupt context,
481 * because the bus read/write functions may wait for an interrupt
482 * to conclude the operation.
484 * Returns negative errno, 0 if there was no change, and 1 in case of change
486 int phy_modify_changed(struct phy_device *phydev, u32 regnum, u16 mask, u16 set)
488 int ret;
490 mutex_lock(&phydev->mdio.bus->mdio_lock);
491 ret = __phy_modify_changed(phydev, regnum, mask, set);
492 mutex_unlock(&phydev->mdio.bus->mdio_lock);
494 return ret;
496 EXPORT_SYMBOL_GPL(phy_modify_changed);
499 * __phy_modify - Convenience function for modifying a PHY register
500 * @phydev: the phy_device struct
501 * @regnum: register number to modify
502 * @mask: bit mask of bits to clear
503 * @set: new value of bits set in mask to write to @regnum
505 * NOTE: MUST NOT be called from interrupt context,
506 * because the bus read/write functions may wait for an interrupt
507 * to conclude the operation.
509 int __phy_modify(struct phy_device *phydev, u32 regnum, u16 mask, u16 set)
511 int ret;
513 ret = __phy_modify_changed(phydev, regnum, mask, set);
515 return ret < 0 ? ret : 0;
517 EXPORT_SYMBOL_GPL(__phy_modify);
520 * phy_modify - Convenience function for modifying a given PHY register
521 * @phydev: the phy_device struct
522 * @regnum: register number to write
523 * @mask: bit mask of bits to clear
524 * @set: new value of bits set in mask to write to @regnum
526 * NOTE: MUST NOT be called from interrupt context,
527 * because the bus read/write functions may wait for an interrupt
528 * to conclude the operation.
530 int phy_modify(struct phy_device *phydev, u32 regnum, u16 mask, u16 set)
532 int ret;
534 mutex_lock(&phydev->mdio.bus->mdio_lock);
535 ret = __phy_modify(phydev, regnum, mask, set);
536 mutex_unlock(&phydev->mdio.bus->mdio_lock);
538 return ret;
540 EXPORT_SYMBOL_GPL(phy_modify);
543 * __phy_modify_mmd_changed - Function for modifying a register on MMD
544 * @phydev: the phy_device struct
545 * @devad: the MMD containing register to modify
546 * @regnum: register number to modify
547 * @mask: bit mask of bits to clear
548 * @set: new value of bits set in mask to write to @regnum
550 * Unlocked helper function which allows a MMD register to be modified as
551 * new register value = (old register value & ~mask) | set
553 * Returns negative errno, 0 if there was no change, and 1 in case of change
555 int __phy_modify_mmd_changed(struct phy_device *phydev, int devad, u32 regnum,
556 u16 mask, u16 set)
558 int new, ret;
560 ret = __phy_read_mmd(phydev, devad, regnum);
561 if (ret < 0)
562 return ret;
564 new = (ret & ~mask) | set;
565 if (new == ret)
566 return 0;
568 ret = __phy_write_mmd(phydev, devad, regnum, new);
570 return ret < 0 ? ret : 1;
572 EXPORT_SYMBOL_GPL(__phy_modify_mmd_changed);
575 * phy_modify_mmd_changed - Function for modifying a register on MMD
576 * @phydev: the phy_device struct
577 * @devad: the MMD containing register to modify
578 * @regnum: register number to modify
579 * @mask: bit mask of bits to clear
580 * @set: new value of bits set in mask to write to @regnum
582 * NOTE: MUST NOT be called from interrupt context,
583 * because the bus read/write functions may wait for an interrupt
584 * to conclude the operation.
586 * Returns negative errno, 0 if there was no change, and 1 in case of change
588 int phy_modify_mmd_changed(struct phy_device *phydev, int devad, u32 regnum,
589 u16 mask, u16 set)
591 int ret;
593 mutex_lock(&phydev->mdio.bus->mdio_lock);
594 ret = __phy_modify_mmd_changed(phydev, devad, regnum, mask, set);
595 mutex_unlock(&phydev->mdio.bus->mdio_lock);
597 return ret;
599 EXPORT_SYMBOL_GPL(phy_modify_mmd_changed);
602 * __phy_modify_mmd - Convenience function for modifying a register on MMD
603 * @phydev: the phy_device struct
604 * @devad: the MMD containing register to modify
605 * @regnum: register number to modify
606 * @mask: bit mask of bits to clear
607 * @set: new value of bits set in mask to write to @regnum
609 * NOTE: MUST NOT be called from interrupt context,
610 * because the bus read/write functions may wait for an interrupt
611 * to conclude the operation.
613 int __phy_modify_mmd(struct phy_device *phydev, int devad, u32 regnum,
614 u16 mask, u16 set)
616 int ret;
618 ret = __phy_modify_mmd_changed(phydev, devad, regnum, mask, set);
620 return ret < 0 ? ret : 0;
622 EXPORT_SYMBOL_GPL(__phy_modify_mmd);
625 * phy_modify_mmd - Convenience function for modifying a register on MMD
626 * @phydev: the phy_device struct
627 * @devad: the MMD containing register to modify
628 * @regnum: register number to modify
629 * @mask: bit mask of bits to clear
630 * @set: new value of bits set in mask to write to @regnum
632 * NOTE: MUST NOT be called from interrupt context,
633 * because the bus read/write functions may wait for an interrupt
634 * to conclude the operation.
636 int phy_modify_mmd(struct phy_device *phydev, int devad, u32 regnum,
637 u16 mask, u16 set)
639 int ret;
641 mutex_lock(&phydev->mdio.bus->mdio_lock);
642 ret = __phy_modify_mmd(phydev, devad, regnum, mask, set);
643 mutex_unlock(&phydev->mdio.bus->mdio_lock);
645 return ret;
647 EXPORT_SYMBOL_GPL(phy_modify_mmd);
649 static int __phy_read_page(struct phy_device *phydev)
651 return phydev->drv->read_page(phydev);
654 static int __phy_write_page(struct phy_device *phydev, int page)
656 return phydev->drv->write_page(phydev, page);
660 * phy_save_page() - take the bus lock and save the current page
661 * @phydev: a pointer to a &struct phy_device
663 * Take the MDIO bus lock, and return the current page number. On error,
664 * returns a negative errno. phy_restore_page() must always be called
665 * after this, irrespective of success or failure of this call.
667 int phy_save_page(struct phy_device *phydev)
669 mutex_lock(&phydev->mdio.bus->mdio_lock);
670 return __phy_read_page(phydev);
672 EXPORT_SYMBOL_GPL(phy_save_page);
675 * phy_select_page() - take the bus lock, save the current page, and set a page
676 * @phydev: a pointer to a &struct phy_device
677 * @page: desired page
679 * Take the MDIO bus lock to protect against concurrent access, save the
680 * current PHY page, and set the current page. On error, returns a
681 * negative errno, otherwise returns the previous page number.
682 * phy_restore_page() must always be called after this, irrespective
683 * of success or failure of this call.
685 int phy_select_page(struct phy_device *phydev, int page)
687 int ret, oldpage;
689 oldpage = ret = phy_save_page(phydev);
690 if (ret < 0)
691 return ret;
693 if (oldpage != page) {
694 ret = __phy_write_page(phydev, page);
695 if (ret < 0)
696 return ret;
699 return oldpage;
701 EXPORT_SYMBOL_GPL(phy_select_page);
704 * phy_restore_page() - restore the page register and release the bus lock
705 * @phydev: a pointer to a &struct phy_device
706 * @oldpage: the old page, return value from phy_save_page() or phy_select_page()
707 * @ret: operation's return code
709 * Release the MDIO bus lock, restoring @oldpage if it is a valid page.
710 * This function propagates the earliest error code from the group of
711 * operations.
713 * Returns:
714 * @oldpage if it was a negative value, otherwise
715 * @ret if it was a negative errno value, otherwise
716 * phy_write_page()'s negative value if it were in error, otherwise
717 * @ret.
719 int phy_restore_page(struct phy_device *phydev, int oldpage, int ret)
721 int r;
723 if (oldpage >= 0) {
724 r = __phy_write_page(phydev, oldpage);
726 /* Propagate the operation return code if the page write
727 * was successful.
729 if (ret >= 0 && r < 0)
730 ret = r;
731 } else {
732 /* Propagate the phy page selection error code */
733 ret = oldpage;
736 mutex_unlock(&phydev->mdio.bus->mdio_lock);
738 return ret;
740 EXPORT_SYMBOL_GPL(phy_restore_page);
743 * phy_read_paged() - Convenience function for reading a paged register
744 * @phydev: a pointer to a &struct phy_device
745 * @page: the page for the phy
746 * @regnum: register number
748 * Same rules as for phy_read().
750 int phy_read_paged(struct phy_device *phydev, int page, u32 regnum)
752 int ret = 0, oldpage;
754 oldpage = phy_select_page(phydev, page);
755 if (oldpage >= 0)
756 ret = __phy_read(phydev, regnum);
758 return phy_restore_page(phydev, oldpage, ret);
760 EXPORT_SYMBOL(phy_read_paged);
763 * phy_write_paged() - Convenience function for writing a paged register
764 * @phydev: a pointer to a &struct phy_device
765 * @page: the page for the phy
766 * @regnum: register number
767 * @val: value to write
769 * Same rules as for phy_write().
771 int phy_write_paged(struct phy_device *phydev, int page, u32 regnum, u16 val)
773 int ret = 0, oldpage;
775 oldpage = phy_select_page(phydev, page);
776 if (oldpage >= 0)
777 ret = __phy_write(phydev, regnum, val);
779 return phy_restore_page(phydev, oldpage, ret);
781 EXPORT_SYMBOL(phy_write_paged);
784 * phy_modify_paged() - Convenience function for modifying a paged register
785 * @phydev: a pointer to a &struct phy_device
786 * @page: the page for the phy
787 * @regnum: register number
788 * @mask: bit mask of bits to clear
789 * @set: bit mask of bits to set
791 * Same rules as for phy_read() and phy_write().
793 int phy_modify_paged(struct phy_device *phydev, int page, u32 regnum,
794 u16 mask, u16 set)
796 int ret = 0, oldpage;
798 oldpage = phy_select_page(phydev, page);
799 if (oldpage >= 0)
800 ret = __phy_modify(phydev, regnum, mask, set);
802 return phy_restore_page(phydev, oldpage, ret);
804 EXPORT_SYMBOL(phy_modify_paged);