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/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.h> 17 #include <linux/gpio/consumer.h> 18 #include <linux/init.h> 19 #include <linux/interrupt.h> 20 #include <linux/io.h> 21 #include <linux/kernel.h> 22 #include <linux/micrel_phy.h> 23 #include <linux/mii.h> 24 #include <linux/mm.h> 25 #include <linux/module.h> 26 #include <linux/netdevice.h> 27 #include <linux/of_device.h> 28 #include <linux/of_gpio.h> 29 #include <linux/of_mdio.h> 30 #include <linux/phy.h> 31 #include <linux/reset.h> 32 #include <linux/skbuff.h> 33 #include <linux/slab.h> 34 #include <linux/spinlock.h> 35 #include <linux/string.h> 36 #include <linux/uaccess.h> 37 #include <linux/unistd.h> 38 39 #define CREATE_TRACE_POINTS 40 #include <trace/events/mdio.h> 41 42 #include "mdio-boardinfo.h" 43 44 static int mdiobus_register_gpiod(struct mdio_device *mdiodev) 45 { 46 /* Deassert the optional reset signal */ 47 mdiodev->reset_gpio = gpiod_get_optional(&mdiodev->dev, 48 "reset", GPIOD_OUT_LOW); 49 if (IS_ERR(mdiodev->reset_gpio)) 50 return PTR_ERR(mdiodev->reset_gpio); 51 52 if (mdiodev->reset_gpio) 53 gpiod_set_consumer_name(mdiodev->reset_gpio, "PHY reset"); 54 55 return 0; 56 } 57 58 static int mdiobus_register_reset(struct mdio_device *mdiodev) 59 { 60 struct reset_control *reset; 61 62 reset = reset_control_get_optional_exclusive(&mdiodev->dev, "phy"); 63 if (IS_ERR(reset)) 64 return PTR_ERR(reset); 65 66 mdiodev->reset_ctrl = reset; 67 68 return 0; 69 } 70 71 int mdiobus_register_device(struct mdio_device *mdiodev) 72 { 73 int err; 74 75 if (mdiodev->bus->mdio_map[mdiodev->addr]) 76 return -EBUSY; 77 78 if (mdiodev->flags & MDIO_DEVICE_FLAG_PHY) { 79 err = mdiobus_register_gpiod(mdiodev); 80 if (err) 81 return err; 82 83 err = mdiobus_register_reset(mdiodev); 84 if (err) 85 return err; 86 87 /* Assert the reset signal */ 88 mdio_device_reset(mdiodev, 1); 89 } 90 91 mdiodev->bus->mdio_map[mdiodev->addr] = mdiodev; 92 93 return 0; 94 } 95 EXPORT_SYMBOL(mdiobus_register_device); 96 97 int mdiobus_unregister_device(struct mdio_device *mdiodev) 98 { 99 if (mdiodev->bus->mdio_map[mdiodev->addr] != mdiodev) 100 return -EINVAL; 101 102 reset_control_put(mdiodev->reset_ctrl); 103 104 mdiodev->bus->mdio_map[mdiodev->addr] = NULL; 105 106 return 0; 107 } 108 EXPORT_SYMBOL(mdiobus_unregister_device); 109 110 struct phy_device *mdiobus_get_phy(struct mii_bus *bus, int addr) 111 { 112 bool addr_valid = addr >= 0 && addr < ARRAY_SIZE(bus->mdio_map); 113 struct mdio_device *mdiodev; 114 115 if (WARN_ONCE(!addr_valid, "addr %d out of range\n", addr)) 116 return NULL; 117 118 mdiodev = bus->mdio_map[addr]; 119 120 if (!mdiodev) 121 return NULL; 122 123 if (!(mdiodev->flags & MDIO_DEVICE_FLAG_PHY)) 124 return NULL; 125 126 return container_of(mdiodev, struct phy_device, mdio); 127 } 128 EXPORT_SYMBOL(mdiobus_get_phy); 129 130 bool mdiobus_is_registered_device(struct mii_bus *bus, int addr) 131 { 132 return bus->mdio_map[addr]; 133 } 134 EXPORT_SYMBOL(mdiobus_is_registered_device); 135 136 /** 137 * mdiobus_alloc_size - allocate a mii_bus structure 138 * @size: extra amount of memory to allocate for private storage. 139 * If non-zero, then bus->priv is points to that memory. 140 * 141 * Description: called by a bus driver to allocate an mii_bus 142 * structure to fill in. 143 */ 144 struct mii_bus *mdiobus_alloc_size(size_t size) 145 { 146 struct mii_bus *bus; 147 size_t aligned_size = ALIGN(sizeof(*bus), NETDEV_ALIGN); 148 size_t alloc_size; 149 int i; 150 151 /* If we alloc extra space, it should be aligned */ 152 if (size) 153 alloc_size = aligned_size + size; 154 else 155 alloc_size = sizeof(*bus); 156 157 bus = kzalloc(alloc_size, GFP_KERNEL); 158 if (!bus) 159 return NULL; 160 161 bus->state = MDIOBUS_ALLOCATED; 162 if (size) 163 bus->priv = (void *)bus + aligned_size; 164 165 /* Initialise the interrupts to polling and 64-bit seqcounts */ 166 for (i = 0; i < PHY_MAX_ADDR; i++) { 167 bus->irq[i] = PHY_POLL; 168 u64_stats_init(&bus->stats[i].syncp); 169 } 170 171 return bus; 172 } 173 EXPORT_SYMBOL(mdiobus_alloc_size); 174 175 /** 176 * mdiobus_release - mii_bus device release callback 177 * @d: the target struct device that contains the mii_bus 178 * 179 * Description: called when the last reference to an mii_bus is 180 * dropped, to free the underlying memory. 181 */ 182 static void mdiobus_release(struct device *d) 183 { 184 struct mii_bus *bus = to_mii_bus(d); 185 186 WARN(bus->state != MDIOBUS_RELEASED && 187 /* for compatibility with error handling in drivers */ 188 bus->state != MDIOBUS_ALLOCATED, 189 "%s: not in RELEASED or ALLOCATED state\n", 190 bus->id); 191 kfree(bus); 192 } 193 194 struct mdio_bus_stat_attr { 195 int addr; 196 unsigned int field_offset; 197 }; 198 199 static u64 mdio_bus_get_stat(struct mdio_bus_stats *s, unsigned int offset) 200 { 201 const char *p = (const char *)s + offset; 202 unsigned int start; 203 u64 val = 0; 204 205 do { 206 start = u64_stats_fetch_begin(&s->syncp); 207 val = u64_stats_read((const u64_stats_t *)p); 208 } while (u64_stats_fetch_retry(&s->syncp, start)); 209 210 return val; 211 } 212 213 static u64 mdio_bus_get_global_stat(struct mii_bus *bus, unsigned int offset) 214 { 215 unsigned int i; 216 u64 val = 0; 217 218 for (i = 0; i < PHY_MAX_ADDR; i++) 219 val += mdio_bus_get_stat(&bus->stats[i], offset); 220 221 return val; 222 } 223 224 static ssize_t mdio_bus_stat_field_show(struct device *dev, 225 struct device_attribute *attr, 226 char *buf) 227 { 228 struct mii_bus *bus = to_mii_bus(dev); 229 struct mdio_bus_stat_attr *sattr; 230 struct dev_ext_attribute *eattr; 231 u64 val; 232 233 eattr = container_of(attr, struct dev_ext_attribute, attr); 234 sattr = eattr->var; 235 236 if (sattr->addr < 0) 237 val = mdio_bus_get_global_stat(bus, sattr->field_offset); 238 else 239 val = mdio_bus_get_stat(&bus->stats[sattr->addr], 240 sattr->field_offset); 241 242 return sysfs_emit(buf, "%llu\n", val); 243 } 244 245 static ssize_t mdio_bus_device_stat_field_show(struct device *dev, 246 struct device_attribute *attr, 247 char *buf) 248 { 249 struct mdio_device *mdiodev = to_mdio_device(dev); 250 struct mii_bus *bus = mdiodev->bus; 251 struct mdio_bus_stat_attr *sattr; 252 struct dev_ext_attribute *eattr; 253 int addr = mdiodev->addr; 254 u64 val; 255 256 eattr = container_of(attr, struct dev_ext_attribute, attr); 257 sattr = eattr->var; 258 259 val = mdio_bus_get_stat(&bus->stats[addr], sattr->field_offset); 260 261 return sysfs_emit(buf, "%llu\n", val); 262 } 263 264 #define MDIO_BUS_STATS_ATTR_DECL(field, file) \ 265 static struct dev_ext_attribute dev_attr_mdio_bus_##field = { \ 266 .attr = { .attr = { .name = file, .mode = 0444 }, \ 267 .show = mdio_bus_stat_field_show, \ 268 }, \ 269 .var = &((struct mdio_bus_stat_attr) { \ 270 -1, offsetof(struct mdio_bus_stats, field) \ 271 }), \ 272 }; \ 273 static struct dev_ext_attribute dev_attr_mdio_bus_device_##field = { \ 274 .attr = { .attr = { .name = file, .mode = 0444 }, \ 275 .show = mdio_bus_device_stat_field_show, \ 276 }, \ 277 .var = &((struct mdio_bus_stat_attr) { \ 278 -1, offsetof(struct mdio_bus_stats, field) \ 279 }), \ 280 }; 281 282 #define MDIO_BUS_STATS_ATTR(field) \ 283 MDIO_BUS_STATS_ATTR_DECL(field, __stringify(field)) 284 285 MDIO_BUS_STATS_ATTR(transfers); 286 MDIO_BUS_STATS_ATTR(errors); 287 MDIO_BUS_STATS_ATTR(writes); 288 MDIO_BUS_STATS_ATTR(reads); 289 290 #define MDIO_BUS_STATS_ADDR_ATTR_DECL(field, addr, file) \ 291 static struct dev_ext_attribute dev_attr_mdio_bus_addr_##field##_##addr = { \ 292 .attr = { .attr = { .name = file, .mode = 0444 }, \ 293 .show = mdio_bus_stat_field_show, \ 294 }, \ 295 .var = &((struct mdio_bus_stat_attr) { \ 296 addr, offsetof(struct mdio_bus_stats, field) \ 297 }), \ 298 } 299 300 #define MDIO_BUS_STATS_ADDR_ATTR(field, addr) \ 301 MDIO_BUS_STATS_ADDR_ATTR_DECL(field, addr, \ 302 __stringify(field) "_" __stringify(addr)) 303 304 #define MDIO_BUS_STATS_ADDR_ATTR_GROUP_DECL(addr) \ 305 MDIO_BUS_STATS_ADDR_ATTR(transfers, addr); \ 306 MDIO_BUS_STATS_ADDR_ATTR(errors, addr); \ 307 MDIO_BUS_STATS_ADDR_ATTR(writes, addr); \ 308 MDIO_BUS_STATS_ADDR_ATTR(reads, addr) \ 309 310 MDIO_BUS_STATS_ADDR_ATTR_GROUP_DECL(0); 311 MDIO_BUS_STATS_ADDR_ATTR_GROUP_DECL(1); 312 MDIO_BUS_STATS_ADDR_ATTR_GROUP_DECL(2); 313 MDIO_BUS_STATS_ADDR_ATTR_GROUP_DECL(3); 314 MDIO_BUS_STATS_ADDR_ATTR_GROUP_DECL(4); 315 MDIO_BUS_STATS_ADDR_ATTR_GROUP_DECL(5); 316 MDIO_BUS_STATS_ADDR_ATTR_GROUP_DECL(6); 317 MDIO_BUS_STATS_ADDR_ATTR_GROUP_DECL(7); 318 MDIO_BUS_STATS_ADDR_ATTR_GROUP_DECL(8); 319 MDIO_BUS_STATS_ADDR_ATTR_GROUP_DECL(9); 320 MDIO_BUS_STATS_ADDR_ATTR_GROUP_DECL(10); 321 MDIO_BUS_STATS_ADDR_ATTR_GROUP_DECL(11); 322 MDIO_BUS_STATS_ADDR_ATTR_GROUP_DECL(12); 323 MDIO_BUS_STATS_ADDR_ATTR_GROUP_DECL(13); 324 MDIO_BUS_STATS_ADDR_ATTR_GROUP_DECL(14); 325 MDIO_BUS_STATS_ADDR_ATTR_GROUP_DECL(15); 326 MDIO_BUS_STATS_ADDR_ATTR_GROUP_DECL(16); 327 MDIO_BUS_STATS_ADDR_ATTR_GROUP_DECL(17); 328 MDIO_BUS_STATS_ADDR_ATTR_GROUP_DECL(18); 329 MDIO_BUS_STATS_ADDR_ATTR_GROUP_DECL(19); 330 MDIO_BUS_STATS_ADDR_ATTR_GROUP_DECL(20); 331 MDIO_BUS_STATS_ADDR_ATTR_GROUP_DECL(21); 332 MDIO_BUS_STATS_ADDR_ATTR_GROUP_DECL(22); 333 MDIO_BUS_STATS_ADDR_ATTR_GROUP_DECL(23); 334 MDIO_BUS_STATS_ADDR_ATTR_GROUP_DECL(24); 335 MDIO_BUS_STATS_ADDR_ATTR_GROUP_DECL(25); 336 MDIO_BUS_STATS_ADDR_ATTR_GROUP_DECL(26); 337 MDIO_BUS_STATS_ADDR_ATTR_GROUP_DECL(27); 338 MDIO_BUS_STATS_ADDR_ATTR_GROUP_DECL(28); 339 MDIO_BUS_STATS_ADDR_ATTR_GROUP_DECL(29); 340 MDIO_BUS_STATS_ADDR_ATTR_GROUP_DECL(30); 341 MDIO_BUS_STATS_ADDR_ATTR_GROUP_DECL(31); 342 343 #define MDIO_BUS_STATS_ADDR_ATTR_GROUP(addr) \ 344 &dev_attr_mdio_bus_addr_transfers_##addr.attr.attr, \ 345 &dev_attr_mdio_bus_addr_errors_##addr.attr.attr, \ 346 &dev_attr_mdio_bus_addr_writes_##addr.attr.attr, \ 347 &dev_attr_mdio_bus_addr_reads_##addr.attr.attr \ 348 349 static struct attribute *mdio_bus_statistics_attrs[] = { 350 &dev_attr_mdio_bus_transfers.attr.attr, 351 &dev_attr_mdio_bus_errors.attr.attr, 352 &dev_attr_mdio_bus_writes.attr.attr, 353 &dev_attr_mdio_bus_reads.attr.attr, 354 MDIO_BUS_STATS_ADDR_ATTR_GROUP(0), 355 MDIO_BUS_STATS_ADDR_ATTR_GROUP(1), 356 MDIO_BUS_STATS_ADDR_ATTR_GROUP(2), 357 MDIO_BUS_STATS_ADDR_ATTR_GROUP(3), 358 MDIO_BUS_STATS_ADDR_ATTR_GROUP(4), 359 MDIO_BUS_STATS_ADDR_ATTR_GROUP(5), 360 MDIO_BUS_STATS_ADDR_ATTR_GROUP(6), 361 MDIO_BUS_STATS_ADDR_ATTR_GROUP(7), 362 MDIO_BUS_STATS_ADDR_ATTR_GROUP(8), 363 MDIO_BUS_STATS_ADDR_ATTR_GROUP(9), 364 MDIO_BUS_STATS_ADDR_ATTR_GROUP(10), 365 MDIO_BUS_STATS_ADDR_ATTR_GROUP(11), 366 MDIO_BUS_STATS_ADDR_ATTR_GROUP(12), 367 MDIO_BUS_STATS_ADDR_ATTR_GROUP(13), 368 MDIO_BUS_STATS_ADDR_ATTR_GROUP(14), 369 MDIO_BUS_STATS_ADDR_ATTR_GROUP(15), 370 MDIO_BUS_STATS_ADDR_ATTR_GROUP(16), 371 MDIO_BUS_STATS_ADDR_ATTR_GROUP(17), 372 MDIO_BUS_STATS_ADDR_ATTR_GROUP(18), 373 MDIO_BUS_STATS_ADDR_ATTR_GROUP(19), 374 MDIO_BUS_STATS_ADDR_ATTR_GROUP(20), 375 MDIO_BUS_STATS_ADDR_ATTR_GROUP(21), 376 MDIO_BUS_STATS_ADDR_ATTR_GROUP(22), 377 MDIO_BUS_STATS_ADDR_ATTR_GROUP(23), 378 MDIO_BUS_STATS_ADDR_ATTR_GROUP(24), 379 MDIO_BUS_STATS_ADDR_ATTR_GROUP(25), 380 MDIO_BUS_STATS_ADDR_ATTR_GROUP(26), 381 MDIO_BUS_STATS_ADDR_ATTR_GROUP(27), 382 MDIO_BUS_STATS_ADDR_ATTR_GROUP(28), 383 MDIO_BUS_STATS_ADDR_ATTR_GROUP(29), 384 MDIO_BUS_STATS_ADDR_ATTR_GROUP(30), 385 MDIO_BUS_STATS_ADDR_ATTR_GROUP(31), 386 NULL, 387 }; 388 389 static const struct attribute_group mdio_bus_statistics_group = { 390 .name = "statistics", 391 .attrs = mdio_bus_statistics_attrs, 392 }; 393 394 static const struct attribute_group *mdio_bus_groups[] = { 395 &mdio_bus_statistics_group, 396 NULL, 397 }; 398 399 static struct class mdio_bus_class = { 400 .name = "mdio_bus", 401 .dev_release = mdiobus_release, 402 .dev_groups = mdio_bus_groups, 403 }; 404 405 /** 406 * mdio_find_bus - Given the name of a mdiobus, find the mii_bus. 407 * @mdio_name: The name of a mdiobus. 408 * 409 * Returns a reference to the mii_bus, or NULL if none found. The 410 * embedded struct device will have its reference count incremented, 411 * and this must be put_deviced'ed once the bus is finished with. 412 */ 413 struct mii_bus *mdio_find_bus(const char *mdio_name) 414 { 415 struct device *d; 416 417 d = class_find_device_by_name(&mdio_bus_class, mdio_name); 418 return d ? to_mii_bus(d) : NULL; 419 } 420 EXPORT_SYMBOL(mdio_find_bus); 421 422 #if IS_ENABLED(CONFIG_OF_MDIO) 423 /** 424 * of_mdio_find_bus - Given an mii_bus node, find the mii_bus. 425 * @mdio_bus_np: Pointer to the mii_bus. 426 * 427 * Returns a reference to the mii_bus, or NULL if none found. The 428 * embedded struct device will have its reference count incremented, 429 * and this must be put once the bus is finished with. 430 * 431 * Because the association of a device_node and mii_bus is made via 432 * of_mdiobus_register(), the mii_bus cannot be found before it is 433 * registered with of_mdiobus_register(). 434 * 435 */ 436 struct mii_bus *of_mdio_find_bus(struct device_node *mdio_bus_np) 437 { 438 struct device *d; 439 440 if (!mdio_bus_np) 441 return NULL; 442 443 d = class_find_device_by_of_node(&mdio_bus_class, mdio_bus_np); 444 return d ? to_mii_bus(d) : NULL; 445 } 446 EXPORT_SYMBOL(of_mdio_find_bus); 447 448 /* Walk the list of subnodes of a mdio bus and look for a node that 449 * matches the mdio device's address with its 'reg' property. If 450 * found, set the of_node pointer for the mdio device. This allows 451 * auto-probed phy devices to be supplied with information passed in 452 * via DT. 453 */ 454 static void of_mdiobus_link_mdiodev(struct mii_bus *bus, 455 struct mdio_device *mdiodev) 456 { 457 struct device *dev = &mdiodev->dev; 458 struct device_node *child; 459 460 if (dev->of_node || !bus->dev.of_node) 461 return; 462 463 for_each_available_child_of_node(bus->dev.of_node, child) { 464 int addr; 465 466 addr = of_mdio_parse_addr(dev, child); 467 if (addr < 0) 468 continue; 469 470 if (addr == mdiodev->addr) { 471 device_set_node(dev, of_fwnode_handle(child)); 472 /* The refcount on "child" is passed to the mdio 473 * device. Do _not_ use of_node_put(child) here. 474 */ 475 return; 476 } 477 } 478 } 479 #else /* !IS_ENABLED(CONFIG_OF_MDIO) */ 480 static inline void of_mdiobus_link_mdiodev(struct mii_bus *mdio, 481 struct mdio_device *mdiodev) 482 { 483 } 484 #endif 485 486 /** 487 * mdiobus_create_device - create a full MDIO device given 488 * a mdio_board_info structure 489 * @bus: MDIO bus to create the devices on 490 * @bi: mdio_board_info structure describing the devices 491 * 492 * Returns 0 on success or < 0 on error. 493 */ 494 static int mdiobus_create_device(struct mii_bus *bus, 495 struct mdio_board_info *bi) 496 { 497 struct mdio_device *mdiodev; 498 int ret = 0; 499 500 mdiodev = mdio_device_create(bus, bi->mdio_addr); 501 if (IS_ERR(mdiodev)) 502 return -ENODEV; 503 504 strncpy(mdiodev->modalias, bi->modalias, 505 sizeof(mdiodev->modalias)); 506 mdiodev->bus_match = mdio_device_bus_match; 507 mdiodev->dev.platform_data = (void *)bi->platform_data; 508 509 ret = mdio_device_register(mdiodev); 510 if (ret) 511 mdio_device_free(mdiodev); 512 513 return ret; 514 } 515 516 static struct phy_device *mdiobus_scan(struct mii_bus *bus, int addr, bool c45) 517 { 518 struct phy_device *phydev = ERR_PTR(-ENODEV); 519 int err; 520 521 phydev = get_phy_device(bus, addr, c45); 522 if (IS_ERR(phydev)) 523 return phydev; 524 525 /* For DT, see if the auto-probed phy has a corresponding child 526 * in the bus node, and set the of_node pointer in this case. 527 */ 528 of_mdiobus_link_mdiodev(bus, &phydev->mdio); 529 530 err = phy_device_register(phydev); 531 if (err) { 532 phy_device_free(phydev); 533 return ERR_PTR(-ENODEV); 534 } 535 536 return phydev; 537 } 538 539 /** 540 * mdiobus_scan_c22 - scan one address on a bus for C22 MDIO devices. 541 * @bus: mii_bus to scan 542 * @addr: address on bus to scan 543 * 544 * This function scans one address on the MDIO bus, looking for 545 * devices which can be identified using a vendor/product ID in 546 * registers 2 and 3. Not all MDIO devices have such registers, but 547 * PHY devices typically do. Hence this function assumes anything 548 * found is a PHY, or can be treated as a PHY. Other MDIO devices, 549 * such as switches, will probably not be found during the scan. 550 */ 551 struct phy_device *mdiobus_scan_c22(struct mii_bus *bus, int addr) 552 { 553 return mdiobus_scan(bus, addr, false); 554 } 555 EXPORT_SYMBOL(mdiobus_scan_c22); 556 557 /** 558 * mdiobus_scan_c45 - scan one address on a bus for C45 MDIO devices. 559 * @bus: mii_bus to scan 560 * @addr: address on bus to scan 561 * 562 * This function scans one address on the MDIO bus, looking for 563 * devices which can be identified using a vendor/product ID in 564 * registers 2 and 3. Not all MDIO devices have such registers, but 565 * PHY devices typically do. Hence this function assumes anything 566 * found is a PHY, or can be treated as a PHY. Other MDIO devices, 567 * such as switches, will probably not be found during the scan. 568 */ 569 static struct phy_device *mdiobus_scan_c45(struct mii_bus *bus, int addr) 570 { 571 return mdiobus_scan(bus, addr, true); 572 } 573 574 static int mdiobus_scan_bus_c22(struct mii_bus *bus) 575 { 576 int i; 577 578 for (i = 0; i < PHY_MAX_ADDR; i++) { 579 if ((bus->phy_mask & BIT(i)) == 0) { 580 struct phy_device *phydev; 581 582 phydev = mdiobus_scan_c22(bus, i); 583 if (IS_ERR(phydev) && (PTR_ERR(phydev) != -ENODEV)) 584 return PTR_ERR(phydev); 585 } 586 } 587 return 0; 588 } 589 590 static int mdiobus_scan_bus_c45(struct mii_bus *bus) 591 { 592 int i; 593 594 for (i = 0; i < PHY_MAX_ADDR; i++) { 595 if ((bus->phy_mask & BIT(i)) == 0) { 596 struct phy_device *phydev; 597 598 /* Don't scan C45 if we already have a C22 device */ 599 if (bus->mdio_map[i]) 600 continue; 601 602 phydev = mdiobus_scan_c45(bus, i); 603 if (IS_ERR(phydev) && (PTR_ERR(phydev) != -ENODEV)) 604 return PTR_ERR(phydev); 605 } 606 } 607 return 0; 608 } 609 610 /* There are some C22 PHYs which do bad things when where is a C45 611 * transaction on the bus, like accepting a read themselves, and 612 * stomping over the true devices reply, to performing a write to 613 * themselves which was intended for another device. Now that C22 614 * devices have been found, see if any of them are bad for C45, and if we 615 * should skip the C45 scan. 616 */ 617 static bool mdiobus_prevent_c45_scan(struct mii_bus *bus) 618 { 619 int i; 620 621 for (i = 0; i < PHY_MAX_ADDR; i++) { 622 struct phy_device *phydev; 623 u32 oui; 624 625 phydev = mdiobus_get_phy(bus, i); 626 if (!phydev) 627 continue; 628 oui = phydev->phy_id >> 10; 629 630 if (oui == MICREL_OUI) 631 return true; 632 } 633 return false; 634 } 635 636 /** 637 * __mdiobus_register - bring up all the PHYs on a given bus and attach them to bus 638 * @bus: target mii_bus 639 * @owner: module containing bus accessor functions 640 * 641 * Description: Called by a bus driver to bring up all the PHYs 642 * on a given bus, and attach them to the bus. Drivers should use 643 * mdiobus_register() rather than __mdiobus_register() unless they 644 * need to pass a specific owner module. MDIO devices which are not 645 * PHYs will not be brought up by this function. They are expected 646 * to be explicitly listed in DT and instantiated by of_mdiobus_register(). 647 * 648 * Returns 0 on success or < 0 on error. 649 */ 650 int __mdiobus_register(struct mii_bus *bus, struct module *owner) 651 { 652 struct mdio_device *mdiodev; 653 struct gpio_desc *gpiod; 654 bool prevent_c45_scan; 655 int i, err; 656 657 if (!bus || !bus->name) 658 return -EINVAL; 659 660 /* An access method always needs both read and write operations */ 661 if (!!bus->read != !!bus->write || !!bus->read_c45 != !!bus->write_c45) 662 return -EINVAL; 663 664 /* At least one method is mandatory */ 665 if (!bus->read && !bus->read_c45) 666 return -EINVAL; 667 668 if (bus->parent && bus->parent->of_node) 669 bus->parent->of_node->fwnode.flags |= 670 FWNODE_FLAG_NEEDS_CHILD_BOUND_ON_ADD; 671 672 WARN(bus->state != MDIOBUS_ALLOCATED && 673 bus->state != MDIOBUS_UNREGISTERED, 674 "%s: not in ALLOCATED or UNREGISTERED state\n", bus->id); 675 676 bus->owner = owner; 677 bus->dev.parent = bus->parent; 678 bus->dev.class = &mdio_bus_class; 679 bus->dev.groups = NULL; 680 dev_set_name(&bus->dev, "%s", bus->id); 681 682 /* We need to set state to MDIOBUS_UNREGISTERED to correctly release 683 * the device in mdiobus_free() 684 * 685 * State will be updated later in this function in case of success 686 */ 687 bus->state = MDIOBUS_UNREGISTERED; 688 689 err = device_register(&bus->dev); 690 if (err) { 691 pr_err("mii_bus %s failed to register\n", bus->id); 692 return -EINVAL; 693 } 694 695 mutex_init(&bus->mdio_lock); 696 mutex_init(&bus->shared_lock); 697 698 /* assert bus level PHY GPIO reset */ 699 gpiod = devm_gpiod_get_optional(&bus->dev, "reset", GPIOD_OUT_HIGH); 700 if (IS_ERR(gpiod)) { 701 err = dev_err_probe(&bus->dev, PTR_ERR(gpiod), 702 "mii_bus %s couldn't get reset GPIO\n", 703 bus->id); 704 device_del(&bus->dev); 705 return err; 706 } else if (gpiod) { 707 bus->reset_gpiod = gpiod; 708 fsleep(bus->reset_delay_us); 709 gpiod_set_value_cansleep(gpiod, 0); 710 if (bus->reset_post_delay_us > 0) 711 fsleep(bus->reset_post_delay_us); 712 } 713 714 if (bus->reset) { 715 err = bus->reset(bus); 716 if (err) 717 goto error_reset_gpiod; 718 } 719 720 if (bus->read) { 721 err = mdiobus_scan_bus_c22(bus); 722 if (err) 723 goto error; 724 } 725 726 prevent_c45_scan = mdiobus_prevent_c45_scan(bus); 727 728 if (!prevent_c45_scan && bus->read_c45) { 729 err = mdiobus_scan_bus_c45(bus); 730 if (err) 731 goto error; 732 } 733 734 mdiobus_setup_mdiodev_from_board_info(bus, mdiobus_create_device); 735 736 bus->state = MDIOBUS_REGISTERED; 737 dev_dbg(&bus->dev, "probed\n"); 738 return 0; 739 740 error: 741 for (i = 0; i < PHY_MAX_ADDR; i++) { 742 mdiodev = bus->mdio_map[i]; 743 if (!mdiodev) 744 continue; 745 746 mdiodev->device_remove(mdiodev); 747 mdiodev->device_free(mdiodev); 748 } 749 error_reset_gpiod: 750 /* Put PHYs in RESET to save power */ 751 if (bus->reset_gpiod) 752 gpiod_set_value_cansleep(bus->reset_gpiod, 1); 753 754 device_del(&bus->dev); 755 return err; 756 } 757 EXPORT_SYMBOL(__mdiobus_register); 758 759 void mdiobus_unregister(struct mii_bus *bus) 760 { 761 struct mdio_device *mdiodev; 762 int i; 763 764 if (WARN_ON_ONCE(bus->state != MDIOBUS_REGISTERED)) 765 return; 766 bus->state = MDIOBUS_UNREGISTERED; 767 768 for (i = 0; i < PHY_MAX_ADDR; i++) { 769 mdiodev = bus->mdio_map[i]; 770 if (!mdiodev) 771 continue; 772 773 if (mdiodev->reset_gpio) 774 gpiod_put(mdiodev->reset_gpio); 775 776 mdiodev->device_remove(mdiodev); 777 mdiodev->device_free(mdiodev); 778 } 779 780 /* Put PHYs in RESET to save power */ 781 if (bus->reset_gpiod) 782 gpiod_set_value_cansleep(bus->reset_gpiod, 1); 783 784 device_del(&bus->dev); 785 } 786 EXPORT_SYMBOL(mdiobus_unregister); 787 788 /** 789 * mdiobus_free - free a struct mii_bus 790 * @bus: mii_bus to free 791 * 792 * This function releases the reference to the underlying device 793 * object in the mii_bus. If this is the last reference, the mii_bus 794 * will be freed. 795 */ 796 void mdiobus_free(struct mii_bus *bus) 797 { 798 /* For compatibility with error handling in drivers. */ 799 if (bus->state == MDIOBUS_ALLOCATED) { 800 kfree(bus); 801 return; 802 } 803 804 WARN(bus->state != MDIOBUS_UNREGISTERED, 805 "%s: not in UNREGISTERED state\n", bus->id); 806 bus->state = MDIOBUS_RELEASED; 807 808 put_device(&bus->dev); 809 } 810 EXPORT_SYMBOL(mdiobus_free); 811 812 static void mdiobus_stats_acct(struct mdio_bus_stats *stats, bool op, int ret) 813 { 814 preempt_disable(); 815 u64_stats_update_begin(&stats->syncp); 816 817 u64_stats_inc(&stats->transfers); 818 if (ret < 0) { 819 u64_stats_inc(&stats->errors); 820 goto out; 821 } 822 823 if (op) 824 u64_stats_inc(&stats->reads); 825 else 826 u64_stats_inc(&stats->writes); 827 out: 828 u64_stats_update_end(&stats->syncp); 829 preempt_enable(); 830 } 831 832 /** 833 * __mdiobus_read - Unlocked version of the mdiobus_read function 834 * @bus: the mii_bus struct 835 * @addr: the phy address 836 * @regnum: register number to read 837 * 838 * Read a MDIO bus register. Caller must hold the mdio bus lock. 839 * 840 * NOTE: MUST NOT be called from interrupt context. 841 */ 842 int __mdiobus_read(struct mii_bus *bus, int addr, u32 regnum) 843 { 844 int retval; 845 846 lockdep_assert_held_once(&bus->mdio_lock); 847 848 if (bus->read) 849 retval = bus->read(bus, addr, regnum); 850 else 851 retval = -EOPNOTSUPP; 852 853 trace_mdio_access(bus, 1, addr, regnum, retval, retval); 854 mdiobus_stats_acct(&bus->stats[addr], true, retval); 855 856 return retval; 857 } 858 EXPORT_SYMBOL(__mdiobus_read); 859 860 /** 861 * __mdiobus_write - Unlocked version of the mdiobus_write function 862 * @bus: the mii_bus struct 863 * @addr: the phy address 864 * @regnum: register number to write 865 * @val: value to write to @regnum 866 * 867 * Write a MDIO bus register. Caller must hold the mdio bus lock. 868 * 869 * NOTE: MUST NOT be called from interrupt context. 870 */ 871 int __mdiobus_write(struct mii_bus *bus, int addr, u32 regnum, u16 val) 872 { 873 int err; 874 875 lockdep_assert_held_once(&bus->mdio_lock); 876 877 if (bus->write) 878 err = bus->write(bus, addr, regnum, val); 879 else 880 err = -EOPNOTSUPP; 881 882 trace_mdio_access(bus, 0, addr, regnum, val, err); 883 mdiobus_stats_acct(&bus->stats[addr], false, err); 884 885 return err; 886 } 887 EXPORT_SYMBOL(__mdiobus_write); 888 889 /** 890 * __mdiobus_modify_changed - Unlocked version of the mdiobus_modify function 891 * @bus: the mii_bus struct 892 * @addr: the phy address 893 * @regnum: register number to modify 894 * @mask: bit mask of bits to clear 895 * @set: bit mask of bits to set 896 * 897 * Read, modify, and if any change, write the register value back to the 898 * device. Any error returns a negative number. 899 * 900 * NOTE: MUST NOT be called from interrupt context. 901 */ 902 int __mdiobus_modify_changed(struct mii_bus *bus, int addr, u32 regnum, 903 u16 mask, u16 set) 904 { 905 int new, ret; 906 907 ret = __mdiobus_read(bus, addr, regnum); 908 if (ret < 0) 909 return ret; 910 911 new = (ret & ~mask) | set; 912 if (new == ret) 913 return 0; 914 915 ret = __mdiobus_write(bus, addr, regnum, new); 916 917 return ret < 0 ? ret : 1; 918 } 919 EXPORT_SYMBOL_GPL(__mdiobus_modify_changed); 920 921 /** 922 * __mdiobus_c45_read - Unlocked version of the mdiobus_c45_read function 923 * @bus: the mii_bus struct 924 * @addr: the phy address 925 * @devad: device address to read 926 * @regnum: register number to read 927 * 928 * Read a MDIO bus register. Caller must hold the mdio bus lock. 929 * 930 * NOTE: MUST NOT be called from interrupt context. 931 */ 932 int __mdiobus_c45_read(struct mii_bus *bus, int addr, int devad, u32 regnum) 933 { 934 int retval; 935 936 lockdep_assert_held_once(&bus->mdio_lock); 937 938 if (bus->read_c45) 939 retval = bus->read_c45(bus, addr, devad, regnum); 940 else 941 retval = -EOPNOTSUPP; 942 943 trace_mdio_access(bus, 1, addr, regnum, retval, retval); 944 mdiobus_stats_acct(&bus->stats[addr], true, retval); 945 946 return retval; 947 } 948 EXPORT_SYMBOL(__mdiobus_c45_read); 949 950 /** 951 * __mdiobus_c45_write - Unlocked version of the mdiobus_write function 952 * @bus: the mii_bus struct 953 * @addr: the phy address 954 * @devad: device address to read 955 * @regnum: register number to write 956 * @val: value to write to @regnum 957 * 958 * Write a MDIO bus register. Caller must hold the mdio bus lock. 959 * 960 * NOTE: MUST NOT be called from interrupt context. 961 */ 962 int __mdiobus_c45_write(struct mii_bus *bus, int addr, int devad, u32 regnum, 963 u16 val) 964 { 965 int err; 966 967 lockdep_assert_held_once(&bus->mdio_lock); 968 969 if (bus->write_c45) 970 err = bus->write_c45(bus, addr, devad, regnum, val); 971 else 972 err = -EOPNOTSUPP; 973 974 trace_mdio_access(bus, 0, addr, regnum, val, err); 975 mdiobus_stats_acct(&bus->stats[addr], false, err); 976 977 return err; 978 } 979 EXPORT_SYMBOL(__mdiobus_c45_write); 980 981 /** 982 * __mdiobus_c45_modify_changed - Unlocked version of the mdiobus_modify function 983 * @bus: the mii_bus struct 984 * @addr: the phy address 985 * @devad: device address to read 986 * @regnum: register number to modify 987 * @mask: bit mask of bits to clear 988 * @set: bit mask of bits to set 989 * 990 * Read, modify, and if any change, write the register value back to the 991 * device. Any error returns a negative number. 992 * 993 * NOTE: MUST NOT be called from interrupt context. 994 */ 995 static int __mdiobus_c45_modify_changed(struct mii_bus *bus, int addr, 996 int devad, u32 regnum, u16 mask, 997 u16 set) 998 { 999 int new, ret; 1000 1001 ret = __mdiobus_c45_read(bus, addr, devad, regnum); 1002 if (ret < 0) 1003 return ret; 1004 1005 new = (ret & ~mask) | set; 1006 if (new == ret) 1007 return 0; 1008 1009 ret = __mdiobus_c45_write(bus, addr, devad, regnum, new); 1010 1011 return ret < 0 ? ret : 1; 1012 } 1013 1014 /** 1015 * mdiobus_read_nested - Nested version of the mdiobus_read function 1016 * @bus: the mii_bus struct 1017 * @addr: the phy address 1018 * @regnum: register number to read 1019 * 1020 * In case of nested MDIO bus access avoid lockdep false positives by 1021 * using mutex_lock_nested(). 1022 * 1023 * NOTE: MUST NOT be called from interrupt context, 1024 * because the bus read/write functions may wait for an interrupt 1025 * to conclude the operation. 1026 */ 1027 int mdiobus_read_nested(struct mii_bus *bus, int addr, u32 regnum) 1028 { 1029 int retval; 1030 1031 mutex_lock_nested(&bus->mdio_lock, MDIO_MUTEX_NESTED); 1032 retval = __mdiobus_read(bus, addr, regnum); 1033 mutex_unlock(&bus->mdio_lock); 1034 1035 return retval; 1036 } 1037 EXPORT_SYMBOL(mdiobus_read_nested); 1038 1039 /** 1040 * mdiobus_read - Convenience function for reading a given MII mgmt register 1041 * @bus: the mii_bus struct 1042 * @addr: the phy address 1043 * @regnum: register number to read 1044 * 1045 * NOTE: MUST NOT be called from interrupt context, 1046 * because the bus read/write functions may wait for an interrupt 1047 * to conclude the operation. 1048 */ 1049 int mdiobus_read(struct mii_bus *bus, int addr, u32 regnum) 1050 { 1051 int retval; 1052 1053 mutex_lock(&bus->mdio_lock); 1054 retval = __mdiobus_read(bus, addr, regnum); 1055 mutex_unlock(&bus->mdio_lock); 1056 1057 return retval; 1058 } 1059 EXPORT_SYMBOL(mdiobus_read); 1060 1061 /** 1062 * mdiobus_c45_read - Convenience function for reading a given MII mgmt register 1063 * @bus: the mii_bus struct 1064 * @addr: the phy address 1065 * @devad: device address to read 1066 * @regnum: register number to read 1067 * 1068 * NOTE: MUST NOT be called from interrupt context, 1069 * because the bus read/write functions may wait for an interrupt 1070 * to conclude the operation. 1071 */ 1072 int mdiobus_c45_read(struct mii_bus *bus, int addr, int devad, u32 regnum) 1073 { 1074 int retval; 1075 1076 mutex_lock(&bus->mdio_lock); 1077 retval = __mdiobus_c45_read(bus, addr, devad, regnum); 1078 mutex_unlock(&bus->mdio_lock); 1079 1080 return retval; 1081 } 1082 EXPORT_SYMBOL(mdiobus_c45_read); 1083 1084 /** 1085 * mdiobus_c45_read_nested - Nested version of the mdiobus_c45_read function 1086 * @bus: the mii_bus struct 1087 * @addr: the phy address 1088 * @devad: device address to read 1089 * @regnum: register number to read 1090 * 1091 * In case of nested MDIO bus access avoid lockdep false positives by 1092 * using mutex_lock_nested(). 1093 * 1094 * NOTE: MUST NOT be called from interrupt context, 1095 * because the bus read/write functions may wait for an interrupt 1096 * to conclude the operation. 1097 */ 1098 int mdiobus_c45_read_nested(struct mii_bus *bus, int addr, int devad, 1099 u32 regnum) 1100 { 1101 int retval; 1102 1103 mutex_lock_nested(&bus->mdio_lock, MDIO_MUTEX_NESTED); 1104 retval = __mdiobus_c45_read(bus, addr, devad, regnum); 1105 mutex_unlock(&bus->mdio_lock); 1106 1107 return retval; 1108 } 1109 EXPORT_SYMBOL(mdiobus_c45_read_nested); 1110 1111 /** 1112 * mdiobus_write_nested - Nested version of the mdiobus_write function 1113 * @bus: the mii_bus struct 1114 * @addr: the phy address 1115 * @regnum: register number to write 1116 * @val: value to write to @regnum 1117 * 1118 * In case of nested MDIO bus access avoid lockdep false positives by 1119 * using mutex_lock_nested(). 1120 * 1121 * NOTE: MUST NOT be called from interrupt context, 1122 * because the bus read/write functions may wait for an interrupt 1123 * to conclude the operation. 1124 */ 1125 int mdiobus_write_nested(struct mii_bus *bus, int addr, u32 regnum, u16 val) 1126 { 1127 int err; 1128 1129 mutex_lock_nested(&bus->mdio_lock, MDIO_MUTEX_NESTED); 1130 err = __mdiobus_write(bus, addr, regnum, val); 1131 mutex_unlock(&bus->mdio_lock); 1132 1133 return err; 1134 } 1135 EXPORT_SYMBOL(mdiobus_write_nested); 1136 1137 /** 1138 * mdiobus_write - Convenience function for writing a given MII mgmt register 1139 * @bus: the mii_bus struct 1140 * @addr: the phy address 1141 * @regnum: register number to write 1142 * @val: value to write to @regnum 1143 * 1144 * NOTE: MUST NOT be called from interrupt context, 1145 * because the bus read/write functions may wait for an interrupt 1146 * to conclude the operation. 1147 */ 1148 int mdiobus_write(struct mii_bus *bus, int addr, u32 regnum, u16 val) 1149 { 1150 int err; 1151 1152 mutex_lock(&bus->mdio_lock); 1153 err = __mdiobus_write(bus, addr, regnum, val); 1154 mutex_unlock(&bus->mdio_lock); 1155 1156 return err; 1157 } 1158 EXPORT_SYMBOL(mdiobus_write); 1159 1160 /** 1161 * mdiobus_c45_write - Convenience function for writing a given MII mgmt register 1162 * @bus: the mii_bus struct 1163 * @addr: the phy address 1164 * @devad: device address to read 1165 * @regnum: register number to write 1166 * @val: value to write to @regnum 1167 * 1168 * NOTE: MUST NOT be called from interrupt context, 1169 * because the bus read/write functions may wait for an interrupt 1170 * to conclude the operation. 1171 */ 1172 int mdiobus_c45_write(struct mii_bus *bus, int addr, int devad, u32 regnum, 1173 u16 val) 1174 { 1175 int err; 1176 1177 mutex_lock(&bus->mdio_lock); 1178 err = __mdiobus_c45_write(bus, addr, devad, regnum, val); 1179 mutex_unlock(&bus->mdio_lock); 1180 1181 return err; 1182 } 1183 EXPORT_SYMBOL(mdiobus_c45_write); 1184 1185 /** 1186 * mdiobus_c45_write_nested - Nested version of the mdiobus_c45_write function 1187 * @bus: the mii_bus struct 1188 * @addr: the phy address 1189 * @devad: device address to read 1190 * @regnum: register number to write 1191 * @val: value to write to @regnum 1192 * 1193 * In case of nested MDIO bus access avoid lockdep false positives by 1194 * using mutex_lock_nested(). 1195 * 1196 * NOTE: MUST NOT be called from interrupt context, 1197 * because the bus read/write functions may wait for an interrupt 1198 * to conclude the operation. 1199 */ 1200 int mdiobus_c45_write_nested(struct mii_bus *bus, int addr, int devad, 1201 u32 regnum, u16 val) 1202 { 1203 int err; 1204 1205 mutex_lock_nested(&bus->mdio_lock, MDIO_MUTEX_NESTED); 1206 err = __mdiobus_c45_write(bus, addr, devad, regnum, val); 1207 mutex_unlock(&bus->mdio_lock); 1208 1209 return err; 1210 } 1211 EXPORT_SYMBOL(mdiobus_c45_write_nested); 1212 1213 /** 1214 * mdiobus_modify - Convenience function for modifying a given mdio device 1215 * register 1216 * @bus: the mii_bus struct 1217 * @addr: the phy address 1218 * @regnum: register number to write 1219 * @mask: bit mask of bits to clear 1220 * @set: bit mask of bits to set 1221 */ 1222 int mdiobus_modify(struct mii_bus *bus, int addr, u32 regnum, u16 mask, u16 set) 1223 { 1224 int err; 1225 1226 mutex_lock(&bus->mdio_lock); 1227 err = __mdiobus_modify_changed(bus, addr, regnum, mask, set); 1228 mutex_unlock(&bus->mdio_lock); 1229 1230 return err < 0 ? err : 0; 1231 } 1232 EXPORT_SYMBOL_GPL(mdiobus_modify); 1233 1234 /** 1235 * mdiobus_c45_modify - Convenience function for modifying a given mdio device 1236 * register 1237 * @bus: the mii_bus struct 1238 * @addr: the phy address 1239 * @devad: device address to read 1240 * @regnum: register number to write 1241 * @mask: bit mask of bits to clear 1242 * @set: bit mask of bits to set 1243 */ 1244 int mdiobus_c45_modify(struct mii_bus *bus, int addr, int devad, u32 regnum, 1245 u16 mask, u16 set) 1246 { 1247 int err; 1248 1249 mutex_lock(&bus->mdio_lock); 1250 err = __mdiobus_c45_modify_changed(bus, addr, devad, regnum, 1251 mask, set); 1252 mutex_unlock(&bus->mdio_lock); 1253 1254 return err < 0 ? err : 0; 1255 } 1256 EXPORT_SYMBOL_GPL(mdiobus_c45_modify); 1257 1258 /** 1259 * mdiobus_modify_changed - Convenience function for modifying a given mdio 1260 * device register and returning if it changed 1261 * @bus: the mii_bus struct 1262 * @addr: the phy address 1263 * @regnum: register number to write 1264 * @mask: bit mask of bits to clear 1265 * @set: bit mask of bits to set 1266 */ 1267 int mdiobus_modify_changed(struct mii_bus *bus, int addr, u32 regnum, 1268 u16 mask, u16 set) 1269 { 1270 int err; 1271 1272 mutex_lock(&bus->mdio_lock); 1273 err = __mdiobus_modify_changed(bus, addr, regnum, mask, set); 1274 mutex_unlock(&bus->mdio_lock); 1275 1276 return err; 1277 } 1278 EXPORT_SYMBOL_GPL(mdiobus_modify_changed); 1279 1280 /** 1281 * mdiobus_c45_modify_changed - Convenience function for modifying a given mdio 1282 * device register and returning if it changed 1283 * @bus: the mii_bus struct 1284 * @addr: the phy address 1285 * @devad: device address to read 1286 * @regnum: register number to write 1287 * @mask: bit mask of bits to clear 1288 * @set: bit mask of bits to set 1289 */ 1290 int mdiobus_c45_modify_changed(struct mii_bus *bus, int devad, int addr, 1291 u32 regnum, u16 mask, u16 set) 1292 { 1293 int err; 1294 1295 mutex_lock(&bus->mdio_lock); 1296 err = __mdiobus_c45_modify_changed(bus, addr, devad, regnum, mask, set); 1297 mutex_unlock(&bus->mdio_lock); 1298 1299 return err; 1300 } 1301 EXPORT_SYMBOL_GPL(mdiobus_c45_modify_changed); 1302 1303 /** 1304 * mdio_bus_match - determine if given MDIO driver supports the given 1305 * MDIO device 1306 * @dev: target MDIO device 1307 * @drv: given MDIO driver 1308 * 1309 * Description: Given a MDIO device, and a MDIO driver, return 1 if 1310 * the driver supports the device. Otherwise, return 0. This may 1311 * require calling the devices own match function, since different classes 1312 * of MDIO devices have different match criteria. 1313 */ 1314 static int mdio_bus_match(struct device *dev, struct device_driver *drv) 1315 { 1316 struct mdio_driver *mdiodrv = to_mdio_driver(drv); 1317 struct mdio_device *mdio = to_mdio_device(dev); 1318 1319 /* Both the driver and device must type-match */ 1320 if (!(mdiodrv->mdiodrv.flags & MDIO_DEVICE_IS_PHY) != 1321 !(mdio->flags & MDIO_DEVICE_FLAG_PHY)) 1322 return 0; 1323 1324 if (of_driver_match_device(dev, drv)) 1325 return 1; 1326 1327 if (mdio->bus_match) 1328 return mdio->bus_match(dev, drv); 1329 1330 return 0; 1331 } 1332 1333 static int mdio_uevent(const struct device *dev, struct kobj_uevent_env *env) 1334 { 1335 int rc; 1336 1337 /* Some devices have extra OF data and an OF-style MODALIAS */ 1338 rc = of_device_uevent_modalias(dev, env); 1339 if (rc != -ENODEV) 1340 return rc; 1341 1342 return 0; 1343 } 1344 1345 static struct attribute *mdio_bus_device_statistics_attrs[] = { 1346 &dev_attr_mdio_bus_device_transfers.attr.attr, 1347 &dev_attr_mdio_bus_device_errors.attr.attr, 1348 &dev_attr_mdio_bus_device_writes.attr.attr, 1349 &dev_attr_mdio_bus_device_reads.attr.attr, 1350 NULL, 1351 }; 1352 1353 static const struct attribute_group mdio_bus_device_statistics_group = { 1354 .name = "statistics", 1355 .attrs = mdio_bus_device_statistics_attrs, 1356 }; 1357 1358 static const struct attribute_group *mdio_bus_dev_groups[] = { 1359 &mdio_bus_device_statistics_group, 1360 NULL, 1361 }; 1362 1363 struct bus_type mdio_bus_type = { 1364 .name = "mdio_bus", 1365 .dev_groups = mdio_bus_dev_groups, 1366 .match = mdio_bus_match, 1367 .uevent = mdio_uevent, 1368 }; 1369 EXPORT_SYMBOL(mdio_bus_type); 1370 1371 int __init mdio_bus_init(void) 1372 { 1373 int ret; 1374 1375 ret = class_register(&mdio_bus_class); 1376 if (!ret) { 1377 ret = bus_register(&mdio_bus_type); 1378 if (ret) 1379 class_unregister(&mdio_bus_class); 1380 } 1381 1382 return ret; 1383 } 1384 1385 #if IS_ENABLED(CONFIG_PHYLIB) 1386 void mdio_bus_exit(void) 1387 { 1388 class_unregister(&mdio_bus_class); 1389 bus_unregister(&mdio_bus_type); 1390 } 1391 EXPORT_SYMBOL_GPL(mdio_bus_exit); 1392 #else 1393 module_init(mdio_bus_init); 1394 /* no module_exit, intentional */ 1395 MODULE_LICENSE("GPL"); 1396 MODULE_DESCRIPTION("MDIO bus/device layer"); 1397 #endif 1398