of: provide a binding for fixed link PHYs
[pandora-kernel.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 static void of_set_phy_supported(struct phy_device *phydev, u32 max_speed)
27 {
28         /* The default values for phydev->supported are provided by the PHY
29          * driver "features" member, we want to reset to sane defaults fist
30          * before supporting higher speeds.
31          */
32         phydev->supported &= PHY_DEFAULT_FEATURES;
33
34         switch (max_speed) {
35         default:
36                 return;
37
38         case SPEED_1000:
39                 phydev->supported |= PHY_1000BT_FEATURES;
40         case SPEED_100:
41                 phydev->supported |= PHY_100BT_FEATURES;
42         case SPEED_10:
43                 phydev->supported |= PHY_10BT_FEATURES;
44         }
45 }
46
47 /* Extract the clause 22 phy ID from the compatible string of the form
48  * ethernet-phy-idAAAA.BBBB */
49 static int of_get_phy_id(struct device_node *device, u32 *phy_id)
50 {
51         struct property *prop;
52         const char *cp;
53         unsigned int upper, lower;
54
55         of_property_for_each_string(device, "compatible", prop, cp) {
56                 if (sscanf(cp, "ethernet-phy-id%4x.%4x", &upper, &lower) == 2) {
57                         *phy_id = ((upper & 0xFFFF) << 16) | (lower & 0xFFFF);
58                         return 0;
59                 }
60         }
61         return -EINVAL;
62 }
63
64 static int of_mdiobus_register_phy(struct mii_bus *mdio, struct device_node *child,
65                                    u32 addr)
66 {
67         struct phy_device *phy;
68         bool is_c45;
69         int rc;
70         u32 max_speed = 0;
71         u32 phy_id;
72
73         is_c45 = of_device_is_compatible(child,
74                                          "ethernet-phy-ieee802.3-c45");
75
76         if (!is_c45 && !of_get_phy_id(child, &phy_id))
77                 phy = phy_device_create(mdio, addr, phy_id, 0, NULL);
78         else
79                 phy = get_phy_device(mdio, addr, is_c45);
80         if (!phy || IS_ERR(phy))
81                 return 1;
82
83         rc = irq_of_parse_and_map(child, 0);
84         if (rc > 0) {
85                 phy->irq = rc;
86                 if (mdio->irq)
87                         mdio->irq[addr] = rc;
88         } else {
89                 if (mdio->irq)
90                         phy->irq = mdio->irq[addr];
91         }
92
93         /* Associate the OF node with the device structure so it
94          * can be looked up later */
95         of_node_get(child);
96         phy->dev.of_node = child;
97
98         /* All data is now stored in the phy struct;
99          * register it */
100         rc = phy_device_register(phy);
101         if (rc) {
102                 phy_device_free(phy);
103                 of_node_put(child);
104                 return 1;
105         }
106
107         /* Set phydev->supported based on the "max-speed" property
108          * if present */
109         if (!of_property_read_u32(child, "max-speed", &max_speed))
110                 of_set_phy_supported(phy, max_speed);
111
112         dev_dbg(&mdio->dev, "registered phy %s at address %i\n",
113                 child->name, addr);
114
115         return 0;
116 }
117
118 /**
119  * of_mdiobus_register - Register mii_bus and create PHYs from the device tree
120  * @mdio: pointer to mii_bus structure
121  * @np: pointer to device_node of MDIO bus.
122  *
123  * This function registers the mii_bus structure and registers a phy_device
124  * for each child node of @np.
125  */
126 int of_mdiobus_register(struct mii_bus *mdio, struct device_node *np)
127 {
128         struct device_node *child;
129         const __be32 *paddr;
130         u32 addr;
131         bool scanphys = false;
132         int rc, i, len;
133
134         /* Mask out all PHYs from auto probing.  Instead the PHYs listed in
135          * the device tree are populated after the bus has been registered */
136         mdio->phy_mask = ~0;
137
138         /* Clear all the IRQ properties */
139         if (mdio->irq)
140                 for (i=0; i<PHY_MAX_ADDR; i++)
141                         mdio->irq[i] = PHY_POLL;
142
143         mdio->dev.of_node = np;
144
145         /* Register the MDIO bus */
146         rc = mdiobus_register(mdio);
147         if (rc)
148                 return rc;
149
150         /* Loop over the child nodes and register a phy_device for each one */
151         for_each_available_child_of_node(np, child) {
152                 /* A PHY must have a reg property in the range [0-31] */
153                 paddr = of_get_property(child, "reg", &len);
154                 if (!paddr || len < sizeof(*paddr)) {
155                         scanphys = true;
156                         dev_err(&mdio->dev, "%s has invalid PHY address\n",
157                                 child->full_name);
158                         continue;
159                 }
160
161                 addr = be32_to_cpup(paddr);
162                 if (addr >= PHY_MAX_ADDR) {
163                         dev_err(&mdio->dev, "%s PHY address %i is too large\n",
164                                 child->full_name, addr);
165                         continue;
166                 }
167
168                 rc = of_mdiobus_register_phy(mdio, child, addr);
169                 if (rc)
170                         continue;
171         }
172
173         if (!scanphys)
174                 return 0;
175
176         /* auto scan for PHYs with empty reg property */
177         for_each_available_child_of_node(np, child) {
178                 /* Skip PHYs with reg property set */
179                 paddr = of_get_property(child, "reg", &len);
180                 if (paddr)
181                         continue;
182
183                 for (addr = 0; addr < PHY_MAX_ADDR; addr++) {
184                         /* skip already registered PHYs */
185                         if (mdio->phy_map[addr])
186                                 continue;
187
188                         /* be noisy to encourage people to set reg property */
189                         dev_info(&mdio->dev, "scan phy %s at address %i\n",
190                                  child->name, addr);
191
192                         rc = of_mdiobus_register_phy(mdio, child, addr);
193                         if (rc)
194                                 continue;
195                 }
196         }
197
198         return 0;
199 }
200 EXPORT_SYMBOL(of_mdiobus_register);
201
202 /* Helper function for of_phy_find_device */
203 static int of_phy_match(struct device *dev, void *phy_np)
204 {
205         return dev->of_node == phy_np;
206 }
207
208 /**
209  * of_phy_find_device - Give a PHY node, find the phy_device
210  * @phy_np: Pointer to the phy's device tree node
211  *
212  * Returns a pointer to the phy_device.
213  */
214 struct phy_device *of_phy_find_device(struct device_node *phy_np)
215 {
216         struct device *d;
217         if (!phy_np)
218                 return NULL;
219
220         d = bus_find_device(&mdio_bus_type, NULL, phy_np, of_phy_match);
221         return d ? to_phy_device(d) : NULL;
222 }
223 EXPORT_SYMBOL(of_phy_find_device);
224
225 /**
226  * of_phy_connect - Connect to the phy described in the device tree
227  * @dev: pointer to net_device claiming the phy
228  * @phy_np: Pointer to device tree node for the PHY
229  * @hndlr: Link state callback for the network device
230  * @iface: PHY data interface type
231  *
232  * Returns a pointer to the phy_device if successful.  NULL otherwise
233  */
234 struct phy_device *of_phy_connect(struct net_device *dev,
235                                   struct device_node *phy_np,
236                                   void (*hndlr)(struct net_device *), u32 flags,
237                                   phy_interface_t iface)
238 {
239         struct phy_device *phy = of_phy_find_device(phy_np);
240
241         if (!phy)
242                 return NULL;
243
244         return phy_connect_direct(dev, phy, hndlr, iface) ? NULL : phy;
245 }
246 EXPORT_SYMBOL(of_phy_connect);
247
248 /**
249  * of_phy_connect_fixed_link - Parse fixed-link property and return a dummy phy
250  * @dev: pointer to net_device claiming the phy
251  * @hndlr: Link state callback for the network device
252  * @iface: PHY data interface type
253  *
254  * This function is a temporary stop-gap and will be removed soon.  It is
255  * only to support the fs_enet, ucc_geth and gianfar Ethernet drivers.  Do
256  * not call this function from new drivers.
257  */
258 struct phy_device *of_phy_connect_fixed_link(struct net_device *dev,
259                                              void (*hndlr)(struct net_device *),
260                                              phy_interface_t iface)
261 {
262         struct device_node *net_np;
263         char bus_id[MII_BUS_ID_SIZE + 3];
264         struct phy_device *phy;
265         const __be32 *phy_id;
266         int sz;
267
268         if (!dev->dev.parent)
269                 return NULL;
270
271         net_np = dev->dev.parent->of_node;
272         if (!net_np)
273                 return NULL;
274
275         phy_id = of_get_property(net_np, "fixed-link", &sz);
276         if (!phy_id || sz < sizeof(*phy_id))
277                 return NULL;
278
279         sprintf(bus_id, PHY_ID_FMT, "fixed-0", be32_to_cpu(phy_id[0]));
280
281         phy = phy_connect(dev, bus_id, hndlr, iface);
282         return IS_ERR(phy) ? NULL : phy;
283 }
284 EXPORT_SYMBOL(of_phy_connect_fixed_link);
285
286 /**
287  * of_phy_attach - Attach to a PHY without starting the state machine
288  * @dev: pointer to net_device claiming the phy
289  * @phy_np: Node pointer for the PHY
290  * @flags: flags to pass to the PHY
291  * @iface: PHY data interface type
292  */
293 struct phy_device *of_phy_attach(struct net_device *dev,
294                                  struct device_node *phy_np, u32 flags,
295                                  phy_interface_t iface)
296 {
297         struct phy_device *phy = of_phy_find_device(phy_np);
298
299         if (!phy)
300                 return NULL;
301
302         return phy_attach_direct(dev, phy, flags, iface) ? NULL : phy;
303 }
304 EXPORT_SYMBOL(of_phy_attach);
305
306 #if defined(CONFIG_FIXED_PHY)
307 /*
308  * of_phy_is_fixed_link() and of_phy_register_fixed_link() must
309  * support two DT bindings:
310  * - the old DT binding, where 'fixed-link' was a property with 5
311  *   cells encoding various informations about the fixed PHY
312  * - the new DT binding, where 'fixed-link' is a sub-node of the
313  *   Ethernet device.
314  */
315 bool of_phy_is_fixed_link(struct device_node *np)
316 {
317         struct device_node *dn;
318         int len;
319
320         /* New binding */
321         dn = of_get_child_by_name(np, "fixed-link");
322         if (dn) {
323                 of_node_put(dn);
324                 return true;
325         }
326
327         /* Old binding */
328         if (of_get_property(np, "fixed-link", &len) &&
329             len == (5 * sizeof(__be32)))
330                 return true;
331
332         return false;
333 }
334 EXPORT_SYMBOL(of_phy_is_fixed_link);
335
336 int of_phy_register_fixed_link(struct device_node *np)
337 {
338         struct fixed_phy_status status = {};
339         struct device_node *fixed_link_node;
340         const __be32 *fixed_link_prop;
341         int len;
342
343         /* New binding */
344         fixed_link_node = of_get_child_by_name(np, "fixed-link");
345         if (fixed_link_node) {
346                 status.link = 1;
347                 status.duplex = of_property_read_bool(np, "full-duplex");
348                 if (of_property_read_u32(fixed_link_node, "speed", &status.speed))
349                         return -EINVAL;
350                 status.pause = of_property_read_bool(np, "pause");
351                 status.asym_pause = of_property_read_bool(np, "asym-pause");
352                 of_node_put(fixed_link_node);
353                 return fixed_phy_register(PHY_POLL, &status, np);
354         }
355
356         /* Old binding */
357         fixed_link_prop = of_get_property(np, "fixed-link", &len);
358         if (fixed_link_prop && len == (5 * sizeof(__be32))) {
359                 status.link = 1;
360                 status.duplex = be32_to_cpu(fixed_link_prop[1]);
361                 status.speed = be32_to_cpu(fixed_link_prop[2]);
362                 status.pause = be32_to_cpu(fixed_link_prop[3]);
363                 status.asym_pause = be32_to_cpu(fixed_link_prop[4]);
364                 return fixed_phy_register(PHY_POLL, &status, np);
365         }
366
367         return -ENODEV;
368 }
369 EXPORT_SYMBOL(of_phy_register_fixed_link);
370 #endif