Merge branch 'pci/resource' into next
[deliverable/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/of.h>
18 #include <linux/of_irq.h>
19 #include <linux/of_mdio.h>
20 #include <linux/module.h>
21
22 MODULE_AUTHOR("Grant Likely <grant.likely@secretlab.ca>");
23 MODULE_LICENSE("GPL");
24
25 static void of_set_phy_supported(struct phy_device *phydev, u32 max_speed)
26 {
27 phydev->supported |= PHY_DEFAULT_FEATURES;
28
29 switch (max_speed) {
30 default:
31 return;
32
33 case SPEED_1000:
34 phydev->supported |= PHY_1000BT_FEATURES;
35 case SPEED_100:
36 phydev->supported |= PHY_100BT_FEATURES;
37 case SPEED_10:
38 phydev->supported |= PHY_10BT_FEATURES;
39 }
40 }
41
42 static int of_mdiobus_register_phy(struct mii_bus *mdio, struct device_node *child,
43 u32 addr)
44 {
45 struct phy_device *phy;
46 bool is_c45;
47 int rc, prev_irq;
48 u32 max_speed = 0;
49
50 is_c45 = of_device_is_compatible(child,
51 "ethernet-phy-ieee802.3-c45");
52
53 phy = get_phy_device(mdio, addr, is_c45);
54 if (!phy || IS_ERR(phy))
55 return 1;
56
57 if (mdio->irq) {
58 prev_irq = mdio->irq[addr];
59 mdio->irq[addr] =
60 irq_of_parse_and_map(child, 0);
61 if (!mdio->irq[addr])
62 mdio->irq[addr] = prev_irq;
63 }
64
65 /* Associate the OF node with the device structure so it
66 * can be looked up later */
67 of_node_get(child);
68 phy->dev.of_node = child;
69
70 /* All data is now stored in the phy struct;
71 * register it */
72 rc = phy_device_register(phy);
73 if (rc) {
74 phy_device_free(phy);
75 of_node_put(child);
76 return 1;
77 }
78
79 /* Set phydev->supported based on the "max-speed" property
80 * if present */
81 if (!of_property_read_u32(child, "max-speed", &max_speed))
82 of_set_phy_supported(phy, max_speed);
83
84 dev_dbg(&mdio->dev, "registered phy %s at address %i\n",
85 child->name, addr);
86
87 return 0;
88 }
89
90 /**
91 * of_mdiobus_register - Register mii_bus and create PHYs from the device tree
92 * @mdio: pointer to mii_bus structure
93 * @np: pointer to device_node of MDIO bus.
94 *
95 * This function registers the mii_bus structure and registers a phy_device
96 * for each child node of @np.
97 */
98 int of_mdiobus_register(struct mii_bus *mdio, struct device_node *np)
99 {
100 struct device_node *child;
101 const __be32 *paddr;
102 u32 addr;
103 bool scanphys = false;
104 int rc, i, len;
105
106 /* Mask out all PHYs from auto probing. Instead the PHYs listed in
107 * the device tree are populated after the bus has been registered */
108 mdio->phy_mask = ~0;
109
110 /* Clear all the IRQ properties */
111 if (mdio->irq)
112 for (i=0; i<PHY_MAX_ADDR; i++)
113 mdio->irq[i] = PHY_POLL;
114
115 mdio->dev.of_node = np;
116
117 /* Register the MDIO bus */
118 rc = mdiobus_register(mdio);
119 if (rc)
120 return rc;
121
122 /* Loop over the child nodes and register a phy_device for each one */
123 for_each_available_child_of_node(np, child) {
124 /* A PHY must have a reg property in the range [0-31] */
125 paddr = of_get_property(child, "reg", &len);
126 if (!paddr || len < sizeof(*paddr)) {
127 scanphys = true;
128 dev_err(&mdio->dev, "%s has invalid PHY address\n",
129 child->full_name);
130 continue;
131 }
132
133 addr = be32_to_cpup(paddr);
134 if (addr >= PHY_MAX_ADDR) {
135 dev_err(&mdio->dev, "%s PHY address %i is too large\n",
136 child->full_name, addr);
137 continue;
138 }
139
140 rc = of_mdiobus_register_phy(mdio, child, addr);
141 if (rc)
142 continue;
143 }
144
145 if (!scanphys)
146 return 0;
147
148 /* auto scan for PHYs with empty reg property */
149 for_each_available_child_of_node(np, child) {
150 /* Skip PHYs with reg property set */
151 paddr = of_get_property(child, "reg", &len);
152 if (paddr)
153 continue;
154
155 for (addr = 0; addr < PHY_MAX_ADDR; addr++) {
156 /* skip already registered PHYs */
157 if (mdio->phy_map[addr])
158 continue;
159
160 /* be noisy to encourage people to set reg property */
161 dev_info(&mdio->dev, "scan phy %s at address %i\n",
162 child->name, addr);
163
164 rc = of_mdiobus_register_phy(mdio, child, addr);
165 if (rc)
166 continue;
167 }
168 }
169
170 return 0;
171 }
172 EXPORT_SYMBOL(of_mdiobus_register);
173
174 /* Helper function for of_phy_find_device */
175 static int of_phy_match(struct device *dev, void *phy_np)
176 {
177 return dev->of_node == phy_np;
178 }
179
180 /**
181 * of_phy_find_device - Give a PHY node, find the phy_device
182 * @phy_np: Pointer to the phy's device tree node
183 *
184 * Returns a pointer to the phy_device.
185 */
186 struct phy_device *of_phy_find_device(struct device_node *phy_np)
187 {
188 struct device *d;
189 if (!phy_np)
190 return NULL;
191
192 d = bus_find_device(&mdio_bus_type, NULL, phy_np, of_phy_match);
193 return d ? to_phy_device(d) : NULL;
194 }
195 EXPORT_SYMBOL(of_phy_find_device);
196
197 /**
198 * of_phy_connect - Connect to the phy described in the device tree
199 * @dev: pointer to net_device claiming the phy
200 * @phy_np: Pointer to device tree node for the PHY
201 * @hndlr: Link state callback for the network device
202 * @iface: PHY data interface type
203 *
204 * Returns a pointer to the phy_device if successful. NULL otherwise
205 */
206 struct phy_device *of_phy_connect(struct net_device *dev,
207 struct device_node *phy_np,
208 void (*hndlr)(struct net_device *), u32 flags,
209 phy_interface_t iface)
210 {
211 struct phy_device *phy = of_phy_find_device(phy_np);
212
213 if (!phy)
214 return NULL;
215
216 return phy_connect_direct(dev, phy, hndlr, iface) ? NULL : phy;
217 }
218 EXPORT_SYMBOL(of_phy_connect);
219
220 /**
221 * of_phy_connect_fixed_link - Parse fixed-link property and return a dummy phy
222 * @dev: pointer to net_device claiming the phy
223 * @hndlr: Link state callback for the network device
224 * @iface: PHY data interface type
225 *
226 * This function is a temporary stop-gap and will be removed soon. It is
227 * only to support the fs_enet, ucc_geth and gianfar Ethernet drivers. Do
228 * not call this function from new drivers.
229 */
230 struct phy_device *of_phy_connect_fixed_link(struct net_device *dev,
231 void (*hndlr)(struct net_device *),
232 phy_interface_t iface)
233 {
234 struct device_node *net_np;
235 char bus_id[MII_BUS_ID_SIZE + 3];
236 struct phy_device *phy;
237 const __be32 *phy_id;
238 int sz;
239
240 if (!dev->dev.parent)
241 return NULL;
242
243 net_np = dev->dev.parent->of_node;
244 if (!net_np)
245 return NULL;
246
247 phy_id = of_get_property(net_np, "fixed-link", &sz);
248 if (!phy_id || sz < sizeof(*phy_id))
249 return NULL;
250
251 sprintf(bus_id, PHY_ID_FMT, "fixed-0", be32_to_cpu(phy_id[0]));
252
253 phy = phy_connect(dev, bus_id, hndlr, iface);
254 return IS_ERR(phy) ? NULL : phy;
255 }
256 EXPORT_SYMBOL(of_phy_connect_fixed_link);
257
258 /**
259 * of_phy_attach - Attach to a PHY without starting the state machine
260 * @dev: pointer to net_device claiming the phy
261 * @phy_np: Node pointer for the PHY
262 * @flags: flags to pass to the PHY
263 * @iface: PHY data interface type
264 */
265 struct phy_device *of_phy_attach(struct net_device *dev,
266 struct device_node *phy_np, u32 flags,
267 phy_interface_t iface)
268 {
269 struct phy_device *phy = of_phy_find_device(phy_np);
270
271 if (!phy)
272 return NULL;
273
274 return phy_attach_direct(dev, phy, flags, iface) ? NULL : phy;
275 }
276 EXPORT_SYMBOL(of_phy_attach);
This page took 0.037683 seconds and 5 git commands to generate.