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