Loading...
1/*
2 * Core PHY library, taken from phy.c
3 *
4 * This program is free software; you can redistribute it and/or modify it
5 * under the terms of the GNU General Public License as published by the
6 * Free Software Foundation; either version 2 of the License, or (at your
7 * option) any later version.
8 */
9#include <linux/export.h>
10#include <linux/phy.h>
11
12const char *phy_speed_to_str(int speed)
13{
14 switch (speed) {
15 case SPEED_10:
16 return "10Mbps";
17 case SPEED_100:
18 return "100Mbps";
19 case SPEED_1000:
20 return "1Gbps";
21 case SPEED_2500:
22 return "2.5Gbps";
23 case SPEED_5000:
24 return "5Gbps";
25 case SPEED_10000:
26 return "10Gbps";
27 case SPEED_14000:
28 return "14Gbps";
29 case SPEED_20000:
30 return "20Gbps";
31 case SPEED_25000:
32 return "25Gbps";
33 case SPEED_40000:
34 return "40Gbps";
35 case SPEED_50000:
36 return "50Gbps";
37 case SPEED_56000:
38 return "56Gbps";
39 case SPEED_100000:
40 return "100Gbps";
41 case SPEED_UNKNOWN:
42 return "Unknown";
43 default:
44 return "Unsupported (update phy-core.c)";
45 }
46}
47EXPORT_SYMBOL_GPL(phy_speed_to_str);
48
49const char *phy_duplex_to_str(unsigned int duplex)
50{
51 if (duplex == DUPLEX_HALF)
52 return "Half";
53 if (duplex == DUPLEX_FULL)
54 return "Full";
55 if (duplex == DUPLEX_UNKNOWN)
56 return "Unknown";
57 return "Unsupported (update phy-core.c)";
58}
59EXPORT_SYMBOL_GPL(phy_duplex_to_str);
60
61/* A mapping of all SUPPORTED settings to speed/duplex. This table
62 * must be grouped by speed and sorted in descending match priority
63 * - iow, descending speed. */
64static const struct phy_setting settings[] = {
65 {
66 .speed = SPEED_10000,
67 .duplex = DUPLEX_FULL,
68 .bit = ETHTOOL_LINK_MODE_10000baseKR_Full_BIT,
69 },
70 {
71 .speed = SPEED_10000,
72 .duplex = DUPLEX_FULL,
73 .bit = ETHTOOL_LINK_MODE_10000baseKX4_Full_BIT,
74 },
75 {
76 .speed = SPEED_10000,
77 .duplex = DUPLEX_FULL,
78 .bit = ETHTOOL_LINK_MODE_10000baseT_Full_BIT,
79 },
80 {
81 .speed = SPEED_2500,
82 .duplex = DUPLEX_FULL,
83 .bit = ETHTOOL_LINK_MODE_2500baseX_Full_BIT,
84 },
85 {
86 .speed = SPEED_1000,
87 .duplex = DUPLEX_FULL,
88 .bit = ETHTOOL_LINK_MODE_1000baseKX_Full_BIT,
89 },
90 {
91 .speed = SPEED_1000,
92 .duplex = DUPLEX_FULL,
93 .bit = ETHTOOL_LINK_MODE_1000baseX_Full_BIT,
94 },
95 {
96 .speed = SPEED_1000,
97 .duplex = DUPLEX_FULL,
98 .bit = ETHTOOL_LINK_MODE_1000baseT_Full_BIT,
99 },
100 {
101 .speed = SPEED_1000,
102 .duplex = DUPLEX_HALF,
103 .bit = ETHTOOL_LINK_MODE_1000baseT_Half_BIT,
104 },
105 {
106 .speed = SPEED_100,
107 .duplex = DUPLEX_FULL,
108 .bit = ETHTOOL_LINK_MODE_100baseT_Full_BIT,
109 },
110 {
111 .speed = SPEED_100,
112 .duplex = DUPLEX_HALF,
113 .bit = ETHTOOL_LINK_MODE_100baseT_Half_BIT,
114 },
115 {
116 .speed = SPEED_10,
117 .duplex = DUPLEX_FULL,
118 .bit = ETHTOOL_LINK_MODE_10baseT_Full_BIT,
119 },
120 {
121 .speed = SPEED_10,
122 .duplex = DUPLEX_HALF,
123 .bit = ETHTOOL_LINK_MODE_10baseT_Half_BIT,
124 },
125};
126
127/**
128 * phy_lookup_setting - lookup a PHY setting
129 * @speed: speed to match
130 * @duplex: duplex to match
131 * @mask: allowed link modes
132 * @maxbit: bit size of link modes
133 * @exact: an exact match is required
134 *
135 * Search the settings array for a setting that matches the speed and
136 * duplex, and which is supported.
137 *
138 * If @exact is unset, either an exact match or %NULL for no match will
139 * be returned.
140 *
141 * If @exact is set, an exact match, the fastest supported setting at
142 * or below the specified speed, the slowest supported setting, or if
143 * they all fail, %NULL will be returned.
144 */
145const struct phy_setting *
146phy_lookup_setting(int speed, int duplex, const unsigned long *mask,
147 size_t maxbit, bool exact)
148{
149 const struct phy_setting *p, *match = NULL, *last = NULL;
150 int i;
151
152 for (i = 0, p = settings; i < ARRAY_SIZE(settings); i++, p++) {
153 if (p->bit < maxbit && test_bit(p->bit, mask)) {
154 last = p;
155 if (p->speed == speed && p->duplex == duplex) {
156 /* Exact match for speed and duplex */
157 match = p;
158 break;
159 } else if (!exact) {
160 if (!match && p->speed <= speed)
161 /* Candidate */
162 match = p;
163
164 if (p->speed < speed)
165 break;
166 }
167 }
168 }
169
170 if (!match && !exact)
171 match = last;
172
173 return match;
174}
175EXPORT_SYMBOL_GPL(phy_lookup_setting);
176
177size_t phy_speeds(unsigned int *speeds, size_t size,
178 unsigned long *mask, size_t maxbit)
179{
180 size_t count;
181 int i;
182
183 for (i = 0, count = 0; i < ARRAY_SIZE(settings) && count < size; i++)
184 if (settings[i].bit < maxbit &&
185 test_bit(settings[i].bit, mask) &&
186 (count == 0 || speeds[count - 1] != settings[i].speed))
187 speeds[count++] = settings[i].speed;
188
189 return count;
190}
191
192/**
193 * phy_resolve_aneg_linkmode - resolve the advertisements into phy settings
194 * @phydev: The phy_device struct
195 *
196 * Resolve our and the link partner advertisements into their corresponding
197 * speed and duplex. If full duplex was negotiated, extract the pause mode
198 * from the link partner mask.
199 */
200void phy_resolve_aneg_linkmode(struct phy_device *phydev)
201{
202 u32 common = phydev->lp_advertising & phydev->advertising;
203
204 if (common & ADVERTISED_10000baseT_Full) {
205 phydev->speed = SPEED_10000;
206 phydev->duplex = DUPLEX_FULL;
207 } else if (common & ADVERTISED_1000baseT_Full) {
208 phydev->speed = SPEED_1000;
209 phydev->duplex = DUPLEX_FULL;
210 } else if (common & ADVERTISED_1000baseT_Half) {
211 phydev->speed = SPEED_1000;
212 phydev->duplex = DUPLEX_HALF;
213 } else if (common & ADVERTISED_100baseT_Full) {
214 phydev->speed = SPEED_100;
215 phydev->duplex = DUPLEX_FULL;
216 } else if (common & ADVERTISED_100baseT_Half) {
217 phydev->speed = SPEED_100;
218 phydev->duplex = DUPLEX_HALF;
219 } else if (common & ADVERTISED_10baseT_Full) {
220 phydev->speed = SPEED_10;
221 phydev->duplex = DUPLEX_FULL;
222 } else if (common & ADVERTISED_10baseT_Half) {
223 phydev->speed = SPEED_10;
224 phydev->duplex = DUPLEX_HALF;
225 }
226
227 if (phydev->duplex == DUPLEX_FULL) {
228 phydev->pause = !!(phydev->lp_advertising & ADVERTISED_Pause);
229 phydev->asym_pause = !!(phydev->lp_advertising &
230 ADVERTISED_Asym_Pause);
231 }
232}
233EXPORT_SYMBOL_GPL(phy_resolve_aneg_linkmode);
234
235static void mmd_phy_indirect(struct mii_bus *bus, int phy_addr, int devad,
236 u16 regnum)
237{
238 /* Write the desired MMD Devad */
239 __mdiobus_write(bus, phy_addr, MII_MMD_CTRL, devad);
240
241 /* Write the desired MMD register address */
242 __mdiobus_write(bus, phy_addr, MII_MMD_DATA, regnum);
243
244 /* Select the Function : DATA with no post increment */
245 __mdiobus_write(bus, phy_addr, MII_MMD_CTRL,
246 devad | MII_MMD_CTRL_NOINCR);
247}
248
249/**
250 * phy_read_mmd - Convenience function for reading a register
251 * from an MMD on a given PHY.
252 * @phydev: The phy_device struct
253 * @devad: The MMD to read from (0..31)
254 * @regnum: The register on the MMD to read (0..65535)
255 *
256 * Same rules as for phy_read();
257 */
258int phy_read_mmd(struct phy_device *phydev, int devad, u32 regnum)
259{
260 int val;
261
262 if (regnum > (u16)~0 || devad > 32)
263 return -EINVAL;
264
265 if (phydev->drv->read_mmd) {
266 val = phydev->drv->read_mmd(phydev, devad, regnum);
267 } else if (phydev->is_c45) {
268 u32 addr = MII_ADDR_C45 | (devad << 16) | (regnum & 0xffff);
269
270 val = mdiobus_read(phydev->mdio.bus, phydev->mdio.addr, addr);
271 } else {
272 struct mii_bus *bus = phydev->mdio.bus;
273 int phy_addr = phydev->mdio.addr;
274
275 mutex_lock(&bus->mdio_lock);
276 mmd_phy_indirect(bus, phy_addr, devad, regnum);
277
278 /* Read the content of the MMD's selected register */
279 val = __mdiobus_read(bus, phy_addr, MII_MMD_DATA);
280 mutex_unlock(&bus->mdio_lock);
281 }
282 return val;
283}
284EXPORT_SYMBOL(phy_read_mmd);
285
286/**
287 * phy_write_mmd - Convenience function for writing a register
288 * on an MMD on a given PHY.
289 * @phydev: The phy_device struct
290 * @devad: The MMD to read from
291 * @regnum: The register on the MMD to read
292 * @val: value to write to @regnum
293 *
294 * Same rules as for phy_write();
295 */
296int phy_write_mmd(struct phy_device *phydev, int devad, u32 regnum, u16 val)
297{
298 int ret;
299
300 if (regnum > (u16)~0 || devad > 32)
301 return -EINVAL;
302
303 if (phydev->drv->write_mmd) {
304 ret = phydev->drv->write_mmd(phydev, devad, regnum, val);
305 } else if (phydev->is_c45) {
306 u32 addr = MII_ADDR_C45 | (devad << 16) | (regnum & 0xffff);
307
308 ret = mdiobus_write(phydev->mdio.bus, phydev->mdio.addr,
309 addr, val);
310 } else {
311 struct mii_bus *bus = phydev->mdio.bus;
312 int phy_addr = phydev->mdio.addr;
313
314 mutex_lock(&bus->mdio_lock);
315 mmd_phy_indirect(bus, phy_addr, devad, regnum);
316
317 /* Write the data into MMD's selected register */
318 __mdiobus_write(bus, phy_addr, MII_MMD_DATA, val);
319 mutex_unlock(&bus->mdio_lock);
320
321 ret = 0;
322 }
323 return ret;
324}
325EXPORT_SYMBOL(phy_write_mmd);
326
327/**
328 * __phy_modify() - Convenience function for modifying a PHY register
329 * @phydev: a pointer to a &struct phy_device
330 * @regnum: register number
331 * @mask: bit mask of bits to clear
332 * @set: bit mask of bits to set
333 *
334 * Unlocked helper function which allows a PHY register to be modified as
335 * new register value = (old register value & ~mask) | set
336 */
337int __phy_modify(struct phy_device *phydev, u32 regnum, u16 mask, u16 set)
338{
339 int ret;
340
341 ret = __phy_read(phydev, regnum);
342 if (ret < 0)
343 return ret;
344
345 ret = __phy_write(phydev, regnum, (ret & ~mask) | set);
346
347 return ret < 0 ? ret : 0;
348}
349EXPORT_SYMBOL_GPL(__phy_modify);
350
351/**
352 * phy_modify - Convenience function for modifying a given PHY register
353 * @phydev: the phy_device struct
354 * @regnum: register number to write
355 * @mask: bit mask of bits to clear
356 * @set: new value of bits set in mask to write to @regnum
357 *
358 * NOTE: MUST NOT be called from interrupt context,
359 * because the bus read/write functions may wait for an interrupt
360 * to conclude the operation.
361 */
362int phy_modify(struct phy_device *phydev, u32 regnum, u16 mask, u16 set)
363{
364 int ret;
365
366 mutex_lock(&phydev->mdio.bus->mdio_lock);
367 ret = __phy_modify(phydev, regnum, mask, set);
368 mutex_unlock(&phydev->mdio.bus->mdio_lock);
369
370 return ret;
371}
372EXPORT_SYMBOL_GPL(phy_modify);
373
374static int __phy_read_page(struct phy_device *phydev)
375{
376 return phydev->drv->read_page(phydev);
377}
378
379static int __phy_write_page(struct phy_device *phydev, int page)
380{
381 return phydev->drv->write_page(phydev, page);
382}
383
384/**
385 * phy_save_page() - take the bus lock and save the current page
386 * @phydev: a pointer to a &struct phy_device
387 *
388 * Take the MDIO bus lock, and return the current page number. On error,
389 * returns a negative errno. phy_restore_page() must always be called
390 * after this, irrespective of success or failure of this call.
391 */
392int phy_save_page(struct phy_device *phydev)
393{
394 mutex_lock(&phydev->mdio.bus->mdio_lock);
395 return __phy_read_page(phydev);
396}
397EXPORT_SYMBOL_GPL(phy_save_page);
398
399/**
400 * phy_select_page() - take the bus lock, save the current page, and set a page
401 * @phydev: a pointer to a &struct phy_device
402 * @page: desired page
403 *
404 * Take the MDIO bus lock to protect against concurrent access, save the
405 * current PHY page, and set the current page. On error, returns a
406 * negative errno, otherwise returns the previous page number.
407 * phy_restore_page() must always be called after this, irrespective
408 * of success or failure of this call.
409 */
410int phy_select_page(struct phy_device *phydev, int page)
411{
412 int ret, oldpage;
413
414 oldpage = ret = phy_save_page(phydev);
415 if (ret < 0)
416 return ret;
417
418 if (oldpage != page) {
419 ret = __phy_write_page(phydev, page);
420 if (ret < 0)
421 return ret;
422 }
423
424 return oldpage;
425}
426EXPORT_SYMBOL_GPL(phy_select_page);
427
428/**
429 * phy_restore_page() - restore the page register and release the bus lock
430 * @phydev: a pointer to a &struct phy_device
431 * @oldpage: the old page, return value from phy_save_page() or phy_select_page()
432 * @ret: operation's return code
433 *
434 * Release the MDIO bus lock, restoring @oldpage if it is a valid page.
435 * This function propagates the earliest error code from the group of
436 * operations.
437 *
438 * Returns:
439 * @oldpage if it was a negative value, otherwise
440 * @ret if it was a negative errno value, otherwise
441 * phy_write_page()'s negative value if it were in error, otherwise
442 * @ret.
443 */
444int phy_restore_page(struct phy_device *phydev, int oldpage, int ret)
445{
446 int r;
447
448 if (oldpage >= 0) {
449 r = __phy_write_page(phydev, oldpage);
450
451 /* Propagate the operation return code if the page write
452 * was successful.
453 */
454 if (ret >= 0 && r < 0)
455 ret = r;
456 } else {
457 /* Propagate the phy page selection error code */
458 ret = oldpage;
459 }
460
461 mutex_unlock(&phydev->mdio.bus->mdio_lock);
462
463 return ret;
464}
465EXPORT_SYMBOL_GPL(phy_restore_page);
466
467/**
468 * phy_read_paged() - Convenience function for reading a paged register
469 * @phydev: a pointer to a &struct phy_device
470 * @page: the page for the phy
471 * @regnum: register number
472 *
473 * Same rules as for phy_read().
474 */
475int phy_read_paged(struct phy_device *phydev, int page, u32 regnum)
476{
477 int ret = 0, oldpage;
478
479 oldpage = phy_select_page(phydev, page);
480 if (oldpage >= 0)
481 ret = __phy_read(phydev, regnum);
482
483 return phy_restore_page(phydev, oldpage, ret);
484}
485EXPORT_SYMBOL(phy_read_paged);
486
487/**
488 * phy_write_paged() - Convenience function for writing a paged register
489 * @phydev: a pointer to a &struct phy_device
490 * @page: the page for the phy
491 * @regnum: register number
492 * @val: value to write
493 *
494 * Same rules as for phy_write().
495 */
496int phy_write_paged(struct phy_device *phydev, int page, u32 regnum, u16 val)
497{
498 int ret = 0, oldpage;
499
500 oldpage = phy_select_page(phydev, page);
501 if (oldpage >= 0)
502 ret = __phy_write(phydev, regnum, val);
503
504 return phy_restore_page(phydev, oldpage, ret);
505}
506EXPORT_SYMBOL(phy_write_paged);
507
508/**
509 * phy_modify_paged() - Convenience function for modifying a paged register
510 * @phydev: a pointer to a &struct phy_device
511 * @page: the page for the phy
512 * @regnum: register number
513 * @mask: bit mask of bits to clear
514 * @set: bit mask of bits to set
515 *
516 * Same rules as for phy_read() and phy_write().
517 */
518int phy_modify_paged(struct phy_device *phydev, int page, u32 regnum,
519 u16 mask, u16 set)
520{
521 int ret = 0, oldpage;
522
523 oldpage = phy_select_page(phydev, page);
524 if (oldpage >= 0)
525 ret = __phy_modify(phydev, regnum, mask, set);
526
527 return phy_restore_page(phydev, oldpage, ret);
528}
529EXPORT_SYMBOL(phy_modify_paged);
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
9const 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}
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
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
74static 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 */
186const struct phy_setting *
187phy_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}
216EXPORT_SYMBOL_GPL(phy_lookup_setting);
217
218size_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
233static 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
248static int __set_phy_supported(struct phy_device *phydev, u32 max_speed)
249{
250 return __set_linkmode_max_speed(max_speed, phydev->supported);
251}
252
253int 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}
265EXPORT_SYMBOL(phy_set_max_speed);
266
267void 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
282void 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
309void 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}
319EXPORT_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 */
329void 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}
345EXPORT_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 */
356void 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}
383EXPORT_SYMBOL_GPL(phy_check_downshift);
384
385static 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
403int 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
413static 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 */
436int __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}
459EXPORT_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 */
470int 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}
480EXPORT_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 */
492int __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}
517EXPORT_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 */
529int 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}
539EXPORT_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 */
554int 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}
564EXPORT_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 */
577int __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}
585EXPORT_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 */
598int 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}
608EXPORT_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 */
623int __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}
640EXPORT_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 */
656int 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}
667EXPORT_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 */
681int __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}
690EXPORT_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 */
704int 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}
715EXPORT_SYMBOL_GPL(phy_modify_mmd);
716
717static 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
725static 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 */
741int phy_save_page(struct phy_device *phydev)
742{
743 phy_lock_mdio_bus(phydev);
744 return __phy_read_page(phydev);
745}
746EXPORT_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 */
759int 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}
775EXPORT_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 */
793int 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}
814EXPORT_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 */
824int 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}
834EXPORT_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 */
845int 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}
855EXPORT_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 */
867int 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}
878EXPORT_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 */
890int 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}
897EXPORT_SYMBOL(phy_modify_paged);