Andy Fleming | 00db818 | 2005-07-30 19:31:23 -0400 | [diff] [blame] | 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 | */ |
Andy Fleming | 00db818 | 2005-07-30 19:31:23 -0400 | [diff] [blame] | 16 | #include <linux/kernel.h> |
Andy Fleming | 00db818 | 2005-07-30 19:31:23 -0400 | [diff] [blame] | 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> |
Andy Fleming | 00db818 | 2005-07-30 19:31:23 -0400 | [diff] [blame] | 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 | |
Randy Dunlap | b3df0da | 2007-03-06 02:41:48 -0800 | [diff] [blame] | 38 | /** |
| 39 | * mdiobus_register - bring up all the PHYs on a given bus and attach them to bus |
| 40 | * @bus: target mii_bus |
Andy Fleming | e139345 | 2005-08-24 18:46:21 -0500 | [diff] [blame] | 41 | * |
Randy Dunlap | b3df0da | 2007-03-06 02:41:48 -0800 | [diff] [blame] | 42 | * Description: Called by a bus driver to bring up all the PHYs |
| 43 | * on a given bus, and attach them to the bus. |
| 44 | * |
| 45 | * Returns 0 on success or < 0 on error. |
Andy Fleming | e139345 | 2005-08-24 18:46:21 -0500 | [diff] [blame] | 46 | */ |
| 47 | int mdiobus_register(struct mii_bus *bus) |
| 48 | { |
| 49 | int i; |
| 50 | int err = 0; |
| 51 | |
Andy Fleming | e139345 | 2005-08-24 18:46:21 -0500 | [diff] [blame] | 52 | if (NULL == bus || NULL == bus->name || |
| 53 | NULL == bus->read || |
| 54 | NULL == bus->write) |
| 55 | return -EINVAL; |
| 56 | |
Adrian Bunk | d1e7fe4 | 2008-02-20 02:13:53 +0200 | [diff] [blame] | 57 | mutex_init(&bus->mdio_lock); |
| 58 | |
Andy Fleming | e139345 | 2005-08-24 18:46:21 -0500 | [diff] [blame] | 59 | if (bus->reset) |
| 60 | bus->reset(bus); |
| 61 | |
| 62 | for (i = 0; i < PHY_MAX_ADDR; i++) { |
Lennert Buytenhek | 4fd5f81 | 2008-08-26 13:08:46 +0200 | [diff] [blame^] | 63 | bus->phy_map[i] = NULL; |
| 64 | if ((bus->phy_mask & (1 << i)) == 0) { |
| 65 | struct phy_device *phydev; |
Andy Fleming | e139345 | 2005-08-24 18:46:21 -0500 | [diff] [blame] | 66 | |
Lennert Buytenhek | 4fd5f81 | 2008-08-26 13:08:46 +0200 | [diff] [blame^] | 67 | phydev = mdiobus_scan(bus, i); |
| 68 | if (IS_ERR(phydev)) |
| 69 | err = PTR_ERR(phydev); |
Herbert Valerio Riedel | 64b1c2b | 2006-05-10 12:12:57 -0400 | [diff] [blame] | 70 | } |
Andy Fleming | e139345 | 2005-08-24 18:46:21 -0500 | [diff] [blame] | 71 | } |
| 72 | |
| 73 | pr_info("%s: probed\n", bus->name); |
| 74 | |
| 75 | return err; |
| 76 | } |
| 77 | EXPORT_SYMBOL(mdiobus_register); |
| 78 | |
| 79 | void mdiobus_unregister(struct mii_bus *bus) |
| 80 | { |
| 81 | int i; |
| 82 | |
| 83 | for (i = 0; i < PHY_MAX_ADDR; i++) { |
Anton Vorontsov | 6f4a7f4 | 2007-12-04 16:17:33 +0300 | [diff] [blame] | 84 | if (bus->phy_map[i]) |
Andy Fleming | e139345 | 2005-08-24 18:46:21 -0500 | [diff] [blame] | 85 | device_unregister(&bus->phy_map[i]->dev); |
Andy Fleming | e139345 | 2005-08-24 18:46:21 -0500 | [diff] [blame] | 86 | } |
| 87 | } |
| 88 | EXPORT_SYMBOL(mdiobus_unregister); |
| 89 | |
Lennert Buytenhek | 4fd5f81 | 2008-08-26 13:08:46 +0200 | [diff] [blame^] | 90 | struct phy_device *mdiobus_scan(struct mii_bus *bus, int addr) |
| 91 | { |
| 92 | struct phy_device *phydev; |
| 93 | int err; |
| 94 | |
| 95 | phydev = get_phy_device(bus, addr); |
| 96 | if (IS_ERR(phydev) || phydev == NULL) |
| 97 | return phydev; |
| 98 | |
| 99 | /* There's a PHY at this address |
| 100 | * We need to set: |
| 101 | * 1) IRQ |
| 102 | * 2) bus_id |
| 103 | * 3) parent |
| 104 | * 4) bus |
| 105 | * 5) mii_bus |
| 106 | * And, we need to register it */ |
| 107 | |
| 108 | phydev->irq = bus->irq != NULL ? bus->irq[addr] : PHY_POLL; |
| 109 | |
| 110 | phydev->dev.parent = bus->dev; |
| 111 | phydev->dev.bus = &mdio_bus_type; |
| 112 | snprintf(phydev->dev.bus_id, BUS_ID_SIZE, PHY_ID_FMT, bus->id, addr); |
| 113 | |
| 114 | phydev->bus = bus; |
| 115 | |
| 116 | /* Run all of the fixups for this PHY */ |
| 117 | phy_scan_fixups(phydev); |
| 118 | |
| 119 | err = device_register(&phydev->dev); |
| 120 | if (err) { |
| 121 | printk(KERN_ERR "phy %d failed to register\n", addr); |
| 122 | phy_device_free(phydev); |
| 123 | phydev = NULL; |
| 124 | } |
| 125 | |
| 126 | bus->phy_map[addr] = phydev; |
| 127 | |
| 128 | return phydev; |
| 129 | } |
| 130 | EXPORT_SYMBOL(mdiobus_scan); |
| 131 | |
Randy Dunlap | b3df0da | 2007-03-06 02:41:48 -0800 | [diff] [blame] | 132 | /** |
| 133 | * mdio_bus_match - determine if given PHY driver supports the given PHY device |
| 134 | * @dev: target PHY device |
| 135 | * @drv: given PHY driver |
Andy Fleming | 00db818 | 2005-07-30 19:31:23 -0400 | [diff] [blame] | 136 | * |
Randy Dunlap | b3df0da | 2007-03-06 02:41:48 -0800 | [diff] [blame] | 137 | * Description: Given a PHY device, and a PHY driver, return 1 if |
| 138 | * the driver supports the device. Otherwise, return 0. |
Andy Fleming | 00db818 | 2005-07-30 19:31:23 -0400 | [diff] [blame] | 139 | */ |
| 140 | static int mdio_bus_match(struct device *dev, struct device_driver *drv) |
| 141 | { |
| 142 | struct phy_device *phydev = to_phy_device(dev); |
| 143 | struct phy_driver *phydrv = to_phy_driver(drv); |
| 144 | |
Kumar Gala | 5f708dd | 2007-06-28 13:26:06 -0500 | [diff] [blame] | 145 | return ((phydrv->phy_id & phydrv->phy_id_mask) == |
| 146 | (phydev->phy_id & phydrv->phy_id_mask)); |
Andy Fleming | 00db818 | 2005-07-30 19:31:23 -0400 | [diff] [blame] | 147 | } |
| 148 | |
| 149 | /* Suspend and resume. Copied from platform_suspend and |
| 150 | * platform_resume |
| 151 | */ |
Pavel Machek | 829ca9a | 2005-09-03 15:56:56 -0700 | [diff] [blame] | 152 | static int mdio_bus_suspend(struct device * dev, pm_message_t state) |
Andy Fleming | 00db818 | 2005-07-30 19:31:23 -0400 | [diff] [blame] | 153 | { |
| 154 | int ret = 0; |
| 155 | struct device_driver *drv = dev->driver; |
| 156 | |
Russell King | 9480e30 | 2005-10-28 09:52:56 -0700 | [diff] [blame] | 157 | if (drv && drv->suspend) |
| 158 | ret = drv->suspend(dev, state); |
| 159 | |
Andy Fleming | 00db818 | 2005-07-30 19:31:23 -0400 | [diff] [blame] | 160 | return ret; |
| 161 | } |
| 162 | |
| 163 | static int mdio_bus_resume(struct device * dev) |
| 164 | { |
| 165 | int ret = 0; |
| 166 | struct device_driver *drv = dev->driver; |
| 167 | |
Russell King | 9480e30 | 2005-10-28 09:52:56 -0700 | [diff] [blame] | 168 | if (drv && drv->resume) |
| 169 | ret = drv->resume(dev); |
| 170 | |
Andy Fleming | 00db818 | 2005-07-30 19:31:23 -0400 | [diff] [blame] | 171 | return ret; |
| 172 | } |
| 173 | |
| 174 | struct bus_type mdio_bus_type = { |
| 175 | .name = "mdio_bus", |
| 176 | .match = mdio_bus_match, |
| 177 | .suspend = mdio_bus_suspend, |
| 178 | .resume = mdio_bus_resume, |
| 179 | }; |
Vitaly Bordug | 11b0bac | 2006-08-14 23:00:29 -0700 | [diff] [blame] | 180 | EXPORT_SYMBOL(mdio_bus_type); |
Andy Fleming | 00db818 | 2005-07-30 19:31:23 -0400 | [diff] [blame] | 181 | |
Jeff Garzik | 67c4f3f | 2005-08-11 02:07:25 -0400 | [diff] [blame] | 182 | int __init mdio_bus_init(void) |
Andy Fleming | 00db818 | 2005-07-30 19:31:23 -0400 | [diff] [blame] | 183 | { |
| 184 | return bus_register(&mdio_bus_type); |
| 185 | } |
| 186 | |
Peter Chubb | dc85dec | 2005-09-03 14:05:06 -0700 | [diff] [blame] | 187 | void mdio_bus_exit(void) |
Andy Fleming | e139345 | 2005-08-24 18:46:21 -0500 | [diff] [blame] | 188 | { |
| 189 | bus_unregister(&mdio_bus_type); |
| 190 | } |