xref: /linux/drivers/net/phy/phy-core.c (revision c27022497dd9b8a8922dbb878c255e4260a90e6c)
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/phy_port.h>
8 #include <linux/of.h>
9 
10 #include "phylib.h"
11 #include "phylib-internal.h"
12 #include "phy-caps.h"
13 
14 /**
15  * phy_speed_to_str - Return a string representing the PHY link speed
16  *
17  * @speed: Speed of the link
18  */
19 const char *phy_speed_to_str(int speed)
20 {
21 	BUILD_BUG_ON_MSG(__ETHTOOL_LINK_MODE_MASK_NBITS != 125,
22 		"Enum ethtool_link_mode_bit_indices and phylib are out of sync. "
23 		"If a speed or mode has been added please update phy_speed_to_str "
24 		"and the PHY settings array.\n");
25 
26 	switch (speed) {
27 	case SPEED_10:
28 		return "10Mbps";
29 	case SPEED_100:
30 		return "100Mbps";
31 	case SPEED_1000:
32 		return "1Gbps";
33 	case SPEED_2500:
34 		return "2.5Gbps";
35 	case SPEED_5000:
36 		return "5Gbps";
37 	case SPEED_10000:
38 		return "10Gbps";
39 	case SPEED_14000:
40 		return "14Gbps";
41 	case SPEED_20000:
42 		return "20Gbps";
43 	case SPEED_25000:
44 		return "25Gbps";
45 	case SPEED_40000:
46 		return "40Gbps";
47 	case SPEED_50000:
48 		return "50Gbps";
49 	case SPEED_56000:
50 		return "56Gbps";
51 	case SPEED_100000:
52 		return "100Gbps";
53 	case SPEED_200000:
54 		return "200Gbps";
55 	case SPEED_400000:
56 		return "400Gbps";
57 	case SPEED_800000:
58 		return "800Gbps";
59 	case SPEED_1600000:
60 		return "1600Gbps";
61 	case SPEED_UNKNOWN:
62 		return "Unknown";
63 	default:
64 		return "Unsupported (update phy-core.c)";
65 	}
66 }
67 EXPORT_SYMBOL_GPL(phy_speed_to_str);
68 
69 /**
70  * phy_duplex_to_str - Return string describing the duplex
71  *
72  * @duplex: Duplex setting to describe
73  */
74 const char *phy_duplex_to_str(unsigned int duplex)
75 {
76 	if (duplex == DUPLEX_HALF)
77 		return "Half";
78 	if (duplex == DUPLEX_FULL)
79 		return "Full";
80 	if (duplex == DUPLEX_UNKNOWN)
81 		return "Unknown";
82 	return "Unsupported (update phy-core.c)";
83 }
84 EXPORT_SYMBOL_GPL(phy_duplex_to_str);
85 
86 /**
87  * phy_rate_matching_to_str - Return a string describing the rate matching
88  *
89  * @rate_matching: Type of rate matching to describe
90  */
91 const char *phy_rate_matching_to_str(int rate_matching)
92 {
93 	switch (rate_matching) {
94 	case RATE_MATCH_NONE:
95 		return "none";
96 	case RATE_MATCH_PAUSE:
97 		return "pause";
98 	case RATE_MATCH_CRS:
99 		return "crs";
100 	case RATE_MATCH_OPEN_LOOP:
101 		return "open-loop";
102 	}
103 	return "Unsupported (update phy-core.c)";
104 }
105 EXPORT_SYMBOL_GPL(phy_rate_matching_to_str);
106 
107 /**
108  * phy_fix_phy_mode_for_mac_delays - Convenience function for fixing PHY
109  * mode based on whether mac adds internal delay
110  *
111  * @interface: The current interface mode of the port
112  * @mac_txid: True if the mac adds internal tx delay
113  * @mac_rxid: True if the mac adds internal rx delay
114  *
115  * Return: fixed PHY mode, or PHY_INTERFACE_MODE_NA if the interface can
116  * not apply the internal delay
117  */
118 phy_interface_t phy_fix_phy_mode_for_mac_delays(phy_interface_t interface,
119 						bool mac_txid, bool mac_rxid)
120 {
121 	if (!phy_interface_mode_is_rgmii(interface))
122 		return interface;
123 
124 	if (mac_txid && mac_rxid) {
125 		if (interface == PHY_INTERFACE_MODE_RGMII_ID)
126 			return PHY_INTERFACE_MODE_RGMII;
127 		return PHY_INTERFACE_MODE_NA;
128 	}
129 
130 	if (mac_txid) {
131 		if (interface == PHY_INTERFACE_MODE_RGMII_ID)
132 			return PHY_INTERFACE_MODE_RGMII_RXID;
133 		if (interface == PHY_INTERFACE_MODE_RGMII_TXID)
134 			return PHY_INTERFACE_MODE_RGMII;
135 		return PHY_INTERFACE_MODE_NA;
136 	}
137 
138 	if (mac_rxid) {
139 		if (interface == PHY_INTERFACE_MODE_RGMII_ID)
140 			return PHY_INTERFACE_MODE_RGMII_TXID;
141 		if (interface == PHY_INTERFACE_MODE_RGMII_RXID)
142 			return PHY_INTERFACE_MODE_RGMII;
143 		return PHY_INTERFACE_MODE_NA;
144 	}
145 
146 	return interface;
147 }
148 EXPORT_SYMBOL_GPL(phy_fix_phy_mode_for_mac_delays);
149 
150 /**
151  * phy_interface_num_ports - Return the number of links that can be carried by
152  *			     a given MAC-PHY physical link. Returns 0 if this is
153  *			     unknown, the number of links else.
154  *
155  * @interface: The interface mode we want to get the number of ports
156  */
157 int phy_interface_num_ports(phy_interface_t interface)
158 {
159 	switch (interface) {
160 	case PHY_INTERFACE_MODE_NA:
161 		return 0;
162 	case PHY_INTERFACE_MODE_INTERNAL:
163 	case PHY_INTERFACE_MODE_MII:
164 	case PHY_INTERFACE_MODE_MIILITE:
165 	case PHY_INTERFACE_MODE_GMII:
166 	case PHY_INTERFACE_MODE_TBI:
167 	case PHY_INTERFACE_MODE_REVMII:
168 	case PHY_INTERFACE_MODE_RMII:
169 	case PHY_INTERFACE_MODE_REVRMII:
170 	case PHY_INTERFACE_MODE_RGMII:
171 	case PHY_INTERFACE_MODE_RGMII_ID:
172 	case PHY_INTERFACE_MODE_RGMII_RXID:
173 	case PHY_INTERFACE_MODE_RGMII_TXID:
174 	case PHY_INTERFACE_MODE_RTBI:
175 	case PHY_INTERFACE_MODE_XGMII:
176 	case PHY_INTERFACE_MODE_XLGMII:
177 	case PHY_INTERFACE_MODE_MOCA:
178 	case PHY_INTERFACE_MODE_TRGMII:
179 	case PHY_INTERFACE_MODE_USXGMII:
180 	case PHY_INTERFACE_MODE_SGMII:
181 	case PHY_INTERFACE_MODE_SMII:
182 	case PHY_INTERFACE_MODE_1000BASEX:
183 	case PHY_INTERFACE_MODE_2500BASEX:
184 	case PHY_INTERFACE_MODE_5GBASER:
185 	case PHY_INTERFACE_MODE_10GBASER:
186 	case PHY_INTERFACE_MODE_25GBASER:
187 	case PHY_INTERFACE_MODE_10GKR:
188 	case PHY_INTERFACE_MODE_100BASEX:
189 	case PHY_INTERFACE_MODE_RXAUI:
190 	case PHY_INTERFACE_MODE_XAUI:
191 	case PHY_INTERFACE_MODE_1000BASEKX:
192 	case PHY_INTERFACE_MODE_50GBASER:
193 	case PHY_INTERFACE_MODE_LAUI:
194 	case PHY_INTERFACE_MODE_100GBASEP:
195 		return 1;
196 	case PHY_INTERFACE_MODE_QSGMII:
197 	case PHY_INTERFACE_MODE_QUSGMII:
198 	case PHY_INTERFACE_MODE_10G_QXGMII:
199 		return 4;
200 	case PHY_INTERFACE_MODE_PSGMII:
201 		return 5;
202 	case PHY_INTERFACE_MODE_MAX:
203 		WARN_ONCE(1, "PHY_INTERFACE_MODE_MAX isn't a valid interface mode");
204 		return 0;
205 	}
206 	return 0;
207 }
208 EXPORT_SYMBOL_GPL(phy_interface_num_ports);
209 
210 static void __set_phy_supported(struct phy_device *phydev, u32 max_speed)
211 {
212 	struct phy_port *port;
213 
214 	phy_caps_linkmode_max_speed(max_speed, phydev->supported);
215 
216 	phy_for_each_port(phydev, port)
217 		phy_caps_linkmode_max_speed(max_speed, port->supported);
218 }
219 
220 /**
221  * phy_set_max_speed - Set the maximum speed the PHY should support
222  *
223  * @phydev: The phy_device struct
224  * @max_speed: Maximum speed
225  *
226  * The PHY might be more capable than the MAC. For example a Fast Ethernet
227  * is connected to a 1G PHY. This function allows the MAC to indicate its
228  * maximum speed, and so limit what the PHY will advertise.
229  */
230 void phy_set_max_speed(struct phy_device *phydev, u32 max_speed)
231 {
232 	__set_phy_supported(phydev, max_speed);
233 
234 	phy_advertise_supported(phydev);
235 }
236 EXPORT_SYMBOL(phy_set_max_speed);
237 
238 void of_set_phy_supported(struct phy_device *phydev)
239 {
240 	struct device_node *node = phydev->mdio.dev.of_node;
241 	u32 max_speed;
242 
243 	if (!IS_ENABLED(CONFIG_OF_MDIO))
244 		return;
245 
246 	if (!node)
247 		return;
248 
249 	if (!of_property_read_u32(node, "max-speed", &max_speed))
250 		__set_phy_supported(phydev, max_speed);
251 }
252 
253 void of_set_phy_eee_broken(struct phy_device *phydev)
254 {
255 	struct device_node *node = phydev->mdio.dev.of_node;
256 	unsigned long *modes = phydev->eee_disabled_modes;
257 
258 	if (!IS_ENABLED(CONFIG_OF_MDIO) || !node)
259 		return;
260 
261 	linkmode_zero(modes);
262 
263 	if (of_property_read_bool(node, "eee-broken-100tx"))
264 		linkmode_set_bit(ETHTOOL_LINK_MODE_100baseT_Full_BIT, modes);
265 	if (of_property_read_bool(node, "eee-broken-1000t"))
266 		linkmode_set_bit(ETHTOOL_LINK_MODE_1000baseT_Full_BIT, modes);
267 	if (of_property_read_bool(node, "eee-broken-10gt"))
268 		linkmode_set_bit(ETHTOOL_LINK_MODE_10000baseT_Full_BIT, modes);
269 	if (of_property_read_bool(node, "eee-broken-1000kx"))
270 		linkmode_set_bit(ETHTOOL_LINK_MODE_1000baseKX_Full_BIT, modes);
271 	if (of_property_read_bool(node, "eee-broken-10gkx4"))
272 		linkmode_set_bit(ETHTOOL_LINK_MODE_10000baseKX4_Full_BIT, modes);
273 	if (of_property_read_bool(node, "eee-broken-10gkr"))
274 		linkmode_set_bit(ETHTOOL_LINK_MODE_10000baseKR_Full_BIT, modes);
275 }
276 
277 /**
278  * of_set_phy_timing_role - Set the master/slave mode of the PHY
279  *
280  * @phydev: The phy_device struct
281  *
282  * Set master/slave configuration of the PHY based on the device tree.
283  */
284 void of_set_phy_timing_role(struct phy_device *phydev)
285 {
286 	struct device_node *node = phydev->mdio.dev.of_node;
287 	const char *master;
288 
289 	if (!IS_ENABLED(CONFIG_OF_MDIO))
290 		return;
291 
292 	if (!node)
293 		return;
294 
295 	if (of_property_read_string(node, "timing-role", &master))
296 		return;
297 
298 	if (strcmp(master, "forced-master") == 0)
299 		phydev->master_slave_set = MASTER_SLAVE_CFG_MASTER_FORCE;
300 	else if (strcmp(master, "forced-slave") == 0)
301 		phydev->master_slave_set = MASTER_SLAVE_CFG_SLAVE_FORCE;
302 	else if (strcmp(master, "preferred-master") == 0)
303 		phydev->master_slave_set = MASTER_SLAVE_CFG_MASTER_PREFERRED;
304 	else if (strcmp(master, "preferred-slave") == 0)
305 		phydev->master_slave_set = MASTER_SLAVE_CFG_SLAVE_PREFERRED;
306 	else
307 		phydev_warn(phydev, "Unknown master-slave mode %s\n", master);
308 }
309 
310 /**
311  * phy_resolve_aneg_pause - Determine pause autoneg results
312  *
313  * @phydev: The phy_device struct
314  *
315  * Once autoneg has completed the local pause settings can be
316  * resolved.  Determine if pause and asymmetric pause should be used
317  * by the MAC.
318  */
319 
320 void phy_resolve_aneg_pause(struct phy_device *phydev)
321 {
322 	if (phydev->duplex == DUPLEX_FULL) {
323 		phydev->pause = linkmode_test_bit(ETHTOOL_LINK_MODE_Pause_BIT,
324 						  phydev->lp_advertising);
325 		phydev->asym_pause = linkmode_test_bit(
326 			ETHTOOL_LINK_MODE_Asym_Pause_BIT,
327 			phydev->lp_advertising);
328 	}
329 }
330 EXPORT_SYMBOL_GPL(phy_resolve_aneg_pause);
331 
332 /**
333  * phy_resolve_aneg_linkmode - resolve the advertisements into PHY settings
334  * @phydev: The phy_device struct
335  *
336  * Resolve our and the link partner advertisements into their corresponding
337  * speed and duplex. If full duplex was negotiated, extract the pause mode
338  * from the link partner mask.
339  */
340 void phy_resolve_aneg_linkmode(struct phy_device *phydev)
341 {
342 	__ETHTOOL_DECLARE_LINK_MODE_MASK(common);
343 	const struct link_capabilities *c;
344 
345 	linkmode_and(common, phydev->lp_advertising, phydev->advertising);
346 
347 	c = phy_caps_lookup_by_linkmode(common);
348 	if (c) {
349 		phydev->speed = c->speed;
350 		phydev->duplex = c->duplex;
351 	}
352 
353 	phy_resolve_aneg_pause(phydev);
354 }
355 EXPORT_SYMBOL_GPL(phy_resolve_aneg_linkmode);
356 
357 /**
358  * phy_check_downshift - check whether downshift occurred
359  * @phydev: The phy_device struct
360  *
361  * Check whether a downshift to a lower speed occurred. If this should be the
362  * case warn the user.
363  * Prerequisite for detecting downshift is that PHY driver implements the
364  * read_status callback and sets phydev->speed to the actual link speed.
365  */
366 void phy_check_downshift(struct phy_device *phydev)
367 {
368 	__ETHTOOL_DECLARE_LINK_MODE_MASK(common);
369 	const struct link_capabilities *c;
370 	int speed = SPEED_UNKNOWN;
371 
372 	phydev->downshifted_rate = 0;
373 
374 	if (phydev->autoneg == AUTONEG_DISABLE ||
375 	    phydev->speed == SPEED_UNKNOWN)
376 		return;
377 
378 	linkmode_and(common, phydev->lp_advertising, phydev->advertising);
379 
380 	c = phy_caps_lookup_by_linkmode(common);
381 	if (c)
382 		speed = c->speed;
383 
384 	if (speed == SPEED_UNKNOWN || phydev->speed >= speed)
385 		return;
386 
387 	phydev_warn(phydev, "Downshift occurred from negotiated speed %s to actual speed %s, check cabling!\n",
388 		    phy_speed_to_str(speed), phy_speed_to_str(phydev->speed));
389 
390 	phydev->downshifted_rate = 1;
391 }
392 
393 static int phy_resolve_min_speed(struct phy_device *phydev, bool fdx_only)
394 {
395 	__ETHTOOL_DECLARE_LINK_MODE_MASK(common);
396 	const struct link_capabilities *c;
397 
398 	linkmode_and(common, phydev->lp_advertising, phydev->advertising);
399 
400 	c = phy_caps_lookup_by_linkmode_rev(common, fdx_only);
401 	if (c)
402 		return c->speed;
403 
404 	return SPEED_UNKNOWN;
405 }
406 
407 int phy_speed_down_core(struct phy_device *phydev)
408 {
409 	int min_common_speed = phy_resolve_min_speed(phydev, true);
410 
411 	if (min_common_speed == SPEED_UNKNOWN)
412 		return -EINVAL;
413 
414 	phy_caps_linkmode_max_speed(min_common_speed, phydev->advertising);
415 
416 	return 0;
417 }
418 
419 static void mmd_phy_indirect(struct mii_bus *bus, int phy_addr, int devad,
420 			     u16 regnum)
421 {
422 	/* Write the desired MMD Devad */
423 	__mdiobus_write(bus, phy_addr, MII_MMD_CTRL, devad);
424 
425 	/* Write the desired MMD register address */
426 	__mdiobus_write(bus, phy_addr, MII_MMD_DATA, regnum);
427 
428 	/* Select the Function : DATA with no post increment */
429 	__mdiobus_write(bus, phy_addr, MII_MMD_CTRL,
430 			devad | MII_MMD_CTRL_NOINCR);
431 }
432 
433 int mmd_phy_read(struct mii_bus *bus, int phy_addr, bool is_c45,
434 		 int devad, u32 regnum)
435 {
436 	if (is_c45)
437 		return __mdiobus_c45_read(bus, phy_addr, devad, regnum);
438 
439 	mmd_phy_indirect(bus, phy_addr, devad, regnum);
440 	/* Read the content of the MMD's selected register */
441 	return __mdiobus_read(bus, phy_addr, MII_MMD_DATA);
442 }
443 EXPORT_SYMBOL_GPL(mmd_phy_read);
444 
445 int mmd_phy_write(struct mii_bus *bus, int phy_addr, bool is_c45,
446 		  int devad, u32 regnum, u16 val)
447 {
448 	if (is_c45)
449 		return __mdiobus_c45_write(bus, phy_addr, devad, regnum, val);
450 
451 	mmd_phy_indirect(bus, phy_addr, devad, regnum);
452 	/* Write the data into MMD's selected register */
453 	return __mdiobus_write(bus, phy_addr, MII_MMD_DATA, val);
454 }
455 EXPORT_SYMBOL_GPL(mmd_phy_write);
456 
457 /**
458  * __phy_read_mmd - Convenience function for reading a register
459  * from an MMD on a given PHY.
460  * @phydev: The phy_device struct
461  * @devad: The MMD to read from (0..31)
462  * @regnum: The register on the MMD to read (0..65535)
463  *
464  * Same rules as for __phy_read();
465  */
466 int __phy_read_mmd(struct phy_device *phydev, int devad, u32 regnum)
467 {
468 	if (regnum > (u16)~0 || devad > 32)
469 		return -EINVAL;
470 
471 	if (phydev->drv && phydev->drv->read_mmd)
472 		return phydev->drv->read_mmd(phydev, devad, regnum);
473 
474 	return mmd_phy_read(phydev->mdio.bus, phydev->mdio.addr,
475 			    phydev->is_c45, devad, regnum);
476 }
477 EXPORT_SYMBOL(__phy_read_mmd);
478 
479 /**
480  * phy_read_mmd - Convenience function for reading a register
481  * from an MMD on a given PHY.
482  * @phydev: The phy_device struct
483  * @devad: The MMD to read from
484  * @regnum: The register on the MMD to read
485  *
486  * Same rules as for phy_read();
487  */
488 int phy_read_mmd(struct phy_device *phydev, int devad, u32 regnum)
489 {
490 	int ret;
491 
492 	phy_lock_mdio_bus(phydev);
493 	ret = __phy_read_mmd(phydev, devad, regnum);
494 	phy_unlock_mdio_bus(phydev);
495 
496 	return ret;
497 }
498 EXPORT_SYMBOL(phy_read_mmd);
499 
500 /**
501  * __phy_write_mmd - Convenience function for writing a register
502  * on an MMD on a given PHY.
503  * @phydev: The phy_device struct
504  * @devad: The MMD to read from
505  * @regnum: The register on the MMD to read
506  * @val: value to write to @regnum
507  *
508  * Same rules as for __phy_write();
509  */
510 int __phy_write_mmd(struct phy_device *phydev, int devad, u32 regnum, u16 val)
511 {
512 	if (regnum > (u16)~0 || devad > 32)
513 		return -EINVAL;
514 
515 	if (phydev->drv && phydev->drv->write_mmd)
516 		return phydev->drv->write_mmd(phydev, devad, regnum, val);
517 
518 	return mmd_phy_write(phydev->mdio.bus, phydev->mdio.addr,
519 			     phydev->is_c45, devad, regnum, val);
520 }
521 EXPORT_SYMBOL(__phy_write_mmd);
522 
523 /**
524  * phy_write_mmd - Convenience function for writing a register
525  * on an MMD on a given PHY.
526  * @phydev: The phy_device struct
527  * @devad: The MMD to read from
528  * @regnum: The register on the MMD to read
529  * @val: value to write to @regnum
530  *
531  * Same rules as for phy_write();
532  */
533 int phy_write_mmd(struct phy_device *phydev, int devad, u32 regnum, u16 val)
534 {
535 	int ret;
536 
537 	phy_lock_mdio_bus(phydev);
538 	ret = __phy_write_mmd(phydev, devad, regnum, val);
539 	phy_unlock_mdio_bus(phydev);
540 
541 	return ret;
542 }
543 EXPORT_SYMBOL(phy_write_mmd);
544 
545 /**
546  * phy_modify_changed - Function for modifying a PHY register
547  * @phydev: the phy_device struct
548  * @regnum: register number to modify
549  * @mask: bit mask of bits to clear
550  * @set: new value of bits set in mask to write to @regnum
551  *
552  * NOTE: MUST NOT be called from interrupt context,
553  * because the bus read/write functions may wait for an interrupt
554  * to conclude the operation.
555  *
556  * Returns negative errno, 0 if there was no change, and 1 in case of change
557  */
558 int phy_modify_changed(struct phy_device *phydev, u32 regnum, u16 mask, u16 set)
559 {
560 	int ret;
561 
562 	phy_lock_mdio_bus(phydev);
563 	ret = __phy_modify_changed(phydev, regnum, mask, set);
564 	phy_unlock_mdio_bus(phydev);
565 
566 	return ret;
567 }
568 EXPORT_SYMBOL_GPL(phy_modify_changed);
569 
570 /**
571  * __phy_modify - Convenience function for modifying a PHY register
572  * @phydev: the phy_device struct
573  * @regnum: register number to modify
574  * @mask: bit mask of bits to clear
575  * @set: new value of bits set in mask to write to @regnum
576  *
577  * NOTE: MUST NOT be called from interrupt context,
578  * because the bus read/write functions may wait for an interrupt
579  * to conclude the operation.
580  */
581 int __phy_modify(struct phy_device *phydev, u32 regnum, u16 mask, u16 set)
582 {
583 	int ret;
584 
585 	ret = __phy_modify_changed(phydev, regnum, mask, set);
586 
587 	return ret < 0 ? ret : 0;
588 }
589 EXPORT_SYMBOL_GPL(__phy_modify);
590 
591 /**
592  * phy_modify - Convenience function for modifying a given PHY register
593  * @phydev: the phy_device struct
594  * @regnum: register number to write
595  * @mask: bit mask of bits to clear
596  * @set: new value of bits set in mask to write to @regnum
597  *
598  * NOTE: MUST NOT be called from interrupt context,
599  * because the bus read/write functions may wait for an interrupt
600  * to conclude the operation.
601  */
602 int phy_modify(struct phy_device *phydev, u32 regnum, u16 mask, u16 set)
603 {
604 	int ret;
605 
606 	phy_lock_mdio_bus(phydev);
607 	ret = __phy_modify(phydev, regnum, mask, set);
608 	phy_unlock_mdio_bus(phydev);
609 
610 	return ret;
611 }
612 EXPORT_SYMBOL_GPL(phy_modify);
613 
614 /**
615  * __phy_modify_mmd_changed - Function for modifying a register on MMD
616  * @phydev: the phy_device struct
617  * @devad: the MMD containing register to modify
618  * @regnum: register number to modify
619  * @mask: bit mask of bits to clear
620  * @set: new value of bits set in mask to write to @regnum
621  *
622  * Unlocked helper function which allows a MMD register to be modified as
623  * new register value = (old register value & ~mask) | set
624  *
625  * Returns negative errno, 0 if there was no change, and 1 in case of change
626  */
627 int __phy_modify_mmd_changed(struct phy_device *phydev, int devad, u32 regnum,
628 			     u16 mask, u16 set)
629 {
630 	int new, ret;
631 
632 	ret = __phy_read_mmd(phydev, devad, regnum);
633 	if (ret < 0)
634 		return ret;
635 
636 	new = (ret & ~mask) | set;
637 	if (new == ret)
638 		return 0;
639 
640 	ret = __phy_write_mmd(phydev, devad, regnum, new);
641 
642 	return ret < 0 ? ret : 1;
643 }
644 EXPORT_SYMBOL_GPL(__phy_modify_mmd_changed);
645 
646 /**
647  * phy_modify_mmd_changed - Function for modifying a register on MMD
648  * @phydev: the phy_device struct
649  * @devad: the MMD containing register to modify
650  * @regnum: register number to modify
651  * @mask: bit mask of bits to clear
652  * @set: new value of bits set in mask to write to @regnum
653  *
654  * NOTE: MUST NOT be called from interrupt context,
655  * because the bus read/write functions may wait for an interrupt
656  * to conclude the operation.
657  *
658  * Returns negative errno, 0 if there was no change, and 1 in case of change
659  */
660 int phy_modify_mmd_changed(struct phy_device *phydev, int devad, u32 regnum,
661 			   u16 mask, u16 set)
662 {
663 	int ret;
664 
665 	phy_lock_mdio_bus(phydev);
666 	ret = __phy_modify_mmd_changed(phydev, devad, regnum, mask, set);
667 	phy_unlock_mdio_bus(phydev);
668 
669 	return ret;
670 }
671 EXPORT_SYMBOL_GPL(phy_modify_mmd_changed);
672 
673 /**
674  * __phy_modify_mmd - Convenience function for modifying a register on MMD
675  * @phydev: the phy_device struct
676  * @devad: the MMD containing register to modify
677  * @regnum: register number to modify
678  * @mask: bit mask of bits to clear
679  * @set: new value of bits set in mask to write to @regnum
680  *
681  * NOTE: MUST NOT be called from interrupt context,
682  * because the bus read/write functions may wait for an interrupt
683  * to conclude the operation.
684  */
685 int __phy_modify_mmd(struct phy_device *phydev, int devad, u32 regnum,
686 		     u16 mask, u16 set)
687 {
688 	int ret;
689 
690 	ret = __phy_modify_mmd_changed(phydev, devad, regnum, mask, set);
691 
692 	return ret < 0 ? ret : 0;
693 }
694 EXPORT_SYMBOL_GPL(__phy_modify_mmd);
695 
696 /**
697  * phy_modify_mmd - Convenience function for modifying a register on MMD
698  * @phydev: the phy_device struct
699  * @devad: the MMD containing register to modify
700  * @regnum: register number to modify
701  * @mask: bit mask of bits to clear
702  * @set: new value of bits set in mask to write to @regnum
703  *
704  * NOTE: MUST NOT be called from interrupt context,
705  * because the bus read/write functions may wait for an interrupt
706  * to conclude the operation.
707  */
708 int phy_modify_mmd(struct phy_device *phydev, int devad, u32 regnum,
709 		   u16 mask, u16 set)
710 {
711 	int ret;
712 
713 	phy_lock_mdio_bus(phydev);
714 	ret = __phy_modify_mmd(phydev, devad, regnum, mask, set);
715 	phy_unlock_mdio_bus(phydev);
716 
717 	return ret;
718 }
719 EXPORT_SYMBOL_GPL(phy_modify_mmd);
720 
721 static int __phy_read_page(struct phy_device *phydev)
722 {
723 	if (WARN_ONCE(!phydev->drv->read_page, "read_page callback not available, PHY driver not loaded?\n"))
724 		return -EOPNOTSUPP;
725 
726 	return phydev->drv->read_page(phydev);
727 }
728 
729 static int __phy_write_page(struct phy_device *phydev, int page)
730 {
731 	if (WARN_ONCE(!phydev->drv->write_page, "write_page callback not available, PHY driver not loaded?\n"))
732 		return -EOPNOTSUPP;
733 
734 	return phydev->drv->write_page(phydev, page);
735 }
736 
737 /**
738  * phy_save_page() - take the bus lock and save the current page
739  * @phydev: a pointer to a &struct phy_device
740  *
741  * Take the MDIO bus lock, and return the current page number. On error,
742  * returns a negative errno. phy_restore_page() must always be called
743  * after this, irrespective of success or failure of this call.
744  */
745 int phy_save_page(struct phy_device *phydev)
746 {
747 	phy_lock_mdio_bus(phydev);
748 	return __phy_read_page(phydev);
749 }
750 EXPORT_SYMBOL_GPL(phy_save_page);
751 
752 /**
753  * phy_select_page() - take the bus lock, save the current page, and set a page
754  * @phydev: a pointer to a &struct phy_device
755  * @page: desired page
756  *
757  * Take the MDIO bus lock to protect against concurrent access, save the
758  * current PHY page, and set the current page.  On error, returns a
759  * negative errno, otherwise returns the previous page number.
760  * phy_restore_page() must always be called after this, irrespective
761  * of success or failure of this call.
762  */
763 int phy_select_page(struct phy_device *phydev, int page)
764 {
765 	int ret, oldpage;
766 
767 	oldpage = ret = phy_save_page(phydev);
768 	if (ret < 0)
769 		return ret;
770 
771 	if (oldpage != page) {
772 		ret = __phy_write_page(phydev, page);
773 		if (ret < 0)
774 			return ret;
775 	}
776 
777 	return oldpage;
778 }
779 EXPORT_SYMBOL_GPL(phy_select_page);
780 
781 /**
782  * phy_restore_page() - restore the page register and release the bus lock
783  * @phydev: a pointer to a &struct phy_device
784  * @oldpage: the old page, return value from phy_save_page() or phy_select_page()
785  * @ret: operation's return code
786  *
787  * Release the MDIO bus lock, restoring @oldpage if it is a valid page.
788  * This function propagates the earliest error code from the group of
789  * operations.
790  *
791  * Returns:
792  *   @oldpage if it was a negative value, otherwise
793  *   @ret if it was a negative errno value, otherwise
794  *   phy_write_page()'s negative value if it were in error, otherwise
795  *   @ret.
796  */
797 int phy_restore_page(struct phy_device *phydev, int oldpage, int ret)
798 {
799 	int r;
800 
801 	if (oldpage >= 0) {
802 		r = __phy_write_page(phydev, oldpage);
803 
804 		/* Propagate the operation return code if the page write
805 		 * was successful.
806 		 */
807 		if (ret >= 0 && r < 0)
808 			ret = r;
809 	} else {
810 		/* Propagate the phy page selection error code */
811 		ret = oldpage;
812 	}
813 
814 	phy_unlock_mdio_bus(phydev);
815 
816 	return ret;
817 }
818 EXPORT_SYMBOL_GPL(phy_restore_page);
819 
820 /**
821  * phy_read_paged() - Convenience function for reading a paged register
822  * @phydev: a pointer to a &struct phy_device
823  * @page: the page for the phy
824  * @regnum: register number
825  *
826  * Same rules as for phy_read().
827  */
828 int phy_read_paged(struct phy_device *phydev, int page, u32 regnum)
829 {
830 	int ret = 0, oldpage;
831 
832 	oldpage = phy_select_page(phydev, page);
833 	if (oldpage >= 0)
834 		ret = __phy_read(phydev, regnum);
835 
836 	return phy_restore_page(phydev, oldpage, ret);
837 }
838 EXPORT_SYMBOL(phy_read_paged);
839 
840 /**
841  * phy_write_paged() - Convenience function for writing a paged register
842  * @phydev: a pointer to a &struct phy_device
843  * @page: the page for the phy
844  * @regnum: register number
845  * @val: value to write
846  *
847  * Same rules as for phy_write().
848  */
849 int phy_write_paged(struct phy_device *phydev, int page, u32 regnum, u16 val)
850 {
851 	int ret = 0, oldpage;
852 
853 	oldpage = phy_select_page(phydev, page);
854 	if (oldpage >= 0)
855 		ret = __phy_write(phydev, regnum, val);
856 
857 	return phy_restore_page(phydev, oldpage, ret);
858 }
859 EXPORT_SYMBOL(phy_write_paged);
860 
861 /**
862  * phy_modify_paged_changed() - Function for modifying a paged register
863  * @phydev: a pointer to a &struct phy_device
864  * @page: the page for the phy
865  * @regnum: register number
866  * @mask: bit mask of bits to clear
867  * @set: bit mask of bits to set
868  *
869  * Returns negative errno, 0 if there was no change, and 1 in case of change
870  */
871 int phy_modify_paged_changed(struct phy_device *phydev, int page, u32 regnum,
872 			     u16 mask, u16 set)
873 {
874 	int ret = 0, oldpage;
875 
876 	oldpage = phy_select_page(phydev, page);
877 	if (oldpage >= 0)
878 		ret = __phy_modify_changed(phydev, regnum, mask, set);
879 
880 	return phy_restore_page(phydev, oldpage, ret);
881 }
882 EXPORT_SYMBOL(phy_modify_paged_changed);
883 
884 /**
885  * phy_modify_paged() - Convenience function for modifying a paged register
886  * @phydev: a pointer to a &struct phy_device
887  * @page: the page for the phy
888  * @regnum: register number
889  * @mask: bit mask of bits to clear
890  * @set: bit mask of bits to set
891  *
892  * Same rules as for phy_read() and phy_write().
893  */
894 int phy_modify_paged(struct phy_device *phydev, int page, u32 regnum,
895 		     u16 mask, u16 set)
896 {
897 	int ret = phy_modify_paged_changed(phydev, page, regnum, mask, set);
898 
899 	return ret < 0 ? ret : 0;
900 }
901 EXPORT_SYMBOL(phy_modify_paged);
902