Soby Mathew | 7abebe8 | 2016-08-08 12:38:52 +0100 | [diff] [blame] | 1 | /* |
Jimmy Brisson | 39f9eee | 2020-08-05 13:44:05 -0500 | [diff] [blame] | 2 | * Copyright (c) 2015-2020, ARM Limited and Contributors. All rights reserved. |
Soby Mathew | 7abebe8 | 2016-08-08 12:38:52 +0100 | [diff] [blame] | 3 | * |
dp-arm | fa3cf0b | 2017-05-03 09:38:09 +0100 | [diff] [blame] | 4 | * SPDX-License-Identifier: BSD-3-Clause |
Soby Mathew | 7abebe8 | 2016-08-08 12:38:52 +0100 | [diff] [blame] | 5 | */ |
| 6 | |
| 7 | #include <arch.h> |
| 8 | #include <asm_macros.S> |
Julius Werner | 4e406bf | 2017-09-18 16:57:51 -0700 | [diff] [blame] | 9 | #include <assert_macros.S> |
| 10 | #include <console_macros.S> |
Antonio Nino Diaz | e0f9063 | 2018-12-14 00:18:21 +0000 | [diff] [blame] | 11 | #include <drivers/ti/uart/uart_16550.h> |
Soby Mathew | 7abebe8 | 2016-08-08 12:38:52 +0100 | [diff] [blame] | 12 | |
Julius Werner | 4e406bf | 2017-09-18 16:57:51 -0700 | [diff] [blame] | 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 |
Antonio Nino Diaz | 659ea8e | 2018-03-22 20:13:44 +0000 | [diff] [blame] | 20 | .globl console_16550_core_flush |
Julius Werner | 4e406bf | 2017-09-18 16:57:51 -0700 | [diff] [blame] | 21 | |
| 22 | .globl console_16550_putc |
| 23 | .globl console_16550_getc |
Antonio Nino Diaz | 659ea8e | 2018-03-22 20:13:44 +0000 | [diff] [blame] | 24 | .globl console_16550_flush |
Soby Mathew | 7abebe8 | 2016-08-08 12:38:52 +0100 | [diff] [blame] | 25 | |
| 26 | /* ----------------------------------------------- |
Julius Werner | 4e406bf | 2017-09-18 16:57:51 -0700 | [diff] [blame] | 27 | * int console_16550_core_init(uintptr_t base_addr, |
Soby Mathew | 7abebe8 | 2016-08-08 12:38:52 +0100 | [diff] [blame] | 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 |
Julius Werner | 4e406bf | 2017-09-18 16:57:51 -0700 | [diff] [blame] | 36 | * Out: return 1 on success, 0 on error |
Soby Mathew | 7abebe8 | 2016-08-08 12:38:52 +0100 | [diff] [blame] | 37 | * Clobber list : x1, x2, x3 |
| 38 | * ----------------------------------------------- |
| 39 | */ |
Julius Werner | 4e406bf | 2017-09-18 16:57:51 -0700 | [diff] [blame] | 40 | func console_16550_core_init |
Soby Mathew | 7abebe8 | 2016-08-08 12:38:52 +0100 | [diff] [blame] | 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] |
Benjamin Fair | 4d5853d | 2016-10-14 01:13:33 +0000 | [diff] [blame] | 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 */ |
Soby Mathew | 7abebe8 | 2016-08-08 12:38:52 +0100 | [diff] [blame] | 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 |
Julius Werner | 4e406bf | 2017-09-18 16:57:51 -0700 | [diff] [blame] | 80 | ret |
Soby Mathew | 7abebe8 | 2016-08-08 12:38:52 +0100 | [diff] [blame] | 81 | init_fail: |
Julius Werner | 4e406bf | 2017-09-18 16:57:51 -0700 | [diff] [blame] | 82 | mov w0, #0 |
Soby Mathew | 7abebe8 | 2016-08-08 12:38:52 +0100 | [diff] [blame] | 83 | ret |
Julius Werner | 4e406bf | 2017-09-18 16:57:51 -0700 | [diff] [blame] | 84 | endfunc console_16550_core_init |
| 85 | |
Julius Werner | 4e406bf | 2017-09-18 16:57:51 -0700 | [diff] [blame] | 86 | .globl console_16550_register |
| 87 | |
| 88 | /* ----------------------------------------------- |
Antonio Nino Diaz | 992c5ac | 2018-10-08 13:26:48 +0100 | [diff] [blame] | 89 | * int console_16550_register(uintptr_t baseaddr, |
| 90 | * uint32_t clock, uint32_t baud, |
Andre Przywara | 98b5a11 | 2020-01-25 00:58:35 +0000 | [diff] [blame] | 91 | * console_t *console); |
Julius Werner | 4e406bf | 2017-09-18 16:57:51 -0700 | [diff] [blame] | 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). |
Andre Przywara | 4049779 | 2019-12-12 12:00:15 +0000 | [diff] [blame] | 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. |
Julius Werner | 4e406bf | 2017-09-18 16:57:51 -0700 | [diff] [blame] | 98 | * In: x0 - UART register base address |
| 99 | * w1 - UART clock in Hz |
Andre Przywara | 4049779 | 2019-12-12 12:00:15 +0000 | [diff] [blame] | 100 | * w2 - Baud rate (ignored if w1 is 0) |
Andre Przywara | 98b5a11 | 2020-01-25 00:58:35 +0000 | [diff] [blame] | 101 | * x3 - pointer to empty console_t struct |
Julius Werner | 4e406bf | 2017-09-18 16:57:51 -0700 | [diff] [blame] | 102 | * Out: return 1 on success, 0 on error |
| 103 | * Clobber list : x0, x1, x2, x6, x7, x14 |
| 104 | * ----------------------------------------------- |
| 105 | */ |
| 106 | func console_16550_register |
| 107 | mov x7, x30 |
| 108 | mov x6, x3 |
| 109 | cbz x6, register_fail |
Andre Przywara | 98b5a11 | 2020-01-25 00:58:35 +0000 | [diff] [blame] | 110 | str x0, [x6, #CONSOLE_T_BASE] |
Julius Werner | 4e406bf | 2017-09-18 16:57:51 -0700 | [diff] [blame] | 111 | |
Andre Przywara | 4049779 | 2019-12-12 12:00:15 +0000 | [diff] [blame] | 112 | /* A clock rate of zero means to skip the initialisation. */ |
| 113 | cbz w1, register_16550 |
| 114 | |
Julius Werner | 4e406bf | 2017-09-18 16:57:51 -0700 | [diff] [blame] | 115 | bl console_16550_core_init |
| 116 | cbz x0, register_fail |
| 117 | |
Andre Przywara | 4049779 | 2019-12-12 12:00:15 +0000 | [diff] [blame] | 118 | register_16550: |
Julius Werner | 4e406bf | 2017-09-18 16:57:51 -0700 | [diff] [blame] | 119 | mov x0, x6 |
| 120 | mov x30, x7 |
Soby Mathew | 58873ae | 2018-10-10 16:03:09 +0100 | [diff] [blame] | 121 | finish_console_register 16550 putc=1, getc=1, flush=1 |
Julius Werner | 4e406bf | 2017-09-18 16:57:51 -0700 | [diff] [blame] | 122 | |
| 123 | register_fail: |
| 124 | ret x7 |
| 125 | endfunc console_16550_register |
Soby Mathew | 7abebe8 | 2016-08-08 12:38:52 +0100 | [diff] [blame] | 126 | |
| 127 | /* -------------------------------------------------------- |
Julius Werner | 4e406bf | 2017-09-18 16:57:51 -0700 | [diff] [blame] | 128 | * int console_16550_core_putc(int c, uintptr_t base_addr) |
Soby Mathew | 7abebe8 | 2016-08-08 12:38:52 +0100 | [diff] [blame] | 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 | */ |
Julius Werner | 4e406bf | 2017-09-18 16:57:51 -0700 | [diff] [blame] | 137 | func console_16550_core_putc |
| 138 | #if ENABLE_ASSERTIONS |
| 139 | cmp x1, #0 |
| 140 | ASM_ASSERT(ne) |
| 141 | #endif /* ENABLE_ASSERTIONS */ |
Soby Mathew | 7abebe8 | 2016-08-08 12:38:52 +0100 | [diff] [blame] | 142 | |
| 143 | /* Prepend '\r' to '\n' */ |
| 144 | cmp w0, #0xA |
| 145 | b.ne 2f |
| 146 | /* Check if the transmit FIFO is full */ |
| 147 | 1: 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 Mathew | 7abebe8 | 2016-08-08 12:38:52 +0100 | [diff] [blame] | 153 | |
| 154 | /* Check if the transmit FIFO is full */ |
| 155 | 2: 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 Mathew | 7abebe8 | 2016-08-08 12:38:52 +0100 | [diff] [blame] | 160 | ret |
Julius Werner | 4e406bf | 2017-09-18 16:57:51 -0700 | [diff] [blame] | 161 | endfunc console_16550_core_putc |
| 162 | |
| 163 | /* -------------------------------------------------------- |
Andre Przywara | 98b5a11 | 2020-01-25 00:58:35 +0000 | [diff] [blame] | 164 | * int console_16550_putc(int c, console_t *console) |
Julius Werner | 4e406bf | 2017-09-18 16:57:51 -0700 | [diff] [blame] | 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 | */ |
| 173 | func console_16550_putc |
| 174 | #if ENABLE_ASSERTIONS |
| 175 | cmp x1, #0 |
| 176 | ASM_ASSERT(ne) |
| 177 | #endif /* ENABLE_ASSERTIONS */ |
Andre Przywara | 98b5a11 | 2020-01-25 00:58:35 +0000 | [diff] [blame] | 178 | ldr x1, [x1, #CONSOLE_T_BASE] |
Julius Werner | 4e406bf | 2017-09-18 16:57:51 -0700 | [diff] [blame] | 179 | b console_16550_core_putc |
| 180 | endfunc console_16550_putc |
Soby Mathew | 7abebe8 | 2016-08-08 12:38:52 +0100 | [diff] [blame] | 181 | |
| 182 | /* --------------------------------------------- |
Julius Werner | 4e406bf | 2017-09-18 16:57:51 -0700 | [diff] [blame] | 183 | * int console_16550_core_getc(uintptr_t base_addr) |
Soby Mathew | 7abebe8 | 2016-08-08 12:38:52 +0100 | [diff] [blame] | 184 | * Function to get a character from the console. |
| 185 | * It returns the character grabbed on success |
Julius Werner | 4e406bf | 2017-09-18 16:57:51 -0700 | [diff] [blame] | 186 | * or -1 on if no character is available. |
| 187 | * In : x0 - console base address |
| 188 | * Out : w0 - character if available, else -1 |
Soby Mathew | 7abebe8 | 2016-08-08 12:38:52 +0100 | [diff] [blame] | 189 | * Clobber list : x0, x1 |
| 190 | * --------------------------------------------- |
| 191 | */ |
Julius Werner | 4e406bf | 2017-09-18 16:57:51 -0700 | [diff] [blame] | 192 | func console_16550_core_getc |
| 193 | #if ENABLE_ASSERTIONS |
| 194 | cmp x0, #0 |
| 195 | ASM_ASSERT(ne) |
| 196 | #endif /* ENABLE_ASSERTIONS */ |
| 197 | |
Soby Mathew | 7abebe8 | 2016-08-08 12:38:52 +0100 | [diff] [blame] | 198 | /* Check if the receive FIFO is empty */ |
| 199 | 1: ldr w1, [x0, #UARTLSR] |
Julius Werner | 4e406bf | 2017-09-18 16:57:51 -0700 | [diff] [blame] | 200 | tbz w1, #UARTLSR_RDR_BIT, no_char |
Soby Mathew | 7abebe8 | 2016-08-08 12:38:52 +0100 | [diff] [blame] | 201 | ldr w0, [x0, #UARTRX] |
| 202 | ret |
Julius Werner | 4e406bf | 2017-09-18 16:57:51 -0700 | [diff] [blame] | 203 | no_char: |
| 204 | mov w0, #ERROR_NO_PENDING_CHAR |
Soby Mathew | 7abebe8 | 2016-08-08 12:38:52 +0100 | [diff] [blame] | 205 | ret |
Julius Werner | 4e406bf | 2017-09-18 16:57:51 -0700 | [diff] [blame] | 206 | endfunc console_16550_core_getc |
| 207 | |
| 208 | /* --------------------------------------------- |
Andre Przywara | 98b5a11 | 2020-01-25 00:58:35 +0000 | [diff] [blame] | 209 | * int console_16550_getc(console_t *console) |
Julius Werner | 4e406bf | 2017-09-18 16:57:51 -0700 | [diff] [blame] | 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 | */ |
| 218 | func console_16550_getc |
| 219 | #if ENABLE_ASSERTIONS |
| 220 | cmp x1, #0 |
| 221 | ASM_ASSERT(ne) |
| 222 | #endif /* ENABLE_ASSERTIONS */ |
Andre Przywara | 98b5a11 | 2020-01-25 00:58:35 +0000 | [diff] [blame] | 223 | ldr x0, [x0, #CONSOLE_T_BASE] |
Julius Werner | 4e406bf | 2017-09-18 16:57:51 -0700 | [diff] [blame] | 224 | b console_16550_core_getc |
| 225 | endfunc console_16550_getc |
Antonio Nino Diaz | 56ec1d5 | 2017-02-08 15:58:12 +0000 | [diff] [blame] | 226 | |
| 227 | /* --------------------------------------------- |
Jimmy Brisson | 39f9eee | 2020-08-05 13:44:05 -0500 | [diff] [blame] | 228 | * void console_16550_core_flush(uintptr_t base_addr) |
Antonio Nino Diaz | 56ec1d5 | 2017-02-08 15:58:12 +0000 | [diff] [blame] | 229 | * Function to force a write of all buffered |
| 230 | * data that hasn't been output. |
| 231 | * In : x0 - console base address |
Jimmy Brisson | 39f9eee | 2020-08-05 13:44:05 -0500 | [diff] [blame] | 232 | * Out : void. |
Antonio Nino Diaz | 56ec1d5 | 2017-02-08 15:58:12 +0000 | [diff] [blame] | 233 | * Clobber list : x0, x1 |
| 234 | * --------------------------------------------- |
| 235 | */ |
Antonio Nino Diaz | 659ea8e | 2018-03-22 20:13:44 +0000 | [diff] [blame] | 236 | func 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 */ |
| 243 | 1: 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 Diaz | 56ec1d5 | 2017-02-08 15:58:12 +0000 | [diff] [blame] | 248 | ret |
Antonio Nino Diaz | 659ea8e | 2018-03-22 20:13:44 +0000 | [diff] [blame] | 249 | endfunc console_16550_core_flush |
| 250 | |
| 251 | /* --------------------------------------------- |
Jimmy Brisson | 39f9eee | 2020-08-05 13:44:05 -0500 | [diff] [blame] | 252 | * void console_16550_flush(console_t *console) |
Antonio Nino Diaz | 659ea8e | 2018-03-22 20:13:44 +0000 | [diff] [blame] | 253 | * Function to force a write of all buffered |
| 254 | * data that hasn't been output. |
| 255 | * In : x0 - pointer to console_t structure |
Jimmy Brisson | 39f9eee | 2020-08-05 13:44:05 -0500 | [diff] [blame] | 256 | * Out : void. |
Antonio Nino Diaz | 659ea8e | 2018-03-22 20:13:44 +0000 | [diff] [blame] | 257 | * Clobber list : x0, x1 |
| 258 | * --------------------------------------------- |
| 259 | */ |
| 260 | func console_16550_flush |
| 261 | #if ENABLE_ASSERTIONS |
| 262 | cmp x0, #0 |
| 263 | ASM_ASSERT(ne) |
| 264 | #endif /* ENABLE_ASSERTIONS */ |
Andre Przywara | 98b5a11 | 2020-01-25 00:58:35 +0000 | [diff] [blame] | 265 | ldr x0, [x0, #CONSOLE_T_BASE] |
Antonio Nino Diaz | 659ea8e | 2018-03-22 20:13:44 +0000 | [diff] [blame] | 266 | b console_16550_core_flush |
| 267 | endfunc console_16550_flush |