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