blob: 170eb411ab5d92dceb1b302a4a3eb3a159dd8c69 [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>
David Daneya30e2c12012-06-27 07:33:37 +000028#include <linux/of_device.h>
Andy Fleming00db8182005-07-30 19:31:23 -040029#include <linux/netdevice.h>
30#include <linux/etherdevice.h>
31#include <linux/skbuff.h>
32#include <linux/spinlock.h>
33#include <linux/mm.h>
34#include <linux/module.h>
Andy Fleming00db8182005-07-30 19:31:23 -040035#include <linux/mii.h>
36#include <linux/ethtool.h>
37#include <linux/phy.h>
38
39#include <asm/io.h>
40#include <asm/irq.h>
41#include <asm/uaccess.h>
42
Randy Dunlapb3df0da2007-03-06 02:41:48 -080043/**
Timur Tabieb8a54a72012-01-12 15:23:04 -080044 * mdiobus_alloc_size - allocate a mii_bus structure
Randy Dunlapaf58f1d2012-01-21 09:03:04 +000045 * @size: extra amount of memory to allocate for private storage.
46 * If non-zero, then bus->priv is points to that memory.
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -070047 *
48 * Description: called by a bus driver to allocate an mii_bus
49 * structure to fill in.
50 */
Timur Tabieb8a54a72012-01-12 15:23:04 -080051struct mii_bus *mdiobus_alloc_size(size_t size)
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -070052{
Lennert Buytenhek46abc022008-10-08 16:33:40 -070053 struct mii_bus *bus;
Timur Tabieb8a54a72012-01-12 15:23:04 -080054 size_t aligned_size = ALIGN(sizeof(*bus), NETDEV_ALIGN);
55 size_t alloc_size;
Lennert Buytenhek46abc022008-10-08 16:33:40 -070056
Timur Tabieb8a54a72012-01-12 15:23:04 -080057 /* If we alloc extra space, it should be aligned */
58 if (size)
59 alloc_size = aligned_size + size;
60 else
61 alloc_size = sizeof(*bus);
62
63 bus = kzalloc(alloc_size, GFP_KERNEL);
64 if (bus) {
Lennert Buytenhek46abc022008-10-08 16:33:40 -070065 bus->state = MDIOBUS_ALLOCATED;
Timur Tabieb8a54a72012-01-12 15:23:04 -080066 if (size)
67 bus->priv = (void *)bus + aligned_size;
68 }
Lennert Buytenhek46abc022008-10-08 16:33:40 -070069
70 return bus;
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -070071}
Timur Tabieb8a54a72012-01-12 15:23:04 -080072EXPORT_SYMBOL(mdiobus_alloc_size);
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -070073
74/**
Lennert Buytenhek46abc022008-10-08 16:33:40 -070075 * mdiobus_release - mii_bus device release callback
Randy Dunlap78c36b12008-10-13 18:46:22 -070076 * @d: the target struct device that contains the mii_bus
Lennert Buytenhek46abc022008-10-08 16:33:40 -070077 *
78 * Description: called when the last reference to an mii_bus is
79 * dropped, to free the underlying memory.
80 */
81static void mdiobus_release(struct device *d)
82{
83 struct mii_bus *bus = to_mii_bus(d);
Krzysztof Halasa161c8d22008-12-25 16:50:41 -080084 BUG_ON(bus->state != MDIOBUS_RELEASED &&
85 /* for compatibility with error handling in drivers */
86 bus->state != MDIOBUS_ALLOCATED);
Lennert Buytenhek46abc022008-10-08 16:33:40 -070087 kfree(bus);
88}
89
90static struct class mdio_bus_class = {
91 .name = "mdio_bus",
92 .dev_release = mdiobus_release,
93};
94
Bjørn Morkb943fbb2012-05-11 05:47:01 +000095#if IS_ENABLED(CONFIG_OF_MDIO)
David Daney25106022012-05-02 15:16:37 +000096/* Helper function for of_mdio_find_bus */
97static int of_mdio_bus_match(struct device *dev, void *mdio_bus_np)
98{
99 return dev->of_node == mdio_bus_np;
100}
101/**
102 * of_mdio_find_bus - Given an mii_bus node, find the mii_bus.
Randy Dunlapf41ef2e2012-06-08 14:07:19 +0000103 * @mdio_bus_np: Pointer to the mii_bus.
David Daney25106022012-05-02 15:16:37 +0000104 *
105 * Returns a pointer to the mii_bus, or NULL if none found.
106 *
107 * Because the association of a device_node and mii_bus is made via
108 * of_mdiobus_register(), the mii_bus cannot be found before it is
109 * registered with of_mdiobus_register().
110 *
111 */
112struct mii_bus *of_mdio_find_bus(struct device_node *mdio_bus_np)
113{
114 struct device *d;
115
116 if (!mdio_bus_np)
117 return NULL;
118
119 d = class_find_device(&mdio_bus_class, NULL, mdio_bus_np,
120 of_mdio_bus_match);
121
122 return d ? to_mii_bus(d) : NULL;
123}
124EXPORT_SYMBOL(of_mdio_find_bus);
125#endif
126
Lennert Buytenhek46abc022008-10-08 16:33:40 -0700127/**
Randy Dunlapb3df0da2007-03-06 02:41:48 -0800128 * mdiobus_register - bring up all the PHYs on a given bus and attach them to bus
129 * @bus: target mii_bus
Andy Fleminge1393452005-08-24 18:46:21 -0500130 *
Randy Dunlapb3df0da2007-03-06 02:41:48 -0800131 * Description: Called by a bus driver to bring up all the PHYs
132 * on a given bus, and attach them to the bus.
133 *
134 * Returns 0 on success or < 0 on error.
Andy Fleminge1393452005-08-24 18:46:21 -0500135 */
136int mdiobus_register(struct mii_bus *bus)
137{
Krzysztof Halasa161c8d22008-12-25 16:50:41 -0800138 int i, err;
Andy Fleminge1393452005-08-24 18:46:21 -0500139
Andy Fleminge1393452005-08-24 18:46:21 -0500140 if (NULL == bus || NULL == bus->name ||
141 NULL == bus->read ||
142 NULL == bus->write)
143 return -EINVAL;
144
Lennert Buytenhek46abc022008-10-08 16:33:40 -0700145 BUG_ON(bus->state != MDIOBUS_ALLOCATED &&
146 bus->state != MDIOBUS_UNREGISTERED);
147
148 bus->dev.parent = bus->parent;
149 bus->dev.class = &mdio_bus_class;
150 bus->dev.groups = NULL;
Stephen Hemminger036b6682009-02-26 10:19:36 +0000151 dev_set_name(&bus->dev, "%s", bus->id);
Lennert Buytenhek46abc022008-10-08 16:33:40 -0700152
153 err = device_register(&bus->dev);
154 if (err) {
Joe Perches8d242482012-06-09 07:49:07 +0000155 pr_err("mii_bus %s failed to register\n", bus->id);
Lennert Buytenhek46abc022008-10-08 16:33:40 -0700156 return -EINVAL;
157 }
158
Adrian Bunkd1e7fe42008-02-20 02:13:53 +0200159 mutex_init(&bus->mdio_lock);
160
Andy Fleminge1393452005-08-24 18:46:21 -0500161 if (bus->reset)
162 bus->reset(bus);
163
164 for (i = 0; i < PHY_MAX_ADDR; i++) {
Lennert Buytenhek4fd5f812008-08-26 13:08:46 +0200165 if ((bus->phy_mask & (1 << i)) == 0) {
166 struct phy_device *phydev;
Andy Fleminge1393452005-08-24 18:46:21 -0500167
Lennert Buytenhek4fd5f812008-08-26 13:08:46 +0200168 phydev = mdiobus_scan(bus, i);
Krzysztof Halasa161c8d22008-12-25 16:50:41 -0800169 if (IS_ERR(phydev)) {
Lennert Buytenhek4fd5f812008-08-26 13:08:46 +0200170 err = PTR_ERR(phydev);
Krzysztof Halasa161c8d22008-12-25 16:50:41 -0800171 goto error;
172 }
Herbert Valerio Riedel64b1c2b2006-05-10 12:12:57 -0400173 }
Andy Fleminge1393452005-08-24 18:46:21 -0500174 }
175
Krzysztof Halasa161c8d22008-12-25 16:50:41 -0800176 bus->state = MDIOBUS_REGISTERED;
Andy Fleminge1393452005-08-24 18:46:21 -0500177 pr_info("%s: probed\n", bus->name);
Krzysztof Halasa161c8d22008-12-25 16:50:41 -0800178 return 0;
Andy Fleminge1393452005-08-24 18:46:21 -0500179
Krzysztof Halasa161c8d22008-12-25 16:50:41 -0800180error:
181 while (--i >= 0) {
182 if (bus->phy_map[i])
183 device_unregister(&bus->phy_map[i]->dev);
184 }
185 device_del(&bus->dev);
Andy Fleminge1393452005-08-24 18:46:21 -0500186 return err;
187}
188EXPORT_SYMBOL(mdiobus_register);
189
190void mdiobus_unregister(struct mii_bus *bus)
191{
192 int i;
193
Lennert Buytenhek46abc022008-10-08 16:33:40 -0700194 BUG_ON(bus->state != MDIOBUS_REGISTERED);
195 bus->state = MDIOBUS_UNREGISTERED;
196
Lennert Buytenhek3e440172008-11-09 05:34:47 +0100197 device_del(&bus->dev);
Andy Fleminge1393452005-08-24 18:46:21 -0500198 for (i = 0; i < PHY_MAX_ADDR; i++) {
Anton Vorontsov6f4a7f42007-12-04 16:17:33 +0300199 if (bus->phy_map[i])
Andy Fleminge1393452005-08-24 18:46:21 -0500200 device_unregister(&bus->phy_map[i]->dev);
Grant Likely4dea5472009-04-25 12:52:46 +0000201 bus->phy_map[i] = NULL;
Andy Fleminge1393452005-08-24 18:46:21 -0500202 }
203}
204EXPORT_SYMBOL(mdiobus_unregister);
205
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -0700206/**
207 * mdiobus_free - free a struct mii_bus
208 * @bus: mii_bus to free
209 *
Lennert Buytenhek46abc022008-10-08 16:33:40 -0700210 * This function releases the reference to the underlying device
211 * object in the mii_bus. If this is the last reference, the mii_bus
212 * will be freed.
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -0700213 */
214void mdiobus_free(struct mii_bus *bus)
215{
Lennert Buytenhek46abc022008-10-08 16:33:40 -0700216 /*
217 * For compatibility with error handling in drivers.
218 */
219 if (bus->state == MDIOBUS_ALLOCATED) {
220 kfree(bus);
221 return;
222 }
223
224 BUG_ON(bus->state != MDIOBUS_UNREGISTERED);
225 bus->state = MDIOBUS_RELEASED;
226
227 put_device(&bus->dev);
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -0700228}
229EXPORT_SYMBOL(mdiobus_free);
230
Lennert Buytenhek4fd5f812008-08-26 13:08:46 +0200231struct phy_device *mdiobus_scan(struct mii_bus *bus, int addr)
232{
233 struct phy_device *phydev;
234 int err;
235
David Daneyac28b9f2012-06-27 07:33:35 +0000236 phydev = get_phy_device(bus, addr, false);
Lennert Buytenhek4fd5f812008-08-26 13:08:46 +0200237 if (IS_ERR(phydev) || phydev == NULL)
238 return phydev;
239
Grant Likely4dea5472009-04-25 12:52:46 +0000240 err = phy_device_register(phydev);
Lennert Buytenhek4fd5f812008-08-26 13:08:46 +0200241 if (err) {
Lennert Buytenhek4fd5f812008-08-26 13:08:46 +0200242 phy_device_free(phydev);
Grant Likely4dea5472009-04-25 12:52:46 +0000243 return NULL;
Lennert Buytenhek4fd5f812008-08-26 13:08:46 +0200244 }
245
Lennert Buytenhek4fd5f812008-08-26 13:08:46 +0200246 return phydev;
247}
248EXPORT_SYMBOL(mdiobus_scan);
249
Randy Dunlapb3df0da2007-03-06 02:41:48 -0800250/**
Lennert Buytenhek2e888102008-09-29 17:12:35 +0000251 * mdiobus_read - Convenience function for reading a given MII mgmt register
252 * @bus: the mii_bus struct
253 * @addr: the phy address
254 * @regnum: register number to read
255 *
256 * NOTE: MUST NOT be called from interrupt context,
257 * because the bus read/write functions may wait for an interrupt
258 * to conclude the operation.
259 */
Jason Gunthorpeabf35df2010-03-09 09:17:42 +0000260int mdiobus_read(struct mii_bus *bus, int addr, u32 regnum)
Lennert Buytenhek2e888102008-09-29 17:12:35 +0000261{
262 int retval;
263
264 BUG_ON(in_interrupt());
265
266 mutex_lock(&bus->mdio_lock);
267 retval = bus->read(bus, addr, regnum);
268 mutex_unlock(&bus->mdio_lock);
269
270 return retval;
271}
272EXPORT_SYMBOL(mdiobus_read);
273
274/**
275 * mdiobus_write - Convenience function for writing a given MII mgmt register
276 * @bus: the mii_bus struct
277 * @addr: the phy address
278 * @regnum: register number to write
279 * @val: value to write to @regnum
280 *
281 * NOTE: MUST NOT be called from interrupt context,
282 * because the bus read/write functions may wait for an interrupt
283 * to conclude the operation.
284 */
Jason Gunthorpeabf35df2010-03-09 09:17:42 +0000285int mdiobus_write(struct mii_bus *bus, int addr, u32 regnum, u16 val)
Lennert Buytenhek2e888102008-09-29 17:12:35 +0000286{
287 int err;
288
289 BUG_ON(in_interrupt());
290
291 mutex_lock(&bus->mdio_lock);
292 err = bus->write(bus, addr, regnum, val);
293 mutex_unlock(&bus->mdio_lock);
294
295 return err;
296}
297EXPORT_SYMBOL(mdiobus_write);
298
299/**
Randy Dunlapb3df0da2007-03-06 02:41:48 -0800300 * mdio_bus_match - determine if given PHY driver supports the given PHY device
301 * @dev: target PHY device
302 * @drv: given PHY driver
Andy Fleming00db8182005-07-30 19:31:23 -0400303 *
Randy Dunlapb3df0da2007-03-06 02:41:48 -0800304 * Description: Given a PHY device, and a PHY driver, return 1 if
305 * the driver supports the device. Otherwise, return 0.
Andy Fleming00db8182005-07-30 19:31:23 -0400306 */
307static int mdio_bus_match(struct device *dev, struct device_driver *drv)
308{
309 struct phy_device *phydev = to_phy_device(dev);
310 struct phy_driver *phydrv = to_phy_driver(drv);
311
David Daneya30e2c12012-06-27 07:33:37 +0000312 if (of_driver_match_device(dev, drv))
313 return 1;
314
315 if (phydrv->match_phy_device)
316 return phydrv->match_phy_device(phydev);
317
Kumar Gala5f708dd2007-06-28 13:26:06 -0500318 return ((phydrv->phy_id & phydrv->phy_id_mask) ==
319 (phydev->phy_id & phydrv->phy_id_mask));
Andy Fleming00db8182005-07-30 19:31:23 -0400320}
321
Anton Vorontsov2f5cb432009-12-30 08:23:30 +0000322#ifdef CONFIG_PM
323
Anton Vorontsov3d1e4db2009-02-01 00:53:34 -0800324static bool mdio_bus_phy_may_suspend(struct phy_device *phydev)
325{
326 struct device_driver *drv = phydev->dev.driver;
327 struct phy_driver *phydrv = to_phy_driver(drv);
328 struct net_device *netdev = phydev->attached_dev;
329
330 if (!drv || !phydrv->suspend)
331 return false;
332
333 /* PHY not attached? May suspend. */
334 if (!netdev)
335 return true;
336
337 /*
338 * Don't suspend PHY if the attched netdev parent may wakeup.
339 * The parent may point to a PCI device, as in tg3 driver.
340 */
341 if (netdev->dev.parent && device_may_wakeup(netdev->dev.parent))
342 return false;
343
344 /*
345 * Also don't suspend PHY if the netdev itself may wakeup. This
346 * is the case for devices w/o underlaying pwr. mgmt. aware bus,
347 * e.g. SoC devices.
348 */
349 if (device_may_wakeup(&netdev->dev))
350 return false;
351
352 return true;
353}
354
Anton Vorontsov2f5cb432009-12-30 08:23:30 +0000355static int mdio_bus_suspend(struct device *dev)
Andy Fleming00db8182005-07-30 19:31:23 -0400356{
Anton Vorontsov3d1e4db2009-02-01 00:53:34 -0800357 struct phy_driver *phydrv = to_phy_driver(dev->driver);
Giuseppe Cavallaro0f0ca342008-11-28 16:24:56 -0800358 struct phy_device *phydev = to_phy_device(dev);
Andy Fleming00db8182005-07-30 19:31:23 -0400359
Anton Vorontsov541cd3e2009-12-30 08:23:28 +0000360 /*
361 * We must stop the state machine manually, otherwise it stops out of
362 * control, possibly with the phydev->lock held. Upon resume, netdev
363 * may call phy routines that try to grab the same lock, and that may
364 * lead to a deadlock.
365 */
Simon Guinotfddd9102010-09-13 22:12:01 +0000366 if (phydev->attached_dev && phydev->adjust_link)
Anton Vorontsov541cd3e2009-12-30 08:23:28 +0000367 phy_stop_machine(phydev);
368
Anton Vorontsov3d1e4db2009-02-01 00:53:34 -0800369 if (!mdio_bus_phy_may_suspend(phydev))
370 return 0;
Anton Vorontsov541cd3e2009-12-30 08:23:28 +0000371
Anton Vorontsov3d1e4db2009-02-01 00:53:34 -0800372 return phydrv->suspend(phydev);
Andy Fleming00db8182005-07-30 19:31:23 -0400373}
374
Anton Vorontsov2f5cb432009-12-30 08:23:30 +0000375static int mdio_bus_resume(struct device *dev)
Andy Fleming00db8182005-07-30 19:31:23 -0400376{
Anton Vorontsov3d1e4db2009-02-01 00:53:34 -0800377 struct phy_driver *phydrv = to_phy_driver(dev->driver);
Giuseppe Cavallaro0f0ca342008-11-28 16:24:56 -0800378 struct phy_device *phydev = to_phy_device(dev);
Anton Vorontsov541cd3e2009-12-30 08:23:28 +0000379 int ret;
Andy Fleming00db8182005-07-30 19:31:23 -0400380
Anton Vorontsov3d1e4db2009-02-01 00:53:34 -0800381 if (!mdio_bus_phy_may_suspend(phydev))
Anton Vorontsov541cd3e2009-12-30 08:23:28 +0000382 goto no_resume;
383
384 ret = phydrv->resume(phydev);
385 if (ret < 0)
386 return ret;
387
388no_resume:
Simon Guinotfddd9102010-09-13 22:12:01 +0000389 if (phydev->attached_dev && phydev->adjust_link)
Anton Vorontsov541cd3e2009-12-30 08:23:28 +0000390 phy_start_machine(phydev, NULL);
391
392 return 0;
Andy Fleming00db8182005-07-30 19:31:23 -0400393}
394
Anton Vorontsov2f5cb432009-12-30 08:23:30 +0000395static int mdio_bus_restore(struct device *dev)
396{
397 struct phy_device *phydev = to_phy_device(dev);
398 struct net_device *netdev = phydev->attached_dev;
399 int ret;
400
401 if (!netdev)
402 return 0;
403
404 ret = phy_init_hw(phydev);
405 if (ret < 0)
406 return ret;
407
408 /* The PHY needs to renegotiate. */
409 phydev->link = 0;
410 phydev->state = PHY_UP;
411
412 phy_start_machine(phydev, NULL);
413
414 return 0;
415}
416
417static struct dev_pm_ops mdio_bus_pm_ops = {
418 .suspend = mdio_bus_suspend,
419 .resume = mdio_bus_resume,
420 .freeze = mdio_bus_suspend,
421 .thaw = mdio_bus_resume,
422 .restore = mdio_bus_restore,
423};
424
425#define MDIO_BUS_PM_OPS (&mdio_bus_pm_ops)
426
427#else
428
429#define MDIO_BUS_PM_OPS NULL
430
431#endif /* CONFIG_PM */
432
Andy Fleming00db8182005-07-30 19:31:23 -0400433struct bus_type mdio_bus_type = {
434 .name = "mdio_bus",
435 .match = mdio_bus_match,
Anton Vorontsov2f5cb432009-12-30 08:23:30 +0000436 .pm = MDIO_BUS_PM_OPS,
Andy Fleming00db8182005-07-30 19:31:23 -0400437};
Vitaly Bordug11b0bac2006-08-14 23:00:29 -0700438EXPORT_SYMBOL(mdio_bus_type);
Andy Fleming00db8182005-07-30 19:31:23 -0400439
Jeff Garzik67c4f3f2005-08-11 02:07:25 -0400440int __init mdio_bus_init(void)
Andy Fleming00db8182005-07-30 19:31:23 -0400441{
Lennert Buytenhek46abc022008-10-08 16:33:40 -0700442 int ret;
443
444 ret = class_register(&mdio_bus_class);
445 if (!ret) {
446 ret = bus_register(&mdio_bus_type);
447 if (ret)
448 class_unregister(&mdio_bus_class);
449 }
450
451 return ret;
Andy Fleming00db8182005-07-30 19:31:23 -0400452}
453
Peter Chubbdc85dec2005-09-03 14:05:06 -0700454void mdio_bus_exit(void)
Andy Fleminge1393452005-08-24 18:46:21 -0500455{
Lennert Buytenhek46abc022008-10-08 16:33:40 -0700456 class_unregister(&mdio_bus_class);
Andy Fleminge1393452005-08-24 18:46:21 -0500457 bus_unregister(&mdio_bus_type);
458}