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