1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * OF helpers for the MDIO (Ethernet PHY) API
4  *
5  * Copyright (c) 2009 Secret Lab Technologies, Ltd.
6  *
7  * This file provides helper functions for extracting PHY device information
8  * out of the OpenFirmware device tree and using it to populate an mii_bus.
9  */
10 
11 #include <linux/device.h>
12 #include <linux/err.h>
13 #include <linux/kernel.h>
14 #include <linux/module.h>
15 #include <linux/netdevice.h>
16 #include <linux/of.h>
17 #include <linux/of_irq.h>
18 #include <linux/of_mdio.h>
19 #include <linux/of_net.h>
20 #include <linux/phy.h>
21 #include <linux/phy_fixed.h>
22 
23 #define DEFAULT_GPIO_RESET_DELAY	10	/* in microseconds */
24 
25 MODULE_AUTHOR("Grant Likely <grant.likely@secretlab.ca>");
26 MODULE_LICENSE("GPL");
27 
28 /* Extract the clause 22 phy ID from the compatible string of the form
29  * ethernet-phy-idAAAA.BBBB */
of_get_phy_id(struct device_node *device, u32 *phy_id)30 static int of_get_phy_id(struct device_node *device, u32 *phy_id)
31 {
32 	struct property *prop;
33 	const char *cp;
34 	unsigned int upper, lower;
35 
36 	of_property_for_each_string(device, "compatible", prop, cp) {
37 		if (sscanf(cp, "ethernet-phy-id%4x.%4x", &upper, &lower) == 2) {
38 			*phy_id = ((upper & 0xFFFF) << 16) | (lower & 0xFFFF);
39 			return 0;
40 		}
41 	}
42 	return -EINVAL;
43 }
44 
of_find_mii_timestamper(struct device_node *node)45 static struct mii_timestamper *of_find_mii_timestamper(struct device_node *node)
46 {
47 	struct of_phandle_args arg;
48 	int err;
49 
50 	err = of_parse_phandle_with_fixed_args(node, "timestamper", 1, 0, &arg);
51 
52 	if (err == -ENOENT)
53 		return NULL;
54 	else if (err)
55 		return ERR_PTR(err);
56 
57 	if (arg.args_count != 1)
58 		return ERR_PTR(-EINVAL);
59 
60 	return register_mii_timestamper(arg.np, arg.args[0]);
61 }
62 
of_mdiobus_phy_device_register(struct mii_bus *mdio, struct phy_device *phy, struct device_node *child, u32 addr)63 int of_mdiobus_phy_device_register(struct mii_bus *mdio, struct phy_device *phy,
64 			      struct device_node *child, u32 addr)
65 {
66 	int rc;
67 
68 	rc = of_irq_get(child, 0);
69 	if (rc == -EPROBE_DEFER)
70 		return rc;
71 
72 	if (rc > 0) {
73 		phy->irq = rc;
74 		mdio->irq[addr] = rc;
75 	} else {
76 		phy->irq = mdio->irq[addr];
77 	}
78 
79 	if (of_property_read_bool(child, "broken-turn-around"))
80 		mdio->phy_ignore_ta_mask |= 1 << addr;
81 
82 	of_property_read_u32(child, "reset-assert-us",
83 			     &phy->mdio.reset_assert_delay);
84 	of_property_read_u32(child, "reset-deassert-us",
85 			     &phy->mdio.reset_deassert_delay);
86 
87 	/* Associate the OF node with the device structure so it
88 	 * can be looked up later */
89 	of_node_get(child);
90 	phy->mdio.dev.of_node = child;
91 	phy->mdio.dev.fwnode = of_fwnode_handle(child);
92 
93 	/* All data is now stored in the phy struct;
94 	 * register it */
95 	rc = phy_device_register(phy);
96 	if (rc) {
97 		of_node_put(child);
98 		return rc;
99 	}
100 
101 	dev_dbg(&mdio->dev, "registered phy %pOFn at address %i\n",
102 		child, addr);
103 	return 0;
104 }
105 EXPORT_SYMBOL(of_mdiobus_phy_device_register);
106 
of_mdiobus_register_phy(struct mii_bus *mdio, struct device_node *child, u32 addr)107 static int of_mdiobus_register_phy(struct mii_bus *mdio,
108 				    struct device_node *child, u32 addr)
109 {
110 	struct mii_timestamper *mii_ts;
111 	struct phy_device *phy;
112 	bool is_c45;
113 	int rc;
114 	u32 phy_id;
115 
116 	mii_ts = of_find_mii_timestamper(child);
117 	if (IS_ERR(mii_ts))
118 		return PTR_ERR(mii_ts);
119 
120 	is_c45 = of_device_is_compatible(child,
121 					 "ethernet-phy-ieee802.3-c45");
122 
123 	if (!is_c45 && !of_get_phy_id(child, &phy_id))
124 		phy = phy_device_create(mdio, addr, phy_id, 0, NULL);
125 	else
126 		phy = get_phy_device(mdio, addr, is_c45);
127 	if (IS_ERR(phy)) {
128 		if (mii_ts)
129 			unregister_mii_timestamper(mii_ts);
130 		return PTR_ERR(phy);
131 	}
132 
133 	rc = of_mdiobus_phy_device_register(mdio, phy, child, addr);
134 	if (rc) {
135 		if (mii_ts)
136 			unregister_mii_timestamper(mii_ts);
137 		phy_device_free(phy);
138 		return rc;
139 	}
140 
141 	/* phy->mii_ts may already be defined by the PHY driver. A
142 	 * mii_timestamper probed via the device tree will still have
143 	 * precedence.
144 	 */
145 	if (mii_ts)
146 		phy->mii_ts = mii_ts;
147 
148 	return 0;
149 }
150 
of_mdiobus_register_device(struct mii_bus *mdio, struct device_node *child, u32 addr)151 static int of_mdiobus_register_device(struct mii_bus *mdio,
152 				      struct device_node *child, u32 addr)
153 {
154 	struct mdio_device *mdiodev;
155 	int rc;
156 
157 	mdiodev = mdio_device_create(mdio, addr);
158 	if (IS_ERR(mdiodev))
159 		return PTR_ERR(mdiodev);
160 
161 	/* Associate the OF node with the device structure so it
162 	 * can be looked up later.
163 	 */
164 	of_node_get(child);
165 	mdiodev->dev.of_node = child;
166 	mdiodev->dev.fwnode = of_fwnode_handle(child);
167 
168 	/* All data is now stored in the mdiodev struct; register it. */
169 	rc = mdio_device_register(mdiodev);
170 	if (rc) {
171 		mdio_device_free(mdiodev);
172 		of_node_put(child);
173 		return rc;
174 	}
175 
176 	dev_dbg(&mdio->dev, "registered mdio device %pOFn at address %i\n",
177 		child, addr);
178 	return 0;
179 }
180 
181 /* The following is a list of PHY compatible strings which appear in
182  * some DTBs. The compatible string is never matched against a PHY
183  * driver, so is pointless. We only expect devices which are not PHYs
184  * to have a compatible string, so they can be matched to an MDIO
185  * driver.  Encourage users to upgrade their DT blobs to remove these.
186  */
187 static const struct of_device_id whitelist_phys[] = {
188 	{ .compatible = "brcm,40nm-ephy" },
189 	{ .compatible = "broadcom,bcm5241" },
190 	{ .compatible = "marvell,88E1111", },
191 	{ .compatible = "marvell,88e1116", },
192 	{ .compatible = "marvell,88e1118", },
193 	{ .compatible = "marvell,88e1145", },
194 	{ .compatible = "marvell,88e1149r", },
195 	{ .compatible = "marvell,88e1310", },
196 	{ .compatible = "marvell,88E1510", },
197 	{ .compatible = "marvell,88E1514", },
198 	{ .compatible = "moxa,moxart-rtl8201cp", },
199 	{}
200 };
201 
202 /*
203  * Return true if the child node is for a phy. It must either:
204  * o Compatible string of "ethernet-phy-idX.X"
205  * o Compatible string of "ethernet-phy-ieee802.3-c45"
206  * o Compatible string of "ethernet-phy-ieee802.3-c22"
207  * o In the white list above (and issue a warning)
208  * o No compatibility string
209  *
210  * A device which is not a phy is expected to have a compatible string
211  * indicating what sort of device it is.
212  */
of_mdiobus_child_is_phy(struct device_node *child)213 bool of_mdiobus_child_is_phy(struct device_node *child)
214 {
215 	u32 phy_id;
216 
217 	if (of_get_phy_id(child, &phy_id) != -EINVAL)
218 		return true;
219 
220 	if (of_device_is_compatible(child, "ethernet-phy-ieee802.3-c45"))
221 		return true;
222 
223 	if (of_device_is_compatible(child, "ethernet-phy-ieee802.3-c22"))
224 		return true;
225 
226 	if (of_match_node(whitelist_phys, child)) {
227 		pr_warn(FW_WARN
228 			"%pOF: Whitelisted compatible string. Please remove\n",
229 			child);
230 		return true;
231 	}
232 
233 	if (!of_find_property(child, "compatible", NULL))
234 		return true;
235 
236 	return false;
237 }
238 EXPORT_SYMBOL(of_mdiobus_child_is_phy);
239 
240 /**
241  * __of_mdiobus_register - Register mii_bus and create PHYs from the device tree
242  * @mdio: pointer to mii_bus structure
243  * @np: pointer to device_node of MDIO bus.
244  * @owner: module owning the @mdio object.
245  *
246  * This function registers the mii_bus structure and registers a phy_device
247  * for each child node of @np.
248  */
__of_mdiobus_register(struct mii_bus *mdio, struct device_node *np, struct module *owner)249 int __of_mdiobus_register(struct mii_bus *mdio, struct device_node *np,
250 			  struct module *owner)
251 {
252 	struct device_node *child;
253 	bool scanphys = false;
254 	int addr, rc;
255 
256 	if (!np)
257 		return __mdiobus_register(mdio, owner);
258 
259 	/* Do not continue if the node is disabled */
260 	if (!of_device_is_available(np))
261 		return -ENODEV;
262 
263 	/* Mask out all PHYs from auto probing.  Instead the PHYs listed in
264 	 * the device tree are populated after the bus has been registered */
265 	mdio->phy_mask = ~0;
266 
267 	mdio->dev.of_node = np;
268 	mdio->dev.fwnode = of_fwnode_handle(np);
269 
270 	/* Get bus level PHY reset GPIO details */
271 	mdio->reset_delay_us = DEFAULT_GPIO_RESET_DELAY;
272 	of_property_read_u32(np, "reset-delay-us", &mdio->reset_delay_us);
273 	mdio->reset_post_delay_us = 0;
274 	of_property_read_u32(np, "reset-post-delay-us", &mdio->reset_post_delay_us);
275 
276 	/* Register the MDIO bus */
277 	rc = __mdiobus_register(mdio, owner);
278 	if (rc)
279 		return rc;
280 
281 	/* Loop over the child nodes and register a phy_device for each phy */
282 	for_each_available_child_of_node(np, child) {
283 		addr = of_mdio_parse_addr(&mdio->dev, child);
284 		if (addr < 0) {
285 			scanphys = true;
286 			continue;
287 		}
288 
289 		if (of_mdiobus_child_is_phy(child))
290 			rc = of_mdiobus_register_phy(mdio, child, addr);
291 		else
292 			rc = of_mdiobus_register_device(mdio, child, addr);
293 
294 		if (rc == -ENODEV)
295 			dev_err(&mdio->dev,
296 				"MDIO device at address %d is missing.\n",
297 				addr);
298 		else if (rc)
299 			goto unregister;
300 	}
301 
302 	if (!scanphys)
303 		return 0;
304 
305 	/* auto scan for PHYs with empty reg property */
306 	for_each_available_child_of_node(np, child) {
307 		/* Skip PHYs with reg property set */
308 		if (of_find_property(child, "reg", NULL))
309 			continue;
310 
311 		for (addr = 0; addr < PHY_MAX_ADDR; addr++) {
312 			/* skip already registered PHYs */
313 			if (mdiobus_is_registered_device(mdio, addr))
314 				continue;
315 
316 			/* be noisy to encourage people to set reg property */
317 			dev_info(&mdio->dev, "scan phy %pOFn at address %i\n",
318 				 child, addr);
319 
320 			if (of_mdiobus_child_is_phy(child)) {
321 				/* -ENODEV is the return code that PHYLIB has
322 				 * standardized on to indicate that bus
323 				 * scanning should continue.
324 				 */
325 				rc = of_mdiobus_register_phy(mdio, child, addr);
326 				if (!rc)
327 					break;
328 				if (rc != -ENODEV)
329 					goto unregister;
330 			}
331 		}
332 	}
333 
334 	return 0;
335 
336 unregister:
337 	of_node_put(child);
338 	mdiobus_unregister(mdio);
339 	return rc;
340 }
341 EXPORT_SYMBOL(__of_mdiobus_register);
342 
343 /**
344  * of_mdio_find_device - Given a device tree node, find the mdio_device
345  * @np: pointer to the mdio_device's device tree node
346  *
347  * If successful, returns a pointer to the mdio_device with the embedded
348  * struct device refcount incremented by one, or NULL on failure.
349  * The caller should call put_device() on the mdio_device after its use
350  */
of_mdio_find_device(struct device_node *np)351 struct mdio_device *of_mdio_find_device(struct device_node *np)
352 {
353 	struct device *d;
354 
355 	if (!np)
356 		return NULL;
357 
358 	d = bus_find_device_by_of_node(&mdio_bus_type, np);
359 	if (!d)
360 		return NULL;
361 
362 	return to_mdio_device(d);
363 }
364 EXPORT_SYMBOL(of_mdio_find_device);
365 
366 /**
367  * of_phy_find_device - Give a PHY node, find the phy_device
368  * @phy_np: Pointer to the phy's device tree node
369  *
370  * If successful, returns a pointer to the phy_device with the embedded
371  * struct device refcount incremented by one, or NULL on failure.
372  */
of_phy_find_device(struct device_node *phy_np)373 struct phy_device *of_phy_find_device(struct device_node *phy_np)
374 {
375 	struct mdio_device *mdiodev;
376 
377 	mdiodev = of_mdio_find_device(phy_np);
378 	if (!mdiodev)
379 		return NULL;
380 
381 	if (mdiodev->flags & MDIO_DEVICE_FLAG_PHY)
382 		return to_phy_device(&mdiodev->dev);
383 
384 	put_device(&mdiodev->dev);
385 
386 	return NULL;
387 }
388 EXPORT_SYMBOL(of_phy_find_device);
389 
390 /**
391  * of_phy_connect - Connect to the phy described in the device tree
392  * @dev: pointer to net_device claiming the phy
393  * @phy_np: Pointer to device tree node for the PHY
394  * @hndlr: Link state callback for the network device
395  * @flags: flags to pass to the PHY
396  * @iface: PHY data interface type
397  *
398  * If successful, returns a pointer to the phy_device with the embedded
399  * struct device refcount incremented by one, or NULL on failure. The
400  * refcount must be dropped by calling phy_disconnect() or phy_detach().
401  */
of_phy_connect(struct net_device *dev, struct device_node *phy_np, void (*hndlr)(struct net_device *), u32 flags, phy_interface_t iface)402 struct phy_device *of_phy_connect(struct net_device *dev,
403 				  struct device_node *phy_np,
404 				  void (*hndlr)(struct net_device *), u32 flags,
405 				  phy_interface_t iface)
406 {
407 	struct phy_device *phy = of_phy_find_device(phy_np);
408 	int ret;
409 
410 	if (!phy)
411 		return NULL;
412 
413 	phy->dev_flags |= flags;
414 
415 	ret = phy_connect_direct(dev, phy, hndlr, iface);
416 
417 	/* refcount is held by phy_connect_direct() on success */
418 	put_device(&phy->mdio.dev);
419 
420 	return ret ? NULL : phy;
421 }
422 EXPORT_SYMBOL(of_phy_connect);
423 
424 /**
425  * of_phy_get_and_connect
426  * - Get phy node and connect to the phy described in the device tree
427  * @dev: pointer to net_device claiming the phy
428  * @np: Pointer to device tree node for the net_device claiming the phy
429  * @hndlr: Link state callback for the network device
430  *
431  * If successful, returns a pointer to the phy_device with the embedded
432  * struct device refcount incremented by one, or NULL on failure. The
433  * refcount must be dropped by calling phy_disconnect() or phy_detach().
434  */
of_phy_get_and_connect(struct net_device *dev, struct device_node *np, void (*hndlr)(struct net_device *))435 struct phy_device *of_phy_get_and_connect(struct net_device *dev,
436 					  struct device_node *np,
437 					  void (*hndlr)(struct net_device *))
438 {
439 	phy_interface_t iface;
440 	struct device_node *phy_np;
441 	struct phy_device *phy;
442 	int ret;
443 
444 	ret = of_get_phy_mode(np, &iface);
445 	if (ret)
446 		return NULL;
447 	if (of_phy_is_fixed_link(np)) {
448 		ret = of_phy_register_fixed_link(np);
449 		if (ret < 0) {
450 			netdev_err(dev, "broken fixed-link specification\n");
451 			return NULL;
452 		}
453 		phy_np = of_node_get(np);
454 	} else {
455 		phy_np = of_parse_phandle(np, "phy-handle", 0);
456 		if (!phy_np)
457 			return NULL;
458 	}
459 
460 	phy = of_phy_connect(dev, phy_np, hndlr, 0, iface);
461 
462 	of_node_put(phy_np);
463 
464 	return phy;
465 }
466 EXPORT_SYMBOL(of_phy_get_and_connect);
467 
468 /**
469  * of_phy_attach - Attach to a PHY without starting the state machine
470  * @dev: pointer to net_device claiming the phy
471  * @phy_np: Node pointer for the PHY
472  * @flags: flags to pass to the PHY
473  * @iface: PHY data interface type
474  *
475  * If successful, returns a pointer to the phy_device with the embedded
476  * struct device refcount incremented by one, or NULL on failure. The
477  * refcount must be dropped by calling phy_disconnect() or phy_detach().
478  */
of_phy_attach(struct net_device *dev, struct device_node *phy_np, u32 flags, phy_interface_t iface)479 struct phy_device *of_phy_attach(struct net_device *dev,
480 				 struct device_node *phy_np, u32 flags,
481 				 phy_interface_t iface)
482 {
483 	struct phy_device *phy = of_phy_find_device(phy_np);
484 	int ret;
485 
486 	if (!phy)
487 		return NULL;
488 
489 	ret = phy_attach_direct(dev, phy, flags, iface);
490 
491 	/* refcount is held by phy_attach_direct() on success */
492 	put_device(&phy->mdio.dev);
493 
494 	return ret ? NULL : phy;
495 }
496 EXPORT_SYMBOL(of_phy_attach);
497 
498 /*
499  * of_phy_is_fixed_link() and of_phy_register_fixed_link() must
500  * support two DT bindings:
501  * - the old DT binding, where 'fixed-link' was a property with 5
502  *   cells encoding various informations about the fixed PHY
503  * - the new DT binding, where 'fixed-link' is a sub-node of the
504  *   Ethernet device.
505  */
of_phy_is_fixed_link(struct device_node *np)506 bool of_phy_is_fixed_link(struct device_node *np)
507 {
508 	struct device_node *dn;
509 	int len, err;
510 	const char *managed;
511 
512 	/* New binding */
513 	dn = of_get_child_by_name(np, "fixed-link");
514 	if (dn) {
515 		of_node_put(dn);
516 		return true;
517 	}
518 
519 	err = of_property_read_string(np, "managed", &managed);
520 	if (err == 0 && strcmp(managed, "auto") != 0)
521 		return true;
522 
523 	/* Old binding */
524 	if (of_get_property(np, "fixed-link", &len) &&
525 	    len == (5 * sizeof(__be32)))
526 		return true;
527 
528 	return false;
529 }
530 EXPORT_SYMBOL(of_phy_is_fixed_link);
531 
of_phy_register_fixed_link(struct device_node *np)532 int of_phy_register_fixed_link(struct device_node *np)
533 {
534 	struct fixed_phy_status status = {};
535 	struct device_node *fixed_link_node;
536 	u32 fixed_link_prop[5];
537 	const char *managed;
538 
539 	if (of_property_read_string(np, "managed", &managed) == 0 &&
540 	    strcmp(managed, "in-band-status") == 0) {
541 		/* status is zeroed, namely its .link member */
542 		goto register_phy;
543 	}
544 
545 	/* New binding */
546 	fixed_link_node = of_get_child_by_name(np, "fixed-link");
547 	if (fixed_link_node) {
548 		status.link = 1;
549 		status.duplex = of_property_read_bool(fixed_link_node,
550 						      "full-duplex");
551 		if (of_property_read_u32(fixed_link_node, "speed",
552 					 &status.speed)) {
553 			of_node_put(fixed_link_node);
554 			return -EINVAL;
555 		}
556 		status.pause = of_property_read_bool(fixed_link_node, "pause");
557 		status.asym_pause = of_property_read_bool(fixed_link_node,
558 							  "asym-pause");
559 		of_node_put(fixed_link_node);
560 
561 		goto register_phy;
562 	}
563 
564 	/* Old binding */
565 	if (of_property_read_u32_array(np, "fixed-link", fixed_link_prop,
566 				       ARRAY_SIZE(fixed_link_prop)) == 0) {
567 		status.link = 1;
568 		status.duplex = fixed_link_prop[1];
569 		status.speed  = fixed_link_prop[2];
570 		status.pause  = fixed_link_prop[3];
571 		status.asym_pause = fixed_link_prop[4];
572 		goto register_phy;
573 	}
574 
575 	return -ENODEV;
576 
577 register_phy:
578 	return PTR_ERR_OR_ZERO(fixed_phy_register(PHY_POLL, &status, np));
579 }
580 EXPORT_SYMBOL(of_phy_register_fixed_link);
581 
of_phy_deregister_fixed_link(struct device_node *np)582 void of_phy_deregister_fixed_link(struct device_node *np)
583 {
584 	struct phy_device *phydev;
585 
586 	phydev = of_phy_find_device(np);
587 	if (!phydev)
588 		return;
589 
590 	fixed_phy_unregister(phydev);
591 
592 	put_device(&phydev->mdio.dev);	/* of_phy_find_device() */
593 	phy_device_free(phydev);	/* fixed_phy_register() */
594 }
595 EXPORT_SYMBOL(of_phy_deregister_fixed_link);
596