5 * Copyright (c) 2004 Freescale Semiconductor, Inc.
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.
14 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
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>
35 #include <linux/module.h>
36 #include <linux/mii.h>
37 #include <linux/ethtool.h>
38 #include <linux/phy.h>
40 #include <linux/uaccess.h>
44 #define CREATE_TRACE_POINTS
45 #include <trace/events/mdio.h>
47 #include "mdio-boardinfo.h"
49 int mdiobus_register_device(struct mdio_device
*mdiodev
)
51 if (mdiodev
->bus
->mdio_map
[mdiodev
->addr
])
54 mdiodev
->bus
->mdio_map
[mdiodev
->addr
] = mdiodev
;
58 EXPORT_SYMBOL(mdiobus_register_device
);
60 int mdiobus_unregister_device(struct mdio_device
*mdiodev
)
62 if (mdiodev
->bus
->mdio_map
[mdiodev
->addr
] != mdiodev
)
65 mdiodev
->bus
->mdio_map
[mdiodev
->addr
] = NULL
;
69 EXPORT_SYMBOL(mdiobus_unregister_device
);
71 struct phy_device
*mdiobus_get_phy(struct mii_bus
*bus
, int addr
)
73 struct mdio_device
*mdiodev
= bus
->mdio_map
[addr
];
78 if (!(mdiodev
->flags
& MDIO_DEVICE_FLAG_PHY
))
81 return container_of(mdiodev
, struct phy_device
, mdio
);
83 EXPORT_SYMBOL(mdiobus_get_phy
);
85 bool mdiobus_is_registered_device(struct mii_bus
*bus
, int addr
)
87 return bus
->mdio_map
[addr
];
89 EXPORT_SYMBOL(mdiobus_is_registered_device
);
92 * mdiobus_alloc_size - allocate a mii_bus structure
93 * @size: extra amount of memory to allocate for private storage.
94 * If non-zero, then bus->priv is points to that memory.
96 * Description: called by a bus driver to allocate an mii_bus
97 * structure to fill in.
99 struct mii_bus
*mdiobus_alloc_size(size_t size
)
102 size_t aligned_size
= ALIGN(sizeof(*bus
), NETDEV_ALIGN
);
106 /* If we alloc extra space, it should be aligned */
108 alloc_size
= aligned_size
+ size
;
110 alloc_size
= sizeof(*bus
);
112 bus
= kzalloc(alloc_size
, GFP_KERNEL
);
116 bus
->state
= MDIOBUS_ALLOCATED
;
118 bus
->priv
= (void *)bus
+ aligned_size
;
120 /* Initialise the interrupts to polling */
121 for (i
= 0; i
< PHY_MAX_ADDR
; i
++)
122 bus
->irq
[i
] = PHY_POLL
;
126 EXPORT_SYMBOL(mdiobus_alloc_size
);
128 static void _devm_mdiobus_free(struct device
*dev
, void *res
)
130 mdiobus_free(*(struct mii_bus
**)res
);
133 static int devm_mdiobus_match(struct device
*dev
, void *res
, void *data
)
135 struct mii_bus
**r
= res
;
137 if (WARN_ON(!r
|| !*r
))
144 * devm_mdiobus_alloc_size - Resource-managed mdiobus_alloc_size()
145 * @dev: Device to allocate mii_bus for
146 * @sizeof_priv: Space to allocate for private structure.
148 * Managed mdiobus_alloc_size. mii_bus allocated with this function is
149 * automatically freed on driver detach.
151 * If an mii_bus allocated with this function needs to be freed separately,
152 * devm_mdiobus_free() must be used.
155 * Pointer to allocated mii_bus on success, NULL on failure.
157 struct mii_bus
*devm_mdiobus_alloc_size(struct device
*dev
, int sizeof_priv
)
159 struct mii_bus
**ptr
, *bus
;
161 ptr
= devres_alloc(_devm_mdiobus_free
, sizeof(*ptr
), GFP_KERNEL
);
165 /* use raw alloc_dr for kmalloc caller tracing */
166 bus
= mdiobus_alloc_size(sizeof_priv
);
169 devres_add(dev
, ptr
);
176 EXPORT_SYMBOL_GPL(devm_mdiobus_alloc_size
);
179 * devm_mdiobus_free - Resource-managed mdiobus_free()
180 * @dev: Device this mii_bus belongs to
181 * @bus: the mii_bus associated with the device
183 * Free mii_bus allocated with devm_mdiobus_alloc_size().
185 void devm_mdiobus_free(struct device
*dev
, struct mii_bus
*bus
)
189 rc
= devres_release(dev
, _devm_mdiobus_free
,
190 devm_mdiobus_match
, bus
);
193 EXPORT_SYMBOL_GPL(devm_mdiobus_free
);
196 * mdiobus_release - mii_bus device release callback
197 * @d: the target struct device that contains the mii_bus
199 * Description: called when the last reference to an mii_bus is
200 * dropped, to free the underlying memory.
202 static void mdiobus_release(struct device
*d
)
204 struct mii_bus
*bus
= to_mii_bus(d
);
205 BUG_ON(bus
->state
!= MDIOBUS_RELEASED
&&
206 /* for compatibility with error handling in drivers */
207 bus
->state
!= MDIOBUS_ALLOCATED
);
211 static struct class mdio_bus_class
= {
213 .dev_release
= mdiobus_release
,
216 #if IS_ENABLED(CONFIG_OF_MDIO)
217 /* Helper function for of_mdio_find_bus */
218 static int of_mdio_bus_match(struct device
*dev
, const void *mdio_bus_np
)
220 return dev
->of_node
== mdio_bus_np
;
223 * of_mdio_find_bus - Given an mii_bus node, find the mii_bus.
224 * @mdio_bus_np: Pointer to the mii_bus.
226 * Returns a reference to the mii_bus, or NULL if none found. The
227 * embedded struct device will have its reference count incremented,
228 * and this must be put once the bus is finished with.
230 * Because the association of a device_node and mii_bus is made via
231 * of_mdiobus_register(), the mii_bus cannot be found before it is
232 * registered with of_mdiobus_register().
235 struct mii_bus
*of_mdio_find_bus(struct device_node
*mdio_bus_np
)
242 d
= class_find_device(&mdio_bus_class
, NULL
, mdio_bus_np
,
245 return d
? to_mii_bus(d
) : NULL
;
247 EXPORT_SYMBOL(of_mdio_find_bus
);
249 /* Walk the list of subnodes of a mdio bus and look for a node that
250 * matches the mdio device's address with its 'reg' property. If
251 * found, set the of_node pointer for the mdio device. This allows
252 * auto-probed phy devices to be supplied with information passed in
255 static void of_mdiobus_link_mdiodev(struct mii_bus
*bus
,
256 struct mdio_device
*mdiodev
)
258 struct device
*dev
= &mdiodev
->dev
;
259 struct device_node
*child
;
261 if (dev
->of_node
|| !bus
->dev
.of_node
)
264 for_each_available_child_of_node(bus
->dev
.of_node
, child
) {
268 ret
= of_property_read_u32(child
, "reg", &addr
);
270 dev_err(dev
, "%s has invalid MDIO address\n",
275 /* A MDIO device must have a reg property in the range [0-31] */
276 if (addr
>= PHY_MAX_ADDR
) {
277 dev_err(dev
, "%s MDIO address %i is too large\n",
278 child
->full_name
, addr
);
282 if (addr
== mdiodev
->addr
) {
283 dev
->of_node
= child
;
288 #else /* !IS_ENABLED(CONFIG_OF_MDIO) */
289 static inline void of_mdiobus_link_mdiodev(struct mii_bus
*mdio
,
290 struct mdio_device
*mdiodev
)
296 * mdiobus_create_device_from_board_info - create a full MDIO device given
297 * a mdio_board_info structure
298 * @bus: MDIO bus to create the devices on
299 * @bi: mdio_board_info structure describing the devices
301 * Returns 0 on success or < 0 on error.
303 static int mdiobus_create_device(struct mii_bus
*bus
,
304 struct mdio_board_info
*bi
)
306 struct mdio_device
*mdiodev
;
309 mdiodev
= mdio_device_create(bus
, bi
->mdio_addr
);
313 strncpy(mdiodev
->modalias
, bi
->modalias
,
314 sizeof(mdiodev
->modalias
));
315 mdiodev
->bus_match
= mdio_device_bus_match
;
316 mdiodev
->dev
.platform_data
= (void *)bi
->platform_data
;
318 ret
= mdio_device_register(mdiodev
);
320 mdio_device_free(mdiodev
);
326 * __mdiobus_register - bring up all the PHYs on a given bus and attach them to bus
327 * @bus: target mii_bus
328 * @owner: module containing bus accessor functions
330 * Description: Called by a bus driver to bring up all the PHYs
331 * on a given bus, and attach them to the bus. Drivers should use
332 * mdiobus_register() rather than __mdiobus_register() unless they
333 * need to pass a specific owner module. MDIO devices which are not
334 * PHYs will not be brought up by this function. They are expected to
335 * to be explicitly listed in DT and instantiated by of_mdiobus_register().
337 * Returns 0 on success or < 0 on error.
339 int __mdiobus_register(struct mii_bus
*bus
, struct module
*owner
)
341 struct mdio_device
*mdiodev
;
343 struct gpio_desc
*gpiod
;
345 if (NULL
== bus
|| NULL
== bus
->name
||
346 NULL
== bus
->read
|| NULL
== bus
->write
)
349 BUG_ON(bus
->state
!= MDIOBUS_ALLOCATED
&&
350 bus
->state
!= MDIOBUS_UNREGISTERED
);
353 bus
->dev
.parent
= bus
->parent
;
354 bus
->dev
.class = &mdio_bus_class
;
355 bus
->dev
.groups
= NULL
;
356 dev_set_name(&bus
->dev
, "%s", bus
->id
);
358 err
= device_register(&bus
->dev
);
360 pr_err("mii_bus %s failed to register\n", bus
->id
);
361 put_device(&bus
->dev
);
365 mutex_init(&bus
->mdio_lock
);
370 /* de-assert bus level PHY GPIO resets */
371 if (bus
->num_reset_gpios
> 0) {
372 bus
->reset_gpiod
= devm_kcalloc(&bus
->dev
,
373 bus
->num_reset_gpios
,
374 sizeof(struct gpio_desc
*),
376 if (!bus
->reset_gpiod
)
380 for (i
= 0; i
< bus
->num_reset_gpios
; i
++) {
381 gpiod
= devm_gpiod_get_index(&bus
->dev
, "reset", i
,
384 err
= PTR_ERR(gpiod
);
385 if (err
!= -ENOENT
) {
387 "mii_bus %s couldn't get reset GPIO\n",
392 bus
->reset_gpiod
[i
] = gpiod
;
393 gpiod_set_value_cansleep(gpiod
, 1);
394 udelay(bus
->reset_delay_us
);
395 gpiod_set_value_cansleep(gpiod
, 0);
399 for (i
= 0; i
< PHY_MAX_ADDR
; i
++) {
400 if ((bus
->phy_mask
& (1 << i
)) == 0) {
401 struct phy_device
*phydev
;
403 phydev
= mdiobus_scan(bus
, i
);
404 if (IS_ERR(phydev
) && (PTR_ERR(phydev
) != -ENODEV
)) {
405 err
= PTR_ERR(phydev
);
411 mdiobus_setup_mdiodev_from_board_info(bus
, mdiobus_create_device
);
413 bus
->state
= MDIOBUS_REGISTERED
;
414 pr_info("%s: probed\n", bus
->name
);
419 mdiodev
= bus
->mdio_map
[i
];
423 mdiodev
->device_remove(mdiodev
);
424 mdiodev
->device_free(mdiodev
);
427 /* Put PHYs in RESET to save power */
428 for (i
= 0; i
< bus
->num_reset_gpios
; i
++) {
429 if (bus
->reset_gpiod
[i
])
430 gpiod_set_value_cansleep(bus
->reset_gpiod
[i
], 1);
433 device_del(&bus
->dev
);
436 EXPORT_SYMBOL(__mdiobus_register
);
438 void mdiobus_unregister(struct mii_bus
*bus
)
440 struct mdio_device
*mdiodev
;
443 BUG_ON(bus
->state
!= MDIOBUS_REGISTERED
);
444 bus
->state
= MDIOBUS_UNREGISTERED
;
446 for (i
= 0; i
< PHY_MAX_ADDR
; i
++) {
447 mdiodev
= bus
->mdio_map
[i
];
451 mdiodev
->device_remove(mdiodev
);
452 mdiodev
->device_free(mdiodev
);
455 /* Put PHYs in RESET to save power */
456 for (i
= 0; i
< bus
->num_reset_gpios
; i
++) {
457 if (bus
->reset_gpiod
[i
])
458 gpiod_set_value_cansleep(bus
->reset_gpiod
[i
], 1);
461 device_del(&bus
->dev
);
463 EXPORT_SYMBOL(mdiobus_unregister
);
466 * mdiobus_free - free a struct mii_bus
467 * @bus: mii_bus to free
469 * This function releases the reference to the underlying device
470 * object in the mii_bus. If this is the last reference, the mii_bus
473 void mdiobus_free(struct mii_bus
*bus
)
475 /* For compatibility with error handling in drivers. */
476 if (bus
->state
== MDIOBUS_ALLOCATED
) {
481 BUG_ON(bus
->state
!= MDIOBUS_UNREGISTERED
);
482 bus
->state
= MDIOBUS_RELEASED
;
484 put_device(&bus
->dev
);
486 EXPORT_SYMBOL(mdiobus_free
);
489 * mdiobus_scan - scan a bus for MDIO devices.
490 * @bus: mii_bus to scan
491 * @addr: address on bus to scan
493 * This function scans the MDIO bus, looking for devices which can be
494 * identified using a vendor/product ID in registers 2 and 3. Not all
495 * MDIO devices have such registers, but PHY devices typically
496 * do. Hence this function assumes anything found is a PHY, or can be
497 * treated as a PHY. Other MDIO devices, such as switches, will
498 * probably not be found during the scan.
500 struct phy_device
*mdiobus_scan(struct mii_bus
*bus
, int addr
)
502 struct phy_device
*phydev
;
505 phydev
= get_phy_device(bus
, addr
, false);
510 * For DT, see if the auto-probed phy has a correspoding child
511 * in the bus node, and set the of_node pointer in this case.
513 of_mdiobus_link_mdiodev(bus
, &phydev
->mdio
);
515 err
= phy_device_register(phydev
);
517 phy_device_free(phydev
);
518 return ERR_PTR(-ENODEV
);
523 EXPORT_SYMBOL(mdiobus_scan
);
526 * mdiobus_read_nested - Nested version of the mdiobus_read function
527 * @bus: the mii_bus struct
528 * @addr: the phy address
529 * @regnum: register number to read
531 * In case of nested MDIO bus access avoid lockdep false positives by
532 * using mutex_lock_nested().
534 * NOTE: MUST NOT be called from interrupt context,
535 * because the bus read/write functions may wait for an interrupt
536 * to conclude the operation.
538 int mdiobus_read_nested(struct mii_bus
*bus
, int addr
, u32 regnum
)
542 BUG_ON(in_interrupt());
544 mutex_lock_nested(&bus
->mdio_lock
, MDIO_MUTEX_NESTED
);
545 retval
= bus
->read(bus
, addr
, regnum
);
546 mutex_unlock(&bus
->mdio_lock
);
548 trace_mdio_access(bus
, 1, addr
, regnum
, retval
, retval
);
552 EXPORT_SYMBOL(mdiobus_read_nested
);
555 * mdiobus_read - Convenience function for reading a given MII mgmt register
556 * @bus: the mii_bus struct
557 * @addr: the phy address
558 * @regnum: register number to read
560 * NOTE: MUST NOT be called from interrupt context,
561 * because the bus read/write functions may wait for an interrupt
562 * to conclude the operation.
564 int mdiobus_read(struct mii_bus
*bus
, int addr
, u32 regnum
)
568 BUG_ON(in_interrupt());
570 mutex_lock(&bus
->mdio_lock
);
571 retval
= bus
->read(bus
, addr
, regnum
);
572 mutex_unlock(&bus
->mdio_lock
);
574 trace_mdio_access(bus
, 1, addr
, regnum
, retval
, retval
);
578 EXPORT_SYMBOL(mdiobus_read
);
581 * mdiobus_write_nested - Nested version of the mdiobus_write function
582 * @bus: the mii_bus struct
583 * @addr: the phy address
584 * @regnum: register number to write
585 * @val: value to write to @regnum
587 * In case of nested MDIO bus access avoid lockdep false positives by
588 * using mutex_lock_nested().
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.
594 int mdiobus_write_nested(struct mii_bus
*bus
, int addr
, u32 regnum
, u16 val
)
598 BUG_ON(in_interrupt());
600 mutex_lock_nested(&bus
->mdio_lock
, MDIO_MUTEX_NESTED
);
601 err
= bus
->write(bus
, addr
, regnum
, val
);
602 mutex_unlock(&bus
->mdio_lock
);
604 trace_mdio_access(bus
, 0, addr
, regnum
, val
, err
);
608 EXPORT_SYMBOL(mdiobus_write_nested
);
611 * mdiobus_write - Convenience function for writing a given MII mgmt register
612 * @bus: the mii_bus struct
613 * @addr: the phy address
614 * @regnum: register number to write
615 * @val: value to write to @regnum
617 * NOTE: MUST NOT be called from interrupt context,
618 * because the bus read/write functions may wait for an interrupt
619 * to conclude the operation.
621 int mdiobus_write(struct mii_bus
*bus
, int addr
, u32 regnum
, u16 val
)
625 BUG_ON(in_interrupt());
627 mutex_lock(&bus
->mdio_lock
);
628 err
= bus
->write(bus
, addr
, regnum
, val
);
629 mutex_unlock(&bus
->mdio_lock
);
631 trace_mdio_access(bus
, 0, addr
, regnum
, val
, err
);
635 EXPORT_SYMBOL(mdiobus_write
);
638 * mdio_bus_match - determine if given MDIO driver supports the given
640 * @dev: target MDIO device
641 * @drv: given MDIO driver
643 * Description: Given a MDIO device, and a MDIO driver, return 1 if
644 * the driver supports the device. Otherwise, return 0. This may
645 * require calling the devices own match function, since different classes
646 * of MDIO devices have different match criteria.
648 static int mdio_bus_match(struct device
*dev
, struct device_driver
*drv
)
650 struct mdio_device
*mdio
= to_mdio_device(dev
);
652 if (of_driver_match_device(dev
, drv
))
656 return mdio
->bus_match(dev
, drv
);
662 static int mdio_bus_suspend(struct device
*dev
)
664 struct mdio_device
*mdio
= to_mdio_device(dev
);
666 if (mdio
->pm_ops
&& mdio
->pm_ops
->suspend
)
667 return mdio
->pm_ops
->suspend(dev
);
672 static int mdio_bus_resume(struct device
*dev
)
674 struct mdio_device
*mdio
= to_mdio_device(dev
);
676 if (mdio
->pm_ops
&& mdio
->pm_ops
->resume
)
677 return mdio
->pm_ops
->resume(dev
);
682 static int mdio_bus_restore(struct device
*dev
)
684 struct mdio_device
*mdio
= to_mdio_device(dev
);
686 if (mdio
->pm_ops
&& mdio
->pm_ops
->restore
)
687 return mdio
->pm_ops
->restore(dev
);
692 static const struct dev_pm_ops mdio_bus_pm_ops
= {
693 .suspend
= mdio_bus_suspend
,
694 .resume
= mdio_bus_resume
,
695 .freeze
= mdio_bus_suspend
,
696 .thaw
= mdio_bus_resume
,
697 .restore
= mdio_bus_restore
,
700 #define MDIO_BUS_PM_OPS (&mdio_bus_pm_ops)
704 #define MDIO_BUS_PM_OPS NULL
706 #endif /* CONFIG_PM */
708 struct bus_type mdio_bus_type
= {
710 .match
= mdio_bus_match
,
711 .pm
= MDIO_BUS_PM_OPS
,
713 EXPORT_SYMBOL(mdio_bus_type
);
715 int __init
mdio_bus_init(void)
719 ret
= class_register(&mdio_bus_class
);
721 ret
= bus_register(&mdio_bus_type
);
723 class_unregister(&mdio_bus_class
);
728 EXPORT_SYMBOL_GPL(mdio_bus_init
);
730 #if IS_ENABLED(CONFIG_PHYLIB)
731 void mdio_bus_exit(void)
733 class_unregister(&mdio_bus_class
);
734 bus_unregister(&mdio_bus_type
);
736 EXPORT_SYMBOL_GPL(mdio_bus_exit
);
738 module_init(mdio_bus_init
);
739 /* no module_exit, intentional */
740 MODULE_LICENSE("GPL");
741 MODULE_DESCRIPTION("MDIO bus/device layer");