blob: 4e840498d79e2db909d7a059655dcc5c3cd03e7f [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 Denkd79de1d2013-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>
wdenk7ac16102004-08-01 22:48:16 +000011#include <serial.h>
wdenke7f34c62003-01-11 09:48:40 +000012#include <watchdog.h>
Mike Frysingerf96c0422011-04-29 18:03:29 +000013#include <linux/compiler.h>
wdenk4a9cbbe2002-08-27 09:48:53 +000014
Wolfgang Denk6405a152006-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
wdenk7ac16102004-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
wdenk7ac16102004-08-01 22:48:16 +000051#endif /* CONFIG_8xx_CONS_SCCx */
wdenk4a9cbbe2002-08-27 09:48:53 +000052
Heiko Schocher362447f2009-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
wdenk2bb11052003-07-17 23:16:40 +000070static void serial_setdivisor(volatile cpm8xx_t *cp)
71{
wdenkfde37042004-01-31 20:06:54 +000072 int divisor=(gd->cpu_clk + 8*gd->baudrate)/16/gd->baudrate;
wdenk2bb11052003-07-17 23:16:40 +000073
74 if(divisor/16>0x1000) {
Wolfgang Denkaf0501a2008-10-19 02:35:50 +020075 /* bad divisor, assume 50MHz clock and 9600 baud */
wdenkfde37042004-01-31 20:06:54 +000076 divisor=(50*1000*1000 + 8*9600)/16/9600;
wdenk2bb11052003-07-17 23:16:40 +000077 }
78
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +020079#ifdef CONFIG_SYS_BRGCLK_PRESCALE
80 divisor /= CONFIG_SYS_BRGCLK_PRESCALE;
wdenk174e0e52003-12-07 22:27:15 +000081#endif
82
wdenk2bb11052003-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
wdenk7ac16102004-08-01 22:48:16 +000097static void smc_setbrg (void)
wdenk4a9cbbe2002-08-27 09:48:53 +000098{
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +020099 volatile immap_t *im = (immap_t *)CONFIG_SYS_IMMR;
wdenk7ac16102004-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)
114{
Jean-Christophe PLAGNIOL-VILLARD03836942008-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 Schocher362447f2009-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-VILLARD03836942008-10-16 15:01:15 +0200129#ifdef CONFIG_SYS_SMC_UCODE_PATCH
Heiko Schocherc8148ed2008-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 Schocherbdfb6bd2009-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 Schocherbdfb6bd2009-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-VILLARD03836942008-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-VILLARD03836942008-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 Schocherbdfb6bd2009-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 Schocherbdfb6bd2009-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 Schocherbdfb6bd2009-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
wdenk444f22b2003-12-07 21:39:28 +0000176#if defined(CONFIG_FADS) || defined(CONFIG_ADS)
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
185#if defined(CONFIG_RPXLITE) || defined(CONFIG_RPXCLASSIC)
186 /* 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-VILLARD03836942008-10-16 15:01:15 +0200194#ifdef CONFIG_SYS_ALLOC_DPRAM
Heiko Schocher362447f2009-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 Schocher362447f2009-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 Schocher362447f2009-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 Schocherbdfb6bd2009-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-VILLARD03836942008-10-16 15:01:15 +0200221#if defined (CONFIG_SYS_SMC_UCODE_PATCH)
Heiko Schocherc8148ed2008-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
228#if defined(CONFIG_MBX)
229 board_serial_init();
230#endif /* CONFIG_MBX */
231
232 /* Set UART mode, 8 bit, no parity, one stop.
233 * Enable receive and transmit.
234 */
235 sp->smc_smcmr = smcr_mk_clen(9) | SMCMR_SM_UART;
236
237 /* Mask all interrupts and remove anything pending.
238 */
239 sp->smc_smcm = 0;
240 sp->smc_smce = 0xff;
241
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200242#ifdef CONFIG_SYS_SPC1920_SMC1_CLK4
Markus Klotzbuechercf7bf182007-01-09 14:57:11 +0100243 /* clock source is PLD */
Wolfgang Denkab5ea782007-03-21 23:26:15 +0100244
Markus Klotzbuechercf7bf182007-01-09 14:57:11 +0100245 /* set freq to 19200 Baud */
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200246 *((volatile uchar *) CONFIG_SYS_SPC1920_PLD_BASE+6) = 0x3;
Markus Klotzbuechercf7bf182007-01-09 14:57:11 +0100247 /* configure clk4 as input */
248 im->im_ioport.iop_pdpar |= 0x800;
249 im->im_ioport.iop_pddir &= ~0x800;
Wolfgang Denk86370712007-01-15 13:41:04 +0100250
Wolfgang Denkab5ea782007-03-21 23:26:15 +0100251 cp->cp_simode = ((cp->cp_simode & ~0xf000) | 0x7000);
Markus Klotzbuecher8f824852006-07-12 08:48:24 +0200252#else
253 /* Set up the baud rate generator */
wdenk7ac16102004-08-01 22:48:16 +0000254 smc_setbrg ();
Markus Klotzbuecher8f824852006-07-12 08:48:24 +0200255#endif
wdenk4a9cbbe2002-08-27 09:48:53 +0000256
Heiko Schocherbdfb6bd2009-02-10 09:32:38 +0100257 /* Make the first buffer the only buffer. */
Heiko Schocher362447f2009-02-10 09:31:47 +0100258 rtx->txbd.cbd_sc |= BD_SC_WRAP;
259 rtx->rxbd.cbd_sc |= BD_SC_EMPTY | BD_SC_WRAP;
wdenk4a9cbbe2002-08-27 09:48:53 +0000260
Heiko Schocher362447f2009-02-10 09:31:47 +0100261 /* single/multi character receive. */
262 up->smc_mrblr = CONFIG_SYS_SMC_RXBUFLEN;
263 up->smc_maxidl = CONFIG_SYS_MAXIDLE;
264 rtx->rxindex = 0;
wdenk4a9cbbe2002-08-27 09:48:53 +0000265
Heiko Schocherbdfb6bd2009-02-10 09:32:38 +0100266 /* Initialize Tx/Rx parameters. */
wdenk4a9cbbe2002-08-27 09:48:53 +0000267 while (cp->cp_cpcr & CPM_CR_FLG) /* wait if cp is busy */
268 ;
269
270 cp->cp_cpcr = mk_cr_cmd(CPM_CR_CH_SMC, CPM_CR_INIT_TRX) | CPM_CR_FLG;
271
272 while (cp->cp_cpcr & CPM_CR_FLG) /* wait if cp is busy */
273 ;
274
Heiko Schocherbdfb6bd2009-02-10 09:32:38 +0100275 /* Enable transmitter/receiver. */
wdenk4a9cbbe2002-08-27 09:48:53 +0000276 sp->smc_smcmr |= SMCMR_REN | SMCMR_TEN;
277
278 return (0);
279}
280
wdenk7ac16102004-08-01 22:48:16 +0000281static void
282smc_putc(const char c)
wdenk4a9cbbe2002-08-27 09:48:53 +0000283{
wdenk4a9cbbe2002-08-27 09:48:53 +0000284 volatile smc_uart_t *up;
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200285 volatile immap_t *im = (immap_t *)CONFIG_SYS_IMMR;
wdenk4a9cbbe2002-08-27 09:48:53 +0000286 volatile cpm8xx_t *cpmp = &(im->im_cpm);
Heiko Schocher362447f2009-02-10 09:31:47 +0100287 volatile serialbuffer_t *rtx;
wdenk4a9cbbe2002-08-27 09:48:53 +0000288
wdenkc08f1582003-04-27 22:52:51 +0000289#ifdef CONFIG_MODEM_SUPPORT
wdenkc08f1582003-04-27 22:52:51 +0000290 if (gd->be_quiet)
291 return;
292#endif
293
wdenk4a9cbbe2002-08-27 09:48:53 +0000294 if (c == '\n')
wdenk7ac16102004-08-01 22:48:16 +0000295 smc_putc ('\r');
wdenk4a9cbbe2002-08-27 09:48:53 +0000296
297 up = (smc_uart_t *)&cpmp->cp_dparam[PROFF_SMC];
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200298#ifdef CONFIG_SYS_SMC_UCODE_PATCH
Heiko Schocherc8148ed2008-01-11 01:12:07 +0100299 up = (smc_uart_t *) &cpmp->cp_dpmem[up->smc_rpbase];
300#endif
wdenk4a9cbbe2002-08-27 09:48:53 +0000301
Heiko Schocher362447f2009-02-10 09:31:47 +0100302 rtx = (serialbuffer_t *)&cpmp->cp_dpmem[up->smc_rbase];
wdenk4a9cbbe2002-08-27 09:48:53 +0000303
Heiko Schocherbdfb6bd2009-02-10 09:32:38 +0100304 /* Wait for last character to go. */
Heiko Schocher362447f2009-02-10 09:31:47 +0100305 rtx->txbuf = c;
306 rtx->txbd.cbd_datlen = 1;
307 rtx->txbd.cbd_sc |= BD_SC_READY;
wdenk4a9cbbe2002-08-27 09:48:53 +0000308 __asm__("eieio");
wdenke7f34c62003-01-11 09:48:40 +0000309
Heiko Schocher362447f2009-02-10 09:31:47 +0100310 while (rtx->txbd.cbd_sc & BD_SC_READY) {
wdenke7f34c62003-01-11 09:48:40 +0000311 WATCHDOG_RESET ();
wdenk4a9cbbe2002-08-27 09:48:53 +0000312 __asm__("eieio");
wdenke7f34c62003-01-11 09:48:40 +0000313 }
wdenk4a9cbbe2002-08-27 09:48:53 +0000314}
315
wdenk7ac16102004-08-01 22:48:16 +0000316static void
317smc_puts (const char *s)
318{
319 while (*s) {
320 smc_putc (*s++);
321 }
322}
323
324static int
325smc_getc(void)
wdenk4a9cbbe2002-08-27 09:48:53 +0000326{
wdenk4a9cbbe2002-08-27 09:48:53 +0000327 volatile smc_uart_t *up;
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200328 volatile immap_t *im = (immap_t *)CONFIG_SYS_IMMR;
wdenk4a9cbbe2002-08-27 09:48:53 +0000329 volatile cpm8xx_t *cpmp = &(im->im_cpm);
Heiko Schocher362447f2009-02-10 09:31:47 +0100330 volatile serialbuffer_t *rtx;
331 unsigned char c;
wdenk4a9cbbe2002-08-27 09:48:53 +0000332
333 up = (smc_uart_t *)&cpmp->cp_dparam[PROFF_SMC];
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200334#ifdef CONFIG_SYS_SMC_UCODE_PATCH
Heiko Schocherc8148ed2008-01-11 01:12:07 +0100335 up = (smc_uart_t *) &cpmp->cp_dpmem[up->smc_rpbase];
336#endif
Heiko Schocher362447f2009-02-10 09:31:47 +0100337 rtx = (serialbuffer_t *)&cpmp->cp_dpmem[up->smc_rbase];
wdenk4a9cbbe2002-08-27 09:48:53 +0000338
Heiko Schocherbdfb6bd2009-02-10 09:32:38 +0100339 /* Wait for character to show up. */
Heiko Schocher362447f2009-02-10 09:31:47 +0100340 while (rtx->rxbd.cbd_sc & BD_SC_EMPTY)
wdenke7f34c62003-01-11 09:48:40 +0000341 WATCHDOG_RESET ();
342
Heiko Schocher362447f2009-02-10 09:31:47 +0100343 /* the characters are read one by one,
344 * use the rxindex to know the next char to deliver
345 */
346 c = *(unsigned char *) (rtx->rxbd.cbd_bufaddr+rtx->rxindex);
347 rtx->rxindex++;
wdenk4a9cbbe2002-08-27 09:48:53 +0000348
Heiko Schocher362447f2009-02-10 09:31:47 +0100349 /* check if all char are readout, then make prepare for next receive */
350 if (rtx->rxindex >= rtx->rxbd.cbd_datlen) {
351 rtx->rxindex = 0;
352 rtx->rxbd.cbd_sc |= BD_SC_EMPTY;
353 }
wdenk4a9cbbe2002-08-27 09:48:53 +0000354 return(c);
355}
356
wdenk7ac16102004-08-01 22:48:16 +0000357static int
358smc_tstc(void)
wdenk4a9cbbe2002-08-27 09:48:53 +0000359{
wdenk4a9cbbe2002-08-27 09:48:53 +0000360 volatile smc_uart_t *up;
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200361 volatile immap_t *im = (immap_t *)CONFIG_SYS_IMMR;
wdenk4a9cbbe2002-08-27 09:48:53 +0000362 volatile cpm8xx_t *cpmp = &(im->im_cpm);
Heiko Schocher362447f2009-02-10 09:31:47 +0100363 volatile serialbuffer_t *rtx;
wdenk4a9cbbe2002-08-27 09:48:53 +0000364
365 up = (smc_uart_t *)&cpmp->cp_dparam[PROFF_SMC];
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200366#ifdef CONFIG_SYS_SMC_UCODE_PATCH
Heiko Schocherc8148ed2008-01-11 01:12:07 +0100367 up = (smc_uart_t *) &cpmp->cp_dpmem[up->smc_rpbase];
368#endif
wdenk4a9cbbe2002-08-27 09:48:53 +0000369
Heiko Schocher362447f2009-02-10 09:31:47 +0100370 rtx = (serialbuffer_t *)&cpmp->cp_dpmem[up->smc_rbase];
wdenk4a9cbbe2002-08-27 09:48:53 +0000371
Heiko Schocher362447f2009-02-10 09:31:47 +0100372 return !(rtx->rxbd.cbd_sc & BD_SC_EMPTY);
wdenk4a9cbbe2002-08-27 09:48:53 +0000373}
374
wdenk7ac16102004-08-01 22:48:16 +0000375struct serial_device serial_smc_device =
376{
Marek Vasut5bcdf242012-09-09 18:48:28 +0200377 .name = "serial_smc",
378 .start = smc_init,
379 .stop = NULL,
380 .setbrg = smc_setbrg,
381 .getc = smc_getc,
382 .tstc = smc_tstc,
383 .putc = smc_putc,
384 .puts = smc_puts,
wdenk7ac16102004-08-01 22:48:16 +0000385};
wdenk4a9cbbe2002-08-27 09:48:53 +0000386
wdenk7ac16102004-08-01 22:48:16 +0000387#endif /* CONFIG_8xx_CONS_SMC1 || CONFIG_8xx_CONS_SMC2 */
388
389#if defined(CONFIG_8xx_CONS_SCC1) || defined(CONFIG_8xx_CONS_SCC2) || \
390 defined(CONFIG_8xx_CONS_SCC3) || defined(CONFIG_8xx_CONS_SCC4)
391
392static void
393scc_setbrg (void)
wdenk4a9cbbe2002-08-27 09:48:53 +0000394{
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200395 volatile immap_t *im = (immap_t *)CONFIG_SYS_IMMR;
wdenk7ac16102004-08-01 22:48:16 +0000396 volatile cpm8xx_t *cp = &(im->im_cpm);
397
398 /* Set up the baud rate generator.
399 * See 8xx_io/commproc.c for details.
400 *
401 * Wire BRG1 to SCCx
402 */
403
404 cp->cp_sicr &= ~(0x000000FF << (8 * SCC_INDEX));
405
406 serial_setdivisor(cp);
407}
408
409static int scc_init (void)
410{
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200411 volatile immap_t *im = (immap_t *)CONFIG_SYS_IMMR;
wdenk4a9cbbe2002-08-27 09:48:53 +0000412 volatile scc_t *sp;
413 volatile scc_uart_t *up;
414 volatile cbd_t *tbdf, *rbdf;
415 volatile cpm8xx_t *cp = &(im->im_cpm);
416 uint dpaddr;
417#if (SCC_INDEX != 2) || !defined(CONFIG_MPC850)
418 volatile iop8xx_t *ip = (iop8xx_t *)&(im->im_ioport);
419#endif
420
421 /* initialize pointers to SCC */
422
423 sp = (scc_t *) &(cp->cp_scc[SCC_INDEX]);
424 up = (scc_uart_t *) &cp->cp_dparam[PROFF_SCC];
425
426#if defined(CONFIG_LWMON) && defined(CONFIG_8xx_CONS_SCC2)
427 { /* Disable Ethernet, enable Serial */
428 uchar c;
429
430 c = pic_read (0x61);
431 c &= ~0x40; /* enable COM3 */
432 c |= 0x80; /* disable Ethernet */
433 pic_write (0x61, c);
434
435 /* enable RTS2 */
436 cp->cp_pbpar |= 0x2000;
437 cp->cp_pbdat |= 0x2000;
438 cp->cp_pbdir |= 0x2000;
439 }
440#endif /* CONFIG_LWMON */
441
Heiko Schocherbdfb6bd2009-02-10 09:32:38 +0100442 /* Disable transmitter/receiver. */
wdenk4a9cbbe2002-08-27 09:48:53 +0000443 sp->scc_gsmrl &= ~(SCC_GSMRL_ENR | SCC_GSMRL_ENT);
444
445#if (SCC_INDEX == 2) && defined(CONFIG_MPC850)
446 /*
447 * The MPC850 has SCC3 on Port B
448 */
449 cp->cp_pbpar |= 0x06;
450 cp->cp_pbdir &= ~0x06;
451 cp->cp_pbodr &= ~0x06;
452
453#elif (SCC_INDEX < 2) || !defined(CONFIG_IP860)
454 /*
455 * Standard configuration for SCC's is on Part A
456 */
457 ip->iop_papar |= ((3 << (2 * SCC_INDEX)));
458 ip->iop_padir &= ~((3 << (2 * SCC_INDEX)));
459 ip->iop_paodr &= ~((3 << (2 * SCC_INDEX)));
460#else
461 /*
462 * The IP860 has SCC3 and SCC4 on Port D
463 */
464 ip->iop_pdpar |= ((3 << (2 * SCC_INDEX)));
465#endif
466
Heiko Schocherbdfb6bd2009-02-10 09:32:38 +0100467 /* Allocate space for two buffer descriptors in the DP ram. */
wdenk4a9cbbe2002-08-27 09:48:53 +0000468
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200469#ifdef CONFIG_SYS_ALLOC_DPRAM
wdenk4a9cbbe2002-08-27 09:48:53 +0000470 dpaddr = dpram_alloc_align (sizeof(cbd_t)*2 + 2, 8) ;
471#else
wdenk7ac16102004-08-01 22:48:16 +0000472 dpaddr = CPM_SERIAL2_BASE ;
wdenk4a9cbbe2002-08-27 09:48:53 +0000473#endif
474
Heiko Schocherbdfb6bd2009-02-10 09:32:38 +0100475 /* Enable SDMA. */
wdenk4a9cbbe2002-08-27 09:48:53 +0000476 im->im_siu_conf.sc_sdcr = 0x0001;
477
478 /* Set the physical address of the host memory buffers in
479 * the buffer descriptors.
480 */
481
482 rbdf = (cbd_t *)&cp->cp_dpmem[dpaddr];
483 rbdf->cbd_bufaddr = (uint) (rbdf+2);
484 rbdf->cbd_sc = 0;
485 tbdf = rbdf + 1;
486 tbdf->cbd_bufaddr = ((uint) (rbdf+2)) + 1;
487 tbdf->cbd_sc = 0;
488
Heiko Schocherbdfb6bd2009-02-10 09:32:38 +0100489 /* Set up the baud rate generator. */
wdenk7ac16102004-08-01 22:48:16 +0000490 scc_setbrg ();
wdenk4a9cbbe2002-08-27 09:48:53 +0000491
Heiko Schocherbdfb6bd2009-02-10 09:32:38 +0100492 /* Set up the uart parameters in the parameter ram. */
wdenk4a9cbbe2002-08-27 09:48:53 +0000493 up->scc_genscc.scc_rbase = dpaddr;
494 up->scc_genscc.scc_tbase = dpaddr+sizeof(cbd_t);
495
Heiko Schocherbdfb6bd2009-02-10 09:32:38 +0100496 /* Initialize Tx/Rx parameters. */
wdenk4a9cbbe2002-08-27 09:48:53 +0000497 while (cp->cp_cpcr & CPM_CR_FLG) /* wait if cp is busy */
498 ;
499 cp->cp_cpcr = mk_cr_cmd(CPM_CR_CH_SCC, CPM_CR_INIT_TRX) | CPM_CR_FLG;
500
501 while (cp->cp_cpcr & CPM_CR_FLG) /* wait if cp is busy */
502 ;
503
504 up->scc_genscc.scc_rfcr = SCC_EB | 0x05;
505 up->scc_genscc.scc_tfcr = SCC_EB | 0x05;
506
507 up->scc_genscc.scc_mrblr = 1; /* Single character receive */
508 up->scc_maxidl = 0; /* disable max idle */
509 up->scc_brkcr = 1; /* send one break character on stop TX */
510 up->scc_parec = 0;
511 up->scc_frmec = 0;
512 up->scc_nosec = 0;
513 up->scc_brkec = 0;
514 up->scc_uaddr1 = 0;
515 up->scc_uaddr2 = 0;
516 up->scc_toseq = 0;
517 up->scc_char1 = 0x8000;
518 up->scc_char2 = 0x8000;
519 up->scc_char3 = 0x8000;
520 up->scc_char4 = 0x8000;
521 up->scc_char5 = 0x8000;
522 up->scc_char6 = 0x8000;
523 up->scc_char7 = 0x8000;
524 up->scc_char8 = 0x8000;
525 up->scc_rccm = 0xc0ff;
526
Heiko Schocherbdfb6bd2009-02-10 09:32:38 +0100527 /* Set low latency / small fifo. */
wdenk4a9cbbe2002-08-27 09:48:53 +0000528 sp->scc_gsmrh = SCC_GSMRH_RFW;
529
530 /* Set SCC(x) clock mode to 16x
531 * See 8xx_io/commproc.c for details.
532 *
533 * Wire BRG1 to SCCn
534 */
535
Heiko Schocherbdfb6bd2009-02-10 09:32:38 +0100536 /* Set UART mode, clock divider 16 on Tx and Rx */
wdenk7ac16102004-08-01 22:48:16 +0000537 sp->scc_gsmrl &= ~0xF;
wdenk4a9cbbe2002-08-27 09:48:53 +0000538 sp->scc_gsmrl |=
539 (SCC_GSMRL_MODE_UART | SCC_GSMRL_TDCR_16 | SCC_GSMRL_RDCR_16);
540
wdenk7ac16102004-08-01 22:48:16 +0000541 sp->scc_psmr = 0;
wdenk4a9cbbe2002-08-27 09:48:53 +0000542 sp->scc_psmr |= SCU_PSMR_CL;
543
Heiko Schocherbdfb6bd2009-02-10 09:32:38 +0100544 /* Mask all interrupts and remove anything pending. */
wdenk4a9cbbe2002-08-27 09:48:53 +0000545 sp->scc_sccm = 0;
546 sp->scc_scce = 0xffff;
547 sp->scc_dsr = 0x7e7e;
548 sp->scc_psmr = 0x3000;
549
Heiko Schocherbdfb6bd2009-02-10 09:32:38 +0100550 /* Make the first buffer the only buffer. */
wdenk4a9cbbe2002-08-27 09:48:53 +0000551 tbdf->cbd_sc |= BD_SC_WRAP;
552 rbdf->cbd_sc |= BD_SC_EMPTY | BD_SC_WRAP;
553
Heiko Schocherbdfb6bd2009-02-10 09:32:38 +0100554 /* Enable transmitter/receiver. */
wdenk4a9cbbe2002-08-27 09:48:53 +0000555 sp->scc_gsmrl |= (SCC_GSMRL_ENR | SCC_GSMRL_ENT);
556
557 return (0);
558}
559
wdenk7ac16102004-08-01 22:48:16 +0000560static void
561scc_putc(const char c)
wdenk4a9cbbe2002-08-27 09:48:53 +0000562{
563 volatile cbd_t *tbdf;
564 volatile char *buf;
565 volatile scc_uart_t *up;
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200566 volatile immap_t *im = (immap_t *)CONFIG_SYS_IMMR;
wdenk4a9cbbe2002-08-27 09:48:53 +0000567 volatile cpm8xx_t *cpmp = &(im->im_cpm);
568
wdenk7ac16102004-08-01 22:48:16 +0000569#ifdef CONFIG_MODEM_SUPPORT
wdenk7ac16102004-08-01 22:48:16 +0000570 if (gd->be_quiet)
571 return;
572#endif
573
wdenk4a9cbbe2002-08-27 09:48:53 +0000574 if (c == '\n')
wdenk7ac16102004-08-01 22:48:16 +0000575 scc_putc ('\r');
wdenk4a9cbbe2002-08-27 09:48:53 +0000576
577 up = (scc_uart_t *)&cpmp->cp_dparam[PROFF_SCC];
578
579 tbdf = (cbd_t *)&cpmp->cp_dpmem[up->scc_genscc.scc_tbase];
580
Heiko Schocherbdfb6bd2009-02-10 09:32:38 +0100581 /* Wait for last character to go. */
wdenk4a9cbbe2002-08-27 09:48:53 +0000582
583 buf = (char *)tbdf->cbd_bufaddr;
wdenk4a9cbbe2002-08-27 09:48:53 +0000584
585 *buf = c;
586 tbdf->cbd_datlen = 1;
587 tbdf->cbd_sc |= BD_SC_READY;
588 __asm__("eieio");
wdenke7f34c62003-01-11 09:48:40 +0000589
590 while (tbdf->cbd_sc & BD_SC_READY) {
wdenk4a9cbbe2002-08-27 09:48:53 +0000591 __asm__("eieio");
wdenke7f34c62003-01-11 09:48:40 +0000592 WATCHDOG_RESET ();
593 }
wdenk4a9cbbe2002-08-27 09:48:53 +0000594}
595
wdenk7ac16102004-08-01 22:48:16 +0000596static void
597scc_puts (const char *s)
598{
599 while (*s) {
600 scc_putc (*s++);
601 }
602}
603
604static int
605scc_getc(void)
wdenk4a9cbbe2002-08-27 09:48:53 +0000606{
607 volatile cbd_t *rbdf;
608 volatile unsigned char *buf;
609 volatile scc_uart_t *up;
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200610 volatile immap_t *im = (immap_t *)CONFIG_SYS_IMMR;
wdenk4a9cbbe2002-08-27 09:48:53 +0000611 volatile cpm8xx_t *cpmp = &(im->im_cpm);
612 unsigned char c;
613
614 up = (scc_uart_t *)&cpmp->cp_dparam[PROFF_SCC];
615
616 rbdf = (cbd_t *)&cpmp->cp_dpmem[up->scc_genscc.scc_rbase];
617
Heiko Schocherbdfb6bd2009-02-10 09:32:38 +0100618 /* Wait for character to show up. */
wdenk4a9cbbe2002-08-27 09:48:53 +0000619 buf = (unsigned char *)rbdf->cbd_bufaddr;
wdenke7f34c62003-01-11 09:48:40 +0000620
wdenk4a9cbbe2002-08-27 09:48:53 +0000621 while (rbdf->cbd_sc & BD_SC_EMPTY)
wdenke7f34c62003-01-11 09:48:40 +0000622 WATCHDOG_RESET ();
623
wdenk4a9cbbe2002-08-27 09:48:53 +0000624 c = *buf;
625 rbdf->cbd_sc |= BD_SC_EMPTY;
626
627 return(c);
628}
629
wdenk7ac16102004-08-01 22:48:16 +0000630static int
631scc_tstc(void)
wdenk4a9cbbe2002-08-27 09:48:53 +0000632{
633 volatile cbd_t *rbdf;
634 volatile scc_uart_t *up;
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200635 volatile immap_t *im = (immap_t *)CONFIG_SYS_IMMR;
wdenk4a9cbbe2002-08-27 09:48:53 +0000636 volatile cpm8xx_t *cpmp = &(im->im_cpm);
637
638 up = (scc_uart_t *)&cpmp->cp_dparam[PROFF_SCC];
639
640 rbdf = (cbd_t *)&cpmp->cp_dpmem[up->scc_genscc.scc_rbase];
641
642 return(!(rbdf->cbd_sc & BD_SC_EMPTY));
643}
644
wdenk7ac16102004-08-01 22:48:16 +0000645struct serial_device serial_scc_device =
646{
Marek Vasut5bcdf242012-09-09 18:48:28 +0200647 .name = "serial_scc",
648 .start = scc_init,
649 .stop = NULL,
650 .setbrg = scc_setbrg,
651 .getc = scc_getc,
652 .tstc = scc_tstc,
653 .putc = scc_putc,
654 .puts = scc_puts,
wdenk7ac16102004-08-01 22:48:16 +0000655};
wdenk4a9cbbe2002-08-27 09:48:53 +0000656
wdenk7ac16102004-08-01 22:48:16 +0000657#endif /* CONFIG_8xx_CONS_SCCx */
wdenk4a9cbbe2002-08-27 09:48:53 +0000658
Mike Frysingerf96c0422011-04-29 18:03:29 +0000659__weak struct serial_device *default_serial_console(void)
660{
661#if defined(CONFIG_8xx_CONS_SMC1) || defined(CONFIG_8xx_CONS_SMC2)
662 return &serial_smc_device;
663#else
664 return &serial_scc_device;
665#endif
666}
667
Marek Vasut419d8942012-09-12 13:50:56 +0200668void mpc8xx_serial_initialize(void)
669{
670#if defined(CONFIG_8xx_CONS_SMC1) || defined(CONFIG_8xx_CONS_SMC2)
671 serial_register(&serial_smc_device);
672#endif
673#if defined(CONFIG_8xx_CONS_SCC1) || defined(CONFIG_8xx_CONS_SCC2) || \
674 defined(CONFIG_8xx_CONS_SCC3) || defined(CONFIG_8xx_CONS_SCC4)
675 serial_register(&serial_scc_device);
676#endif
677}
678
wdenk7ac16102004-08-01 22:48:16 +0000679#ifdef CONFIG_MODEM_SUPPORT
680void disable_putc(void)
wdenk4a9cbbe2002-08-27 09:48:53 +0000681{
wdenk7ac16102004-08-01 22:48:16 +0000682 gd->be_quiet = 1;
wdenk4a9cbbe2002-08-27 09:48:53 +0000683}
684
wdenk7ac16102004-08-01 22:48:16 +0000685void enable_putc(void)
686{
wdenk7ac16102004-08-01 22:48:16 +0000687 gd->be_quiet = 0;
688}
689#endif
wdenk4a9cbbe2002-08-27 09:48:53 +0000690
Jon Loeliger526e5ce2007-07-09 19:06:00 -0500691#if defined(CONFIG_CMD_KGDB)
wdenk4a9cbbe2002-08-27 09:48:53 +0000692
693void
694kgdb_serial_init(void)
695{
wdenk7ac16102004-08-01 22:48:16 +0000696 int i = -1;
697
Mike Frysinger1ef72f62011-04-29 18:03:31 +0000698 if (strcmp(default_serial_console()->name, "serial_smc") == 0)
wdenk7ac16102004-08-01 22:48:16 +0000699 {
wdenk4a9cbbe2002-08-27 09:48:53 +0000700#if defined(CONFIG_8xx_CONS_SMC1)
wdenk7ac16102004-08-01 22:48:16 +0000701 i = 1;
wdenk4a9cbbe2002-08-27 09:48:53 +0000702#elif defined(CONFIG_8xx_CONS_SMC2)
wdenk7ac16102004-08-01 22:48:16 +0000703 i = 2;
704#endif
705 }
Mike Frysinger1ef72f62011-04-29 18:03:31 +0000706 else if (strcmp(default_serial_console()->name, "serial_scc") == 0)
wdenk7ac16102004-08-01 22:48:16 +0000707 {
708#if defined(CONFIG_8xx_CONS_SCC1)
709 i = 1;
wdenk4a9cbbe2002-08-27 09:48:53 +0000710#elif defined(CONFIG_8xx_CONS_SCC2)
wdenk7ac16102004-08-01 22:48:16 +0000711 i = 2;
wdenk4a9cbbe2002-08-27 09:48:53 +0000712#elif defined(CONFIG_8xx_CONS_SCC3)
wdenk7ac16102004-08-01 22:48:16 +0000713 i = 3;
wdenk4a9cbbe2002-08-27 09:48:53 +0000714#elif defined(CONFIG_8xx_CONS_SCC4)
wdenk7ac16102004-08-01 22:48:16 +0000715 i = 4;
wdenk4a9cbbe2002-08-27 09:48:53 +0000716#endif
wdenk7ac16102004-08-01 22:48:16 +0000717 }
718
719 if (i >= 0)
720 {
Mike Frysinger1ef72f62011-04-29 18:03:31 +0000721 serial_printf("[on %s%d] ", default_serial_console()->name, i);
wdenk7ac16102004-08-01 22:48:16 +0000722 }
wdenk4a9cbbe2002-08-27 09:48:53 +0000723}
724
725void
726putDebugChar (int c)
727{
728 serial_putc (c);
729}
730
731void
732putDebugStr (const char *str)
733{
734 serial_puts (str);
735}
736
737int
738getDebugChar (void)
739{
740 return serial_getc();
741}
742
743void
744kgdb_interruptible (int yes)
745{
746 return;
747}
Jon Loeliger07efe2a2007-07-10 10:27:39 -0500748#endif
wdenk4a9cbbe2002-08-27 09:48:53 +0000749
750#endif /* CONFIG_8xx_CONS_NONE */