AMCC PPC 460SX redwood SoC platform initial framework
[deliverable/linux.git] / drivers / net / phy / mdio-gpio.c
CommitLineData
a5edeccb 1/*
f004f3ea
PZ
2 * GPIO based MDIO bitbang driver.
3 * Supports OpenFirmware.
a5edeccb
LP
4 *
5 * Copyright (c) 2008 CSE Semaphore Belgium.
6 * by Laurent Pinchart <laurentp@cse-semaphore.com>
7 *
f004f3ea
PZ
8 * Copyright (C) 2008, Paulius Zaleckas <paulius.zaleckas@teltonika.lt>
9 *
a5edeccb
LP
10 * Based on earlier work by
11 *
12 * Copyright (c) 2003 Intracom S.A.
13 * by Pantelis Antoniou <panto@intracom.gr>
14 *
15 * 2005 (c) MontaVista Software, Inc.
16 * Vitaly Bordug <vbordug@ru.mvista.com>
17 *
18 * This file is licensed under the terms of the GNU General Public License
19 * version 2. This program is licensed "as is" without any warranty of any
20 * kind, whether express or implied.
21 */
22
23#include <linux/module.h>
24#include <linux/slab.h>
25#include <linux/init.h>
26#include <linux/interrupt.h>
f004f3ea
PZ
27#include <linux/platform_device.h>
28#include <linux/gpio.h>
29#include <linux/mdio-gpio.h>
30
31#ifdef CONFIG_OF_GPIO
a5edeccb
LP
32#include <linux/of_gpio.h>
33#include <linux/of_platform.h>
f004f3ea 34#endif
a5edeccb
LP
35
36struct mdio_gpio_info {
37 struct mdiobb_ctrl ctrl;
38 int mdc, mdio;
39};
40
41static void mdio_dir(struct mdiobb_ctrl *ctrl, int dir)
42{
43 struct mdio_gpio_info *bitbang =
44 container_of(ctrl, struct mdio_gpio_info, ctrl);
45
46 if (dir)
47 gpio_direction_output(bitbang->mdio, 1);
48 else
49 gpio_direction_input(bitbang->mdio);
50}
51
f004f3ea 52static int mdio_get(struct mdiobb_ctrl *ctrl)
a5edeccb
LP
53{
54 struct mdio_gpio_info *bitbang =
55 container_of(ctrl, struct mdio_gpio_info, ctrl);
56
57 return gpio_get_value(bitbang->mdio);
58}
59
f004f3ea 60static void mdio_set(struct mdiobb_ctrl *ctrl, int what)
a5edeccb
LP
61{
62 struct mdio_gpio_info *bitbang =
63 container_of(ctrl, struct mdio_gpio_info, ctrl);
64
65 gpio_set_value(bitbang->mdio, what);
66}
67
f004f3ea 68static void mdc_set(struct mdiobb_ctrl *ctrl, int what)
a5edeccb
LP
69{
70 struct mdio_gpio_info *bitbang =
71 container_of(ctrl, struct mdio_gpio_info, ctrl);
72
73 gpio_set_value(bitbang->mdc, what);
74}
75
76static struct mdiobb_ops mdio_gpio_ops = {
77 .owner = THIS_MODULE,
f004f3ea 78 .set_mdc = mdc_set,
a5edeccb 79 .set_mdio_dir = mdio_dir,
f004f3ea
PZ
80 .set_mdio_data = mdio_set,
81 .get_mdio_data = mdio_get,
a5edeccb
LP
82};
83
f004f3ea
PZ
84static int __devinit mdio_gpio_bus_init(struct device *dev,
85 struct mdio_gpio_platform_data *pdata,
86 int bus_id)
a5edeccb 87{
a5edeccb
LP
88 struct mii_bus *new_bus;
89 struct mdio_gpio_info *bitbang;
90 int ret = -ENOMEM;
91 int i;
92
f004f3ea 93 bitbang = kzalloc(sizeof(*bitbang), GFP_KERNEL);
a5edeccb
LP
94 if (!bitbang)
95 goto out;
96
97 bitbang->ctrl.ops = &mdio_gpio_ops;
f004f3ea
PZ
98 bitbang->mdc = pdata->mdc;
99 bitbang->mdio = pdata->mdio;
a5edeccb
LP
100
101 new_bus = alloc_mdio_bitbang(&bitbang->ctrl);
102 if (!new_bus)
298cf9be 103 goto out_free_bitbang;
a5edeccb 104
f004f3ea 105 new_bus->name = "GPIO Bitbanged MDIO",
a5edeccb 106
f004f3ea 107 ret = -ENODEV;
a5edeccb 108
f004f3ea
PZ
109 new_bus->phy_mask = pdata->phy_mask;
110 new_bus->irq = pdata->irqs;
111 new_bus->parent = dev;
112
113 if (new_bus->phy_mask == ~0)
a5edeccb
LP
114 goto out_free_bus;
115
116 for (i = 0; i < PHY_MAX_ADDR; i++)
f004f3ea
PZ
117 if (!new_bus->irq[i])
118 new_bus->irq[i] = PHY_POLL;
a5edeccb 119
f004f3ea
PZ
120 snprintf(new_bus->id, MII_BUS_ID_SIZE, "%x", bus_id);
121
122 if (gpio_request(bitbang->mdc, "mdc"))
123 goto out_free_bus;
a5edeccb 124
f004f3ea
PZ
125 if (gpio_request(bitbang->mdio, "mdio"))
126 goto out_free_mdc;
127
128 dev_set_drvdata(dev, new_bus);
a5edeccb
LP
129
130 ret = mdiobus_register(new_bus);
131 if (ret)
f004f3ea 132 goto out_free_all;
a5edeccb
LP
133
134 return 0;
135
f004f3ea
PZ
136out_free_all:
137 dev_set_drvdata(dev, NULL);
138 gpio_free(bitbang->mdio);
139out_free_mdc:
140 gpio_free(bitbang->mdc);
a5edeccb 141out_free_bus:
a5edeccb 142 free_mdio_bitbang(new_bus);
298cf9be
LB
143out_free_bitbang:
144 kfree(bitbang);
a5edeccb
LP
145out:
146 return ret;
147}
148
f004f3ea 149static void __devexit mdio_gpio_bus_destroy(struct device *dev)
a5edeccb 150{
f004f3ea 151 struct mii_bus *bus = dev_get_drvdata(dev);
a5edeccb
LP
152 struct mdio_gpio_info *bitbang = bus->priv;
153
154 mdiobus_unregister(bus);
155 free_mdio_bitbang(bus);
f004f3ea
PZ
156 dev_set_drvdata(dev, NULL);
157 gpio_free(bitbang->mdc);
158 gpio_free(bitbang->mdio);
a5edeccb 159 kfree(bitbang);
f004f3ea
PZ
160}
161
162static int __devinit mdio_gpio_probe(struct platform_device *pdev)
163{
164 struct mdio_gpio_platform_data *pdata = pdev->dev.platform_data;
165
166 if (!pdata)
167 return -ENODEV;
168
169 return mdio_gpio_bus_init(&pdev->dev, pdata, pdev->id);
170}
171
172static int __devexit mdio_gpio_remove(struct platform_device *pdev)
173{
174 mdio_gpio_bus_destroy(&pdev->dev);
175
176 return 0;
177}
178
179#ifdef CONFIG_OF_GPIO
180static void __devinit add_phy(struct mdio_gpio_platform_data *pdata,
181 struct device_node *np)
182{
183 const u32 *data;
184 int len, id, irq;
185
186 data = of_get_property(np, "reg", &len);
187 if (!data || len != 4)
188 return;
189
190 id = *data;
191 pdata->phy_mask &= ~(1 << id);
192
193 irq = of_irq_to_resource(np, 0, NULL);
194 if (irq)
195 pdata->irqs[id] = irq;
196}
197
198static int __devinit mdio_ofgpio_probe(struct of_device *ofdev,
199 const struct of_device_id *match)
200{
201 struct device_node *np = NULL;
202 struct mdio_gpio_platform_data *pdata;
203
204 pdata = kzalloc(sizeof(*pdata), GFP_KERNEL);
205 if (!pdata)
206 return -ENOMEM;
207
208 pdata->mdc = of_get_gpio(ofdev->node, 0);
209 pdata->mdio = of_get_gpio(ofdev->node, 1);
210
211 if (pdata->mdc < 0 || pdata->mdio < 0)
212 goto out_free;
213
214 while ((np = of_get_next_child(ofdev->node, np)))
215 if (!strcmp(np->type, "ethernet-phy"))
216 add_phy(pdata, np);
217
218 return mdio_gpio_bus_init(&ofdev->dev, pdata, pdata->mdc);
219
220out_free:
221 kfree(pdata);
222 return -ENODEV;
223}
224
225static int __devexit mdio_ofgpio_remove(struct of_device *ofdev)
226{
227 mdio_gpio_bus_destroy(&ofdev->dev);
228 kfree(ofdev->dev.platform_data);
a5edeccb
LP
229
230 return 0;
231}
232
233static struct of_device_id mdio_ofgpio_match[] = {
234 {
235 .compatible = "virtual,mdio-gpio",
236 },
237 {},
238};
239
240static struct of_platform_driver mdio_ofgpio_driver = {
241 .name = "mdio-gpio",
242 .match_table = mdio_ofgpio_match,
243 .probe = mdio_ofgpio_probe,
f004f3ea 244 .remove = __devexit_p(mdio_ofgpio_remove),
a5edeccb
LP
245};
246
f004f3ea 247static inline int __init mdio_ofgpio_init(void)
a5edeccb
LP
248{
249 return of_register_platform_driver(&mdio_ofgpio_driver);
250}
251
f004f3ea 252static inline void __exit mdio_ofgpio_exit(void)
a5edeccb
LP
253{
254 of_unregister_platform_driver(&mdio_ofgpio_driver);
255}
f004f3ea
PZ
256#else
257static inline int __init mdio_ofgpio_init(void) { return 0; }
258static inline void __exit mdio_ofgpio_exit(void) { }
259#endif /* CONFIG_OF_GPIO */
260
261static struct platform_driver mdio_gpio_driver = {
262 .probe = mdio_gpio_probe,
263 .remove = __devexit_p(mdio_gpio_remove),
264 .driver = {
265 .name = "mdio-gpio",
266 .owner = THIS_MODULE,
267 },
268};
269
270static int __init mdio_gpio_init(void)
271{
272 int ret;
273
274 ret = mdio_ofgpio_init();
275 if (ret)
276 return ret;
277
278 ret = platform_driver_register(&mdio_gpio_driver);
279 if (ret)
280 mdio_ofgpio_exit();
281
282 return ret;
283}
284module_init(mdio_gpio_init);
285
286static void __exit mdio_gpio_exit(void)
287{
288 platform_driver_unregister(&mdio_gpio_driver);
289 mdio_ofgpio_exit();
290}
291module_exit(mdio_gpio_exit);
a5edeccb 292
f004f3ea
PZ
293MODULE_ALIAS("platform:mdio-gpio");
294MODULE_AUTHOR("Laurent Pinchart, Paulius Zaleckas");
295MODULE_LICENSE("GPL");
296MODULE_DESCRIPTION("Generic driver for MDIO bus emulation using GPIO");
This page took 0.308635 seconds and 5 git commands to generate.