Merge git://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux-2.6
[wrt350n-kernel.git] / drivers / net / fs_enet / mii-fec.c
bloba89cf15090b8840ed3d79f986ab2c30e1b6bbedf
1 /*
2 * Combined Ethernet driver for Motorola MPC8xx and MPC82xx.
4 * Copyright (c) 2003 Intracom S.A.
5 * by Pantelis Antoniou <panto@intracom.gr>
7 * 2005 (c) MontaVista Software, Inc.
8 * Vitaly Bordug <vbordug@ru.mvista.com>
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
12 * kind, whether express or implied.
15 #include <linux/module.h>
16 #include <linux/types.h>
17 #include <linux/kernel.h>
18 #include <linux/string.h>
19 #include <linux/ptrace.h>
20 #include <linux/errno.h>
21 #include <linux/ioport.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/mii.h>
31 #include <linux/ethtool.h>
32 #include <linux/bitops.h>
33 #include <linux/platform_device.h>
35 #include <asm/pgtable.h>
36 #include <asm/irq.h>
37 #include <asm/uaccess.h>
39 #ifdef CONFIG_PPC_CPM_NEW_BINDING
40 #include <asm/of_platform.h>
41 #endif
43 #include "fs_enet.h"
44 #include "fec.h"
46 /* Make MII read/write commands for the FEC.
48 #define mk_mii_read(REG) (0x60020000 | ((REG & 0x1f) << 18))
49 #define mk_mii_write(REG, VAL) (0x50020000 | ((REG & 0x1f) << 18) | (VAL & 0xffff))
50 #define mk_mii_end 0
52 #define FEC_MII_LOOPS 10000
54 #ifndef CONFIG_PPC_CPM_NEW_BINDING
55 static int match_has_phy (struct device *dev, void* data)
57 struct platform_device* pdev = container_of(dev, struct platform_device, dev);
58 struct fs_platform_info* fpi;
59 if(strcmp(pdev->name, (char*)data))
61 return 0;
64 fpi = pdev->dev.platform_data;
65 if((fpi)&&(fpi->has_phy))
66 return 1;
67 return 0;
70 static int fs_mii_fec_init(struct fec_info* fec, struct fs_mii_fec_platform_info *fmpi)
72 struct resource *r;
73 fec_t __iomem *fecp;
74 char* name = "fsl-cpm-fec";
76 /* we need fec in order to be useful */
77 struct platform_device *fec_pdev =
78 container_of(bus_find_device(&platform_bus_type, NULL, name, match_has_phy),
79 struct platform_device, dev);
81 if(fec_pdev == NULL) {
82 printk(KERN_ERR"Unable to find PHY for %s", name);
83 return -ENODEV;
86 r = platform_get_resource_byname(fec_pdev, IORESOURCE_MEM, "regs");
88 fec->fecp = fecp = ioremap(r->start,sizeof(fec_t));
89 fec->mii_speed = fmpi->mii_speed;
91 setbits32(&fecp->fec_r_cntrl, FEC_RCNTRL_MII_MODE); /* MII enable */
92 setbits32(&fecp->fec_ecntrl, FEC_ECNTRL_PINMUX | FEC_ECNTRL_ETHER_EN);
93 out_be32(&fecp->fec_ievent, FEC_ENET_MII);
94 out_be32(&fecp->fec_mii_speed, fec->mii_speed);
96 return 0;
98 #endif
100 static int fs_enet_fec_mii_read(struct mii_bus *bus , int phy_id, int location)
102 struct fec_info* fec = bus->priv;
103 fec_t __iomem *fecp = fec->fecp;
104 int i, ret = -1;
106 if ((in_be32(&fecp->fec_r_cntrl) & FEC_RCNTRL_MII_MODE) == 0)
107 BUG();
109 /* Add PHY address to register command. */
110 out_be32(&fecp->fec_mii_data, (phy_id << 23) | mk_mii_read(location));
112 for (i = 0; i < FEC_MII_LOOPS; i++)
113 if ((in_be32(&fecp->fec_ievent) & FEC_ENET_MII) != 0)
114 break;
116 if (i < FEC_MII_LOOPS) {
117 out_be32(&fecp->fec_ievent, FEC_ENET_MII);
118 ret = in_be32(&fecp->fec_mii_data) & 0xffff;
121 return ret;
124 static int fs_enet_fec_mii_write(struct mii_bus *bus, int phy_id, int location, u16 val)
126 struct fec_info* fec = bus->priv;
127 fec_t __iomem *fecp = fec->fecp;
128 int i;
130 /* this must never happen */
131 if ((in_be32(&fecp->fec_r_cntrl) & FEC_RCNTRL_MII_MODE) == 0)
132 BUG();
134 /* Add PHY address to register command. */
135 out_be32(&fecp->fec_mii_data, (phy_id << 23) | mk_mii_write(location, val));
137 for (i = 0; i < FEC_MII_LOOPS; i++)
138 if ((in_be32(&fecp->fec_ievent) & FEC_ENET_MII) != 0)
139 break;
141 if (i < FEC_MII_LOOPS)
142 out_be32(&fecp->fec_ievent, FEC_ENET_MII);
144 return 0;
148 static int fs_enet_fec_mii_reset(struct mii_bus *bus)
150 /* nothing here - for now */
151 return 0;
154 #ifdef CONFIG_PPC_CPM_NEW_BINDING
155 static void __devinit add_phy(struct mii_bus *bus, struct device_node *np)
157 const u32 *data;
158 int len, id, irq;
160 data = of_get_property(np, "reg", &len);
161 if (!data || len != 4)
162 return;
164 id = *data;
165 bus->phy_mask &= ~(1 << id);
167 irq = of_irq_to_resource(np, 0, NULL);
168 if (irq != NO_IRQ)
169 bus->irq[id] = irq;
172 static int __devinit fs_enet_mdio_probe(struct of_device *ofdev,
173 const struct of_device_id *match)
175 struct device_node *np = NULL;
176 struct resource res;
177 struct mii_bus *new_bus;
178 struct fec_info *fec;
179 int ret = -ENOMEM, i;
181 new_bus = kzalloc(sizeof(struct mii_bus), GFP_KERNEL);
182 if (!new_bus)
183 goto out;
185 fec = kzalloc(sizeof(struct fec_info), GFP_KERNEL);
186 if (!fec)
187 goto out_mii;
189 new_bus->priv = fec;
190 new_bus->name = "FEC MII Bus";
191 new_bus->read = &fs_enet_fec_mii_read;
192 new_bus->write = &fs_enet_fec_mii_write;
193 new_bus->reset = &fs_enet_fec_mii_reset;
195 ret = of_address_to_resource(ofdev->node, 0, &res);
196 if (ret)
197 return ret;
199 new_bus->id = res.start;
201 fec->fecp = ioremap(res.start, res.end - res.start + 1);
202 if (!fec->fecp)
203 goto out_fec;
205 fec->mii_speed = ((ppc_proc_freq + 4999999) / 5000000) << 1;
207 setbits32(&fec->fecp->fec_r_cntrl, FEC_RCNTRL_MII_MODE);
208 setbits32(&fec->fecp->fec_ecntrl, FEC_ECNTRL_PINMUX |
209 FEC_ECNTRL_ETHER_EN);
210 out_be32(&fec->fecp->fec_ievent, FEC_ENET_MII);
211 out_be32(&fec->fecp->fec_mii_speed, fec->mii_speed);
213 new_bus->phy_mask = ~0;
214 new_bus->irq = kmalloc(sizeof(int) * PHY_MAX_ADDR, GFP_KERNEL);
215 if (!new_bus->irq)
216 goto out_unmap_regs;
218 for (i = 0; i < PHY_MAX_ADDR; i++)
219 new_bus->irq[i] = -1;
221 while ((np = of_get_next_child(ofdev->node, np)))
222 if (!strcmp(np->type, "ethernet-phy"))
223 add_phy(new_bus, np);
225 new_bus->dev = &ofdev->dev;
226 dev_set_drvdata(&ofdev->dev, new_bus);
228 ret = mdiobus_register(new_bus);
229 if (ret)
230 goto out_free_irqs;
232 return 0;
234 out_free_irqs:
235 dev_set_drvdata(&ofdev->dev, NULL);
236 kfree(new_bus->irq);
237 out_unmap_regs:
238 iounmap(fec->fecp);
239 out_fec:
240 kfree(fec);
241 out_mii:
242 kfree(new_bus);
243 out:
244 return ret;
247 static int fs_enet_mdio_remove(struct of_device *ofdev)
249 struct mii_bus *bus = dev_get_drvdata(&ofdev->dev);
250 struct fec_info *fec = bus->priv;
252 mdiobus_unregister(bus);
253 dev_set_drvdata(&ofdev->dev, NULL);
254 kfree(bus->irq);
255 iounmap(fec->fecp);
256 kfree(fec);
257 kfree(bus);
259 return 0;
262 static struct of_device_id fs_enet_mdio_fec_match[] = {
264 .compatible = "fsl,pq1-fec-mdio",
269 static struct of_platform_driver fs_enet_fec_mdio_driver = {
270 .name = "fsl-fec-mdio",
271 .match_table = fs_enet_mdio_fec_match,
272 .probe = fs_enet_mdio_probe,
273 .remove = fs_enet_mdio_remove,
276 static int fs_enet_mdio_fec_init(void)
278 return of_register_platform_driver(&fs_enet_fec_mdio_driver);
281 static void fs_enet_mdio_fec_exit(void)
283 of_unregister_platform_driver(&fs_enet_fec_mdio_driver);
286 module_init(fs_enet_mdio_fec_init);
287 module_exit(fs_enet_mdio_fec_exit);
288 #else
289 static int __devinit fs_enet_fec_mdio_probe(struct device *dev)
291 struct platform_device *pdev = to_platform_device(dev);
292 struct fs_mii_fec_platform_info *pdata;
293 struct mii_bus *new_bus;
294 struct fec_info *fec;
295 int err = 0;
296 if (NULL == dev)
297 return -EINVAL;
298 new_bus = kzalloc(sizeof(struct mii_bus), GFP_KERNEL);
300 if (NULL == new_bus)
301 return -ENOMEM;
303 fec = kzalloc(sizeof(struct fec_info), GFP_KERNEL);
305 if (NULL == fec)
306 return -ENOMEM;
308 new_bus->name = "FEC MII Bus",
309 new_bus->read = &fs_enet_fec_mii_read,
310 new_bus->write = &fs_enet_fec_mii_write,
311 new_bus->reset = &fs_enet_fec_mii_reset,
312 new_bus->id = pdev->id;
314 pdata = (struct fs_mii_fec_platform_info *)pdev->dev.platform_data;
316 if (NULL == pdata) {
317 printk(KERN_ERR "fs_enet FEC mdio %d: Missing platform data!\n", pdev->id);
318 return -ENODEV;
321 /*set up workspace*/
323 fs_mii_fec_init(fec, pdata);
324 new_bus->priv = fec;
326 new_bus->irq = pdata->irq;
328 new_bus->dev = dev;
329 dev_set_drvdata(dev, new_bus);
331 err = mdiobus_register(new_bus);
333 if (0 != err) {
334 printk (KERN_ERR "%s: Cannot register as MDIO bus\n",
335 new_bus->name);
336 goto bus_register_fail;
339 return 0;
341 bus_register_fail:
342 kfree(new_bus);
344 return err;
348 static int fs_enet_fec_mdio_remove(struct device *dev)
350 struct mii_bus *bus = dev_get_drvdata(dev);
352 mdiobus_unregister(bus);
354 dev_set_drvdata(dev, NULL);
355 kfree(bus->priv);
357 bus->priv = NULL;
358 kfree(bus);
360 return 0;
363 static struct device_driver fs_enet_fec_mdio_driver = {
364 .name = "fsl-cpm-fec-mdio",
365 .bus = &platform_bus_type,
366 .probe = fs_enet_fec_mdio_probe,
367 .remove = fs_enet_fec_mdio_remove,
370 int fs_enet_mdio_fec_init(void)
372 return driver_register(&fs_enet_fec_mdio_driver);
375 void fs_enet_mdio_fec_exit(void)
377 driver_unregister(&fs_enet_fec_mdio_driver);
379 #endif