blob: dab46e8c509935a8087c9b1d081dd68f5ac0c07d [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>
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,
91 * console_16550_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).
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
Soby Mathew58873ae2018-10-10 16:03:09 +0100114 finish_console_register 16550 putc=1, getc=1, flush=1
Julius Werner4e406bf2017-09-18 16:57:51 -0700115
116register_fail:
117 ret x7
118endfunc console_16550_register
Soby Mathew7abebe82016-08-08 12:38:52 +0100119
120 /* --------------------------------------------------------
Julius Werner4e406bf2017-09-18 16:57:51 -0700121 * int console_16550_core_putc(int c, uintptr_t base_addr)
Soby Mathew7abebe82016-08-08 12:38:52 +0100122 * Function to output a character over the console. It
123 * returns the character printed on success or -1 on error.
124 * In : w0 - character to be printed
125 * x1 - console base address
126 * Out : return -1 on error else return character.
127 * Clobber list : x2
128 * --------------------------------------------------------
129 */
Julius Werner4e406bf2017-09-18 16:57:51 -0700130func console_16550_core_putc
131#if ENABLE_ASSERTIONS
132 cmp x1, #0
133 ASM_ASSERT(ne)
134#endif /* ENABLE_ASSERTIONS */
Soby Mathew7abebe82016-08-08 12:38:52 +0100135
136 /* Prepend '\r' to '\n' */
137 cmp w0, #0xA
138 b.ne 2f
139 /* Check if the transmit FIFO is full */
1401: ldr w2, [x1, #UARTLSR]
141 and w2, w2, #(UARTLSR_TEMT | UARTLSR_THRE)
142 cmp w2, #(UARTLSR_TEMT | UARTLSR_THRE)
143 b.ne 1b
144 mov w2, #0xD /* '\r' */
145 str w2, [x1, #UARTTX]
Soby Mathew7abebe82016-08-08 12:38:52 +0100146
147 /* Check if the transmit FIFO is full */
1482: ldr w2, [x1, #UARTLSR]
149 and w2, w2, #(UARTLSR_TEMT | UARTLSR_THRE)
150 cmp w2, #(UARTLSR_TEMT | UARTLSR_THRE)
151 b.ne 2b
152 str w0, [x1, #UARTTX]
Soby Mathew7abebe82016-08-08 12:38:52 +0100153 ret
Julius Werner4e406bf2017-09-18 16:57:51 -0700154endfunc console_16550_core_putc
155
156 /* --------------------------------------------------------
157 * int console_16550_putc(int c, console_16550_t *console)
158 * Function to output a character over the console. It
159 * returns the character printed on success or -1 on error.
160 * In : w0 - character to be printed
161 * x1 - pointer to console_t structure
162 * Out : return -1 on error else return character.
163 * Clobber list : x2
164 * --------------------------------------------------------
165 */
166func console_16550_putc
167#if ENABLE_ASSERTIONS
168 cmp x1, #0
169 ASM_ASSERT(ne)
170#endif /* ENABLE_ASSERTIONS */
171 ldr x1, [x1, #CONSOLE_T_16550_BASE]
172 b console_16550_core_putc
173endfunc console_16550_putc
Soby Mathew7abebe82016-08-08 12:38:52 +0100174
175 /* ---------------------------------------------
Julius Werner4e406bf2017-09-18 16:57:51 -0700176 * int console_16550_core_getc(uintptr_t base_addr)
Soby Mathew7abebe82016-08-08 12:38:52 +0100177 * Function to get a character from the console.
178 * It returns the character grabbed on success
Julius Werner4e406bf2017-09-18 16:57:51 -0700179 * or -1 on if no character is available.
180 * In : x0 - console base address
181 * Out : w0 - character if available, else -1
Soby Mathew7abebe82016-08-08 12:38:52 +0100182 * Clobber list : x0, x1
183 * ---------------------------------------------
184 */
Julius Werner4e406bf2017-09-18 16:57:51 -0700185func console_16550_core_getc
186#if ENABLE_ASSERTIONS
187 cmp x0, #0
188 ASM_ASSERT(ne)
189#endif /* ENABLE_ASSERTIONS */
190
Soby Mathew7abebe82016-08-08 12:38:52 +0100191 /* Check if the receive FIFO is empty */
1921: ldr w1, [x0, #UARTLSR]
Julius Werner4e406bf2017-09-18 16:57:51 -0700193 tbz w1, #UARTLSR_RDR_BIT, no_char
Soby Mathew7abebe82016-08-08 12:38:52 +0100194 ldr w0, [x0, #UARTRX]
195 ret
Julius Werner4e406bf2017-09-18 16:57:51 -0700196no_char:
197 mov w0, #ERROR_NO_PENDING_CHAR
Soby Mathew7abebe82016-08-08 12:38:52 +0100198 ret
Julius Werner4e406bf2017-09-18 16:57:51 -0700199endfunc console_16550_core_getc
200
201 /* ---------------------------------------------
202 * int console_16550_getc(console_16550_t *console)
203 * Function to get a character from the console.
204 * It returns the character grabbed on success
205 * or -1 on if no character is available.
206 * In : x0 - pointer to console_t stucture
207 * Out : w0 - character if available, else -1
208 * Clobber list : x0, x1
209 * ---------------------------------------------
210 */
211func console_16550_getc
212#if ENABLE_ASSERTIONS
213 cmp x1, #0
214 ASM_ASSERT(ne)
215#endif /* ENABLE_ASSERTIONS */
216 ldr x0, [x0, #CONSOLE_T_16550_BASE]
217 b console_16550_core_getc
218endfunc console_16550_getc
Antonio Nino Diaz56ec1d52017-02-08 15:58:12 +0000219
220 /* ---------------------------------------------
Antonio Nino Diaz659ea8e2018-03-22 20:13:44 +0000221 * int console_16550_core_flush(uintptr_t base_addr)
Antonio Nino Diaz56ec1d52017-02-08 15:58:12 +0000222 * Function to force a write of all buffered
223 * data that hasn't been output.
224 * In : x0 - console base address
225 * Out : return -1 on error else return 0.
226 * Clobber list : x0, x1
227 * ---------------------------------------------
228 */
Antonio Nino Diaz659ea8e2018-03-22 20:13:44 +0000229func console_16550_core_flush
230#if ENABLE_ASSERTIONS
231 cmp x0, #0
232 ASM_ASSERT(ne)
233#endif /* ENABLE_ASSERTIONS */
234
235 /* Loop until the transmit FIFO is empty */
2361: ldr w1, [x0, #UARTLSR]
237 and w1, w1, #(UARTLSR_TEMT | UARTLSR_THRE)
238 cmp w1, #(UARTLSR_TEMT | UARTLSR_THRE)
239 b.ne 1b
240
Antonio Nino Diaz56ec1d52017-02-08 15:58:12 +0000241 mov w0, #0
242 ret
Antonio Nino Diaz659ea8e2018-03-22 20:13:44 +0000243endfunc console_16550_core_flush
244
245 /* ---------------------------------------------
246 * int console_16550_flush(console_pl011_t *console)
247 * Function to force a write of all buffered
248 * data that hasn't been output.
249 * In : x0 - pointer to console_t structure
250 * Out : return -1 on error else return 0.
251 * Clobber list : x0, x1
252 * ---------------------------------------------
253 */
254func console_16550_flush
255#if ENABLE_ASSERTIONS
256 cmp x0, #0
257 ASM_ASSERT(ne)
258#endif /* ENABLE_ASSERTIONS */
259 ldr x0, [x0, #CONSOLE_T_16550_BASE]
260 b console_16550_core_flush
261endfunc console_16550_flush