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