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