xref: /linux/drivers/net/mdio/of_mdio.c (revision 3c4fc7bf4c9e66fe71abcbf93f62f4ddb89b7f15)
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/fwnode_mdio.h>
14 #include <linux/kernel.h>
15 #include <linux/module.h>
16 #include <linux/netdevice.h>
17 #include <linux/of.h>
18 #include <linux/of_irq.h>
19 #include <linux/of_mdio.h>
20 #include <linux/of_net.h>
21 #include <linux/phy.h>
22 #include <linux/phy_fixed.h>
23 
24 #define DEFAULT_GPIO_RESET_DELAY	10	/* in microseconds */
25 
26 MODULE_AUTHOR("Grant Likely <grant.likely@secretlab.ca>");
27 MODULE_LICENSE("GPL");
28 
29 /* Extract the clause 22 phy ID from the compatible string of the form
30  * ethernet-phy-idAAAA.BBBB */
31 static int of_get_phy_id(struct device_node *device, u32 *phy_id)
32 {
33 	return fwnode_get_phy_id(of_fwnode_handle(device), phy_id);
34 }
35 
36 int of_mdiobus_phy_device_register(struct mii_bus *mdio, struct phy_device *phy,
37 				   struct device_node *child, u32 addr)
38 {
39 	return fwnode_mdiobus_phy_device_register(mdio, phy,
40 						  of_fwnode_handle(child),
41 						  addr);
42 }
43 EXPORT_SYMBOL(of_mdiobus_phy_device_register);
44 
45 static int of_mdiobus_register_phy(struct mii_bus *mdio,
46 				    struct device_node *child, u32 addr)
47 {
48 	return fwnode_mdiobus_register_phy(mdio, of_fwnode_handle(child), addr);
49 }
50 
51 static int of_mdiobus_register_device(struct mii_bus *mdio,
52 				      struct device_node *child, u32 addr)
53 {
54 	struct fwnode_handle *fwnode = of_fwnode_handle(child);
55 	struct mdio_device *mdiodev;
56 	int rc;
57 
58 	mdiodev = mdio_device_create(mdio, addr);
59 	if (IS_ERR(mdiodev))
60 		return PTR_ERR(mdiodev);
61 
62 	/* Associate the OF node with the device structure so it
63 	 * can be looked up later.
64 	 */
65 	fwnode_handle_get(fwnode);
66 	device_set_node(&mdiodev->dev, fwnode);
67 
68 	/* All data is now stored in the mdiodev struct; register it. */
69 	rc = mdio_device_register(mdiodev);
70 	if (rc) {
71 		mdio_device_free(mdiodev);
72 		of_node_put(child);
73 		return rc;
74 	}
75 
76 	dev_dbg(&mdio->dev, "registered mdio device %pOFn at address %i\n",
77 		child, addr);
78 	return 0;
79 }
80 
81 /* The following is a list of PHY compatible strings which appear in
82  * some DTBs. The compatible string is never matched against a PHY
83  * driver, so is pointless. We only expect devices which are not PHYs
84  * to have a compatible string, so they can be matched to an MDIO
85  * driver.  Encourage users to upgrade their DT blobs to remove these.
86  */
87 static const struct of_device_id whitelist_phys[] = {
88 	{ .compatible = "brcm,40nm-ephy" },
89 	{ .compatible = "broadcom,bcm5241" },
90 	{ .compatible = "marvell,88E1111", },
91 	{ .compatible = "marvell,88e1116", },
92 	{ .compatible = "marvell,88e1118", },
93 	{ .compatible = "marvell,88e1145", },
94 	{ .compatible = "marvell,88e1149r", },
95 	{ .compatible = "marvell,88e1310", },
96 	{ .compatible = "marvell,88E1510", },
97 	{ .compatible = "marvell,88E1514", },
98 	{ .compatible = "moxa,moxart-rtl8201cp", },
99 	{}
100 };
101 
102 /*
103  * Return true if the child node is for a phy. It must either:
104  * o Compatible string of "ethernet-phy-idX.X"
105  * o Compatible string of "ethernet-phy-ieee802.3-c45"
106  * o Compatible string of "ethernet-phy-ieee802.3-c22"
107  * o In the white list above (and issue a warning)
108  * o No compatibility string
109  *
110  * A device which is not a phy is expected to have a compatible string
111  * indicating what sort of device it is.
112  */
113 bool of_mdiobus_child_is_phy(struct device_node *child)
114 {
115 	u32 phy_id;
116 
117 	if (of_get_phy_id(child, &phy_id) != -EINVAL)
118 		return true;
119 
120 	if (of_device_is_compatible(child, "ethernet-phy-ieee802.3-c45"))
121 		return true;
122 
123 	if (of_device_is_compatible(child, "ethernet-phy-ieee802.3-c22"))
124 		return true;
125 
126 	if (of_match_node(whitelist_phys, child)) {
127 		pr_warn(FW_WARN
128 			"%pOF: Whitelisted compatible string. Please remove\n",
129 			child);
130 		return true;
131 	}
132 
133 	if (!of_find_property(child, "compatible", NULL))
134 		return true;
135 
136 	return false;
137 }
138 EXPORT_SYMBOL(of_mdiobus_child_is_phy);
139 
140 /**
141  * of_mdiobus_register - Register mii_bus and create PHYs from the device tree
142  * @mdio: pointer to mii_bus structure
143  * @np: pointer to device_node of MDIO bus.
144  *
145  * This function registers the mii_bus structure and registers a phy_device
146  * for each child node of @np.
147  */
148 int of_mdiobus_register(struct mii_bus *mdio, struct device_node *np)
149 {
150 	struct device_node *child;
151 	bool scanphys = false;
152 	int addr, rc;
153 
154 	if (!np)
155 		return mdiobus_register(mdio);
156 
157 	/* Do not continue if the node is disabled */
158 	if (!of_device_is_available(np))
159 		return -ENODEV;
160 
161 	/* Mask out all PHYs from auto probing.  Instead the PHYs listed in
162 	 * the device tree are populated after the bus has been registered */
163 	mdio->phy_mask = ~0;
164 
165 	device_set_node(&mdio->dev, of_fwnode_handle(np));
166 
167 	/* Get bus level PHY reset GPIO details */
168 	mdio->reset_delay_us = DEFAULT_GPIO_RESET_DELAY;
169 	of_property_read_u32(np, "reset-delay-us", &mdio->reset_delay_us);
170 	mdio->reset_post_delay_us = 0;
171 	of_property_read_u32(np, "reset-post-delay-us", &mdio->reset_post_delay_us);
172 
173 	/* Register the MDIO bus */
174 	rc = mdiobus_register(mdio);
175 	if (rc)
176 		return rc;
177 
178 	/* Loop over the child nodes and register a phy_device for each phy */
179 	for_each_available_child_of_node(np, child) {
180 		addr = of_mdio_parse_addr(&mdio->dev, child);
181 		if (addr < 0) {
182 			scanphys = true;
183 			continue;
184 		}
185 
186 		if (of_mdiobus_child_is_phy(child))
187 			rc = of_mdiobus_register_phy(mdio, child, addr);
188 		else
189 			rc = of_mdiobus_register_device(mdio, child, addr);
190 
191 		if (rc == -ENODEV)
192 			dev_err(&mdio->dev,
193 				"MDIO device at address %d is missing.\n",
194 				addr);
195 		else if (rc)
196 			goto unregister;
197 	}
198 
199 	if (!scanphys)
200 		return 0;
201 
202 	/* auto scan for PHYs with empty reg property */
203 	for_each_available_child_of_node(np, child) {
204 		/* Skip PHYs with reg property set */
205 		if (of_find_property(child, "reg", NULL))
206 			continue;
207 
208 		for (addr = 0; addr < PHY_MAX_ADDR; addr++) {
209 			/* skip already registered PHYs */
210 			if (mdiobus_is_registered_device(mdio, addr))
211 				continue;
212 
213 			/* be noisy to encourage people to set reg property */
214 			dev_info(&mdio->dev, "scan phy %pOFn at address %i\n",
215 				 child, addr);
216 
217 			if (of_mdiobus_child_is_phy(child)) {
218 				/* -ENODEV is the return code that PHYLIB has
219 				 * standardized on to indicate that bus
220 				 * scanning should continue.
221 				 */
222 				rc = of_mdiobus_register_phy(mdio, child, addr);
223 				if (!rc)
224 					break;
225 				if (rc != -ENODEV)
226 					goto unregister;
227 			}
228 		}
229 	}
230 
231 	return 0;
232 
233 unregister:
234 	of_node_put(child);
235 	mdiobus_unregister(mdio);
236 	return rc;
237 }
238 EXPORT_SYMBOL(of_mdiobus_register);
239 
240 /**
241  * of_mdio_find_device - Given a device tree node, find the mdio_device
242  * @np: pointer to the mdio_device's device tree node
243  *
244  * If successful, returns a pointer to the mdio_device with the embedded
245  * struct device refcount incremented by one, or NULL on failure.
246  * The caller should call put_device() on the mdio_device after its use
247  */
248 struct mdio_device *of_mdio_find_device(struct device_node *np)
249 {
250 	return fwnode_mdio_find_device(of_fwnode_handle(np));
251 }
252 EXPORT_SYMBOL(of_mdio_find_device);
253 
254 /**
255  * of_phy_find_device - Give a PHY node, find the phy_device
256  * @phy_np: Pointer to the phy's device tree node
257  *
258  * If successful, returns a pointer to the phy_device with the embedded
259  * struct device refcount incremented by one, or NULL on failure.
260  */
261 struct phy_device *of_phy_find_device(struct device_node *phy_np)
262 {
263 	return fwnode_phy_find_device(of_fwnode_handle(phy_np));
264 }
265 EXPORT_SYMBOL(of_phy_find_device);
266 
267 /**
268  * of_phy_connect - Connect to the phy described in the device tree
269  * @dev: pointer to net_device claiming the phy
270  * @phy_np: Pointer to device tree node for the PHY
271  * @hndlr: Link state callback for the network device
272  * @flags: flags to pass to the PHY
273  * @iface: PHY data interface type
274  *
275  * If successful, returns a pointer to the phy_device with the embedded
276  * struct device refcount incremented by one, or NULL on failure. The
277  * refcount must be dropped by calling phy_disconnect() or phy_detach().
278  */
279 struct phy_device *of_phy_connect(struct net_device *dev,
280 				  struct device_node *phy_np,
281 				  void (*hndlr)(struct net_device *), u32 flags,
282 				  phy_interface_t iface)
283 {
284 	struct phy_device *phy = of_phy_find_device(phy_np);
285 	int ret;
286 
287 	if (!phy)
288 		return NULL;
289 
290 	phy->dev_flags |= flags;
291 
292 	ret = phy_connect_direct(dev, phy, hndlr, iface);
293 
294 	/* refcount is held by phy_connect_direct() on success */
295 	put_device(&phy->mdio.dev);
296 
297 	return ret ? NULL : phy;
298 }
299 EXPORT_SYMBOL(of_phy_connect);
300 
301 /**
302  * of_phy_get_and_connect
303  * - Get phy node and connect to the phy described in the device tree
304  * @dev: pointer to net_device claiming the phy
305  * @np: Pointer to device tree node for the net_device claiming the phy
306  * @hndlr: Link state callback for the network device
307  *
308  * If successful, returns a pointer to the phy_device with the embedded
309  * struct device refcount incremented by one, or NULL on failure. The
310  * refcount must be dropped by calling phy_disconnect() or phy_detach().
311  */
312 struct phy_device *of_phy_get_and_connect(struct net_device *dev,
313 					  struct device_node *np,
314 					  void (*hndlr)(struct net_device *))
315 {
316 	phy_interface_t iface;
317 	struct device_node *phy_np;
318 	struct phy_device *phy;
319 	int ret;
320 
321 	ret = of_get_phy_mode(np, &iface);
322 	if (ret)
323 		return NULL;
324 	if (of_phy_is_fixed_link(np)) {
325 		ret = of_phy_register_fixed_link(np);
326 		if (ret < 0) {
327 			netdev_err(dev, "broken fixed-link specification\n");
328 			return NULL;
329 		}
330 		phy_np = of_node_get(np);
331 	} else {
332 		phy_np = of_parse_phandle(np, "phy-handle", 0);
333 		if (!phy_np)
334 			return NULL;
335 	}
336 
337 	phy = of_phy_connect(dev, phy_np, hndlr, 0, iface);
338 
339 	of_node_put(phy_np);
340 
341 	return phy;
342 }
343 EXPORT_SYMBOL(of_phy_get_and_connect);
344 
345 /*
346  * of_phy_is_fixed_link() and of_phy_register_fixed_link() must
347  * support two DT bindings:
348  * - the old DT binding, where 'fixed-link' was a property with 5
349  *   cells encoding various information about the fixed PHY
350  * - the new DT binding, where 'fixed-link' is a sub-node of the
351  *   Ethernet device.
352  */
353 bool of_phy_is_fixed_link(struct device_node *np)
354 {
355 	struct device_node *dn;
356 	int len, err;
357 	const char *managed;
358 
359 	/* New binding */
360 	dn = of_get_child_by_name(np, "fixed-link");
361 	if (dn) {
362 		of_node_put(dn);
363 		return true;
364 	}
365 
366 	err = of_property_read_string(np, "managed", &managed);
367 	if (err == 0 && strcmp(managed, "auto") != 0)
368 		return true;
369 
370 	/* Old binding */
371 	if (of_get_property(np, "fixed-link", &len) &&
372 	    len == (5 * sizeof(__be32)))
373 		return true;
374 
375 	return false;
376 }
377 EXPORT_SYMBOL(of_phy_is_fixed_link);
378 
379 int of_phy_register_fixed_link(struct device_node *np)
380 {
381 	struct fixed_phy_status status = {};
382 	struct device_node *fixed_link_node;
383 	u32 fixed_link_prop[5];
384 	const char *managed;
385 
386 	if (of_property_read_string(np, "managed", &managed) == 0 &&
387 	    strcmp(managed, "in-band-status") == 0) {
388 		/* status is zeroed, namely its .link member */
389 		goto register_phy;
390 	}
391 
392 	/* New binding */
393 	fixed_link_node = of_get_child_by_name(np, "fixed-link");
394 	if (fixed_link_node) {
395 		status.link = 1;
396 		status.duplex = of_property_read_bool(fixed_link_node,
397 						      "full-duplex");
398 		if (of_property_read_u32(fixed_link_node, "speed",
399 					 &status.speed)) {
400 			of_node_put(fixed_link_node);
401 			return -EINVAL;
402 		}
403 		status.pause = of_property_read_bool(fixed_link_node, "pause");
404 		status.asym_pause = of_property_read_bool(fixed_link_node,
405 							  "asym-pause");
406 		of_node_put(fixed_link_node);
407 
408 		goto register_phy;
409 	}
410 
411 	/* Old binding */
412 	if (of_property_read_u32_array(np, "fixed-link", fixed_link_prop,
413 				       ARRAY_SIZE(fixed_link_prop)) == 0) {
414 		status.link = 1;
415 		status.duplex = fixed_link_prop[1];
416 		status.speed  = fixed_link_prop[2];
417 		status.pause  = fixed_link_prop[3];
418 		status.asym_pause = fixed_link_prop[4];
419 		goto register_phy;
420 	}
421 
422 	return -ENODEV;
423 
424 register_phy:
425 	return PTR_ERR_OR_ZERO(fixed_phy_register(PHY_POLL, &status, np));
426 }
427 EXPORT_SYMBOL(of_phy_register_fixed_link);
428 
429 void of_phy_deregister_fixed_link(struct device_node *np)
430 {
431 	struct phy_device *phydev;
432 
433 	phydev = of_phy_find_device(np);
434 	if (!phydev)
435 		return;
436 
437 	fixed_phy_unregister(phydev);
438 
439 	put_device(&phydev->mdio.dev);	/* of_phy_find_device() */
440 	phy_device_free(phydev);	/* fixed_phy_register() */
441 }
442 EXPORT_SYMBOL(of_phy_deregister_fixed_link);
443