Heiko Stuebner | 73c3712 | 2019-03-07 10:26:19 +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. |
Heiko Stuebner | 73c3712 | 2019-03-07 10:26:19 +0100 | [diff] [blame] | 3 | * |
| 4 | * SPDX-License-Identifier: BSD-3-Clause |
| 5 | */ |
| 6 | |
| 7 | #include <arch.h> |
| 8 | #include <asm_macros.S> |
| 9 | #include <assert_macros.S> |
| 10 | #include <console_macros.S> |
| 11 | #include <drivers/ti/uart/uart_16550.h> |
| 12 | |
| 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 |
| 20 | .globl console_16550_core_flush |
| 21 | |
| 22 | .globl console_16550_putc |
| 23 | .globl console_16550_getc |
| 24 | .globl console_16550_flush |
| 25 | |
| 26 | /* ----------------------------------------------- |
| 27 | * int console_16550_core_init(uintptr_t base_addr, |
| 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: r0 - console base address |
| 34 | * r1 - Uart clock in Hz |
| 35 | * r2 - Baud rate |
| 36 | * Out: return 1 on success, 0 on error |
| 37 | * Clobber list : r1, r2, r3 |
| 38 | * ----------------------------------------------- |
| 39 | */ |
| 40 | func console_16550_core_init |
| 41 | /* Check the input base address */ |
| 42 | cmp r0, #0 |
| 43 | beq init_fail |
| 44 | /* Check baud rate and uart clock for sanity */ |
| 45 | cmp r1, #0 |
| 46 | beq init_fail |
| 47 | cmp r2, #0 |
| 48 | beq init_fail |
| 49 | |
| 50 | /* Program the baudrate */ |
| 51 | /* Divisor = Uart clock / (16 * baudrate) */ |
| 52 | lsl r2, r2, #4 |
| 53 | udiv r2, r1, r2 |
| 54 | and r1, r2, #0xff /* w1 = DLL */ |
| 55 | lsr r2, r2, #8 |
| 56 | and r2, r2, #0xff /* w2 = DLLM */ |
| 57 | ldr r3, [r0, #UARTLCR] |
| 58 | orr r3, r3, #UARTLCR_DLAB |
| 59 | str r3, [r0, #UARTLCR] /* enable DLL, DLLM programming */ |
| 60 | str r1, [r0, #UARTDLL] /* program DLL */ |
| 61 | str r2, [r0, #UARTDLLM] /* program DLLM */ |
| 62 | mov r2, #~UARTLCR_DLAB |
| 63 | and r3, r3, r2 |
| 64 | str r3, [r0, #UARTLCR] /* disable DLL, DLLM programming */ |
| 65 | |
| 66 | /* 8n1 */ |
| 67 | mov r3, #3 |
| 68 | str r3, [r0, #UARTLCR] |
| 69 | /* no interrupt */ |
| 70 | mov r3, #0 |
| 71 | str r3, [r0, #UARTIER] |
| 72 | #ifdef TI_16550_MDR_QUIRK |
| 73 | /* UART must be enabled on some platforms via the MDR register */ |
| 74 | str r3, [r0, #UARTMDR1] |
| 75 | #endif /* TI_16550_MDR_QUIRK */ |
| 76 | /* enable fifo, DMA */ |
| 77 | mov r3, #(UARTFCR_FIFOEN | UARTFCR_DMAEN) |
| 78 | str r3, [r0, #UARTFCR] |
| 79 | /* DTR + RTS */ |
| 80 | mov r3, #3 |
| 81 | str r3, [r0, #UARTMCR] |
| 82 | mov r0, #1 |
| 83 | bx lr |
| 84 | init_fail: |
| 85 | mov r0, #0 |
| 86 | bx lr |
| 87 | endfunc console_16550_core_init |
| 88 | |
| 89 | .globl console_16550_register |
| 90 | |
| 91 | /* ------------------------------------------------------- |
Andre Przywara | 4049779 | 2019-12-12 12:00:15 +0000 | [diff] [blame] | 92 | * int console_16550_register(uintptr_t baseaddr, |
Heiko Stuebner | 73c3712 | 2019-03-07 10:26:19 +0100 | [diff] [blame] | 93 | * uint32_t clock, uint32_t baud, |
Andre Przywara | 98b5a11 | 2020-01-25 00:58:35 +0000 | [diff] [blame] | 94 | * console_t *console); |
Andre Przywara | 4049779 | 2019-12-12 12:00:15 +0000 | [diff] [blame] | 95 | * Function to initialize and register a new 16550 |
Heiko Stuebner | 73c3712 | 2019-03-07 10:26:19 +0100 | [diff] [blame] | 96 | * console. Storage passed in for the console struct |
| 97 | * *must* be persistent (i.e. not from the stack). |
Andre Przywara | 4049779 | 2019-12-12 12:00:15 +0000 | [diff] [blame] | 98 | * If r1 (UART clock) is 0, initialisation will be |
| 99 | * skipped, relying on previous code to have done |
| 100 | * this already. r2 is ignored then as well. |
Heiko Stuebner | 73c3712 | 2019-03-07 10:26:19 +0100 | [diff] [blame] | 101 | * In: r0 - UART register base address |
| 102 | * r1 - UART clock in Hz |
Andre Przywara | 4049779 | 2019-12-12 12:00:15 +0000 | [diff] [blame] | 103 | * r2 - Baud rate (ignored if r1 is 0) |
Andre Przywara | 98b5a11 | 2020-01-25 00:58:35 +0000 | [diff] [blame] | 104 | * r3 - pointer to empty console_t struct |
Heiko Stuebner | 73c3712 | 2019-03-07 10:26:19 +0100 | [diff] [blame] | 105 | * Out: return 1 on success, 0 on error |
| 106 | * Clobber list : r0, r1, r2 |
| 107 | * ------------------------------------------------------- |
| 108 | */ |
| 109 | func console_16550_register |
| 110 | push {r4, lr} |
| 111 | mov r4, r3 |
| 112 | cmp r4, #0 |
| 113 | beq register_fail |
Andre Przywara | 98b5a11 | 2020-01-25 00:58:35 +0000 | [diff] [blame] | 114 | str r0, [r4, #CONSOLE_T_BASE] |
Heiko Stuebner | 73c3712 | 2019-03-07 10:26:19 +0100 | [diff] [blame] | 115 | |
Andre Przywara | 4049779 | 2019-12-12 12:00:15 +0000 | [diff] [blame] | 116 | /* A clock rate of zero means to skip the initialisation. */ |
| 117 | cmp r1, #0 |
| 118 | beq register_16550 |
| 119 | |
Heiko Stuebner | 73c3712 | 2019-03-07 10:26:19 +0100 | [diff] [blame] | 120 | bl console_16550_core_init |
| 121 | cmp r0, #0 |
| 122 | beq register_fail |
| 123 | |
Andre Przywara | 4049779 | 2019-12-12 12:00:15 +0000 | [diff] [blame] | 124 | register_16550: |
Heiko Stuebner | 73c3712 | 2019-03-07 10:26:19 +0100 | [diff] [blame] | 125 | mov r0, r4 |
| 126 | pop {r4, lr} |
Sandrine Bailleux | f57e203 | 2023-10-11 08:38:00 +0200 | [diff] [blame] | 127 | finish_console_register 16550 putc=1, getc=ENABLE_CONSOLE_GETC, flush=1 |
Heiko Stuebner | 73c3712 | 2019-03-07 10:26:19 +0100 | [diff] [blame] | 128 | |
| 129 | register_fail: |
| 130 | pop {r4, pc} |
| 131 | endfunc console_16550_register |
| 132 | |
| 133 | /* -------------------------------------------------------- |
| 134 | * int console_16550_core_putc(int c, uintptr_t base_addr) |
| 135 | * Function to output a character over the console. It |
| 136 | * returns the character printed on success or -1 on error. |
| 137 | * In : r0 - character to be printed |
| 138 | * r1 - console base address |
| 139 | * Out : return -1 on error else return character. |
| 140 | * Clobber list : r2 |
| 141 | * -------------------------------------------------------- |
| 142 | */ |
| 143 | func console_16550_core_putc |
| 144 | #if ENABLE_ASSERTIONS |
| 145 | cmp r1, #0 |
| 146 | ASM_ASSERT(ne) |
| 147 | #endif /* ENABLE_ASSERTIONS */ |
| 148 | |
| 149 | /* Prepend '\r' to '\n' */ |
| 150 | cmp r0, #0xA |
| 151 | bne 2f |
| 152 | /* Check if the transmit FIFO is full */ |
| 153 | 1: ldr r2, [r1, #UARTLSR] |
| 154 | and r2, r2, #(UARTLSR_TEMT | UARTLSR_THRE) |
| 155 | cmp r2, #(UARTLSR_TEMT | UARTLSR_THRE) |
| 156 | bne 1b |
| 157 | mov r2, #0xD /* '\r' */ |
| 158 | str r2, [r1, #UARTTX] |
| 159 | |
| 160 | /* Check if the transmit FIFO is full */ |
| 161 | 2: ldr r2, [r1, #UARTLSR] |
| 162 | and r2, r2, #(UARTLSR_TEMT | UARTLSR_THRE) |
| 163 | cmp r2, #(UARTLSR_TEMT | UARTLSR_THRE) |
| 164 | bne 2b |
| 165 | str r0, [r1, #UARTTX] |
| 166 | bx lr |
| 167 | endfunc console_16550_core_putc |
| 168 | |
| 169 | /* -------------------------------------------------------- |
Andre Przywara | 98b5a11 | 2020-01-25 00:58:35 +0000 | [diff] [blame] | 170 | * int console_16550_putc(int c, console_t *console) |
Heiko Stuebner | 73c3712 | 2019-03-07 10:26:19 +0100 | [diff] [blame] | 171 | * Function to output a character over the console. It |
| 172 | * returns the character printed on success or -1 on error. |
| 173 | * In : r0 - character to be printed |
| 174 | * r1 - pointer to console_t structure |
| 175 | * Out : return -1 on error else return character. |
| 176 | * Clobber list : r2 |
| 177 | * -------------------------------------------------------- |
| 178 | */ |
| 179 | func console_16550_putc |
| 180 | #if ENABLE_ASSERTIONS |
| 181 | cmp r1, #0 |
| 182 | ASM_ASSERT(ne) |
| 183 | #endif /* ENABLE_ASSERTIONS */ |
Andre Przywara | 98b5a11 | 2020-01-25 00:58:35 +0000 | [diff] [blame] | 184 | ldr r1, [r1, #CONSOLE_T_BASE] |
Heiko Stuebner | 73c3712 | 2019-03-07 10:26:19 +0100 | [diff] [blame] | 185 | b console_16550_core_putc |
| 186 | endfunc console_16550_putc |
| 187 | |
| 188 | /* --------------------------------------------- |
| 189 | * int console_16550_core_getc(uintptr_t base_addr) |
| 190 | * Function to get a character from the console. |
| 191 | * It returns the character grabbed on success |
| 192 | * or -1 on if no character is available. |
| 193 | * In : r0 - console base address |
| 194 | * Clobber list : r0, r1 |
| 195 | * --------------------------------------------- |
| 196 | */ |
| 197 | func console_16550_core_getc |
| 198 | #if ENABLE_ASSERTIONS |
| 199 | cmp r0, #0 |
| 200 | ASM_ASSERT(ne) |
| 201 | #endif /* ENABLE_ASSERTIONS */ |
| 202 | |
| 203 | /* Check if the receive FIFO is empty */ |
| 204 | 1: ldr r1, [r0, #UARTLSR] |
| 205 | tst r1, #UARTLSR_RDR_BIT |
| 206 | beq no_char |
| 207 | ldr r1, [r0, #UARTRX] |
| 208 | mov r0, r1 |
| 209 | bx lr |
| 210 | no_char: |
| 211 | mov r0, #ERROR_NO_PENDING_CHAR |
| 212 | bx lr |
| 213 | endfunc console_16550_core_getc |
| 214 | |
| 215 | /* --------------------------------------------- |
Andre Przywara | 98b5a11 | 2020-01-25 00:58:35 +0000 | [diff] [blame] | 216 | * int console_16550_getc(console_t *console) |
Heiko Stuebner | 73c3712 | 2019-03-07 10:26:19 +0100 | [diff] [blame] | 217 | * Function to get a character from the console. |
| 218 | * It returns the character grabbed on success |
| 219 | * or -1 on if no character is available. |
| 220 | * In : r0 - pointer to console_t stucture |
| 221 | * Out : r0 - character if available, else -1 |
| 222 | * Clobber list : r0, r1 |
| 223 | * --------------------------------------------- |
| 224 | */ |
| 225 | func console_16550_getc |
| 226 | #if ENABLE_ASSERTIONS |
| 227 | cmp r0, #0 |
| 228 | ASM_ASSERT(ne) |
| 229 | #endif /* ENABLE_ASSERTIONS */ |
Andre Przywara | 98b5a11 | 2020-01-25 00:58:35 +0000 | [diff] [blame] | 230 | ldr r0, [r0, #CONSOLE_T_BASE] |
Heiko Stuebner | 73c3712 | 2019-03-07 10:26:19 +0100 | [diff] [blame] | 231 | b console_16550_core_getc |
| 232 | endfunc console_16550_getc |
| 233 | |
| 234 | /* --------------------------------------------- |
Jimmy Brisson | 39f9eee | 2020-08-05 13:44:05 -0500 | [diff] [blame] | 235 | * void console_16550_core_flush(uintptr_t base_addr) |
Heiko Stuebner | 73c3712 | 2019-03-07 10:26:19 +0100 | [diff] [blame] | 236 | * Function to force a write of all buffered |
| 237 | * data that hasn't been output. |
| 238 | * In : r0 - console base address |
Jimmy Brisson | 39f9eee | 2020-08-05 13:44:05 -0500 | [diff] [blame] | 239 | * Out : void. |
Heiko Stuebner | 73c3712 | 2019-03-07 10:26:19 +0100 | [diff] [blame] | 240 | * Clobber list : r0, r1 |
| 241 | * --------------------------------------------- |
| 242 | */ |
| 243 | func console_16550_core_flush |
| 244 | #if ENABLE_ASSERTIONS |
| 245 | cmp r0, #0 |
| 246 | ASM_ASSERT(ne) |
| 247 | #endif /* ENABLE_ASSERTIONS */ |
| 248 | |
| 249 | /* Loop until the transmit FIFO is empty */ |
| 250 | 1: ldr r1, [r0, #UARTLSR] |
| 251 | and r1, r1, #(UARTLSR_TEMT | UARTLSR_THRE) |
| 252 | cmp r1, #(UARTLSR_TEMT | UARTLSR_THRE) |
| 253 | bne 1b |
| 254 | |
Heiko Stuebner | 73c3712 | 2019-03-07 10:26:19 +0100 | [diff] [blame] | 255 | bx lr |
| 256 | endfunc console_16550_core_flush |
| 257 | |
| 258 | /* --------------------------------------------- |
Jimmy Brisson | 39f9eee | 2020-08-05 13:44:05 -0500 | [diff] [blame] | 259 | * void console_16550_flush(console_t *console) |
Heiko Stuebner | 73c3712 | 2019-03-07 10:26:19 +0100 | [diff] [blame] | 260 | * Function to force a write of all buffered |
| 261 | * data that hasn't been output. |
| 262 | * In : r0 - pointer to console_t structure |
Jimmy Brisson | 39f9eee | 2020-08-05 13:44:05 -0500 | [diff] [blame] | 263 | * Out : void |
Heiko Stuebner | 73c3712 | 2019-03-07 10:26:19 +0100 | [diff] [blame] | 264 | * Clobber list : r0, r1 |
| 265 | * --------------------------------------------- |
| 266 | */ |
| 267 | func console_16550_flush |
| 268 | #if ENABLE_ASSERTIONS |
| 269 | cmp r0, #0 |
| 270 | ASM_ASSERT(ne) |
| 271 | #endif /* ENABLE_ASSERTIONS */ |
Andre Przywara | 98b5a11 | 2020-01-25 00:58:35 +0000 | [diff] [blame] | 272 | ldr r0, [r0, #CONSOLE_T_BASE] |
Heiko Stuebner | 73c3712 | 2019-03-07 10:26:19 +0100 | [diff] [blame] | 273 | b console_16550_core_flush |
| 274 | endfunc console_16550_flush |