1
2
3
4
5
6
7
8
9#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
10
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>
37
38#define CREATE_TRACE_POINTS
39#include <trace/events/mdio.h>
40
41#include "mdio-boardinfo.h"
42
43static int mdiobus_register_gpiod(struct mdio_device *mdiodev)
44{
45
46 mdiodev->reset_gpio = gpiod_get_optional(&mdiodev->dev,
47 "reset", GPIOD_OUT_LOW);
48 if (IS_ERR(mdiodev->reset_gpio))
49 return PTR_ERR(mdiodev->reset_gpio);
50
51 if (mdiodev->reset_gpio)
52 gpiod_set_consumer_name(mdiodev->reset_gpio, "PHY reset");
53
54 return 0;
55}
56
57static int mdiobus_register_reset(struct mdio_device *mdiodev)
58{
59 struct reset_control *reset;
60
61 reset = reset_control_get_optional_exclusive(&mdiodev->dev, "phy");
62 if (IS_ERR(reset))
63 return PTR_ERR(reset);
64
65 mdiodev->reset_ctrl = reset;
66
67 return 0;
68}
69
70int mdiobus_register_device(struct mdio_device *mdiodev)
71{
72 int err;
73
74 if (mdiodev->bus->mdio_map[mdiodev->addr])
75 return -EBUSY;
76
77 if (mdiodev->flags & MDIO_DEVICE_FLAG_PHY) {
78 err = mdiobus_register_gpiod(mdiodev);
79 if (err)
80 return err;
81
82 err = mdiobus_register_reset(mdiodev);
83 if (err)
84 return err;
85
86
87 mdio_device_reset(mdiodev, 1);
88 }
89
90 mdiodev->bus->mdio_map[mdiodev->addr] = mdiodev;
91
92 return 0;
93}
94EXPORT_SYMBOL(mdiobus_register_device);
95
96int mdiobus_unregister_device(struct mdio_device *mdiodev)
97{
98 if (mdiodev->bus->mdio_map[mdiodev->addr] != mdiodev)
99 return -EINVAL;
100
101 reset_control_put(mdiodev->reset_ctrl);
102
103 mdiodev->bus->mdio_map[mdiodev->addr] = NULL;
104
105 return 0;
106}
107EXPORT_SYMBOL(mdiobus_unregister_device);
108
109struct phy_device *mdiobus_get_phy(struct mii_bus *bus, int addr)
110{
111 struct mdio_device *mdiodev = bus->mdio_map[addr];
112
113 if (!mdiodev)
114 return NULL;
115
116 if (!(mdiodev->flags & MDIO_DEVICE_FLAG_PHY))
117 return NULL;
118
119 return container_of(mdiodev, struct phy_device, mdio);
120}
121EXPORT_SYMBOL(mdiobus_get_phy);
122
123bool mdiobus_is_registered_device(struct mii_bus *bus, int addr)
124{
125 return bus->mdio_map[addr];
126}
127EXPORT_SYMBOL(mdiobus_is_registered_device);
128
129
130
131
132
133
134
135
136
137struct mii_bus *mdiobus_alloc_size(size_t size)
138{
139 struct mii_bus *bus;
140 size_t aligned_size = ALIGN(sizeof(*bus), NETDEV_ALIGN);
141 size_t alloc_size;
142 int i;
143
144
145 if (size)
146 alloc_size = aligned_size + size;
147 else
148 alloc_size = sizeof(*bus);
149
150 bus = kzalloc(alloc_size, GFP_KERNEL);
151 if (!bus)
152 return NULL;
153
154 bus->state = MDIOBUS_ALLOCATED;
155 if (size)
156 bus->priv = (void *)bus + aligned_size;
157
158
159 for (i = 0; i < PHY_MAX_ADDR; i++) {
160 bus->irq[i] = PHY_POLL;
161 u64_stats_init(&bus->stats[i].syncp);
162 }
163
164 return bus;
165}
166EXPORT_SYMBOL(mdiobus_alloc_size);
167
168static void _devm_mdiobus_free(struct device *dev, void *res)
169{
170 struct mii_bus *bus = *(struct mii_bus **)res;
171
172 if (bus->is_managed_registered && bus->state == MDIOBUS_REGISTERED)
173 mdiobus_unregister(bus);
174
175 mdiobus_free(bus);
176}
177
178static int devm_mdiobus_match(struct device *dev, void *res, void *data)
179{
180 struct mii_bus **r = res;
181
182 if (WARN_ON(!r || !*r))
183 return 0;
184
185 return *r == data;
186}
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202struct mii_bus *devm_mdiobus_alloc_size(struct device *dev, int sizeof_priv)
203{
204 struct mii_bus **ptr, *bus;
205
206 ptr = devres_alloc(_devm_mdiobus_free, sizeof(*ptr), GFP_KERNEL);
207 if (!ptr)
208 return NULL;
209
210
211 bus = mdiobus_alloc_size(sizeof_priv);
212 if (bus) {
213 *ptr = bus;
214 devres_add(dev, ptr);
215 bus->is_managed = 1;
216 } else {
217 devres_free(ptr);
218 }
219
220 return bus;
221}
222EXPORT_SYMBOL_GPL(devm_mdiobus_alloc_size);
223
224
225
226
227
228
229
230
231void devm_mdiobus_free(struct device *dev, struct mii_bus *bus)
232{
233 int rc;
234
235 rc = devres_release(dev, _devm_mdiobus_free,
236 devm_mdiobus_match, bus);
237 WARN_ON(rc);
238}
239EXPORT_SYMBOL_GPL(devm_mdiobus_free);
240
241
242
243
244
245
246
247
248static void mdiobus_release(struct device *d)
249{
250 struct mii_bus *bus = to_mii_bus(d);
251 BUG_ON(bus->state != MDIOBUS_RELEASED &&
252
253 bus->state != MDIOBUS_ALLOCATED);
254 kfree(bus);
255}
256
257struct mdio_bus_stat_attr {
258 int addr;
259 unsigned int field_offset;
260};
261
262static u64 mdio_bus_get_stat(struct mdio_bus_stats *s, unsigned int offset)
263{
264 const char *p = (const char *)s + offset;
265 unsigned int start;
266 u64 val = 0;
267
268 do {
269 start = u64_stats_fetch_begin(&s->syncp);
270 val = u64_stats_read((const u64_stats_t *)p);
271 } while (u64_stats_fetch_retry(&s->syncp, start));
272
273 return val;
274}
275
276static u64 mdio_bus_get_global_stat(struct mii_bus *bus, unsigned int offset)
277{
278 unsigned int i;
279 u64 val = 0;
280
281 for (i = 0; i < PHY_MAX_ADDR; i++)
282 val += mdio_bus_get_stat(&bus->stats[i], offset);
283
284 return val;
285}
286
287static ssize_t mdio_bus_stat_field_show(struct device *dev,
288 struct device_attribute *attr,
289 char *buf)
290{
291 struct mii_bus *bus = to_mii_bus(dev);
292 struct mdio_bus_stat_attr *sattr;
293 struct dev_ext_attribute *eattr;
294 u64 val;
295
296 eattr = container_of(attr, struct dev_ext_attribute, attr);
297 sattr = eattr->var;
298
299 if (sattr->addr < 0)
300 val = mdio_bus_get_global_stat(bus, sattr->field_offset);
301 else
302 val = mdio_bus_get_stat(&bus->stats[sattr->addr],
303 sattr->field_offset);
304
305 return sprintf(buf, "%llu\n", val);
306}
307
308static ssize_t mdio_bus_device_stat_field_show(struct device *dev,
309 struct device_attribute *attr,
310 char *buf)
311{
312 struct mdio_device *mdiodev = to_mdio_device(dev);
313 struct mii_bus *bus = mdiodev->bus;
314 struct mdio_bus_stat_attr *sattr;
315 struct dev_ext_attribute *eattr;
316 int addr = mdiodev->addr;
317 u64 val;
318
319 eattr = container_of(attr, struct dev_ext_attribute, attr);
320 sattr = eattr->var;
321
322 val = mdio_bus_get_stat(&bus->stats[addr], sattr->field_offset);
323
324 return sprintf(buf, "%llu\n", val);
325}
326
327#define MDIO_BUS_STATS_ATTR_DECL(field, file) \
328static struct dev_ext_attribute dev_attr_mdio_bus_##field = { \
329 .attr = { .attr = { .name = file, .mode = 0444 }, \
330 .show = mdio_bus_stat_field_show, \
331 }, \
332 .var = &((struct mdio_bus_stat_attr) { \
333 -1, offsetof(struct mdio_bus_stats, field) \
334 }), \
335}; \
336static struct dev_ext_attribute dev_attr_mdio_bus_device_##field = { \
337 .attr = { .attr = { .name = file, .mode = 0444 }, \
338 .show = mdio_bus_device_stat_field_show, \
339 }, \
340 .var = &((struct mdio_bus_stat_attr) { \
341 -1, offsetof(struct mdio_bus_stats, field) \
342 }), \
343};
344
345#define MDIO_BUS_STATS_ATTR(field) \
346 MDIO_BUS_STATS_ATTR_DECL(field, __stringify(field))
347
348MDIO_BUS_STATS_ATTR(transfers);
349MDIO_BUS_STATS_ATTR(errors);
350MDIO_BUS_STATS_ATTR(writes);
351MDIO_BUS_STATS_ATTR(reads);
352
353#define MDIO_BUS_STATS_ADDR_ATTR_DECL(field, addr, file) \
354static struct dev_ext_attribute dev_attr_mdio_bus_addr_##field##_##addr = { \
355 .attr = { .attr = { .name = file, .mode = 0444 }, \
356 .show = mdio_bus_stat_field_show, \
357 }, \
358 .var = &((struct mdio_bus_stat_attr) { \
359 addr, offsetof(struct mdio_bus_stats, field) \
360 }), \
361}
362
363#define MDIO_BUS_STATS_ADDR_ATTR(field, addr) \
364 MDIO_BUS_STATS_ADDR_ATTR_DECL(field, addr, \
365 __stringify(field) "_" __stringify(addr))
366
367#define MDIO_BUS_STATS_ADDR_ATTR_GROUP_DECL(addr) \
368 MDIO_BUS_STATS_ADDR_ATTR(transfers, addr); \
369 MDIO_BUS_STATS_ADDR_ATTR(errors, addr); \
370 MDIO_BUS_STATS_ADDR_ATTR(writes, addr); \
371 MDIO_BUS_STATS_ADDR_ATTR(reads, addr) \
372
373MDIO_BUS_STATS_ADDR_ATTR_GROUP_DECL(0);
374MDIO_BUS_STATS_ADDR_ATTR_GROUP_DECL(1);
375MDIO_BUS_STATS_ADDR_ATTR_GROUP_DECL(2);
376MDIO_BUS_STATS_ADDR_ATTR_GROUP_DECL(3);
377MDIO_BUS_STATS_ADDR_ATTR_GROUP_DECL(4);
378MDIO_BUS_STATS_ADDR_ATTR_GROUP_DECL(5);
379MDIO_BUS_STATS_ADDR_ATTR_GROUP_DECL(6);
380MDIO_BUS_STATS_ADDR_ATTR_GROUP_DECL(7);
381MDIO_BUS_STATS_ADDR_ATTR_GROUP_DECL(8);
382MDIO_BUS_STATS_ADDR_ATTR_GROUP_DECL(9);
383MDIO_BUS_STATS_ADDR_ATTR_GROUP_DECL(10);
384MDIO_BUS_STATS_ADDR_ATTR_GROUP_DECL(11);
385MDIO_BUS_STATS_ADDR_ATTR_GROUP_DECL(12);
386MDIO_BUS_STATS_ADDR_ATTR_GROUP_DECL(13);
387MDIO_BUS_STATS_ADDR_ATTR_GROUP_DECL(14);
388MDIO_BUS_STATS_ADDR_ATTR_GROUP_DECL(15);
389MDIO_BUS_STATS_ADDR_ATTR_GROUP_DECL(16);
390MDIO_BUS_STATS_ADDR_ATTR_GROUP_DECL(17);
391MDIO_BUS_STATS_ADDR_ATTR_GROUP_DECL(18);
392MDIO_BUS_STATS_ADDR_ATTR_GROUP_DECL(19);
393MDIO_BUS_STATS_ADDR_ATTR_GROUP_DECL(20);
394MDIO_BUS_STATS_ADDR_ATTR_GROUP_DECL(21);
395MDIO_BUS_STATS_ADDR_ATTR_GROUP_DECL(22);
396MDIO_BUS_STATS_ADDR_ATTR_GROUP_DECL(23);
397MDIO_BUS_STATS_ADDR_ATTR_GROUP_DECL(24);
398MDIO_BUS_STATS_ADDR_ATTR_GROUP_DECL(25);
399MDIO_BUS_STATS_ADDR_ATTR_GROUP_DECL(26);
400MDIO_BUS_STATS_ADDR_ATTR_GROUP_DECL(27);
401MDIO_BUS_STATS_ADDR_ATTR_GROUP_DECL(28);
402MDIO_BUS_STATS_ADDR_ATTR_GROUP_DECL(29);
403MDIO_BUS_STATS_ADDR_ATTR_GROUP_DECL(30);
404MDIO_BUS_STATS_ADDR_ATTR_GROUP_DECL(31);
405
406#define MDIO_BUS_STATS_ADDR_ATTR_GROUP(addr) \
407 &dev_attr_mdio_bus_addr_transfers_##addr.attr.attr, \
408 &dev_attr_mdio_bus_addr_errors_##addr.attr.attr, \
409 &dev_attr_mdio_bus_addr_writes_##addr.attr.attr, \
410 &dev_attr_mdio_bus_addr_reads_##addr.attr.attr \
411
412static struct attribute *mdio_bus_statistics_attrs[] = {
413 &dev_attr_mdio_bus_transfers.attr.attr,
414 &dev_attr_mdio_bus_errors.attr.attr,
415 &dev_attr_mdio_bus_writes.attr.attr,
416 &dev_attr_mdio_bus_reads.attr.attr,
417 MDIO_BUS_STATS_ADDR_ATTR_GROUP(0),
418 MDIO_BUS_STATS_ADDR_ATTR_GROUP(1),
419 MDIO_BUS_STATS_ADDR_ATTR_GROUP(2),
420 MDIO_BUS_STATS_ADDR_ATTR_GROUP(3),
421 MDIO_BUS_STATS_ADDR_ATTR_GROUP(4),
422 MDIO_BUS_STATS_ADDR_ATTR_GROUP(5),
423 MDIO_BUS_STATS_ADDR_ATTR_GROUP(6),
424 MDIO_BUS_STATS_ADDR_ATTR_GROUP(7),
425 MDIO_BUS_STATS_ADDR_ATTR_GROUP(8),
426 MDIO_BUS_STATS_ADDR_ATTR_GROUP(9),
427 MDIO_BUS_STATS_ADDR_ATTR_GROUP(10),
428 MDIO_BUS_STATS_ADDR_ATTR_GROUP(11),
429 MDIO_BUS_STATS_ADDR_ATTR_GROUP(12),
430 MDIO_BUS_STATS_ADDR_ATTR_GROUP(13),
431 MDIO_BUS_STATS_ADDR_ATTR_GROUP(14),
432 MDIO_BUS_STATS_ADDR_ATTR_GROUP(15),
433 MDIO_BUS_STATS_ADDR_ATTR_GROUP(16),
434 MDIO_BUS_STATS_ADDR_ATTR_GROUP(17),
435 MDIO_BUS_STATS_ADDR_ATTR_GROUP(18),
436 MDIO_BUS_STATS_ADDR_ATTR_GROUP(19),
437 MDIO_BUS_STATS_ADDR_ATTR_GROUP(20),
438 MDIO_BUS_STATS_ADDR_ATTR_GROUP(21),
439 MDIO_BUS_STATS_ADDR_ATTR_GROUP(22),
440 MDIO_BUS_STATS_ADDR_ATTR_GROUP(23),
441 MDIO_BUS_STATS_ADDR_ATTR_GROUP(24),
442 MDIO_BUS_STATS_ADDR_ATTR_GROUP(25),
443 MDIO_BUS_STATS_ADDR_ATTR_GROUP(26),
444 MDIO_BUS_STATS_ADDR_ATTR_GROUP(27),
445 MDIO_BUS_STATS_ADDR_ATTR_GROUP(28),
446 MDIO_BUS_STATS_ADDR_ATTR_GROUP(29),
447 MDIO_BUS_STATS_ADDR_ATTR_GROUP(30),
448 MDIO_BUS_STATS_ADDR_ATTR_GROUP(31),
449 NULL,
450};
451
452static const struct attribute_group mdio_bus_statistics_group = {
453 .name = "statistics",
454 .attrs = mdio_bus_statistics_attrs,
455};
456
457static const struct attribute_group *mdio_bus_groups[] = {
458 &mdio_bus_statistics_group,
459 NULL,
460};
461
462static struct class mdio_bus_class = {
463 .name = "mdio_bus",
464 .dev_release = mdiobus_release,
465 .dev_groups = mdio_bus_groups,
466};
467
468
469
470
471
472
473
474
475
476struct mii_bus *mdio_find_bus(const char *mdio_name)
477{
478 struct device *d;
479
480 d = class_find_device_by_name(&mdio_bus_class, mdio_name);
481 return d ? to_mii_bus(d) : NULL;
482}
483EXPORT_SYMBOL(mdio_find_bus);
484
485#if IS_ENABLED(CONFIG_OF_MDIO)
486
487
488
489
490
491
492
493
494
495
496
497
498
499struct mii_bus *of_mdio_find_bus(struct device_node *mdio_bus_np)
500{
501 struct device *d;
502
503 if (!mdio_bus_np)
504 return NULL;
505
506 d = class_find_device_by_of_node(&mdio_bus_class, mdio_bus_np);
507 return d ? to_mii_bus(d) : NULL;
508}
509EXPORT_SYMBOL(of_mdio_find_bus);
510
511
512
513
514
515
516
517static void of_mdiobus_link_mdiodev(struct mii_bus *bus,
518 struct mdio_device *mdiodev)
519{
520 struct device *dev = &mdiodev->dev;
521 struct device_node *child;
522
523 if (dev->of_node || !bus->dev.of_node)
524 return;
525
526 for_each_available_child_of_node(bus->dev.of_node, child) {
527 int addr;
528
529 addr = of_mdio_parse_addr(dev, child);
530 if (addr < 0)
531 continue;
532
533 if (addr == mdiodev->addr) {
534 dev->of_node = child;
535 dev->fwnode = of_fwnode_handle(child);
536 return;
537 }
538 }
539}
540#else
541static inline void of_mdiobus_link_mdiodev(struct mii_bus *mdio,
542 struct mdio_device *mdiodev)
543{
544}
545#endif
546
547
548
549
550
551
552
553
554
555static int mdiobus_create_device(struct mii_bus *bus,
556 struct mdio_board_info *bi)
557{
558 struct mdio_device *mdiodev;
559 int ret = 0;
560
561 mdiodev = mdio_device_create(bus, bi->mdio_addr);
562 if (IS_ERR(mdiodev))
563 return -ENODEV;
564
565 strncpy(mdiodev->modalias, bi->modalias,
566 sizeof(mdiodev->modalias));
567 mdiodev->bus_match = mdio_device_bus_match;
568 mdiodev->dev.platform_data = (void *)bi->platform_data;
569
570 ret = mdio_device_register(mdiodev);
571 if (ret)
572 mdio_device_free(mdiodev);
573
574 return ret;
575}
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591int __mdiobus_register(struct mii_bus *bus, struct module *owner)
592{
593 struct mdio_device *mdiodev;
594 int i, err;
595 struct gpio_desc *gpiod;
596
597 if (NULL == bus || NULL == bus->name ||
598 NULL == bus->read || NULL == bus->write)
599 return -EINVAL;
600
601 BUG_ON(bus->state != MDIOBUS_ALLOCATED &&
602 bus->state != MDIOBUS_UNREGISTERED);
603
604 bus->owner = owner;
605 bus->dev.parent = bus->parent;
606 bus->dev.class = &mdio_bus_class;
607 bus->dev.groups = NULL;
608 dev_set_name(&bus->dev, "%s", bus->id);
609
610 err = device_register(&bus->dev);
611 if (err) {
612 pr_err("mii_bus %s failed to register\n", bus->id);
613 return -EINVAL;
614 }
615
616 mutex_init(&bus->mdio_lock);
617 mutex_init(&bus->shared_lock);
618
619
620 gpiod = devm_gpiod_get_optional(&bus->dev, "reset", GPIOD_OUT_LOW);
621 if (IS_ERR(gpiod)) {
622 dev_err(&bus->dev, "mii_bus %s couldn't get reset GPIO\n",
623 bus->id);
624 device_del(&bus->dev);
625 return PTR_ERR(gpiod);
626 } else if (gpiod) {
627 bus->reset_gpiod = gpiod;
628
629 gpiod_set_value_cansleep(gpiod, 1);
630 udelay(bus->reset_delay_us);
631 gpiod_set_value_cansleep(gpiod, 0);
632 }
633
634 if (bus->reset) {
635 err = bus->reset(bus);
636 if (err)
637 goto error_reset_gpiod;
638 }
639
640 for (i = 0; i < PHY_MAX_ADDR; i++) {
641 if ((bus->phy_mask & (1 << i)) == 0) {
642 struct phy_device *phydev;
643
644 phydev = mdiobus_scan(bus, i);
645 if (IS_ERR(phydev) && (PTR_ERR(phydev) != -ENODEV)) {
646 err = PTR_ERR(phydev);
647 goto error;
648 }
649 }
650 }
651
652 mdiobus_setup_mdiodev_from_board_info(bus, mdiobus_create_device);
653
654 bus->state = MDIOBUS_REGISTERED;
655 pr_info("%s: probed\n", bus->name);
656 return 0;
657
658error:
659 while (--i >= 0) {
660 mdiodev = bus->mdio_map[i];
661 if (!mdiodev)
662 continue;
663
664 mdiodev->device_remove(mdiodev);
665 mdiodev->device_free(mdiodev);
666 }
667error_reset_gpiod:
668
669 if (bus->reset_gpiod)
670 gpiod_set_value_cansleep(bus->reset_gpiod, 1);
671
672 device_del(&bus->dev);
673 return err;
674}
675EXPORT_SYMBOL(__mdiobus_register);
676
677void mdiobus_unregister(struct mii_bus *bus)
678{
679 struct mdio_device *mdiodev;
680 int i;
681
682 BUG_ON(bus->state != MDIOBUS_REGISTERED);
683 bus->state = MDIOBUS_UNREGISTERED;
684
685 for (i = 0; i < PHY_MAX_ADDR; i++) {
686 mdiodev = bus->mdio_map[i];
687 if (!mdiodev)
688 continue;
689
690 if (mdiodev->reset_gpio)
691 gpiod_put(mdiodev->reset_gpio);
692
693 mdiodev->device_remove(mdiodev);
694 mdiodev->device_free(mdiodev);
695 }
696
697
698 if (bus->reset_gpiod)
699 gpiod_set_value_cansleep(bus->reset_gpiod, 1);
700
701 device_del(&bus->dev);
702}
703EXPORT_SYMBOL(mdiobus_unregister);
704
705
706
707
708
709
710
711
712
713void mdiobus_free(struct mii_bus *bus)
714{
715
716 if (bus->state == MDIOBUS_ALLOCATED) {
717 kfree(bus);
718 return;
719 }
720
721 BUG_ON(bus->state != MDIOBUS_UNREGISTERED);
722 bus->state = MDIOBUS_RELEASED;
723
724 put_device(&bus->dev);
725}
726EXPORT_SYMBOL(mdiobus_free);
727
728
729
730
731
732
733
734
735
736
737
738
739
740struct phy_device *mdiobus_scan(struct mii_bus *bus, int addr)
741{
742 struct phy_device *phydev;
743 int err;
744
745 phydev = get_phy_device(bus, addr, false);
746 if (IS_ERR(phydev))
747 return phydev;
748
749
750
751
752
753 of_mdiobus_link_mdiodev(bus, &phydev->mdio);
754
755 err = phy_device_register(phydev);
756 if (err) {
757 phy_device_free(phydev);
758 return ERR_PTR(-ENODEV);
759 }
760
761 return phydev;
762}
763EXPORT_SYMBOL(mdiobus_scan);
764
765static void mdiobus_stats_acct(struct mdio_bus_stats *stats, bool op, int ret)
766{
767 preempt_disable();
768 u64_stats_update_begin(&stats->syncp);
769
770 u64_stats_inc(&stats->transfers);
771 if (ret < 0) {
772 u64_stats_inc(&stats->errors);
773 goto out;
774 }
775
776 if (op)
777 u64_stats_inc(&stats->reads);
778 else
779 u64_stats_inc(&stats->writes);
780out:
781 u64_stats_update_end(&stats->syncp);
782 preempt_enable();
783}
784
785
786
787
788
789
790
791
792
793
794
795int __mdiobus_read(struct mii_bus *bus, int addr, u32 regnum)
796{
797 int retval;
798
799 WARN_ON_ONCE(!mutex_is_locked(&bus->mdio_lock));
800
801 retval = bus->read(bus, addr, regnum);
802
803 trace_mdio_access(bus, 1, addr, regnum, retval, retval);
804 mdiobus_stats_acct(&bus->stats[addr], true, retval);
805
806 return retval;
807}
808EXPORT_SYMBOL(__mdiobus_read);
809
810
811
812
813
814
815
816
817
818
819
820
821int __mdiobus_write(struct mii_bus *bus, int addr, u32 regnum, u16 val)
822{
823 int err;
824
825 WARN_ON_ONCE(!mutex_is_locked(&bus->mdio_lock));
826
827 err = bus->write(bus, addr, regnum, val);
828
829 trace_mdio_access(bus, 0, addr, regnum, val, err);
830 mdiobus_stats_acct(&bus->stats[addr], false, err);
831
832 return err;
833}
834EXPORT_SYMBOL(__mdiobus_write);
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849int __mdiobus_modify_changed(struct mii_bus *bus, int addr, u32 regnum,
850 u16 mask, u16 set)
851{
852 int new, ret;
853
854 ret = __mdiobus_read(bus, addr, regnum);
855 if (ret < 0)
856 return ret;
857
858 new = (ret & ~mask) | set;
859 if (new == ret)
860 return 0;
861
862 ret = __mdiobus_write(bus, addr, regnum, new);
863
864 return ret < 0 ? ret : 1;
865}
866EXPORT_SYMBOL_GPL(__mdiobus_modify_changed);
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881int mdiobus_read_nested(struct mii_bus *bus, int addr, u32 regnum)
882{
883 int retval;
884
885 if (WARN_ON_ONCE(in_interrupt()))
886 return -EINVAL;
887
888 mutex_lock_nested(&bus->mdio_lock, MDIO_MUTEX_NESTED);
889 retval = __mdiobus_read(bus, addr, regnum);
890 mutex_unlock(&bus->mdio_lock);
891
892 return retval;
893}
894EXPORT_SYMBOL(mdiobus_read_nested);
895
896
897
898
899
900
901
902
903
904
905
906int mdiobus_read(struct mii_bus *bus, int addr, u32 regnum)
907{
908 int retval;
909
910 if (WARN_ON_ONCE(in_interrupt()))
911 return -EINVAL;
912
913 mutex_lock(&bus->mdio_lock);
914 retval = __mdiobus_read(bus, addr, regnum);
915 mutex_unlock(&bus->mdio_lock);
916
917 return retval;
918}
919EXPORT_SYMBOL(mdiobus_read);
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935int mdiobus_write_nested(struct mii_bus *bus, int addr, u32 regnum, u16 val)
936{
937 int err;
938
939 if (WARN_ON_ONCE(in_interrupt()))
940 return -EINVAL;
941
942 mutex_lock_nested(&bus->mdio_lock, MDIO_MUTEX_NESTED);
943 err = __mdiobus_write(bus, addr, regnum, val);
944 mutex_unlock(&bus->mdio_lock);
945
946 return err;
947}
948EXPORT_SYMBOL(mdiobus_write_nested);
949
950
951
952
953
954
955
956
957
958
959
960
961int mdiobus_write(struct mii_bus *bus, int addr, u32 regnum, u16 val)
962{
963 int err;
964
965 if (WARN_ON_ONCE(in_interrupt()))
966 return -EINVAL;
967
968 mutex_lock(&bus->mdio_lock);
969 err = __mdiobus_write(bus, addr, regnum, val);
970 mutex_unlock(&bus->mdio_lock);
971
972 return err;
973}
974EXPORT_SYMBOL(mdiobus_write);
975
976
977
978
979
980
981
982
983
984
985int mdiobus_modify(struct mii_bus *bus, int addr, u32 regnum, u16 mask, u16 set)
986{
987 int err;
988
989 if (WARN_ON_ONCE(in_interrupt()))
990 return -EINVAL;
991
992 mutex_lock(&bus->mdio_lock);
993 err = __mdiobus_modify_changed(bus, addr, regnum, mask, set);
994 mutex_unlock(&bus->mdio_lock);
995
996 return err < 0 ? err : 0;
997}
998EXPORT_SYMBOL_GPL(mdiobus_modify);
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011static int mdio_bus_match(struct device *dev, struct device_driver *drv)
1012{
1013 struct mdio_device *mdio = to_mdio_device(dev);
1014
1015 if (of_driver_match_device(dev, drv))
1016 return 1;
1017
1018 if (mdio->bus_match)
1019 return mdio->bus_match(dev, drv);
1020
1021 return 0;
1022}
1023
1024static int mdio_uevent(struct device *dev, struct kobj_uevent_env *env)
1025{
1026 int rc;
1027
1028
1029 rc = of_device_uevent_modalias(dev, env);
1030 if (rc != -ENODEV)
1031 return rc;
1032
1033 return 0;
1034}
1035
1036static struct attribute *mdio_bus_device_statistics_attrs[] = {
1037 &dev_attr_mdio_bus_device_transfers.attr.attr,
1038 &dev_attr_mdio_bus_device_errors.attr.attr,
1039 &dev_attr_mdio_bus_device_writes.attr.attr,
1040 &dev_attr_mdio_bus_device_reads.attr.attr,
1041 NULL,
1042};
1043
1044static const struct attribute_group mdio_bus_device_statistics_group = {
1045 .name = "statistics",
1046 .attrs = mdio_bus_device_statistics_attrs,
1047};
1048
1049static const struct attribute_group *mdio_bus_dev_groups[] = {
1050 &mdio_bus_device_statistics_group,
1051 NULL,
1052};
1053
1054struct bus_type mdio_bus_type = {
1055 .name = "mdio_bus",
1056 .dev_groups = mdio_bus_dev_groups,
1057 .match = mdio_bus_match,
1058 .uevent = mdio_uevent,
1059};
1060EXPORT_SYMBOL(mdio_bus_type);
1061
1062int __init mdio_bus_init(void)
1063{
1064 int ret;
1065
1066 ret = class_register(&mdio_bus_class);
1067 if (!ret) {
1068 ret = bus_register(&mdio_bus_type);
1069 if (ret)
1070 class_unregister(&mdio_bus_class);
1071 }
1072
1073 return ret;
1074}
1075EXPORT_SYMBOL_GPL(mdio_bus_init);
1076
1077#if IS_ENABLED(CONFIG_PHYLIB)
1078void mdio_bus_exit(void)
1079{
1080 class_unregister(&mdio_bus_class);
1081 bus_unregister(&mdio_bus_type);
1082}
1083EXPORT_SYMBOL_GPL(mdio_bus_exit);
1084#else
1085module_init(mdio_bus_init);
1086
1087MODULE_LICENSE("GPL");
1088MODULE_DESCRIPTION("MDIO bus/device layer");
1089#endif
1090