Lines Matching +full:bus +full:- +full:addr

1 // SPDX-License-Identifier: GPL-2.0+
2 /* MDIO Bus interface
41 #include "mdio-boardinfo.h"
46 mdiodev->reset_gpio = gpiod_get_optional(&mdiodev->dev, in mdiobus_register_gpiod()
48 if (IS_ERR(mdiodev->reset_gpio)) in mdiobus_register_gpiod()
49 return PTR_ERR(mdiodev->reset_gpio); in mdiobus_register_gpiod()
51 if (mdiodev->reset_gpio) in mdiobus_register_gpiod()
52 gpiod_set_consumer_name(mdiodev->reset_gpio, "PHY reset"); in mdiobus_register_gpiod()
61 reset = reset_control_get_optional_exclusive(&mdiodev->dev, "phy"); in mdiobus_register_reset()
65 mdiodev->reset_ctrl = reset; in mdiobus_register_reset()
74 if (mdiodev->bus->mdio_map[mdiodev->addr]) in mdiobus_register_device()
75 return -EBUSY; in mdiobus_register_device()
77 if (mdiodev->flags & MDIO_DEVICE_FLAG_PHY) { in mdiobus_register_device()
90 mdiodev->bus->mdio_map[mdiodev->addr] = mdiodev; in mdiobus_register_device()
98 if (mdiodev->bus->mdio_map[mdiodev->addr] != mdiodev) in mdiobus_unregister_device()
99 return -EINVAL; in mdiobus_unregister_device()
101 reset_control_put(mdiodev->reset_ctrl); in mdiobus_unregister_device()
103 mdiodev->bus->mdio_map[mdiodev->addr] = NULL; in mdiobus_unregister_device()
109 struct phy_device *mdiobus_get_phy(struct mii_bus *bus, int addr) in mdiobus_get_phy() argument
111 struct mdio_device *mdiodev = bus->mdio_map[addr]; in mdiobus_get_phy()
116 if (!(mdiodev->flags & MDIO_DEVICE_FLAG_PHY)) in mdiobus_get_phy()
123 bool mdiobus_is_registered_device(struct mii_bus *bus, int addr) in mdiobus_is_registered_device() argument
125 return bus->mdio_map[addr]; in mdiobus_is_registered_device()
130 * mdiobus_alloc_size - allocate a mii_bus structure
132 * If non-zero, then bus->priv is points to that memory.
134 * Description: called by a bus driver to allocate an mii_bus
139 struct mii_bus *bus; in mdiobus_alloc_size() local
140 size_t aligned_size = ALIGN(sizeof(*bus), NETDEV_ALIGN); in mdiobus_alloc_size()
148 alloc_size = sizeof(*bus); in mdiobus_alloc_size()
150 bus = kzalloc(alloc_size, GFP_KERNEL); in mdiobus_alloc_size()
151 if (!bus) in mdiobus_alloc_size()
154 bus->state = MDIOBUS_ALLOCATED; in mdiobus_alloc_size()
156 bus->priv = (void *)bus + aligned_size; in mdiobus_alloc_size()
158 /* Initialise the interrupts to polling and 64-bit seqcounts */ in mdiobus_alloc_size()
160 bus->irq[i] = PHY_POLL; in mdiobus_alloc_size()
161 u64_stats_init(&bus->stats[i].syncp); in mdiobus_alloc_size()
164 return bus; in mdiobus_alloc_size()
169 * mdiobus_release - mii_bus device release callback
177 struct mii_bus *bus = to_mii_bus(d); in mdiobus_release() local
178 BUG_ON(bus->state != MDIOBUS_RELEASED && in mdiobus_release()
180 bus->state != MDIOBUS_ALLOCATED); in mdiobus_release()
181 kfree(bus); in mdiobus_release()
185 int addr; member
196 start = u64_stats_fetch_begin(&s->syncp); in mdio_bus_get_stat()
198 } while (u64_stats_fetch_retry(&s->syncp, start)); in mdio_bus_get_stat()
203 static u64 mdio_bus_get_global_stat(struct mii_bus *bus, unsigned int offset) in mdio_bus_get_global_stat() argument
209 val += mdio_bus_get_stat(&bus->stats[i], offset); in mdio_bus_get_global_stat()
218 struct mii_bus *bus = to_mii_bus(dev); in mdio_bus_stat_field_show() local
224 sattr = eattr->var; in mdio_bus_stat_field_show()
226 if (sattr->addr < 0) in mdio_bus_stat_field_show()
227 val = mdio_bus_get_global_stat(bus, sattr->field_offset); in mdio_bus_stat_field_show()
229 val = mdio_bus_get_stat(&bus->stats[sattr->addr], in mdio_bus_stat_field_show()
230 sattr->field_offset); in mdio_bus_stat_field_show()
240 struct mii_bus *bus = mdiodev->bus; in mdio_bus_device_stat_field_show() local
243 int addr = mdiodev->addr; in mdio_bus_device_stat_field_show() local
247 sattr = eattr->var; in mdio_bus_device_stat_field_show()
249 val = mdio_bus_get_stat(&bus->stats[addr], sattr->field_offset); in mdio_bus_device_stat_field_show()
260 -1, offsetof(struct mdio_bus_stats, field) \
268 -1, offsetof(struct mdio_bus_stats, field) \
280 #define MDIO_BUS_STATS_ADDR_ATTR_DECL(field, addr, file) \ argument
281 static struct dev_ext_attribute dev_attr_mdio_bus_addr_##field##_##addr = { \
286 addr, offsetof(struct mdio_bus_stats, field) \
290 #define MDIO_BUS_STATS_ADDR_ATTR(field, addr) \ argument
291 MDIO_BUS_STATS_ADDR_ATTR_DECL(field, addr, \
292 __stringify(field) "_" __stringify(addr))
294 #define MDIO_BUS_STATS_ADDR_ATTR_GROUP_DECL(addr) \ argument
295 MDIO_BUS_STATS_ADDR_ATTR(transfers, addr); \
296 MDIO_BUS_STATS_ADDR_ATTR(errors, addr); \
297 MDIO_BUS_STATS_ADDR_ATTR(writes, addr); \
298 MDIO_BUS_STATS_ADDR_ATTR(reads, addr) \
333 #define MDIO_BUS_STATS_ADDR_ATTR_GROUP(addr) \ argument
334 &dev_attr_mdio_bus_addr_transfers_##addr.attr.attr, \
335 &dev_attr_mdio_bus_addr_errors_##addr.attr.attr, \
336 &dev_attr_mdio_bus_addr_writes_##addr.attr.attr, \
337 &dev_attr_mdio_bus_addr_reads_##addr.attr.attr \
396 * mdio_find_bus - Given the name of a mdiobus, find the mii_bus.
401 * and this must be put_deviced'ed once the bus is finished with.
414 * of_mdio_find_bus - Given an mii_bus node, find the mii_bus.
419 * and this must be put once the bus is finished with.
438 /* Walk the list of subnodes of a mdio bus and look for a node that
441 * auto-probed phy devices to be supplied with information passed in
444 static void of_mdiobus_link_mdiodev(struct mii_bus *bus, in of_mdiobus_link_mdiodev() argument
447 struct device *dev = &mdiodev->dev; in of_mdiobus_link_mdiodev()
450 if (dev->of_node || !bus->dev.of_node) in of_mdiobus_link_mdiodev()
453 for_each_available_child_of_node(bus->dev.of_node, child) { in of_mdiobus_link_mdiodev()
454 int addr; in of_mdiobus_link_mdiodev() local
456 addr = of_mdio_parse_addr(dev, child); in of_mdiobus_link_mdiodev()
457 if (addr < 0) in of_mdiobus_link_mdiodev()
460 if (addr == mdiodev->addr) { in of_mdiobus_link_mdiodev()
461 dev->of_node = child; in of_mdiobus_link_mdiodev()
462 dev->fwnode = of_fwnode_handle(child); in of_mdiobus_link_mdiodev()
475 * mdiobus_create_device_from_board_info - create a full MDIO device given
477 * @bus: MDIO bus to create the devices on
482 static int mdiobus_create_device(struct mii_bus *bus, in mdiobus_create_device() argument
488 mdiodev = mdio_device_create(bus, bi->mdio_addr); in mdiobus_create_device()
490 return -ENODEV; in mdiobus_create_device()
492 strncpy(mdiodev->modalias, bi->modalias, in mdiobus_create_device()
493 sizeof(mdiodev->modalias)); in mdiobus_create_device()
494 mdiodev->bus_match = mdio_device_bus_match; in mdiobus_create_device()
495 mdiodev->dev.platform_data = (void *)bi->platform_data; in mdiobus_create_device()
505 * __mdiobus_register - bring up all the PHYs on a given bus and attach them to bus
506 * @bus: target mii_bus
507 * @owner: module containing bus accessor functions
509 * Description: Called by a bus driver to bring up all the PHYs
510 * on a given bus, and attach them to the bus. Drivers should use
518 int __mdiobus_register(struct mii_bus *bus, struct module *owner) in __mdiobus_register() argument
524 if (NULL == bus || NULL == bus->name || in __mdiobus_register()
525 NULL == bus->read || NULL == bus->write) in __mdiobus_register()
526 return -EINVAL; in __mdiobus_register()
528 BUG_ON(bus->state != MDIOBUS_ALLOCATED && in __mdiobus_register()
529 bus->state != MDIOBUS_UNREGISTERED); in __mdiobus_register()
531 bus->owner = owner; in __mdiobus_register()
532 bus->dev.parent = bus->parent; in __mdiobus_register()
533 bus->dev.class = &mdio_bus_class; in __mdiobus_register()
534 bus->dev.groups = NULL; in __mdiobus_register()
535 dev_set_name(&bus->dev, "%s", bus->id); in __mdiobus_register()
537 err = device_register(&bus->dev); in __mdiobus_register()
539 pr_err("mii_bus %s failed to register\n", bus->id); in __mdiobus_register()
540 return -EINVAL; in __mdiobus_register()
543 mutex_init(&bus->mdio_lock); in __mdiobus_register()
544 mutex_init(&bus->shared_lock); in __mdiobus_register()
546 /* de-assert bus level PHY GPIO reset */ in __mdiobus_register()
547 gpiod = devm_gpiod_get_optional(&bus->dev, "reset", GPIOD_OUT_LOW); in __mdiobus_register()
549 dev_err(&bus->dev, "mii_bus %s couldn't get reset GPIO\n", in __mdiobus_register()
550 bus->id); in __mdiobus_register()
551 device_del(&bus->dev); in __mdiobus_register()
554 bus->reset_gpiod = gpiod; in __mdiobus_register()
557 fsleep(bus->reset_delay_us); in __mdiobus_register()
559 if (bus->reset_post_delay_us > 0) in __mdiobus_register()
560 fsleep(bus->reset_post_delay_us); in __mdiobus_register()
563 if (bus->reset) { in __mdiobus_register()
564 err = bus->reset(bus); in __mdiobus_register()
570 if ((bus->phy_mask & (1 << i)) == 0) { in __mdiobus_register()
573 phydev = mdiobus_scan(bus, i); in __mdiobus_register()
574 if (IS_ERR(phydev) && (PTR_ERR(phydev) != -ENODEV)) { in __mdiobus_register()
581 mdiobus_setup_mdiodev_from_board_info(bus, mdiobus_create_device); in __mdiobus_register()
583 bus->state = MDIOBUS_REGISTERED; in __mdiobus_register()
584 pr_info("%s: probed\n", bus->name); in __mdiobus_register()
588 while (--i >= 0) { in __mdiobus_register()
589 mdiodev = bus->mdio_map[i]; in __mdiobus_register()
593 mdiodev->device_remove(mdiodev); in __mdiobus_register()
594 mdiodev->device_free(mdiodev); in __mdiobus_register()
598 if (bus->reset_gpiod) in __mdiobus_register()
599 gpiod_set_value_cansleep(bus->reset_gpiod, 1); in __mdiobus_register()
601 device_del(&bus->dev); in __mdiobus_register()
606 void mdiobus_unregister(struct mii_bus *bus) in mdiobus_unregister() argument
611 BUG_ON(bus->state != MDIOBUS_REGISTERED); in mdiobus_unregister()
612 bus->state = MDIOBUS_UNREGISTERED; in mdiobus_unregister()
615 mdiodev = bus->mdio_map[i]; in mdiobus_unregister()
619 if (mdiodev->reset_gpio) in mdiobus_unregister()
620 gpiod_put(mdiodev->reset_gpio); in mdiobus_unregister()
622 mdiodev->device_remove(mdiodev); in mdiobus_unregister()
623 mdiodev->device_free(mdiodev); in mdiobus_unregister()
627 if (bus->reset_gpiod) in mdiobus_unregister()
628 gpiod_set_value_cansleep(bus->reset_gpiod, 1); in mdiobus_unregister()
630 device_del(&bus->dev); in mdiobus_unregister()
635 * mdiobus_free - free a struct mii_bus
636 * @bus: mii_bus to free
642 void mdiobus_free(struct mii_bus *bus) in mdiobus_free() argument
645 if (bus->state == MDIOBUS_ALLOCATED) { in mdiobus_free()
646 kfree(bus); in mdiobus_free()
650 BUG_ON(bus->state != MDIOBUS_UNREGISTERED); in mdiobus_free()
651 bus->state = MDIOBUS_RELEASED; in mdiobus_free()
653 put_device(&bus->dev); in mdiobus_free()
658 * mdiobus_scan - scan a bus for MDIO devices.
659 * @bus: mii_bus to scan
660 * @addr: address on bus to scan
662 * This function scans the MDIO bus, looking for devices which can be
669 struct phy_device *mdiobus_scan(struct mii_bus *bus, int addr) in mdiobus_scan() argument
671 struct phy_device *phydev = ERR_PTR(-ENODEV); in mdiobus_scan()
674 switch (bus->probe_capabilities) { in mdiobus_scan()
677 phydev = get_phy_device(bus, addr, false); in mdiobus_scan()
680 phydev = get_phy_device(bus, addr, true); in mdiobus_scan()
683 phydev = get_phy_device(bus, addr, false); in mdiobus_scan()
685 phydev = get_phy_device(bus, addr, true); in mdiobus_scan()
693 * For DT, see if the auto-probed phy has a correspoding child in mdiobus_scan()
694 * in the bus node, and set the of_node pointer in this case. in mdiobus_scan()
696 of_mdiobus_link_mdiodev(bus, &phydev->mdio); in mdiobus_scan()
701 return ERR_PTR(-ENODEV); in mdiobus_scan()
711 u64_stats_update_begin(&stats->syncp); in mdiobus_stats_acct()
713 u64_stats_inc(&stats->transfers); in mdiobus_stats_acct()
715 u64_stats_inc(&stats->errors); in mdiobus_stats_acct()
720 u64_stats_inc(&stats->reads); in mdiobus_stats_acct()
722 u64_stats_inc(&stats->writes); in mdiobus_stats_acct()
724 u64_stats_update_end(&stats->syncp); in mdiobus_stats_acct()
729 * __mdiobus_read - Unlocked version of the mdiobus_read function
730 * @bus: the mii_bus struct
731 * @addr: the phy address
734 * Read a MDIO bus register. Caller must hold the mdio bus lock.
738 int __mdiobus_read(struct mii_bus *bus, int addr, u32 regnum) in __mdiobus_read() argument
742 WARN_ON_ONCE(!mutex_is_locked(&bus->mdio_lock)); in __mdiobus_read()
744 retval = bus->read(bus, addr, regnum); in __mdiobus_read()
746 trace_mdio_access(bus, 1, addr, regnum, retval, retval); in __mdiobus_read()
747 mdiobus_stats_acct(&bus->stats[addr], true, retval); in __mdiobus_read()
754 * __mdiobus_write - Unlocked version of the mdiobus_write function
755 * @bus: the mii_bus struct
756 * @addr: the phy address
760 * Write a MDIO bus register. Caller must hold the mdio bus lock.
764 int __mdiobus_write(struct mii_bus *bus, int addr, u32 regnum, u16 val) in __mdiobus_write() argument
768 WARN_ON_ONCE(!mutex_is_locked(&bus->mdio_lock)); in __mdiobus_write()
770 err = bus->write(bus, addr, regnum, val); in __mdiobus_write()
772 trace_mdio_access(bus, 0, addr, regnum, val, err); in __mdiobus_write()
773 mdiobus_stats_acct(&bus->stats[addr], false, err); in __mdiobus_write()
780 * __mdiobus_modify_changed - Unlocked version of the mdiobus_modify function
781 * @bus: the mii_bus struct
782 * @addr: the phy address
792 int __mdiobus_modify_changed(struct mii_bus *bus, int addr, u32 regnum, in __mdiobus_modify_changed() argument
797 ret = __mdiobus_read(bus, addr, regnum); in __mdiobus_modify_changed()
805 ret = __mdiobus_write(bus, addr, regnum, new); in __mdiobus_modify_changed()
812 * mdiobus_read_nested - Nested version of the mdiobus_read function
813 * @bus: the mii_bus struct
814 * @addr: the phy address
817 * In case of nested MDIO bus access avoid lockdep false positives by
821 * because the bus read/write functions may wait for an interrupt
824 int mdiobus_read_nested(struct mii_bus *bus, int addr, u32 regnum) in mdiobus_read_nested() argument
828 mutex_lock_nested(&bus->mdio_lock, MDIO_MUTEX_NESTED); in mdiobus_read_nested()
829 retval = __mdiobus_read(bus, addr, regnum); in mdiobus_read_nested()
830 mutex_unlock(&bus->mdio_lock); in mdiobus_read_nested()
837 * mdiobus_read - Convenience function for reading a given MII mgmt register
838 * @bus: the mii_bus struct
839 * @addr: the phy address
843 * because the bus read/write functions may wait for an interrupt
846 int mdiobus_read(struct mii_bus *bus, int addr, u32 regnum) in mdiobus_read() argument
850 mutex_lock(&bus->mdio_lock); in mdiobus_read()
851 retval = __mdiobus_read(bus, addr, regnum); in mdiobus_read()
852 mutex_unlock(&bus->mdio_lock); in mdiobus_read()
859 * mdiobus_write_nested - Nested version of the mdiobus_write function
860 * @bus: the mii_bus struct
861 * @addr: the phy address
865 * In case of nested MDIO bus access avoid lockdep false positives by
869 * because the bus read/write functions may wait for an interrupt
872 int mdiobus_write_nested(struct mii_bus *bus, int addr, u32 regnum, u16 val) in mdiobus_write_nested() argument
876 mutex_lock_nested(&bus->mdio_lock, MDIO_MUTEX_NESTED); in mdiobus_write_nested()
877 err = __mdiobus_write(bus, addr, regnum, val); in mdiobus_write_nested()
878 mutex_unlock(&bus->mdio_lock); in mdiobus_write_nested()
885 * mdiobus_write - Convenience function for writing a given MII mgmt register
886 * @bus: the mii_bus struct
887 * @addr: the phy address
892 * because the bus read/write functions may wait for an interrupt
895 int mdiobus_write(struct mii_bus *bus, int addr, u32 regnum, u16 val) in mdiobus_write() argument
899 mutex_lock(&bus->mdio_lock); in mdiobus_write()
900 err = __mdiobus_write(bus, addr, regnum, val); in mdiobus_write()
901 mutex_unlock(&bus->mdio_lock); in mdiobus_write()
908 * mdiobus_modify - Convenience function for modifying a given mdio device
910 * @bus: the mii_bus struct
911 * @addr: the phy address
916 int mdiobus_modify(struct mii_bus *bus, int addr, u32 regnum, u16 mask, u16 set) in mdiobus_modify() argument
920 mutex_lock(&bus->mdio_lock); in mdiobus_modify()
921 err = __mdiobus_modify_changed(bus, addr, regnum, mask, set); in mdiobus_modify()
922 mutex_unlock(&bus->mdio_lock); in mdiobus_modify()
929 * mdio_bus_match - determine if given MDIO driver supports the given
946 if (mdio->bus_match) in mdio_bus_match()
947 return mdio->bus_match(dev, drv); in mdio_bus_match()
956 /* Some devices have extra OF data and an OF-style MODALIAS */ in mdio_uevent()
958 if (rc != -ENODEV) in mdio_uevent()
1016 MODULE_DESCRIPTION("MDIO bus/device layer");