1/*
2 * Copyright (c) 2015-2020, 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 <drivers/ti/uart/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	.globl console_16550_register
87
88	/* -----------------------------------------------
89	 * int console_16550_register(uintptr_t baseaddr,
90	 *     uint32_t clock, uint32_t baud,
91	 *     console_t *console);
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	 * If w1 (UART clock) is 0, initialisation will be
96	 * skipped, relying on previous code to have done
97	 * this already. w2 is ignored then as well.
98	 * In: x0 - UART register base address
99	 *     w1 - UART clock in Hz
100	 *     w2 - Baud rate (ignored if w1 is 0)
101	 *     x3 - pointer to empty console_t struct
102	 * Out: return 1 on success, 0 on error
103	 * Clobber list : x0, x1, x2, x6, x7, x14
104	 * -----------------------------------------------
105	 */
106func console_16550_register
107	mov	x7, x30
108	mov	x6, x3
109	cbz	x6, register_fail
110	str	x0, [x6, #CONSOLE_T_BASE]
111
112	/* A clock rate of zero means to skip the initialisation. */
113	cbz	w1, register_16550
114
115	bl	console_16550_core_init
116	cbz	x0, register_fail
117
118register_16550:
119	mov	x0, x6
120	mov	x30, x7
121	finish_console_register 16550 putc=1, getc=1, flush=1
122
123register_fail:
124	ret	x7
125endfunc console_16550_register
126
127	/* --------------------------------------------------------
128	 * int console_16550_core_putc(int c, uintptr_t base_addr)
129	 * Function to output a character over the console. It
130	 * returns the character printed on success or -1 on error.
131	 * In : w0 - character to be printed
132	 *      x1 - console base address
133	 * Out : return -1 on error else return character.
134	 * Clobber list : x2
135	 * --------------------------------------------------------
136	 */
137func console_16550_core_putc
138#if ENABLE_ASSERTIONS
139	cmp	x1, #0
140	ASM_ASSERT(ne)
141#endif /* ENABLE_ASSERTIONS */
142
143	/* Prepend '\r' to '\n' */
144	cmp	w0, #0xA
145	b.ne	2f
146	/* Check if the transmit FIFO is full */
1471:	ldr	w2, [x1, #UARTLSR]
148	and	w2, w2, #(UARTLSR_TEMT | UARTLSR_THRE)
149	cmp	w2, #(UARTLSR_TEMT | UARTLSR_THRE)
150	b.ne	1b
151	mov	w2, #0xD		/* '\r' */
152	str	w2, [x1, #UARTTX]
153
154	/* Check if the transmit FIFO is full */
1552:	ldr	w2, [x1, #UARTLSR]
156	and	w2, w2, #(UARTLSR_TEMT | UARTLSR_THRE)
157	cmp	w2, #(UARTLSR_TEMT | UARTLSR_THRE)
158	b.ne	2b
159	str	w0, [x1, #UARTTX]
160	ret
161endfunc console_16550_core_putc
162
163	/* --------------------------------------------------------
164	 * int console_16550_putc(int c, console_t *console)
165	 * Function to output a character over the console. It
166	 * returns the character printed on success or -1 on error.
167	 * In : w0 - character to be printed
168	 *      x1 - pointer to console_t structure
169	 * Out : return -1 on error else return character.
170	 * Clobber list : x2
171	 * --------------------------------------------------------
172	 */
173func console_16550_putc
174#if ENABLE_ASSERTIONS
175	cmp	x1, #0
176	ASM_ASSERT(ne)
177#endif /* ENABLE_ASSERTIONS */
178	ldr	x1, [x1, #CONSOLE_T_BASE]
179	b	console_16550_core_putc
180endfunc console_16550_putc
181
182	/* ---------------------------------------------
183	 * int console_16550_core_getc(uintptr_t base_addr)
184	 * Function to get a character from the console.
185	 * It returns the character grabbed on success
186	 * or -1 on if no character is available.
187	 * In :  x0 - console base address
188	 * Out : w0 - character if available, else -1
189	 * Clobber list : x0, x1
190	 * ---------------------------------------------
191	 */
192func console_16550_core_getc
193#if ENABLE_ASSERTIONS
194	cmp	x0, #0
195	ASM_ASSERT(ne)
196#endif /* ENABLE_ASSERTIONS */
197
198	/* Check if the receive FIFO is empty */
1991:	ldr	w1, [x0, #UARTLSR]
200	tbz	w1, #UARTLSR_RDR_BIT, no_char
201	ldr	w0, [x0, #UARTRX]
202	ret
203no_char:
204	mov	w0, #ERROR_NO_PENDING_CHAR
205	ret
206endfunc console_16550_core_getc
207
208	/* ---------------------------------------------
209	 * int console_16550_getc(console_t *console)
210	 * Function to get a character from the console.
211	 * It returns the character grabbed on success
212	 * or -1 on if no character is available.
213	 * In :  x0 - pointer to console_t stucture
214	 * Out : w0 - character if available, else -1
215	 * Clobber list : x0, x1
216	 * ---------------------------------------------
217	 */
218func console_16550_getc
219#if ENABLE_ASSERTIONS
220	cmp	x1, #0
221	ASM_ASSERT(ne)
222#endif /* ENABLE_ASSERTIONS */
223	ldr	x0, [x0, #CONSOLE_T_BASE]
224	b	console_16550_core_getc
225endfunc console_16550_getc
226
227	/* ---------------------------------------------
228	 * void console_16550_core_flush(uintptr_t base_addr)
229	 * Function to force a write of all buffered
230	 * data that hasn't been output.
231	 * In : x0 - console base address
232	 * Out : void.
233	 * Clobber list : x0, x1
234	 * ---------------------------------------------
235	 */
236func console_16550_core_flush
237#if ENABLE_ASSERTIONS
238	cmp	x0, #0
239	ASM_ASSERT(ne)
240#endif /* ENABLE_ASSERTIONS */
241
242	/* Loop until the transmit FIFO is empty */
2431:	ldr	w1, [x0, #UARTLSR]
244	and	w1, w1, #(UARTLSR_TEMT | UARTLSR_THRE)
245	cmp	w1, #(UARTLSR_TEMT | UARTLSR_THRE)
246	b.ne	1b
247
248	ret
249endfunc console_16550_core_flush
250
251	/* ---------------------------------------------
252	 * void console_16550_flush(console_t *console)
253	 * Function to force a write of all buffered
254	 * data that hasn't been output.
255	 * In : x0 - pointer to console_t structure
256	 * Out : void.
257	 * Clobber list : x0, x1
258	 * ---------------------------------------------
259	 */
260func console_16550_flush
261#if ENABLE_ASSERTIONS
262	cmp	x0, #0
263	ASM_ASSERT(ne)
264#endif /* ENABLE_ASSERTIONS */
265	ldr	x0, [x0, #CONSOLE_T_BASE]
266	b	console_16550_core_flush
267endfunc console_16550_flush
268