1 // SPDX-License-Identifier: GPL-2.0+
2 /* MDIO Bus interface
3 *
4 * Author: Andy Fleming
5 *
6 * Copyright (c) 2004 Freescale Semiconductor, Inc.
7 */
8
9 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
10
11 #include <linux/delay.h>
12 #include <linux/device.h>
13 #include <linux/errno.h>
14 #include <linux/etherdevice.h>
15 #include <linux/ethtool.h>
16 #include <linux/gpio.h>
17 #include <linux/gpio/consumer.h>
18 #include <linux/init.h>
19 #include <linux/interrupt.h>
20 #include <linux/io.h>
21 #include <linux/kernel.h>
22 #include <linux/mii.h>
23 #include <linux/mm.h>
24 #include <linux/module.h>
25 #include <linux/netdevice.h>
26 #include <linux/of_device.h>
27 #include <linux/of_gpio.h>
28 #include <linux/of_mdio.h>
29 #include <linux/phy.h>
30 #include <linux/reset.h>
31 #include <linux/skbuff.h>
32 #include <linux/slab.h>
33 #include <linux/spinlock.h>
34 #include <linux/string.h>
35 #include <linux/uaccess.h>
36 #include <linux/unistd.h>
37
38 #define CREATE_TRACE_POINTS
39 #include <trace/events/mdio.h>
40
41 #include "mdio-boardinfo.h"
42
mdiobus_register_gpiod(struct mdio_device * mdiodev)43 static int mdiobus_register_gpiod(struct mdio_device *mdiodev)
44 {
45 /* Deassert the optional reset signal */
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
mdiobus_register_reset(struct mdio_device * mdiodev)57 static 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
mdiobus_register_device(struct mdio_device * mdiodev)70 int 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 /* Assert the reset signal */
87 mdio_device_reset(mdiodev, 1);
88 }
89
90 mdiodev->bus->mdio_map[mdiodev->addr] = mdiodev;
91
92 return 0;
93 }
94 EXPORT_SYMBOL(mdiobus_register_device);
95
mdiobus_unregister_device(struct mdio_device * mdiodev)96 int 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 }
107 EXPORT_SYMBOL(mdiobus_unregister_device);
108
mdiobus_get_phy(struct mii_bus * bus,int addr)109 struct phy_device *mdiobus_get_phy(struct mii_bus *bus, int addr)
110 {
111 struct mdio_device *mdiodev;
112
113 if (addr < 0 || addr >= ARRAY_SIZE(bus->mdio_map))
114 return NULL;
115
116 mdiodev = bus->mdio_map[addr];
117
118 if (!mdiodev)
119 return NULL;
120
121 if (!(mdiodev->flags & MDIO_DEVICE_FLAG_PHY))
122 return NULL;
123
124 return container_of(mdiodev, struct phy_device, mdio);
125 }
126 EXPORT_SYMBOL(mdiobus_get_phy);
127
mdiobus_is_registered_device(struct mii_bus * bus,int addr)128 bool mdiobus_is_registered_device(struct mii_bus *bus, int addr)
129 {
130 return bus->mdio_map[addr];
131 }
132 EXPORT_SYMBOL(mdiobus_is_registered_device);
133
134 /**
135 * mdiobus_alloc_size - allocate a mii_bus structure
136 * @size: extra amount of memory to allocate for private storage.
137 * If non-zero, then bus->priv is points to that memory.
138 *
139 * Description: called by a bus driver to allocate an mii_bus
140 * structure to fill in.
141 */
mdiobus_alloc_size(size_t size)142 struct mii_bus *mdiobus_alloc_size(size_t size)
143 {
144 struct mii_bus *bus;
145 size_t aligned_size = ALIGN(sizeof(*bus), NETDEV_ALIGN);
146 size_t alloc_size;
147 int i;
148
149 /* If we alloc extra space, it should be aligned */
150 if (size)
151 alloc_size = aligned_size + size;
152 else
153 alloc_size = sizeof(*bus);
154
155 bus = kzalloc(alloc_size, GFP_KERNEL);
156 if (!bus)
157 return NULL;
158
159 bus->state = MDIOBUS_ALLOCATED;
160 if (size)
161 bus->priv = (void *)bus + aligned_size;
162
163 /* Initialise the interrupts to polling and 64-bit seqcounts */
164 for (i = 0; i < PHY_MAX_ADDR; i++) {
165 bus->irq[i] = PHY_POLL;
166 u64_stats_init(&bus->stats[i].syncp);
167 }
168
169 return bus;
170 }
171 EXPORT_SYMBOL(mdiobus_alloc_size);
172
173 /**
174 * mdiobus_release - mii_bus device release callback
175 * @d: the target struct device that contains the mii_bus
176 *
177 * Description: called when the last reference to an mii_bus is
178 * dropped, to free the underlying memory.
179 */
mdiobus_release(struct device * d)180 static void mdiobus_release(struct device *d)
181 {
182 struct mii_bus *bus = to_mii_bus(d);
183
184 BUG_ON(bus->state != MDIOBUS_RELEASED &&
185 /* for compatibility with error handling in drivers */
186 bus->state != MDIOBUS_ALLOCATED);
187 kfree(bus);
188 }
189
190 struct mdio_bus_stat_attr {
191 int addr;
192 unsigned int field_offset;
193 };
194
mdio_bus_get_stat(struct mdio_bus_stats * s,unsigned int offset)195 static u64 mdio_bus_get_stat(struct mdio_bus_stats *s, unsigned int offset)
196 {
197 const char *p = (const char *)s + offset;
198 unsigned int start;
199 u64 val = 0;
200
201 do {
202 start = u64_stats_fetch_begin(&s->syncp);
203 val = u64_stats_read((const u64_stats_t *)p);
204 } while (u64_stats_fetch_retry(&s->syncp, start));
205
206 return val;
207 }
208
mdio_bus_get_global_stat(struct mii_bus * bus,unsigned int offset)209 static u64 mdio_bus_get_global_stat(struct mii_bus *bus, unsigned int offset)
210 {
211 unsigned int i;
212 u64 val = 0;
213
214 for (i = 0; i < PHY_MAX_ADDR; i++)
215 val += mdio_bus_get_stat(&bus->stats[i], offset);
216
217 return val;
218 }
219
mdio_bus_stat_field_show(struct device * dev,struct device_attribute * attr,char * buf)220 static ssize_t mdio_bus_stat_field_show(struct device *dev,
221 struct device_attribute *attr,
222 char *buf)
223 {
224 struct mii_bus *bus = to_mii_bus(dev);
225 struct mdio_bus_stat_attr *sattr;
226 struct dev_ext_attribute *eattr;
227 u64 val;
228
229 eattr = container_of(attr, struct dev_ext_attribute, attr);
230 sattr = eattr->var;
231
232 if (sattr->addr < 0)
233 val = mdio_bus_get_global_stat(bus, sattr->field_offset);
234 else
235 val = mdio_bus_get_stat(&bus->stats[sattr->addr],
236 sattr->field_offset);
237
238 return sprintf(buf, "%llu\n", val);
239 }
240
mdio_bus_device_stat_field_show(struct device * dev,struct device_attribute * attr,char * buf)241 static ssize_t mdio_bus_device_stat_field_show(struct device *dev,
242 struct device_attribute *attr,
243 char *buf)
244 {
245 struct mdio_device *mdiodev = to_mdio_device(dev);
246 struct mii_bus *bus = mdiodev->bus;
247 struct mdio_bus_stat_attr *sattr;
248 struct dev_ext_attribute *eattr;
249 int addr = mdiodev->addr;
250 u64 val;
251
252 eattr = container_of(attr, struct dev_ext_attribute, attr);
253 sattr = eattr->var;
254
255 val = mdio_bus_get_stat(&bus->stats[addr], sattr->field_offset);
256
257 return sprintf(buf, "%llu\n", val);
258 }
259
260 #define MDIO_BUS_STATS_ATTR_DECL(field, file) \
261 static struct dev_ext_attribute dev_attr_mdio_bus_##field = { \
262 .attr = { .attr = { .name = file, .mode = 0444 }, \
263 .show = mdio_bus_stat_field_show, \
264 }, \
265 .var = &((struct mdio_bus_stat_attr) { \
266 -1, offsetof(struct mdio_bus_stats, field) \
267 }), \
268 }; \
269 static struct dev_ext_attribute dev_attr_mdio_bus_device_##field = { \
270 .attr = { .attr = { .name = file, .mode = 0444 }, \
271 .show = mdio_bus_device_stat_field_show, \
272 }, \
273 .var = &((struct mdio_bus_stat_attr) { \
274 -1, offsetof(struct mdio_bus_stats, field) \
275 }), \
276 };
277
278 #define MDIO_BUS_STATS_ATTR(field) \
279 MDIO_BUS_STATS_ATTR_DECL(field, __stringify(field))
280
281 MDIO_BUS_STATS_ATTR(transfers);
282 MDIO_BUS_STATS_ATTR(errors);
283 MDIO_BUS_STATS_ATTR(writes);
284 MDIO_BUS_STATS_ATTR(reads);
285
286 #define MDIO_BUS_STATS_ADDR_ATTR_DECL(field, addr, file) \
287 static struct dev_ext_attribute dev_attr_mdio_bus_addr_##field##_##addr = { \
288 .attr = { .attr = { .name = file, .mode = 0444 }, \
289 .show = mdio_bus_stat_field_show, \
290 }, \
291 .var = &((struct mdio_bus_stat_attr) { \
292 addr, offsetof(struct mdio_bus_stats, field) \
293 }), \
294 }
295
296 #define MDIO_BUS_STATS_ADDR_ATTR(field, addr) \
297 MDIO_BUS_STATS_ADDR_ATTR_DECL(field, addr, \
298 __stringify(field) "_" __stringify(addr))
299
300 #define MDIO_BUS_STATS_ADDR_ATTR_GROUP_DECL(addr) \
301 MDIO_BUS_STATS_ADDR_ATTR(transfers, addr); \
302 MDIO_BUS_STATS_ADDR_ATTR(errors, addr); \
303 MDIO_BUS_STATS_ADDR_ATTR(writes, addr); \
304 MDIO_BUS_STATS_ADDR_ATTR(reads, addr) \
305
306 MDIO_BUS_STATS_ADDR_ATTR_GROUP_DECL(0);
307 MDIO_BUS_STATS_ADDR_ATTR_GROUP_DECL(1);
308 MDIO_BUS_STATS_ADDR_ATTR_GROUP_DECL(2);
309 MDIO_BUS_STATS_ADDR_ATTR_GROUP_DECL(3);
310 MDIO_BUS_STATS_ADDR_ATTR_GROUP_DECL(4);
311 MDIO_BUS_STATS_ADDR_ATTR_GROUP_DECL(5);
312 MDIO_BUS_STATS_ADDR_ATTR_GROUP_DECL(6);
313 MDIO_BUS_STATS_ADDR_ATTR_GROUP_DECL(7);
314 MDIO_BUS_STATS_ADDR_ATTR_GROUP_DECL(8);
315 MDIO_BUS_STATS_ADDR_ATTR_GROUP_DECL(9);
316 MDIO_BUS_STATS_ADDR_ATTR_GROUP_DECL(10);
317 MDIO_BUS_STATS_ADDR_ATTR_GROUP_DECL(11);
318 MDIO_BUS_STATS_ADDR_ATTR_GROUP_DECL(12);
319 MDIO_BUS_STATS_ADDR_ATTR_GROUP_DECL(13);
320 MDIO_BUS_STATS_ADDR_ATTR_GROUP_DECL(14);
321 MDIO_BUS_STATS_ADDR_ATTR_GROUP_DECL(15);
322 MDIO_BUS_STATS_ADDR_ATTR_GROUP_DECL(16);
323 MDIO_BUS_STATS_ADDR_ATTR_GROUP_DECL(17);
324 MDIO_BUS_STATS_ADDR_ATTR_GROUP_DECL(18);
325 MDIO_BUS_STATS_ADDR_ATTR_GROUP_DECL(19);
326 MDIO_BUS_STATS_ADDR_ATTR_GROUP_DECL(20);
327 MDIO_BUS_STATS_ADDR_ATTR_GROUP_DECL(21);
328 MDIO_BUS_STATS_ADDR_ATTR_GROUP_DECL(22);
329 MDIO_BUS_STATS_ADDR_ATTR_GROUP_DECL(23);
330 MDIO_BUS_STATS_ADDR_ATTR_GROUP_DECL(24);
331 MDIO_BUS_STATS_ADDR_ATTR_GROUP_DECL(25);
332 MDIO_BUS_STATS_ADDR_ATTR_GROUP_DECL(26);
333 MDIO_BUS_STATS_ADDR_ATTR_GROUP_DECL(27);
334 MDIO_BUS_STATS_ADDR_ATTR_GROUP_DECL(28);
335 MDIO_BUS_STATS_ADDR_ATTR_GROUP_DECL(29);
336 MDIO_BUS_STATS_ADDR_ATTR_GROUP_DECL(30);
337 MDIO_BUS_STATS_ADDR_ATTR_GROUP_DECL(31);
338
339 #define MDIO_BUS_STATS_ADDR_ATTR_GROUP(addr) \
340 &dev_attr_mdio_bus_addr_transfers_##addr.attr.attr, \
341 &dev_attr_mdio_bus_addr_errors_##addr.attr.attr, \
342 &dev_attr_mdio_bus_addr_writes_##addr.attr.attr, \
343 &dev_attr_mdio_bus_addr_reads_##addr.attr.attr \
344
345 static struct attribute *mdio_bus_statistics_attrs[] = {
346 &dev_attr_mdio_bus_transfers.attr.attr,
347 &dev_attr_mdio_bus_errors.attr.attr,
348 &dev_attr_mdio_bus_writes.attr.attr,
349 &dev_attr_mdio_bus_reads.attr.attr,
350 MDIO_BUS_STATS_ADDR_ATTR_GROUP(0),
351 MDIO_BUS_STATS_ADDR_ATTR_GROUP(1),
352 MDIO_BUS_STATS_ADDR_ATTR_GROUP(2),
353 MDIO_BUS_STATS_ADDR_ATTR_GROUP(3),
354 MDIO_BUS_STATS_ADDR_ATTR_GROUP(4),
355 MDIO_BUS_STATS_ADDR_ATTR_GROUP(5),
356 MDIO_BUS_STATS_ADDR_ATTR_GROUP(6),
357 MDIO_BUS_STATS_ADDR_ATTR_GROUP(7),
358 MDIO_BUS_STATS_ADDR_ATTR_GROUP(8),
359 MDIO_BUS_STATS_ADDR_ATTR_GROUP(9),
360 MDIO_BUS_STATS_ADDR_ATTR_GROUP(10),
361 MDIO_BUS_STATS_ADDR_ATTR_GROUP(11),
362 MDIO_BUS_STATS_ADDR_ATTR_GROUP(12),
363 MDIO_BUS_STATS_ADDR_ATTR_GROUP(13),
364 MDIO_BUS_STATS_ADDR_ATTR_GROUP(14),
365 MDIO_BUS_STATS_ADDR_ATTR_GROUP(15),
366 MDIO_BUS_STATS_ADDR_ATTR_GROUP(16),
367 MDIO_BUS_STATS_ADDR_ATTR_GROUP(17),
368 MDIO_BUS_STATS_ADDR_ATTR_GROUP(18),
369 MDIO_BUS_STATS_ADDR_ATTR_GROUP(19),
370 MDIO_BUS_STATS_ADDR_ATTR_GROUP(20),
371 MDIO_BUS_STATS_ADDR_ATTR_GROUP(21),
372 MDIO_BUS_STATS_ADDR_ATTR_GROUP(22),
373 MDIO_BUS_STATS_ADDR_ATTR_GROUP(23),
374 MDIO_BUS_STATS_ADDR_ATTR_GROUP(24),
375 MDIO_BUS_STATS_ADDR_ATTR_GROUP(25),
376 MDIO_BUS_STATS_ADDR_ATTR_GROUP(26),
377 MDIO_BUS_STATS_ADDR_ATTR_GROUP(27),
378 MDIO_BUS_STATS_ADDR_ATTR_GROUP(28),
379 MDIO_BUS_STATS_ADDR_ATTR_GROUP(29),
380 MDIO_BUS_STATS_ADDR_ATTR_GROUP(30),
381 MDIO_BUS_STATS_ADDR_ATTR_GROUP(31),
382 NULL,
383 };
384
385 static const struct attribute_group mdio_bus_statistics_group = {
386 .name = "statistics",
387 .attrs = mdio_bus_statistics_attrs,
388 };
389
390 static const struct attribute_group *mdio_bus_groups[] = {
391 &mdio_bus_statistics_group,
392 NULL,
393 };
394
395 static struct class mdio_bus_class = {
396 .name = "mdio_bus",
397 .dev_release = mdiobus_release,
398 .dev_groups = mdio_bus_groups,
399 };
400
401 /**
402 * mdio_find_bus - Given the name of a mdiobus, find the mii_bus.
403 * @mdio_name: The name of a mdiobus.
404 *
405 * Returns a reference to the mii_bus, or NULL if none found. The
406 * embedded struct device will have its reference count incremented,
407 * and this must be put_deviced'ed once the bus is finished with.
408 */
mdio_find_bus(const char * mdio_name)409 struct mii_bus *mdio_find_bus(const char *mdio_name)
410 {
411 struct device *d;
412
413 d = class_find_device_by_name(&mdio_bus_class, mdio_name);
414 return d ? to_mii_bus(d) : NULL;
415 }
416 EXPORT_SYMBOL(mdio_find_bus);
417
418 #if IS_ENABLED(CONFIG_OF_MDIO)
419 /**
420 * of_mdio_find_bus - Given an mii_bus node, find the mii_bus.
421 * @mdio_bus_np: Pointer to the mii_bus.
422 *
423 * Returns a reference to the mii_bus, or NULL if none found. The
424 * embedded struct device will have its reference count incremented,
425 * and this must be put once the bus is finished with.
426 *
427 * Because the association of a device_node and mii_bus is made via
428 * of_mdiobus_register(), the mii_bus cannot be found before it is
429 * registered with of_mdiobus_register().
430 *
431 */
of_mdio_find_bus(struct device_node * mdio_bus_np)432 struct mii_bus *of_mdio_find_bus(struct device_node *mdio_bus_np)
433 {
434 struct device *d;
435
436 if (!mdio_bus_np)
437 return NULL;
438
439 d = class_find_device_by_of_node(&mdio_bus_class, mdio_bus_np);
440 return d ? to_mii_bus(d) : NULL;
441 }
442 EXPORT_SYMBOL(of_mdio_find_bus);
443
444 /* Walk the list of subnodes of a mdio bus and look for a node that
445 * matches the mdio device's address with its 'reg' property. If
446 * found, set the of_node pointer for the mdio device. This allows
447 * auto-probed phy devices to be supplied with information passed in
448 * via DT.
449 */
of_mdiobus_link_mdiodev(struct mii_bus * bus,struct mdio_device * mdiodev)450 static void of_mdiobus_link_mdiodev(struct mii_bus *bus,
451 struct mdio_device *mdiodev)
452 {
453 struct device *dev = &mdiodev->dev;
454 struct device_node *child;
455
456 if (dev->of_node || !bus->dev.of_node)
457 return;
458
459 for_each_available_child_of_node(bus->dev.of_node, child) {
460 int addr;
461
462 addr = of_mdio_parse_addr(dev, child);
463 if (addr < 0)
464 continue;
465
466 if (addr == mdiodev->addr) {
467 device_set_node(dev, of_fwnode_handle(child));
468 return;
469 }
470 }
471 }
472 #else /* !IS_ENABLED(CONFIG_OF_MDIO) */
of_mdiobus_link_mdiodev(struct mii_bus * mdio,struct mdio_device * mdiodev)473 static inline void of_mdiobus_link_mdiodev(struct mii_bus *mdio,
474 struct mdio_device *mdiodev)
475 {
476 }
477 #endif
478
479 /**
480 * mdiobus_create_device - create a full MDIO device given
481 * a mdio_board_info structure
482 * @bus: MDIO bus to create the devices on
483 * @bi: mdio_board_info structure describing the devices
484 *
485 * Returns 0 on success or < 0 on error.
486 */
mdiobus_create_device(struct mii_bus * bus,struct mdio_board_info * bi)487 static int mdiobus_create_device(struct mii_bus *bus,
488 struct mdio_board_info *bi)
489 {
490 struct mdio_device *mdiodev;
491 int ret = 0;
492
493 mdiodev = mdio_device_create(bus, bi->mdio_addr);
494 if (IS_ERR(mdiodev))
495 return -ENODEV;
496
497 strncpy(mdiodev->modalias, bi->modalias,
498 sizeof(mdiodev->modalias));
499 mdiodev->bus_match = mdio_device_bus_match;
500 mdiodev->dev.platform_data = (void *)bi->platform_data;
501
502 ret = mdio_device_register(mdiodev);
503 if (ret)
504 mdio_device_free(mdiodev);
505
506 return ret;
507 }
508
509 /**
510 * __mdiobus_register - bring up all the PHYs on a given bus and attach them to bus
511 * @bus: target mii_bus
512 * @owner: module containing bus accessor functions
513 *
514 * Description: Called by a bus driver to bring up all the PHYs
515 * on a given bus, and attach them to the bus. Drivers should use
516 * mdiobus_register() rather than __mdiobus_register() unless they
517 * need to pass a specific owner module. MDIO devices which are not
518 * PHYs will not be brought up by this function. They are expected
519 * to be explicitly listed in DT and instantiated by of_mdiobus_register().
520 *
521 * Returns 0 on success or < 0 on error.
522 */
__mdiobus_register(struct mii_bus * bus,struct module * owner)523 int __mdiobus_register(struct mii_bus *bus, struct module *owner)
524 {
525 struct mdio_device *mdiodev;
526 int i, err;
527 struct gpio_desc *gpiod;
528
529 if (NULL == bus || NULL == bus->name ||
530 NULL == bus->read || NULL == bus->write)
531 return -EINVAL;
532
533 if (bus->parent && bus->parent->of_node)
534 bus->parent->of_node->fwnode.flags |=
535 FWNODE_FLAG_NEEDS_CHILD_BOUND_ON_ADD;
536
537 BUG_ON(bus->state != MDIOBUS_ALLOCATED &&
538 bus->state != MDIOBUS_UNREGISTERED);
539
540 bus->owner = owner;
541 bus->dev.parent = bus->parent;
542 bus->dev.class = &mdio_bus_class;
543 bus->dev.groups = NULL;
544 dev_set_name(&bus->dev, "%s", bus->id);
545
546 /* We need to set state to MDIOBUS_UNREGISTERED to correctly release
547 * the device in mdiobus_free()
548 *
549 * State will be updated later in this function in case of success
550 */
551 bus->state = MDIOBUS_UNREGISTERED;
552
553 err = device_register(&bus->dev);
554 if (err) {
555 pr_err("mii_bus %s failed to register\n", bus->id);
556 return -EINVAL;
557 }
558
559 mutex_init(&bus->mdio_lock);
560 mutex_init(&bus->shared_lock);
561
562 /* assert bus level PHY GPIO reset */
563 gpiod = devm_gpiod_get_optional(&bus->dev, "reset", GPIOD_OUT_HIGH);
564 if (IS_ERR(gpiod)) {
565 err = dev_err_probe(&bus->dev, PTR_ERR(gpiod),
566 "mii_bus %s couldn't get reset GPIO\n",
567 bus->id);
568 device_del(&bus->dev);
569 return err;
570 } else if (gpiod) {
571 bus->reset_gpiod = gpiod;
572 fsleep(bus->reset_delay_us);
573 gpiod_set_value_cansleep(gpiod, 0);
574 if (bus->reset_post_delay_us > 0)
575 fsleep(bus->reset_post_delay_us);
576 }
577
578 if (bus->reset) {
579 err = bus->reset(bus);
580 if (err)
581 goto error_reset_gpiod;
582 }
583
584 for (i = 0; i < PHY_MAX_ADDR; i++) {
585 if ((bus->phy_mask & BIT(i)) == 0) {
586 struct phy_device *phydev;
587
588 phydev = mdiobus_scan(bus, i);
589 if (IS_ERR(phydev) && (PTR_ERR(phydev) != -ENODEV)) {
590 err = PTR_ERR(phydev);
591 goto error;
592 }
593 }
594 }
595
596 mdiobus_setup_mdiodev_from_board_info(bus, mdiobus_create_device);
597
598 bus->state = MDIOBUS_REGISTERED;
599 dev_dbg(&bus->dev, "probed\n");
600 return 0;
601
602 error:
603 while (--i >= 0) {
604 mdiodev = bus->mdio_map[i];
605 if (!mdiodev)
606 continue;
607
608 mdiodev->device_remove(mdiodev);
609 mdiodev->device_free(mdiodev);
610 }
611 error_reset_gpiod:
612 /* Put PHYs in RESET to save power */
613 if (bus->reset_gpiod)
614 gpiod_set_value_cansleep(bus->reset_gpiod, 1);
615
616 device_del(&bus->dev);
617 return err;
618 }
619 EXPORT_SYMBOL(__mdiobus_register);
620
mdiobus_unregister(struct mii_bus * bus)621 void mdiobus_unregister(struct mii_bus *bus)
622 {
623 struct mdio_device *mdiodev;
624 int i;
625
626 if (WARN_ON_ONCE(bus->state != MDIOBUS_REGISTERED))
627 return;
628 bus->state = MDIOBUS_UNREGISTERED;
629
630 for (i = 0; i < PHY_MAX_ADDR; i++) {
631 mdiodev = bus->mdio_map[i];
632 if (!mdiodev)
633 continue;
634
635 if (mdiodev->reset_gpio)
636 gpiod_put(mdiodev->reset_gpio);
637
638 mdiodev->device_remove(mdiodev);
639 mdiodev->device_free(mdiodev);
640 }
641
642 /* Put PHYs in RESET to save power */
643 if (bus->reset_gpiod)
644 gpiod_set_value_cansleep(bus->reset_gpiod, 1);
645
646 device_del(&bus->dev);
647 }
648 EXPORT_SYMBOL(mdiobus_unregister);
649
650 /**
651 * mdiobus_free - free a struct mii_bus
652 * @bus: mii_bus to free
653 *
654 * This function releases the reference to the underlying device
655 * object in the mii_bus. If this is the last reference, the mii_bus
656 * will be freed.
657 */
mdiobus_free(struct mii_bus * bus)658 void mdiobus_free(struct mii_bus *bus)
659 {
660 /* For compatibility with error handling in drivers. */
661 if (bus->state == MDIOBUS_ALLOCATED) {
662 kfree(bus);
663 return;
664 }
665
666 BUG_ON(bus->state != MDIOBUS_UNREGISTERED);
667 bus->state = MDIOBUS_RELEASED;
668
669 put_device(&bus->dev);
670 }
671 EXPORT_SYMBOL(mdiobus_free);
672
673 /**
674 * mdiobus_scan - scan a bus for MDIO devices.
675 * @bus: mii_bus to scan
676 * @addr: address on bus to scan
677 *
678 * This function scans the MDIO bus, looking for devices which can be
679 * identified using a vendor/product ID in registers 2 and 3. Not all
680 * MDIO devices have such registers, but PHY devices typically
681 * do. Hence this function assumes anything found is a PHY, or can be
682 * treated as a PHY. Other MDIO devices, such as switches, will
683 * probably not be found during the scan.
684 */
mdiobus_scan(struct mii_bus * bus,int addr)685 struct phy_device *mdiobus_scan(struct mii_bus *bus, int addr)
686 {
687 struct phy_device *phydev = ERR_PTR(-ENODEV);
688 int err;
689
690 switch (bus->probe_capabilities) {
691 case MDIOBUS_NO_CAP:
692 case MDIOBUS_C22:
693 phydev = get_phy_device(bus, addr, false);
694 break;
695 case MDIOBUS_C45:
696 phydev = get_phy_device(bus, addr, true);
697 break;
698 case MDIOBUS_C22_C45:
699 phydev = get_phy_device(bus, addr, false);
700 if (IS_ERR(phydev))
701 phydev = get_phy_device(bus, addr, true);
702 break;
703 }
704
705 if (IS_ERR(phydev))
706 return phydev;
707
708 /*
709 * For DT, see if the auto-probed phy has a correspoding child
710 * in the bus node, and set the of_node pointer in this case.
711 */
712 of_mdiobus_link_mdiodev(bus, &phydev->mdio);
713
714 err = phy_device_register(phydev);
715 if (err) {
716 phy_device_free(phydev);
717 return ERR_PTR(-ENODEV);
718 }
719
720 return phydev;
721 }
722 EXPORT_SYMBOL(mdiobus_scan);
723
mdiobus_stats_acct(struct mdio_bus_stats * stats,bool op,int ret)724 static void mdiobus_stats_acct(struct mdio_bus_stats *stats, bool op, int ret)
725 {
726 preempt_disable();
727 u64_stats_update_begin(&stats->syncp);
728
729 u64_stats_inc(&stats->transfers);
730 if (ret < 0) {
731 u64_stats_inc(&stats->errors);
732 goto out;
733 }
734
735 if (op)
736 u64_stats_inc(&stats->reads);
737 else
738 u64_stats_inc(&stats->writes);
739 out:
740 u64_stats_update_end(&stats->syncp);
741 preempt_enable();
742 }
743
744 /**
745 * __mdiobus_read - Unlocked version of the mdiobus_read function
746 * @bus: the mii_bus struct
747 * @addr: the phy address
748 * @regnum: register number to read
749 *
750 * Read a MDIO bus register. Caller must hold the mdio bus lock.
751 *
752 * NOTE: MUST NOT be called from interrupt context.
753 */
__mdiobus_read(struct mii_bus * bus,int addr,u32 regnum)754 int __mdiobus_read(struct mii_bus *bus, int addr, u32 regnum)
755 {
756 int retval;
757
758 lockdep_assert_held_once(&bus->mdio_lock);
759
760 retval = bus->read(bus, addr, regnum);
761
762 trace_mdio_access(bus, 1, addr, regnum, retval, retval);
763 mdiobus_stats_acct(&bus->stats[addr], true, retval);
764
765 return retval;
766 }
767 EXPORT_SYMBOL(__mdiobus_read);
768
769 /**
770 * __mdiobus_write - Unlocked version of the mdiobus_write function
771 * @bus: the mii_bus struct
772 * @addr: the phy address
773 * @regnum: register number to write
774 * @val: value to write to @regnum
775 *
776 * Write a MDIO bus register. Caller must hold the mdio bus lock.
777 *
778 * NOTE: MUST NOT be called from interrupt context.
779 */
__mdiobus_write(struct mii_bus * bus,int addr,u32 regnum,u16 val)780 int __mdiobus_write(struct mii_bus *bus, int addr, u32 regnum, u16 val)
781 {
782 int err;
783
784 lockdep_assert_held_once(&bus->mdio_lock);
785
786 err = bus->write(bus, addr, regnum, val);
787
788 trace_mdio_access(bus, 0, addr, regnum, val, err);
789 mdiobus_stats_acct(&bus->stats[addr], false, err);
790
791 return err;
792 }
793 EXPORT_SYMBOL(__mdiobus_write);
794
795 /**
796 * __mdiobus_modify_changed - Unlocked version of the mdiobus_modify function
797 * @bus: the mii_bus struct
798 * @addr: the phy address
799 * @regnum: register number to modify
800 * @mask: bit mask of bits to clear
801 * @set: bit mask of bits to set
802 *
803 * Read, modify, and if any change, write the register value back to the
804 * device. Any error returns a negative number.
805 *
806 * NOTE: MUST NOT be called from interrupt context.
807 */
__mdiobus_modify_changed(struct mii_bus * bus,int addr,u32 regnum,u16 mask,u16 set)808 int __mdiobus_modify_changed(struct mii_bus *bus, int addr, u32 regnum,
809 u16 mask, u16 set)
810 {
811 int new, ret;
812
813 ret = __mdiobus_read(bus, addr, regnum);
814 if (ret < 0)
815 return ret;
816
817 new = (ret & ~mask) | set;
818 if (new == ret)
819 return 0;
820
821 ret = __mdiobus_write(bus, addr, regnum, new);
822
823 return ret < 0 ? ret : 1;
824 }
825 EXPORT_SYMBOL_GPL(__mdiobus_modify_changed);
826
827 /**
828 * mdiobus_read_nested - Nested version of the mdiobus_read function
829 * @bus: the mii_bus struct
830 * @addr: the phy address
831 * @regnum: register number to read
832 *
833 * In case of nested MDIO bus access avoid lockdep false positives by
834 * using mutex_lock_nested().
835 *
836 * NOTE: MUST NOT be called from interrupt context,
837 * because the bus read/write functions may wait for an interrupt
838 * to conclude the operation.
839 */
mdiobus_read_nested(struct mii_bus * bus,int addr,u32 regnum)840 int mdiobus_read_nested(struct mii_bus *bus, int addr, u32 regnum)
841 {
842 int retval;
843
844 mutex_lock_nested(&bus->mdio_lock, MDIO_MUTEX_NESTED);
845 retval = __mdiobus_read(bus, addr, regnum);
846 mutex_unlock(&bus->mdio_lock);
847
848 return retval;
849 }
850 EXPORT_SYMBOL(mdiobus_read_nested);
851
852 /**
853 * mdiobus_read - Convenience function for reading a given MII mgmt register
854 * @bus: the mii_bus struct
855 * @addr: the phy address
856 * @regnum: register number to read
857 *
858 * NOTE: MUST NOT be called from interrupt context,
859 * because the bus read/write functions may wait for an interrupt
860 * to conclude the operation.
861 */
mdiobus_read(struct mii_bus * bus,int addr,u32 regnum)862 int mdiobus_read(struct mii_bus *bus, int addr, u32 regnum)
863 {
864 int retval;
865
866 mutex_lock(&bus->mdio_lock);
867 retval = __mdiobus_read(bus, addr, regnum);
868 mutex_unlock(&bus->mdio_lock);
869
870 return retval;
871 }
872 EXPORT_SYMBOL(mdiobus_read);
873
874 /**
875 * mdiobus_write_nested - Nested version of the mdiobus_write function
876 * @bus: the mii_bus struct
877 * @addr: the phy address
878 * @regnum: register number to write
879 * @val: value to write to @regnum
880 *
881 * In case of nested MDIO bus access avoid lockdep false positives by
882 * using mutex_lock_nested().
883 *
884 * NOTE: MUST NOT be called from interrupt context,
885 * because the bus read/write functions may wait for an interrupt
886 * to conclude the operation.
887 */
mdiobus_write_nested(struct mii_bus * bus,int addr,u32 regnum,u16 val)888 int mdiobus_write_nested(struct mii_bus *bus, int addr, u32 regnum, u16 val)
889 {
890 int err;
891
892 mutex_lock_nested(&bus->mdio_lock, MDIO_MUTEX_NESTED);
893 err = __mdiobus_write(bus, addr, regnum, val);
894 mutex_unlock(&bus->mdio_lock);
895
896 return err;
897 }
898 EXPORT_SYMBOL(mdiobus_write_nested);
899
900 /**
901 * mdiobus_write - Convenience function for writing a given MII mgmt register
902 * @bus: the mii_bus struct
903 * @addr: the phy address
904 * @regnum: register number to write
905 * @val: value to write to @regnum
906 *
907 * NOTE: MUST NOT be called from interrupt context,
908 * because the bus read/write functions may wait for an interrupt
909 * to conclude the operation.
910 */
mdiobus_write(struct mii_bus * bus,int addr,u32 regnum,u16 val)911 int mdiobus_write(struct mii_bus *bus, int addr, u32 regnum, u16 val)
912 {
913 int err;
914
915 mutex_lock(&bus->mdio_lock);
916 err = __mdiobus_write(bus, addr, regnum, val);
917 mutex_unlock(&bus->mdio_lock);
918
919 return err;
920 }
921 EXPORT_SYMBOL(mdiobus_write);
922
923 /**
924 * mdiobus_modify - Convenience function for modifying a given mdio device
925 * register
926 * @bus: the mii_bus struct
927 * @addr: the phy address
928 * @regnum: register number to write
929 * @mask: bit mask of bits to clear
930 * @set: bit mask of bits to set
931 */
mdiobus_modify(struct mii_bus * bus,int addr,u32 regnum,u16 mask,u16 set)932 int mdiobus_modify(struct mii_bus *bus, int addr, u32 regnum, u16 mask, u16 set)
933 {
934 int err;
935
936 mutex_lock(&bus->mdio_lock);
937 err = __mdiobus_modify_changed(bus, addr, regnum, mask, set);
938 mutex_unlock(&bus->mdio_lock);
939
940 return err < 0 ? err : 0;
941 }
942 EXPORT_SYMBOL_GPL(mdiobus_modify);
943
944 /**
945 * mdio_bus_match - determine if given MDIO driver supports the given
946 * MDIO device
947 * @dev: target MDIO device
948 * @drv: given MDIO driver
949 *
950 * Description: Given a MDIO device, and a MDIO driver, return 1 if
951 * the driver supports the device. Otherwise, return 0. This may
952 * require calling the devices own match function, since different classes
953 * of MDIO devices have different match criteria.
954 */
mdio_bus_match(struct device * dev,struct device_driver * drv)955 static int mdio_bus_match(struct device *dev, struct device_driver *drv)
956 {
957 struct mdio_device *mdio = to_mdio_device(dev);
958
959 if (of_driver_match_device(dev, drv))
960 return 1;
961
962 if (mdio->bus_match)
963 return mdio->bus_match(dev, drv);
964
965 return 0;
966 }
967
mdio_uevent(struct device * dev,struct kobj_uevent_env * env)968 static int mdio_uevent(struct device *dev, struct kobj_uevent_env *env)
969 {
970 int rc;
971
972 /* Some devices have extra OF data and an OF-style MODALIAS */
973 rc = of_device_uevent_modalias(dev, env);
974 if (rc != -ENODEV)
975 return rc;
976
977 return 0;
978 }
979
980 static struct attribute *mdio_bus_device_statistics_attrs[] = {
981 &dev_attr_mdio_bus_device_transfers.attr.attr,
982 &dev_attr_mdio_bus_device_errors.attr.attr,
983 &dev_attr_mdio_bus_device_writes.attr.attr,
984 &dev_attr_mdio_bus_device_reads.attr.attr,
985 NULL,
986 };
987
988 static const struct attribute_group mdio_bus_device_statistics_group = {
989 .name = "statistics",
990 .attrs = mdio_bus_device_statistics_attrs,
991 };
992
993 static const struct attribute_group *mdio_bus_dev_groups[] = {
994 &mdio_bus_device_statistics_group,
995 NULL,
996 };
997
998 struct bus_type mdio_bus_type = {
999 .name = "mdio_bus",
1000 .dev_groups = mdio_bus_dev_groups,
1001 .match = mdio_bus_match,
1002 .uevent = mdio_uevent,
1003 };
1004 EXPORT_SYMBOL(mdio_bus_type);
1005
mdio_bus_init(void)1006 int __init mdio_bus_init(void)
1007 {
1008 int ret;
1009
1010 ret = class_register(&mdio_bus_class);
1011 if (!ret) {
1012 ret = bus_register(&mdio_bus_type);
1013 if (ret)
1014 class_unregister(&mdio_bus_class);
1015 }
1016
1017 return ret;
1018 }
1019
1020 #if IS_ENABLED(CONFIG_PHYLIB)
mdio_bus_exit(void)1021 void mdio_bus_exit(void)
1022 {
1023 class_unregister(&mdio_bus_class);
1024 bus_unregister(&mdio_bus_type);
1025 }
1026 EXPORT_SYMBOL_GPL(mdio_bus_exit);
1027 #else
1028 module_init(mdio_bus_init);
1029 /* no module_exit, intentional */
1030 MODULE_LICENSE("GPL");
1031 MODULE_DESCRIPTION("MDIO bus/device layer");
1032 #endif
1033