xref: /OK3568_Linux_fs/u-boot/drivers/serial/serial_mpc8xx.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun /*
2*4882a593Smuzhiyun  * (C) Copyright 2000
3*4882a593Smuzhiyun  * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
4*4882a593Smuzhiyun  *
5*4882a593Smuzhiyun  * SPDX-License-Identifier:	GPL-2.0+
6*4882a593Smuzhiyun  */
7*4882a593Smuzhiyun 
8*4882a593Smuzhiyun #include <common.h>
9*4882a593Smuzhiyun #include <command.h>
10*4882a593Smuzhiyun #include <serial.h>
11*4882a593Smuzhiyun #include <watchdog.h>
12*4882a593Smuzhiyun #include <asm/cpm_8xx.h>
13*4882a593Smuzhiyun #include <linux/compiler.h>
14*4882a593Smuzhiyun 
15*4882a593Smuzhiyun DECLARE_GLOBAL_DATA_PTR;
16*4882a593Smuzhiyun 
17*4882a593Smuzhiyun #if defined(CONFIG_8xx_CONS_SMC1)	/* Console on SMC1 */
18*4882a593Smuzhiyun #define	SMC_INDEX	0
19*4882a593Smuzhiyun #define PROFF_SMC	PROFF_SMC1
20*4882a593Smuzhiyun #define CPM_CR_CH_SMC	CPM_CR_CH_SMC1
21*4882a593Smuzhiyun #define IOPINS		0xc0
22*4882a593Smuzhiyun 
23*4882a593Smuzhiyun #elif defined(CONFIG_8xx_CONS_SMC2)	/* Console on SMC2 */
24*4882a593Smuzhiyun #define SMC_INDEX	1
25*4882a593Smuzhiyun #define PROFF_SMC	PROFF_SMC2
26*4882a593Smuzhiyun #define CPM_CR_CH_SMC	CPM_CR_CH_SMC2
27*4882a593Smuzhiyun #define IOPINS		0xc00
28*4882a593Smuzhiyun 
29*4882a593Smuzhiyun #endif /* CONFIG_8xx_CONS_SMCx */
30*4882a593Smuzhiyun 
31*4882a593Smuzhiyun struct serialbuffer {
32*4882a593Smuzhiyun 	cbd_t	rxbd;		/* Rx BD */
33*4882a593Smuzhiyun 	cbd_t	txbd;		/* Tx BD */
34*4882a593Smuzhiyun 	uint	rxindex;	/* index for next character to read */
35*4882a593Smuzhiyun 	uchar	rxbuf[CONFIG_SYS_SMC_RXBUFLEN];/* rx buffers */
36*4882a593Smuzhiyun 	uchar	txbuf;	/* tx buffers */
37*4882a593Smuzhiyun };
38*4882a593Smuzhiyun 
serial_setdivisor(cpm8xx_t __iomem * cp)39*4882a593Smuzhiyun static void serial_setdivisor(cpm8xx_t __iomem *cp)
40*4882a593Smuzhiyun {
41*4882a593Smuzhiyun 	int divisor = (gd->cpu_clk + 8 * gd->baudrate) / 16 / gd->baudrate;
42*4882a593Smuzhiyun 
43*4882a593Smuzhiyun 	if (divisor / 16 > 0x1000) {
44*4882a593Smuzhiyun 		/* bad divisor, assume 50MHz clock and 9600 baud */
45*4882a593Smuzhiyun 		divisor = (50 * 1000 * 1000 + 8 * 9600) / 16 / 9600;
46*4882a593Smuzhiyun 	}
47*4882a593Smuzhiyun 
48*4882a593Smuzhiyun 	divisor /= CONFIG_SYS_BRGCLK_PRESCALE;
49*4882a593Smuzhiyun 
50*4882a593Smuzhiyun 	if (divisor <= 0x1000)
51*4882a593Smuzhiyun 		out_be32(&cp->cp_brgc1, ((divisor - 1) << 1) | CPM_BRG_EN);
52*4882a593Smuzhiyun 	else
53*4882a593Smuzhiyun 		out_be32(&cp->cp_brgc1, ((divisor / 16 - 1) << 1) | CPM_BRG_EN |
54*4882a593Smuzhiyun 			 CPM_BRG_DIV16);
55*4882a593Smuzhiyun }
56*4882a593Smuzhiyun 
57*4882a593Smuzhiyun /*
58*4882a593Smuzhiyun  * Minimal serial functions needed to use one of the SMC ports
59*4882a593Smuzhiyun  * as serial console interface.
60*4882a593Smuzhiyun  */
61*4882a593Smuzhiyun 
smc_setbrg(void)62*4882a593Smuzhiyun static void smc_setbrg(void)
63*4882a593Smuzhiyun {
64*4882a593Smuzhiyun 	immap_t __iomem *im = (immap_t __iomem *)CONFIG_SYS_IMMR;
65*4882a593Smuzhiyun 	cpm8xx_t __iomem *cp = &(im->im_cpm);
66*4882a593Smuzhiyun 
67*4882a593Smuzhiyun 	/* Set up the baud rate generator.
68*4882a593Smuzhiyun 	 * See 8xx_io/commproc.c for details.
69*4882a593Smuzhiyun 	 *
70*4882a593Smuzhiyun 	 * Wire BRG1 to SMCx
71*4882a593Smuzhiyun 	 */
72*4882a593Smuzhiyun 
73*4882a593Smuzhiyun 	out_be32(&cp->cp_simode, 0);
74*4882a593Smuzhiyun 
75*4882a593Smuzhiyun 	serial_setdivisor(cp);
76*4882a593Smuzhiyun }
77*4882a593Smuzhiyun 
smc_init(void)78*4882a593Smuzhiyun static int smc_init(void)
79*4882a593Smuzhiyun {
80*4882a593Smuzhiyun 	immap_t __iomem *im = (immap_t __iomem *)CONFIG_SYS_IMMR;
81*4882a593Smuzhiyun 	smc_t __iomem *sp;
82*4882a593Smuzhiyun 	smc_uart_t __iomem *up;
83*4882a593Smuzhiyun 	cpm8xx_t __iomem *cp = &(im->im_cpm);
84*4882a593Smuzhiyun 	struct serialbuffer __iomem *rtx;
85*4882a593Smuzhiyun 
86*4882a593Smuzhiyun 	/* initialize pointers to SMC */
87*4882a593Smuzhiyun 
88*4882a593Smuzhiyun 	sp = cp->cp_smc + SMC_INDEX;
89*4882a593Smuzhiyun 	up = (smc_uart_t __iomem *)&cp->cp_dparam[PROFF_SMC];
90*4882a593Smuzhiyun 	/* Disable relocation */
91*4882a593Smuzhiyun 	out_be16(&up->smc_rpbase, 0);
92*4882a593Smuzhiyun 
93*4882a593Smuzhiyun 	/* Disable transmitter/receiver. */
94*4882a593Smuzhiyun 	clrbits_be16(&sp->smc_smcmr, SMCMR_REN | SMCMR_TEN);
95*4882a593Smuzhiyun 
96*4882a593Smuzhiyun 	/* Enable SDMA. */
97*4882a593Smuzhiyun 	out_be32(&im->im_siu_conf.sc_sdcr, 1);
98*4882a593Smuzhiyun 
99*4882a593Smuzhiyun 	/* clear error conditions */
100*4882a593Smuzhiyun 	out_8(&im->im_sdma.sdma_sdsr, CONFIG_SYS_SDSR);
101*4882a593Smuzhiyun 
102*4882a593Smuzhiyun 	/* clear SDMA interrupt mask */
103*4882a593Smuzhiyun 	out_8(&im->im_sdma.sdma_sdmr, CONFIG_SYS_SDMR);
104*4882a593Smuzhiyun 
105*4882a593Smuzhiyun 	/* Use Port B for SMCx instead of other functions. */
106*4882a593Smuzhiyun 	setbits_be32(&cp->cp_pbpar, IOPINS);
107*4882a593Smuzhiyun 	clrbits_be32(&cp->cp_pbdir, IOPINS);
108*4882a593Smuzhiyun 	clrbits_be16(&cp->cp_pbodr, IOPINS);
109*4882a593Smuzhiyun 
110*4882a593Smuzhiyun 	/* Set the physical address of the host memory buffers in
111*4882a593Smuzhiyun 	 * the buffer descriptors.
112*4882a593Smuzhiyun 	 */
113*4882a593Smuzhiyun 	rtx = (struct serialbuffer __iomem *)&cp->cp_dpmem[CPM_SERIAL_BASE];
114*4882a593Smuzhiyun 	/* Allocate space for two buffer descriptors in the DP ram.
115*4882a593Smuzhiyun 	 * For now, this address seems OK, but it may have to
116*4882a593Smuzhiyun 	 * change with newer versions of the firmware.
117*4882a593Smuzhiyun 	 * damm: allocating space after the two buffers for rx/tx data
118*4882a593Smuzhiyun 	 */
119*4882a593Smuzhiyun 
120*4882a593Smuzhiyun 	out_be32(&rtx->rxbd.cbd_bufaddr, (__force uint)&rtx->rxbuf);
121*4882a593Smuzhiyun 	out_be16(&rtx->rxbd.cbd_sc, 0);
122*4882a593Smuzhiyun 
123*4882a593Smuzhiyun 	out_be32(&rtx->txbd.cbd_bufaddr, (__force uint)&rtx->txbuf);
124*4882a593Smuzhiyun 	out_be16(&rtx->txbd.cbd_sc, 0);
125*4882a593Smuzhiyun 
126*4882a593Smuzhiyun 	/* Set up the uart parameters in the parameter ram. */
127*4882a593Smuzhiyun 	out_be16(&up->smc_rbase, CPM_SERIAL_BASE);
128*4882a593Smuzhiyun 	out_be16(&up->smc_tbase, CPM_SERIAL_BASE + sizeof(cbd_t));
129*4882a593Smuzhiyun 	out_8(&up->smc_rfcr, SMC_EB);
130*4882a593Smuzhiyun 	out_8(&up->smc_tfcr, SMC_EB);
131*4882a593Smuzhiyun 
132*4882a593Smuzhiyun 	/* Set UART mode, 8 bit, no parity, one stop.
133*4882a593Smuzhiyun 	 * Enable receive and transmit.
134*4882a593Smuzhiyun 	 */
135*4882a593Smuzhiyun 	out_be16(&sp->smc_smcmr, smcr_mk_clen(9) | SMCMR_SM_UART);
136*4882a593Smuzhiyun 
137*4882a593Smuzhiyun 	/* Mask all interrupts and remove anything pending.
138*4882a593Smuzhiyun 	*/
139*4882a593Smuzhiyun 	out_8(&sp->smc_smcm, 0);
140*4882a593Smuzhiyun 	out_8(&sp->smc_smce, 0xff);
141*4882a593Smuzhiyun 
142*4882a593Smuzhiyun 	/* Set up the baud rate generator */
143*4882a593Smuzhiyun 	smc_setbrg();
144*4882a593Smuzhiyun 
145*4882a593Smuzhiyun 	/* Make the first buffer the only buffer. */
146*4882a593Smuzhiyun 	setbits_be16(&rtx->txbd.cbd_sc, BD_SC_WRAP);
147*4882a593Smuzhiyun 	setbits_be16(&rtx->rxbd.cbd_sc, BD_SC_EMPTY | BD_SC_WRAP);
148*4882a593Smuzhiyun 
149*4882a593Smuzhiyun 	/* single/multi character receive. */
150*4882a593Smuzhiyun 	out_be16(&up->smc_mrblr, CONFIG_SYS_SMC_RXBUFLEN);
151*4882a593Smuzhiyun 	out_be16(&up->smc_maxidl, CONFIG_SYS_MAXIDLE);
152*4882a593Smuzhiyun 	out_be32(&rtx->rxindex, 0);
153*4882a593Smuzhiyun 
154*4882a593Smuzhiyun 	/* Initialize Tx/Rx parameters.	*/
155*4882a593Smuzhiyun 	while (in_be16(&cp->cp_cpcr) & CPM_CR_FLG)	/* wait if cp is busy */
156*4882a593Smuzhiyun 		;
157*4882a593Smuzhiyun 
158*4882a593Smuzhiyun 	out_be16(&cp->cp_cpcr,
159*4882a593Smuzhiyun 		 mk_cr_cmd(CPM_CR_CH_SMC, CPM_CR_INIT_TRX) | CPM_CR_FLG);
160*4882a593Smuzhiyun 
161*4882a593Smuzhiyun 	while (in_be16(&cp->cp_cpcr) & CPM_CR_FLG)	/* wait if cp is busy */
162*4882a593Smuzhiyun 		;
163*4882a593Smuzhiyun 
164*4882a593Smuzhiyun 	/* Enable transmitter/receiver.	*/
165*4882a593Smuzhiyun 	setbits_be16(&sp->smc_smcmr, SMCMR_REN | SMCMR_TEN);
166*4882a593Smuzhiyun 
167*4882a593Smuzhiyun 	return 0;
168*4882a593Smuzhiyun }
169*4882a593Smuzhiyun 
smc_putc(const char c)170*4882a593Smuzhiyun static void smc_putc(const char c)
171*4882a593Smuzhiyun {
172*4882a593Smuzhiyun 	immap_t	__iomem *im = (immap_t __iomem *)CONFIG_SYS_IMMR;
173*4882a593Smuzhiyun 	cpm8xx_t	__iomem *cpmp = &(im->im_cpm);
174*4882a593Smuzhiyun 	struct serialbuffer	__iomem *rtx;
175*4882a593Smuzhiyun 
176*4882a593Smuzhiyun 	if (c == '\n')
177*4882a593Smuzhiyun 		smc_putc('\r');
178*4882a593Smuzhiyun 
179*4882a593Smuzhiyun 	rtx = (struct serialbuffer __iomem *)&cpmp->cp_dpmem[CPM_SERIAL_BASE];
180*4882a593Smuzhiyun 
181*4882a593Smuzhiyun 	/* Wait for last character to go. */
182*4882a593Smuzhiyun 	out_8(&rtx->txbuf, c);
183*4882a593Smuzhiyun 	out_be16(&rtx->txbd.cbd_datlen, 1);
184*4882a593Smuzhiyun 	setbits_be16(&rtx->txbd.cbd_sc, BD_SC_READY);
185*4882a593Smuzhiyun 
186*4882a593Smuzhiyun 	while (in_be16(&rtx->txbd.cbd_sc) & BD_SC_READY)
187*4882a593Smuzhiyun 		WATCHDOG_RESET();
188*4882a593Smuzhiyun }
189*4882a593Smuzhiyun 
smc_puts(const char * s)190*4882a593Smuzhiyun static void smc_puts(const char *s)
191*4882a593Smuzhiyun {
192*4882a593Smuzhiyun 	while (*s)
193*4882a593Smuzhiyun 		smc_putc(*s++);
194*4882a593Smuzhiyun }
195*4882a593Smuzhiyun 
smc_getc(void)196*4882a593Smuzhiyun static int smc_getc(void)
197*4882a593Smuzhiyun {
198*4882a593Smuzhiyun 	immap_t	__iomem *im = (immap_t __iomem *)CONFIG_SYS_IMMR;
199*4882a593Smuzhiyun 	cpm8xx_t	__iomem *cpmp = &(im->im_cpm);
200*4882a593Smuzhiyun 	struct serialbuffer	__iomem *rtx;
201*4882a593Smuzhiyun 	unsigned char  c;
202*4882a593Smuzhiyun 	uint rxindex;
203*4882a593Smuzhiyun 
204*4882a593Smuzhiyun 	rtx = (struct serialbuffer __iomem *)&cpmp->cp_dpmem[CPM_SERIAL_BASE];
205*4882a593Smuzhiyun 
206*4882a593Smuzhiyun 	/* Wait for character to show up. */
207*4882a593Smuzhiyun 	while (in_be16(&rtx->rxbd.cbd_sc) & BD_SC_EMPTY)
208*4882a593Smuzhiyun 		WATCHDOG_RESET();
209*4882a593Smuzhiyun 
210*4882a593Smuzhiyun 	/* the characters are read one by one,
211*4882a593Smuzhiyun 	 * use the rxindex to know the next char to deliver
212*4882a593Smuzhiyun 	 */
213*4882a593Smuzhiyun 	rxindex = in_be32(&rtx->rxindex);
214*4882a593Smuzhiyun 	c = in_8(rtx->rxbuf + rxindex);
215*4882a593Smuzhiyun 	rxindex++;
216*4882a593Smuzhiyun 
217*4882a593Smuzhiyun 	/* check if all char are readout, then make prepare for next receive */
218*4882a593Smuzhiyun 	if (rxindex >= in_be16(&rtx->rxbd.cbd_datlen)) {
219*4882a593Smuzhiyun 		rxindex = 0;
220*4882a593Smuzhiyun 		setbits_be16(&rtx->rxbd.cbd_sc, BD_SC_EMPTY);
221*4882a593Smuzhiyun 	}
222*4882a593Smuzhiyun 	out_be32(&rtx->rxindex, rxindex);
223*4882a593Smuzhiyun 	return c;
224*4882a593Smuzhiyun }
225*4882a593Smuzhiyun 
smc_tstc(void)226*4882a593Smuzhiyun static int smc_tstc(void)
227*4882a593Smuzhiyun {
228*4882a593Smuzhiyun 	immap_t	__iomem *im = (immap_t __iomem *)CONFIG_SYS_IMMR;
229*4882a593Smuzhiyun 	cpm8xx_t	__iomem *cpmp = &(im->im_cpm);
230*4882a593Smuzhiyun 	struct serialbuffer	__iomem *rtx;
231*4882a593Smuzhiyun 
232*4882a593Smuzhiyun 	rtx = (struct serialbuffer __iomem *)&cpmp->cp_dpmem[CPM_SERIAL_BASE];
233*4882a593Smuzhiyun 
234*4882a593Smuzhiyun 	return !(in_be16(&rtx->rxbd.cbd_sc) & BD_SC_EMPTY);
235*4882a593Smuzhiyun }
236*4882a593Smuzhiyun 
237*4882a593Smuzhiyun struct serial_device serial_smc_device = {
238*4882a593Smuzhiyun 	.name	= "serial_smc",
239*4882a593Smuzhiyun 	.start	= smc_init,
240*4882a593Smuzhiyun 	.stop	= NULL,
241*4882a593Smuzhiyun 	.setbrg	= smc_setbrg,
242*4882a593Smuzhiyun 	.getc	= smc_getc,
243*4882a593Smuzhiyun 	.tstc	= smc_tstc,
244*4882a593Smuzhiyun 	.putc	= smc_putc,
245*4882a593Smuzhiyun 	.puts	= smc_puts,
246*4882a593Smuzhiyun };
247*4882a593Smuzhiyun 
default_serial_console(void)248*4882a593Smuzhiyun __weak struct serial_device *default_serial_console(void)
249*4882a593Smuzhiyun {
250*4882a593Smuzhiyun 	return &serial_smc_device;
251*4882a593Smuzhiyun }
252*4882a593Smuzhiyun 
mpc8xx_serial_initialize(void)253*4882a593Smuzhiyun void mpc8xx_serial_initialize(void)
254*4882a593Smuzhiyun {
255*4882a593Smuzhiyun 	serial_register(&serial_smc_device);
256*4882a593Smuzhiyun }
257