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