[PATCH] remove many unneeded #includes of sched.h
[linux-2.6/linux-2.6-openrd.git] / drivers / net / fs_enet / mii-fec.c
blob235b177fb9acb1253efa7ce98aaf8bdeebaefaed
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/pci.h>
25 #include <linux/init.h>
26 #include <linux/delay.h>
27 #include <linux/netdevice.h>
28 #include <linux/etherdevice.h>
29 #include <linux/skbuff.h>
30 #include <linux/spinlock.h>
31 #include <linux/mii.h>
32 #include <linux/ethtool.h>
33 #include <linux/bitops.h>
34 #include <linux/platform_device.h>
36 #include <asm/pgtable.h>
37 #include <asm/irq.h>
38 #include <asm/uaccess.h>
40 #include "fs_enet.h"
41 #include "fec.h"
43 /* Make MII read/write commands for the FEC.
45 #define mk_mii_read(REG) (0x60020000 | ((REG & 0x1f) << 18))
46 #define mk_mii_write(REG, VAL) (0x50020000 | ((REG & 0x1f) << 18) | (VAL & 0xffff))
47 #define mk_mii_end 0
49 #define FEC_MII_LOOPS 10000
51 static int match_has_phy (struct device *dev, void* data)
53 struct platform_device* pdev = container_of(dev, struct platform_device, dev);
54 struct fs_platform_info* fpi;
55 if(strcmp(pdev->name, (char*)data))
57 return 0;
60 fpi = pdev->dev.platform_data;
61 if((fpi)&&(fpi->has_phy))
62 return 1;
63 return 0;
66 static int fs_mii_fec_init(struct fec_info* fec, struct fs_mii_fec_platform_info *fmpi)
68 struct resource *r;
69 fec_t *fecp;
70 char* name = "fsl-cpm-fec";
72 /* we need fec in order to be useful */
73 struct platform_device *fec_pdev =
74 container_of(bus_find_device(&platform_bus_type, NULL, name, match_has_phy),
75 struct platform_device, dev);
77 if(fec_pdev == NULL) {
78 printk(KERN_ERR"Unable to find PHY for %s", name);
79 return -ENODEV;
82 r = platform_get_resource_byname(fec_pdev, IORESOURCE_MEM, "regs");
84 fec->fecp = fecp = (fec_t*)ioremap(r->start,sizeof(fec_t));
85 fec->mii_speed = fmpi->mii_speed;
87 setbits32(&fecp->fec_r_cntrl, FEC_RCNTRL_MII_MODE); /* MII enable */
88 setbits32(&fecp->fec_ecntrl, FEC_ECNTRL_PINMUX | FEC_ECNTRL_ETHER_EN);
89 out_be32(&fecp->fec_ievent, FEC_ENET_MII);
90 out_be32(&fecp->fec_mii_speed, fec->mii_speed);
92 return 0;
95 static int fs_enet_fec_mii_read(struct mii_bus *bus , int phy_id, int location)
97 struct fec_info* fec = bus->priv;
98 fec_t *fecp = fec->fecp;
99 int i, ret = -1;
101 if ((in_be32(&fecp->fec_r_cntrl) & FEC_RCNTRL_MII_MODE) == 0)
102 BUG();
104 /* Add PHY address to register command. */
105 out_be32(&fecp->fec_mii_data, (phy_id << 23) | mk_mii_read(location));
107 for (i = 0; i < FEC_MII_LOOPS; i++)
108 if ((in_be32(&fecp->fec_ievent) & FEC_ENET_MII) != 0)
109 break;
111 if (i < FEC_MII_LOOPS) {
112 out_be32(&fecp->fec_ievent, FEC_ENET_MII);
113 ret = in_be32(&fecp->fec_mii_data) & 0xffff;
116 return ret;
120 static int fs_enet_fec_mii_write(struct mii_bus *bus, int phy_id, int location, u16 val)
122 struct fec_info* fec = bus->priv;
123 fec_t *fecp = fec->fecp;
124 int i;
126 /* this must never happen */
127 if ((in_be32(&fecp->fec_r_cntrl) & FEC_RCNTRL_MII_MODE) == 0)
128 BUG();
130 /* Add PHY address to register command. */
131 out_be32(&fecp->fec_mii_data, (phy_id << 23) | mk_mii_write(location, val));
133 for (i = 0; i < FEC_MII_LOOPS; i++)
134 if ((in_be32(&fecp->fec_ievent) & FEC_ENET_MII) != 0)
135 break;
137 if (i < FEC_MII_LOOPS)
138 out_be32(&fecp->fec_ievent, FEC_ENET_MII);
140 return 0;
144 static int fs_enet_fec_mii_reset(struct mii_bus *bus)
146 /* nothing here - for now */
147 return 0;
150 static int __devinit fs_enet_fec_mdio_probe(struct device *dev)
152 struct platform_device *pdev = to_platform_device(dev);
153 struct fs_mii_fec_platform_info *pdata;
154 struct mii_bus *new_bus;
155 struct fec_info *fec;
156 int err = 0;
157 if (NULL == dev)
158 return -EINVAL;
159 new_bus = kzalloc(sizeof(struct mii_bus), GFP_KERNEL);
161 if (NULL == new_bus)
162 return -ENOMEM;
164 fec = kzalloc(sizeof(struct fec_info), GFP_KERNEL);
166 if (NULL == fec)
167 return -ENOMEM;
169 new_bus->name = "FEC MII Bus",
170 new_bus->read = &fs_enet_fec_mii_read,
171 new_bus->write = &fs_enet_fec_mii_write,
172 new_bus->reset = &fs_enet_fec_mii_reset,
173 new_bus->id = pdev->id;
175 pdata = (struct fs_mii_fec_platform_info *)pdev->dev.platform_data;
177 if (NULL == pdata) {
178 printk(KERN_ERR "fs_enet FEC mdio %d: Missing platform data!\n", pdev->id);
179 return -ENODEV;
182 /*set up workspace*/
184 fs_mii_fec_init(fec, pdata);
185 new_bus->priv = fec;
187 new_bus->irq = pdata->irq;
189 new_bus->dev = dev;
190 dev_set_drvdata(dev, new_bus);
192 err = mdiobus_register(new_bus);
194 if (0 != err) {
195 printk (KERN_ERR "%s: Cannot register as MDIO bus\n",
196 new_bus->name);
197 goto bus_register_fail;
200 return 0;
202 bus_register_fail:
203 kfree(new_bus);
205 return err;
209 static int fs_enet_fec_mdio_remove(struct device *dev)
211 struct mii_bus *bus = dev_get_drvdata(dev);
213 mdiobus_unregister(bus);
215 dev_set_drvdata(dev, NULL);
216 kfree(bus->priv);
218 bus->priv = NULL;
219 kfree(bus);
221 return 0;
224 static struct device_driver fs_enet_fec_mdio_driver = {
225 .name = "fsl-cpm-fec-mdio",
226 .bus = &platform_bus_type,
227 .probe = fs_enet_fec_mdio_probe,
228 .remove = fs_enet_fec_mdio_remove,
231 int fs_enet_mdio_fec_init(void)
233 return driver_register(&fs_enet_fec_mdio_driver);
236 void fs_enet_mdio_fec_exit(void)
238 driver_unregister(&fs_enet_fec_mdio_driver);