]> git.proxmox.com Git - mirror_ubuntu-jammy-kernel.git/blame - drivers/net/phy/phy-core.c
Merge git://git.kernel.org/pub/scm/linux/kernel/git/netdev/net
[mirror_ubuntu-jammy-kernel.git] / drivers / net / phy / phy-core.c
CommitLineData
a2443fd1 1// SPDX-License-Identifier: GPL-2.0+
9860118b
RK
2/*
3 * Core PHY library, taken from phy.c
9860118b
RK
4 */
5#include <linux/export.h>
6#include <linux/phy.h>
a4eaed9f 7#include <linux/of.h>
9860118b 8
da4625ac
RK
9const char *phy_speed_to_str(int speed)
10{
14af7fd1 11 BUILD_BUG_ON_MSG(__ETHTOOL_LINK_MODE_MASK_NBITS != 74,
c6576bfe
HK
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
da4625ac
RK
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";
5a3144e4
HK
43 case SPEED_200000:
44 return "200Gbps";
14af7fd1
JP
45 case SPEED_400000:
46 return "400Gbps";
da4625ac
RK
47 case SPEED_UNKNOWN:
48 return "Unknown";
49 default:
50 return "Unsupported (update phy-core.c)";
51 }
52}
53EXPORT_SYMBOL_GPL(phy_speed_to_str);
54
55const 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}
65EXPORT_SYMBOL_GPL(phy_duplex_to_str);
66
0ccb4fc6
RK
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. */
f1538eca
HK
70
71#define PHY_SETTING(s, d, b) { .speed = SPEED_ ## s, .duplex = DUPLEX_ ## d, \
72 .bit = ETHTOOL_LINK_MODE_ ## b ## _BIT}
73
0ccb4fc6 74static const struct phy_setting settings[] = {
14af7fd1
JP
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 ),
5a3144e4
HK
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 ),
3c6b59d6 87 /* 100G */
f1538eca
HK
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 ),
5a3144e4
HK
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 ),
3c6b59d6 97 /* 56G */
f1538eca
HK
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 ),
3c6b59d6 102 /* 50G */
f1538eca
HK
103 PHY_SETTING( 50000, FULL, 50000baseCR2_Full ),
104 PHY_SETTING( 50000, FULL, 50000baseKR2_Full ),
105 PHY_SETTING( 50000, FULL, 50000baseSR2_Full ),
5a3144e4
HK
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 ),
3c6b59d6 111 /* 40G */
f1538eca
HK
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 ),
3c6b59d6 116 /* 25G */
f1538eca
HK
117 PHY_SETTING( 25000, FULL, 25000baseCR_Full ),
118 PHY_SETTING( 25000, FULL, 25000baseKR_Full ),
119 PHY_SETTING( 25000, FULL, 25000baseSR_Full ),
3c6b59d6 120 /* 20G */
f1538eca
HK
121 PHY_SETTING( 20000, FULL, 20000baseKR2_Full ),
122 PHY_SETTING( 20000, FULL, 20000baseMLD2_Full ),
3c6b59d6 123 /* 10G */
f1538eca
HK
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 ),
3c6b59d6 133 /* 5G */
f1538eca 134 PHY_SETTING( 5000, FULL, 5000baseT_Full ),
3c6b59d6 135 /* 2.5G */
f1538eca
HK
136 PHY_SETTING( 2500, FULL, 2500baseT_Full ),
137 PHY_SETTING( 2500, FULL, 2500baseX_Full ),
3c6b59d6 138 /* 1G */
f1538eca
HK
139 PHY_SETTING( 1000, FULL, 1000baseKX_Full ),
140 PHY_SETTING( 1000, FULL, 1000baseT_Full ),
141 PHY_SETTING( 1000, HALF, 1000baseT_Half ),
b2557764 142 PHY_SETTING( 1000, FULL, 1000baseT1_Full ),
f1538eca 143 PHY_SETTING( 1000, FULL, 1000baseX_Full ),
3c6b59d6 144 /* 100M */
f1538eca 145 PHY_SETTING( 100, FULL, 100baseT_Full ),
b2557764 146 PHY_SETTING( 100, FULL, 100baseT1_Full ),
f1538eca 147 PHY_SETTING( 100, HALF, 100baseT_Half ),
3c6b59d6 148 /* 10M */
f1538eca
HK
149 PHY_SETTING( 10, FULL, 10baseT_Full ),
150 PHY_SETTING( 10, HALF, 10baseT_Half ),
0ccb4fc6 151};
f1538eca 152#undef PHY_SETTING
0ccb4fc6
RK
153
154/**
155 * phy_lookup_setting - lookup a PHY setting
156 * @speed: speed to match
157 * @duplex: duplex to match
158 * @mask: allowed link modes
0ccb4fc6
RK
159 * @exact: an exact match is required
160 *
161 * Search the settings array for a setting that matches the speed and
162 * duplex, and which is supported.
163 *
164 * If @exact is unset, either an exact match or %NULL for no match will
165 * be returned.
166 *
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.
170 */
171const struct phy_setting *
3c1bcc86 172phy_lookup_setting(int speed, int duplex, const unsigned long *mask, bool exact)
0ccb4fc6
RK
173{
174 const struct phy_setting *p, *match = NULL, *last = NULL;
175 int i;
176
177 for (i = 0, p = settings; i < ARRAY_SIZE(settings); i++, p++) {
3c1bcc86
AL
178 if (p->bit < __ETHTOOL_LINK_MODE_MASK_NBITS &&
179 test_bit(p->bit, mask)) {
0ccb4fc6
RK
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;
189
190 if (p->speed < speed)
191 break;
192 }
193 }
194 }
195
196 if (!match && !exact)
197 match = last;
198
199 return match;
200}
201EXPORT_SYMBOL_GPL(phy_lookup_setting);
202
203size_t phy_speeds(unsigned int *speeds, size_t size,
3c1bcc86 204 unsigned long *mask)
0ccb4fc6
RK
205{
206 size_t count;
207 int i;
208
209 for (i = 0, count = 0; i < ARRAY_SIZE(settings) && count < size; i++)
3c1bcc86 210 if (settings[i].bit < __ETHTOOL_LINK_MODE_MASK_NBITS &&
0ccb4fc6
RK
211 test_bit(settings[i].bit, mask) &&
212 (count == 0 || speeds[count - 1] != settings[i].speed))
213 speeds[count++] = settings[i].speed;
214
215 return count;
216}
217
7b261e0e 218static int __set_linkmode_max_speed(u32 max_speed, unsigned long *addr)
a4eaed9f
MC
219{
220 const struct phy_setting *p;
221 int i;
222
223 for (i = 0, p = settings; i < ARRAY_SIZE(settings); i++, p++) {
224 if (p->speed > max_speed)
7b261e0e 225 linkmode_clear_bit(p->bit, addr);
a4eaed9f
MC
226 else
227 break;
228 }
229
230 return 0;
231}
232
7b261e0e
HK
233static int __set_phy_supported(struct phy_device *phydev, u32 max_speed)
234{
235 return __set_linkmode_max_speed(max_speed, phydev->supported);
236}
237
a4eaed9f
MC
238int phy_set_max_speed(struct phy_device *phydev, u32 max_speed)
239{
240 int err;
241
242 err = __set_phy_supported(phydev, max_speed);
243 if (err)
244 return err;
245
22c0ef6b 246 phy_advertise_supported(phydev);
a4eaed9f
MC
247
248 return 0;
249}
250EXPORT_SYMBOL(phy_set_max_speed);
251
252void of_set_phy_supported(struct phy_device *phydev)
253{
254 struct device_node *node = phydev->mdio.dev.of_node;
255 u32 max_speed;
256
257 if (!IS_ENABLED(CONFIG_OF_MDIO))
258 return;
259
260 if (!node)
261 return;
262
263 if (!of_property_read_u32(node, "max-speed", &max_speed))
264 __set_phy_supported(phydev, max_speed);
265}
266
3feb9b23
MC
267void of_set_phy_eee_broken(struct phy_device *phydev)
268{
269 struct device_node *node = phydev->mdio.dev.of_node;
270 u32 broken = 0;
271
272 if (!IS_ENABLED(CONFIG_OF_MDIO))
273 return;
274
275 if (!node)
276 return;
277
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;
290
291 phydev->eee_broken_modes = broken;
292}
293
2d880b87
RK
294void phy_resolve_aneg_pause(struct phy_device *phydev)
295{
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);
302 }
303}
304EXPORT_SYMBOL_GPL(phy_resolve_aneg_pause);
305
8c5e850c 306/**
cc1122b0 307 * phy_resolve_aneg_linkmode - resolve the advertisements into phy settings
8c5e850c
RK
308 * @phydev: The phy_device struct
309 *
cc1122b0 310 * Resolve our and the link partner advertisements into their corresponding
8c5e850c
RK
311 * speed and duplex. If full duplex was negotiated, extract the pause mode
312 * from the link partner mask.
313 */
314void phy_resolve_aneg_linkmode(struct phy_device *phydev)
315{
3c1bcc86 316 __ETHTOOL_DECLARE_LINK_MODE_MASK(common);
a2703de7 317 int i;
8c5e850c 318
c0ec3c27 319 linkmode_and(common, phydev->lp_advertising, phydev->advertising);
3c1bcc86 320
a2703de7
HK
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;
326 }
8c5e850c 327
2d880b87 328 phy_resolve_aneg_pause(phydev);
8c5e850c
RK
329}
330EXPORT_SYMBOL_GPL(phy_resolve_aneg_linkmode);
331
331c56ac
HK
332static int phy_resolve_min_speed(struct phy_device *phydev, bool fdx_only)
333{
334 __ETHTOOL_DECLARE_LINK_MODE_MASK(common);
335 int i = ARRAY_SIZE(settings);
336
337 linkmode_and(common, phydev->lp_advertising, phydev->advertising);
338
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;
344 }
345 }
346
347 return SPEED_UNKNOWN;
348}
349
350int phy_speed_down_core(struct phy_device *phydev)
351{
352 int min_common_speed = phy_resolve_min_speed(phydev, true);
353
354 if (min_common_speed == SPEED_UNKNOWN)
355 return -EINVAL;
356
357 return __set_linkmode_max_speed(min_common_speed, phydev->advertising);
358}
359
060fbc89
RK
360static void mmd_phy_indirect(struct mii_bus *bus, int phy_addr, int devad,
361 u16 regnum)
9860118b
RK
362{
363 /* Write the desired MMD Devad */
1b2dea2e 364 __mdiobus_write(bus, phy_addr, MII_MMD_CTRL, devad);
9860118b
RK
365
366 /* Write the desired MMD register address */
1b2dea2e 367 __mdiobus_write(bus, phy_addr, MII_MMD_DATA, regnum);
9860118b
RK
368
369 /* Select the Function : DATA with no post increment */
1b2dea2e
RK
370 __mdiobus_write(bus, phy_addr, MII_MMD_CTRL,
371 devad | MII_MMD_CTRL_NOINCR);
9860118b
RK
372}
373
9860118b 374/**
1878f0dc 375 * __phy_read_mmd - Convenience function for reading a register
9860118b
RK
376 * from an MMD on a given PHY.
377 * @phydev: The phy_device struct
3b85d8df
RK
378 * @devad: The MMD to read from (0..31)
379 * @regnum: The register on the MMD to read (0..65535)
9860118b 380 *
1878f0dc 381 * Same rules as for __phy_read();
9860118b 382 */
1878f0dc 383int __phy_read_mmd(struct phy_device *phydev, int devad, u32 regnum)
9860118b 384{
3b85d8df
RK
385 int val;
386
1ee6b9bc
RK
387 if (regnum > (u16)~0 || devad > 32)
388 return -EINVAL;
9860118b 389
3b85d8df
RK
390 if (phydev->drv->read_mmd) {
391 val = phydev->drv->read_mmd(phydev, devad, regnum);
392 } else if (phydev->is_c45) {
1ee6b9bc 393 u32 addr = MII_ADDR_C45 | (devad << 16) | (regnum & 0xffff);
1ee6b9bc 394
1878f0dc 395 val = __mdiobus_read(phydev->mdio.bus, phydev->mdio.addr, addr);
3b85d8df 396 } else {
9860118b 397 struct mii_bus *bus = phydev->mdio.bus;
3b85d8df 398 int phy_addr = phydev->mdio.addr;
9860118b 399
060fbc89 400 mmd_phy_indirect(bus, phy_addr, devad, regnum);
9860118b 401
3b85d8df 402 /* Read the content of the MMD's selected register */
1b2dea2e 403 val = __mdiobus_read(bus, phy_addr, MII_MMD_DATA);
9860118b 404 }
3b85d8df 405 return val;
9860118b 406}
1878f0dc
NY
407EXPORT_SYMBOL(__phy_read_mmd);
408
409/**
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
415 *
416 * Same rules as for phy_read();
417 */
418int phy_read_mmd(struct phy_device *phydev, int devad, u32 regnum)
419{
420 int ret;
421
bec170e5 422 phy_lock_mdio_bus(phydev);
1878f0dc 423 ret = __phy_read_mmd(phydev, devad, regnum);
bec170e5 424 phy_unlock_mdio_bus(phydev);
1878f0dc
NY
425
426 return ret;
427}
3b85d8df 428EXPORT_SYMBOL(phy_read_mmd);
9860118b
RK
429
430/**
1878f0dc 431 * __phy_write_mmd - Convenience function for writing a register
9860118b
RK
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
437 *
1878f0dc 438 * Same rules as for __phy_write();
9860118b 439 */
1878f0dc 440int __phy_write_mmd(struct phy_device *phydev, int devad, u32 regnum, u16 val)
9860118b 441{
3b85d8df
RK
442 int ret;
443
1ee6b9bc
RK
444 if (regnum > (u16)~0 || devad > 32)
445 return -EINVAL;
446
1dbba4cb 447 if (phydev->drv->write_mmd) {
3b85d8df
RK
448 ret = phydev->drv->write_mmd(phydev, devad, regnum, val);
449 } else if (phydev->is_c45) {
1ee6b9bc
RK
450 u32 addr = MII_ADDR_C45 | (devad << 16) | (regnum & 0xffff);
451
1878f0dc
NY
452 ret = __mdiobus_write(phydev->mdio.bus, phydev->mdio.addr,
453 addr, val);
3b85d8df
RK
454 } else {
455 struct mii_bus *bus = phydev->mdio.bus;
456 int phy_addr = phydev->mdio.addr;
9860118b 457
060fbc89 458 mmd_phy_indirect(bus, phy_addr, devad, regnum);
9860118b 459
3b85d8df 460 /* Write the data into MMD's selected register */
1b2dea2e 461 __mdiobus_write(bus, phy_addr, MII_MMD_DATA, val);
3b85d8df
RK
462
463 ret = 0;
464 }
465 return ret;
9860118b 466}
1878f0dc
NY
467EXPORT_SYMBOL(__phy_write_mmd);
468
469/**
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
476 *
477 * Same rules as for phy_write();
478 */
479int phy_write_mmd(struct phy_device *phydev, int devad, u32 regnum, u16 val)
480{
481 int ret;
482
bec170e5 483 phy_lock_mdio_bus(phydev);
1878f0dc 484 ret = __phy_write_mmd(phydev, devad, regnum, val);
bec170e5 485 phy_unlock_mdio_bus(phydev);
1878f0dc
NY
486
487 return ret;
488}
9860118b 489EXPORT_SYMBOL(phy_write_mmd);
788f9933
RK
490
491/**
b8554d4f 492 * __phy_modify_changed() - Convenience function for modifying a PHY register
788f9933
RK
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
497 *
498 * Unlocked helper function which allows a PHY register to be modified as
f102852f 499 * new register value = (old register value & ~mask) | set
b8554d4f
HK
500 *
501 * Returns negative errno, 0 if there was no change, and 1 in case of change
788f9933 502 */
b8554d4f
HK
503int __phy_modify_changed(struct phy_device *phydev, u32 regnum, u16 mask,
504 u16 set)
788f9933 505{
b8554d4f 506 int new, ret;
788f9933
RK
507
508 ret = __phy_read(phydev, regnum);
9f239fe6
AL
509 if (ret < 0)
510 return ret;
788f9933 511
b8554d4f
HK
512 new = (ret & ~mask) | set;
513 if (new == ret)
514 return 0;
515
516 ret = __phy_write(phydev, regnum, new);
517
518 return ret < 0 ? ret : 1;
519}
520EXPORT_SYMBOL_GPL(__phy_modify_changed);
521
522/**
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
528 *
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.
532 *
533 * Returns negative errno, 0 if there was no change, and 1 in case of change
534 */
535int phy_modify_changed(struct phy_device *phydev, u32 regnum, u16 mask, u16 set)
536{
537 int ret;
538
bec170e5 539 phy_lock_mdio_bus(phydev);
b8554d4f 540 ret = __phy_modify_changed(phydev, regnum, mask, set);
bec170e5 541 phy_unlock_mdio_bus(phydev);
b8554d4f
HK
542
543 return ret;
544}
545EXPORT_SYMBOL_GPL(phy_modify_changed);
546
547/**
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
553 *
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.
557 */
558int __phy_modify(struct phy_device *phydev, u32 regnum, u16 mask, u16 set)
559{
560 int ret;
561
562 ret = __phy_modify_changed(phydev, regnum, mask, set);
9f239fe6
AL
563
564 return ret < 0 ? ret : 0;
788f9933
RK
565}
566EXPORT_SYMBOL_GPL(__phy_modify);
78ffc4ac 567
2b74e5be
RK
568/**
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
574 *
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.
578 */
579int phy_modify(struct phy_device *phydev, u32 regnum, u16 mask, u16 set)
580{
581 int ret;
582
bec170e5 583 phy_lock_mdio_bus(phydev);
2b74e5be 584 ret = __phy_modify(phydev, regnum, mask, set);
bec170e5 585 phy_unlock_mdio_bus(phydev);
2b74e5be
RK
586
587 return ret;
588}
589EXPORT_SYMBOL_GPL(phy_modify);
590
1878f0dc 591/**
b8554d4f 592 * __phy_modify_mmd_changed - Function for modifying a register on MMD
1878f0dc
NY
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
598 *
599 * Unlocked helper function which allows a MMD register to be modified as
600 * new register value = (old register value & ~mask) | set
b8554d4f
HK
601 *
602 * Returns negative errno, 0 if there was no change, and 1 in case of change
1878f0dc 603 */
b8554d4f
HK
604int __phy_modify_mmd_changed(struct phy_device *phydev, int devad, u32 regnum,
605 u16 mask, u16 set)
1878f0dc 606{
b8554d4f 607 int new, ret;
1878f0dc
NY
608
609 ret = __phy_read_mmd(phydev, devad, regnum);
610 if (ret < 0)
611 return ret;
612
b8554d4f
HK
613 new = (ret & ~mask) | set;
614 if (new == ret)
615 return 0;
616
617 ret = __phy_write_mmd(phydev, devad, regnum, new);
618
619 return ret < 0 ? ret : 1;
620}
621EXPORT_SYMBOL_GPL(__phy_modify_mmd_changed);
622
623/**
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
630 *
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.
634 *
635 * Returns negative errno, 0 if there was no change, and 1 in case of change
636 */
637int phy_modify_mmd_changed(struct phy_device *phydev, int devad, u32 regnum,
638 u16 mask, u16 set)
639{
640 int ret;
641
bec170e5 642 phy_lock_mdio_bus(phydev);
b8554d4f 643 ret = __phy_modify_mmd_changed(phydev, devad, regnum, mask, set);
bec170e5 644 phy_unlock_mdio_bus(phydev);
b8554d4f
HK
645
646 return ret;
647}
648EXPORT_SYMBOL_GPL(phy_modify_mmd_changed);
649
650/**
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
657 *
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.
661 */
662int __phy_modify_mmd(struct phy_device *phydev, int devad, u32 regnum,
663 u16 mask, u16 set)
664{
665 int ret;
666
667 ret = __phy_modify_mmd_changed(phydev, devad, regnum, mask, set);
1878f0dc
NY
668
669 return ret < 0 ? ret : 0;
670}
671EXPORT_SYMBOL_GPL(__phy_modify_mmd);
672
673/**
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
680 *
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.
684 */
685int phy_modify_mmd(struct phy_device *phydev, int devad, u32 regnum,
686 u16 mask, u16 set)
687{
688 int ret;
689
bec170e5 690 phy_lock_mdio_bus(phydev);
1878f0dc 691 ret = __phy_modify_mmd(phydev, devad, regnum, mask, set);
bec170e5 692 phy_unlock_mdio_bus(phydev);
1878f0dc
NY
693
694 return ret;
695}
696EXPORT_SYMBOL_GPL(phy_modify_mmd);
697
78ffc4ac
RK
698static int __phy_read_page(struct phy_device *phydev)
699{
f86854a2
HK
700 if (WARN_ONCE(!phydev->drv->read_page, "read_page callback not available, PHY driver not loaded?\n"))
701 return -EOPNOTSUPP;
702
78ffc4ac
RK
703 return phydev->drv->read_page(phydev);
704}
705
706static int __phy_write_page(struct phy_device *phydev, int page)
707{
f86854a2
HK
708 if (WARN_ONCE(!phydev->drv->write_page, "write_page callback not available, PHY driver not loaded?\n"))
709 return -EOPNOTSUPP;
710
78ffc4ac
RK
711 return phydev->drv->write_page(phydev, page);
712}
713
714/**
715 * phy_save_page() - take the bus lock and save the current page
716 * @phydev: a pointer to a &struct phy_device
717 *
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.
721 */
722int phy_save_page(struct phy_device *phydev)
723{
bec170e5 724 phy_lock_mdio_bus(phydev);
78ffc4ac
RK
725 return __phy_read_page(phydev);
726}
727EXPORT_SYMBOL_GPL(phy_save_page);
728
729/**
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
733 *
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.
739 */
740int phy_select_page(struct phy_device *phydev, int page)
741{
742 int ret, oldpage;
743
744 oldpage = ret = phy_save_page(phydev);
745 if (ret < 0)
746 return ret;
747
748 if (oldpage != page) {
749 ret = __phy_write_page(phydev, page);
750 if (ret < 0)
751 return ret;
752 }
753
754 return oldpage;
755}
756EXPORT_SYMBOL_GPL(phy_select_page);
757
758/**
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
763 *
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.
767 *
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.
773 */
774int phy_restore_page(struct phy_device *phydev, int oldpage, int ret)
775{
776 int r;
777
778 if (oldpage >= 0) {
779 r = __phy_write_page(phydev, oldpage);
780
781 /* Propagate the operation return code if the page write
782 * was successful.
783 */
784 if (ret >= 0 && r < 0)
785 ret = r;
786 } else {
787 /* Propagate the phy page selection error code */
788 ret = oldpage;
789 }
790
bec170e5 791 phy_unlock_mdio_bus(phydev);
78ffc4ac
RK
792
793 return ret;
794}
795EXPORT_SYMBOL_GPL(phy_restore_page);
796
797/**
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
802 *
803 * Same rules as for phy_read().
804 */
805int phy_read_paged(struct phy_device *phydev, int page, u32 regnum)
806{
807 int ret = 0, oldpage;
808
809 oldpage = phy_select_page(phydev, page);
810 if (oldpage >= 0)
811 ret = __phy_read(phydev, regnum);
812
813 return phy_restore_page(phydev, oldpage, ret);
814}
815EXPORT_SYMBOL(phy_read_paged);
816
817/**
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
823 *
824 * Same rules as for phy_write().
825 */
826int phy_write_paged(struct phy_device *phydev, int page, u32 regnum, u16 val)
827{
828 int ret = 0, oldpage;
829
830 oldpage = phy_select_page(phydev, page);
831 if (oldpage >= 0)
832 ret = __phy_write(phydev, regnum, val);
833
834 return phy_restore_page(phydev, oldpage, ret);
835}
836EXPORT_SYMBOL(phy_write_paged);
837
838/**
bf22b343 839 * phy_modify_paged_changed() - Function for modifying a paged register
78ffc4ac
RK
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
845 *
bf22b343 846 * Returns negative errno, 0 if there was no change, and 1 in case of change
78ffc4ac 847 */
bf22b343
HK
848int phy_modify_paged_changed(struct phy_device *phydev, int page, u32 regnum,
849 u16 mask, u16 set)
78ffc4ac
RK
850{
851 int ret = 0, oldpage;
852
853 oldpage = phy_select_page(phydev, page);
854 if (oldpage >= 0)
bf22b343 855 ret = __phy_modify_changed(phydev, regnum, mask, set);
78ffc4ac
RK
856
857 return phy_restore_page(phydev, oldpage, ret);
858}
bf22b343
HK
859EXPORT_SYMBOL(phy_modify_paged_changed);
860
861/**
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
868 *
869 * Same rules as for phy_read() and phy_write().
870 */
871int phy_modify_paged(struct phy_device *phydev, int page, u32 regnum,
872 u16 mask, u16 set)
873{
874 int ret = phy_modify_paged_changed(phydev, page, regnum, mask, set);
875
876 return ret < 0 ? ret : 0;
877}
78ffc4ac 878EXPORT_SYMBOL(phy_modify_paged);