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 | /** |
Lennert Buytenhek | 298cf9b | 2008-10-08 16:29:57 -0700 | [diff] [blame] | 39 | * mdiobus_alloc - allocate a mii_bus structure |
| 40 | * |
| 41 | * Description: called by a bus driver to allocate an mii_bus |
| 42 | * structure to fill in. |
| 43 | */ |
| 44 | struct mii_bus *mdiobus_alloc(void) |
| 45 | { |
Lennert Buytenhek | 46abc02 | 2008-10-08 16:33:40 -0700 | [diff] [blame] | 46 | struct mii_bus *bus; |
| 47 | |
| 48 | bus = kzalloc(sizeof(*bus), GFP_KERNEL); |
| 49 | if (bus != NULL) |
| 50 | bus->state = MDIOBUS_ALLOCATED; |
| 51 | |
| 52 | return bus; |
Lennert Buytenhek | 298cf9b | 2008-10-08 16:29:57 -0700 | [diff] [blame] | 53 | } |
| 54 | EXPORT_SYMBOL(mdiobus_alloc); |
| 55 | |
| 56 | /** |
Lennert Buytenhek | 46abc02 | 2008-10-08 16:33:40 -0700 | [diff] [blame] | 57 | * mdiobus_release - mii_bus device release callback |
Randy Dunlap | 78c36b1 | 2008-10-13 18:46:22 -0700 | [diff] [blame^] | 58 | * @d: the target struct device that contains the mii_bus |
Lennert Buytenhek | 46abc02 | 2008-10-08 16:33:40 -0700 | [diff] [blame] | 59 | * |
| 60 | * Description: called when the last reference to an mii_bus is |
| 61 | * dropped, to free the underlying memory. |
| 62 | */ |
| 63 | static void mdiobus_release(struct device *d) |
| 64 | { |
| 65 | struct mii_bus *bus = to_mii_bus(d); |
| 66 | BUG_ON(bus->state != MDIOBUS_RELEASED); |
| 67 | kfree(bus); |
| 68 | } |
| 69 | |
| 70 | static struct class mdio_bus_class = { |
| 71 | .name = "mdio_bus", |
| 72 | .dev_release = mdiobus_release, |
| 73 | }; |
| 74 | |
| 75 | /** |
Randy Dunlap | b3df0da | 2007-03-06 02:41:48 -0800 | [diff] [blame] | 76 | * mdiobus_register - bring up all the PHYs on a given bus and attach them to bus |
| 77 | * @bus: target mii_bus |
Andy Fleming | e139345 | 2005-08-24 18:46:21 -0500 | [diff] [blame] | 78 | * |
Randy Dunlap | b3df0da | 2007-03-06 02:41:48 -0800 | [diff] [blame] | 79 | * Description: Called by a bus driver to bring up all the PHYs |
| 80 | * on a given bus, and attach them to the bus. |
| 81 | * |
| 82 | * Returns 0 on success or < 0 on error. |
Andy Fleming | e139345 | 2005-08-24 18:46:21 -0500 | [diff] [blame] | 83 | */ |
| 84 | int mdiobus_register(struct mii_bus *bus) |
| 85 | { |
| 86 | int i; |
| 87 | int err = 0; |
| 88 | |
Andy Fleming | e139345 | 2005-08-24 18:46:21 -0500 | [diff] [blame] | 89 | if (NULL == bus || NULL == bus->name || |
| 90 | NULL == bus->read || |
| 91 | NULL == bus->write) |
| 92 | return -EINVAL; |
| 93 | |
Lennert Buytenhek | 46abc02 | 2008-10-08 16:33:40 -0700 | [diff] [blame] | 94 | BUG_ON(bus->state != MDIOBUS_ALLOCATED && |
| 95 | bus->state != MDIOBUS_UNREGISTERED); |
| 96 | |
| 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); |
| 101 | |
| 102 | err = device_register(&bus->dev); |
| 103 | if (err) { |
| 104 | printk(KERN_ERR "mii_bus %s failed to register\n", bus->id); |
| 105 | return -EINVAL; |
| 106 | } |
| 107 | |
| 108 | bus->state = MDIOBUS_REGISTERED; |
| 109 | |
Adrian Bunk | d1e7fe4 | 2008-02-20 02:13:53 +0200 | [diff] [blame] | 110 | mutex_init(&bus->mdio_lock); |
| 111 | |
Andy Fleming | e139345 | 2005-08-24 18:46:21 -0500 | [diff] [blame] | 112 | if (bus->reset) |
| 113 | bus->reset(bus); |
| 114 | |
| 115 | for (i = 0; i < PHY_MAX_ADDR; i++) { |
Lennert Buytenhek | 4fd5f81 | 2008-08-26 13:08:46 +0200 | [diff] [blame] | 116 | bus->phy_map[i] = NULL; |
| 117 | if ((bus->phy_mask & (1 << i)) == 0) { |
| 118 | struct phy_device *phydev; |
Andy Fleming | e139345 | 2005-08-24 18:46:21 -0500 | [diff] [blame] | 119 | |
Lennert Buytenhek | 4fd5f81 | 2008-08-26 13:08:46 +0200 | [diff] [blame] | 120 | phydev = mdiobus_scan(bus, i); |
| 121 | if (IS_ERR(phydev)) |
| 122 | err = PTR_ERR(phydev); |
Herbert Valerio Riedel | 64b1c2b | 2006-05-10 12:12:57 -0400 | [diff] [blame] | 123 | } |
Andy Fleming | e139345 | 2005-08-24 18:46:21 -0500 | [diff] [blame] | 124 | } |
| 125 | |
| 126 | pr_info("%s: probed\n", bus->name); |
| 127 | |
| 128 | return err; |
| 129 | } |
| 130 | EXPORT_SYMBOL(mdiobus_register); |
| 131 | |
| 132 | void mdiobus_unregister(struct mii_bus *bus) |
| 133 | { |
| 134 | int i; |
| 135 | |
Lennert Buytenhek | 46abc02 | 2008-10-08 16:33:40 -0700 | [diff] [blame] | 136 | BUG_ON(bus->state != MDIOBUS_REGISTERED); |
| 137 | bus->state = MDIOBUS_UNREGISTERED; |
| 138 | |
| 139 | device_unregister(&bus->dev); |
Andy Fleming | e139345 | 2005-08-24 18:46:21 -0500 | [diff] [blame] | 140 | for (i = 0; i < PHY_MAX_ADDR; i++) { |
Anton Vorontsov | 6f4a7f4 | 2007-12-04 16:17:33 +0300 | [diff] [blame] | 141 | if (bus->phy_map[i]) |
Andy Fleming | e139345 | 2005-08-24 18:46:21 -0500 | [diff] [blame] | 142 | device_unregister(&bus->phy_map[i]->dev); |
Andy Fleming | e139345 | 2005-08-24 18:46:21 -0500 | [diff] [blame] | 143 | } |
| 144 | } |
| 145 | EXPORT_SYMBOL(mdiobus_unregister); |
| 146 | |
Lennert Buytenhek | 298cf9b | 2008-10-08 16:29:57 -0700 | [diff] [blame] | 147 | /** |
| 148 | * mdiobus_free - free a struct mii_bus |
| 149 | * @bus: mii_bus to free |
| 150 | * |
Lennert Buytenhek | 46abc02 | 2008-10-08 16:33:40 -0700 | [diff] [blame] | 151 | * This function releases the reference to the underlying device |
| 152 | * object in the mii_bus. If this is the last reference, the mii_bus |
| 153 | * will be freed. |
Lennert Buytenhek | 298cf9b | 2008-10-08 16:29:57 -0700 | [diff] [blame] | 154 | */ |
| 155 | void mdiobus_free(struct mii_bus *bus) |
| 156 | { |
Lennert Buytenhek | 46abc02 | 2008-10-08 16:33:40 -0700 | [diff] [blame] | 157 | /* |
| 158 | * For compatibility with error handling in drivers. |
| 159 | */ |
| 160 | if (bus->state == MDIOBUS_ALLOCATED) { |
| 161 | kfree(bus); |
| 162 | return; |
| 163 | } |
| 164 | |
| 165 | BUG_ON(bus->state != MDIOBUS_UNREGISTERED); |
| 166 | bus->state = MDIOBUS_RELEASED; |
| 167 | |
| 168 | put_device(&bus->dev); |
Lennert Buytenhek | 298cf9b | 2008-10-08 16:29:57 -0700 | [diff] [blame] | 169 | } |
| 170 | EXPORT_SYMBOL(mdiobus_free); |
| 171 | |
Lennert Buytenhek | 4fd5f81 | 2008-08-26 13:08:46 +0200 | [diff] [blame] | 172 | struct phy_device *mdiobus_scan(struct mii_bus *bus, int addr) |
| 173 | { |
| 174 | struct phy_device *phydev; |
| 175 | int err; |
| 176 | |
| 177 | phydev = get_phy_device(bus, addr); |
| 178 | if (IS_ERR(phydev) || phydev == NULL) |
| 179 | return phydev; |
| 180 | |
| 181 | /* There's a PHY at this address |
| 182 | * We need to set: |
| 183 | * 1) IRQ |
| 184 | * 2) bus_id |
| 185 | * 3) parent |
| 186 | * 4) bus |
| 187 | * 5) mii_bus |
| 188 | * And, we need to register it */ |
| 189 | |
| 190 | phydev->irq = bus->irq != NULL ? bus->irq[addr] : PHY_POLL; |
| 191 | |
Lennert Buytenhek | 18ee49d | 2008-10-01 15:41:33 +0000 | [diff] [blame] | 192 | phydev->dev.parent = bus->parent; |
Lennert Buytenhek | 4fd5f81 | 2008-08-26 13:08:46 +0200 | [diff] [blame] | 193 | phydev->dev.bus = &mdio_bus_type; |
| 194 | snprintf(phydev->dev.bus_id, BUS_ID_SIZE, PHY_ID_FMT, bus->id, addr); |
| 195 | |
| 196 | phydev->bus = bus; |
| 197 | |
| 198 | /* Run all of the fixups for this PHY */ |
| 199 | phy_scan_fixups(phydev); |
| 200 | |
| 201 | err = device_register(&phydev->dev); |
| 202 | if (err) { |
| 203 | printk(KERN_ERR "phy %d failed to register\n", addr); |
| 204 | phy_device_free(phydev); |
| 205 | phydev = NULL; |
| 206 | } |
| 207 | |
| 208 | bus->phy_map[addr] = phydev; |
| 209 | |
| 210 | return phydev; |
| 211 | } |
| 212 | EXPORT_SYMBOL(mdiobus_scan); |
| 213 | |
Randy Dunlap | b3df0da | 2007-03-06 02:41:48 -0800 | [diff] [blame] | 214 | /** |
Lennert Buytenhek | 2e88810 | 2008-09-29 17:12:35 +0000 | [diff] [blame] | 215 | * mdiobus_read - Convenience function for reading a given MII mgmt register |
| 216 | * @bus: the mii_bus struct |
| 217 | * @addr: the phy address |
| 218 | * @regnum: register number to read |
| 219 | * |
| 220 | * NOTE: MUST NOT be called from interrupt context, |
| 221 | * because the bus read/write functions may wait for an interrupt |
| 222 | * to conclude the operation. |
| 223 | */ |
| 224 | int mdiobus_read(struct mii_bus *bus, int addr, u16 regnum) |
| 225 | { |
| 226 | int retval; |
| 227 | |
| 228 | BUG_ON(in_interrupt()); |
| 229 | |
| 230 | mutex_lock(&bus->mdio_lock); |
| 231 | retval = bus->read(bus, addr, regnum); |
| 232 | mutex_unlock(&bus->mdio_lock); |
| 233 | |
| 234 | return retval; |
| 235 | } |
| 236 | EXPORT_SYMBOL(mdiobus_read); |
| 237 | |
| 238 | /** |
| 239 | * mdiobus_write - Convenience function for writing a given MII mgmt register |
| 240 | * @bus: the mii_bus struct |
| 241 | * @addr: the phy address |
| 242 | * @regnum: register number to write |
| 243 | * @val: value to write to @regnum |
| 244 | * |
| 245 | * NOTE: MUST NOT be called from interrupt context, |
| 246 | * because the bus read/write functions may wait for an interrupt |
| 247 | * to conclude the operation. |
| 248 | */ |
| 249 | int mdiobus_write(struct mii_bus *bus, int addr, u16 regnum, u16 val) |
| 250 | { |
| 251 | int err; |
| 252 | |
| 253 | BUG_ON(in_interrupt()); |
| 254 | |
| 255 | mutex_lock(&bus->mdio_lock); |
| 256 | err = bus->write(bus, addr, regnum, val); |
| 257 | mutex_unlock(&bus->mdio_lock); |
| 258 | |
| 259 | return err; |
| 260 | } |
| 261 | EXPORT_SYMBOL(mdiobus_write); |
| 262 | |
| 263 | /** |
Randy Dunlap | b3df0da | 2007-03-06 02:41:48 -0800 | [diff] [blame] | 264 | * mdio_bus_match - determine if given PHY driver supports the given PHY device |
| 265 | * @dev: target PHY device |
| 266 | * @drv: given PHY driver |
Andy Fleming | 00db818 | 2005-07-30 19:31:23 -0400 | [diff] [blame] | 267 | * |
Randy Dunlap | b3df0da | 2007-03-06 02:41:48 -0800 | [diff] [blame] | 268 | * Description: Given a PHY device, and a PHY driver, return 1 if |
| 269 | * the driver supports the device. Otherwise, return 0. |
Andy Fleming | 00db818 | 2005-07-30 19:31:23 -0400 | [diff] [blame] | 270 | */ |
| 271 | static int mdio_bus_match(struct device *dev, struct device_driver *drv) |
| 272 | { |
| 273 | struct phy_device *phydev = to_phy_device(dev); |
| 274 | struct phy_driver *phydrv = to_phy_driver(drv); |
| 275 | |
Kumar Gala | 5f708dd | 2007-06-28 13:26:06 -0500 | [diff] [blame] | 276 | return ((phydrv->phy_id & phydrv->phy_id_mask) == |
| 277 | (phydev->phy_id & phydrv->phy_id_mask)); |
Andy Fleming | 00db818 | 2005-07-30 19:31:23 -0400 | [diff] [blame] | 278 | } |
| 279 | |
| 280 | /* Suspend and resume. Copied from platform_suspend and |
| 281 | * platform_resume |
| 282 | */ |
Pavel Machek | 829ca9a | 2005-09-03 15:56:56 -0700 | [diff] [blame] | 283 | static int mdio_bus_suspend(struct device * dev, pm_message_t state) |
Andy Fleming | 00db818 | 2005-07-30 19:31:23 -0400 | [diff] [blame] | 284 | { |
| 285 | int ret = 0; |
| 286 | struct device_driver *drv = dev->driver; |
| 287 | |
Russell King | 9480e30 | 2005-10-28 09:52:56 -0700 | [diff] [blame] | 288 | if (drv && drv->suspend) |
| 289 | ret = drv->suspend(dev, state); |
| 290 | |
Andy Fleming | 00db818 | 2005-07-30 19:31:23 -0400 | [diff] [blame] | 291 | return ret; |
| 292 | } |
| 293 | |
| 294 | static int mdio_bus_resume(struct device * dev) |
| 295 | { |
| 296 | int ret = 0; |
| 297 | struct device_driver *drv = dev->driver; |
| 298 | |
Russell King | 9480e30 | 2005-10-28 09:52:56 -0700 | [diff] [blame] | 299 | if (drv && drv->resume) |
| 300 | ret = drv->resume(dev); |
| 301 | |
Andy Fleming | 00db818 | 2005-07-30 19:31:23 -0400 | [diff] [blame] | 302 | return ret; |
| 303 | } |
| 304 | |
| 305 | struct bus_type mdio_bus_type = { |
| 306 | .name = "mdio_bus", |
| 307 | .match = mdio_bus_match, |
| 308 | .suspend = mdio_bus_suspend, |
| 309 | .resume = mdio_bus_resume, |
| 310 | }; |
Vitaly Bordug | 11b0bac | 2006-08-14 23:00:29 -0700 | [diff] [blame] | 311 | EXPORT_SYMBOL(mdio_bus_type); |
Andy Fleming | 00db818 | 2005-07-30 19:31:23 -0400 | [diff] [blame] | 312 | |
Jeff Garzik | 67c4f3f | 2005-08-11 02:07:25 -0400 | [diff] [blame] | 313 | int __init mdio_bus_init(void) |
Andy Fleming | 00db818 | 2005-07-30 19:31:23 -0400 | [diff] [blame] | 314 | { |
Lennert Buytenhek | 46abc02 | 2008-10-08 16:33:40 -0700 | [diff] [blame] | 315 | int ret; |
| 316 | |
| 317 | ret = class_register(&mdio_bus_class); |
| 318 | if (!ret) { |
| 319 | ret = bus_register(&mdio_bus_type); |
| 320 | if (ret) |
| 321 | class_unregister(&mdio_bus_class); |
| 322 | } |
| 323 | |
| 324 | return ret; |
Andy Fleming | 00db818 | 2005-07-30 19:31:23 -0400 | [diff] [blame] | 325 | } |
| 326 | |
Peter Chubb | dc85dec | 2005-09-03 14:05:06 -0700 | [diff] [blame] | 327 | void mdio_bus_exit(void) |
Andy Fleming | e139345 | 2005-08-24 18:46:21 -0500 | [diff] [blame] | 328 | { |
Lennert Buytenhek | 46abc02 | 2008-10-08 16:33:40 -0700 | [diff] [blame] | 329 | class_unregister(&mdio_bus_class); |
Andy Fleming | e139345 | 2005-08-24 18:46:21 -0500 | [diff] [blame] | 330 | bus_unregister(&mdio_bus_type); |
| 331 | } |