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