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