Merge branch 'merge'
[pandora-kernel.git] / drivers / net / fs_enet / mii-fec.c
1 /*
2  * Combined Ethernet driver for Motorola MPC8xx and MPC82xx.
3  *
4  * Copyright (c) 2003 Intracom S.A.
5  *  by Pantelis Antoniou <panto@intracom.gr>
6  *
7  * 2005 (c) MontaVista Software, Inc.
8  * Vitaly Bordug <vbordug@ru.mvista.com>
9  *
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.
13  */
14
15
16 #include <linux/config.h>
17 #include <linux/module.h>
18 #include <linux/types.h>
19 #include <linux/kernel.h>
20 #include <linux/sched.h>
21 #include <linux/string.h>
22 #include <linux/ptrace.h>
23 #include <linux/errno.h>
24 #include <linux/ioport.h>
25 #include <linux/slab.h>
26 #include <linux/interrupt.h>
27 #include <linux/pci.h>
28 #include <linux/init.h>
29 #include <linux/delay.h>
30 #include <linux/netdevice.h>
31 #include <linux/etherdevice.h>
32 #include <linux/skbuff.h>
33 #include <linux/spinlock.h>
34 #include <linux/mii.h>
35 #include <linux/ethtool.h>
36 #include <linux/bitops.h>
37 #include <linux/platform_device.h>
38
39 #include <asm/pgtable.h>
40 #include <asm/irq.h>
41 #include <asm/uaccess.h>
42
43 #include "fs_enet.h"
44 #include "fec.h"
45
46 /* Make MII read/write commands for the FEC.
47 */
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
51
52 #define FEC_MII_LOOPS   10000
53
54 static int match_has_phy (struct device *dev, void* data)
55 {
56         struct platform_device* pdev = container_of(dev, struct platform_device, dev);
57         struct fs_platform_info* fpi;
58         if(strcmp(pdev->name, (char*)data))
59         {
60             return 0;
61         }
62
63         fpi = pdev->dev.platform_data;
64         if((fpi)&&(fpi->has_phy))
65                 return 1;
66         return 0;
67 }
68
69 static int fs_mii_fec_init(struct fec_info* fec, struct fs_mii_fec_platform_info *fmpi)
70 {
71         struct resource *r;
72         fec_t *fecp;
73         char* name = "fsl-cpm-fec";
74
75         /* we need fec in order to be useful */
76         struct platform_device *fec_pdev =
77                 container_of(bus_find_device(&platform_bus_type, NULL, name, match_has_phy),
78                                 struct platform_device, dev);
79
80         if(fec_pdev == NULL) {
81                 printk(KERN_ERR"Unable to find PHY for %s", name);
82                 return -ENODEV;
83         }
84
85         r = platform_get_resource_byname(fec_pdev, IORESOURCE_MEM, "regs");
86
87         fec->fecp = fecp = (fec_t*)ioremap(r->start,sizeof(fec_t));
88         fec->mii_speed = fmpi->mii_speed;
89
90         setbits32(&fecp->fec_r_cntrl, FEC_RCNTRL_MII_MODE);     /* MII enable */
91         setbits32(&fecp->fec_ecntrl, FEC_ECNTRL_PINMUX | FEC_ECNTRL_ETHER_EN);
92         out_be32(&fecp->fec_ievent, FEC_ENET_MII);
93         out_be32(&fecp->fec_mii_speed, fec->mii_speed);
94
95         return 0;
96 }
97
98 static int fs_enet_fec_mii_read(struct mii_bus *bus , int phy_id, int location)
99 {
100         struct fec_info* fec = bus->priv;
101         fec_t *fecp = fec->fecp;
102         int i, ret = -1;
103
104         if ((in_be32(&fecp->fec_r_cntrl) & FEC_RCNTRL_MII_MODE) == 0)
105                 BUG();
106
107         /* Add PHY address to register command.  */
108         out_be32(&fecp->fec_mii_data, (phy_id << 23) | mk_mii_read(location));
109
110         for (i = 0; i < FEC_MII_LOOPS; i++)
111                 if ((in_be32(&fecp->fec_ievent) & FEC_ENET_MII) != 0)
112                         break;
113
114         if (i < FEC_MII_LOOPS) {
115                 out_be32(&fecp->fec_ievent, FEC_ENET_MII);
116                 ret = in_be32(&fecp->fec_mii_data) & 0xffff;
117         }
118
119         return ret;
120
121 }
122
123 static int fs_enet_fec_mii_write(struct mii_bus *bus, int phy_id, int location, u16 val)
124 {
125         struct fec_info* fec = bus->priv;
126         fec_t *fecp = fec->fecp;
127         int i;
128
129         /* this must never happen */
130         if ((in_be32(&fecp->fec_r_cntrl) & FEC_RCNTRL_MII_MODE) == 0)
131                 BUG();
132
133         /* Add PHY address to register command.  */
134         out_be32(&fecp->fec_mii_data, (phy_id << 23) | mk_mii_write(location, val));
135
136         for (i = 0; i < FEC_MII_LOOPS; i++)
137                 if ((in_be32(&fecp->fec_ievent) & FEC_ENET_MII) != 0)
138                         break;
139
140         if (i < FEC_MII_LOOPS)
141                 out_be32(&fecp->fec_ievent, FEC_ENET_MII);
142
143         return 0;
144
145 }
146
147 static int fs_enet_fec_mii_reset(struct mii_bus *bus)
148 {
149         /* nothing here - for now */
150         return 0;
151 }
152
153 static int __devinit fs_enet_fec_mdio_probe(struct device *dev)
154 {
155         struct platform_device *pdev = to_platform_device(dev);
156         struct fs_mii_fec_platform_info *pdata;
157         struct mii_bus *new_bus;
158         struct fec_info *fec;
159         int err = 0;
160         if (NULL == dev)
161                 return -EINVAL;
162         new_bus = kzalloc(sizeof(struct mii_bus), GFP_KERNEL);
163
164         if (NULL == new_bus)
165                 return -ENOMEM;
166
167         fec = kzalloc(sizeof(struct fec_info), GFP_KERNEL);
168
169         if (NULL == fec)
170                 return -ENOMEM;
171
172         new_bus->name = "FEC MII Bus",
173         new_bus->read = &fs_enet_fec_mii_read,
174         new_bus->write = &fs_enet_fec_mii_write,
175         new_bus->reset = &fs_enet_fec_mii_reset,
176         new_bus->id = pdev->id;
177
178         pdata = (struct fs_mii_fec_platform_info *)pdev->dev.platform_data;
179
180         if (NULL == pdata) {
181                 printk(KERN_ERR "fs_enet FEC mdio %d: Missing platform data!\n", pdev->id);
182                 return -ENODEV;
183         }
184
185         /*set up workspace*/
186
187         fs_mii_fec_init(fec, pdata);
188         new_bus->priv = fec;
189
190         new_bus->irq = pdata->irq;
191
192         new_bus->dev = dev;
193         dev_set_drvdata(dev, new_bus);
194
195         err = mdiobus_register(new_bus);
196
197         if (0 != err) {
198                 printk (KERN_ERR "%s: Cannot register as MDIO bus\n",
199                                 new_bus->name);
200                 goto bus_register_fail;
201         }
202
203         return 0;
204
205 bus_register_fail:
206         kfree(new_bus);
207
208         return err;
209 }
210
211
212 static int fs_enet_fec_mdio_remove(struct device *dev)
213 {
214         struct mii_bus *bus = dev_get_drvdata(dev);
215
216         mdiobus_unregister(bus);
217
218         dev_set_drvdata(dev, NULL);
219         kfree(bus->priv);
220
221         bus->priv = NULL;
222         kfree(bus);
223
224         return 0;
225 }
226
227 static struct device_driver fs_enet_fec_mdio_driver = {
228         .name = "fsl-cpm-fec-mdio",
229         .bus = &platform_bus_type,
230         .probe = fs_enet_fec_mdio_probe,
231         .remove = fs_enet_fec_mdio_remove,
232 };
233
234 int fs_enet_mdio_fec_init(void)
235 {
236         return driver_register(&fs_enet_fec_mdio_driver);
237 }
238
239 void fs_enet_mdio_fec_exit(void)
240 {
241         driver_unregister(&fs_enet_fec_mdio_driver);
242 }
243