blob: d206691f15d89cc23c144e618da25c68ea380d02 [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{
46 return kzalloc(sizeof(struct mii_bus), GFP_KERNEL);
47}
48EXPORT_SYMBOL(mdiobus_alloc);
49
50/**
Randy Dunlapb3df0da2007-03-06 02:41:48 -080051 * mdiobus_register - bring up all the PHYs on a given bus and attach them to bus
52 * @bus: target mii_bus
Andy Fleminge1393452005-08-24 18:46:21 -050053 *
Randy Dunlapb3df0da2007-03-06 02:41:48 -080054 * Description: Called by a bus driver to bring up all the PHYs
55 * on a given bus, and attach them to the bus.
56 *
57 * Returns 0 on success or < 0 on error.
Andy Fleminge1393452005-08-24 18:46:21 -050058 */
59int mdiobus_register(struct mii_bus *bus)
60{
61 int i;
62 int err = 0;
63
Andy Fleminge1393452005-08-24 18:46:21 -050064 if (NULL == bus || NULL == bus->name ||
65 NULL == bus->read ||
66 NULL == bus->write)
67 return -EINVAL;
68
Adrian Bunkd1e7fe42008-02-20 02:13:53 +020069 mutex_init(&bus->mdio_lock);
70
Andy Fleminge1393452005-08-24 18:46:21 -050071 if (bus->reset)
72 bus->reset(bus);
73
74 for (i = 0; i < PHY_MAX_ADDR; i++) {
Lennert Buytenhek4fd5f812008-08-26 13:08:46 +020075 bus->phy_map[i] = NULL;
76 if ((bus->phy_mask & (1 << i)) == 0) {
77 struct phy_device *phydev;
Andy Fleminge1393452005-08-24 18:46:21 -050078
Lennert Buytenhek4fd5f812008-08-26 13:08:46 +020079 phydev = mdiobus_scan(bus, i);
80 if (IS_ERR(phydev))
81 err = PTR_ERR(phydev);
Herbert Valerio Riedel64b1c2b2006-05-10 12:12:57 -040082 }
Andy Fleminge1393452005-08-24 18:46:21 -050083 }
84
85 pr_info("%s: probed\n", bus->name);
86
87 return err;
88}
89EXPORT_SYMBOL(mdiobus_register);
90
91void mdiobus_unregister(struct mii_bus *bus)
92{
93 int i;
94
95 for (i = 0; i < PHY_MAX_ADDR; i++) {
Anton Vorontsov6f4a7f42007-12-04 16:17:33 +030096 if (bus->phy_map[i])
Andy Fleminge1393452005-08-24 18:46:21 -050097 device_unregister(&bus->phy_map[i]->dev);
Andy Fleminge1393452005-08-24 18:46:21 -050098 }
99}
100EXPORT_SYMBOL(mdiobus_unregister);
101
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -0700102/**
103 * mdiobus_free - free a struct mii_bus
104 * @bus: mii_bus to free
105 *
106 * This function frees the mii_bus.
107 */
108void mdiobus_free(struct mii_bus *bus)
109{
110 kfree(bus);
111}
112EXPORT_SYMBOL(mdiobus_free);
113
Lennert Buytenhek4fd5f812008-08-26 13:08:46 +0200114struct phy_device *mdiobus_scan(struct mii_bus *bus, int addr)
115{
116 struct phy_device *phydev;
117 int err;
118
119 phydev = get_phy_device(bus, addr);
120 if (IS_ERR(phydev) || phydev == NULL)
121 return phydev;
122
123 /* There's a PHY at this address
124 * We need to set:
125 * 1) IRQ
126 * 2) bus_id
127 * 3) parent
128 * 4) bus
129 * 5) mii_bus
130 * And, we need to register it */
131
132 phydev->irq = bus->irq != NULL ? bus->irq[addr] : PHY_POLL;
133
Lennert Buytenhek18ee49d2008-10-01 15:41:33 +0000134 phydev->dev.parent = bus->parent;
Lennert Buytenhek4fd5f812008-08-26 13:08:46 +0200135 phydev->dev.bus = &mdio_bus_type;
136 snprintf(phydev->dev.bus_id, BUS_ID_SIZE, PHY_ID_FMT, bus->id, addr);
137
138 phydev->bus = bus;
139
140 /* Run all of the fixups for this PHY */
141 phy_scan_fixups(phydev);
142
143 err = device_register(&phydev->dev);
144 if (err) {
145 printk(KERN_ERR "phy %d failed to register\n", addr);
146 phy_device_free(phydev);
147 phydev = NULL;
148 }
149
150 bus->phy_map[addr] = phydev;
151
152 return phydev;
153}
154EXPORT_SYMBOL(mdiobus_scan);
155
Randy Dunlapb3df0da2007-03-06 02:41:48 -0800156/**
157 * mdio_bus_match - determine if given PHY driver supports the given PHY device
158 * @dev: target PHY device
159 * @drv: given PHY driver
Andy Fleming00db8182005-07-30 19:31:23 -0400160 *
Randy Dunlapb3df0da2007-03-06 02:41:48 -0800161 * Description: Given a PHY device, and a PHY driver, return 1 if
162 * the driver supports the device. Otherwise, return 0.
Andy Fleming00db8182005-07-30 19:31:23 -0400163 */
164static int mdio_bus_match(struct device *dev, struct device_driver *drv)
165{
166 struct phy_device *phydev = to_phy_device(dev);
167 struct phy_driver *phydrv = to_phy_driver(drv);
168
Kumar Gala5f708dd2007-06-28 13:26:06 -0500169 return ((phydrv->phy_id & phydrv->phy_id_mask) ==
170 (phydev->phy_id & phydrv->phy_id_mask));
Andy Fleming00db8182005-07-30 19:31:23 -0400171}
172
173/* Suspend and resume. Copied from platform_suspend and
174 * platform_resume
175 */
Pavel Machek829ca9a2005-09-03 15:56:56 -0700176static int mdio_bus_suspend(struct device * dev, pm_message_t state)
Andy Fleming00db8182005-07-30 19:31:23 -0400177{
178 int ret = 0;
179 struct device_driver *drv = dev->driver;
180
Russell King9480e302005-10-28 09:52:56 -0700181 if (drv && drv->suspend)
182 ret = drv->suspend(dev, state);
183
Andy Fleming00db8182005-07-30 19:31:23 -0400184 return ret;
185}
186
187static int mdio_bus_resume(struct device * dev)
188{
189 int ret = 0;
190 struct device_driver *drv = dev->driver;
191
Russell King9480e302005-10-28 09:52:56 -0700192 if (drv && drv->resume)
193 ret = drv->resume(dev);
194
Andy Fleming00db8182005-07-30 19:31:23 -0400195 return ret;
196}
197
198struct bus_type mdio_bus_type = {
199 .name = "mdio_bus",
200 .match = mdio_bus_match,
201 .suspend = mdio_bus_suspend,
202 .resume = mdio_bus_resume,
203};
Vitaly Bordug11b0bac2006-08-14 23:00:29 -0700204EXPORT_SYMBOL(mdio_bus_type);
Andy Fleming00db8182005-07-30 19:31:23 -0400205
Jeff Garzik67c4f3f2005-08-11 02:07:25 -0400206int __init mdio_bus_init(void)
Andy Fleming00db8182005-07-30 19:31:23 -0400207{
208 return bus_register(&mdio_bus_type);
209}
210
Peter Chubbdc85dec2005-09-03 14:05:06 -0700211void mdio_bus_exit(void)
Andy Fleminge1393452005-08-24 18:46:21 -0500212{
213 bus_unregister(&mdio_bus_type);
214}