Commit | Line | Data |
---|---|---|
00db8189 AF |
1 | /* |
2 | * drivers/net/phy/mdio_bus.c | |
3 | * | |
4 | * MDIO Bus interface | |
5 | * | |
6 | * Author: Andy Fleming | |
7 | * | |
8 | * Copyright (c) 2004 Freescale Semiconductor, Inc. | |
9 | * | |
10 | * This program is free software; you can redistribute it and/or modify it | |
11 | * under the terms of the GNU General Public License as published by the | |
12 | * Free Software Foundation; either version 2 of the License, or (at your | |
13 | * option) any later version. | |
14 | * | |
15 | */ | |
00db8189 | 16 | #include <linux/kernel.h> |
00db8189 AF |
17 | #include <linux/string.h> |
18 | #include <linux/errno.h> | |
19 | #include <linux/unistd.h> | |
20 | #include <linux/slab.h> | |
21 | #include <linux/interrupt.h> | |
22 | #include <linux/init.h> | |
23 | #include <linux/delay.h> | |
24 | #include <linux/netdevice.h> | |
25 | #include <linux/etherdevice.h> | |
26 | #include <linux/skbuff.h> | |
27 | #include <linux/spinlock.h> | |
28 | #include <linux/mm.h> | |
29 | #include <linux/module.h> | |
00db8189 AF |
30 | #include <linux/mii.h> |
31 | #include <linux/ethtool.h> | |
32 | #include <linux/phy.h> | |
33 | ||
34 | #include <asm/io.h> | |
35 | #include <asm/irq.h> | |
36 | #include <asm/uaccess.h> | |
37 | ||
298cf9be LB |
38 | /** |
39 | * mdiobus_alloc - allocate a mii_bus structure | |
40 | * | |
41 | * Description: called by a bus driver to allocate an mii_bus | |
42 | * structure to fill in. | |
43 | */ | |
44 | struct mii_bus *mdiobus_alloc(void) | |
45 | { | |
46abc021 LB |
46 | struct mii_bus *bus; |
47 | ||
48 | bus = kzalloc(sizeof(*bus), GFP_KERNEL); | |
49 | if (bus != NULL) | |
50 | bus->state = MDIOBUS_ALLOCATED; | |
51 | ||
52 | return bus; | |
298cf9be LB |
53 | } |
54 | EXPORT_SYMBOL(mdiobus_alloc); | |
55 | ||
46abc021 LB |
56 | /** |
57 | * mdiobus_release - mii_bus device release callback | |
58 | * | |
59 | * Description: called when the last reference to an mii_bus is | |
60 | * dropped, to free the underlying memory. | |
61 | */ | |
62 | static void mdiobus_release(struct device *d) | |
63 | { | |
64 | struct mii_bus *bus = to_mii_bus(d); | |
65 | BUG_ON(bus->state != MDIOBUS_RELEASED); | |
66 | kfree(bus); | |
67 | } | |
68 | ||
69 | static struct class mdio_bus_class = { | |
70 | .name = "mdio_bus", | |
71 | .dev_release = mdiobus_release, | |
72 | }; | |
73 | ||
b3df0da8 RD |
74 | /** |
75 | * mdiobus_register - bring up all the PHYs on a given bus and attach them to bus | |
76 | * @bus: target mii_bus | |
e1393456 | 77 | * |
b3df0da8 RD |
78 | * Description: Called by a bus driver to bring up all the PHYs |
79 | * on a given bus, and attach them to the bus. | |
80 | * | |
81 | * Returns 0 on success or < 0 on error. | |
e1393456 AF |
82 | */ |
83 | int mdiobus_register(struct mii_bus *bus) | |
84 | { | |
85 | int i; | |
86 | int err = 0; | |
87 | ||
e1393456 AF |
88 | if (NULL == bus || NULL == bus->name || |
89 | NULL == bus->read || | |
90 | NULL == bus->write) | |
91 | return -EINVAL; | |
92 | ||
46abc021 LB |
93 | BUG_ON(bus->state != MDIOBUS_ALLOCATED && |
94 | bus->state != MDIOBUS_UNREGISTERED); | |
95 | ||
96 | bus->dev.parent = bus->parent; | |
97 | bus->dev.class = &mdio_bus_class; | |
98 | bus->dev.groups = NULL; | |
99 | memcpy(bus->dev.bus_id, bus->id, MII_BUS_ID_SIZE); | |
100 | ||
101 | err = device_register(&bus->dev); | |
102 | if (err) { | |
103 | printk(KERN_ERR "mii_bus %s failed to register\n", bus->id); | |
104 | return -EINVAL; | |
105 | } | |
106 | ||
107 | bus->state = MDIOBUS_REGISTERED; | |
108 | ||
d1e7fe4d AB |
109 | mutex_init(&bus->mdio_lock); |
110 | ||
e1393456 AF |
111 | if (bus->reset) |
112 | bus->reset(bus); | |
113 | ||
114 | for (i = 0; i < PHY_MAX_ADDR; i++) { | |
4fd5f812 LB |
115 | bus->phy_map[i] = NULL; |
116 | if ((bus->phy_mask & (1 << i)) == 0) { | |
117 | struct phy_device *phydev; | |
e1393456 | 118 | |
4fd5f812 LB |
119 | phydev = mdiobus_scan(bus, i); |
120 | if (IS_ERR(phydev)) | |
121 | err = PTR_ERR(phydev); | |
64b1c2b4 | 122 | } |
e1393456 AF |
123 | } |
124 | ||
125 | pr_info("%s: probed\n", bus->name); | |
126 | ||
127 | return err; | |
128 | } | |
129 | EXPORT_SYMBOL(mdiobus_register); | |
130 | ||
131 | void mdiobus_unregister(struct mii_bus *bus) | |
132 | { | |
133 | int i; | |
134 | ||
46abc021 LB |
135 | BUG_ON(bus->state != MDIOBUS_REGISTERED); |
136 | bus->state = MDIOBUS_UNREGISTERED; | |
137 | ||
138 | device_unregister(&bus->dev); | |
e1393456 | 139 | for (i = 0; i < PHY_MAX_ADDR; i++) { |
6f4a7f41 | 140 | if (bus->phy_map[i]) |
e1393456 | 141 | device_unregister(&bus->phy_map[i]->dev); |
e1393456 AF |
142 | } |
143 | } | |
144 | EXPORT_SYMBOL(mdiobus_unregister); | |
145 | ||
298cf9be LB |
146 | /** |
147 | * mdiobus_free - free a struct mii_bus | |
148 | * @bus: mii_bus to free | |
149 | * | |
46abc021 LB |
150 | * This function releases the reference to the underlying device |
151 | * object in the mii_bus. If this is the last reference, the mii_bus | |
152 | * will be freed. | |
298cf9be LB |
153 | */ |
154 | void mdiobus_free(struct mii_bus *bus) | |
155 | { | |
46abc021 LB |
156 | /* |
157 | * For compatibility with error handling in drivers. | |
158 | */ | |
159 | if (bus->state == MDIOBUS_ALLOCATED) { | |
160 | kfree(bus); | |
161 | return; | |
162 | } | |
163 | ||
164 | BUG_ON(bus->state != MDIOBUS_UNREGISTERED); | |
165 | bus->state = MDIOBUS_RELEASED; | |
166 | ||
167 | put_device(&bus->dev); | |
298cf9be LB |
168 | } |
169 | EXPORT_SYMBOL(mdiobus_free); | |
170 | ||
4fd5f812 LB |
171 | struct phy_device *mdiobus_scan(struct mii_bus *bus, int addr) |
172 | { | |
173 | struct phy_device *phydev; | |
174 | int err; | |
175 | ||
176 | phydev = get_phy_device(bus, addr); | |
177 | if (IS_ERR(phydev) || phydev == NULL) | |
178 | return phydev; | |
179 | ||
180 | /* There's a PHY at this address | |
181 | * We need to set: | |
182 | * 1) IRQ | |
183 | * 2) bus_id | |
184 | * 3) parent | |
185 | * 4) bus | |
186 | * 5) mii_bus | |
187 | * And, we need to register it */ | |
188 | ||
189 | phydev->irq = bus->irq != NULL ? bus->irq[addr] : PHY_POLL; | |
190 | ||
18ee49dd | 191 | phydev->dev.parent = bus->parent; |
4fd5f812 LB |
192 | phydev->dev.bus = &mdio_bus_type; |
193 | snprintf(phydev->dev.bus_id, BUS_ID_SIZE, PHY_ID_FMT, bus->id, addr); | |
194 | ||
195 | phydev->bus = bus; | |
196 | ||
197 | /* Run all of the fixups for this PHY */ | |
198 | phy_scan_fixups(phydev); | |
199 | ||
200 | err = device_register(&phydev->dev); | |
201 | if (err) { | |
202 | printk(KERN_ERR "phy %d failed to register\n", addr); | |
203 | phy_device_free(phydev); | |
204 | phydev = NULL; | |
205 | } | |
206 | ||
207 | bus->phy_map[addr] = phydev; | |
208 | ||
209 | return phydev; | |
210 | } | |
211 | EXPORT_SYMBOL(mdiobus_scan); | |
212 | ||
2e888103 LB |
213 | /** |
214 | * mdiobus_read - Convenience function for reading a given MII mgmt register | |
215 | * @bus: the mii_bus struct | |
216 | * @addr: the phy address | |
217 | * @regnum: register number to read | |
218 | * | |
219 | * NOTE: MUST NOT be called from interrupt context, | |
220 | * because the bus read/write functions may wait for an interrupt | |
221 | * to conclude the operation. | |
222 | */ | |
223 | int mdiobus_read(struct mii_bus *bus, int addr, u16 regnum) | |
224 | { | |
225 | int retval; | |
226 | ||
227 | BUG_ON(in_interrupt()); | |
228 | ||
229 | mutex_lock(&bus->mdio_lock); | |
230 | retval = bus->read(bus, addr, regnum); | |
231 | mutex_unlock(&bus->mdio_lock); | |
232 | ||
233 | return retval; | |
234 | } | |
235 | EXPORT_SYMBOL(mdiobus_read); | |
236 | ||
237 | /** | |
238 | * mdiobus_write - Convenience function for writing a given MII mgmt register | |
239 | * @bus: the mii_bus struct | |
240 | * @addr: the phy address | |
241 | * @regnum: register number to write | |
242 | * @val: value to write to @regnum | |
243 | * | |
244 | * NOTE: MUST NOT be called from interrupt context, | |
245 | * because the bus read/write functions may wait for an interrupt | |
246 | * to conclude the operation. | |
247 | */ | |
248 | int mdiobus_write(struct mii_bus *bus, int addr, u16 regnum, u16 val) | |
249 | { | |
250 | int err; | |
251 | ||
252 | BUG_ON(in_interrupt()); | |
253 | ||
254 | mutex_lock(&bus->mdio_lock); | |
255 | err = bus->write(bus, addr, regnum, val); | |
256 | mutex_unlock(&bus->mdio_lock); | |
257 | ||
258 | return err; | |
259 | } | |
260 | EXPORT_SYMBOL(mdiobus_write); | |
261 | ||
b3df0da8 RD |
262 | /** |
263 | * mdio_bus_match - determine if given PHY driver supports the given PHY device | |
264 | * @dev: target PHY device | |
265 | * @drv: given PHY driver | |
00db8189 | 266 | * |
b3df0da8 RD |
267 | * Description: Given a PHY device, and a PHY driver, return 1 if |
268 | * the driver supports the device. Otherwise, return 0. | |
00db8189 AF |
269 | */ |
270 | static int mdio_bus_match(struct device *dev, struct device_driver *drv) | |
271 | { | |
272 | struct phy_device *phydev = to_phy_device(dev); | |
273 | struct phy_driver *phydrv = to_phy_driver(drv); | |
274 | ||
5f708dd9 KG |
275 | return ((phydrv->phy_id & phydrv->phy_id_mask) == |
276 | (phydev->phy_id & phydrv->phy_id_mask)); | |
00db8189 AF |
277 | } |
278 | ||
279 | /* Suspend and resume. Copied from platform_suspend and | |
280 | * platform_resume | |
281 | */ | |
829ca9a3 | 282 | static int mdio_bus_suspend(struct device * dev, pm_message_t state) |
00db8189 AF |
283 | { |
284 | int ret = 0; | |
285 | struct device_driver *drv = dev->driver; | |
286 | ||
9480e307 RK |
287 | if (drv && drv->suspend) |
288 | ret = drv->suspend(dev, state); | |
289 | ||
00db8189 AF |
290 | return ret; |
291 | } | |
292 | ||
293 | static int mdio_bus_resume(struct device * dev) | |
294 | { | |
295 | int ret = 0; | |
296 | struct device_driver *drv = dev->driver; | |
297 | ||
9480e307 RK |
298 | if (drv && drv->resume) |
299 | ret = drv->resume(dev); | |
300 | ||
00db8189 AF |
301 | return ret; |
302 | } | |
303 | ||
304 | struct bus_type mdio_bus_type = { | |
305 | .name = "mdio_bus", | |
306 | .match = mdio_bus_match, | |
307 | .suspend = mdio_bus_suspend, | |
308 | .resume = mdio_bus_resume, | |
309 | }; | |
11b0bacd | 310 | EXPORT_SYMBOL(mdio_bus_type); |
00db8189 | 311 | |
67c4f3fa | 312 | int __init mdio_bus_init(void) |
00db8189 | 313 | { |
46abc021 LB |
314 | int ret; |
315 | ||
316 | ret = class_register(&mdio_bus_class); | |
317 | if (!ret) { | |
318 | ret = bus_register(&mdio_bus_type); | |
319 | if (ret) | |
320 | class_unregister(&mdio_bus_class); | |
321 | } | |
322 | ||
323 | return ret; | |
00db8189 AF |
324 | } |
325 | ||
dc85dec6 | 326 | void mdio_bus_exit(void) |
e1393456 | 327 | { |
46abc021 | 328 | class_unregister(&mdio_bus_class); |
e1393456 AF |
329 | bus_unregister(&mdio_bus_type); |
330 | } |