blob: 94e0b7ed76f16ff8c2c2a7f43aaf341c4cace12d [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 */
Andy Fleming00db8182005-07-30 19:31:23 -040016#include <linux/kernel.h>
Andy Fleming00db8182005-07-30 19:31:23 -040017#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/netdevice.h>
25#include <linux/etherdevice.h>
26#include <linux/skbuff.h>
27#include <linux/spinlock.h>
28#include <linux/mm.h>
29#include <linux/module.h>
Andy Fleming00db8182005-07-30 19:31:23 -040030#include <linux/mii.h>
31#include <linux/ethtool.h>
32#include <linux/phy.h>
33
34#include <asm/io.h>
35#include <asm/irq.h>
36#include <asm/uaccess.h>
37
Randy Dunlapb3df0da2007-03-06 02:41:48 -080038/**
39 * mdiobus_register - bring up all the PHYs on a given bus and attach them to bus
40 * @bus: target mii_bus
Andy Fleminge1393452005-08-24 18:46:21 -050041 *
Randy Dunlapb3df0da2007-03-06 02:41:48 -080042 * Description: Called by a bus driver to bring up all the PHYs
43 * on a given bus, and attach them to the bus.
44 *
45 * Returns 0 on success or < 0 on error.
Andy Fleminge1393452005-08-24 18:46:21 -050046 */
47int mdiobus_register(struct mii_bus *bus)
48{
49 int i;
50 int err = 0;
51
Andy Fleminge1393452005-08-24 18:46:21 -050052 if (NULL == bus || NULL == bus->name ||
53 NULL == bus->read ||
54 NULL == bus->write)
55 return -EINVAL;
56
Adrian Bunkd1e7fe42008-02-20 02:13:53 +020057 mutex_init(&bus->mdio_lock);
58
Andy Fleminge1393452005-08-24 18:46:21 -050059 if (bus->reset)
60 bus->reset(bus);
61
62 for (i = 0; i < PHY_MAX_ADDR; i++) {
63 struct phy_device *phydev;
64
Herbert Valerio Riedel64b1c2b2006-05-10 12:12:57 -040065 if (bus->phy_mask & (1 << i)) {
66 bus->phy_map[i] = NULL;
Matt Porterf8964242005-11-02 16:13:06 -070067 continue;
Herbert Valerio Riedel64b1c2b2006-05-10 12:12:57 -040068 }
Matt Porterf8964242005-11-02 16:13:06 -070069
Andy Fleminge1393452005-08-24 18:46:21 -050070 phydev = get_phy_device(bus, i);
71
72 if (IS_ERR(phydev))
73 return PTR_ERR(phydev);
74
75 /* There's a PHY at this address
76 * We need to set:
77 * 1) IRQ
78 * 2) bus_id
79 * 3) parent
80 * 4) bus
81 * 5) mii_bus
82 * And, we need to register it */
83 if (phydev) {
84 phydev->irq = bus->irq[i];
85
86 phydev->dev.parent = bus->dev;
87 phydev->dev.bus = &mdio_bus_type;
Kumar Galaa4d00f12006-01-11 11:27:33 -080088 snprintf(phydev->dev.bus_id, BUS_ID_SIZE, PHY_ID_FMT, bus->id, i);
Andy Fleminge1393452005-08-24 18:46:21 -050089
90 phydev->bus = bus;
91
Andy Flemingf62220d2008-04-18 17:29:54 -050092 /* Run all of the fixups for this PHY */
93 phy_scan_fixups(phydev);
94
Andy Fleminge1393452005-08-24 18:46:21 -050095 err = device_register(&phydev->dev);
96
Anton Vorontsov6f4a7f42007-12-04 16:17:33 +030097 if (err) {
Andy Fleminge1393452005-08-24 18:46:21 -050098 printk(KERN_ERR "phy %d failed to register\n",
99 i);
Anton Vorontsov6f4a7f42007-12-04 16:17:33 +0300100 phy_device_free(phydev);
101 phydev = NULL;
102 }
Andy Fleminge1393452005-08-24 18:46:21 -0500103 }
104
105 bus->phy_map[i] = phydev;
106 }
107
108 pr_info("%s: probed\n", bus->name);
109
110 return err;
111}
112EXPORT_SYMBOL(mdiobus_register);
113
114void mdiobus_unregister(struct mii_bus *bus)
115{
116 int i;
117
118 for (i = 0; i < PHY_MAX_ADDR; i++) {
Anton Vorontsov6f4a7f42007-12-04 16:17:33 +0300119 if (bus->phy_map[i])
Andy Fleminge1393452005-08-24 18:46:21 -0500120 device_unregister(&bus->phy_map[i]->dev);
Andy Fleminge1393452005-08-24 18:46:21 -0500121 }
122}
123EXPORT_SYMBOL(mdiobus_unregister);
124
Randy Dunlapb3df0da2007-03-06 02:41:48 -0800125/**
126 * mdio_bus_match - determine if given PHY driver supports the given PHY device
127 * @dev: target PHY device
128 * @drv: given PHY driver
Andy Fleming00db8182005-07-30 19:31:23 -0400129 *
Randy Dunlapb3df0da2007-03-06 02:41:48 -0800130 * Description: Given a PHY device, and a PHY driver, return 1 if
131 * the driver supports the device. Otherwise, return 0.
Andy Fleming00db8182005-07-30 19:31:23 -0400132 */
133static int mdio_bus_match(struct device *dev, struct device_driver *drv)
134{
135 struct phy_device *phydev = to_phy_device(dev);
136 struct phy_driver *phydrv = to_phy_driver(drv);
137
Kumar Gala5f708dd2007-06-28 13:26:06 -0500138 return ((phydrv->phy_id & phydrv->phy_id_mask) ==
139 (phydev->phy_id & phydrv->phy_id_mask));
Andy Fleming00db8182005-07-30 19:31:23 -0400140}
141
142/* Suspend and resume. Copied from platform_suspend and
143 * platform_resume
144 */
Pavel Machek829ca9a2005-09-03 15:56:56 -0700145static int mdio_bus_suspend(struct device * dev, pm_message_t state)
Andy Fleming00db8182005-07-30 19:31:23 -0400146{
147 int ret = 0;
148 struct device_driver *drv = dev->driver;
149
Russell King9480e302005-10-28 09:52:56 -0700150 if (drv && drv->suspend)
151 ret = drv->suspend(dev, state);
152
Andy Fleming00db8182005-07-30 19:31:23 -0400153 return ret;
154}
155
156static int mdio_bus_resume(struct device * dev)
157{
158 int ret = 0;
159 struct device_driver *drv = dev->driver;
160
Russell King9480e302005-10-28 09:52:56 -0700161 if (drv && drv->resume)
162 ret = drv->resume(dev);
163
Andy Fleming00db8182005-07-30 19:31:23 -0400164 return ret;
165}
166
167struct bus_type mdio_bus_type = {
168 .name = "mdio_bus",
169 .match = mdio_bus_match,
170 .suspend = mdio_bus_suspend,
171 .resume = mdio_bus_resume,
172};
Vitaly Bordug11b0bac2006-08-14 23:00:29 -0700173EXPORT_SYMBOL(mdio_bus_type);
Andy Fleming00db8182005-07-30 19:31:23 -0400174
Jeff Garzik67c4f3f2005-08-11 02:07:25 -0400175int __init mdio_bus_init(void)
Andy Fleming00db8182005-07-30 19:31:23 -0400176{
177 return bus_register(&mdio_bus_type);
178}
179
Peter Chubbdc85dec2005-09-03 14:05:06 -0700180void mdio_bus_exit(void)
Andy Fleminge1393452005-08-24 18:46:21 -0500181{
182 bus_unregister(&mdio_bus_type);
183}