treewide: remove redundant IS_ERR() before error code check
[linux/fpc-iii.git] / drivers / net / phy / mdio_bus.c
blob9bb9f37f21dc3d92d73bde135949b96a3a3ecbfd
1 // SPDX-License-Identifier: GPL-2.0+
2 /* MDIO Bus interface
4 * Author: Andy Fleming
6 * Copyright (c) 2004 Freescale Semiconductor, Inc.
7 */
9 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
11 #include <linux/kernel.h>
12 #include <linux/string.h>
13 #include <linux/errno.h>
14 #include <linux/unistd.h>
15 #include <linux/slab.h>
16 #include <linux/interrupt.h>
17 #include <linux/init.h>
18 #include <linux/delay.h>
19 #include <linux/device.h>
20 #include <linux/gpio.h>
21 #include <linux/gpio/consumer.h>
22 #include <linux/of_device.h>
23 #include <linux/of_mdio.h>
24 #include <linux/of_gpio.h>
25 #include <linux/netdevice.h>
26 #include <linux/etherdevice.h>
27 #include <linux/reset.h>
28 #include <linux/skbuff.h>
29 #include <linux/spinlock.h>
30 #include <linux/mm.h>
31 #include <linux/module.h>
32 #include <linux/mii.h>
33 #include <linux/ethtool.h>
34 #include <linux/phy.h>
35 #include <linux/io.h>
36 #include <linux/uaccess.h>
38 #define CREATE_TRACE_POINTS
39 #include <trace/events/mdio.h>
41 #include "mdio-boardinfo.h"
43 static int mdiobus_register_gpiod(struct mdio_device *mdiodev)
45 int error;
47 /* Deassert the optional reset signal */
48 mdiodev->reset_gpio = gpiod_get_optional(&mdiodev->dev,
49 "reset", GPIOD_OUT_LOW);
50 error = PTR_ERR_OR_ZERO(mdiodev->reset_gpio);
51 if (error)
52 return error;
54 if (mdiodev->reset_gpio)
55 gpiod_set_consumer_name(mdiodev->reset_gpio, "PHY reset");
57 return 0;
60 static int mdiobus_register_reset(struct mdio_device *mdiodev)
62 struct reset_control *reset;
64 reset = reset_control_get_optional_exclusive(&mdiodev->dev, "phy");
65 if (IS_ERR(reset))
66 return PTR_ERR(reset);
68 mdiodev->reset_ctrl = reset;
70 return 0;
73 int mdiobus_register_device(struct mdio_device *mdiodev)
75 int err;
77 if (mdiodev->bus->mdio_map[mdiodev->addr])
78 return -EBUSY;
80 if (mdiodev->flags & MDIO_DEVICE_FLAG_PHY) {
81 err = mdiobus_register_gpiod(mdiodev);
82 if (err)
83 return err;
85 err = mdiobus_register_reset(mdiodev);
86 if (err)
87 return err;
89 /* Assert the reset signal */
90 mdio_device_reset(mdiodev, 1);
93 mdiodev->bus->mdio_map[mdiodev->addr] = mdiodev;
95 return 0;
97 EXPORT_SYMBOL(mdiobus_register_device);
99 int mdiobus_unregister_device(struct mdio_device *mdiodev)
101 if (mdiodev->bus->mdio_map[mdiodev->addr] != mdiodev)
102 return -EINVAL;
104 reset_control_put(mdiodev->reset_ctrl);
106 mdiodev->bus->mdio_map[mdiodev->addr] = NULL;
108 return 0;
110 EXPORT_SYMBOL(mdiobus_unregister_device);
112 struct phy_device *mdiobus_get_phy(struct mii_bus *bus, int addr)
114 struct mdio_device *mdiodev = bus->mdio_map[addr];
116 if (!mdiodev)
117 return NULL;
119 if (!(mdiodev->flags & MDIO_DEVICE_FLAG_PHY))
120 return NULL;
122 return container_of(mdiodev, struct phy_device, mdio);
124 EXPORT_SYMBOL(mdiobus_get_phy);
126 bool mdiobus_is_registered_device(struct mii_bus *bus, int addr)
128 return bus->mdio_map[addr];
130 EXPORT_SYMBOL(mdiobus_is_registered_device);
133 * mdiobus_alloc_size - allocate a mii_bus structure
134 * @size: extra amount of memory to allocate for private storage.
135 * If non-zero, then bus->priv is points to that memory.
137 * Description: called by a bus driver to allocate an mii_bus
138 * structure to fill in.
140 struct mii_bus *mdiobus_alloc_size(size_t size)
142 struct mii_bus *bus;
143 size_t aligned_size = ALIGN(sizeof(*bus), NETDEV_ALIGN);
144 size_t alloc_size;
145 int i;
147 /* If we alloc extra space, it should be aligned */
148 if (size)
149 alloc_size = aligned_size + size;
150 else
151 alloc_size = sizeof(*bus);
153 bus = kzalloc(alloc_size, GFP_KERNEL);
154 if (!bus)
155 return NULL;
157 bus->state = MDIOBUS_ALLOCATED;
158 if (size)
159 bus->priv = (void *)bus + aligned_size;
161 /* Initialise the interrupts to polling and 64-bit seqcounts */
162 for (i = 0; i < PHY_MAX_ADDR; i++) {
163 bus->irq[i] = PHY_POLL;
164 u64_stats_init(&bus->stats[i].syncp);
167 return bus;
169 EXPORT_SYMBOL(mdiobus_alloc_size);
171 static void _devm_mdiobus_free(struct device *dev, void *res)
173 mdiobus_free(*(struct mii_bus **)res);
176 static int devm_mdiobus_match(struct device *dev, void *res, void *data)
178 struct mii_bus **r = res;
180 if (WARN_ON(!r || !*r))
181 return 0;
183 return *r == data;
187 * devm_mdiobus_alloc_size - Resource-managed mdiobus_alloc_size()
188 * @dev: Device to allocate mii_bus for
189 * @sizeof_priv: Space to allocate for private structure.
191 * Managed mdiobus_alloc_size. mii_bus allocated with this function is
192 * automatically freed on driver detach.
194 * If an mii_bus allocated with this function needs to be freed separately,
195 * devm_mdiobus_free() must be used.
197 * RETURNS:
198 * Pointer to allocated mii_bus on success, NULL on failure.
200 struct mii_bus *devm_mdiobus_alloc_size(struct device *dev, int sizeof_priv)
202 struct mii_bus **ptr, *bus;
204 ptr = devres_alloc(_devm_mdiobus_free, sizeof(*ptr), GFP_KERNEL);
205 if (!ptr)
206 return NULL;
208 /* use raw alloc_dr for kmalloc caller tracing */
209 bus = mdiobus_alloc_size(sizeof_priv);
210 if (bus) {
211 *ptr = bus;
212 devres_add(dev, ptr);
213 } else {
214 devres_free(ptr);
217 return bus;
219 EXPORT_SYMBOL_GPL(devm_mdiobus_alloc_size);
222 * devm_mdiobus_free - Resource-managed mdiobus_free()
223 * @dev: Device this mii_bus belongs to
224 * @bus: the mii_bus associated with the device
226 * Free mii_bus allocated with devm_mdiobus_alloc_size().
228 void devm_mdiobus_free(struct device *dev, struct mii_bus *bus)
230 int rc;
232 rc = devres_release(dev, _devm_mdiobus_free,
233 devm_mdiobus_match, bus);
234 WARN_ON(rc);
236 EXPORT_SYMBOL_GPL(devm_mdiobus_free);
239 * mdiobus_release - mii_bus device release callback
240 * @d: the target struct device that contains the mii_bus
242 * Description: called when the last reference to an mii_bus is
243 * dropped, to free the underlying memory.
245 static void mdiobus_release(struct device *d)
247 struct mii_bus *bus = to_mii_bus(d);
248 BUG_ON(bus->state != MDIOBUS_RELEASED &&
249 /* for compatibility with error handling in drivers */
250 bus->state != MDIOBUS_ALLOCATED);
251 kfree(bus);
254 struct mdio_bus_stat_attr {
255 int addr;
256 unsigned int field_offset;
259 static u64 mdio_bus_get_stat(struct mdio_bus_stats *s, unsigned int offset)
261 const char *p = (const char *)s + offset;
262 unsigned int start;
263 u64 val = 0;
265 do {
266 start = u64_stats_fetch_begin(&s->syncp);
267 val = u64_stats_read((const u64_stats_t *)p);
268 } while (u64_stats_fetch_retry(&s->syncp, start));
270 return val;
273 static u64 mdio_bus_get_global_stat(struct mii_bus *bus, unsigned int offset)
275 unsigned int i;
276 u64 val = 0;
278 for (i = 0; i < PHY_MAX_ADDR; i++)
279 val += mdio_bus_get_stat(&bus->stats[i], offset);
281 return val;
284 static ssize_t mdio_bus_stat_field_show(struct device *dev,
285 struct device_attribute *attr,
286 char *buf)
288 struct mii_bus *bus = to_mii_bus(dev);
289 struct mdio_bus_stat_attr *sattr;
290 struct dev_ext_attribute *eattr;
291 u64 val;
293 eattr = container_of(attr, struct dev_ext_attribute, attr);
294 sattr = eattr->var;
296 if (sattr->addr < 0)
297 val = mdio_bus_get_global_stat(bus, sattr->field_offset);
298 else
299 val = mdio_bus_get_stat(&bus->stats[sattr->addr],
300 sattr->field_offset);
302 return sprintf(buf, "%llu\n", val);
305 static ssize_t mdio_bus_device_stat_field_show(struct device *dev,
306 struct device_attribute *attr,
307 char *buf)
309 struct mdio_device *mdiodev = to_mdio_device(dev);
310 struct mii_bus *bus = mdiodev->bus;
311 struct mdio_bus_stat_attr *sattr;
312 struct dev_ext_attribute *eattr;
313 int addr = mdiodev->addr;
314 u64 val;
316 eattr = container_of(attr, struct dev_ext_attribute, attr);
317 sattr = eattr->var;
319 val = mdio_bus_get_stat(&bus->stats[addr], sattr->field_offset);
321 return sprintf(buf, "%llu\n", val);
324 #define MDIO_BUS_STATS_ATTR_DECL(field, file) \
325 static struct dev_ext_attribute dev_attr_mdio_bus_##field = { \
326 .attr = { .attr = { .name = file, .mode = 0444 }, \
327 .show = mdio_bus_stat_field_show, \
328 }, \
329 .var = &((struct mdio_bus_stat_attr) { \
330 -1, offsetof(struct mdio_bus_stats, field) \
331 }), \
332 }; \
333 static struct dev_ext_attribute dev_attr_mdio_bus_device_##field = { \
334 .attr = { .attr = { .name = file, .mode = 0444 }, \
335 .show = mdio_bus_device_stat_field_show, \
336 }, \
337 .var = &((struct mdio_bus_stat_attr) { \
338 -1, offsetof(struct mdio_bus_stats, field) \
339 }), \
342 #define MDIO_BUS_STATS_ATTR(field) \
343 MDIO_BUS_STATS_ATTR_DECL(field, __stringify(field))
345 MDIO_BUS_STATS_ATTR(transfers);
346 MDIO_BUS_STATS_ATTR(errors);
347 MDIO_BUS_STATS_ATTR(writes);
348 MDIO_BUS_STATS_ATTR(reads);
350 #define MDIO_BUS_STATS_ADDR_ATTR_DECL(field, addr, file) \
351 static struct dev_ext_attribute dev_attr_mdio_bus_addr_##field##_##addr = { \
352 .attr = { .attr = { .name = file, .mode = 0444 }, \
353 .show = mdio_bus_stat_field_show, \
354 }, \
355 .var = &((struct mdio_bus_stat_attr) { \
356 addr, offsetof(struct mdio_bus_stats, field) \
357 }), \
360 #define MDIO_BUS_STATS_ADDR_ATTR(field, addr) \
361 MDIO_BUS_STATS_ADDR_ATTR_DECL(field, addr, \
362 __stringify(field) "_" __stringify(addr))
364 #define MDIO_BUS_STATS_ADDR_ATTR_GROUP_DECL(addr) \
365 MDIO_BUS_STATS_ADDR_ATTR(transfers, addr); \
366 MDIO_BUS_STATS_ADDR_ATTR(errors, addr); \
367 MDIO_BUS_STATS_ADDR_ATTR(writes, addr); \
368 MDIO_BUS_STATS_ADDR_ATTR(reads, addr) \
370 MDIO_BUS_STATS_ADDR_ATTR_GROUP_DECL(0);
371 MDIO_BUS_STATS_ADDR_ATTR_GROUP_DECL(1);
372 MDIO_BUS_STATS_ADDR_ATTR_GROUP_DECL(2);
373 MDIO_BUS_STATS_ADDR_ATTR_GROUP_DECL(3);
374 MDIO_BUS_STATS_ADDR_ATTR_GROUP_DECL(4);
375 MDIO_BUS_STATS_ADDR_ATTR_GROUP_DECL(5);
376 MDIO_BUS_STATS_ADDR_ATTR_GROUP_DECL(6);
377 MDIO_BUS_STATS_ADDR_ATTR_GROUP_DECL(7);
378 MDIO_BUS_STATS_ADDR_ATTR_GROUP_DECL(8);
379 MDIO_BUS_STATS_ADDR_ATTR_GROUP_DECL(9);
380 MDIO_BUS_STATS_ADDR_ATTR_GROUP_DECL(10);
381 MDIO_BUS_STATS_ADDR_ATTR_GROUP_DECL(11);
382 MDIO_BUS_STATS_ADDR_ATTR_GROUP_DECL(12);
383 MDIO_BUS_STATS_ADDR_ATTR_GROUP_DECL(13);
384 MDIO_BUS_STATS_ADDR_ATTR_GROUP_DECL(14);
385 MDIO_BUS_STATS_ADDR_ATTR_GROUP_DECL(15);
386 MDIO_BUS_STATS_ADDR_ATTR_GROUP_DECL(16);
387 MDIO_BUS_STATS_ADDR_ATTR_GROUP_DECL(17);
388 MDIO_BUS_STATS_ADDR_ATTR_GROUP_DECL(18);
389 MDIO_BUS_STATS_ADDR_ATTR_GROUP_DECL(19);
390 MDIO_BUS_STATS_ADDR_ATTR_GROUP_DECL(20);
391 MDIO_BUS_STATS_ADDR_ATTR_GROUP_DECL(21);
392 MDIO_BUS_STATS_ADDR_ATTR_GROUP_DECL(22);
393 MDIO_BUS_STATS_ADDR_ATTR_GROUP_DECL(23);
394 MDIO_BUS_STATS_ADDR_ATTR_GROUP_DECL(24);
395 MDIO_BUS_STATS_ADDR_ATTR_GROUP_DECL(25);
396 MDIO_BUS_STATS_ADDR_ATTR_GROUP_DECL(26);
397 MDIO_BUS_STATS_ADDR_ATTR_GROUP_DECL(27);
398 MDIO_BUS_STATS_ADDR_ATTR_GROUP_DECL(28);
399 MDIO_BUS_STATS_ADDR_ATTR_GROUP_DECL(29);
400 MDIO_BUS_STATS_ADDR_ATTR_GROUP_DECL(30);
401 MDIO_BUS_STATS_ADDR_ATTR_GROUP_DECL(31);
403 #define MDIO_BUS_STATS_ADDR_ATTR_GROUP(addr) \
404 &dev_attr_mdio_bus_addr_transfers_##addr.attr.attr, \
405 &dev_attr_mdio_bus_addr_errors_##addr.attr.attr, \
406 &dev_attr_mdio_bus_addr_writes_##addr.attr.attr, \
407 &dev_attr_mdio_bus_addr_reads_##addr.attr.attr \
409 static struct attribute *mdio_bus_statistics_attrs[] = {
410 &dev_attr_mdio_bus_transfers.attr.attr,
411 &dev_attr_mdio_bus_errors.attr.attr,
412 &dev_attr_mdio_bus_writes.attr.attr,
413 &dev_attr_mdio_bus_reads.attr.attr,
414 MDIO_BUS_STATS_ADDR_ATTR_GROUP(0),
415 MDIO_BUS_STATS_ADDR_ATTR_GROUP(1),
416 MDIO_BUS_STATS_ADDR_ATTR_GROUP(2),
417 MDIO_BUS_STATS_ADDR_ATTR_GROUP(3),
418 MDIO_BUS_STATS_ADDR_ATTR_GROUP(4),
419 MDIO_BUS_STATS_ADDR_ATTR_GROUP(5),
420 MDIO_BUS_STATS_ADDR_ATTR_GROUP(6),
421 MDIO_BUS_STATS_ADDR_ATTR_GROUP(7),
422 MDIO_BUS_STATS_ADDR_ATTR_GROUP(8),
423 MDIO_BUS_STATS_ADDR_ATTR_GROUP(9),
424 MDIO_BUS_STATS_ADDR_ATTR_GROUP(10),
425 MDIO_BUS_STATS_ADDR_ATTR_GROUP(11),
426 MDIO_BUS_STATS_ADDR_ATTR_GROUP(12),
427 MDIO_BUS_STATS_ADDR_ATTR_GROUP(13),
428 MDIO_BUS_STATS_ADDR_ATTR_GROUP(14),
429 MDIO_BUS_STATS_ADDR_ATTR_GROUP(15),
430 MDIO_BUS_STATS_ADDR_ATTR_GROUP(16),
431 MDIO_BUS_STATS_ADDR_ATTR_GROUP(17),
432 MDIO_BUS_STATS_ADDR_ATTR_GROUP(18),
433 MDIO_BUS_STATS_ADDR_ATTR_GROUP(19),
434 MDIO_BUS_STATS_ADDR_ATTR_GROUP(20),
435 MDIO_BUS_STATS_ADDR_ATTR_GROUP(21),
436 MDIO_BUS_STATS_ADDR_ATTR_GROUP(22),
437 MDIO_BUS_STATS_ADDR_ATTR_GROUP(23),
438 MDIO_BUS_STATS_ADDR_ATTR_GROUP(24),
439 MDIO_BUS_STATS_ADDR_ATTR_GROUP(25),
440 MDIO_BUS_STATS_ADDR_ATTR_GROUP(26),
441 MDIO_BUS_STATS_ADDR_ATTR_GROUP(27),
442 MDIO_BUS_STATS_ADDR_ATTR_GROUP(28),
443 MDIO_BUS_STATS_ADDR_ATTR_GROUP(29),
444 MDIO_BUS_STATS_ADDR_ATTR_GROUP(30),
445 MDIO_BUS_STATS_ADDR_ATTR_GROUP(31),
446 NULL,
449 static const struct attribute_group mdio_bus_statistics_group = {
450 .name = "statistics",
451 .attrs = mdio_bus_statistics_attrs,
454 static const struct attribute_group *mdio_bus_groups[] = {
455 &mdio_bus_statistics_group,
456 NULL,
459 static struct class mdio_bus_class = {
460 .name = "mdio_bus",
461 .dev_release = mdiobus_release,
462 .dev_groups = mdio_bus_groups,
465 #if IS_ENABLED(CONFIG_OF_MDIO)
467 * of_mdio_find_bus - Given an mii_bus node, find the mii_bus.
468 * @mdio_bus_np: Pointer to the mii_bus.
470 * Returns a reference to the mii_bus, or NULL if none found. The
471 * embedded struct device will have its reference count incremented,
472 * and this must be put once the bus is finished with.
474 * Because the association of a device_node and mii_bus is made via
475 * of_mdiobus_register(), the mii_bus cannot be found before it is
476 * registered with of_mdiobus_register().
479 struct mii_bus *of_mdio_find_bus(struct device_node *mdio_bus_np)
481 struct device *d;
483 if (!mdio_bus_np)
484 return NULL;
486 d = class_find_device_by_of_node(&mdio_bus_class, mdio_bus_np);
487 return d ? to_mii_bus(d) : NULL;
489 EXPORT_SYMBOL(of_mdio_find_bus);
491 /* Walk the list of subnodes of a mdio bus and look for a node that
492 * matches the mdio device's address with its 'reg' property. If
493 * found, set the of_node pointer for the mdio device. This allows
494 * auto-probed phy devices to be supplied with information passed in
495 * via DT.
497 static void of_mdiobus_link_mdiodev(struct mii_bus *bus,
498 struct mdio_device *mdiodev)
500 struct device *dev = &mdiodev->dev;
501 struct device_node *child;
503 if (dev->of_node || !bus->dev.of_node)
504 return;
506 for_each_available_child_of_node(bus->dev.of_node, child) {
507 int addr;
509 addr = of_mdio_parse_addr(dev, child);
510 if (addr < 0)
511 continue;
513 if (addr == mdiodev->addr) {
514 dev->of_node = child;
515 dev->fwnode = of_fwnode_handle(child);
516 return;
520 #else /* !IS_ENABLED(CONFIG_OF_MDIO) */
521 static inline void of_mdiobus_link_mdiodev(struct mii_bus *mdio,
522 struct mdio_device *mdiodev)
525 #endif
528 * mdiobus_create_device_from_board_info - create a full MDIO device given
529 * a mdio_board_info structure
530 * @bus: MDIO bus to create the devices on
531 * @bi: mdio_board_info structure describing the devices
533 * Returns 0 on success or < 0 on error.
535 static int mdiobus_create_device(struct mii_bus *bus,
536 struct mdio_board_info *bi)
538 struct mdio_device *mdiodev;
539 int ret = 0;
541 mdiodev = mdio_device_create(bus, bi->mdio_addr);
542 if (IS_ERR(mdiodev))
543 return -ENODEV;
545 strncpy(mdiodev->modalias, bi->modalias,
546 sizeof(mdiodev->modalias));
547 mdiodev->bus_match = mdio_device_bus_match;
548 mdiodev->dev.platform_data = (void *)bi->platform_data;
550 ret = mdio_device_register(mdiodev);
551 if (ret)
552 mdio_device_free(mdiodev);
554 return ret;
558 * __mdiobus_register - bring up all the PHYs on a given bus and attach them to bus
559 * @bus: target mii_bus
560 * @owner: module containing bus accessor functions
562 * Description: Called by a bus driver to bring up all the PHYs
563 * on a given bus, and attach them to the bus. Drivers should use
564 * mdiobus_register() rather than __mdiobus_register() unless they
565 * need to pass a specific owner module. MDIO devices which are not
566 * PHYs will not be brought up by this function. They are expected to
567 * to be explicitly listed in DT and instantiated by of_mdiobus_register().
569 * Returns 0 on success or < 0 on error.
571 int __mdiobus_register(struct mii_bus *bus, struct module *owner)
573 struct mdio_device *mdiodev;
574 int i, err;
575 struct gpio_desc *gpiod;
577 if (NULL == bus || NULL == bus->name ||
578 NULL == bus->read || NULL == bus->write)
579 return -EINVAL;
581 BUG_ON(bus->state != MDIOBUS_ALLOCATED &&
582 bus->state != MDIOBUS_UNREGISTERED);
584 bus->owner = owner;
585 bus->dev.parent = bus->parent;
586 bus->dev.class = &mdio_bus_class;
587 bus->dev.groups = NULL;
588 dev_set_name(&bus->dev, "%s", bus->id);
590 err = device_register(&bus->dev);
591 if (err) {
592 pr_err("mii_bus %s failed to register\n", bus->id);
593 return -EINVAL;
596 mutex_init(&bus->mdio_lock);
598 /* de-assert bus level PHY GPIO reset */
599 gpiod = devm_gpiod_get_optional(&bus->dev, "reset", GPIOD_OUT_LOW);
600 if (IS_ERR(gpiod)) {
601 dev_err(&bus->dev, "mii_bus %s couldn't get reset GPIO\n",
602 bus->id);
603 device_del(&bus->dev);
604 return PTR_ERR(gpiod);
605 } else if (gpiod) {
606 bus->reset_gpiod = gpiod;
608 gpiod_set_value_cansleep(gpiod, 1);
609 udelay(bus->reset_delay_us);
610 gpiod_set_value_cansleep(gpiod, 0);
613 if (bus->reset)
614 bus->reset(bus);
616 for (i = 0; i < PHY_MAX_ADDR; i++) {
617 if ((bus->phy_mask & (1 << i)) == 0) {
618 struct phy_device *phydev;
620 phydev = mdiobus_scan(bus, i);
621 if (IS_ERR(phydev) && (PTR_ERR(phydev) != -ENODEV)) {
622 err = PTR_ERR(phydev);
623 goto error;
628 mdiobus_setup_mdiodev_from_board_info(bus, mdiobus_create_device);
630 bus->state = MDIOBUS_REGISTERED;
631 pr_info("%s: probed\n", bus->name);
632 return 0;
634 error:
635 while (--i >= 0) {
636 mdiodev = bus->mdio_map[i];
637 if (!mdiodev)
638 continue;
640 mdiodev->device_remove(mdiodev);
641 mdiodev->device_free(mdiodev);
644 /* Put PHYs in RESET to save power */
645 if (bus->reset_gpiod)
646 gpiod_set_value_cansleep(bus->reset_gpiod, 1);
648 device_del(&bus->dev);
649 return err;
651 EXPORT_SYMBOL(__mdiobus_register);
653 void mdiobus_unregister(struct mii_bus *bus)
655 struct mdio_device *mdiodev;
656 int i;
658 BUG_ON(bus->state != MDIOBUS_REGISTERED);
659 bus->state = MDIOBUS_UNREGISTERED;
661 for (i = 0; i < PHY_MAX_ADDR; i++) {
662 mdiodev = bus->mdio_map[i];
663 if (!mdiodev)
664 continue;
666 if (mdiodev->reset_gpio)
667 gpiod_put(mdiodev->reset_gpio);
669 mdiodev->device_remove(mdiodev);
670 mdiodev->device_free(mdiodev);
673 /* Put PHYs in RESET to save power */
674 if (bus->reset_gpiod)
675 gpiod_set_value_cansleep(bus->reset_gpiod, 1);
677 device_del(&bus->dev);
679 EXPORT_SYMBOL(mdiobus_unregister);
682 * mdiobus_free - free a struct mii_bus
683 * @bus: mii_bus to free
685 * This function releases the reference to the underlying device
686 * object in the mii_bus. If this is the last reference, the mii_bus
687 * will be freed.
689 void mdiobus_free(struct mii_bus *bus)
691 /* For compatibility with error handling in drivers. */
692 if (bus->state == MDIOBUS_ALLOCATED) {
693 kfree(bus);
694 return;
697 BUG_ON(bus->state != MDIOBUS_UNREGISTERED);
698 bus->state = MDIOBUS_RELEASED;
700 put_device(&bus->dev);
702 EXPORT_SYMBOL(mdiobus_free);
705 * mdiobus_scan - scan a bus for MDIO devices.
706 * @bus: mii_bus to scan
707 * @addr: address on bus to scan
709 * This function scans the MDIO bus, looking for devices which can be
710 * identified using a vendor/product ID in registers 2 and 3. Not all
711 * MDIO devices have such registers, but PHY devices typically
712 * do. Hence this function assumes anything found is a PHY, or can be
713 * treated as a PHY. Other MDIO devices, such as switches, will
714 * probably not be found during the scan.
716 struct phy_device *mdiobus_scan(struct mii_bus *bus, int addr)
718 struct phy_device *phydev;
719 int err;
721 phydev = get_phy_device(bus, addr, false);
722 if (IS_ERR(phydev))
723 return phydev;
726 * For DT, see if the auto-probed phy has a correspoding child
727 * in the bus node, and set the of_node pointer in this case.
729 of_mdiobus_link_mdiodev(bus, &phydev->mdio);
731 err = phy_device_register(phydev);
732 if (err) {
733 phy_device_free(phydev);
734 return ERR_PTR(-ENODEV);
737 return phydev;
739 EXPORT_SYMBOL(mdiobus_scan);
741 static void mdiobus_stats_acct(struct mdio_bus_stats *stats, bool op, int ret)
743 u64_stats_update_begin(&stats->syncp);
745 u64_stats_inc(&stats->transfers);
746 if (ret < 0) {
747 u64_stats_inc(&stats->errors);
748 goto out;
751 if (op)
752 u64_stats_inc(&stats->reads);
753 else
754 u64_stats_inc(&stats->writes);
755 out:
756 u64_stats_update_end(&stats->syncp);
760 * __mdiobus_read - Unlocked version of the mdiobus_read function
761 * @bus: the mii_bus struct
762 * @addr: the phy address
763 * @regnum: register number to read
765 * Read a MDIO bus register. Caller must hold the mdio bus lock.
767 * NOTE: MUST NOT be called from interrupt context.
769 int __mdiobus_read(struct mii_bus *bus, int addr, u32 regnum)
771 int retval;
773 WARN_ON_ONCE(!mutex_is_locked(&bus->mdio_lock));
775 retval = bus->read(bus, addr, regnum);
777 trace_mdio_access(bus, 1, addr, regnum, retval, retval);
778 mdiobus_stats_acct(&bus->stats[addr], true, retval);
780 return retval;
782 EXPORT_SYMBOL(__mdiobus_read);
785 * __mdiobus_write - Unlocked version of the mdiobus_write function
786 * @bus: the mii_bus struct
787 * @addr: the phy address
788 * @regnum: register number to write
789 * @val: value to write to @regnum
791 * Write a MDIO bus register. Caller must hold the mdio bus lock.
793 * NOTE: MUST NOT be called from interrupt context.
795 int __mdiobus_write(struct mii_bus *bus, int addr, u32 regnum, u16 val)
797 int err;
799 WARN_ON_ONCE(!mutex_is_locked(&bus->mdio_lock));
801 err = bus->write(bus, addr, regnum, val);
803 trace_mdio_access(bus, 0, addr, regnum, val, err);
804 mdiobus_stats_acct(&bus->stats[addr], false, err);
806 return err;
808 EXPORT_SYMBOL(__mdiobus_write);
811 * mdiobus_read_nested - Nested version of the mdiobus_read function
812 * @bus: the mii_bus struct
813 * @addr: the phy address
814 * @regnum: register number to read
816 * In case of nested MDIO bus access avoid lockdep false positives by
817 * using mutex_lock_nested().
819 * NOTE: MUST NOT be called from interrupt context,
820 * because the bus read/write functions may wait for an interrupt
821 * to conclude the operation.
823 int mdiobus_read_nested(struct mii_bus *bus, int addr, u32 regnum)
825 int retval;
827 BUG_ON(in_interrupt());
829 mutex_lock_nested(&bus->mdio_lock, MDIO_MUTEX_NESTED);
830 retval = __mdiobus_read(bus, addr, regnum);
831 mutex_unlock(&bus->mdio_lock);
833 return retval;
835 EXPORT_SYMBOL(mdiobus_read_nested);
838 * mdiobus_read - Convenience function for reading a given MII mgmt register
839 * @bus: the mii_bus struct
840 * @addr: the phy address
841 * @regnum: register number to read
843 * NOTE: MUST NOT be called from interrupt context,
844 * because the bus read/write functions may wait for an interrupt
845 * to conclude the operation.
847 int mdiobus_read(struct mii_bus *bus, int addr, u32 regnum)
849 int retval;
851 BUG_ON(in_interrupt());
853 mutex_lock(&bus->mdio_lock);
854 retval = __mdiobus_read(bus, addr, regnum);
855 mutex_unlock(&bus->mdio_lock);
857 return retval;
859 EXPORT_SYMBOL(mdiobus_read);
862 * mdiobus_write_nested - Nested version of the mdiobus_write function
863 * @bus: the mii_bus struct
864 * @addr: the phy address
865 * @regnum: register number to write
866 * @val: value to write to @regnum
868 * In case of nested MDIO bus access avoid lockdep false positives by
869 * using mutex_lock_nested().
871 * NOTE: MUST NOT be called from interrupt context,
872 * because the bus read/write functions may wait for an interrupt
873 * to conclude the operation.
875 int mdiobus_write_nested(struct mii_bus *bus, int addr, u32 regnum, u16 val)
877 int err;
879 BUG_ON(in_interrupt());
881 mutex_lock_nested(&bus->mdio_lock, MDIO_MUTEX_NESTED);
882 err = __mdiobus_write(bus, addr, regnum, val);
883 mutex_unlock(&bus->mdio_lock);
885 return err;
887 EXPORT_SYMBOL(mdiobus_write_nested);
890 * mdiobus_write - Convenience function for writing a given MII mgmt register
891 * @bus: the mii_bus struct
892 * @addr: the phy address
893 * @regnum: register number to write
894 * @val: value to write to @regnum
896 * NOTE: MUST NOT be called from interrupt context,
897 * because the bus read/write functions may wait for an interrupt
898 * to conclude the operation.
900 int mdiobus_write(struct mii_bus *bus, int addr, u32 regnum, u16 val)
902 int err;
904 BUG_ON(in_interrupt());
906 mutex_lock(&bus->mdio_lock);
907 err = __mdiobus_write(bus, addr, regnum, val);
908 mutex_unlock(&bus->mdio_lock);
910 return err;
912 EXPORT_SYMBOL(mdiobus_write);
915 * mdio_bus_match - determine if given MDIO driver supports the given
916 * MDIO device
917 * @dev: target MDIO device
918 * @drv: given MDIO driver
920 * Description: Given a MDIO device, and a MDIO driver, return 1 if
921 * the driver supports the device. Otherwise, return 0. This may
922 * require calling the devices own match function, since different classes
923 * of MDIO devices have different match criteria.
925 static int mdio_bus_match(struct device *dev, struct device_driver *drv)
927 struct mdio_device *mdio = to_mdio_device(dev);
929 if (of_driver_match_device(dev, drv))
930 return 1;
932 if (mdio->bus_match)
933 return mdio->bus_match(dev, drv);
935 return 0;
938 static int mdio_uevent(struct device *dev, struct kobj_uevent_env *env)
940 int rc;
942 /* Some devices have extra OF data and an OF-style MODALIAS */
943 rc = of_device_uevent_modalias(dev, env);
944 if (rc != -ENODEV)
945 return rc;
947 return 0;
950 static struct attribute *mdio_bus_device_statistics_attrs[] = {
951 &dev_attr_mdio_bus_device_transfers.attr.attr,
952 &dev_attr_mdio_bus_device_errors.attr.attr,
953 &dev_attr_mdio_bus_device_writes.attr.attr,
954 &dev_attr_mdio_bus_device_reads.attr.attr,
955 NULL,
958 static const struct attribute_group mdio_bus_device_statistics_group = {
959 .name = "statistics",
960 .attrs = mdio_bus_device_statistics_attrs,
963 static const struct attribute_group *mdio_bus_dev_groups[] = {
964 &mdio_bus_device_statistics_group,
965 NULL,
968 struct bus_type mdio_bus_type = {
969 .name = "mdio_bus",
970 .dev_groups = mdio_bus_dev_groups,
971 .match = mdio_bus_match,
972 .uevent = mdio_uevent,
974 EXPORT_SYMBOL(mdio_bus_type);
976 int __init mdio_bus_init(void)
978 int ret;
980 ret = class_register(&mdio_bus_class);
981 if (!ret) {
982 ret = bus_register(&mdio_bus_type);
983 if (ret)
984 class_unregister(&mdio_bus_class);
987 return ret;
989 EXPORT_SYMBOL_GPL(mdio_bus_init);
991 #if IS_ENABLED(CONFIG_PHYLIB)
992 void mdio_bus_exit(void)
994 class_unregister(&mdio_bus_class);
995 bus_unregister(&mdio_bus_type);
997 EXPORT_SYMBOL_GPL(mdio_bus_exit);
998 #else
999 module_init(mdio_bus_init);
1000 /* no module_exit, intentional */
1001 MODULE_LICENSE("GPL");
1002 MODULE_DESCRIPTION("MDIO bus/device layer");
1003 #endif