xref: /openbmc/linux/drivers/net/phy/phy-core.c (revision 3381df09)
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 != 75,
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 	/* 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		),
87 	/* 100G */
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		),
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		),
97 	/* 56G */
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	  	),
102 	/* 50G */
103 	PHY_SETTING(  50000, FULL,  50000baseCR2_Full		),
104 	PHY_SETTING(  50000, FULL,  50000baseKR2_Full		),
105 	PHY_SETTING(  50000, FULL,  50000baseSR2_Full		),
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		),
111 	/* 40G */
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		),
116 	/* 25G */
117 	PHY_SETTING(  25000, FULL,  25000baseCR_Full		),
118 	PHY_SETTING(  25000, FULL,  25000baseKR_Full		),
119 	PHY_SETTING(  25000, FULL,  25000baseSR_Full		),
120 	/* 20G */
121 	PHY_SETTING(  20000, FULL,  20000baseKR2_Full		),
122 	PHY_SETTING(  20000, FULL,  20000baseMLD2_Full		),
123 	/* 10G */
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		),
133 	/* 5G */
134 	PHY_SETTING(   5000, FULL,   5000baseT_Full		),
135 	/* 2.5G */
136 	PHY_SETTING(   2500, FULL,   2500baseT_Full		),
137 	PHY_SETTING(   2500, FULL,   2500baseX_Full		),
138 	/* 1G */
139 	PHY_SETTING(   1000, FULL,   1000baseKX_Full		),
140 	PHY_SETTING(   1000, FULL,   1000baseT_Full		),
141 	PHY_SETTING(   1000, HALF,   1000baseT_Half		),
142 	PHY_SETTING(   1000, FULL,   1000baseT1_Full		),
143 	PHY_SETTING(   1000, FULL,   1000baseX_Full		),
144 	/* 100M */
145 	PHY_SETTING(    100, FULL,    100baseT_Full		),
146 	PHY_SETTING(    100, FULL,    100baseT1_Full		),
147 	PHY_SETTING(    100, HALF,    100baseT_Half		),
148 	/* 10M */
149 	PHY_SETTING(     10, FULL,     10baseT_Full		),
150 	PHY_SETTING(     10, HALF,     10baseT_Half		),
151 };
152 #undef PHY_SETTING
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
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  */
171 const struct phy_setting *
172 phy_lookup_setting(int speed, int duplex, const unsigned long *mask, bool exact)
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++) {
178 		if (p->bit < __ETHTOOL_LINK_MODE_MASK_NBITS &&
179 		    test_bit(p->bit, mask)) {
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 }
201 EXPORT_SYMBOL_GPL(phy_lookup_setting);
202 
203 size_t phy_speeds(unsigned int *speeds, size_t size,
204 		  unsigned long *mask)
205 {
206 	size_t count;
207 	int i;
208 
209 	for (i = 0, count = 0; i < ARRAY_SIZE(settings) && count < size; i++)
210 		if (settings[i].bit < __ETHTOOL_LINK_MODE_MASK_NBITS &&
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 
218 static int __set_linkmode_max_speed(u32 max_speed, unsigned long *addr)
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)
225 			linkmode_clear_bit(p->bit, addr);
226 		else
227 			break;
228 	}
229 
230 	return 0;
231 }
232 
233 static int __set_phy_supported(struct phy_device *phydev, u32 max_speed)
234 {
235 	return __set_linkmode_max_speed(max_speed, phydev->supported);
236 }
237 
238 int 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 
246 	phy_advertise_supported(phydev);
247 
248 	return 0;
249 }
250 EXPORT_SYMBOL(phy_set_max_speed);
251 
252 void 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 
267 void 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 
294 void 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 }
304 EXPORT_SYMBOL_GPL(phy_resolve_aneg_pause);
305 
306 /**
307  * phy_resolve_aneg_linkmode - resolve the advertisements into phy settings
308  * @phydev: The phy_device struct
309  *
310  * Resolve our and the link partner advertisements into their corresponding
311  * speed and duplex. If full duplex was negotiated, extract the pause mode
312  * from the link partner mask.
313  */
314 void phy_resolve_aneg_linkmode(struct phy_device *phydev)
315 {
316 	__ETHTOOL_DECLARE_LINK_MODE_MASK(common);
317 	int i;
318 
319 	linkmode_and(common, phydev->lp_advertising, phydev->advertising);
320 
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 		}
327 
328 	phy_resolve_aneg_pause(phydev);
329 }
330 EXPORT_SYMBOL_GPL(phy_resolve_aneg_linkmode);
331 
332 /**
333  * phy_check_downshift - check whether downshift occurred
334  * @phydev: The phy_device struct
335  *
336  * Check whether a downshift to a lower speed occurred. If this should be the
337  * case warn the user.
338  * Prerequisite for detecting downshift is that PHY driver implements the
339  * read_status callback and sets phydev->speed to the actual link speed.
340  */
341 void phy_check_downshift(struct phy_device *phydev)
342 {
343 	__ETHTOOL_DECLARE_LINK_MODE_MASK(common);
344 	int i, speed = SPEED_UNKNOWN;
345 
346 	phydev->downshifted_rate = 0;
347 
348 	if (phydev->autoneg == AUTONEG_DISABLE ||
349 	    phydev->speed == SPEED_UNKNOWN)
350 		return;
351 
352 	linkmode_and(common, phydev->lp_advertising, phydev->advertising);
353 
354 	for (i = 0; i < ARRAY_SIZE(settings); i++)
355 		if (test_bit(settings[i].bit, common)) {
356 			speed = settings[i].speed;
357 			break;
358 		}
359 
360 	if (speed == SPEED_UNKNOWN || phydev->speed >= speed)
361 		return;
362 
363 	phydev_warn(phydev, "Downshift occurred from negotiated speed %s to actual speed %s, check cabling!\n",
364 		    phy_speed_to_str(speed), phy_speed_to_str(phydev->speed));
365 
366 	phydev->downshifted_rate = 1;
367 }
368 EXPORT_SYMBOL_GPL(phy_check_downshift);
369 
370 static int phy_resolve_min_speed(struct phy_device *phydev, bool fdx_only)
371 {
372 	__ETHTOOL_DECLARE_LINK_MODE_MASK(common);
373 	int i = ARRAY_SIZE(settings);
374 
375 	linkmode_and(common, phydev->lp_advertising, phydev->advertising);
376 
377 	while (--i >= 0) {
378 		if (test_bit(settings[i].bit, common)) {
379 			if (fdx_only && settings[i].duplex != DUPLEX_FULL)
380 				continue;
381 			return settings[i].speed;
382 		}
383 	}
384 
385 	return SPEED_UNKNOWN;
386 }
387 
388 int phy_speed_down_core(struct phy_device *phydev)
389 {
390 	int min_common_speed = phy_resolve_min_speed(phydev, true);
391 
392 	if (min_common_speed == SPEED_UNKNOWN)
393 		return -EINVAL;
394 
395 	return __set_linkmode_max_speed(min_common_speed, phydev->advertising);
396 }
397 
398 static void mmd_phy_indirect(struct mii_bus *bus, int phy_addr, int devad,
399 			     u16 regnum)
400 {
401 	/* Write the desired MMD Devad */
402 	__mdiobus_write(bus, phy_addr, MII_MMD_CTRL, devad);
403 
404 	/* Write the desired MMD register address */
405 	__mdiobus_write(bus, phy_addr, MII_MMD_DATA, regnum);
406 
407 	/* Select the Function : DATA with no post increment */
408 	__mdiobus_write(bus, phy_addr, MII_MMD_CTRL,
409 			devad | MII_MMD_CTRL_NOINCR);
410 }
411 
412 /**
413  * __phy_read_mmd - Convenience function for reading a register
414  * from an MMD on a given PHY.
415  * @phydev: The phy_device struct
416  * @devad: The MMD to read from (0..31)
417  * @regnum: The register on the MMD to read (0..65535)
418  *
419  * Same rules as for __phy_read();
420  */
421 int __phy_read_mmd(struct phy_device *phydev, int devad, u32 regnum)
422 {
423 	int val;
424 
425 	if (regnum > (u16)~0 || devad > 32)
426 		return -EINVAL;
427 
428 	if (phydev->drv && phydev->drv->read_mmd) {
429 		val = phydev->drv->read_mmd(phydev, devad, regnum);
430 	} else if (phydev->is_c45) {
431 		u32 addr = MII_ADDR_C45 | (devad << 16) | (regnum & 0xffff);
432 
433 		val = __mdiobus_read(phydev->mdio.bus, phydev->mdio.addr, addr);
434 	} else {
435 		struct mii_bus *bus = phydev->mdio.bus;
436 		int phy_addr = phydev->mdio.addr;
437 
438 		mmd_phy_indirect(bus, phy_addr, devad, regnum);
439 
440 		/* Read the content of the MMD's selected register */
441 		val = __mdiobus_read(bus, phy_addr, MII_MMD_DATA);
442 	}
443 	return val;
444 }
445 EXPORT_SYMBOL(__phy_read_mmd);
446 
447 /**
448  * phy_read_mmd - Convenience function for reading a register
449  * from an MMD on a given PHY.
450  * @phydev: The phy_device struct
451  * @devad: The MMD to read from
452  * @regnum: The register on the MMD to read
453  *
454  * Same rules as for phy_read();
455  */
456 int phy_read_mmd(struct phy_device *phydev, int devad, u32 regnum)
457 {
458 	int ret;
459 
460 	phy_lock_mdio_bus(phydev);
461 	ret = __phy_read_mmd(phydev, devad, regnum);
462 	phy_unlock_mdio_bus(phydev);
463 
464 	return ret;
465 }
466 EXPORT_SYMBOL(phy_read_mmd);
467 
468 /**
469  * __phy_write_mmd - Convenience function for writing a register
470  * on an MMD on a given PHY.
471  * @phydev: The phy_device struct
472  * @devad: The MMD to read from
473  * @regnum: The register on the MMD to read
474  * @val: value to write to @regnum
475  *
476  * Same rules as for __phy_write();
477  */
478 int __phy_write_mmd(struct phy_device *phydev, int devad, u32 regnum, u16 val)
479 {
480 	int ret;
481 
482 	if (regnum > (u16)~0 || devad > 32)
483 		return -EINVAL;
484 
485 	if (phydev->drv && phydev->drv->write_mmd) {
486 		ret = phydev->drv->write_mmd(phydev, devad, regnum, val);
487 	} else if (phydev->is_c45) {
488 		u32 addr = MII_ADDR_C45 | (devad << 16) | (regnum & 0xffff);
489 
490 		ret = __mdiobus_write(phydev->mdio.bus, phydev->mdio.addr,
491 				      addr, val);
492 	} else {
493 		struct mii_bus *bus = phydev->mdio.bus;
494 		int phy_addr = phydev->mdio.addr;
495 
496 		mmd_phy_indirect(bus, phy_addr, devad, regnum);
497 
498 		/* Write the data into MMD's selected register */
499 		__mdiobus_write(bus, phy_addr, MII_MMD_DATA, val);
500 
501 		ret = 0;
502 	}
503 	return ret;
504 }
505 EXPORT_SYMBOL(__phy_write_mmd);
506 
507 /**
508  * phy_write_mmd - Convenience function for writing a register
509  * on an MMD on a given PHY.
510  * @phydev: The phy_device struct
511  * @devad: The MMD to read from
512  * @regnum: The register on the MMD to read
513  * @val: value to write to @regnum
514  *
515  * Same rules as for phy_write();
516  */
517 int phy_write_mmd(struct phy_device *phydev, int devad, u32 regnum, u16 val)
518 {
519 	int ret;
520 
521 	phy_lock_mdio_bus(phydev);
522 	ret = __phy_write_mmd(phydev, devad, regnum, val);
523 	phy_unlock_mdio_bus(phydev);
524 
525 	return ret;
526 }
527 EXPORT_SYMBOL(phy_write_mmd);
528 
529 /**
530  * phy_modify_changed - Function for modifying a PHY register
531  * @phydev: the phy_device struct
532  * @regnum: register number to modify
533  * @mask: bit mask of bits to clear
534  * @set: new value of bits set in mask to write to @regnum
535  *
536  * NOTE: MUST NOT be called from interrupt context,
537  * because the bus read/write functions may wait for an interrupt
538  * to conclude the operation.
539  *
540  * Returns negative errno, 0 if there was no change, and 1 in case of change
541  */
542 int phy_modify_changed(struct phy_device *phydev, u32 regnum, u16 mask, u16 set)
543 {
544 	int ret;
545 
546 	phy_lock_mdio_bus(phydev);
547 	ret = __phy_modify_changed(phydev, regnum, mask, set);
548 	phy_unlock_mdio_bus(phydev);
549 
550 	return ret;
551 }
552 EXPORT_SYMBOL_GPL(phy_modify_changed);
553 
554 /**
555  * __phy_modify - Convenience function for modifying a PHY register
556  * @phydev: the phy_device struct
557  * @regnum: register number to modify
558  * @mask: bit mask of bits to clear
559  * @set: new value of bits set in mask to write to @regnum
560  *
561  * NOTE: MUST NOT be called from interrupt context,
562  * because the bus read/write functions may wait for an interrupt
563  * to conclude the operation.
564  */
565 int __phy_modify(struct phy_device *phydev, u32 regnum, u16 mask, u16 set)
566 {
567 	int ret;
568 
569 	ret = __phy_modify_changed(phydev, regnum, mask, set);
570 
571 	return ret < 0 ? ret : 0;
572 }
573 EXPORT_SYMBOL_GPL(__phy_modify);
574 
575 /**
576  * phy_modify - Convenience function for modifying a given PHY register
577  * @phydev: the phy_device struct
578  * @regnum: register number to write
579  * @mask: bit mask of bits to clear
580  * @set: new value of bits set in mask to write to @regnum
581  *
582  * NOTE: MUST NOT be called from interrupt context,
583  * because the bus read/write functions may wait for an interrupt
584  * to conclude the operation.
585  */
586 int phy_modify(struct phy_device *phydev, u32 regnum, u16 mask, u16 set)
587 {
588 	int ret;
589 
590 	phy_lock_mdio_bus(phydev);
591 	ret = __phy_modify(phydev, regnum, mask, set);
592 	phy_unlock_mdio_bus(phydev);
593 
594 	return ret;
595 }
596 EXPORT_SYMBOL_GPL(phy_modify);
597 
598 /**
599  * __phy_modify_mmd_changed - Function for modifying a register on MMD
600  * @phydev: the phy_device struct
601  * @devad: the MMD containing register to modify
602  * @regnum: register number to modify
603  * @mask: bit mask of bits to clear
604  * @set: new value of bits set in mask to write to @regnum
605  *
606  * Unlocked helper function which allows a MMD register to be modified as
607  * new register value = (old register value & ~mask) | set
608  *
609  * Returns negative errno, 0 if there was no change, and 1 in case of change
610  */
611 int __phy_modify_mmd_changed(struct phy_device *phydev, int devad, u32 regnum,
612 			     u16 mask, u16 set)
613 {
614 	int new, ret;
615 
616 	ret = __phy_read_mmd(phydev, devad, regnum);
617 	if (ret < 0)
618 		return ret;
619 
620 	new = (ret & ~mask) | set;
621 	if (new == ret)
622 		return 0;
623 
624 	ret = __phy_write_mmd(phydev, devad, regnum, new);
625 
626 	return ret < 0 ? ret : 1;
627 }
628 EXPORT_SYMBOL_GPL(__phy_modify_mmd_changed);
629 
630 /**
631  * phy_modify_mmd_changed - Function for modifying a register on MMD
632  * @phydev: the phy_device struct
633  * @devad: the MMD containing register to modify
634  * @regnum: register number to modify
635  * @mask: bit mask of bits to clear
636  * @set: new value of bits set in mask to write to @regnum
637  *
638  * NOTE: MUST NOT be called from interrupt context,
639  * because the bus read/write functions may wait for an interrupt
640  * to conclude the operation.
641  *
642  * Returns negative errno, 0 if there was no change, and 1 in case of change
643  */
644 int phy_modify_mmd_changed(struct phy_device *phydev, int devad, u32 regnum,
645 			   u16 mask, u16 set)
646 {
647 	int ret;
648 
649 	phy_lock_mdio_bus(phydev);
650 	ret = __phy_modify_mmd_changed(phydev, devad, regnum, mask, set);
651 	phy_unlock_mdio_bus(phydev);
652 
653 	return ret;
654 }
655 EXPORT_SYMBOL_GPL(phy_modify_mmd_changed);
656 
657 /**
658  * __phy_modify_mmd - Convenience function for modifying a register on MMD
659  * @phydev: the phy_device struct
660  * @devad: the MMD containing register to modify
661  * @regnum: register number to modify
662  * @mask: bit mask of bits to clear
663  * @set: new value of bits set in mask to write to @regnum
664  *
665  * NOTE: MUST NOT be called from interrupt context,
666  * because the bus read/write functions may wait for an interrupt
667  * to conclude the operation.
668  */
669 int __phy_modify_mmd(struct phy_device *phydev, int devad, u32 regnum,
670 		     u16 mask, u16 set)
671 {
672 	int ret;
673 
674 	ret = __phy_modify_mmd_changed(phydev, devad, regnum, mask, set);
675 
676 	return ret < 0 ? ret : 0;
677 }
678 EXPORT_SYMBOL_GPL(__phy_modify_mmd);
679 
680 /**
681  * phy_modify_mmd - Convenience function for modifying a register on MMD
682  * @phydev: the phy_device struct
683  * @devad: the MMD containing register to modify
684  * @regnum: register number to modify
685  * @mask: bit mask of bits to clear
686  * @set: new value of bits set in mask to write to @regnum
687  *
688  * NOTE: MUST NOT be called from interrupt context,
689  * because the bus read/write functions may wait for an interrupt
690  * to conclude the operation.
691  */
692 int phy_modify_mmd(struct phy_device *phydev, int devad, u32 regnum,
693 		   u16 mask, u16 set)
694 {
695 	int ret;
696 
697 	phy_lock_mdio_bus(phydev);
698 	ret = __phy_modify_mmd(phydev, devad, regnum, mask, set);
699 	phy_unlock_mdio_bus(phydev);
700 
701 	return ret;
702 }
703 EXPORT_SYMBOL_GPL(phy_modify_mmd);
704 
705 static int __phy_read_page(struct phy_device *phydev)
706 {
707 	if (WARN_ONCE(!phydev->drv->read_page, "read_page callback not available, PHY driver not loaded?\n"))
708 		return -EOPNOTSUPP;
709 
710 	return phydev->drv->read_page(phydev);
711 }
712 
713 static int __phy_write_page(struct phy_device *phydev, int page)
714 {
715 	if (WARN_ONCE(!phydev->drv->write_page, "write_page callback not available, PHY driver not loaded?\n"))
716 		return -EOPNOTSUPP;
717 
718 	return phydev->drv->write_page(phydev, page);
719 }
720 
721 /**
722  * phy_save_page() - take the bus lock and save the current page
723  * @phydev: a pointer to a &struct phy_device
724  *
725  * Take the MDIO bus lock, and return the current page number. On error,
726  * returns a negative errno. phy_restore_page() must always be called
727  * after this, irrespective of success or failure of this call.
728  */
729 int phy_save_page(struct phy_device *phydev)
730 {
731 	phy_lock_mdio_bus(phydev);
732 	return __phy_read_page(phydev);
733 }
734 EXPORT_SYMBOL_GPL(phy_save_page);
735 
736 /**
737  * phy_select_page() - take the bus lock, save the current page, and set a page
738  * @phydev: a pointer to a &struct phy_device
739  * @page: desired page
740  *
741  * Take the MDIO bus lock to protect against concurrent access, save the
742  * current PHY page, and set the current page.  On error, returns a
743  * negative errno, otherwise returns the previous page number.
744  * phy_restore_page() must always be called after this, irrespective
745  * of success or failure of this call.
746  */
747 int phy_select_page(struct phy_device *phydev, int page)
748 {
749 	int ret, oldpage;
750 
751 	oldpage = ret = phy_save_page(phydev);
752 	if (ret < 0)
753 		return ret;
754 
755 	if (oldpage != page) {
756 		ret = __phy_write_page(phydev, page);
757 		if (ret < 0)
758 			return ret;
759 	}
760 
761 	return oldpage;
762 }
763 EXPORT_SYMBOL_GPL(phy_select_page);
764 
765 /**
766  * phy_restore_page() - restore the page register and release the bus lock
767  * @phydev: a pointer to a &struct phy_device
768  * @oldpage: the old page, return value from phy_save_page() or phy_select_page()
769  * @ret: operation's return code
770  *
771  * Release the MDIO bus lock, restoring @oldpage if it is a valid page.
772  * This function propagates the earliest error code from the group of
773  * operations.
774  *
775  * Returns:
776  *   @oldpage if it was a negative value, otherwise
777  *   @ret if it was a negative errno value, otherwise
778  *   phy_write_page()'s negative value if it were in error, otherwise
779  *   @ret.
780  */
781 int phy_restore_page(struct phy_device *phydev, int oldpage, int ret)
782 {
783 	int r;
784 
785 	if (oldpage >= 0) {
786 		r = __phy_write_page(phydev, oldpage);
787 
788 		/* Propagate the operation return code if the page write
789 		 * was successful.
790 		 */
791 		if (ret >= 0 && r < 0)
792 			ret = r;
793 	} else {
794 		/* Propagate the phy page selection error code */
795 		ret = oldpage;
796 	}
797 
798 	phy_unlock_mdio_bus(phydev);
799 
800 	return ret;
801 }
802 EXPORT_SYMBOL_GPL(phy_restore_page);
803 
804 /**
805  * phy_read_paged() - Convenience function for reading a paged register
806  * @phydev: a pointer to a &struct phy_device
807  * @page: the page for the phy
808  * @regnum: register number
809  *
810  * Same rules as for phy_read().
811  */
812 int phy_read_paged(struct phy_device *phydev, int page, u32 regnum)
813 {
814 	int ret = 0, oldpage;
815 
816 	oldpage = phy_select_page(phydev, page);
817 	if (oldpage >= 0)
818 		ret = __phy_read(phydev, regnum);
819 
820 	return phy_restore_page(phydev, oldpage, ret);
821 }
822 EXPORT_SYMBOL(phy_read_paged);
823 
824 /**
825  * phy_write_paged() - Convenience function for writing a paged register
826  * @phydev: a pointer to a &struct phy_device
827  * @page: the page for the phy
828  * @regnum: register number
829  * @val: value to write
830  *
831  * Same rules as for phy_write().
832  */
833 int phy_write_paged(struct phy_device *phydev, int page, u32 regnum, u16 val)
834 {
835 	int ret = 0, oldpage;
836 
837 	oldpage = phy_select_page(phydev, page);
838 	if (oldpage >= 0)
839 		ret = __phy_write(phydev, regnum, val);
840 
841 	return phy_restore_page(phydev, oldpage, ret);
842 }
843 EXPORT_SYMBOL(phy_write_paged);
844 
845 /**
846  * phy_modify_paged_changed() - Function for modifying a paged register
847  * @phydev: a pointer to a &struct phy_device
848  * @page: the page for the phy
849  * @regnum: register number
850  * @mask: bit mask of bits to clear
851  * @set: bit mask of bits to set
852  *
853  * Returns negative errno, 0 if there was no change, and 1 in case of change
854  */
855 int phy_modify_paged_changed(struct phy_device *phydev, int page, u32 regnum,
856 			     u16 mask, u16 set)
857 {
858 	int ret = 0, oldpage;
859 
860 	oldpage = phy_select_page(phydev, page);
861 	if (oldpage >= 0)
862 		ret = __phy_modify_changed(phydev, regnum, mask, set);
863 
864 	return phy_restore_page(phydev, oldpage, ret);
865 }
866 EXPORT_SYMBOL(phy_modify_paged_changed);
867 
868 /**
869  * phy_modify_paged() - Convenience function for modifying a paged register
870  * @phydev: a pointer to a &struct phy_device
871  * @page: the page for the phy
872  * @regnum: register number
873  * @mask: bit mask of bits to clear
874  * @set: bit mask of bits to set
875  *
876  * Same rules as for phy_read() and phy_write().
877  */
878 int phy_modify_paged(struct phy_device *phydev, int page, u32 regnum,
879 		     u16 mask, u16 set)
880 {
881 	int ret = phy_modify_paged_changed(phydev, page, regnum, mask, set);
882 
883 	return ret < 0 ? ret : 0;
884 }
885 EXPORT_SYMBOL(phy_modify_paged);
886