3 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
5 * See file CREDITS for list of people who contributed to this
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License as
10 * published by the Free Software Foundation; either version 2 of
11 * the License, or (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
29 * The Serial Communication Controllers (SCC) listed in ctlr_list array below
30 * are tested in the loopback ethernet mode.
31 * The controllers are configured accordingly and several packets
32 * are transmitted. The configurable test parameters are:
33 * MIN_PACKET_LENGTH - minimum size of packet to transmit
34 * MAX_PACKET_LENGTH - maximum size of packet to transmit
35 * TEST_NUM - number of tests
41 #if CONFIG_POST & CFG_POST_ETHER
42 #if defined(CONFIG_8xx)
44 #elif defined(CONFIG_MPC8260)
45 #include <asm/cpm_8260.h>
47 #error "Apparently a bad configuration, please fix."
54 #define MIN_PACKET_LENGTH 64
55 #define MAX_PACKET_LENGTH 256
60 extern void spi_init_f (void);
61 extern void spi_init_r (void);
63 /* The list of controllers to test */
64 #if defined(CONFIG_MPC823)
65 static int ctlr_list[][2] = { {CTLR_SCC, 1} };
67 static int ctlr_list[][2] = { };
70 #define CTRL_LIST_SIZE (sizeof(ctlr_list) / sizeof(ctlr_list[0]))
73 void (*init) (int index);
74 void (*halt) (int index);
75 int (*send) (int index, volatile void *packet, int length);
76 int (*recv) (int index, void *packet, int length);
79 static char *ctlr_name[1] = { "SCC" };
81 /* Ethernet Transmit and Receive Buffers */
82 #define DBUF_LENGTH 1520
88 static char txbuf[DBUF_LENGTH];
90 static uint rxIdx; /* index of the current RX buffer */
91 static uint txIdx; /* index of the current TX buffer */
94 * SCC Ethernet Tx and Rx buffer descriptors allocated at the
95 * immr->udata_bd address on Dual-Port RAM
96 * Provide for Double Buffering
99 typedef volatile struct CommonBufferDescriptor {
100 cbd_t rxbd[PKTBUFSRX]; /* Rx BD */
101 cbd_t txbd[TX_BUF_CNT]; /* Tx BD */
110 static void scc_init (int scc_index)
112 DECLARE_GLOBAL_DATA_PTR;
116 { PROFF_SCC1, PROFF_SCC2, PROFF_SCC3, PROFF_SCC4 };
117 static unsigned int cpm_cr[] =
118 { CPM_CR_CH_SCC1, CPM_CR_CH_SCC2, CPM_CR_CH_SCC3,
122 scc_enet_t *pram_ptr;
124 volatile immap_t *immr = (immap_t *) CFG_IMMR;
126 immr->im_cpm.cp_scc[scc_index].scc_gsmrl &=
127 ~(SCC_GSMRL_ENR | SCC_GSMRL_ENT);
129 #if defined(CONFIG_FADS)
130 #if defined(CONFIG_MPC860T) || defined(CONFIG_MPC86xADS)
131 /* The FADS860T and MPC86xADS don't use the MODEM_EN or DATA_VOICE signals. */
132 *((uint *) BCSR4) &= ~BCSR4_ETHLOOP;
133 *((uint *) BCSR4) |= BCSR4_TFPLDL | BCSR4_TPSQEL;
134 *((uint *) BCSR1) &= ~BCSR1_ETHEN;
136 *((uint *) BCSR4) &= ~(BCSR4_ETHLOOP | BCSR4_MODEM_EN);
137 *((uint *) BCSR4) |= BCSR4_TFPLDL | BCSR4_TPSQEL | BCSR4_DATA_VOICE;
138 *((uint *) BCSR1) &= ~BCSR1_ETHEN;
142 pram_ptr = (scc_enet_t *) & (immr->im_cpm.cp_dparam[proff[scc_index]]);
147 #ifdef CFG_ALLOC_DPRAM
148 rtx = (RTXBD *) (immr->im_cpm.cp_dpmem +
149 dpram_alloc_align (sizeof (RTXBD), 8));
151 rtx = (RTXBD *) (immr->im_cpm.cp_dpmem + CPM_SCC_BASE);
156 #if (defined(PA_ENET_RXD) && defined(PA_ENET_TXD))
157 /* Configure port A pins for Txd and Rxd.
159 immr->im_ioport.iop_papar |= (PA_ENET_RXD | PA_ENET_TXD);
160 immr->im_ioport.iop_padir &= ~(PA_ENET_RXD | PA_ENET_TXD);
161 immr->im_ioport.iop_paodr &= ~PA_ENET_TXD;
162 #elif (defined(PB_ENET_RXD) && defined(PB_ENET_TXD))
163 /* Configure port B pins for Txd and Rxd.
165 immr->im_cpm.cp_pbpar |= (PB_ENET_RXD | PB_ENET_TXD);
166 immr->im_cpm.cp_pbdir &= ~(PB_ENET_RXD | PB_ENET_TXD);
167 immr->im_cpm.cp_pbodr &= ~PB_ENET_TXD;
169 #error Configuration Error: exactly ONE of PA_ENET_[RT]XD, PB_ENET_[RT]XD must be defined
172 #if defined(PC_ENET_LBK)
173 /* Configure port C pins to disable External Loopback
175 immr->im_ioport.iop_pcpar &= ~PC_ENET_LBK;
176 immr->im_ioport.iop_pcdir |= PC_ENET_LBK;
177 immr->im_ioport.iop_pcso &= ~PC_ENET_LBK;
178 immr->im_ioport.iop_pcdat &= ~PC_ENET_LBK; /* Disable Loopback */
179 #endif /* PC_ENET_LBK */
181 /* Configure port C pins to enable CLSN and RENA.
183 immr->im_ioport.iop_pcpar &= ~(PC_ENET_CLSN | PC_ENET_RENA);
184 immr->im_ioport.iop_pcdir &= ~(PC_ENET_CLSN | PC_ENET_RENA);
185 immr->im_ioport.iop_pcso |= (PC_ENET_CLSN | PC_ENET_RENA);
187 /* Configure port A for TCLK and RCLK.
189 immr->im_ioport.iop_papar |= (PA_ENET_TCLK | PA_ENET_RCLK);
190 immr->im_ioport.iop_padir &= ~(PA_ENET_TCLK | PA_ENET_RCLK);
193 * Configure Serial Interface clock routing -- see section 16.7.5.3
194 * First, clear all SCC bits to zero, then set the ones we want.
197 immr->im_cpm.cp_sicr &= ~SICR_ENET_MASK;
198 immr->im_cpm.cp_sicr |= SICR_ENET_CLKRT;
201 * SCC2 receive clock is BRG2
202 * SCC2 transmit clock is BRG3
204 immr->im_cpm.cp_brgc2 = 0x0001000C;
205 immr->im_cpm.cp_brgc3 = 0x0001000C;
207 immr->im_cpm.cp_sicr &= ~0x00003F00;
208 immr->im_cpm.cp_sicr |= 0x00000a00;
213 * Initialize SDCR -- see section 16.9.23.7
214 * SDMA configuration register
216 immr->im_siu_conf.sc_sdcr = 0x01;
220 * Setup SCC Ethernet Parameter RAM
223 pram_ptr->sen_genscc.scc_rfcr = 0x18; /* Normal Operation and Mot byte ordering */
224 pram_ptr->sen_genscc.scc_tfcr = 0x18; /* Mot byte ordering, Normal access */
226 pram_ptr->sen_genscc.scc_mrblr = DBUF_LENGTH; /* max. ET package len 1520 */
228 pram_ptr->sen_genscc.scc_rbase = (unsigned int) (&rtx->rxbd[0]); /* Set RXBD tbl start at Dual Port */
229 pram_ptr->sen_genscc.scc_tbase = (unsigned int) (&rtx->txbd[0]); /* Set TXBD tbl start at Dual Port */
232 * Setup Receiver Buffer Descriptors (13.14.24.18)
237 for (i = 0; i < PKTBUFSRX; i++) {
238 rtx->rxbd[i].cbd_sc = BD_ENET_RX_EMPTY;
239 rtx->rxbd[i].cbd_datlen = 0; /* Reset */
240 rtx->rxbd[i].cbd_bufaddr = (uint) NetRxPackets[i];
243 rtx->rxbd[PKTBUFSRX - 1].cbd_sc |= BD_ENET_RX_WRAP;
246 * Setup Ethernet Transmitter Buffer Descriptors (13.14.24.19)
248 * Add PADs to Short FRAMES, Wrap, Last, Tx CRC
251 for (i = 0; i < TX_BUF_CNT; i++) {
252 rtx->txbd[i].cbd_sc =
253 (BD_ENET_TX_PAD | BD_ENET_TX_LAST | BD_ENET_TX_TC);
254 rtx->txbd[i].cbd_datlen = 0; /* Reset */
255 rtx->txbd[i].cbd_bufaddr = (uint) (&txbuf[0]);
258 rtx->txbd[TX_BUF_CNT - 1].cbd_sc |= BD_ENET_TX_WRAP;
261 * Enter Command: Initialize Rx Params for SCC
264 do { /* Spin until ready to issue command */
266 } while (immr->im_cpm.cp_cpcr & CPM_CR_FLG);
268 immr->im_cpm.cp_cpcr =
269 ((CPM_CR_INIT_RX << 8) | (cpm_cr[scc_index] << 4) |
271 do { /* Spin until command processed */
273 } while (immr->im_cpm.cp_cpcr & CPM_CR_FLG);
276 * Ethernet Specific Parameter RAM
277 * see table 13-16, pg. 660,
278 * pg. 681 (example with suggested settings)
281 pram_ptr->sen_cpres = ~(0x0); /* Preset CRC */
282 pram_ptr->sen_cmask = 0xdebb20e3; /* Constant Mask for CRC */
283 pram_ptr->sen_crcec = 0x0; /* Error Counter CRC (unused) */
284 pram_ptr->sen_alec = 0x0; /* Alignment Error Counter (unused) */
285 pram_ptr->sen_disfc = 0x0; /* Discard Frame Counter (unused) */
286 pram_ptr->sen_pads = 0x8888; /* Short Frame PAD Characters */
288 pram_ptr->sen_retlim = 15; /* Retry Limit Threshold */
289 pram_ptr->sen_maxflr = 1518; /* MAX Frame Length Register */
290 pram_ptr->sen_minflr = 64; /* MIN Frame Length Register */
292 pram_ptr->sen_maxd1 = DBUF_LENGTH; /* MAX DMA1 Length Register */
293 pram_ptr->sen_maxd2 = DBUF_LENGTH; /* MAX DMA2 Length Register */
295 pram_ptr->sen_gaddr1 = 0x0; /* Group Address Filter 1 (unused) */
296 pram_ptr->sen_gaddr2 = 0x0; /* Group Address Filter 2 (unused) */
297 pram_ptr->sen_gaddr3 = 0x0; /* Group Address Filter 3 (unused) */
298 pram_ptr->sen_gaddr4 = 0x0; /* Group Address Filter 4 (unused) */
300 #define ea bd->bi_enetaddr
301 pram_ptr->sen_paddrh = (ea[5] << 8) + ea[4];
302 pram_ptr->sen_paddrm = (ea[3] << 8) + ea[2];
303 pram_ptr->sen_paddrl = (ea[1] << 8) + ea[0];
306 pram_ptr->sen_pper = 0x0; /* Persistence (unused) */
307 pram_ptr->sen_iaddr1 = 0x0; /* Individual Address Filter 1 (unused) */
308 pram_ptr->sen_iaddr2 = 0x0; /* Individual Address Filter 2 (unused) */
309 pram_ptr->sen_iaddr3 = 0x0; /* Individual Address Filter 3 (unused) */
310 pram_ptr->sen_iaddr4 = 0x0; /* Individual Address Filter 4 (unused) */
311 pram_ptr->sen_taddrh = 0x0; /* Tmp Address (MSB) (unused) */
312 pram_ptr->sen_taddrm = 0x0; /* Tmp Address (unused) */
313 pram_ptr->sen_taddrl = 0x0; /* Tmp Address (LSB) (unused) */
316 * Enter Command: Initialize Tx Params for SCC
319 do { /* Spin until ready to issue command */
321 } while (immr->im_cpm.cp_cpcr & CPM_CR_FLG);
323 immr->im_cpm.cp_cpcr =
324 ((CPM_CR_INIT_TX << 8) | (cpm_cr[scc_index] << 4) |
326 do { /* Spin until command processed */
328 } while (immr->im_cpm.cp_cpcr & CPM_CR_FLG);
331 * Mask all Events in SCCM - we use polling mode
333 immr->im_cpm.cp_scc[scc_index].scc_sccm = 0;
336 * Clear Events in SCCE -- Clear bits by writing 1's
339 immr->im_cpm.cp_scc[scc_index].scc_scce = ~(0x0);
343 * Initialize GSMR High 32-Bits
344 * Settings: Normal Mode
347 immr->im_cpm.cp_scc[scc_index].scc_gsmrh = 0;
350 * Initialize GSMR Low 32-Bits, but do not Enable Transmit/Receive
354 * TPP = Repeating 10's
359 immr->im_cpm.cp_scc[scc_index].scc_gsmrl = (SCC_GSMRL_TCI |
362 SCC_GSMRL_DIAG_LOOP |
363 SCC_GSMRL_MODE_ENET);
366 * Initialize the DSR -- see section 13.14.4 (pg. 513) v0.4
369 immr->im_cpm.cp_scc[scc_index].scc_dsr = 0xd555;
372 * Initialize the PSMR
375 * NIB = Begin searching for SFD 22 bits after RENA
376 * LPB = Loopback Enable (Needed when FDE is set)
378 immr->im_cpm.cp_scc[scc_index].scc_psmr = SCC_PSMR_ENCRC |
379 SCC_PSMR_NIB22 | SCC_PSMR_LPB;
383 * Configure Ethernet TENA Signal
386 #if (defined(PC_ENET_TENA) && !defined(PB_ENET_TENA))
387 immr->im_ioport.iop_pcpar |= PC_ENET_TENA;
388 immr->im_ioport.iop_pcdir &= ~PC_ENET_TENA;
389 #elif (defined(PB_ENET_TENA) && !defined(PC_ENET_TENA))
390 immr->im_cpm.cp_pbpar |= PB_ENET_TENA;
391 immr->im_cpm.cp_pbdir |= PB_ENET_TENA;
393 #error Configuration Error: exactly ONE of PB_ENET_TENA, PC_ENET_TENA must be defined
396 #if defined(CONFIG_ADS) && defined(CONFIG_MPC860)
398 * Port C is used to control the PHY,MC68160.
400 immr->im_ioport.iop_pcdir |=
401 (PC_ENET_ETHLOOP | PC_ENET_TPFLDL | PC_ENET_TPSQEL);
403 immr->im_ioport.iop_pcdat |= PC_ENET_TPFLDL;
404 immr->im_ioport.iop_pcdat &= ~(PC_ENET_ETHLOOP | PC_ENET_TPSQEL);
405 *((uint *) BCSR1) &= ~BCSR1_ETHEN;
406 #endif /* MPC860ADS */
408 #if defined(CONFIG_AMX860)
410 * Port B is used to control the PHY,MC68160.
412 immr->im_cpm.cp_pbdir |=
413 (PB_ENET_ETHLOOP | PB_ENET_TPFLDL | PB_ENET_TPSQEL);
415 immr->im_cpm.cp_pbdat |= PB_ENET_TPFLDL;
416 immr->im_cpm.cp_pbdat &= ~(PB_ENET_ETHLOOP | PB_ENET_TPSQEL);
418 immr->im_ioport.iop_pddir |= PD_ENET_ETH_EN;
419 immr->im_ioport.iop_pddat &= ~PD_ENET_ETH_EN;
424 #ifdef CONFIG_RPXCLASSIC
425 *((uchar *) BCSR0) &= ~BCSR0_ETHLPBK;
426 *((uchar *) BCSR0) |= (BCSR0_ETHEN | BCSR0_COLTEST | BCSR0_FULLDPLX);
429 #ifdef CONFIG_RPXLITE
430 *((uchar *) BCSR0) |= BCSR0_ETHEN;
438 * Set the ENT/ENR bits in the GSMR Low -- Enable Transmit/Receive
441 immr->im_cpm.cp_scc[scc_index].scc_gsmrl |=
442 (SCC_GSMRL_ENR | SCC_GSMRL_ENT);
445 * Work around transmit problem with first eth packet
447 #if defined (CONFIG_FADS)
448 udelay (10000); /* wait 10 ms */
449 #elif defined (CONFIG_AMX860) || defined(CONFIG_RPXCLASSIC)
450 udelay (100000); /* wait 100 ms */
454 static void scc_halt (int scc_index)
456 volatile immap_t *immr = (immap_t *) CFG_IMMR;
458 immr->im_cpm.cp_scc[scc_index].scc_gsmrl &=
459 ~(SCC_GSMRL_ENR | SCC_GSMRL_ENT);
460 immr->im_ioport.iop_pcso &= ~(PC_ENET_CLSN | PC_ENET_RENA);
463 static int scc_send (int index, volatile void *packet, int length)
467 while ((rtx->txbd[txIdx].cbd_sc & BD_ENET_TX_READY) && (j < TOUT_LOOP)) {
468 udelay (1); /* will also trigger Wd if needed */
472 printf ("TX not ready\n");
473 rtx->txbd[txIdx].cbd_bufaddr = (uint) packet;
474 rtx->txbd[txIdx].cbd_datlen = length;
475 rtx->txbd[txIdx].cbd_sc |=
476 (BD_ENET_TX_READY | BD_ENET_TX_LAST | BD_ENET_TX_WRAP);
477 while ((rtx->txbd[txIdx].cbd_sc & BD_ENET_TX_READY) && (j < TOUT_LOOP)) {
478 udelay (1); /* will also trigger Wd if needed */
482 printf ("TX timeout\n");
483 i = (rtx->txbd[txIdx].
484 cbd_sc & BD_ENET_TX_STATS) /* return only status bits */ ;
488 static int scc_recv (int index, void *packet, int max_length)
492 if (rtx->rxbd[rxIdx].cbd_sc & BD_ENET_RX_EMPTY) {
493 goto Done; /* nothing received */
496 if (!(rtx->rxbd[rxIdx].cbd_sc & 0x003f)) {
497 length = rtx->rxbd[rxIdx].cbd_datlen - 4;
499 (void *) (NetRxPackets[rxIdx]),
500 length < max_length ? length : max_length);
503 /* Give the buffer back to the SCC. */
504 rtx->rxbd[rxIdx].cbd_datlen = 0;
506 /* wrap around buffer index when necessary */
507 if ((rxIdx + 1) >= PKTBUFSRX) {
508 rtx->rxbd[PKTBUFSRX - 1].cbd_sc =
509 (BD_ENET_RX_WRAP | BD_ENET_RX_EMPTY);
512 rtx->rxbd[rxIdx].cbd_sc = BD_ENET_RX_EMPTY;
524 static void packet_fill (char *packet, int length)
526 char c = (char) length;
536 for (i = 6; i < length; i++) {
541 static int packet_check (char *packet, int length)
543 char c = (char) length;
546 for (i = 6; i < length; i++) {
547 if (packet[i] != c++)
554 static int test_ctlr (int ctlr, int index)
557 char packet_send[MAX_PACKET_LENGTH];
558 char packet_recv[MAX_PACKET_LENGTH];
563 ctlr_proc[ctlr].init (index);
565 for (i = 0; i < TEST_NUM; i++) {
566 for (l = MIN_PACKET_LENGTH; l <= MAX_PACKET_LENGTH; l++) {
567 packet_fill (packet_send, l);
569 ctlr_proc[ctlr].send (index, packet_send, l);
571 length = ctlr_proc[ctlr].recv (index, packet_recv,
574 if (length != l || packet_check (packet_recv, length) < 0) {
584 ctlr_proc[ctlr].halt (index);
587 * SCC2 Ethernet parameter RAM space overlaps
588 * the SPI parameter RAM space. So we need to restore
589 * the SPI configuration after SCC2 ethernet test.
591 #if defined(CONFIG_SPI)
592 if (ctlr == CTLR_SCC && index == 1) {
599 post_log ("ethernet %s%d test failed\n", ctlr_name[ctlr],
606 int ether_post_test (int flags)
611 ctlr_proc[CTLR_SCC].init = scc_init;
612 ctlr_proc[CTLR_SCC].halt = scc_halt;
613 ctlr_proc[CTLR_SCC].send = scc_send;
614 ctlr_proc[CTLR_SCC].recv = scc_recv;
616 for (i = 0; i < CTRL_LIST_SIZE; i++) {
617 if (test_ctlr (ctlr_list[i][0], ctlr_list[i][1]) != 0) {
622 #if !defined(CONFIG_8xx_CONS_NONE)
623 serial_reinit_all ();
628 #endif /* CONFIG_POST & CFG_POST_ETHER */
630 #endif /* CONFIG_POST */