3 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
5 * SPDX-License-Identifier: GPL-2.0+
13 * The Serial Management Controllers (SMC) and the Serial Communication
14 * Controllers (SCC) listed in ctlr_list array below are tested in
15 * the loopback UART mode.
16 * The controllers are configured accordingly and several characters
17 * are transmitted. The configurable test parameters are:
18 * MIN_PACKET_LENGTH - minimum size of packet to transmit
19 * MAX_PACKET_LENGTH - maximum size of packet to transmit
20 * TEST_NUM - number of tests
24 #if CONFIG_POST & CONFIG_SYS_POST_UART
25 #if defined(CONFIG_8xx)
27 #elif defined(CONFIG_MPC8260)
28 #include <asm/cpm_8260.h>
30 #error "Apparently a bad configuration, please fix."
35 DECLARE_GLOBAL_DATA_PTR;
40 /* The list of controllers to test */
41 #if defined(CONFIG_MPC823)
42 static int ctlr_list[][2] =
43 { {CTLR_SMC, 0}, {CTLR_SMC, 1}, {CTLR_SCC, 1} };
45 static int ctlr_list[][2] = { };
49 void (*init) (int index);
50 void (*halt) (int index);
51 void (*putc) (int index, const char c);
52 int (*getc) (int index);
55 static char *ctlr_name[2] = { "SMC", "SCC" };
57 static int proff_smc[] = { PROFF_SMC1, PROFF_SMC2 };
58 static int proff_scc[] =
59 { PROFF_SCC1, PROFF_SCC2, PROFF_SCC3, PROFF_SCC4 };
65 static void smc_init (int smc_index)
67 static int cpm_cr_ch[] = { CPM_CR_CH_SMC1, CPM_CR_CH_SMC2 };
69 volatile immap_t *im = (immap_t *) CONFIG_SYS_IMMR;
71 volatile smc_uart_t *up;
72 volatile cbd_t *tbdf, *rbdf;
73 volatile cpm8xx_t *cp = &(im->im_cpm);
76 /* initialize pointers to SMC */
78 sp = (smc_t *) & (cp->cp_smc[smc_index]);
79 up = (smc_uart_t *) & cp->cp_dparam[proff_smc[smc_index]];
81 /* Disable transmitter/receiver.
83 sp->smc_smcmr &= ~(SMCMR_REN | SMCMR_TEN);
87 im->im_siu_conf.sc_sdcr = 1;
89 /* clear error conditions */
90 #ifdef CONFIG_SYS_SDSR
91 im->im_sdma.sdma_sdsr = CONFIG_SYS_SDSR;
93 im->im_sdma.sdma_sdsr = 0x83;
96 /* clear SDMA interrupt mask */
97 #ifdef CONFIG_SYS_SDMR
98 im->im_sdma.sdma_sdmr = CONFIG_SYS_SDMR;
100 im->im_sdma.sdma_sdmr = 0x00;
103 #if defined(CONFIG_FADS)
106 ~(smc_index == 1 ? BCSR1_RS232EN_1 : BCSR1_RS232EN_2);
109 #if defined(CONFIG_RPXLITE) || defined(CONFIG_RPXCLASSIC)
110 /* Enable Monitor Port Transceiver */
111 *((uchar *) BCSR0) |= BCSR0_ENMONXCVR;
114 /* Set the physical address of the host memory buffers in
115 * the buffer descriptors.
118 #ifdef CONFIG_SYS_ALLOC_DPRAM
119 dpaddr = dpram_alloc_align (sizeof (cbd_t) * 2 + 2, 8);
121 dpaddr = CPM_POST_BASE;
124 /* Allocate space for two buffer descriptors in the DP ram.
125 * For now, this address seems OK, but it may have to
126 * change with newer versions of the firmware.
127 * damm: allocating space after the two buffers for rx/tx data
130 rbdf = (cbd_t *) & cp->cp_dpmem[dpaddr];
131 rbdf->cbd_bufaddr = (uint) (rbdf + 2);
134 tbdf->cbd_bufaddr = ((uint) (rbdf + 2)) + 1;
137 /* Set up the uart parameters in the parameter ram.
139 up->smc_rbase = dpaddr;
140 up->smc_tbase = dpaddr + sizeof (cbd_t);
141 up->smc_rfcr = SMC_EB;
142 up->smc_tfcr = SMC_EB;
144 #if defined(CONFIG_MBX)
145 board_serial_init ();
148 /* Set UART mode, 8 bit, no parity, one stop.
149 * Enable receive and transmit.
150 * Set local loopback mode.
152 sp->smc_smcmr = smcr_mk_clen (9) | SMCMR_SM_UART | (ushort) 0x0004;
154 /* Mask all interrupts and remove anything pending.
159 /* Set up the baud rate generator.
161 cp->cp_simode = 0x00000000;
164 (((gd->cpu_clk / 16 / gd->baudrate) -
165 1) << 1) | CPM_BRG_EN;
167 /* Make the first buffer the only buffer.
169 tbdf->cbd_sc |= BD_SC_WRAP;
170 rbdf->cbd_sc |= BD_SC_EMPTY | BD_SC_WRAP;
172 /* Single character receive.
177 /* Initialize Tx/Rx parameters.
180 while (cp->cp_cpcr & CPM_CR_FLG) /* wait if cp is busy */
184 mk_cr_cmd (cpm_cr_ch[smc_index], CPM_CR_INIT_TRX) | CPM_CR_FLG;
186 while (cp->cp_cpcr & CPM_CR_FLG) /* wait if cp is busy */
189 /* Enable transmitter/receiver.
191 sp->smc_smcmr |= SMCMR_REN | SMCMR_TEN;
194 static void smc_halt(int smc_index)
198 static void smc_putc (int smc_index, const char c)
200 volatile cbd_t *tbdf;
202 volatile smc_uart_t *up;
203 volatile immap_t *im = (immap_t *) CONFIG_SYS_IMMR;
204 volatile cpm8xx_t *cpmp = &(im->im_cpm);
206 up = (smc_uart_t *) & cpmp->cp_dparam[proff_smc[smc_index]];
208 tbdf = (cbd_t *) & cpmp->cp_dpmem[up->smc_tbase];
210 /* Wait for last character to go.
213 buf = (char *) tbdf->cbd_bufaddr;
216 while (tbdf->cbd_sc & BD_SC_READY)
221 tbdf->cbd_datlen = 1;
222 tbdf->cbd_sc |= BD_SC_READY;
225 while (tbdf->cbd_sc & BD_SC_READY)
230 static int smc_getc (int smc_index)
232 volatile cbd_t *rbdf;
233 volatile unsigned char *buf;
234 volatile smc_uart_t *up;
235 volatile immap_t *im = (immap_t *) CONFIG_SYS_IMMR;
236 volatile cpm8xx_t *cpmp = &(im->im_cpm);
240 up = (smc_uart_t *) & cpmp->cp_dparam[proff_smc[smc_index]];
242 rbdf = (cbd_t *) & cpmp->cp_dpmem[up->smc_rbase];
244 /* Wait for character to show up.
246 buf = (unsigned char *) rbdf->cbd_bufaddr;
248 while (rbdf->cbd_sc & BD_SC_EMPTY);
250 for (i = 100; i > 0; i--) {
251 if (!(rbdf->cbd_sc & BD_SC_EMPTY))
260 rbdf->cbd_sc |= BD_SC_EMPTY;
269 static void scc_init (int scc_index)
271 static int cpm_cr_ch[] = {
278 volatile immap_t *im = (immap_t *) CONFIG_SYS_IMMR;
280 volatile scc_uart_t *up;
281 volatile cbd_t *tbdf, *rbdf;
282 volatile cpm8xx_t *cp = &(im->im_cpm);
285 /* initialize pointers to SCC */
287 sp = (scc_t *) & (cp->cp_scc[scc_index]);
288 up = (scc_uart_t *) & cp->cp_dparam[proff_scc[scc_index]];
290 /* Disable transmitter/receiver.
292 sp->scc_gsmrl &= ~(SCC_GSMRL_ENR | SCC_GSMRL_ENT);
295 /* Allocate space for two buffer descriptors in the DP ram.
298 #ifdef CONFIG_SYS_ALLOC_DPRAM
299 dpaddr = dpram_alloc_align (sizeof (cbd_t) * 2 + 2, 8);
301 dpaddr = CPM_POST_BASE;
306 im->im_siu_conf.sc_sdcr = 0x0001;
308 /* Set the physical address of the host memory buffers in
309 * the buffer descriptors.
312 rbdf = (cbd_t *) & cp->cp_dpmem[dpaddr];
313 rbdf->cbd_bufaddr = (uint) (rbdf + 2);
316 tbdf->cbd_bufaddr = ((uint) (rbdf + 2)) + 1;
319 /* Set up the baud rate generator.
321 cp->cp_sicr &= ~(0x000000FF << (8 * scc_index));
322 /* no |= needed, since BRG1 is 000 */
325 (((gd->cpu_clk / 16 / gd->baudrate) -
326 1) << 1) | CPM_BRG_EN;
328 /* Set up the uart parameters in the parameter ram.
330 up->scc_genscc.scc_rbase = dpaddr;
331 up->scc_genscc.scc_tbase = dpaddr + sizeof (cbd_t);
333 /* Initialize Tx/Rx parameters.
335 while (cp->cp_cpcr & CPM_CR_FLG) /* wait if cp is busy */
338 mk_cr_cmd (cpm_cr_ch[scc_index], CPM_CR_INIT_TRX) | CPM_CR_FLG;
340 while (cp->cp_cpcr & CPM_CR_FLG) /* wait if cp is busy */
343 up->scc_genscc.scc_rfcr = SCC_EB | 0x05;
344 up->scc_genscc.scc_tfcr = SCC_EB | 0x05;
346 up->scc_genscc.scc_mrblr = 1; /* Single character receive */
347 up->scc_maxidl = 0; /* disable max idle */
348 up->scc_brkcr = 1; /* send one break character on stop TX */
356 up->scc_char1 = 0x8000;
357 up->scc_char2 = 0x8000;
358 up->scc_char3 = 0x8000;
359 up->scc_char4 = 0x8000;
360 up->scc_char5 = 0x8000;
361 up->scc_char6 = 0x8000;
362 up->scc_char7 = 0x8000;
363 up->scc_char8 = 0x8000;
364 up->scc_rccm = 0xc0ff;
366 /* Set low latency / small fifo.
368 sp->scc_gsmrh = SCC_GSMRH_RFW;
372 sp->scc_gsmrl &= ~0xF;
373 sp->scc_gsmrl |= SCC_GSMRL_MODE_UART;
375 /* Set local loopback mode.
377 sp->scc_gsmrl &= ~SCC_GSMRL_DIAG_LE;
378 sp->scc_gsmrl |= SCC_GSMRL_DIAG_LOOP;
380 /* Set clock divider 16 on Tx and Rx
382 sp->scc_gsmrl |= (SCC_GSMRL_TDCR_16 | SCC_GSMRL_RDCR_16);
384 sp->scc_psmr |= SCU_PSMR_CL;
386 /* Mask all interrupts and remove anything pending.
389 sp->scc_scce = 0xffff;
390 sp->scc_dsr = 0x7e7e;
391 sp->scc_psmr = 0x3000;
393 /* Make the first buffer the only buffer.
395 tbdf->cbd_sc |= BD_SC_WRAP;
396 rbdf->cbd_sc |= BD_SC_EMPTY | BD_SC_WRAP;
398 /* Enable transmitter/receiver.
400 sp->scc_gsmrl |= (SCC_GSMRL_ENR | SCC_GSMRL_ENT);
403 static void scc_halt(int scc_index)
405 volatile immap_t *im = (immap_t *) CONFIG_SYS_IMMR;
406 volatile cpm8xx_t *cp = &(im->im_cpm);
407 volatile scc_t *sp = (scc_t *) & (cp->cp_scc[scc_index]);
409 sp->scc_gsmrl &= ~(SCC_GSMRL_ENR | SCC_GSMRL_ENT | SCC_GSMRL_DIAG_LE);
412 static void scc_putc (int scc_index, const char c)
414 volatile cbd_t *tbdf;
416 volatile scc_uart_t *up;
417 volatile immap_t *im = (immap_t *) CONFIG_SYS_IMMR;
418 volatile cpm8xx_t *cpmp = &(im->im_cpm);
420 up = (scc_uart_t *) & cpmp->cp_dparam[proff_scc[scc_index]];
422 tbdf = (cbd_t *) & cpmp->cp_dpmem[up->scc_genscc.scc_tbase];
424 /* Wait for last character to go.
427 buf = (char *) tbdf->cbd_bufaddr;
430 while (tbdf->cbd_sc & BD_SC_READY)
435 tbdf->cbd_datlen = 1;
436 tbdf->cbd_sc |= BD_SC_READY;
439 while (tbdf->cbd_sc & BD_SC_READY)
444 static int scc_getc (int scc_index)
446 volatile cbd_t *rbdf;
447 volatile unsigned char *buf;
448 volatile scc_uart_t *up;
449 volatile immap_t *im = (immap_t *) CONFIG_SYS_IMMR;
450 volatile cpm8xx_t *cpmp = &(im->im_cpm);
454 up = (scc_uart_t *) & cpmp->cp_dparam[proff_scc[scc_index]];
456 rbdf = (cbd_t *) & cpmp->cp_dpmem[up->scc_genscc.scc_rbase];
458 /* Wait for character to show up.
460 buf = (unsigned char *) rbdf->cbd_bufaddr;
462 while (rbdf->cbd_sc & BD_SC_EMPTY);
464 for (i = 100; i > 0; i--) {
465 if (!(rbdf->cbd_sc & BD_SC_EMPTY))
474 rbdf->cbd_sc |= BD_SC_EMPTY;
483 static int test_ctlr (int ctlr, int index)
486 char test_str[] = "*** UART Test String ***\r\n";
489 ctlr_proc[ctlr].init (index);
491 for (i = 0; i < sizeof (test_str) - 1; i++) {
492 ctlr_proc[ctlr].putc (index, test_str[i]);
493 if (ctlr_proc[ctlr].getc (index) != test_str[i])
500 ctlr_proc[ctlr].halt (index);
503 post_log ("uart %s%d test failed\n",
504 ctlr_name[ctlr], index + 1);
510 int uart_post_test (int flags)
515 ctlr_proc[CTLR_SMC].init = smc_init;
516 ctlr_proc[CTLR_SMC].halt = smc_halt;
517 ctlr_proc[CTLR_SMC].putc = smc_putc;
518 ctlr_proc[CTLR_SMC].getc = smc_getc;
520 ctlr_proc[CTLR_SCC].init = scc_init;
521 ctlr_proc[CTLR_SCC].halt = scc_halt;
522 ctlr_proc[CTLR_SCC].putc = scc_putc;
523 ctlr_proc[CTLR_SCC].getc = scc_getc;
525 for (i = 0; i < ARRAY_SIZE(ctlr_list); i++) {
526 if (test_ctlr (ctlr_list[i][0], ctlr_list[i][1]) != 0) {
531 #if !defined(CONFIG_8xx_CONS_NONE)
532 serial_reinit_all ();
538 #endif /* CONFIG_POST & CONFIG_SYS_POST_UART */