blob: 1b236bdf6b928d0dafa81c64ddcd3ac679c32e6c [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 */
16#include <linux/config.h>
17#include <linux/kernel.h>
18#include <linux/sched.h>
19#include <linux/string.h>
20#include <linux/errno.h>
21#include <linux/unistd.h>
22#include <linux/slab.h>
23#include <linux/interrupt.h>
24#include <linux/init.h>
25#include <linux/delay.h>
26#include <linux/netdevice.h>
27#include <linux/etherdevice.h>
28#include <linux/skbuff.h>
29#include <linux/spinlock.h>
30#include <linux/mm.h>
31#include <linux/module.h>
Andy Fleming00db8182005-07-30 19:31:23 -040032#include <linux/mii.h>
33#include <linux/ethtool.h>
34#include <linux/phy.h>
35
36#include <asm/io.h>
37#include <asm/irq.h>
38#include <asm/uaccess.h>
39
Andy Fleminge1393452005-08-24 18:46:21 -050040/* mdiobus_register
41 *
42 * description: Called by a bus driver to bring up all the PHYs
43 * on a given bus, and attach them to the bus
44 */
45int mdiobus_register(struct mii_bus *bus)
46{
47 int i;
48 int err = 0;
49
50 spin_lock_init(&bus->mdio_lock);
51
52 if (NULL == bus || NULL == bus->name ||
53 NULL == bus->read ||
54 NULL == bus->write)
55 return -EINVAL;
56
57 if (bus->reset)
58 bus->reset(bus);
59
60 for (i = 0; i < PHY_MAX_ADDR; i++) {
61 struct phy_device *phydev;
62
Herbert Valerio Riedel64b1c2b2006-05-10 12:12:57 -040063 if (bus->phy_mask & (1 << i)) {
64 bus->phy_map[i] = NULL;
Matt Porterf8964242005-11-02 16:13:06 -070065 continue;
Herbert Valerio Riedel64b1c2b2006-05-10 12:12:57 -040066 }
Matt Porterf8964242005-11-02 16:13:06 -070067
Andy Fleminge1393452005-08-24 18:46:21 -050068 phydev = get_phy_device(bus, i);
69
70 if (IS_ERR(phydev))
71 return PTR_ERR(phydev);
72
73 /* There's a PHY at this address
74 * We need to set:
75 * 1) IRQ
76 * 2) bus_id
77 * 3) parent
78 * 4) bus
79 * 5) mii_bus
80 * And, we need to register it */
81 if (phydev) {
82 phydev->irq = bus->irq[i];
83
84 phydev->dev.parent = bus->dev;
85 phydev->dev.bus = &mdio_bus_type;
Kumar Galaa4d00f12006-01-11 11:27:33 -080086 snprintf(phydev->dev.bus_id, BUS_ID_SIZE, PHY_ID_FMT, bus->id, i);
Andy Fleminge1393452005-08-24 18:46:21 -050087
88 phydev->bus = bus;
89
90 err = device_register(&phydev->dev);
91
92 if (err)
93 printk(KERN_ERR "phy %d failed to register\n",
94 i);
95 }
96
97 bus->phy_map[i] = phydev;
98 }
99
100 pr_info("%s: probed\n", bus->name);
101
102 return err;
103}
104EXPORT_SYMBOL(mdiobus_register);
105
106void mdiobus_unregister(struct mii_bus *bus)
107{
108 int i;
109
110 for (i = 0; i < PHY_MAX_ADDR; i++) {
111 if (bus->phy_map[i]) {
112 device_unregister(&bus->phy_map[i]->dev);
113 kfree(bus->phy_map[i]);
114 }
115 }
116}
117EXPORT_SYMBOL(mdiobus_unregister);
118
Andy Fleming00db8182005-07-30 19:31:23 -0400119/* mdio_bus_match
120 *
121 * description: Given a PHY device, and a PHY driver, return 1 if
122 * the driver supports the device. Otherwise, return 0
123 */
124static int mdio_bus_match(struct device *dev, struct device_driver *drv)
125{
126 struct phy_device *phydev = to_phy_device(dev);
127 struct phy_driver *phydrv = to_phy_driver(drv);
128
129 return (phydrv->phy_id == (phydev->phy_id & phydrv->phy_id_mask));
130}
131
132/* Suspend and resume. Copied from platform_suspend and
133 * platform_resume
134 */
Pavel Machek829ca9a2005-09-03 15:56:56 -0700135static int mdio_bus_suspend(struct device * dev, pm_message_t state)
Andy Fleming00db8182005-07-30 19:31:23 -0400136{
137 int ret = 0;
138 struct device_driver *drv = dev->driver;
139
Russell King9480e302005-10-28 09:52:56 -0700140 if (drv && drv->suspend)
141 ret = drv->suspend(dev, state);
142
Andy Fleming00db8182005-07-30 19:31:23 -0400143 return ret;
144}
145
146static int mdio_bus_resume(struct device * dev)
147{
148 int ret = 0;
149 struct device_driver *drv = dev->driver;
150
Russell King9480e302005-10-28 09:52:56 -0700151 if (drv && drv->resume)
152 ret = drv->resume(dev);
153
Andy Fleming00db8182005-07-30 19:31:23 -0400154 return ret;
155}
156
157struct bus_type mdio_bus_type = {
158 .name = "mdio_bus",
159 .match = mdio_bus_match,
160 .suspend = mdio_bus_suspend,
161 .resume = mdio_bus_resume,
162};
163
Jeff Garzik67c4f3f2005-08-11 02:07:25 -0400164int __init mdio_bus_init(void)
Andy Fleming00db8182005-07-30 19:31:23 -0400165{
166 return bus_register(&mdio_bus_type);
167}
168
Peter Chubbdc85dec2005-09-03 14:05:06 -0700169void mdio_bus_exit(void)
Andy Fleminge1393452005-08-24 18:46:21 -0500170{
171 bus_unregister(&mdio_bus_type);
172}