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

source code of linux/drivers/net/mdio/of_mdio.c