kmemtrace: SLOB hooks.
[linux-2.6/kmemtrace.git] / drivers / net / fs_enet / mii-fec.c
blobf0014cfbb275ab9206cfe2f55765be08f4a88e84
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 goto out_res;
199 snprintf(new_bus->id, MII_BUS_ID_SIZE, "%x", 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_res:
240 out_fec:
241 kfree(fec);
242 out_mii:
243 kfree(new_bus);
244 out:
245 return ret;
248 static int fs_enet_mdio_remove(struct of_device *ofdev)
250 struct mii_bus *bus = dev_get_drvdata(&ofdev->dev);
251 struct fec_info *fec = bus->priv;
253 mdiobus_unregister(bus);
254 dev_set_drvdata(&ofdev->dev, NULL);
255 kfree(bus->irq);
256 iounmap(fec->fecp);
257 kfree(fec);
258 kfree(bus);
260 return 0;
263 static struct of_device_id fs_enet_mdio_fec_match[] = {
265 .compatible = "fsl,pq1-fec-mdio",
270 static struct of_platform_driver fs_enet_fec_mdio_driver = {
271 .name = "fsl-fec-mdio",
272 .match_table = fs_enet_mdio_fec_match,
273 .probe = fs_enet_mdio_probe,
274 .remove = fs_enet_mdio_remove,
277 static int fs_enet_mdio_fec_init(void)
279 return of_register_platform_driver(&fs_enet_fec_mdio_driver);
282 static void fs_enet_mdio_fec_exit(void)
284 of_unregister_platform_driver(&fs_enet_fec_mdio_driver);
287 module_init(fs_enet_mdio_fec_init);
288 module_exit(fs_enet_mdio_fec_exit);
289 #else
290 static int __devinit fs_enet_fec_mdio_probe(struct device *dev)
292 struct platform_device *pdev = to_platform_device(dev);
293 struct fs_mii_fec_platform_info *pdata;
294 struct mii_bus *new_bus;
295 struct fec_info *fec;
296 int err = 0;
297 if (NULL == dev)
298 return -EINVAL;
299 new_bus = kzalloc(sizeof(struct mii_bus), GFP_KERNEL);
301 if (NULL == new_bus)
302 return -ENOMEM;
304 fec = kzalloc(sizeof(struct fec_info), GFP_KERNEL);
306 if (NULL == fec)
307 return -ENOMEM;
309 new_bus->name = "FEC MII Bus",
310 new_bus->read = &fs_enet_fec_mii_read,
311 new_bus->write = &fs_enet_fec_mii_write,
312 new_bus->reset = &fs_enet_fec_mii_reset,
313 snprintf(new_bus->id, MII_BUS_ID_SIZE, "%x", pdev->id);
315 pdata = (struct fs_mii_fec_platform_info *)pdev->dev.platform_data;
317 if (NULL == pdata) {
318 printk(KERN_ERR "fs_enet FEC mdio %d: Missing platform data!\n", pdev->id);
319 return -ENODEV;
322 /*set up workspace*/
324 fs_mii_fec_init(fec, pdata);
325 new_bus->priv = fec;
327 new_bus->irq = pdata->irq;
329 new_bus->dev = dev;
330 dev_set_drvdata(dev, new_bus);
332 err = mdiobus_register(new_bus);
334 if (0 != err) {
335 printk (KERN_ERR "%s: Cannot register as MDIO bus\n",
336 new_bus->name);
337 goto bus_register_fail;
340 return 0;
342 bus_register_fail:
343 kfree(new_bus);
345 return err;
349 static int fs_enet_fec_mdio_remove(struct device *dev)
351 struct mii_bus *bus = dev_get_drvdata(dev);
353 mdiobus_unregister(bus);
355 dev_set_drvdata(dev, NULL);
356 kfree(bus->priv);
358 bus->priv = NULL;
359 kfree(bus);
361 return 0;
364 static struct device_driver fs_enet_fec_mdio_driver = {
365 .name = "fsl-cpm-fec-mdio",
366 .bus = &platform_bus_type,
367 .probe = fs_enet_fec_mdio_probe,
368 .remove = fs_enet_fec_mdio_remove,
371 int fs_enet_mdio_fec_init(void)
373 return driver_register(&fs_enet_fec_mdio_driver);
376 void fs_enet_mdio_fec_exit(void)
378 driver_unregister(&fs_enet_fec_mdio_driver);
380 #endif