ff8e14b01eeb3e9ea8c6872124f3a336ef081c5c
[linux-2.6-microblaze.git] / drivers / net / phy / phy-core.c
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>
8
9 const char *phy_speed_to_str(int speed)
10 {
11         BUILD_BUG_ON_MSG(__ETHTOOL_LINK_MODE_MASK_NBITS != 90,
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");
15
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)";
51         }
52 }
53 EXPORT_SYMBOL_GPL(phy_speed_to_str);
54
55 const char *phy_duplex_to_str(unsigned int duplex)
56 {
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)";
64 }
65 EXPORT_SYMBOL_GPL(phy_duplex_to_str);
66
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. */
70
71 #define PHY_SETTING(s, d, b) { .speed = SPEED_ ## s, .duplex = DUPLEX_ ## d, \
72                                .bit = ETHTOOL_LINK_MODE_ ## b ## _BIT}
73
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         PHY_SETTING( 400000, FULL, 400000baseCR4_Full           ),
82         PHY_SETTING( 400000, FULL, 400000baseKR4_Full           ),
83         PHY_SETTING( 400000, FULL, 400000baseLR4_ER4_FR4_Full   ),
84         PHY_SETTING( 400000, FULL, 400000baseDR4_Full           ),
85         PHY_SETTING( 400000, FULL, 400000baseSR4_Full           ),
86         /* 200G */
87         PHY_SETTING( 200000, FULL, 200000baseCR4_Full           ),
88         PHY_SETTING( 200000, FULL, 200000baseKR4_Full           ),
89         PHY_SETTING( 200000, FULL, 200000baseLR4_ER4_FR4_Full   ),
90         PHY_SETTING( 200000, FULL, 200000baseDR4_Full           ),
91         PHY_SETTING( 200000, FULL, 200000baseSR4_Full           ),
92         PHY_SETTING( 200000, FULL, 200000baseCR2_Full           ),
93         PHY_SETTING( 200000, FULL, 200000baseKR2_Full           ),
94         PHY_SETTING( 200000, FULL, 200000baseLR2_ER2_FR2_Full   ),
95         PHY_SETTING( 200000, FULL, 200000baseDR2_Full           ),
96         PHY_SETTING( 200000, FULL, 200000baseSR2_Full           ),
97         /* 100G */
98         PHY_SETTING( 100000, FULL, 100000baseCR4_Full           ),
99         PHY_SETTING( 100000, FULL, 100000baseKR4_Full           ),
100         PHY_SETTING( 100000, FULL, 100000baseLR4_ER4_Full       ),
101         PHY_SETTING( 100000, FULL, 100000baseSR4_Full           ),
102         PHY_SETTING( 100000, FULL, 100000baseCR2_Full           ),
103         PHY_SETTING( 100000, FULL, 100000baseKR2_Full           ),
104         PHY_SETTING( 100000, FULL, 100000baseLR2_ER2_FR2_Full   ),
105         PHY_SETTING( 100000, FULL, 100000baseDR2_Full           ),
106         PHY_SETTING( 100000, FULL, 100000baseSR2_Full           ),
107         PHY_SETTING( 100000, FULL, 100000baseCR_Full            ),
108         PHY_SETTING( 100000, FULL, 100000baseKR_Full            ),
109         PHY_SETTING( 100000, FULL, 100000baseLR_ER_FR_Full      ),
110         PHY_SETTING( 100000, FULL, 100000baseDR_Full            ),
111         PHY_SETTING( 100000, FULL, 100000baseSR_Full            ),
112         /* 56G */
113         PHY_SETTING(  56000, FULL,  56000baseCR4_Full           ),
114         PHY_SETTING(  56000, FULL,  56000baseKR4_Full           ),
115         PHY_SETTING(  56000, FULL,  56000baseLR4_Full           ),
116         PHY_SETTING(  56000, FULL,  56000baseSR4_Full           ),
117         /* 50G */
118         PHY_SETTING(  50000, FULL,  50000baseCR2_Full           ),
119         PHY_SETTING(  50000, FULL,  50000baseKR2_Full           ),
120         PHY_SETTING(  50000, FULL,  50000baseSR2_Full           ),
121         PHY_SETTING(  50000, FULL,  50000baseCR_Full            ),
122         PHY_SETTING(  50000, FULL,  50000baseKR_Full            ),
123         PHY_SETTING(  50000, FULL,  50000baseLR_ER_FR_Full      ),
124         PHY_SETTING(  50000, FULL,  50000baseDR_Full            ),
125         PHY_SETTING(  50000, FULL,  50000baseSR_Full            ),
126         /* 40G */
127         PHY_SETTING(  40000, FULL,  40000baseCR4_Full           ),
128         PHY_SETTING(  40000, FULL,  40000baseKR4_Full           ),
129         PHY_SETTING(  40000, FULL,  40000baseLR4_Full           ),
130         PHY_SETTING(  40000, FULL,  40000baseSR4_Full           ),
131         /* 25G */
132         PHY_SETTING(  25000, FULL,  25000baseCR_Full            ),
133         PHY_SETTING(  25000, FULL,  25000baseKR_Full            ),
134         PHY_SETTING(  25000, FULL,  25000baseSR_Full            ),
135         /* 20G */
136         PHY_SETTING(  20000, FULL,  20000baseKR2_Full           ),
137         PHY_SETTING(  20000, FULL,  20000baseMLD2_Full          ),
138         /* 10G */
139         PHY_SETTING(  10000, FULL,  10000baseCR_Full            ),
140         PHY_SETTING(  10000, FULL,  10000baseER_Full            ),
141         PHY_SETTING(  10000, FULL,  10000baseKR_Full            ),
142         PHY_SETTING(  10000, FULL,  10000baseKX4_Full           ),
143         PHY_SETTING(  10000, FULL,  10000baseLR_Full            ),
144         PHY_SETTING(  10000, FULL,  10000baseLRM_Full           ),
145         PHY_SETTING(  10000, FULL,  10000baseR_FEC              ),
146         PHY_SETTING(  10000, FULL,  10000baseSR_Full            ),
147         PHY_SETTING(  10000, FULL,  10000baseT_Full             ),
148         /* 5G */
149         PHY_SETTING(   5000, FULL,   5000baseT_Full             ),
150         /* 2.5G */
151         PHY_SETTING(   2500, FULL,   2500baseT_Full             ),
152         PHY_SETTING(   2500, FULL,   2500baseX_Full             ),
153         /* 1G */
154         PHY_SETTING(   1000, FULL,   1000baseKX_Full            ),
155         PHY_SETTING(   1000, FULL,   1000baseT_Full             ),
156         PHY_SETTING(   1000, HALF,   1000baseT_Half             ),
157         PHY_SETTING(   1000, FULL,   1000baseT1_Full            ),
158         PHY_SETTING(   1000, FULL,   1000baseX_Full             ),
159         /* 100M */
160         PHY_SETTING(    100, FULL,    100baseT_Full             ),
161         PHY_SETTING(    100, FULL,    100baseT1_Full            ),
162         PHY_SETTING(    100, HALF,    100baseT_Half             ),
163         /* 10M */
164         PHY_SETTING(     10, FULL,     10baseT_Full             ),
165         PHY_SETTING(     10, HALF,     10baseT_Half             ),
166 };
167 #undef PHY_SETTING
168
169 /**
170  * phy_lookup_setting - lookup a PHY setting
171  * @speed: speed to match
172  * @duplex: duplex to match
173  * @mask: allowed link modes
174  * @exact: an exact match is required
175  *
176  * Search the settings array for a setting that matches the speed and
177  * duplex, and which is supported.
178  *
179  * If @exact is unset, either an exact match or %NULL for no match will
180  * be returned.
181  *
182  * If @exact is set, an exact match, the fastest supported setting at
183  * or below the specified speed, the slowest supported setting, or if
184  * they all fail, %NULL will be returned.
185  */
186 const struct phy_setting *
187 phy_lookup_setting(int speed, int duplex, const unsigned long *mask, bool exact)
188 {
189         const struct phy_setting *p, *match = NULL, *last = NULL;
190         int i;
191
192         for (i = 0, p = settings; i < ARRAY_SIZE(settings); i++, p++) {
193                 if (p->bit < __ETHTOOL_LINK_MODE_MASK_NBITS &&
194                     test_bit(p->bit, mask)) {
195                         last = p;
196                         if (p->speed == speed && p->duplex == duplex) {
197                                 /* Exact match for speed and duplex */
198                                 match = p;
199                                 break;
200                         } else if (!exact) {
201                                 if (!match && p->speed <= speed)
202                                         /* Candidate */
203                                         match = p;
204
205                                 if (p->speed < speed)
206                                         break;
207                         }
208                 }
209         }
210
211         if (!match && !exact)
212                 match = last;
213
214         return match;
215 }
216 EXPORT_SYMBOL_GPL(phy_lookup_setting);
217
218 size_t phy_speeds(unsigned int *speeds, size_t size,
219                   unsigned long *mask)
220 {
221         size_t count;
222         int i;
223
224         for (i = 0, count = 0; i < ARRAY_SIZE(settings) && count < size; i++)
225                 if (settings[i].bit < __ETHTOOL_LINK_MODE_MASK_NBITS &&
226                     test_bit(settings[i].bit, mask) &&
227                     (count == 0 || speeds[count - 1] != settings[i].speed))
228                         speeds[count++] = settings[i].speed;
229
230         return count;
231 }
232
233 static int __set_linkmode_max_speed(u32 max_speed, unsigned long *addr)
234 {
235         const struct phy_setting *p;
236         int i;
237
238         for (i = 0, p = settings; i < ARRAY_SIZE(settings); i++, p++) {
239                 if (p->speed > max_speed)
240                         linkmode_clear_bit(p->bit, addr);
241                 else
242                         break;
243         }
244
245         return 0;
246 }
247
248 static int __set_phy_supported(struct phy_device *phydev, u32 max_speed)
249 {
250         return __set_linkmode_max_speed(max_speed, phydev->supported);
251 }
252
253 int phy_set_max_speed(struct phy_device *phydev, u32 max_speed)
254 {
255         int err;
256
257         err = __set_phy_supported(phydev, max_speed);
258         if (err)
259                 return err;
260
261         phy_advertise_supported(phydev);
262
263         return 0;
264 }
265 EXPORT_SYMBOL(phy_set_max_speed);
266
267 void of_set_phy_supported(struct phy_device *phydev)
268 {
269         struct device_node *node = phydev->mdio.dev.of_node;
270         u32 max_speed;
271
272         if (!IS_ENABLED(CONFIG_OF_MDIO))
273                 return;
274
275         if (!node)
276                 return;
277
278         if (!of_property_read_u32(node, "max-speed", &max_speed))
279                 __set_phy_supported(phydev, max_speed);
280 }
281
282 void of_set_phy_eee_broken(struct phy_device *phydev)
283 {
284         struct device_node *node = phydev->mdio.dev.of_node;
285         u32 broken = 0;
286
287         if (!IS_ENABLED(CONFIG_OF_MDIO))
288                 return;
289
290         if (!node)
291                 return;
292
293         if (of_property_read_bool(node, "eee-broken-100tx"))
294                 broken |= MDIO_EEE_100TX;
295         if (of_property_read_bool(node, "eee-broken-1000t"))
296                 broken |= MDIO_EEE_1000T;
297         if (of_property_read_bool(node, "eee-broken-10gt"))
298                 broken |= MDIO_EEE_10GT;
299         if (of_property_read_bool(node, "eee-broken-1000kx"))
300                 broken |= MDIO_EEE_1000KX;
301         if (of_property_read_bool(node, "eee-broken-10gkx4"))
302                 broken |= MDIO_EEE_10GKX4;
303         if (of_property_read_bool(node, "eee-broken-10gkr"))
304                 broken |= MDIO_EEE_10GKR;
305
306         phydev->eee_broken_modes = broken;
307 }
308
309 void phy_resolve_aneg_pause(struct phy_device *phydev)
310 {
311         if (phydev->duplex == DUPLEX_FULL) {
312                 phydev->pause = linkmode_test_bit(ETHTOOL_LINK_MODE_Pause_BIT,
313                                                   phydev->lp_advertising);
314                 phydev->asym_pause = linkmode_test_bit(
315                         ETHTOOL_LINK_MODE_Asym_Pause_BIT,
316                         phydev->lp_advertising);
317         }
318 }
319 EXPORT_SYMBOL_GPL(phy_resolve_aneg_pause);
320
321 /**
322  * phy_resolve_aneg_linkmode - resolve the advertisements into phy settings
323  * @phydev: The phy_device struct
324  *
325  * Resolve our and the link partner advertisements into their corresponding
326  * speed and duplex. If full duplex was negotiated, extract the pause mode
327  * from the link partner mask.
328  */
329 void phy_resolve_aneg_linkmode(struct phy_device *phydev)
330 {
331         __ETHTOOL_DECLARE_LINK_MODE_MASK(common);
332         int i;
333
334         linkmode_and(common, phydev->lp_advertising, phydev->advertising);
335
336         for (i = 0; i < ARRAY_SIZE(settings); i++)
337                 if (test_bit(settings[i].bit, common)) {
338                         phydev->speed = settings[i].speed;
339                         phydev->duplex = settings[i].duplex;
340                         break;
341                 }
342
343         phy_resolve_aneg_pause(phydev);
344 }
345 EXPORT_SYMBOL_GPL(phy_resolve_aneg_linkmode);
346
347 /**
348  * phy_check_downshift - check whether downshift occurred
349  * @phydev: The phy_device struct
350  *
351  * Check whether a downshift to a lower speed occurred. If this should be the
352  * case warn the user.
353  * Prerequisite for detecting downshift is that PHY driver implements the
354  * read_status callback and sets phydev->speed to the actual link speed.
355  */
356 void phy_check_downshift(struct phy_device *phydev)
357 {
358         __ETHTOOL_DECLARE_LINK_MODE_MASK(common);
359         int i, speed = SPEED_UNKNOWN;
360
361         phydev->downshifted_rate = 0;
362
363         if (phydev->autoneg == AUTONEG_DISABLE ||
364             phydev->speed == SPEED_UNKNOWN)
365                 return;
366
367         linkmode_and(common, phydev->lp_advertising, phydev->advertising);
368
369         for (i = 0; i < ARRAY_SIZE(settings); i++)
370                 if (test_bit(settings[i].bit, common)) {
371                         speed = settings[i].speed;
372                         break;
373                 }
374
375         if (speed == SPEED_UNKNOWN || phydev->speed >= speed)
376                 return;
377
378         phydev_warn(phydev, "Downshift occurred from negotiated speed %s to actual speed %s, check cabling!\n",
379                     phy_speed_to_str(speed), phy_speed_to_str(phydev->speed));
380
381         phydev->downshifted_rate = 1;
382 }
383 EXPORT_SYMBOL_GPL(phy_check_downshift);
384
385 static int phy_resolve_min_speed(struct phy_device *phydev, bool fdx_only)
386 {
387         __ETHTOOL_DECLARE_LINK_MODE_MASK(common);
388         int i = ARRAY_SIZE(settings);
389
390         linkmode_and(common, phydev->lp_advertising, phydev->advertising);
391
392         while (--i >= 0) {
393                 if (test_bit(settings[i].bit, common)) {
394                         if (fdx_only && settings[i].duplex != DUPLEX_FULL)
395                                 continue;
396                         return settings[i].speed;
397                 }
398         }
399
400         return SPEED_UNKNOWN;
401 }
402
403 int phy_speed_down_core(struct phy_device *phydev)
404 {
405         int min_common_speed = phy_resolve_min_speed(phydev, true);
406
407         if (min_common_speed == SPEED_UNKNOWN)
408                 return -EINVAL;
409
410         return __set_linkmode_max_speed(min_common_speed, phydev->advertising);
411 }
412
413 static void mmd_phy_indirect(struct mii_bus *bus, int phy_addr, int devad,
414                              u16 regnum)
415 {
416         /* Write the desired MMD Devad */
417         __mdiobus_write(bus, phy_addr, MII_MMD_CTRL, devad);
418
419         /* Write the desired MMD register address */
420         __mdiobus_write(bus, phy_addr, MII_MMD_DATA, regnum);
421
422         /* Select the Function : DATA with no post increment */
423         __mdiobus_write(bus, phy_addr, MII_MMD_CTRL,
424                         devad | MII_MMD_CTRL_NOINCR);
425 }
426
427 /**
428  * __phy_read_mmd - Convenience function for reading a register
429  * from an MMD on a given PHY.
430  * @phydev: The phy_device struct
431  * @devad: The MMD to read from (0..31)
432  * @regnum: The register on the MMD to read (0..65535)
433  *
434  * Same rules as for __phy_read();
435  */
436 int __phy_read_mmd(struct phy_device *phydev, int devad, u32 regnum)
437 {
438         int val;
439
440         if (regnum > (u16)~0 || devad > 32)
441                 return -EINVAL;
442
443         if (phydev->drv && phydev->drv->read_mmd) {
444                 val = phydev->drv->read_mmd(phydev, devad, regnum);
445         } else if (phydev->is_c45) {
446                 val = __mdiobus_c45_read(phydev->mdio.bus, phydev->mdio.addr,
447                                          devad, regnum);
448         } else {
449                 struct mii_bus *bus = phydev->mdio.bus;
450                 int phy_addr = phydev->mdio.addr;
451
452                 mmd_phy_indirect(bus, phy_addr, devad, regnum);
453
454                 /* Read the content of the MMD's selected register */
455                 val = __mdiobus_read(bus, phy_addr, MII_MMD_DATA);
456         }
457         return val;
458 }
459 EXPORT_SYMBOL(__phy_read_mmd);
460
461 /**
462  * phy_read_mmd - Convenience function for reading a register
463  * from an MMD on a given PHY.
464  * @phydev: The phy_device struct
465  * @devad: The MMD to read from
466  * @regnum: The register on the MMD to read
467  *
468  * Same rules as for phy_read();
469  */
470 int phy_read_mmd(struct phy_device *phydev, int devad, u32 regnum)
471 {
472         int ret;
473
474         phy_lock_mdio_bus(phydev);
475         ret = __phy_read_mmd(phydev, devad, regnum);
476         phy_unlock_mdio_bus(phydev);
477
478         return ret;
479 }
480 EXPORT_SYMBOL(phy_read_mmd);
481
482 /**
483  * __phy_write_mmd - Convenience function for writing a register
484  * on an MMD on a given PHY.
485  * @phydev: The phy_device struct
486  * @devad: The MMD to read from
487  * @regnum: The register on the MMD to read
488  * @val: value to write to @regnum
489  *
490  * Same rules as for __phy_write();
491  */
492 int __phy_write_mmd(struct phy_device *phydev, int devad, u32 regnum, u16 val)
493 {
494         int ret;
495
496         if (regnum > (u16)~0 || devad > 32)
497                 return -EINVAL;
498
499         if (phydev->drv && phydev->drv->write_mmd) {
500                 ret = phydev->drv->write_mmd(phydev, devad, regnum, val);
501         } else if (phydev->is_c45) {
502                 ret = __mdiobus_c45_write(phydev->mdio.bus, phydev->mdio.addr,
503                                           devad, regnum, val);
504         } else {
505                 struct mii_bus *bus = phydev->mdio.bus;
506                 int phy_addr = phydev->mdio.addr;
507
508                 mmd_phy_indirect(bus, phy_addr, devad, regnum);
509
510                 /* Write the data into MMD's selected register */
511                 __mdiobus_write(bus, phy_addr, MII_MMD_DATA, val);
512
513                 ret = 0;
514         }
515         return ret;
516 }
517 EXPORT_SYMBOL(__phy_write_mmd);
518
519 /**
520  * phy_write_mmd - Convenience function for writing a register
521  * on an MMD on a given PHY.
522  * @phydev: The phy_device struct
523  * @devad: The MMD to read from
524  * @regnum: The register on the MMD to read
525  * @val: value to write to @regnum
526  *
527  * Same rules as for phy_write();
528  */
529 int phy_write_mmd(struct phy_device *phydev, int devad, u32 regnum, u16 val)
530 {
531         int ret;
532
533         phy_lock_mdio_bus(phydev);
534         ret = __phy_write_mmd(phydev, devad, regnum, val);
535         phy_unlock_mdio_bus(phydev);
536
537         return ret;
538 }
539 EXPORT_SYMBOL(phy_write_mmd);
540
541 /**
542  * phy_modify_changed - Function for modifying a PHY register
543  * @phydev: the phy_device struct
544  * @regnum: register number to modify
545  * @mask: bit mask of bits to clear
546  * @set: new value of bits set in mask to write to @regnum
547  *
548  * NOTE: MUST NOT be called from interrupt context,
549  * because the bus read/write functions may wait for an interrupt
550  * to conclude the operation.
551  *
552  * Returns negative errno, 0 if there was no change, and 1 in case of change
553  */
554 int phy_modify_changed(struct phy_device *phydev, u32 regnum, u16 mask, u16 set)
555 {
556         int ret;
557
558         phy_lock_mdio_bus(phydev);
559         ret = __phy_modify_changed(phydev, regnum, mask, set);
560         phy_unlock_mdio_bus(phydev);
561
562         return ret;
563 }
564 EXPORT_SYMBOL_GPL(phy_modify_changed);
565
566 /**
567  * __phy_modify - Convenience function for modifying a PHY register
568  * @phydev: the phy_device struct
569  * @regnum: register number to modify
570  * @mask: bit mask of bits to clear
571  * @set: new value of bits set in mask to write to @regnum
572  *
573  * NOTE: MUST NOT be called from interrupt context,
574  * because the bus read/write functions may wait for an interrupt
575  * to conclude the operation.
576  */
577 int __phy_modify(struct phy_device *phydev, u32 regnum, u16 mask, u16 set)
578 {
579         int ret;
580
581         ret = __phy_modify_changed(phydev, regnum, mask, set);
582
583         return ret < 0 ? ret : 0;
584 }
585 EXPORT_SYMBOL_GPL(__phy_modify);
586
587 /**
588  * phy_modify - Convenience function for modifying a given PHY register
589  * @phydev: the phy_device struct
590  * @regnum: register number to write
591  * @mask: bit mask of bits to clear
592  * @set: new value of bits set in mask to write to @regnum
593  *
594  * NOTE: MUST NOT be called from interrupt context,
595  * because the bus read/write functions may wait for an interrupt
596  * to conclude the operation.
597  */
598 int phy_modify(struct phy_device *phydev, u32 regnum, u16 mask, u16 set)
599 {
600         int ret;
601
602         phy_lock_mdio_bus(phydev);
603         ret = __phy_modify(phydev, regnum, mask, set);
604         phy_unlock_mdio_bus(phydev);
605
606         return ret;
607 }
608 EXPORT_SYMBOL_GPL(phy_modify);
609
610 /**
611  * __phy_modify_mmd_changed - Function for modifying a register on MMD
612  * @phydev: the phy_device struct
613  * @devad: the MMD containing register to modify
614  * @regnum: register number to modify
615  * @mask: bit mask of bits to clear
616  * @set: new value of bits set in mask to write to @regnum
617  *
618  * Unlocked helper function which allows a MMD register to be modified as
619  * new register value = (old register value & ~mask) | set
620  *
621  * Returns negative errno, 0 if there was no change, and 1 in case of change
622  */
623 int __phy_modify_mmd_changed(struct phy_device *phydev, int devad, u32 regnum,
624                              u16 mask, u16 set)
625 {
626         int new, ret;
627
628         ret = __phy_read_mmd(phydev, devad, regnum);
629         if (ret < 0)
630                 return ret;
631
632         new = (ret & ~mask) | set;
633         if (new == ret)
634                 return 0;
635
636         ret = __phy_write_mmd(phydev, devad, regnum, new);
637
638         return ret < 0 ? ret : 1;
639 }
640 EXPORT_SYMBOL_GPL(__phy_modify_mmd_changed);
641
642 /**
643  * phy_modify_mmd_changed - Function for modifying a register on MMD
644  * @phydev: the phy_device struct
645  * @devad: the MMD containing register to modify
646  * @regnum: register number to modify
647  * @mask: bit mask of bits to clear
648  * @set: new value of bits set in mask to write to @regnum
649  *
650  * NOTE: MUST NOT be called from interrupt context,
651  * because the bus read/write functions may wait for an interrupt
652  * to conclude the operation.
653  *
654  * Returns negative errno, 0 if there was no change, and 1 in case of change
655  */
656 int phy_modify_mmd_changed(struct phy_device *phydev, int devad, u32 regnum,
657                            u16 mask, u16 set)
658 {
659         int ret;
660
661         phy_lock_mdio_bus(phydev);
662         ret = __phy_modify_mmd_changed(phydev, devad, regnum, mask, set);
663         phy_unlock_mdio_bus(phydev);
664
665         return ret;
666 }
667 EXPORT_SYMBOL_GPL(phy_modify_mmd_changed);
668
669 /**
670  * __phy_modify_mmd - Convenience function for modifying a register on MMD
671  * @phydev: the phy_device struct
672  * @devad: the MMD containing register to modify
673  * @regnum: register number to modify
674  * @mask: bit mask of bits to clear
675  * @set: new value of bits set in mask to write to @regnum
676  *
677  * NOTE: MUST NOT be called from interrupt context,
678  * because the bus read/write functions may wait for an interrupt
679  * to conclude the operation.
680  */
681 int __phy_modify_mmd(struct phy_device *phydev, int devad, u32 regnum,
682                      u16 mask, u16 set)
683 {
684         int ret;
685
686         ret = __phy_modify_mmd_changed(phydev, devad, regnum, mask, set);
687
688         return ret < 0 ? ret : 0;
689 }
690 EXPORT_SYMBOL_GPL(__phy_modify_mmd);
691
692 /**
693  * phy_modify_mmd - Convenience function for modifying a register on MMD
694  * @phydev: the phy_device struct
695  * @devad: the MMD containing register to modify
696  * @regnum: register number to modify
697  * @mask: bit mask of bits to clear
698  * @set: new value of bits set in mask to write to @regnum
699  *
700  * NOTE: MUST NOT be called from interrupt context,
701  * because the bus read/write functions may wait for an interrupt
702  * to conclude the operation.
703  */
704 int phy_modify_mmd(struct phy_device *phydev, int devad, u32 regnum,
705                    u16 mask, u16 set)
706 {
707         int ret;
708
709         phy_lock_mdio_bus(phydev);
710         ret = __phy_modify_mmd(phydev, devad, regnum, mask, set);
711         phy_unlock_mdio_bus(phydev);
712
713         return ret;
714 }
715 EXPORT_SYMBOL_GPL(phy_modify_mmd);
716
717 static int __phy_read_page(struct phy_device *phydev)
718 {
719         if (WARN_ONCE(!phydev->drv->read_page, "read_page callback not available, PHY driver not loaded?\n"))
720                 return -EOPNOTSUPP;
721
722         return phydev->drv->read_page(phydev);
723 }
724
725 static int __phy_write_page(struct phy_device *phydev, int page)
726 {
727         if (WARN_ONCE(!phydev->drv->write_page, "write_page callback not available, PHY driver not loaded?\n"))
728                 return -EOPNOTSUPP;
729
730         return phydev->drv->write_page(phydev, page);
731 }
732
733 /**
734  * phy_save_page() - take the bus lock and save the current page
735  * @phydev: a pointer to a &struct phy_device
736  *
737  * Take the MDIO bus lock, and return the current page number. On error,
738  * returns a negative errno. phy_restore_page() must always be called
739  * after this, irrespective of success or failure of this call.
740  */
741 int phy_save_page(struct phy_device *phydev)
742 {
743         phy_lock_mdio_bus(phydev);
744         return __phy_read_page(phydev);
745 }
746 EXPORT_SYMBOL_GPL(phy_save_page);
747
748 /**
749  * phy_select_page() - take the bus lock, save the current page, and set a page
750  * @phydev: a pointer to a &struct phy_device
751  * @page: desired page
752  *
753  * Take the MDIO bus lock to protect against concurrent access, save the
754  * current PHY page, and set the current page.  On error, returns a
755  * negative errno, otherwise returns the previous page number.
756  * phy_restore_page() must always be called after this, irrespective
757  * of success or failure of this call.
758  */
759 int phy_select_page(struct phy_device *phydev, int page)
760 {
761         int ret, oldpage;
762
763         oldpage = ret = phy_save_page(phydev);
764         if (ret < 0)
765                 return ret;
766
767         if (oldpage != page) {
768                 ret = __phy_write_page(phydev, page);
769                 if (ret < 0)
770                         return ret;
771         }
772
773         return oldpage;
774 }
775 EXPORT_SYMBOL_GPL(phy_select_page);
776
777 /**
778  * phy_restore_page() - restore the page register and release the bus lock
779  * @phydev: a pointer to a &struct phy_device
780  * @oldpage: the old page, return value from phy_save_page() or phy_select_page()
781  * @ret: operation's return code
782  *
783  * Release the MDIO bus lock, restoring @oldpage if it is a valid page.
784  * This function propagates the earliest error code from the group of
785  * operations.
786  *
787  * Returns:
788  *   @oldpage if it was a negative value, otherwise
789  *   @ret if it was a negative errno value, otherwise
790  *   phy_write_page()'s negative value if it were in error, otherwise
791  *   @ret.
792  */
793 int phy_restore_page(struct phy_device *phydev, int oldpage, int ret)
794 {
795         int r;
796
797         if (oldpage >= 0) {
798                 r = __phy_write_page(phydev, oldpage);
799
800                 /* Propagate the operation return code if the page write
801                  * was successful.
802                  */
803                 if (ret >= 0 && r < 0)
804                         ret = r;
805         } else {
806                 /* Propagate the phy page selection error code */
807                 ret = oldpage;
808         }
809
810         phy_unlock_mdio_bus(phydev);
811
812         return ret;
813 }
814 EXPORT_SYMBOL_GPL(phy_restore_page);
815
816 /**
817  * phy_read_paged() - Convenience function for reading a paged register
818  * @phydev: a pointer to a &struct phy_device
819  * @page: the page for the phy
820  * @regnum: register number
821  *
822  * Same rules as for phy_read().
823  */
824 int phy_read_paged(struct phy_device *phydev, int page, u32 regnum)
825 {
826         int ret = 0, oldpage;
827
828         oldpage = phy_select_page(phydev, page);
829         if (oldpage >= 0)
830                 ret = __phy_read(phydev, regnum);
831
832         return phy_restore_page(phydev, oldpage, ret);
833 }
834 EXPORT_SYMBOL(phy_read_paged);
835
836 /**
837  * phy_write_paged() - Convenience function for writing a paged register
838  * @phydev: a pointer to a &struct phy_device
839  * @page: the page for the phy
840  * @regnum: register number
841  * @val: value to write
842  *
843  * Same rules as for phy_write().
844  */
845 int phy_write_paged(struct phy_device *phydev, int page, u32 regnum, u16 val)
846 {
847         int ret = 0, oldpage;
848
849         oldpage = phy_select_page(phydev, page);
850         if (oldpage >= 0)
851                 ret = __phy_write(phydev, regnum, val);
852
853         return phy_restore_page(phydev, oldpage, ret);
854 }
855 EXPORT_SYMBOL(phy_write_paged);
856
857 /**
858  * phy_modify_paged_changed() - Function for modifying a paged register
859  * @phydev: a pointer to a &struct phy_device
860  * @page: the page for the phy
861  * @regnum: register number
862  * @mask: bit mask of bits to clear
863  * @set: bit mask of bits to set
864  *
865  * Returns negative errno, 0 if there was no change, and 1 in case of change
866  */
867 int phy_modify_paged_changed(struct phy_device *phydev, int page, u32 regnum,
868                              u16 mask, u16 set)
869 {
870         int ret = 0, oldpage;
871
872         oldpage = phy_select_page(phydev, page);
873         if (oldpage >= 0)
874                 ret = __phy_modify_changed(phydev, regnum, mask, set);
875
876         return phy_restore_page(phydev, oldpage, ret);
877 }
878 EXPORT_SYMBOL(phy_modify_paged_changed);
879
880 /**
881  * phy_modify_paged() - Convenience function for modifying a paged register
882  * @phydev: a pointer to a &struct phy_device
883  * @page: the page for the phy
884  * @regnum: register number
885  * @mask: bit mask of bits to clear
886  * @set: bit mask of bits to set
887  *
888  * Same rules as for phy_read() and phy_write().
889  */
890 int phy_modify_paged(struct phy_device *phydev, int page, u32 regnum,
891                      u16 mask, u16 set)
892 {
893         int ret = phy_modify_paged_changed(phydev, page, regnum, mask, set);
894
895         return ret < 0 ? ret : 0;
896 }
897 EXPORT_SYMBOL(phy_modify_paged);