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