4c1e01ed16dc04f9d082e47b19a4cdeb2b9a9cbe
[linux-2.6-microblaze.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_attach - Attach to a PHY without starting the state machine
250  * @dev: pointer to net_device claiming the phy
251  * @phy_np: Node pointer for the PHY
252  * @flags: flags to pass to the PHY
253  * @iface: PHY data interface type
254  */
255 struct phy_device *of_phy_attach(struct net_device *dev,
256                                  struct device_node *phy_np, u32 flags,
257                                  phy_interface_t iface)
258 {
259         struct phy_device *phy = of_phy_find_device(phy_np);
260
261         if (!phy)
262                 return NULL;
263
264         return phy_attach_direct(dev, phy, flags, iface) ? NULL : phy;
265 }
266 EXPORT_SYMBOL(of_phy_attach);
267
268 #if defined(CONFIG_FIXED_PHY)
269 /*
270  * of_phy_is_fixed_link() and of_phy_register_fixed_link() must
271  * support two DT bindings:
272  * - the old DT binding, where 'fixed-link' was a property with 5
273  *   cells encoding various informations about the fixed PHY
274  * - the new DT binding, where 'fixed-link' is a sub-node of the
275  *   Ethernet device.
276  */
277 bool of_phy_is_fixed_link(struct device_node *np)
278 {
279         struct device_node *dn;
280         int len;
281
282         /* New binding */
283         dn = of_get_child_by_name(np, "fixed-link");
284         if (dn) {
285                 of_node_put(dn);
286                 return true;
287         }
288
289         /* Old binding */
290         if (of_get_property(np, "fixed-link", &len) &&
291             len == (5 * sizeof(__be32)))
292                 return true;
293
294         return false;
295 }
296 EXPORT_SYMBOL(of_phy_is_fixed_link);
297
298 int of_phy_register_fixed_link(struct device_node *np)
299 {
300         struct fixed_phy_status status = {};
301         struct device_node *fixed_link_node;
302         const __be32 *fixed_link_prop;
303         int len;
304
305         /* New binding */
306         fixed_link_node = of_get_child_by_name(np, "fixed-link");
307         if (fixed_link_node) {
308                 status.link = 1;
309                 status.duplex = of_property_read_bool(np, "full-duplex");
310                 if (of_property_read_u32(fixed_link_node, "speed", &status.speed))
311                         return -EINVAL;
312                 status.pause = of_property_read_bool(np, "pause");
313                 status.asym_pause = of_property_read_bool(np, "asym-pause");
314                 of_node_put(fixed_link_node);
315                 return fixed_phy_register(PHY_POLL, &status, np);
316         }
317
318         /* Old binding */
319         fixed_link_prop = of_get_property(np, "fixed-link", &len);
320         if (fixed_link_prop && len == (5 * sizeof(__be32))) {
321                 status.link = 1;
322                 status.duplex = be32_to_cpu(fixed_link_prop[1]);
323                 status.speed = be32_to_cpu(fixed_link_prop[2]);
324                 status.pause = be32_to_cpu(fixed_link_prop[3]);
325                 status.asym_pause = be32_to_cpu(fixed_link_prop[4]);
326                 return fixed_phy_register(PHY_POLL, &status, np);
327         }
328
329         return -ENODEV;
330 }
331 EXPORT_SYMBOL(of_phy_register_fixed_link);
332 #endif