blob: 56e7e5c119588db01e5b37a0432be5b841531c93 [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>
10#include <console_macros.S>
Soby Mathew7abebe82016-08-08 12:38:52 +010011#include <uart_16550.h>
12
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]
69 /* enable fifo, DMA */
70 mov w3, #(UARTFCR_FIFOEN | UARTFCR_DMAEN)
71 str w3, [x0, #UARTFCR]
72 /* DTR + RTS */
73 mov w3, #3
74 str w3, [x0, #UARTMCR]
75 mov w0, #1
Julius Werner4e406bf2017-09-18 16:57:51 -070076 ret
Soby Mathew7abebe82016-08-08 12:38:52 +010077init_fail:
Julius Werner4e406bf2017-09-18 16:57:51 -070078 mov w0, #0
Soby Mathew7abebe82016-08-08 12:38:52 +010079 ret
Julius Werner4e406bf2017-09-18 16:57:51 -070080endfunc console_16550_core_init
81
82#if MULTI_CONSOLE_API
83 .globl console_16550_register
84
85 /* -----------------------------------------------
86 * int console_16550_register(console_16550_t *console,
87 uintptr_t base, uint32_t clk, uint32_t baud)
88 * Function to initialize and register a new 16550
89 * console. Storage passed in for the console struct
90 * *must* be persistent (i.e. not from the stack).
91 * In: x0 - UART register base address
92 * w1 - UART clock in Hz
93 * w2 - Baud rate
94 * x3 - pointer to empty console_16550_t struct
95 * Out: return 1 on success, 0 on error
96 * Clobber list : x0, x1, x2, x6, x7, x14
97 * -----------------------------------------------
98 */
99func console_16550_register
100 mov x7, x30
101 mov x6, x3
102 cbz x6, register_fail
103 str x0, [x6, #CONSOLE_T_16550_BASE]
104
105 bl console_16550_core_init
106 cbz x0, register_fail
107
108 mov x0, x6
109 mov x30, x7
110 finish_console_register 16550
111
112register_fail:
113 ret x7
114endfunc console_16550_register
115#else
116 .globl console_core_init
117 .globl console_core_putc
118 .globl console_core_getc
119 .globl console_core_flush
120 .equ console_core_init,console_16550_core_init
121 .equ console_core_putc,console_16550_core_putc
122 .equ console_core_getc,console_16550_core_getc
Antonio Nino Diaz659ea8e2018-03-22 20:13:44 +0000123 .equ console_core_flush,console_16550_core_flush
Julius Werner4e406bf2017-09-18 16:57:51 -0700124#endif
Soby Mathew7abebe82016-08-08 12:38:52 +0100125
126 /* --------------------------------------------------------
Julius Werner4e406bf2017-09-18 16:57:51 -0700127 * int console_16550_core_putc(int c, uintptr_t base_addr)
Soby Mathew7abebe82016-08-08 12:38:52 +0100128 * Function to output a character over the console. It
129 * returns the character printed on success or -1 on error.
130 * In : w0 - character to be printed
131 * x1 - console base address
132 * Out : return -1 on error else return character.
133 * Clobber list : x2
134 * --------------------------------------------------------
135 */
Julius Werner4e406bf2017-09-18 16:57:51 -0700136func console_16550_core_putc
137#if ENABLE_ASSERTIONS
138 cmp x1, #0
139 ASM_ASSERT(ne)
140#endif /* ENABLE_ASSERTIONS */
Soby Mathew7abebe82016-08-08 12:38:52 +0100141
142 /* Prepend '\r' to '\n' */
143 cmp w0, #0xA
144 b.ne 2f
145 /* Check if the transmit FIFO is full */
1461: ldr w2, [x1, #UARTLSR]
147 and w2, w2, #(UARTLSR_TEMT | UARTLSR_THRE)
148 cmp w2, #(UARTLSR_TEMT | UARTLSR_THRE)
149 b.ne 1b
150 mov w2, #0xD /* '\r' */
151 str w2, [x1, #UARTTX]
Soby Mathew7abebe82016-08-08 12:38:52 +0100152
153 /* Check if the transmit FIFO is full */
1542: ldr w2, [x1, #UARTLSR]
155 and w2, w2, #(UARTLSR_TEMT | UARTLSR_THRE)
156 cmp w2, #(UARTLSR_TEMT | UARTLSR_THRE)
157 b.ne 2b
158 str w0, [x1, #UARTTX]
Soby Mathew7abebe82016-08-08 12:38:52 +0100159 ret
Julius Werner4e406bf2017-09-18 16:57:51 -0700160endfunc console_16550_core_putc
161
162 /* --------------------------------------------------------
163 * int console_16550_putc(int c, console_16550_t *console)
164 * Function to output a character over the console. It
165 * returns the character printed on success or -1 on error.
166 * In : w0 - character to be printed
167 * x1 - pointer to console_t structure
168 * Out : return -1 on error else return character.
169 * Clobber list : x2
170 * --------------------------------------------------------
171 */
172func console_16550_putc
173#if ENABLE_ASSERTIONS
174 cmp x1, #0
175 ASM_ASSERT(ne)
176#endif /* ENABLE_ASSERTIONS */
177 ldr x1, [x1, #CONSOLE_T_16550_BASE]
178 b console_16550_core_putc
179endfunc console_16550_putc
Soby Mathew7abebe82016-08-08 12:38:52 +0100180
181 /* ---------------------------------------------
Julius Werner4e406bf2017-09-18 16:57:51 -0700182 * int console_16550_core_getc(uintptr_t base_addr)
Soby Mathew7abebe82016-08-08 12:38:52 +0100183 * Function to get a character from the console.
184 * It returns the character grabbed on success
Julius Werner4e406bf2017-09-18 16:57:51 -0700185 * or -1 on if no character is available.
186 * In : x0 - console base address
187 * Out : w0 - character if available, else -1
Soby Mathew7abebe82016-08-08 12:38:52 +0100188 * Clobber list : x0, x1
189 * ---------------------------------------------
190 */
Julius Werner4e406bf2017-09-18 16:57:51 -0700191func console_16550_core_getc
192#if ENABLE_ASSERTIONS
193 cmp x0, #0
194 ASM_ASSERT(ne)
195#endif /* ENABLE_ASSERTIONS */
196
Soby Mathew7abebe82016-08-08 12:38:52 +0100197 /* Check if the receive FIFO is empty */
1981: ldr w1, [x0, #UARTLSR]
Julius Werner4e406bf2017-09-18 16:57:51 -0700199 tbz w1, #UARTLSR_RDR_BIT, no_char
Soby Mathew7abebe82016-08-08 12:38:52 +0100200 ldr w0, [x0, #UARTRX]
201 ret
Julius Werner4e406bf2017-09-18 16:57:51 -0700202no_char:
203 mov w0, #ERROR_NO_PENDING_CHAR
Soby Mathew7abebe82016-08-08 12:38:52 +0100204 ret
Julius Werner4e406bf2017-09-18 16:57:51 -0700205endfunc console_16550_core_getc
206
207 /* ---------------------------------------------
208 * int console_16550_getc(console_16550_t *console)
209 * Function to get a character from the console.
210 * It returns the character grabbed on success
211 * or -1 on if no character is available.
212 * In : x0 - pointer to console_t stucture
213 * Out : w0 - character if available, else -1
214 * Clobber list : x0, x1
215 * ---------------------------------------------
216 */
217func console_16550_getc
218#if ENABLE_ASSERTIONS
219 cmp x1, #0
220 ASM_ASSERT(ne)
221#endif /* ENABLE_ASSERTIONS */
222 ldr x0, [x0, #CONSOLE_T_16550_BASE]
223 b console_16550_core_getc
224endfunc console_16550_getc
Antonio Nino Diaz56ec1d52017-02-08 15:58:12 +0000225
226 /* ---------------------------------------------
Antonio Nino Diaz659ea8e2018-03-22 20:13:44 +0000227 * int console_16550_core_flush(uintptr_t base_addr)
Antonio Nino Diaz56ec1d52017-02-08 15:58:12 +0000228 * Function to force a write of all buffered
229 * data that hasn't been output.
230 * In : x0 - console base address
231 * Out : return -1 on error else return 0.
232 * Clobber list : x0, x1
233 * ---------------------------------------------
234 */
Antonio Nino Diaz659ea8e2018-03-22 20:13:44 +0000235func console_16550_core_flush
236#if ENABLE_ASSERTIONS
237 cmp x0, #0
238 ASM_ASSERT(ne)
239#endif /* ENABLE_ASSERTIONS */
240
241 /* Loop until the transmit FIFO is empty */
2421: ldr w1, [x0, #UARTLSR]
243 and w1, w1, #(UARTLSR_TEMT | UARTLSR_THRE)
244 cmp w1, #(UARTLSR_TEMT | UARTLSR_THRE)
245 b.ne 1b
246
Antonio Nino Diaz56ec1d52017-02-08 15:58:12 +0000247 mov w0, #0
248 ret
Antonio Nino Diaz659ea8e2018-03-22 20:13:44 +0000249endfunc console_16550_core_flush
250
251 /* ---------------------------------------------
252 * int console_16550_flush(console_pl011_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 : return -1 on error else return 0.
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_16550_BASE]
266 b console_16550_core_flush
267endfunc console_16550_flush