blob: bb16b17937cf3e9503c5517d88097ff98fe21146 [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
wdenk4a9cbbe2002-08-27 09:48:53 +0000176 /* Set the physical address of the host memory buffers in
177 * the buffer descriptors.
178 */
179
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200180#ifdef CONFIG_SYS_ALLOC_DPRAM
Heiko Schocher2b3f12c2009-02-10 09:31:47 +0100181 /* allocate
182 * size of struct serialbuffer with bd rx/tx, buffer rx/tx and rx index
183 */
184 dpaddr = dpram_alloc_align((sizeof(serialbuffer_t)), 8);
wdenk4a9cbbe2002-08-27 09:48:53 +0000185#else
186 dpaddr = CPM_SERIAL_BASE ;
187#endif
188
Heiko Schocher2b3f12c2009-02-10 09:31:47 +0100189 rtx = (serialbuffer_t *)&cp->cp_dpmem[dpaddr];
wdenk4a9cbbe2002-08-27 09:48:53 +0000190 /* Allocate space for two buffer descriptors in the DP ram.
191 * For now, this address seems OK, but it may have to
192 * change with newer versions of the firmware.
193 * damm: allocating space after the two buffers for rx/tx data
194 */
195
Heiko Schocher2b3f12c2009-02-10 09:31:47 +0100196 rtx->rxbd.cbd_bufaddr = (uint) &rtx->rxbuf;
197 rtx->rxbd.cbd_sc = 0;
198
199 rtx->txbd.cbd_bufaddr = (uint) &rtx->txbuf;
200 rtx->txbd.cbd_sc = 0;
wdenk4a9cbbe2002-08-27 09:48:53 +0000201
Heiko Schocher255d28e2009-02-10 09:32:38 +0100202 /* Set up the uart parameters in the parameter ram. */
wdenk4a9cbbe2002-08-27 09:48:53 +0000203 up->smc_rbase = dpaddr;
204 up->smc_tbase = dpaddr+sizeof(cbd_t);
205 up->smc_rfcr = SMC_EB;
206 up->smc_tfcr = SMC_EB;
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200207#if defined (CONFIG_SYS_SMC_UCODE_PATCH)
Heiko Schocherb423d052008-01-11 01:12:07 +0100208 up->smc_rbptr = up->smc_rbase;
209 up->smc_tbptr = up->smc_tbase;
210 up->smc_rstate = 0;
211 up->smc_tstate = 0;
212#endif
wdenk4a9cbbe2002-08-27 09:48:53 +0000213
wdenk4a9cbbe2002-08-27 09:48:53 +0000214 /* Set UART mode, 8 bit, no parity, one stop.
215 * Enable receive and transmit.
216 */
217 sp->smc_smcmr = smcr_mk_clen(9) | SMCMR_SM_UART;
218
219 /* Mask all interrupts and remove anything pending.
220 */
221 sp->smc_smcm = 0;
222 sp->smc_smce = 0xff;
223
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200224#ifdef CONFIG_SYS_SPC1920_SMC1_CLK4
Markus Klotzbuecher81395672007-01-09 14:57:11 +0100225 /* clock source is PLD */
Wolfgang Denk2a8dfe02007-03-21 23:26:15 +0100226
Markus Klotzbuecher81395672007-01-09 14:57:11 +0100227 /* set freq to 19200 Baud */
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200228 *((volatile uchar *) CONFIG_SYS_SPC1920_PLD_BASE+6) = 0x3;
Markus Klotzbuecher81395672007-01-09 14:57:11 +0100229 /* configure clk4 as input */
230 im->im_ioport.iop_pdpar |= 0x800;
231 im->im_ioport.iop_pddir &= ~0x800;
Wolfgang Denkf11033e2007-01-15 13:41:04 +0100232
Wolfgang Denk2a8dfe02007-03-21 23:26:15 +0100233 cp->cp_simode = ((cp->cp_simode & ~0xf000) | 0x7000);
Markus Klotzbuecherb02d0172006-07-12 08:48:24 +0200234#else
235 /* Set up the baud rate generator */
wdenk281e00a2004-08-01 22:48:16 +0000236 smc_setbrg ();
Markus Klotzbuecherb02d0172006-07-12 08:48:24 +0200237#endif
wdenk4a9cbbe2002-08-27 09:48:53 +0000238
Heiko Schocher255d28e2009-02-10 09:32:38 +0100239 /* Make the first buffer the only buffer. */
Heiko Schocher2b3f12c2009-02-10 09:31:47 +0100240 rtx->txbd.cbd_sc |= BD_SC_WRAP;
241 rtx->rxbd.cbd_sc |= BD_SC_EMPTY | BD_SC_WRAP;
wdenk4a9cbbe2002-08-27 09:48:53 +0000242
Heiko Schocher2b3f12c2009-02-10 09:31:47 +0100243 /* single/multi character receive. */
244 up->smc_mrblr = CONFIG_SYS_SMC_RXBUFLEN;
245 up->smc_maxidl = CONFIG_SYS_MAXIDLE;
246 rtx->rxindex = 0;
wdenk4a9cbbe2002-08-27 09:48:53 +0000247
Heiko Schocher255d28e2009-02-10 09:32:38 +0100248 /* Initialize Tx/Rx parameters. */
wdenk4a9cbbe2002-08-27 09:48:53 +0000249 while (cp->cp_cpcr & CPM_CR_FLG) /* wait if cp is busy */
250 ;
251
252 cp->cp_cpcr = mk_cr_cmd(CPM_CR_CH_SMC, CPM_CR_INIT_TRX) | CPM_CR_FLG;
253
254 while (cp->cp_cpcr & CPM_CR_FLG) /* wait if cp is busy */
255 ;
256
Heiko Schocher255d28e2009-02-10 09:32:38 +0100257 /* Enable transmitter/receiver. */
wdenk4a9cbbe2002-08-27 09:48:53 +0000258 sp->smc_smcmr |= SMCMR_REN | SMCMR_TEN;
259
260 return (0);
261}
262
wdenk281e00a2004-08-01 22:48:16 +0000263static void
264smc_putc(const char c)
wdenk4a9cbbe2002-08-27 09:48:53 +0000265{
wdenk4a9cbbe2002-08-27 09:48:53 +0000266 volatile smc_uart_t *up;
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200267 volatile immap_t *im = (immap_t *)CONFIG_SYS_IMMR;
wdenk4a9cbbe2002-08-27 09:48:53 +0000268 volatile cpm8xx_t *cpmp = &(im->im_cpm);
Heiko Schocher2b3f12c2009-02-10 09:31:47 +0100269 volatile serialbuffer_t *rtx;
wdenk4a9cbbe2002-08-27 09:48:53 +0000270
wdenk4532cb62003-04-27 22:52:51 +0000271#ifdef CONFIG_MODEM_SUPPORT
wdenk4532cb62003-04-27 22:52:51 +0000272 if (gd->be_quiet)
273 return;
274#endif
275
wdenk4a9cbbe2002-08-27 09:48:53 +0000276 if (c == '\n')
wdenk281e00a2004-08-01 22:48:16 +0000277 smc_putc ('\r');
wdenk4a9cbbe2002-08-27 09:48:53 +0000278
279 up = (smc_uart_t *)&cpmp->cp_dparam[PROFF_SMC];
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200280#ifdef CONFIG_SYS_SMC_UCODE_PATCH
Heiko Schocherb423d052008-01-11 01:12:07 +0100281 up = (smc_uart_t *) &cpmp->cp_dpmem[up->smc_rpbase];
282#endif
wdenk4a9cbbe2002-08-27 09:48:53 +0000283
Heiko Schocher2b3f12c2009-02-10 09:31:47 +0100284 rtx = (serialbuffer_t *)&cpmp->cp_dpmem[up->smc_rbase];
wdenk4a9cbbe2002-08-27 09:48:53 +0000285
Heiko Schocher255d28e2009-02-10 09:32:38 +0100286 /* Wait for last character to go. */
Heiko Schocher2b3f12c2009-02-10 09:31:47 +0100287 rtx->txbuf = c;
288 rtx->txbd.cbd_datlen = 1;
289 rtx->txbd.cbd_sc |= BD_SC_READY;
wdenk4a9cbbe2002-08-27 09:48:53 +0000290 __asm__("eieio");
wdenkd0fb80c2003-01-11 09:48:40 +0000291
Heiko Schocher2b3f12c2009-02-10 09:31:47 +0100292 while (rtx->txbd.cbd_sc & BD_SC_READY) {
wdenkd0fb80c2003-01-11 09:48:40 +0000293 WATCHDOG_RESET ();
wdenk4a9cbbe2002-08-27 09:48:53 +0000294 __asm__("eieio");
wdenkd0fb80c2003-01-11 09:48:40 +0000295 }
wdenk4a9cbbe2002-08-27 09:48:53 +0000296}
297
wdenk281e00a2004-08-01 22:48:16 +0000298static void
299smc_puts (const char *s)
300{
301 while (*s) {
302 smc_putc (*s++);
303 }
304}
305
306static int
307smc_getc(void)
wdenk4a9cbbe2002-08-27 09:48:53 +0000308{
wdenk4a9cbbe2002-08-27 09:48:53 +0000309 volatile smc_uart_t *up;
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200310 volatile immap_t *im = (immap_t *)CONFIG_SYS_IMMR;
wdenk4a9cbbe2002-08-27 09:48:53 +0000311 volatile cpm8xx_t *cpmp = &(im->im_cpm);
Heiko Schocher2b3f12c2009-02-10 09:31:47 +0100312 volatile serialbuffer_t *rtx;
313 unsigned char c;
wdenk4a9cbbe2002-08-27 09:48:53 +0000314
315 up = (smc_uart_t *)&cpmp->cp_dparam[PROFF_SMC];
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200316#ifdef CONFIG_SYS_SMC_UCODE_PATCH
Heiko Schocherb423d052008-01-11 01:12:07 +0100317 up = (smc_uart_t *) &cpmp->cp_dpmem[up->smc_rpbase];
318#endif
Heiko Schocher2b3f12c2009-02-10 09:31:47 +0100319 rtx = (serialbuffer_t *)&cpmp->cp_dpmem[up->smc_rbase];
wdenk4a9cbbe2002-08-27 09:48:53 +0000320
Heiko Schocher255d28e2009-02-10 09:32:38 +0100321 /* Wait for character to show up. */
Heiko Schocher2b3f12c2009-02-10 09:31:47 +0100322 while (rtx->rxbd.cbd_sc & BD_SC_EMPTY)
wdenkd0fb80c2003-01-11 09:48:40 +0000323 WATCHDOG_RESET ();
324
Heiko Schocher2b3f12c2009-02-10 09:31:47 +0100325 /* the characters are read one by one,
326 * use the rxindex to know the next char to deliver
327 */
328 c = *(unsigned char *) (rtx->rxbd.cbd_bufaddr+rtx->rxindex);
329 rtx->rxindex++;
wdenk4a9cbbe2002-08-27 09:48:53 +0000330
Heiko Schocher2b3f12c2009-02-10 09:31:47 +0100331 /* check if all char are readout, then make prepare for next receive */
332 if (rtx->rxindex >= rtx->rxbd.cbd_datlen) {
333 rtx->rxindex = 0;
334 rtx->rxbd.cbd_sc |= BD_SC_EMPTY;
335 }
wdenk4a9cbbe2002-08-27 09:48:53 +0000336 return(c);
337}
338
wdenk281e00a2004-08-01 22:48:16 +0000339static int
340smc_tstc(void)
wdenk4a9cbbe2002-08-27 09:48:53 +0000341{
wdenk4a9cbbe2002-08-27 09:48:53 +0000342 volatile smc_uart_t *up;
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200343 volatile immap_t *im = (immap_t *)CONFIG_SYS_IMMR;
wdenk4a9cbbe2002-08-27 09:48:53 +0000344 volatile cpm8xx_t *cpmp = &(im->im_cpm);
Heiko Schocher2b3f12c2009-02-10 09:31:47 +0100345 volatile serialbuffer_t *rtx;
wdenk4a9cbbe2002-08-27 09:48:53 +0000346
347 up = (smc_uart_t *)&cpmp->cp_dparam[PROFF_SMC];
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200348#ifdef CONFIG_SYS_SMC_UCODE_PATCH
Heiko Schocherb423d052008-01-11 01:12:07 +0100349 up = (smc_uart_t *) &cpmp->cp_dpmem[up->smc_rpbase];
350#endif
wdenk4a9cbbe2002-08-27 09:48:53 +0000351
Heiko Schocher2b3f12c2009-02-10 09:31:47 +0100352 rtx = (serialbuffer_t *)&cpmp->cp_dpmem[up->smc_rbase];
wdenk4a9cbbe2002-08-27 09:48:53 +0000353
Heiko Schocher2b3f12c2009-02-10 09:31:47 +0100354 return !(rtx->rxbd.cbd_sc & BD_SC_EMPTY);
wdenk4a9cbbe2002-08-27 09:48:53 +0000355}
356
wdenk281e00a2004-08-01 22:48:16 +0000357struct serial_device serial_smc_device =
358{
Marek Vasut90bad892012-09-09 18:48:28 +0200359 .name = "serial_smc",
360 .start = smc_init,
361 .stop = NULL,
362 .setbrg = smc_setbrg,
363 .getc = smc_getc,
364 .tstc = smc_tstc,
365 .putc = smc_putc,
366 .puts = smc_puts,
wdenk281e00a2004-08-01 22:48:16 +0000367};
wdenk4a9cbbe2002-08-27 09:48:53 +0000368
wdenk281e00a2004-08-01 22:48:16 +0000369#endif /* CONFIG_8xx_CONS_SMC1 || CONFIG_8xx_CONS_SMC2 */
370
371#if defined(CONFIG_8xx_CONS_SCC1) || defined(CONFIG_8xx_CONS_SCC2) || \
372 defined(CONFIG_8xx_CONS_SCC3) || defined(CONFIG_8xx_CONS_SCC4)
373
374static void
375scc_setbrg (void)
376{
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200377 volatile immap_t *im = (immap_t *)CONFIG_SYS_IMMR;
wdenk281e00a2004-08-01 22:48:16 +0000378 volatile cpm8xx_t *cp = &(im->im_cpm);
379
380 /* Set up the baud rate generator.
381 * See 8xx_io/commproc.c for details.
382 *
383 * Wire BRG1 to SCCx
384 */
385
386 cp->cp_sicr &= ~(0x000000FF << (8 * SCC_INDEX));
387
388 serial_setdivisor(cp);
389}
390
391static int scc_init (void)
wdenk4a9cbbe2002-08-27 09:48:53 +0000392{
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200393 volatile immap_t *im = (immap_t *)CONFIG_SYS_IMMR;
wdenk4a9cbbe2002-08-27 09:48:53 +0000394 volatile scc_t *sp;
395 volatile scc_uart_t *up;
396 volatile cbd_t *tbdf, *rbdf;
397 volatile cpm8xx_t *cp = &(im->im_cpm);
398 uint dpaddr;
399#if (SCC_INDEX != 2) || !defined(CONFIG_MPC850)
400 volatile iop8xx_t *ip = (iop8xx_t *)&(im->im_ioport);
401#endif
402
403 /* initialize pointers to SCC */
404
405 sp = (scc_t *) &(cp->cp_scc[SCC_INDEX]);
406 up = (scc_uart_t *) &cp->cp_dparam[PROFF_SCC];
407
Heiko Schocher255d28e2009-02-10 09:32:38 +0100408 /* Disable transmitter/receiver. */
wdenk4a9cbbe2002-08-27 09:48:53 +0000409 sp->scc_gsmrl &= ~(SCC_GSMRL_ENR | SCC_GSMRL_ENT);
410
411#if (SCC_INDEX == 2) && defined(CONFIG_MPC850)
412 /*
413 * The MPC850 has SCC3 on Port B
414 */
415 cp->cp_pbpar |= 0x06;
416 cp->cp_pbdir &= ~0x06;
417 cp->cp_pbodr &= ~0x06;
418
419#elif (SCC_INDEX < 2) || !defined(CONFIG_IP860)
420 /*
421 * Standard configuration for SCC's is on Part A
422 */
423 ip->iop_papar |= ((3 << (2 * SCC_INDEX)));
424 ip->iop_padir &= ~((3 << (2 * SCC_INDEX)));
425 ip->iop_paodr &= ~((3 << (2 * SCC_INDEX)));
426#else
427 /*
428 * The IP860 has SCC3 and SCC4 on Port D
429 */
430 ip->iop_pdpar |= ((3 << (2 * SCC_INDEX)));
431#endif
432
Heiko Schocher255d28e2009-02-10 09:32:38 +0100433 /* Allocate space for two buffer descriptors in the DP ram. */
wdenk4a9cbbe2002-08-27 09:48:53 +0000434
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200435#ifdef CONFIG_SYS_ALLOC_DPRAM
wdenk4a9cbbe2002-08-27 09:48:53 +0000436 dpaddr = dpram_alloc_align (sizeof(cbd_t)*2 + 2, 8) ;
437#else
wdenk281e00a2004-08-01 22:48:16 +0000438 dpaddr = CPM_SERIAL2_BASE ;
wdenk4a9cbbe2002-08-27 09:48:53 +0000439#endif
440
Heiko Schocher255d28e2009-02-10 09:32:38 +0100441 /* Enable SDMA. */
wdenk4a9cbbe2002-08-27 09:48:53 +0000442 im->im_siu_conf.sc_sdcr = 0x0001;
443
444 /* Set the physical address of the host memory buffers in
445 * the buffer descriptors.
446 */
447
448 rbdf = (cbd_t *)&cp->cp_dpmem[dpaddr];
449 rbdf->cbd_bufaddr = (uint) (rbdf+2);
450 rbdf->cbd_sc = 0;
451 tbdf = rbdf + 1;
452 tbdf->cbd_bufaddr = ((uint) (rbdf+2)) + 1;
453 tbdf->cbd_sc = 0;
454
Heiko Schocher255d28e2009-02-10 09:32:38 +0100455 /* Set up the baud rate generator. */
wdenk281e00a2004-08-01 22:48:16 +0000456 scc_setbrg ();
wdenk4a9cbbe2002-08-27 09:48:53 +0000457
Heiko Schocher255d28e2009-02-10 09:32:38 +0100458 /* Set up the uart parameters in the parameter ram. */
wdenk4a9cbbe2002-08-27 09:48:53 +0000459 up->scc_genscc.scc_rbase = dpaddr;
460 up->scc_genscc.scc_tbase = dpaddr+sizeof(cbd_t);
461
Heiko Schocher255d28e2009-02-10 09:32:38 +0100462 /* Initialize Tx/Rx parameters. */
wdenk4a9cbbe2002-08-27 09:48:53 +0000463 while (cp->cp_cpcr & CPM_CR_FLG) /* wait if cp is busy */
464 ;
465 cp->cp_cpcr = mk_cr_cmd(CPM_CR_CH_SCC, CPM_CR_INIT_TRX) | CPM_CR_FLG;
466
467 while (cp->cp_cpcr & CPM_CR_FLG) /* wait if cp is busy */
468 ;
469
470 up->scc_genscc.scc_rfcr = SCC_EB | 0x05;
471 up->scc_genscc.scc_tfcr = SCC_EB | 0x05;
472
473 up->scc_genscc.scc_mrblr = 1; /* Single character receive */
474 up->scc_maxidl = 0; /* disable max idle */
475 up->scc_brkcr = 1; /* send one break character on stop TX */
476 up->scc_parec = 0;
477 up->scc_frmec = 0;
478 up->scc_nosec = 0;
479 up->scc_brkec = 0;
480 up->scc_uaddr1 = 0;
481 up->scc_uaddr2 = 0;
482 up->scc_toseq = 0;
483 up->scc_char1 = 0x8000;
484 up->scc_char2 = 0x8000;
485 up->scc_char3 = 0x8000;
486 up->scc_char4 = 0x8000;
487 up->scc_char5 = 0x8000;
488 up->scc_char6 = 0x8000;
489 up->scc_char7 = 0x8000;
490 up->scc_char8 = 0x8000;
491 up->scc_rccm = 0xc0ff;
492
Heiko Schocher255d28e2009-02-10 09:32:38 +0100493 /* Set low latency / small fifo. */
wdenk4a9cbbe2002-08-27 09:48:53 +0000494 sp->scc_gsmrh = SCC_GSMRH_RFW;
495
496 /* Set SCC(x) clock mode to 16x
497 * See 8xx_io/commproc.c for details.
498 *
499 * Wire BRG1 to SCCn
500 */
501
Heiko Schocher255d28e2009-02-10 09:32:38 +0100502 /* Set UART mode, clock divider 16 on Tx and Rx */
wdenk281e00a2004-08-01 22:48:16 +0000503 sp->scc_gsmrl &= ~0xF;
wdenk4a9cbbe2002-08-27 09:48:53 +0000504 sp->scc_gsmrl |=
505 (SCC_GSMRL_MODE_UART | SCC_GSMRL_TDCR_16 | SCC_GSMRL_RDCR_16);
506
wdenk281e00a2004-08-01 22:48:16 +0000507 sp->scc_psmr = 0;
wdenk4a9cbbe2002-08-27 09:48:53 +0000508 sp->scc_psmr |= SCU_PSMR_CL;
509
Heiko Schocher255d28e2009-02-10 09:32:38 +0100510 /* Mask all interrupts and remove anything pending. */
wdenk4a9cbbe2002-08-27 09:48:53 +0000511 sp->scc_sccm = 0;
512 sp->scc_scce = 0xffff;
513 sp->scc_dsr = 0x7e7e;
514 sp->scc_psmr = 0x3000;
515
Heiko Schocher255d28e2009-02-10 09:32:38 +0100516 /* Make the first buffer the only buffer. */
wdenk4a9cbbe2002-08-27 09:48:53 +0000517 tbdf->cbd_sc |= BD_SC_WRAP;
518 rbdf->cbd_sc |= BD_SC_EMPTY | BD_SC_WRAP;
519
Heiko Schocher255d28e2009-02-10 09:32:38 +0100520 /* Enable transmitter/receiver. */
wdenk4a9cbbe2002-08-27 09:48:53 +0000521 sp->scc_gsmrl |= (SCC_GSMRL_ENR | SCC_GSMRL_ENT);
522
523 return (0);
524}
525
wdenk281e00a2004-08-01 22:48:16 +0000526static void
527scc_putc(const char c)
wdenk4a9cbbe2002-08-27 09:48:53 +0000528{
529 volatile cbd_t *tbdf;
530 volatile char *buf;
531 volatile scc_uart_t *up;
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200532 volatile immap_t *im = (immap_t *)CONFIG_SYS_IMMR;
wdenk4a9cbbe2002-08-27 09:48:53 +0000533 volatile cpm8xx_t *cpmp = &(im->im_cpm);
534
wdenk281e00a2004-08-01 22:48:16 +0000535#ifdef CONFIG_MODEM_SUPPORT
wdenk281e00a2004-08-01 22:48:16 +0000536 if (gd->be_quiet)
537 return;
538#endif
539
wdenk4a9cbbe2002-08-27 09:48:53 +0000540 if (c == '\n')
wdenk281e00a2004-08-01 22:48:16 +0000541 scc_putc ('\r');
wdenk4a9cbbe2002-08-27 09:48:53 +0000542
543 up = (scc_uart_t *)&cpmp->cp_dparam[PROFF_SCC];
544
545 tbdf = (cbd_t *)&cpmp->cp_dpmem[up->scc_genscc.scc_tbase];
546
Heiko Schocher255d28e2009-02-10 09:32:38 +0100547 /* Wait for last character to go. */
wdenk4a9cbbe2002-08-27 09:48:53 +0000548
549 buf = (char *)tbdf->cbd_bufaddr;
wdenk4a9cbbe2002-08-27 09:48:53 +0000550
551 *buf = c;
552 tbdf->cbd_datlen = 1;
553 tbdf->cbd_sc |= BD_SC_READY;
554 __asm__("eieio");
wdenkd0fb80c2003-01-11 09:48:40 +0000555
556 while (tbdf->cbd_sc & BD_SC_READY) {
wdenk4a9cbbe2002-08-27 09:48:53 +0000557 __asm__("eieio");
wdenkd0fb80c2003-01-11 09:48:40 +0000558 WATCHDOG_RESET ();
559 }
wdenk4a9cbbe2002-08-27 09:48:53 +0000560}
561
wdenk281e00a2004-08-01 22:48:16 +0000562static void
563scc_puts (const char *s)
564{
565 while (*s) {
566 scc_putc (*s++);
567 }
568}
569
570static int
571scc_getc(void)
wdenk4a9cbbe2002-08-27 09:48:53 +0000572{
573 volatile cbd_t *rbdf;
574 volatile unsigned char *buf;
575 volatile scc_uart_t *up;
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200576 volatile immap_t *im = (immap_t *)CONFIG_SYS_IMMR;
wdenk4a9cbbe2002-08-27 09:48:53 +0000577 volatile cpm8xx_t *cpmp = &(im->im_cpm);
578 unsigned char c;
579
580 up = (scc_uart_t *)&cpmp->cp_dparam[PROFF_SCC];
581
582 rbdf = (cbd_t *)&cpmp->cp_dpmem[up->scc_genscc.scc_rbase];
583
Heiko Schocher255d28e2009-02-10 09:32:38 +0100584 /* Wait for character to show up. */
wdenk4a9cbbe2002-08-27 09:48:53 +0000585 buf = (unsigned char *)rbdf->cbd_bufaddr;
wdenkd0fb80c2003-01-11 09:48:40 +0000586
wdenk4a9cbbe2002-08-27 09:48:53 +0000587 while (rbdf->cbd_sc & BD_SC_EMPTY)
wdenkd0fb80c2003-01-11 09:48:40 +0000588 WATCHDOG_RESET ();
589
wdenk4a9cbbe2002-08-27 09:48:53 +0000590 c = *buf;
591 rbdf->cbd_sc |= BD_SC_EMPTY;
592
593 return(c);
594}
595
wdenk281e00a2004-08-01 22:48:16 +0000596static int
597scc_tstc(void)
wdenk4a9cbbe2002-08-27 09:48:53 +0000598{
599 volatile cbd_t *rbdf;
600 volatile scc_uart_t *up;
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200601 volatile immap_t *im = (immap_t *)CONFIG_SYS_IMMR;
wdenk4a9cbbe2002-08-27 09:48:53 +0000602 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
wdenk281e00a2004-08-01 22:48:16 +0000611struct serial_device serial_scc_device =
wdenk4a9cbbe2002-08-27 09:48:53 +0000612{
Marek Vasut90bad892012-09-09 18:48:28 +0200613 .name = "serial_scc",
614 .start = scc_init,
615 .stop = NULL,
616 .setbrg = scc_setbrg,
617 .getc = scc_getc,
618 .tstc = scc_tstc,
619 .putc = scc_putc,
620 .puts = scc_puts,
wdenk281e00a2004-08-01 22:48:16 +0000621};
622
623#endif /* CONFIG_8xx_CONS_SCCx */
624
Mike Frysinger6c768ca2011-04-29 18:03:29 +0000625__weak struct serial_device *default_serial_console(void)
626{
627#if defined(CONFIG_8xx_CONS_SMC1) || defined(CONFIG_8xx_CONS_SMC2)
628 return &serial_smc_device;
629#else
630 return &serial_scc_device;
631#endif
632}
633
Marek Vasutf0eb1f62012-09-12 13:50:56 +0200634void mpc8xx_serial_initialize(void)
635{
636#if defined(CONFIG_8xx_CONS_SMC1) || defined(CONFIG_8xx_CONS_SMC2)
637 serial_register(&serial_smc_device);
638#endif
639#if defined(CONFIG_8xx_CONS_SCC1) || defined(CONFIG_8xx_CONS_SCC2) || \
640 defined(CONFIG_8xx_CONS_SCC3) || defined(CONFIG_8xx_CONS_SCC4)
641 serial_register(&serial_scc_device);
642#endif
643}
644
wdenk281e00a2004-08-01 22:48:16 +0000645#ifdef CONFIG_MODEM_SUPPORT
646void disable_putc(void)
647{
wdenk281e00a2004-08-01 22:48:16 +0000648 gd->be_quiet = 1;
wdenk4a9cbbe2002-08-27 09:48:53 +0000649}
650
wdenk281e00a2004-08-01 22:48:16 +0000651void enable_putc(void)
652{
wdenk281e00a2004-08-01 22:48:16 +0000653 gd->be_quiet = 0;
654}
655#endif
wdenk4a9cbbe2002-08-27 09:48:53 +0000656
Jon Loeliger44312832007-07-09 19:06:00 -0500657#if defined(CONFIG_CMD_KGDB)
wdenk4a9cbbe2002-08-27 09:48:53 +0000658
659void
660kgdb_serial_init(void)
661{
wdenk281e00a2004-08-01 22:48:16 +0000662 int i = -1;
663
Mike Frysinger1c9a5602011-04-29 18:03:31 +0000664 if (strcmp(default_serial_console()->name, "serial_smc") == 0)
wdenk281e00a2004-08-01 22:48:16 +0000665 {
wdenk4a9cbbe2002-08-27 09:48:53 +0000666#if defined(CONFIG_8xx_CONS_SMC1)
wdenk281e00a2004-08-01 22:48:16 +0000667 i = 1;
wdenk4a9cbbe2002-08-27 09:48:53 +0000668#elif defined(CONFIG_8xx_CONS_SMC2)
wdenk281e00a2004-08-01 22:48:16 +0000669 i = 2;
wdenk4a9cbbe2002-08-27 09:48:53 +0000670#endif
wdenk281e00a2004-08-01 22:48:16 +0000671 }
Mike Frysinger1c9a5602011-04-29 18:03:31 +0000672 else if (strcmp(default_serial_console()->name, "serial_scc") == 0)
wdenk281e00a2004-08-01 22:48:16 +0000673 {
674#if defined(CONFIG_8xx_CONS_SCC1)
675 i = 1;
676#elif defined(CONFIG_8xx_CONS_SCC2)
677 i = 2;
678#elif defined(CONFIG_8xx_CONS_SCC3)
679 i = 3;
680#elif defined(CONFIG_8xx_CONS_SCC4)
681 i = 4;
682#endif
683 }
684
685 if (i >= 0)
686 {
Mike Frysinger1c9a5602011-04-29 18:03:31 +0000687 serial_printf("[on %s%d] ", default_serial_console()->name, i);
wdenk281e00a2004-08-01 22:48:16 +0000688 }
wdenk4a9cbbe2002-08-27 09:48:53 +0000689}
690
691void
692putDebugChar (int c)
693{
694 serial_putc (c);
695}
696
697void
698putDebugStr (const char *str)
699{
700 serial_puts (str);
701}
702
703int
704getDebugChar (void)
705{
706 return serial_getc();
707}
708
709void
710kgdb_interruptible (int yes)
711{
712 return;
713}
Jon Loeliger068b60a2007-07-10 10:27:39 -0500714#endif
wdenk4a9cbbe2002-08-27 09:48:53 +0000715
716#endif /* CONFIG_8xx_CONS_NONE */