1
2
3
4
5
6
7
8
9
10
11
12#include <linux/kernel.h>
13#include <linux/device.h>
14#include <linux/netdevice.h>
15#include <linux/err.h>
16#include <linux/phy.h>
17#include <linux/of.h>
18#include <linux/of_irq.h>
19#include <linux/of_mdio.h>
20#include <linux/module.h>
21
22MODULE_AUTHOR("Grant Likely <grant.likely@secretlab.ca>");
23MODULE_LICENSE("GPL");
24
25
26
27
28
29
30
31
32
33int of_mdiobus_register(struct mii_bus *mdio, struct device_node *np)
34{
35 struct phy_device *phy;
36 struct device_node *child;
37 int rc, i;
38
39
40
41 mdio->phy_mask = ~0;
42
43
44 if (mdio->irq)
45 for (i=0; i<PHY_MAX_ADDR; i++)
46 mdio->irq[i] = PHY_POLL;
47
48
49 rc = mdiobus_register(mdio);
50 if (rc)
51 return rc;
52
53
54 for_each_child_of_node(np, child) {
55 const __be32 *paddr;
56 u32 addr;
57 int len;
58
59
60 paddr = of_get_property(child, "reg", &len);
61 if (!paddr || len < sizeof(*paddr)) {
62 dev_err(&mdio->dev, "%s has invalid PHY address\n",
63 child->full_name);
64 continue;
65 }
66
67 addr = be32_to_cpup(paddr);
68 if (addr >= 32) {
69 dev_err(&mdio->dev, "%s PHY address %i is too large\n",
70 child->full_name, addr);
71 continue;
72 }
73
74 if (mdio->irq) {
75 mdio->irq[addr] = irq_of_parse_and_map(child, 0);
76 if (!mdio->irq[addr])
77 mdio->irq[addr] = PHY_POLL;
78 }
79
80 phy = get_phy_device(mdio, addr);
81 if (!phy || IS_ERR(phy)) {
82 dev_err(&mdio->dev, "error probing PHY at address %i\n",
83 addr);
84 continue;
85 }
86 phy_scan_fixups(phy);
87
88
89
90 of_node_get(child);
91 phy->dev.of_node = child;
92
93
94 rc = phy_device_register(phy);
95 if (rc) {
96 phy_device_free(phy);
97 of_node_put(child);
98 continue;
99 }
100
101 dev_dbg(&mdio->dev, "registered phy %s at address %i\n",
102 child->name, addr);
103 }
104
105 return 0;
106}
107EXPORT_SYMBOL(of_mdiobus_register);
108
109
110static int of_phy_match(struct device *dev, void *phy_np)
111{
112 return dev->of_node == phy_np;
113}
114
115
116
117
118
119
120
121struct phy_device *of_phy_find_device(struct device_node *phy_np)
122{
123 struct device *d;
124 if (!phy_np)
125 return NULL;
126
127 d = bus_find_device(&mdio_bus_type, NULL, phy_np, of_phy_match);
128 return d ? to_phy_device(d) : NULL;
129}
130EXPORT_SYMBOL(of_phy_find_device);
131
132
133
134
135
136
137
138
139
140
141struct phy_device *of_phy_connect(struct net_device *dev,
142 struct device_node *phy_np,
143 void (*hndlr)(struct net_device *), u32 flags,
144 phy_interface_t iface)
145{
146 struct phy_device *phy = of_phy_find_device(phy_np);
147
148 if (!phy)
149 return NULL;
150
151 return phy_connect_direct(dev, phy, hndlr, flags, iface) ? NULL : phy;
152}
153EXPORT_SYMBOL(of_phy_connect);
154
155
156
157
158
159
160
161
162
163
164
165struct phy_device *of_phy_connect_fixed_link(struct net_device *dev,
166 void (*hndlr)(struct net_device *),
167 phy_interface_t iface)
168{
169 struct device_node *net_np;
170 char bus_id[MII_BUS_ID_SIZE + 3];
171 struct phy_device *phy;
172 const __be32 *phy_id;
173 int sz;
174
175 if (!dev->dev.parent)
176 return NULL;
177
178 net_np = dev->dev.parent->of_node;
179 if (!net_np)
180 return NULL;
181
182 phy_id = of_get_property(net_np, "fixed-link", &sz);
183 if (!phy_id || sz < sizeof(*phy_id))
184 return NULL;
185
186 sprintf(bus_id, PHY_ID_FMT, "0", be32_to_cpu(phy_id[0]));
187
188 phy = phy_connect(dev, bus_id, hndlr, 0, iface);
189 return IS_ERR(phy) ? NULL : phy;
190}
191EXPORT_SYMBOL(of_phy_connect_fixed_link);
192