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/config.h>
17 #include <linux/kernel.h>
18 #include <linux/sched.h>
19 #include <linux/string.h>
20 #include <linux/errno.h>
21 #include <linux/unistd.h>
22 #include <linux/slab.h>
23 #include <linux/interrupt.h>
24 #include <linux/init.h>
25 #include <linux/delay.h>
26 #include <linux/netdevice.h>
27 #include <linux/etherdevice.h>
28 #include <linux/skbuff.h>
29 #include <linux/spinlock.h>
31 #include <linux/module.h>
32 #include <linux/mii.h>
33 #include <linux/ethtool.h>
34 #include <linux/phy.h>
38 #include <asm/uaccess.h>
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 int mdiobus_register(struct mii_bus *bus)
50 spin_lock_init(&bus->mdio_lock);
52 if (NULL == bus || NULL == bus->name ||
60 for (i = 0; i < PHY_MAX_ADDR; i++) {
61 struct phy_device *phydev;
63 if (bus->phy_mask & (1 << i)) {
64 bus->phy_map[i] = NULL;
68 phydev = get_phy_device(bus, i);
71 return PTR_ERR(phydev);
73 /* There's a PHY at this address
80 * And, we need to register it */
82 phydev->irq = bus->irq[i];
84 phydev->dev.parent = bus->dev;
85 phydev->dev.bus = &mdio_bus_type;
86 snprintf(phydev->dev.bus_id, BUS_ID_SIZE, PHY_ID_FMT, bus->id, i);
90 err = device_register(&phydev->dev);
93 printk(KERN_ERR "phy %d failed to register\n",
97 bus->phy_map[i] = phydev;
100 pr_info("%s: probed\n", bus->name);
104 EXPORT_SYMBOL(mdiobus_register);
106 void mdiobus_unregister(struct mii_bus *bus)
110 for (i = 0; i < PHY_MAX_ADDR; i++) {
111 if (bus->phy_map[i]) {
112 device_unregister(&bus->phy_map[i]->dev);
113 kfree(bus->phy_map[i]);
117 EXPORT_SYMBOL(mdiobus_unregister);
121 * description: Given a PHY device, and a PHY driver, return 1 if
122 * the driver supports the device. Otherwise, return 0
124 static int mdio_bus_match(struct device *dev, struct device_driver *drv)
126 struct phy_device *phydev = to_phy_device(dev);
127 struct phy_driver *phydrv = to_phy_driver(drv);
129 return (phydrv->phy_id == (phydev->phy_id & phydrv->phy_id_mask));
132 /* Suspend and resume. Copied from platform_suspend and
135 static int mdio_bus_suspend(struct device * dev, pm_message_t state)
138 struct device_driver *drv = dev->driver;
140 if (drv && drv->suspend)
141 ret = drv->suspend(dev, state);
146 static int mdio_bus_resume(struct device * dev)
149 struct device_driver *drv = dev->driver;
151 if (drv && drv->resume)
152 ret = drv->resume(dev);
157 struct bus_type mdio_bus_type = {
159 .match = mdio_bus_match,
160 .suspend = mdio_bus_suspend,
161 .resume = mdio_bus_resume,
164 int __init mdio_bus_init(void)
166 return bus_register(&mdio_bus_type);
169 void mdio_bus_exit(void)
171 bus_unregister(&mdio_bus_type);