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