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>
37 #include <asm/uaccess.h>
39 #ifdef CONFIG_PPC_CPM_NEW_BINDING
40 #include <asm/of_platform.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))
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))
64 fpi = pdev->dev.platform_data;
65 if((fpi)&&(fpi->has_phy))
70 static int fs_mii_fec_init(struct fec_info* fec, struct fs_mii_fec_platform_info *fmpi)
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);
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);
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;
106 if ((in_be32(&fecp->fec_r_cntrl) & FEC_RCNTRL_MII_MODE) == 0)
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)
116 if (i < FEC_MII_LOOPS) {
117 out_be32(&fecp->fec_ievent, FEC_ENET_MII);
118 ret = in_be32(&fecp->fec_mii_data) & 0xffff;
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;
130 /* this must never happen */
131 if ((in_be32(&fecp->fec_r_cntrl) & FEC_RCNTRL_MII_MODE) == 0)
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)
141 if (i < FEC_MII_LOOPS)
142 out_be32(&fecp->fec_ievent, FEC_ENET_MII);
148 static int fs_enet_fec_mii_reset(struct mii_bus *bus)
150 /* nothing here - for now */
154 #ifdef CONFIG_PPC_CPM_NEW_BINDING
155 static void __devinit add_phy(struct mii_bus *bus, struct device_node *np)
160 data = of_get_property(np, "reg", &len);
161 if (!data || len != 4)
165 bus->phy_mask &= ~(1 << id);
167 irq = of_irq_to_resource(np, 0, NULL);
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;
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);
185 fec = kzalloc(sizeof(struct fec_info), GFP_KERNEL);
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);
199 new_bus->id = res.start;
201 fec->fecp = ioremap(res.start, res.end - res.start + 1);
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);
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);
235 dev_set_drvdata(&ofdev->dev, NULL);
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);
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);
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;
298 new_bus = kzalloc(sizeof(struct mii_bus), GFP_KERNEL);
303 fec = kzalloc(sizeof(struct fec_info), GFP_KERNEL);
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;
317 printk(KERN_ERR "fs_enet FEC mdio %d: Missing platform data!\n", pdev->id);
323 fs_mii_fec_init(fec, pdata);
326 new_bus->irq = pdata->irq;
329 dev_set_drvdata(dev, new_bus);
331 err = mdiobus_register(new_bus);
334 printk (KERN_ERR "%s: Cannot register as MDIO bus\n",
336 goto bus_register_fail;
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);
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);