3 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
5 * See file CREDITS for list of people who contributed to this
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.
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.
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,
30 DECLARE_GLOBAL_DATA_PTR;
32 #if !defined(CONFIG_8xx_CONS_NONE) /* No Console at all */
34 #if defined(CONFIG_8xx_CONS_SMC1) /* Console on SMC1 */
36 #define PROFF_SMC PROFF_SMC1
37 #define CPM_CR_CH_SMC CPM_CR_CH_SMC1
39 #elif defined(CONFIG_8xx_CONS_SMC2) /* Console on SMC2 */
41 #define PROFF_SMC PROFF_SMC2
42 #define CPM_CR_CH_SMC CPM_CR_CH_SMC2
44 #endif /* CONFIG_8xx_CONS_SMCx */
46 #if defined(CONFIG_8xx_CONS_SCC1) /* Console on SCC1 */
48 #define PROFF_SCC PROFF_SCC1
49 #define CPM_CR_CH_SCC CPM_CR_CH_SCC1
51 #elif defined(CONFIG_8xx_CONS_SCC2) /* Console on SCC2 */
53 #define PROFF_SCC PROFF_SCC2
54 #define CPM_CR_CH_SCC CPM_CR_CH_SCC2
56 #elif defined(CONFIG_8xx_CONS_SCC3) /* Console on SCC3 */
58 #define PROFF_SCC PROFF_SCC3
59 #define CPM_CR_CH_SCC CPM_CR_CH_SCC3
61 #elif defined(CONFIG_8xx_CONS_SCC4) /* Console on SCC4 */
63 #define PROFF_SCC PROFF_SCC4
64 #define CPM_CR_CH_SCC CPM_CR_CH_SCC4
66 #endif /* CONFIG_8xx_CONS_SCCx */
68 #if !defined(CONFIG_SYS_SMC_RXBUFLEN)
69 #define CONFIG_SYS_SMC_RXBUFLEN 1
70 #define CONFIG_SYS_MAXIDLE 0
72 #if !defined(CONFIG_SYS_MAXIDLE)
73 #error "you must define CONFIG_SYS_MAXIDLE"
77 typedef volatile struct serialbuffer {
78 cbd_t rxbd; /* Rx BD */
79 cbd_t txbd; /* Tx BD */
80 uint rxindex; /* index for next character to read */
81 volatile uchar rxbuf[CONFIG_SYS_SMC_RXBUFLEN];/* rx buffers */
82 volatile uchar txbuf; /* tx buffers */
85 static void serial_setdivisor(volatile cpm8xx_t *cp)
87 int divisor=(gd->cpu_clk + 8*gd->baudrate)/16/gd->baudrate;
89 if(divisor/16>0x1000) {
90 /* bad divisor, assume 50MHz clock and 9600 baud */
91 divisor=(50*1000*1000 + 8*9600)/16/9600;
94 #ifdef CONFIG_SYS_BRGCLK_PRESCALE
95 divisor /= CONFIG_SYS_BRGCLK_PRESCALE;
99 cp->cp_brgc1=((divisor-1)<<1) | CPM_BRG_EN;
101 cp->cp_brgc1=((divisor/16-1)<<1) | CPM_BRG_EN | CPM_BRG_DIV16;
105 #if (defined (CONFIG_8xx_CONS_SMC1) || defined (CONFIG_8xx_CONS_SMC2))
108 * Minimal serial functions needed to use one of the SMC ports
109 * as serial console interface.
112 static void smc_setbrg (void)
114 volatile immap_t *im = (immap_t *)CONFIG_SYS_IMMR;
115 volatile cpm8xx_t *cp = &(im->im_cpm);
117 /* Set up the baud rate generator.
118 * See 8xx_io/commproc.c for details.
123 cp->cp_simode = 0x00000000;
125 serial_setdivisor(cp);
128 static int smc_init (void)
130 volatile immap_t *im = (immap_t *)CONFIG_SYS_IMMR;
132 volatile smc_uart_t *up;
133 volatile cpm8xx_t *cp = &(im->im_cpm);
134 #if (!defined(CONFIG_8xx_CONS_SMC1)) && (defined(CONFIG_MPC823) || defined(CONFIG_MPC850))
135 volatile iop8xx_t *ip = (iop8xx_t *)&(im->im_ioport);
138 volatile serialbuffer_t *rtx;
140 /* initialize pointers to SMC */
142 sp = (smc_t *) &(cp->cp_smc[SMC_INDEX]);
143 up = (smc_uart_t *) &cp->cp_dparam[PROFF_SMC];
144 #ifdef CONFIG_SYS_SMC_UCODE_PATCH
145 up = (smc_uart_t *) &cp->cp_dpmem[up->smc_rpbase];
147 /* Disable relocation */
151 /* Disable transmitter/receiver.
153 sp->smc_smcmr &= ~(SMCMR_REN | SMCMR_TEN);
157 im->im_siu_conf.sc_sdcr = 1;
159 /* clear error conditions */
160 #ifdef CONFIG_SYS_SDSR
161 im->im_sdma.sdma_sdsr = CONFIG_SYS_SDSR;
163 im->im_sdma.sdma_sdsr = 0x83;
166 /* clear SDMA interrupt mask */
167 #ifdef CONFIG_SYS_SDMR
168 im->im_sdma.sdma_sdmr = CONFIG_SYS_SDMR;
170 im->im_sdma.sdma_sdmr = 0x00;
173 #if defined(CONFIG_8xx_CONS_SMC1)
174 /* Use Port B for SMC1 instead of other functions.
176 cp->cp_pbpar |= 0x000000c0;
177 cp->cp_pbdir &= ~0x000000c0;
178 cp->cp_pbodr &= ~0x000000c0;
179 #else /* CONFIG_8xx_CONS_SMC2 */
180 # if defined(CONFIG_MPC823) || defined(CONFIG_MPC850)
181 /* Use Port A for SMC2 instead of other functions.
183 ip->iop_papar |= 0x00c0;
184 ip->iop_padir &= ~0x00c0;
185 ip->iop_paodr &= ~0x00c0;
186 # else /* must be a 860 then */
187 /* Use Port B for SMC2 instead of other functions.
189 cp->cp_pbpar |= 0x00000c00;
190 cp->cp_pbdir &= ~0x00000c00;
191 cp->cp_pbodr &= ~0x00000c00;
195 #if defined(CONFIG_FADS) || defined(CONFIG_ADS)
197 #if defined(CONFIG_8xx_CONS_SMC1)
198 *((uint *) BCSR1) &= ~BCSR1_RS232EN_1;
200 *((uint *) BCSR1) &= ~BCSR1_RS232EN_2;
202 #endif /* CONFIG_FADS */
204 #if defined(CONFIG_RPXLITE) || defined(CONFIG_RPXCLASSIC)
205 /* Enable Monitor Port Transceiver */
206 *((uchar *) BCSR0) |= BCSR0_ENMONXCVR ;
207 #endif /* CONFIG_RPXLITE */
209 /* Set the physical address of the host memory buffers in
210 * the buffer descriptors.
213 #ifdef CONFIG_SYS_ALLOC_DPRAM
215 * size of struct serialbuffer with bd rx/tx, buffer rx/tx and rx index
217 dpaddr = dpram_alloc_align((sizeof(serialbuffer_t)), 8);
219 dpaddr = CPM_SERIAL_BASE ;
222 rtx = (serialbuffer_t *)&cp->cp_dpmem[dpaddr];
223 /* Allocate space for two buffer descriptors in the DP ram.
224 * For now, this address seems OK, but it may have to
225 * change with newer versions of the firmware.
226 * damm: allocating space after the two buffers for rx/tx data
229 rtx->rxbd.cbd_bufaddr = (uint) &rtx->rxbuf;
230 rtx->rxbd.cbd_sc = 0;
232 rtx->txbd.cbd_bufaddr = (uint) &rtx->txbuf;
233 rtx->txbd.cbd_sc = 0;
235 /* Set up the uart parameters in the parameter ram.
237 up->smc_rbase = dpaddr;
238 up->smc_tbase = dpaddr+sizeof(cbd_t);
239 up->smc_rfcr = SMC_EB;
240 up->smc_tfcr = SMC_EB;
241 #if defined (CONFIG_SYS_SMC_UCODE_PATCH)
242 up->smc_rbptr = up->smc_rbase;
243 up->smc_tbptr = up->smc_tbase;
248 #if defined(CONFIG_MBX)
250 #endif /* CONFIG_MBX */
252 /* Set UART mode, 8 bit, no parity, one stop.
253 * Enable receive and transmit.
255 sp->smc_smcmr = smcr_mk_clen(9) | SMCMR_SM_UART;
257 /* Mask all interrupts and remove anything pending.
262 #ifdef CONFIG_SYS_SPC1920_SMC1_CLK4
263 /* clock source is PLD */
265 /* set freq to 19200 Baud */
266 *((volatile uchar *) CONFIG_SYS_SPC1920_PLD_BASE+6) = 0x3;
267 /* configure clk4 as input */
268 im->im_ioport.iop_pdpar |= 0x800;
269 im->im_ioport.iop_pddir &= ~0x800;
271 cp->cp_simode = ((cp->cp_simode & ~0xf000) | 0x7000);
273 /* Set up the baud rate generator */
277 /* Make the first buffer the only buffer.
279 rtx->txbd.cbd_sc |= BD_SC_WRAP;
280 rtx->rxbd.cbd_sc |= BD_SC_EMPTY | BD_SC_WRAP;
282 /* single/multi character receive. */
283 up->smc_mrblr = CONFIG_SYS_SMC_RXBUFLEN;
284 up->smc_maxidl = CONFIG_SYS_MAXIDLE;
287 /* Initialize Tx/Rx parameters.
290 while (cp->cp_cpcr & CPM_CR_FLG) /* wait if cp is busy */
293 cp->cp_cpcr = mk_cr_cmd(CPM_CR_CH_SMC, CPM_CR_INIT_TRX) | CPM_CR_FLG;
295 while (cp->cp_cpcr & CPM_CR_FLG) /* wait if cp is busy */
298 /* Enable transmitter/receiver.
300 sp->smc_smcmr |= SMCMR_REN | SMCMR_TEN;
306 smc_putc(const char c)
308 volatile smc_uart_t *up;
309 volatile immap_t *im = (immap_t *)CONFIG_SYS_IMMR;
310 volatile cpm8xx_t *cpmp = &(im->im_cpm);
311 volatile serialbuffer_t *rtx;
313 #ifdef CONFIG_MODEM_SUPPORT
321 up = (smc_uart_t *)&cpmp->cp_dparam[PROFF_SMC];
322 #ifdef CONFIG_SYS_SMC_UCODE_PATCH
323 up = (smc_uart_t *) &cpmp->cp_dpmem[up->smc_rpbase];
326 rtx = (serialbuffer_t *)&cpmp->cp_dpmem[up->smc_rbase];
328 /* Wait for last character to go.
331 rtx->txbd.cbd_datlen = 1;
332 rtx->txbd.cbd_sc |= BD_SC_READY;
335 while (rtx->txbd.cbd_sc & BD_SC_READY) {
342 smc_puts (const char *s)
352 volatile smc_uart_t *up;
353 volatile immap_t *im = (immap_t *)CONFIG_SYS_IMMR;
354 volatile cpm8xx_t *cpmp = &(im->im_cpm);
355 volatile serialbuffer_t *rtx;
358 up = (smc_uart_t *)&cpmp->cp_dparam[PROFF_SMC];
359 #ifdef CONFIG_SYS_SMC_UCODE_PATCH
360 up = (smc_uart_t *) &cpmp->cp_dpmem[up->smc_rpbase];
362 rtx = (serialbuffer_t *)&cpmp->cp_dpmem[up->smc_rbase];
364 /* Wait for character to show up.
366 while (rtx->rxbd.cbd_sc & BD_SC_EMPTY)
369 /* the characters are read one by one,
370 * use the rxindex to know the next char to deliver
372 c = *(unsigned char *) (rtx->rxbd.cbd_bufaddr+rtx->rxindex);
375 /* check if all char are readout, then make prepare for next receive */
376 if (rtx->rxindex >= rtx->rxbd.cbd_datlen) {
378 rtx->rxbd.cbd_sc |= BD_SC_EMPTY;
386 volatile smc_uart_t *up;
387 volatile immap_t *im = (immap_t *)CONFIG_SYS_IMMR;
388 volatile cpm8xx_t *cpmp = &(im->im_cpm);
389 volatile serialbuffer_t *rtx;
391 up = (smc_uart_t *)&cpmp->cp_dparam[PROFF_SMC];
392 #ifdef CONFIG_SYS_SMC_UCODE_PATCH
393 up = (smc_uart_t *) &cpmp->cp_dpmem[up->smc_rpbase];
396 rtx = (serialbuffer_t *)&cpmp->cp_dpmem[up->smc_rbase];
398 return !(rtx->rxbd.cbd_sc & BD_SC_EMPTY);
401 struct serial_device serial_smc_device =
413 #endif /* CONFIG_8xx_CONS_SMC1 || CONFIG_8xx_CONS_SMC2 */
415 #if defined(CONFIG_8xx_CONS_SCC1) || defined(CONFIG_8xx_CONS_SCC2) || \
416 defined(CONFIG_8xx_CONS_SCC3) || defined(CONFIG_8xx_CONS_SCC4)
421 volatile immap_t *im = (immap_t *)CONFIG_SYS_IMMR;
422 volatile cpm8xx_t *cp = &(im->im_cpm);
424 /* Set up the baud rate generator.
425 * See 8xx_io/commproc.c for details.
430 cp->cp_sicr &= ~(0x000000FF << (8 * SCC_INDEX));
432 serial_setdivisor(cp);
435 static int scc_init (void)
437 volatile immap_t *im = (immap_t *)CONFIG_SYS_IMMR;
439 volatile scc_uart_t *up;
440 volatile cbd_t *tbdf, *rbdf;
441 volatile cpm8xx_t *cp = &(im->im_cpm);
443 #if (SCC_INDEX != 2) || !defined(CONFIG_MPC850)
444 volatile iop8xx_t *ip = (iop8xx_t *)&(im->im_ioport);
447 /* initialize pointers to SCC */
449 sp = (scc_t *) &(cp->cp_scc[SCC_INDEX]);
450 up = (scc_uart_t *) &cp->cp_dparam[PROFF_SCC];
452 #if defined(CONFIG_LWMON) && defined(CONFIG_8xx_CONS_SCC2)
453 { /* Disable Ethernet, enable Serial */
457 c &= ~0x40; /* enable COM3 */
458 c |= 0x80; /* disable Ethernet */
462 cp->cp_pbpar |= 0x2000;
463 cp->cp_pbdat |= 0x2000;
464 cp->cp_pbdir |= 0x2000;
466 #endif /* CONFIG_LWMON */
468 /* Disable transmitter/receiver.
470 sp->scc_gsmrl &= ~(SCC_GSMRL_ENR | SCC_GSMRL_ENT);
472 #if (SCC_INDEX == 2) && defined(CONFIG_MPC850)
474 * The MPC850 has SCC3 on Port B
476 cp->cp_pbpar |= 0x06;
477 cp->cp_pbdir &= ~0x06;
478 cp->cp_pbodr &= ~0x06;
480 #elif (SCC_INDEX < 2) || !defined(CONFIG_IP860)
482 * Standard configuration for SCC's is on Part A
484 ip->iop_papar |= ((3 << (2 * SCC_INDEX)));
485 ip->iop_padir &= ~((3 << (2 * SCC_INDEX)));
486 ip->iop_paodr &= ~((3 << (2 * SCC_INDEX)));
489 * The IP860 has SCC3 and SCC4 on Port D
491 ip->iop_pdpar |= ((3 << (2 * SCC_INDEX)));
494 /* Allocate space for two buffer descriptors in the DP ram.
497 #ifdef CONFIG_SYS_ALLOC_DPRAM
498 dpaddr = dpram_alloc_align (sizeof(cbd_t)*2 + 2, 8) ;
500 dpaddr = CPM_SERIAL2_BASE ;
505 im->im_siu_conf.sc_sdcr = 0x0001;
507 /* Set the physical address of the host memory buffers in
508 * the buffer descriptors.
511 rbdf = (cbd_t *)&cp->cp_dpmem[dpaddr];
512 rbdf->cbd_bufaddr = (uint) (rbdf+2);
515 tbdf->cbd_bufaddr = ((uint) (rbdf+2)) + 1;
518 /* Set up the baud rate generator.
522 /* Set up the uart parameters in the parameter ram.
524 up->scc_genscc.scc_rbase = dpaddr;
525 up->scc_genscc.scc_tbase = dpaddr+sizeof(cbd_t);
527 /* Initialize Tx/Rx parameters.
529 while (cp->cp_cpcr & CPM_CR_FLG) /* wait if cp is busy */
531 cp->cp_cpcr = mk_cr_cmd(CPM_CR_CH_SCC, CPM_CR_INIT_TRX) | CPM_CR_FLG;
533 while (cp->cp_cpcr & CPM_CR_FLG) /* wait if cp is busy */
536 up->scc_genscc.scc_rfcr = SCC_EB | 0x05;
537 up->scc_genscc.scc_tfcr = SCC_EB | 0x05;
539 up->scc_genscc.scc_mrblr = 1; /* Single character receive */
540 up->scc_maxidl = 0; /* disable max idle */
541 up->scc_brkcr = 1; /* send one break character on stop TX */
549 up->scc_char1 = 0x8000;
550 up->scc_char2 = 0x8000;
551 up->scc_char3 = 0x8000;
552 up->scc_char4 = 0x8000;
553 up->scc_char5 = 0x8000;
554 up->scc_char6 = 0x8000;
555 up->scc_char7 = 0x8000;
556 up->scc_char8 = 0x8000;
557 up->scc_rccm = 0xc0ff;
559 /* Set low latency / small fifo.
561 sp->scc_gsmrh = SCC_GSMRH_RFW;
563 /* Set SCC(x) clock mode to 16x
564 * See 8xx_io/commproc.c for details.
569 /* Set UART mode, clock divider 16 on Tx and Rx
571 sp->scc_gsmrl &= ~0xF;
573 (SCC_GSMRL_MODE_UART | SCC_GSMRL_TDCR_16 | SCC_GSMRL_RDCR_16);
576 sp->scc_psmr |= SCU_PSMR_CL;
578 /* Mask all interrupts and remove anything pending.
581 sp->scc_scce = 0xffff;
582 sp->scc_dsr = 0x7e7e;
583 sp->scc_psmr = 0x3000;
585 /* Make the first buffer the only buffer.
587 tbdf->cbd_sc |= BD_SC_WRAP;
588 rbdf->cbd_sc |= BD_SC_EMPTY | BD_SC_WRAP;
590 /* Enable transmitter/receiver.
592 sp->scc_gsmrl |= (SCC_GSMRL_ENR | SCC_GSMRL_ENT);
598 scc_putc(const char c)
600 volatile cbd_t *tbdf;
602 volatile scc_uart_t *up;
603 volatile immap_t *im = (immap_t *)CONFIG_SYS_IMMR;
604 volatile cpm8xx_t *cpmp = &(im->im_cpm);
606 #ifdef CONFIG_MODEM_SUPPORT
614 up = (scc_uart_t *)&cpmp->cp_dparam[PROFF_SCC];
616 tbdf = (cbd_t *)&cpmp->cp_dpmem[up->scc_genscc.scc_tbase];
618 /* Wait for last character to go.
621 buf = (char *)tbdf->cbd_bufaddr;
624 tbdf->cbd_datlen = 1;
625 tbdf->cbd_sc |= BD_SC_READY;
628 while (tbdf->cbd_sc & BD_SC_READY) {
635 scc_puts (const char *s)
645 volatile cbd_t *rbdf;
646 volatile unsigned char *buf;
647 volatile scc_uart_t *up;
648 volatile immap_t *im = (immap_t *)CONFIG_SYS_IMMR;
649 volatile cpm8xx_t *cpmp = &(im->im_cpm);
652 up = (scc_uart_t *)&cpmp->cp_dparam[PROFF_SCC];
654 rbdf = (cbd_t *)&cpmp->cp_dpmem[up->scc_genscc.scc_rbase];
656 /* Wait for character to show up.
658 buf = (unsigned char *)rbdf->cbd_bufaddr;
660 while (rbdf->cbd_sc & BD_SC_EMPTY)
664 rbdf->cbd_sc |= BD_SC_EMPTY;
672 volatile cbd_t *rbdf;
673 volatile scc_uart_t *up;
674 volatile immap_t *im = (immap_t *)CONFIG_SYS_IMMR;
675 volatile cpm8xx_t *cpmp = &(im->im_cpm);
677 up = (scc_uart_t *)&cpmp->cp_dparam[PROFF_SCC];
679 rbdf = (cbd_t *)&cpmp->cp_dpmem[up->scc_genscc.scc_rbase];
681 return(!(rbdf->cbd_sc & BD_SC_EMPTY));
684 struct serial_device serial_scc_device =
696 #endif /* CONFIG_8xx_CONS_SCCx */
698 #ifdef CONFIG_MODEM_SUPPORT
699 void disable_putc(void)
704 void enable_putc(void)
710 #if defined(CONFIG_CMD_KGDB)
713 kgdb_serial_init(void)
717 if (strcmp(default_serial_console()->ctlr, "SMC") == 0)
719 #if defined(CONFIG_8xx_CONS_SMC1)
721 #elif defined(CONFIG_8xx_CONS_SMC2)
725 else if (strcmp(default_serial_console()->ctlr, "SMC") == 0)
727 #if defined(CONFIG_8xx_CONS_SCC1)
729 #elif defined(CONFIG_8xx_CONS_SCC2)
731 #elif defined(CONFIG_8xx_CONS_SCC3)
733 #elif defined(CONFIG_8xx_CONS_SCC4)
740 serial_printf("[on %s%d] ", default_serial_console()->ctlr, i);
751 putDebugStr (const char *str)
759 return serial_getc();
763 kgdb_interruptible (int yes)
769 #endif /* CONFIG_8xx_CONS_NONE */