xref: /rk3399_ARM-atf/drivers/ti/uart/aarch64/16550_console.S (revision c948f77136c42a92d0bb660543a3600c36dcf7f1)
1/*
2 * Copyright (c) 2015-2018, ARM Limited and Contributors. All rights reserved.
3 *
4 * SPDX-License-Identifier: BSD-3-Clause
5 */
6
7#include <arch.h>
8#include <asm_macros.S>
9#include <assert_macros.S>
10#define USE_FINISH_CONSOLE_REG_2
11#include <console_macros.S>
12#include <drivers/ti/uart/uart_16550.h>
13
14	/*
15	 * "core" functions are low-level implementations that don't require
16	 * writable memory and are thus safe to call in BL1 crash context.
17	 */
18	.globl console_16550_core_init
19	.globl console_16550_core_putc
20	.globl console_16550_core_getc
21	.globl console_16550_core_flush
22
23	.globl console_16550_putc
24	.globl console_16550_getc
25	.globl console_16550_flush
26
27	/* -----------------------------------------------
28	 * int console_16550_core_init(uintptr_t base_addr,
29	 * unsigned int uart_clk, unsigned int baud_rate)
30	 * Function to initialize the console without a
31	 * C Runtime to print debug information. This
32	 * function will be accessed by console_init and
33	 * crash reporting.
34	 * In: x0 - console base address
35	 *     w1 - Uart clock in Hz
36	 *     w2 - Baud rate
37	 * Out: return 1 on success, 0 on error
38	 * Clobber list : x1, x2, x3
39	 * -----------------------------------------------
40	 */
41func console_16550_core_init
42	/* Check the input base address */
43	cbz	x0, init_fail
44	/* Check baud rate and uart clock for sanity */
45	cbz	w1, init_fail
46	cbz	w2, init_fail
47
48	/* Program the baudrate */
49	/* Divisor =  Uart clock / (16 * baudrate) */
50	lsl	w2, w2, #4
51	udiv	w2, w1, w2
52	and	w1, w2, #0xff		/* w1 = DLL */
53	lsr	w2, w2, #8
54	and	w2, w2, #0xff		/* w2 = DLLM */
55	ldr	w3, [x0, #UARTLCR]
56	orr	w3, w3, #UARTLCR_DLAB
57	str	w3, [x0, #UARTLCR]	/* enable DLL, DLLM programming */
58	str	w1, [x0, #UARTDLL]	/* program DLL */
59	str	w2, [x0, #UARTDLLM]	/* program DLLM */
60	mov	w2, #~UARTLCR_DLAB
61	and	w3, w3, w2
62	str	w3, [x0, #UARTLCR]	/* disable DLL, DLLM programming */
63
64	/* 8n1 */
65	mov	w3, #3
66	str	w3, [x0, #UARTLCR]
67	/* no interrupt */
68	mov	w3, #0
69	str	w3, [x0, #UARTIER]
70#ifdef TI_16550_MDR_QUIRK
71	/* UART must be enabled on some platforms via the MDR register */
72	str	w3, [x0, #UARTMDR1]
73#endif /* TI_16550_MDR_QUIRK */
74	/* enable fifo, DMA */
75	mov	w3, #(UARTFCR_FIFOEN | UARTFCR_DMAEN)
76	str	w3, [x0, #UARTFCR]
77	/* DTR + RTS */
78	mov	w3, #3
79	str	w3, [x0, #UARTMCR]
80	mov	w0, #1
81	ret
82init_fail:
83	mov	w0, #0
84	ret
85endfunc console_16550_core_init
86
87#if MULTI_CONSOLE_API
88	.globl console_16550_register
89
90	/* -----------------------------------------------
91	 * int console_16550_register(uintptr_t baseaddr,
92	 *     uint32_t clock, uint32_t baud,
93	 *     console_16550_t *console);
94	 * Function to initialize and register a new 16550
95	 * console. Storage passed in for the console struct
96	 * *must* be persistent (i.e. not from the stack).
97	 * In: x0 - UART register base address
98	 *     w1 - UART clock in Hz
99	 *     w2 - Baud rate
100	 *     x3 - pointer to empty console_16550_t struct
101	 * Out: return 1 on success, 0 on error
102	 * Clobber list : x0, x1, x2, x6, x7, x14
103	 * -----------------------------------------------
104	 */
105func console_16550_register
106	mov	x7, x30
107	mov	x6, x3
108	cbz	x6, register_fail
109	str	x0, [x6, #CONSOLE_T_16550_BASE]
110
111	bl	console_16550_core_init
112	cbz	x0, register_fail
113
114	mov	x0, x6
115	mov	x30, x7
116	finish_console_register 16550 putc=1, getc=1, flush=1
117
118register_fail:
119	ret	x7
120endfunc console_16550_register
121#else
122	.globl console_core_init
123	.globl console_core_putc
124	.globl console_core_getc
125	.globl console_core_flush
126	.equ console_core_init,console_16550_core_init
127	.equ console_core_putc,console_16550_core_putc
128	.equ console_core_getc,console_16550_core_getc
129	.equ console_core_flush,console_16550_core_flush
130#endif
131
132	/* --------------------------------------------------------
133	 * int console_16550_core_putc(int c, uintptr_t base_addr)
134	 * Function to output a character over the console. It
135	 * returns the character printed on success or -1 on error.
136	 * In : w0 - character to be printed
137	 *      x1 - console base address
138	 * Out : return -1 on error else return character.
139	 * Clobber list : x2
140	 * --------------------------------------------------------
141	 */
142func console_16550_core_putc
143#if ENABLE_ASSERTIONS
144	cmp	x1, #0
145	ASM_ASSERT(ne)
146#endif /* ENABLE_ASSERTIONS */
147
148	/* Prepend '\r' to '\n' */
149	cmp	w0, #0xA
150	b.ne	2f
151	/* Check if the transmit FIFO is full */
1521:	ldr	w2, [x1, #UARTLSR]
153	and	w2, w2, #(UARTLSR_TEMT | UARTLSR_THRE)
154	cmp	w2, #(UARTLSR_TEMT | UARTLSR_THRE)
155	b.ne	1b
156	mov	w2, #0xD		/* '\r' */
157	str	w2, [x1, #UARTTX]
158
159	/* Check if the transmit FIFO is full */
1602:	ldr	w2, [x1, #UARTLSR]
161	and	w2, w2, #(UARTLSR_TEMT | UARTLSR_THRE)
162	cmp	w2, #(UARTLSR_TEMT | UARTLSR_THRE)
163	b.ne	2b
164	str	w0, [x1, #UARTTX]
165	ret
166endfunc console_16550_core_putc
167
168	/* --------------------------------------------------------
169	 * int console_16550_putc(int c, console_16550_t *console)
170	 * Function to output a character over the console. It
171	 * returns the character printed on success or -1 on error.
172	 * In : w0 - character to be printed
173	 *      x1 - pointer to console_t structure
174	 * Out : return -1 on error else return character.
175	 * Clobber list : x2
176	 * --------------------------------------------------------
177	 */
178func console_16550_putc
179#if ENABLE_ASSERTIONS
180	cmp	x1, #0
181	ASM_ASSERT(ne)
182#endif /* ENABLE_ASSERTIONS */
183	ldr	x1, [x1, #CONSOLE_T_16550_BASE]
184	b	console_16550_core_putc
185endfunc console_16550_putc
186
187	/* ---------------------------------------------
188	 * int console_16550_core_getc(uintptr_t base_addr)
189	 * Function to get a character from the console.
190	 * It returns the character grabbed on success
191	 * or -1 on if no character is available.
192	 * In :  x0 - console base address
193	 * Out : w0 - character if available, else -1
194	 * Clobber list : x0, x1
195	 * ---------------------------------------------
196	 */
197func console_16550_core_getc
198#if ENABLE_ASSERTIONS
199	cmp	x0, #0
200	ASM_ASSERT(ne)
201#endif /* ENABLE_ASSERTIONS */
202
203	/* Check if the receive FIFO is empty */
2041:	ldr	w1, [x0, #UARTLSR]
205	tbz	w1, #UARTLSR_RDR_BIT, no_char
206	ldr	w0, [x0, #UARTRX]
207	ret
208no_char:
209	mov	w0, #ERROR_NO_PENDING_CHAR
210	ret
211endfunc console_16550_core_getc
212
213	/* ---------------------------------------------
214	 * int console_16550_getc(console_16550_t *console)
215	 * Function to get a character from the console.
216	 * It returns the character grabbed on success
217	 * or -1 on if no character is available.
218	 * In :  x0 - pointer to console_t stucture
219	 * Out : w0 - character if available, else -1
220	 * Clobber list : x0, x1
221	 * ---------------------------------------------
222	 */
223func console_16550_getc
224#if ENABLE_ASSERTIONS
225	cmp	x1, #0
226	ASM_ASSERT(ne)
227#endif /* ENABLE_ASSERTIONS */
228	ldr	x0, [x0, #CONSOLE_T_16550_BASE]
229	b	console_16550_core_getc
230endfunc console_16550_getc
231
232	/* ---------------------------------------------
233	 * int console_16550_core_flush(uintptr_t base_addr)
234	 * Function to force a write of all buffered
235	 * data that hasn't been output.
236	 * In : x0 - console base address
237	 * Out : return -1 on error else return 0.
238	 * Clobber list : x0, x1
239	 * ---------------------------------------------
240	 */
241func console_16550_core_flush
242#if ENABLE_ASSERTIONS
243	cmp	x0, #0
244	ASM_ASSERT(ne)
245#endif /* ENABLE_ASSERTIONS */
246
247	/* Loop until the transmit FIFO is empty */
2481:	ldr	w1, [x0, #UARTLSR]
249	and	w1, w1, #(UARTLSR_TEMT | UARTLSR_THRE)
250	cmp	w1, #(UARTLSR_TEMT | UARTLSR_THRE)
251	b.ne	1b
252
253	mov	w0, #0
254	ret
255endfunc console_16550_core_flush
256
257	/* ---------------------------------------------
258	 * int console_16550_flush(console_pl011_t *console)
259	 * Function to force a write of all buffered
260	 * data that hasn't been output.
261	 * In : x0 - pointer to console_t structure
262	 * Out : return -1 on error else return 0.
263	 * Clobber list : x0, x1
264	 * ---------------------------------------------
265	 */
266func console_16550_flush
267#if ENABLE_ASSERTIONS
268	cmp	x0, #0
269	ASM_ASSERT(ne)
270#endif /* ENABLE_ASSERTIONS */
271	ldr	x0, [x0, #CONSOLE_T_16550_BASE]
272	b	console_16550_core_flush
273endfunc console_16550_flush
274