1 // SPDX-License-Identifier: GPL-2.0-only
3 * OF helpers for the MDIO (Ethernet PHY) API
5 * Copyright (c) 2009 Secret Lab Technologies, Ltd.
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.
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>
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>
24 #define DEFAULT_GPIO_RESET_DELAY 10 /* in microseconds */
26 MODULE_AUTHOR("Grant Likely <grant.likely@secretlab.ca>");
27 MODULE_LICENSE("GPL");
28 MODULE_DESCRIPTION("OpenFirmware MDIO bus (Ethernet PHY) accessors");
30 /* Extract the clause 22 phy ID from the compatible string of the form
31 * ethernet-phy-idAAAA.BBBB */
32 static int of_get_phy_id(struct device_node
*device
, u32
*phy_id
)
34 return fwnode_get_phy_id(of_fwnode_handle(device
), phy_id
);
37 int of_mdiobus_phy_device_register(struct mii_bus
*mdio
, struct phy_device
*phy
,
38 struct device_node
*child
, u32 addr
)
40 return fwnode_mdiobus_phy_device_register(mdio
, phy
,
41 of_fwnode_handle(child
),
44 EXPORT_SYMBOL(of_mdiobus_phy_device_register
);
46 static int of_mdiobus_register_phy(struct mii_bus
*mdio
,
47 struct device_node
*child
, u32 addr
)
49 return fwnode_mdiobus_register_phy(mdio
, of_fwnode_handle(child
), addr
);
52 static int of_mdiobus_register_device(struct mii_bus
*mdio
,
53 struct device_node
*child
, u32 addr
)
55 struct fwnode_handle
*fwnode
= of_fwnode_handle(child
);
56 struct mdio_device
*mdiodev
;
59 mdiodev
= mdio_device_create(mdio
, addr
);
61 return PTR_ERR(mdiodev
);
63 /* Associate the OF node with the device structure so it
64 * can be looked up later.
66 fwnode_handle_get(fwnode
);
67 device_set_node(&mdiodev
->dev
, fwnode
);
69 /* All data is now stored in the mdiodev struct; register it. */
70 rc
= mdio_device_register(mdiodev
);
72 device_set_node(&mdiodev
->dev
, NULL
);
73 fwnode_handle_put(fwnode
);
74 mdio_device_free(mdiodev
);
78 dev_dbg(&mdio
->dev
, "registered mdio device %pOFn at address %i\n",
83 /* The following is a list of PHY compatible strings which appear in
84 * some DTBs. The compatible string is never matched against a PHY
85 * driver, so is pointless. We only expect devices which are not PHYs
86 * to have a compatible string, so they can be matched to an MDIO
87 * driver. Encourage users to upgrade their DT blobs to remove these.
89 static const struct of_device_id whitelist_phys
[] = {
90 { .compatible
= "brcm,40nm-ephy" },
91 { .compatible
= "broadcom,bcm5241" },
92 { .compatible
= "marvell,88E1111", },
93 { .compatible
= "marvell,88e1116", },
94 { .compatible
= "marvell,88e1118", },
95 { .compatible
= "marvell,88e1145", },
96 { .compatible
= "marvell,88e1149r", },
97 { .compatible
= "marvell,88e1310", },
98 { .compatible
= "marvell,88E1510", },
99 { .compatible
= "marvell,88E1514", },
100 { .compatible
= "moxa,moxart-rtl8201cp", },
105 * Return true if the child node is for a phy. It must either:
106 * o Compatible string of "ethernet-phy-idX.X"
107 * o Compatible string of "ethernet-phy-ieee802.3-c45"
108 * o Compatible string of "ethernet-phy-ieee802.3-c22"
109 * o In the white list above (and issue a warning)
110 * o No compatibility string
112 * A device which is not a phy is expected to have a compatible string
113 * indicating what sort of device it is.
115 bool of_mdiobus_child_is_phy(struct device_node
*child
)
119 if (of_get_phy_id(child
, &phy_id
) != -EINVAL
)
122 if (of_device_is_compatible(child
, "ethernet-phy-ieee802.3-c45"))
125 if (of_device_is_compatible(child
, "ethernet-phy-ieee802.3-c22"))
128 if (of_match_node(whitelist_phys
, child
)) {
130 "%pOF: Whitelisted compatible string. Please remove\n",
135 if (!of_property_present(child
, "compatible"))
140 EXPORT_SYMBOL(of_mdiobus_child_is_phy
);
142 static int __of_mdiobus_parse_phys(struct mii_bus
*mdio
, struct device_node
*np
,
145 struct device_node
*child
;
148 /* Loop over the child nodes and register a phy_device for each phy */
149 for_each_available_child_of_node(np
, child
) {
150 if (of_node_name_eq(child
, "ethernet-phy-package")) {
151 /* Ignore invalid ethernet-phy-package node */
152 if (!of_property_present(child
, "reg"))
155 rc
= __of_mdiobus_parse_phys(mdio
, child
, NULL
);
156 if (rc
&& rc
!= -ENODEV
)
162 addr
= of_mdio_parse_addr(&mdio
->dev
, child
);
164 /* Skip scanning for invalid ethernet-phy-package node */
170 if (of_mdiobus_child_is_phy(child
))
171 rc
= of_mdiobus_register_phy(mdio
, child
, addr
);
173 rc
= of_mdiobus_register_device(mdio
, child
, addr
);
177 "MDIO device at address %d is missing.\n",
190 * __of_mdiobus_register - Register mii_bus and create PHYs from the device tree
191 * @mdio: pointer to mii_bus structure
192 * @np: pointer to device_node of MDIO bus.
193 * @owner: module owning the @mdio object.
195 * This function registers the mii_bus structure and registers a phy_device
196 * for each child node of @np.
198 int __of_mdiobus_register(struct mii_bus
*mdio
, struct device_node
*np
,
199 struct module
*owner
)
201 struct device_node
*child
;
202 bool scanphys
= false;
206 return __mdiobus_register(mdio
, owner
);
208 /* Do not continue if the node is disabled */
209 if (!of_device_is_available(np
))
212 /* Mask out all PHYs from auto probing. Instead the PHYs listed in
213 * the device tree are populated after the bus has been registered */
216 device_set_node(&mdio
->dev
, of_fwnode_handle(np
));
218 /* Get bus level PHY reset GPIO details */
219 mdio
->reset_delay_us
= DEFAULT_GPIO_RESET_DELAY
;
220 of_property_read_u32(np
, "reset-delay-us", &mdio
->reset_delay_us
);
221 mdio
->reset_post_delay_us
= 0;
222 of_property_read_u32(np
, "reset-post-delay-us", &mdio
->reset_post_delay_us
);
224 /* Register the MDIO bus */
225 rc
= __mdiobus_register(mdio
, owner
);
229 /* Loop over the child nodes and register a phy_device for each phy */
230 rc
= __of_mdiobus_parse_phys(mdio
, np
, &scanphys
);
237 /* auto scan for PHYs with empty reg property */
238 for_each_available_child_of_node(np
, child
) {
239 /* Skip PHYs with reg property set or ethernet-phy-package node */
240 if (of_property_present(child
, "reg") ||
241 of_node_name_eq(child
, "ethernet-phy-package"))
244 for (addr
= 0; addr
< PHY_MAX_ADDR
; addr
++) {
245 /* skip already registered PHYs */
246 if (mdiobus_is_registered_device(mdio
, addr
))
249 /* be noisy to encourage people to set reg property */
250 dev_info(&mdio
->dev
, "scan phy %pOFn at address %i\n",
253 if (of_mdiobus_child_is_phy(child
)) {
254 /* -ENODEV is the return code that PHYLIB has
255 * standardized on to indicate that bus
256 * scanning should continue.
258 rc
= of_mdiobus_register_phy(mdio
, child
, addr
);
272 mdiobus_unregister(mdio
);
275 EXPORT_SYMBOL(__of_mdiobus_register
);
278 * of_mdio_find_device - Given a device tree node, find the mdio_device
279 * @np: pointer to the mdio_device's device tree node
281 * If successful, returns a pointer to the mdio_device with the embedded
282 * struct device refcount incremented by one, or NULL on failure.
283 * The caller should call put_device() on the mdio_device after its use
285 struct mdio_device
*of_mdio_find_device(struct device_node
*np
)
287 return fwnode_mdio_find_device(of_fwnode_handle(np
));
289 EXPORT_SYMBOL(of_mdio_find_device
);
292 * of_phy_find_device - Give a PHY node, find the phy_device
293 * @phy_np: Pointer to the phy's device tree node
295 * If successful, returns a pointer to the phy_device with the embedded
296 * struct device refcount incremented by one, or NULL on failure.
298 struct phy_device
*of_phy_find_device(struct device_node
*phy_np
)
300 return fwnode_phy_find_device(of_fwnode_handle(phy_np
));
302 EXPORT_SYMBOL(of_phy_find_device
);
305 * of_phy_connect - Connect to the phy described in the device tree
306 * @dev: pointer to net_device claiming the phy
307 * @phy_np: Pointer to device tree node for the PHY
308 * @hndlr: Link state callback for the network device
309 * @flags: flags to pass to the PHY
310 * @iface: PHY data interface type
312 * If successful, returns a pointer to the phy_device with the embedded
313 * struct device refcount incremented by one, or NULL on failure. The
314 * refcount must be dropped by calling phy_disconnect() or phy_detach().
316 struct phy_device
*of_phy_connect(struct net_device
*dev
,
317 struct device_node
*phy_np
,
318 void (*hndlr
)(struct net_device
*), u32 flags
,
319 phy_interface_t iface
)
321 struct phy_device
*phy
= of_phy_find_device(phy_np
);
327 phy
->dev_flags
|= flags
;
329 ret
= phy_connect_direct(dev
, phy
, hndlr
, iface
);
331 /* refcount is held by phy_connect_direct() on success */
332 put_device(&phy
->mdio
.dev
);
334 return ret
? NULL
: phy
;
336 EXPORT_SYMBOL(of_phy_connect
);
339 * of_phy_get_and_connect
340 * - Get phy node and connect to the phy described in the device tree
341 * @dev: pointer to net_device claiming the phy
342 * @np: Pointer to device tree node for the net_device claiming the phy
343 * @hndlr: Link state callback for the network device
345 * If successful, returns a pointer to the phy_device with the embedded
346 * struct device refcount incremented by one, or NULL on failure. The
347 * refcount must be dropped by calling phy_disconnect() or phy_detach().
349 struct phy_device
*of_phy_get_and_connect(struct net_device
*dev
,
350 struct device_node
*np
,
351 void (*hndlr
)(struct net_device
*))
353 phy_interface_t iface
;
354 struct device_node
*phy_np
;
355 struct phy_device
*phy
;
358 ret
= of_get_phy_mode(np
, &iface
);
361 if (of_phy_is_fixed_link(np
)) {
362 ret
= of_phy_register_fixed_link(np
);
364 netdev_err(dev
, "broken fixed-link specification\n");
367 phy_np
= of_node_get(np
);
369 phy_np
= of_parse_phandle(np
, "phy-handle", 0);
374 phy
= of_phy_connect(dev
, phy_np
, hndlr
, 0, iface
);
380 EXPORT_SYMBOL(of_phy_get_and_connect
);
383 * of_phy_is_fixed_link() and of_phy_register_fixed_link() must
384 * support two DT bindings:
385 * - the old DT binding, where 'fixed-link' was a property with 5
386 * cells encoding various information about the fixed PHY
387 * - the new DT binding, where 'fixed-link' is a sub-node of the
390 bool of_phy_is_fixed_link(struct device_node
*np
)
392 struct device_node
*dn
;
397 dn
= of_get_child_by_name(np
, "fixed-link");
403 err
= of_property_read_string(np
, "managed", &managed
);
404 if (err
== 0 && strcmp(managed
, "auto") != 0)
408 if (of_property_count_u32_elems(np
, "fixed-link") == 5)
413 EXPORT_SYMBOL(of_phy_is_fixed_link
);
415 int of_phy_register_fixed_link(struct device_node
*np
)
417 struct fixed_phy_status status
= {};
418 struct device_node
*fixed_link_node
;
419 u32 fixed_link_prop
[5];
422 if (of_property_read_string(np
, "managed", &managed
) == 0 &&
423 strcmp(managed
, "in-band-status") == 0) {
424 /* status is zeroed, namely its .link member */
429 fixed_link_node
= of_get_child_by_name(np
, "fixed-link");
430 if (fixed_link_node
) {
432 status
.duplex
= of_property_read_bool(fixed_link_node
,
434 if (of_property_read_u32(fixed_link_node
, "speed",
436 of_node_put(fixed_link_node
);
439 status
.pause
= of_property_read_bool(fixed_link_node
, "pause");
440 status
.asym_pause
= of_property_read_bool(fixed_link_node
,
442 of_node_put(fixed_link_node
);
448 if (of_property_read_u32_array(np
, "fixed-link", fixed_link_prop
,
449 ARRAY_SIZE(fixed_link_prop
)) == 0) {
451 status
.duplex
= fixed_link_prop
[1];
452 status
.speed
= fixed_link_prop
[2];
453 status
.pause
= fixed_link_prop
[3];
454 status
.asym_pause
= fixed_link_prop
[4];
461 return PTR_ERR_OR_ZERO(fixed_phy_register(PHY_POLL
, &status
, np
));
463 EXPORT_SYMBOL(of_phy_register_fixed_link
);
465 void of_phy_deregister_fixed_link(struct device_node
*np
)
467 struct phy_device
*phydev
;
469 phydev
= of_phy_find_device(np
);
473 fixed_phy_unregister(phydev
);
475 put_device(&phydev
->mdio
.dev
); /* of_phy_find_device() */
476 phy_device_free(phydev
); /* fixed_phy_register() */
478 EXPORT_SYMBOL(of_phy_deregister_fixed_link
);