blob: 811a637695caf4270247cbace802bca469bc03e0 [file] [log] [blame]
Andy Fleming00db8182005-07-30 19:31:23 -04001/*
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 */
Andy Fleming00db8182005-07-30 19:31:23 -040016#include <linux/kernel.h>
Andy Fleming00db8182005-07-30 19:31:23 -040017#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>
Andy Fleming00db8182005-07-30 19:31:23 -040030#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
Randy Dunlapb3df0da2007-03-06 02:41:48 -080038/**
Lennert Buytenhek298cf9be2008-10-08 16:29:57 -070039 * 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 */
44struct mii_bus *mdiobus_alloc(void)
45{
Lennert Buytenhek46abc022008-10-08 16:33:40 -070046 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;
Lennert Buytenhek298cf9be2008-10-08 16:29:57 -070053}
54EXPORT_SYMBOL(mdiobus_alloc);
55
56/**
Lennert Buytenhek46abc022008-10-08 16:33:40 -070057 * mdiobus_release - mii_bus device release callback
Randy Dunlap78c36b12008-10-13 18:46:22 -070058 * @d: the target struct device that contains the mii_bus
Lennert Buytenhek46abc022008-10-08 16:33:40 -070059 *
60 * Description: called when the last reference to an mii_bus is
61 * dropped, to free the underlying memory.
62 */
63static void mdiobus_release(struct device *d)
64{
65 struct mii_bus *bus = to_mii_bus(d);
Krzysztof Halasa161c8d22008-12-25 16:50:41 -080066 BUG_ON(bus->state != MDIOBUS_RELEASED &&
67 /* for compatibility with error handling in drivers */
68 bus->state != MDIOBUS_ALLOCATED);
Lennert Buytenhek46abc022008-10-08 16:33:40 -070069 kfree(bus);
70}
71
72static struct class mdio_bus_class = {
73 .name = "mdio_bus",
74 .dev_release = mdiobus_release,
75};
76
77/**
Randy Dunlapb3df0da2007-03-06 02:41:48 -080078 * mdiobus_register - bring up all the PHYs on a given bus and attach them to bus
79 * @bus: target mii_bus
Andy Fleminge1393452005-08-24 18:46:21 -050080 *
Randy Dunlapb3df0da2007-03-06 02:41:48 -080081 * Description: Called by a bus driver to bring up all the PHYs
82 * on a given bus, and attach them to the bus.
83 *
84 * Returns 0 on success or < 0 on error.
Andy Fleminge1393452005-08-24 18:46:21 -050085 */
86int mdiobus_register(struct mii_bus *bus)
87{
Krzysztof Halasa161c8d22008-12-25 16:50:41 -080088 int i, err;
Andy Fleminge1393452005-08-24 18:46:21 -050089
Andy Fleminge1393452005-08-24 18:46:21 -050090 if (NULL == bus || NULL == bus->name ||
91 NULL == bus->read ||
92 NULL == bus->write)
93 return -EINVAL;
94
Lennert Buytenhek46abc022008-10-08 16:33:40 -070095 BUG_ON(bus->state != MDIOBUS_ALLOCATED &&
96 bus->state != MDIOBUS_UNREGISTERED);
97
98 bus->dev.parent = bus->parent;
99 bus->dev.class = &mdio_bus_class;
100 bus->dev.groups = NULL;
Kay Sieversfb28ad32008-11-10 13:55:14 -0800101 dev_set_name(&bus->dev, bus->id);
Lennert Buytenhek46abc022008-10-08 16:33:40 -0700102
103 err = device_register(&bus->dev);
104 if (err) {
105 printk(KERN_ERR "mii_bus %s failed to register\n", bus->id);
106 return -EINVAL;
107 }
108
Adrian Bunkd1e7fe42008-02-20 02:13:53 +0200109 mutex_init(&bus->mdio_lock);
110
Andy Fleminge1393452005-08-24 18:46:21 -0500111 if (bus->reset)
112 bus->reset(bus);
113
114 for (i = 0; i < PHY_MAX_ADDR; i++) {
Lennert Buytenhek4fd5f812008-08-26 13:08:46 +0200115 bus->phy_map[i] = NULL;
116 if ((bus->phy_mask & (1 << i)) == 0) {
117 struct phy_device *phydev;
Andy Fleminge1393452005-08-24 18:46:21 -0500118
Lennert Buytenhek4fd5f812008-08-26 13:08:46 +0200119 phydev = mdiobus_scan(bus, i);
Krzysztof Halasa161c8d22008-12-25 16:50:41 -0800120 if (IS_ERR(phydev)) {
Lennert Buytenhek4fd5f812008-08-26 13:08:46 +0200121 err = PTR_ERR(phydev);
Krzysztof Halasa161c8d22008-12-25 16:50:41 -0800122 goto error;
123 }
Herbert Valerio Riedel64b1c2b2006-05-10 12:12:57 -0400124 }
Andy Fleminge1393452005-08-24 18:46:21 -0500125 }
126
Krzysztof Halasa161c8d22008-12-25 16:50:41 -0800127 bus->state = MDIOBUS_REGISTERED;
Andy Fleminge1393452005-08-24 18:46:21 -0500128 pr_info("%s: probed\n", bus->name);
Krzysztof Halasa161c8d22008-12-25 16:50:41 -0800129 return 0;
Andy Fleminge1393452005-08-24 18:46:21 -0500130
Krzysztof Halasa161c8d22008-12-25 16:50:41 -0800131error:
132 while (--i >= 0) {
133 if (bus->phy_map[i])
134 device_unregister(&bus->phy_map[i]->dev);
135 }
136 device_del(&bus->dev);
Andy Fleminge1393452005-08-24 18:46:21 -0500137 return err;
138}
139EXPORT_SYMBOL(mdiobus_register);
140
141void mdiobus_unregister(struct mii_bus *bus)
142{
143 int i;
144
Lennert Buytenhek46abc022008-10-08 16:33:40 -0700145 BUG_ON(bus->state != MDIOBUS_REGISTERED);
146 bus->state = MDIOBUS_UNREGISTERED;
147
Lennert Buytenhek3e440172008-11-09 05:34:47 +0100148 device_del(&bus->dev);
Andy Fleminge1393452005-08-24 18:46:21 -0500149 for (i = 0; i < PHY_MAX_ADDR; i++) {
Anton Vorontsov6f4a7f42007-12-04 16:17:33 +0300150 if (bus->phy_map[i])
Andy Fleminge1393452005-08-24 18:46:21 -0500151 device_unregister(&bus->phy_map[i]->dev);
Andy Fleminge1393452005-08-24 18:46:21 -0500152 }
153}
154EXPORT_SYMBOL(mdiobus_unregister);
155
Lennert Buytenhek298cf9be2008-10-08 16:29:57 -0700156/**
157 * mdiobus_free - free a struct mii_bus
158 * @bus: mii_bus to free
159 *
Lennert Buytenhek46abc022008-10-08 16:33:40 -0700160 * This function releases the reference to the underlying device
161 * object in the mii_bus. If this is the last reference, the mii_bus
162 * will be freed.
Lennert Buytenhek298cf9be2008-10-08 16:29:57 -0700163 */
164void mdiobus_free(struct mii_bus *bus)
165{
Lennert Buytenhek46abc022008-10-08 16:33:40 -0700166 /*
167 * For compatibility with error handling in drivers.
168 */
169 if (bus->state == MDIOBUS_ALLOCATED) {
170 kfree(bus);
171 return;
172 }
173
174 BUG_ON(bus->state != MDIOBUS_UNREGISTERED);
175 bus->state = MDIOBUS_RELEASED;
176
177 put_device(&bus->dev);
Lennert Buytenhek298cf9be2008-10-08 16:29:57 -0700178}
179EXPORT_SYMBOL(mdiobus_free);
180
Lennert Buytenhek4fd5f812008-08-26 13:08:46 +0200181struct phy_device *mdiobus_scan(struct mii_bus *bus, int addr)
182{
183 struct phy_device *phydev;
184 int err;
185
186 phydev = get_phy_device(bus, addr);
187 if (IS_ERR(phydev) || phydev == NULL)
188 return phydev;
189
190 /* There's a PHY at this address
191 * We need to set:
192 * 1) IRQ
193 * 2) bus_id
194 * 3) parent
195 * 4) bus
196 * 5) mii_bus
197 * And, we need to register it */
198
199 phydev->irq = bus->irq != NULL ? bus->irq[addr] : PHY_POLL;
200
Lennert Buytenhek18ee49d2008-10-01 15:41:33 +0000201 phydev->dev.parent = bus->parent;
Lennert Buytenhek4fd5f812008-08-26 13:08:46 +0200202 phydev->dev.bus = &mdio_bus_type;
Kay Sieversfb28ad32008-11-10 13:55:14 -0800203 dev_set_name(&phydev->dev, PHY_ID_FMT, bus->id, addr);
Lennert Buytenhek4fd5f812008-08-26 13:08:46 +0200204
205 phydev->bus = bus;
206
207 /* Run all of the fixups for this PHY */
208 phy_scan_fixups(phydev);
209
210 err = device_register(&phydev->dev);
211 if (err) {
212 printk(KERN_ERR "phy %d failed to register\n", addr);
213 phy_device_free(phydev);
214 phydev = NULL;
215 }
216
217 bus->phy_map[addr] = phydev;
218
219 return phydev;
220}
221EXPORT_SYMBOL(mdiobus_scan);
222
Randy Dunlapb3df0da2007-03-06 02:41:48 -0800223/**
Lennert Buytenhek2e888102008-09-29 17:12:35 +0000224 * mdiobus_read - Convenience function for reading a given MII mgmt register
225 * @bus: the mii_bus struct
226 * @addr: the phy address
227 * @regnum: register number to read
228 *
229 * NOTE: MUST NOT be called from interrupt context,
230 * because the bus read/write functions may wait for an interrupt
231 * to conclude the operation.
232 */
233int mdiobus_read(struct mii_bus *bus, int addr, u16 regnum)
234{
235 int retval;
236
237 BUG_ON(in_interrupt());
238
239 mutex_lock(&bus->mdio_lock);
240 retval = bus->read(bus, addr, regnum);
241 mutex_unlock(&bus->mdio_lock);
242
243 return retval;
244}
245EXPORT_SYMBOL(mdiobus_read);
246
247/**
248 * mdiobus_write - Convenience function for writing a given MII mgmt register
249 * @bus: the mii_bus struct
250 * @addr: the phy address
251 * @regnum: register number to write
252 * @val: value to write to @regnum
253 *
254 * NOTE: MUST NOT be called from interrupt context,
255 * because the bus read/write functions may wait for an interrupt
256 * to conclude the operation.
257 */
258int mdiobus_write(struct mii_bus *bus, int addr, u16 regnum, u16 val)
259{
260 int err;
261
262 BUG_ON(in_interrupt());
263
264 mutex_lock(&bus->mdio_lock);
265 err = bus->write(bus, addr, regnum, val);
266 mutex_unlock(&bus->mdio_lock);
267
268 return err;
269}
270EXPORT_SYMBOL(mdiobus_write);
271
272/**
Randy Dunlapb3df0da2007-03-06 02:41:48 -0800273 * mdio_bus_match - determine if given PHY driver supports the given PHY device
274 * @dev: target PHY device
275 * @drv: given PHY driver
Andy Fleming00db8182005-07-30 19:31:23 -0400276 *
Randy Dunlapb3df0da2007-03-06 02:41:48 -0800277 * Description: Given a PHY device, and a PHY driver, return 1 if
278 * the driver supports the device. Otherwise, return 0.
Andy Fleming00db8182005-07-30 19:31:23 -0400279 */
280static int mdio_bus_match(struct device *dev, struct device_driver *drv)
281{
282 struct phy_device *phydev = to_phy_device(dev);
283 struct phy_driver *phydrv = to_phy_driver(drv);
284
Kumar Gala5f708dd2007-06-28 13:26:06 -0500285 return ((phydrv->phy_id & phydrv->phy_id_mask) ==
286 (phydev->phy_id & phydrv->phy_id_mask));
Andy Fleming00db8182005-07-30 19:31:23 -0400287}
288
289/* Suspend and resume. Copied from platform_suspend and
290 * platform_resume
291 */
Pavel Machek829ca9a2005-09-03 15:56:56 -0700292static int mdio_bus_suspend(struct device * dev, pm_message_t state)
Andy Fleming00db8182005-07-30 19:31:23 -0400293{
294 int ret = 0;
295 struct device_driver *drv = dev->driver;
Giuseppe Cavallaro0f0ca342008-11-28 16:24:56 -0800296 struct phy_driver *phydrv = to_phy_driver(drv);
297 struct phy_device *phydev = to_phy_device(dev);
Andy Fleming00db8182005-07-30 19:31:23 -0400298
Anton Vorontsov6f051062009-01-22 13:51:24 -0800299 if (drv && phydrv->suspend && !device_may_wakeup(phydev->dev.parent))
300 ret = phydrv->suspend(phydev);
Russell King9480e302005-10-28 09:52:56 -0700301
Andy Fleming00db8182005-07-30 19:31:23 -0400302 return ret;
303}
304
305static int mdio_bus_resume(struct device * dev)
306{
307 int ret = 0;
308 struct device_driver *drv = dev->driver;
Giuseppe Cavallaro0f0ca342008-11-28 16:24:56 -0800309 struct phy_driver *phydrv = to_phy_driver(drv);
310 struct phy_device *phydev = to_phy_device(dev);
Andy Fleming00db8182005-07-30 19:31:23 -0400311
Anton Vorontsov6f051062009-01-22 13:51:24 -0800312 if (drv && phydrv->resume && !device_may_wakeup(phydev->dev.parent))
Giuseppe Cavallaro0f0ca342008-11-28 16:24:56 -0800313 ret = phydrv->resume(phydev);
Russell King9480e302005-10-28 09:52:56 -0700314
Andy Fleming00db8182005-07-30 19:31:23 -0400315 return ret;
316}
317
318struct bus_type mdio_bus_type = {
319 .name = "mdio_bus",
320 .match = mdio_bus_match,
321 .suspend = mdio_bus_suspend,
322 .resume = mdio_bus_resume,
323};
Vitaly Bordug11b0bac2006-08-14 23:00:29 -0700324EXPORT_SYMBOL(mdio_bus_type);
Andy Fleming00db8182005-07-30 19:31:23 -0400325
Jeff Garzik67c4f3f2005-08-11 02:07:25 -0400326int __init mdio_bus_init(void)
Andy Fleming00db8182005-07-30 19:31:23 -0400327{
Lennert Buytenhek46abc022008-10-08 16:33:40 -0700328 int ret;
329
330 ret = class_register(&mdio_bus_class);
331 if (!ret) {
332 ret = bus_register(&mdio_bus_type);
333 if (ret)
334 class_unregister(&mdio_bus_class);
335 }
336
337 return ret;
Andy Fleming00db8182005-07-30 19:31:23 -0400338}
339
Peter Chubbdc85dec2005-09-03 14:05:06 -0700340void mdio_bus_exit(void)
Andy Fleminge1393452005-08-24 18:46:21 -0500341{
Lennert Buytenhek46abc022008-10-08 16:33:40 -0700342 class_unregister(&mdio_bus_class);
Andy Fleminge1393452005-08-24 18:46:21 -0500343 bus_unregister(&mdio_bus_type);
344}