1 // SPDX-License-Identifier: GPL-2.0+ 2 /* MDIO Bus provider interface 3 * 4 * Author: Andy Fleming 5 * 6 * Copyright (c) 2004 Freescale Semiconductor, Inc. 7 */ 8 9 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt 10 11 #include <linux/delay.h> 12 #include <linux/device.h> 13 #include <linux/errno.h> 14 #include <linux/etherdevice.h> 15 #include <linux/ethtool.h> 16 #include <linux/gpio/consumer.h> 17 #include <linux/init.h> 18 #include <linux/io.h> 19 #include <linux/kernel.h> 20 #include <linux/micrel_phy.h> 21 #include <linux/mii.h> 22 #include <linux/mm.h> 23 #include <linux/netdevice.h> 24 #include <linux/of_device.h> 25 #include <linux/of_mdio.h> 26 #include <linux/phy.h> 27 #include <linux/slab.h> 28 #include <linux/string.h> 29 #include <linux/uaccess.h> 30 #include <linux/unistd.h> 31 32 /** 33 * mdiobus_alloc_size - allocate a mii_bus structure 34 * @size: extra amount of memory to allocate for private storage. 35 * If non-zero, then bus->priv is points to that memory. 36 * 37 * Description: called by a bus driver to allocate an mii_bus 38 * structure to fill in. 39 */ 40 struct mii_bus *mdiobus_alloc_size(size_t size) 41 { 42 struct mii_bus *bus; 43 size_t aligned_size = ALIGN(sizeof(*bus), NETDEV_ALIGN); 44 size_t alloc_size; 45 int i; 46 47 /* If we alloc extra space, it should be aligned */ 48 if (size) 49 alloc_size = aligned_size + size; 50 else 51 alloc_size = sizeof(*bus); 52 53 bus = kzalloc(alloc_size, GFP_KERNEL); 54 if (!bus) 55 return NULL; 56 57 bus->state = MDIOBUS_ALLOCATED; 58 if (size) 59 bus->priv = (void *)bus + aligned_size; 60 61 /* Initialise the interrupts to polling and 64-bit seqcounts */ 62 for (i = 0; i < PHY_MAX_ADDR; i++) { 63 bus->irq[i] = PHY_POLL; 64 u64_stats_init(&bus->stats[i].syncp); 65 } 66 67 return bus; 68 } 69 EXPORT_SYMBOL(mdiobus_alloc_size); 70 71 #if IS_ENABLED(CONFIG_OF_MDIO) 72 /* Walk the list of subnodes of a mdio bus and look for a node that 73 * matches the mdio device's address with its 'reg' property. If 74 * found, set the of_node pointer for the mdio device. This allows 75 * auto-probed phy devices to be supplied with information passed in 76 * via DT. 77 * If a PHY package is found, PHY is searched also there. 78 */ 79 static int of_mdiobus_find_phy(struct device *dev, struct mdio_device *mdiodev, 80 struct device_node *np) 81 { 82 struct device_node *child; 83 84 for_each_available_child_of_node(np, child) { 85 int addr; 86 87 if (of_node_name_eq(child, "ethernet-phy-package")) { 88 /* Validate PHY package reg presence */ 89 if (!of_property_present(child, "reg")) { 90 of_node_put(child); 91 return -EINVAL; 92 } 93 94 if (!of_mdiobus_find_phy(dev, mdiodev, child)) { 95 /* The refcount for the PHY package will be 96 * incremented later when PHY join the Package. 97 */ 98 of_node_put(child); 99 return 0; 100 } 101 102 continue; 103 } 104 105 addr = of_mdio_parse_addr(dev, child); 106 if (addr < 0) 107 continue; 108 109 if (addr == mdiodev->addr) { 110 device_set_node(dev, of_fwnode_handle(child)); 111 /* The refcount on "child" is passed to the mdio 112 * device. Do _not_ use of_node_put(child) here. 113 */ 114 return 0; 115 } 116 } 117 118 return -ENODEV; 119 } 120 121 static void of_mdiobus_link_mdiodev(struct mii_bus *bus, 122 struct mdio_device *mdiodev) 123 { 124 struct device *dev = &mdiodev->dev; 125 126 if (dev->of_node || !bus->dev.of_node) 127 return; 128 129 of_mdiobus_find_phy(dev, mdiodev, bus->dev.of_node); 130 } 131 #endif 132 133 static struct phy_device *mdiobus_scan(struct mii_bus *bus, int addr, bool c45) 134 { 135 struct phy_device *phydev = ERR_PTR(-ENODEV); 136 struct fwnode_handle *fwnode; 137 char node_name[16]; 138 int err; 139 140 phydev = get_phy_device(bus, addr, c45); 141 if (IS_ERR(phydev)) 142 return phydev; 143 144 #if IS_ENABLED(CONFIG_OF_MDIO) 145 /* For DT, see if the auto-probed phy has a corresponding child 146 * in the bus node, and set the of_node pointer in this case. 147 */ 148 of_mdiobus_link_mdiodev(bus, &phydev->mdio); 149 #endif 150 151 /* Search for a swnode for the phy in the swnode hierarchy of the bus. 152 * If there is no swnode for the phy provided, just ignore it. 153 */ 154 if (dev_fwnode(&bus->dev) && !dev_fwnode(&phydev->mdio.dev)) { 155 snprintf(node_name, sizeof(node_name), "ethernet-phy@%d", 156 addr); 157 fwnode = fwnode_get_named_child_node(dev_fwnode(&bus->dev), 158 node_name); 159 if (fwnode) 160 device_set_node(&phydev->mdio.dev, fwnode); 161 } 162 163 err = phy_device_register(phydev); 164 if (err) { 165 phy_device_free(phydev); 166 return ERR_PTR(-ENODEV); 167 } 168 169 return phydev; 170 } 171 172 /** 173 * mdiobus_scan_c22 - scan one address on a bus for C22 MDIO devices. 174 * @bus: mii_bus to scan 175 * @addr: address on bus to scan 176 * 177 * This function scans one address on the MDIO bus, looking for 178 * devices which can be identified using a vendor/product ID in 179 * registers 2 and 3. Not all MDIO devices have such registers, but 180 * PHY devices typically do. Hence this function assumes anything 181 * found is a PHY, or can be treated as a PHY. Other MDIO devices, 182 * such as switches, will probably not be found during the scan. 183 */ 184 struct phy_device *mdiobus_scan_c22(struct mii_bus *bus, int addr) 185 { 186 return mdiobus_scan(bus, addr, false); 187 } 188 EXPORT_SYMBOL(mdiobus_scan_c22); 189 190 /** 191 * mdiobus_scan_c45 - scan one address on a bus for C45 MDIO devices. 192 * @bus: mii_bus to scan 193 * @addr: address on bus to scan 194 * 195 * This function scans one address on the MDIO bus, looking for 196 * devices which can be identified using a vendor/product ID in 197 * registers 2 and 3. Not all MDIO devices have such registers, but 198 * PHY devices typically do. Hence this function assumes anything 199 * found is a PHY, or can be treated as a PHY. Other MDIO devices, 200 * such as switches, will probably not be found during the scan. 201 */ 202 static struct phy_device *mdiobus_scan_c45(struct mii_bus *bus, int addr) 203 { 204 return mdiobus_scan(bus, addr, true); 205 } 206 207 static int mdiobus_scan_bus_c22(struct mii_bus *bus) 208 { 209 int i; 210 211 for (i = 0; i < PHY_MAX_ADDR; i++) { 212 if ((bus->phy_mask & BIT(i)) == 0) { 213 struct phy_device *phydev; 214 215 phydev = mdiobus_scan_c22(bus, i); 216 if (IS_ERR(phydev) && (PTR_ERR(phydev) != -ENODEV)) 217 return PTR_ERR(phydev); 218 } 219 } 220 return 0; 221 } 222 223 static int mdiobus_scan_bus_c45(struct mii_bus *bus) 224 { 225 int i; 226 227 for (i = 0; i < PHY_MAX_ADDR; i++) { 228 if ((bus->phy_mask & BIT(i)) == 0) { 229 struct phy_device *phydev; 230 231 /* Don't scan C45 if we already have a C22 device */ 232 if (bus->mdio_map[i]) 233 continue; 234 235 phydev = mdiobus_scan_c45(bus, i); 236 if (IS_ERR(phydev) && (PTR_ERR(phydev) != -ENODEV)) 237 return PTR_ERR(phydev); 238 } 239 } 240 return 0; 241 } 242 243 /* There are some C22 PHYs which do bad things when where is a C45 244 * transaction on the bus, like accepting a read themselves, and 245 * stomping over the true devices reply, to performing a write to 246 * themselves which was intended for another device. Now that C22 247 * devices have been found, see if any of them are bad for C45, and if we 248 * should skip the C45 scan. 249 */ 250 static bool mdiobus_prevent_c45_scan(struct mii_bus *bus) 251 { 252 struct phy_device *phydev; 253 254 mdiobus_for_each_phy(bus, phydev) { 255 u32 oui = phydev->phy_id >> 10; 256 257 if (oui == MICREL_OUI) 258 return true; 259 } 260 261 return false; 262 } 263 264 /** 265 * __mdiobus_register - bring up all the PHYs on a given bus and attach them to bus 266 * @bus: target mii_bus 267 * @owner: module containing bus accessor functions 268 * 269 * Description: Called by a bus driver to bring up all the PHYs 270 * on a given bus, and attach them to the bus. Drivers should use 271 * mdiobus_register() rather than __mdiobus_register() unless they 272 * need to pass a specific owner module. MDIO devices which are not 273 * PHYs will not be brought up by this function. They are expected 274 * to be explicitly listed in DT and instantiated by of_mdiobus_register(). 275 * 276 * Returns 0 on success or < 0 on error. 277 */ 278 int __mdiobus_register(struct mii_bus *bus, struct module *owner) 279 { 280 struct mdio_device *mdiodev; 281 struct gpio_desc *gpiod; 282 bool prevent_c45_scan; 283 int i, err; 284 285 if (!bus || !bus->name) 286 return -EINVAL; 287 288 /* An access method always needs both read and write operations */ 289 if (!!bus->read != !!bus->write || !!bus->read_c45 != !!bus->write_c45) 290 return -EINVAL; 291 292 /* At least one method is mandatory */ 293 if (!bus->read && !bus->read_c45) 294 return -EINVAL; 295 296 if (bus->parent && bus->parent->of_node) 297 bus->parent->of_node->fwnode.flags |= 298 FWNODE_FLAG_NEEDS_CHILD_BOUND_ON_ADD; 299 300 WARN(bus->state != MDIOBUS_ALLOCATED && 301 bus->state != MDIOBUS_UNREGISTERED, 302 "%s: not in ALLOCATED or UNREGISTERED state\n", bus->id); 303 304 bus->owner = owner; 305 bus->dev.parent = bus->parent; 306 bus->dev.class = &mdio_bus_class; 307 bus->dev.groups = NULL; 308 dev_set_name(&bus->dev, "%s", bus->id); 309 310 /* If the bus state is allocated, we're registering a fresh bus 311 * that may have a fwnode associated with it. Grab a reference 312 * to the fwnode. This will be dropped when the bus is released. 313 * If the bus was set to unregistered, it means that the bus was 314 * previously registered, and we've already grabbed a reference. 315 */ 316 if (bus->state == MDIOBUS_ALLOCATED) 317 fwnode_handle_get(dev_fwnode(&bus->dev)); 318 319 /* We need to set state to MDIOBUS_UNREGISTERED to correctly release 320 * the device in mdiobus_free() 321 * 322 * State will be updated later in this function in case of success 323 */ 324 bus->state = MDIOBUS_UNREGISTERED; 325 326 err = device_register(&bus->dev); 327 if (err) { 328 pr_err("mii_bus %s failed to register\n", bus->id); 329 return -EINVAL; 330 } 331 332 mutex_init(&bus->mdio_lock); 333 mutex_init(&bus->shared_lock); 334 335 /* assert bus level PHY GPIO reset */ 336 gpiod = devm_gpiod_get_optional(&bus->dev, "reset", GPIOD_OUT_HIGH); 337 if (IS_ERR(gpiod)) { 338 err = dev_err_probe(&bus->dev, PTR_ERR(gpiod), 339 "mii_bus %s couldn't get reset GPIO\n", 340 bus->id); 341 device_del(&bus->dev); 342 return err; 343 } else if (gpiod) { 344 bus->reset_gpiod = gpiod; 345 fsleep(bus->reset_delay_us); 346 gpiod_set_value_cansleep(gpiod, 0); 347 if (bus->reset_post_delay_us > 0) 348 fsleep(bus->reset_post_delay_us); 349 } 350 351 if (bus->reset) { 352 err = bus->reset(bus); 353 if (err) 354 goto error_reset_gpiod; 355 } 356 357 if (bus->read) { 358 err = mdiobus_scan_bus_c22(bus); 359 if (err) 360 goto error; 361 } 362 363 prevent_c45_scan = mdiobus_prevent_c45_scan(bus); 364 365 if (!prevent_c45_scan && bus->read_c45) { 366 err = mdiobus_scan_bus_c45(bus); 367 if (err) 368 goto error; 369 } 370 371 bus->state = MDIOBUS_REGISTERED; 372 dev_dbg(&bus->dev, "probed\n"); 373 return 0; 374 375 error: 376 for (i = 0; i < PHY_MAX_ADDR; i++) { 377 mdiodev = bus->mdio_map[i]; 378 if (!mdiodev) 379 continue; 380 381 mdiodev->device_remove(mdiodev); 382 mdiodev->device_free(mdiodev); 383 } 384 error_reset_gpiod: 385 /* Put PHYs in RESET to save power */ 386 if (bus->reset_gpiod) 387 gpiod_set_value_cansleep(bus->reset_gpiod, 1); 388 389 device_del(&bus->dev); 390 return err; 391 } 392 EXPORT_SYMBOL(__mdiobus_register); 393 394 void mdiobus_unregister(struct mii_bus *bus) 395 { 396 struct mdio_device *mdiodev; 397 int i; 398 399 if (WARN_ON_ONCE(bus->state != MDIOBUS_REGISTERED)) 400 return; 401 bus->state = MDIOBUS_UNREGISTERED; 402 403 for (i = 0; i < PHY_MAX_ADDR; i++) { 404 mdiodev = bus->mdio_map[i]; 405 if (!mdiodev) 406 continue; 407 408 mdiodev->device_remove(mdiodev); 409 mdiodev->device_free(mdiodev); 410 } 411 412 /* Put PHYs in RESET to save power */ 413 if (bus->reset_gpiod) 414 gpiod_set_value_cansleep(bus->reset_gpiod, 1); 415 416 device_del(&bus->dev); 417 } 418 EXPORT_SYMBOL(mdiobus_unregister); 419 420 /** 421 * mdiobus_free - free a struct mii_bus 422 * @bus: mii_bus to free 423 * 424 * This function releases the reference to the underlying device 425 * object in the mii_bus. If this is the last reference, the mii_bus 426 * will be freed. 427 */ 428 void mdiobus_free(struct mii_bus *bus) 429 { 430 /* For compatibility with error handling in drivers. */ 431 if (bus->state == MDIOBUS_ALLOCATED) { 432 kfree(bus); 433 return; 434 } 435 436 WARN(bus->state != MDIOBUS_UNREGISTERED, 437 "%s: not in UNREGISTERED state\n", bus->id); 438 bus->state = MDIOBUS_RELEASED; 439 440 put_device(&bus->dev); 441 } 442 EXPORT_SYMBOL(mdiobus_free); 443