Merge ../linux-2.6-watchdog-mm
[pandora-kernel.git] / drivers / net / fs_enet / mac-fec.c
1 /*
2  * Freescale Ethernet controllers
3  *
4  * Copyright (c) 2005 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/kernel.h>
17 #include <linux/types.h>
18 #include <linux/sched.h>
19 #include <linux/string.h>
20 #include <linux/ptrace.h>
21 #include <linux/errno.h>
22 #include <linux/ioport.h>
23 #include <linux/slab.h>
24 #include <linux/interrupt.h>
25 #include <linux/pci.h>
26 #include <linux/init.h>
27 #include <linux/delay.h>
28 #include <linux/netdevice.h>
29 #include <linux/etherdevice.h>
30 #include <linux/skbuff.h>
31 #include <linux/spinlock.h>
32 #include <linux/mii.h>
33 #include <linux/ethtool.h>
34 #include <linux/bitops.h>
35 #include <linux/fs.h>
36 #include <linux/platform_device.h>
37
38 #include <asm/irq.h>
39 #include <asm/uaccess.h>
40
41 #ifdef CONFIG_8xx
42 #include <asm/8xx_immap.h>
43 #include <asm/pgtable.h>
44 #include <asm/mpc8xx.h>
45 #include <asm/commproc.h>
46 #endif
47
48 #include "fs_enet.h"
49 #include "fec.h"
50
51 /*************************************************/
52
53 #if defined(CONFIG_CPM1)
54 /* for a CPM1 __raw_xxx's are sufficient */
55 #define __fs_out32(addr, x)     __raw_writel(x, addr)
56 #define __fs_out16(addr, x)     __raw_writew(x, addr)
57 #define __fs_in32(addr) __raw_readl(addr)
58 #define __fs_in16(addr) __raw_readw(addr)
59 #else
60 /* for others play it safe */
61 #define __fs_out32(addr, x)     out_be32(addr, x)
62 #define __fs_out16(addr, x)     out_be16(addr, x)
63 #define __fs_in32(addr) in_be32(addr)
64 #define __fs_in16(addr) in_be16(addr)
65 #endif
66
67 /* write */
68 #define FW(_fecp, _reg, _v) __fs_out32(&(_fecp)->fec_ ## _reg, (_v))
69
70 /* read */
71 #define FR(_fecp, _reg) __fs_in32(&(_fecp)->fec_ ## _reg)
72
73 /* set bits */
74 #define FS(_fecp, _reg, _v) FW(_fecp, _reg, FR(_fecp, _reg) | (_v))
75
76 /* clear bits */
77 #define FC(_fecp, _reg, _v) FW(_fecp, _reg, FR(_fecp, _reg) & ~(_v))
78
79 /*
80  * Delay to wait for FEC reset command to complete (in us)
81  */
82 #define FEC_RESET_DELAY         50
83
84 static int whack_reset(fec_t * fecp)
85 {
86         int i;
87
88         FW(fecp, ecntrl, FEC_ECNTRL_PINMUX | FEC_ECNTRL_RESET);
89         for (i = 0; i < FEC_RESET_DELAY; i++) {
90                 if ((FR(fecp, ecntrl) & FEC_ECNTRL_RESET) == 0)
91                         return 0;       /* OK */
92                 udelay(1);
93         }
94
95         return -1;
96 }
97
98 static int do_pd_setup(struct fs_enet_private *fep)
99 {
100         struct platform_device *pdev = to_platform_device(fep->dev); 
101         struct resource *r;
102         
103         /* Fill out IRQ field */
104         fep->interrupt = platform_get_irq_byname(pdev,"interrupt");
105         if (fep->interrupt < 0)
106                 return -EINVAL;
107
108         r = platform_get_resource_byname(pdev, IORESOURCE_MEM, "regs");
109         fep->fec.fecp = ioremap(r->start, r->end - r->start + 1);
110
111         if(fep->fec.fecp == NULL)
112                 return -EINVAL;
113
114         return 0;
115         
116 }
117
118 #define FEC_NAPI_RX_EVENT_MSK   (FEC_ENET_RXF | FEC_ENET_RXB)
119 #define FEC_RX_EVENT            (FEC_ENET_RXF)
120 #define FEC_TX_EVENT            (FEC_ENET_TXF)
121 #define FEC_ERR_EVENT_MSK       (FEC_ENET_HBERR | FEC_ENET_BABR | \
122                                  FEC_ENET_BABT | FEC_ENET_EBERR)
123
124 static int setup_data(struct net_device *dev)
125 {
126         struct fs_enet_private *fep = netdev_priv(dev);
127
128         if (do_pd_setup(fep) != 0)
129                 return -EINVAL;
130
131         fep->fec.hthi = 0;
132         fep->fec.htlo = 0;
133
134         fep->ev_napi_rx = FEC_NAPI_RX_EVENT_MSK;
135         fep->ev_rx = FEC_RX_EVENT;
136         fep->ev_tx = FEC_TX_EVENT;
137         fep->ev_err = FEC_ERR_EVENT_MSK;
138
139         return 0;
140 }
141
142 static int allocate_bd(struct net_device *dev)
143 {
144         struct fs_enet_private *fep = netdev_priv(dev);
145         const struct fs_platform_info *fpi = fep->fpi;
146         
147         fep->ring_base = dma_alloc_coherent(fep->dev,
148                                             (fpi->tx_ring + fpi->rx_ring) *
149                                             sizeof(cbd_t), &fep->ring_mem_addr,
150                                             GFP_KERNEL);
151         if (fep->ring_base == NULL)
152                 return -ENOMEM;
153
154         return 0;
155 }
156
157 static void free_bd(struct net_device *dev)
158 {
159         struct fs_enet_private *fep = netdev_priv(dev);
160         const struct fs_platform_info *fpi = fep->fpi;
161
162         if(fep->ring_base)
163                 dma_free_coherent(fep->dev, (fpi->tx_ring + fpi->rx_ring)
164                                         * sizeof(cbd_t),
165                                         fep->ring_base,
166                                         fep->ring_mem_addr);
167 }
168
169 static void cleanup_data(struct net_device *dev)
170 {
171         /* nothing */
172 }
173
174 static void set_promiscuous_mode(struct net_device *dev)
175 {
176         struct fs_enet_private *fep = netdev_priv(dev);
177         fec_t *fecp = fep->fec.fecp;
178
179         FS(fecp, r_cntrl, FEC_RCNTRL_PROM);
180 }
181
182 static void set_multicast_start(struct net_device *dev)
183 {
184         struct fs_enet_private *fep = netdev_priv(dev);
185
186         fep->fec.hthi = 0;
187         fep->fec.htlo = 0;
188 }
189
190 static void set_multicast_one(struct net_device *dev, const u8 *mac)
191 {
192         struct fs_enet_private *fep = netdev_priv(dev);
193         int temp, hash_index, i, j;
194         u32 crc, csrVal;
195         u8 byte, msb;
196
197         crc = 0xffffffff;
198         for (i = 0; i < 6; i++) {
199                 byte = mac[i];
200                 for (j = 0; j < 8; j++) {
201                         msb = crc >> 31;
202                         crc <<= 1;
203                         if (msb ^ (byte & 0x1))
204                                 crc ^= FEC_CRC_POLY;
205                         byte >>= 1;
206                 }
207         }
208
209         temp = (crc & 0x3f) >> 1;
210         hash_index = ((temp & 0x01) << 4) |
211                      ((temp & 0x02) << 2) |
212                      ((temp & 0x04)) |
213                      ((temp & 0x08) >> 2) |
214                      ((temp & 0x10) >> 4);
215         csrVal = 1 << hash_index;
216         if (crc & 1)
217                 fep->fec.hthi |= csrVal;
218         else
219                 fep->fec.htlo |= csrVal;
220 }
221
222 static void set_multicast_finish(struct net_device *dev)
223 {
224         struct fs_enet_private *fep = netdev_priv(dev);
225         fec_t *fecp = fep->fec.fecp;
226
227         /* if all multi or too many multicasts; just enable all */
228         if ((dev->flags & IFF_ALLMULTI) != 0 ||
229             dev->mc_count > FEC_MAX_MULTICAST_ADDRS) {
230                 fep->fec.hthi = 0xffffffffU;
231                 fep->fec.htlo = 0xffffffffU;
232         }
233
234         FC(fecp, r_cntrl, FEC_RCNTRL_PROM);
235         FW(fecp, hash_table_high, fep->fec.hthi);
236         FW(fecp, hash_table_low, fep->fec.htlo);
237 }
238
239 static void set_multicast_list(struct net_device *dev)
240 {
241         struct dev_mc_list *pmc;
242
243         if ((dev->flags & IFF_PROMISC) == 0) {
244                 set_multicast_start(dev);
245                 for (pmc = dev->mc_list; pmc != NULL; pmc = pmc->next)
246                         set_multicast_one(dev, pmc->dmi_addr);
247                 set_multicast_finish(dev);
248         } else
249                 set_promiscuous_mode(dev);
250 }
251
252 static void restart(struct net_device *dev)
253 {
254 #ifdef CONFIG_DUET
255         immap_t *immap = fs_enet_immap;
256         u32 cptr;
257 #endif
258         struct fs_enet_private *fep = netdev_priv(dev);
259         fec_t *fecp = fep->fec.fecp;
260         const struct fs_platform_info *fpi = fep->fpi;
261         dma_addr_t rx_bd_base_phys, tx_bd_base_phys;
262         int r;
263         u32 addrhi, addrlo;
264
265         struct mii_bus* mii = fep->phydev->bus;
266         struct fec_info* fec_inf = mii->priv;
267
268         r = whack_reset(fep->fec.fecp);
269         if (r != 0)
270                 printk(KERN_ERR DRV_MODULE_NAME
271                                 ": %s FEC Reset FAILED!\n", dev->name);
272         /*
273          * Set station address.
274          */
275         addrhi = ((u32) dev->dev_addr[0] << 24) |
276                  ((u32) dev->dev_addr[1] << 16) |
277                  ((u32) dev->dev_addr[2] <<  8) |
278                   (u32) dev->dev_addr[3];
279         addrlo = ((u32) dev->dev_addr[4] << 24) |
280                  ((u32) dev->dev_addr[5] << 16);
281         FW(fecp, addr_low, addrhi);
282         FW(fecp, addr_high, addrlo);
283
284         /*
285          * Reset all multicast. 
286          */
287         FW(fecp, hash_table_high, fep->fec.hthi);
288         FW(fecp, hash_table_low, fep->fec.htlo);
289
290         /*
291          * Set maximum receive buffer size. 
292          */
293         FW(fecp, r_buff_size, PKT_MAXBLR_SIZE);
294         FW(fecp, r_hash, PKT_MAXBUF_SIZE);
295
296         /* get physical address */
297         rx_bd_base_phys = fep->ring_mem_addr;
298         tx_bd_base_phys = rx_bd_base_phys + sizeof(cbd_t) * fpi->rx_ring;
299
300         /*
301          * Set receive and transmit descriptor base. 
302          */
303         FW(fecp, r_des_start, rx_bd_base_phys);
304         FW(fecp, x_des_start, tx_bd_base_phys);
305
306         fs_init_bds(dev);
307
308         /*
309          * Enable big endian and don't care about SDMA FC. 
310          */
311         FW(fecp, fun_code, 0x78000000);
312
313         /*
314          * Set MII speed.
315          */
316         FW(fecp, mii_speed, fec_inf->mii_speed);
317
318         /*
319          * Clear any outstanding interrupt.
320          */
321         FW(fecp, ievent, 0xffc0);
322 #ifndef CONFIG_PPC_MERGE
323         FW(fecp, ivec, (fep->interrupt / 2) << 29);
324 #else
325         FW(fecp, ivec, (virq_to_hw(fep->interrupt) / 2) << 29);
326 #endif
327
328         /*
329          * adjust to speed (only for DUET & RMII)
330          */
331 #ifdef CONFIG_DUET
332         if (fpi->use_rmii) {
333                 cptr = in_be32(&immap->im_cpm.cp_cptr);
334                 switch (fs_get_fec_index(fpi->fs_no)) {
335                 case 0:
336                         cptr |= 0x100;
337                         if (fep->speed == 10)
338                                 cptr |= 0x0000010;
339                         else if (fep->speed == 100)
340                                 cptr &= ~0x0000010;
341                         break;
342                 case 1:
343                         cptr |= 0x80;
344                         if (fep->speed == 10)
345                                 cptr |= 0x0000008;
346                         else if (fep->speed == 100)
347                                 cptr &= ~0x0000008;
348                         break;
349                 default:
350                         BUG();  /* should never happen */
351                         break;
352                 }
353                 out_be32(&immap->im_cpm.cp_cptr, cptr);
354         }
355 #endif
356
357
358         FW(fecp, r_cntrl, FEC_RCNTRL_MII_MODE); /* MII enable */
359         /*
360          * adjust to duplex mode
361          */
362         if (fep->phydev->duplex) {
363                 FC(fecp, r_cntrl, FEC_RCNTRL_DRT);
364                 FS(fecp, x_cntrl, FEC_TCNTRL_FDEN);     /* FD enable */
365         } else {
366                 FS(fecp, r_cntrl, FEC_RCNTRL_DRT);
367                 FC(fecp, x_cntrl, FEC_TCNTRL_FDEN);     /* FD disable */
368         }
369
370         /*
371          * Enable interrupts we wish to service. 
372          */
373         FW(fecp, imask, FEC_ENET_TXF | FEC_ENET_TXB |
374            FEC_ENET_RXF | FEC_ENET_RXB);
375
376         /*
377          * And last, enable the transmit and receive processing. 
378          */
379         FW(fecp, ecntrl, FEC_ECNTRL_PINMUX | FEC_ECNTRL_ETHER_EN);
380         FW(fecp, r_des_active, 0x01000000);
381 }
382
383 static void stop(struct net_device *dev)
384 {
385         struct fs_enet_private *fep = netdev_priv(dev);
386         const struct fs_platform_info *fpi = fep->fpi;
387         fec_t *fecp = fep->fec.fecp;
388
389         struct fec_info* feci= fep->phydev->bus->priv;
390
391         int i;
392
393         if ((FR(fecp, ecntrl) & FEC_ECNTRL_ETHER_EN) == 0)
394                 return;         /* already down */
395
396         FW(fecp, x_cntrl, 0x01);        /* Graceful transmit stop */
397         for (i = 0; ((FR(fecp, ievent) & 0x10000000) == 0) &&
398              i < FEC_RESET_DELAY; i++)
399                 udelay(1);
400
401         if (i == FEC_RESET_DELAY)
402                 printk(KERN_WARNING DRV_MODULE_NAME
403                        ": %s FEC timeout on graceful transmit stop\n",
404                        dev->name);
405         /*
406          * Disable FEC. Let only MII interrupts. 
407          */
408         FW(fecp, imask, 0);
409         FC(fecp, ecntrl, FEC_ECNTRL_ETHER_EN);
410
411         fs_cleanup_bds(dev);
412
413         /* shut down FEC1? that's where the mii bus is */
414         if (fpi->has_phy) {
415                 FS(fecp, r_cntrl, FEC_RCNTRL_MII_MODE); /* MII enable */
416                 FS(fecp, ecntrl, FEC_ECNTRL_PINMUX | FEC_ECNTRL_ETHER_EN);
417                 FW(fecp, ievent, FEC_ENET_MII);
418                 FW(fecp, mii_speed, feci->mii_speed);
419         }
420 }
421
422 static void pre_request_irq(struct net_device *dev, int irq)
423 {
424 #ifndef CONFIG_PPC_MERGE
425         immap_t *immap = fs_enet_immap;
426         u32 siel;
427
428         /* SIU interrupt */
429         if (irq >= SIU_IRQ0 && irq < SIU_LEVEL7) {
430
431                 siel = in_be32(&immap->im_siu_conf.sc_siel);
432                 if ((irq & 1) == 0)
433                         siel |= (0x80000000 >> irq);
434                 else
435                         siel &= ~(0x80000000 >> (irq & ~1));
436                 out_be32(&immap->im_siu_conf.sc_siel, siel);
437         }
438 #endif
439 }
440
441 static void post_free_irq(struct net_device *dev, int irq)
442 {
443         /* nothing */
444 }
445
446 static void napi_clear_rx_event(struct net_device *dev)
447 {
448         struct fs_enet_private *fep = netdev_priv(dev);
449         fec_t *fecp = fep->fec.fecp;
450
451         FW(fecp, ievent, FEC_NAPI_RX_EVENT_MSK);
452 }
453
454 static void napi_enable_rx(struct net_device *dev)
455 {
456         struct fs_enet_private *fep = netdev_priv(dev);
457         fec_t *fecp = fep->fec.fecp;
458
459         FS(fecp, imask, FEC_NAPI_RX_EVENT_MSK);
460 }
461
462 static void napi_disable_rx(struct net_device *dev)
463 {
464         struct fs_enet_private *fep = netdev_priv(dev);
465         fec_t *fecp = fep->fec.fecp;
466
467         FC(fecp, imask, FEC_NAPI_RX_EVENT_MSK);
468 }
469
470 static void rx_bd_done(struct net_device *dev)
471 {
472         struct fs_enet_private *fep = netdev_priv(dev);
473         fec_t *fecp = fep->fec.fecp;
474
475         FW(fecp, r_des_active, 0x01000000);
476 }
477
478 static void tx_kickstart(struct net_device *dev)
479 {
480         struct fs_enet_private *fep = netdev_priv(dev);
481         fec_t *fecp = fep->fec.fecp;
482
483         FW(fecp, x_des_active, 0x01000000);
484 }
485
486 static u32 get_int_events(struct net_device *dev)
487 {
488         struct fs_enet_private *fep = netdev_priv(dev);
489         fec_t *fecp = fep->fec.fecp;
490
491         return FR(fecp, ievent) & FR(fecp, imask);
492 }
493
494 static void clear_int_events(struct net_device *dev, u32 int_events)
495 {
496         struct fs_enet_private *fep = netdev_priv(dev);
497         fec_t *fecp = fep->fec.fecp;
498
499         FW(fecp, ievent, int_events);
500 }
501
502 static void ev_error(struct net_device *dev, u32 int_events)
503 {
504         printk(KERN_WARNING DRV_MODULE_NAME
505                ": %s FEC ERROR(s) 0x%x\n", dev->name, int_events);
506 }
507
508 int get_regs(struct net_device *dev, void *p, int *sizep)
509 {
510         struct fs_enet_private *fep = netdev_priv(dev);
511
512         if (*sizep < sizeof(fec_t))
513                 return -EINVAL;
514
515         memcpy_fromio(p, fep->fec.fecp, sizeof(fec_t));
516
517         return 0;
518 }
519
520 int get_regs_len(struct net_device *dev)
521 {
522         return sizeof(fec_t);
523 }
524
525 void tx_restart(struct net_device *dev)
526 {
527         /* nothing */
528 }
529
530 /*************************************************************************/
531
532 const struct fs_ops fs_fec_ops = {
533         .setup_data             = setup_data,
534         .cleanup_data           = cleanup_data,
535         .set_multicast_list     = set_multicast_list,
536         .restart                = restart,
537         .stop                   = stop,
538         .pre_request_irq        = pre_request_irq,
539         .post_free_irq          = post_free_irq,
540         .napi_clear_rx_event    = napi_clear_rx_event,
541         .napi_enable_rx         = napi_enable_rx,
542         .napi_disable_rx        = napi_disable_rx,
543         .rx_bd_done             = rx_bd_done,
544         .tx_kickstart           = tx_kickstart,
545         .get_int_events         = get_int_events,
546         .clear_int_events       = clear_int_events,
547         .ev_error               = ev_error,
548         .get_regs               = get_regs,
549         .get_regs_len           = get_regs_len,
550         .tx_restart             = tx_restart,
551         .allocate_bd            = allocate_bd,
552         .free_bd                = free_bd,
553 };
554