xref: /openbmc/linux/drivers/net/phy/mdio_bus.c (revision 8365a898)
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 	/* 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;
743 	int err;
744 
745 	phydev = get_phy_device(bus, addr, false);
746 	if (IS_ERR(phydev))
747 		return phydev;
748 
749 	/*
750 	 * For DT, see if the auto-probed phy has a correspoding child
751 	 * in the bus node, and set the of_node pointer in this case.
752 	 */
753 	of_mdiobus_link_mdiodev(bus, &phydev->mdio);
754 
755 	err = phy_device_register(phydev);
756 	if (err) {
757 		phy_device_free(phydev);
758 		return ERR_PTR(-ENODEV);
759 	}
760 
761 	return phydev;
762 }
763 EXPORT_SYMBOL(mdiobus_scan);
764 
765 static void mdiobus_stats_acct(struct mdio_bus_stats *stats, bool op, int ret)
766 {
767 	preempt_disable();
768 	u64_stats_update_begin(&stats->syncp);
769 
770 	u64_stats_inc(&stats->transfers);
771 	if (ret < 0) {
772 		u64_stats_inc(&stats->errors);
773 		goto out;
774 	}
775 
776 	if (op)
777 		u64_stats_inc(&stats->reads);
778 	else
779 		u64_stats_inc(&stats->writes);
780 out:
781 	u64_stats_update_end(&stats->syncp);
782 	preempt_enable();
783 }
784 
785 /**
786  * __mdiobus_read - Unlocked version of the mdiobus_read function
787  * @bus: the mii_bus struct
788  * @addr: the phy address
789  * @regnum: register number to read
790  *
791  * Read a MDIO bus register. Caller must hold the mdio bus lock.
792  *
793  * NOTE: MUST NOT be called from interrupt context.
794  */
795 int __mdiobus_read(struct mii_bus *bus, int addr, u32 regnum)
796 {
797 	int retval;
798 
799 	WARN_ON_ONCE(!mutex_is_locked(&bus->mdio_lock));
800 
801 	retval = bus->read(bus, addr, regnum);
802 
803 	trace_mdio_access(bus, 1, addr, regnum, retval, retval);
804 	mdiobus_stats_acct(&bus->stats[addr], true, retval);
805 
806 	return retval;
807 }
808 EXPORT_SYMBOL(__mdiobus_read);
809 
810 /**
811  * __mdiobus_write - Unlocked version of the mdiobus_write function
812  * @bus: the mii_bus struct
813  * @addr: the phy address
814  * @regnum: register number to write
815  * @val: value to write to @regnum
816  *
817  * Write a MDIO bus register. Caller must hold the mdio bus lock.
818  *
819  * NOTE: MUST NOT be called from interrupt context.
820  */
821 int __mdiobus_write(struct mii_bus *bus, int addr, u32 regnum, u16 val)
822 {
823 	int err;
824 
825 	WARN_ON_ONCE(!mutex_is_locked(&bus->mdio_lock));
826 
827 	err = bus->write(bus, addr, regnum, val);
828 
829 	trace_mdio_access(bus, 0, addr, regnum, val, err);
830 	mdiobus_stats_acct(&bus->stats[addr], false, err);
831 
832 	return err;
833 }
834 EXPORT_SYMBOL(__mdiobus_write);
835 
836 /**
837  * __mdiobus_modify_changed - Unlocked version of the mdiobus_modify function
838  * @bus: the mii_bus struct
839  * @addr: the phy address
840  * @regnum: register number to modify
841  * @mask: bit mask of bits to clear
842  * @set: bit mask of bits to set
843  *
844  * Read, modify, and if any change, write the register value back to the
845  * device. Any error returns a negative number.
846  *
847  * NOTE: MUST NOT be called from interrupt context.
848  */
849 int __mdiobus_modify_changed(struct mii_bus *bus, int addr, u32 regnum,
850 			     u16 mask, u16 set)
851 {
852 	int new, ret;
853 
854 	ret = __mdiobus_read(bus, addr, regnum);
855 	if (ret < 0)
856 		return ret;
857 
858 	new = (ret & ~mask) | set;
859 	if (new == ret)
860 		return 0;
861 
862 	ret = __mdiobus_write(bus, addr, regnum, new);
863 
864 	return ret < 0 ? ret : 1;
865 }
866 EXPORT_SYMBOL_GPL(__mdiobus_modify_changed);
867 
868 /**
869  * mdiobus_read_nested - Nested version of the mdiobus_read function
870  * @bus: the mii_bus struct
871  * @addr: the phy address
872  * @regnum: register number to read
873  *
874  * In case of nested MDIO bus access avoid lockdep false positives by
875  * using mutex_lock_nested().
876  *
877  * NOTE: MUST NOT be called from interrupt context,
878  * because the bus read/write functions may wait for an interrupt
879  * to conclude the operation.
880  */
881 int mdiobus_read_nested(struct mii_bus *bus, int addr, u32 regnum)
882 {
883 	int retval;
884 
885 	if (WARN_ON_ONCE(in_interrupt()))
886 		return -EINVAL;
887 
888 	mutex_lock_nested(&bus->mdio_lock, MDIO_MUTEX_NESTED);
889 	retval = __mdiobus_read(bus, addr, regnum);
890 	mutex_unlock(&bus->mdio_lock);
891 
892 	return retval;
893 }
894 EXPORT_SYMBOL(mdiobus_read_nested);
895 
896 /**
897  * mdiobus_read - Convenience function for reading a given MII mgmt register
898  * @bus: the mii_bus struct
899  * @addr: the phy address
900  * @regnum: register number to read
901  *
902  * NOTE: MUST NOT be called from interrupt context,
903  * because the bus read/write functions may wait for an interrupt
904  * to conclude the operation.
905  */
906 int mdiobus_read(struct mii_bus *bus, int addr, u32 regnum)
907 {
908 	int retval;
909 
910 	if (WARN_ON_ONCE(in_interrupt()))
911 		return -EINVAL;
912 
913 	mutex_lock(&bus->mdio_lock);
914 	retval = __mdiobus_read(bus, addr, regnum);
915 	mutex_unlock(&bus->mdio_lock);
916 
917 	return retval;
918 }
919 EXPORT_SYMBOL(mdiobus_read);
920 
921 /**
922  * mdiobus_write_nested - Nested version of the mdiobus_write function
923  * @bus: the mii_bus struct
924  * @addr: the phy address
925  * @regnum: register number to write
926  * @val: value to write to @regnum
927  *
928  * In case of nested MDIO bus access avoid lockdep false positives by
929  * using mutex_lock_nested().
930  *
931  * NOTE: MUST NOT be called from interrupt context,
932  * because the bus read/write functions may wait for an interrupt
933  * to conclude the operation.
934  */
935 int mdiobus_write_nested(struct mii_bus *bus, int addr, u32 regnum, u16 val)
936 {
937 	int err;
938 
939 	if (WARN_ON_ONCE(in_interrupt()))
940 		return -EINVAL;
941 
942 	mutex_lock_nested(&bus->mdio_lock, MDIO_MUTEX_NESTED);
943 	err = __mdiobus_write(bus, addr, regnum, val);
944 	mutex_unlock(&bus->mdio_lock);
945 
946 	return err;
947 }
948 EXPORT_SYMBOL(mdiobus_write_nested);
949 
950 /**
951  * mdiobus_write - Convenience function for writing a given MII mgmt register
952  * @bus: the mii_bus struct
953  * @addr: the phy address
954  * @regnum: register number to write
955  * @val: value to write to @regnum
956  *
957  * NOTE: MUST NOT be called from interrupt context,
958  * because the bus read/write functions may wait for an interrupt
959  * to conclude the operation.
960  */
961 int mdiobus_write(struct mii_bus *bus, int addr, u32 regnum, u16 val)
962 {
963 	int err;
964 
965 	if (WARN_ON_ONCE(in_interrupt()))
966 		return -EINVAL;
967 
968 	mutex_lock(&bus->mdio_lock);
969 	err = __mdiobus_write(bus, addr, regnum, val);
970 	mutex_unlock(&bus->mdio_lock);
971 
972 	return err;
973 }
974 EXPORT_SYMBOL(mdiobus_write);
975 
976 /**
977  * mdiobus_modify - Convenience function for modifying a given mdio device
978  *	register
979  * @bus: the mii_bus struct
980  * @addr: the phy address
981  * @regnum: register number to write
982  * @mask: bit mask of bits to clear
983  * @set: bit mask of bits to set
984  */
985 int mdiobus_modify(struct mii_bus *bus, int addr, u32 regnum, u16 mask, u16 set)
986 {
987 	int err;
988 
989 	if (WARN_ON_ONCE(in_interrupt()))
990 		return -EINVAL;
991 
992 	mutex_lock(&bus->mdio_lock);
993 	err = __mdiobus_modify_changed(bus, addr, regnum, mask, set);
994 	mutex_unlock(&bus->mdio_lock);
995 
996 	return err < 0 ? err : 0;
997 }
998 EXPORT_SYMBOL_GPL(mdiobus_modify);
999 
1000 /**
1001  * mdio_bus_match - determine if given MDIO driver supports the given
1002  *		    MDIO device
1003  * @dev: target MDIO device
1004  * @drv: given MDIO driver
1005  *
1006  * Description: Given a MDIO device, and a MDIO driver, return 1 if
1007  *   the driver supports the device.  Otherwise, return 0. This may
1008  *   require calling the devices own match function, since different classes
1009  *   of MDIO devices have different match criteria.
1010  */
1011 static int mdio_bus_match(struct device *dev, struct device_driver *drv)
1012 {
1013 	struct mdio_device *mdio = to_mdio_device(dev);
1014 
1015 	if (of_driver_match_device(dev, drv))
1016 		return 1;
1017 
1018 	if (mdio->bus_match)
1019 		return mdio->bus_match(dev, drv);
1020 
1021 	return 0;
1022 }
1023 
1024 static int mdio_uevent(struct device *dev, struct kobj_uevent_env *env)
1025 {
1026 	int rc;
1027 
1028 	/* Some devices have extra OF data and an OF-style MODALIAS */
1029 	rc = of_device_uevent_modalias(dev, env);
1030 	if (rc != -ENODEV)
1031 		return rc;
1032 
1033 	return 0;
1034 }
1035 
1036 static struct attribute *mdio_bus_device_statistics_attrs[] = {
1037 	&dev_attr_mdio_bus_device_transfers.attr.attr,
1038 	&dev_attr_mdio_bus_device_errors.attr.attr,
1039 	&dev_attr_mdio_bus_device_writes.attr.attr,
1040 	&dev_attr_mdio_bus_device_reads.attr.attr,
1041 	NULL,
1042 };
1043 
1044 static const struct attribute_group mdio_bus_device_statistics_group = {
1045 	.name	= "statistics",
1046 	.attrs	= mdio_bus_device_statistics_attrs,
1047 };
1048 
1049 static const struct attribute_group *mdio_bus_dev_groups[] = {
1050 	&mdio_bus_device_statistics_group,
1051 	NULL,
1052 };
1053 
1054 struct bus_type mdio_bus_type = {
1055 	.name		= "mdio_bus",
1056 	.dev_groups	= mdio_bus_dev_groups,
1057 	.match		= mdio_bus_match,
1058 	.uevent		= mdio_uevent,
1059 };
1060 EXPORT_SYMBOL(mdio_bus_type);
1061 
1062 int __init mdio_bus_init(void)
1063 {
1064 	int ret;
1065 
1066 	ret = class_register(&mdio_bus_class);
1067 	if (!ret) {
1068 		ret = bus_register(&mdio_bus_type);
1069 		if (ret)
1070 			class_unregister(&mdio_bus_class);
1071 	}
1072 
1073 	return ret;
1074 }
1075 EXPORT_SYMBOL_GPL(mdio_bus_init);
1076 
1077 #if IS_ENABLED(CONFIG_PHYLIB)
1078 void mdio_bus_exit(void)
1079 {
1080 	class_unregister(&mdio_bus_class);
1081 	bus_unregister(&mdio_bus_type);
1082 }
1083 EXPORT_SYMBOL_GPL(mdio_bus_exit);
1084 #else
1085 module_init(mdio_bus_init);
1086 /* no module_exit, intentional */
1087 MODULE_LICENSE("GPL");
1088 MODULE_DESCRIPTION("MDIO bus/device layer");
1089 #endif
1090