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 | */ |
Joe Perches | 8d24248 | 2012-06-09 07:49:07 +0000 | [diff] [blame^] | 16 | |
| 17 | #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt |
| 18 | |
Andy Fleming | 00db818 | 2005-07-30 19:31:23 -0400 | [diff] [blame] | 19 | #include <linux/kernel.h> |
Andy Fleming | 00db818 | 2005-07-30 19:31:23 -0400 | [diff] [blame] | 20 | #include <linux/string.h> |
| 21 | #include <linux/errno.h> |
| 22 | #include <linux/unistd.h> |
| 23 | #include <linux/slab.h> |
| 24 | #include <linux/interrupt.h> |
| 25 | #include <linux/init.h> |
| 26 | #include <linux/delay.h> |
Anton Vorontsov | 3d1e4db | 2009-02-01 00:53:34 -0800 | [diff] [blame] | 27 | #include <linux/device.h> |
Andy Fleming | 00db818 | 2005-07-30 19:31:23 -0400 | [diff] [blame] | 28 | #include <linux/netdevice.h> |
| 29 | #include <linux/etherdevice.h> |
| 30 | #include <linux/skbuff.h> |
| 31 | #include <linux/spinlock.h> |
| 32 | #include <linux/mm.h> |
| 33 | #include <linux/module.h> |
Andy Fleming | 00db818 | 2005-07-30 19:31:23 -0400 | [diff] [blame] | 34 | #include <linux/mii.h> |
| 35 | #include <linux/ethtool.h> |
| 36 | #include <linux/phy.h> |
| 37 | |
| 38 | #include <asm/io.h> |
| 39 | #include <asm/irq.h> |
| 40 | #include <asm/uaccess.h> |
| 41 | |
Randy Dunlap | b3df0da | 2007-03-06 02:41:48 -0800 | [diff] [blame] | 42 | /** |
Timur Tabi | eb8a54a7 | 2012-01-12 15:23:04 -0800 | [diff] [blame] | 43 | * mdiobus_alloc_size - allocate a mii_bus structure |
Randy Dunlap | af58f1d | 2012-01-21 09:03:04 +0000 | [diff] [blame] | 44 | * @size: extra amount of memory to allocate for private storage. |
| 45 | * If non-zero, then bus->priv is points to that memory. |
Lennert Buytenhek | 298cf9b | 2008-10-08 16:29:57 -0700 | [diff] [blame] | 46 | * |
| 47 | * Description: called by a bus driver to allocate an mii_bus |
| 48 | * structure to fill in. |
| 49 | */ |
Timur Tabi | eb8a54a7 | 2012-01-12 15:23:04 -0800 | [diff] [blame] | 50 | struct mii_bus *mdiobus_alloc_size(size_t size) |
Lennert Buytenhek | 298cf9b | 2008-10-08 16:29:57 -0700 | [diff] [blame] | 51 | { |
Lennert Buytenhek | 46abc02 | 2008-10-08 16:33:40 -0700 | [diff] [blame] | 52 | struct mii_bus *bus; |
Timur Tabi | eb8a54a7 | 2012-01-12 15:23:04 -0800 | [diff] [blame] | 53 | size_t aligned_size = ALIGN(sizeof(*bus), NETDEV_ALIGN); |
| 54 | size_t alloc_size; |
Lennert Buytenhek | 46abc02 | 2008-10-08 16:33:40 -0700 | [diff] [blame] | 55 | |
Timur Tabi | eb8a54a7 | 2012-01-12 15:23:04 -0800 | [diff] [blame] | 56 | /* If we alloc extra space, it should be aligned */ |
| 57 | if (size) |
| 58 | alloc_size = aligned_size + size; |
| 59 | else |
| 60 | alloc_size = sizeof(*bus); |
| 61 | |
| 62 | bus = kzalloc(alloc_size, GFP_KERNEL); |
| 63 | if (bus) { |
Lennert Buytenhek | 46abc02 | 2008-10-08 16:33:40 -0700 | [diff] [blame] | 64 | bus->state = MDIOBUS_ALLOCATED; |
Timur Tabi | eb8a54a7 | 2012-01-12 15:23:04 -0800 | [diff] [blame] | 65 | if (size) |
| 66 | bus->priv = (void *)bus + aligned_size; |
| 67 | } |
Lennert Buytenhek | 46abc02 | 2008-10-08 16:33:40 -0700 | [diff] [blame] | 68 | |
| 69 | return bus; |
Lennert Buytenhek | 298cf9b | 2008-10-08 16:29:57 -0700 | [diff] [blame] | 70 | } |
Timur Tabi | eb8a54a7 | 2012-01-12 15:23:04 -0800 | [diff] [blame] | 71 | EXPORT_SYMBOL(mdiobus_alloc_size); |
Lennert Buytenhek | 298cf9b | 2008-10-08 16:29:57 -0700 | [diff] [blame] | 72 | |
| 73 | /** |
Lennert Buytenhek | 46abc02 | 2008-10-08 16:33:40 -0700 | [diff] [blame] | 74 | * mdiobus_release - mii_bus device release callback |
Randy Dunlap | 78c36b1 | 2008-10-13 18:46:22 -0700 | [diff] [blame] | 75 | * @d: the target struct device that contains the mii_bus |
Lennert Buytenhek | 46abc02 | 2008-10-08 16:33:40 -0700 | [diff] [blame] | 76 | * |
| 77 | * Description: called when the last reference to an mii_bus is |
| 78 | * dropped, to free the underlying memory. |
| 79 | */ |
| 80 | static void mdiobus_release(struct device *d) |
| 81 | { |
| 82 | struct mii_bus *bus = to_mii_bus(d); |
Krzysztof Halasa | 161c8d2 | 2008-12-25 16:50:41 -0800 | [diff] [blame] | 83 | BUG_ON(bus->state != MDIOBUS_RELEASED && |
| 84 | /* for compatibility with error handling in drivers */ |
| 85 | bus->state != MDIOBUS_ALLOCATED); |
Lennert Buytenhek | 46abc02 | 2008-10-08 16:33:40 -0700 | [diff] [blame] | 86 | kfree(bus); |
| 87 | } |
| 88 | |
| 89 | static struct class mdio_bus_class = { |
| 90 | .name = "mdio_bus", |
| 91 | .dev_release = mdiobus_release, |
| 92 | }; |
| 93 | |
Bjørn Mork | b943fbb | 2012-05-11 05:47:01 +0000 | [diff] [blame] | 94 | #if IS_ENABLED(CONFIG_OF_MDIO) |
David Daney | 2510602 | 2012-05-02 15:16:37 +0000 | [diff] [blame] | 95 | /* Helper function for of_mdio_find_bus */ |
| 96 | static int of_mdio_bus_match(struct device *dev, void *mdio_bus_np) |
| 97 | { |
| 98 | return dev->of_node == mdio_bus_np; |
| 99 | } |
| 100 | /** |
| 101 | * of_mdio_find_bus - Given an mii_bus node, find the mii_bus. |
| 102 | * @mdio_np: Pointer to the mii_bus. |
| 103 | * |
| 104 | * Returns a pointer to the mii_bus, or NULL if none found. |
| 105 | * |
| 106 | * Because the association of a device_node and mii_bus is made via |
| 107 | * of_mdiobus_register(), the mii_bus cannot be found before it is |
| 108 | * registered with of_mdiobus_register(). |
| 109 | * |
| 110 | */ |
| 111 | struct mii_bus *of_mdio_find_bus(struct device_node *mdio_bus_np) |
| 112 | { |
| 113 | struct device *d; |
| 114 | |
| 115 | if (!mdio_bus_np) |
| 116 | return NULL; |
| 117 | |
| 118 | d = class_find_device(&mdio_bus_class, NULL, mdio_bus_np, |
| 119 | of_mdio_bus_match); |
| 120 | |
| 121 | return d ? to_mii_bus(d) : NULL; |
| 122 | } |
| 123 | EXPORT_SYMBOL(of_mdio_find_bus); |
| 124 | #endif |
| 125 | |
Lennert Buytenhek | 46abc02 | 2008-10-08 16:33:40 -0700 | [diff] [blame] | 126 | /** |
Randy Dunlap | b3df0da | 2007-03-06 02:41:48 -0800 | [diff] [blame] | 127 | * mdiobus_register - bring up all the PHYs on a given bus and attach them to bus |
| 128 | * @bus: target mii_bus |
Andy Fleming | e139345 | 2005-08-24 18:46:21 -0500 | [diff] [blame] | 129 | * |
Randy Dunlap | b3df0da | 2007-03-06 02:41:48 -0800 | [diff] [blame] | 130 | * Description: Called by a bus driver to bring up all the PHYs |
| 131 | * on a given bus, and attach them to the bus. |
| 132 | * |
| 133 | * Returns 0 on success or < 0 on error. |
Andy Fleming | e139345 | 2005-08-24 18:46:21 -0500 | [diff] [blame] | 134 | */ |
| 135 | int mdiobus_register(struct mii_bus *bus) |
| 136 | { |
Krzysztof Halasa | 161c8d2 | 2008-12-25 16:50:41 -0800 | [diff] [blame] | 137 | int i, err; |
Andy Fleming | e139345 | 2005-08-24 18:46:21 -0500 | [diff] [blame] | 138 | |
Andy Fleming | e139345 | 2005-08-24 18:46:21 -0500 | [diff] [blame] | 139 | if (NULL == bus || NULL == bus->name || |
| 140 | NULL == bus->read || |
| 141 | NULL == bus->write) |
| 142 | return -EINVAL; |
| 143 | |
Lennert Buytenhek | 46abc02 | 2008-10-08 16:33:40 -0700 | [diff] [blame] | 144 | BUG_ON(bus->state != MDIOBUS_ALLOCATED && |
| 145 | bus->state != MDIOBUS_UNREGISTERED); |
| 146 | |
| 147 | bus->dev.parent = bus->parent; |
| 148 | bus->dev.class = &mdio_bus_class; |
| 149 | bus->dev.groups = NULL; |
Stephen Hemminger | 036b668 | 2009-02-26 10:19:36 +0000 | [diff] [blame] | 150 | dev_set_name(&bus->dev, "%s", bus->id); |
Lennert Buytenhek | 46abc02 | 2008-10-08 16:33:40 -0700 | [diff] [blame] | 151 | |
| 152 | err = device_register(&bus->dev); |
| 153 | if (err) { |
Joe Perches | 8d24248 | 2012-06-09 07:49:07 +0000 | [diff] [blame^] | 154 | pr_err("mii_bus %s failed to register\n", bus->id); |
Lennert Buytenhek | 46abc02 | 2008-10-08 16:33:40 -0700 | [diff] [blame] | 155 | return -EINVAL; |
| 156 | } |
| 157 | |
Adrian Bunk | d1e7fe4 | 2008-02-20 02:13:53 +0200 | [diff] [blame] | 158 | mutex_init(&bus->mdio_lock); |
| 159 | |
Andy Fleming | e139345 | 2005-08-24 18:46:21 -0500 | [diff] [blame] | 160 | if (bus->reset) |
| 161 | bus->reset(bus); |
| 162 | |
| 163 | for (i = 0; i < PHY_MAX_ADDR; i++) { |
Lennert Buytenhek | 4fd5f81 | 2008-08-26 13:08:46 +0200 | [diff] [blame] | 164 | if ((bus->phy_mask & (1 << i)) == 0) { |
| 165 | struct phy_device *phydev; |
Andy Fleming | e139345 | 2005-08-24 18:46:21 -0500 | [diff] [blame] | 166 | |
Lennert Buytenhek | 4fd5f81 | 2008-08-26 13:08:46 +0200 | [diff] [blame] | 167 | phydev = mdiobus_scan(bus, i); |
Krzysztof Halasa | 161c8d2 | 2008-12-25 16:50:41 -0800 | [diff] [blame] | 168 | if (IS_ERR(phydev)) { |
Lennert Buytenhek | 4fd5f81 | 2008-08-26 13:08:46 +0200 | [diff] [blame] | 169 | err = PTR_ERR(phydev); |
Krzysztof Halasa | 161c8d2 | 2008-12-25 16:50:41 -0800 | [diff] [blame] | 170 | goto error; |
| 171 | } |
Herbert Valerio Riedel | 64b1c2b | 2006-05-10 12:12:57 -0400 | [diff] [blame] | 172 | } |
Andy Fleming | e139345 | 2005-08-24 18:46:21 -0500 | [diff] [blame] | 173 | } |
| 174 | |
Krzysztof Halasa | 161c8d2 | 2008-12-25 16:50:41 -0800 | [diff] [blame] | 175 | bus->state = MDIOBUS_REGISTERED; |
Andy Fleming | e139345 | 2005-08-24 18:46:21 -0500 | [diff] [blame] | 176 | pr_info("%s: probed\n", bus->name); |
Krzysztof Halasa | 161c8d2 | 2008-12-25 16:50:41 -0800 | [diff] [blame] | 177 | return 0; |
Andy Fleming | e139345 | 2005-08-24 18:46:21 -0500 | [diff] [blame] | 178 | |
Krzysztof Halasa | 161c8d2 | 2008-12-25 16:50:41 -0800 | [diff] [blame] | 179 | error: |
| 180 | while (--i >= 0) { |
| 181 | if (bus->phy_map[i]) |
| 182 | device_unregister(&bus->phy_map[i]->dev); |
| 183 | } |
| 184 | device_del(&bus->dev); |
Andy Fleming | e139345 | 2005-08-24 18:46:21 -0500 | [diff] [blame] | 185 | return err; |
| 186 | } |
| 187 | EXPORT_SYMBOL(mdiobus_register); |
| 188 | |
| 189 | void mdiobus_unregister(struct mii_bus *bus) |
| 190 | { |
| 191 | int i; |
| 192 | |
Lennert Buytenhek | 46abc02 | 2008-10-08 16:33:40 -0700 | [diff] [blame] | 193 | BUG_ON(bus->state != MDIOBUS_REGISTERED); |
| 194 | bus->state = MDIOBUS_UNREGISTERED; |
| 195 | |
Lennert Buytenhek | 3e44017 | 2008-11-09 05:34:47 +0100 | [diff] [blame] | 196 | device_del(&bus->dev); |
Andy Fleming | e139345 | 2005-08-24 18:46:21 -0500 | [diff] [blame] | 197 | for (i = 0; i < PHY_MAX_ADDR; i++) { |
Anton Vorontsov | 6f4a7f4 | 2007-12-04 16:17:33 +0300 | [diff] [blame] | 198 | if (bus->phy_map[i]) |
Andy Fleming | e139345 | 2005-08-24 18:46:21 -0500 | [diff] [blame] | 199 | device_unregister(&bus->phy_map[i]->dev); |
Grant Likely | 4dea547 | 2009-04-25 12:52:46 +0000 | [diff] [blame] | 200 | bus->phy_map[i] = NULL; |
Andy Fleming | e139345 | 2005-08-24 18:46:21 -0500 | [diff] [blame] | 201 | } |
| 202 | } |
| 203 | EXPORT_SYMBOL(mdiobus_unregister); |
| 204 | |
Lennert Buytenhek | 298cf9b | 2008-10-08 16:29:57 -0700 | [diff] [blame] | 205 | /** |
| 206 | * mdiobus_free - free a struct mii_bus |
| 207 | * @bus: mii_bus to free |
| 208 | * |
Lennert Buytenhek | 46abc02 | 2008-10-08 16:33:40 -0700 | [diff] [blame] | 209 | * This function releases the reference to the underlying device |
| 210 | * object in the mii_bus. If this is the last reference, the mii_bus |
| 211 | * will be freed. |
Lennert Buytenhek | 298cf9b | 2008-10-08 16:29:57 -0700 | [diff] [blame] | 212 | */ |
| 213 | void mdiobus_free(struct mii_bus *bus) |
| 214 | { |
Lennert Buytenhek | 46abc02 | 2008-10-08 16:33:40 -0700 | [diff] [blame] | 215 | /* |
| 216 | * For compatibility with error handling in drivers. |
| 217 | */ |
| 218 | if (bus->state == MDIOBUS_ALLOCATED) { |
| 219 | kfree(bus); |
| 220 | return; |
| 221 | } |
| 222 | |
| 223 | BUG_ON(bus->state != MDIOBUS_UNREGISTERED); |
| 224 | bus->state = MDIOBUS_RELEASED; |
| 225 | |
| 226 | put_device(&bus->dev); |
Lennert Buytenhek | 298cf9b | 2008-10-08 16:29:57 -0700 | [diff] [blame] | 227 | } |
| 228 | EXPORT_SYMBOL(mdiobus_free); |
| 229 | |
Lennert Buytenhek | 4fd5f81 | 2008-08-26 13:08:46 +0200 | [diff] [blame] | 230 | struct phy_device *mdiobus_scan(struct mii_bus *bus, int addr) |
| 231 | { |
| 232 | struct phy_device *phydev; |
| 233 | int err; |
| 234 | |
| 235 | phydev = get_phy_device(bus, addr); |
| 236 | if (IS_ERR(phydev) || phydev == NULL) |
| 237 | return phydev; |
| 238 | |
Grant Likely | 4dea547 | 2009-04-25 12:52:46 +0000 | [diff] [blame] | 239 | err = phy_device_register(phydev); |
Lennert Buytenhek | 4fd5f81 | 2008-08-26 13:08:46 +0200 | [diff] [blame] | 240 | if (err) { |
Lennert Buytenhek | 4fd5f81 | 2008-08-26 13:08:46 +0200 | [diff] [blame] | 241 | phy_device_free(phydev); |
Grant Likely | 4dea547 | 2009-04-25 12:52:46 +0000 | [diff] [blame] | 242 | return NULL; |
Lennert Buytenhek | 4fd5f81 | 2008-08-26 13:08:46 +0200 | [diff] [blame] | 243 | } |
| 244 | |
Lennert Buytenhek | 4fd5f81 | 2008-08-26 13:08:46 +0200 | [diff] [blame] | 245 | return phydev; |
| 246 | } |
| 247 | EXPORT_SYMBOL(mdiobus_scan); |
| 248 | |
Randy Dunlap | b3df0da | 2007-03-06 02:41:48 -0800 | [diff] [blame] | 249 | /** |
Lennert Buytenhek | 2e88810 | 2008-09-29 17:12:35 +0000 | [diff] [blame] | 250 | * mdiobus_read - Convenience function for reading a given MII mgmt register |
| 251 | * @bus: the mii_bus struct |
| 252 | * @addr: the phy address |
| 253 | * @regnum: register number to read |
| 254 | * |
| 255 | * NOTE: MUST NOT be called from interrupt context, |
| 256 | * because the bus read/write functions may wait for an interrupt |
| 257 | * to conclude the operation. |
| 258 | */ |
Jason Gunthorpe | abf35df | 2010-03-09 09:17:42 +0000 | [diff] [blame] | 259 | int mdiobus_read(struct mii_bus *bus, int addr, u32 regnum) |
Lennert Buytenhek | 2e88810 | 2008-09-29 17:12:35 +0000 | [diff] [blame] | 260 | { |
| 261 | int retval; |
| 262 | |
| 263 | BUG_ON(in_interrupt()); |
| 264 | |
| 265 | mutex_lock(&bus->mdio_lock); |
| 266 | retval = bus->read(bus, addr, regnum); |
| 267 | mutex_unlock(&bus->mdio_lock); |
| 268 | |
| 269 | return retval; |
| 270 | } |
| 271 | EXPORT_SYMBOL(mdiobus_read); |
| 272 | |
| 273 | /** |
| 274 | * mdiobus_write - Convenience function for writing a given MII mgmt register |
| 275 | * @bus: the mii_bus struct |
| 276 | * @addr: the phy address |
| 277 | * @regnum: register number to write |
| 278 | * @val: value to write to @regnum |
| 279 | * |
| 280 | * NOTE: MUST NOT be called from interrupt context, |
| 281 | * because the bus read/write functions may wait for an interrupt |
| 282 | * to conclude the operation. |
| 283 | */ |
Jason Gunthorpe | abf35df | 2010-03-09 09:17:42 +0000 | [diff] [blame] | 284 | int mdiobus_write(struct mii_bus *bus, int addr, u32 regnum, u16 val) |
Lennert Buytenhek | 2e88810 | 2008-09-29 17:12:35 +0000 | [diff] [blame] | 285 | { |
| 286 | int err; |
| 287 | |
| 288 | BUG_ON(in_interrupt()); |
| 289 | |
| 290 | mutex_lock(&bus->mdio_lock); |
| 291 | err = bus->write(bus, addr, regnum, val); |
| 292 | mutex_unlock(&bus->mdio_lock); |
| 293 | |
| 294 | return err; |
| 295 | } |
| 296 | EXPORT_SYMBOL(mdiobus_write); |
| 297 | |
| 298 | /** |
Randy Dunlap | b3df0da | 2007-03-06 02:41:48 -0800 | [diff] [blame] | 299 | * mdio_bus_match - determine if given PHY driver supports the given PHY device |
| 300 | * @dev: target PHY device |
| 301 | * @drv: given PHY driver |
Andy Fleming | 00db818 | 2005-07-30 19:31:23 -0400 | [diff] [blame] | 302 | * |
Randy Dunlap | b3df0da | 2007-03-06 02:41:48 -0800 | [diff] [blame] | 303 | * Description: Given a PHY device, and a PHY driver, return 1 if |
| 304 | * the driver supports the device. Otherwise, return 0. |
Andy Fleming | 00db818 | 2005-07-30 19:31:23 -0400 | [diff] [blame] | 305 | */ |
| 306 | static int mdio_bus_match(struct device *dev, struct device_driver *drv) |
| 307 | { |
| 308 | struct phy_device *phydev = to_phy_device(dev); |
| 309 | struct phy_driver *phydrv = to_phy_driver(drv); |
| 310 | |
Kumar Gala | 5f708dd | 2007-06-28 13:26:06 -0500 | [diff] [blame] | 311 | return ((phydrv->phy_id & phydrv->phy_id_mask) == |
| 312 | (phydev->phy_id & phydrv->phy_id_mask)); |
Andy Fleming | 00db818 | 2005-07-30 19:31:23 -0400 | [diff] [blame] | 313 | } |
| 314 | |
Anton Vorontsov | 2f5cb43 | 2009-12-30 08:23:30 +0000 | [diff] [blame] | 315 | #ifdef CONFIG_PM |
| 316 | |
Anton Vorontsov | 3d1e4db | 2009-02-01 00:53:34 -0800 | [diff] [blame] | 317 | static bool mdio_bus_phy_may_suspend(struct phy_device *phydev) |
| 318 | { |
| 319 | struct device_driver *drv = phydev->dev.driver; |
| 320 | struct phy_driver *phydrv = to_phy_driver(drv); |
| 321 | struct net_device *netdev = phydev->attached_dev; |
| 322 | |
| 323 | if (!drv || !phydrv->suspend) |
| 324 | return false; |
| 325 | |
| 326 | /* PHY not attached? May suspend. */ |
| 327 | if (!netdev) |
| 328 | return true; |
| 329 | |
| 330 | /* |
| 331 | * Don't suspend PHY if the attched netdev parent may wakeup. |
| 332 | * The parent may point to a PCI device, as in tg3 driver. |
| 333 | */ |
| 334 | if (netdev->dev.parent && device_may_wakeup(netdev->dev.parent)) |
| 335 | return false; |
| 336 | |
| 337 | /* |
| 338 | * Also don't suspend PHY if the netdev itself may wakeup. This |
| 339 | * is the case for devices w/o underlaying pwr. mgmt. aware bus, |
| 340 | * e.g. SoC devices. |
| 341 | */ |
| 342 | if (device_may_wakeup(&netdev->dev)) |
| 343 | return false; |
| 344 | |
| 345 | return true; |
| 346 | } |
| 347 | |
Anton Vorontsov | 2f5cb43 | 2009-12-30 08:23:30 +0000 | [diff] [blame] | 348 | static int mdio_bus_suspend(struct device *dev) |
Andy Fleming | 00db818 | 2005-07-30 19:31:23 -0400 | [diff] [blame] | 349 | { |
Anton Vorontsov | 3d1e4db | 2009-02-01 00:53:34 -0800 | [diff] [blame] | 350 | struct phy_driver *phydrv = to_phy_driver(dev->driver); |
Giuseppe Cavallaro | 0f0ca34 | 2008-11-28 16:24:56 -0800 | [diff] [blame] | 351 | struct phy_device *phydev = to_phy_device(dev); |
Andy Fleming | 00db818 | 2005-07-30 19:31:23 -0400 | [diff] [blame] | 352 | |
Anton Vorontsov | 541cd3e | 2009-12-30 08:23:28 +0000 | [diff] [blame] | 353 | /* |
| 354 | * We must stop the state machine manually, otherwise it stops out of |
| 355 | * control, possibly with the phydev->lock held. Upon resume, netdev |
| 356 | * may call phy routines that try to grab the same lock, and that may |
| 357 | * lead to a deadlock. |
| 358 | */ |
Simon Guinot | fddd910 | 2010-09-13 22:12:01 +0000 | [diff] [blame] | 359 | if (phydev->attached_dev && phydev->adjust_link) |
Anton Vorontsov | 541cd3e | 2009-12-30 08:23:28 +0000 | [diff] [blame] | 360 | phy_stop_machine(phydev); |
| 361 | |
Anton Vorontsov | 3d1e4db | 2009-02-01 00:53:34 -0800 | [diff] [blame] | 362 | if (!mdio_bus_phy_may_suspend(phydev)) |
| 363 | return 0; |
Anton Vorontsov | 541cd3e | 2009-12-30 08:23:28 +0000 | [diff] [blame] | 364 | |
Anton Vorontsov | 3d1e4db | 2009-02-01 00:53:34 -0800 | [diff] [blame] | 365 | return phydrv->suspend(phydev); |
Andy Fleming | 00db818 | 2005-07-30 19:31:23 -0400 | [diff] [blame] | 366 | } |
| 367 | |
Anton Vorontsov | 2f5cb43 | 2009-12-30 08:23:30 +0000 | [diff] [blame] | 368 | static int mdio_bus_resume(struct device *dev) |
Andy Fleming | 00db818 | 2005-07-30 19:31:23 -0400 | [diff] [blame] | 369 | { |
Anton Vorontsov | 3d1e4db | 2009-02-01 00:53:34 -0800 | [diff] [blame] | 370 | struct phy_driver *phydrv = to_phy_driver(dev->driver); |
Giuseppe Cavallaro | 0f0ca34 | 2008-11-28 16:24:56 -0800 | [diff] [blame] | 371 | struct phy_device *phydev = to_phy_device(dev); |
Anton Vorontsov | 541cd3e | 2009-12-30 08:23:28 +0000 | [diff] [blame] | 372 | int ret; |
Andy Fleming | 00db818 | 2005-07-30 19:31:23 -0400 | [diff] [blame] | 373 | |
Anton Vorontsov | 3d1e4db | 2009-02-01 00:53:34 -0800 | [diff] [blame] | 374 | if (!mdio_bus_phy_may_suspend(phydev)) |
Anton Vorontsov | 541cd3e | 2009-12-30 08:23:28 +0000 | [diff] [blame] | 375 | goto no_resume; |
| 376 | |
| 377 | ret = phydrv->resume(phydev); |
| 378 | if (ret < 0) |
| 379 | return ret; |
| 380 | |
| 381 | no_resume: |
Simon Guinot | fddd910 | 2010-09-13 22:12:01 +0000 | [diff] [blame] | 382 | if (phydev->attached_dev && phydev->adjust_link) |
Anton Vorontsov | 541cd3e | 2009-12-30 08:23:28 +0000 | [diff] [blame] | 383 | phy_start_machine(phydev, NULL); |
| 384 | |
| 385 | return 0; |
Andy Fleming | 00db818 | 2005-07-30 19:31:23 -0400 | [diff] [blame] | 386 | } |
| 387 | |
Anton Vorontsov | 2f5cb43 | 2009-12-30 08:23:30 +0000 | [diff] [blame] | 388 | static int mdio_bus_restore(struct device *dev) |
| 389 | { |
| 390 | struct phy_device *phydev = to_phy_device(dev); |
| 391 | struct net_device *netdev = phydev->attached_dev; |
| 392 | int ret; |
| 393 | |
| 394 | if (!netdev) |
| 395 | return 0; |
| 396 | |
| 397 | ret = phy_init_hw(phydev); |
| 398 | if (ret < 0) |
| 399 | return ret; |
| 400 | |
| 401 | /* The PHY needs to renegotiate. */ |
| 402 | phydev->link = 0; |
| 403 | phydev->state = PHY_UP; |
| 404 | |
| 405 | phy_start_machine(phydev, NULL); |
| 406 | |
| 407 | return 0; |
| 408 | } |
| 409 | |
| 410 | static struct dev_pm_ops mdio_bus_pm_ops = { |
| 411 | .suspend = mdio_bus_suspend, |
| 412 | .resume = mdio_bus_resume, |
| 413 | .freeze = mdio_bus_suspend, |
| 414 | .thaw = mdio_bus_resume, |
| 415 | .restore = mdio_bus_restore, |
| 416 | }; |
| 417 | |
| 418 | #define MDIO_BUS_PM_OPS (&mdio_bus_pm_ops) |
| 419 | |
| 420 | #else |
| 421 | |
| 422 | #define MDIO_BUS_PM_OPS NULL |
| 423 | |
| 424 | #endif /* CONFIG_PM */ |
| 425 | |
Andy Fleming | 00db818 | 2005-07-30 19:31:23 -0400 | [diff] [blame] | 426 | struct bus_type mdio_bus_type = { |
| 427 | .name = "mdio_bus", |
| 428 | .match = mdio_bus_match, |
Anton Vorontsov | 2f5cb43 | 2009-12-30 08:23:30 +0000 | [diff] [blame] | 429 | .pm = MDIO_BUS_PM_OPS, |
Andy Fleming | 00db818 | 2005-07-30 19:31:23 -0400 | [diff] [blame] | 430 | }; |
Vitaly Bordug | 11b0bac | 2006-08-14 23:00:29 -0700 | [diff] [blame] | 431 | EXPORT_SYMBOL(mdio_bus_type); |
Andy Fleming | 00db818 | 2005-07-30 19:31:23 -0400 | [diff] [blame] | 432 | |
Jeff Garzik | 67c4f3f | 2005-08-11 02:07:25 -0400 | [diff] [blame] | 433 | int __init mdio_bus_init(void) |
Andy Fleming | 00db818 | 2005-07-30 19:31:23 -0400 | [diff] [blame] | 434 | { |
Lennert Buytenhek | 46abc02 | 2008-10-08 16:33:40 -0700 | [diff] [blame] | 435 | int ret; |
| 436 | |
| 437 | ret = class_register(&mdio_bus_class); |
| 438 | if (!ret) { |
| 439 | ret = bus_register(&mdio_bus_type); |
| 440 | if (ret) |
| 441 | class_unregister(&mdio_bus_class); |
| 442 | } |
| 443 | |
| 444 | return ret; |
Andy Fleming | 00db818 | 2005-07-30 19:31:23 -0400 | [diff] [blame] | 445 | } |
| 446 | |
Peter Chubb | dc85dec | 2005-09-03 14:05:06 -0700 | [diff] [blame] | 447 | void mdio_bus_exit(void) |
Andy Fleming | e139345 | 2005-08-24 18:46:21 -0500 | [diff] [blame] | 448 | { |
Lennert Buytenhek | 46abc02 | 2008-10-08 16:33:40 -0700 | [diff] [blame] | 449 | class_unregister(&mdio_bus_class); |
Andy Fleming | e139345 | 2005-08-24 18:46:21 -0500 | [diff] [blame] | 450 | bus_unregister(&mdio_bus_type); |
| 451 | } |