2 * drivers/net/phy/mdio_bus.c
8 * Copyright (c) 2004 Freescale Semiconductor, Inc.
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.
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>
29 #include <linux/module.h>
30 #include <linux/mii.h>
31 #include <linux/ethtool.h>
32 #include <linux/phy.h>
36 #include <asm/uaccess.h>
39 * mdiobus_alloc - allocate a mii_bus structure
41 * Description: called by a bus driver to allocate an mii_bus
42 * structure to fill in.
44 struct mii_bus *mdiobus_alloc(void)
48 bus = kzalloc(sizeof(*bus), GFP_KERNEL);
50 bus->state = MDIOBUS_ALLOCATED;
54 EXPORT_SYMBOL(mdiobus_alloc);
57 * mdiobus_release - mii_bus device release callback
58 * @d: the target struct device that contains the mii_bus
60 * Description: called when the last reference to an mii_bus is
61 * dropped, to free the underlying memory.
63 static void mdiobus_release(struct device *d)
65 struct mii_bus *bus = to_mii_bus(d);
66 BUG_ON(bus->state != MDIOBUS_RELEASED);
70 static struct class mdio_bus_class = {
72 .dev_release = mdiobus_release,
76 * mdiobus_register - bring up all the PHYs on a given bus and attach them to bus
77 * @bus: target mii_bus
79 * Description: Called by a bus driver to bring up all the PHYs
80 * on a given bus, and attach them to the bus.
82 * Returns 0 on success or < 0 on error.
84 int mdiobus_register(struct mii_bus *bus)
89 if (NULL == bus || NULL == bus->name ||
94 BUG_ON(bus->state != MDIOBUS_ALLOCATED &&
95 bus->state != MDIOBUS_UNREGISTERED);
97 bus->dev.parent = bus->parent;
98 bus->dev.class = &mdio_bus_class;
99 bus->dev.groups = NULL;
100 memcpy(bus->dev.bus_id, bus->id, MII_BUS_ID_SIZE);
102 err = device_register(&bus->dev);
104 printk(KERN_ERR "mii_bus %s failed to register\n", bus->id);
108 mutex_init(&bus->mdio_lock);
113 for (i = 0; i < PHY_MAX_ADDR; i++) {
114 bus->phy_map[i] = NULL;
115 if ((bus->phy_mask & (1 << i)) == 0) {
116 struct phy_device *phydev;
118 phydev = mdiobus_scan(bus, i);
120 err = PTR_ERR(phydev);
125 bus->state = MDIOBUS_REGISTERED;
127 pr_info("%s: probed\n", bus->name);
131 EXPORT_SYMBOL(mdiobus_register);
133 void mdiobus_unregister(struct mii_bus *bus)
137 BUG_ON(bus->state != MDIOBUS_REGISTERED);
138 bus->state = MDIOBUS_UNREGISTERED;
140 device_del(&bus->dev);
141 for (i = 0; i < PHY_MAX_ADDR; i++) {
143 device_unregister(&bus->phy_map[i]->dev);
146 EXPORT_SYMBOL(mdiobus_unregister);
149 * mdiobus_free - free a struct mii_bus
150 * @bus: mii_bus to free
152 * This function releases the reference to the underlying device
153 * object in the mii_bus. If this is the last reference, the mii_bus
156 void mdiobus_free(struct mii_bus *bus)
159 * For compatibility with error handling in drivers.
161 if (bus->state == MDIOBUS_ALLOCATED) {
166 BUG_ON(bus->state != MDIOBUS_UNREGISTERED);
167 bus->state = MDIOBUS_RELEASED;
169 put_device(&bus->dev);
171 EXPORT_SYMBOL(mdiobus_free);
173 struct phy_device *mdiobus_scan(struct mii_bus *bus, int addr)
175 struct phy_device *phydev;
178 phydev = get_phy_device(bus, addr);
179 if (IS_ERR(phydev) || phydev == NULL)
182 /* There's a PHY at this address
189 * And, we need to register it */
191 phydev->irq = bus->irq != NULL ? bus->irq[addr] : PHY_POLL;
193 phydev->dev.parent = bus->parent;
194 phydev->dev.bus = &mdio_bus_type;
195 snprintf(phydev->dev.bus_id, BUS_ID_SIZE, PHY_ID_FMT, bus->id, addr);
199 /* Run all of the fixups for this PHY */
200 phy_scan_fixups(phydev);
202 err = device_register(&phydev->dev);
204 printk(KERN_ERR "phy %d failed to register\n", addr);
205 phy_device_free(phydev);
209 bus->phy_map[addr] = phydev;
213 EXPORT_SYMBOL(mdiobus_scan);
216 * mdiobus_read - Convenience function for reading a given MII mgmt register
217 * @bus: the mii_bus struct
218 * @addr: the phy address
219 * @regnum: register number to read
221 * NOTE: MUST NOT be called from interrupt context,
222 * because the bus read/write functions may wait for an interrupt
223 * to conclude the operation.
225 int mdiobus_read(struct mii_bus *bus, int addr, u16 regnum)
229 BUG_ON(in_interrupt());
231 mutex_lock(&bus->mdio_lock);
232 retval = bus->read(bus, addr, regnum);
233 mutex_unlock(&bus->mdio_lock);
237 EXPORT_SYMBOL(mdiobus_read);
240 * mdiobus_write - Convenience function for writing a given MII mgmt register
241 * @bus: the mii_bus struct
242 * @addr: the phy address
243 * @regnum: register number to write
244 * @val: value to write to @regnum
246 * NOTE: MUST NOT be called from interrupt context,
247 * because the bus read/write functions may wait for an interrupt
248 * to conclude the operation.
250 int mdiobus_write(struct mii_bus *bus, int addr, u16 regnum, u16 val)
254 BUG_ON(in_interrupt());
256 mutex_lock(&bus->mdio_lock);
257 err = bus->write(bus, addr, regnum, val);
258 mutex_unlock(&bus->mdio_lock);
262 EXPORT_SYMBOL(mdiobus_write);
265 * mdio_bus_match - determine if given PHY driver supports the given PHY device
266 * @dev: target PHY device
267 * @drv: given PHY driver
269 * Description: Given a PHY device, and a PHY driver, return 1 if
270 * the driver supports the device. Otherwise, return 0.
272 static int mdio_bus_match(struct device *dev, struct device_driver *drv)
274 struct phy_device *phydev = to_phy_device(dev);
275 struct phy_driver *phydrv = to_phy_driver(drv);
277 return ((phydrv->phy_id & phydrv->phy_id_mask) ==
278 (phydev->phy_id & phydrv->phy_id_mask));
281 /* Suspend and resume. Copied from platform_suspend and
284 static int mdio_bus_suspend(struct device * dev, pm_message_t state)
287 struct device_driver *drv = dev->driver;
289 if (drv && drv->suspend)
290 ret = drv->suspend(dev, state);
295 static int mdio_bus_resume(struct device * dev)
298 struct device_driver *drv = dev->driver;
300 if (drv && drv->resume)
301 ret = drv->resume(dev);
306 struct bus_type mdio_bus_type = {
308 .match = mdio_bus_match,
309 .suspend = mdio_bus_suspend,
310 .resume = mdio_bus_resume,
312 EXPORT_SYMBOL(mdio_bus_type);
314 int __init mdio_bus_init(void)
318 ret = class_register(&mdio_bus_class);
320 ret = bus_register(&mdio_bus_type);
322 class_unregister(&mdio_bus_class);
328 void mdio_bus_exit(void)
330 class_unregister(&mdio_bus_class);
331 bus_unregister(&mdio_bus_type);