blob: 785b640dd21c463f278a3f62aa70b6ce92b81287 [file] [log] [blame]
Soby Mathew7abebe82016-08-08 12:38:52 +01001/*
Antonio Nino Diaz659ea8e2018-03-22 20:13:44 +00002 * Copyright (c) 2015-2018, 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>
Soby Mathew58873ae2018-10-10 16:03:09 +010010#define USE_FINISH_CONSOLE_REG_2
Julius Werner4e406bf2017-09-18 16:57:51 -070011#include <console_macros.S>
Soby Mathew7abebe82016-08-08 12:38:52 +010012#include <uart_16550.h>
13
Julius Werner4e406bf2017-09-18 16:57:51 -070014 /*
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
Antonio Nino Diaz659ea8e2018-03-22 20:13:44 +000021 .globl console_16550_core_flush
Julius Werner4e406bf2017-09-18 16:57:51 -070022
23 .globl console_16550_putc
24 .globl console_16550_getc
Antonio Nino Diaz659ea8e2018-03-22 20:13:44 +000025 .globl console_16550_flush
Soby Mathew7abebe82016-08-08 12:38:52 +010026
27 /* -----------------------------------------------
Julius Werner4e406bf2017-09-18 16:57:51 -070028 * int console_16550_core_init(uintptr_t base_addr,
Soby Mathew7abebe82016-08-08 12:38:52 +010029 * 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
Julius Werner4e406bf2017-09-18 16:57:51 -070037 * Out: return 1 on success, 0 on error
Soby Mathew7abebe82016-08-08 12:38:52 +010038 * Clobber list : x1, x2, x3
39 * -----------------------------------------------
40 */
Julius Werner4e406bf2017-09-18 16:57:51 -070041func console_16550_core_init
Soby Mathew7abebe82016-08-08 12:38:52 +010042 /* 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]
Benjamin Fair4d5853d2016-10-14 01:13:33 +000070#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 */
Soby Mathew7abebe82016-08-08 12:38:52 +010074 /* 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
Julius Werner4e406bf2017-09-18 16:57:51 -070081 ret
Soby Mathew7abebe82016-08-08 12:38:52 +010082init_fail:
Julius Werner4e406bf2017-09-18 16:57:51 -070083 mov w0, #0
Soby Mathew7abebe82016-08-08 12:38:52 +010084 ret
Julius Werner4e406bf2017-09-18 16:57:51 -070085endfunc console_16550_core_init
86
87#if MULTI_CONSOLE_API
88 .globl console_16550_register
89
90 /* -----------------------------------------------
Antonio Nino Diaz992c5ac2018-10-08 13:26:48 +010091 * int console_16550_register(uintptr_t baseaddr,
92 * uint32_t clock, uint32_t baud,
93 * console_16550_t *console);
Julius Werner4e406bf2017-09-18 16:57:51 -070094 * 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
Soby Mathew58873ae2018-10-10 16:03:09 +0100116 finish_console_register 16550 putc=1, getc=1, flush=1
Julius Werner4e406bf2017-09-18 16:57:51 -0700117
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
Antonio Nino Diaz659ea8e2018-03-22 20:13:44 +0000129 .equ console_core_flush,console_16550_core_flush
Julius Werner4e406bf2017-09-18 16:57:51 -0700130#endif
Soby Mathew7abebe82016-08-08 12:38:52 +0100131
132 /* --------------------------------------------------------
Julius Werner4e406bf2017-09-18 16:57:51 -0700133 * int console_16550_core_putc(int c, uintptr_t base_addr)
Soby Mathew7abebe82016-08-08 12:38:52 +0100134 * 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 */
Julius Werner4e406bf2017-09-18 16:57:51 -0700142func console_16550_core_putc
143#if ENABLE_ASSERTIONS
144 cmp x1, #0
145 ASM_ASSERT(ne)
146#endif /* ENABLE_ASSERTIONS */
Soby Mathew7abebe82016-08-08 12:38:52 +0100147
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]
Soby Mathew7abebe82016-08-08 12:38:52 +0100158
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]
Soby Mathew7abebe82016-08-08 12:38:52 +0100165 ret
Julius Werner4e406bf2017-09-18 16:57:51 -0700166endfunc 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
Soby Mathew7abebe82016-08-08 12:38:52 +0100186
187 /* ---------------------------------------------
Julius Werner4e406bf2017-09-18 16:57:51 -0700188 * int console_16550_core_getc(uintptr_t base_addr)
Soby Mathew7abebe82016-08-08 12:38:52 +0100189 * Function to get a character from the console.
190 * It returns the character grabbed on success
Julius Werner4e406bf2017-09-18 16:57:51 -0700191 * or -1 on if no character is available.
192 * In : x0 - console base address
193 * Out : w0 - character if available, else -1
Soby Mathew7abebe82016-08-08 12:38:52 +0100194 * Clobber list : x0, x1
195 * ---------------------------------------------
196 */
Julius Werner4e406bf2017-09-18 16:57:51 -0700197func console_16550_core_getc
198#if ENABLE_ASSERTIONS
199 cmp x0, #0
200 ASM_ASSERT(ne)
201#endif /* ENABLE_ASSERTIONS */
202
Soby Mathew7abebe82016-08-08 12:38:52 +0100203 /* Check if the receive FIFO is empty */
2041: ldr w1, [x0, #UARTLSR]
Julius Werner4e406bf2017-09-18 16:57:51 -0700205 tbz w1, #UARTLSR_RDR_BIT, no_char
Soby Mathew7abebe82016-08-08 12:38:52 +0100206 ldr w0, [x0, #UARTRX]
207 ret
Julius Werner4e406bf2017-09-18 16:57:51 -0700208no_char:
209 mov w0, #ERROR_NO_PENDING_CHAR
Soby Mathew7abebe82016-08-08 12:38:52 +0100210 ret
Julius Werner4e406bf2017-09-18 16:57:51 -0700211endfunc 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
Antonio Nino Diaz56ec1d52017-02-08 15:58:12 +0000231
232 /* ---------------------------------------------
Antonio Nino Diaz659ea8e2018-03-22 20:13:44 +0000233 * int console_16550_core_flush(uintptr_t base_addr)
Antonio Nino Diaz56ec1d52017-02-08 15:58:12 +0000234 * 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 */
Antonio Nino Diaz659ea8e2018-03-22 20:13:44 +0000241func 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
Antonio Nino Diaz56ec1d52017-02-08 15:58:12 +0000253 mov w0, #0
254 ret
Antonio Nino Diaz659ea8e2018-03-22 20:13:44 +0000255endfunc 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