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