phylib: move to dynamic allocation of struct mii_bus
[deliverable/linux.git] / drivers / net / fs_enet / mii-bitbang.c
CommitLineData
48257c4f
PA
1/*
2 * Combined Ethernet driver for Motorola MPC8xx and MPC82xx.
3 *
9b8ee8e7 4 * Copyright (c) 2003 Intracom S.A.
48257c4f 5 * by Pantelis Antoniou <panto@intracom.gr>
9b8ee8e7
VB
6 *
7 * 2005 (c) MontaVista Software, Inc.
48257c4f
PA
8 * Vitaly Bordug <vbordug@ru.mvista.com>
9 *
9b8ee8e7
VB
10 * This file is licensed under the terms of the GNU General Public License
11 * version 2. This program is licensed "as is" without any warranty of any
48257c4f
PA
12 * kind, whether express or implied.
13 */
14
48257c4f 15#include <linux/module.h>
48257c4f
PA
16#include <linux/ioport.h>
17#include <linux/slab.h>
48257c4f 18#include <linux/init.h>
2b5b3a60 19#include <linux/interrupt.h>
48257c4f
PA
20#include <linux/netdevice.h>
21#include <linux/etherdevice.h>
48257c4f 22#include <linux/mii.h>
5b4b8454 23#include <linux/platform_device.h>
2b5b3a60 24#include <linux/mdio-bitbang.h>
976de6a8 25#include <linux/of_platform.h>
48257c4f
PA
26
27#include "fs_enet.h"
28
976de6a8 29struct bb_info {
2b5b3a60 30 struct mdiobb_ctrl ctrl;
976de6a8
SW
31 __be32 __iomem *dir;
32 __be32 __iomem *dat;
33 u32 mdio_msk;
34 u32 mdc_msk;
976de6a8
SW
35};
36
37/* FIXME: If any other users of GPIO crop up, then these will have to
38 * have some sort of global synchronization to avoid races with other
39 * pins on the same port. The ideal solution would probably be to
40 * bind the ports to a GPIO driver, and have this be a client of it.
41 */
42static inline void bb_set(u32 __iomem *p, u32 m)
48257c4f 43{
976de6a8 44 out_be32(p, in_be32(p) | m);
48257c4f
PA
45}
46
976de6a8 47static inline void bb_clr(u32 __iomem *p, u32 m)
48257c4f 48{
976de6a8 49 out_be32(p, in_be32(p) & ~m);
48257c4f
PA
50}
51
976de6a8 52static inline int bb_read(u32 __iomem *p, u32 m)
48257c4f 53{
976de6a8 54 return (in_be32(p) & m) != 0;
48257c4f
PA
55}
56
2b5b3a60 57static inline void mdio_dir(struct mdiobb_ctrl *ctrl, int dir)
48257c4f 58{
2b5b3a60 59 struct bb_info *bitbang = container_of(ctrl, struct bb_info, ctrl);
48257c4f 60
2b5b3a60
SW
61 if (dir)
62 bb_set(bitbang->dir, bitbang->mdio_msk);
63 else
64 bb_clr(bitbang->dir, bitbang->mdio_msk);
65
66 /* Read back to flush the write. */
67 in_be32(bitbang->dir);
48257c4f
PA
68}
69
2b5b3a60 70static inline int mdio_read(struct mdiobb_ctrl *ctrl)
48257c4f 71{
2b5b3a60 72 struct bb_info *bitbang = container_of(ctrl, struct bb_info, ctrl);
976de6a8 73 return bb_read(bitbang->dat, bitbang->mdio_msk);
48257c4f
PA
74}
75
2b5b3a60 76static inline void mdio(struct mdiobb_ctrl *ctrl, int what)
48257c4f 77{
2b5b3a60
SW
78 struct bb_info *bitbang = container_of(ctrl, struct bb_info, ctrl);
79
48257c4f 80 if (what)
976de6a8 81 bb_set(bitbang->dat, bitbang->mdio_msk);
48257c4f 82 else
976de6a8 83 bb_clr(bitbang->dat, bitbang->mdio_msk);
2b5b3a60
SW
84
85 /* Read back to flush the write. */
86 in_be32(bitbang->dat);
48257c4f
PA
87}
88
2b5b3a60 89static inline void mdc(struct mdiobb_ctrl *ctrl, int what)
48257c4f 90{
2b5b3a60
SW
91 struct bb_info *bitbang = container_of(ctrl, struct bb_info, ctrl);
92
48257c4f 93 if (what)
976de6a8 94 bb_set(bitbang->dat, bitbang->mdc_msk);
48257c4f 95 else
976de6a8 96 bb_clr(bitbang->dat, bitbang->mdc_msk);
48257c4f 97
2b5b3a60
SW
98 /* Read back to flush the write. */
99 in_be32(bitbang->dat);
48257c4f
PA
100}
101
2b5b3a60
SW
102static struct mdiobb_ops bb_ops = {
103 .owner = THIS_MODULE,
104 .set_mdc = mdc,
105 .set_mdio_dir = mdio_dir,
106 .set_mdio_data = mdio,
107 .get_mdio_data = mdio_read,
108};
5b4b8454 109
976de6a8
SW
110static int __devinit fs_mii_bitbang_init(struct mii_bus *bus,
111 struct device_node *np)
48257c4f 112{
976de6a8
SW
113 struct resource res;
114 const u32 *data;
115 int mdio_pin, mdc_pin, len;
116 struct bb_info *bitbang = bus->priv;
48257c4f 117
976de6a8
SW
118 int ret = of_address_to_resource(np, 0, &res);
119 if (ret)
120 return ret;
121
122 if (res.end - res.start < 13)
123 return -ENODEV;
124
125 /* This should really encode the pin number as well, but all
126 * we get is an int, and the odds of multiple bitbang mdio buses
127 * is low enough that it's not worth going too crazy.
128 */
9d9326d3 129 snprintf(bus->id, MII_BUS_ID_SIZE, "%x", res.start);
976de6a8
SW
130
131 data = of_get_property(np, "fsl,mdio-pin", &len);
132 if (!data || len != 4)
133 return -ENODEV;
134 mdio_pin = *data;
135
136 data = of_get_property(np, "fsl,mdc-pin", &len);
137 if (!data || len != 4)
138 return -ENODEV;
139 mdc_pin = *data;
140
141 bitbang->dir = ioremap(res.start, res.end - res.start + 1);
142 if (!bitbang->dir)
143 return -ENOMEM;
144
145 bitbang->dat = bitbang->dir + 4;
146 bitbang->mdio_msk = 1 << (31 - mdio_pin);
147 bitbang->mdc_msk = 1 << (31 - mdc_pin);
976de6a8
SW
148
149 return 0;
150}
151
152static void __devinit add_phy(struct mii_bus *bus, struct device_node *np)
153{
154 const u32 *data;
155 int len, id, irq;
156
157 data = of_get_property(np, "reg", &len);
158 if (!data || len != 4)
159 return;
160
161 id = *data;
162 bus->phy_mask &= ~(1 << id);
5b4b8454 163
976de6a8
SW
164 irq = of_irq_to_resource(np, 0, NULL);
165 if (irq != NO_IRQ)
166 bus->irq[id] = irq;
167}
168
169static int __devinit fs_enet_mdio_probe(struct of_device *ofdev,
170 const struct of_device_id *match)
171{
172 struct device_node *np = NULL;
173 struct mii_bus *new_bus;
174 struct bb_info *bitbang;
175 int ret = -ENOMEM;
176 int i;
177
976de6a8
SW
178 bitbang = kzalloc(sizeof(struct bb_info), GFP_KERNEL);
179 if (!bitbang)
2b5b3a60
SW
180 goto out;
181
182 bitbang->ctrl.ops = &bb_ops;
183
184 new_bus = alloc_mdio_bitbang(&bitbang->ctrl);
185 if (!new_bus)
186 goto out_free_priv;
976de6a8 187
976de6a8 188 new_bus->name = "CPM2 Bitbanged MII",
976de6a8
SW
189
190 ret = fs_mii_bitbang_init(new_bus, ofdev->node);
191 if (ret)
2b5b3a60 192 goto out_free_bus;
976de6a8
SW
193
194 new_bus->phy_mask = ~0;
195 new_bus->irq = kmalloc(sizeof(int) * PHY_MAX_ADDR, GFP_KERNEL);
196 if (!new_bus->irq)
197 goto out_unmap_regs;
198
199 for (i = 0; i < PHY_MAX_ADDR; i++)
200 new_bus->irq[i] = -1;
201
202 while ((np = of_get_next_child(ofdev->node, np)))
203 if (!strcmp(np->type, "ethernet-phy"))
204 add_phy(new_bus, np);
205
18ee49dd 206 new_bus->parent = &ofdev->dev;
976de6a8
SW
207 dev_set_drvdata(&ofdev->dev, new_bus);
208
209 ret = mdiobus_register(new_bus);
210 if (ret)
211 goto out_free_irqs;
212
213 return 0;
214
215out_free_irqs:
216 dev_set_drvdata(&ofdev->dev, NULL);
217 kfree(new_bus->irq);
218out_unmap_regs:
219 iounmap(bitbang->dir);
976de6a8 220out_free_bus:
2b5b3a60 221 free_mdio_bitbang(new_bus);
298cf9be
LB
222out_free_priv:
223 kfree(bitbang);
976de6a8
SW
224out:
225 return ret;
226}
227
228static int fs_enet_mdio_remove(struct of_device *ofdev)
229{
230 struct mii_bus *bus = dev_get_drvdata(&ofdev->dev);
231 struct bb_info *bitbang = bus->priv;
232
233 mdiobus_unregister(bus);
234 dev_set_drvdata(&ofdev->dev, NULL);
235 kfree(bus->irq);
298cf9be 236 free_mdio_bitbang(bus);
976de6a8
SW
237 iounmap(bitbang->dir);
238 kfree(bitbang);
976de6a8
SW
239
240 return 0;
241}
242
243static struct of_device_id fs_enet_mdio_bb_match[] = {
244 {
245 .compatible = "fsl,cpm2-mdio-bitbang",
246 },
247 {},
248};
249
250static struct of_platform_driver fs_enet_bb_mdio_driver = {
251 .name = "fsl-bb-mdio",
252 .match_table = fs_enet_mdio_bb_match,
253 .probe = fs_enet_mdio_probe,
254 .remove = fs_enet_mdio_remove,
255};
256
2b5b3a60 257static int fs_enet_mdio_bb_init(void)
976de6a8
SW
258{
259 return of_register_platform_driver(&fs_enet_bb_mdio_driver);
260}
261
2b5b3a60 262static void fs_enet_mdio_bb_exit(void)
976de6a8
SW
263{
264 of_unregister_platform_driver(&fs_enet_bb_mdio_driver);
265}
266
267module_init(fs_enet_mdio_bb_init);
268module_exit(fs_enet_mdio_bb_exit);
This page took 0.372644 seconds and 5 git commands to generate.