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