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