* Patch by Yuli Barcohen, 3 Dec 2003:
[platform/kernel/u-boot.git] / cpu / mpc8xx / serial.c
1 /*
2  * (C) Copyright 2000
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 #include <commproc.h>
26 #include <command.h>
27 #include <watchdog.h>
28
29 #if !defined(CONFIG_8xx_CONS_NONE)      /* No Console at all */
30
31 #if defined(CONFIG_8xx_CONS_SMC1)       /* Console on SMC1 */
32 #define SMC_INDEX       0
33 #undef  SCC_INDEX
34 #define PROFF_SMC       PROFF_SMC1
35 #define CPM_CR_CH_SMC   CPM_CR_CH_SMC1
36
37 #elif defined(CONFIG_8xx_CONS_SMC2)     /* Console on SMC2 */
38 #define SMC_INDEX       1
39 #undef  SCC_INDEX
40 #define PROFF_SMC       PROFF_SMC2
41 #define CPM_CR_CH_SMC   CPM_CR_CH_SMC2
42
43 #elif defined(CONFIG_8xx_CONS_SCC1)     /* Console on SCC1 */
44 #undef  SMC_INDEX
45 #define SCC_INDEX       0
46 #define PROFF_SCC       PROFF_SCC1
47 #define CPM_CR_CH_SCC   CPM_CR_CH_SCC1
48
49 #elif defined(CONFIG_8xx_CONS_SCC2)     /* Console on SCC2 */
50 #undef  SMC_INDEX
51 #define SCC_INDEX       1
52 #define PROFF_SCC       PROFF_SCC2
53 #define CPM_CR_CH_SCC   CPM_CR_CH_SCC2
54
55 #elif defined(CONFIG_8xx_CONS_SCC3)     /* Console on SCC3 */
56 #undef  SMC_INDEX
57 #define SCC_INDEX       2
58 #define PROFF_SCC       PROFF_SCC3
59 #define CPM_CR_CH_SCC   CPM_CR_CH_SCC3
60
61 #elif defined(CONFIG_8xx_CONS_SCC4)     /* Console on SCC4 */
62 #undef  SMC_INDEX
63 #define SCC_INDEX       3
64 #define PROFF_SCC       PROFF_SCC4
65 #define CPM_CR_CH_SCC   CPM_CR_CH_SCC4
66
67 #else /* CONFIG_8xx_CONS_? */
68 #error "console not correctly defined"
69 #endif
70
71 static void serial_setdivisor(volatile cpm8xx_t *cp)
72 {
73         DECLARE_GLOBAL_DATA_PTR;
74         int divisor=gd->cpu_clk/16/gd->baudrate;
75
76         if(divisor/16>0x1000) {
77                 /* bad divisor, assume 50Mhz clock and 9600 baud */
78                 divisor=(50*1000*1000)/16/9600;
79         }
80
81         if(divisor<=0x1000) {
82                 cp->cp_brgc1=((divisor-1)<<1) | CPM_BRG_EN;
83         } else {
84                 cp->cp_brgc1=((divisor/16-1)<<1) | CPM_BRG_EN | CPM_BRG_DIV16;
85         }
86 }
87
88 #if (defined (CONFIG_8xx_CONS_SMC1) || defined (CONFIG_8xx_CONS_SMC2))
89
90 /*
91  * Minimal serial functions needed to use one of the SMC ports
92  * as serial console interface.
93  */
94
95 int serial_init (void)
96 {
97         volatile immap_t *im = (immap_t *)CFG_IMMR;
98         volatile smc_t *sp;
99         volatile smc_uart_t *up;
100         volatile cbd_t *tbdf, *rbdf;
101         volatile cpm8xx_t *cp = &(im->im_cpm);
102 #if (!defined(CONFIG_8xx_CONS_SMC1)) && (defined(CONFIG_MPC823) || defined(CONFIG_MPC850))
103         volatile iop8xx_t *ip = (iop8xx_t *)&(im->im_ioport);
104 #endif
105         uint    dpaddr;
106
107         /* initialize pointers to SMC */
108
109         sp = (smc_t *) &(cp->cp_smc[SMC_INDEX]);
110         up = (smc_uart_t *) &cp->cp_dparam[PROFF_SMC];
111
112         /* Disable transmitter/receiver.
113         */
114         sp->smc_smcmr &= ~(SMCMR_REN | SMCMR_TEN);
115
116         /* Enable SDMA.
117         */
118         im->im_siu_conf.sc_sdcr = 1;
119
120         /* clear error conditions */
121 #ifdef  CFG_SDSR
122         im->im_sdma.sdma_sdsr = CFG_SDSR;
123 #else
124         im->im_sdma.sdma_sdsr = 0x83;
125 #endif
126
127         /* clear SDMA interrupt mask */
128 #ifdef  CFG_SDMR
129         im->im_sdma.sdma_sdmr = CFG_SDMR;
130 #else
131         im->im_sdma.sdma_sdmr = 0x00;
132 #endif
133
134 #if defined(CONFIG_8xx_CONS_SMC1)
135         /* Use Port B for SMC1 instead of other functions.
136         */
137         cp->cp_pbpar |=  0x000000c0;
138         cp->cp_pbdir &= ~0x000000c0;
139         cp->cp_pbodr &= ~0x000000c0;
140 #else   /* CONFIG_8xx_CONS_SMC2 */
141 # if defined(CONFIG_MPC823) || defined(CONFIG_MPC850)
142         /* Use Port A for SMC2 instead of other functions.
143         */
144         ip->iop_papar |=  0x00c0;
145         ip->iop_padir &= ~0x00c0;
146         ip->iop_paodr &= ~0x00c0;
147 # else  /* must be a 860 then */
148         /* Use Port B for SMC2 instead of other functions.
149         */
150         cp->cp_pbpar |=  0x00000c00;
151         cp->cp_pbdir &= ~0x00000c00;
152         cp->cp_pbodr &= ~0x00000c00;
153 # endif
154 #endif
155
156 #if defined(CONFIG_FADS) || defined(CONFIG_ADS)
157         /* Enable RS232 */
158 #if defined(CONFIG_8xx_CONS_SMC1)
159         *((uint *) BCSR1) &= ~BCSR1_RS232EN_1;
160 #else
161         *((uint *) BCSR1) &= ~BCSR1_RS232EN_2;
162 #endif
163 #endif  /* CONFIG_FADS */
164
165 #if defined(CONFIG_RPXLITE) || defined(CONFIG_RPXCLASSIC)
166         /* Enable Monitor Port Transceiver */
167         *((uchar *) BCSR0) |= BCSR0_ENMONXCVR ;
168 #endif /* CONFIG_RPXLITE */
169
170         /* Set the physical address of the host memory buffers in
171          * the buffer descriptors.
172          */
173
174 #ifdef CFG_ALLOC_DPRAM
175         dpaddr = dpram_alloc_align (sizeof(cbd_t)*2 + 2, 8) ;
176 #else
177         dpaddr = CPM_SERIAL_BASE ;
178 #endif
179
180         /* Allocate space for two buffer descriptors in the DP ram.
181          * For now, this address seems OK, but it may have to
182          * change with newer versions of the firmware.
183          * damm: allocating space after the two buffers for rx/tx data
184          */
185
186         rbdf = (cbd_t *)&cp->cp_dpmem[dpaddr];
187         rbdf->cbd_bufaddr = (uint) (rbdf+2);
188         rbdf->cbd_sc = 0;
189         tbdf = rbdf + 1;
190         tbdf->cbd_bufaddr = ((uint) (rbdf+2)) + 1;
191         tbdf->cbd_sc = 0;
192
193         /* Set up the uart parameters in the parameter ram.
194         */
195         up->smc_rbase = dpaddr;
196         up->smc_tbase = dpaddr+sizeof(cbd_t);
197         up->smc_rfcr = SMC_EB;
198         up->smc_tfcr = SMC_EB;
199
200 #if defined(CONFIG_MBX)
201         board_serial_init();
202 #endif  /* CONFIG_MBX */
203
204         /* Set UART mode, 8 bit, no parity, one stop.
205          * Enable receive and transmit.
206          */
207         sp->smc_smcmr = smcr_mk_clen(9) |  SMCMR_SM_UART;
208
209         /* Mask all interrupts and remove anything pending.
210         */
211         sp->smc_smcm = 0;
212         sp->smc_smce = 0xff;
213
214         /* Set up the baud rate generator.
215         */
216         serial_setbrg ();
217
218         /* Make the first buffer the only buffer.
219         */
220         tbdf->cbd_sc |= BD_SC_WRAP;
221         rbdf->cbd_sc |= BD_SC_EMPTY | BD_SC_WRAP;
222
223         /* Single character receive.
224         */
225         up->smc_mrblr = 1;
226         up->smc_maxidl = 0;
227
228         /* Initialize Tx/Rx parameters.
229         */
230
231         while (cp->cp_cpcr & CPM_CR_FLG)  /* wait if cp is busy */
232           ;
233
234         cp->cp_cpcr = mk_cr_cmd(CPM_CR_CH_SMC, CPM_CR_INIT_TRX) | CPM_CR_FLG;
235
236         while (cp->cp_cpcr & CPM_CR_FLG)  /* wait if cp is busy */
237           ;
238
239         /* Enable transmitter/receiver.
240         */
241         sp->smc_smcmr |= SMCMR_REN | SMCMR_TEN;
242
243         return (0);
244 }
245
246 void
247 serial_setbrg (void)
248 {
249         volatile immap_t *im = (immap_t *)CFG_IMMR;
250         volatile cpm8xx_t *cp = &(im->im_cpm);
251
252         /* Set up the baud rate generator.
253          * See 8xx_io/commproc.c for details.
254          *
255          * Wire BRG1 to SMCx
256          */
257
258         cp->cp_simode = 0x00000000;
259
260         serial_setdivisor(cp);
261 }
262
263 #ifdef CONFIG_MODEM_SUPPORT
264 void disable_putc(void)
265 {
266         DECLARE_GLOBAL_DATA_PTR;
267         gd->be_quiet = 1;
268 }
269
270 void enable_putc(void)
271 {
272         DECLARE_GLOBAL_DATA_PTR;
273         gd->be_quiet = 0;
274 }
275 #endif
276
277 void
278 serial_putc(const char c)
279 {
280         volatile cbd_t          *tbdf;
281         volatile char           *buf;
282         volatile smc_uart_t     *up;
283         volatile immap_t        *im = (immap_t *)CFG_IMMR;
284         volatile cpm8xx_t       *cpmp = &(im->im_cpm);
285
286 #ifdef CONFIG_MODEM_SUPPORT
287         DECLARE_GLOBAL_DATA_PTR;
288
289         if (gd->be_quiet)
290                 return;
291 #endif
292
293         if (c == '\n')
294                 serial_putc ('\r');
295
296         up = (smc_uart_t *)&cpmp->cp_dparam[PROFF_SMC];
297
298         tbdf = (cbd_t *)&cpmp->cp_dpmem[up->smc_tbase];
299
300         /* Wait for last character to go.
301         */
302
303         buf = (char *)tbdf->cbd_bufaddr;
304
305         *buf = c;
306         tbdf->cbd_datlen = 1;
307         tbdf->cbd_sc |= BD_SC_READY;
308         __asm__("eieio");
309
310         while (tbdf->cbd_sc & BD_SC_READY) {
311                 WATCHDOG_RESET ();
312                 __asm__("eieio");
313         }
314 }
315
316 int
317 serial_getc(void)
318 {
319         volatile cbd_t          *rbdf;
320         volatile unsigned char  *buf;
321         volatile smc_uart_t     *up;
322         volatile immap_t        *im = (immap_t *)CFG_IMMR;
323         volatile cpm8xx_t       *cpmp = &(im->im_cpm);
324         unsigned char           c;
325
326         up = (smc_uart_t *)&cpmp->cp_dparam[PROFF_SMC];
327
328         rbdf = (cbd_t *)&cpmp->cp_dpmem[up->smc_rbase];
329
330         /* Wait for character to show up.
331         */
332         buf = (unsigned char *)rbdf->cbd_bufaddr;
333
334         while (rbdf->cbd_sc & BD_SC_EMPTY)
335                 WATCHDOG_RESET ();
336
337         c = *buf;
338         rbdf->cbd_sc |= BD_SC_EMPTY;
339
340         return(c);
341 }
342
343 int
344 serial_tstc()
345 {
346         volatile cbd_t          *rbdf;
347         volatile smc_uart_t     *up;
348         volatile immap_t        *im = (immap_t *)CFG_IMMR;
349         volatile cpm8xx_t       *cpmp = &(im->im_cpm);
350
351         up = (smc_uart_t *)&cpmp->cp_dparam[PROFF_SMC];
352
353         rbdf = (cbd_t *)&cpmp->cp_dpmem[up->smc_rbase];
354
355         return(!(rbdf->cbd_sc & BD_SC_EMPTY));
356 }
357
358 #else   /* ! CONFIG_8xx_CONS_SMC1, CONFIG_8xx_CONS_SMC2 */
359
360 int serial_init (void)
361 {
362         volatile immap_t *im = (immap_t *)CFG_IMMR;
363         volatile scc_t *sp;
364         volatile scc_uart_t *up;
365         volatile cbd_t *tbdf, *rbdf;
366         volatile cpm8xx_t *cp = &(im->im_cpm);
367         uint     dpaddr;
368 #if (SCC_INDEX != 2) || !defined(CONFIG_MPC850)
369         volatile iop8xx_t *ip = (iop8xx_t *)&(im->im_ioport);
370 #endif
371
372         /* initialize pointers to SCC */
373
374         sp = (scc_t *) &(cp->cp_scc[SCC_INDEX]);
375         up = (scc_uart_t *) &cp->cp_dparam[PROFF_SCC];
376
377 #if defined(CONFIG_LWMON) && defined(CONFIG_8xx_CONS_SCC2)
378     {   /* Disable Ethernet, enable Serial */
379         uchar c;
380
381         c = pic_read  (0x61);
382         c &= ~0x40;     /* enable COM3 */
383         c |=  0x80;     /* disable Ethernet */
384         pic_write (0x61, c);
385
386         /* enable RTS2 */
387         cp->cp_pbpar |=  0x2000;
388         cp->cp_pbdat |=  0x2000;
389         cp->cp_pbdir |=  0x2000;
390     }
391 #endif  /* CONFIG_LWMON */
392
393         /* Disable transmitter/receiver.
394         */
395         sp->scc_gsmrl &= ~(SCC_GSMRL_ENR | SCC_GSMRL_ENT);
396
397 #if (SCC_INDEX == 2) && defined(CONFIG_MPC850)
398         /*
399          * The MPC850 has SCC3 on Port B
400          */
401         cp->cp_pbpar |=  0x06;
402         cp->cp_pbdir &= ~0x06;
403         cp->cp_pbodr &= ~0x06;
404
405 #elif (SCC_INDEX < 2) || !defined(CONFIG_IP860)
406         /*
407          * Standard configuration for SCC's is on Part A
408          */
409         ip->iop_papar |=  ((3 << (2 * SCC_INDEX)));
410         ip->iop_padir &= ~((3 << (2 * SCC_INDEX)));
411         ip->iop_paodr &= ~((3 << (2 * SCC_INDEX)));
412 #else
413         /*
414          * The IP860 has SCC3 and SCC4 on Port D
415          */
416         ip->iop_pdpar |=  ((3 << (2 * SCC_INDEX)));
417 #endif
418
419         /* Allocate space for two buffer descriptors in the DP ram.
420          */
421
422 #ifdef CFG_ALLOC_DPRAM
423         dpaddr = dpram_alloc_align (sizeof(cbd_t)*2 + 2, 8) ;
424 #else
425         dpaddr = CPM_SERIAL_BASE ;
426 #endif
427
428         /* Enable SDMA.
429         */
430         im->im_siu_conf.sc_sdcr = 0x0001;
431
432         /* Set the physical address of the host memory buffers in
433          * the buffer descriptors.
434          */
435
436         rbdf = (cbd_t *)&cp->cp_dpmem[dpaddr];
437         rbdf->cbd_bufaddr = (uint) (rbdf+2);
438         rbdf->cbd_sc = 0;
439         tbdf = rbdf + 1;
440         tbdf->cbd_bufaddr = ((uint) (rbdf+2)) + 1;
441         tbdf->cbd_sc = 0;
442
443         /* Set up the baud rate generator.
444         */
445         serial_setbrg ();
446
447         /* Set up the uart parameters in the parameter ram.
448         */
449         up->scc_genscc.scc_rbase = dpaddr;
450         up->scc_genscc.scc_tbase = dpaddr+sizeof(cbd_t);
451
452         /* Initialize Tx/Rx parameters.
453         */
454         while (cp->cp_cpcr & CPM_CR_FLG)  /* wait if cp is busy */
455                 ;
456         cp->cp_cpcr = mk_cr_cmd(CPM_CR_CH_SCC, CPM_CR_INIT_TRX) | CPM_CR_FLG;
457
458         while (cp->cp_cpcr & CPM_CR_FLG)  /* wait if cp is busy */
459                 ;
460
461         up->scc_genscc.scc_rfcr  = SCC_EB | 0x05;
462         up->scc_genscc.scc_tfcr  = SCC_EB | 0x05;
463
464         up->scc_genscc.scc_mrblr = 1;   /* Single character receive */
465         up->scc_maxidl = 0;             /* disable max idle */
466         up->scc_brkcr  = 1;             /* send one break character on stop TX */
467         up->scc_parec  = 0;
468         up->scc_frmec  = 0;
469         up->scc_nosec  = 0;
470         up->scc_brkec  = 0;
471         up->scc_uaddr1 = 0;
472         up->scc_uaddr2 = 0;
473         up->scc_toseq  = 0;
474         up->scc_char1  = 0x8000;
475         up->scc_char2  = 0x8000;
476         up->scc_char3  = 0x8000;
477         up->scc_char4  = 0x8000;
478         up->scc_char5  = 0x8000;
479         up->scc_char6  = 0x8000;
480         up->scc_char7  = 0x8000;
481         up->scc_char8  = 0x8000;
482         up->scc_rccm   = 0xc0ff;
483
484         /* Set low latency / small fifo.
485          */
486         sp->scc_gsmrh = SCC_GSMRH_RFW;
487
488         /* Set SCC(x) clock mode to 16x
489          * See 8xx_io/commproc.c for details.
490          *
491          * Wire BRG1 to SCCn
492          */
493
494         /* Set UART mode, clock divider 16 on Tx and Rx
495          */
496         sp->scc_gsmrl |=
497                 (SCC_GSMRL_MODE_UART | SCC_GSMRL_TDCR_16 | SCC_GSMRL_RDCR_16);
498
499         sp->scc_psmr  |= SCU_PSMR_CL;
500
501         /* Mask all interrupts and remove anything pending.
502         */
503         sp->scc_sccm = 0;
504         sp->scc_scce = 0xffff;
505         sp->scc_dsr  = 0x7e7e;
506         sp->scc_psmr = 0x3000;
507
508         /* Make the first buffer the only buffer.
509         */
510         tbdf->cbd_sc |= BD_SC_WRAP;
511         rbdf->cbd_sc |= BD_SC_EMPTY | BD_SC_WRAP;
512
513         /* Enable transmitter/receiver.
514         */
515         sp->scc_gsmrl |= (SCC_GSMRL_ENR | SCC_GSMRL_ENT);
516
517         return (0);
518 }
519
520 void
521 serial_setbrg (void)
522 {
523         volatile immap_t *im = (immap_t *)CFG_IMMR;
524         volatile cpm8xx_t *cp = &(im->im_cpm);
525
526         /* Set up the baud rate generator.
527          * See 8xx_io/commproc.c for details.
528          *
529          * Wire BRG1 to SCCx
530          */
531
532         cp->cp_sicr &= ~(0x000000FF << (8 * SCC_INDEX));
533
534         serial_setdivisor(cp);
535 }
536
537 void
538 serial_putc(const char c)
539 {
540         volatile cbd_t          *tbdf;
541         volatile char           *buf;
542         volatile scc_uart_t     *up;
543         volatile immap_t        *im = (immap_t *)CFG_IMMR;
544         volatile cpm8xx_t       *cpmp = &(im->im_cpm);
545
546         if (c == '\n')
547                 serial_putc ('\r');
548
549         up = (scc_uart_t *)&cpmp->cp_dparam[PROFF_SCC];
550
551         tbdf = (cbd_t *)&cpmp->cp_dpmem[up->scc_genscc.scc_tbase];
552
553         /* Wait for last character to go.
554         */
555
556         buf = (char *)tbdf->cbd_bufaddr;
557
558         *buf = c;
559         tbdf->cbd_datlen = 1;
560         tbdf->cbd_sc |= BD_SC_READY;
561         __asm__("eieio");
562
563         while (tbdf->cbd_sc & BD_SC_READY) {
564                 __asm__("eieio");
565                 WATCHDOG_RESET ();
566         }
567 }
568
569 int
570 serial_getc(void)
571 {
572         volatile cbd_t          *rbdf;
573         volatile unsigned char  *buf;
574         volatile scc_uart_t     *up;
575         volatile immap_t        *im = (immap_t *)CFG_IMMR;
576         volatile cpm8xx_t       *cpmp = &(im->im_cpm);
577         unsigned char           c;
578
579         up = (scc_uart_t *)&cpmp->cp_dparam[PROFF_SCC];
580
581         rbdf = (cbd_t *)&cpmp->cp_dpmem[up->scc_genscc.scc_rbase];
582
583         /* Wait for character to show up.
584         */
585         buf = (unsigned char *)rbdf->cbd_bufaddr;
586
587         while (rbdf->cbd_sc & BD_SC_EMPTY)
588                 WATCHDOG_RESET ();
589
590         c = *buf;
591         rbdf->cbd_sc |= BD_SC_EMPTY;
592
593         return(c);
594 }
595
596 int
597 serial_tstc()
598 {
599         volatile cbd_t          *rbdf;
600         volatile scc_uart_t     *up;
601         volatile immap_t        *im = (immap_t *)CFG_IMMR;
602         volatile cpm8xx_t       *cpmp = &(im->im_cpm);
603
604         up = (scc_uart_t *)&cpmp->cp_dparam[PROFF_SCC];
605
606         rbdf = (cbd_t *)&cpmp->cp_dpmem[up->scc_genscc.scc_rbase];
607
608         return(!(rbdf->cbd_sc & BD_SC_EMPTY));
609 }
610
611 #endif  /* CONFIG_8xx_CONS_SMC1, CONFIG_8xx_CONS_SMC2 */
612
613
614 void
615 serial_puts (const char *s)
616 {
617         while (*s) {
618                 serial_putc (*s++);
619         }
620 }
621
622
623 #if (CONFIG_COMMANDS & CFG_CMD_KGDB)
624
625 void
626 kgdb_serial_init(void)
627 {
628 #if defined(CONFIG_8xx_CONS_SMC1)
629         serial_printf("[on SMC1] ");
630 #elif defined(CONFIG_8xx_CONS_SMC2)
631         serial_printf("[on SMC2] ");
632 #elif defined(CONFIG_8xx_CONS_SCC1)
633         serial_printf("[on SCC1] ");
634 #elif defined(CONFIG_8xx_CONS_SCC2)
635         serial_printf("[on SCC2] ");
636 #elif defined(CONFIG_8xx_CONS_SCC3)
637         serial_printf("[on SCC3] ");
638 #elif defined(CONFIG_8xx_CONS_SCC4)
639         serial_printf("[on SCC4] ");
640 #endif
641 }
642
643 void
644 putDebugChar (int c)
645 {
646         serial_putc (c);
647 }
648
649 void
650 putDebugStr (const char *str)
651 {
652         serial_puts (str);
653 }
654
655 int
656 getDebugChar (void)
657 {
658         return serial_getc();
659 }
660
661 void
662 kgdb_interruptible (int yes)
663 {
664         return;
665 }
666 #endif  /* CFG_CMD_KGDB */
667
668 #endif  /* CONFIG_8xx_CONS_NONE */