blob: d4a4992b493591aef6a2630f6f17bca330f6e9ee [file] [log] [blame]
Andy Fleming00db8182005-07-30 19:31:23 -04001/*
2 * drivers/net/phy/mdio_bus.c
3 *
4 * MDIO Bus interface
5 *
6 * Author: Andy Fleming
7 *
8 * Copyright (c) 2004 Freescale Semiconductor, Inc.
9 *
10 * This program is free software; you can redistribute it and/or modify it
11 * under the terms of the GNU General Public License as published by the
12 * Free Software Foundation; either version 2 of the License, or (at your
13 * option) any later version.
14 *
15 */
Joe Perches8d242482012-06-09 07:49:07 +000016
17#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
18
Andy Fleming00db8182005-07-30 19:31:23 -040019#include <linux/kernel.h>
Andy Fleming00db8182005-07-30 19:31:23 -040020#include <linux/string.h>
21#include <linux/errno.h>
22#include <linux/unistd.h>
23#include <linux/slab.h>
24#include <linux/interrupt.h>
25#include <linux/init.h>
26#include <linux/delay.h>
Anton Vorontsov3d1e4db2009-02-01 00:53:34 -080027#include <linux/device.h>
Andy Fleming00db8182005-07-30 19:31:23 -040028#include <linux/netdevice.h>
29#include <linux/etherdevice.h>
30#include <linux/skbuff.h>
31#include <linux/spinlock.h>
32#include <linux/mm.h>
33#include <linux/module.h>
Andy Fleming00db8182005-07-30 19:31:23 -040034#include <linux/mii.h>
35#include <linux/ethtool.h>
36#include <linux/phy.h>
37
38#include <asm/io.h>
39#include <asm/irq.h>
40#include <asm/uaccess.h>
41
Randy Dunlapb3df0da2007-03-06 02:41:48 -080042/**
Timur Tabieb8a54a72012-01-12 15:23:04 -080043 * mdiobus_alloc_size - allocate a mii_bus structure
Randy Dunlapaf58f1d2012-01-21 09:03:04 +000044 * @size: extra amount of memory to allocate for private storage.
45 * If non-zero, then bus->priv is points to that memory.
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -070046 *
47 * Description: called by a bus driver to allocate an mii_bus
48 * structure to fill in.
49 */
Timur Tabieb8a54a72012-01-12 15:23:04 -080050struct mii_bus *mdiobus_alloc_size(size_t size)
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -070051{
Lennert Buytenhek46abc022008-10-08 16:33:40 -070052 struct mii_bus *bus;
Timur Tabieb8a54a72012-01-12 15:23:04 -080053 size_t aligned_size = ALIGN(sizeof(*bus), NETDEV_ALIGN);
54 size_t alloc_size;
Lennert Buytenhek46abc022008-10-08 16:33:40 -070055
Timur Tabieb8a54a72012-01-12 15:23:04 -080056 /* If we alloc extra space, it should be aligned */
57 if (size)
58 alloc_size = aligned_size + size;
59 else
60 alloc_size = sizeof(*bus);
61
62 bus = kzalloc(alloc_size, GFP_KERNEL);
63 if (bus) {
Lennert Buytenhek46abc022008-10-08 16:33:40 -070064 bus->state = MDIOBUS_ALLOCATED;
Timur Tabieb8a54a72012-01-12 15:23:04 -080065 if (size)
66 bus->priv = (void *)bus + aligned_size;
67 }
Lennert Buytenhek46abc022008-10-08 16:33:40 -070068
69 return bus;
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -070070}
Timur Tabieb8a54a72012-01-12 15:23:04 -080071EXPORT_SYMBOL(mdiobus_alloc_size);
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -070072
73/**
Lennert Buytenhek46abc022008-10-08 16:33:40 -070074 * mdiobus_release - mii_bus device release callback
Randy Dunlap78c36b12008-10-13 18:46:22 -070075 * @d: the target struct device that contains the mii_bus
Lennert Buytenhek46abc022008-10-08 16:33:40 -070076 *
77 * Description: called when the last reference to an mii_bus is
78 * dropped, to free the underlying memory.
79 */
80static void mdiobus_release(struct device *d)
81{
82 struct mii_bus *bus = to_mii_bus(d);
Krzysztof Halasa161c8d22008-12-25 16:50:41 -080083 BUG_ON(bus->state != MDIOBUS_RELEASED &&
84 /* for compatibility with error handling in drivers */
85 bus->state != MDIOBUS_ALLOCATED);
Lennert Buytenhek46abc022008-10-08 16:33:40 -070086 kfree(bus);
87}
88
89static struct class mdio_bus_class = {
90 .name = "mdio_bus",
91 .dev_release = mdiobus_release,
92};
93
Bjørn Morkb943fbb2012-05-11 05:47:01 +000094#if IS_ENABLED(CONFIG_OF_MDIO)
David Daney25106022012-05-02 15:16:37 +000095/* Helper function for of_mdio_find_bus */
96static int of_mdio_bus_match(struct device *dev, void *mdio_bus_np)
97{
98 return dev->of_node == mdio_bus_np;
99}
100/**
101 * of_mdio_find_bus - Given an mii_bus node, find the mii_bus.
102 * @mdio_np: Pointer to the mii_bus.
103 *
104 * Returns a pointer to the mii_bus, or NULL if none found.
105 *
106 * Because the association of a device_node and mii_bus is made via
107 * of_mdiobus_register(), the mii_bus cannot be found before it is
108 * registered with of_mdiobus_register().
109 *
110 */
111struct mii_bus *of_mdio_find_bus(struct device_node *mdio_bus_np)
112{
113 struct device *d;
114
115 if (!mdio_bus_np)
116 return NULL;
117
118 d = class_find_device(&mdio_bus_class, NULL, mdio_bus_np,
119 of_mdio_bus_match);
120
121 return d ? to_mii_bus(d) : NULL;
122}
123EXPORT_SYMBOL(of_mdio_find_bus);
124#endif
125
Lennert Buytenhek46abc022008-10-08 16:33:40 -0700126/**
Randy Dunlapb3df0da2007-03-06 02:41:48 -0800127 * mdiobus_register - bring up all the PHYs on a given bus and attach them to bus
128 * @bus: target mii_bus
Andy Fleminge1393452005-08-24 18:46:21 -0500129 *
Randy Dunlapb3df0da2007-03-06 02:41:48 -0800130 * Description: Called by a bus driver to bring up all the PHYs
131 * on a given bus, and attach them to the bus.
132 *
133 * Returns 0 on success or < 0 on error.
Andy Fleminge1393452005-08-24 18:46:21 -0500134 */
135int mdiobus_register(struct mii_bus *bus)
136{
Krzysztof Halasa161c8d22008-12-25 16:50:41 -0800137 int i, err;
Andy Fleminge1393452005-08-24 18:46:21 -0500138
Andy Fleminge1393452005-08-24 18:46:21 -0500139 if (NULL == bus || NULL == bus->name ||
140 NULL == bus->read ||
141 NULL == bus->write)
142 return -EINVAL;
143
Lennert Buytenhek46abc022008-10-08 16:33:40 -0700144 BUG_ON(bus->state != MDIOBUS_ALLOCATED &&
145 bus->state != MDIOBUS_UNREGISTERED);
146
147 bus->dev.parent = bus->parent;
148 bus->dev.class = &mdio_bus_class;
149 bus->dev.groups = NULL;
Stephen Hemminger036b6682009-02-26 10:19:36 +0000150 dev_set_name(&bus->dev, "%s", bus->id);
Lennert Buytenhek46abc022008-10-08 16:33:40 -0700151
152 err = device_register(&bus->dev);
153 if (err) {
Joe Perches8d242482012-06-09 07:49:07 +0000154 pr_err("mii_bus %s failed to register\n", bus->id);
Lennert Buytenhek46abc022008-10-08 16:33:40 -0700155 return -EINVAL;
156 }
157
Adrian Bunkd1e7fe42008-02-20 02:13:53 +0200158 mutex_init(&bus->mdio_lock);
159
Andy Fleminge1393452005-08-24 18:46:21 -0500160 if (bus->reset)
161 bus->reset(bus);
162
163 for (i = 0; i < PHY_MAX_ADDR; i++) {
Lennert Buytenhek4fd5f812008-08-26 13:08:46 +0200164 if ((bus->phy_mask & (1 << i)) == 0) {
165 struct phy_device *phydev;
Andy Fleminge1393452005-08-24 18:46:21 -0500166
Lennert Buytenhek4fd5f812008-08-26 13:08:46 +0200167 phydev = mdiobus_scan(bus, i);
Krzysztof Halasa161c8d22008-12-25 16:50:41 -0800168 if (IS_ERR(phydev)) {
Lennert Buytenhek4fd5f812008-08-26 13:08:46 +0200169 err = PTR_ERR(phydev);
Krzysztof Halasa161c8d22008-12-25 16:50:41 -0800170 goto error;
171 }
Herbert Valerio Riedel64b1c2b2006-05-10 12:12:57 -0400172 }
Andy Fleminge1393452005-08-24 18:46:21 -0500173 }
174
Krzysztof Halasa161c8d22008-12-25 16:50:41 -0800175 bus->state = MDIOBUS_REGISTERED;
Andy Fleminge1393452005-08-24 18:46:21 -0500176 pr_info("%s: probed\n", bus->name);
Krzysztof Halasa161c8d22008-12-25 16:50:41 -0800177 return 0;
Andy Fleminge1393452005-08-24 18:46:21 -0500178
Krzysztof Halasa161c8d22008-12-25 16:50:41 -0800179error:
180 while (--i >= 0) {
181 if (bus->phy_map[i])
182 device_unregister(&bus->phy_map[i]->dev);
183 }
184 device_del(&bus->dev);
Andy Fleminge1393452005-08-24 18:46:21 -0500185 return err;
186}
187EXPORT_SYMBOL(mdiobus_register);
188
189void mdiobus_unregister(struct mii_bus *bus)
190{
191 int i;
192
Lennert Buytenhek46abc022008-10-08 16:33:40 -0700193 BUG_ON(bus->state != MDIOBUS_REGISTERED);
194 bus->state = MDIOBUS_UNREGISTERED;
195
Lennert Buytenhek3e440172008-11-09 05:34:47 +0100196 device_del(&bus->dev);
Andy Fleminge1393452005-08-24 18:46:21 -0500197 for (i = 0; i < PHY_MAX_ADDR; i++) {
Anton Vorontsov6f4a7f42007-12-04 16:17:33 +0300198 if (bus->phy_map[i])
Andy Fleminge1393452005-08-24 18:46:21 -0500199 device_unregister(&bus->phy_map[i]->dev);
Grant Likely4dea5472009-04-25 12:52:46 +0000200 bus->phy_map[i] = NULL;
Andy Fleminge1393452005-08-24 18:46:21 -0500201 }
202}
203EXPORT_SYMBOL(mdiobus_unregister);
204
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -0700205/**
206 * mdiobus_free - free a struct mii_bus
207 * @bus: mii_bus to free
208 *
Lennert Buytenhek46abc022008-10-08 16:33:40 -0700209 * This function releases the reference to the underlying device
210 * object in the mii_bus. If this is the last reference, the mii_bus
211 * will be freed.
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -0700212 */
213void mdiobus_free(struct mii_bus *bus)
214{
Lennert Buytenhek46abc022008-10-08 16:33:40 -0700215 /*
216 * For compatibility with error handling in drivers.
217 */
218 if (bus->state == MDIOBUS_ALLOCATED) {
219 kfree(bus);
220 return;
221 }
222
223 BUG_ON(bus->state != MDIOBUS_UNREGISTERED);
224 bus->state = MDIOBUS_RELEASED;
225
226 put_device(&bus->dev);
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -0700227}
228EXPORT_SYMBOL(mdiobus_free);
229
Lennert Buytenhek4fd5f812008-08-26 13:08:46 +0200230struct phy_device *mdiobus_scan(struct mii_bus *bus, int addr)
231{
232 struct phy_device *phydev;
233 int err;
234
235 phydev = get_phy_device(bus, addr);
236 if (IS_ERR(phydev) || phydev == NULL)
237 return phydev;
238
Grant Likely4dea5472009-04-25 12:52:46 +0000239 err = phy_device_register(phydev);
Lennert Buytenhek4fd5f812008-08-26 13:08:46 +0200240 if (err) {
Lennert Buytenhek4fd5f812008-08-26 13:08:46 +0200241 phy_device_free(phydev);
Grant Likely4dea5472009-04-25 12:52:46 +0000242 return NULL;
Lennert Buytenhek4fd5f812008-08-26 13:08:46 +0200243 }
244
Lennert Buytenhek4fd5f812008-08-26 13:08:46 +0200245 return phydev;
246}
247EXPORT_SYMBOL(mdiobus_scan);
248
Randy Dunlapb3df0da2007-03-06 02:41:48 -0800249/**
Lennert Buytenhek2e888102008-09-29 17:12:35 +0000250 * mdiobus_read - Convenience function for reading a given MII mgmt register
251 * @bus: the mii_bus struct
252 * @addr: the phy address
253 * @regnum: register number to read
254 *
255 * NOTE: MUST NOT be called from interrupt context,
256 * because the bus read/write functions may wait for an interrupt
257 * to conclude the operation.
258 */
Jason Gunthorpeabf35df2010-03-09 09:17:42 +0000259int mdiobus_read(struct mii_bus *bus, int addr, u32 regnum)
Lennert Buytenhek2e888102008-09-29 17:12:35 +0000260{
261 int retval;
262
263 BUG_ON(in_interrupt());
264
265 mutex_lock(&bus->mdio_lock);
266 retval = bus->read(bus, addr, regnum);
267 mutex_unlock(&bus->mdio_lock);
268
269 return retval;
270}
271EXPORT_SYMBOL(mdiobus_read);
272
273/**
274 * mdiobus_write - Convenience function for writing a given MII mgmt register
275 * @bus: the mii_bus struct
276 * @addr: the phy address
277 * @regnum: register number to write
278 * @val: value to write to @regnum
279 *
280 * NOTE: MUST NOT be called from interrupt context,
281 * because the bus read/write functions may wait for an interrupt
282 * to conclude the operation.
283 */
Jason Gunthorpeabf35df2010-03-09 09:17:42 +0000284int mdiobus_write(struct mii_bus *bus, int addr, u32 regnum, u16 val)
Lennert Buytenhek2e888102008-09-29 17:12:35 +0000285{
286 int err;
287
288 BUG_ON(in_interrupt());
289
290 mutex_lock(&bus->mdio_lock);
291 err = bus->write(bus, addr, regnum, val);
292 mutex_unlock(&bus->mdio_lock);
293
294 return err;
295}
296EXPORT_SYMBOL(mdiobus_write);
297
298/**
Randy Dunlapb3df0da2007-03-06 02:41:48 -0800299 * mdio_bus_match - determine if given PHY driver supports the given PHY device
300 * @dev: target PHY device
301 * @drv: given PHY driver
Andy Fleming00db8182005-07-30 19:31:23 -0400302 *
Randy Dunlapb3df0da2007-03-06 02:41:48 -0800303 * Description: Given a PHY device, and a PHY driver, return 1 if
304 * the driver supports the device. Otherwise, return 0.
Andy Fleming00db8182005-07-30 19:31:23 -0400305 */
306static int mdio_bus_match(struct device *dev, struct device_driver *drv)
307{
308 struct phy_device *phydev = to_phy_device(dev);
309 struct phy_driver *phydrv = to_phy_driver(drv);
310
Kumar Gala5f708dd2007-06-28 13:26:06 -0500311 return ((phydrv->phy_id & phydrv->phy_id_mask) ==
312 (phydev->phy_id & phydrv->phy_id_mask));
Andy Fleming00db8182005-07-30 19:31:23 -0400313}
314
Anton Vorontsov2f5cb432009-12-30 08:23:30 +0000315#ifdef CONFIG_PM
316
Anton Vorontsov3d1e4db2009-02-01 00:53:34 -0800317static bool mdio_bus_phy_may_suspend(struct phy_device *phydev)
318{
319 struct device_driver *drv = phydev->dev.driver;
320 struct phy_driver *phydrv = to_phy_driver(drv);
321 struct net_device *netdev = phydev->attached_dev;
322
323 if (!drv || !phydrv->suspend)
324 return false;
325
326 /* PHY not attached? May suspend. */
327 if (!netdev)
328 return true;
329
330 /*
331 * Don't suspend PHY if the attched netdev parent may wakeup.
332 * The parent may point to a PCI device, as in tg3 driver.
333 */
334 if (netdev->dev.parent && device_may_wakeup(netdev->dev.parent))
335 return false;
336
337 /*
338 * Also don't suspend PHY if the netdev itself may wakeup. This
339 * is the case for devices w/o underlaying pwr. mgmt. aware bus,
340 * e.g. SoC devices.
341 */
342 if (device_may_wakeup(&netdev->dev))
343 return false;
344
345 return true;
346}
347
Anton Vorontsov2f5cb432009-12-30 08:23:30 +0000348static int mdio_bus_suspend(struct device *dev)
Andy Fleming00db8182005-07-30 19:31:23 -0400349{
Anton Vorontsov3d1e4db2009-02-01 00:53:34 -0800350 struct phy_driver *phydrv = to_phy_driver(dev->driver);
Giuseppe Cavallaro0f0ca342008-11-28 16:24:56 -0800351 struct phy_device *phydev = to_phy_device(dev);
Andy Fleming00db8182005-07-30 19:31:23 -0400352
Anton Vorontsov541cd3e2009-12-30 08:23:28 +0000353 /*
354 * We must stop the state machine manually, otherwise it stops out of
355 * control, possibly with the phydev->lock held. Upon resume, netdev
356 * may call phy routines that try to grab the same lock, and that may
357 * lead to a deadlock.
358 */
Simon Guinotfddd9102010-09-13 22:12:01 +0000359 if (phydev->attached_dev && phydev->adjust_link)
Anton Vorontsov541cd3e2009-12-30 08:23:28 +0000360 phy_stop_machine(phydev);
361
Anton Vorontsov3d1e4db2009-02-01 00:53:34 -0800362 if (!mdio_bus_phy_may_suspend(phydev))
363 return 0;
Anton Vorontsov541cd3e2009-12-30 08:23:28 +0000364
Anton Vorontsov3d1e4db2009-02-01 00:53:34 -0800365 return phydrv->suspend(phydev);
Andy Fleming00db8182005-07-30 19:31:23 -0400366}
367
Anton Vorontsov2f5cb432009-12-30 08:23:30 +0000368static int mdio_bus_resume(struct device *dev)
Andy Fleming00db8182005-07-30 19:31:23 -0400369{
Anton Vorontsov3d1e4db2009-02-01 00:53:34 -0800370 struct phy_driver *phydrv = to_phy_driver(dev->driver);
Giuseppe Cavallaro0f0ca342008-11-28 16:24:56 -0800371 struct phy_device *phydev = to_phy_device(dev);
Anton Vorontsov541cd3e2009-12-30 08:23:28 +0000372 int ret;
Andy Fleming00db8182005-07-30 19:31:23 -0400373
Anton Vorontsov3d1e4db2009-02-01 00:53:34 -0800374 if (!mdio_bus_phy_may_suspend(phydev))
Anton Vorontsov541cd3e2009-12-30 08:23:28 +0000375 goto no_resume;
376
377 ret = phydrv->resume(phydev);
378 if (ret < 0)
379 return ret;
380
381no_resume:
Simon Guinotfddd9102010-09-13 22:12:01 +0000382 if (phydev->attached_dev && phydev->adjust_link)
Anton Vorontsov541cd3e2009-12-30 08:23:28 +0000383 phy_start_machine(phydev, NULL);
384
385 return 0;
Andy Fleming00db8182005-07-30 19:31:23 -0400386}
387
Anton Vorontsov2f5cb432009-12-30 08:23:30 +0000388static int mdio_bus_restore(struct device *dev)
389{
390 struct phy_device *phydev = to_phy_device(dev);
391 struct net_device *netdev = phydev->attached_dev;
392 int ret;
393
394 if (!netdev)
395 return 0;
396
397 ret = phy_init_hw(phydev);
398 if (ret < 0)
399 return ret;
400
401 /* The PHY needs to renegotiate. */
402 phydev->link = 0;
403 phydev->state = PHY_UP;
404
405 phy_start_machine(phydev, NULL);
406
407 return 0;
408}
409
410static struct dev_pm_ops mdio_bus_pm_ops = {
411 .suspend = mdio_bus_suspend,
412 .resume = mdio_bus_resume,
413 .freeze = mdio_bus_suspend,
414 .thaw = mdio_bus_resume,
415 .restore = mdio_bus_restore,
416};
417
418#define MDIO_BUS_PM_OPS (&mdio_bus_pm_ops)
419
420#else
421
422#define MDIO_BUS_PM_OPS NULL
423
424#endif /* CONFIG_PM */
425
Andy Fleming00db8182005-07-30 19:31:23 -0400426struct bus_type mdio_bus_type = {
427 .name = "mdio_bus",
428 .match = mdio_bus_match,
Anton Vorontsov2f5cb432009-12-30 08:23:30 +0000429 .pm = MDIO_BUS_PM_OPS,
Andy Fleming00db8182005-07-30 19:31:23 -0400430};
Vitaly Bordug11b0bac2006-08-14 23:00:29 -0700431EXPORT_SYMBOL(mdio_bus_type);
Andy Fleming00db8182005-07-30 19:31:23 -0400432
Jeff Garzik67c4f3f2005-08-11 02:07:25 -0400433int __init mdio_bus_init(void)
Andy Fleming00db8182005-07-30 19:31:23 -0400434{
Lennert Buytenhek46abc022008-10-08 16:33:40 -0700435 int ret;
436
437 ret = class_register(&mdio_bus_class);
438 if (!ret) {
439 ret = bus_register(&mdio_bus_type);
440 if (ret)
441 class_unregister(&mdio_bus_class);
442 }
443
444 return ret;
Andy Fleming00db8182005-07-30 19:31:23 -0400445}
446
Peter Chubbdc85dec2005-09-03 14:05:06 -0700447void mdio_bus_exit(void)
Andy Fleminge1393452005-08-24 18:46:21 -0500448{
Lennert Buytenhek46abc022008-10-08 16:33:40 -0700449 class_unregister(&mdio_bus_class);
Andy Fleminge1393452005-08-24 18:46:21 -0500450 bus_unregister(&mdio_bus_type);
451}