Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0+
0002 /* MDIO Bus interface
0003  *
0004  * Author: Andy Fleming
0005  *
0006  * Copyright (c) 2004 Freescale Semiconductor, Inc.
0007  */
0008 
0009 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
0010 
0011 #include <linux/delay.h>
0012 #include <linux/device.h>
0013 #include <linux/errno.h>
0014 #include <linux/etherdevice.h>
0015 #include <linux/ethtool.h>
0016 #include <linux/gpio.h>
0017 #include <linux/gpio/consumer.h>
0018 #include <linux/init.h>
0019 #include <linux/interrupt.h>
0020 #include <linux/io.h>
0021 #include <linux/kernel.h>
0022 #include <linux/mii.h>
0023 #include <linux/mm.h>
0024 #include <linux/module.h>
0025 #include <linux/netdevice.h>
0026 #include <linux/of_device.h>
0027 #include <linux/of_gpio.h>
0028 #include <linux/of_mdio.h>
0029 #include <linux/phy.h>
0030 #include <linux/reset.h>
0031 #include <linux/skbuff.h>
0032 #include <linux/slab.h>
0033 #include <linux/spinlock.h>
0034 #include <linux/string.h>
0035 #include <linux/uaccess.h>
0036 #include <linux/unistd.h>
0037 
0038 #define CREATE_TRACE_POINTS
0039 #include <trace/events/mdio.h>
0040 
0041 #include "mdio-boardinfo.h"
0042 
0043 static int mdiobus_register_gpiod(struct mdio_device *mdiodev)
0044 {
0045     /* Deassert the optional reset signal */
0046     mdiodev->reset_gpio = gpiod_get_optional(&mdiodev->dev,
0047                          "reset", GPIOD_OUT_LOW);
0048     if (IS_ERR(mdiodev->reset_gpio))
0049         return PTR_ERR(mdiodev->reset_gpio);
0050 
0051     if (mdiodev->reset_gpio)
0052         gpiod_set_consumer_name(mdiodev->reset_gpio, "PHY reset");
0053 
0054     return 0;
0055 }
0056 
0057 static int mdiobus_register_reset(struct mdio_device *mdiodev)
0058 {
0059     struct reset_control *reset;
0060 
0061     reset = reset_control_get_optional_exclusive(&mdiodev->dev, "phy");
0062     if (IS_ERR(reset))
0063         return PTR_ERR(reset);
0064 
0065     mdiodev->reset_ctrl = reset;
0066 
0067     return 0;
0068 }
0069 
0070 int mdiobus_register_device(struct mdio_device *mdiodev)
0071 {
0072     int err;
0073 
0074     if (mdiodev->bus->mdio_map[mdiodev->addr])
0075         return -EBUSY;
0076 
0077     if (mdiodev->flags & MDIO_DEVICE_FLAG_PHY) {
0078         err = mdiobus_register_gpiod(mdiodev);
0079         if (err)
0080             return err;
0081 
0082         err = mdiobus_register_reset(mdiodev);
0083         if (err)
0084             return err;
0085 
0086         /* Assert the reset signal */
0087         mdio_device_reset(mdiodev, 1);
0088     }
0089 
0090     mdiodev->bus->mdio_map[mdiodev->addr] = mdiodev;
0091 
0092     return 0;
0093 }
0094 EXPORT_SYMBOL(mdiobus_register_device);
0095 
0096 int mdiobus_unregister_device(struct mdio_device *mdiodev)
0097 {
0098     if (mdiodev->bus->mdio_map[mdiodev->addr] != mdiodev)
0099         return -EINVAL;
0100 
0101     reset_control_put(mdiodev->reset_ctrl);
0102 
0103     mdiodev->bus->mdio_map[mdiodev->addr] = NULL;
0104 
0105     return 0;
0106 }
0107 EXPORT_SYMBOL(mdiobus_unregister_device);
0108 
0109 struct phy_device *mdiobus_get_phy(struct mii_bus *bus, int addr)
0110 {
0111     struct mdio_device *mdiodev = bus->mdio_map[addr];
0112 
0113     if (!mdiodev)
0114         return NULL;
0115 
0116     if (!(mdiodev->flags & MDIO_DEVICE_FLAG_PHY))
0117         return NULL;
0118 
0119     return container_of(mdiodev, struct phy_device, mdio);
0120 }
0121 EXPORT_SYMBOL(mdiobus_get_phy);
0122 
0123 bool mdiobus_is_registered_device(struct mii_bus *bus, int addr)
0124 {
0125     return bus->mdio_map[addr];
0126 }
0127 EXPORT_SYMBOL(mdiobus_is_registered_device);
0128 
0129 /**
0130  * mdiobus_alloc_size - allocate a mii_bus structure
0131  * @size: extra amount of memory to allocate for private storage.
0132  * If non-zero, then bus->priv is points to that memory.
0133  *
0134  * Description: called by a bus driver to allocate an mii_bus
0135  * structure to fill in.
0136  */
0137 struct mii_bus *mdiobus_alloc_size(size_t size)
0138 {
0139     struct mii_bus *bus;
0140     size_t aligned_size = ALIGN(sizeof(*bus), NETDEV_ALIGN);
0141     size_t alloc_size;
0142     int i;
0143 
0144     /* If we alloc extra space, it should be aligned */
0145     if (size)
0146         alloc_size = aligned_size + size;
0147     else
0148         alloc_size = sizeof(*bus);
0149 
0150     bus = kzalloc(alloc_size, GFP_KERNEL);
0151     if (!bus)
0152         return NULL;
0153 
0154     bus->state = MDIOBUS_ALLOCATED;
0155     if (size)
0156         bus->priv = (void *)bus + aligned_size;
0157 
0158     /* Initialise the interrupts to polling and 64-bit seqcounts */
0159     for (i = 0; i < PHY_MAX_ADDR; i++) {
0160         bus->irq[i] = PHY_POLL;
0161         u64_stats_init(&bus->stats[i].syncp);
0162     }
0163 
0164     return bus;
0165 }
0166 EXPORT_SYMBOL(mdiobus_alloc_size);
0167 
0168 /**
0169  * mdiobus_release - mii_bus device release callback
0170  * @d: the target struct device that contains the mii_bus
0171  *
0172  * Description: called when the last reference to an mii_bus is
0173  * dropped, to free the underlying memory.
0174  */
0175 static void mdiobus_release(struct device *d)
0176 {
0177     struct mii_bus *bus = to_mii_bus(d);
0178 
0179     WARN(bus->state != MDIOBUS_RELEASED &&
0180          /* for compatibility with error handling in drivers */
0181          bus->state != MDIOBUS_ALLOCATED,
0182          "%s: not in RELEASED or ALLOCATED state\n",
0183          bus->id);
0184     kfree(bus);
0185 }
0186 
0187 struct mdio_bus_stat_attr {
0188     int addr;
0189     unsigned int field_offset;
0190 };
0191 
0192 static u64 mdio_bus_get_stat(struct mdio_bus_stats *s, unsigned int offset)
0193 {
0194     const char *p = (const char *)s + offset;
0195     unsigned int start;
0196     u64 val = 0;
0197 
0198     do {
0199         start = u64_stats_fetch_begin(&s->syncp);
0200         val = u64_stats_read((const u64_stats_t *)p);
0201     } while (u64_stats_fetch_retry(&s->syncp, start));
0202 
0203     return val;
0204 }
0205 
0206 static u64 mdio_bus_get_global_stat(struct mii_bus *bus, unsigned int offset)
0207 {
0208     unsigned int i;
0209     u64 val = 0;
0210 
0211     for (i = 0; i < PHY_MAX_ADDR; i++)
0212         val += mdio_bus_get_stat(&bus->stats[i], offset);
0213 
0214     return val;
0215 }
0216 
0217 static ssize_t mdio_bus_stat_field_show(struct device *dev,
0218                     struct device_attribute *attr,
0219                     char *buf)
0220 {
0221     struct mii_bus *bus = to_mii_bus(dev);
0222     struct mdio_bus_stat_attr *sattr;
0223     struct dev_ext_attribute *eattr;
0224     u64 val;
0225 
0226     eattr = container_of(attr, struct dev_ext_attribute, attr);
0227     sattr = eattr->var;
0228 
0229     if (sattr->addr < 0)
0230         val = mdio_bus_get_global_stat(bus, sattr->field_offset);
0231     else
0232         val = mdio_bus_get_stat(&bus->stats[sattr->addr],
0233                     sattr->field_offset);
0234 
0235     return sprintf(buf, "%llu\n", val);
0236 }
0237 
0238 static ssize_t mdio_bus_device_stat_field_show(struct device *dev,
0239                            struct device_attribute *attr,
0240                            char *buf)
0241 {
0242     struct mdio_device *mdiodev = to_mdio_device(dev);
0243     struct mii_bus *bus = mdiodev->bus;
0244     struct mdio_bus_stat_attr *sattr;
0245     struct dev_ext_attribute *eattr;
0246     int addr = mdiodev->addr;
0247     u64 val;
0248 
0249     eattr = container_of(attr, struct dev_ext_attribute, attr);
0250     sattr = eattr->var;
0251 
0252     val = mdio_bus_get_stat(&bus->stats[addr], sattr->field_offset);
0253 
0254     return sprintf(buf, "%llu\n", val);
0255 }
0256 
0257 #define MDIO_BUS_STATS_ATTR_DECL(field, file)               \
0258 static struct dev_ext_attribute dev_attr_mdio_bus_##field = {       \
0259     .attr = { .attr = { .name = file, .mode = 0444 },       \
0260              .show = mdio_bus_stat_field_show,          \
0261     },                              \
0262     .var = &((struct mdio_bus_stat_attr) {              \
0263         -1, offsetof(struct mdio_bus_stats, field)      \
0264     }),                             \
0265 };                                  \
0266 static struct dev_ext_attribute dev_attr_mdio_bus_device_##field = {    \
0267     .attr = { .attr = { .name = file, .mode = 0444 },       \
0268              .show = mdio_bus_device_stat_field_show,       \
0269     },                              \
0270     .var = &((struct mdio_bus_stat_attr) {              \
0271         -1, offsetof(struct mdio_bus_stats, field)      \
0272     }),                             \
0273 };
0274 
0275 #define MDIO_BUS_STATS_ATTR(field)                  \
0276     MDIO_BUS_STATS_ATTR_DECL(field, __stringify(field))
0277 
0278 MDIO_BUS_STATS_ATTR(transfers);
0279 MDIO_BUS_STATS_ATTR(errors);
0280 MDIO_BUS_STATS_ATTR(writes);
0281 MDIO_BUS_STATS_ATTR(reads);
0282 
0283 #define MDIO_BUS_STATS_ADDR_ATTR_DECL(field, addr, file)        \
0284 static struct dev_ext_attribute dev_attr_mdio_bus_addr_##field##_##addr = { \
0285     .attr = { .attr = { .name = file, .mode = 0444 },       \
0286              .show = mdio_bus_stat_field_show,          \
0287     },                              \
0288     .var = &((struct mdio_bus_stat_attr) {              \
0289         addr, offsetof(struct mdio_bus_stats, field)        \
0290     }),                             \
0291 }
0292 
0293 #define MDIO_BUS_STATS_ADDR_ATTR(field, addr)               \
0294     MDIO_BUS_STATS_ADDR_ATTR_DECL(field, addr,          \
0295                  __stringify(field) "_" __stringify(addr))
0296 
0297 #define MDIO_BUS_STATS_ADDR_ATTR_GROUP_DECL(addr)           \
0298     MDIO_BUS_STATS_ADDR_ATTR(transfers, addr);          \
0299     MDIO_BUS_STATS_ADDR_ATTR(errors, addr);             \
0300     MDIO_BUS_STATS_ADDR_ATTR(writes, addr);             \
0301     MDIO_BUS_STATS_ADDR_ATTR(reads, addr)               \
0302 
0303 MDIO_BUS_STATS_ADDR_ATTR_GROUP_DECL(0);
0304 MDIO_BUS_STATS_ADDR_ATTR_GROUP_DECL(1);
0305 MDIO_BUS_STATS_ADDR_ATTR_GROUP_DECL(2);
0306 MDIO_BUS_STATS_ADDR_ATTR_GROUP_DECL(3);
0307 MDIO_BUS_STATS_ADDR_ATTR_GROUP_DECL(4);
0308 MDIO_BUS_STATS_ADDR_ATTR_GROUP_DECL(5);
0309 MDIO_BUS_STATS_ADDR_ATTR_GROUP_DECL(6);
0310 MDIO_BUS_STATS_ADDR_ATTR_GROUP_DECL(7);
0311 MDIO_BUS_STATS_ADDR_ATTR_GROUP_DECL(8);
0312 MDIO_BUS_STATS_ADDR_ATTR_GROUP_DECL(9);
0313 MDIO_BUS_STATS_ADDR_ATTR_GROUP_DECL(10);
0314 MDIO_BUS_STATS_ADDR_ATTR_GROUP_DECL(11);
0315 MDIO_BUS_STATS_ADDR_ATTR_GROUP_DECL(12);
0316 MDIO_BUS_STATS_ADDR_ATTR_GROUP_DECL(13);
0317 MDIO_BUS_STATS_ADDR_ATTR_GROUP_DECL(14);
0318 MDIO_BUS_STATS_ADDR_ATTR_GROUP_DECL(15);
0319 MDIO_BUS_STATS_ADDR_ATTR_GROUP_DECL(16);
0320 MDIO_BUS_STATS_ADDR_ATTR_GROUP_DECL(17);
0321 MDIO_BUS_STATS_ADDR_ATTR_GROUP_DECL(18);
0322 MDIO_BUS_STATS_ADDR_ATTR_GROUP_DECL(19);
0323 MDIO_BUS_STATS_ADDR_ATTR_GROUP_DECL(20);
0324 MDIO_BUS_STATS_ADDR_ATTR_GROUP_DECL(21);
0325 MDIO_BUS_STATS_ADDR_ATTR_GROUP_DECL(22);
0326 MDIO_BUS_STATS_ADDR_ATTR_GROUP_DECL(23);
0327 MDIO_BUS_STATS_ADDR_ATTR_GROUP_DECL(24);
0328 MDIO_BUS_STATS_ADDR_ATTR_GROUP_DECL(25);
0329 MDIO_BUS_STATS_ADDR_ATTR_GROUP_DECL(26);
0330 MDIO_BUS_STATS_ADDR_ATTR_GROUP_DECL(27);
0331 MDIO_BUS_STATS_ADDR_ATTR_GROUP_DECL(28);
0332 MDIO_BUS_STATS_ADDR_ATTR_GROUP_DECL(29);
0333 MDIO_BUS_STATS_ADDR_ATTR_GROUP_DECL(30);
0334 MDIO_BUS_STATS_ADDR_ATTR_GROUP_DECL(31);
0335 
0336 #define MDIO_BUS_STATS_ADDR_ATTR_GROUP(addr)                \
0337     &dev_attr_mdio_bus_addr_transfers_##addr.attr.attr,     \
0338     &dev_attr_mdio_bus_addr_errors_##addr.attr.attr,        \
0339     &dev_attr_mdio_bus_addr_writes_##addr.attr.attr,        \
0340     &dev_attr_mdio_bus_addr_reads_##addr.attr.attr          \
0341 
0342 static struct attribute *mdio_bus_statistics_attrs[] = {
0343     &dev_attr_mdio_bus_transfers.attr.attr,
0344     &dev_attr_mdio_bus_errors.attr.attr,
0345     &dev_attr_mdio_bus_writes.attr.attr,
0346     &dev_attr_mdio_bus_reads.attr.attr,
0347     MDIO_BUS_STATS_ADDR_ATTR_GROUP(0),
0348     MDIO_BUS_STATS_ADDR_ATTR_GROUP(1),
0349     MDIO_BUS_STATS_ADDR_ATTR_GROUP(2),
0350     MDIO_BUS_STATS_ADDR_ATTR_GROUP(3),
0351     MDIO_BUS_STATS_ADDR_ATTR_GROUP(4),
0352     MDIO_BUS_STATS_ADDR_ATTR_GROUP(5),
0353     MDIO_BUS_STATS_ADDR_ATTR_GROUP(6),
0354     MDIO_BUS_STATS_ADDR_ATTR_GROUP(7),
0355     MDIO_BUS_STATS_ADDR_ATTR_GROUP(8),
0356     MDIO_BUS_STATS_ADDR_ATTR_GROUP(9),
0357     MDIO_BUS_STATS_ADDR_ATTR_GROUP(10),
0358     MDIO_BUS_STATS_ADDR_ATTR_GROUP(11),
0359     MDIO_BUS_STATS_ADDR_ATTR_GROUP(12),
0360     MDIO_BUS_STATS_ADDR_ATTR_GROUP(13),
0361     MDIO_BUS_STATS_ADDR_ATTR_GROUP(14),
0362     MDIO_BUS_STATS_ADDR_ATTR_GROUP(15),
0363     MDIO_BUS_STATS_ADDR_ATTR_GROUP(16),
0364     MDIO_BUS_STATS_ADDR_ATTR_GROUP(17),
0365     MDIO_BUS_STATS_ADDR_ATTR_GROUP(18),
0366     MDIO_BUS_STATS_ADDR_ATTR_GROUP(19),
0367     MDIO_BUS_STATS_ADDR_ATTR_GROUP(20),
0368     MDIO_BUS_STATS_ADDR_ATTR_GROUP(21),
0369     MDIO_BUS_STATS_ADDR_ATTR_GROUP(22),
0370     MDIO_BUS_STATS_ADDR_ATTR_GROUP(23),
0371     MDIO_BUS_STATS_ADDR_ATTR_GROUP(24),
0372     MDIO_BUS_STATS_ADDR_ATTR_GROUP(25),
0373     MDIO_BUS_STATS_ADDR_ATTR_GROUP(26),
0374     MDIO_BUS_STATS_ADDR_ATTR_GROUP(27),
0375     MDIO_BUS_STATS_ADDR_ATTR_GROUP(28),
0376     MDIO_BUS_STATS_ADDR_ATTR_GROUP(29),
0377     MDIO_BUS_STATS_ADDR_ATTR_GROUP(30),
0378     MDIO_BUS_STATS_ADDR_ATTR_GROUP(31),
0379     NULL,
0380 };
0381 
0382 static const struct attribute_group mdio_bus_statistics_group = {
0383     .name   = "statistics",
0384     .attrs  = mdio_bus_statistics_attrs,
0385 };
0386 
0387 static const struct attribute_group *mdio_bus_groups[] = {
0388     &mdio_bus_statistics_group,
0389     NULL,
0390 };
0391 
0392 static struct class mdio_bus_class = {
0393     .name       = "mdio_bus",
0394     .dev_release    = mdiobus_release,
0395     .dev_groups = mdio_bus_groups,
0396 };
0397 
0398 /**
0399  * mdio_find_bus - Given the name of a mdiobus, find the mii_bus.
0400  * @mdio_name: The name of a mdiobus.
0401  *
0402  * Returns a reference to the mii_bus, or NULL if none found.  The
0403  * embedded struct device will have its reference count incremented,
0404  * and this must be put_deviced'ed once the bus is finished with.
0405  */
0406 struct mii_bus *mdio_find_bus(const char *mdio_name)
0407 {
0408     struct device *d;
0409 
0410     d = class_find_device_by_name(&mdio_bus_class, mdio_name);
0411     return d ? to_mii_bus(d) : NULL;
0412 }
0413 EXPORT_SYMBOL(mdio_find_bus);
0414 
0415 #if IS_ENABLED(CONFIG_OF_MDIO)
0416 /**
0417  * of_mdio_find_bus - Given an mii_bus node, find the mii_bus.
0418  * @mdio_bus_np: Pointer to the mii_bus.
0419  *
0420  * Returns a reference to the mii_bus, or NULL if none found.  The
0421  * embedded struct device will have its reference count incremented,
0422  * and this must be put once the bus is finished with.
0423  *
0424  * Because the association of a device_node and mii_bus is made via
0425  * of_mdiobus_register(), the mii_bus cannot be found before it is
0426  * registered with of_mdiobus_register().
0427  *
0428  */
0429 struct mii_bus *of_mdio_find_bus(struct device_node *mdio_bus_np)
0430 {
0431     struct device *d;
0432 
0433     if (!mdio_bus_np)
0434         return NULL;
0435 
0436     d = class_find_device_by_of_node(&mdio_bus_class, mdio_bus_np);
0437     return d ? to_mii_bus(d) : NULL;
0438 }
0439 EXPORT_SYMBOL(of_mdio_find_bus);
0440 
0441 /* Walk the list of subnodes of a mdio bus and look for a node that
0442  * matches the mdio device's address with its 'reg' property. If
0443  * found, set the of_node pointer for the mdio device. This allows
0444  * auto-probed phy devices to be supplied with information passed in
0445  * via DT.
0446  */
0447 static void of_mdiobus_link_mdiodev(struct mii_bus *bus,
0448                     struct mdio_device *mdiodev)
0449 {
0450     struct device *dev = &mdiodev->dev;
0451     struct device_node *child;
0452 
0453     if (dev->of_node || !bus->dev.of_node)
0454         return;
0455 
0456     for_each_available_child_of_node(bus->dev.of_node, child) {
0457         int addr;
0458 
0459         addr = of_mdio_parse_addr(dev, child);
0460         if (addr < 0)
0461             continue;
0462 
0463         if (addr == mdiodev->addr) {
0464             device_set_node(dev, of_fwnode_handle(child));
0465             /* The refcount on "child" is passed to the mdio
0466              * device. Do _not_ use of_node_put(child) here.
0467              */
0468             return;
0469         }
0470     }
0471 }
0472 #else /* !IS_ENABLED(CONFIG_OF_MDIO) */
0473 static inline void of_mdiobus_link_mdiodev(struct mii_bus *mdio,
0474                        struct mdio_device *mdiodev)
0475 {
0476 }
0477 #endif
0478 
0479 /**
0480  * mdiobus_create_device - create a full MDIO device given
0481  * a mdio_board_info structure
0482  * @bus: MDIO bus to create the devices on
0483  * @bi: mdio_board_info structure describing the devices
0484  *
0485  * Returns 0 on success or < 0 on error.
0486  */
0487 static int mdiobus_create_device(struct mii_bus *bus,
0488                  struct mdio_board_info *bi)
0489 {
0490     struct mdio_device *mdiodev;
0491     int ret = 0;
0492 
0493     mdiodev = mdio_device_create(bus, bi->mdio_addr);
0494     if (IS_ERR(mdiodev))
0495         return -ENODEV;
0496 
0497     strncpy(mdiodev->modalias, bi->modalias,
0498         sizeof(mdiodev->modalias));
0499     mdiodev->bus_match = mdio_device_bus_match;
0500     mdiodev->dev.platform_data = (void *)bi->platform_data;
0501 
0502     ret = mdio_device_register(mdiodev);
0503     if (ret)
0504         mdio_device_free(mdiodev);
0505 
0506     return ret;
0507 }
0508 
0509 /**
0510  * __mdiobus_register - bring up all the PHYs on a given bus and attach them to bus
0511  * @bus: target mii_bus
0512  * @owner: module containing bus accessor functions
0513  *
0514  * Description: Called by a bus driver to bring up all the PHYs
0515  *   on a given bus, and attach them to the bus. Drivers should use
0516  *   mdiobus_register() rather than __mdiobus_register() unless they
0517  *   need to pass a specific owner module. MDIO devices which are not
0518  *   PHYs will not be brought up by this function. They are expected
0519  *   to be explicitly listed in DT and instantiated by of_mdiobus_register().
0520  *
0521  * Returns 0 on success or < 0 on error.
0522  */
0523 int __mdiobus_register(struct mii_bus *bus, struct module *owner)
0524 {
0525     struct mdio_device *mdiodev;
0526     int i, err;
0527     struct gpio_desc *gpiod;
0528 
0529     if (NULL == bus || NULL == bus->name ||
0530         NULL == bus->read || NULL == bus->write)
0531         return -EINVAL;
0532 
0533     if (bus->parent && bus->parent->of_node)
0534         bus->parent->of_node->fwnode.flags |=
0535                     FWNODE_FLAG_NEEDS_CHILD_BOUND_ON_ADD;
0536 
0537     WARN(bus->state != MDIOBUS_ALLOCATED &&
0538          bus->state != MDIOBUS_UNREGISTERED,
0539          "%s: not in ALLOCATED or UNREGISTERED state\n", bus->id);
0540 
0541     bus->owner = owner;
0542     bus->dev.parent = bus->parent;
0543     bus->dev.class = &mdio_bus_class;
0544     bus->dev.groups = NULL;
0545     dev_set_name(&bus->dev, "%s", bus->id);
0546 
0547     /* We need to set state to MDIOBUS_UNREGISTERED to correctly release
0548      * the device in mdiobus_free()
0549      *
0550      * State will be updated later in this function in case of success
0551      */
0552     bus->state = MDIOBUS_UNREGISTERED;
0553 
0554     err = device_register(&bus->dev);
0555     if (err) {
0556         pr_err("mii_bus %s failed to register\n", bus->id);
0557         return -EINVAL;
0558     }
0559 
0560     mutex_init(&bus->mdio_lock);
0561     mutex_init(&bus->shared_lock);
0562 
0563     /* assert bus level PHY GPIO reset */
0564     gpiod = devm_gpiod_get_optional(&bus->dev, "reset", GPIOD_OUT_HIGH);
0565     if (IS_ERR(gpiod)) {
0566         err = dev_err_probe(&bus->dev, PTR_ERR(gpiod),
0567                     "mii_bus %s couldn't get reset GPIO\n",
0568                     bus->id);
0569         device_del(&bus->dev);
0570         return err;
0571     } else  if (gpiod) {
0572         bus->reset_gpiod = gpiod;
0573         fsleep(bus->reset_delay_us);
0574         gpiod_set_value_cansleep(gpiod, 0);
0575         if (bus->reset_post_delay_us > 0)
0576             fsleep(bus->reset_post_delay_us);
0577     }
0578 
0579     if (bus->reset) {
0580         err = bus->reset(bus);
0581         if (err)
0582             goto error_reset_gpiod;
0583     }
0584 
0585     for (i = 0; i < PHY_MAX_ADDR; i++) {
0586         if ((bus->phy_mask & (1 << i)) == 0) {
0587             struct phy_device *phydev;
0588 
0589             phydev = mdiobus_scan(bus, i);
0590             if (IS_ERR(phydev) && (PTR_ERR(phydev) != -ENODEV)) {
0591                 err = PTR_ERR(phydev);
0592                 goto error;
0593             }
0594         }
0595     }
0596 
0597     mdiobus_setup_mdiodev_from_board_info(bus, mdiobus_create_device);
0598 
0599     bus->state = MDIOBUS_REGISTERED;
0600     dev_dbg(&bus->dev, "probed\n");
0601     return 0;
0602 
0603 error:
0604     while (--i >= 0) {
0605         mdiodev = bus->mdio_map[i];
0606         if (!mdiodev)
0607             continue;
0608 
0609         mdiodev->device_remove(mdiodev);
0610         mdiodev->device_free(mdiodev);
0611     }
0612 error_reset_gpiod:
0613     /* Put PHYs in RESET to save power */
0614     if (bus->reset_gpiod)
0615         gpiod_set_value_cansleep(bus->reset_gpiod, 1);
0616 
0617     device_del(&bus->dev);
0618     return err;
0619 }
0620 EXPORT_SYMBOL(__mdiobus_register);
0621 
0622 void mdiobus_unregister(struct mii_bus *bus)
0623 {
0624     struct mdio_device *mdiodev;
0625     int i;
0626 
0627     if (WARN_ON_ONCE(bus->state != MDIOBUS_REGISTERED))
0628         return;
0629     bus->state = MDIOBUS_UNREGISTERED;
0630 
0631     for (i = 0; i < PHY_MAX_ADDR; i++) {
0632         mdiodev = bus->mdio_map[i];
0633         if (!mdiodev)
0634             continue;
0635 
0636         if (mdiodev->reset_gpio)
0637             gpiod_put(mdiodev->reset_gpio);
0638 
0639         mdiodev->device_remove(mdiodev);
0640         mdiodev->device_free(mdiodev);
0641     }
0642 
0643     /* Put PHYs in RESET to save power */
0644     if (bus->reset_gpiod)
0645         gpiod_set_value_cansleep(bus->reset_gpiod, 1);
0646 
0647     device_del(&bus->dev);
0648 }
0649 EXPORT_SYMBOL(mdiobus_unregister);
0650 
0651 /**
0652  * mdiobus_free - free a struct mii_bus
0653  * @bus: mii_bus to free
0654  *
0655  * This function releases the reference to the underlying device
0656  * object in the mii_bus.  If this is the last reference, the mii_bus
0657  * will be freed.
0658  */
0659 void mdiobus_free(struct mii_bus *bus)
0660 {
0661     /* For compatibility with error handling in drivers. */
0662     if (bus->state == MDIOBUS_ALLOCATED) {
0663         kfree(bus);
0664         return;
0665     }
0666 
0667     WARN(bus->state != MDIOBUS_UNREGISTERED,
0668          "%s: not in UNREGISTERED state\n", bus->id);
0669     bus->state = MDIOBUS_RELEASED;
0670 
0671     put_device(&bus->dev);
0672 }
0673 EXPORT_SYMBOL(mdiobus_free);
0674 
0675 /**
0676  * mdiobus_scan - scan a bus for MDIO devices.
0677  * @bus: mii_bus to scan
0678  * @addr: address on bus to scan
0679  *
0680  * This function scans the MDIO bus, looking for devices which can be
0681  * identified using a vendor/product ID in registers 2 and 3. Not all
0682  * MDIO devices have such registers, but PHY devices typically
0683  * do. Hence this function assumes anything found is a PHY, or can be
0684  * treated as a PHY. Other MDIO devices, such as switches, will
0685  * probably not be found during the scan.
0686  */
0687 struct phy_device *mdiobus_scan(struct mii_bus *bus, int addr)
0688 {
0689     struct phy_device *phydev = ERR_PTR(-ENODEV);
0690     int err;
0691 
0692     switch (bus->probe_capabilities) {
0693     case MDIOBUS_NO_CAP:
0694     case MDIOBUS_C22:
0695         phydev = get_phy_device(bus, addr, false);
0696         break;
0697     case MDIOBUS_C45:
0698         phydev = get_phy_device(bus, addr, true);
0699         break;
0700     case MDIOBUS_C22_C45:
0701         phydev = get_phy_device(bus, addr, false);
0702         if (IS_ERR(phydev))
0703             phydev = get_phy_device(bus, addr, true);
0704         break;
0705     }
0706 
0707     if (IS_ERR(phydev))
0708         return phydev;
0709 
0710     /*
0711      * For DT, see if the auto-probed phy has a correspoding child
0712      * in the bus node, and set the of_node pointer in this case.
0713      */
0714     of_mdiobus_link_mdiodev(bus, &phydev->mdio);
0715 
0716     err = phy_device_register(phydev);
0717     if (err) {
0718         phy_device_free(phydev);
0719         return ERR_PTR(-ENODEV);
0720     }
0721 
0722     return phydev;
0723 }
0724 EXPORT_SYMBOL(mdiobus_scan);
0725 
0726 static void mdiobus_stats_acct(struct mdio_bus_stats *stats, bool op, int ret)
0727 {
0728     preempt_disable();
0729     u64_stats_update_begin(&stats->syncp);
0730 
0731     u64_stats_inc(&stats->transfers);
0732     if (ret < 0) {
0733         u64_stats_inc(&stats->errors);
0734         goto out;
0735     }
0736 
0737     if (op)
0738         u64_stats_inc(&stats->reads);
0739     else
0740         u64_stats_inc(&stats->writes);
0741 out:
0742     u64_stats_update_end(&stats->syncp);
0743     preempt_enable();
0744 }
0745 
0746 /**
0747  * __mdiobus_read - Unlocked version of the mdiobus_read function
0748  * @bus: the mii_bus struct
0749  * @addr: the phy address
0750  * @regnum: register number to read
0751  *
0752  * Read a MDIO bus register. Caller must hold the mdio bus lock.
0753  *
0754  * NOTE: MUST NOT be called from interrupt context.
0755  */
0756 int __mdiobus_read(struct mii_bus *bus, int addr, u32 regnum)
0757 {
0758     int retval;
0759 
0760     lockdep_assert_held_once(&bus->mdio_lock);
0761 
0762     retval = bus->read(bus, addr, regnum);
0763 
0764     trace_mdio_access(bus, 1, addr, regnum, retval, retval);
0765     mdiobus_stats_acct(&bus->stats[addr], true, retval);
0766 
0767     return retval;
0768 }
0769 EXPORT_SYMBOL(__mdiobus_read);
0770 
0771 /**
0772  * __mdiobus_write - Unlocked version of the mdiobus_write function
0773  * @bus: the mii_bus struct
0774  * @addr: the phy address
0775  * @regnum: register number to write
0776  * @val: value to write to @regnum
0777  *
0778  * Write a MDIO bus register. Caller must hold the mdio bus lock.
0779  *
0780  * NOTE: MUST NOT be called from interrupt context.
0781  */
0782 int __mdiobus_write(struct mii_bus *bus, int addr, u32 regnum, u16 val)
0783 {
0784     int err;
0785 
0786     lockdep_assert_held_once(&bus->mdio_lock);
0787 
0788     err = bus->write(bus, addr, regnum, val);
0789 
0790     trace_mdio_access(bus, 0, addr, regnum, val, err);
0791     mdiobus_stats_acct(&bus->stats[addr], false, err);
0792 
0793     return err;
0794 }
0795 EXPORT_SYMBOL(__mdiobus_write);
0796 
0797 /**
0798  * __mdiobus_modify_changed - Unlocked version of the mdiobus_modify function
0799  * @bus: the mii_bus struct
0800  * @addr: the phy address
0801  * @regnum: register number to modify
0802  * @mask: bit mask of bits to clear
0803  * @set: bit mask of bits to set
0804  *
0805  * Read, modify, and if any change, write the register value back to the
0806  * device. Any error returns a negative number.
0807  *
0808  * NOTE: MUST NOT be called from interrupt context.
0809  */
0810 int __mdiobus_modify_changed(struct mii_bus *bus, int addr, u32 regnum,
0811                  u16 mask, u16 set)
0812 {
0813     int new, ret;
0814 
0815     ret = __mdiobus_read(bus, addr, regnum);
0816     if (ret < 0)
0817         return ret;
0818 
0819     new = (ret & ~mask) | set;
0820     if (new == ret)
0821         return 0;
0822 
0823     ret = __mdiobus_write(bus, addr, regnum, new);
0824 
0825     return ret < 0 ? ret : 1;
0826 }
0827 EXPORT_SYMBOL_GPL(__mdiobus_modify_changed);
0828 
0829 /**
0830  * mdiobus_read_nested - Nested version of the mdiobus_read function
0831  * @bus: the mii_bus struct
0832  * @addr: the phy address
0833  * @regnum: register number to read
0834  *
0835  * In case of nested MDIO bus access avoid lockdep false positives by
0836  * using mutex_lock_nested().
0837  *
0838  * NOTE: MUST NOT be called from interrupt context,
0839  * because the bus read/write functions may wait for an interrupt
0840  * to conclude the operation.
0841  */
0842 int mdiobus_read_nested(struct mii_bus *bus, int addr, u32 regnum)
0843 {
0844     int retval;
0845 
0846     mutex_lock_nested(&bus->mdio_lock, MDIO_MUTEX_NESTED);
0847     retval = __mdiobus_read(bus, addr, regnum);
0848     mutex_unlock(&bus->mdio_lock);
0849 
0850     return retval;
0851 }
0852 EXPORT_SYMBOL(mdiobus_read_nested);
0853 
0854 /**
0855  * mdiobus_read - Convenience function for reading a given MII mgmt register
0856  * @bus: the mii_bus struct
0857  * @addr: the phy address
0858  * @regnum: register number to read
0859  *
0860  * NOTE: MUST NOT be called from interrupt context,
0861  * because the bus read/write functions may wait for an interrupt
0862  * to conclude the operation.
0863  */
0864 int mdiobus_read(struct mii_bus *bus, int addr, u32 regnum)
0865 {
0866     int retval;
0867 
0868     mutex_lock(&bus->mdio_lock);
0869     retval = __mdiobus_read(bus, addr, regnum);
0870     mutex_unlock(&bus->mdio_lock);
0871 
0872     return retval;
0873 }
0874 EXPORT_SYMBOL(mdiobus_read);
0875 
0876 /**
0877  * mdiobus_write_nested - Nested version of the mdiobus_write function
0878  * @bus: the mii_bus struct
0879  * @addr: the phy address
0880  * @regnum: register number to write
0881  * @val: value to write to @regnum
0882  *
0883  * In case of nested MDIO bus access avoid lockdep false positives by
0884  * using mutex_lock_nested().
0885  *
0886  * NOTE: MUST NOT be called from interrupt context,
0887  * because the bus read/write functions may wait for an interrupt
0888  * to conclude the operation.
0889  */
0890 int mdiobus_write_nested(struct mii_bus *bus, int addr, u32 regnum, u16 val)
0891 {
0892     int err;
0893 
0894     mutex_lock_nested(&bus->mdio_lock, MDIO_MUTEX_NESTED);
0895     err = __mdiobus_write(bus, addr, regnum, val);
0896     mutex_unlock(&bus->mdio_lock);
0897 
0898     return err;
0899 }
0900 EXPORT_SYMBOL(mdiobus_write_nested);
0901 
0902 /**
0903  * mdiobus_write - Convenience function for writing a given MII mgmt register
0904  * @bus: the mii_bus struct
0905  * @addr: the phy address
0906  * @regnum: register number to write
0907  * @val: value to write to @regnum
0908  *
0909  * NOTE: MUST NOT be called from interrupt context,
0910  * because the bus read/write functions may wait for an interrupt
0911  * to conclude the operation.
0912  */
0913 int mdiobus_write(struct mii_bus *bus, int addr, u32 regnum, u16 val)
0914 {
0915     int err;
0916 
0917     mutex_lock(&bus->mdio_lock);
0918     err = __mdiobus_write(bus, addr, regnum, val);
0919     mutex_unlock(&bus->mdio_lock);
0920 
0921     return err;
0922 }
0923 EXPORT_SYMBOL(mdiobus_write);
0924 
0925 /**
0926  * mdiobus_modify - Convenience function for modifying a given mdio device
0927  *  register
0928  * @bus: the mii_bus struct
0929  * @addr: the phy address
0930  * @regnum: register number to write
0931  * @mask: bit mask of bits to clear
0932  * @set: bit mask of bits to set
0933  */
0934 int mdiobus_modify(struct mii_bus *bus, int addr, u32 regnum, u16 mask, u16 set)
0935 {
0936     int err;
0937 
0938     mutex_lock(&bus->mdio_lock);
0939     err = __mdiobus_modify_changed(bus, addr, regnum, mask, set);
0940     mutex_unlock(&bus->mdio_lock);
0941 
0942     return err < 0 ? err : 0;
0943 }
0944 EXPORT_SYMBOL_GPL(mdiobus_modify);
0945 
0946 /**
0947  * mdiobus_modify_changed - Convenience function for modifying a given mdio
0948  *  device register and returning if it changed
0949  * @bus: the mii_bus struct
0950  * @addr: the phy address
0951  * @regnum: register number to write
0952  * @mask: bit mask of bits to clear
0953  * @set: bit mask of bits to set
0954  */
0955 int mdiobus_modify_changed(struct mii_bus *bus, int addr, u32 regnum,
0956                u16 mask, u16 set)
0957 {
0958     int err;
0959 
0960     mutex_lock(&bus->mdio_lock);
0961     err = __mdiobus_modify_changed(bus, addr, regnum, mask, set);
0962     mutex_unlock(&bus->mdio_lock);
0963 
0964     return err;
0965 }
0966 EXPORT_SYMBOL_GPL(mdiobus_modify_changed);
0967 
0968 /**
0969  * mdio_bus_match - determine if given MDIO driver supports the given
0970  *          MDIO device
0971  * @dev: target MDIO device
0972  * @drv: given MDIO driver
0973  *
0974  * Description: Given a MDIO device, and a MDIO driver, return 1 if
0975  *   the driver supports the device.  Otherwise, return 0. This may
0976  *   require calling the devices own match function, since different classes
0977  *   of MDIO devices have different match criteria.
0978  */
0979 static int mdio_bus_match(struct device *dev, struct device_driver *drv)
0980 {
0981     struct mdio_driver *mdiodrv = to_mdio_driver(drv);
0982     struct mdio_device *mdio = to_mdio_device(dev);
0983 
0984     /* Both the driver and device must type-match */
0985     if (!(mdiodrv->mdiodrv.flags & MDIO_DEVICE_IS_PHY) !=
0986         !(mdio->flags & MDIO_DEVICE_FLAG_PHY))
0987         return 0;
0988 
0989     if (of_driver_match_device(dev, drv))
0990         return 1;
0991 
0992     if (mdio->bus_match)
0993         return mdio->bus_match(dev, drv);
0994 
0995     return 0;
0996 }
0997 
0998 static int mdio_uevent(struct device *dev, struct kobj_uevent_env *env)
0999 {
1000     int rc;
1001 
1002     /* Some devices have extra OF data and an OF-style MODALIAS */
1003     rc = of_device_uevent_modalias(dev, env);
1004     if (rc != -ENODEV)
1005         return rc;
1006 
1007     return 0;
1008 }
1009 
1010 static struct attribute *mdio_bus_device_statistics_attrs[] = {
1011     &dev_attr_mdio_bus_device_transfers.attr.attr,
1012     &dev_attr_mdio_bus_device_errors.attr.attr,
1013     &dev_attr_mdio_bus_device_writes.attr.attr,
1014     &dev_attr_mdio_bus_device_reads.attr.attr,
1015     NULL,
1016 };
1017 
1018 static const struct attribute_group mdio_bus_device_statistics_group = {
1019     .name   = "statistics",
1020     .attrs  = mdio_bus_device_statistics_attrs,
1021 };
1022 
1023 static const struct attribute_group *mdio_bus_dev_groups[] = {
1024     &mdio_bus_device_statistics_group,
1025     NULL,
1026 };
1027 
1028 struct bus_type mdio_bus_type = {
1029     .name       = "mdio_bus",
1030     .dev_groups = mdio_bus_dev_groups,
1031     .match      = mdio_bus_match,
1032     .uevent     = mdio_uevent,
1033 };
1034 EXPORT_SYMBOL(mdio_bus_type);
1035 
1036 int __init mdio_bus_init(void)
1037 {
1038     int ret;
1039 
1040     ret = class_register(&mdio_bus_class);
1041     if (!ret) {
1042         ret = bus_register(&mdio_bus_type);
1043         if (ret)
1044             class_unregister(&mdio_bus_class);
1045     }
1046 
1047     return ret;
1048 }
1049 
1050 #if IS_ENABLED(CONFIG_PHYLIB)
1051 void mdio_bus_exit(void)
1052 {
1053     class_unregister(&mdio_bus_class);
1054     bus_unregister(&mdio_bus_type);
1055 }
1056 EXPORT_SYMBOL_GPL(mdio_bus_exit);
1057 #else
1058 module_init(mdio_bus_init);
1059 /* no module_exit, intentional */
1060 MODULE_LICENSE("GPL");
1061 MODULE_DESCRIPTION("MDIO bus/device layer");
1062 #endif