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 dev->of_node = child;
467 dev->fwnode = 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_from_board_info - 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 to
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 BUG_ON(bus->state != MDIOBUS_ALLOCATED &&
534 bus->state != MDIOBUS_UNREGISTERED);
535
536 bus->owner = owner;
537 bus->dev.parent = bus->parent;
538 bus->dev.class = &mdio_bus_class;
539 bus->dev.groups = NULL;
540 dev_set_name(&bus->dev, "%s", bus->id);
541
542 /* We need to set state to MDIOBUS_UNREGISTERED to correctly release
543 * the device in mdiobus_free()
544 *
545 * State will be updated later in this function in case of success
546 */
547 bus->state = MDIOBUS_UNREGISTERED;
548
549 err = device_register(&bus->dev);
550 if (err) {
551 pr_err("mii_bus %s failed to register\n", bus->id);
552 return -EINVAL;
553 }
554
555 mutex_init(&bus->mdio_lock);
556 mutex_init(&bus->shared_lock);
557
558 /* de-assert bus level PHY GPIO reset */
559 gpiod = devm_gpiod_get_optional(&bus->dev, "reset", GPIOD_OUT_LOW);
560 if (IS_ERR(gpiod)) {
561 dev_err(&bus->dev, "mii_bus %s couldn't get reset GPIO\n",
562 bus->id);
563 device_del(&bus->dev);
564 return PTR_ERR(gpiod);
565 } else if (gpiod) {
566 bus->reset_gpiod = gpiod;
567
568 gpiod_set_value_cansleep(gpiod, 1);
569 fsleep(bus->reset_delay_us);
570 gpiod_set_value_cansleep(gpiod, 0);
571 if (bus->reset_post_delay_us > 0)
572 fsleep(bus->reset_post_delay_us);
573 }
574
575 if (bus->reset) {
576 err = bus->reset(bus);
577 if (err)
578 goto error_reset_gpiod;
579 }
580
581 for (i = 0; i < PHY_MAX_ADDR; i++) {
582 if ((bus->phy_mask & BIT(i)) == 0) {
583 struct phy_device *phydev;
584
585 phydev = mdiobus_scan(bus, i);
586 if (IS_ERR(phydev) && (PTR_ERR(phydev) != -ENODEV)) {
587 err = PTR_ERR(phydev);
588 goto error;
589 }
590 }
591 }
592
593 mdiobus_setup_mdiodev_from_board_info(bus, mdiobus_create_device);
594
595 bus->state = MDIOBUS_REGISTERED;
596 dev_dbg(&bus->dev, "probed\n");
597 return 0;
598
599 error:
600 while (--i >= 0) {
601 mdiodev = bus->mdio_map[i];
602 if (!mdiodev)
603 continue;
604
605 mdiodev->device_remove(mdiodev);
606 mdiodev->device_free(mdiodev);
607 }
608 error_reset_gpiod:
609 /* Put PHYs in RESET to save power */
610 if (bus->reset_gpiod)
611 gpiod_set_value_cansleep(bus->reset_gpiod, 1);
612
613 device_del(&bus->dev);
614 return err;
615 }
616 EXPORT_SYMBOL(__mdiobus_register);
617
mdiobus_unregister(struct mii_bus * bus)618 void mdiobus_unregister(struct mii_bus *bus)
619 {
620 struct mdio_device *mdiodev;
621 int i;
622
623 if (WARN_ON_ONCE(bus->state != MDIOBUS_REGISTERED))
624 return;
625 bus->state = MDIOBUS_UNREGISTERED;
626
627 for (i = 0; i < PHY_MAX_ADDR; i++) {
628 mdiodev = bus->mdio_map[i];
629 if (!mdiodev)
630 continue;
631
632 if (mdiodev->reset_gpio)
633 gpiod_put(mdiodev->reset_gpio);
634
635 mdiodev->device_remove(mdiodev);
636 mdiodev->device_free(mdiodev);
637 }
638
639 /* Put PHYs in RESET to save power */
640 if (bus->reset_gpiod)
641 gpiod_set_value_cansleep(bus->reset_gpiod, 1);
642
643 device_del(&bus->dev);
644 }
645 EXPORT_SYMBOL(mdiobus_unregister);
646
647 /**
648 * mdiobus_free - free a struct mii_bus
649 * @bus: mii_bus to free
650 *
651 * This function releases the reference to the underlying device
652 * object in the mii_bus. If this is the last reference, the mii_bus
653 * will be freed.
654 */
mdiobus_free(struct mii_bus * bus)655 void mdiobus_free(struct mii_bus *bus)
656 {
657 /* For compatibility with error handling in drivers. */
658 if (bus->state == MDIOBUS_ALLOCATED) {
659 kfree(bus);
660 return;
661 }
662
663 BUG_ON(bus->state != MDIOBUS_UNREGISTERED);
664 bus->state = MDIOBUS_RELEASED;
665
666 put_device(&bus->dev);
667 }
668 EXPORT_SYMBOL(mdiobus_free);
669
670 /**
671 * mdiobus_scan - scan a bus for MDIO devices.
672 * @bus: mii_bus to scan
673 * @addr: address on bus to scan
674 *
675 * This function scans the MDIO bus, looking for devices which can be
676 * identified using a vendor/product ID in registers 2 and 3. Not all
677 * MDIO devices have such registers, but PHY devices typically
678 * do. Hence this function assumes anything found is a PHY, or can be
679 * treated as a PHY. Other MDIO devices, such as switches, will
680 * probably not be found during the scan.
681 */
mdiobus_scan(struct mii_bus * bus,int addr)682 struct phy_device *mdiobus_scan(struct mii_bus *bus, int addr)
683 {
684 struct phy_device *phydev = ERR_PTR(-ENODEV);
685 int err;
686
687 switch (bus->probe_capabilities) {
688 case MDIOBUS_NO_CAP:
689 case MDIOBUS_C22:
690 phydev = get_phy_device(bus, addr, false);
691 break;
692 case MDIOBUS_C45:
693 phydev = get_phy_device(bus, addr, true);
694 break;
695 case MDIOBUS_C22_C45:
696 phydev = get_phy_device(bus, addr, false);
697 if (IS_ERR(phydev))
698 phydev = get_phy_device(bus, addr, true);
699 break;
700 }
701
702 if (IS_ERR(phydev))
703 return phydev;
704
705 /*
706 * For DT, see if the auto-probed phy has a correspoding child
707 * in the bus node, and set the of_node pointer in this case.
708 */
709 of_mdiobus_link_mdiodev(bus, &phydev->mdio);
710
711 err = phy_device_register(phydev);
712 if (err) {
713 phy_device_free(phydev);
714 return ERR_PTR(-ENODEV);
715 }
716
717 return phydev;
718 }
719 EXPORT_SYMBOL(mdiobus_scan);
720
mdiobus_stats_acct(struct mdio_bus_stats * stats,bool op,int ret)721 static void mdiobus_stats_acct(struct mdio_bus_stats *stats, bool op, int ret)
722 {
723 preempt_disable();
724 u64_stats_update_begin(&stats->syncp);
725
726 u64_stats_inc(&stats->transfers);
727 if (ret < 0) {
728 u64_stats_inc(&stats->errors);
729 goto out;
730 }
731
732 if (op)
733 u64_stats_inc(&stats->reads);
734 else
735 u64_stats_inc(&stats->writes);
736 out:
737 u64_stats_update_end(&stats->syncp);
738 preempt_enable();
739 }
740
741 /**
742 * __mdiobus_read - Unlocked version of the mdiobus_read function
743 * @bus: the mii_bus struct
744 * @addr: the phy address
745 * @regnum: register number to read
746 *
747 * Read a MDIO bus register. Caller must hold the mdio bus lock.
748 *
749 * NOTE: MUST NOT be called from interrupt context.
750 */
__mdiobus_read(struct mii_bus * bus,int addr,u32 regnum)751 int __mdiobus_read(struct mii_bus *bus, int addr, u32 regnum)
752 {
753 int retval;
754
755 WARN_ON_ONCE(!mutex_is_locked(&bus->mdio_lock));
756
757 retval = bus->read(bus, addr, regnum);
758
759 trace_mdio_access(bus, 1, addr, regnum, retval, retval);
760 mdiobus_stats_acct(&bus->stats[addr], true, retval);
761
762 return retval;
763 }
764 EXPORT_SYMBOL(__mdiobus_read);
765
766 /**
767 * __mdiobus_write - Unlocked version of the mdiobus_write function
768 * @bus: the mii_bus struct
769 * @addr: the phy address
770 * @regnum: register number to write
771 * @val: value to write to @regnum
772 *
773 * Write a MDIO bus register. Caller must hold the mdio bus lock.
774 *
775 * NOTE: MUST NOT be called from interrupt context.
776 */
__mdiobus_write(struct mii_bus * bus,int addr,u32 regnum,u16 val)777 int __mdiobus_write(struct mii_bus *bus, int addr, u32 regnum, u16 val)
778 {
779 int err;
780
781 WARN_ON_ONCE(!mutex_is_locked(&bus->mdio_lock));
782
783 err = bus->write(bus, addr, regnum, val);
784
785 trace_mdio_access(bus, 0, addr, regnum, val, err);
786 mdiobus_stats_acct(&bus->stats[addr], false, err);
787
788 return err;
789 }
790 EXPORT_SYMBOL(__mdiobus_write);
791
792 /**
793 * __mdiobus_modify_changed - Unlocked version of the mdiobus_modify function
794 * @bus: the mii_bus struct
795 * @addr: the phy address
796 * @regnum: register number to modify
797 * @mask: bit mask of bits to clear
798 * @set: bit mask of bits to set
799 *
800 * Read, modify, and if any change, write the register value back to the
801 * device. Any error returns a negative number.
802 *
803 * NOTE: MUST NOT be called from interrupt context.
804 */
__mdiobus_modify_changed(struct mii_bus * bus,int addr,u32 regnum,u16 mask,u16 set)805 int __mdiobus_modify_changed(struct mii_bus *bus, int addr, u32 regnum,
806 u16 mask, u16 set)
807 {
808 int new, ret;
809
810 ret = __mdiobus_read(bus, addr, regnum);
811 if (ret < 0)
812 return ret;
813
814 new = (ret & ~mask) | set;
815 if (new == ret)
816 return 0;
817
818 ret = __mdiobus_write(bus, addr, regnum, new);
819
820 return ret < 0 ? ret : 1;
821 }
822 EXPORT_SYMBOL_GPL(__mdiobus_modify_changed);
823
824 /**
825 * mdiobus_read_nested - Nested version of the mdiobus_read function
826 * @bus: the mii_bus struct
827 * @addr: the phy address
828 * @regnum: register number to read
829 *
830 * In case of nested MDIO bus access avoid lockdep false positives by
831 * using mutex_lock_nested().
832 *
833 * NOTE: MUST NOT be called from interrupt context,
834 * because the bus read/write functions may wait for an interrupt
835 * to conclude the operation.
836 */
mdiobus_read_nested(struct mii_bus * bus,int addr,u32 regnum)837 int mdiobus_read_nested(struct mii_bus *bus, int addr, u32 regnum)
838 {
839 int retval;
840
841 mutex_lock_nested(&bus->mdio_lock, MDIO_MUTEX_NESTED);
842 retval = __mdiobus_read(bus, addr, regnum);
843 mutex_unlock(&bus->mdio_lock);
844
845 return retval;
846 }
847 EXPORT_SYMBOL(mdiobus_read_nested);
848
849 /**
850 * mdiobus_read - Convenience function for reading a given MII mgmt register
851 * @bus: the mii_bus struct
852 * @addr: the phy address
853 * @regnum: register number to read
854 *
855 * NOTE: MUST NOT be called from interrupt context,
856 * because the bus read/write functions may wait for an interrupt
857 * to conclude the operation.
858 */
mdiobus_read(struct mii_bus * bus,int addr,u32 regnum)859 int mdiobus_read(struct mii_bus *bus, int addr, u32 regnum)
860 {
861 int retval;
862
863 mutex_lock(&bus->mdio_lock);
864 retval = __mdiobus_read(bus, addr, regnum);
865 mutex_unlock(&bus->mdio_lock);
866
867 return retval;
868 }
869 EXPORT_SYMBOL(mdiobus_read);
870
871 /**
872 * mdiobus_write_nested - Nested version of the mdiobus_write function
873 * @bus: the mii_bus struct
874 * @addr: the phy address
875 * @regnum: register number to write
876 * @val: value to write to @regnum
877 *
878 * In case of nested MDIO bus access avoid lockdep false positives by
879 * using mutex_lock_nested().
880 *
881 * NOTE: MUST NOT be called from interrupt context,
882 * because the bus read/write functions may wait for an interrupt
883 * to conclude the operation.
884 */
mdiobus_write_nested(struct mii_bus * bus,int addr,u32 regnum,u16 val)885 int mdiobus_write_nested(struct mii_bus *bus, int addr, u32 regnum, u16 val)
886 {
887 int err;
888
889 mutex_lock_nested(&bus->mdio_lock, MDIO_MUTEX_NESTED);
890 err = __mdiobus_write(bus, addr, regnum, val);
891 mutex_unlock(&bus->mdio_lock);
892
893 return err;
894 }
895 EXPORT_SYMBOL(mdiobus_write_nested);
896
897 /**
898 * mdiobus_write - Convenience function for writing a given MII mgmt register
899 * @bus: the mii_bus struct
900 * @addr: the phy address
901 * @regnum: register number to write
902 * @val: value to write to @regnum
903 *
904 * NOTE: MUST NOT be called from interrupt context,
905 * because the bus read/write functions may wait for an interrupt
906 * to conclude the operation.
907 */
mdiobus_write(struct mii_bus * bus,int addr,u32 regnum,u16 val)908 int mdiobus_write(struct mii_bus *bus, int addr, u32 regnum, u16 val)
909 {
910 int err;
911
912 mutex_lock(&bus->mdio_lock);
913 err = __mdiobus_write(bus, addr, regnum, val);
914 mutex_unlock(&bus->mdio_lock);
915
916 return err;
917 }
918 EXPORT_SYMBOL(mdiobus_write);
919
920 /**
921 * mdiobus_modify - Convenience function for modifying a given mdio device
922 * register
923 * @bus: the mii_bus struct
924 * @addr: the phy address
925 * @regnum: register number to write
926 * @mask: bit mask of bits to clear
927 * @set: bit mask of bits to set
928 */
mdiobus_modify(struct mii_bus * bus,int addr,u32 regnum,u16 mask,u16 set)929 int mdiobus_modify(struct mii_bus *bus, int addr, u32 regnum, u16 mask, u16 set)
930 {
931 int err;
932
933 mutex_lock(&bus->mdio_lock);
934 err = __mdiobus_modify_changed(bus, addr, regnum, mask, set);
935 mutex_unlock(&bus->mdio_lock);
936
937 return err < 0 ? err : 0;
938 }
939 EXPORT_SYMBOL_GPL(mdiobus_modify);
940
941 /**
942 * mdio_bus_match - determine if given MDIO driver supports the given
943 * MDIO device
944 * @dev: target MDIO device
945 * @drv: given MDIO driver
946 *
947 * Description: Given a MDIO device, and a MDIO driver, return 1 if
948 * the driver supports the device. Otherwise, return 0. This may
949 * require calling the devices own match function, since different classes
950 * of MDIO devices have different match criteria.
951 */
mdio_bus_match(struct device * dev,struct device_driver * drv)952 static int mdio_bus_match(struct device *dev, struct device_driver *drv)
953 {
954 struct mdio_device *mdio = to_mdio_device(dev);
955
956 if (of_driver_match_device(dev, drv))
957 return 1;
958
959 if (mdio->bus_match)
960 return mdio->bus_match(dev, drv);
961
962 return 0;
963 }
964
mdio_uevent(struct device * dev,struct kobj_uevent_env * env)965 static int mdio_uevent(struct device *dev, struct kobj_uevent_env *env)
966 {
967 int rc;
968
969 /* Some devices have extra OF data and an OF-style MODALIAS */
970 rc = of_device_uevent_modalias(dev, env);
971 if (rc != -ENODEV)
972 return rc;
973
974 return 0;
975 }
976
977 static struct attribute *mdio_bus_device_statistics_attrs[] = {
978 &dev_attr_mdio_bus_device_transfers.attr.attr,
979 &dev_attr_mdio_bus_device_errors.attr.attr,
980 &dev_attr_mdio_bus_device_writes.attr.attr,
981 &dev_attr_mdio_bus_device_reads.attr.attr,
982 NULL,
983 };
984
985 static const struct attribute_group mdio_bus_device_statistics_group = {
986 .name = "statistics",
987 .attrs = mdio_bus_device_statistics_attrs,
988 };
989
990 static const struct attribute_group *mdio_bus_dev_groups[] = {
991 &mdio_bus_device_statistics_group,
992 NULL,
993 };
994
995 struct bus_type mdio_bus_type = {
996 .name = "mdio_bus",
997 .dev_groups = mdio_bus_dev_groups,
998 .match = mdio_bus_match,
999 .uevent = mdio_uevent,
1000 };
1001 EXPORT_SYMBOL(mdio_bus_type);
1002
mdio_bus_init(void)1003 int __init mdio_bus_init(void)
1004 {
1005 int ret;
1006
1007 ret = class_register(&mdio_bus_class);
1008 if (!ret) {
1009 ret = bus_register(&mdio_bus_type);
1010 if (ret)
1011 class_unregister(&mdio_bus_class);
1012 }
1013
1014 return ret;
1015 }
1016
1017 #if IS_ENABLED(CONFIG_PHYLIB)
mdio_bus_exit(void)1018 void mdio_bus_exit(void)
1019 {
1020 class_unregister(&mdio_bus_class);
1021 bus_unregister(&mdio_bus_type);
1022 }
1023 EXPORT_SYMBOL_GPL(mdio_bus_exit);
1024 #else
1025 module_init(mdio_bus_init);
1026 /* no module_exit, intentional */
1027 MODULE_LICENSE("GPL");
1028 MODULE_DESCRIPTION("MDIO bus/device layer");
1029 #endif
1030