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
59 * Description: called when the last reference to an mii_bus is
60 * dropped, to free the underlying memory.
62 static void mdiobus_release(struct device *d)
64 struct mii_bus *bus = to_mii_bus(d);
65 BUG_ON(bus->state != MDIOBUS_RELEASED);
69 static struct class mdio_bus_class = {
71 .dev_release = mdiobus_release,
75 * mdiobus_register - bring up all the PHYs on a given bus and attach them to bus
76 * @bus: target mii_bus
78 * Description: Called by a bus driver to bring up all the PHYs
79 * on a given bus, and attach them to the bus.
81 * Returns 0 on success or < 0 on error.
83 int mdiobus_register(struct mii_bus *bus)
88 if (NULL == bus || NULL == bus->name ||
93 BUG_ON(bus->state != MDIOBUS_ALLOCATED &&
94 bus->state != MDIOBUS_UNREGISTERED);
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);
101 err = device_register(&bus->dev);
103 printk(KERN_ERR "mii_bus %s failed to register\n", bus->id);
107 bus->state = MDIOBUS_REGISTERED;
109 mutex_init(&bus->mdio_lock);
114 for (i = 0; i < PHY_MAX_ADDR; i++) {
115 bus->phy_map[i] = NULL;
116 if ((bus->phy_mask & (1 << i)) == 0) {
117 struct phy_device *phydev;
119 phydev = mdiobus_scan(bus, i);
121 err = PTR_ERR(phydev);
125 pr_info("%s: probed\n", bus->name);
129 EXPORT_SYMBOL(mdiobus_register);
131 void mdiobus_unregister(struct mii_bus *bus)
135 BUG_ON(bus->state != MDIOBUS_REGISTERED);
136 bus->state = MDIOBUS_UNREGISTERED;
138 device_unregister(&bus->dev);
139 for (i = 0; i < PHY_MAX_ADDR; i++) {
141 device_unregister(&bus->phy_map[i]->dev);
144 EXPORT_SYMBOL(mdiobus_unregister);
147 * mdiobus_free - free a struct mii_bus
148 * @bus: mii_bus to free
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
154 void mdiobus_free(struct mii_bus *bus)
157 * For compatibility with error handling in drivers.
159 if (bus->state == MDIOBUS_ALLOCATED) {
164 BUG_ON(bus->state != MDIOBUS_UNREGISTERED);
165 bus->state = MDIOBUS_RELEASED;
167 put_device(&bus->dev);
169 EXPORT_SYMBOL(mdiobus_free);
171 struct phy_device *mdiobus_scan(struct mii_bus *bus, int addr)
173 struct phy_device *phydev;
176 phydev = get_phy_device(bus, addr);
177 if (IS_ERR(phydev) || phydev == NULL)
180 /* There's a PHY at this address
187 * And, we need to register it */
189 phydev->irq = bus->irq != NULL ? bus->irq[addr] : PHY_POLL;
191 phydev->dev.parent = bus->parent;
192 phydev->dev.bus = &mdio_bus_type;
193 snprintf(phydev->dev.bus_id, BUS_ID_SIZE, PHY_ID_FMT, bus->id, addr);
197 /* Run all of the fixups for this PHY */
198 phy_scan_fixups(phydev);
200 err = device_register(&phydev->dev);
202 printk(KERN_ERR "phy %d failed to register\n", addr);
203 phy_device_free(phydev);
207 bus->phy_map[addr] = phydev;
211 EXPORT_SYMBOL(mdiobus_scan);
214 * mdio_bus_match - determine if given PHY driver supports the given PHY device
215 * @dev: target PHY device
216 * @drv: given PHY driver
218 * Description: Given a PHY device, and a PHY driver, return 1 if
219 * the driver supports the device. Otherwise, return 0.
221 static int mdio_bus_match(struct device *dev, struct device_driver *drv)
223 struct phy_device *phydev = to_phy_device(dev);
224 struct phy_driver *phydrv = to_phy_driver(drv);
226 return ((phydrv->phy_id & phydrv->phy_id_mask) ==
227 (phydev->phy_id & phydrv->phy_id_mask));
230 /* Suspend and resume. Copied from platform_suspend and
233 static int mdio_bus_suspend(struct device * dev, pm_message_t state)
236 struct device_driver *drv = dev->driver;
238 if (drv && drv->suspend)
239 ret = drv->suspend(dev, state);
244 static int mdio_bus_resume(struct device * dev)
247 struct device_driver *drv = dev->driver;
249 if (drv && drv->resume)
250 ret = drv->resume(dev);
255 struct bus_type mdio_bus_type = {
257 .match = mdio_bus_match,
258 .suspend = mdio_bus_suspend,
259 .resume = mdio_bus_resume,
261 EXPORT_SYMBOL(mdio_bus_type);
263 int __init mdio_bus_init(void)
267 ret = class_register(&mdio_bus_class);
269 ret = bus_register(&mdio_bus_type);
271 class_unregister(&mdio_bus_class);
277 void mdio_bus_exit(void)
279 class_unregister(&mdio_bus_class);
280 bus_unregister(&mdio_bus_type);