xref: /rk3399_rockchip-uboot/arch/powerpc/cpu/mpc85xx/serial_scc.c (revision 326ea986ac150acdc7656d57fca647db80b50158)
1a47a12beSStefan Roese /*
2a47a12beSStefan Roese  * (C) Copyright 2003 Motorola Inc.
3a47a12beSStefan Roese  * Xianghua Xiao (X.Xiao@motorola.com)
4a47a12beSStefan Roese  * Modified based on 8260 for 8560.
5a47a12beSStefan Roese  *
6a47a12beSStefan Roese  * (C) Copyright 2000
7a47a12beSStefan Roese  * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
8a47a12beSStefan Roese  *
9*1a459660SWolfgang Denk  * SPDX-License-Identifier:	GPL-2.0+
10a47a12beSStefan Roese  *
11a47a12beSStefan Roese  * Hacked for MPC8260 by Murray.Jensen@cmst.csiro.au, 19-Oct-00.
12a47a12beSStefan Roese  */
13a47a12beSStefan Roese 
14a47a12beSStefan Roese /*
15a47a12beSStefan Roese  * Minimal serial functions needed to use one of the SCC ports
16a47a12beSStefan Roese  * as serial console interface.
17a47a12beSStefan Roese  */
18a47a12beSStefan Roese 
19a47a12beSStefan Roese #include <common.h>
20a47a12beSStefan Roese #include <asm/cpm_85xx.h>
217a311546SMarek Vasut #include <serial.h>
227a311546SMarek Vasut #include <linux/compiler.h>
23a47a12beSStefan Roese 
24a47a12beSStefan Roese DECLARE_GLOBAL_DATA_PTR;
25a47a12beSStefan Roese 
26a47a12beSStefan Roese #if defined(CONFIG_CONS_ON_SCC)
27a47a12beSStefan Roese 
28a47a12beSStefan Roese #if CONFIG_CONS_INDEX == 1	/* Console on SCC1 */
29a47a12beSStefan Roese 
30a47a12beSStefan Roese #define SCC_INDEX		0
31a47a12beSStefan Roese #define PROFF_SCC		PROFF_SCC1
32a47a12beSStefan Roese #define CMXSCR_MASK		(CMXSCR_GR1|CMXSCR_SC1|\
33a47a12beSStefan Roese 					CMXSCR_RS1CS_MSK|CMXSCR_TS1CS_MSK)
34a47a12beSStefan Roese #define CMXSCR_VALUE		(CMXSCR_RS1CS_BRG1|CMXSCR_TS1CS_BRG1)
35a47a12beSStefan Roese #define CPM_CR_SCC_PAGE		CPM_CR_SCC1_PAGE
36a47a12beSStefan Roese #define CPM_CR_SCC_SBLOCK	CPM_CR_SCC1_SBLOCK
37a47a12beSStefan Roese 
38a47a12beSStefan Roese #elif CONFIG_CONS_INDEX == 2	/* Console on SCC2 */
39a47a12beSStefan Roese 
40a47a12beSStefan Roese #define SCC_INDEX		1
41a47a12beSStefan Roese #define PROFF_SCC		PROFF_SCC2
42a47a12beSStefan Roese #define CMXSCR_MASK		(CMXSCR_GR2|CMXSCR_SC2|\
43a47a12beSStefan Roese 					CMXSCR_RS2CS_MSK|CMXSCR_TS2CS_MSK)
44a47a12beSStefan Roese #define CMXSCR_VALUE		(CMXSCR_RS2CS_BRG2|CMXSCR_TS2CS_BRG2)
45a47a12beSStefan Roese #define CPM_CR_SCC_PAGE		CPM_CR_SCC2_PAGE
46a47a12beSStefan Roese #define CPM_CR_SCC_SBLOCK	CPM_CR_SCC2_SBLOCK
47a47a12beSStefan Roese 
48a47a12beSStefan Roese #elif CONFIG_CONS_INDEX == 3	/* Console on SCC3 */
49a47a12beSStefan Roese 
50a47a12beSStefan Roese #define SCC_INDEX		2
51a47a12beSStefan Roese #define PROFF_SCC		PROFF_SCC3
52a47a12beSStefan Roese #define CMXSCR_MASK		(CMXSCR_GR3|CMXSCR_SC3|\
53a47a12beSStefan Roese 					CMXSCR_RS3CS_MSK|CMXSCR_TS3CS_MSK)
54a47a12beSStefan Roese #define CMXSCR_VALUE		(CMXSCR_RS3CS_BRG3|CMXSCR_TS3CS_BRG3)
55a47a12beSStefan Roese #define CPM_CR_SCC_PAGE		CPM_CR_SCC3_PAGE
56a47a12beSStefan Roese #define CPM_CR_SCC_SBLOCK	CPM_CR_SCC3_SBLOCK
57a47a12beSStefan Roese 
58a47a12beSStefan Roese #elif CONFIG_CONS_INDEX == 4	/* Console on SCC4 */
59a47a12beSStefan Roese 
60a47a12beSStefan Roese #define SCC_INDEX		3
61a47a12beSStefan Roese #define PROFF_SCC		PROFF_SCC4
62a47a12beSStefan Roese #define CMXSCR_MASK		(CMXSCR_GR4|CMXSCR_SC4|\
63a47a12beSStefan Roese 					CMXSCR_RS4CS_MSK|CMXSCR_TS4CS_MSK)
64a47a12beSStefan Roese #define CMXSCR_VALUE		(CMXSCR_RS4CS_BRG4|CMXSCR_TS4CS_BRG4)
65a47a12beSStefan Roese #define CPM_CR_SCC_PAGE		CPM_CR_SCC4_PAGE
66a47a12beSStefan Roese #define CPM_CR_SCC_SBLOCK	CPM_CR_SCC4_SBLOCK
67a47a12beSStefan Roese 
68a47a12beSStefan Roese #else
69a47a12beSStefan Roese 
70a47a12beSStefan Roese #error "console not correctly defined"
71a47a12beSStefan Roese 
72a47a12beSStefan Roese #endif
73a47a12beSStefan Roese 
mpc85xx_serial_init(void)747a311546SMarek Vasut static int mpc85xx_serial_init(void)
75a47a12beSStefan Roese {
76a47a12beSStefan Roese 	volatile ccsr_cpm_t *cpm = (ccsr_cpm_t *)CONFIG_SYS_MPC85xx_CPM_ADDR;
77a47a12beSStefan Roese 	volatile ccsr_cpm_scc_t *sp;
78a47a12beSStefan Roese 	volatile scc_uart_t *up;
79a47a12beSStefan Roese 	volatile cbd_t *tbdf, *rbdf;
80a47a12beSStefan Roese 	volatile ccsr_cpm_cp_t *cp = &(cpm->im_cpm_cp);
81a47a12beSStefan Roese 	uint	dpaddr;
82a47a12beSStefan Roese 
83a47a12beSStefan Roese 	/* initialize pointers to SCC */
84a47a12beSStefan Roese 
85a47a12beSStefan Roese 	sp = (ccsr_cpm_scc_t *) &(cpm->im_cpm_scc[SCC_INDEX]);
86a47a12beSStefan Roese 	up = (scc_uart_t *)&(cpm->im_dprambase[PROFF_SCC]);
87a47a12beSStefan Roese 
88a47a12beSStefan Roese 	/* Disable transmitter/receiver.
89a47a12beSStefan Roese 	*/
90a47a12beSStefan Roese 	sp->gsmrl &= ~(SCC_GSMRL_ENR | SCC_GSMRL_ENT);
91a47a12beSStefan Roese 
92a47a12beSStefan Roese 	/* put the SCC channel into NMSI (non multiplexd serial interface)
93a47a12beSStefan Roese 	 * mode and wire the selected SCC Tx and Rx clocks to BRGx (15-15).
94a47a12beSStefan Roese 	 */
95a47a12beSStefan Roese 	cpm->im_cpm_mux.cmxscr = \
96a47a12beSStefan Roese 		(cpm->im_cpm_mux.cmxscr&~CMXSCR_MASK)|CMXSCR_VALUE;
97a47a12beSStefan Roese 
98a47a12beSStefan Roese 	/* Set up the baud rate generator.
99a47a12beSStefan Roese 	*/
100a47a12beSStefan Roese 	serial_setbrg ();
101a47a12beSStefan Roese 
102a47a12beSStefan Roese 	/* Allocate space for two buffer descriptors in the DP ram.
103a47a12beSStefan Roese 	 * damm: allocating space after the two buffers for rx/tx data
104a47a12beSStefan Roese 	 */
105a47a12beSStefan Roese 
106a47a12beSStefan Roese 	dpaddr = m8560_cpm_dpalloc((2 * sizeof (cbd_t)) + 2, 16);
107a47a12beSStefan Roese 
108a47a12beSStefan Roese 	/* Set the physical address of the host memory buffers in
109a47a12beSStefan Roese 	 * the buffer descriptors.
110a47a12beSStefan Roese 	 */
111a47a12beSStefan Roese 	rbdf = (cbd_t *)&(cpm->im_dprambase[dpaddr]);
112a47a12beSStefan Roese 	rbdf->cbd_bufaddr = (uint) (rbdf+2);
113a47a12beSStefan Roese 	rbdf->cbd_sc = BD_SC_EMPTY | BD_SC_WRAP;
114a47a12beSStefan Roese 	tbdf = rbdf + 1;
115a47a12beSStefan Roese 	tbdf->cbd_bufaddr = ((uint) (rbdf+2)) + 1;
116a47a12beSStefan Roese 	tbdf->cbd_sc = BD_SC_WRAP;
117a47a12beSStefan Roese 
118a47a12beSStefan Roese 	/* Set up the uart parameters in the parameter ram.
119a47a12beSStefan Roese 	*/
120a47a12beSStefan Roese 	up->scc_genscc.scc_rbase = dpaddr;
121a47a12beSStefan Roese 	up->scc_genscc.scc_tbase = dpaddr+sizeof(cbd_t);
122a47a12beSStefan Roese 	up->scc_genscc.scc_rfcr = CPMFCR_EB;
123a47a12beSStefan Roese 	up->scc_genscc.scc_tfcr = CPMFCR_EB;
124a47a12beSStefan Roese 	up->scc_genscc.scc_mrblr = 1;
125a47a12beSStefan Roese 	up->scc_maxidl = 0;
126a47a12beSStefan Roese 	up->scc_brkcr = 1;
127a47a12beSStefan Roese 	up->scc_parec = 0;
128a47a12beSStefan Roese 	up->scc_frmec = 0;
129a47a12beSStefan Roese 	up->scc_nosec = 0;
130a47a12beSStefan Roese 	up->scc_brkec = 0;
131a47a12beSStefan Roese 	up->scc_uaddr1 = 0;
132a47a12beSStefan Roese 	up->scc_uaddr2 = 0;
133a47a12beSStefan Roese 	up->scc_toseq = 0;
134a47a12beSStefan Roese 	up->scc_char1 = up->scc_char2 = up->scc_char3 = up->scc_char4 = 0x8000;
135a47a12beSStefan Roese 	up->scc_char5 = up->scc_char6 = up->scc_char7 = up->scc_char8 = 0x8000;
136a47a12beSStefan Roese 	up->scc_rccm = 0xc0ff;
137a47a12beSStefan Roese 
138a47a12beSStefan Roese 	/* Mask all interrupts and remove anything pending.
139a47a12beSStefan Roese 	*/
140a47a12beSStefan Roese 	sp->sccm = 0;
141a47a12beSStefan Roese 	sp->scce = 0xffff;
142a47a12beSStefan Roese 
143a47a12beSStefan Roese 	/* Set 8 bit FIFO, 16 bit oversampling and UART mode.
144a47a12beSStefan Roese 	*/
145a47a12beSStefan Roese 	sp->gsmrh = SCC_GSMRH_RFW;	/* 8 bit FIFO */
146a47a12beSStefan Roese 	sp->gsmrl = \
147a47a12beSStefan Roese 		SCC_GSMRL_TDCR_16 | SCC_GSMRL_RDCR_16 | SCC_GSMRL_MODE_UART;
148a47a12beSStefan Roese 
149a47a12beSStefan Roese 	/* Set CTS no flow control, 1 stop bit, 8 bit character length,
150a47a12beSStefan Roese 	 * normal async UART mode, no parity
151a47a12beSStefan Roese 	 */
152a47a12beSStefan Roese 	sp->psmr = SCU_PSMR_CL;
153a47a12beSStefan Roese 
154a47a12beSStefan Roese 	/* execute the "Init Rx and Tx params" CP command.
155a47a12beSStefan Roese 	*/
156a47a12beSStefan Roese 
157a47a12beSStefan Roese 	while (cp->cpcr & CPM_CR_FLG)  /* wait if cp is busy */
158a47a12beSStefan Roese 	  ;
159a47a12beSStefan Roese 
160a47a12beSStefan Roese 	cp->cpcr = mk_cr_cmd(CPM_CR_SCC_PAGE, CPM_CR_SCC_SBLOCK,
161a47a12beSStefan Roese 					0, CPM_CR_INIT_TRX) | CPM_CR_FLG;
162a47a12beSStefan Roese 
163a47a12beSStefan Roese 	while (cp->cpcr & CPM_CR_FLG)  /* wait if cp is busy */
164a47a12beSStefan Roese 	  ;
165a47a12beSStefan Roese 
166a47a12beSStefan Roese 	/* Enable transmitter/receiver.
167a47a12beSStefan Roese 	*/
168a47a12beSStefan Roese 	sp->gsmrl |= SCC_GSMRL_ENR | SCC_GSMRL_ENT;
169a47a12beSStefan Roese 
170a47a12beSStefan Roese 	return (0);
171a47a12beSStefan Roese }
172a47a12beSStefan Roese 
mpc85xx_serial_setbrg(void)1737a311546SMarek Vasut static void mpc85xx_serial_setbrg(void)
174a47a12beSStefan Roese {
175a47a12beSStefan Roese #if defined(CONFIG_CONS_USE_EXTC)
176a47a12beSStefan Roese 	m8560_cpm_extcbrg(SCC_INDEX, gd->baudrate,
177a47a12beSStefan Roese 		CONFIG_CONS_EXTC_RATE, CONFIG_CONS_EXTC_PINSEL);
178a47a12beSStefan Roese #else
179a47a12beSStefan Roese 	m8560_cpm_setbrg(SCC_INDEX, gd->baudrate);
180a47a12beSStefan Roese #endif
181a47a12beSStefan Roese }
182a47a12beSStefan Roese 
mpc85xx_serial_putc(const char c)1837a311546SMarek Vasut static void mpc85xx_serial_putc(const char c)
184a47a12beSStefan Roese {
185a47a12beSStefan Roese 	volatile scc_uart_t	*up;
186a47a12beSStefan Roese 	volatile cbd_t		*tbdf;
187a47a12beSStefan Roese 	volatile ccsr_cpm_t	*cpm = (ccsr_cpm_t *)CONFIG_SYS_MPC85xx_CPM_ADDR;
188a47a12beSStefan Roese 
189a47a12beSStefan Roese 	if (c == '\n')
190a47a12beSStefan Roese 		serial_putc ('\r');
191a47a12beSStefan Roese 
192a47a12beSStefan Roese 	up = (scc_uart_t *)&(cpm->im_dprambase[PROFF_SCC]);
193a47a12beSStefan Roese 	tbdf = (cbd_t *)&(cpm->im_dprambase[up->scc_genscc.scc_tbase]);
194a47a12beSStefan Roese 
195a47a12beSStefan Roese 	/* Wait for last character to go.
196a47a12beSStefan Roese 	 */
197a47a12beSStefan Roese 	while (tbdf->cbd_sc & BD_SC_READY)
198a47a12beSStefan Roese 		;
199a47a12beSStefan Roese 
200a47a12beSStefan Roese 	/* Load the character into the transmit buffer.
201a47a12beSStefan Roese 	 */
202a47a12beSStefan Roese 	*(volatile char *)tbdf->cbd_bufaddr = c;
203a47a12beSStefan Roese 	tbdf->cbd_datlen = 1;
204a47a12beSStefan Roese 	tbdf->cbd_sc |= BD_SC_READY;
205a47a12beSStefan Roese }
206a47a12beSStefan Roese 
mpc85xx_serial_getc(void)2077a311546SMarek Vasut static int mpc85xx_serial_getc(void)
208a47a12beSStefan Roese {
209a47a12beSStefan Roese 	volatile cbd_t		*rbdf;
210a47a12beSStefan Roese 	volatile scc_uart_t	*up;
211a47a12beSStefan Roese 	volatile ccsr_cpm_t	*cpm = (ccsr_cpm_t *)CONFIG_SYS_MPC85xx_CPM_ADDR;
212a47a12beSStefan Roese 	unsigned char		c;
213a47a12beSStefan Roese 
214a47a12beSStefan Roese 	up = (scc_uart_t *)&(cpm->im_dprambase[PROFF_SCC]);
215a47a12beSStefan Roese 	rbdf = (cbd_t *)&(cpm->im_dprambase[up->scc_genscc.scc_rbase]);
216a47a12beSStefan Roese 
217a47a12beSStefan Roese 	/* Wait for character to show up.
218a47a12beSStefan Roese 	 */
219a47a12beSStefan Roese 	while (rbdf->cbd_sc & BD_SC_EMPTY)
220a47a12beSStefan Roese 		;
221a47a12beSStefan Roese 
222a47a12beSStefan Roese 	/* Grab the char and clear the buffer again.
223a47a12beSStefan Roese 	 */
224a47a12beSStefan Roese 	c = *(volatile unsigned char *)rbdf->cbd_bufaddr;
225a47a12beSStefan Roese 	rbdf->cbd_sc |= BD_SC_EMPTY;
226a47a12beSStefan Roese 
227a47a12beSStefan Roese 	return (c);
228a47a12beSStefan Roese }
229a47a12beSStefan Roese 
mpc85xx_serial_tstc(void)2307a311546SMarek Vasut static int mpc85xx_serial_tstc(void)
231a47a12beSStefan Roese {
232a47a12beSStefan Roese 	volatile cbd_t		*rbdf;
233a47a12beSStefan Roese 	volatile scc_uart_t	*up;
234a47a12beSStefan Roese 	volatile ccsr_cpm_t	*cpm = (ccsr_cpm_t *)CONFIG_SYS_MPC85xx_CPM_ADDR;
235a47a12beSStefan Roese 
236a47a12beSStefan Roese 	up = (scc_uart_t *)&(cpm->im_dprambase[PROFF_SCC]);
237a47a12beSStefan Roese 	rbdf = (cbd_t *)&(cpm->im_dprambase[up->scc_genscc.scc_rbase]);
238a47a12beSStefan Roese 
239a47a12beSStefan Roese 	return ((rbdf->cbd_sc & BD_SC_EMPTY) == 0);
240a47a12beSStefan Roese }
241a47a12beSStefan Roese 
2427a311546SMarek Vasut static struct serial_device mpc85xx_serial_drv = {
2437a311546SMarek Vasut 	.name	= "mpc85xx_serial",
2447a311546SMarek Vasut 	.start	= mpc85xx_serial_init,
2457a311546SMarek Vasut 	.stop	= NULL,
2467a311546SMarek Vasut 	.setbrg	= mpc85xx_serial_setbrg,
2477a311546SMarek Vasut 	.putc	= mpc85xx_serial_putc,
248ec3fd689SMarek Vasut 	.puts	= default_serial_puts,
2497a311546SMarek Vasut 	.getc	= mpc85xx_serial_getc,
2507a311546SMarek Vasut 	.tstc	= mpc85xx_serial_tstc,
2517a311546SMarek Vasut };
2527a311546SMarek Vasut 
mpc85xx_serial_initialize(void)2537a311546SMarek Vasut void mpc85xx_serial_initialize(void)
2547a311546SMarek Vasut {
2557a311546SMarek Vasut 	serial_register(&mpc85xx_serial_drv);
2567a311546SMarek Vasut }
2577a311546SMarek Vasut 
default_serial_console(void)2587a311546SMarek Vasut __weak struct serial_device *default_serial_console(void)
2597a311546SMarek Vasut {
2607a311546SMarek Vasut 	return &mpc85xx_serial_drv;
2617a311546SMarek Vasut }
262a47a12beSStefan Roese #endif	/* CONFIG_CONS_ON_SCC */
263