Merge branch 'for-linus' of gregkh@master.kernel.org:/pub/scm/linux/kernel/git/dtor...
[pandora-kernel.git] / drivers / net / fs_enet / mac-fcc.c
1 /*
2  * FCC driver for Motorola MPC82xx (PQ2).
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/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/immap_cpm2.h>
39 #include <asm/mpc8260.h>
40 #include <asm/cpm2.h>
41
42 #include <asm/pgtable.h>
43 #include <asm/irq.h>
44 #include <asm/uaccess.h>
45
46 #include "fs_enet.h"
47
48 /*************************************************/
49
50 /* FCC access macros */
51
52 #define __fcc_out32(addr, x)    out_be32((unsigned *)addr, x)
53 #define __fcc_out16(addr, x)    out_be16((unsigned short *)addr, x)
54 #define __fcc_out8(addr, x)     out_8((unsigned char *)addr, x)
55 #define __fcc_in32(addr)        in_be32((unsigned *)addr)
56 #define __fcc_in16(addr)        in_be16((unsigned short *)addr)
57 #define __fcc_in8(addr)         in_8((unsigned char *)addr)
58
59 /* parameter space */
60
61 /* write, read, set bits, clear bits */
62 #define W32(_p, _m, _v) __fcc_out32(&(_p)->_m, (_v))
63 #define R32(_p, _m)     __fcc_in32(&(_p)->_m)
64 #define S32(_p, _m, _v) W32(_p, _m, R32(_p, _m) | (_v))
65 #define C32(_p, _m, _v) W32(_p, _m, R32(_p, _m) & ~(_v))
66
67 #define W16(_p, _m, _v) __fcc_out16(&(_p)->_m, (_v))
68 #define R16(_p, _m)     __fcc_in16(&(_p)->_m)
69 #define S16(_p, _m, _v) W16(_p, _m, R16(_p, _m) | (_v))
70 #define C16(_p, _m, _v) W16(_p, _m, R16(_p, _m) & ~(_v))
71
72 #define W8(_p, _m, _v)  __fcc_out8(&(_p)->_m, (_v))
73 #define R8(_p, _m)      __fcc_in8(&(_p)->_m)
74 #define S8(_p, _m, _v)  W8(_p, _m, R8(_p, _m) | (_v))
75 #define C8(_p, _m, _v)  W8(_p, _m, R8(_p, _m) & ~(_v))
76
77 /*************************************************/
78
79 #define FCC_MAX_MULTICAST_ADDRS 64
80
81 #define mk_mii_read(REG)        (0x60020000 | ((REG & 0x1f) << 18))
82 #define mk_mii_write(REG, VAL)  (0x50020000 | ((REG & 0x1f) << 18) | (VAL & 0xffff))
83 #define mk_mii_end              0
84
85 #define MAX_CR_CMD_LOOPS        10000
86
87 static inline int fcc_cr_cmd(struct fs_enet_private *fep, u32 mcn, u32 op)
88 {
89         const struct fs_platform_info *fpi = fep->fpi;
90
91         cpm2_map_t *immap = fs_enet_immap;
92         cpm_cpm2_t *cpmp = &immap->im_cpm;
93         u32 v;
94         int i;
95
96         /* Currently I don't know what feature call will look like. But 
97            I guess there'd be something like do_cpm_cmd() which will require page & sblock */
98         v = mk_cr_cmd(fpi->cp_page, fpi->cp_block, mcn, op);
99         W32(cpmp, cp_cpcr, v | CPM_CR_FLG);
100         for (i = 0; i < MAX_CR_CMD_LOOPS; i++)
101                 if ((R32(cpmp, cp_cpcr) & CPM_CR_FLG) == 0)
102                         break;
103
104         if (i >= MAX_CR_CMD_LOOPS) {
105                 printk(KERN_ERR "%s(): Not able to issue CPM command\n",
106                        __FUNCTION__);
107                 return 1;
108         }
109
110         return 0;
111 }
112
113 static int do_pd_setup(struct fs_enet_private *fep)
114 {
115         struct platform_device *pdev = to_platform_device(fep->dev);
116         struct resource *r;
117
118         /* Fill out IRQ field */
119         fep->interrupt = platform_get_irq(pdev, 0);
120         if (fep->interrupt < 0)
121                 return -EINVAL;
122
123         /* Attach the memory for the FCC Parameter RAM */
124         r = platform_get_resource_byname(pdev, IORESOURCE_MEM, "fcc_pram");
125         fep->fcc.ep = (void *)r->start;
126
127         if (fep->fcc.ep == NULL)
128                 return -EINVAL;
129
130         r = platform_get_resource_byname(pdev, IORESOURCE_MEM, "fcc_regs");
131         fep->fcc.fccp = (void *)r->start;
132
133         if (fep->fcc.fccp == NULL)
134                 return -EINVAL;
135
136         fep->fcc.fcccp = (void *)fep->fpi->fcc_regs_c;
137
138         if (fep->fcc.fcccp == NULL)
139                 return -EINVAL;
140
141         return 0;
142 }
143
144 #define FCC_NAPI_RX_EVENT_MSK   (FCC_ENET_RXF | FCC_ENET_RXB)
145 #define FCC_RX_EVENT            (FCC_ENET_RXF)
146 #define FCC_TX_EVENT            (FCC_ENET_TXB)
147 #define FCC_ERR_EVENT_MSK       (FCC_ENET_TXE | FCC_ENET_BSY)
148
149 static int setup_data(struct net_device *dev)
150 {
151         struct fs_enet_private *fep = netdev_priv(dev);
152         const struct fs_platform_info *fpi = fep->fpi;
153
154         fep->fcc.idx = fs_get_fcc_index(fpi->fs_no);
155         if ((unsigned int)fep->fcc.idx >= 3)    /* max 3 FCCs */
156                 return -EINVAL;
157
158         fep->fcc.mem = (void *)fpi->mem_offset;
159
160         if (do_pd_setup(fep) != 0)
161                 return -EINVAL;
162
163         fep->ev_napi_rx = FCC_NAPI_RX_EVENT_MSK;
164         fep->ev_rx = FCC_RX_EVENT;
165         fep->ev_tx = FCC_TX_EVENT;
166         fep->ev_err = FCC_ERR_EVENT_MSK;
167
168         return 0;
169 }
170
171 static int allocate_bd(struct net_device *dev)
172 {
173         struct fs_enet_private *fep = netdev_priv(dev);
174         const struct fs_platform_info *fpi = fep->fpi;
175
176         fep->ring_base = dma_alloc_coherent(fep->dev,
177                                             (fpi->tx_ring + fpi->rx_ring) *
178                                             sizeof(cbd_t), &fep->ring_mem_addr,
179                                             GFP_KERNEL);
180         if (fep->ring_base == NULL)
181                 return -ENOMEM;
182
183         return 0;
184 }
185
186 static void free_bd(struct net_device *dev)
187 {
188         struct fs_enet_private *fep = netdev_priv(dev);
189         const struct fs_platform_info *fpi = fep->fpi;
190
191         if (fep->ring_base)
192                 dma_free_coherent(fep->dev,
193                         (fpi->tx_ring + fpi->rx_ring) * sizeof(cbd_t),
194                         fep->ring_base, fep->ring_mem_addr);
195 }
196
197 static void cleanup_data(struct net_device *dev)
198 {
199         /* nothing */
200 }
201
202 static void set_promiscuous_mode(struct net_device *dev)
203 {
204         struct fs_enet_private *fep = netdev_priv(dev);
205         fcc_t *fccp = fep->fcc.fccp;
206
207         S32(fccp, fcc_fpsmr, FCC_PSMR_PRO);
208 }
209
210 static void set_multicast_start(struct net_device *dev)
211 {
212         struct fs_enet_private *fep = netdev_priv(dev);
213         fcc_enet_t *ep = fep->fcc.ep;
214
215         W32(ep, fen_gaddrh, 0);
216         W32(ep, fen_gaddrl, 0);
217 }
218
219 static void set_multicast_one(struct net_device *dev, const u8 *mac)
220 {
221         struct fs_enet_private *fep = netdev_priv(dev);
222         fcc_enet_t *ep = fep->fcc.ep;
223         u16 taddrh, taddrm, taddrl;
224
225         taddrh = ((u16)mac[5] << 8) | mac[4];
226         taddrm = ((u16)mac[3] << 8) | mac[2];
227         taddrl = ((u16)mac[1] << 8) | mac[0];
228
229         W16(ep, fen_taddrh, taddrh);
230         W16(ep, fen_taddrm, taddrm);
231         W16(ep, fen_taddrl, taddrl);
232         fcc_cr_cmd(fep, 0x0C, CPM_CR_SET_GADDR);
233 }
234
235 static void set_multicast_finish(struct net_device *dev)
236 {
237         struct fs_enet_private *fep = netdev_priv(dev);
238         fcc_t *fccp = fep->fcc.fccp;
239         fcc_enet_t *ep = fep->fcc.ep;
240
241         /* clear promiscuous always */
242         C32(fccp, fcc_fpsmr, FCC_PSMR_PRO);
243
244         /* if all multi or too many multicasts; just enable all */
245         if ((dev->flags & IFF_ALLMULTI) != 0 ||
246             dev->mc_count > FCC_MAX_MULTICAST_ADDRS) {
247
248                 W32(ep, fen_gaddrh, 0xffffffff);
249                 W32(ep, fen_gaddrl, 0xffffffff);
250         }
251
252         /* read back */
253         fep->fcc.gaddrh = R32(ep, fen_gaddrh);
254         fep->fcc.gaddrl = R32(ep, fen_gaddrl);
255 }
256
257 static void set_multicast_list(struct net_device *dev)
258 {
259         struct dev_mc_list *pmc;
260
261         if ((dev->flags & IFF_PROMISC) == 0) {
262                 set_multicast_start(dev);
263                 for (pmc = dev->mc_list; pmc != NULL; pmc = pmc->next)
264                         set_multicast_one(dev, pmc->dmi_addr);
265                 set_multicast_finish(dev);
266         } else
267                 set_promiscuous_mode(dev);
268 }
269
270 static void restart(struct net_device *dev)
271 {
272         struct fs_enet_private *fep = netdev_priv(dev);
273         const struct fs_platform_info *fpi = fep->fpi;
274         fcc_t *fccp = fep->fcc.fccp;
275         fcc_c_t *fcccp = fep->fcc.fcccp;
276         fcc_enet_t *ep = fep->fcc.ep;
277         dma_addr_t rx_bd_base_phys, tx_bd_base_phys;
278         u16 paddrh, paddrm, paddrl;
279         u16 mem_addr;
280         const unsigned char *mac;
281         int i;
282
283         C32(fccp, fcc_gfmr, FCC_GFMR_ENR | FCC_GFMR_ENT);
284
285         /* clear everything (slow & steady does it) */
286         for (i = 0; i < sizeof(*ep); i++)
287                 __fcc_out8((char *)ep + i, 0);
288
289         /* get physical address */
290         rx_bd_base_phys = fep->ring_mem_addr;
291         tx_bd_base_phys = rx_bd_base_phys + sizeof(cbd_t) * fpi->rx_ring;
292
293         /* point to bds */
294         W32(ep, fen_genfcc.fcc_rbase, rx_bd_base_phys);
295         W32(ep, fen_genfcc.fcc_tbase, tx_bd_base_phys);
296
297         /* Set maximum bytes per receive buffer.
298          * It must be a multiple of 32.
299          */
300         W16(ep, fen_genfcc.fcc_mrblr, PKT_MAXBLR_SIZE);
301
302         W32(ep, fen_genfcc.fcc_rstate, (CPMFCR_GBL | CPMFCR_EB) << 24);
303         W32(ep, fen_genfcc.fcc_tstate, (CPMFCR_GBL | CPMFCR_EB) << 24);
304
305         /* Allocate space in the reserved FCC area of DPRAM for the
306          * internal buffers.  No one uses this space (yet), so we
307          * can do this.  Later, we will add resource management for
308          * this area.
309          */
310
311         mem_addr = (u32) fep->fcc.mem;  /* de-fixup dpram offset */
312
313         W16(ep, fen_genfcc.fcc_riptr, (mem_addr & 0xffff));
314         W16(ep, fen_genfcc.fcc_tiptr, ((mem_addr + 32) & 0xffff));
315         W16(ep, fen_padptr, mem_addr + 64);
316
317         /* fill with special symbol...  */
318         memset(fep->fcc.mem + fpi->dpram_offset + 64, 0x88, 32);
319
320         W32(ep, fen_genfcc.fcc_rbptr, 0);
321         W32(ep, fen_genfcc.fcc_tbptr, 0);
322         W32(ep, fen_genfcc.fcc_rcrc, 0);
323         W32(ep, fen_genfcc.fcc_tcrc, 0);
324         W16(ep, fen_genfcc.fcc_res1, 0);
325         W32(ep, fen_genfcc.fcc_res2, 0);
326
327         /* no CAM */
328         W32(ep, fen_camptr, 0);
329
330         /* Set CRC preset and mask */
331         W32(ep, fen_cmask, 0xdebb20e3);
332         W32(ep, fen_cpres, 0xffffffff);
333
334         W32(ep, fen_crcec, 0);          /* CRC Error counter       */
335         W32(ep, fen_alec, 0);           /* alignment error counter */
336         W32(ep, fen_disfc, 0);          /* discard frame counter   */
337         W16(ep, fen_retlim, 15);        /* Retry limit threshold   */
338         W16(ep, fen_pper, 0);           /* Normal persistence      */
339
340         /* set group address */
341         W32(ep, fen_gaddrh, fep->fcc.gaddrh);
342         W32(ep, fen_gaddrl, fep->fcc.gaddrh);
343
344         /* Clear hash filter tables */
345         W32(ep, fen_iaddrh, 0);
346         W32(ep, fen_iaddrl, 0);
347
348         /* Clear the Out-of-sequence TxBD  */
349         W16(ep, fen_tfcstat, 0);
350         W16(ep, fen_tfclen, 0);
351         W32(ep, fen_tfcptr, 0);
352
353         W16(ep, fen_mflr, PKT_MAXBUF_SIZE);     /* maximum frame length register */
354         W16(ep, fen_minflr, PKT_MINBUF_SIZE);   /* minimum frame length register */
355
356         /* set address */
357         mac = dev->dev_addr;
358         paddrh = ((u16)mac[5] << 8) | mac[4];
359         paddrm = ((u16)mac[3] << 8) | mac[2];
360         paddrl = ((u16)mac[1] << 8) | mac[0];
361
362         W16(ep, fen_paddrh, paddrh);
363         W16(ep, fen_paddrm, paddrm);
364         W16(ep, fen_paddrl, paddrl);
365
366         W16(ep, fen_taddrh, 0);
367         W16(ep, fen_taddrm, 0);
368         W16(ep, fen_taddrl, 0);
369
370         W16(ep, fen_maxd1, 1520);       /* maximum DMA1 length */
371         W16(ep, fen_maxd2, 1520);       /* maximum DMA2 length */
372
373         /* Clear stat counters, in case we ever enable RMON */
374         W32(ep, fen_octc, 0);
375         W32(ep, fen_colc, 0);
376         W32(ep, fen_broc, 0);
377         W32(ep, fen_mulc, 0);
378         W32(ep, fen_uspc, 0);
379         W32(ep, fen_frgc, 0);
380         W32(ep, fen_ospc, 0);
381         W32(ep, fen_jbrc, 0);
382         W32(ep, fen_p64c, 0);
383         W32(ep, fen_p65c, 0);
384         W32(ep, fen_p128c, 0);
385         W32(ep, fen_p256c, 0);
386         W32(ep, fen_p512c, 0);
387         W32(ep, fen_p1024c, 0);
388
389         W16(ep, fen_rfthr, 0);  /* Suggested by manual */
390         W16(ep, fen_rfcnt, 0);
391         W16(ep, fen_cftype, 0);
392
393         fs_init_bds(dev);
394
395         /* adjust to speed (for RMII mode) */
396         if (fpi->use_rmii) {
397                 if (fep->speed == 100)
398                         C8(fcccp, fcc_gfemr, 0x20);
399                 else
400                         S8(fcccp, fcc_gfemr, 0x20);
401         }
402
403         fcc_cr_cmd(fep, 0x0c, CPM_CR_INIT_TRX);
404
405         /* clear events */
406         W16(fccp, fcc_fcce, 0xffff);
407
408         /* Enable interrupts we wish to service */
409         W16(fccp, fcc_fccm, FCC_ENET_TXE | FCC_ENET_RXF | FCC_ENET_TXB);
410
411         /* Set GFMR to enable Ethernet operating mode */
412         W32(fccp, fcc_gfmr, FCC_GFMR_TCI | FCC_GFMR_MODE_ENET);
413
414         /* set sync/delimiters */
415         W16(fccp, fcc_fdsr, 0xd555);
416
417         W32(fccp, fcc_fpsmr, FCC_PSMR_ENCRC);
418
419         if (fpi->use_rmii)
420                 S32(fccp, fcc_fpsmr, FCC_PSMR_RMII);
421
422         /* adjust to duplex mode */
423         if (fep->duplex)
424                 S32(fccp, fcc_fpsmr, FCC_PSMR_FDE | FCC_PSMR_LPB);
425         else
426                 C32(fccp, fcc_fpsmr, FCC_PSMR_FDE | FCC_PSMR_LPB);
427
428         S32(fccp, fcc_gfmr, FCC_GFMR_ENR | FCC_GFMR_ENT);
429 }
430
431 static void stop(struct net_device *dev)
432 {
433         struct fs_enet_private *fep = netdev_priv(dev);
434         fcc_t *fccp = fep->fcc.fccp;
435
436         /* stop ethernet */
437         C32(fccp, fcc_gfmr, FCC_GFMR_ENR | FCC_GFMR_ENT);
438
439         /* clear events */
440         W16(fccp, fcc_fcce, 0xffff);
441
442         /* clear interrupt mask */
443         W16(fccp, fcc_fccm, 0);
444
445         fs_cleanup_bds(dev);
446 }
447
448 static void pre_request_irq(struct net_device *dev, int irq)
449 {
450         /* nothing */
451 }
452
453 static void post_free_irq(struct net_device *dev, int irq)
454 {
455         /* nothing */
456 }
457
458 static void napi_clear_rx_event(struct net_device *dev)
459 {
460         struct fs_enet_private *fep = netdev_priv(dev);
461         fcc_t *fccp = fep->fcc.fccp;
462
463         W16(fccp, fcc_fcce, FCC_NAPI_RX_EVENT_MSK);
464 }
465
466 static void napi_enable_rx(struct net_device *dev)
467 {
468         struct fs_enet_private *fep = netdev_priv(dev);
469         fcc_t *fccp = fep->fcc.fccp;
470
471         S16(fccp, fcc_fccm, FCC_NAPI_RX_EVENT_MSK);
472 }
473
474 static void napi_disable_rx(struct net_device *dev)
475 {
476         struct fs_enet_private *fep = netdev_priv(dev);
477         fcc_t *fccp = fep->fcc.fccp;
478
479         C16(fccp, fcc_fccm, FCC_NAPI_RX_EVENT_MSK);
480 }
481
482 static void rx_bd_done(struct net_device *dev)
483 {
484         /* nothing */
485 }
486
487 static void tx_kickstart(struct net_device *dev)
488 {
489         /* nothing */
490 }
491
492 static u32 get_int_events(struct net_device *dev)
493 {
494         struct fs_enet_private *fep = netdev_priv(dev);
495         fcc_t *fccp = fep->fcc.fccp;
496
497         return (u32)R16(fccp, fcc_fcce);
498 }
499
500 static void clear_int_events(struct net_device *dev, u32 int_events)
501 {
502         struct fs_enet_private *fep = netdev_priv(dev);
503         fcc_t *fccp = fep->fcc.fccp;
504
505         W16(fccp, fcc_fcce, int_events & 0xffff);
506 }
507
508 static void ev_error(struct net_device *dev, u32 int_events)
509 {
510         printk(KERN_WARNING DRV_MODULE_NAME
511                ": %s FS_ENET ERROR(s) 0x%x\n", dev->name, int_events);
512 }
513
514 int get_regs(struct net_device *dev, void *p, int *sizep)
515 {
516         struct fs_enet_private *fep = netdev_priv(dev);
517
518         if (*sizep < sizeof(fcc_t) + sizeof(fcc_c_t) + sizeof(fcc_enet_t))
519                 return -EINVAL;
520
521         memcpy_fromio(p, fep->fcc.fccp, sizeof(fcc_t));
522         p = (char *)p + sizeof(fcc_t);
523
524         memcpy_fromio(p, fep->fcc.fcccp, sizeof(fcc_c_t));
525         p = (char *)p + sizeof(fcc_c_t);
526
527         memcpy_fromio(p, fep->fcc.ep, sizeof(fcc_enet_t));
528
529         return 0;
530 }
531
532 int get_regs_len(struct net_device *dev)
533 {
534         return sizeof(fcc_t) + sizeof(fcc_c_t) + sizeof(fcc_enet_t);
535 }
536
537 /* Some transmit errors cause the transmitter to shut
538  * down.  We now issue a restart transmit.  Since the
539  * errors close the BD and update the pointers, the restart
540  * _should_ pick up without having to reset any of our
541  * pointers either.  Also, To workaround 8260 device erratum 
542  * CPM37, we must disable and then re-enable the transmitter
543  * following a Late Collision, Underrun, or Retry Limit error.
544  */
545 void tx_restart(struct net_device *dev)
546 {
547         struct fs_enet_private *fep = netdev_priv(dev);
548         fcc_t *fccp = fep->fcc.fccp;
549
550         C32(fccp, fcc_gfmr, FCC_GFMR_ENT);
551         udelay(10);
552         S32(fccp, fcc_gfmr, FCC_GFMR_ENT);
553
554         fcc_cr_cmd(fep, 0x0C, CPM_CR_RESTART_TX);
555 }
556
557 /*************************************************************************/
558
559 const struct fs_ops fs_fcc_ops = {
560         .setup_data             = setup_data,
561         .cleanup_data           = cleanup_data,
562         .set_multicast_list     = set_multicast_list,
563         .restart                = restart,
564         .stop                   = stop,
565         .pre_request_irq        = pre_request_irq,
566         .post_free_irq          = post_free_irq,
567         .napi_clear_rx_event    = napi_clear_rx_event,
568         .napi_enable_rx         = napi_enable_rx,
569         .napi_disable_rx        = napi_disable_rx,
570         .rx_bd_done             = rx_bd_done,
571         .tx_kickstart           = tx_kickstart,
572         .get_int_events         = get_int_events,
573         .clear_int_events       = clear_int_events,
574         .ev_error               = ev_error,
575         .get_regs               = get_regs,
576         .get_regs_len           = get_regs_len,
577         .tx_restart             = tx_restart,
578         .allocate_bd            = allocate_bd,
579         .free_bd                = free_bd,
580 };