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_register - bring up all the PHYs on a given bus and attach them to bus
40 * @bus: target mii_bus
42 * Description: Called by a bus driver to bring up all the PHYs
43 * on a given bus, and attach them to the bus.
45 * Returns 0 on success or < 0 on error.
47 int mdiobus_register(struct mii_bus *bus)
52 if (NULL == bus || NULL == bus->name ||
57 mutex_init(&bus->mdio_lock);
62 for (i = 0; i < PHY_MAX_ADDR; i++) {
63 struct phy_device *phydev;
65 if (bus->phy_mask & (1 << i)) {
66 bus->phy_map[i] = NULL;
70 phydev = get_phy_device(bus, i);
73 return PTR_ERR(phydev);
75 /* There's a PHY at this address
82 * And, we need to register it */
84 phydev->irq = bus->irq[i];
86 phydev->dev.parent = bus->dev;
87 phydev->dev.bus = &mdio_bus_type;
88 snprintf(phydev->dev.bus_id, BUS_ID_SIZE, PHY_ID_FMT, bus->id, i);
92 /* Run all of the fixups for this PHY */
93 phy_scan_fixups(phydev);
95 err = device_register(&phydev->dev);
98 printk(KERN_ERR "phy %d failed to register\n",
100 phy_device_free(phydev);
105 bus->phy_map[i] = phydev;
108 pr_info("%s: probed\n", bus->name);
112 EXPORT_SYMBOL(mdiobus_register);
114 void mdiobus_unregister(struct mii_bus *bus)
118 for (i = 0; i < PHY_MAX_ADDR; i++) {
120 device_unregister(&bus->phy_map[i]->dev);
123 EXPORT_SYMBOL(mdiobus_unregister);
126 * mdio_bus_match - determine if given PHY driver supports the given PHY device
127 * @dev: target PHY device
128 * @drv: given PHY driver
130 * Description: Given a PHY device, and a PHY driver, return 1 if
131 * the driver supports the device. Otherwise, return 0.
133 static int mdio_bus_match(struct device *dev, struct device_driver *drv)
135 struct phy_device *phydev = to_phy_device(dev);
136 struct phy_driver *phydrv = to_phy_driver(drv);
138 return ((phydrv->phy_id & phydrv->phy_id_mask) ==
139 (phydev->phy_id & phydrv->phy_id_mask));
142 /* Suspend and resume. Copied from platform_suspend and
145 static int mdio_bus_suspend(struct device * dev, pm_message_t state)
148 struct device_driver *drv = dev->driver;
150 if (drv && drv->suspend)
151 ret = drv->suspend(dev, state);
156 static int mdio_bus_resume(struct device * dev)
159 struct device_driver *drv = dev->driver;
161 if (drv && drv->resume)
162 ret = drv->resume(dev);
167 struct bus_type mdio_bus_type = {
169 .match = mdio_bus_match,
170 .suspend = mdio_bus_suspend,
171 .resume = mdio_bus_resume,
173 EXPORT_SYMBOL(mdio_bus_type);
175 int __init mdio_bus_init(void)
177 return bus_register(&mdio_bus_type);
180 void mdio_bus_exit(void)
182 bus_unregister(&mdio_bus_type);