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