Lines Matching +full:ethernet +full:- +full:phy +full:- +full:package

1 // SPDX-License-Identifier: GPL-2.0+
28 #include <linux/phy.h>
40 #include "mdio-boardinfo.h"
45 mdiodev->reset_gpio = gpiod_get_optional(&mdiodev->dev, in mdiobus_register_gpiod()
47 if (IS_ERR(mdiodev->reset_gpio)) in mdiobus_register_gpiod()
48 return PTR_ERR(mdiodev->reset_gpio); in mdiobus_register_gpiod()
50 if (mdiodev->reset_gpio) in mdiobus_register_gpiod()
51 gpiod_set_consumer_name(mdiodev->reset_gpio, "PHY reset"); in mdiobus_register_gpiod()
60 reset = reset_control_get_optional_exclusive(&mdiodev->dev, "phy"); in mdiobus_register_reset()
64 mdiodev->reset_ctrl = reset; in mdiobus_register_reset()
73 if (mdiodev->bus->mdio_map[mdiodev->addr]) in mdiobus_register_device()
74 return -EBUSY; in mdiobus_register_device()
76 if (mdiodev->flags & MDIO_DEVICE_FLAG_PHY) { in mdiobus_register_device()
89 mdiodev->bus->mdio_map[mdiodev->addr] = mdiodev; in mdiobus_register_device()
97 if (mdiodev->bus->mdio_map[mdiodev->addr] != mdiodev) in mdiobus_unregister_device()
98 return -EINVAL; in mdiobus_unregister_device()
100 reset_control_put(mdiodev->reset_ctrl); in mdiobus_unregister_device()
102 mdiodev->bus->mdio_map[mdiodev->addr] = NULL; in mdiobus_unregister_device()
110 bool addr_valid = addr >= 0 && addr < ARRAY_SIZE(bus->mdio_map); in mdiobus_find_device()
115 return bus->mdio_map[addr]; in mdiobus_find_device()
126 if (!(mdiodev->flags & MDIO_DEVICE_FLAG_PHY)) in mdiobus_get_phy()
140 * mdiobus_alloc_size - allocate a mii_bus structure
142 * If non-zero, then bus->priv is points to that memory.
164 bus->state = MDIOBUS_ALLOCATED; in mdiobus_alloc_size()
166 bus->priv = (void *)bus + aligned_size; in mdiobus_alloc_size()
168 /* Initialise the interrupts to polling and 64-bit seqcounts */ in mdiobus_alloc_size()
170 bus->irq[i] = PHY_POLL; in mdiobus_alloc_size()
171 u64_stats_init(&bus->stats[i].syncp); in mdiobus_alloc_size()
179 * mdiobus_release - mii_bus device release callback
189 WARN(bus->state != MDIOBUS_RELEASED && in mdiobus_release()
191 bus->state != MDIOBUS_ALLOCATED, in mdiobus_release()
193 bus->id); in mdiobus_release()
195 if (bus->state == MDIOBUS_RELEASED) in mdiobus_release()
213 start = u64_stats_fetch_begin(&s->syncp); in mdio_bus_get_stat()
215 } while (u64_stats_fetch_retry(&s->syncp, start)); in mdio_bus_get_stat()
226 val += mdio_bus_get_stat(&bus->stats[i], offset); in mdio_bus_get_global_stat()
241 sattr = eattr->var; in mdio_bus_stat_field_show()
243 if (sattr->addr < 0) in mdio_bus_stat_field_show()
244 val = mdio_bus_get_global_stat(bus, sattr->field_offset); in mdio_bus_stat_field_show()
246 val = mdio_bus_get_stat(&bus->stats[sattr->addr], in mdio_bus_stat_field_show()
247 sattr->field_offset); in mdio_bus_stat_field_show()
257 struct mii_bus *bus = mdiodev->bus; in mdio_bus_device_stat_field_show()
260 int addr = mdiodev->addr; in mdio_bus_device_stat_field_show()
264 sattr = eattr->var; in mdio_bus_device_stat_field_show()
266 val = mdio_bus_get_stat(&bus->stats[addr], sattr->field_offset); in mdio_bus_device_stat_field_show()
277 -1, offsetof(struct mdio_bus_stats, field) \
285 -1, offsetof(struct mdio_bus_stats, field) \
413 * mdio_find_bus - Given the name of a mdiobus, find the mii_bus.
431 * of_mdio_find_bus - Given an mii_bus node, find the mii_bus.
458 * auto-probed phy devices to be supplied with information passed in
460 * If a PHY package is found, PHY is searched also there.
470 if (of_node_name_eq(child, "ethernet-phy-package")) { in of_mdiobus_find_phy()
471 /* Validate PHY package reg presence */ in of_mdiobus_find_phy()
474 return -EINVAL; in of_mdiobus_find_phy()
478 /* The refcount for the PHY package will be in of_mdiobus_find_phy()
479 * incremented later when PHY join the Package. in of_mdiobus_find_phy()
492 if (addr == mdiodev->addr) { in of_mdiobus_find_phy()
501 return -ENODEV; in of_mdiobus_find_phy()
507 struct device *dev = &mdiodev->dev; in of_mdiobus_link_mdiodev()
509 if (dev->of_node || !bus->dev.of_node) in of_mdiobus_link_mdiodev()
512 of_mdiobus_find_phy(dev, mdiodev, bus->dev.of_node); in of_mdiobus_link_mdiodev()
522 * mdiobus_create_device - create a full MDIO device given
535 mdiodev = mdio_device_create(bus, bi->mdio_addr); in mdiobus_create_device()
537 return -ENODEV; in mdiobus_create_device()
539 strscpy(mdiodev->modalias, bi->modalias, in mdiobus_create_device()
540 sizeof(mdiodev->modalias)); in mdiobus_create_device()
541 mdiodev->bus_match = mdio_device_bus_match; in mdiobus_create_device()
542 mdiodev->dev.platform_data = (void *)bi->platform_data; in mdiobus_create_device()
553 struct phy_device *phydev = ERR_PTR(-ENODEV); in mdiobus_scan()
560 /* For DT, see if the auto-probed phy has a corresponding child in mdiobus_scan()
563 of_mdiobus_link_mdiodev(bus, &phydev->mdio); in mdiobus_scan()
568 return ERR_PTR(-ENODEV); in mdiobus_scan()
575 * mdiobus_scan_c22 - scan one address on a bus for C22 MDIO devices.
582 * PHY devices typically do. Hence this function assumes anything
583 * found is a PHY, or can be treated as a PHY. Other MDIO devices,
593 * mdiobus_scan_c45 - scan one address on a bus for C45 MDIO devices.
600 * PHY devices typically do. Hence this function assumes anything
601 * found is a PHY, or can be treated as a PHY. Other MDIO devices,
614 if ((bus->phy_mask & BIT(i)) == 0) { in mdiobus_scan_bus_c22()
618 if (IS_ERR(phydev) && (PTR_ERR(phydev) != -ENODEV)) in mdiobus_scan_bus_c22()
630 if ((bus->phy_mask & BIT(i)) == 0) { in mdiobus_scan_bus_c45()
634 if (bus->mdio_map[i]) in mdiobus_scan_bus_c45()
638 if (IS_ERR(phydev) && (PTR_ERR(phydev) != -ENODEV)) in mdiobus_scan_bus_c45()
663 oui = phydev->phy_id >> 10; in mdiobus_prevent_c45_scan()
672 * __mdiobus_register - bring up all the PHYs on a given bus and attach them to bus
692 if (!bus || !bus->name) in __mdiobus_register()
693 return -EINVAL; in __mdiobus_register()
696 if (!!bus->read != !!bus->write || !!bus->read_c45 != !!bus->write_c45) in __mdiobus_register()
697 return -EINVAL; in __mdiobus_register()
700 if (!bus->read && !bus->read_c45) in __mdiobus_register()
701 return -EINVAL; in __mdiobus_register()
703 if (bus->parent && bus->parent->of_node) in __mdiobus_register()
704 bus->parent->of_node->fwnode.flags |= in __mdiobus_register()
707 WARN(bus->state != MDIOBUS_ALLOCATED && in __mdiobus_register()
708 bus->state != MDIOBUS_UNREGISTERED, in __mdiobus_register()
709 "%s: not in ALLOCATED or UNREGISTERED state\n", bus->id); in __mdiobus_register()
711 bus->owner = owner; in __mdiobus_register()
712 bus->dev.parent = bus->parent; in __mdiobus_register()
713 bus->dev.class = &mdio_bus_class; in __mdiobus_register()
714 bus->dev.groups = NULL; in __mdiobus_register()
715 dev_set_name(&bus->dev, "%s", bus->id); in __mdiobus_register()
723 if (bus->state == MDIOBUS_ALLOCATED) in __mdiobus_register()
724 fwnode_handle_get(dev_fwnode(&bus->dev)); in __mdiobus_register()
731 bus->state = MDIOBUS_UNREGISTERED; in __mdiobus_register()
733 err = device_register(&bus->dev); in __mdiobus_register()
735 pr_err("mii_bus %s failed to register\n", bus->id); in __mdiobus_register()
736 return -EINVAL; in __mdiobus_register()
739 mutex_init(&bus->mdio_lock); in __mdiobus_register()
740 mutex_init(&bus->shared_lock); in __mdiobus_register()
742 /* assert bus level PHY GPIO reset */ in __mdiobus_register()
743 gpiod = devm_gpiod_get_optional(&bus->dev, "reset", GPIOD_OUT_HIGH); in __mdiobus_register()
745 err = dev_err_probe(&bus->dev, PTR_ERR(gpiod), in __mdiobus_register()
747 bus->id); in __mdiobus_register()
748 device_del(&bus->dev); in __mdiobus_register()
751 bus->reset_gpiod = gpiod; in __mdiobus_register()
752 fsleep(bus->reset_delay_us); in __mdiobus_register()
754 if (bus->reset_post_delay_us > 0) in __mdiobus_register()
755 fsleep(bus->reset_post_delay_us); in __mdiobus_register()
758 if (bus->reset) { in __mdiobus_register()
759 err = bus->reset(bus); in __mdiobus_register()
764 if (bus->read) { in __mdiobus_register()
772 if (!prevent_c45_scan && bus->read_c45) { in __mdiobus_register()
780 bus->state = MDIOBUS_REGISTERED; in __mdiobus_register()
781 dev_dbg(&bus->dev, "probed\n"); in __mdiobus_register()
786 mdiodev = bus->mdio_map[i]; in __mdiobus_register()
790 mdiodev->device_remove(mdiodev); in __mdiobus_register()
791 mdiodev->device_free(mdiodev); in __mdiobus_register()
795 if (bus->reset_gpiod) in __mdiobus_register()
796 gpiod_set_value_cansleep(bus->reset_gpiod, 1); in __mdiobus_register()
798 device_del(&bus->dev); in __mdiobus_register()
808 if (WARN_ON_ONCE(bus->state != MDIOBUS_REGISTERED)) in mdiobus_unregister()
810 bus->state = MDIOBUS_UNREGISTERED; in mdiobus_unregister()
813 mdiodev = bus->mdio_map[i]; in mdiobus_unregister()
817 if (mdiodev->reset_gpio) in mdiobus_unregister()
818 gpiod_put(mdiodev->reset_gpio); in mdiobus_unregister()
820 mdiodev->device_remove(mdiodev); in mdiobus_unregister()
821 mdiodev->device_free(mdiodev); in mdiobus_unregister()
825 if (bus->reset_gpiod) in mdiobus_unregister()
826 gpiod_set_value_cansleep(bus->reset_gpiod, 1); in mdiobus_unregister()
828 device_del(&bus->dev); in mdiobus_unregister()
833 * mdiobus_free - free a struct mii_bus
843 if (bus->state == MDIOBUS_ALLOCATED) { in mdiobus_free()
848 WARN(bus->state != MDIOBUS_UNREGISTERED, in mdiobus_free()
849 "%s: not in UNREGISTERED state\n", bus->id); in mdiobus_free()
850 bus->state = MDIOBUS_RELEASED; in mdiobus_free()
852 put_device(&bus->dev); in mdiobus_free()
859 u64_stats_update_begin(&stats->syncp); in mdiobus_stats_acct()
861 u64_stats_inc(&stats->transfers); in mdiobus_stats_acct()
863 u64_stats_inc(&stats->errors); in mdiobus_stats_acct()
868 u64_stats_inc(&stats->reads); in mdiobus_stats_acct()
870 u64_stats_inc(&stats->writes); in mdiobus_stats_acct()
872 u64_stats_update_end(&stats->syncp); in mdiobus_stats_acct()
877 * __mdiobus_read - Unlocked version of the mdiobus_read function
879 * @addr: the phy address
890 lockdep_assert_held_once(&bus->mdio_lock); in __mdiobus_read()
892 if (bus->read) in __mdiobus_read()
893 retval = bus->read(bus, addr, regnum); in __mdiobus_read()
895 retval = -EOPNOTSUPP; in __mdiobus_read()
898 mdiobus_stats_acct(&bus->stats[addr], true, retval); in __mdiobus_read()
905 * __mdiobus_write - Unlocked version of the mdiobus_write function
907 * @addr: the phy address
919 lockdep_assert_held_once(&bus->mdio_lock); in __mdiobus_write()
921 if (bus->write) in __mdiobus_write()
922 err = bus->write(bus, addr, regnum, val); in __mdiobus_write()
924 err = -EOPNOTSUPP; in __mdiobus_write()
927 mdiobus_stats_acct(&bus->stats[addr], false, err); in __mdiobus_write()
934 * __mdiobus_modify_changed - Unlocked version of the mdiobus_modify function
936 * @addr: the phy address
966 * __mdiobus_c45_read - Unlocked version of the mdiobus_c45_read function
968 * @addr: the phy address
980 lockdep_assert_held_once(&bus->mdio_lock); in __mdiobus_c45_read()
982 if (bus->read_c45) in __mdiobus_c45_read()
983 retval = bus->read_c45(bus, addr, devad, regnum); in __mdiobus_c45_read()
985 retval = -EOPNOTSUPP; in __mdiobus_c45_read()
988 mdiobus_stats_acct(&bus->stats[addr], true, retval); in __mdiobus_c45_read()
995 * __mdiobus_c45_write - Unlocked version of the mdiobus_write function
997 * @addr: the phy address
1011 lockdep_assert_held_once(&bus->mdio_lock); in __mdiobus_c45_write()
1013 if (bus->write_c45) in __mdiobus_c45_write()
1014 err = bus->write_c45(bus, addr, devad, regnum, val); in __mdiobus_c45_write()
1016 err = -EOPNOTSUPP; in __mdiobus_c45_write()
1019 mdiobus_stats_acct(&bus->stats[addr], false, err); in __mdiobus_c45_write()
1026 * __mdiobus_c45_modify_changed - Unlocked version of the mdiobus_modify function
1028 * @addr: the phy address
1059 * mdiobus_read_nested - Nested version of the mdiobus_read function
1061 * @addr: the phy address
1075 mutex_lock_nested(&bus->mdio_lock, MDIO_MUTEX_NESTED); in mdiobus_read_nested()
1077 mutex_unlock(&bus->mdio_lock); in mdiobus_read_nested()
1084 * mdiobus_read - Convenience function for reading a given MII mgmt register
1086 * @addr: the phy address
1097 mutex_lock(&bus->mdio_lock); in mdiobus_read()
1099 mutex_unlock(&bus->mdio_lock); in mdiobus_read()
1106 * mdiobus_c45_read - Convenience function for reading a given MII mgmt register
1108 * @addr: the phy address
1120 mutex_lock(&bus->mdio_lock); in mdiobus_c45_read()
1122 mutex_unlock(&bus->mdio_lock); in mdiobus_c45_read()
1129 * mdiobus_c45_read_nested - Nested version of the mdiobus_c45_read function
1131 * @addr: the phy address
1147 mutex_lock_nested(&bus->mdio_lock, MDIO_MUTEX_NESTED); in mdiobus_c45_read_nested()
1149 mutex_unlock(&bus->mdio_lock); in mdiobus_c45_read_nested()
1156 * mdiobus_write_nested - Nested version of the mdiobus_write function
1158 * @addr: the phy address
1173 mutex_lock_nested(&bus->mdio_lock, MDIO_MUTEX_NESTED); in mdiobus_write_nested()
1175 mutex_unlock(&bus->mdio_lock); in mdiobus_write_nested()
1182 * mdiobus_write - Convenience function for writing a given MII mgmt register
1184 * @addr: the phy address
1196 mutex_lock(&bus->mdio_lock); in mdiobus_write()
1198 mutex_unlock(&bus->mdio_lock); in mdiobus_write()
1205 * mdiobus_c45_write - Convenience function for writing a given MII mgmt register
1207 * @addr: the phy address
1221 mutex_lock(&bus->mdio_lock); in mdiobus_c45_write()
1223 mutex_unlock(&bus->mdio_lock); in mdiobus_c45_write()
1230 * mdiobus_c45_write_nested - Nested version of the mdiobus_c45_write function
1232 * @addr: the phy address
1249 mutex_lock_nested(&bus->mdio_lock, MDIO_MUTEX_NESTED); in mdiobus_c45_write_nested()
1251 mutex_unlock(&bus->mdio_lock); in mdiobus_c45_write_nested()
1258 * __mdiobus_modify - Convenience function for modifying a given mdio device
1261 * @addr: the phy address
1278 * mdiobus_modify - Convenience function for modifying a given mdio device
1281 * @addr: the phy address
1290 mutex_lock(&bus->mdio_lock); in mdiobus_modify()
1292 mutex_unlock(&bus->mdio_lock); in mdiobus_modify()
1299 * mdiobus_c45_modify - Convenience function for modifying a given mdio device
1302 * @addr: the phy address
1313 mutex_lock(&bus->mdio_lock); in mdiobus_c45_modify()
1316 mutex_unlock(&bus->mdio_lock); in mdiobus_c45_modify()
1323 * mdiobus_modify_changed - Convenience function for modifying a given mdio
1326 * @addr: the phy address
1336 mutex_lock(&bus->mdio_lock); in mdiobus_modify_changed()
1338 mutex_unlock(&bus->mdio_lock); in mdiobus_modify_changed()
1345 * mdiobus_c45_modify_changed - Convenience function for modifying a given mdio
1348 * @addr: the phy address
1359 mutex_lock(&bus->mdio_lock); in mdiobus_c45_modify_changed()
1361 mutex_unlock(&bus->mdio_lock); in mdiobus_c45_modify_changed()
1368 * mdio_bus_match - determine if given MDIO driver supports the given
1383 /* Both the driver and device must type-match */ in mdio_bus_match()
1384 if (!(mdiodrv->mdiodrv.flags & MDIO_DEVICE_IS_PHY) != in mdio_bus_match()
1385 !(mdio->flags & MDIO_DEVICE_FLAG_PHY)) in mdio_bus_match()
1391 if (mdio->bus_match) in mdio_bus_match()
1392 return mdio->bus_match(dev, drv); in mdio_bus_match()
1401 /* Some devices have extra OF data and an OF-style MODALIAS */ in mdio_uevent()
1403 if (rc != -ENODEV) in mdio_uevent()