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