blob: 2b1b5a9d764915b6ce2bff5daffaad0b9b5e92c9 [file] [log] [blame]
Soby Mathew7abebe82016-08-08 12:38:52 +01001/*
Jimmy Brisson39f9eee2020-08-05 13:44:05 -05002 * Copyright (c) 2015-2020, ARM Limited and Contributors. All rights reserved.
Soby Mathew7abebe82016-08-08 12:38:52 +01003 *
dp-armfa3cf0b2017-05-03 09:38:09 +01004 * SPDX-License-Identifier: BSD-3-Clause
Soby Mathew7abebe82016-08-08 12:38:52 +01005 */
6
7#include <arch.h>
8#include <asm_macros.S>
Julius Werner4e406bf2017-09-18 16:57:51 -07009#include <assert_macros.S>
10#include <console_macros.S>
Antonio Nino Diaze0f90632018-12-14 00:18:21 +000011#include <drivers/ti/uart/uart_16550.h>
Soby Mathew7abebe82016-08-08 12:38:52 +010012
Julius Werner4e406bf2017-09-18 16:57:51 -070013 /*
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
Antonio Nino Diaz659ea8e2018-03-22 20:13:44 +000020 .globl console_16550_core_flush
Julius Werner4e406bf2017-09-18 16:57:51 -070021
22 .globl console_16550_putc
23 .globl console_16550_getc
Antonio Nino Diaz659ea8e2018-03-22 20:13:44 +000024 .globl console_16550_flush
Soby Mathew7abebe82016-08-08 12:38:52 +010025
26 /* -----------------------------------------------
Julius Werner4e406bf2017-09-18 16:57:51 -070027 * int console_16550_core_init(uintptr_t base_addr,
Soby Mathew7abebe82016-08-08 12:38:52 +010028 * 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
Julius Werner4e406bf2017-09-18 16:57:51 -070036 * Out: return 1 on success, 0 on error
Soby Mathew7abebe82016-08-08 12:38:52 +010037 * Clobber list : x1, x2, x3
38 * -----------------------------------------------
39 */
Julius Werner4e406bf2017-09-18 16:57:51 -070040func console_16550_core_init
Soby Mathew7abebe82016-08-08 12:38:52 +010041 /* 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]
Benjamin Fair4d5853d2016-10-14 01:13:33 +000069#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 */
Soby Mathew7abebe82016-08-08 12:38:52 +010073 /* 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
Julius Werner4e406bf2017-09-18 16:57:51 -070080 ret
Soby Mathew7abebe82016-08-08 12:38:52 +010081init_fail:
Julius Werner4e406bf2017-09-18 16:57:51 -070082 mov w0, #0
Soby Mathew7abebe82016-08-08 12:38:52 +010083 ret
Julius Werner4e406bf2017-09-18 16:57:51 -070084endfunc console_16550_core_init
85
Julius Werner4e406bf2017-09-18 16:57:51 -070086 .globl console_16550_register
87
88 /* -----------------------------------------------
Antonio Nino Diaz992c5ac2018-10-08 13:26:48 +010089 * int console_16550_register(uintptr_t baseaddr,
90 * uint32_t clock, uint32_t baud,
Andre Przywara98b5a112020-01-25 00:58:35 +000091 * console_t *console);
Julius Werner4e406bf2017-09-18 16:57:51 -070092 * 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).
Andre Przywara40497792019-12-12 12:00:15 +000095 * 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.
Julius Werner4e406bf2017-09-18 16:57:51 -070098 * In: x0 - UART register base address
99 * w1 - UART clock in Hz
Andre Przywara40497792019-12-12 12:00:15 +0000100 * w2 - Baud rate (ignored if w1 is 0)
Andre Przywara98b5a112020-01-25 00:58:35 +0000101 * x3 - pointer to empty console_t struct
Julius Werner4e406bf2017-09-18 16:57:51 -0700102 * 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
Andre Przywara98b5a112020-01-25 00:58:35 +0000110 str x0, [x6, #CONSOLE_T_BASE]
Julius Werner4e406bf2017-09-18 16:57:51 -0700111
Andre Przywara40497792019-12-12 12:00:15 +0000112 /* A clock rate of zero means to skip the initialisation. */
113 cbz w1, register_16550
114
Julius Werner4e406bf2017-09-18 16:57:51 -0700115 bl console_16550_core_init
116 cbz x0, register_fail
117
Andre Przywara40497792019-12-12 12:00:15 +0000118register_16550:
Julius Werner4e406bf2017-09-18 16:57:51 -0700119 mov x0, x6
120 mov x30, x7
Sandrine Bailleuxf57e2032023-10-11 08:38:00 +0200121 finish_console_register 16550 putc=1, getc=ENABLE_CONSOLE_GETC, flush=1
Julius Werner4e406bf2017-09-18 16:57:51 -0700122
123register_fail:
124 ret x7
125endfunc console_16550_register
Soby Mathew7abebe82016-08-08 12:38:52 +0100126
127 /* --------------------------------------------------------
Julius Werner4e406bf2017-09-18 16:57:51 -0700128 * int console_16550_core_putc(int c, uintptr_t base_addr)
Soby Mathew7abebe82016-08-08 12:38:52 +0100129 * 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 */
Julius Werner4e406bf2017-09-18 16:57:51 -0700137func console_16550_core_putc
138#if ENABLE_ASSERTIONS
139 cmp x1, #0
140 ASM_ASSERT(ne)
141#endif /* ENABLE_ASSERTIONS */
Soby Mathew7abebe82016-08-08 12:38:52 +0100142
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]
Soby Mathew7abebe82016-08-08 12:38:52 +0100153
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]
Soby Mathew7abebe82016-08-08 12:38:52 +0100160 ret
Julius Werner4e406bf2017-09-18 16:57:51 -0700161endfunc console_16550_core_putc
162
163 /* --------------------------------------------------------
Andre Przywara98b5a112020-01-25 00:58:35 +0000164 * int console_16550_putc(int c, console_t *console)
Julius Werner4e406bf2017-09-18 16:57:51 -0700165 * 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 */
Andre Przywara98b5a112020-01-25 00:58:35 +0000178 ldr x1, [x1, #CONSOLE_T_BASE]
Julius Werner4e406bf2017-09-18 16:57:51 -0700179 b console_16550_core_putc
180endfunc console_16550_putc
Soby Mathew7abebe82016-08-08 12:38:52 +0100181
182 /* ---------------------------------------------
Julius Werner4e406bf2017-09-18 16:57:51 -0700183 * int console_16550_core_getc(uintptr_t base_addr)
Soby Mathew7abebe82016-08-08 12:38:52 +0100184 * Function to get a character from the console.
185 * It returns the character grabbed on success
Julius Werner4e406bf2017-09-18 16:57:51 -0700186 * or -1 on if no character is available.
187 * In : x0 - console base address
188 * Out : w0 - character if available, else -1
Soby Mathew7abebe82016-08-08 12:38:52 +0100189 * Clobber list : x0, x1
190 * ---------------------------------------------
191 */
Julius Werner4e406bf2017-09-18 16:57:51 -0700192func console_16550_core_getc
193#if ENABLE_ASSERTIONS
194 cmp x0, #0
195 ASM_ASSERT(ne)
196#endif /* ENABLE_ASSERTIONS */
197
Soby Mathew7abebe82016-08-08 12:38:52 +0100198 /* Check if the receive FIFO is empty */
1991: ldr w1, [x0, #UARTLSR]
Julius Werner4e406bf2017-09-18 16:57:51 -0700200 tbz w1, #UARTLSR_RDR_BIT, no_char
Soby Mathew7abebe82016-08-08 12:38:52 +0100201 ldr w0, [x0, #UARTRX]
202 ret
Julius Werner4e406bf2017-09-18 16:57:51 -0700203no_char:
204 mov w0, #ERROR_NO_PENDING_CHAR
Soby Mathew7abebe82016-08-08 12:38:52 +0100205 ret
Julius Werner4e406bf2017-09-18 16:57:51 -0700206endfunc console_16550_core_getc
207
208 /* ---------------------------------------------
Andre Przywara98b5a112020-01-25 00:58:35 +0000209 * int console_16550_getc(console_t *console)
Julius Werner4e406bf2017-09-18 16:57:51 -0700210 * 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 */
Andre Przywara98b5a112020-01-25 00:58:35 +0000223 ldr x0, [x0, #CONSOLE_T_BASE]
Julius Werner4e406bf2017-09-18 16:57:51 -0700224 b console_16550_core_getc
225endfunc console_16550_getc
Antonio Nino Diaz56ec1d52017-02-08 15:58:12 +0000226
227 /* ---------------------------------------------
Jimmy Brisson39f9eee2020-08-05 13:44:05 -0500228 * void console_16550_core_flush(uintptr_t base_addr)
Antonio Nino Diaz56ec1d52017-02-08 15:58:12 +0000229 * Function to force a write of all buffered
230 * data that hasn't been output.
231 * In : x0 - console base address
Jimmy Brisson39f9eee2020-08-05 13:44:05 -0500232 * Out : void.
Antonio Nino Diaz56ec1d52017-02-08 15:58:12 +0000233 * Clobber list : x0, x1
234 * ---------------------------------------------
235 */
Antonio Nino Diaz659ea8e2018-03-22 20:13:44 +0000236func 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
Antonio Nino Diaz56ec1d52017-02-08 15:58:12 +0000248 ret
Antonio Nino Diaz659ea8e2018-03-22 20:13:44 +0000249endfunc console_16550_core_flush
250
251 /* ---------------------------------------------
Jimmy Brisson39f9eee2020-08-05 13:44:05 -0500252 * void console_16550_flush(console_t *console)
Antonio Nino Diaz659ea8e2018-03-22 20:13:44 +0000253 * Function to force a write of all buffered
254 * data that hasn't been output.
255 * In : x0 - pointer to console_t structure
Jimmy Brisson39f9eee2020-08-05 13:44:05 -0500256 * Out : void.
Antonio Nino Diaz659ea8e2018-03-22 20:13:44 +0000257 * 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 */
Andre Przywara98b5a112020-01-25 00:58:35 +0000265 ldr x0, [x0, #CONSOLE_T_BASE]
Antonio Nino Diaz659ea8e2018-03-22 20:13:44 +0000266 b console_16550_core_flush
267endfunc console_16550_flush