1
2
3
4
5
6
7
8
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
25
26MODULE_AUTHOR("Grant Likely <grant.likely@secretlab.ca>");
27MODULE_LICENSE("GPL");
28
29
30
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
63
64
65 fwnode_handle_get(fwnode);
66 device_set_node(&mdiodev->dev, fwnode);
67
68
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
82
83
84
85
86
87static 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
104
105
106
107
108
109
110
111
112
113bool 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}
138EXPORT_SYMBOL(of_mdiobus_child_is_phy);
139
140
141
142
143
144
145
146
147
148int 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
158 if (!of_device_is_available(np))
159 return -ENODEV;
160
161
162
163 mdio->phy_mask = ~0;
164
165 device_set_node(&mdio->dev, of_fwnode_handle(np));
166
167
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
174 rc = mdiobus_register(mdio);
175 if (rc)
176 return rc;
177
178
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
203 for_each_available_child_of_node(np, child) {
204
205 if (of_find_property(child, "reg", NULL))
206 continue;
207
208 for (addr = 0; addr < PHY_MAX_ADDR; addr++) {
209
210 if (mdiobus_is_registered_device(mdio, addr))
211 continue;
212
213
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
219
220
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
233unregister:
234 mdiobus_unregister(mdio);
235 return rc;
236}
237EXPORT_SYMBOL(of_mdiobus_register);
238
239
240
241
242
243
244
245
246
247struct mdio_device *of_mdio_find_device(struct device_node *np)
248{
249 return fwnode_mdio_find_device(of_fwnode_handle(np));
250}
251EXPORT_SYMBOL(of_mdio_find_device);
252
253
254
255
256
257
258
259
260struct phy_device *of_phy_find_device(struct device_node *phy_np)
261{
262 return fwnode_phy_find_device(of_fwnode_handle(phy_np));
263}
264EXPORT_SYMBOL(of_phy_find_device);
265
266
267
268
269
270
271
272
273
274
275
276
277
278struct phy_device *of_phy_connect(struct net_device *dev,
279 struct device_node *phy_np,
280 void (*hndlr)(struct net_device *), u32 flags,
281 phy_interface_t iface)
282{
283 struct phy_device *phy = of_phy_find_device(phy_np);
284 int ret;
285
286 if (!phy)
287 return NULL;
288
289 phy->dev_flags |= flags;
290
291 ret = phy_connect_direct(dev, phy, hndlr, iface);
292
293
294 put_device(&phy->mdio.dev);
295
296 return ret ? NULL : phy;
297}
298EXPORT_SYMBOL(of_phy_connect);
299
300
301
302
303
304
305
306
307
308
309
310
311struct phy_device *of_phy_get_and_connect(struct net_device *dev,
312 struct device_node *np,
313 void (*hndlr)(struct net_device *))
314{
315 phy_interface_t iface;
316 struct device_node *phy_np;
317 struct phy_device *phy;
318 int ret;
319
320 ret = of_get_phy_mode(np, &iface);
321 if (ret)
322 return NULL;
323 if (of_phy_is_fixed_link(np)) {
324 ret = of_phy_register_fixed_link(np);
325 if (ret < 0) {
326 netdev_err(dev, "broken fixed-link specification\n");
327 return NULL;
328 }
329 phy_np = of_node_get(np);
330 } else {
331 phy_np = of_parse_phandle(np, "phy-handle", 0);
332 if (!phy_np)
333 return NULL;
334 }
335
336 phy = of_phy_connect(dev, phy_np, hndlr, 0, iface);
337
338 of_node_put(phy_np);
339
340 return phy;
341}
342EXPORT_SYMBOL(of_phy_get_and_connect);
343
344
345
346
347
348
349
350
351
352bool of_phy_is_fixed_link(struct device_node *np)
353{
354 struct device_node *dn;
355 int len, err;
356 const char *managed;
357
358
359 dn = of_get_child_by_name(np, "fixed-link");
360 if (dn) {
361 of_node_put(dn);
362 return true;
363 }
364
365 err = of_property_read_string(np, "managed", &managed);
366 if (err == 0 && strcmp(managed, "auto") != 0)
367 return true;
368
369
370 if (of_get_property(np, "fixed-link", &len) &&
371 len == (5 * sizeof(__be32)))
372 return true;
373
374 return false;
375}
376EXPORT_SYMBOL(of_phy_is_fixed_link);
377
378int of_phy_register_fixed_link(struct device_node *np)
379{
380 struct fixed_phy_status status = {};
381 struct device_node *fixed_link_node;
382 u32 fixed_link_prop[5];
383 const char *managed;
384
385 if (of_property_read_string(np, "managed", &managed) == 0 &&
386 strcmp(managed, "in-band-status") == 0) {
387
388 goto register_phy;
389 }
390
391
392 fixed_link_node = of_get_child_by_name(np, "fixed-link");
393 if (fixed_link_node) {
394 status.link = 1;
395 status.duplex = of_property_read_bool(fixed_link_node,
396 "full-duplex");
397 if (of_property_read_u32(fixed_link_node, "speed",
398 &status.speed)) {
399 of_node_put(fixed_link_node);
400 return -EINVAL;
401 }
402 status.pause = of_property_read_bool(fixed_link_node, "pause");
403 status.asym_pause = of_property_read_bool(fixed_link_node,
404 "asym-pause");
405 of_node_put(fixed_link_node);
406
407 goto register_phy;
408 }
409
410
411 if (of_property_read_u32_array(np, "fixed-link", fixed_link_prop,
412 ARRAY_SIZE(fixed_link_prop)) == 0) {
413 status.link = 1;
414 status.duplex = fixed_link_prop[1];
415 status.speed = fixed_link_prop[2];
416 status.pause = fixed_link_prop[3];
417 status.asym_pause = fixed_link_prop[4];
418 goto register_phy;
419 }
420
421 return -ENODEV;
422
423register_phy:
424 return PTR_ERR_OR_ZERO(fixed_phy_register(PHY_POLL, &status, np));
425}
426EXPORT_SYMBOL(of_phy_register_fixed_link);
427
428void of_phy_deregister_fixed_link(struct device_node *np)
429{
430 struct phy_device *phydev;
431
432 phydev = of_phy_find_device(np);
433 if (!phydev)
434 return;
435
436 fixed_phy_unregister(phydev);
437
438 put_device(&phydev->mdio.dev);
439 phy_device_free(phydev);
440}
441EXPORT_SYMBOL(of_phy_deregister_fixed_link);
442