1 // SPDX-License-Identifier: GPL-2.0+ 2 /* MDIO Bus 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/kernel.h> 12 #include <linux/string.h> 13 #include <linux/errno.h> 14 #include <linux/unistd.h> 15 #include <linux/slab.h> 16 #include <linux/interrupt.h> 17 #include <linux/init.h> 18 #include <linux/delay.h> 19 #include <linux/device.h> 20 #include <linux/gpio.h> 21 #include <linux/gpio/consumer.h> 22 #include <linux/of_device.h> 23 #include <linux/of_mdio.h> 24 #include <linux/of_gpio.h> 25 #include <linux/netdevice.h> 26 #include <linux/etherdevice.h> 27 #include <linux/reset.h> 28 #include <linux/skbuff.h> 29 #include <linux/spinlock.h> 30 #include <linux/mm.h> 31 #include <linux/module.h> 32 #include <linux/mii.h> 33 #include <linux/ethtool.h> 34 #include <linux/phy.h> 35 #include <linux/io.h> 36 #include <linux/uaccess.h> 37 38 #define CREATE_TRACE_POINTS 39 #include <trace/events/mdio.h> 40 41 #include "mdio-boardinfo.h" 42 43 static int mdiobus_register_gpiod(struct mdio_device *mdiodev) 44 { 45 int error; 46 47 /* Deassert the optional reset signal */ 48 mdiodev->reset_gpio = gpiod_get_optional(&mdiodev->dev, 49 "reset", GPIOD_OUT_LOW); 50 error = PTR_ERR_OR_ZERO(mdiodev->reset_gpio); 51 if (error) 52 return error; 53 54 if (mdiodev->reset_gpio) 55 gpiod_set_consumer_name(mdiodev->reset_gpio, "PHY reset"); 56 57 return 0; 58 } 59 60 static int mdiobus_register_reset(struct mdio_device *mdiodev) 61 { 62 struct reset_control *reset; 63 64 reset = reset_control_get_optional_exclusive(&mdiodev->dev, "phy"); 65 if (IS_ERR(reset)) 66 return PTR_ERR(reset); 67 68 mdiodev->reset_ctrl = reset; 69 70 return 0; 71 } 72 73 int mdiobus_register_device(struct mdio_device *mdiodev) 74 { 75 int err; 76 77 if (mdiodev->bus->mdio_map[mdiodev->addr]) 78 return -EBUSY; 79 80 if (mdiodev->flags & MDIO_DEVICE_FLAG_PHY) { 81 err = mdiobus_register_gpiod(mdiodev); 82 if (err) 83 return err; 84 85 err = mdiobus_register_reset(mdiodev); 86 if (err) 87 return err; 88 89 /* Assert the reset signal */ 90 mdio_device_reset(mdiodev, 1); 91 } 92 93 mdiodev->bus->mdio_map[mdiodev->addr] = mdiodev; 94 95 return 0; 96 } 97 EXPORT_SYMBOL(mdiobus_register_device); 98 99 int mdiobus_unregister_device(struct mdio_device *mdiodev) 100 { 101 if (mdiodev->bus->mdio_map[mdiodev->addr] != mdiodev) 102 return -EINVAL; 103 104 reset_control_put(mdiodev->reset_ctrl); 105 106 mdiodev->bus->mdio_map[mdiodev->addr] = NULL; 107 108 return 0; 109 } 110 EXPORT_SYMBOL(mdiobus_unregister_device); 111 112 struct phy_device *mdiobus_get_phy(struct mii_bus *bus, int addr) 113 { 114 struct mdio_device *mdiodev = bus->mdio_map[addr]; 115 116 if (!mdiodev) 117 return NULL; 118 119 if (!(mdiodev->flags & MDIO_DEVICE_FLAG_PHY)) 120 return NULL; 121 122 return container_of(mdiodev, struct phy_device, mdio); 123 } 124 EXPORT_SYMBOL(mdiobus_get_phy); 125 126 bool mdiobus_is_registered_device(struct mii_bus *bus, int addr) 127 { 128 return bus->mdio_map[addr]; 129 } 130 EXPORT_SYMBOL(mdiobus_is_registered_device); 131 132 /** 133 * mdiobus_alloc_size - allocate a mii_bus structure 134 * @size: extra amount of memory to allocate for private storage. 135 * If non-zero, then bus->priv is points to that memory. 136 * 137 * Description: called by a bus driver to allocate an mii_bus 138 * structure to fill in. 139 */ 140 struct mii_bus *mdiobus_alloc_size(size_t size) 141 { 142 struct mii_bus *bus; 143 size_t aligned_size = ALIGN(sizeof(*bus), NETDEV_ALIGN); 144 size_t alloc_size; 145 int i; 146 147 /* If we alloc extra space, it should be aligned */ 148 if (size) 149 alloc_size = aligned_size + size; 150 else 151 alloc_size = sizeof(*bus); 152 153 bus = kzalloc(alloc_size, GFP_KERNEL); 154 if (!bus) 155 return NULL; 156 157 bus->state = MDIOBUS_ALLOCATED; 158 if (size) 159 bus->priv = (void *)bus + aligned_size; 160 161 /* Initialise the interrupts to polling and 64-bit seqcounts */ 162 for (i = 0; i < PHY_MAX_ADDR; i++) { 163 bus->irq[i] = PHY_POLL; 164 u64_stats_init(&bus->stats[i].syncp); 165 } 166 167 return bus; 168 } 169 EXPORT_SYMBOL(mdiobus_alloc_size); 170 171 static void _devm_mdiobus_free(struct device *dev, void *res) 172 { 173 mdiobus_free(*(struct mii_bus **)res); 174 } 175 176 static int devm_mdiobus_match(struct device *dev, void *res, void *data) 177 { 178 struct mii_bus **r = res; 179 180 if (WARN_ON(!r || !*r)) 181 return 0; 182 183 return *r == data; 184 } 185 186 /** 187 * devm_mdiobus_alloc_size - Resource-managed mdiobus_alloc_size() 188 * @dev: Device to allocate mii_bus for 189 * @sizeof_priv: Space to allocate for private structure. 190 * 191 * Managed mdiobus_alloc_size. mii_bus allocated with this function is 192 * automatically freed on driver detach. 193 * 194 * If an mii_bus allocated with this function needs to be freed separately, 195 * devm_mdiobus_free() must be used. 196 * 197 * RETURNS: 198 * Pointer to allocated mii_bus on success, NULL on failure. 199 */ 200 struct mii_bus *devm_mdiobus_alloc_size(struct device *dev, int sizeof_priv) 201 { 202 struct mii_bus **ptr, *bus; 203 204 ptr = devres_alloc(_devm_mdiobus_free, sizeof(*ptr), GFP_KERNEL); 205 if (!ptr) 206 return NULL; 207 208 /* use raw alloc_dr for kmalloc caller tracing */ 209 bus = mdiobus_alloc_size(sizeof_priv); 210 if (bus) { 211 *ptr = bus; 212 devres_add(dev, ptr); 213 } else { 214 devres_free(ptr); 215 } 216 217 return bus; 218 } 219 EXPORT_SYMBOL_GPL(devm_mdiobus_alloc_size); 220 221 /** 222 * devm_mdiobus_free - Resource-managed mdiobus_free() 223 * @dev: Device this mii_bus belongs to 224 * @bus: the mii_bus associated with the device 225 * 226 * Free mii_bus allocated with devm_mdiobus_alloc_size(). 227 */ 228 void devm_mdiobus_free(struct device *dev, struct mii_bus *bus) 229 { 230 int rc; 231 232 rc = devres_release(dev, _devm_mdiobus_free, 233 devm_mdiobus_match, bus); 234 WARN_ON(rc); 235 } 236 EXPORT_SYMBOL_GPL(devm_mdiobus_free); 237 238 /** 239 * mdiobus_release - mii_bus device release callback 240 * @d: the target struct device that contains the mii_bus 241 * 242 * Description: called when the last reference to an mii_bus is 243 * dropped, to free the underlying memory. 244 */ 245 static void mdiobus_release(struct device *d) 246 { 247 struct mii_bus *bus = to_mii_bus(d); 248 BUG_ON(bus->state != MDIOBUS_RELEASED && 249 /* for compatibility with error handling in drivers */ 250 bus->state != MDIOBUS_ALLOCATED); 251 kfree(bus); 252 } 253 254 struct mdio_bus_stat_attr { 255 int addr; 256 unsigned int field_offset; 257 }; 258 259 static u64 mdio_bus_get_stat(struct mdio_bus_stats *s, unsigned int offset) 260 { 261 const char *p = (const char *)s + offset; 262 unsigned int start; 263 u64 val = 0; 264 265 do { 266 start = u64_stats_fetch_begin(&s->syncp); 267 val = u64_stats_read((const u64_stats_t *)p); 268 } while (u64_stats_fetch_retry(&s->syncp, start)); 269 270 return val; 271 } 272 273 static u64 mdio_bus_get_global_stat(struct mii_bus *bus, unsigned int offset) 274 { 275 unsigned int i; 276 u64 val = 0; 277 278 for (i = 0; i < PHY_MAX_ADDR; i++) 279 val += mdio_bus_get_stat(&bus->stats[i], offset); 280 281 return val; 282 } 283 284 static ssize_t mdio_bus_stat_field_show(struct device *dev, 285 struct device_attribute *attr, 286 char *buf) 287 { 288 struct mii_bus *bus = to_mii_bus(dev); 289 struct mdio_bus_stat_attr *sattr; 290 struct dev_ext_attribute *eattr; 291 u64 val; 292 293 eattr = container_of(attr, struct dev_ext_attribute, attr); 294 sattr = eattr->var; 295 296 if (sattr->addr < 0) 297 val = mdio_bus_get_global_stat(bus, sattr->field_offset); 298 else 299 val = mdio_bus_get_stat(&bus->stats[sattr->addr], 300 sattr->field_offset); 301 302 return sprintf(buf, "%llu\n", val); 303 } 304 305 static ssize_t mdio_bus_device_stat_field_show(struct device *dev, 306 struct device_attribute *attr, 307 char *buf) 308 { 309 struct mdio_device *mdiodev = to_mdio_device(dev); 310 struct mii_bus *bus = mdiodev->bus; 311 struct mdio_bus_stat_attr *sattr; 312 struct dev_ext_attribute *eattr; 313 int addr = mdiodev->addr; 314 u64 val; 315 316 eattr = container_of(attr, struct dev_ext_attribute, attr); 317 sattr = eattr->var; 318 319 val = mdio_bus_get_stat(&bus->stats[addr], sattr->field_offset); 320 321 return sprintf(buf, "%llu\n", val); 322 } 323 324 #define MDIO_BUS_STATS_ATTR_DECL(field, file) \ 325 static struct dev_ext_attribute dev_attr_mdio_bus_##field = { \ 326 .attr = { .attr = { .name = file, .mode = 0444 }, \ 327 .show = mdio_bus_stat_field_show, \ 328 }, \ 329 .var = &((struct mdio_bus_stat_attr) { \ 330 -1, offsetof(struct mdio_bus_stats, field) \ 331 }), \ 332 }; \ 333 static struct dev_ext_attribute dev_attr_mdio_bus_device_##field = { \ 334 .attr = { .attr = { .name = file, .mode = 0444 }, \ 335 .show = mdio_bus_device_stat_field_show, \ 336 }, \ 337 .var = &((struct mdio_bus_stat_attr) { \ 338 -1, offsetof(struct mdio_bus_stats, field) \ 339 }), \ 340 }; 341 342 #define MDIO_BUS_STATS_ATTR(field) \ 343 MDIO_BUS_STATS_ATTR_DECL(field, __stringify(field)) 344 345 MDIO_BUS_STATS_ATTR(transfers); 346 MDIO_BUS_STATS_ATTR(errors); 347 MDIO_BUS_STATS_ATTR(writes); 348 MDIO_BUS_STATS_ATTR(reads); 349 350 #define MDIO_BUS_STATS_ADDR_ATTR_DECL(field, addr, file) \ 351 static struct dev_ext_attribute dev_attr_mdio_bus_addr_##field##_##addr = { \ 352 .attr = { .attr = { .name = file, .mode = 0444 }, \ 353 .show = mdio_bus_stat_field_show, \ 354 }, \ 355 .var = &((struct mdio_bus_stat_attr) { \ 356 addr, offsetof(struct mdio_bus_stats, field) \ 357 }), \ 358 } 359 360 #define MDIO_BUS_STATS_ADDR_ATTR(field, addr) \ 361 MDIO_BUS_STATS_ADDR_ATTR_DECL(field, addr, \ 362 __stringify(field) "_" __stringify(addr)) 363 364 #define MDIO_BUS_STATS_ADDR_ATTR_GROUP_DECL(addr) \ 365 MDIO_BUS_STATS_ADDR_ATTR(transfers, addr); \ 366 MDIO_BUS_STATS_ADDR_ATTR(errors, addr); \ 367 MDIO_BUS_STATS_ADDR_ATTR(writes, addr); \ 368 MDIO_BUS_STATS_ADDR_ATTR(reads, addr) \ 369 370 MDIO_BUS_STATS_ADDR_ATTR_GROUP_DECL(0); 371 MDIO_BUS_STATS_ADDR_ATTR_GROUP_DECL(1); 372 MDIO_BUS_STATS_ADDR_ATTR_GROUP_DECL(2); 373 MDIO_BUS_STATS_ADDR_ATTR_GROUP_DECL(3); 374 MDIO_BUS_STATS_ADDR_ATTR_GROUP_DECL(4); 375 MDIO_BUS_STATS_ADDR_ATTR_GROUP_DECL(5); 376 MDIO_BUS_STATS_ADDR_ATTR_GROUP_DECL(6); 377 MDIO_BUS_STATS_ADDR_ATTR_GROUP_DECL(7); 378 MDIO_BUS_STATS_ADDR_ATTR_GROUP_DECL(8); 379 MDIO_BUS_STATS_ADDR_ATTR_GROUP_DECL(9); 380 MDIO_BUS_STATS_ADDR_ATTR_GROUP_DECL(10); 381 MDIO_BUS_STATS_ADDR_ATTR_GROUP_DECL(11); 382 MDIO_BUS_STATS_ADDR_ATTR_GROUP_DECL(12); 383 MDIO_BUS_STATS_ADDR_ATTR_GROUP_DECL(13); 384 MDIO_BUS_STATS_ADDR_ATTR_GROUP_DECL(14); 385 MDIO_BUS_STATS_ADDR_ATTR_GROUP_DECL(15); 386 MDIO_BUS_STATS_ADDR_ATTR_GROUP_DECL(16); 387 MDIO_BUS_STATS_ADDR_ATTR_GROUP_DECL(17); 388 MDIO_BUS_STATS_ADDR_ATTR_GROUP_DECL(18); 389 MDIO_BUS_STATS_ADDR_ATTR_GROUP_DECL(19); 390 MDIO_BUS_STATS_ADDR_ATTR_GROUP_DECL(20); 391 MDIO_BUS_STATS_ADDR_ATTR_GROUP_DECL(21); 392 MDIO_BUS_STATS_ADDR_ATTR_GROUP_DECL(22); 393 MDIO_BUS_STATS_ADDR_ATTR_GROUP_DECL(23); 394 MDIO_BUS_STATS_ADDR_ATTR_GROUP_DECL(24); 395 MDIO_BUS_STATS_ADDR_ATTR_GROUP_DECL(25); 396 MDIO_BUS_STATS_ADDR_ATTR_GROUP_DECL(26); 397 MDIO_BUS_STATS_ADDR_ATTR_GROUP_DECL(27); 398 MDIO_BUS_STATS_ADDR_ATTR_GROUP_DECL(28); 399 MDIO_BUS_STATS_ADDR_ATTR_GROUP_DECL(29); 400 MDIO_BUS_STATS_ADDR_ATTR_GROUP_DECL(30); 401 MDIO_BUS_STATS_ADDR_ATTR_GROUP_DECL(31); 402 403 #define MDIO_BUS_STATS_ADDR_ATTR_GROUP(addr) \ 404 &dev_attr_mdio_bus_addr_transfers_##addr.attr.attr, \ 405 &dev_attr_mdio_bus_addr_errors_##addr.attr.attr, \ 406 &dev_attr_mdio_bus_addr_writes_##addr.attr.attr, \ 407 &dev_attr_mdio_bus_addr_reads_##addr.attr.attr \ 408 409 static struct attribute *mdio_bus_statistics_attrs[] = { 410 &dev_attr_mdio_bus_transfers.attr.attr, 411 &dev_attr_mdio_bus_errors.attr.attr, 412 &dev_attr_mdio_bus_writes.attr.attr, 413 &dev_attr_mdio_bus_reads.attr.attr, 414 MDIO_BUS_STATS_ADDR_ATTR_GROUP(0), 415 MDIO_BUS_STATS_ADDR_ATTR_GROUP(1), 416 MDIO_BUS_STATS_ADDR_ATTR_GROUP(2), 417 MDIO_BUS_STATS_ADDR_ATTR_GROUP(3), 418 MDIO_BUS_STATS_ADDR_ATTR_GROUP(4), 419 MDIO_BUS_STATS_ADDR_ATTR_GROUP(5), 420 MDIO_BUS_STATS_ADDR_ATTR_GROUP(6), 421 MDIO_BUS_STATS_ADDR_ATTR_GROUP(7), 422 MDIO_BUS_STATS_ADDR_ATTR_GROUP(8), 423 MDIO_BUS_STATS_ADDR_ATTR_GROUP(9), 424 MDIO_BUS_STATS_ADDR_ATTR_GROUP(10), 425 MDIO_BUS_STATS_ADDR_ATTR_GROUP(11), 426 MDIO_BUS_STATS_ADDR_ATTR_GROUP(12), 427 MDIO_BUS_STATS_ADDR_ATTR_GROUP(13), 428 MDIO_BUS_STATS_ADDR_ATTR_GROUP(14), 429 MDIO_BUS_STATS_ADDR_ATTR_GROUP(15), 430 MDIO_BUS_STATS_ADDR_ATTR_GROUP(16), 431 MDIO_BUS_STATS_ADDR_ATTR_GROUP(17), 432 MDIO_BUS_STATS_ADDR_ATTR_GROUP(18), 433 MDIO_BUS_STATS_ADDR_ATTR_GROUP(19), 434 MDIO_BUS_STATS_ADDR_ATTR_GROUP(20), 435 MDIO_BUS_STATS_ADDR_ATTR_GROUP(21), 436 MDIO_BUS_STATS_ADDR_ATTR_GROUP(22), 437 MDIO_BUS_STATS_ADDR_ATTR_GROUP(23), 438 MDIO_BUS_STATS_ADDR_ATTR_GROUP(24), 439 MDIO_BUS_STATS_ADDR_ATTR_GROUP(25), 440 MDIO_BUS_STATS_ADDR_ATTR_GROUP(26), 441 MDIO_BUS_STATS_ADDR_ATTR_GROUP(27), 442 MDIO_BUS_STATS_ADDR_ATTR_GROUP(28), 443 MDIO_BUS_STATS_ADDR_ATTR_GROUP(29), 444 MDIO_BUS_STATS_ADDR_ATTR_GROUP(30), 445 MDIO_BUS_STATS_ADDR_ATTR_GROUP(31), 446 NULL, 447 }; 448 449 static const struct attribute_group mdio_bus_statistics_group = { 450 .name = "statistics", 451 .attrs = mdio_bus_statistics_attrs, 452 }; 453 454 static const struct attribute_group *mdio_bus_groups[] = { 455 &mdio_bus_statistics_group, 456 NULL, 457 }; 458 459 static struct class mdio_bus_class = { 460 .name = "mdio_bus", 461 .dev_release = mdiobus_release, 462 .dev_groups = mdio_bus_groups, 463 }; 464 465 #if IS_ENABLED(CONFIG_OF_MDIO) 466 /** 467 * of_mdio_find_bus - Given an mii_bus node, find the mii_bus. 468 * @mdio_bus_np: Pointer to the mii_bus. 469 * 470 * Returns a reference to the mii_bus, or NULL if none found. The 471 * embedded struct device will have its reference count incremented, 472 * and this must be put once the bus is finished with. 473 * 474 * Because the association of a device_node and mii_bus is made via 475 * of_mdiobus_register(), the mii_bus cannot be found before it is 476 * registered with of_mdiobus_register(). 477 * 478 */ 479 struct mii_bus *of_mdio_find_bus(struct device_node *mdio_bus_np) 480 { 481 struct device *d; 482 483 if (!mdio_bus_np) 484 return NULL; 485 486 d = class_find_device_by_of_node(&mdio_bus_class, mdio_bus_np); 487 return d ? to_mii_bus(d) : NULL; 488 } 489 EXPORT_SYMBOL(of_mdio_find_bus); 490 491 /* Walk the list of subnodes of a mdio bus and look for a node that 492 * matches the mdio device's address with its 'reg' property. If 493 * found, set the of_node pointer for the mdio device. This allows 494 * auto-probed phy devices to be supplied with information passed in 495 * via DT. 496 */ 497 static void of_mdiobus_link_mdiodev(struct mii_bus *bus, 498 struct mdio_device *mdiodev) 499 { 500 struct device *dev = &mdiodev->dev; 501 struct device_node *child; 502 503 if (dev->of_node || !bus->dev.of_node) 504 return; 505 506 for_each_available_child_of_node(bus->dev.of_node, child) { 507 int addr; 508 509 addr = of_mdio_parse_addr(dev, child); 510 if (addr < 0) 511 continue; 512 513 if (addr == mdiodev->addr) { 514 dev->of_node = child; 515 dev->fwnode = of_fwnode_handle(child); 516 return; 517 } 518 } 519 } 520 #else /* !IS_ENABLED(CONFIG_OF_MDIO) */ 521 static inline void of_mdiobus_link_mdiodev(struct mii_bus *mdio, 522 struct mdio_device *mdiodev) 523 { 524 } 525 #endif 526 527 /** 528 * mdiobus_create_device_from_board_info - create a full MDIO device given 529 * a mdio_board_info structure 530 * @bus: MDIO bus to create the devices on 531 * @bi: mdio_board_info structure describing the devices 532 * 533 * Returns 0 on success or < 0 on error. 534 */ 535 static int mdiobus_create_device(struct mii_bus *bus, 536 struct mdio_board_info *bi) 537 { 538 struct mdio_device *mdiodev; 539 int ret = 0; 540 541 mdiodev = mdio_device_create(bus, bi->mdio_addr); 542 if (IS_ERR(mdiodev)) 543 return -ENODEV; 544 545 strncpy(mdiodev->modalias, bi->modalias, 546 sizeof(mdiodev->modalias)); 547 mdiodev->bus_match = mdio_device_bus_match; 548 mdiodev->dev.platform_data = (void *)bi->platform_data; 549 550 ret = mdio_device_register(mdiodev); 551 if (ret) 552 mdio_device_free(mdiodev); 553 554 return ret; 555 } 556 557 /** 558 * __mdiobus_register - bring up all the PHYs on a given bus and attach them to bus 559 * @bus: target mii_bus 560 * @owner: module containing bus accessor functions 561 * 562 * Description: Called by a bus driver to bring up all the PHYs 563 * on a given bus, and attach them to the bus. Drivers should use 564 * mdiobus_register() rather than __mdiobus_register() unless they 565 * need to pass a specific owner module. MDIO devices which are not 566 * PHYs will not be brought up by this function. They are expected to 567 * to be explicitly listed in DT and instantiated by of_mdiobus_register(). 568 * 569 * Returns 0 on success or < 0 on error. 570 */ 571 int __mdiobus_register(struct mii_bus *bus, struct module *owner) 572 { 573 struct mdio_device *mdiodev; 574 int i, err; 575 struct gpio_desc *gpiod; 576 577 if (NULL == bus || NULL == bus->name || 578 NULL == bus->read || NULL == bus->write) 579 return -EINVAL; 580 581 BUG_ON(bus->state != MDIOBUS_ALLOCATED && 582 bus->state != MDIOBUS_UNREGISTERED); 583 584 bus->owner = owner; 585 bus->dev.parent = bus->parent; 586 bus->dev.class = &mdio_bus_class; 587 bus->dev.groups = NULL; 588 dev_set_name(&bus->dev, "%s", bus->id); 589 590 err = device_register(&bus->dev); 591 if (err) { 592 pr_err("mii_bus %s failed to register\n", bus->id); 593 return -EINVAL; 594 } 595 596 mutex_init(&bus->mdio_lock); 597 598 /* de-assert bus level PHY GPIO reset */ 599 gpiod = devm_gpiod_get_optional(&bus->dev, "reset", GPIOD_OUT_LOW); 600 if (IS_ERR(gpiod)) { 601 dev_err(&bus->dev, "mii_bus %s couldn't get reset GPIO\n", 602 bus->id); 603 device_del(&bus->dev); 604 return PTR_ERR(gpiod); 605 } else if (gpiod) { 606 bus->reset_gpiod = gpiod; 607 608 gpiod_set_value_cansleep(gpiod, 1); 609 udelay(bus->reset_delay_us); 610 gpiod_set_value_cansleep(gpiod, 0); 611 } 612 613 if (bus->reset) 614 bus->reset(bus); 615 616 for (i = 0; i < PHY_MAX_ADDR; i++) { 617 if ((bus->phy_mask & (1 << i)) == 0) { 618 struct phy_device *phydev; 619 620 phydev = mdiobus_scan(bus, i); 621 if (IS_ERR(phydev) && (PTR_ERR(phydev) != -ENODEV)) { 622 err = PTR_ERR(phydev); 623 goto error; 624 } 625 } 626 } 627 628 mdiobus_setup_mdiodev_from_board_info(bus, mdiobus_create_device); 629 630 bus->state = MDIOBUS_REGISTERED; 631 pr_info("%s: probed\n", bus->name); 632 return 0; 633 634 error: 635 while (--i >= 0) { 636 mdiodev = bus->mdio_map[i]; 637 if (!mdiodev) 638 continue; 639 640 mdiodev->device_remove(mdiodev); 641 mdiodev->device_free(mdiodev); 642 } 643 644 /* Put PHYs in RESET to save power */ 645 if (bus->reset_gpiod) 646 gpiod_set_value_cansleep(bus->reset_gpiod, 1); 647 648 device_del(&bus->dev); 649 return err; 650 } 651 EXPORT_SYMBOL(__mdiobus_register); 652 653 void mdiobus_unregister(struct mii_bus *bus) 654 { 655 struct mdio_device *mdiodev; 656 int i; 657 658 BUG_ON(bus->state != MDIOBUS_REGISTERED); 659 bus->state = MDIOBUS_UNREGISTERED; 660 661 for (i = 0; i < PHY_MAX_ADDR; i++) { 662 mdiodev = bus->mdio_map[i]; 663 if (!mdiodev) 664 continue; 665 666 if (mdiodev->reset_gpio) 667 gpiod_put(mdiodev->reset_gpio); 668 669 mdiodev->device_remove(mdiodev); 670 mdiodev->device_free(mdiodev); 671 } 672 673 /* Put PHYs in RESET to save power */ 674 if (bus->reset_gpiod) 675 gpiod_set_value_cansleep(bus->reset_gpiod, 1); 676 677 device_del(&bus->dev); 678 } 679 EXPORT_SYMBOL(mdiobus_unregister); 680 681 /** 682 * mdiobus_free - free a struct mii_bus 683 * @bus: mii_bus to free 684 * 685 * This function releases the reference to the underlying device 686 * object in the mii_bus. If this is the last reference, the mii_bus 687 * will be freed. 688 */ 689 void mdiobus_free(struct mii_bus *bus) 690 { 691 /* For compatibility with error handling in drivers. */ 692 if (bus->state == MDIOBUS_ALLOCATED) { 693 kfree(bus); 694 return; 695 } 696 697 BUG_ON(bus->state != MDIOBUS_UNREGISTERED); 698 bus->state = MDIOBUS_RELEASED; 699 700 put_device(&bus->dev); 701 } 702 EXPORT_SYMBOL(mdiobus_free); 703 704 /** 705 * mdiobus_scan - scan a bus for MDIO devices. 706 * @bus: mii_bus to scan 707 * @addr: address on bus to scan 708 * 709 * This function scans the MDIO bus, looking for devices which can be 710 * identified using a vendor/product ID in registers 2 and 3. Not all 711 * MDIO devices have such registers, but PHY devices typically 712 * do. Hence this function assumes anything found is a PHY, or can be 713 * treated as a PHY. Other MDIO devices, such as switches, will 714 * probably not be found during the scan. 715 */ 716 struct phy_device *mdiobus_scan(struct mii_bus *bus, int addr) 717 { 718 struct phy_device *phydev; 719 int err; 720 721 phydev = get_phy_device(bus, addr, false); 722 if (IS_ERR(phydev)) 723 return phydev; 724 725 /* 726 * For DT, see if the auto-probed phy has a correspoding child 727 * in the bus node, and set the of_node pointer in this case. 728 */ 729 of_mdiobus_link_mdiodev(bus, &phydev->mdio); 730 731 err = phy_device_register(phydev); 732 if (err) { 733 phy_device_free(phydev); 734 return ERR_PTR(-ENODEV); 735 } 736 737 return phydev; 738 } 739 EXPORT_SYMBOL(mdiobus_scan); 740 741 static void mdiobus_stats_acct(struct mdio_bus_stats *stats, bool op, int ret) 742 { 743 u64_stats_update_begin(&stats->syncp); 744 745 u64_stats_inc(&stats->transfers); 746 if (ret < 0) { 747 u64_stats_inc(&stats->errors); 748 goto out; 749 } 750 751 if (op) 752 u64_stats_inc(&stats->reads); 753 else 754 u64_stats_inc(&stats->writes); 755 out: 756 u64_stats_update_end(&stats->syncp); 757 } 758 759 /** 760 * __mdiobus_read - Unlocked version of the mdiobus_read function 761 * @bus: the mii_bus struct 762 * @addr: the phy address 763 * @regnum: register number to read 764 * 765 * Read a MDIO bus register. Caller must hold the mdio bus lock. 766 * 767 * NOTE: MUST NOT be called from interrupt context. 768 */ 769 int __mdiobus_read(struct mii_bus *bus, int addr, u32 regnum) 770 { 771 int retval; 772 773 WARN_ON_ONCE(!mutex_is_locked(&bus->mdio_lock)); 774 775 retval = bus->read(bus, addr, regnum); 776 777 trace_mdio_access(bus, 1, addr, regnum, retval, retval); 778 mdiobus_stats_acct(&bus->stats[addr], true, retval); 779 780 return retval; 781 } 782 EXPORT_SYMBOL(__mdiobus_read); 783 784 /** 785 * __mdiobus_write - Unlocked version of the mdiobus_write function 786 * @bus: the mii_bus struct 787 * @addr: the phy address 788 * @regnum: register number to write 789 * @val: value to write to @regnum 790 * 791 * Write a MDIO bus register. Caller must hold the mdio bus lock. 792 * 793 * NOTE: MUST NOT be called from interrupt context. 794 */ 795 int __mdiobus_write(struct mii_bus *bus, int addr, u32 regnum, u16 val) 796 { 797 int err; 798 799 WARN_ON_ONCE(!mutex_is_locked(&bus->mdio_lock)); 800 801 err = bus->write(bus, addr, regnum, val); 802 803 trace_mdio_access(bus, 0, addr, regnum, val, err); 804 mdiobus_stats_acct(&bus->stats[addr], false, err); 805 806 return err; 807 } 808 EXPORT_SYMBOL(__mdiobus_write); 809 810 /** 811 * mdiobus_read_nested - Nested version of the mdiobus_read function 812 * @bus: the mii_bus struct 813 * @addr: the phy address 814 * @regnum: register number to read 815 * 816 * In case of nested MDIO bus access avoid lockdep false positives by 817 * using mutex_lock_nested(). 818 * 819 * NOTE: MUST NOT be called from interrupt context, 820 * because the bus read/write functions may wait for an interrupt 821 * to conclude the operation. 822 */ 823 int mdiobus_read_nested(struct mii_bus *bus, int addr, u32 regnum) 824 { 825 int retval; 826 827 BUG_ON(in_interrupt()); 828 829 mutex_lock_nested(&bus->mdio_lock, MDIO_MUTEX_NESTED); 830 retval = __mdiobus_read(bus, addr, regnum); 831 mutex_unlock(&bus->mdio_lock); 832 833 return retval; 834 } 835 EXPORT_SYMBOL(mdiobus_read_nested); 836 837 /** 838 * mdiobus_read - Convenience function for reading a given MII mgmt register 839 * @bus: the mii_bus struct 840 * @addr: the phy address 841 * @regnum: register number to read 842 * 843 * NOTE: MUST NOT be called from interrupt context, 844 * because the bus read/write functions may wait for an interrupt 845 * to conclude the operation. 846 */ 847 int mdiobus_read(struct mii_bus *bus, int addr, u32 regnum) 848 { 849 int retval; 850 851 BUG_ON(in_interrupt()); 852 853 mutex_lock(&bus->mdio_lock); 854 retval = __mdiobus_read(bus, addr, regnum); 855 mutex_unlock(&bus->mdio_lock); 856 857 return retval; 858 } 859 EXPORT_SYMBOL(mdiobus_read); 860 861 /** 862 * mdiobus_write_nested - Nested version of the mdiobus_write function 863 * @bus: the mii_bus struct 864 * @addr: the phy address 865 * @regnum: register number to write 866 * @val: value to write to @regnum 867 * 868 * In case of nested MDIO bus access avoid lockdep false positives by 869 * using mutex_lock_nested(). 870 * 871 * NOTE: MUST NOT be called from interrupt context, 872 * because the bus read/write functions may wait for an interrupt 873 * to conclude the operation. 874 */ 875 int mdiobus_write_nested(struct mii_bus *bus, int addr, u32 regnum, u16 val) 876 { 877 int err; 878 879 BUG_ON(in_interrupt()); 880 881 mutex_lock_nested(&bus->mdio_lock, MDIO_MUTEX_NESTED); 882 err = __mdiobus_write(bus, addr, regnum, val); 883 mutex_unlock(&bus->mdio_lock); 884 885 return err; 886 } 887 EXPORT_SYMBOL(mdiobus_write_nested); 888 889 /** 890 * mdiobus_write - Convenience function for writing a given MII mgmt register 891 * @bus: the mii_bus struct 892 * @addr: the phy address 893 * @regnum: register number to write 894 * @val: value to write to @regnum 895 * 896 * NOTE: MUST NOT be called from interrupt context, 897 * because the bus read/write functions may wait for an interrupt 898 * to conclude the operation. 899 */ 900 int mdiobus_write(struct mii_bus *bus, int addr, u32 regnum, u16 val) 901 { 902 int err; 903 904 BUG_ON(in_interrupt()); 905 906 mutex_lock(&bus->mdio_lock); 907 err = __mdiobus_write(bus, addr, regnum, val); 908 mutex_unlock(&bus->mdio_lock); 909 910 return err; 911 } 912 EXPORT_SYMBOL(mdiobus_write); 913 914 /** 915 * mdio_bus_match - determine if given MDIO driver supports the given 916 * MDIO device 917 * @dev: target MDIO device 918 * @drv: given MDIO driver 919 * 920 * Description: Given a MDIO device, and a MDIO driver, return 1 if 921 * the driver supports the device. Otherwise, return 0. This may 922 * require calling the devices own match function, since different classes 923 * of MDIO devices have different match criteria. 924 */ 925 static int mdio_bus_match(struct device *dev, struct device_driver *drv) 926 { 927 struct mdio_device *mdio = to_mdio_device(dev); 928 929 if (of_driver_match_device(dev, drv)) 930 return 1; 931 932 if (mdio->bus_match) 933 return mdio->bus_match(dev, drv); 934 935 return 0; 936 } 937 938 static int mdio_uevent(struct device *dev, struct kobj_uevent_env *env) 939 { 940 int rc; 941 942 /* Some devices have extra OF data and an OF-style MODALIAS */ 943 rc = of_device_uevent_modalias(dev, env); 944 if (rc != -ENODEV) 945 return rc; 946 947 return 0; 948 } 949 950 static struct attribute *mdio_bus_device_statistics_attrs[] = { 951 &dev_attr_mdio_bus_device_transfers.attr.attr, 952 &dev_attr_mdio_bus_device_errors.attr.attr, 953 &dev_attr_mdio_bus_device_writes.attr.attr, 954 &dev_attr_mdio_bus_device_reads.attr.attr, 955 NULL, 956 }; 957 958 static const struct attribute_group mdio_bus_device_statistics_group = { 959 .name = "statistics", 960 .attrs = mdio_bus_device_statistics_attrs, 961 }; 962 963 static const struct attribute_group *mdio_bus_dev_groups[] = { 964 &mdio_bus_device_statistics_group, 965 NULL, 966 }; 967 968 struct bus_type mdio_bus_type = { 969 .name = "mdio_bus", 970 .dev_groups = mdio_bus_dev_groups, 971 .match = mdio_bus_match, 972 .uevent = mdio_uevent, 973 }; 974 EXPORT_SYMBOL(mdio_bus_type); 975 976 int __init mdio_bus_init(void) 977 { 978 int ret; 979 980 ret = class_register(&mdio_bus_class); 981 if (!ret) { 982 ret = bus_register(&mdio_bus_type); 983 if (ret) 984 class_unregister(&mdio_bus_class); 985 } 986 987 return ret; 988 } 989 EXPORT_SYMBOL_GPL(mdio_bus_init); 990 991 #if IS_ENABLED(CONFIG_PHYLIB) 992 void mdio_bus_exit(void) 993 { 994 class_unregister(&mdio_bus_class); 995 bus_unregister(&mdio_bus_type); 996 } 997 EXPORT_SYMBOL_GPL(mdio_bus_exit); 998 #else 999 module_init(mdio_bus_init); 1000 /* no module_exit, intentional */ 1001 MODULE_LICENSE("GPL"); 1002 MODULE_DESCRIPTION("MDIO bus/device layer"); 1003 #endif 1004