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/of_device.h>
26 #include <linux/of_mdio.h>
27 #include <linux/netdevice.h>
28 #include <linux/etherdevice.h>
29 #include <linux/skbuff.h>
30 #include <linux/spinlock.h>
32 #include <linux/module.h>
33 #include <linux/mii.h>
34 #include <linux/ethtool.h>
35 #include <linux/phy.h>
37 #include <linux/uaccess.h>
41 #define CREATE_TRACE_POINTS
42 #include <trace/events/mdio.h>
44 #include "mdio-boardinfo.h"
46 int mdiobus_register_device(struct mdio_device
*mdiodev
)
48 if (mdiodev
->bus
->mdio_map
[mdiodev
->addr
])
51 mdiodev
->bus
->mdio_map
[mdiodev
->addr
] = mdiodev
;
55 EXPORT_SYMBOL(mdiobus_register_device
);
57 int mdiobus_unregister_device(struct mdio_device
*mdiodev
)
59 if (mdiodev
->bus
->mdio_map
[mdiodev
->addr
] != mdiodev
)
62 mdiodev
->bus
->mdio_map
[mdiodev
->addr
] = NULL
;
66 EXPORT_SYMBOL(mdiobus_unregister_device
);
68 struct phy_device
*mdiobus_get_phy(struct mii_bus
*bus
, int addr
)
70 struct mdio_device
*mdiodev
= bus
->mdio_map
[addr
];
75 if (!(mdiodev
->flags
& MDIO_DEVICE_FLAG_PHY
))
78 return container_of(mdiodev
, struct phy_device
, mdio
);
80 EXPORT_SYMBOL(mdiobus_get_phy
);
82 bool mdiobus_is_registered_device(struct mii_bus
*bus
, int addr
)
84 return bus
->mdio_map
[addr
];
86 EXPORT_SYMBOL(mdiobus_is_registered_device
);
89 * mdiobus_alloc_size - allocate a mii_bus structure
90 * @size: extra amount of memory to allocate for private storage.
91 * If non-zero, then bus->priv is points to that memory.
93 * Description: called by a bus driver to allocate an mii_bus
94 * structure to fill in.
96 struct mii_bus
*mdiobus_alloc_size(size_t size
)
99 size_t aligned_size
= ALIGN(sizeof(*bus
), NETDEV_ALIGN
);
103 /* If we alloc extra space, it should be aligned */
105 alloc_size
= aligned_size
+ size
;
107 alloc_size
= sizeof(*bus
);
109 bus
= kzalloc(alloc_size
, GFP_KERNEL
);
113 bus
->state
= MDIOBUS_ALLOCATED
;
115 bus
->priv
= (void *)bus
+ aligned_size
;
117 /* Initialise the interrupts to polling */
118 for (i
= 0; i
< PHY_MAX_ADDR
; i
++)
119 bus
->irq
[i
] = PHY_POLL
;
123 EXPORT_SYMBOL(mdiobus_alloc_size
);
125 static void _devm_mdiobus_free(struct device
*dev
, void *res
)
127 mdiobus_free(*(struct mii_bus
**)res
);
130 static int devm_mdiobus_match(struct device
*dev
, void *res
, void *data
)
132 struct mii_bus
**r
= res
;
134 if (WARN_ON(!r
|| !*r
))
141 * devm_mdiobus_alloc_size - Resource-managed mdiobus_alloc_size()
142 * @dev: Device to allocate mii_bus for
143 * @sizeof_priv: Space to allocate for private structure.
145 * Managed mdiobus_alloc_size. mii_bus allocated with this function is
146 * automatically freed on driver detach.
148 * If an mii_bus allocated with this function needs to be freed separately,
149 * devm_mdiobus_free() must be used.
152 * Pointer to allocated mii_bus on success, NULL on failure.
154 struct mii_bus
*devm_mdiobus_alloc_size(struct device
*dev
, int sizeof_priv
)
156 struct mii_bus
**ptr
, *bus
;
158 ptr
= devres_alloc(_devm_mdiobus_free
, sizeof(*ptr
), GFP_KERNEL
);
162 /* use raw alloc_dr for kmalloc caller tracing */
163 bus
= mdiobus_alloc_size(sizeof_priv
);
166 devres_add(dev
, ptr
);
173 EXPORT_SYMBOL_GPL(devm_mdiobus_alloc_size
);
176 * devm_mdiobus_free - Resource-managed mdiobus_free()
177 * @dev: Device this mii_bus belongs to
178 * @bus: the mii_bus associated with the device
180 * Free mii_bus allocated with devm_mdiobus_alloc_size().
182 void devm_mdiobus_free(struct device
*dev
, struct mii_bus
*bus
)
186 rc
= devres_release(dev
, _devm_mdiobus_free
,
187 devm_mdiobus_match
, bus
);
190 EXPORT_SYMBOL_GPL(devm_mdiobus_free
);
193 * mdiobus_release - mii_bus device release callback
194 * @d: the target struct device that contains the mii_bus
196 * Description: called when the last reference to an mii_bus is
197 * dropped, to free the underlying memory.
199 static void mdiobus_release(struct device
*d
)
201 struct mii_bus
*bus
= to_mii_bus(d
);
202 BUG_ON(bus
->state
!= MDIOBUS_RELEASED
&&
203 /* for compatibility with error handling in drivers */
204 bus
->state
!= MDIOBUS_ALLOCATED
);
208 static struct class mdio_bus_class
= {
210 .dev_release
= mdiobus_release
,
213 #if IS_ENABLED(CONFIG_OF_MDIO)
214 /* Helper function for of_mdio_find_bus */
215 static int of_mdio_bus_match(struct device
*dev
, const void *mdio_bus_np
)
217 return dev
->of_node
== mdio_bus_np
;
220 * of_mdio_find_bus - Given an mii_bus node, find the mii_bus.
221 * @mdio_bus_np: Pointer to the mii_bus.
223 * Returns a reference to the mii_bus, or NULL if none found. The
224 * embedded struct device will have its reference count incremented,
225 * and this must be put once the bus is finished with.
227 * Because the association of a device_node and mii_bus is made via
228 * of_mdiobus_register(), the mii_bus cannot be found before it is
229 * registered with of_mdiobus_register().
232 struct mii_bus
*of_mdio_find_bus(struct device_node
*mdio_bus_np
)
239 d
= class_find_device(&mdio_bus_class
, NULL
, mdio_bus_np
,
242 return d
? to_mii_bus(d
) : NULL
;
244 EXPORT_SYMBOL(of_mdio_find_bus
);
246 /* Walk the list of subnodes of a mdio bus and look for a node that
247 * matches the mdio device's address with its 'reg' property. If
248 * found, set the of_node pointer for the mdio device. This allows
249 * auto-probed phy devices to be supplied with information passed in
252 static void of_mdiobus_link_mdiodev(struct mii_bus
*bus
,
253 struct mdio_device
*mdiodev
)
255 struct device
*dev
= &mdiodev
->dev
;
256 struct device_node
*child
;
258 if (dev
->of_node
|| !bus
->dev
.of_node
)
261 for_each_available_child_of_node(bus
->dev
.of_node
, child
) {
265 ret
= of_property_read_u32(child
, "reg", &addr
);
267 dev_err(dev
, "%s has invalid MDIO address\n",
272 /* A MDIO device must have a reg property in the range [0-31] */
273 if (addr
>= PHY_MAX_ADDR
) {
274 dev_err(dev
, "%s MDIO address %i is too large\n",
275 child
->full_name
, addr
);
279 if (addr
== mdiodev
->addr
) {
280 dev
->of_node
= child
;
285 #else /* !IS_ENABLED(CONFIG_OF_MDIO) */
286 static inline void of_mdiobus_link_mdiodev(struct mii_bus
*mdio
,
287 struct mdio_device
*mdiodev
)
293 * __mdiobus_register - bring up all the PHYs on a given bus and attach them to bus
294 * @bus: target mii_bus
295 * @owner: module containing bus accessor functions
297 * Description: Called by a bus driver to bring up all the PHYs
298 * on a given bus, and attach them to the bus. Drivers should use
299 * mdiobus_register() rather than __mdiobus_register() unless they
300 * need to pass a specific owner module. MDIO devices which are not
301 * PHYs will not be brought up by this function. They are expected to
302 * to be explicitly listed in DT and instantiated by of_mdiobus_register().
304 * Returns 0 on success or < 0 on error.
306 int __mdiobus_register(struct mii_bus
*bus
, struct module
*owner
)
308 struct mdio_device
*mdiodev
;
311 if (NULL
== bus
|| NULL
== bus
->name
||
312 NULL
== bus
->read
|| NULL
== bus
->write
)
315 BUG_ON(bus
->state
!= MDIOBUS_ALLOCATED
&&
316 bus
->state
!= MDIOBUS_UNREGISTERED
);
319 bus
->dev
.parent
= bus
->parent
;
320 bus
->dev
.class = &mdio_bus_class
;
321 bus
->dev
.groups
= NULL
;
322 dev_set_name(&bus
->dev
, "%s", bus
->id
);
324 err
= device_register(&bus
->dev
);
326 pr_err("mii_bus %s failed to register\n", bus
->id
);
327 put_device(&bus
->dev
);
331 mutex_init(&bus
->mdio_lock
);
336 for (i
= 0; i
< PHY_MAX_ADDR
; i
++) {
337 if ((bus
->phy_mask
& (1 << i
)) == 0) {
338 struct phy_device
*phydev
;
340 phydev
= mdiobus_scan(bus
, i
);
341 if (IS_ERR(phydev
) && (PTR_ERR(phydev
) != -ENODEV
)) {
342 err
= PTR_ERR(phydev
);
348 mdiobus_setup_mdiodev_from_board_info(bus
);
350 bus
->state
= MDIOBUS_REGISTERED
;
351 pr_info("%s: probed\n", bus
->name
);
356 mdiodev
= bus
->mdio_map
[i
];
360 mdiodev
->device_remove(mdiodev
);
361 mdiodev
->device_free(mdiodev
);
363 device_del(&bus
->dev
);
366 EXPORT_SYMBOL(__mdiobus_register
);
368 void mdiobus_unregister(struct mii_bus
*bus
)
370 struct mdio_device
*mdiodev
;
373 BUG_ON(bus
->state
!= MDIOBUS_REGISTERED
);
374 bus
->state
= MDIOBUS_UNREGISTERED
;
376 for (i
= 0; i
< PHY_MAX_ADDR
; i
++) {
377 mdiodev
= bus
->mdio_map
[i
];
381 mdiodev
->device_remove(mdiodev
);
382 mdiodev
->device_free(mdiodev
);
384 device_del(&bus
->dev
);
386 EXPORT_SYMBOL(mdiobus_unregister
);
389 * mdiobus_free - free a struct mii_bus
390 * @bus: mii_bus to free
392 * This function releases the reference to the underlying device
393 * object in the mii_bus. If this is the last reference, the mii_bus
396 void mdiobus_free(struct mii_bus
*bus
)
398 /* For compatibility with error handling in drivers. */
399 if (bus
->state
== MDIOBUS_ALLOCATED
) {
404 BUG_ON(bus
->state
!= MDIOBUS_UNREGISTERED
);
405 bus
->state
= MDIOBUS_RELEASED
;
407 put_device(&bus
->dev
);
409 EXPORT_SYMBOL(mdiobus_free
);
412 * mdiobus_scan - scan a bus for MDIO devices.
413 * @bus: mii_bus to scan
414 * @addr: address on bus to scan
416 * This function scans the MDIO bus, looking for devices which can be
417 * identified using a vendor/product ID in registers 2 and 3. Not all
418 * MDIO devices have such registers, but PHY devices typically
419 * do. Hence this function assumes anything found is a PHY, or can be
420 * treated as a PHY. Other MDIO devices, such as switches, will
421 * probably not be found during the scan.
423 struct phy_device
*mdiobus_scan(struct mii_bus
*bus
, int addr
)
425 struct phy_device
*phydev
;
428 phydev
= get_phy_device(bus
, addr
, false);
433 * For DT, see if the auto-probed phy has a correspoding child
434 * in the bus node, and set the of_node pointer in this case.
436 of_mdiobus_link_mdiodev(bus
, &phydev
->mdio
);
438 err
= phy_device_register(phydev
);
440 phy_device_free(phydev
);
441 return ERR_PTR(-ENODEV
);
446 EXPORT_SYMBOL(mdiobus_scan
);
449 * mdiobus_read_nested - Nested version of the mdiobus_read function
450 * @bus: the mii_bus struct
451 * @addr: the phy address
452 * @regnum: register number to read
454 * In case of nested MDIO bus access avoid lockdep false positives by
455 * using mutex_lock_nested().
457 * NOTE: MUST NOT be called from interrupt context,
458 * because the bus read/write functions may wait for an interrupt
459 * to conclude the operation.
461 int mdiobus_read_nested(struct mii_bus
*bus
, int addr
, u32 regnum
)
465 BUG_ON(in_interrupt());
467 mutex_lock_nested(&bus
->mdio_lock
, MDIO_MUTEX_NESTED
);
468 retval
= bus
->read(bus
, addr
, regnum
);
469 mutex_unlock(&bus
->mdio_lock
);
471 trace_mdio_access(bus
, 1, addr
, regnum
, retval
, retval
);
475 EXPORT_SYMBOL(mdiobus_read_nested
);
478 * mdiobus_read - Convenience function for reading a given MII mgmt register
479 * @bus: the mii_bus struct
480 * @addr: the phy address
481 * @regnum: register number to read
483 * NOTE: MUST NOT be called from interrupt context,
484 * because the bus read/write functions may wait for an interrupt
485 * to conclude the operation.
487 int mdiobus_read(struct mii_bus
*bus
, int addr
, u32 regnum
)
491 BUG_ON(in_interrupt());
493 mutex_lock(&bus
->mdio_lock
);
494 retval
= bus
->read(bus
, addr
, regnum
);
495 mutex_unlock(&bus
->mdio_lock
);
497 trace_mdio_access(bus
, 1, addr
, regnum
, retval
, retval
);
501 EXPORT_SYMBOL(mdiobus_read
);
504 * mdiobus_write_nested - Nested version of the mdiobus_write function
505 * @bus: the mii_bus struct
506 * @addr: the phy address
507 * @regnum: register number to write
508 * @val: value to write to @regnum
510 * In case of nested MDIO bus access avoid lockdep false positives by
511 * using mutex_lock_nested().
513 * NOTE: MUST NOT be called from interrupt context,
514 * because the bus read/write functions may wait for an interrupt
515 * to conclude the operation.
517 int mdiobus_write_nested(struct mii_bus
*bus
, int addr
, u32 regnum
, u16 val
)
521 BUG_ON(in_interrupt());
523 mutex_lock_nested(&bus
->mdio_lock
, MDIO_MUTEX_NESTED
);
524 err
= bus
->write(bus
, addr
, regnum
, val
);
525 mutex_unlock(&bus
->mdio_lock
);
527 trace_mdio_access(bus
, 0, addr
, regnum
, val
, err
);
531 EXPORT_SYMBOL(mdiobus_write_nested
);
534 * mdiobus_write - Convenience function for writing a given MII mgmt register
535 * @bus: the mii_bus struct
536 * @addr: the phy address
537 * @regnum: register number to write
538 * @val: value to write to @regnum
540 * NOTE: MUST NOT be called from interrupt context,
541 * because the bus read/write functions may wait for an interrupt
542 * to conclude the operation.
544 int mdiobus_write(struct mii_bus
*bus
, int addr
, u32 regnum
, u16 val
)
548 BUG_ON(in_interrupt());
550 mutex_lock(&bus
->mdio_lock
);
551 err
= bus
->write(bus
, addr
, regnum
, val
);
552 mutex_unlock(&bus
->mdio_lock
);
554 trace_mdio_access(bus
, 0, addr
, regnum
, val
, err
);
558 EXPORT_SYMBOL(mdiobus_write
);
561 * mdio_bus_match - determine if given MDIO driver supports the given
563 * @dev: target MDIO device
564 * @drv: given MDIO driver
566 * Description: Given a MDIO device, and a MDIO driver, return 1 if
567 * the driver supports the device. Otherwise, return 0. This may
568 * require calling the devices own match function, since different classes
569 * of MDIO devices have different match criteria.
571 static int mdio_bus_match(struct device
*dev
, struct device_driver
*drv
)
573 struct mdio_device
*mdio
= to_mdio_device(dev
);
575 if (of_driver_match_device(dev
, drv
))
579 return mdio
->bus_match(dev
, drv
);
585 static int mdio_bus_suspend(struct device
*dev
)
587 struct mdio_device
*mdio
= to_mdio_device(dev
);
589 if (mdio
->pm_ops
&& mdio
->pm_ops
->suspend
)
590 return mdio
->pm_ops
->suspend(dev
);
595 static int mdio_bus_resume(struct device
*dev
)
597 struct mdio_device
*mdio
= to_mdio_device(dev
);
599 if (mdio
->pm_ops
&& mdio
->pm_ops
->resume
)
600 return mdio
->pm_ops
->resume(dev
);
605 static int mdio_bus_restore(struct device
*dev
)
607 struct mdio_device
*mdio
= to_mdio_device(dev
);
609 if (mdio
->pm_ops
&& mdio
->pm_ops
->restore
)
610 return mdio
->pm_ops
->restore(dev
);
615 static const struct dev_pm_ops mdio_bus_pm_ops
= {
616 .suspend
= mdio_bus_suspend
,
617 .resume
= mdio_bus_resume
,
618 .freeze
= mdio_bus_suspend
,
619 .thaw
= mdio_bus_resume
,
620 .restore
= mdio_bus_restore
,
623 #define MDIO_BUS_PM_OPS (&mdio_bus_pm_ops)
627 #define MDIO_BUS_PM_OPS NULL
629 #endif /* CONFIG_PM */
631 struct bus_type mdio_bus_type
= {
633 .match
= mdio_bus_match
,
634 .pm
= MDIO_BUS_PM_OPS
,
636 EXPORT_SYMBOL(mdio_bus_type
);
638 int __init
mdio_bus_init(void)
642 ret
= class_register(&mdio_bus_class
);
644 ret
= bus_register(&mdio_bus_type
);
646 class_unregister(&mdio_bus_class
);
652 void mdio_bus_exit(void)
654 class_unregister(&mdio_bus_class
);
655 bus_unregister(&mdio_bus_type
);