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