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