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 struct mdio_device *mdiodev = bus->mdio_map[addr]; 113 114 if (!mdiodev) 115 return NULL; 116 117 if (!(mdiodev->flags & MDIO_DEVICE_FLAG_PHY)) 118 return NULL; 119 120 return container_of(mdiodev, struct phy_device, mdio); 121 } 122 EXPORT_SYMBOL(mdiobus_get_phy); 123 124 bool mdiobus_is_registered_device(struct mii_bus *bus, int addr) 125 { 126 return bus->mdio_map[addr]; 127 } 128 EXPORT_SYMBOL(mdiobus_is_registered_device); 129 130 /** 131 * mdiobus_alloc_size - allocate a mii_bus structure 132 * @size: extra amount of memory to allocate for private storage. 133 * If non-zero, then bus->priv is points to that memory. 134 * 135 * Description: called by a bus driver to allocate an mii_bus 136 * structure to fill in. 137 */ 138 struct mii_bus *mdiobus_alloc_size(size_t size) 139 { 140 struct mii_bus *bus; 141 size_t aligned_size = ALIGN(sizeof(*bus), NETDEV_ALIGN); 142 size_t alloc_size; 143 int i; 144 145 /* If we alloc extra space, it should be aligned */ 146 if (size) 147 alloc_size = aligned_size + size; 148 else 149 alloc_size = sizeof(*bus); 150 151 bus = kzalloc(alloc_size, GFP_KERNEL); 152 if (!bus) 153 return NULL; 154 155 bus->state = MDIOBUS_ALLOCATED; 156 if (size) 157 bus->priv = (void *)bus + aligned_size; 158 159 /* Initialise the interrupts to polling and 64-bit seqcounts */ 160 for (i = 0; i < PHY_MAX_ADDR; i++) { 161 bus->irq[i] = PHY_POLL; 162 u64_stats_init(&bus->stats[i].syncp); 163 } 164 165 return bus; 166 } 167 EXPORT_SYMBOL(mdiobus_alloc_size); 168 169 /** 170 * mdiobus_release - mii_bus device release callback 171 * @d: the target struct device that contains the mii_bus 172 * 173 * Description: called when the last reference to an mii_bus is 174 * dropped, to free the underlying memory. 175 */ 176 static void mdiobus_release(struct device *d) 177 { 178 struct mii_bus *bus = to_mii_bus(d); 179 180 WARN(bus->state != MDIOBUS_RELEASED && 181 /* for compatibility with error handling in drivers */ 182 bus->state != MDIOBUS_ALLOCATED, 183 "%s: not in RELEASED or ALLOCATED state\n", 184 bus->id); 185 kfree(bus); 186 } 187 188 struct mdio_bus_stat_attr { 189 int addr; 190 unsigned int field_offset; 191 }; 192 193 static u64 mdio_bus_get_stat(struct mdio_bus_stats *s, unsigned int offset) 194 { 195 const char *p = (const char *)s + offset; 196 unsigned int start; 197 u64 val = 0; 198 199 do { 200 start = u64_stats_fetch_begin(&s->syncp); 201 val = u64_stats_read((const u64_stats_t *)p); 202 } while (u64_stats_fetch_retry(&s->syncp, start)); 203 204 return val; 205 } 206 207 static u64 mdio_bus_get_global_stat(struct mii_bus *bus, unsigned int offset) 208 { 209 unsigned int i; 210 u64 val = 0; 211 212 for (i = 0; i < PHY_MAX_ADDR; i++) 213 val += mdio_bus_get_stat(&bus->stats[i], offset); 214 215 return val; 216 } 217 218 static ssize_t mdio_bus_stat_field_show(struct device *dev, 219 struct device_attribute *attr, 220 char *buf) 221 { 222 struct mii_bus *bus = to_mii_bus(dev); 223 struct mdio_bus_stat_attr *sattr; 224 struct dev_ext_attribute *eattr; 225 u64 val; 226 227 eattr = container_of(attr, struct dev_ext_attribute, attr); 228 sattr = eattr->var; 229 230 if (sattr->addr < 0) 231 val = mdio_bus_get_global_stat(bus, sattr->field_offset); 232 else 233 val = mdio_bus_get_stat(&bus->stats[sattr->addr], 234 sattr->field_offset); 235 236 return sysfs_emit(buf, "%llu\n", val); 237 } 238 239 static ssize_t mdio_bus_device_stat_field_show(struct device *dev, 240 struct device_attribute *attr, 241 char *buf) 242 { 243 struct mdio_device *mdiodev = to_mdio_device(dev); 244 struct mii_bus *bus = mdiodev->bus; 245 struct mdio_bus_stat_attr *sattr; 246 struct dev_ext_attribute *eattr; 247 int addr = mdiodev->addr; 248 u64 val; 249 250 eattr = container_of(attr, struct dev_ext_attribute, attr); 251 sattr = eattr->var; 252 253 val = mdio_bus_get_stat(&bus->stats[addr], sattr->field_offset); 254 255 return sysfs_emit(buf, "%llu\n", val); 256 } 257 258 #define MDIO_BUS_STATS_ATTR_DECL(field, file) \ 259 static struct dev_ext_attribute dev_attr_mdio_bus_##field = { \ 260 .attr = { .attr = { .name = file, .mode = 0444 }, \ 261 .show = mdio_bus_stat_field_show, \ 262 }, \ 263 .var = &((struct mdio_bus_stat_attr) { \ 264 -1, offsetof(struct mdio_bus_stats, field) \ 265 }), \ 266 }; \ 267 static struct dev_ext_attribute dev_attr_mdio_bus_device_##field = { \ 268 .attr = { .attr = { .name = file, .mode = 0444 }, \ 269 .show = mdio_bus_device_stat_field_show, \ 270 }, \ 271 .var = &((struct mdio_bus_stat_attr) { \ 272 -1, offsetof(struct mdio_bus_stats, field) \ 273 }), \ 274 }; 275 276 #define MDIO_BUS_STATS_ATTR(field) \ 277 MDIO_BUS_STATS_ATTR_DECL(field, __stringify(field)) 278 279 MDIO_BUS_STATS_ATTR(transfers); 280 MDIO_BUS_STATS_ATTR(errors); 281 MDIO_BUS_STATS_ATTR(writes); 282 MDIO_BUS_STATS_ATTR(reads); 283 284 #define MDIO_BUS_STATS_ADDR_ATTR_DECL(field, addr, file) \ 285 static struct dev_ext_attribute dev_attr_mdio_bus_addr_##field##_##addr = { \ 286 .attr = { .attr = { .name = file, .mode = 0444 }, \ 287 .show = mdio_bus_stat_field_show, \ 288 }, \ 289 .var = &((struct mdio_bus_stat_attr) { \ 290 addr, offsetof(struct mdio_bus_stats, field) \ 291 }), \ 292 } 293 294 #define MDIO_BUS_STATS_ADDR_ATTR(field, addr) \ 295 MDIO_BUS_STATS_ADDR_ATTR_DECL(field, addr, \ 296 __stringify(field) "_" __stringify(addr)) 297 298 #define MDIO_BUS_STATS_ADDR_ATTR_GROUP_DECL(addr) \ 299 MDIO_BUS_STATS_ADDR_ATTR(transfers, addr); \ 300 MDIO_BUS_STATS_ADDR_ATTR(errors, addr); \ 301 MDIO_BUS_STATS_ADDR_ATTR(writes, addr); \ 302 MDIO_BUS_STATS_ADDR_ATTR(reads, addr) \ 303 304 MDIO_BUS_STATS_ADDR_ATTR_GROUP_DECL(0); 305 MDIO_BUS_STATS_ADDR_ATTR_GROUP_DECL(1); 306 MDIO_BUS_STATS_ADDR_ATTR_GROUP_DECL(2); 307 MDIO_BUS_STATS_ADDR_ATTR_GROUP_DECL(3); 308 MDIO_BUS_STATS_ADDR_ATTR_GROUP_DECL(4); 309 MDIO_BUS_STATS_ADDR_ATTR_GROUP_DECL(5); 310 MDIO_BUS_STATS_ADDR_ATTR_GROUP_DECL(6); 311 MDIO_BUS_STATS_ADDR_ATTR_GROUP_DECL(7); 312 MDIO_BUS_STATS_ADDR_ATTR_GROUP_DECL(8); 313 MDIO_BUS_STATS_ADDR_ATTR_GROUP_DECL(9); 314 MDIO_BUS_STATS_ADDR_ATTR_GROUP_DECL(10); 315 MDIO_BUS_STATS_ADDR_ATTR_GROUP_DECL(11); 316 MDIO_BUS_STATS_ADDR_ATTR_GROUP_DECL(12); 317 MDIO_BUS_STATS_ADDR_ATTR_GROUP_DECL(13); 318 MDIO_BUS_STATS_ADDR_ATTR_GROUP_DECL(14); 319 MDIO_BUS_STATS_ADDR_ATTR_GROUP_DECL(15); 320 MDIO_BUS_STATS_ADDR_ATTR_GROUP_DECL(16); 321 MDIO_BUS_STATS_ADDR_ATTR_GROUP_DECL(17); 322 MDIO_BUS_STATS_ADDR_ATTR_GROUP_DECL(18); 323 MDIO_BUS_STATS_ADDR_ATTR_GROUP_DECL(19); 324 MDIO_BUS_STATS_ADDR_ATTR_GROUP_DECL(20); 325 MDIO_BUS_STATS_ADDR_ATTR_GROUP_DECL(21); 326 MDIO_BUS_STATS_ADDR_ATTR_GROUP_DECL(22); 327 MDIO_BUS_STATS_ADDR_ATTR_GROUP_DECL(23); 328 MDIO_BUS_STATS_ADDR_ATTR_GROUP_DECL(24); 329 MDIO_BUS_STATS_ADDR_ATTR_GROUP_DECL(25); 330 MDIO_BUS_STATS_ADDR_ATTR_GROUP_DECL(26); 331 MDIO_BUS_STATS_ADDR_ATTR_GROUP_DECL(27); 332 MDIO_BUS_STATS_ADDR_ATTR_GROUP_DECL(28); 333 MDIO_BUS_STATS_ADDR_ATTR_GROUP_DECL(29); 334 MDIO_BUS_STATS_ADDR_ATTR_GROUP_DECL(30); 335 MDIO_BUS_STATS_ADDR_ATTR_GROUP_DECL(31); 336 337 #define MDIO_BUS_STATS_ADDR_ATTR_GROUP(addr) \ 338 &dev_attr_mdio_bus_addr_transfers_##addr.attr.attr, \ 339 &dev_attr_mdio_bus_addr_errors_##addr.attr.attr, \ 340 &dev_attr_mdio_bus_addr_writes_##addr.attr.attr, \ 341 &dev_attr_mdio_bus_addr_reads_##addr.attr.attr \ 342 343 static struct attribute *mdio_bus_statistics_attrs[] = { 344 &dev_attr_mdio_bus_transfers.attr.attr, 345 &dev_attr_mdio_bus_errors.attr.attr, 346 &dev_attr_mdio_bus_writes.attr.attr, 347 &dev_attr_mdio_bus_reads.attr.attr, 348 MDIO_BUS_STATS_ADDR_ATTR_GROUP(0), 349 MDIO_BUS_STATS_ADDR_ATTR_GROUP(1), 350 MDIO_BUS_STATS_ADDR_ATTR_GROUP(2), 351 MDIO_BUS_STATS_ADDR_ATTR_GROUP(3), 352 MDIO_BUS_STATS_ADDR_ATTR_GROUP(4), 353 MDIO_BUS_STATS_ADDR_ATTR_GROUP(5), 354 MDIO_BUS_STATS_ADDR_ATTR_GROUP(6), 355 MDIO_BUS_STATS_ADDR_ATTR_GROUP(7), 356 MDIO_BUS_STATS_ADDR_ATTR_GROUP(8), 357 MDIO_BUS_STATS_ADDR_ATTR_GROUP(9), 358 MDIO_BUS_STATS_ADDR_ATTR_GROUP(10), 359 MDIO_BUS_STATS_ADDR_ATTR_GROUP(11), 360 MDIO_BUS_STATS_ADDR_ATTR_GROUP(12), 361 MDIO_BUS_STATS_ADDR_ATTR_GROUP(13), 362 MDIO_BUS_STATS_ADDR_ATTR_GROUP(14), 363 MDIO_BUS_STATS_ADDR_ATTR_GROUP(15), 364 MDIO_BUS_STATS_ADDR_ATTR_GROUP(16), 365 MDIO_BUS_STATS_ADDR_ATTR_GROUP(17), 366 MDIO_BUS_STATS_ADDR_ATTR_GROUP(18), 367 MDIO_BUS_STATS_ADDR_ATTR_GROUP(19), 368 MDIO_BUS_STATS_ADDR_ATTR_GROUP(20), 369 MDIO_BUS_STATS_ADDR_ATTR_GROUP(21), 370 MDIO_BUS_STATS_ADDR_ATTR_GROUP(22), 371 MDIO_BUS_STATS_ADDR_ATTR_GROUP(23), 372 MDIO_BUS_STATS_ADDR_ATTR_GROUP(24), 373 MDIO_BUS_STATS_ADDR_ATTR_GROUP(25), 374 MDIO_BUS_STATS_ADDR_ATTR_GROUP(26), 375 MDIO_BUS_STATS_ADDR_ATTR_GROUP(27), 376 MDIO_BUS_STATS_ADDR_ATTR_GROUP(28), 377 MDIO_BUS_STATS_ADDR_ATTR_GROUP(29), 378 MDIO_BUS_STATS_ADDR_ATTR_GROUP(30), 379 MDIO_BUS_STATS_ADDR_ATTR_GROUP(31), 380 NULL, 381 }; 382 383 static const struct attribute_group mdio_bus_statistics_group = { 384 .name = "statistics", 385 .attrs = mdio_bus_statistics_attrs, 386 }; 387 388 static const struct attribute_group *mdio_bus_groups[] = { 389 &mdio_bus_statistics_group, 390 NULL, 391 }; 392 393 static struct class mdio_bus_class = { 394 .name = "mdio_bus", 395 .dev_release = mdiobus_release, 396 .dev_groups = mdio_bus_groups, 397 }; 398 399 /** 400 * mdio_find_bus - Given the name of a mdiobus, find the mii_bus. 401 * @mdio_name: The name of a mdiobus. 402 * 403 * Returns a reference to the mii_bus, or NULL if none found. The 404 * embedded struct device will have its reference count incremented, 405 * and this must be put_deviced'ed once the bus is finished with. 406 */ 407 struct mii_bus *mdio_find_bus(const char *mdio_name) 408 { 409 struct device *d; 410 411 d = class_find_device_by_name(&mdio_bus_class, mdio_name); 412 return d ? to_mii_bus(d) : NULL; 413 } 414 EXPORT_SYMBOL(mdio_find_bus); 415 416 #if IS_ENABLED(CONFIG_OF_MDIO) 417 /** 418 * of_mdio_find_bus - Given an mii_bus node, find the mii_bus. 419 * @mdio_bus_np: Pointer to the mii_bus. 420 * 421 * Returns a reference to the mii_bus, or NULL if none found. The 422 * embedded struct device will have its reference count incremented, 423 * and this must be put once the bus is finished with. 424 * 425 * Because the association of a device_node and mii_bus is made via 426 * of_mdiobus_register(), the mii_bus cannot be found before it is 427 * registered with of_mdiobus_register(). 428 * 429 */ 430 struct mii_bus *of_mdio_find_bus(struct device_node *mdio_bus_np) 431 { 432 struct device *d; 433 434 if (!mdio_bus_np) 435 return NULL; 436 437 d = class_find_device_by_of_node(&mdio_bus_class, mdio_bus_np); 438 return d ? to_mii_bus(d) : NULL; 439 } 440 EXPORT_SYMBOL(of_mdio_find_bus); 441 442 /* Walk the list of subnodes of a mdio bus and look for a node that 443 * matches the mdio device's address with its 'reg' property. If 444 * found, set the of_node pointer for the mdio device. This allows 445 * auto-probed phy devices to be supplied with information passed in 446 * via DT. 447 */ 448 static void of_mdiobus_link_mdiodev(struct mii_bus *bus, 449 struct mdio_device *mdiodev) 450 { 451 struct device *dev = &mdiodev->dev; 452 struct device_node *child; 453 454 if (dev->of_node || !bus->dev.of_node) 455 return; 456 457 for_each_available_child_of_node(bus->dev.of_node, child) { 458 int addr; 459 460 addr = of_mdio_parse_addr(dev, child); 461 if (addr < 0) 462 continue; 463 464 if (addr == mdiodev->addr) { 465 device_set_node(dev, of_fwnode_handle(child)); 466 /* The refcount on "child" is passed to the mdio 467 * device. Do _not_ use of_node_put(child) here. 468 */ 469 return; 470 } 471 } 472 } 473 #else /* !IS_ENABLED(CONFIG_OF_MDIO) */ 474 static inline void of_mdiobus_link_mdiodev(struct mii_bus *mdio, 475 struct mdio_device *mdiodev) 476 { 477 } 478 #endif 479 480 /** 481 * mdiobus_create_device - create a full MDIO device given 482 * a mdio_board_info structure 483 * @bus: MDIO bus to create the devices on 484 * @bi: mdio_board_info structure describing the devices 485 * 486 * Returns 0 on success or < 0 on error. 487 */ 488 static int mdiobus_create_device(struct mii_bus *bus, 489 struct mdio_board_info *bi) 490 { 491 struct mdio_device *mdiodev; 492 int ret = 0; 493 494 mdiodev = mdio_device_create(bus, bi->mdio_addr); 495 if (IS_ERR(mdiodev)) 496 return -ENODEV; 497 498 strncpy(mdiodev->modalias, bi->modalias, 499 sizeof(mdiodev->modalias)); 500 mdiodev->bus_match = mdio_device_bus_match; 501 mdiodev->dev.platform_data = (void *)bi->platform_data; 502 503 ret = mdio_device_register(mdiodev); 504 if (ret) 505 mdio_device_free(mdiodev); 506 507 return ret; 508 } 509 510 static struct phy_device *mdiobus_scan(struct mii_bus *bus, int addr, bool c45) 511 { 512 struct phy_device *phydev = ERR_PTR(-ENODEV); 513 int err; 514 515 phydev = get_phy_device(bus, addr, c45); 516 if (IS_ERR(phydev)) 517 return phydev; 518 519 /* For DT, see if the auto-probed phy has a corresponding child 520 * in the bus node, and set the of_node pointer in this case. 521 */ 522 of_mdiobus_link_mdiodev(bus, &phydev->mdio); 523 524 err = phy_device_register(phydev); 525 if (err) { 526 phy_device_free(phydev); 527 return ERR_PTR(-ENODEV); 528 } 529 530 return phydev; 531 } 532 533 /** 534 * mdiobus_scan_c22 - scan one address on a bus for C22 MDIO devices. 535 * @bus: mii_bus to scan 536 * @addr: address on bus to scan 537 * 538 * This function scans one address on the MDIO bus, looking for 539 * devices which can be identified using a vendor/product ID in 540 * registers 2 and 3. Not all MDIO devices have such registers, but 541 * PHY devices typically do. Hence this function assumes anything 542 * found is a PHY, or can be treated as a PHY. Other MDIO devices, 543 * such as switches, will probably not be found during the scan. 544 */ 545 struct phy_device *mdiobus_scan_c22(struct mii_bus *bus, int addr) 546 { 547 return mdiobus_scan(bus, addr, false); 548 } 549 EXPORT_SYMBOL(mdiobus_scan_c22); 550 551 /** 552 * mdiobus_scan_c45 - scan one address on a bus for C45 MDIO devices. 553 * @bus: mii_bus to scan 554 * @addr: address on bus to scan 555 * 556 * This function scans one address on the MDIO bus, looking for 557 * devices which can be identified using a vendor/product ID in 558 * registers 2 and 3. Not all MDIO devices have such registers, but 559 * PHY devices typically do. Hence this function assumes anything 560 * found is a PHY, or can be treated as a PHY. Other MDIO devices, 561 * such as switches, will probably not be found during the scan. 562 */ 563 static struct phy_device *mdiobus_scan_c45(struct mii_bus *bus, int addr) 564 { 565 return mdiobus_scan(bus, addr, true); 566 } 567 568 static int mdiobus_scan_bus_c22(struct mii_bus *bus) 569 { 570 int i; 571 572 for (i = 0; i < PHY_MAX_ADDR; i++) { 573 if ((bus->phy_mask & BIT(i)) == 0) { 574 struct phy_device *phydev; 575 576 phydev = mdiobus_scan_c22(bus, i); 577 if (IS_ERR(phydev) && (PTR_ERR(phydev) != -ENODEV)) 578 return PTR_ERR(phydev); 579 } 580 } 581 return 0; 582 } 583 584 static int mdiobus_scan_bus_c45(struct mii_bus *bus) 585 { 586 int i; 587 588 for (i = 0; i < PHY_MAX_ADDR; i++) { 589 if ((bus->phy_mask & BIT(i)) == 0) { 590 struct phy_device *phydev; 591 592 /* Don't scan C45 if we already have a C22 device */ 593 if (bus->mdio_map[i]) 594 continue; 595 596 phydev = mdiobus_scan_c45(bus, i); 597 if (IS_ERR(phydev) && (PTR_ERR(phydev) != -ENODEV)) 598 return PTR_ERR(phydev); 599 } 600 } 601 return 0; 602 } 603 604 /* There are some C22 PHYs which do bad things when where is a C45 605 * transaction on the bus, like accepting a read themselves, and 606 * stomping over the true devices reply, to performing a write to 607 * themselves which was intended for another device. Now that C22 608 * devices have been found, see if any of them are bad for C45, and if we 609 * should skip the C45 scan. 610 */ 611 static bool mdiobus_prevent_c45_scan(struct mii_bus *bus) 612 { 613 int i; 614 615 for (i = 0; i < PHY_MAX_ADDR; i++) { 616 struct phy_device *phydev; 617 u32 oui; 618 619 phydev = mdiobus_get_phy(bus, i); 620 if (!phydev) 621 continue; 622 oui = phydev->phy_id >> 10; 623 624 if (oui == MICREL_OUI) 625 return true; 626 } 627 return false; 628 } 629 630 /** 631 * __mdiobus_register - bring up all the PHYs on a given bus and attach them to bus 632 * @bus: target mii_bus 633 * @owner: module containing bus accessor functions 634 * 635 * Description: Called by a bus driver to bring up all the PHYs 636 * on a given bus, and attach them to the bus. Drivers should use 637 * mdiobus_register() rather than __mdiobus_register() unless they 638 * need to pass a specific owner module. MDIO devices which are not 639 * PHYs will not be brought up by this function. They are expected 640 * to be explicitly listed in DT and instantiated by of_mdiobus_register(). 641 * 642 * Returns 0 on success or < 0 on error. 643 */ 644 int __mdiobus_register(struct mii_bus *bus, struct module *owner) 645 { 646 struct mdio_device *mdiodev; 647 struct gpio_desc *gpiod; 648 bool prevent_c45_scan; 649 int i, err; 650 651 if (!bus || !bus->name) 652 return -EINVAL; 653 654 /* An access method always needs both read and write operations */ 655 if (!!bus->read != !!bus->write || !!bus->read_c45 != !!bus->write_c45) 656 return -EINVAL; 657 658 /* At least one method is mandatory */ 659 if (!bus->read && !bus->read_c45) 660 return -EINVAL; 661 662 if (bus->parent && bus->parent->of_node) 663 bus->parent->of_node->fwnode.flags |= 664 FWNODE_FLAG_NEEDS_CHILD_BOUND_ON_ADD; 665 666 WARN(bus->state != MDIOBUS_ALLOCATED && 667 bus->state != MDIOBUS_UNREGISTERED, 668 "%s: not in ALLOCATED or UNREGISTERED state\n", bus->id); 669 670 bus->owner = owner; 671 bus->dev.parent = bus->parent; 672 bus->dev.class = &mdio_bus_class; 673 bus->dev.groups = NULL; 674 dev_set_name(&bus->dev, "%s", bus->id); 675 676 /* We need to set state to MDIOBUS_UNREGISTERED to correctly release 677 * the device in mdiobus_free() 678 * 679 * State will be updated later in this function in case of success 680 */ 681 bus->state = MDIOBUS_UNREGISTERED; 682 683 err = device_register(&bus->dev); 684 if (err) { 685 pr_err("mii_bus %s failed to register\n", bus->id); 686 return -EINVAL; 687 } 688 689 mutex_init(&bus->mdio_lock); 690 mutex_init(&bus->shared_lock); 691 692 /* assert bus level PHY GPIO reset */ 693 gpiod = devm_gpiod_get_optional(&bus->dev, "reset", GPIOD_OUT_HIGH); 694 if (IS_ERR(gpiod)) { 695 err = dev_err_probe(&bus->dev, PTR_ERR(gpiod), 696 "mii_bus %s couldn't get reset GPIO\n", 697 bus->id); 698 device_del(&bus->dev); 699 return err; 700 } else if (gpiod) { 701 bus->reset_gpiod = gpiod; 702 fsleep(bus->reset_delay_us); 703 gpiod_set_value_cansleep(gpiod, 0); 704 if (bus->reset_post_delay_us > 0) 705 fsleep(bus->reset_post_delay_us); 706 } 707 708 if (bus->reset) { 709 err = bus->reset(bus); 710 if (err) 711 goto error_reset_gpiod; 712 } 713 714 if (bus->read) { 715 err = mdiobus_scan_bus_c22(bus); 716 if (err) 717 goto error; 718 } 719 720 prevent_c45_scan = mdiobus_prevent_c45_scan(bus); 721 722 if (!prevent_c45_scan && bus->read_c45) { 723 err = mdiobus_scan_bus_c45(bus); 724 if (err) 725 goto error; 726 } 727 728 mdiobus_setup_mdiodev_from_board_info(bus, mdiobus_create_device); 729 730 bus->state = MDIOBUS_REGISTERED; 731 dev_dbg(&bus->dev, "probed\n"); 732 return 0; 733 734 error: 735 for (i = 0; i < PHY_MAX_ADDR; i++) { 736 mdiodev = bus->mdio_map[i]; 737 if (!mdiodev) 738 continue; 739 740 mdiodev->device_remove(mdiodev); 741 mdiodev->device_free(mdiodev); 742 } 743 error_reset_gpiod: 744 /* Put PHYs in RESET to save power */ 745 if (bus->reset_gpiod) 746 gpiod_set_value_cansleep(bus->reset_gpiod, 1); 747 748 device_del(&bus->dev); 749 return err; 750 } 751 EXPORT_SYMBOL(__mdiobus_register); 752 753 void mdiobus_unregister(struct mii_bus *bus) 754 { 755 struct mdio_device *mdiodev; 756 int i; 757 758 if (WARN_ON_ONCE(bus->state != MDIOBUS_REGISTERED)) 759 return; 760 bus->state = MDIOBUS_UNREGISTERED; 761 762 for (i = 0; i < PHY_MAX_ADDR; i++) { 763 mdiodev = bus->mdio_map[i]; 764 if (!mdiodev) 765 continue; 766 767 if (mdiodev->reset_gpio) 768 gpiod_put(mdiodev->reset_gpio); 769 770 mdiodev->device_remove(mdiodev); 771 mdiodev->device_free(mdiodev); 772 } 773 774 /* Put PHYs in RESET to save power */ 775 if (bus->reset_gpiod) 776 gpiod_set_value_cansleep(bus->reset_gpiod, 1); 777 778 device_del(&bus->dev); 779 } 780 EXPORT_SYMBOL(mdiobus_unregister); 781 782 /** 783 * mdiobus_free - free a struct mii_bus 784 * @bus: mii_bus to free 785 * 786 * This function releases the reference to the underlying device 787 * object in the mii_bus. If this is the last reference, the mii_bus 788 * will be freed. 789 */ 790 void mdiobus_free(struct mii_bus *bus) 791 { 792 /* For compatibility with error handling in drivers. */ 793 if (bus->state == MDIOBUS_ALLOCATED) { 794 kfree(bus); 795 return; 796 } 797 798 WARN(bus->state != MDIOBUS_UNREGISTERED, 799 "%s: not in UNREGISTERED state\n", bus->id); 800 bus->state = MDIOBUS_RELEASED; 801 802 put_device(&bus->dev); 803 } 804 EXPORT_SYMBOL(mdiobus_free); 805 806 static void mdiobus_stats_acct(struct mdio_bus_stats *stats, bool op, int ret) 807 { 808 preempt_disable(); 809 u64_stats_update_begin(&stats->syncp); 810 811 u64_stats_inc(&stats->transfers); 812 if (ret < 0) { 813 u64_stats_inc(&stats->errors); 814 goto out; 815 } 816 817 if (op) 818 u64_stats_inc(&stats->reads); 819 else 820 u64_stats_inc(&stats->writes); 821 out: 822 u64_stats_update_end(&stats->syncp); 823 preempt_enable(); 824 } 825 826 /** 827 * __mdiobus_read - Unlocked version of the mdiobus_read function 828 * @bus: the mii_bus struct 829 * @addr: the phy address 830 * @regnum: register number to read 831 * 832 * Read a MDIO bus register. Caller must hold the mdio bus lock. 833 * 834 * NOTE: MUST NOT be called from interrupt context. 835 */ 836 int __mdiobus_read(struct mii_bus *bus, int addr, u32 regnum) 837 { 838 int retval; 839 840 lockdep_assert_held_once(&bus->mdio_lock); 841 842 if (bus->read) 843 retval = bus->read(bus, addr, regnum); 844 else 845 retval = -EOPNOTSUPP; 846 847 trace_mdio_access(bus, 1, addr, regnum, retval, retval); 848 mdiobus_stats_acct(&bus->stats[addr], true, retval); 849 850 return retval; 851 } 852 EXPORT_SYMBOL(__mdiobus_read); 853 854 /** 855 * __mdiobus_write - Unlocked version of the mdiobus_write function 856 * @bus: the mii_bus struct 857 * @addr: the phy address 858 * @regnum: register number to write 859 * @val: value to write to @regnum 860 * 861 * Write a MDIO bus register. Caller must hold the mdio bus lock. 862 * 863 * NOTE: MUST NOT be called from interrupt context. 864 */ 865 int __mdiobus_write(struct mii_bus *bus, int addr, u32 regnum, u16 val) 866 { 867 int err; 868 869 lockdep_assert_held_once(&bus->mdio_lock); 870 871 if (bus->write) 872 err = bus->write(bus, addr, regnum, val); 873 else 874 err = -EOPNOTSUPP; 875 876 trace_mdio_access(bus, 0, addr, regnum, val, err); 877 mdiobus_stats_acct(&bus->stats[addr], false, err); 878 879 return err; 880 } 881 EXPORT_SYMBOL(__mdiobus_write); 882 883 /** 884 * __mdiobus_modify_changed - Unlocked version of the mdiobus_modify function 885 * @bus: the mii_bus struct 886 * @addr: the phy address 887 * @regnum: register number to modify 888 * @mask: bit mask of bits to clear 889 * @set: bit mask of bits to set 890 * 891 * Read, modify, and if any change, write the register value back to the 892 * device. Any error returns a negative number. 893 * 894 * NOTE: MUST NOT be called from interrupt context. 895 */ 896 int __mdiobus_modify_changed(struct mii_bus *bus, int addr, u32 regnum, 897 u16 mask, u16 set) 898 { 899 int new, ret; 900 901 ret = __mdiobus_read(bus, addr, regnum); 902 if (ret < 0) 903 return ret; 904 905 new = (ret & ~mask) | set; 906 if (new == ret) 907 return 0; 908 909 ret = __mdiobus_write(bus, addr, regnum, new); 910 911 return ret < 0 ? ret : 1; 912 } 913 EXPORT_SYMBOL_GPL(__mdiobus_modify_changed); 914 915 static u32 mdiobus_c45_addr(int devad, u16 regnum) 916 { 917 return MII_ADDR_C45 | devad << MII_DEVADDR_C45_SHIFT | regnum; 918 } 919 920 /** 921 * __mdiobus_c45_read - Unlocked version of the mdiobus_c45_read function 922 * @bus: the mii_bus struct 923 * @addr: the phy address 924 * @devad: device address to read 925 * @regnum: register number to read 926 * 927 * Read a MDIO bus register. Caller must hold the mdio bus lock. 928 * 929 * NOTE: MUST NOT be called from interrupt context. 930 */ 931 int __mdiobus_c45_read(struct mii_bus *bus, int addr, int devad, u32 regnum) 932 { 933 int retval; 934 935 lockdep_assert_held_once(&bus->mdio_lock); 936 937 if (bus->read_c45) 938 retval = bus->read_c45(bus, addr, devad, regnum); 939 else 940 retval = bus->read(bus, addr, mdiobus_c45_addr(devad, regnum)); 941 942 trace_mdio_access(bus, 1, addr, regnum, retval, retval); 943 mdiobus_stats_acct(&bus->stats[addr], true, retval); 944 945 return retval; 946 } 947 EXPORT_SYMBOL(__mdiobus_c45_read); 948 949 /** 950 * __mdiobus_c45_write - Unlocked version of the mdiobus_write function 951 * @bus: the mii_bus struct 952 * @addr: the phy address 953 * @devad: device address to read 954 * @regnum: register number to write 955 * @val: value to write to @regnum 956 * 957 * Write a MDIO bus register. Caller must hold the mdio bus lock. 958 * 959 * NOTE: MUST NOT be called from interrupt context. 960 */ 961 int __mdiobus_c45_write(struct mii_bus *bus, int addr, int devad, u32 regnum, 962 u16 val) 963 { 964 int err; 965 966 lockdep_assert_held_once(&bus->mdio_lock); 967 968 if (bus->write_c45) 969 err = bus->write_c45(bus, addr, devad, regnum, val); 970 else 971 err = bus->write(bus, addr, mdiobus_c45_addr(devad, regnum), 972 val); 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(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