Merge branch 'master' into upstream
[pandora-kernel.git] / drivers / net / phy / mdio_bus.c
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  */
16 #include <linux/kernel.h>
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>
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
38 /* mdiobus_register 
39  *
40  * description: Called by a bus driver to bring up all the PHYs
41  *   on a given bus, and attach them to the bus
42  */
43 int mdiobus_register(struct mii_bus *bus)
44 {
45         int i;
46         int err = 0;
47
48         spin_lock_init(&bus->mdio_lock);
49
50         if (NULL == bus || NULL == bus->name ||
51                         NULL == bus->read ||
52                         NULL == bus->write)
53                 return -EINVAL;
54
55         if (bus->reset)
56                 bus->reset(bus);
57
58         for (i = 0; i < PHY_MAX_ADDR; i++) {
59                 struct phy_device *phydev;
60
61                 if (bus->phy_mask & (1 << i)) {
62                         bus->phy_map[i] = NULL;
63                         continue;
64                 }
65
66                 phydev = get_phy_device(bus, i);
67
68                 if (IS_ERR(phydev))
69                         return PTR_ERR(phydev);
70
71                 /* There's a PHY at this address
72                  * We need to set:
73                  * 1) IRQ
74                  * 2) bus_id
75                  * 3) parent
76                  * 4) bus
77                  * 5) mii_bus
78                  * And, we need to register it */
79                 if (phydev) {
80                         phydev->irq = bus->irq[i];
81
82                         phydev->dev.parent = bus->dev;
83                         phydev->dev.bus = &mdio_bus_type;
84                         snprintf(phydev->dev.bus_id, BUS_ID_SIZE, PHY_ID_FMT, bus->id, i);
85
86                         phydev->bus = bus;
87
88                         err = device_register(&phydev->dev);
89
90                         if (err)
91                                 printk(KERN_ERR "phy %d failed to register\n",
92                                                 i);
93                 }
94
95                 bus->phy_map[i] = phydev;
96         }
97
98         pr_info("%s: probed\n", bus->name);
99
100         return err;
101 }
102 EXPORT_SYMBOL(mdiobus_register);
103
104 void mdiobus_unregister(struct mii_bus *bus)
105 {
106         int i;
107
108         for (i = 0; i < PHY_MAX_ADDR; i++) {
109                 if (bus->phy_map[i]) {
110                         device_unregister(&bus->phy_map[i]->dev);
111                         kfree(bus->phy_map[i]);
112                 }
113         }
114 }
115 EXPORT_SYMBOL(mdiobus_unregister);
116
117 /* mdio_bus_match
118  *
119  * description: Given a PHY device, and a PHY driver, return 1 if
120  *   the driver supports the device.  Otherwise, return 0
121  */
122 static int mdio_bus_match(struct device *dev, struct device_driver *drv)
123 {
124         struct phy_device *phydev = to_phy_device(dev);
125         struct phy_driver *phydrv = to_phy_driver(drv);
126
127         return (phydrv->phy_id == (phydev->phy_id & phydrv->phy_id_mask));
128 }
129
130 /* Suspend and resume.  Copied from platform_suspend and
131  * platform_resume
132  */
133 static int mdio_bus_suspend(struct device * dev, pm_message_t state)
134 {
135         int ret = 0;
136         struct device_driver *drv = dev->driver;
137
138         if (drv && drv->suspend)
139                 ret = drv->suspend(dev, state);
140
141         return ret;
142 }
143
144 static int mdio_bus_resume(struct device * dev)
145 {
146         int ret = 0;
147         struct device_driver *drv = dev->driver;
148
149         if (drv && drv->resume)
150                 ret = drv->resume(dev);
151
152         return ret;
153 }
154
155 struct bus_type mdio_bus_type = {
156         .name           = "mdio_bus",
157         .match          = mdio_bus_match,
158         .suspend        = mdio_bus_suspend,
159         .resume         = mdio_bus_resume,
160 };
161 EXPORT_SYMBOL(mdio_bus_type);
162
163 int __init mdio_bus_init(void)
164 {
165         return bus_register(&mdio_bus_type);
166 }
167
168 void mdio_bus_exit(void)
169 {
170         bus_unregister(&mdio_bus_type);
171 }