xref: /openbmc/linux/drivers/net/phy/mdio_bus.c (revision 22fc4c4c9fd60427bcda00878cee94e7622cfa7a)
1 /* MDIO Bus interface
2  *
3  * Author: Andy Fleming
4  *
5  * Copyright (c) 2004 Freescale Semiconductor, Inc.
6  *
7  * This program is free software; you can redistribute  it and/or modify it
8  * under  the terms of  the GNU General  Public License as published by the
9  * Free Software Foundation;  either version 2 of the  License, or (at your
10  * option) any later version.
11  *
12  */
13 
14 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
15 
16 #include <linux/kernel.h>
17 #include <linux/string.h>
18 #include <linux/errno.h>
19 #include <linux/unistd.h>
20 #include <linux/slab.h>
21 #include <linux/interrupt.h>
22 #include <linux/init.h>
23 #include <linux/delay.h>
24 #include <linux/device.h>
25 #include <linux/gpio.h>
26 #include <linux/gpio/consumer.h>
27 #include <linux/of_device.h>
28 #include <linux/of_mdio.h>
29 #include <linux/of_gpio.h>
30 #include <linux/netdevice.h>
31 #include <linux/etherdevice.h>
32 #include <linux/skbuff.h>
33 #include <linux/spinlock.h>
34 #include <linux/mm.h>
35 #include <linux/module.h>
36 #include <linux/mii.h>
37 #include <linux/ethtool.h>
38 #include <linux/phy.h>
39 #include <linux/io.h>
40 #include <linux/uaccess.h>
41 
42 #define CREATE_TRACE_POINTS
43 #include <trace/events/mdio.h>
44 
45 #include "mdio-boardinfo.h"
46 
47 static int mdiobus_register_gpiod(struct mdio_device *mdiodev)
48 {
49 	struct gpio_desc *gpiod = NULL;
50 
51 	/* Deassert the optional reset signal */
52 	if (mdiodev->dev.of_node)
53 		gpiod = fwnode_get_named_gpiod(&mdiodev->dev.of_node->fwnode,
54 					       "reset-gpios", 0, GPIOD_OUT_LOW,
55 					       "PHY reset");
56 	if (PTR_ERR(gpiod) == -ENOENT ||
57 	    PTR_ERR(gpiod) == -ENOSYS)
58 		gpiod = NULL;
59 	else if (IS_ERR(gpiod))
60 		return PTR_ERR(gpiod);
61 
62 	mdiodev->reset = gpiod;
63 
64 	/* Assert the reset signal again */
65 	mdio_device_reset(mdiodev, 1);
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 
83 	mdiodev->bus->mdio_map[mdiodev->addr] = mdiodev;
84 
85 	return 0;
86 }
87 EXPORT_SYMBOL(mdiobus_register_device);
88 
89 int mdiobus_unregister_device(struct mdio_device *mdiodev)
90 {
91 	if (mdiodev->bus->mdio_map[mdiodev->addr] != mdiodev)
92 		return -EINVAL;
93 
94 	mdiodev->bus->mdio_map[mdiodev->addr] = NULL;
95 
96 	return 0;
97 }
98 EXPORT_SYMBOL(mdiobus_unregister_device);
99 
100 struct phy_device *mdiobus_get_phy(struct mii_bus *bus, int addr)
101 {
102 	struct mdio_device *mdiodev = bus->mdio_map[addr];
103 
104 	if (!mdiodev)
105 		return NULL;
106 
107 	if (!(mdiodev->flags & MDIO_DEVICE_FLAG_PHY))
108 		return NULL;
109 
110 	return container_of(mdiodev, struct phy_device, mdio);
111 }
112 EXPORT_SYMBOL(mdiobus_get_phy);
113 
114 bool mdiobus_is_registered_device(struct mii_bus *bus, int addr)
115 {
116 	return bus->mdio_map[addr];
117 }
118 EXPORT_SYMBOL(mdiobus_is_registered_device);
119 
120 /**
121  * mdiobus_alloc_size - allocate a mii_bus structure
122  * @size: extra amount of memory to allocate for private storage.
123  * If non-zero, then bus->priv is points to that memory.
124  *
125  * Description: called by a bus driver to allocate an mii_bus
126  * structure to fill in.
127  */
128 struct mii_bus *mdiobus_alloc_size(size_t size)
129 {
130 	struct mii_bus *bus;
131 	size_t aligned_size = ALIGN(sizeof(*bus), NETDEV_ALIGN);
132 	size_t alloc_size;
133 	int i;
134 
135 	/* If we alloc extra space, it should be aligned */
136 	if (size)
137 		alloc_size = aligned_size + size;
138 	else
139 		alloc_size = sizeof(*bus);
140 
141 	bus = kzalloc(alloc_size, GFP_KERNEL);
142 	if (!bus)
143 		return NULL;
144 
145 	bus->state = MDIOBUS_ALLOCATED;
146 	if (size)
147 		bus->priv = (void *)bus + aligned_size;
148 
149 	/* Initialise the interrupts to polling */
150 	for (i = 0; i < PHY_MAX_ADDR; i++)
151 		bus->irq[i] = PHY_POLL;
152 
153 	return bus;
154 }
155 EXPORT_SYMBOL(mdiobus_alloc_size);
156 
157 static void _devm_mdiobus_free(struct device *dev, void *res)
158 {
159 	mdiobus_free(*(struct mii_bus **)res);
160 }
161 
162 static int devm_mdiobus_match(struct device *dev, void *res, void *data)
163 {
164 	struct mii_bus **r = res;
165 
166 	if (WARN_ON(!r || !*r))
167 		return 0;
168 
169 	return *r == data;
170 }
171 
172 /**
173  * devm_mdiobus_alloc_size - Resource-managed mdiobus_alloc_size()
174  * @dev:		Device to allocate mii_bus for
175  * @sizeof_priv:	Space to allocate for private structure.
176  *
177  * Managed mdiobus_alloc_size. mii_bus allocated with this function is
178  * automatically freed on driver detach.
179  *
180  * If an mii_bus allocated with this function needs to be freed separately,
181  * devm_mdiobus_free() must be used.
182  *
183  * RETURNS:
184  * Pointer to allocated mii_bus on success, NULL on failure.
185  */
186 struct mii_bus *devm_mdiobus_alloc_size(struct device *dev, int sizeof_priv)
187 {
188 	struct mii_bus **ptr, *bus;
189 
190 	ptr = devres_alloc(_devm_mdiobus_free, sizeof(*ptr), GFP_KERNEL);
191 	if (!ptr)
192 		return NULL;
193 
194 	/* use raw alloc_dr for kmalloc caller tracing */
195 	bus = mdiobus_alloc_size(sizeof_priv);
196 	if (bus) {
197 		*ptr = bus;
198 		devres_add(dev, ptr);
199 	} else {
200 		devres_free(ptr);
201 	}
202 
203 	return bus;
204 }
205 EXPORT_SYMBOL_GPL(devm_mdiobus_alloc_size);
206 
207 /**
208  * devm_mdiobus_free - Resource-managed mdiobus_free()
209  * @dev:		Device this mii_bus belongs to
210  * @bus:		the mii_bus associated with the device
211  *
212  * Free mii_bus allocated with devm_mdiobus_alloc_size().
213  */
214 void devm_mdiobus_free(struct device *dev, struct mii_bus *bus)
215 {
216 	int rc;
217 
218 	rc = devres_release(dev, _devm_mdiobus_free,
219 			    devm_mdiobus_match, bus);
220 	WARN_ON(rc);
221 }
222 EXPORT_SYMBOL_GPL(devm_mdiobus_free);
223 
224 /**
225  * mdiobus_release - mii_bus device release callback
226  * @d: the target struct device that contains the mii_bus
227  *
228  * Description: called when the last reference to an mii_bus is
229  * dropped, to free the underlying memory.
230  */
231 static void mdiobus_release(struct device *d)
232 {
233 	struct mii_bus *bus = to_mii_bus(d);
234 	BUG_ON(bus->state != MDIOBUS_RELEASED &&
235 	       /* for compatibility with error handling in drivers */
236 	       bus->state != MDIOBUS_ALLOCATED);
237 	kfree(bus);
238 }
239 
240 static struct class mdio_bus_class = {
241 	.name		= "mdio_bus",
242 	.dev_release	= mdiobus_release,
243 };
244 
245 #if IS_ENABLED(CONFIG_OF_MDIO)
246 /* Helper function for of_mdio_find_bus */
247 static int of_mdio_bus_match(struct device *dev, const void *mdio_bus_np)
248 {
249 	return dev->of_node == mdio_bus_np;
250 }
251 /**
252  * of_mdio_find_bus - Given an mii_bus node, find the mii_bus.
253  * @mdio_bus_np: Pointer to the mii_bus.
254  *
255  * Returns a reference to the mii_bus, or NULL if none found.  The
256  * embedded struct device will have its reference count incremented,
257  * and this must be put once the bus is finished with.
258  *
259  * Because the association of a device_node and mii_bus is made via
260  * of_mdiobus_register(), the mii_bus cannot be found before it is
261  * registered with of_mdiobus_register().
262  *
263  */
264 struct mii_bus *of_mdio_find_bus(struct device_node *mdio_bus_np)
265 {
266 	struct device *d;
267 
268 	if (!mdio_bus_np)
269 		return NULL;
270 
271 	d = class_find_device(&mdio_bus_class, NULL,  mdio_bus_np,
272 			      of_mdio_bus_match);
273 
274 	return d ? to_mii_bus(d) : NULL;
275 }
276 EXPORT_SYMBOL(of_mdio_find_bus);
277 
278 /* Walk the list of subnodes of a mdio bus and look for a node that
279  * matches the mdio device's address with its 'reg' property. If
280  * found, set the of_node pointer for the mdio device. This allows
281  * auto-probed phy devices to be supplied with information passed in
282  * via DT.
283  */
284 static void of_mdiobus_link_mdiodev(struct mii_bus *bus,
285 				    struct mdio_device *mdiodev)
286 {
287 	struct device *dev = &mdiodev->dev;
288 	struct device_node *child;
289 
290 	if (dev->of_node || !bus->dev.of_node)
291 		return;
292 
293 	for_each_available_child_of_node(bus->dev.of_node, child) {
294 		int addr;
295 
296 		addr = of_mdio_parse_addr(dev, child);
297 		if (addr < 0)
298 			continue;
299 
300 		if (addr == mdiodev->addr) {
301 			dev->of_node = child;
302 			dev->fwnode = of_fwnode_handle(child);
303 			return;
304 		}
305 	}
306 }
307 #else /* !IS_ENABLED(CONFIG_OF_MDIO) */
308 static inline void of_mdiobus_link_mdiodev(struct mii_bus *mdio,
309 					   struct mdio_device *mdiodev)
310 {
311 }
312 #endif
313 
314 /**
315  * mdiobus_create_device_from_board_info - create a full MDIO device given
316  * a mdio_board_info structure
317  * @bus: MDIO bus to create the devices on
318  * @bi: mdio_board_info structure describing the devices
319  *
320  * Returns 0 on success or < 0 on error.
321  */
322 static int mdiobus_create_device(struct mii_bus *bus,
323 				 struct mdio_board_info *bi)
324 {
325 	struct mdio_device *mdiodev;
326 	int ret = 0;
327 
328 	mdiodev = mdio_device_create(bus, bi->mdio_addr);
329 	if (IS_ERR(mdiodev))
330 		return -ENODEV;
331 
332 	strncpy(mdiodev->modalias, bi->modalias,
333 		sizeof(mdiodev->modalias));
334 	mdiodev->bus_match = mdio_device_bus_match;
335 	mdiodev->dev.platform_data = (void *)bi->platform_data;
336 
337 	ret = mdio_device_register(mdiodev);
338 	if (ret)
339 		mdio_device_free(mdiodev);
340 
341 	return ret;
342 }
343 
344 /**
345  * __mdiobus_register - bring up all the PHYs on a given bus and attach them to bus
346  * @bus: target mii_bus
347  * @owner: module containing bus accessor functions
348  *
349  * Description: Called by a bus driver to bring up all the PHYs
350  *   on a given bus, and attach them to the bus. Drivers should use
351  *   mdiobus_register() rather than __mdiobus_register() unless they
352  *   need to pass a specific owner module. MDIO devices which are not
353  *   PHYs will not be brought up by this function. They are expected to
354  *   to be explicitly listed in DT and instantiated by of_mdiobus_register().
355  *
356  * Returns 0 on success or < 0 on error.
357  */
358 int __mdiobus_register(struct mii_bus *bus, struct module *owner)
359 {
360 	struct mdio_device *mdiodev;
361 	int i, err;
362 	struct gpio_desc *gpiod;
363 
364 	if (NULL == bus || NULL == bus->name ||
365 	    NULL == bus->read || NULL == bus->write)
366 		return -EINVAL;
367 
368 	BUG_ON(bus->state != MDIOBUS_ALLOCATED &&
369 	       bus->state != MDIOBUS_UNREGISTERED);
370 
371 	bus->owner = owner;
372 	bus->dev.parent = bus->parent;
373 	bus->dev.class = &mdio_bus_class;
374 	bus->dev.groups = NULL;
375 	dev_set_name(&bus->dev, "%s", bus->id);
376 
377 	err = device_register(&bus->dev);
378 	if (err) {
379 		pr_err("mii_bus %s failed to register\n", bus->id);
380 		put_device(&bus->dev);
381 		return -EINVAL;
382 	}
383 
384 	mutex_init(&bus->mdio_lock);
385 
386 	/* de-assert bus level PHY GPIO reset */
387 	gpiod = devm_gpiod_get_optional(&bus->dev, "reset", GPIOD_OUT_LOW);
388 	if (IS_ERR(gpiod)) {
389 		dev_err(&bus->dev, "mii_bus %s couldn't get reset GPIO\n",
390 			bus->id);
391 		return PTR_ERR(gpiod);
392 	} else	if (gpiod) {
393 		bus->reset_gpiod = gpiod;
394 
395 		gpiod_set_value_cansleep(gpiod, 1);
396 		udelay(bus->reset_delay_us);
397 		gpiod_set_value_cansleep(gpiod, 0);
398 	}
399 
400 	if (bus->reset)
401 		bus->reset(bus);
402 
403 	for (i = 0; i < PHY_MAX_ADDR; i++) {
404 		if ((bus->phy_mask & (1 << i)) == 0) {
405 			struct phy_device *phydev;
406 
407 			phydev = mdiobus_scan(bus, i);
408 			if (IS_ERR(phydev) && (PTR_ERR(phydev) != -ENODEV)) {
409 				err = PTR_ERR(phydev);
410 				goto error;
411 			}
412 		}
413 	}
414 
415 	mdiobus_setup_mdiodev_from_board_info(bus, mdiobus_create_device);
416 
417 	bus->state = MDIOBUS_REGISTERED;
418 	pr_info("%s: probed\n", bus->name);
419 	return 0;
420 
421 error:
422 	while (--i >= 0) {
423 		mdiodev = bus->mdio_map[i];
424 		if (!mdiodev)
425 			continue;
426 
427 		mdiodev->device_remove(mdiodev);
428 		mdiodev->device_free(mdiodev);
429 	}
430 
431 	/* Put PHYs in RESET to save power */
432 	if (bus->reset_gpiod)
433 		gpiod_set_value_cansleep(bus->reset_gpiod, 1);
434 
435 	device_del(&bus->dev);
436 	return err;
437 }
438 EXPORT_SYMBOL(__mdiobus_register);
439 
440 void mdiobus_unregister(struct mii_bus *bus)
441 {
442 	struct mdio_device *mdiodev;
443 	int i;
444 
445 	BUG_ON(bus->state != MDIOBUS_REGISTERED);
446 	bus->state = MDIOBUS_UNREGISTERED;
447 
448 	for (i = 0; i < PHY_MAX_ADDR; i++) {
449 		mdiodev = bus->mdio_map[i];
450 		if (!mdiodev)
451 			continue;
452 
453 		if (mdiodev->reset)
454 			gpiod_put(mdiodev->reset);
455 
456 		mdiodev->device_remove(mdiodev);
457 		mdiodev->device_free(mdiodev);
458 	}
459 
460 	/* Put PHYs in RESET to save power */
461 	if (bus->reset_gpiod)
462 		gpiod_set_value_cansleep(bus->reset_gpiod, 1);
463 
464 	device_del(&bus->dev);
465 }
466 EXPORT_SYMBOL(mdiobus_unregister);
467 
468 /**
469  * mdiobus_free - free a struct mii_bus
470  * @bus: mii_bus to free
471  *
472  * This function releases the reference to the underlying device
473  * object in the mii_bus.  If this is the last reference, the mii_bus
474  * will be freed.
475  */
476 void mdiobus_free(struct mii_bus *bus)
477 {
478 	/* For compatibility with error handling in drivers. */
479 	if (bus->state == MDIOBUS_ALLOCATED) {
480 		kfree(bus);
481 		return;
482 	}
483 
484 	BUG_ON(bus->state != MDIOBUS_UNREGISTERED);
485 	bus->state = MDIOBUS_RELEASED;
486 
487 	put_device(&bus->dev);
488 }
489 EXPORT_SYMBOL(mdiobus_free);
490 
491 /**
492  * mdiobus_scan - scan a bus for MDIO devices.
493  * @bus: mii_bus to scan
494  * @addr: address on bus to scan
495  *
496  * This function scans the MDIO bus, looking for devices which can be
497  * identified using a vendor/product ID in registers 2 and 3. Not all
498  * MDIO devices have such registers, but PHY devices typically
499  * do. Hence this function assumes anything found is a PHY, or can be
500  * treated as a PHY. Other MDIO devices, such as switches, will
501  * probably not be found during the scan.
502  */
503 struct phy_device *mdiobus_scan(struct mii_bus *bus, int addr)
504 {
505 	struct phy_device *phydev;
506 	int err;
507 
508 	phydev = get_phy_device(bus, addr, false);
509 	if (IS_ERR(phydev))
510 		return phydev;
511 
512 	/*
513 	 * For DT, see if the auto-probed phy has a correspoding child
514 	 * in the bus node, and set the of_node pointer in this case.
515 	 */
516 	of_mdiobus_link_mdiodev(bus, &phydev->mdio);
517 
518 	err = phy_device_register(phydev);
519 	if (err) {
520 		phy_device_free(phydev);
521 		return ERR_PTR(-ENODEV);
522 	}
523 
524 	return phydev;
525 }
526 EXPORT_SYMBOL(mdiobus_scan);
527 
528 /**
529  * __mdiobus_read - Unlocked version of the mdiobus_read function
530  * @bus: the mii_bus struct
531  * @addr: the phy address
532  * @regnum: register number to read
533  *
534  * Read a MDIO bus register. Caller must hold the mdio bus lock.
535  *
536  * NOTE: MUST NOT be called from interrupt context.
537  */
538 int __mdiobus_read(struct mii_bus *bus, int addr, u32 regnum)
539 {
540 	int retval;
541 
542 	WARN_ON_ONCE(!mutex_is_locked(&bus->mdio_lock));
543 
544 	retval = bus->read(bus, addr, regnum);
545 
546 	trace_mdio_access(bus, 1, addr, regnum, retval, retval);
547 
548 	return retval;
549 }
550 EXPORT_SYMBOL(__mdiobus_read);
551 
552 /**
553  * __mdiobus_write - Unlocked version of the mdiobus_write function
554  * @bus: the mii_bus struct
555  * @addr: the phy address
556  * @regnum: register number to write
557  * @val: value to write to @regnum
558  *
559  * Write a MDIO bus register. Caller must hold the mdio bus lock.
560  *
561  * NOTE: MUST NOT be called from interrupt context.
562  */
563 int __mdiobus_write(struct mii_bus *bus, int addr, u32 regnum, u16 val)
564 {
565 	int err;
566 
567 	WARN_ON_ONCE(!mutex_is_locked(&bus->mdio_lock));
568 
569 	err = bus->write(bus, addr, regnum, val);
570 
571 	trace_mdio_access(bus, 0, addr, regnum, val, err);
572 
573 	return err;
574 }
575 EXPORT_SYMBOL(__mdiobus_write);
576 
577 /**
578  * mdiobus_read_nested - Nested version of the mdiobus_read function
579  * @bus: the mii_bus struct
580  * @addr: the phy address
581  * @regnum: register number to read
582  *
583  * In case of nested MDIO bus access avoid lockdep false positives by
584  * using mutex_lock_nested().
585  *
586  * NOTE: MUST NOT be called from interrupt context,
587  * because the bus read/write functions may wait for an interrupt
588  * to conclude the operation.
589  */
590 int mdiobus_read_nested(struct mii_bus *bus, int addr, u32 regnum)
591 {
592 	int retval;
593 
594 	BUG_ON(in_interrupt());
595 
596 	mutex_lock_nested(&bus->mdio_lock, MDIO_MUTEX_NESTED);
597 	retval = __mdiobus_read(bus, addr, regnum);
598 	mutex_unlock(&bus->mdio_lock);
599 
600 	return retval;
601 }
602 EXPORT_SYMBOL(mdiobus_read_nested);
603 
604 /**
605  * mdiobus_read - Convenience function for reading a given MII mgmt register
606  * @bus: the mii_bus struct
607  * @addr: the phy address
608  * @regnum: register number to read
609  *
610  * NOTE: MUST NOT be called from interrupt context,
611  * because the bus read/write functions may wait for an interrupt
612  * to conclude the operation.
613  */
614 int mdiobus_read(struct mii_bus *bus, int addr, u32 regnum)
615 {
616 	int retval;
617 
618 	BUG_ON(in_interrupt());
619 
620 	mutex_lock(&bus->mdio_lock);
621 	retval = __mdiobus_read(bus, addr, regnum);
622 	mutex_unlock(&bus->mdio_lock);
623 
624 	return retval;
625 }
626 EXPORT_SYMBOL(mdiobus_read);
627 
628 /**
629  * mdiobus_write_nested - Nested version of the mdiobus_write function
630  * @bus: the mii_bus struct
631  * @addr: the phy address
632  * @regnum: register number to write
633  * @val: value to write to @regnum
634  *
635  * In case of nested MDIO bus access avoid lockdep false positives by
636  * using mutex_lock_nested().
637  *
638  * NOTE: MUST NOT be called from interrupt context,
639  * because the bus read/write functions may wait for an interrupt
640  * to conclude the operation.
641  */
642 int mdiobus_write_nested(struct mii_bus *bus, int addr, u32 regnum, u16 val)
643 {
644 	int err;
645 
646 	BUG_ON(in_interrupt());
647 
648 	mutex_lock_nested(&bus->mdio_lock, MDIO_MUTEX_NESTED);
649 	err = __mdiobus_write(bus, addr, regnum, val);
650 	mutex_unlock(&bus->mdio_lock);
651 
652 	return err;
653 }
654 EXPORT_SYMBOL(mdiobus_write_nested);
655 
656 /**
657  * mdiobus_write - Convenience function for writing a given MII mgmt register
658  * @bus: the mii_bus struct
659  * @addr: the phy address
660  * @regnum: register number to write
661  * @val: value to write to @regnum
662  *
663  * NOTE: MUST NOT be called from interrupt context,
664  * because the bus read/write functions may wait for an interrupt
665  * to conclude the operation.
666  */
667 int mdiobus_write(struct mii_bus *bus, int addr, u32 regnum, u16 val)
668 {
669 	int err;
670 
671 	BUG_ON(in_interrupt());
672 
673 	mutex_lock(&bus->mdio_lock);
674 	err = __mdiobus_write(bus, addr, regnum, val);
675 	mutex_unlock(&bus->mdio_lock);
676 
677 	return err;
678 }
679 EXPORT_SYMBOL(mdiobus_write);
680 
681 /**
682  * mdio_bus_match - determine if given MDIO driver supports the given
683  *		    MDIO device
684  * @dev: target MDIO device
685  * @drv: given MDIO driver
686  *
687  * Description: Given a MDIO device, and a MDIO driver, return 1 if
688  *   the driver supports the device.  Otherwise, return 0. This may
689  *   require calling the devices own match function, since different classes
690  *   of MDIO devices have different match criteria.
691  */
692 static int mdio_bus_match(struct device *dev, struct device_driver *drv)
693 {
694 	struct mdio_device *mdio = to_mdio_device(dev);
695 
696 	if (of_driver_match_device(dev, drv))
697 		return 1;
698 
699 	if (mdio->bus_match)
700 		return mdio->bus_match(dev, drv);
701 
702 	return 0;
703 }
704 
705 static int mdio_uevent(struct device *dev, struct kobj_uevent_env *env)
706 {
707 	int rc;
708 
709 	/* Some devices have extra OF data and an OF-style MODALIAS */
710 	rc = of_device_uevent_modalias(dev, env);
711 	if (rc != -ENODEV)
712 		return rc;
713 
714 	return 0;
715 }
716 
717 struct bus_type mdio_bus_type = {
718 	.name		= "mdio_bus",
719 	.match		= mdio_bus_match,
720 	.uevent		= mdio_uevent,
721 };
722 EXPORT_SYMBOL(mdio_bus_type);
723 
724 int __init mdio_bus_init(void)
725 {
726 	int ret;
727 
728 	ret = class_register(&mdio_bus_class);
729 	if (!ret) {
730 		ret = bus_register(&mdio_bus_type);
731 		if (ret)
732 			class_unregister(&mdio_bus_class);
733 	}
734 
735 	return ret;
736 }
737 EXPORT_SYMBOL_GPL(mdio_bus_init);
738 
739 #if IS_ENABLED(CONFIG_PHYLIB)
740 void mdio_bus_exit(void)
741 {
742 	class_unregister(&mdio_bus_class);
743 	bus_unregister(&mdio_bus_type);
744 }
745 EXPORT_SYMBOL_GPL(mdio_bus_exit);
746 #else
747 module_init(mdio_bus_init);
748 /* no module_exit, intentional */
749 MODULE_LICENSE("GPL");
750 MODULE_DESCRIPTION("MDIO bus/device layer");
751 #endif
752