xref: /arm-trusted-firmware/drivers/marvell/uart/a3700_console.S (revision 91f16700b400a8c0651d24a598fc48ee2997a0d7)
1*91f16700Schasinglulu/*
2*91f16700Schasinglulu * Copyright (C) 2016 Marvell International Ltd.
3*91f16700Schasinglulu *
4*91f16700Schasinglulu * SPDX-License-Identifier:	BSD-3-Clause
5*91f16700Schasinglulu * https://spdx.org/licenses
6*91f16700Schasinglulu */
7*91f16700Schasinglulu
8*91f16700Schasinglulu#include <arch.h>
9*91f16700Schasinglulu#include <asm_macros.S>
10*91f16700Schasinglulu#include <console_macros.S>
11*91f16700Schasinglulu#include <drivers/marvell/uart/a3700_console.h>
12*91f16700Schasinglulu
13*91f16700Schasinglulu	/*
14*91f16700Schasinglulu	 * "core" functions are low-level implementations that don't require
15*91f16700Schasinglulu	 * writable memory and are thus safe to call in BL1 crash context.
16*91f16700Schasinglulu	 */
17*91f16700Schasinglulu	.globl console_a3700_core_putc
18*91f16700Schasinglulu	.globl console_a3700_core_init
19*91f16700Schasinglulu	.globl console_a3700_core_getc
20*91f16700Schasinglulu	.globl console_a3700_core_flush
21*91f16700Schasinglulu
22*91f16700Schasinglulu	.globl console_a3700_putc
23*91f16700Schasinglulu	.globl console_a3700_getc
24*91f16700Schasinglulu	.globl console_a3700_flush
25*91f16700Schasinglulu
26*91f16700Schasinglulu	/* -----------------------------------------------
27*91f16700Schasinglulu	 * int console_a3700_core_init(unsigned long base_addr,
28*91f16700Schasinglulu	 * unsigned int uart_clk, unsigned int baud_rate)
29*91f16700Schasinglulu	 * Function to initialize the console without a
30*91f16700Schasinglulu	 * C Runtime to print debug information. This
31*91f16700Schasinglulu	 * function will be accessed by console_init and
32*91f16700Schasinglulu	 * crash reporting.
33*91f16700Schasinglulu	 * In: x0 - console base address
34*91f16700Schasinglulu	 *     w1 - Uart clock in Hz
35*91f16700Schasinglulu	 *     w2 - Baud rate
36*91f16700Schasinglulu	 * Out: return 1 on success
37*91f16700Schasinglulu	 * Clobber list : x1, x2, x3, x4
38*91f16700Schasinglulu	 * -----------------------------------------------
39*91f16700Schasinglulu	 */
40*91f16700Schasinglulufunc console_a3700_core_init
41*91f16700Schasinglulu	/* Check the input base address */
42*91f16700Schasinglulu	cbz	x0, init_fail
43*91f16700Schasinglulu	/* Check baud rate and uart clock for sanity */
44*91f16700Schasinglulu	cbz	w1, init_fail
45*91f16700Schasinglulu	cbz	w2, init_fail
46*91f16700Schasinglulu
47*91f16700Schasinglulu	/*
48*91f16700Schasinglulu	 * Wait for the TX (THR and TSR) to be empty. If wait for 3ms, the TX FIFO is
49*91f16700Schasinglulu	 * still not empty, TX FIFO will reset by all means.
50*91f16700Schasinglulu	 */
51*91f16700Schasinglulu	mov	w4, #30				/* max time out 30 * 100 us */
52*91f16700Schasinglulu2:
53*91f16700Schasinglulu	/* Check whether TX (THR and TSR) is empty */
54*91f16700Schasinglulu	ldr	w3, [x0, #UART_STATUS_REG]
55*91f16700Schasinglulu	and	w3, w3, #UARTLSR_TXEMPTY
56*91f16700Schasinglulu	cmp	w3, #0
57*91f16700Schasinglulu	b.ne	4f
58*91f16700Schasinglulu
59*91f16700Schasinglulu	/* Delay */
60*91f16700Schasinglulu	mov	w3, #60000	/* 60000 cycles of below 3 instructions on 1200 MHz CPU ~~ 100 us */
61*91f16700Schasinglulu3:
62*91f16700Schasinglulu	sub	w3, w3, #1
63*91f16700Schasinglulu	cmp	w3, #0
64*91f16700Schasinglulu	b.ne	3b
65*91f16700Schasinglulu
66*91f16700Schasinglulu	/* Check whether wait timeout expired */
67*91f16700Schasinglulu	sub	w4, w4, #1
68*91f16700Schasinglulu	cmp	w4, #0
69*91f16700Schasinglulu	b.ne	2b
70*91f16700Schasinglulu
71*91f16700Schasinglulu4:
72*91f16700Schasinglulu	/* Reset UART via North Bridge Peripheral */
73*91f16700Schasinglulu	mov_imm	x4, MVEBU_NB_RESET_REG
74*91f16700Schasinglulu	ldr	w3, [x4]
75*91f16700Schasinglulu	bic	w3, w3, #MVEBU_NB_RESET_UART_N
76*91f16700Schasinglulu	str	w3, [x4]
77*91f16700Schasinglulu	orr	w3, w3, #MVEBU_NB_RESET_UART_N
78*91f16700Schasinglulu	str	w3, [x4]
79*91f16700Schasinglulu
80*91f16700Schasinglulu	/* Reset FIFO */
81*91f16700Schasinglulu	mov	w3, #UART_CTRL_RXFIFO_RESET
82*91f16700Schasinglulu	orr	w3, w3, #UART_CTRL_TXFIFO_RESET
83*91f16700Schasinglulu	str	w3, [x0, #UART_CTRL_REG]
84*91f16700Schasinglulu
85*91f16700Schasinglulu	/* Delay */
86*91f16700Schasinglulu	mov	w3, #2000
87*91f16700Schasinglulu1:
88*91f16700Schasinglulu	sub	w3, w3, #1
89*91f16700Schasinglulu	cmp	w3, #0
90*91f16700Schasinglulu	b.ne	1b
91*91f16700Schasinglulu
92*91f16700Schasinglulu	/* Program the baudrate */
93*91f16700Schasinglulu	/* Divisor = Round(Uartclock / (16 * baudrate)) */
94*91f16700Schasinglulu	lsl	w2, w2, #4
95*91f16700Schasinglulu	add	w1, w1, w2, lsr #1
96*91f16700Schasinglulu	udiv	w2, w1, w2
97*91f16700Schasinglulu	and	w2, w2, #0x3ff /* clear all other bits to use default clock */
98*91f16700Schasinglulu
99*91f16700Schasinglulu	str	w2, [x0, #UART_BAUD_REG]/* set baud rate divisor */
100*91f16700Schasinglulu
101*91f16700Schasinglulu	/* Set UART to default 16X scheme */
102*91f16700Schasinglulu	mov	w3, #0
103*91f16700Schasinglulu	str	w3, [x0, #UART_POSSR_REG]
104*91f16700Schasinglulu
105*91f16700Schasinglulu	/* No Parity, 1 Stop */
106*91f16700Schasinglulu	mov	w3, #0
107*91f16700Schasinglulu	str	w3, [x0, #UART_CTRL_REG]
108*91f16700Schasinglulu
109*91f16700Schasinglulu	mov	w0, #1
110*91f16700Schasinglulu	ret
111*91f16700Schasingluluinit_fail:
112*91f16700Schasinglulu	mov	w0, #0
113*91f16700Schasinglulu	ret
114*91f16700Schasingluluendfunc console_a3700_core_init
115*91f16700Schasinglulu
116*91f16700Schasinglulu	.globl console_a3700_register
117*91f16700Schasinglulu
118*91f16700Schasinglulu	/* -----------------------------------------------
119*91f16700Schasinglulu	 * int console_a3700_register(console_t *console,
120*91f16700Schasinglulu		uintptr_t base, uint32_t clk, uint32_t baud)
121*91f16700Schasinglulu	 * Function to initialize and register a new a3700
122*91f16700Schasinglulu	 * console. Storage passed in for the console struct
123*91f16700Schasinglulu	 * *must* be persistent (i.e. not from the stack).
124*91f16700Schasinglulu	 * In: x0 - UART register base address
125*91f16700Schasinglulu	 *     w1 - UART clock in Hz
126*91f16700Schasinglulu	 *     w2 - Baud rate
127*91f16700Schasinglulu	 *     x3 - pointer to empty console_t struct
128*91f16700Schasinglulu	 * Out: return 1 on success, 0 on error
129*91f16700Schasinglulu	 * Clobber list : x0, x1, x2, x3, x4, x6, x7, x14
130*91f16700Schasinglulu	 * -----------------------------------------------
131*91f16700Schasinglulu	 */
132*91f16700Schasinglulufunc console_a3700_register
133*91f16700Schasinglulu	mov	x7, x30
134*91f16700Schasinglulu	mov	x6, x3
135*91f16700Schasinglulu	cbz	x6, register_fail
136*91f16700Schasinglulu	str	x0, [x6, #CONSOLE_T_BASE]
137*91f16700Schasinglulu
138*91f16700Schasinglulu	bl	console_a3700_core_init
139*91f16700Schasinglulu	cbz	x0, register_fail
140*91f16700Schasinglulu
141*91f16700Schasinglulu	mov	x0, x6
142*91f16700Schasinglulu	mov	x30, x7
143*91f16700Schasinglulu	finish_console_register a3700, putc=1, getc=ENABLE_CONSOLE_GETC, flush=1
144*91f16700Schasinglulu
145*91f16700Schasingluluregister_fail:
146*91f16700Schasinglulu	ret	x7
147*91f16700Schasingluluendfunc console_a3700_register
148*91f16700Schasinglulu
149*91f16700Schasinglulu	/* --------------------------------------------------------
150*91f16700Schasinglulu	 * int console_a3700_core_putc(int c, unsigned int base_addr)
151*91f16700Schasinglulu	 * Function to output a character over the console. It
152*91f16700Schasinglulu	 * returns the character printed on success or -1 on error.
153*91f16700Schasinglulu	 * In : w0 - character to be printed
154*91f16700Schasinglulu	 *      x1 - console base address
155*91f16700Schasinglulu	 * Out : return -1 on error else return character.
156*91f16700Schasinglulu	 * Clobber list : x2
157*91f16700Schasinglulu	 * --------------------------------------------------------
158*91f16700Schasinglulu	 */
159*91f16700Schasinglulufunc console_a3700_core_putc
160*91f16700Schasinglulu	/* Check the input parameter */
161*91f16700Schasinglulu	cbz	x1, putc_error
162*91f16700Schasinglulu
163*91f16700Schasinglulu	/* Prepend '\r' to '\n' */
164*91f16700Schasinglulu	cmp	w0, #0xA
165*91f16700Schasinglulu	b.ne	2f
166*91f16700Schasinglulu	/* Check if the transmit FIFO is full */
167*91f16700Schasinglulu1:	ldr	w2, [x1, #UART_STATUS_REG]
168*91f16700Schasinglulu	and	w2, w2, #UARTLSR_TXFIFOFULL
169*91f16700Schasinglulu	cmp	w2, #UARTLSR_TXFIFOFULL
170*91f16700Schasinglulu	b.eq	1b
171*91f16700Schasinglulu	mov	w2, #0xD		/* '\r' */
172*91f16700Schasinglulu	str	w2, [x1, #UART_TX_REG]
173*91f16700Schasinglulu
174*91f16700Schasinglulu	/* Check if the transmit FIFO is full */
175*91f16700Schasinglulu2:	ldr	w2, [x1, #UART_STATUS_REG]
176*91f16700Schasinglulu	and	w2, w2, #UARTLSR_TXFIFOFULL
177*91f16700Schasinglulu	cmp	w2, #UARTLSR_TXFIFOFULL
178*91f16700Schasinglulu	b.eq	2b
179*91f16700Schasinglulu	str	w0, [x1, #UART_TX_REG]
180*91f16700Schasinglulu	ret
181*91f16700Schasingluluputc_error:
182*91f16700Schasinglulu	mov	w0, #-1
183*91f16700Schasinglulu	ret
184*91f16700Schasingluluendfunc console_a3700_core_putc
185*91f16700Schasinglulu
186*91f16700Schasinglulu	/* --------------------------------------------------------
187*91f16700Schasinglulu	 * int console_a3700_putc(int c, console_t *console)
188*91f16700Schasinglulu	 * Function to output a character over the console. It
189*91f16700Schasinglulu	 * returns the character printed on success or -1 on error.
190*91f16700Schasinglulu	 * In : w0 - character to be printed
191*91f16700Schasinglulu	 *      x1 - pointer to console_t structure
192*91f16700Schasinglulu	 * Out : return -1 on error else return character.
193*91f16700Schasinglulu	 * Clobber list : x2
194*91f16700Schasinglulu	 * --------------------------------------------------------
195*91f16700Schasinglulu	 */
196*91f16700Schasinglulufunc console_a3700_putc
197*91f16700Schasinglulu	ldr	x1, [x1, #CONSOLE_T_BASE]
198*91f16700Schasinglulu	b	console_a3700_core_putc
199*91f16700Schasingluluendfunc console_a3700_putc
200*91f16700Schasinglulu
201*91f16700Schasinglulu	/* ---------------------------------------------
202*91f16700Schasinglulu	 * int console_a3700_core_getc(void)
203*91f16700Schasinglulu	 * Function to get a character from the console.
204*91f16700Schasinglulu	 * It returns the character grabbed on success
205*91f16700Schasinglulu	 * or -1 if no character is available.
206*91f16700Schasinglulu	 * In : w0 - console base address
207*91f16700Schasinglulu	 * Out : w0 - character if available, else -1
208*91f16700Schasinglulu	 * Clobber list : x0, x1
209*91f16700Schasinglulu	 * ---------------------------------------------
210*91f16700Schasinglulu	 */
211*91f16700Schasinglulufunc console_a3700_core_getc
212*91f16700Schasinglulu	/* Check if there is a pending character */
213*91f16700Schasinglulu	ldr	w1, [x0, #UART_STATUS_REG]
214*91f16700Schasinglulu	and	w1, w1, #UARTLSR_RXRDY
215*91f16700Schasinglulu	cmp	w1, #UARTLSR_RXRDY
216*91f16700Schasinglulu	b.ne	getc_no_char
217*91f16700Schasinglulu	ldr	w0, [x0, #UART_RX_REG]
218*91f16700Schasinglulu	and	w0, w0, #0xff
219*91f16700Schasinglulu	ret
220*91f16700Schasinglulugetc_no_char:
221*91f16700Schasinglulu	mov	w0, #ERROR_NO_PENDING_CHAR
222*91f16700Schasinglulu	ret
223*91f16700Schasingluluendfunc console_a3700_core_getc
224*91f16700Schasinglulu
225*91f16700Schasinglulu	/* ---------------------------------------------
226*91f16700Schasinglulu	 * int console_a3700_getc(console_t *console)
227*91f16700Schasinglulu	 * Function to get a character from the console.
228*91f16700Schasinglulu	 * It returns the character grabbed on success
229*91f16700Schasinglulu	 * or -1 on if no character is available.
230*91f16700Schasinglulu	 * In :  x0 - pointer to console_t structure
231*91f16700Schasinglulu	 * Out : w0 - character if available, else -1
232*91f16700Schasinglulu	 * Clobber list : x0, x1
233*91f16700Schasinglulu	 * ---------------------------------------------
234*91f16700Schasinglulu	 */
235*91f16700Schasinglulufunc console_a3700_getc
236*91f16700Schasinglulu	ldr	x0, [x0, #CONSOLE_T_BASE]
237*91f16700Schasinglulu	b	console_a3700_core_getc
238*91f16700Schasingluluendfunc console_a3700_getc
239*91f16700Schasinglulu
240*91f16700Schasinglulu	/* ---------------------------------------------
241*91f16700Schasinglulu	 * void console_a3700_core_flush(uintptr_t base_addr)
242*91f16700Schasinglulu	 * Function to force a write of all buffered
243*91f16700Schasinglulu	 * data that hasn't been output.
244*91f16700Schasinglulu	 * In : x0 - console base address
245*91f16700Schasinglulu	 * Out : void.
246*91f16700Schasinglulu	 * Clobber list : x0, x1
247*91f16700Schasinglulu	 * ---------------------------------------------
248*91f16700Schasinglulu	 */
249*91f16700Schasinglulufunc console_a3700_core_flush
250*91f16700Schasinglulu	/* Wait for the TX (THR and TSR) to be empty */
251*91f16700Schasinglulu1:	ldr	w1, [x0, #UART_STATUS_REG]
252*91f16700Schasinglulu	and	w1, w1, #UARTLSR_TXEMPTY
253*91f16700Schasinglulu	cmp	w1, #UARTLSR_TXEMPTY
254*91f16700Schasinglulu	b.ne	1b
255*91f16700Schasinglulu	ret
256*91f16700Schasingluluendfunc console_a3700_core_flush
257*91f16700Schasinglulu
258*91f16700Schasinglulu	/* ---------------------------------------------
259*91f16700Schasinglulu	 * void console_a3700_flush(console_t *console)
260*91f16700Schasinglulu	 * Function to force a write of all buffered
261*91f16700Schasinglulu	 * data that hasn't been output.
262*91f16700Schasinglulu	 * In : x0 - pointer to console_t structure
263*91f16700Schasinglulu	 * Out : void.
264*91f16700Schasinglulu	 * Clobber list : x0, x1
265*91f16700Schasinglulu	 * ---------------------------------------------
266*91f16700Schasinglulu	 */
267*91f16700Schasinglulufunc console_a3700_flush
268*91f16700Schasinglulu	ldr	x0, [x0, #CONSOLE_T_BASE]
269*91f16700Schasinglulu	b	console_a3700_core_flush
270*91f16700Schasingluluendfunc console_a3700_flush
271*91f16700Schasinglulu
272