blob: f0014cfbb275ab9206cfe2f55765be08f4a88e84 [file] [log] [blame]
Vitaly Bordug5b4b8452006-08-14 23:00:30 -07001/*
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
Vitaly Bordug5b4b8452006-08-14 23:00:30 -070015#include <linux/module.h>
16#include <linux/types.h>
17#include <linux/kernel.h>
Vitaly Bordug5b4b8452006-08-14 23:00:30 -070018#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>
Vitaly Bordug5b4b8452006-08-14 23:00:30 -070024#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
Scott Wood976de6a2007-10-02 10:55:58 -050039#ifdef CONFIG_PPC_CPM_NEW_BINDING
40#include <asm/of_platform.h>
41#endif
42
Vitaly Bordug5b4b8452006-08-14 23:00:30 -070043#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
Scott Wood976de6a2007-10-02 10:55:58 -050054#ifndef CONFIG_PPC_CPM_NEW_BINDING
Vitaly Bordug5b4b8452006-08-14 23:00:30 -070055static int match_has_phy (struct device *dev, void* data)
56{
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))
60 {
61 return 0;
62 }
63
64 fpi = pdev->dev.platform_data;
65 if((fpi)&&(fpi->has_phy))
66 return 1;
67 return 0;
68}
69
70static int fs_mii_fec_init(struct fec_info* fec, struct fs_mii_fec_platform_info *fmpi)
71{
72 struct resource *r;
Scott Wood31a5bb02007-10-01 14:20:58 -050073 fec_t __iomem *fecp;
Vitaly Bordug5b4b8452006-08-14 23:00:30 -070074 char* name = "fsl-cpm-fec";
75
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);
80
81 if(fec_pdev == NULL) {
82 printk(KERN_ERR"Unable to find PHY for %s", name);
83 return -ENODEV;
84 }
85
86 r = platform_get_resource_byname(fec_pdev, IORESOURCE_MEM, "regs");
87
Scott Wood31a5bb02007-10-01 14:20:58 -050088 fec->fecp = fecp = ioremap(r->start,sizeof(fec_t));
Vitaly Bordug5b4b8452006-08-14 23:00:30 -070089 fec->mii_speed = fmpi->mii_speed;
90
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);
95
96 return 0;
97}
Scott Wood976de6a2007-10-02 10:55:58 -050098#endif
Vitaly Bordug5b4b8452006-08-14 23:00:30 -070099
100static int fs_enet_fec_mii_read(struct mii_bus *bus , int phy_id, int location)
101{
102 struct fec_info* fec = bus->priv;
Scott Wood31a5bb02007-10-01 14:20:58 -0500103 fec_t __iomem *fecp = fec->fecp;
Vitaly Bordug5b4b8452006-08-14 23:00:30 -0700104 int i, ret = -1;
105
106 if ((in_be32(&fecp->fec_r_cntrl) & FEC_RCNTRL_MII_MODE) == 0)
107 BUG();
108
109 /* Add PHY address to register command. */
110 out_be32(&fecp->fec_mii_data, (phy_id << 23) | mk_mii_read(location));
111
112 for (i = 0; i < FEC_MII_LOOPS; i++)
113 if ((in_be32(&fecp->fec_ievent) & FEC_ENET_MII) != 0)
114 break;
115
116 if (i < FEC_MII_LOOPS) {
117 out_be32(&fecp->fec_ievent, FEC_ENET_MII);
118 ret = in_be32(&fecp->fec_mii_data) & 0xffff;
119 }
120
121 return ret;
Vitaly Bordug5b4b8452006-08-14 23:00:30 -0700122}
123
124static int fs_enet_fec_mii_write(struct mii_bus *bus, int phy_id, int location, u16 val)
125{
126 struct fec_info* fec = bus->priv;
Scott Wood31a5bb02007-10-01 14:20:58 -0500127 fec_t __iomem *fecp = fec->fecp;
Vitaly Bordug5b4b8452006-08-14 23:00:30 -0700128 int i;
129
130 /* this must never happen */
131 if ((in_be32(&fecp->fec_r_cntrl) & FEC_RCNTRL_MII_MODE) == 0)
132 BUG();
133
134 /* Add PHY address to register command. */
135 out_be32(&fecp->fec_mii_data, (phy_id << 23) | mk_mii_write(location, val));
136
137 for (i = 0; i < FEC_MII_LOOPS; i++)
138 if ((in_be32(&fecp->fec_ievent) & FEC_ENET_MII) != 0)
139 break;
140
141 if (i < FEC_MII_LOOPS)
142 out_be32(&fecp->fec_ievent, FEC_ENET_MII);
143
144 return 0;
145
146}
147
148static int fs_enet_fec_mii_reset(struct mii_bus *bus)
149{
150 /* nothing here - for now */
151 return 0;
152}
153
Scott Wood976de6a2007-10-02 10:55:58 -0500154#ifdef CONFIG_PPC_CPM_NEW_BINDING
155static void __devinit add_phy(struct mii_bus *bus, struct device_node *np)
156{
157 const u32 *data;
158 int len, id, irq;
159
160 data = of_get_property(np, "reg", &len);
161 if (!data || len != 4)
162 return;
163
164 id = *data;
165 bus->phy_mask &= ~(1 << id);
166
167 irq = of_irq_to_resource(np, 0, NULL);
168 if (irq != NO_IRQ)
169 bus->irq[id] = irq;
170}
171
172static int __devinit fs_enet_mdio_probe(struct of_device *ofdev,
173 const struct of_device_id *match)
174{
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;
180
181 new_bus = kzalloc(sizeof(struct mii_bus), GFP_KERNEL);
182 if (!new_bus)
183 goto out;
184
185 fec = kzalloc(sizeof(struct fec_info), GFP_KERNEL);
186 if (!fec)
187 goto out_mii;
188
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;
194
195 ret = of_address_to_resource(ofdev->node, 0, &res);
196 if (ret)
Scott Wooda86e2cb2008-05-02 13:42:41 -0500197 goto out_res;
Scott Wood976de6a2007-10-02 10:55:58 -0500198
Andy Fleming9d9326d2008-04-09 19:38:13 -0500199 snprintf(new_bus->id, MII_BUS_ID_SIZE, "%x", res.start);
Scott Wood976de6a2007-10-02 10:55:58 -0500200
201 fec->fecp = ioremap(res.start, res.end - res.start + 1);
202 if (!fec->fecp)
203 goto out_fec;
204
205 fec->mii_speed = ((ppc_proc_freq + 4999999) / 5000000) << 1;
206
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);
212
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;
217
218 for (i = 0; i < PHY_MAX_ADDR; i++)
219 new_bus->irq[i] = -1;
220
221 while ((np = of_get_next_child(ofdev->node, np)))
222 if (!strcmp(np->type, "ethernet-phy"))
223 add_phy(new_bus, np);
224
225 new_bus->dev = &ofdev->dev;
226 dev_set_drvdata(&ofdev->dev, new_bus);
227
228 ret = mdiobus_register(new_bus);
229 if (ret)
230 goto out_free_irqs;
231
232 return 0;
233
234out_free_irqs:
235 dev_set_drvdata(&ofdev->dev, NULL);
236 kfree(new_bus->irq);
237out_unmap_regs:
238 iounmap(fec->fecp);
Scott Wooda86e2cb2008-05-02 13:42:41 -0500239out_res:
Scott Wood976de6a2007-10-02 10:55:58 -0500240out_fec:
241 kfree(fec);
242out_mii:
243 kfree(new_bus);
244out:
245 return ret;
246}
247
248static int fs_enet_mdio_remove(struct of_device *ofdev)
249{
250 struct mii_bus *bus = dev_get_drvdata(&ofdev->dev);
251 struct fec_info *fec = bus->priv;
252
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);
259
260 return 0;
261}
262
263static struct of_device_id fs_enet_mdio_fec_match[] = {
264 {
265 .compatible = "fsl,pq1-fec-mdio",
266 },
267 {},
268};
269
270static 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,
275};
276
277static int fs_enet_mdio_fec_init(void)
278{
279 return of_register_platform_driver(&fs_enet_fec_mdio_driver);
280}
281
282static void fs_enet_mdio_fec_exit(void)
283{
284 of_unregister_platform_driver(&fs_enet_fec_mdio_driver);
285}
286
287module_init(fs_enet_mdio_fec_init);
288module_exit(fs_enet_mdio_fec_exit);
289#else
Vitaly Bordug5b4b8452006-08-14 23:00:30 -0700290static int __devinit fs_enet_fec_mdio_probe(struct device *dev)
291{
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);
300
301 if (NULL == new_bus)
302 return -ENOMEM;
303
304 fec = kzalloc(sizeof(struct fec_info), GFP_KERNEL);
305
306 if (NULL == fec)
307 return -ENOMEM;
308
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,
Andy Fleming9d9326d2008-04-09 19:38:13 -0500313 snprintf(new_bus->id, MII_BUS_ID_SIZE, "%x", pdev->id);
Vitaly Bordug5b4b8452006-08-14 23:00:30 -0700314
315 pdata = (struct fs_mii_fec_platform_info *)pdev->dev.platform_data;
316
317 if (NULL == pdata) {
318 printk(KERN_ERR "fs_enet FEC mdio %d: Missing platform data!\n", pdev->id);
319 return -ENODEV;
320 }
321
322 /*set up workspace*/
323
324 fs_mii_fec_init(fec, pdata);
325 new_bus->priv = fec;
326
327 new_bus->irq = pdata->irq;
328
329 new_bus->dev = dev;
330 dev_set_drvdata(dev, new_bus);
331
332 err = mdiobus_register(new_bus);
333
334 if (0 != err) {
335 printk (KERN_ERR "%s: Cannot register as MDIO bus\n",
336 new_bus->name);
337 goto bus_register_fail;
338 }
339
340 return 0;
341
342bus_register_fail:
343 kfree(new_bus);
344
345 return err;
346}
347
348
349static int fs_enet_fec_mdio_remove(struct device *dev)
350{
351 struct mii_bus *bus = dev_get_drvdata(dev);
352
353 mdiobus_unregister(bus);
354
355 dev_set_drvdata(dev, NULL);
356 kfree(bus->priv);
357
358 bus->priv = NULL;
359 kfree(bus);
360
361 return 0;
362}
363
364static 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,
369};
370
371int fs_enet_mdio_fec_init(void)
372{
373 return driver_register(&fs_enet_fec_mdio_driver);
374}
375
376void fs_enet_mdio_fec_exit(void)
377{
378 driver_unregister(&fs_enet_fec_mdio_driver);
379}
Scott Wood976de6a2007-10-02 10:55:58 -0500380#endif