Linux 4.1.16
[linux/fpc-iii.git] / drivers / net / phy / mdio_bus.c
blob095ef3fe369af5ebe08254384abc38176df1aef1
1 /* MDIO Bus interface
3 * Author: Andy Fleming
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>
31 #include <linux/mm.h>
32 #include <linux/module.h>
33 #include <linux/mii.h>
34 #include <linux/ethtool.h>
35 #include <linux/phy.h>
36 #include <linux/io.h>
37 #include <linux/uaccess.h>
39 #include <asm/irq.h>
41 /**
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)
51 struct mii_bus *bus;
52 size_t aligned_size = ALIGN(sizeof(*bus), NETDEV_ALIGN);
53 size_t alloc_size;
55 /* If we alloc extra space, it should be aligned */
56 if (size)
57 alloc_size = aligned_size + size;
58 else
59 alloc_size = sizeof(*bus);
61 bus = kzalloc(alloc_size, GFP_KERNEL);
62 if (bus) {
63 bus->state = MDIOBUS_ALLOCATED;
64 if (size)
65 bus->priv = (void *)bus + aligned_size;
68 return bus;
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))
82 return 0;
84 return *r == data;
87 /**
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.
98 * RETURNS:
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);
106 if (!ptr)
107 return NULL;
109 /* use raw alloc_dr for kmalloc caller tracing */
110 bus = mdiobus_alloc_size(sizeof_priv);
111 if (bus) {
112 *ptr = bus;
113 devres_add(dev, ptr);
114 } else {
115 devres_free(ptr);
118 return bus;
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)
131 int rc;
133 rc = devres_release(dev, _devm_mdiobus_free,
134 devm_mdiobus_match, bus);
135 WARN_ON(rc);
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);
152 kfree(bus);
155 static struct class mdio_bus_class = {
156 .name = "mdio_bus",
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)
179 struct device *d;
181 if (!mdio_bus_np)
182 return NULL;
184 d = class_find_device(&mdio_bus_class, NULL, mdio_bus_np,
185 of_mdio_bus_match);
187 return d ? to_mii_bus(d) : NULL;
189 EXPORT_SYMBOL(of_mdio_find_bus);
191 /* Walk the list of subnodes of a mdio bus and look for a node that matches the
192 * phy's address with its 'reg' property. If found, set the of_node pointer for
193 * the phy. This allows auto-probed pyh devices to be supplied with information
194 * passed in via DT.
196 static void of_mdiobus_link_phydev(struct mii_bus *mdio,
197 struct phy_device *phydev)
199 struct device *dev = &phydev->dev;
200 struct device_node *child;
202 if (dev->of_node || !mdio->dev.of_node)
203 return;
205 for_each_available_child_of_node(mdio->dev.of_node, child) {
206 int addr;
207 int ret;
209 ret = of_property_read_u32(child, "reg", &addr);
210 if (ret < 0) {
211 dev_err(dev, "%s has invalid PHY address\n",
212 child->full_name);
213 continue;
216 /* A PHY must have a reg property in the range [0-31] */
217 if (addr >= PHY_MAX_ADDR) {
218 dev_err(dev, "%s PHY address %i is too large\n",
219 child->full_name, addr);
220 continue;
223 if (addr == phydev->addr) {
224 dev->of_node = child;
225 return;
229 #else /* !IS_ENABLED(CONFIG_OF_MDIO) */
230 static inline void of_mdiobus_link_phydev(struct mii_bus *mdio,
231 struct phy_device *phydev)
234 #endif
237 * mdiobus_register - bring up all the PHYs on a given bus and attach them to bus
238 * @bus: target mii_bus
240 * Description: Called by a bus driver to bring up all the PHYs
241 * on a given bus, and attach them to the bus.
243 * Returns 0 on success or < 0 on error.
245 int mdiobus_register(struct mii_bus *bus)
247 int i, err;
249 if (NULL == bus || NULL == bus->name ||
250 NULL == bus->read || NULL == bus->write)
251 return -EINVAL;
253 BUG_ON(bus->state != MDIOBUS_ALLOCATED &&
254 bus->state != MDIOBUS_UNREGISTERED);
256 bus->dev.parent = bus->parent;
257 bus->dev.class = &mdio_bus_class;
258 bus->dev.groups = NULL;
259 dev_set_name(&bus->dev, "%s", bus->id);
261 err = device_register(&bus->dev);
262 if (err) {
263 pr_err("mii_bus %s failed to register\n", bus->id);
264 put_device(&bus->dev);
265 return -EINVAL;
268 mutex_init(&bus->mdio_lock);
270 if (bus->reset)
271 bus->reset(bus);
273 for (i = 0; i < PHY_MAX_ADDR; i++) {
274 if ((bus->phy_mask & (1 << i)) == 0) {
275 struct phy_device *phydev;
277 phydev = mdiobus_scan(bus, i);
278 if (IS_ERR(phydev)) {
279 err = PTR_ERR(phydev);
280 goto error;
285 bus->state = MDIOBUS_REGISTERED;
286 pr_info("%s: probed\n", bus->name);
287 return 0;
289 error:
290 while (--i >= 0) {
291 if (bus->phy_map[i])
292 device_unregister(&bus->phy_map[i]->dev);
294 device_del(&bus->dev);
295 return err;
297 EXPORT_SYMBOL(mdiobus_register);
299 void mdiobus_unregister(struct mii_bus *bus)
301 int i;
303 BUG_ON(bus->state != MDIOBUS_REGISTERED);
304 bus->state = MDIOBUS_UNREGISTERED;
306 device_del(&bus->dev);
307 for (i = 0; i < PHY_MAX_ADDR; i++) {
308 if (bus->phy_map[i])
309 device_unregister(&bus->phy_map[i]->dev);
310 bus->phy_map[i] = NULL;
313 EXPORT_SYMBOL(mdiobus_unregister);
316 * mdiobus_free - free a struct mii_bus
317 * @bus: mii_bus to free
319 * This function releases the reference to the underlying device
320 * object in the mii_bus. If this is the last reference, the mii_bus
321 * will be freed.
323 void mdiobus_free(struct mii_bus *bus)
325 /* For compatibility with error handling in drivers. */
326 if (bus->state == MDIOBUS_ALLOCATED) {
327 kfree(bus);
328 return;
331 BUG_ON(bus->state != MDIOBUS_UNREGISTERED);
332 bus->state = MDIOBUS_RELEASED;
334 put_device(&bus->dev);
336 EXPORT_SYMBOL(mdiobus_free);
338 struct phy_device *mdiobus_scan(struct mii_bus *bus, int addr)
340 struct phy_device *phydev;
341 int err;
343 phydev = get_phy_device(bus, addr, false);
344 if (IS_ERR(phydev) || phydev == NULL)
345 return phydev;
348 * For DT, see if the auto-probed phy has a correspoding child
349 * in the bus node, and set the of_node pointer in this case.
351 of_mdiobus_link_phydev(bus, phydev);
353 err = phy_device_register(phydev);
354 if (err) {
355 phy_device_free(phydev);
356 return NULL;
359 return phydev;
361 EXPORT_SYMBOL(mdiobus_scan);
364 * mdiobus_read - Convenience function for reading a given MII mgmt register
365 * @bus: the mii_bus struct
366 * @addr: the phy address
367 * @regnum: register number to read
369 * NOTE: MUST NOT be called from interrupt context,
370 * because the bus read/write functions may wait for an interrupt
371 * to conclude the operation.
373 int mdiobus_read(struct mii_bus *bus, int addr, u32 regnum)
375 int retval;
377 BUG_ON(in_interrupt());
379 mutex_lock(&bus->mdio_lock);
380 retval = bus->read(bus, addr, regnum);
381 mutex_unlock(&bus->mdio_lock);
383 return retval;
385 EXPORT_SYMBOL(mdiobus_read);
388 * mdiobus_write - Convenience function for writing a given MII mgmt register
389 * @bus: the mii_bus struct
390 * @addr: the phy address
391 * @regnum: register number to write
392 * @val: value to write to @regnum
394 * NOTE: MUST NOT be called from interrupt context,
395 * because the bus read/write functions may wait for an interrupt
396 * to conclude the operation.
398 int mdiobus_write(struct mii_bus *bus, int addr, u32 regnum, u16 val)
400 int err;
402 BUG_ON(in_interrupt());
404 mutex_lock(&bus->mdio_lock);
405 err = bus->write(bus, addr, regnum, val);
406 mutex_unlock(&bus->mdio_lock);
408 return err;
410 EXPORT_SYMBOL(mdiobus_write);
413 * mdio_bus_match - determine if given PHY driver supports the given PHY device
414 * @dev: target PHY device
415 * @drv: given PHY driver
417 * Description: Given a PHY device, and a PHY driver, return 1 if
418 * the driver supports the device. Otherwise, return 0.
420 static int mdio_bus_match(struct device *dev, struct device_driver *drv)
422 struct phy_device *phydev = to_phy_device(dev);
423 struct phy_driver *phydrv = to_phy_driver(drv);
425 if (of_driver_match_device(dev, drv))
426 return 1;
428 if (phydrv->match_phy_device)
429 return phydrv->match_phy_device(phydev);
431 return (phydrv->phy_id & phydrv->phy_id_mask) ==
432 (phydev->phy_id & phydrv->phy_id_mask);
435 #ifdef CONFIG_PM
437 static bool mdio_bus_phy_may_suspend(struct phy_device *phydev)
439 struct device_driver *drv = phydev->dev.driver;
440 struct phy_driver *phydrv = to_phy_driver(drv);
441 struct net_device *netdev = phydev->attached_dev;
443 if (!drv || !phydrv->suspend)
444 return false;
446 /* PHY not attached? May suspend if the PHY has not already been
447 * suspended as part of a prior call to phy_disconnect() ->
448 * phy_detach() -> phy_suspend() because the parent netdev might be the
449 * MDIO bus driver and clock gated at this point.
451 if (!netdev)
452 return !phydev->suspended;
454 /* Don't suspend PHY if the attched netdev parent may wakeup.
455 * The parent may point to a PCI device, as in tg3 driver.
457 if (netdev->dev.parent && device_may_wakeup(netdev->dev.parent))
458 return false;
460 /* Also don't suspend PHY if the netdev itself may wakeup. This
461 * is the case for devices w/o underlaying pwr. mgmt. aware bus,
462 * e.g. SoC devices.
464 if (device_may_wakeup(&netdev->dev))
465 return false;
467 return true;
470 static int mdio_bus_suspend(struct device *dev)
472 struct phy_device *phydev = to_phy_device(dev);
474 /* We must stop the state machine manually, otherwise it stops out of
475 * control, possibly with the phydev->lock held. Upon resume, netdev
476 * may call phy routines that try to grab the same lock, and that may
477 * lead to a deadlock.
479 if (phydev->attached_dev && phydev->adjust_link)
480 phy_stop_machine(phydev);
482 if (!mdio_bus_phy_may_suspend(phydev))
483 return 0;
485 return phy_suspend(phydev);
488 static int mdio_bus_resume(struct device *dev)
490 struct phy_device *phydev = to_phy_device(dev);
491 int ret;
493 if (!mdio_bus_phy_may_suspend(phydev))
494 goto no_resume;
496 ret = phy_resume(phydev);
497 if (ret < 0)
498 return ret;
500 no_resume:
501 if (phydev->attached_dev && phydev->adjust_link)
502 phy_start_machine(phydev);
504 return 0;
507 static int mdio_bus_restore(struct device *dev)
509 struct phy_device *phydev = to_phy_device(dev);
510 struct net_device *netdev = phydev->attached_dev;
511 int ret;
513 if (!netdev)
514 return 0;
516 ret = phy_init_hw(phydev);
517 if (ret < 0)
518 return ret;
520 /* The PHY needs to renegotiate. */
521 phydev->link = 0;
522 phydev->state = PHY_UP;
524 phy_start_machine(phydev);
526 return 0;
529 static const struct dev_pm_ops mdio_bus_pm_ops = {
530 .suspend = mdio_bus_suspend,
531 .resume = mdio_bus_resume,
532 .freeze = mdio_bus_suspend,
533 .thaw = mdio_bus_resume,
534 .restore = mdio_bus_restore,
537 #define MDIO_BUS_PM_OPS (&mdio_bus_pm_ops)
539 #else
541 #define MDIO_BUS_PM_OPS NULL
543 #endif /* CONFIG_PM */
545 static ssize_t
546 phy_id_show(struct device *dev, struct device_attribute *attr, char *buf)
548 struct phy_device *phydev = to_phy_device(dev);
550 return sprintf(buf, "0x%.8lx\n", (unsigned long)phydev->phy_id);
552 static DEVICE_ATTR_RO(phy_id);
554 static ssize_t
555 phy_interface_show(struct device *dev, struct device_attribute *attr, char *buf)
557 struct phy_device *phydev = to_phy_device(dev);
558 const char *mode = NULL;
560 if (phy_is_internal(phydev))
561 mode = "internal";
562 else
563 mode = phy_modes(phydev->interface);
565 return sprintf(buf, "%s\n", mode);
567 static DEVICE_ATTR_RO(phy_interface);
569 static ssize_t
570 phy_has_fixups_show(struct device *dev, struct device_attribute *attr, char *buf)
572 struct phy_device *phydev = to_phy_device(dev);
574 return sprintf(buf, "%d\n", phydev->has_fixups);
576 static DEVICE_ATTR_RO(phy_has_fixups);
578 static struct attribute *mdio_dev_attrs[] = {
579 &dev_attr_phy_id.attr,
580 &dev_attr_phy_interface.attr,
581 &dev_attr_phy_has_fixups.attr,
582 NULL,
584 ATTRIBUTE_GROUPS(mdio_dev);
586 struct bus_type mdio_bus_type = {
587 .name = "mdio_bus",
588 .match = mdio_bus_match,
589 .pm = MDIO_BUS_PM_OPS,
590 .dev_groups = mdio_dev_groups,
592 EXPORT_SYMBOL(mdio_bus_type);
594 int __init mdio_bus_init(void)
596 int ret;
598 ret = class_register(&mdio_bus_class);
599 if (!ret) {
600 ret = bus_register(&mdio_bus_type);
601 if (ret)
602 class_unregister(&mdio_bus_class);
605 return ret;
608 void mdio_bus_exit(void)
610 class_unregister(&mdio_bus_class);
611 bus_unregister(&mdio_bus_type);