]>
Commit | Line | Data |
---|---|---|
00db8189 AF |
1 | /* |
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 | */ | |
00db8189 | 16 | #include <linux/kernel.h> |
00db8189 AF |
17 | #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> | |
00db8189 AF |
30 | #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 | ||
b3df0da8 RD |
38 | /** |
39 | * mdiobus_register - bring up all the PHYs on a given bus and attach them to bus | |
40 | * @bus: target mii_bus | |
e1393456 | 41 | * |
b3df0da8 RD |
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 | * | |
45 | * Returns 0 on success or < 0 on error. | |
e1393456 AF |
46 | */ |
47 | int mdiobus_register(struct mii_bus *bus) | |
48 | { | |
49 | int i; | |
50 | int err = 0; | |
51 | ||
35b5f6b1 | 52 | mutex_init(&bus->mdio_lock); |
e1393456 AF |
53 | |
54 | if (NULL == bus || NULL == bus->name || | |
55 | NULL == bus->read || | |
56 | NULL == bus->write) | |
57 | return -EINVAL; | |
58 | ||
59 | if (bus->reset) | |
60 | bus->reset(bus); | |
61 | ||
62 | for (i = 0; i < PHY_MAX_ADDR; i++) { | |
63 | struct phy_device *phydev; | |
64 | ||
64b1c2b4 HVR |
65 | if (bus->phy_mask & (1 << i)) { |
66 | bus->phy_map[i] = NULL; | |
f896424c | 67 | continue; |
64b1c2b4 | 68 | } |
f896424c | 69 | |
e1393456 AF |
70 | 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; | |
a4d00f17 | 88 | snprintf(phydev->dev.bus_id, BUS_ID_SIZE, PHY_ID_FMT, bus->id, i); |
e1393456 AF |
89 | |
90 | phydev->bus = bus; | |
91 | ||
92 | err = device_register(&phydev->dev); | |
93 | ||
6f4a7f41 | 94 | if (err) { |
e1393456 AF |
95 | printk(KERN_ERR "phy %d failed to register\n", |
96 | i); | |
6f4a7f41 AV |
97 | phy_device_free(phydev); |
98 | phydev = NULL; | |
99 | } | |
e1393456 AF |
100 | } |
101 | ||
102 | bus->phy_map[i] = phydev; | |
103 | } | |
104 | ||
105 | pr_info("%s: probed\n", bus->name); | |
106 | ||
107 | return err; | |
108 | } | |
109 | EXPORT_SYMBOL(mdiobus_register); | |
110 | ||
111 | void mdiobus_unregister(struct mii_bus *bus) | |
112 | { | |
113 | int i; | |
114 | ||
115 | for (i = 0; i < PHY_MAX_ADDR; i++) { | |
6f4a7f41 | 116 | if (bus->phy_map[i]) |
e1393456 | 117 | device_unregister(&bus->phy_map[i]->dev); |
e1393456 AF |
118 | } |
119 | } | |
120 | EXPORT_SYMBOL(mdiobus_unregister); | |
121 | ||
b3df0da8 RD |
122 | /** |
123 | * mdio_bus_match - determine if given PHY driver supports the given PHY device | |
124 | * @dev: target PHY device | |
125 | * @drv: given PHY driver | |
00db8189 | 126 | * |
b3df0da8 RD |
127 | * Description: Given a PHY device, and a PHY driver, return 1 if |
128 | * the driver supports the device. Otherwise, return 0. | |
00db8189 AF |
129 | */ |
130 | static int mdio_bus_match(struct device *dev, struct device_driver *drv) | |
131 | { | |
132 | struct phy_device *phydev = to_phy_device(dev); | |
133 | struct phy_driver *phydrv = to_phy_driver(drv); | |
134 | ||
5f708dd9 KG |
135 | return ((phydrv->phy_id & phydrv->phy_id_mask) == |
136 | (phydev->phy_id & phydrv->phy_id_mask)); | |
00db8189 AF |
137 | } |
138 | ||
139 | /* Suspend and resume. Copied from platform_suspend and | |
140 | * platform_resume | |
141 | */ | |
829ca9a3 | 142 | static int mdio_bus_suspend(struct device * dev, pm_message_t state) |
00db8189 AF |
143 | { |
144 | int ret = 0; | |
145 | struct device_driver *drv = dev->driver; | |
146 | ||
9480e307 RK |
147 | if (drv && drv->suspend) |
148 | ret = drv->suspend(dev, state); | |
149 | ||
00db8189 AF |
150 | return ret; |
151 | } | |
152 | ||
153 | static int mdio_bus_resume(struct device * dev) | |
154 | { | |
155 | int ret = 0; | |
156 | struct device_driver *drv = dev->driver; | |
157 | ||
9480e307 RK |
158 | if (drv && drv->resume) |
159 | ret = drv->resume(dev); | |
160 | ||
00db8189 AF |
161 | return ret; |
162 | } | |
163 | ||
164 | struct bus_type mdio_bus_type = { | |
165 | .name = "mdio_bus", | |
166 | .match = mdio_bus_match, | |
167 | .suspend = mdio_bus_suspend, | |
168 | .resume = mdio_bus_resume, | |
169 | }; | |
11b0bacd | 170 | EXPORT_SYMBOL(mdio_bus_type); |
00db8189 | 171 | |
67c4f3fa | 172 | int __init mdio_bus_init(void) |
00db8189 AF |
173 | { |
174 | return bus_register(&mdio_bus_type); | |
175 | } | |
176 | ||
dc85dec6 | 177 | void mdio_bus_exit(void) |
e1393456 AF |
178 | { |
179 | bus_unregister(&mdio_bus_type); | |
180 | } |