]> git.karo-electronics.de Git - karo-tx-linux.git/blob - drivers/of/of_mdio.c
Merge branch 'master' of git://git.kernel.org/pub/scm/linux/kernel/git/klassert/ipsec...
[karo-tx-linux.git] / drivers / of / of_mdio.c
1 /*
2  * OF helpers for the MDIO (Ethernet PHY) API
3  *
4  * Copyright (c) 2009 Secret Lab Technologies, Ltd.
5  *
6  * This file is released under the GPLv2
7  *
8  * This file provides helper functions for extracting PHY device information
9  * out of the OpenFirmware device tree and using it to populate an mii_bus.
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/phy_fixed.h>
18 #include <linux/of.h>
19 #include <linux/of_irq.h>
20 #include <linux/of_mdio.h>
21 #include <linux/module.h>
22
23 MODULE_AUTHOR("Grant Likely <grant.likely@secretlab.ca>");
24 MODULE_LICENSE("GPL");
25
26 /* Extract the clause 22 phy ID from the compatible string of the form
27  * ethernet-phy-idAAAA.BBBB */
28 static int of_get_phy_id(struct device_node *device, u32 *phy_id)
29 {
30         struct property *prop;
31         const char *cp;
32         unsigned int upper, lower;
33
34         of_property_for_each_string(device, "compatible", prop, cp) {
35                 if (sscanf(cp, "ethernet-phy-id%4x.%4x", &upper, &lower) == 2) {
36                         *phy_id = ((upper & 0xFFFF) << 16) | (lower & 0xFFFF);
37                         return 0;
38                 }
39         }
40         return -EINVAL;
41 }
42
43 static int of_mdiobus_register_phy(struct mii_bus *mdio, struct device_node *child,
44                                    u32 addr)
45 {
46         struct phy_device *phy;
47         bool is_c45;
48         int rc;
49         u32 max_speed = 0;
50         u32 phy_id;
51
52         is_c45 = of_device_is_compatible(child,
53                                          "ethernet-phy-ieee802.3-c45");
54
55         if (!is_c45 && !of_get_phy_id(child, &phy_id))
56                 phy = phy_device_create(mdio, addr, phy_id, 0, NULL);
57         else
58                 phy = get_phy_device(mdio, addr, is_c45);
59         if (!phy || IS_ERR(phy))
60                 return 1;
61
62         rc = irq_of_parse_and_map(child, 0);
63         if (rc > 0) {
64                 phy->irq = rc;
65                 if (mdio->irq)
66                         mdio->irq[addr] = rc;
67         } else {
68                 if (mdio->irq)
69                         phy->irq = mdio->irq[addr];
70         }
71
72         /* Associate the OF node with the device structure so it
73          * can be looked up later */
74         of_node_get(child);
75         phy->dev.of_node = child;
76
77         /* All data is now stored in the phy struct;
78          * register it */
79         rc = phy_device_register(phy);
80         if (rc) {
81                 phy_device_free(phy);
82                 of_node_put(child);
83                 return 1;
84         }
85
86         dev_dbg(&mdio->dev, "registered phy %s at address %i\n",
87                 child->name, addr);
88
89         return 0;
90 }
91
92 /**
93  * of_mdiobus_register - Register mii_bus and create PHYs from the device tree
94  * @mdio: pointer to mii_bus structure
95  * @np: pointer to device_node of MDIO bus.
96  *
97  * This function registers the mii_bus structure and registers a phy_device
98  * for each child node of @np.
99  */
100 int of_mdiobus_register(struct mii_bus *mdio, struct device_node *np)
101 {
102         struct device_node *child;
103         const __be32 *paddr;
104         u32 addr;
105         bool scanphys = false;
106         int rc, i, len;
107
108         /* Mask out all PHYs from auto probing.  Instead the PHYs listed in
109          * the device tree are populated after the bus has been registered */
110         mdio->phy_mask = ~0;
111
112         /* Clear all the IRQ properties */
113         if (mdio->irq)
114                 for (i=0; i<PHY_MAX_ADDR; i++)
115                         mdio->irq[i] = PHY_POLL;
116
117         mdio->dev.of_node = np;
118
119         /* Register the MDIO bus */
120         rc = mdiobus_register(mdio);
121         if (rc)
122                 return rc;
123
124         /* Loop over the child nodes and register a phy_device for each one */
125         for_each_available_child_of_node(np, child) {
126                 /* A PHY must have a reg property in the range [0-31] */
127                 paddr = of_get_property(child, "reg", &len);
128                 if (!paddr || len < sizeof(*paddr)) {
129                         scanphys = true;
130                         dev_err(&mdio->dev, "%s has invalid PHY address\n",
131                                 child->full_name);
132                         continue;
133                 }
134
135                 addr = be32_to_cpup(paddr);
136                 if (addr >= PHY_MAX_ADDR) {
137                         dev_err(&mdio->dev, "%s PHY address %i is too large\n",
138                                 child->full_name, addr);
139                         continue;
140                 }
141
142                 rc = of_mdiobus_register_phy(mdio, child, addr);
143                 if (rc)
144                         continue;
145         }
146
147         if (!scanphys)
148                 return 0;
149
150         /* auto scan for PHYs with empty reg property */
151         for_each_available_child_of_node(np, child) {
152                 /* Skip PHYs with reg property set */
153                 paddr = of_get_property(child, "reg", &len);
154                 if (paddr)
155                         continue;
156
157                 for (addr = 0; addr < PHY_MAX_ADDR; addr++) {
158                         /* skip already registered PHYs */
159                         if (mdio->phy_map[addr])
160                                 continue;
161
162                         /* be noisy to encourage people to set reg property */
163                         dev_info(&mdio->dev, "scan phy %s at address %i\n",
164                                  child->name, addr);
165
166                         rc = of_mdiobus_register_phy(mdio, child, addr);
167                         if (rc)
168                                 continue;
169                 }
170         }
171
172         return 0;
173 }
174 EXPORT_SYMBOL(of_mdiobus_register);
175
176 /* Helper function for of_phy_find_device */
177 static int of_phy_match(struct device *dev, void *phy_np)
178 {
179         return dev->of_node == phy_np;
180 }
181
182 /**
183  * of_phy_find_device - Give a PHY node, find the phy_device
184  * @phy_np: Pointer to the phy's device tree node
185  *
186  * Returns a pointer to the phy_device.
187  */
188 struct phy_device *of_phy_find_device(struct device_node *phy_np)
189 {
190         struct device *d;
191         if (!phy_np)
192                 return NULL;
193
194         d = bus_find_device(&mdio_bus_type, NULL, phy_np, of_phy_match);
195         return d ? to_phy_device(d) : NULL;
196 }
197 EXPORT_SYMBOL(of_phy_find_device);
198
199 /**
200  * of_phy_connect - Connect to the phy described in the device tree
201  * @dev: pointer to net_device claiming the phy
202  * @phy_np: Pointer to device tree node for the PHY
203  * @hndlr: Link state callback for the network device
204  * @iface: PHY data interface type
205  *
206  * Returns a pointer to the phy_device if successful.  NULL otherwise
207  */
208 struct phy_device *of_phy_connect(struct net_device *dev,
209                                   struct device_node *phy_np,
210                                   void (*hndlr)(struct net_device *), u32 flags,
211                                   phy_interface_t iface)
212 {
213         struct phy_device *phy = of_phy_find_device(phy_np);
214
215         if (!phy)
216                 return NULL;
217
218         return phy_connect_direct(dev, phy, hndlr, iface) ? NULL : phy;
219 }
220 EXPORT_SYMBOL(of_phy_connect);
221
222 /**
223  * of_phy_attach - Attach to a PHY without starting the state machine
224  * @dev: pointer to net_device claiming the phy
225  * @phy_np: Node pointer for the PHY
226  * @flags: flags to pass to the PHY
227  * @iface: PHY data interface type
228  */
229 struct phy_device *of_phy_attach(struct net_device *dev,
230                                  struct device_node *phy_np, u32 flags,
231                                  phy_interface_t iface)
232 {
233         struct phy_device *phy = of_phy_find_device(phy_np);
234
235         if (!phy)
236                 return NULL;
237
238         return phy_attach_direct(dev, phy, flags, iface) ? NULL : phy;
239 }
240 EXPORT_SYMBOL(of_phy_attach);
241
242 #if defined(CONFIG_FIXED_PHY)
243 /*
244  * of_phy_is_fixed_link() and of_phy_register_fixed_link() must
245  * support two DT bindings:
246  * - the old DT binding, where 'fixed-link' was a property with 5
247  *   cells encoding various informations about the fixed PHY
248  * - the new DT binding, where 'fixed-link' is a sub-node of the
249  *   Ethernet device.
250  */
251 bool of_phy_is_fixed_link(struct device_node *np)
252 {
253         struct device_node *dn;
254         int len;
255
256         /* New binding */
257         dn = of_get_child_by_name(np, "fixed-link");
258         if (dn) {
259                 of_node_put(dn);
260                 return true;
261         }
262
263         /* Old binding */
264         if (of_get_property(np, "fixed-link", &len) &&
265             len == (5 * sizeof(__be32)))
266                 return true;
267
268         return false;
269 }
270 EXPORT_SYMBOL(of_phy_is_fixed_link);
271
272 int of_phy_register_fixed_link(struct device_node *np)
273 {
274         struct fixed_phy_status status = {};
275         struct device_node *fixed_link_node;
276         const __be32 *fixed_link_prop;
277         int len;
278
279         /* New binding */
280         fixed_link_node = of_get_child_by_name(np, "fixed-link");
281         if (fixed_link_node) {
282                 status.link = 1;
283                 status.duplex = of_property_read_bool(np, "full-duplex");
284                 if (of_property_read_u32(fixed_link_node, "speed", &status.speed))
285                         return -EINVAL;
286                 status.pause = of_property_read_bool(np, "pause");
287                 status.asym_pause = of_property_read_bool(np, "asym-pause");
288                 of_node_put(fixed_link_node);
289                 return fixed_phy_register(PHY_POLL, &status, np);
290         }
291
292         /* Old binding */
293         fixed_link_prop = of_get_property(np, "fixed-link", &len);
294         if (fixed_link_prop && len == (5 * sizeof(__be32))) {
295                 status.link = 1;
296                 status.duplex = be32_to_cpu(fixed_link_prop[1]);
297                 status.speed = be32_to_cpu(fixed_link_prop[2]);
298                 status.pause = be32_to_cpu(fixed_link_prop[3]);
299                 status.asym_pause = be32_to_cpu(fixed_link_prop[4]);
300                 return fixed_phy_register(PHY_POLL, &status, np);
301         }
302
303         return -ENODEV;
304 }
305 EXPORT_SYMBOL(of_phy_register_fixed_link);
306 #endif