blob: 289fc267edf30eb5094d24f416ca644c9494d401 [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/**
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -070039 * mdiobus_alloc - allocate a mii_bus structure
40 *
41 * Description: called by a bus driver to allocate an mii_bus
42 * structure to fill in.
43 */
44struct mii_bus *mdiobus_alloc(void)
45{
Lennert Buytenhek46abc022008-10-08 16:33:40 -070046 struct mii_bus *bus;
47
48 bus = kzalloc(sizeof(*bus), GFP_KERNEL);
49 if (bus != NULL)
50 bus->state = MDIOBUS_ALLOCATED;
51
52 return bus;
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -070053}
54EXPORT_SYMBOL(mdiobus_alloc);
55
56/**
Lennert Buytenhek46abc022008-10-08 16:33:40 -070057 * mdiobus_release - mii_bus device release callback
Randy Dunlap78c36b12008-10-13 18:46:22 -070058 * @d: the target struct device that contains the mii_bus
Lennert Buytenhek46abc022008-10-08 16:33:40 -070059 *
60 * Description: called when the last reference to an mii_bus is
61 * dropped, to free the underlying memory.
62 */
63static void mdiobus_release(struct device *d)
64{
65 struct mii_bus *bus = to_mii_bus(d);
66 BUG_ON(bus->state != MDIOBUS_RELEASED);
67 kfree(bus);
68}
69
70static struct class mdio_bus_class = {
71 .name = "mdio_bus",
72 .dev_release = mdiobus_release,
73};
74
75/**
Randy Dunlapb3df0da2007-03-06 02:41:48 -080076 * mdiobus_register - bring up all the PHYs on a given bus and attach them to bus
77 * @bus: target mii_bus
Andy Fleminge1393452005-08-24 18:46:21 -050078 *
Randy Dunlapb3df0da2007-03-06 02:41:48 -080079 * Description: Called by a bus driver to bring up all the PHYs
80 * on a given bus, and attach them to the bus.
81 *
82 * Returns 0 on success or < 0 on error.
Andy Fleminge1393452005-08-24 18:46:21 -050083 */
84int mdiobus_register(struct mii_bus *bus)
85{
86 int i;
87 int err = 0;
88
Andy Fleminge1393452005-08-24 18:46:21 -050089 if (NULL == bus || NULL == bus->name ||
90 NULL == bus->read ||
91 NULL == bus->write)
92 return -EINVAL;
93
Lennert Buytenhek46abc022008-10-08 16:33:40 -070094 BUG_ON(bus->state != MDIOBUS_ALLOCATED &&
95 bus->state != MDIOBUS_UNREGISTERED);
96
97 bus->dev.parent = bus->parent;
98 bus->dev.class = &mdio_bus_class;
99 bus->dev.groups = NULL;
100 memcpy(bus->dev.bus_id, bus->id, MII_BUS_ID_SIZE);
101
102 err = device_register(&bus->dev);
103 if (err) {
104 printk(KERN_ERR "mii_bus %s failed to register\n", bus->id);
105 return -EINVAL;
106 }
107
Adrian Bunkd1e7fe42008-02-20 02:13:53 +0200108 mutex_init(&bus->mdio_lock);
109
Andy Fleminge1393452005-08-24 18:46:21 -0500110 if (bus->reset)
111 bus->reset(bus);
112
113 for (i = 0; i < PHY_MAX_ADDR; i++) {
Lennert Buytenhek4fd5f812008-08-26 13:08:46 +0200114 bus->phy_map[i] = NULL;
115 if ((bus->phy_mask & (1 << i)) == 0) {
116 struct phy_device *phydev;
Andy Fleminge1393452005-08-24 18:46:21 -0500117
Lennert Buytenhek4fd5f812008-08-26 13:08:46 +0200118 phydev = mdiobus_scan(bus, i);
119 if (IS_ERR(phydev))
120 err = PTR_ERR(phydev);
Herbert Valerio Riedel64b1c2b2006-05-10 12:12:57 -0400121 }
Andy Fleminge1393452005-08-24 18:46:21 -0500122 }
123
Krzysztof Halasae8e57522008-12-17 00:24:13 -0800124 if (!err)
125 bus->state = MDIOBUS_REGISTERED;
126
Andy Fleminge1393452005-08-24 18:46:21 -0500127 pr_info("%s: probed\n", bus->name);
128
129 return err;
130}
131EXPORT_SYMBOL(mdiobus_register);
132
133void mdiobus_unregister(struct mii_bus *bus)
134{
135 int i;
136
Lennert Buytenhek46abc022008-10-08 16:33:40 -0700137 BUG_ON(bus->state != MDIOBUS_REGISTERED);
138 bus->state = MDIOBUS_UNREGISTERED;
139
Lennert Buytenhek3e44017b52008-11-09 05:34:47 +0100140 device_del(&bus->dev);
Andy Fleminge1393452005-08-24 18:46:21 -0500141 for (i = 0; i < PHY_MAX_ADDR; i++) {
Anton Vorontsov6f4a7f42007-12-04 16:17:33 +0300142 if (bus->phy_map[i])
Andy Fleminge1393452005-08-24 18:46:21 -0500143 device_unregister(&bus->phy_map[i]->dev);
Andy Fleminge1393452005-08-24 18:46:21 -0500144 }
145}
146EXPORT_SYMBOL(mdiobus_unregister);
147
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -0700148/**
149 * mdiobus_free - free a struct mii_bus
150 * @bus: mii_bus to free
151 *
Lennert Buytenhek46abc022008-10-08 16:33:40 -0700152 * This function releases the reference to the underlying device
153 * object in the mii_bus. If this is the last reference, the mii_bus
154 * will be freed.
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -0700155 */
156void mdiobus_free(struct mii_bus *bus)
157{
Lennert Buytenhek46abc022008-10-08 16:33:40 -0700158 /*
159 * For compatibility with error handling in drivers.
160 */
161 if (bus->state == MDIOBUS_ALLOCATED) {
162 kfree(bus);
163 return;
164 }
165
166 BUG_ON(bus->state != MDIOBUS_UNREGISTERED);
167 bus->state = MDIOBUS_RELEASED;
168
169 put_device(&bus->dev);
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -0700170}
171EXPORT_SYMBOL(mdiobus_free);
172
Lennert Buytenhek4fd5f812008-08-26 13:08:46 +0200173struct phy_device *mdiobus_scan(struct mii_bus *bus, int addr)
174{
175 struct phy_device *phydev;
176 int err;
177
178 phydev = get_phy_device(bus, addr);
179 if (IS_ERR(phydev) || phydev == NULL)
180 return phydev;
181
182 /* There's a PHY at this address
183 * We need to set:
184 * 1) IRQ
185 * 2) bus_id
186 * 3) parent
187 * 4) bus
188 * 5) mii_bus
189 * And, we need to register it */
190
191 phydev->irq = bus->irq != NULL ? bus->irq[addr] : PHY_POLL;
192
Lennert Buytenhek18ee49d2008-10-01 15:41:33 +0000193 phydev->dev.parent = bus->parent;
Lennert Buytenhek4fd5f812008-08-26 13:08:46 +0200194 phydev->dev.bus = &mdio_bus_type;
195 snprintf(phydev->dev.bus_id, BUS_ID_SIZE, PHY_ID_FMT, bus->id, addr);
196
197 phydev->bus = bus;
198
199 /* Run all of the fixups for this PHY */
200 phy_scan_fixups(phydev);
201
202 err = device_register(&phydev->dev);
203 if (err) {
204 printk(KERN_ERR "phy %d failed to register\n", addr);
205 phy_device_free(phydev);
206 phydev = NULL;
207 }
208
209 bus->phy_map[addr] = phydev;
210
211 return phydev;
212}
213EXPORT_SYMBOL(mdiobus_scan);
214
Randy Dunlapb3df0da2007-03-06 02:41:48 -0800215/**
Lennert Buytenhek2e888102008-09-29 17:12:35 +0000216 * mdiobus_read - Convenience function for reading a given MII mgmt register
217 * @bus: the mii_bus struct
218 * @addr: the phy address
219 * @regnum: register number to read
220 *
221 * NOTE: MUST NOT be called from interrupt context,
222 * because the bus read/write functions may wait for an interrupt
223 * to conclude the operation.
224 */
225int mdiobus_read(struct mii_bus *bus, int addr, u16 regnum)
226{
227 int retval;
228
229 BUG_ON(in_interrupt());
230
231 mutex_lock(&bus->mdio_lock);
232 retval = bus->read(bus, addr, regnum);
233 mutex_unlock(&bus->mdio_lock);
234
235 return retval;
236}
237EXPORT_SYMBOL(mdiobus_read);
238
239/**
240 * mdiobus_write - Convenience function for writing a given MII mgmt register
241 * @bus: the mii_bus struct
242 * @addr: the phy address
243 * @regnum: register number to write
244 * @val: value to write to @regnum
245 *
246 * NOTE: MUST NOT be called from interrupt context,
247 * because the bus read/write functions may wait for an interrupt
248 * to conclude the operation.
249 */
250int mdiobus_write(struct mii_bus *bus, int addr, u16 regnum, u16 val)
251{
252 int err;
253
254 BUG_ON(in_interrupt());
255
256 mutex_lock(&bus->mdio_lock);
257 err = bus->write(bus, addr, regnum, val);
258 mutex_unlock(&bus->mdio_lock);
259
260 return err;
261}
262EXPORT_SYMBOL(mdiobus_write);
263
264/**
Randy Dunlapb3df0da2007-03-06 02:41:48 -0800265 * mdio_bus_match - determine if given PHY driver supports the given PHY device
266 * @dev: target PHY device
267 * @drv: given PHY driver
Andy Fleming00db8182005-07-30 19:31:23 -0400268 *
Randy Dunlapb3df0da2007-03-06 02:41:48 -0800269 * Description: Given a PHY device, and a PHY driver, return 1 if
270 * the driver supports the device. Otherwise, return 0.
Andy Fleming00db8182005-07-30 19:31:23 -0400271 */
272static int mdio_bus_match(struct device *dev, struct device_driver *drv)
273{
274 struct phy_device *phydev = to_phy_device(dev);
275 struct phy_driver *phydrv = to_phy_driver(drv);
276
Kumar Gala5f708dd2007-06-28 13:26:06 -0500277 return ((phydrv->phy_id & phydrv->phy_id_mask) ==
278 (phydev->phy_id & phydrv->phy_id_mask));
Andy Fleming00db8182005-07-30 19:31:23 -0400279}
280
281/* Suspend and resume. Copied from platform_suspend and
282 * platform_resume
283 */
Pavel Machek829ca9a2005-09-03 15:56:56 -0700284static int mdio_bus_suspend(struct device * dev, pm_message_t state)
Andy Fleming00db8182005-07-30 19:31:23 -0400285{
286 int ret = 0;
287 struct device_driver *drv = dev->driver;
288
Russell King9480e302005-10-28 09:52:56 -0700289 if (drv && drv->suspend)
290 ret = drv->suspend(dev, state);
291
Andy Fleming00db8182005-07-30 19:31:23 -0400292 return ret;
293}
294
295static int mdio_bus_resume(struct device * dev)
296{
297 int ret = 0;
298 struct device_driver *drv = dev->driver;
299
Russell King9480e302005-10-28 09:52:56 -0700300 if (drv && drv->resume)
301 ret = drv->resume(dev);
302
Andy Fleming00db8182005-07-30 19:31:23 -0400303 return ret;
304}
305
306struct bus_type mdio_bus_type = {
307 .name = "mdio_bus",
308 .match = mdio_bus_match,
309 .suspend = mdio_bus_suspend,
310 .resume = mdio_bus_resume,
311};
Vitaly Bordug11b0bac2006-08-14 23:00:29 -0700312EXPORT_SYMBOL(mdio_bus_type);
Andy Fleming00db8182005-07-30 19:31:23 -0400313
Jeff Garzik67c4f3f2005-08-11 02:07:25 -0400314int __init mdio_bus_init(void)
Andy Fleming00db8182005-07-30 19:31:23 -0400315{
Lennert Buytenhek46abc022008-10-08 16:33:40 -0700316 int ret;
317
318 ret = class_register(&mdio_bus_class);
319 if (!ret) {
320 ret = bus_register(&mdio_bus_type);
321 if (ret)
322 class_unregister(&mdio_bus_class);
323 }
324
325 return ret;
Andy Fleming00db8182005-07-30 19:31:23 -0400326}
327
Peter Chubbdc85dec2005-09-03 14:05:06 -0700328void mdio_bus_exit(void)
Andy Fleminge1393452005-08-24 18:46:21 -0500329{
Lennert Buytenhek46abc022008-10-08 16:33:40 -0700330 class_unregister(&mdio_bus_class);
Andy Fleminge1393452005-08-24 18:46:21 -0500331 bus_unregister(&mdio_bus_type);
332}