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 | |
Grygorii Strashko | 6d48f44 | 2014-04-30 15:23:33 +0300 | [diff] [blame] | 72 | static void _devm_mdiobus_free(struct device *dev, void *res) |
| 73 | { |
| 74 | mdiobus_free(*(struct mii_bus **)res); |
| 75 | } |
| 76 | |
| 77 | static int devm_mdiobus_match(struct device *dev, void *res, void *data) |
| 78 | { |
| 79 | struct mii_bus **r = res; |
| 80 | |
| 81 | if (WARN_ON(!r || !*r)) |
| 82 | return 0; |
| 83 | |
| 84 | return *r == data; |
| 85 | } |
| 86 | |
| 87 | /** |
| 88 | * devm_mdiobus_alloc_size - Resource-managed mdiobus_alloc_size() |
| 89 | * @dev: Device to allocate mii_bus for |
| 90 | * @sizeof_priv: Space to allocate for private structure. |
| 91 | * |
| 92 | * Managed mdiobus_alloc_size. mii_bus allocated with this function is |
| 93 | * automatically freed on driver detach. |
| 94 | * |
| 95 | * If an mii_bus allocated with this function needs to be freed separately, |
| 96 | * devm_mdiobus_free() must be used. |
| 97 | * |
| 98 | * RETURNS: |
| 99 | * Pointer to allocated mii_bus on success, NULL on failure. |
| 100 | */ |
| 101 | struct mii_bus *devm_mdiobus_alloc_size(struct device *dev, int sizeof_priv) |
| 102 | { |
| 103 | struct mii_bus **ptr, *bus; |
| 104 | |
| 105 | ptr = devres_alloc(_devm_mdiobus_free, sizeof(*ptr), GFP_KERNEL); |
| 106 | if (!ptr) |
| 107 | return NULL; |
| 108 | |
| 109 | /* use raw alloc_dr for kmalloc caller tracing */ |
| 110 | bus = mdiobus_alloc_size(sizeof_priv); |
| 111 | if (bus) { |
| 112 | *ptr = bus; |
| 113 | devres_add(dev, ptr); |
| 114 | } else { |
| 115 | devres_free(ptr); |
| 116 | } |
| 117 | |
| 118 | return bus; |
| 119 | } |
Arnd Bergmann | 93dccc5 | 2014-05-08 16:46:52 +0200 | [diff] [blame] | 120 | EXPORT_SYMBOL_GPL(devm_mdiobus_alloc_size); |
Grygorii Strashko | 6d48f44 | 2014-04-30 15:23:33 +0300 | [diff] [blame] | 121 | |
| 122 | /** |
| 123 | * devm_mdiobus_free - Resource-managed mdiobus_free() |
| 124 | * @dev: Device this mii_bus belongs to |
| 125 | * @bus: the mii_bus associated with the device |
| 126 | * |
| 127 | * Free mii_bus allocated with devm_mdiobus_alloc_size(). |
| 128 | */ |
| 129 | void devm_mdiobus_free(struct device *dev, struct mii_bus *bus) |
| 130 | { |
| 131 | int rc; |
| 132 | |
| 133 | rc = devres_release(dev, _devm_mdiobus_free, |
| 134 | devm_mdiobus_match, bus); |
| 135 | WARN_ON(rc); |
| 136 | } |
| 137 | EXPORT_SYMBOL_GPL(devm_mdiobus_free); |
| 138 | |
Lennert Buytenhek | 298cf9b | 2008-10-08 16:29:57 -0700 | [diff] [blame] | 139 | /** |
Lennert Buytenhek | 46abc02 | 2008-10-08 16:33:40 -0700 | [diff] [blame] | 140 | * mdiobus_release - mii_bus device release callback |
Randy Dunlap | 78c36b1 | 2008-10-13 18:46:22 -0700 | [diff] [blame] | 141 | * @d: the target struct device that contains the mii_bus |
Lennert Buytenhek | 46abc02 | 2008-10-08 16:33:40 -0700 | [diff] [blame] | 142 | * |
| 143 | * Description: called when the last reference to an mii_bus is |
| 144 | * dropped, to free the underlying memory. |
| 145 | */ |
| 146 | static void mdiobus_release(struct device *d) |
| 147 | { |
| 148 | struct mii_bus *bus = to_mii_bus(d); |
Krzysztof Halasa | 161c8d2 | 2008-12-25 16:50:41 -0800 | [diff] [blame] | 149 | BUG_ON(bus->state != MDIOBUS_RELEASED && |
| 150 | /* for compatibility with error handling in drivers */ |
| 151 | bus->state != MDIOBUS_ALLOCATED); |
Lennert Buytenhek | 46abc02 | 2008-10-08 16:33:40 -0700 | [diff] [blame] | 152 | kfree(bus); |
| 153 | } |
| 154 | |
| 155 | static struct class mdio_bus_class = { |
| 156 | .name = "mdio_bus", |
| 157 | .dev_release = mdiobus_release, |
| 158 | }; |
| 159 | |
Bjørn Mork | b943fbb | 2012-05-11 05:47:01 +0000 | [diff] [blame] | 160 | #if IS_ENABLED(CONFIG_OF_MDIO) |
David Daney | 2510602 | 2012-05-02 15:16:37 +0000 | [diff] [blame] | 161 | /* Helper function for of_mdio_find_bus */ |
Michał Mirosław | 9f3b795 | 2013-02-01 20:40:17 +0100 | [diff] [blame] | 162 | 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] | 163 | { |
| 164 | return dev->of_node == mdio_bus_np; |
| 165 | } |
| 166 | /** |
| 167 | * 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] | 168 | * @mdio_bus_np: Pointer to the mii_bus. |
David Daney | 2510602 | 2012-05-02 15:16:37 +0000 | [diff] [blame] | 169 | * |
Russell King | a136442 | 2015-09-24 20:35:52 +0100 | [diff] [blame] | 170 | * Returns a reference to the mii_bus, or NULL if none found. The |
| 171 | * embedded struct device will have its reference count incremented, |
| 172 | * and this must be put once the bus is finished with. |
David Daney | 2510602 | 2012-05-02 15:16:37 +0000 | [diff] [blame] | 173 | * |
| 174 | * Because the association of a device_node and mii_bus is made via |
| 175 | * of_mdiobus_register(), the mii_bus cannot be found before it is |
| 176 | * registered with of_mdiobus_register(). |
| 177 | * |
| 178 | */ |
| 179 | struct mii_bus *of_mdio_find_bus(struct device_node *mdio_bus_np) |
| 180 | { |
| 181 | struct device *d; |
| 182 | |
| 183 | if (!mdio_bus_np) |
| 184 | return NULL; |
| 185 | |
| 186 | d = class_find_device(&mdio_bus_class, NULL, mdio_bus_np, |
| 187 | of_mdio_bus_match); |
| 188 | |
| 189 | return d ? to_mii_bus(d) : NULL; |
| 190 | } |
| 191 | EXPORT_SYMBOL(of_mdio_find_bus); |
Daniel Mack | d9daa24 | 2014-06-28 01:23:35 +0200 | [diff] [blame] | 192 | |
| 193 | /* Walk the list of subnodes of a mdio bus and look for a node that matches the |
| 194 | * phy's address with its 'reg' property. If found, set the of_node pointer for |
| 195 | * the phy. This allows auto-probed pyh devices to be supplied with information |
| 196 | * passed in via DT. |
| 197 | */ |
| 198 | static void of_mdiobus_link_phydev(struct mii_bus *mdio, |
| 199 | struct phy_device *phydev) |
| 200 | { |
| 201 | struct device *dev = &phydev->dev; |
| 202 | struct device_node *child; |
| 203 | |
| 204 | if (dev->of_node || !mdio->dev.of_node) |
| 205 | return; |
| 206 | |
| 207 | for_each_available_child_of_node(mdio->dev.of_node, child) { |
| 208 | int addr; |
| 209 | int ret; |
| 210 | |
| 211 | ret = of_property_read_u32(child, "reg", &addr); |
| 212 | if (ret < 0) { |
| 213 | dev_err(dev, "%s has invalid PHY address\n", |
| 214 | child->full_name); |
| 215 | continue; |
| 216 | } |
| 217 | |
| 218 | /* A PHY must have a reg property in the range [0-31] */ |
| 219 | if (addr >= PHY_MAX_ADDR) { |
| 220 | dev_err(dev, "%s PHY address %i is too large\n", |
| 221 | child->full_name, addr); |
| 222 | continue; |
| 223 | } |
| 224 | |
| 225 | if (addr == phydev->addr) { |
| 226 | dev->of_node = child; |
| 227 | return; |
| 228 | } |
| 229 | } |
| 230 | } |
| 231 | #else /* !IS_ENABLED(CONFIG_OF_MDIO) */ |
| 232 | static inline void of_mdiobus_link_phydev(struct mii_bus *mdio, |
| 233 | struct phy_device *phydev) |
| 234 | { |
| 235 | } |
David Daney | 2510602 | 2012-05-02 15:16:37 +0000 | [diff] [blame] | 236 | #endif |
| 237 | |
Lennert Buytenhek | 46abc02 | 2008-10-08 16:33:40 -0700 | [diff] [blame] | 238 | /** |
Randy Dunlap | b3df0da | 2007-03-06 02:41:48 -0800 | [diff] [blame] | 239 | * mdiobus_register - bring up all the PHYs on a given bus and attach them to bus |
| 240 | * @bus: target mii_bus |
Andy Fleming | e139345 | 2005-08-24 18:46:21 -0500 | [diff] [blame] | 241 | * |
Randy Dunlap | b3df0da | 2007-03-06 02:41:48 -0800 | [diff] [blame] | 242 | * Description: Called by a bus driver to bring up all the PHYs |
| 243 | * on a given bus, and attach them to the bus. |
| 244 | * |
| 245 | * Returns 0 on success or < 0 on error. |
Andy Fleming | e139345 | 2005-08-24 18:46:21 -0500 | [diff] [blame] | 246 | */ |
Russell King | 3e3aaf6 | 2015-09-24 20:36:02 +0100 | [diff] [blame] | 247 | int __mdiobus_register(struct mii_bus *bus, struct module *owner) |
Andy Fleming | e139345 | 2005-08-24 18:46:21 -0500 | [diff] [blame] | 248 | { |
Krzysztof Halasa | 161c8d2 | 2008-12-25 16:50:41 -0800 | [diff] [blame] | 249 | int i, err; |
Andy Fleming | e139345 | 2005-08-24 18:46:21 -0500 | [diff] [blame] | 250 | |
Andy Fleming | e139345 | 2005-08-24 18:46:21 -0500 | [diff] [blame] | 251 | if (NULL == bus || NULL == bus->name || |
Sergei Shtylyov | 02d320c | 2014-01-05 03:18:27 +0300 | [diff] [blame] | 252 | NULL == bus->read || NULL == bus->write) |
Andy Fleming | e139345 | 2005-08-24 18:46:21 -0500 | [diff] [blame] | 253 | return -EINVAL; |
| 254 | |
Lennert Buytenhek | 46abc02 | 2008-10-08 16:33:40 -0700 | [diff] [blame] | 255 | BUG_ON(bus->state != MDIOBUS_ALLOCATED && |
| 256 | bus->state != MDIOBUS_UNREGISTERED); |
| 257 | |
Russell King | 3e3aaf6 | 2015-09-24 20:36:02 +0100 | [diff] [blame] | 258 | bus->owner = owner; |
Lennert Buytenhek | 46abc02 | 2008-10-08 16:33:40 -0700 | [diff] [blame] | 259 | bus->dev.parent = bus->parent; |
| 260 | bus->dev.class = &mdio_bus_class; |
| 261 | bus->dev.groups = NULL; |
Stephen Hemminger | 036b668 | 2009-02-26 10:19:36 +0000 | [diff] [blame] | 262 | dev_set_name(&bus->dev, "%s", bus->id); |
Lennert Buytenhek | 46abc02 | 2008-10-08 16:33:40 -0700 | [diff] [blame] | 263 | |
| 264 | err = device_register(&bus->dev); |
| 265 | if (err) { |
Joe Perches | 8d24248 | 2012-06-09 07:49:07 +0000 | [diff] [blame] | 266 | pr_err("mii_bus %s failed to register\n", bus->id); |
Levente Kurusa | 0c692d0 | 2014-01-30 15:45:46 -0800 | [diff] [blame] | 267 | put_device(&bus->dev); |
Lennert Buytenhek | 46abc02 | 2008-10-08 16:33:40 -0700 | [diff] [blame] | 268 | return -EINVAL; |
| 269 | } |
| 270 | |
Adrian Bunk | d1e7fe4 | 2008-02-20 02:13:53 +0200 | [diff] [blame] | 271 | mutex_init(&bus->mdio_lock); |
| 272 | |
Andy Fleming | e139345 | 2005-08-24 18:46:21 -0500 | [diff] [blame] | 273 | if (bus->reset) |
| 274 | bus->reset(bus); |
| 275 | |
| 276 | for (i = 0; i < PHY_MAX_ADDR; i++) { |
Lennert Buytenhek | 4fd5f81 | 2008-08-26 13:08:46 +0200 | [diff] [blame] | 277 | if ((bus->phy_mask & (1 << i)) == 0) { |
| 278 | struct phy_device *phydev; |
Andy Fleming | e139345 | 2005-08-24 18:46:21 -0500 | [diff] [blame] | 279 | |
Lennert Buytenhek | 4fd5f81 | 2008-08-26 13:08:46 +0200 | [diff] [blame] | 280 | phydev = mdiobus_scan(bus, i); |
Krzysztof Halasa | 161c8d2 | 2008-12-25 16:50:41 -0800 | [diff] [blame] | 281 | if (IS_ERR(phydev)) { |
Lennert Buytenhek | 4fd5f81 | 2008-08-26 13:08:46 +0200 | [diff] [blame] | 282 | err = PTR_ERR(phydev); |
Krzysztof Halasa | 161c8d2 | 2008-12-25 16:50:41 -0800 | [diff] [blame] | 283 | goto error; |
| 284 | } |
Herbert Valerio Riedel | 64b1c2b | 2006-05-10 12:12:57 -0400 | [diff] [blame] | 285 | } |
Andy Fleming | e139345 | 2005-08-24 18:46:21 -0500 | [diff] [blame] | 286 | } |
| 287 | |
Krzysztof Halasa | 161c8d2 | 2008-12-25 16:50:41 -0800 | [diff] [blame] | 288 | bus->state = MDIOBUS_REGISTERED; |
Andy Fleming | e139345 | 2005-08-24 18:46:21 -0500 | [diff] [blame] | 289 | pr_info("%s: probed\n", bus->name); |
Krzysztof Halasa | 161c8d2 | 2008-12-25 16:50:41 -0800 | [diff] [blame] | 290 | return 0; |
Andy Fleming | e139345 | 2005-08-24 18:46:21 -0500 | [diff] [blame] | 291 | |
Krzysztof Halasa | 161c8d2 | 2008-12-25 16:50:41 -0800 | [diff] [blame] | 292 | error: |
| 293 | while (--i >= 0) { |
Russell King | 38737e4 | 2015-09-24 20:36:28 +0100 | [diff] [blame] | 294 | struct phy_device *phydev = bus->phy_map[i]; |
| 295 | if (phydev) { |
| 296 | phy_device_remove(phydev); |
| 297 | phy_device_free(phydev); |
| 298 | } |
Krzysztof Halasa | 161c8d2 | 2008-12-25 16:50:41 -0800 | [diff] [blame] | 299 | } |
| 300 | device_del(&bus->dev); |
Andy Fleming | e139345 | 2005-08-24 18:46:21 -0500 | [diff] [blame] | 301 | return err; |
| 302 | } |
Russell King | 3e3aaf6 | 2015-09-24 20:36:02 +0100 | [diff] [blame] | 303 | EXPORT_SYMBOL(__mdiobus_register); |
Andy Fleming | e139345 | 2005-08-24 18:46:21 -0500 | [diff] [blame] | 304 | |
| 305 | void mdiobus_unregister(struct mii_bus *bus) |
| 306 | { |
| 307 | int i; |
| 308 | |
Lennert Buytenhek | 46abc02 | 2008-10-08 16:33:40 -0700 | [diff] [blame] | 309 | BUG_ON(bus->state != MDIOBUS_REGISTERED); |
| 310 | bus->state = MDIOBUS_UNREGISTERED; |
| 311 | |
Andy Fleming | e139345 | 2005-08-24 18:46:21 -0500 | [diff] [blame] | 312 | for (i = 0; i < PHY_MAX_ADDR; i++) { |
Russell King | 38737e4 | 2015-09-24 20:36:28 +0100 | [diff] [blame] | 313 | struct phy_device *phydev = bus->phy_map[i]; |
| 314 | if (phydev) { |
| 315 | phy_device_remove(phydev); |
| 316 | phy_device_free(phydev); |
| 317 | } |
Andy Fleming | e139345 | 2005-08-24 18:46:21 -0500 | [diff] [blame] | 318 | } |
Mark Salter | b6c6aed | 2015-09-01 09:36:05 -0400 | [diff] [blame] | 319 | device_del(&bus->dev); |
Andy Fleming | e139345 | 2005-08-24 18:46:21 -0500 | [diff] [blame] | 320 | } |
| 321 | EXPORT_SYMBOL(mdiobus_unregister); |
| 322 | |
Lennert Buytenhek | 298cf9b | 2008-10-08 16:29:57 -0700 | [diff] [blame] | 323 | /** |
| 324 | * mdiobus_free - free a struct mii_bus |
| 325 | * @bus: mii_bus to free |
| 326 | * |
Lennert Buytenhek | 46abc02 | 2008-10-08 16:33:40 -0700 | [diff] [blame] | 327 | * This function releases the reference to the underlying device |
| 328 | * object in the mii_bus. If this is the last reference, the mii_bus |
| 329 | * will be freed. |
Lennert Buytenhek | 298cf9b | 2008-10-08 16:29:57 -0700 | [diff] [blame] | 330 | */ |
| 331 | void mdiobus_free(struct mii_bus *bus) |
| 332 | { |
Sergei Shtylyov | 02d320c | 2014-01-05 03:18:27 +0300 | [diff] [blame] | 333 | /* For compatibility with error handling in drivers. */ |
Lennert Buytenhek | 46abc02 | 2008-10-08 16:33:40 -0700 | [diff] [blame] | 334 | if (bus->state == MDIOBUS_ALLOCATED) { |
| 335 | kfree(bus); |
| 336 | return; |
| 337 | } |
| 338 | |
| 339 | BUG_ON(bus->state != MDIOBUS_UNREGISTERED); |
| 340 | bus->state = MDIOBUS_RELEASED; |
| 341 | |
| 342 | put_device(&bus->dev); |
Lennert Buytenhek | 298cf9b | 2008-10-08 16:29:57 -0700 | [diff] [blame] | 343 | } |
| 344 | EXPORT_SYMBOL(mdiobus_free); |
| 345 | |
Lennert Buytenhek | 4fd5f81 | 2008-08-26 13:08:46 +0200 | [diff] [blame] | 346 | struct phy_device *mdiobus_scan(struct mii_bus *bus, int addr) |
| 347 | { |
| 348 | struct phy_device *phydev; |
| 349 | int err; |
| 350 | |
David Daney | ac28b9f | 2012-06-27 07:33:35 +0000 | [diff] [blame] | 351 | phydev = get_phy_device(bus, addr, false); |
Lennert Buytenhek | 4fd5f81 | 2008-08-26 13:08:46 +0200 | [diff] [blame] | 352 | if (IS_ERR(phydev) || phydev == NULL) |
| 353 | return phydev; |
| 354 | |
Daniel Mack | 86f6cf4 | 2014-05-24 09:34:26 +0200 | [diff] [blame] | 355 | /* |
| 356 | * For DT, see if the auto-probed phy has a correspoding child |
| 357 | * in the bus node, and set the of_node pointer in this case. |
| 358 | */ |
| 359 | of_mdiobus_link_phydev(bus, phydev); |
| 360 | |
Grant Likely | 4dea547 | 2009-04-25 12:52:46 +0000 | [diff] [blame] | 361 | err = phy_device_register(phydev); |
Lennert Buytenhek | 4fd5f81 | 2008-08-26 13:08:46 +0200 | [diff] [blame] | 362 | if (err) { |
Lennert Buytenhek | 4fd5f81 | 2008-08-26 13:08:46 +0200 | [diff] [blame] | 363 | phy_device_free(phydev); |
Grant Likely | 4dea547 | 2009-04-25 12:52:46 +0000 | [diff] [blame] | 364 | return NULL; |
Lennert Buytenhek | 4fd5f81 | 2008-08-26 13:08:46 +0200 | [diff] [blame] | 365 | } |
| 366 | |
Lennert Buytenhek | 4fd5f81 | 2008-08-26 13:08:46 +0200 | [diff] [blame] | 367 | return phydev; |
| 368 | } |
| 369 | EXPORT_SYMBOL(mdiobus_scan); |
| 370 | |
Randy Dunlap | b3df0da | 2007-03-06 02:41:48 -0800 | [diff] [blame] | 371 | /** |
Lennert Buytenhek | 2e88810 | 2008-09-29 17:12:35 +0000 | [diff] [blame] | 372 | * mdiobus_read - Convenience function for reading a given MII mgmt register |
| 373 | * @bus: the mii_bus struct |
| 374 | * @addr: the phy address |
| 375 | * @regnum: register number to read |
| 376 | * |
| 377 | * NOTE: MUST NOT be called from interrupt context, |
| 378 | * because the bus read/write functions may wait for an interrupt |
| 379 | * to conclude the operation. |
| 380 | */ |
Jason Gunthorpe | abf35df | 2010-03-09 09:17:42 +0000 | [diff] [blame] | 381 | int mdiobus_read(struct mii_bus *bus, int addr, u32 regnum) |
Lennert Buytenhek | 2e88810 | 2008-09-29 17:12:35 +0000 | [diff] [blame] | 382 | { |
| 383 | int retval; |
| 384 | |
| 385 | BUG_ON(in_interrupt()); |
| 386 | |
| 387 | mutex_lock(&bus->mdio_lock); |
| 388 | retval = bus->read(bus, addr, regnum); |
| 389 | mutex_unlock(&bus->mdio_lock); |
| 390 | |
| 391 | return retval; |
| 392 | } |
| 393 | EXPORT_SYMBOL(mdiobus_read); |
| 394 | |
| 395 | /** |
| 396 | * mdiobus_write - Convenience function for writing a given MII mgmt register |
| 397 | * @bus: the mii_bus struct |
| 398 | * @addr: the phy address |
| 399 | * @regnum: register number to write |
| 400 | * @val: value to write to @regnum |
| 401 | * |
| 402 | * NOTE: MUST NOT be called from interrupt context, |
| 403 | * because the bus read/write functions may wait for an interrupt |
| 404 | * to conclude the operation. |
| 405 | */ |
Jason Gunthorpe | abf35df | 2010-03-09 09:17:42 +0000 | [diff] [blame] | 406 | 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] | 407 | { |
| 408 | int err; |
| 409 | |
| 410 | BUG_ON(in_interrupt()); |
| 411 | |
| 412 | mutex_lock(&bus->mdio_lock); |
| 413 | err = bus->write(bus, addr, regnum, val); |
| 414 | mutex_unlock(&bus->mdio_lock); |
| 415 | |
| 416 | return err; |
| 417 | } |
| 418 | EXPORT_SYMBOL(mdiobus_write); |
| 419 | |
| 420 | /** |
Randy Dunlap | b3df0da | 2007-03-06 02:41:48 -0800 | [diff] [blame] | 421 | * mdio_bus_match - determine if given PHY driver supports the given PHY device |
| 422 | * @dev: target PHY device |
| 423 | * @drv: given PHY driver |
Andy Fleming | 00db818 | 2005-07-30 19:31:23 -0400 | [diff] [blame] | 424 | * |
Randy Dunlap | b3df0da | 2007-03-06 02:41:48 -0800 | [diff] [blame] | 425 | * Description: Given a PHY device, and a PHY driver, return 1 if |
| 426 | * the driver supports the device. Otherwise, return 0. |
Andy Fleming | 00db818 | 2005-07-30 19:31:23 -0400 | [diff] [blame] | 427 | */ |
| 428 | static int mdio_bus_match(struct device *dev, struct device_driver *drv) |
| 429 | { |
| 430 | struct phy_device *phydev = to_phy_device(dev); |
| 431 | struct phy_driver *phydrv = to_phy_driver(drv); |
Shaohui Xie | e0536cd | 2015-07-17 18:07:19 +0800 | [diff] [blame] | 432 | const int num_ids = ARRAY_SIZE(phydev->c45_ids.device_ids); |
| 433 | int i; |
Andy Fleming | 00db818 | 2005-07-30 19:31:23 -0400 | [diff] [blame] | 434 | |
David Daney | a30e2c1 | 2012-06-27 07:33:37 +0000 | [diff] [blame] | 435 | if (of_driver_match_device(dev, drv)) |
| 436 | return 1; |
| 437 | |
| 438 | if (phydrv->match_phy_device) |
| 439 | return phydrv->match_phy_device(phydev); |
| 440 | |
Shaohui Xie | e0536cd | 2015-07-17 18:07:19 +0800 | [diff] [blame] | 441 | if (phydev->is_c45) { |
| 442 | for (i = 1; i < num_ids; i++) { |
| 443 | if (!(phydev->c45_ids.devices_in_package & (1 << i))) |
| 444 | continue; |
| 445 | |
| 446 | if ((phydrv->phy_id & phydrv->phy_id_mask) == |
| 447 | (phydev->c45_ids.device_ids[i] & |
| 448 | phydrv->phy_id_mask)) |
| 449 | return 1; |
| 450 | } |
| 451 | return 0; |
| 452 | } else { |
| 453 | return (phydrv->phy_id & phydrv->phy_id_mask) == |
| 454 | (phydev->phy_id & phydrv->phy_id_mask); |
| 455 | } |
Andy Fleming | 00db818 | 2005-07-30 19:31:23 -0400 | [diff] [blame] | 456 | } |
| 457 | |
Anton Vorontsov | 2f5cb43 | 2009-12-30 08:23:30 +0000 | [diff] [blame] | 458 | #ifdef CONFIG_PM |
| 459 | |
Anton Vorontsov | 3d1e4db | 2009-02-01 00:53:34 -0800 | [diff] [blame] | 460 | static bool mdio_bus_phy_may_suspend(struct phy_device *phydev) |
| 461 | { |
| 462 | struct device_driver *drv = phydev->dev.driver; |
| 463 | struct phy_driver *phydrv = to_phy_driver(drv); |
| 464 | struct net_device *netdev = phydev->attached_dev; |
| 465 | |
| 466 | if (!drv || !phydrv->suspend) |
| 467 | return false; |
| 468 | |
Florian Fainelli | 803dd9c | 2015-01-26 22:05:40 -0800 | [diff] [blame] | 469 | /* PHY not attached? May suspend if the PHY has not already been |
| 470 | * suspended as part of a prior call to phy_disconnect() -> |
| 471 | * phy_detach() -> phy_suspend() because the parent netdev might be the |
| 472 | * MDIO bus driver and clock gated at this point. |
| 473 | */ |
Anton Vorontsov | 3d1e4db | 2009-02-01 00:53:34 -0800 | [diff] [blame] | 474 | if (!netdev) |
Florian Fainelli | 803dd9c | 2015-01-26 22:05:40 -0800 | [diff] [blame] | 475 | return !phydev->suspended; |
Anton Vorontsov | 3d1e4db | 2009-02-01 00:53:34 -0800 | [diff] [blame] | 476 | |
Sergei Shtylyov | 02d320c | 2014-01-05 03:18:27 +0300 | [diff] [blame] | 477 | /* Don't suspend PHY if the attched netdev parent may wakeup. |
Anton Vorontsov | 3d1e4db | 2009-02-01 00:53:34 -0800 | [diff] [blame] | 478 | * The parent may point to a PCI device, as in tg3 driver. |
| 479 | */ |
| 480 | if (netdev->dev.parent && device_may_wakeup(netdev->dev.parent)) |
| 481 | return false; |
| 482 | |
Sergei Shtylyov | 02d320c | 2014-01-05 03:18:27 +0300 | [diff] [blame] | 483 | /* Also don't suspend PHY if the netdev itself may wakeup. This |
Anton Vorontsov | 3d1e4db | 2009-02-01 00:53:34 -0800 | [diff] [blame] | 484 | * is the case for devices w/o underlaying pwr. mgmt. aware bus, |
| 485 | * e.g. SoC devices. |
| 486 | */ |
| 487 | if (device_may_wakeup(&netdev->dev)) |
| 488 | return false; |
| 489 | |
| 490 | return true; |
| 491 | } |
| 492 | |
Anton Vorontsov | 2f5cb43 | 2009-12-30 08:23:30 +0000 | [diff] [blame] | 493 | static int mdio_bus_suspend(struct device *dev) |
Andy Fleming | 00db818 | 2005-07-30 19:31:23 -0400 | [diff] [blame] | 494 | { |
Giuseppe Cavallaro | 0f0ca34 | 2008-11-28 16:24:56 -0800 | [diff] [blame] | 495 | struct phy_device *phydev = to_phy_device(dev); |
Andy Fleming | 00db818 | 2005-07-30 19:31:23 -0400 | [diff] [blame] | 496 | |
Sergei Shtylyov | 02d320c | 2014-01-05 03:18:27 +0300 | [diff] [blame] | 497 | /* We must stop the state machine manually, otherwise it stops out of |
Anton Vorontsov | 541cd3e | 2009-12-30 08:23:28 +0000 | [diff] [blame] | 498 | * control, possibly with the phydev->lock held. Upon resume, netdev |
| 499 | * may call phy routines that try to grab the same lock, and that may |
| 500 | * lead to a deadlock. |
| 501 | */ |
Simon Guinot | fddd910 | 2010-09-13 22:12:01 +0000 | [diff] [blame] | 502 | if (phydev->attached_dev && phydev->adjust_link) |
Anton Vorontsov | 541cd3e | 2009-12-30 08:23:28 +0000 | [diff] [blame] | 503 | phy_stop_machine(phydev); |
| 504 | |
Anton Vorontsov | 3d1e4db | 2009-02-01 00:53:34 -0800 | [diff] [blame] | 505 | if (!mdio_bus_phy_may_suspend(phydev)) |
| 506 | return 0; |
Anton Vorontsov | 541cd3e | 2009-12-30 08:23:28 +0000 | [diff] [blame] | 507 | |
Florian Fainelli | 9272efa | 2015-01-26 22:05:37 -0800 | [diff] [blame] | 508 | return phy_suspend(phydev); |
Andy Fleming | 00db818 | 2005-07-30 19:31:23 -0400 | [diff] [blame] | 509 | } |
| 510 | |
Anton Vorontsov | 2f5cb43 | 2009-12-30 08:23:30 +0000 | [diff] [blame] | 511 | static int mdio_bus_resume(struct device *dev) |
Andy Fleming | 00db818 | 2005-07-30 19:31:23 -0400 | [diff] [blame] | 512 | { |
Giuseppe Cavallaro | 0f0ca34 | 2008-11-28 16:24:56 -0800 | [diff] [blame] | 513 | struct phy_device *phydev = to_phy_device(dev); |
Anton Vorontsov | 541cd3e | 2009-12-30 08:23:28 +0000 | [diff] [blame] | 514 | int ret; |
Andy Fleming | 00db818 | 2005-07-30 19:31:23 -0400 | [diff] [blame] | 515 | |
Anton Vorontsov | 3d1e4db | 2009-02-01 00:53:34 -0800 | [diff] [blame] | 516 | if (!mdio_bus_phy_may_suspend(phydev)) |
Anton Vorontsov | 541cd3e | 2009-12-30 08:23:28 +0000 | [diff] [blame] | 517 | goto no_resume; |
| 518 | |
Florian Fainelli | 9272efa | 2015-01-26 22:05:37 -0800 | [diff] [blame] | 519 | ret = phy_resume(phydev); |
Anton Vorontsov | 541cd3e | 2009-12-30 08:23:28 +0000 | [diff] [blame] | 520 | if (ret < 0) |
| 521 | return ret; |
| 522 | |
| 523 | no_resume: |
Simon Guinot | fddd910 | 2010-09-13 22:12:01 +0000 | [diff] [blame] | 524 | if (phydev->attached_dev && phydev->adjust_link) |
Sergei Shtylyov | 29935ae | 2014-01-05 03:27:17 +0300 | [diff] [blame] | 525 | phy_start_machine(phydev); |
Anton Vorontsov | 541cd3e | 2009-12-30 08:23:28 +0000 | [diff] [blame] | 526 | |
| 527 | return 0; |
Andy Fleming | 00db818 | 2005-07-30 19:31:23 -0400 | [diff] [blame] | 528 | } |
| 529 | |
Anton Vorontsov | 2f5cb43 | 2009-12-30 08:23:30 +0000 | [diff] [blame] | 530 | static int mdio_bus_restore(struct device *dev) |
| 531 | { |
| 532 | struct phy_device *phydev = to_phy_device(dev); |
| 533 | struct net_device *netdev = phydev->attached_dev; |
| 534 | int ret; |
| 535 | |
| 536 | if (!netdev) |
| 537 | return 0; |
| 538 | |
| 539 | ret = phy_init_hw(phydev); |
| 540 | if (ret < 0) |
| 541 | return ret; |
| 542 | |
| 543 | /* The PHY needs to renegotiate. */ |
| 544 | phydev->link = 0; |
| 545 | phydev->state = PHY_UP; |
| 546 | |
Sergei Shtylyov | 29935ae | 2014-01-05 03:27:17 +0300 | [diff] [blame] | 547 | phy_start_machine(phydev); |
Anton Vorontsov | 2f5cb43 | 2009-12-30 08:23:30 +0000 | [diff] [blame] | 548 | |
| 549 | return 0; |
| 550 | } |
| 551 | |
Sergei Shtylyov | 02d320c | 2014-01-05 03:18:27 +0300 | [diff] [blame] | 552 | static const struct dev_pm_ops mdio_bus_pm_ops = { |
Anton Vorontsov | 2f5cb43 | 2009-12-30 08:23:30 +0000 | [diff] [blame] | 553 | .suspend = mdio_bus_suspend, |
| 554 | .resume = mdio_bus_resume, |
| 555 | .freeze = mdio_bus_suspend, |
| 556 | .thaw = mdio_bus_resume, |
| 557 | .restore = mdio_bus_restore, |
| 558 | }; |
| 559 | |
| 560 | #define MDIO_BUS_PM_OPS (&mdio_bus_pm_ops) |
| 561 | |
| 562 | #else |
| 563 | |
| 564 | #define MDIO_BUS_PM_OPS NULL |
| 565 | |
| 566 | #endif /* CONFIG_PM */ |
| 567 | |
Nick Bowler | 56277f4 | 2012-11-07 06:20:34 +0000 | [diff] [blame] | 568 | static ssize_t |
| 569 | phy_id_show(struct device *dev, struct device_attribute *attr, char *buf) |
| 570 | { |
| 571 | struct phy_device *phydev = to_phy_device(dev); |
| 572 | |
| 573 | return sprintf(buf, "0x%.8lx\n", (unsigned long)phydev->phy_id); |
| 574 | } |
Greg Kroah-Hartman | 4192c74 | 2013-10-06 23:55:41 -0700 | [diff] [blame] | 575 | static DEVICE_ATTR_RO(phy_id); |
Nick Bowler | 56277f4 | 2012-11-07 06:20:34 +0000 | [diff] [blame] | 576 | |
Florian Fainelli | 3d055d8 | 2014-02-11 17:27:40 -0800 | [diff] [blame] | 577 | static ssize_t |
| 578 | phy_interface_show(struct device *dev, struct device_attribute *attr, char *buf) |
| 579 | { |
| 580 | struct phy_device *phydev = to_phy_device(dev); |
Florian Fainelli | 574746d | 2014-08-27 11:44:33 -0700 | [diff] [blame] | 581 | const char *mode = NULL; |
Florian Fainelli | 3d055d8 | 2014-02-11 17:27:40 -0800 | [diff] [blame] | 582 | |
Florian Fainelli | 574746d | 2014-08-27 11:44:33 -0700 | [diff] [blame] | 583 | if (phy_is_internal(phydev)) |
| 584 | mode = "internal"; |
| 585 | else |
| 586 | mode = phy_modes(phydev->interface); |
| 587 | |
| 588 | return sprintf(buf, "%s\n", mode); |
Florian Fainelli | 3d055d8 | 2014-02-11 17:27:40 -0800 | [diff] [blame] | 589 | } |
| 590 | static DEVICE_ATTR_RO(phy_interface); |
| 591 | |
Florian Fainelli | 8bed1285 | 2014-02-11 17:27:42 -0800 | [diff] [blame] | 592 | static ssize_t |
| 593 | phy_has_fixups_show(struct device *dev, struct device_attribute *attr, char *buf) |
| 594 | { |
| 595 | struct phy_device *phydev = to_phy_device(dev); |
| 596 | |
| 597 | return sprintf(buf, "%d\n", phydev->has_fixups); |
| 598 | } |
| 599 | static DEVICE_ATTR_RO(phy_has_fixups); |
| 600 | |
Greg Kroah-Hartman | 4192c74 | 2013-10-06 23:55:41 -0700 | [diff] [blame] | 601 | static struct attribute *mdio_dev_attrs[] = { |
| 602 | &dev_attr_phy_id.attr, |
Florian Fainelli | 3d055d8 | 2014-02-11 17:27:40 -0800 | [diff] [blame] | 603 | &dev_attr_phy_interface.attr, |
Florian Fainelli | 8bed1285 | 2014-02-11 17:27:42 -0800 | [diff] [blame] | 604 | &dev_attr_phy_has_fixups.attr, |
Greg Kroah-Hartman | 4192c74 | 2013-10-06 23:55:41 -0700 | [diff] [blame] | 605 | NULL, |
Nick Bowler | 56277f4 | 2012-11-07 06:20:34 +0000 | [diff] [blame] | 606 | }; |
Greg Kroah-Hartman | 4192c74 | 2013-10-06 23:55:41 -0700 | [diff] [blame] | 607 | ATTRIBUTE_GROUPS(mdio_dev); |
Nick Bowler | 56277f4 | 2012-11-07 06:20:34 +0000 | [diff] [blame] | 608 | |
Andy Fleming | 00db818 | 2005-07-30 19:31:23 -0400 | [diff] [blame] | 609 | struct bus_type mdio_bus_type = { |
| 610 | .name = "mdio_bus", |
| 611 | .match = mdio_bus_match, |
Anton Vorontsov | 2f5cb43 | 2009-12-30 08:23:30 +0000 | [diff] [blame] | 612 | .pm = MDIO_BUS_PM_OPS, |
Greg Kroah-Hartman | 4192c74 | 2013-10-06 23:55:41 -0700 | [diff] [blame] | 613 | .dev_groups = mdio_dev_groups, |
Andy Fleming | 00db818 | 2005-07-30 19:31:23 -0400 | [diff] [blame] | 614 | }; |
Vitaly Bordug | 11b0bac | 2006-08-14 23:00:29 -0700 | [diff] [blame] | 615 | EXPORT_SYMBOL(mdio_bus_type); |
Andy Fleming | 00db818 | 2005-07-30 19:31:23 -0400 | [diff] [blame] | 616 | |
Jeff Garzik | 67c4f3f | 2005-08-11 02:07:25 -0400 | [diff] [blame] | 617 | int __init mdio_bus_init(void) |
Andy Fleming | 00db818 | 2005-07-30 19:31:23 -0400 | [diff] [blame] | 618 | { |
Lennert Buytenhek | 46abc02 | 2008-10-08 16:33:40 -0700 | [diff] [blame] | 619 | int ret; |
| 620 | |
| 621 | ret = class_register(&mdio_bus_class); |
| 622 | if (!ret) { |
| 623 | ret = bus_register(&mdio_bus_type); |
| 624 | if (ret) |
| 625 | class_unregister(&mdio_bus_class); |
| 626 | } |
| 627 | |
| 628 | return ret; |
Andy Fleming | 00db818 | 2005-07-30 19:31:23 -0400 | [diff] [blame] | 629 | } |
| 630 | |
Peter Chubb | dc85dec | 2005-09-03 14:05:06 -0700 | [diff] [blame] | 631 | void mdio_bus_exit(void) |
Andy Fleming | e139345 | 2005-08-24 18:46:21 -0500 | [diff] [blame] | 632 | { |
Lennert Buytenhek | 46abc02 | 2008-10-08 16:33:40 -0700 | [diff] [blame] | 633 | class_unregister(&mdio_bus_class); |
Andy Fleming | e139345 | 2005-08-24 18:46:21 -0500 | [diff] [blame] | 634 | bus_unregister(&mdio_bus_type); |
| 635 | } |