]> git.kernelconcepts.de Git - karo-tx-uboot.git/blob - post/uart.c
Patch by Guillaume Alexandre,, 04 Nov 2002:
[karo-tx-uboot.git] / post / uart.c
1 /*
2  * (C) Copyright 2002
3  * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
4  *
5  * See file CREDITS for list of people who contributed to this
6  * project.
7  *
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.
12  *
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.
17  *
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,
21  * MA 02111-1307 USA
22  */
23
24 #include <common.h>
25
26 /*
27  * UART test
28  *
29  * The Serial Management Controllers (SMC) and the Serial Communication
30  * Controllers (SCC) listed in ctlr_list array below are tested in
31  * the loopback UART mode.
32  * The controllers are configured accordingly and several characters
33  * are transmitted. The configurable test parameters are:
34  *   MIN_PACKET_LENGTH - minimum size of packet to transmit
35  *   MAX_PACKET_LENGTH - maximum size of packet to transmit
36  *   TEST_NUM - number of tests
37  */
38
39 #ifdef CONFIG_POST
40
41 #include <post.h>
42 #if defined(CONFIG_8xx)
43 #include <commproc.h>
44 #elif defined(CONFIG_MPC8260)
45 #include <asm/cpm_8260.h>
46 #else
47 #error "Apparently a bad configuration, please fix."
48 #endif
49 #include <command.h>
50 #include <net.h>
51
52 #if CONFIG_POST & CFG_POST_UART
53
54 #define CTLR_SMC 0
55 #define CTLR_SCC 1
56
57 /* The list of controllers to test */
58 #if defined(CONFIG_MPC823)
59 static int ctlr_list[][2] =
60                 { {CTLR_SMC, 0}, {CTLR_SMC, 1}, {CTLR_SCC, 1} };
61 #else
62 static int ctlr_list[][2] = { };
63 #endif
64
65 #define CTRL_LIST_SIZE (sizeof(ctlr_list) / sizeof(ctlr_list[0]))
66
67 static struct {
68         void (*init) (int index);
69         void (*putc) (int index, const char c);
70         int (*getc) (int index);
71 } ctlr_proc[2];
72
73 static char *ctlr_name[2] = { "SMC", "SCC" };
74
75 static int used_by_uart[2] = { -1, -1 };
76 static int used_by_ether[2] = { -1, -1 };
77
78 static int proff_smc[] = { PROFF_SMC1, PROFF_SMC2 };
79 static int proff_scc[] =
80                 { PROFF_SCC1, PROFF_SCC2, PROFF_SCC3, PROFF_SCC4 };
81
82   /*
83    * SMC callbacks
84    */
85
86 static void smc_init (int smc_index)
87 {
88         DECLARE_GLOBAL_DATA_PTR;
89
90         static int cpm_cr_ch[] = { CPM_CR_CH_SMC1, CPM_CR_CH_SMC2 };
91
92         volatile immap_t *im = (immap_t *) CFG_IMMR;
93         volatile smc_t *sp;
94         volatile smc_uart_t *up;
95         volatile cbd_t *tbdf, *rbdf;
96         volatile cpm8xx_t *cp = &(im->im_cpm);
97         uint dpaddr;
98
99         /* initialize pointers to SMC */
100
101         sp = (smc_t *) & (cp->cp_smc[smc_index]);
102         up = (smc_uart_t *) & cp->cp_dparam[proff_smc[smc_index]];
103
104         /* Disable transmitter/receiver.
105          */
106         sp->smc_smcmr &= ~(SMCMR_REN | SMCMR_TEN);
107
108         /* Enable SDMA.
109          */
110         im->im_siu_conf.sc_sdcr = 1;
111
112         /* clear error conditions */
113 #ifdef  CFG_SDSR
114         im->im_sdma.sdma_sdsr = CFG_SDSR;
115 #else
116         im->im_sdma.sdma_sdsr = 0x83;
117 #endif
118
119         /* clear SDMA interrupt mask */
120 #ifdef  CFG_SDMR
121         im->im_sdma.sdma_sdmr = CFG_SDMR;
122 #else
123         im->im_sdma.sdma_sdmr = 0x00;
124 #endif
125
126 #if defined(CONFIG_FADS)
127         /* Enable RS232 */
128         *((uint *) BCSR1) &=
129                         ~(smc_index == 1 ? BCSR1_RS232EN_1 : BCSR1_RS232EN_2);
130 #endif
131
132 #if defined(CONFIG_RPXLITE) || defined(CONFIG_RPXCLASSIC)
133         /* Enable Monitor Port Transceiver */
134         *((uchar *) BCSR0) |= BCSR0_ENMONXCVR;
135 #endif
136
137         /* Set the physical address of the host memory buffers in
138          * the buffer descriptors.
139          */
140
141 #ifdef CFG_ALLOC_DPRAM
142         dpaddr = dpram_alloc_align (sizeof (cbd_t) * 2 + 2, 8);
143 #else
144         dpaddr = CPM_POST_BASE;
145 #endif
146
147         /* Allocate space for two buffer descriptors in the DP ram.
148          * For now, this address seems OK, but it may have to
149          * change with newer versions of the firmware.
150          * damm: allocating space after the two buffers for rx/tx data
151          */
152
153         rbdf = (cbd_t *) & cp->cp_dpmem[dpaddr];
154         rbdf->cbd_bufaddr = (uint) (rbdf + 2);
155         rbdf->cbd_sc = 0;
156         tbdf = rbdf + 1;
157         tbdf->cbd_bufaddr = ((uint) (rbdf + 2)) + 1;
158         tbdf->cbd_sc = 0;
159
160         /* Set up the uart parameters in the parameter ram.
161          */
162         up->smc_rbase = dpaddr;
163         up->smc_tbase = dpaddr + sizeof (cbd_t);
164         up->smc_rfcr = SMC_EB;
165         up->smc_tfcr = SMC_EB;
166
167 #if defined(CONFIG_MBX)
168         board_serial_init ();
169 #endif
170
171         /* Set UART mode, 8 bit, no parity, one stop.
172          * Enable receive and transmit.
173          * Set local loopback mode.
174          */
175         sp->smc_smcmr = smcr_mk_clen (9) | SMCMR_SM_UART | (ushort) 0x0004;
176
177         /* Mask all interrupts and remove anything pending.
178          */
179         sp->smc_smcm = 0;
180         sp->smc_smce = 0xff;
181
182         /* Set up the baud rate generator.
183          */
184         cp->cp_simode = 0x00000000;
185
186         cp->cp_brgc1 =
187                         (((gd->cpu_clk / 16 / gd->baudrate) -
188                           1) << 1) | CPM_BRG_EN;
189
190         /* Make the first buffer the only buffer.
191          */
192         tbdf->cbd_sc |= BD_SC_WRAP;
193         rbdf->cbd_sc |= BD_SC_EMPTY | BD_SC_WRAP;
194
195         /* Single character receive.
196          */
197         up->smc_mrblr = 1;
198         up->smc_maxidl = 0;
199
200         /* Initialize Tx/Rx parameters.
201          */
202
203         while (cp->cp_cpcr & CPM_CR_FLG)        /* wait if cp is busy */
204                 ;
205
206         cp->cp_cpcr =
207                         mk_cr_cmd (cpm_cr_ch[smc_index], CPM_CR_INIT_TRX) | CPM_CR_FLG;
208
209         while (cp->cp_cpcr & CPM_CR_FLG)        /* wait if cp is busy */
210                 ;
211
212         /* Enable transmitter/receiver.
213          */
214         sp->smc_smcmr |= SMCMR_REN | SMCMR_TEN;
215 }
216
217 static void smc_putc (int smc_index, const char c)
218 {
219         volatile cbd_t *tbdf;
220         volatile char *buf;
221         volatile smc_uart_t *up;
222         volatile immap_t *im = (immap_t *) CFG_IMMR;
223         volatile cpm8xx_t *cpmp = &(im->im_cpm);
224
225         up = (smc_uart_t *) & cpmp->cp_dparam[proff_smc[smc_index]];
226
227         tbdf = (cbd_t *) & cpmp->cp_dpmem[up->smc_tbase];
228
229         /* Wait for last character to go.
230          */
231
232         buf = (char *) tbdf->cbd_bufaddr;
233 #if 0
234         __asm__ ("eieio");
235         while (tbdf->cbd_sc & BD_SC_READY)
236                 __asm__ ("eieio");
237 #endif
238
239         *buf = c;
240         tbdf->cbd_datlen = 1;
241         tbdf->cbd_sc |= BD_SC_READY;
242         __asm__ ("eieio");
243 #if 1
244         while (tbdf->cbd_sc & BD_SC_READY)
245                 __asm__ ("eieio");
246 #endif
247 }
248
249 static int smc_getc (int smc_index)
250 {
251         volatile cbd_t *rbdf;
252         volatile unsigned char *buf;
253         volatile smc_uart_t *up;
254         volatile immap_t *im = (immap_t *) CFG_IMMR;
255         volatile cpm8xx_t *cpmp = &(im->im_cpm);
256         unsigned char c;
257         int i;
258
259         up = (smc_uart_t *) & cpmp->cp_dparam[proff_smc[smc_index]];
260
261         rbdf = (cbd_t *) & cpmp->cp_dpmem[up->smc_rbase];
262
263         /* Wait for character to show up.
264          */
265         buf = (unsigned char *) rbdf->cbd_bufaddr;
266 #if 0
267         while (rbdf->cbd_sc & BD_SC_EMPTY);
268 #else
269         for (i = 100; i > 0; i--) {
270                 if (!(rbdf->cbd_sc & BD_SC_EMPTY))
271                         break;
272                 udelay (1000);
273         }
274
275         if (i == 0)
276                 return -1;
277 #endif
278         c = *buf;
279         rbdf->cbd_sc |= BD_SC_EMPTY;
280
281         return (c);
282 }
283
284   /*
285    * SCC callbacks
286    */
287
288 static void scc_init (int scc_index)
289 {
290         DECLARE_GLOBAL_DATA_PTR;
291
292         static int cpm_cr_ch[] = {
293                 CPM_CR_CH_SCC1,
294                 CPM_CR_CH_SCC2,
295                 CPM_CR_CH_SCC3,
296                 CPM_CR_CH_SCC4,
297         };
298
299         volatile immap_t *im = (immap_t *) CFG_IMMR;
300         volatile scc_t *sp;
301         volatile scc_uart_t *up;
302         volatile cbd_t *tbdf, *rbdf;
303         volatile cpm8xx_t *cp = &(im->im_cpm);
304         uint dpaddr;
305
306         /* initialize pointers to SCC */
307
308         sp = (scc_t *) & (cp->cp_scc[scc_index]);
309         up = (scc_uart_t *) & cp->cp_dparam[proff_scc[scc_index]];
310
311         /* Disable transmitter/receiver.
312          */
313         sp->scc_gsmrl &= ~(SCC_GSMRL_ENR | SCC_GSMRL_ENT);
314
315
316         /* Allocate space for two buffer descriptors in the DP ram.
317          */
318
319 #ifdef CFG_ALLOC_DPRAM
320         dpaddr = dpram_alloc_align (sizeof (cbd_t) * 2 + 2, 8);
321 #else
322         dpaddr = CPM_POST_BASE;
323 #endif
324
325         /* Enable SDMA.
326          */
327         im->im_siu_conf.sc_sdcr = 0x0001;
328
329         /* Set the physical address of the host memory buffers in
330          * the buffer descriptors.
331          */
332
333         rbdf = (cbd_t *) & cp->cp_dpmem[dpaddr];
334         rbdf->cbd_bufaddr = (uint) (rbdf + 2);
335         rbdf->cbd_sc = 0;
336         tbdf = rbdf + 1;
337         tbdf->cbd_bufaddr = ((uint) (rbdf + 2)) + 1;
338         tbdf->cbd_sc = 0;
339
340         /* Set up the baud rate generator.
341          */
342         cp->cp_sicr &= ~(0x000000FF << (8 * scc_index));
343         /* no |= needed, since BRG1 is 000 */
344
345         cp->cp_brgc1 =
346                         (((gd->cpu_clk / 16 / gd->baudrate) -
347                           1) << 1) | CPM_BRG_EN;
348
349         /* Set up the uart parameters in the parameter ram.
350          */
351         up->scc_genscc.scc_rbase = dpaddr;
352         up->scc_genscc.scc_tbase = dpaddr + sizeof (cbd_t);
353
354         /* Initialize Tx/Rx parameters.
355          */
356         while (cp->cp_cpcr & CPM_CR_FLG)        /* wait if cp is busy */
357                 ;
358         cp->cp_cpcr =
359                         mk_cr_cmd (cpm_cr_ch[scc_index], CPM_CR_INIT_TRX) | CPM_CR_FLG;
360
361         while (cp->cp_cpcr & CPM_CR_FLG)        /* wait if cp is busy */
362                 ;
363
364         up->scc_genscc.scc_rfcr = SCC_EB | 0x05;
365         up->scc_genscc.scc_tfcr = SCC_EB | 0x05;
366
367         up->scc_genscc.scc_mrblr = 1;   /* Single character receive */
368         up->scc_maxidl = 0;             /* disable max idle */
369         up->scc_brkcr = 1;              /* send one break character on stop TX */
370         up->scc_parec = 0;
371         up->scc_frmec = 0;
372         up->scc_nosec = 0;
373         up->scc_brkec = 0;
374         up->scc_uaddr1 = 0;
375         up->scc_uaddr2 = 0;
376         up->scc_toseq = 0;
377         up->scc_char1 = 0x8000;
378         up->scc_char2 = 0x8000;
379         up->scc_char3 = 0x8000;
380         up->scc_char4 = 0x8000;
381         up->scc_char5 = 0x8000;
382         up->scc_char6 = 0x8000;
383         up->scc_char7 = 0x8000;
384         up->scc_char8 = 0x8000;
385         up->scc_rccm = 0xc0ff;
386
387         /* Set low latency / small fifo.
388          */
389         sp->scc_gsmrh = SCC_GSMRH_RFW;
390
391         /* Set UART mode
392          */
393         sp->scc_gsmrl &= ~0xF;
394         sp->scc_gsmrl |= SCC_GSMRL_MODE_UART;
395
396         /* Set local loopback mode.
397          */
398         sp->scc_gsmrl &= ~SCC_GSMRL_DIAG_LE;
399         sp->scc_gsmrl |= SCC_GSMRL_DIAG_LOOP;
400
401         /* Set clock divider 16 on Tx and Rx
402          */
403         sp->scc_gsmrl |= (SCC_GSMRL_TDCR_16 | SCC_GSMRL_RDCR_16);
404
405         sp->scc_psmr |= SCU_PSMR_CL;
406
407         /* Mask all interrupts and remove anything pending.
408          */
409         sp->scc_sccm = 0;
410         sp->scc_scce = 0xffff;
411         sp->scc_dsr = 0x7e7e;
412         sp->scc_psmr = 0x3000;
413
414         /* Make the first buffer the only buffer.
415          */
416         tbdf->cbd_sc |= BD_SC_WRAP;
417         rbdf->cbd_sc |= BD_SC_EMPTY | BD_SC_WRAP;
418
419         /* Enable transmitter/receiver.
420          */
421         sp->scc_gsmrl |= (SCC_GSMRL_ENR | SCC_GSMRL_ENT);
422 }
423
424 static void scc_putc (int scc_index, const char c)
425 {
426         volatile cbd_t *tbdf;
427         volatile char *buf;
428         volatile scc_uart_t *up;
429         volatile immap_t *im = (immap_t *) CFG_IMMR;
430         volatile cpm8xx_t *cpmp = &(im->im_cpm);
431
432         up = (scc_uart_t *) & cpmp->cp_dparam[proff_scc[scc_index]];
433
434         tbdf = (cbd_t *) & cpmp->cp_dpmem[up->scc_genscc.scc_tbase];
435
436         /* Wait for last character to go.
437          */
438
439         buf = (char *) tbdf->cbd_bufaddr;
440 #if 0
441         __asm__ ("eieio");
442         while (tbdf->cbd_sc & BD_SC_READY)
443                 __asm__ ("eieio");
444 #endif
445
446         *buf = c;
447         tbdf->cbd_datlen = 1;
448         tbdf->cbd_sc |= BD_SC_READY;
449         __asm__ ("eieio");
450 #if 1
451         while (tbdf->cbd_sc & BD_SC_READY)
452                 __asm__ ("eieio");
453 #endif
454 }
455
456 static int scc_getc (int scc_index)
457 {
458         volatile cbd_t *rbdf;
459         volatile unsigned char *buf;
460         volatile scc_uart_t *up;
461         volatile immap_t *im = (immap_t *) CFG_IMMR;
462         volatile cpm8xx_t *cpmp = &(im->im_cpm);
463         unsigned char c;
464         int i;
465
466         up = (scc_uart_t *) & cpmp->cp_dparam[proff_scc[scc_index]];
467
468         rbdf = (cbd_t *) & cpmp->cp_dpmem[up->scc_genscc.scc_rbase];
469
470         /* Wait for character to show up.
471          */
472         buf = (unsigned char *) rbdf->cbd_bufaddr;
473 #if 0
474         while (rbdf->cbd_sc & BD_SC_EMPTY);
475 #else
476         for (i = 100; i > 0; i--) {
477                 if (!(rbdf->cbd_sc & BD_SC_EMPTY))
478                         break;
479                 udelay (1000);
480         }
481
482         if (i == 0)
483                 return -1;
484 #endif
485         c = *buf;
486         rbdf->cbd_sc |= BD_SC_EMPTY;
487
488         return (c);
489 }
490
491   /*
492    * Test routines
493    */
494
495 static int test_ctlr (int ctlr, int index)
496 {
497         int res = -1;
498         char test_str[] = "*** UART Test String ***\r\n";
499         int i;
500
501 #if !defined(CONFIG_8xx_CONS_NONE)
502         if (used_by_uart[ctlr] == index) {
503                 while (ctlr_proc[ctlr].getc (index) != -1);
504         }
505 #endif
506
507         ctlr_proc[ctlr].init (index);
508
509         for (i = 0; i < sizeof (test_str) - 1; i++) {
510                 ctlr_proc[ctlr].putc (index, test_str[i]);
511                 if (ctlr_proc[ctlr].getc (index) != test_str[i])
512                         goto Done;
513         }
514
515         res = 0;
516
517   Done:
518
519 #if !defined(CONFIG_8xx_CONS_NONE)
520         if (used_by_uart[ctlr] == index) {
521                 serial_init ();
522         }
523 #endif
524
525 #if defined(SCC_ENET)
526         if (used_by_ether[ctlr] == index) {
527                 DECLARE_GLOBAL_DATA_PTR;
528
529                 eth_init (gd->bd);
530         }
531 #endif
532
533         if (res != 0) {
534                 post_log ("uart %s%d test failed\n",
535                                 ctlr_name[ctlr], index + 1);
536         }
537
538         return res;
539 }
540
541 int uart_post_test (int flags)
542 {
543         int res = 0;
544         int i;
545
546 #if defined(CONFIG_8xx_CONS_SMC1)
547         used_by_uart[CTLR_SMC] = 0;
548 #elif defined(CONFIG_8xx_CONS_SMC2)
549         used_by_uart[CTLR_SMC] = 1;
550 #elif defined(CONFIG_8xx_CONS_SCC1)
551         used_by_uart[CTLR_SCC] = 0;
552 #elif defined(CONFIG_8xx_CONS_SCC2)
553         used_by_uart[CTLR_SCC] = 1;
554 #elif defined(CONFIG_8xx_CONS_SCC3)
555         used_by_uart[CTLR_SCC] = 2;
556 #elif defined(CONFIG_8xx_CONS_SCC4)
557         used_by_uart[CTLR_SCC] = 3;
558 #endif
559
560 #if defined(SCC_ENET)
561         used_by_ether[CTLR_SCC] = SCC_ENET;
562 #endif
563
564         ctlr_proc[CTLR_SMC].init = smc_init;
565         ctlr_proc[CTLR_SMC].putc = smc_putc;
566         ctlr_proc[CTLR_SMC].getc = smc_getc;
567
568         ctlr_proc[CTLR_SCC].init = scc_init;
569         ctlr_proc[CTLR_SCC].putc = scc_putc;
570         ctlr_proc[CTLR_SCC].getc = scc_getc;
571
572         for (i = 0; i < CTRL_LIST_SIZE; i++) {
573                 if (test_ctlr (ctlr_list[i][0], ctlr_list[i][1]) != 0) {
574                         res = -1;
575                 }
576         }
577
578         return res;
579 }
580
581 #endif /* CONFIG_POST & CFG_POST_UART */
582
583 #endif /* CONFIG_POST */