TEXT_BASE is in board/sandpoint/config.mk so say so...
[u-boot.git] / cpu / mpc8260 / ether_fcc.c
1 /*
2  * MPC8260 FCC Fast Ethernet
3  *
4  * Copyright (c) 2000 MontaVista Software, Inc.   Dan Malek (dmalek@jlc.net)
5  *
6  * (C) Copyright 2000 Sysgo Real-Time Solutions, GmbH <www.elinos.com>
7  * Marius Groeger <mgroeger@sysgo.de>
8  *
9  * See file CREDITS for list of people who contributed to this
10  * project.
11  *
12  * This program is free software; you can redistribute it and/or
13  * modify it under the terms of the GNU General Public License as
14  * published by the Free Software Foundation; either version 2 of
15  * the License, or (at your option) any later version.
16  *
17  * This program is distributed in the hope that it will be useful,
18  * but WITHOUT ANY WARRANTY; without even the implied warranty of
19  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20  * GNU General Public License for more details.
21  *
22  * You should have received a copy of the GNU General Public License
23  * along with this program; if not, write to the Free Software
24  * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
25  * MA 02111-1307 USA
26  */
27
28 /*
29  * MPC8260 FCC Fast Ethernet
30  * Basic ET HW initialization and packet RX/TX routines
31  *
32  * This code will not perform the IO port configuration. This should be
33  * done in the iop_conf_t structure specific for the board.
34  *
35  * TODO:
36  * add a PHY driver to do the negotiation
37  * reflect negotiation results in FPSMR
38  * look for ways to configure the board specific stuff elsewhere, eg.
39  *    config_xxx.h or the board directory
40  */
41
42 #include <common.h>
43 #include <malloc.h>
44 #include <asm/cpm_8260.h>
45 #include <mpc8260.h>
46 #include <command.h>
47 #include <config.h>
48 #include <net.h>
49
50 #if defined(CONFIG_MII) || (CONFIG_COMMANDS & CFG_CMD_MII)
51 #include <miiphy.h>
52 #endif
53
54 #if defined(CONFIG_ETHER_ON_FCC) && (CONFIG_COMMANDS & CFG_CMD_NET) && \
55         defined(CONFIG_NET_MULTI)
56
57 static struct ether_fcc_info_s
58 {
59         int ether_index;
60         int proff_enet;
61         ulong cpm_cr_enet_sblock;
62         ulong cpm_cr_enet_page;
63         ulong cmxfcr_mask;
64         ulong cmxfcr_value;
65 }
66         ether_fcc_info[] =
67 {
68 #ifdef CONFIG_ETHER_ON_FCC1
69 {
70         0,
71         PROFF_FCC1,
72         CPM_CR_FCC1_SBLOCK,
73         CPM_CR_FCC1_PAGE,
74         CFG_CMXFCR_MASK1,
75         CFG_CMXFCR_VALUE1
76 },
77 #endif
78
79 #ifdef CONFIG_ETHER_ON_FCC2
80 {
81         1,
82         PROFF_FCC2,
83         CPM_CR_FCC2_SBLOCK,
84         CPM_CR_FCC2_PAGE,
85         CFG_CMXFCR_MASK2,
86         CFG_CMXFCR_VALUE2
87 },
88 #endif
89
90 #ifdef CONFIG_ETHER_ON_FCC3
91 {
92         2,
93         PROFF_FCC3,
94         CPM_CR_FCC3_SBLOCK,
95         CPM_CR_FCC3_PAGE,
96         CFG_CMXFCR_MASK3,
97         CFG_CMXFCR_VALUE3
98 },
99 #endif
100 };
101
102 /*---------------------------------------------------------------------*/
103
104 /* Maximum input DMA size.  Must be a should(?) be a multiple of 4. */
105 #define PKT_MAXDMA_SIZE         1520
106
107 /* The FCC stores dest/src/type, data, and checksum for receive packets. */
108 #define PKT_MAXBUF_SIZE         1518
109 #define PKT_MINBUF_SIZE         64
110
111 /* Maximum input buffer size.  Must be a multiple of 32. */
112 #define PKT_MAXBLR_SIZE         1536
113
114 #define TOUT_LOOP 1000000
115
116 #define TX_BUF_CNT 2
117 #ifdef __GNUC__
118 static char txbuf[TX_BUF_CNT][PKT_MAXBLR_SIZE] __attribute__ ((aligned(8)));
119 #else
120 #error "txbuf must be 64-bit aligned"
121 #endif
122
123 static uint rxIdx;      /* index of the current RX buffer */
124 static uint txIdx;      /* index of the current TX buffer */
125
126 /*
127  * FCC Ethernet Tx and Rx buffer descriptors.
128  * Provide for Double Buffering
129  * Note: PKTBUFSRX is defined in net.h
130  */
131
132 typedef volatile struct rtxbd {
133     cbd_t rxbd[PKTBUFSRX];
134     cbd_t txbd[TX_BUF_CNT];
135 } RTXBD;
136
137 /*  Good news: the FCC supports external BDs! */
138 #ifdef __GNUC__
139 static RTXBD rtx __attribute__ ((aligned(8)));
140 #else
141 #error "rtx must be 64-bit aligned"
142 #endif
143
144 static int fec_send(struct eth_device* dev, volatile void *packet, int length)
145 {
146     int i;
147     int result = 0;
148
149     if (length <= 0) {
150         printf("fec: bad packet size: %d\n", length);
151         goto out;
152     }
153
154     for(i=0; rtx.txbd[txIdx].cbd_sc & BD_ENET_TX_READY; i++) {
155         if (i >= TOUT_LOOP) {
156             puts ("fec: tx buffer not ready\n");
157             goto out;
158         }
159     }
160
161     rtx.txbd[txIdx].cbd_bufaddr = (uint)packet;
162     rtx.txbd[txIdx].cbd_datlen = length;
163     rtx.txbd[txIdx].cbd_sc |= (BD_ENET_TX_READY | BD_ENET_TX_LAST |
164                                BD_ENET_TX_WRAP);
165
166     for(i=0; rtx.txbd[txIdx].cbd_sc & BD_ENET_TX_READY; i++) {
167         if (i >= TOUT_LOOP) {
168             puts ("fec: tx error\n");
169             goto out;
170         }
171     }
172
173 #ifdef ET_DEBUG
174     printf("cycles: %d status: %04x\n", i, rtx.txbd[txIdx].cbd_sc);
175 #endif
176
177     /* return only status bits */
178     result = rtx.txbd[txIdx].cbd_sc & BD_ENET_TX_STATS;
179
180 out:
181     return result;
182 }
183
184 static int fec_recv(struct eth_device* dev)
185 {
186     int length;
187
188     for (;;)
189     {
190         if (rtx.rxbd[rxIdx].cbd_sc & BD_ENET_RX_EMPTY) {
191             length = -1;
192             break;     /* nothing received - leave for() loop */
193         }
194         length = rtx.rxbd[rxIdx].cbd_datlen;
195
196         if (rtx.rxbd[rxIdx].cbd_sc & 0x003f) {
197             printf("fec: rx error %04x\n", rtx.rxbd[rxIdx].cbd_sc);
198         }
199         else {
200             /* Pass the packet up to the protocol layers. */
201             NetReceive(NetRxPackets[rxIdx], length - 4);
202         }
203
204
205         /* Give the buffer back to the FCC. */
206         rtx.rxbd[rxIdx].cbd_datlen = 0;
207
208         /* wrap around buffer index when necessary */
209         if ((rxIdx + 1) >= PKTBUFSRX) {
210             rtx.rxbd[PKTBUFSRX - 1].cbd_sc = (BD_ENET_RX_WRAP | BD_ENET_RX_EMPTY);
211             rxIdx = 0;
212         }
213         else {
214             rtx.rxbd[rxIdx].cbd_sc = BD_ENET_RX_EMPTY;
215             rxIdx++;
216         }
217     }
218     return length;
219 }
220
221
222 static int fec_init(struct eth_device* dev, bd_t *bis)
223 {
224     struct ether_fcc_info_s * info = dev->priv;
225     int i;
226     volatile immap_t *immr = (immap_t *)CFG_IMMR;
227     volatile cpm8260_t *cp = &(immr->im_cpm);
228     fcc_enet_t *pram_ptr;
229     unsigned long mem_addr;
230
231 #if 0
232     mii_discover_phy();
233 #endif
234
235     /* 28.9 - (1-2): ioports have been set up already */
236
237     /* 28.9 - (3): connect FCC's tx and rx clocks */
238     immr->im_cpmux.cmx_uar = 0;
239     immr->im_cpmux.cmx_fcr = (immr->im_cpmux.cmx_fcr & ~info->cmxfcr_mask) |
240                                                         info->cmxfcr_value;
241
242     /* 28.9 - (4): GFMR: disable tx/rx, CCITT CRC, Mode Ethernet */
243     immr->im_fcc[info->ether_index].fcc_gfmr =
244       FCC_GFMR_MODE_ENET | FCC_GFMR_TCRC_32;
245
246     /* 28.9 - (5): FPSMR: enable full duplex, select CCITT CRC for Ethernet */
247     immr->im_fcc[info->ether_index].fcc_fpsmr = CFG_FCC_PSMR | FCC_PSMR_ENCRC;
248
249     /* 28.9 - (6): FDSR: Ethernet Syn */
250     immr->im_fcc[info->ether_index].fcc_fdsr = 0xD555;
251
252     /* reset indeces to current rx/tx bd (see eth_send()/eth_rx()) */
253     rxIdx = 0;
254     txIdx = 0;
255
256     /* Setup Receiver Buffer Descriptors */
257     for (i = 0; i < PKTBUFSRX; i++)
258     {
259       rtx.rxbd[i].cbd_sc = BD_ENET_RX_EMPTY;
260       rtx.rxbd[i].cbd_datlen = 0;
261       rtx.rxbd[i].cbd_bufaddr = (uint)NetRxPackets[i];
262     }
263     rtx.rxbd[PKTBUFSRX - 1].cbd_sc |= BD_ENET_RX_WRAP;
264
265     /* Setup Ethernet Transmitter Buffer Descriptors */
266     for (i = 0; i < TX_BUF_CNT; i++)
267     {
268       rtx.txbd[i].cbd_sc = (BD_ENET_TX_PAD | BD_ENET_TX_LAST | BD_ENET_TX_TC);
269       rtx.txbd[i].cbd_datlen = 0;
270       rtx.txbd[i].cbd_bufaddr = (uint)&txbuf[i][0];
271     }
272     rtx.txbd[TX_BUF_CNT - 1].cbd_sc |= BD_ENET_TX_WRAP;
273
274     /* 28.9 - (7): initialise parameter ram */
275     pram_ptr = (fcc_enet_t *)&(immr->im_dprambase[info->proff_enet]);
276
277     /* clear whole structure to make sure all reserved fields are zero */
278     memset((void*)pram_ptr, 0, sizeof(fcc_enet_t));
279
280     /*
281      * common Parameter RAM area
282      *
283      * Allocate space in the reserved FCC area of DPRAM for the
284      * internal buffers.  No one uses this space (yet), so we
285      * can do this.  Later, we will add resource management for
286      * this area.
287      */
288     mem_addr = CPM_FCC_SPECIAL_BASE + ((info->ether_index) * 64);
289     pram_ptr->fen_genfcc.fcc_riptr = mem_addr;
290     pram_ptr->fen_genfcc.fcc_tiptr = mem_addr+32;
291     /*
292      * Set maximum bytes per receive buffer.
293      * It must be a multiple of 32.
294      */
295     pram_ptr->fen_genfcc.fcc_mrblr = PKT_MAXBLR_SIZE;
296     pram_ptr->fen_genfcc.fcc_rstate = (CPMFCR_GBL | CPMFCR_EB |
297                                        CFG_CPMFCR_RAMTYPE) << 24;
298     pram_ptr->fen_genfcc.fcc_rbase = (unsigned int)(&rtx.rxbd[rxIdx]);
299     pram_ptr->fen_genfcc.fcc_tstate = (CPMFCR_GBL | CPMFCR_EB |
300                                        CFG_CPMFCR_RAMTYPE) << 24;
301     pram_ptr->fen_genfcc.fcc_tbase = (unsigned int)(&rtx.txbd[txIdx]);
302
303     /* protocol-specific area */
304     pram_ptr->fen_cmask = 0xdebb20e3;   /* CRC mask */
305     pram_ptr->fen_cpres = 0xffffffff;   /* CRC preset */
306     pram_ptr->fen_retlim = 15;          /* Retry limit threshold */
307     pram_ptr->fen_mflr = PKT_MAXBUF_SIZE;   /* maximum frame length register */
308     /*
309      * Set Ethernet station address.
310      *
311      * This is supplied in the board information structure, so we
312      * copy that into the controller.
313      * So, far we have only been given one Ethernet address. We make
314      * it unique by setting a few bits in the upper byte of the
315      * non-static part of the address.
316      */
317 #define ea eth_get_dev()->enetaddr
318     pram_ptr->fen_paddrh = (ea[5] << 8) + ea[4];
319     pram_ptr->fen_paddrm = (ea[3] << 8) + ea[2];
320     pram_ptr->fen_paddrl = (ea[1] << 8) + ea[0];
321 #undef ea
322     pram_ptr->fen_minflr = PKT_MINBUF_SIZE; /* minimum frame length register */
323     /* pad pointer. use tiptr since we don't need a specific padding char */
324     pram_ptr->fen_padptr = pram_ptr->fen_genfcc.fcc_tiptr;
325     pram_ptr->fen_maxd1 = PKT_MAXDMA_SIZE;      /* maximum DMA1 length */
326     pram_ptr->fen_maxd2 = PKT_MAXDMA_SIZE;      /* maximum DMA2 length */
327     pram_ptr->fen_rfthr = 1;
328     pram_ptr->fen_rfcnt = 1;
329 #if 0
330     printf("pram_ptr->fen_genfcc.fcc_rbase %08lx\n",
331         pram_ptr->fen_genfcc.fcc_rbase);
332     printf("pram_ptr->fen_genfcc.fcc_tbase %08lx\n",
333         pram_ptr->fen_genfcc.fcc_tbase);
334 #endif
335
336     /* 28.9 - (8): clear out events in FCCE */
337     immr->im_fcc[info->ether_index].fcc_fcce = ~0x0;
338
339     /* 28.9 - (9): FCCM: mask all events */
340     immr->im_fcc[info->ether_index].fcc_fccm = 0;
341
342     /* 28.9 - (10-12): we don't use ethernet interrupts */
343
344     /* 28.9 - (13)
345      *
346      * Let's re-initialize the channel now.  We have to do it later
347      * than the manual describes because we have just now finished
348      * the BD initialization.
349      */
350     cp->cp_cpcr = mk_cr_cmd(info->cpm_cr_enet_page,
351                             info->cpm_cr_enet_sblock,
352                             0x0c,
353                             CPM_CR_INIT_TRX) | CPM_CR_FLG;
354     do {
355         __asm__ __volatile__ ("eieio");
356     } while (cp->cp_cpcr & CPM_CR_FLG);
357
358     /* 28.9 - (14): enable tx/rx in gfmr */
359     immr->im_fcc[info->ether_index].fcc_gfmr |= FCC_GFMR_ENT | FCC_GFMR_ENR;
360
361     return 1;
362 }
363
364 static void fec_halt(struct eth_device* dev)
365 {
366     struct ether_fcc_info_s * info = dev->priv;
367     volatile immap_t *immr = (immap_t *)CFG_IMMR;
368
369     /* write GFMR: disable tx/rx */
370     immr->im_fcc[info->ether_index].fcc_gfmr &=
371                                                 ~(FCC_GFMR_ENT | FCC_GFMR_ENR);
372 }
373
374 int fec_initialize(bd_t *bis)
375 {
376         struct eth_device* dev;
377         int i;
378
379         for (i = 0; i < sizeof(ether_fcc_info) / sizeof(ether_fcc_info[0]); i++)
380         {
381                 dev = (struct eth_device*) malloc(sizeof *dev);
382                 memset(dev, 0, sizeof *dev);
383
384                 sprintf(dev->name, "FCC%d ETHERNET",
385                         ether_fcc_info[i].ether_index + 1);
386                 dev->priv   = &ether_fcc_info[i];
387                 dev->init   = fec_init;
388                 dev->halt   = fec_halt;
389                 dev->send   = fec_send;
390                 dev->recv   = fec_recv;
391
392                 eth_register(dev);
393
394 #if (defined(CONFIG_MII) || (CONFIG_COMMANDS & CFG_CMD_MII)) \
395                 && defined(CONFIG_BITBANGMII)
396                 miiphy_register(dev->name,
397                                 bb_miiphy_read, bb_miiphy_write);
398 #endif
399         }
400
401         return 1;
402 }
403
404 #ifdef CONFIG_ETHER_LOOPBACK_TEST
405
406 #define ELBT_BUFSZ      1024    /* must be multiple of 32 */
407
408 #define ELBT_CRCSZ      4
409
410 #define ELBT_NRXBD      4       /* must be at least 2 */
411 #define ELBT_NTXBD      4
412
413 #define ELBT_MAXRXERR   32
414 #define ELBT_MAXTXERR   32
415
416 #define ELBT_CLSWAIT    1000    /* msec to wait for further input frames */
417
418 typedef
419         struct {
420                 uint off;
421                 char *lab;
422         }
423 elbt_prdesc;
424
425 typedef
426         struct {
427                 uint _l, _f, m, bc, mc, lg, no, sh, cr, ov, cl;
428                 uint badsrc, badtyp, badlen, badbit;
429         }
430 elbt_rxeacc;
431
432 static elbt_prdesc rxeacc_descs[] = {
433         { offsetof(elbt_rxeacc, _l),            "Not Last in Frame"     },
434         { offsetof(elbt_rxeacc, _f),            "Not First in Frame"    },
435         { offsetof(elbt_rxeacc, m),             "Address Miss"          },
436         { offsetof(elbt_rxeacc, bc),            "Broadcast Address"     },
437         { offsetof(elbt_rxeacc, mc),            "Multicast Address"     },
438         { offsetof(elbt_rxeacc, lg),            "Frame Length Violation"},
439         { offsetof(elbt_rxeacc, no),            "Non-Octet Alignment"   },
440         { offsetof(elbt_rxeacc, sh),            "Short Frame"           },
441         { offsetof(elbt_rxeacc, cr),            "CRC Error"             },
442         { offsetof(elbt_rxeacc, ov),            "Overrun"               },
443         { offsetof(elbt_rxeacc, cl),            "Collision"             },
444         { offsetof(elbt_rxeacc, badsrc),        "Bad Src Address"       },
445         { offsetof(elbt_rxeacc, badtyp),        "Bad Frame Type"        },
446         { offsetof(elbt_rxeacc, badlen),        "Bad Frame Length"      },
447         { offsetof(elbt_rxeacc, badbit),        "Data Compare Errors"   },
448 };
449 static int rxeacc_ndesc = sizeof (rxeacc_descs) / sizeof (rxeacc_descs[0]);
450
451 typedef
452         struct {
453                 uint def, hb, lc, rl, rc, un, csl;
454         }
455 elbt_txeacc;
456
457 static elbt_prdesc txeacc_descs[] = {
458         { offsetof(elbt_txeacc, def),           "Defer Indication"      },
459         { offsetof(elbt_txeacc, hb),            "Heartbeat"             },
460         { offsetof(elbt_txeacc, lc),            "Late Collision"        },
461         { offsetof(elbt_txeacc, rl),            "Retransmission Limit"  },
462         { offsetof(elbt_txeacc, rc),            "Retry Count"           },
463         { offsetof(elbt_txeacc, un),            "Underrun"              },
464         { offsetof(elbt_txeacc, csl),           "Carrier Sense Lost"    },
465 };
466 static int txeacc_ndesc = sizeof (txeacc_descs) / sizeof (txeacc_descs[0]);
467
468 typedef
469         struct {
470                 uchar rxbufs[ELBT_NRXBD][ELBT_BUFSZ];
471                 uchar txbufs[ELBT_NTXBD][ELBT_BUFSZ];
472                 cbd_t rxbd[ELBT_NRXBD];
473                 cbd_t txbd[ELBT_NTXBD];
474                 enum { Idle, Running, Closing, Closed } state;
475                 int proff, page, sblock;
476                 uint clstime, nsent, ntxerr, nrcvd, nrxerr;
477                 ushort rxerrs[ELBT_MAXRXERR], txerrs[ELBT_MAXTXERR];
478                 elbt_rxeacc rxeacc;
479                 elbt_txeacc txeacc;
480         } __attribute__ ((aligned(8)))
481 elbt_chan;
482
483 static uchar patbytes[ELBT_NTXBD] = {
484         0xff, 0xaa, 0x55, 0x00
485 };
486 static uint patwords[ELBT_NTXBD] = {
487         0xffffffff, 0xaaaaaaaa, 0x55555555, 0x00000000
488 };
489
490 #ifdef __GNUC__
491 static elbt_chan elbt_chans[3] __attribute__ ((aligned(8)));
492 #else
493 #error "elbt_chans must be 64-bit aligned"
494 #endif
495
496 #define CPM_CR_GRACEFUL_STOP_TX ((ushort)0x0005)
497
498 static elbt_prdesc epram_descs[] = {
499         { offsetof(fcc_enet_t, fen_crcec),      "CRC Errors"            },
500         { offsetof(fcc_enet_t, fen_alec),       "Alignment Errors"      },
501         { offsetof(fcc_enet_t, fen_disfc),      "Discarded Frames"      },
502         { offsetof(fcc_enet_t, fen_octc),       "Octets"                },
503         { offsetof(fcc_enet_t, fen_colc),       "Collisions"            },
504         { offsetof(fcc_enet_t, fen_broc),       "Broadcast Frames"      },
505         { offsetof(fcc_enet_t, fen_mulc),       "Multicast Frames"      },
506         { offsetof(fcc_enet_t, fen_uspc),       "Undersize Frames"      },
507         { offsetof(fcc_enet_t, fen_frgc),       "Fragments"             },
508         { offsetof(fcc_enet_t, fen_ospc),       "Oversize Frames"       },
509         { offsetof(fcc_enet_t, fen_jbrc),       "Jabbers"               },
510         { offsetof(fcc_enet_t, fen_p64c),       "64 Octet Frames"       },
511         { offsetof(fcc_enet_t, fen_p65c),       "65-127 Octet Frames"   },
512         { offsetof(fcc_enet_t, fen_p128c),      "128-255 Octet Frames"  },
513         { offsetof(fcc_enet_t, fen_p256c),      "256-511 Octet Frames"  },
514         { offsetof(fcc_enet_t, fen_p512c),      "512-1023 Octet Frames" },
515         { offsetof(fcc_enet_t, fen_p1024c),     "1024-1518 Octet Frames"},
516 };
517 static int epram_ndesc = sizeof (epram_descs) / sizeof (epram_descs[0]);
518
519 /*
520  * given an elbt_prdesc array and an array of base addresses, print
521  * each prdesc down the screen with the values fetched from each
522  * base address across the screen
523  */
524 static void
525 print_desc (elbt_prdesc descs[], int ndesc, uchar *bases[], int nbase)
526 {
527         elbt_prdesc *dp = descs, *edp = dp + ndesc;
528         int i;
529
530         printf ("%32s", "");
531
532         for (i = 0; i < nbase; i++)
533                 printf ("  Channel %d", i);
534
535         putc ('\n');
536
537         while (dp < edp) {
538
539                 printf ("%-32s", dp->lab);
540
541                 for (i = 0; i < nbase; i++) {
542                         uint val = *(uint *)(bases[i] + dp->off);
543
544                         printf (" %10u", val);
545                 }
546
547                 putc ('\n');
548
549                 dp++;
550         }
551 }
552
553 /*
554  * return number of bits that are set in a value; value contains
555  * nbits (right-justified) bits.
556  */
557 static uint __inline__
558 nbs (uint value, uint nbits)
559 {
560         uint cnt = 0;
561 #if 1
562         uint pos = sizeof (uint) * 8;
563
564         __asm__ __volatile__ ("\
565         mtctr   %2\n\
566 1:      rlwnm.  %2,%1,%4,31,31\n\
567         beq     2f\n\
568         addi    %0,%0,1\n\
569 2:      subi    %4,%4,1\n\
570         bdnz    1b"
571         : "=r"(cnt)
572         : "r"(value), "r"(nbits), "r"(cnt), "r"(pos)
573         : "ctr", "cc" );
574 #else
575         uint mask = 1;
576
577         do {
578                 if (value & mask)
579                         cnt++;
580                 mask <<= 1;
581         } while (--nbits);
582 #endif
583
584         return (cnt);
585 }
586
587 static ulong
588 badbits (uchar *bp, int n, ulong pat)
589 {
590         ulong *lp, cnt = 0;
591         int nl;
592
593         while (n > 0 && ((ulong)bp & (sizeof (ulong) - 1)) != 0) {
594                 uchar diff;
595
596                 diff = *bp++ ^ (uchar)pat;
597
598                 if (diff)
599                         cnt += nbs ((ulong)diff, 8);
600
601                 n--;
602         }
603
604         lp = (ulong *)bp;
605         nl = n / sizeof (ulong);
606         n -= nl * sizeof (ulong);
607
608         while (nl > 0) {
609                 ulong diff;
610
611                 diff = *lp++ ^ pat;
612
613                 if (diff)
614                         cnt += nbs (diff, 32);
615
616                 nl--;
617         }
618
619         bp = (uchar *)lp;
620
621         while (n > 0) {
622                 uchar diff;
623
624                 diff = *bp++ ^ (uchar)pat;
625
626                 if (diff)
627                         cnt += nbs ((ulong)diff, 8);
628
629                 n--;
630         }
631
632         return (cnt);
633 }
634
635 static inline unsigned short
636 swap16 (unsigned short x)
637 {
638         return (((x & 0xff) << 8) | ((x & 0xff00) >> 8));
639 }
640
641 /* broadcast is not an error - we send them like that */
642 #define BD_ENET_RX_ERRS (BD_ENET_RX_STATS & ~BD_ENET_RX_BC)
643
644 void
645 eth_loopback_test (void)
646 {
647         DECLARE_GLOBAL_DATA_PTR;
648
649         volatile immap_t *immr = (immap_t *)CFG_IMMR;
650         volatile cpm8260_t *cp = &(immr->im_cpm);
651         int c, nclosed;
652         ulong runtime, nmsec;
653         uchar *bases[3];
654
655         puts ("FCC Ethernet External loopback test\n");
656
657         memcpy (NetOurEther, gd->bd->bi_enetaddr, 6);
658
659         /*
660          * global initialisations for all FCC channels
661          */
662
663         /* 28.9 - (1-2): ioports have been set up already */
664
665 #if defined(CONFIG_HYMOD)
666         /*
667          * Attention: this is board-specific
668          * 0, FCC1
669          * 1, FCC2
670          * 2, FCC3
671          */
672 #       define FCC_START_LOOP 0
673 #       define FCC_END_LOOP   2
674
675         /*
676          * Attention: this is board-specific
677          * - FCC1 Rx-CLK is CLK10
678          * - FCC1 Tx-CLK is CLK11
679          * - FCC2 Rx-CLK is CLK13
680          * - FCC2 Tx-CLK is CLK14
681          * - FCC3 Rx-CLK is CLK15
682          * - FCC3 Tx-CLK is CLK16
683          */
684
685         /* 28.9 - (3): connect FCC's tx and rx clocks */
686         immr->im_cpmux.cmx_uar = 0;
687         immr->im_cpmux.cmx_fcr = CMXFCR_RF1CS_CLK10|CMXFCR_TF1CS_CLK11|\
688             CMXFCR_RF2CS_CLK13|CMXFCR_TF2CS_CLK14|\
689             CMXFCR_RF3CS_CLK15|CMXFCR_TF3CS_CLK16;
690 #elif defined(CONFIG_SBC8260) || defined(CONFIG_SACSng)
691         /*
692          * Attention: this is board-specific
693          * 1, FCC2
694          */
695 #       define FCC_START_LOOP 1
696 #       define FCC_END_LOOP   1
697
698         /*
699          * Attention: this is board-specific
700          * - FCC2 Rx-CLK is CLK13
701          * - FCC2 Tx-CLK is CLK14
702          */
703
704         /* 28.9 - (3): connect FCC's tx and rx clocks */
705         immr->im_cpmux.cmx_uar = 0;
706         immr->im_cpmux.cmx_fcr = CMXFCR_RF2CS_CLK13|CMXFCR_TF2CS_CLK14;
707 #else
708 #error "eth_loopback_test not supported on your board"
709 #endif
710
711         puts ("Initialise FCC channels:");
712
713         for (c = FCC_START_LOOP; c <= FCC_END_LOOP; c++) {
714                 elbt_chan *ecp = &elbt_chans[c];
715                 volatile fcc_t *fcp = &immr->im_fcc[c];
716                 volatile fcc_enet_t *fpp;
717                 int i;
718                 ulong addr;
719
720                 /*
721                  * initialise channel data
722                  */
723
724                 printf (" %d", c);
725
726                 memset ((void *)ecp, 0, sizeof (*ecp));
727
728                 ecp->state = Idle;
729
730                 switch (c) {
731
732                 case 0: /* FCC1 */
733                         ecp->proff = PROFF_FCC1;
734                         ecp->page = CPM_CR_FCC1_PAGE;
735                         ecp->sblock = CPM_CR_FCC1_SBLOCK;
736                         break;
737
738                 case 1: /* FCC2 */
739                         ecp->proff = PROFF_FCC2;
740                         ecp->page = CPM_CR_FCC2_PAGE;
741                         ecp->sblock = CPM_CR_FCC2_SBLOCK;
742                         break;
743
744                 case 2: /* FCC3 */
745                         ecp->proff = PROFF_FCC3;
746                         ecp->page = CPM_CR_FCC3_PAGE;
747                         ecp->sblock = CPM_CR_FCC3_SBLOCK;
748                         break;
749                 }
750
751                 /*
752                  * set up tx buffers and bds
753                  */
754
755                 for (i = 0; i < ELBT_NTXBD; i++) {
756                         cbd_t *bdp = &ecp->txbd[i];
757                         uchar *bp = &ecp->txbufs[i][0];
758
759                         bdp->cbd_bufaddr = (uint)bp;
760                         /* room for crc */
761                         bdp->cbd_datlen = ELBT_BUFSZ - ELBT_CRCSZ;
762                         bdp->cbd_sc = BD_ENET_TX_READY | BD_ENET_TX_PAD | \
763                                 BD_ENET_TX_LAST | BD_ENET_TX_TC;
764
765                         memset ((void *)bp, patbytes[i], ELBT_BUFSZ);
766                         NetSetEther (bp, NetBcastAddr, 0x8000);
767                 }
768                 ecp->txbd[ELBT_NTXBD - 1].cbd_sc |= BD_ENET_TX_WRAP;
769
770                 /*
771                  * set up rx buffers and bds
772                  */
773
774                 for (i = 0; i < ELBT_NRXBD; i++) {
775                     cbd_t *bdp = &ecp->rxbd[i];
776                     uchar *bp = &ecp->rxbufs[i][0];
777
778                     bdp->cbd_bufaddr = (uint)bp;
779                     bdp->cbd_datlen = 0;
780                     bdp->cbd_sc = BD_ENET_RX_EMPTY;
781
782                     memset ((void *)bp, 0, ELBT_BUFSZ);
783                 }
784                 ecp->rxbd[ELBT_NRXBD - 1].cbd_sc |= BD_ENET_RX_WRAP;
785
786                 /*
787                  * set up the FCC channel hardware
788                  */
789
790                 /* 28.9 - (4): GFMR: disable tx/rx, CCITT CRC, Mode Ethernet */
791                 fcp->fcc_gfmr = FCC_GFMR_MODE_ENET | FCC_GFMR_TCRC_32;
792
793                 /* 28.9 - (5): FPSMR: fd, enet CRC, Promis, RMON, Rx SHort */
794                 fcp->fcc_fpsmr = FCC_PSMR_FDE | FCC_PSMR_LPB | \
795                         FCC_PSMR_ENCRC | FCC_PSMR_PRO | \
796                         FCC_PSMR_MON | FCC_PSMR_RSH;
797
798                 /* 28.9 - (6): FDSR: Ethernet Syn */
799                 fcp->fcc_fdsr = 0xD555;
800
801                 /* 29.9 - (7): initialise parameter ram */
802                 fpp = (fcc_enet_t *)&(immr->im_dprambase[ecp->proff]);
803
804                 /* clear whole struct to make sure all resv fields are zero */
805                 memset ((void *)fpp, 0, sizeof (fcc_enet_t));
806
807                 /*
808                  * common Parameter RAM area
809                  *
810                  * Allocate space in the reserved FCC area of DPRAM for the
811                  * internal buffers.  No one uses this space (yet), so we
812                  * can do this.  Later, we will add resource management for
813                  * this area.
814                  */
815                 addr = CPM_FCC_SPECIAL_BASE + (c * 64);
816                 fpp->fen_genfcc.fcc_riptr = addr;
817                 fpp->fen_genfcc.fcc_tiptr = addr + 32;
818
819                 /*
820                  * Set maximum bytes per receive buffer.
821                  * It must be a multiple of 32.
822                  * buffers are in 60x bus memory.
823                  */
824                 fpp->fen_genfcc.fcc_mrblr = PKT_MAXBLR_SIZE;
825                 fpp->fen_genfcc.fcc_rstate = (CPMFCR_GBL | CPMFCR_EB) << 24;
826                 fpp->fen_genfcc.fcc_rbase = (unsigned int)(&ecp->rxbd[0]);
827                 fpp->fen_genfcc.fcc_tstate = (CPMFCR_GBL | CPMFCR_EB) << 24;
828                 fpp->fen_genfcc.fcc_tbase = (unsigned int)(&ecp->txbd[0]);
829
830                 /* protocol-specific area */
831                 fpp->fen_cmask = 0xdebb20e3;    /* CRC mask */
832                 fpp->fen_cpres = 0xffffffff;    /* CRC preset */
833                 fpp->fen_retlim = 15;           /* Retry limit threshold */
834                 fpp->fen_mflr = PKT_MAXBUF_SIZE;/* max frame length register */
835
836                 /*
837                  * Set Ethernet station address.
838                  *
839                  * This is supplied in the board information structure, so we
840                  * copy that into the controller.
841                  * So, far we have only been given one Ethernet address. We use
842                  * the same address for all channels
843                  */
844 #define ea gd->bd->bi_enetaddr
845                 fpp->fen_paddrh = (ea[5] << 8) + ea[4];
846                 fpp->fen_paddrm = (ea[3] << 8) + ea[2];
847                 fpp->fen_paddrl = (ea[1] << 8) + ea[0];
848 #undef ea
849
850                 fpp->fen_minflr = PKT_MINBUF_SIZE; /* min frame len register */
851                 /*
852                  * pad pointer. use tiptr since we don't need
853                  * a specific padding char
854                  */
855                 fpp->fen_padptr = fpp->fen_genfcc.fcc_tiptr;
856                 fpp->fen_maxd1 = PKT_MAXDMA_SIZE;       /* max DMA1 length */
857                 fpp->fen_maxd2 = PKT_MAXDMA_SIZE;       /* max DMA2 length */
858                 fpp->fen_rfthr = 1;
859                 fpp->fen_rfcnt = 1;
860
861                 /* 28.9 - (8): clear out events in FCCE */
862                 fcp->fcc_fcce = ~0x0;
863
864                 /* 28.9 - (9): FCCM: mask all events */
865                 fcp->fcc_fccm = 0;
866
867                 /* 28.9 - (10-12): we don't use ethernet interrupts */
868
869                 /* 28.9 - (13)
870                  *
871                  * Let's re-initialize the channel now.  We have to do it later
872                  * than the manual describes because we have just now finished
873                  * the BD initialization.
874                  */
875                 cp->cp_cpcr = mk_cr_cmd (ecp->page, ecp->sblock, \
876                         0x0c, CPM_CR_INIT_TRX) | CPM_CR_FLG;
877                 do {
878                         __asm__ __volatile__ ("eieio");
879                 } while (cp->cp_cpcr & CPM_CR_FLG);
880         }
881
882         puts (" done\nStarting test... (Ctrl-C to Finish)\n");
883
884         /*
885          * Note: don't want serial output from here until the end of the
886          * test - the delays would probably stuff things up.
887          */
888
889         clear_ctrlc ();
890         runtime = get_timer (0);
891
892         do {
893                 nclosed = 0;
894
895                 for (c = FCC_START_LOOP; c <= FCC_END_LOOP; c++) {
896                         volatile fcc_t *fcp = &immr->im_fcc[c];
897                         elbt_chan *ecp = &elbt_chans[c];
898                         int i;
899
900                         switch (ecp->state) {
901
902                         case Idle:
903                                 /*
904                                  * set the channel Running ...
905                                  */
906
907                                 /* 28.9 - (14): enable tx/rx in gfmr */
908                                 fcp->fcc_gfmr |= FCC_GFMR_ENT | FCC_GFMR_ENR;
909
910                                 ecp->state = Running;
911                                 break;
912
913                         case Running:
914                                 /*
915                                  * (while Running only) check for
916                                  * termination of the test
917                                  */
918
919                                 (void)ctrlc ();
920
921                                 if (had_ctrlc ()) {
922                                         /*
923                                          * initiate a "graceful stop transmit"
924                                          * on the channel
925                                          */
926                                         cp->cp_cpcr = mk_cr_cmd (ecp->page, \
927                                                 ecp->sblock, 0x0c, \
928                                                 CPM_CR_GRACEFUL_STOP_TX) | \
929                                                 CPM_CR_FLG;
930                                         do {
931                                                 __asm__ __volatile__ ("eieio");
932                                         } while (cp->cp_cpcr & CPM_CR_FLG);
933
934                                         ecp->clstime = get_timer (0);
935                                         ecp->state = Closing;
936                                 }
937                                 /* fall through ... */
938
939                         case Closing:
940                                 /*
941                                  * (while Running or Closing) poll the channel:
942                                  * - check for any non-READY tx buffers and
943                                  *   make them ready
944                                  * - check for any non-EMPTY rx buffers and
945                                  *   check that they were received correctly,
946                                  *   adjust counters etc, then make empty
947                                  */
948
949                                 for (i = 0; i < ELBT_NTXBD; i++) {
950                                         cbd_t *bdp = &ecp->txbd[i];
951                                         ushort sc = bdp->cbd_sc;
952
953                                         if ((sc & BD_ENET_TX_READY) != 0)
954                                                 continue;
955
956                                         /*
957                                          * this frame has finished
958                                          * transmitting
959                                          */
960                                         ecp->nsent++;
961
962                                         if (sc & BD_ENET_TX_STATS) {
963                                                 ulong n;
964
965                                                 /*
966                                                  * we had an error on
967                                                  * the transmission
968                                                  */
969                                                 n = ecp->ntxerr++;
970                                                 if (n < ELBT_MAXTXERR)
971                                                         ecp->txerrs[n] = sc;
972
973                                                 if (sc & BD_ENET_TX_DEF)
974                                                         ecp->txeacc.def++;
975                                                 if (sc & BD_ENET_TX_HB)
976                                                         ecp->txeacc.hb++;
977                                                 if (sc & BD_ENET_TX_LC)
978                                                         ecp->txeacc.lc++;
979                                                 if (sc & BD_ENET_TX_RL)
980                                                         ecp->txeacc.rl++;
981                                                 if (sc & BD_ENET_TX_RCMASK)
982                                                         ecp->txeacc.rc++;
983                                                 if (sc & BD_ENET_TX_UN)
984                                                         ecp->txeacc.un++;
985                                                 if (sc & BD_ENET_TX_CSL)
986                                                         ecp->txeacc.csl++;
987
988                                                 bdp->cbd_sc &= \
989                                                         ~BD_ENET_TX_STATS;
990                                         }
991
992                                         if (ecp->state == Closing)
993                                                 ecp->clstime = get_timer (0);
994
995                                         /* make it ready again */
996                                         bdp->cbd_sc |= BD_ENET_TX_READY;
997                                 }
998
999                                 for (i = 0; i < ELBT_NRXBD; i++) {
1000                                         cbd_t *bdp = &ecp->rxbd[i];
1001                                         ushort sc = bdp->cbd_sc, mask;
1002
1003                                         if ((sc & BD_ENET_RX_EMPTY) != 0)
1004                                                 continue;
1005
1006                                         /* we have a new frame in this buffer */
1007                                         ecp->nrcvd++;
1008
1009                                         mask = BD_ENET_RX_LAST|BD_ENET_RX_FIRST;
1010                                         if ((sc & mask) != mask) {
1011                                                 /* somethings wrong here ... */
1012                                                 if (!(sc & BD_ENET_RX_LAST))
1013                                                         ecp->rxeacc._l++;
1014                                                 if (!(sc & BD_ENET_RX_FIRST))
1015                                                         ecp->rxeacc._f++;
1016                                         }
1017
1018                                         if (sc & BD_ENET_RX_ERRS) {
1019                                                 ulong n;
1020
1021                                                 /*
1022                                                  * we had some sort of error
1023                                                  * on the frame
1024                                                  */
1025                                                 n = ecp->nrxerr++;
1026                                                 if (n < ELBT_MAXRXERR)
1027                                                         ecp->rxerrs[n] = sc;
1028
1029                                                 if (sc & BD_ENET_RX_MISS)
1030                                                         ecp->rxeacc.m++;
1031                                                 if (sc & BD_ENET_RX_BC)
1032                                                         ecp->rxeacc.bc++;
1033                                                 if (sc & BD_ENET_RX_MC)
1034                                                         ecp->rxeacc.mc++;
1035                                                 if (sc & BD_ENET_RX_LG)
1036                                                         ecp->rxeacc.lg++;
1037                                                 if (sc & BD_ENET_RX_NO)
1038                                                         ecp->rxeacc.no++;
1039                                                 if (sc & BD_ENET_RX_SH)
1040                                                         ecp->rxeacc.sh++;
1041                                                 if (sc & BD_ENET_RX_CR)
1042                                                         ecp->rxeacc.cr++;
1043                                                 if (sc & BD_ENET_RX_OV)
1044                                                         ecp->rxeacc.ov++;
1045                                                 if (sc & BD_ENET_RX_CL)
1046                                                         ecp->rxeacc.cl++;
1047
1048                                                 bdp->cbd_sc &= \
1049                                                         ~BD_ENET_RX_ERRS;
1050                                         }
1051                                         else {
1052                                                 ushort datlen = bdp->cbd_datlen;
1053                                                 Ethernet_t *ehp;
1054                                                 ushort prot;
1055                                                 int ours, tb, n, nbytes;
1056
1057                                                 ehp = (Ethernet_t *) \
1058                                                         &ecp->rxbufs[i][0];
1059
1060                                                 ours = memcmp (ehp->et_src, \
1061                                                         NetOurEther, 6);
1062
1063                                                 prot = swap16 (ehp->et_protlen);
1064                                                 tb = prot & 0x8000;
1065                                                 n = prot & 0x7fff;
1066
1067                                                 nbytes = ELBT_BUFSZ - \
1068                                                         offsetof (Ethernet_t, \
1069                                                                 et_dsap) - \
1070                                                         ELBT_CRCSZ;
1071
1072                                                 /* check the frame is correct */
1073                                                 if (datlen != ELBT_BUFSZ)
1074                                                         ecp->rxeacc.badlen++;
1075                                                 else if (!ours)
1076                                                         ecp->rxeacc.badsrc++;
1077                                                 else if (!tb || n >= ELBT_NTXBD)
1078                                                         ecp->rxeacc.badtyp++;
1079                                                 else {
1080                                                         ulong patword = \
1081                                                                 patwords[n];
1082                                                         uint nbb;
1083
1084                                                         nbb = badbits ( \
1085                                                                 &ehp->et_dsap, \
1086                                                                 nbytes, \
1087                                                                 patword);
1088
1089                                                         ecp->rxeacc.badbit += \
1090                                                                 nbb;
1091                                                 }
1092                                         }
1093
1094                                         if (ecp->state == Closing)
1095                                             ecp->clstime = get_timer (0);
1096
1097                                         /* make it empty again */
1098                                         bdp->cbd_sc |= BD_ENET_RX_EMPTY;
1099                                 }
1100
1101                                 if (ecp->state != Closing)
1102                                         break;
1103
1104                                 /*
1105                                  * (while Closing) check to see if
1106                                  * waited long enough
1107                                  */
1108
1109                                 if (get_timer (ecp->clstime) >= ELBT_CLSWAIT) {
1110                                         /* write GFMR: disable tx/rx */
1111                                         fcp->fcc_gfmr &= \
1112                                                 ~(FCC_GFMR_ENT | FCC_GFMR_ENR);
1113                                         ecp->state = Closed;
1114                                 }
1115
1116                                 break;
1117
1118                         case Closed:
1119                                 nclosed++;
1120                                 break;
1121                         }
1122                 }
1123
1124         } while (nclosed < (FCC_END_LOOP - FCC_START_LOOP + 1));
1125
1126         runtime = get_timer (runtime);
1127         if (runtime <= ELBT_CLSWAIT) {
1128                 printf ("Whoops! somehow elapsed time (%ld) is wrong (<= %d)\n",
1129                         runtime, ELBT_CLSWAIT);
1130                 return;
1131         }
1132         nmsec = runtime - ELBT_CLSWAIT;
1133
1134         printf ("Test Finished in %ldms (plus %dms close wait period)!\n\n",
1135                 nmsec, ELBT_CLSWAIT);
1136
1137         /*
1138          * now print stats
1139          */
1140
1141         for (c = FCC_START_LOOP; c <= FCC_END_LOOP; c++) {
1142                 elbt_chan *ecp = &elbt_chans[c];
1143                 uint rxpps, txpps, nerr;
1144
1145                 rxpps = (ecp->nrcvd * 1000) / nmsec;
1146                 txpps = (ecp->nsent * 1000) / nmsec;
1147
1148                 printf ("Channel %d: %d rcvd (%d pps, %d rxerrs), "
1149                         "%d sent (%d pps, %d txerrs)\n\n", c,
1150                         ecp->nrcvd, rxpps, ecp->nrxerr,
1151                         ecp->nsent, txpps, ecp->ntxerr);
1152
1153                 if ((nerr = ecp->nrxerr) > 0) {
1154                         ulong i;
1155
1156                         printf ("\tFirst %d rx errs:", nerr);
1157                         for (i = 0; i < nerr; i++)
1158                                 printf (" %04x", ecp->rxerrs[i]);
1159                         putc ('\n');
1160                 }
1161
1162                 if ((nerr = ecp->ntxerr) > 0) {
1163                         ulong i;
1164
1165                         printf ("\tFirst %d tx errs:", nerr);
1166                         for (i = 0; i < nerr; i++)
1167                                 printf (" %04x", ecp->txerrs[i]);
1168                         putc ('\n');
1169                 }
1170         }
1171
1172         puts ("Receive Error Counts:\n");
1173         for (c = FCC_START_LOOP; c <= FCC_END_LOOP; c++)
1174                 bases[c] = (uchar *)&elbt_chans[c].rxeacc;
1175         print_desc (rxeacc_descs, rxeacc_ndesc, bases, 3);
1176
1177         puts ("\nTransmit Error Counts:\n");
1178         for (c = FCC_START_LOOP; c <= FCC_END_LOOP; c++)
1179                 bases[c] = (uchar *)&elbt_chans[c].txeacc;
1180         print_desc (txeacc_descs, txeacc_ndesc, bases, 3);
1181
1182         puts ("\nRMON(-like) Counters:\n");
1183         for (c = FCC_START_LOOP; c <= FCC_END_LOOP; c++)
1184                 bases[c] = (uchar *)&immr->im_dprambase[elbt_chans[c].proff];
1185         print_desc (epram_descs, epram_ndesc, bases, 3);
1186 }
1187
1188 #endif /* CONFIG_ETHER_LOOPBACK_TEST */
1189
1190 #endif  /* CONFIG_ETHER_ON_FCC && CFG_CMD_NET && CONFIG_NET_MULTI */