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