53db696b948fcf728453117499b760eb64898e5d
[deliverable/linux.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 #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>
34
35 #include <asm/pgtable.h>
36 #include <asm/irq.h>
37 #include <asm/uaccess.h>
38
39 #include "fs_enet.h"
40 #include "fec.h"
41
42 /* Make MII read/write commands for the FEC.
43 */
44 #define mk_mii_read(REG) (0x60020000 | ((REG & 0x1f) << 18))
45 #define mk_mii_write(REG, VAL) (0x50020000 | ((REG & 0x1f) << 18) | (VAL & 0xffff))
46 #define mk_mii_end 0
47
48 #define FEC_MII_LOOPS 10000
49
50 static int match_has_phy (struct device *dev, void* data)
51 {
52 struct platform_device* pdev = container_of(dev, struct platform_device, dev);
53 struct fs_platform_info* fpi;
54 if(strcmp(pdev->name, (char*)data))
55 {
56 return 0;
57 }
58
59 fpi = pdev->dev.platform_data;
60 if((fpi)&&(fpi->has_phy))
61 return 1;
62 return 0;
63 }
64
65 static int fs_mii_fec_init(struct fec_info* fec, struct fs_mii_fec_platform_info *fmpi)
66 {
67 struct resource *r;
68 fec_t *fecp;
69 char* name = "fsl-cpm-fec";
70
71 /* we need fec in order to be useful */
72 struct platform_device *fec_pdev =
73 container_of(bus_find_device(&platform_bus_type, NULL, name, match_has_phy),
74 struct platform_device, dev);
75
76 if(fec_pdev == NULL) {
77 printk(KERN_ERR"Unable to find PHY for %s", name);
78 return -ENODEV;
79 }
80
81 r = platform_get_resource_byname(fec_pdev, IORESOURCE_MEM, "regs");
82
83 fec->fecp = fecp = (fec_t*)ioremap(r->start,sizeof(fec_t));
84 fec->mii_speed = fmpi->mii_speed;
85
86 setbits32(&fecp->fec_r_cntrl, FEC_RCNTRL_MII_MODE); /* MII enable */
87 setbits32(&fecp->fec_ecntrl, FEC_ECNTRL_PINMUX | FEC_ECNTRL_ETHER_EN);
88 out_be32(&fecp->fec_ievent, FEC_ENET_MII);
89 out_be32(&fecp->fec_mii_speed, fec->mii_speed);
90
91 return 0;
92 }
93
94 static int fs_enet_fec_mii_read(struct mii_bus *bus , int phy_id, int location)
95 {
96 struct fec_info* fec = bus->priv;
97 fec_t *fecp = fec->fecp;
98 int i, ret = -1;
99
100 if ((in_be32(&fecp->fec_r_cntrl) & FEC_RCNTRL_MII_MODE) == 0)
101 BUG();
102
103 /* Add PHY address to register command. */
104 out_be32(&fecp->fec_mii_data, (phy_id << 23) | mk_mii_read(location));
105
106 for (i = 0; i < FEC_MII_LOOPS; i++)
107 if ((in_be32(&fecp->fec_ievent) & FEC_ENET_MII) != 0)
108 break;
109
110 if (i < FEC_MII_LOOPS) {
111 out_be32(&fecp->fec_ievent, FEC_ENET_MII);
112 ret = in_be32(&fecp->fec_mii_data) & 0xffff;
113 }
114
115 return ret;
116 }
117
118 static int fs_enet_fec_mii_write(struct mii_bus *bus, int phy_id, int location, u16 val)
119 {
120 struct fec_info* fec = bus->priv;
121 fec_t *fecp = fec->fecp;
122 int i;
123
124 /* this must never happen */
125 if ((in_be32(&fecp->fec_r_cntrl) & FEC_RCNTRL_MII_MODE) == 0)
126 BUG();
127
128 /* Add PHY address to register command. */
129 out_be32(&fecp->fec_mii_data, (phy_id << 23) | mk_mii_write(location, val));
130
131 for (i = 0; i < FEC_MII_LOOPS; i++)
132 if ((in_be32(&fecp->fec_ievent) & FEC_ENET_MII) != 0)
133 break;
134
135 if (i < FEC_MII_LOOPS)
136 out_be32(&fecp->fec_ievent, FEC_ENET_MII);
137
138 return 0;
139
140 }
141
142 static int fs_enet_fec_mii_reset(struct mii_bus *bus)
143 {
144 /* nothing here - for now */
145 return 0;
146 }
147
148 static int __devinit fs_enet_fec_mdio_probe(struct device *dev)
149 {
150 struct platform_device *pdev = to_platform_device(dev);
151 struct fs_mii_fec_platform_info *pdata;
152 struct mii_bus *new_bus;
153 struct fec_info *fec;
154 int err = 0;
155 if (NULL == dev)
156 return -EINVAL;
157 new_bus = kzalloc(sizeof(struct mii_bus), GFP_KERNEL);
158
159 if (NULL == new_bus)
160 return -ENOMEM;
161
162 fec = kzalloc(sizeof(struct fec_info), GFP_KERNEL);
163
164 if (NULL == fec)
165 return -ENOMEM;
166
167 new_bus->name = "FEC MII Bus",
168 new_bus->read = &fs_enet_fec_mii_read,
169 new_bus->write = &fs_enet_fec_mii_write,
170 new_bus->reset = &fs_enet_fec_mii_reset,
171 new_bus->id = pdev->id;
172
173 pdata = (struct fs_mii_fec_platform_info *)pdev->dev.platform_data;
174
175 if (NULL == pdata) {
176 printk(KERN_ERR "fs_enet FEC mdio %d: Missing platform data!\n", pdev->id);
177 return -ENODEV;
178 }
179
180 /*set up workspace*/
181
182 fs_mii_fec_init(fec, pdata);
183 new_bus->priv = fec;
184
185 new_bus->irq = pdata->irq;
186
187 new_bus->dev = dev;
188 dev_set_drvdata(dev, new_bus);
189
190 err = mdiobus_register(new_bus);
191
192 if (0 != err) {
193 printk (KERN_ERR "%s: Cannot register as MDIO bus\n",
194 new_bus->name);
195 goto bus_register_fail;
196 }
197
198 return 0;
199
200 bus_register_fail:
201 kfree(new_bus);
202
203 return err;
204 }
205
206
207 static int fs_enet_fec_mdio_remove(struct device *dev)
208 {
209 struct mii_bus *bus = dev_get_drvdata(dev);
210
211 mdiobus_unregister(bus);
212
213 dev_set_drvdata(dev, NULL);
214 kfree(bus->priv);
215
216 bus->priv = NULL;
217 kfree(bus);
218
219 return 0;
220 }
221
222 static struct device_driver fs_enet_fec_mdio_driver = {
223 .name = "fsl-cpm-fec-mdio",
224 .bus = &platform_bus_type,
225 .probe = fs_enet_fec_mdio_probe,
226 .remove = fs_enet_fec_mdio_remove,
227 };
228
229 int fs_enet_mdio_fec_init(void)
230 {
231 return driver_register(&fs_enet_fec_mdio_driver);
232 }
233
234 void fs_enet_mdio_fec_exit(void)
235 {
236 driver_unregister(&fs_enet_fec_mdio_driver);
237 }
238
This page took 0.04381 seconds and 4 git commands to generate.