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