blob: 932141144ce2e5a297b93a7d6edce43e1f9bb664 [file] [log] [blame]
wdenk4a9cbbe2002-08-27 09:48:53 +00001/*
2 * (C) Copyright 2000
3 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
4 *
Wolfgang Denk1a459662013-07-08 09:37:19 +02005 * SPDX-License-Identifier: GPL-2.0+
wdenk4a9cbbe2002-08-27 09:48:53 +00006 */
7
8#include <common.h>
9#include <commproc.h>
10#include <command.h>
wdenk281e00a2004-08-01 22:48:16 +000011#include <serial.h>
wdenkd0fb80c2003-01-11 09:48:40 +000012#include <watchdog.h>
Mike Frysinger6c768ca2011-04-29 18:03:29 +000013#include <linux/compiler.h>
wdenk4a9cbbe2002-08-27 09:48:53 +000014
Wolfgang Denkd87080b2006-03-31 18:32:53 +020015DECLARE_GLOBAL_DATA_PTR;
16
wdenk4a9cbbe2002-08-27 09:48:53 +000017#if !defined(CONFIG_8xx_CONS_NONE) /* No Console at all */
18
19#if defined(CONFIG_8xx_CONS_SMC1) /* Console on SMC1 */
20#define SMC_INDEX 0
wdenk4a9cbbe2002-08-27 09:48:53 +000021#define PROFF_SMC PROFF_SMC1
22#define CPM_CR_CH_SMC CPM_CR_CH_SMC1
23
24#elif defined(CONFIG_8xx_CONS_SMC2) /* Console on SMC2 */
25#define SMC_INDEX 1
wdenk4a9cbbe2002-08-27 09:48:53 +000026#define PROFF_SMC PROFF_SMC2
27#define CPM_CR_CH_SMC CPM_CR_CH_SMC2
28
wdenk281e00a2004-08-01 22:48:16 +000029#endif /* CONFIG_8xx_CONS_SMCx */
30
31#if defined(CONFIG_8xx_CONS_SCC1) /* Console on SCC1 */
wdenk4a9cbbe2002-08-27 09:48:53 +000032#define SCC_INDEX 0
33#define PROFF_SCC PROFF_SCC1
34#define CPM_CR_CH_SCC CPM_CR_CH_SCC1
35
36#elif defined(CONFIG_8xx_CONS_SCC2) /* Console on SCC2 */
wdenk4a9cbbe2002-08-27 09:48:53 +000037#define SCC_INDEX 1
38#define PROFF_SCC PROFF_SCC2
39#define CPM_CR_CH_SCC CPM_CR_CH_SCC2
40
41#elif defined(CONFIG_8xx_CONS_SCC3) /* Console on SCC3 */
wdenk4a9cbbe2002-08-27 09:48:53 +000042#define SCC_INDEX 2
43#define PROFF_SCC PROFF_SCC3
44#define CPM_CR_CH_SCC CPM_CR_CH_SCC3
45
46#elif defined(CONFIG_8xx_CONS_SCC4) /* Console on SCC4 */
wdenk4a9cbbe2002-08-27 09:48:53 +000047#define SCC_INDEX 3
48#define PROFF_SCC PROFF_SCC4
49#define CPM_CR_CH_SCC CPM_CR_CH_SCC4
50
wdenk281e00a2004-08-01 22:48:16 +000051#endif /* CONFIG_8xx_CONS_SCCx */
wdenk4a9cbbe2002-08-27 09:48:53 +000052
Heiko Schocher2b3f12c2009-02-10 09:31:47 +010053#if !defined(CONFIG_SYS_SMC_RXBUFLEN)
54#define CONFIG_SYS_SMC_RXBUFLEN 1
55#define CONFIG_SYS_MAXIDLE 0
56#else
57#if !defined(CONFIG_SYS_MAXIDLE)
58#error "you must define CONFIG_SYS_MAXIDLE"
59#endif
60#endif
61
62typedef volatile struct serialbuffer {
63 cbd_t rxbd; /* Rx BD */
64 cbd_t txbd; /* Tx BD */
65 uint rxindex; /* index for next character to read */
66 volatile uchar rxbuf[CONFIG_SYS_SMC_RXBUFLEN];/* rx buffers */
67 volatile uchar txbuf; /* tx buffers */
68} serialbuffer_t;
69
wdenk2535d602003-07-17 23:16:40 +000070static void serial_setdivisor(volatile cpm8xx_t *cp)
71{
wdenk75d1ea72004-01-31 20:06:54 +000072 int divisor=(gd->cpu_clk + 8*gd->baudrate)/16/gd->baudrate;
wdenk2535d602003-07-17 23:16:40 +000073
74 if(divisor/16>0x1000) {
Wolfgang Denk8ed44d92008-10-19 02:35:50 +020075 /* bad divisor, assume 50MHz clock and 9600 baud */
wdenk75d1ea72004-01-31 20:06:54 +000076 divisor=(50*1000*1000 + 8*9600)/16/9600;
wdenk2535d602003-07-17 23:16:40 +000077 }
78
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +020079#ifdef CONFIG_SYS_BRGCLK_PRESCALE
80 divisor /= CONFIG_SYS_BRGCLK_PRESCALE;
wdenk3bbc8992003-12-07 22:27:15 +000081#endif
82
wdenk2535d602003-07-17 23:16:40 +000083 if(divisor<=0x1000) {
84 cp->cp_brgc1=((divisor-1)<<1) | CPM_BRG_EN;
85 } else {
86 cp->cp_brgc1=((divisor/16-1)<<1) | CPM_BRG_EN | CPM_BRG_DIV16;
87 }
88}
89
wdenk4a9cbbe2002-08-27 09:48:53 +000090#if (defined (CONFIG_8xx_CONS_SMC1) || defined (CONFIG_8xx_CONS_SMC2))
91
92/*
93 * Minimal serial functions needed to use one of the SMC ports
94 * as serial console interface.
95 */
96
wdenk281e00a2004-08-01 22:48:16 +000097static void smc_setbrg (void)
98{
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +020099 volatile immap_t *im = (immap_t *)CONFIG_SYS_IMMR;
wdenk281e00a2004-08-01 22:48:16 +0000100 volatile cpm8xx_t *cp = &(im->im_cpm);
101
102 /* Set up the baud rate generator.
103 * See 8xx_io/commproc.c for details.
104 *
105 * Wire BRG1 to SMCx
106 */
107
108 cp->cp_simode = 0x00000000;
109
110 serial_setdivisor(cp);
111}
112
113static int smc_init (void)
wdenk4a9cbbe2002-08-27 09:48:53 +0000114{
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200115 volatile immap_t *im = (immap_t *)CONFIG_SYS_IMMR;
wdenk4a9cbbe2002-08-27 09:48:53 +0000116 volatile smc_t *sp;
117 volatile smc_uart_t *up;
wdenk4a9cbbe2002-08-27 09:48:53 +0000118 volatile cpm8xx_t *cp = &(im->im_cpm);
119#if (!defined(CONFIG_8xx_CONS_SMC1)) && (defined(CONFIG_MPC823) || defined(CONFIG_MPC850))
120 volatile iop8xx_t *ip = (iop8xx_t *)&(im->im_ioport);
121#endif
122 uint dpaddr;
Heiko Schocher2b3f12c2009-02-10 09:31:47 +0100123 volatile serialbuffer_t *rtx;
wdenk4a9cbbe2002-08-27 09:48:53 +0000124
125 /* initialize pointers to SMC */
126
127 sp = (smc_t *) &(cp->cp_smc[SMC_INDEX]);
128 up = (smc_uart_t *) &cp->cp_dparam[PROFF_SMC];
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200129#ifdef CONFIG_SYS_SMC_UCODE_PATCH
Heiko Schocherb423d052008-01-11 01:12:07 +0100130 up = (smc_uart_t *) &cp->cp_dpmem[up->smc_rpbase];
131#else
132 /* Disable relocation */
133 up->smc_rpbase = 0;
134#endif
wdenk4a9cbbe2002-08-27 09:48:53 +0000135
Heiko Schocher255d28e2009-02-10 09:32:38 +0100136 /* Disable transmitter/receiver. */
wdenk4a9cbbe2002-08-27 09:48:53 +0000137 sp->smc_smcmr &= ~(SMCMR_REN | SMCMR_TEN);
138
Heiko Schocher255d28e2009-02-10 09:32:38 +0100139 /* Enable SDMA. */
wdenk4a9cbbe2002-08-27 09:48:53 +0000140 im->im_siu_conf.sc_sdcr = 1;
141
142 /* clear error conditions */
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200143#ifdef CONFIG_SYS_SDSR
144 im->im_sdma.sdma_sdsr = CONFIG_SYS_SDSR;
wdenk4a9cbbe2002-08-27 09:48:53 +0000145#else
146 im->im_sdma.sdma_sdsr = 0x83;
147#endif
148
149 /* clear SDMA interrupt mask */
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200150#ifdef CONFIG_SYS_SDMR
151 im->im_sdma.sdma_sdmr = CONFIG_SYS_SDMR;
wdenk4a9cbbe2002-08-27 09:48:53 +0000152#else
153 im->im_sdma.sdma_sdmr = 0x00;
154#endif
155
156#if defined(CONFIG_8xx_CONS_SMC1)
Heiko Schocher255d28e2009-02-10 09:32:38 +0100157 /* Use Port B for SMC1 instead of other functions. */
wdenk4a9cbbe2002-08-27 09:48:53 +0000158 cp->cp_pbpar |= 0x000000c0;
159 cp->cp_pbdir &= ~0x000000c0;
160 cp->cp_pbodr &= ~0x000000c0;
161#else /* CONFIG_8xx_CONS_SMC2 */
162# if defined(CONFIG_MPC823) || defined(CONFIG_MPC850)
Heiko Schocher255d28e2009-02-10 09:32:38 +0100163 /* Use Port A for SMC2 instead of other functions. */
wdenk4a9cbbe2002-08-27 09:48:53 +0000164 ip->iop_papar |= 0x00c0;
165 ip->iop_padir &= ~0x00c0;
166 ip->iop_paodr &= ~0x00c0;
167# else /* must be a 860 then */
168 /* Use Port B for SMC2 instead of other functions.
Heiko Schocher255d28e2009-02-10 09:32:38 +0100169 */
wdenk4a9cbbe2002-08-27 09:48:53 +0000170 cp->cp_pbpar |= 0x00000c00;
171 cp->cp_pbdir &= ~0x00000c00;
172 cp->cp_pbodr &= ~0x00000c00;
173# endif
174#endif
175
Masahiro Yamadaaa6e1e42014-04-04 15:25:05 +0900176#if defined(CONFIG_FADS)
wdenk4a9cbbe2002-08-27 09:48:53 +0000177 /* Enable RS232 */
178#if defined(CONFIG_8xx_CONS_SMC1)
179 *((uint *) BCSR1) &= ~BCSR1_RS232EN_1;
180#else
181 *((uint *) BCSR1) &= ~BCSR1_RS232EN_2;
182#endif
183#endif /* CONFIG_FADS */
184
Masahiro Yamada4fb39252014-04-04 15:25:06 +0900185#if defined(CONFIG_RPXLITE)
wdenk4a9cbbe2002-08-27 09:48:53 +0000186 /* Enable Monitor Port Transceiver */
187 *((uchar *) BCSR0) |= BCSR0_ENMONXCVR ;
188#endif /* CONFIG_RPXLITE */
189
190 /* Set the physical address of the host memory buffers in
191 * the buffer descriptors.
192 */
193
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200194#ifdef CONFIG_SYS_ALLOC_DPRAM
Heiko Schocher2b3f12c2009-02-10 09:31:47 +0100195 /* allocate
196 * size of struct serialbuffer with bd rx/tx, buffer rx/tx and rx index
197 */
198 dpaddr = dpram_alloc_align((sizeof(serialbuffer_t)), 8);
wdenk4a9cbbe2002-08-27 09:48:53 +0000199#else
200 dpaddr = CPM_SERIAL_BASE ;
201#endif
202
Heiko Schocher2b3f12c2009-02-10 09:31:47 +0100203 rtx = (serialbuffer_t *)&cp->cp_dpmem[dpaddr];
wdenk4a9cbbe2002-08-27 09:48:53 +0000204 /* Allocate space for two buffer descriptors in the DP ram.
205 * For now, this address seems OK, but it may have to
206 * change with newer versions of the firmware.
207 * damm: allocating space after the two buffers for rx/tx data
208 */
209
Heiko Schocher2b3f12c2009-02-10 09:31:47 +0100210 rtx->rxbd.cbd_bufaddr = (uint) &rtx->rxbuf;
211 rtx->rxbd.cbd_sc = 0;
212
213 rtx->txbd.cbd_bufaddr = (uint) &rtx->txbuf;
214 rtx->txbd.cbd_sc = 0;
wdenk4a9cbbe2002-08-27 09:48:53 +0000215
Heiko Schocher255d28e2009-02-10 09:32:38 +0100216 /* Set up the uart parameters in the parameter ram. */
wdenk4a9cbbe2002-08-27 09:48:53 +0000217 up->smc_rbase = dpaddr;
218 up->smc_tbase = dpaddr+sizeof(cbd_t);
219 up->smc_rfcr = SMC_EB;
220 up->smc_tfcr = SMC_EB;
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200221#if defined (CONFIG_SYS_SMC_UCODE_PATCH)
Heiko Schocherb423d052008-01-11 01:12:07 +0100222 up->smc_rbptr = up->smc_rbase;
223 up->smc_tbptr = up->smc_tbase;
224 up->smc_rstate = 0;
225 up->smc_tstate = 0;
226#endif
wdenk4a9cbbe2002-08-27 09:48:53 +0000227
wdenk4a9cbbe2002-08-27 09:48:53 +0000228 /* Set UART mode, 8 bit, no parity, one stop.
229 * Enable receive and transmit.
230 */
231 sp->smc_smcmr = smcr_mk_clen(9) | SMCMR_SM_UART;
232
233 /* Mask all interrupts and remove anything pending.
234 */
235 sp->smc_smcm = 0;
236 sp->smc_smce = 0xff;
237
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200238#ifdef CONFIG_SYS_SPC1920_SMC1_CLK4
Markus Klotzbuecher81395672007-01-09 14:57:11 +0100239 /* clock source is PLD */
Wolfgang Denk2a8dfe02007-03-21 23:26:15 +0100240
Markus Klotzbuecher81395672007-01-09 14:57:11 +0100241 /* set freq to 19200 Baud */
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200242 *((volatile uchar *) CONFIG_SYS_SPC1920_PLD_BASE+6) = 0x3;
Markus Klotzbuecher81395672007-01-09 14:57:11 +0100243 /* configure clk4 as input */
244 im->im_ioport.iop_pdpar |= 0x800;
245 im->im_ioport.iop_pddir &= ~0x800;
Wolfgang Denkf11033e2007-01-15 13:41:04 +0100246
Wolfgang Denk2a8dfe02007-03-21 23:26:15 +0100247 cp->cp_simode = ((cp->cp_simode & ~0xf000) | 0x7000);
Markus Klotzbuecherb02d0172006-07-12 08:48:24 +0200248#else
249 /* Set up the baud rate generator */
wdenk281e00a2004-08-01 22:48:16 +0000250 smc_setbrg ();
Markus Klotzbuecherb02d0172006-07-12 08:48:24 +0200251#endif
wdenk4a9cbbe2002-08-27 09:48:53 +0000252
Heiko Schocher255d28e2009-02-10 09:32:38 +0100253 /* Make the first buffer the only buffer. */
Heiko Schocher2b3f12c2009-02-10 09:31:47 +0100254 rtx->txbd.cbd_sc |= BD_SC_WRAP;
255 rtx->rxbd.cbd_sc |= BD_SC_EMPTY | BD_SC_WRAP;
wdenk4a9cbbe2002-08-27 09:48:53 +0000256
Heiko Schocher2b3f12c2009-02-10 09:31:47 +0100257 /* single/multi character receive. */
258 up->smc_mrblr = CONFIG_SYS_SMC_RXBUFLEN;
259 up->smc_maxidl = CONFIG_SYS_MAXIDLE;
260 rtx->rxindex = 0;
wdenk4a9cbbe2002-08-27 09:48:53 +0000261
Heiko Schocher255d28e2009-02-10 09:32:38 +0100262 /* Initialize Tx/Rx parameters. */
wdenk4a9cbbe2002-08-27 09:48:53 +0000263 while (cp->cp_cpcr & CPM_CR_FLG) /* wait if cp is busy */
264 ;
265
266 cp->cp_cpcr = mk_cr_cmd(CPM_CR_CH_SMC, CPM_CR_INIT_TRX) | CPM_CR_FLG;
267
268 while (cp->cp_cpcr & CPM_CR_FLG) /* wait if cp is busy */
269 ;
270
Heiko Schocher255d28e2009-02-10 09:32:38 +0100271 /* Enable transmitter/receiver. */
wdenk4a9cbbe2002-08-27 09:48:53 +0000272 sp->smc_smcmr |= SMCMR_REN | SMCMR_TEN;
273
274 return (0);
275}
276
wdenk281e00a2004-08-01 22:48:16 +0000277static void
278smc_putc(const char c)
wdenk4a9cbbe2002-08-27 09:48:53 +0000279{
wdenk4a9cbbe2002-08-27 09:48:53 +0000280 volatile smc_uart_t *up;
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200281 volatile immap_t *im = (immap_t *)CONFIG_SYS_IMMR;
wdenk4a9cbbe2002-08-27 09:48:53 +0000282 volatile cpm8xx_t *cpmp = &(im->im_cpm);
Heiko Schocher2b3f12c2009-02-10 09:31:47 +0100283 volatile serialbuffer_t *rtx;
wdenk4a9cbbe2002-08-27 09:48:53 +0000284
wdenk4532cb62003-04-27 22:52:51 +0000285#ifdef CONFIG_MODEM_SUPPORT
wdenk4532cb62003-04-27 22:52:51 +0000286 if (gd->be_quiet)
287 return;
288#endif
289
wdenk4a9cbbe2002-08-27 09:48:53 +0000290 if (c == '\n')
wdenk281e00a2004-08-01 22:48:16 +0000291 smc_putc ('\r');
wdenk4a9cbbe2002-08-27 09:48:53 +0000292
293 up = (smc_uart_t *)&cpmp->cp_dparam[PROFF_SMC];
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200294#ifdef CONFIG_SYS_SMC_UCODE_PATCH
Heiko Schocherb423d052008-01-11 01:12:07 +0100295 up = (smc_uart_t *) &cpmp->cp_dpmem[up->smc_rpbase];
296#endif
wdenk4a9cbbe2002-08-27 09:48:53 +0000297
Heiko Schocher2b3f12c2009-02-10 09:31:47 +0100298 rtx = (serialbuffer_t *)&cpmp->cp_dpmem[up->smc_rbase];
wdenk4a9cbbe2002-08-27 09:48:53 +0000299
Heiko Schocher255d28e2009-02-10 09:32:38 +0100300 /* Wait for last character to go. */
Heiko Schocher2b3f12c2009-02-10 09:31:47 +0100301 rtx->txbuf = c;
302 rtx->txbd.cbd_datlen = 1;
303 rtx->txbd.cbd_sc |= BD_SC_READY;
wdenk4a9cbbe2002-08-27 09:48:53 +0000304 __asm__("eieio");
wdenkd0fb80c2003-01-11 09:48:40 +0000305
Heiko Schocher2b3f12c2009-02-10 09:31:47 +0100306 while (rtx->txbd.cbd_sc & BD_SC_READY) {
wdenkd0fb80c2003-01-11 09:48:40 +0000307 WATCHDOG_RESET ();
wdenk4a9cbbe2002-08-27 09:48:53 +0000308 __asm__("eieio");
wdenkd0fb80c2003-01-11 09:48:40 +0000309 }
wdenk4a9cbbe2002-08-27 09:48:53 +0000310}
311
wdenk281e00a2004-08-01 22:48:16 +0000312static void
313smc_puts (const char *s)
314{
315 while (*s) {
316 smc_putc (*s++);
317 }
318}
319
320static int
321smc_getc(void)
wdenk4a9cbbe2002-08-27 09:48:53 +0000322{
wdenk4a9cbbe2002-08-27 09:48:53 +0000323 volatile smc_uart_t *up;
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200324 volatile immap_t *im = (immap_t *)CONFIG_SYS_IMMR;
wdenk4a9cbbe2002-08-27 09:48:53 +0000325 volatile cpm8xx_t *cpmp = &(im->im_cpm);
Heiko Schocher2b3f12c2009-02-10 09:31:47 +0100326 volatile serialbuffer_t *rtx;
327 unsigned char c;
wdenk4a9cbbe2002-08-27 09:48:53 +0000328
329 up = (smc_uart_t *)&cpmp->cp_dparam[PROFF_SMC];
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200330#ifdef CONFIG_SYS_SMC_UCODE_PATCH
Heiko Schocherb423d052008-01-11 01:12:07 +0100331 up = (smc_uart_t *) &cpmp->cp_dpmem[up->smc_rpbase];
332#endif
Heiko Schocher2b3f12c2009-02-10 09:31:47 +0100333 rtx = (serialbuffer_t *)&cpmp->cp_dpmem[up->smc_rbase];
wdenk4a9cbbe2002-08-27 09:48:53 +0000334
Heiko Schocher255d28e2009-02-10 09:32:38 +0100335 /* Wait for character to show up. */
Heiko Schocher2b3f12c2009-02-10 09:31:47 +0100336 while (rtx->rxbd.cbd_sc & BD_SC_EMPTY)
wdenkd0fb80c2003-01-11 09:48:40 +0000337 WATCHDOG_RESET ();
338
Heiko Schocher2b3f12c2009-02-10 09:31:47 +0100339 /* the characters are read one by one,
340 * use the rxindex to know the next char to deliver
341 */
342 c = *(unsigned char *) (rtx->rxbd.cbd_bufaddr+rtx->rxindex);
343 rtx->rxindex++;
wdenk4a9cbbe2002-08-27 09:48:53 +0000344
Heiko Schocher2b3f12c2009-02-10 09:31:47 +0100345 /* check if all char are readout, then make prepare for next receive */
346 if (rtx->rxindex >= rtx->rxbd.cbd_datlen) {
347 rtx->rxindex = 0;
348 rtx->rxbd.cbd_sc |= BD_SC_EMPTY;
349 }
wdenk4a9cbbe2002-08-27 09:48:53 +0000350 return(c);
351}
352
wdenk281e00a2004-08-01 22:48:16 +0000353static int
354smc_tstc(void)
wdenk4a9cbbe2002-08-27 09:48:53 +0000355{
wdenk4a9cbbe2002-08-27 09:48:53 +0000356 volatile smc_uart_t *up;
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200357 volatile immap_t *im = (immap_t *)CONFIG_SYS_IMMR;
wdenk4a9cbbe2002-08-27 09:48:53 +0000358 volatile cpm8xx_t *cpmp = &(im->im_cpm);
Heiko Schocher2b3f12c2009-02-10 09:31:47 +0100359 volatile serialbuffer_t *rtx;
wdenk4a9cbbe2002-08-27 09:48:53 +0000360
361 up = (smc_uart_t *)&cpmp->cp_dparam[PROFF_SMC];
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200362#ifdef CONFIG_SYS_SMC_UCODE_PATCH
Heiko Schocherb423d052008-01-11 01:12:07 +0100363 up = (smc_uart_t *) &cpmp->cp_dpmem[up->smc_rpbase];
364#endif
wdenk4a9cbbe2002-08-27 09:48:53 +0000365
Heiko Schocher2b3f12c2009-02-10 09:31:47 +0100366 rtx = (serialbuffer_t *)&cpmp->cp_dpmem[up->smc_rbase];
wdenk4a9cbbe2002-08-27 09:48:53 +0000367
Heiko Schocher2b3f12c2009-02-10 09:31:47 +0100368 return !(rtx->rxbd.cbd_sc & BD_SC_EMPTY);
wdenk4a9cbbe2002-08-27 09:48:53 +0000369}
370
wdenk281e00a2004-08-01 22:48:16 +0000371struct serial_device serial_smc_device =
372{
Marek Vasut90bad892012-09-09 18:48:28 +0200373 .name = "serial_smc",
374 .start = smc_init,
375 .stop = NULL,
376 .setbrg = smc_setbrg,
377 .getc = smc_getc,
378 .tstc = smc_tstc,
379 .putc = smc_putc,
380 .puts = smc_puts,
wdenk281e00a2004-08-01 22:48:16 +0000381};
wdenk4a9cbbe2002-08-27 09:48:53 +0000382
wdenk281e00a2004-08-01 22:48:16 +0000383#endif /* CONFIG_8xx_CONS_SMC1 || CONFIG_8xx_CONS_SMC2 */
384
385#if defined(CONFIG_8xx_CONS_SCC1) || defined(CONFIG_8xx_CONS_SCC2) || \
386 defined(CONFIG_8xx_CONS_SCC3) || defined(CONFIG_8xx_CONS_SCC4)
387
388static void
389scc_setbrg (void)
390{
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200391 volatile immap_t *im = (immap_t *)CONFIG_SYS_IMMR;
wdenk281e00a2004-08-01 22:48:16 +0000392 volatile cpm8xx_t *cp = &(im->im_cpm);
393
394 /* Set up the baud rate generator.
395 * See 8xx_io/commproc.c for details.
396 *
397 * Wire BRG1 to SCCx
398 */
399
400 cp->cp_sicr &= ~(0x000000FF << (8 * SCC_INDEX));
401
402 serial_setdivisor(cp);
403}
404
405static int scc_init (void)
wdenk4a9cbbe2002-08-27 09:48:53 +0000406{
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200407 volatile immap_t *im = (immap_t *)CONFIG_SYS_IMMR;
wdenk4a9cbbe2002-08-27 09:48:53 +0000408 volatile scc_t *sp;
409 volatile scc_uart_t *up;
410 volatile cbd_t *tbdf, *rbdf;
411 volatile cpm8xx_t *cp = &(im->im_cpm);
412 uint dpaddr;
413#if (SCC_INDEX != 2) || !defined(CONFIG_MPC850)
414 volatile iop8xx_t *ip = (iop8xx_t *)&(im->im_ioport);
415#endif
416
417 /* initialize pointers to SCC */
418
419 sp = (scc_t *) &(cp->cp_scc[SCC_INDEX]);
420 up = (scc_uart_t *) &cp->cp_dparam[PROFF_SCC];
421
422#if defined(CONFIG_LWMON) && defined(CONFIG_8xx_CONS_SCC2)
423 { /* Disable Ethernet, enable Serial */
424 uchar c;
425
426 c = pic_read (0x61);
427 c &= ~0x40; /* enable COM3 */
428 c |= 0x80; /* disable Ethernet */
429 pic_write (0x61, c);
430
431 /* enable RTS2 */
432 cp->cp_pbpar |= 0x2000;
433 cp->cp_pbdat |= 0x2000;
434 cp->cp_pbdir |= 0x2000;
435 }
436#endif /* CONFIG_LWMON */
437
Heiko Schocher255d28e2009-02-10 09:32:38 +0100438 /* Disable transmitter/receiver. */
wdenk4a9cbbe2002-08-27 09:48:53 +0000439 sp->scc_gsmrl &= ~(SCC_GSMRL_ENR | SCC_GSMRL_ENT);
440
441#if (SCC_INDEX == 2) && defined(CONFIG_MPC850)
442 /*
443 * The MPC850 has SCC3 on Port B
444 */
445 cp->cp_pbpar |= 0x06;
446 cp->cp_pbdir &= ~0x06;
447 cp->cp_pbodr &= ~0x06;
448
449#elif (SCC_INDEX < 2) || !defined(CONFIG_IP860)
450 /*
451 * Standard configuration for SCC's is on Part A
452 */
453 ip->iop_papar |= ((3 << (2 * SCC_INDEX)));
454 ip->iop_padir &= ~((3 << (2 * SCC_INDEX)));
455 ip->iop_paodr &= ~((3 << (2 * SCC_INDEX)));
456#else
457 /*
458 * The IP860 has SCC3 and SCC4 on Port D
459 */
460 ip->iop_pdpar |= ((3 << (2 * SCC_INDEX)));
461#endif
462
Heiko Schocher255d28e2009-02-10 09:32:38 +0100463 /* Allocate space for two buffer descriptors in the DP ram. */
wdenk4a9cbbe2002-08-27 09:48:53 +0000464
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200465#ifdef CONFIG_SYS_ALLOC_DPRAM
wdenk4a9cbbe2002-08-27 09:48:53 +0000466 dpaddr = dpram_alloc_align (sizeof(cbd_t)*2 + 2, 8) ;
467#else
wdenk281e00a2004-08-01 22:48:16 +0000468 dpaddr = CPM_SERIAL2_BASE ;
wdenk4a9cbbe2002-08-27 09:48:53 +0000469#endif
470
Heiko Schocher255d28e2009-02-10 09:32:38 +0100471 /* Enable SDMA. */
wdenk4a9cbbe2002-08-27 09:48:53 +0000472 im->im_siu_conf.sc_sdcr = 0x0001;
473
474 /* Set the physical address of the host memory buffers in
475 * the buffer descriptors.
476 */
477
478 rbdf = (cbd_t *)&cp->cp_dpmem[dpaddr];
479 rbdf->cbd_bufaddr = (uint) (rbdf+2);
480 rbdf->cbd_sc = 0;
481 tbdf = rbdf + 1;
482 tbdf->cbd_bufaddr = ((uint) (rbdf+2)) + 1;
483 tbdf->cbd_sc = 0;
484
Heiko Schocher255d28e2009-02-10 09:32:38 +0100485 /* Set up the baud rate generator. */
wdenk281e00a2004-08-01 22:48:16 +0000486 scc_setbrg ();
wdenk4a9cbbe2002-08-27 09:48:53 +0000487
Heiko Schocher255d28e2009-02-10 09:32:38 +0100488 /* Set up the uart parameters in the parameter ram. */
wdenk4a9cbbe2002-08-27 09:48:53 +0000489 up->scc_genscc.scc_rbase = dpaddr;
490 up->scc_genscc.scc_tbase = dpaddr+sizeof(cbd_t);
491
Heiko Schocher255d28e2009-02-10 09:32:38 +0100492 /* Initialize Tx/Rx parameters. */
wdenk4a9cbbe2002-08-27 09:48:53 +0000493 while (cp->cp_cpcr & CPM_CR_FLG) /* wait if cp is busy */
494 ;
495 cp->cp_cpcr = mk_cr_cmd(CPM_CR_CH_SCC, CPM_CR_INIT_TRX) | CPM_CR_FLG;
496
497 while (cp->cp_cpcr & CPM_CR_FLG) /* wait if cp is busy */
498 ;
499
500 up->scc_genscc.scc_rfcr = SCC_EB | 0x05;
501 up->scc_genscc.scc_tfcr = SCC_EB | 0x05;
502
503 up->scc_genscc.scc_mrblr = 1; /* Single character receive */
504 up->scc_maxidl = 0; /* disable max idle */
505 up->scc_brkcr = 1; /* send one break character on stop TX */
506 up->scc_parec = 0;
507 up->scc_frmec = 0;
508 up->scc_nosec = 0;
509 up->scc_brkec = 0;
510 up->scc_uaddr1 = 0;
511 up->scc_uaddr2 = 0;
512 up->scc_toseq = 0;
513 up->scc_char1 = 0x8000;
514 up->scc_char2 = 0x8000;
515 up->scc_char3 = 0x8000;
516 up->scc_char4 = 0x8000;
517 up->scc_char5 = 0x8000;
518 up->scc_char6 = 0x8000;
519 up->scc_char7 = 0x8000;
520 up->scc_char8 = 0x8000;
521 up->scc_rccm = 0xc0ff;
522
Heiko Schocher255d28e2009-02-10 09:32:38 +0100523 /* Set low latency / small fifo. */
wdenk4a9cbbe2002-08-27 09:48:53 +0000524 sp->scc_gsmrh = SCC_GSMRH_RFW;
525
526 /* Set SCC(x) clock mode to 16x
527 * See 8xx_io/commproc.c for details.
528 *
529 * Wire BRG1 to SCCn
530 */
531
Heiko Schocher255d28e2009-02-10 09:32:38 +0100532 /* Set UART mode, clock divider 16 on Tx and Rx */
wdenk281e00a2004-08-01 22:48:16 +0000533 sp->scc_gsmrl &= ~0xF;
wdenk4a9cbbe2002-08-27 09:48:53 +0000534 sp->scc_gsmrl |=
535 (SCC_GSMRL_MODE_UART | SCC_GSMRL_TDCR_16 | SCC_GSMRL_RDCR_16);
536
wdenk281e00a2004-08-01 22:48:16 +0000537 sp->scc_psmr = 0;
wdenk4a9cbbe2002-08-27 09:48:53 +0000538 sp->scc_psmr |= SCU_PSMR_CL;
539
Heiko Schocher255d28e2009-02-10 09:32:38 +0100540 /* Mask all interrupts and remove anything pending. */
wdenk4a9cbbe2002-08-27 09:48:53 +0000541 sp->scc_sccm = 0;
542 sp->scc_scce = 0xffff;
543 sp->scc_dsr = 0x7e7e;
544 sp->scc_psmr = 0x3000;
545
Heiko Schocher255d28e2009-02-10 09:32:38 +0100546 /* Make the first buffer the only buffer. */
wdenk4a9cbbe2002-08-27 09:48:53 +0000547 tbdf->cbd_sc |= BD_SC_WRAP;
548 rbdf->cbd_sc |= BD_SC_EMPTY | BD_SC_WRAP;
549
Heiko Schocher255d28e2009-02-10 09:32:38 +0100550 /* Enable transmitter/receiver. */
wdenk4a9cbbe2002-08-27 09:48:53 +0000551 sp->scc_gsmrl |= (SCC_GSMRL_ENR | SCC_GSMRL_ENT);
552
553 return (0);
554}
555
wdenk281e00a2004-08-01 22:48:16 +0000556static void
557scc_putc(const char c)
wdenk4a9cbbe2002-08-27 09:48:53 +0000558{
559 volatile cbd_t *tbdf;
560 volatile char *buf;
561 volatile scc_uart_t *up;
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200562 volatile immap_t *im = (immap_t *)CONFIG_SYS_IMMR;
wdenk4a9cbbe2002-08-27 09:48:53 +0000563 volatile cpm8xx_t *cpmp = &(im->im_cpm);
564
wdenk281e00a2004-08-01 22:48:16 +0000565#ifdef CONFIG_MODEM_SUPPORT
wdenk281e00a2004-08-01 22:48:16 +0000566 if (gd->be_quiet)
567 return;
568#endif
569
wdenk4a9cbbe2002-08-27 09:48:53 +0000570 if (c == '\n')
wdenk281e00a2004-08-01 22:48:16 +0000571 scc_putc ('\r');
wdenk4a9cbbe2002-08-27 09:48:53 +0000572
573 up = (scc_uart_t *)&cpmp->cp_dparam[PROFF_SCC];
574
575 tbdf = (cbd_t *)&cpmp->cp_dpmem[up->scc_genscc.scc_tbase];
576
Heiko Schocher255d28e2009-02-10 09:32:38 +0100577 /* Wait for last character to go. */
wdenk4a9cbbe2002-08-27 09:48:53 +0000578
579 buf = (char *)tbdf->cbd_bufaddr;
wdenk4a9cbbe2002-08-27 09:48:53 +0000580
581 *buf = c;
582 tbdf->cbd_datlen = 1;
583 tbdf->cbd_sc |= BD_SC_READY;
584 __asm__("eieio");
wdenkd0fb80c2003-01-11 09:48:40 +0000585
586 while (tbdf->cbd_sc & BD_SC_READY) {
wdenk4a9cbbe2002-08-27 09:48:53 +0000587 __asm__("eieio");
wdenkd0fb80c2003-01-11 09:48:40 +0000588 WATCHDOG_RESET ();
589 }
wdenk4a9cbbe2002-08-27 09:48:53 +0000590}
591
wdenk281e00a2004-08-01 22:48:16 +0000592static void
593scc_puts (const char *s)
594{
595 while (*s) {
596 scc_putc (*s++);
597 }
598}
599
600static int
601scc_getc(void)
wdenk4a9cbbe2002-08-27 09:48:53 +0000602{
603 volatile cbd_t *rbdf;
604 volatile unsigned char *buf;
605 volatile scc_uart_t *up;
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200606 volatile immap_t *im = (immap_t *)CONFIG_SYS_IMMR;
wdenk4a9cbbe2002-08-27 09:48:53 +0000607 volatile cpm8xx_t *cpmp = &(im->im_cpm);
608 unsigned char c;
609
610 up = (scc_uart_t *)&cpmp->cp_dparam[PROFF_SCC];
611
612 rbdf = (cbd_t *)&cpmp->cp_dpmem[up->scc_genscc.scc_rbase];
613
Heiko Schocher255d28e2009-02-10 09:32:38 +0100614 /* Wait for character to show up. */
wdenk4a9cbbe2002-08-27 09:48:53 +0000615 buf = (unsigned char *)rbdf->cbd_bufaddr;
wdenkd0fb80c2003-01-11 09:48:40 +0000616
wdenk4a9cbbe2002-08-27 09:48:53 +0000617 while (rbdf->cbd_sc & BD_SC_EMPTY)
wdenkd0fb80c2003-01-11 09:48:40 +0000618 WATCHDOG_RESET ();
619
wdenk4a9cbbe2002-08-27 09:48:53 +0000620 c = *buf;
621 rbdf->cbd_sc |= BD_SC_EMPTY;
622
623 return(c);
624}
625
wdenk281e00a2004-08-01 22:48:16 +0000626static int
627scc_tstc(void)
wdenk4a9cbbe2002-08-27 09:48:53 +0000628{
629 volatile cbd_t *rbdf;
630 volatile scc_uart_t *up;
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200631 volatile immap_t *im = (immap_t *)CONFIG_SYS_IMMR;
wdenk4a9cbbe2002-08-27 09:48:53 +0000632 volatile cpm8xx_t *cpmp = &(im->im_cpm);
633
634 up = (scc_uart_t *)&cpmp->cp_dparam[PROFF_SCC];
635
636 rbdf = (cbd_t *)&cpmp->cp_dpmem[up->scc_genscc.scc_rbase];
637
638 return(!(rbdf->cbd_sc & BD_SC_EMPTY));
639}
640
wdenk281e00a2004-08-01 22:48:16 +0000641struct serial_device serial_scc_device =
wdenk4a9cbbe2002-08-27 09:48:53 +0000642{
Marek Vasut90bad892012-09-09 18:48:28 +0200643 .name = "serial_scc",
644 .start = scc_init,
645 .stop = NULL,
646 .setbrg = scc_setbrg,
647 .getc = scc_getc,
648 .tstc = scc_tstc,
649 .putc = scc_putc,
650 .puts = scc_puts,
wdenk281e00a2004-08-01 22:48:16 +0000651};
652
653#endif /* CONFIG_8xx_CONS_SCCx */
654
Mike Frysinger6c768ca2011-04-29 18:03:29 +0000655__weak struct serial_device *default_serial_console(void)
656{
657#if defined(CONFIG_8xx_CONS_SMC1) || defined(CONFIG_8xx_CONS_SMC2)
658 return &serial_smc_device;
659#else
660 return &serial_scc_device;
661#endif
662}
663
Marek Vasutf0eb1f62012-09-12 13:50:56 +0200664void mpc8xx_serial_initialize(void)
665{
666#if defined(CONFIG_8xx_CONS_SMC1) || defined(CONFIG_8xx_CONS_SMC2)
667 serial_register(&serial_smc_device);
668#endif
669#if defined(CONFIG_8xx_CONS_SCC1) || defined(CONFIG_8xx_CONS_SCC2) || \
670 defined(CONFIG_8xx_CONS_SCC3) || defined(CONFIG_8xx_CONS_SCC4)
671 serial_register(&serial_scc_device);
672#endif
673}
674
wdenk281e00a2004-08-01 22:48:16 +0000675#ifdef CONFIG_MODEM_SUPPORT
676void disable_putc(void)
677{
wdenk281e00a2004-08-01 22:48:16 +0000678 gd->be_quiet = 1;
wdenk4a9cbbe2002-08-27 09:48:53 +0000679}
680
wdenk281e00a2004-08-01 22:48:16 +0000681void enable_putc(void)
682{
wdenk281e00a2004-08-01 22:48:16 +0000683 gd->be_quiet = 0;
684}
685#endif
wdenk4a9cbbe2002-08-27 09:48:53 +0000686
Jon Loeliger44312832007-07-09 19:06:00 -0500687#if defined(CONFIG_CMD_KGDB)
wdenk4a9cbbe2002-08-27 09:48:53 +0000688
689void
690kgdb_serial_init(void)
691{
wdenk281e00a2004-08-01 22:48:16 +0000692 int i = -1;
693
Mike Frysinger1c9a5602011-04-29 18:03:31 +0000694 if (strcmp(default_serial_console()->name, "serial_smc") == 0)
wdenk281e00a2004-08-01 22:48:16 +0000695 {
wdenk4a9cbbe2002-08-27 09:48:53 +0000696#if defined(CONFIG_8xx_CONS_SMC1)
wdenk281e00a2004-08-01 22:48:16 +0000697 i = 1;
wdenk4a9cbbe2002-08-27 09:48:53 +0000698#elif defined(CONFIG_8xx_CONS_SMC2)
wdenk281e00a2004-08-01 22:48:16 +0000699 i = 2;
wdenk4a9cbbe2002-08-27 09:48:53 +0000700#endif
wdenk281e00a2004-08-01 22:48:16 +0000701 }
Mike Frysinger1c9a5602011-04-29 18:03:31 +0000702 else if (strcmp(default_serial_console()->name, "serial_scc") == 0)
wdenk281e00a2004-08-01 22:48:16 +0000703 {
704#if defined(CONFIG_8xx_CONS_SCC1)
705 i = 1;
706#elif defined(CONFIG_8xx_CONS_SCC2)
707 i = 2;
708#elif defined(CONFIG_8xx_CONS_SCC3)
709 i = 3;
710#elif defined(CONFIG_8xx_CONS_SCC4)
711 i = 4;
712#endif
713 }
714
715 if (i >= 0)
716 {
Mike Frysinger1c9a5602011-04-29 18:03:31 +0000717 serial_printf("[on %s%d] ", default_serial_console()->name, i);
wdenk281e00a2004-08-01 22:48:16 +0000718 }
wdenk4a9cbbe2002-08-27 09:48:53 +0000719}
720
721void
722putDebugChar (int c)
723{
724 serial_putc (c);
725}
726
727void
728putDebugStr (const char *str)
729{
730 serial_puts (str);
731}
732
733int
734getDebugChar (void)
735{
736 return serial_getc();
737}
738
739void
740kgdb_interruptible (int yes)
741{
742 return;
743}
Jon Loeliger068b60a2007-07-10 10:27:39 -0500744#endif
wdenk4a9cbbe2002-08-27 09:48:53 +0000745
746#endif /* CONFIG_8xx_CONS_NONE */