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>
42 * mdiobus_alloc_size - allocate a mii_bus structure
43 * @size: extra amount of memory to allocate for private storage.
44 * If non-zero, then bus->priv is points to that memory.
46 * Description: called by a bus driver to allocate an mii_bus
47 * structure to fill in.
49 struct mii_bus *mdiobus_alloc_size(size_t size)
52 size_t aligned_size = ALIGN(sizeof(*bus), NETDEV_ALIGN);
55 /* If we alloc extra space, it should be aligned */
57 alloc_size = aligned_size + size;
59 alloc_size = sizeof(*bus);
61 bus = kzalloc(alloc_size, GFP_KERNEL);
63 bus->state = MDIOBUS_ALLOCATED;
65 bus->priv = (void *)bus + aligned_size;
70 EXPORT_SYMBOL(mdiobus_alloc_size);
72 static void _devm_mdiobus_free(struct device *dev, void *res)
74 mdiobus_free(*(struct mii_bus **)res);
77 static int devm_mdiobus_match(struct device *dev, void *res, void *data)
79 struct mii_bus **r = res;
81 if (WARN_ON(!r || !*r))
88 * devm_mdiobus_alloc_size - Resource-managed mdiobus_alloc_size()
89 * @dev: Device to allocate mii_bus for
90 * @sizeof_priv: Space to allocate for private structure.
92 * Managed mdiobus_alloc_size. mii_bus allocated with this function is
93 * automatically freed on driver detach.
95 * If an mii_bus allocated with this function needs to be freed separately,
96 * devm_mdiobus_free() must be used.
99 * Pointer to allocated mii_bus on success, NULL on failure.
101 struct mii_bus *devm_mdiobus_alloc_size(struct device *dev, int sizeof_priv)
103 struct mii_bus **ptr, *bus;
105 ptr = devres_alloc(_devm_mdiobus_free, sizeof(*ptr), GFP_KERNEL);
109 /* use raw alloc_dr for kmalloc caller tracing */
110 bus = mdiobus_alloc_size(sizeof_priv);
113 devres_add(dev, ptr);
120 EXPORT_SYMBOL_GPL(devm_mdiobus_alloc_size);
123 * devm_mdiobus_free - Resource-managed mdiobus_free()
124 * @dev: Device this mii_bus belongs to
125 * @bus: the mii_bus associated with the device
127 * Free mii_bus allocated with devm_mdiobus_alloc_size().
129 void devm_mdiobus_free(struct device *dev, struct mii_bus *bus)
133 rc = devres_release(dev, _devm_mdiobus_free,
134 devm_mdiobus_match, bus);
137 EXPORT_SYMBOL_GPL(devm_mdiobus_free);
140 * mdiobus_release - mii_bus device release callback
141 * @d: the target struct device that contains the mii_bus
143 * Description: called when the last reference to an mii_bus is
144 * dropped, to free the underlying memory.
146 static void mdiobus_release(struct device *d)
148 struct mii_bus *bus = to_mii_bus(d);
149 BUG_ON(bus->state != MDIOBUS_RELEASED &&
150 /* for compatibility with error handling in drivers */
151 bus->state != MDIOBUS_ALLOCATED);
155 static struct class mdio_bus_class = {
157 .dev_release = mdiobus_release,
160 #if IS_ENABLED(CONFIG_OF_MDIO)
161 /* Helper function for of_mdio_find_bus */
162 static int of_mdio_bus_match(struct device *dev, const void *mdio_bus_np)
164 return dev->of_node == mdio_bus_np;
167 * of_mdio_find_bus - Given an mii_bus node, find the mii_bus.
168 * @mdio_bus_np: Pointer to the mii_bus.
170 * Returns a pointer to the mii_bus, or NULL if none found.
172 * Because the association of a device_node and mii_bus is made via
173 * of_mdiobus_register(), the mii_bus cannot be found before it is
174 * registered with of_mdiobus_register().
177 struct mii_bus *of_mdio_find_bus(struct device_node *mdio_bus_np)
184 d = class_find_device(&mdio_bus_class, NULL, mdio_bus_np,
187 return d ? to_mii_bus(d) : NULL;
189 EXPORT_SYMBOL(of_mdio_find_bus);
193 * mdiobus_register - bring up all the PHYs on a given bus and attach them to bus
194 * @bus: target mii_bus
196 * Description: Called by a bus driver to bring up all the PHYs
197 * on a given bus, and attach them to the bus.
199 * Returns 0 on success or < 0 on error.
201 int mdiobus_register(struct mii_bus *bus)
205 if (NULL == bus || NULL == bus->name ||
206 NULL == bus->read || NULL == bus->write)
209 BUG_ON(bus->state != MDIOBUS_ALLOCATED &&
210 bus->state != MDIOBUS_UNREGISTERED);
212 bus->dev.parent = bus->parent;
213 bus->dev.class = &mdio_bus_class;
214 bus->dev.groups = NULL;
215 dev_set_name(&bus->dev, "%s", bus->id);
217 err = device_register(&bus->dev);
219 pr_err("mii_bus %s failed to register\n", bus->id);
220 put_device(&bus->dev);
224 mutex_init(&bus->mdio_lock);
229 for (i = 0; i < PHY_MAX_ADDR; i++) {
230 if ((bus->phy_mask & (1 << i)) == 0) {
231 struct phy_device *phydev;
233 phydev = mdiobus_scan(bus, i);
234 if (IS_ERR(phydev)) {
235 err = PTR_ERR(phydev);
241 bus->state = MDIOBUS_REGISTERED;
242 pr_info("%s: probed\n", bus->name);
248 device_unregister(&bus->phy_map[i]->dev);
250 device_del(&bus->dev);
253 EXPORT_SYMBOL(mdiobus_register);
255 void mdiobus_unregister(struct mii_bus *bus)
259 BUG_ON(bus->state != MDIOBUS_REGISTERED);
260 bus->state = MDIOBUS_UNREGISTERED;
262 device_del(&bus->dev);
263 for (i = 0; i < PHY_MAX_ADDR; i++) {
265 device_unregister(&bus->phy_map[i]->dev);
266 bus->phy_map[i] = NULL;
269 EXPORT_SYMBOL(mdiobus_unregister);
272 * mdiobus_free - free a struct mii_bus
273 * @bus: mii_bus to free
275 * This function releases the reference to the underlying device
276 * object in the mii_bus. If this is the last reference, the mii_bus
279 void mdiobus_free(struct mii_bus *bus)
281 /* For compatibility with error handling in drivers. */
282 if (bus->state == MDIOBUS_ALLOCATED) {
287 BUG_ON(bus->state != MDIOBUS_UNREGISTERED);
288 bus->state = MDIOBUS_RELEASED;
290 put_device(&bus->dev);
292 EXPORT_SYMBOL(mdiobus_free);
294 struct phy_device *mdiobus_scan(struct mii_bus *bus, int addr)
296 struct phy_device *phydev;
299 phydev = get_phy_device(bus, addr, false);
300 if (IS_ERR(phydev) || phydev == NULL)
304 * For DT, see if the auto-probed phy has a correspoding child
305 * in the bus node, and set the of_node pointer in this case.
307 of_mdiobus_link_phydev(bus, phydev);
309 err = phy_device_register(phydev);
311 phy_device_free(phydev);
317 EXPORT_SYMBOL(mdiobus_scan);
320 * mdiobus_read - Convenience function for reading a given MII mgmt register
321 * @bus: the mii_bus struct
322 * @addr: the phy address
323 * @regnum: register number to read
325 * NOTE: MUST NOT be called from interrupt context,
326 * because the bus read/write functions may wait for an interrupt
327 * to conclude the operation.
329 int mdiobus_read(struct mii_bus *bus, int addr, u32 regnum)
333 BUG_ON(in_interrupt());
335 mutex_lock(&bus->mdio_lock);
336 retval = bus->read(bus, addr, regnum);
337 mutex_unlock(&bus->mdio_lock);
341 EXPORT_SYMBOL(mdiobus_read);
344 * mdiobus_write - Convenience function for writing a given MII mgmt register
345 * @bus: the mii_bus struct
346 * @addr: the phy address
347 * @regnum: register number to write
348 * @val: value to write to @regnum
350 * NOTE: MUST NOT be called from interrupt context,
351 * because the bus read/write functions may wait for an interrupt
352 * to conclude the operation.
354 int mdiobus_write(struct mii_bus *bus, int addr, u32 regnum, u16 val)
358 BUG_ON(in_interrupt());
360 mutex_lock(&bus->mdio_lock);
361 err = bus->write(bus, addr, regnum, val);
362 mutex_unlock(&bus->mdio_lock);
366 EXPORT_SYMBOL(mdiobus_write);
369 * mdio_bus_match - determine if given PHY driver supports the given PHY device
370 * @dev: target PHY device
371 * @drv: given PHY driver
373 * Description: Given a PHY device, and a PHY driver, return 1 if
374 * the driver supports the device. Otherwise, return 0.
376 static int mdio_bus_match(struct device *dev, struct device_driver *drv)
378 struct phy_device *phydev = to_phy_device(dev);
379 struct phy_driver *phydrv = to_phy_driver(drv);
381 if (of_driver_match_device(dev, drv))
384 if (phydrv->match_phy_device)
385 return phydrv->match_phy_device(phydev);
387 return (phydrv->phy_id & phydrv->phy_id_mask) ==
388 (phydev->phy_id & phydrv->phy_id_mask);
393 static bool mdio_bus_phy_may_suspend(struct phy_device *phydev)
395 struct device_driver *drv = phydev->dev.driver;
396 struct phy_driver *phydrv = to_phy_driver(drv);
397 struct net_device *netdev = phydev->attached_dev;
399 if (!drv || !phydrv->suspend)
402 /* PHY not attached? May suspend. */
406 /* Don't suspend PHY if the attched netdev parent may wakeup.
407 * The parent may point to a PCI device, as in tg3 driver.
409 if (netdev->dev.parent && device_may_wakeup(netdev->dev.parent))
412 /* Also don't suspend PHY if the netdev itself may wakeup. This
413 * is the case for devices w/o underlaying pwr. mgmt. aware bus,
416 if (device_may_wakeup(&netdev->dev))
422 static int mdio_bus_suspend(struct device *dev)
424 struct phy_driver *phydrv = to_phy_driver(dev->driver);
425 struct phy_device *phydev = to_phy_device(dev);
427 /* We must stop the state machine manually, otherwise it stops out of
428 * control, possibly with the phydev->lock held. Upon resume, netdev
429 * may call phy routines that try to grab the same lock, and that may
430 * lead to a deadlock.
432 if (phydev->attached_dev && phydev->adjust_link)
433 phy_stop_machine(phydev);
435 if (!mdio_bus_phy_may_suspend(phydev))
438 return phydrv->suspend(phydev);
441 static int mdio_bus_resume(struct device *dev)
443 struct phy_driver *phydrv = to_phy_driver(dev->driver);
444 struct phy_device *phydev = to_phy_device(dev);
447 if (!mdio_bus_phy_may_suspend(phydev))
450 ret = phydrv->resume(phydev);
455 if (phydev->attached_dev && phydev->adjust_link)
456 phy_start_machine(phydev);
461 static int mdio_bus_restore(struct device *dev)
463 struct phy_device *phydev = to_phy_device(dev);
464 struct net_device *netdev = phydev->attached_dev;
470 ret = phy_init_hw(phydev);
474 /* The PHY needs to renegotiate. */
476 phydev->state = PHY_UP;
478 phy_start_machine(phydev);
483 static const struct dev_pm_ops mdio_bus_pm_ops = {
484 .suspend = mdio_bus_suspend,
485 .resume = mdio_bus_resume,
486 .freeze = mdio_bus_suspend,
487 .thaw = mdio_bus_resume,
488 .restore = mdio_bus_restore,
491 #define MDIO_BUS_PM_OPS (&mdio_bus_pm_ops)
495 #define MDIO_BUS_PM_OPS NULL
497 #endif /* CONFIG_PM */
500 phy_id_show(struct device *dev, struct device_attribute *attr, char *buf)
502 struct phy_device *phydev = to_phy_device(dev);
504 return sprintf(buf, "0x%.8lx\n", (unsigned long)phydev->phy_id);
506 static DEVICE_ATTR_RO(phy_id);
509 phy_interface_show(struct device *dev, struct device_attribute *attr, char *buf)
511 struct phy_device *phydev = to_phy_device(dev);
513 return sprintf(buf, "%s\n", phy_modes(phydev->interface));
515 static DEVICE_ATTR_RO(phy_interface);
518 phy_has_fixups_show(struct device *dev, struct device_attribute *attr, char *buf)
520 struct phy_device *phydev = to_phy_device(dev);
522 return sprintf(buf, "%d\n", phydev->has_fixups);
524 static DEVICE_ATTR_RO(phy_has_fixups);
526 static struct attribute *mdio_dev_attrs[] = {
527 &dev_attr_phy_id.attr,
528 &dev_attr_phy_interface.attr,
529 &dev_attr_phy_has_fixups.attr,
532 ATTRIBUTE_GROUPS(mdio_dev);
534 struct bus_type mdio_bus_type = {
536 .match = mdio_bus_match,
537 .pm = MDIO_BUS_PM_OPS,
538 .dev_groups = mdio_dev_groups,
540 EXPORT_SYMBOL(mdio_bus_type);
542 int __init mdio_bus_init(void)
546 ret = class_register(&mdio_bus_class);
548 ret = bus_register(&mdio_bus_type);
550 class_unregister(&mdio_bus_class);
556 void mdio_bus_exit(void)
558 class_unregister(&mdio_bus_class);
559 bus_unregister(&mdio_bus_type);