Soby Mathew | 7abebe8 | 2016-08-08 12:38:52 +0100 | [diff] [blame] | 1 | /* |
Antonio Nino Diaz | 659ea8e | 2018-03-22 20:13:44 +0000 | [diff] [blame] | 2 | * Copyright (c) 2015-2018, 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> |
Soby Mathew | 58873ae | 2018-10-10 16:03:09 +0100 | [diff] [blame] | 10 | #define USE_FINISH_CONSOLE_REG_2 |
Julius Werner | 4e406bf | 2017-09-18 16:57:51 -0700 | [diff] [blame] | 11 | #include <console_macros.S> |
Antonio Nino Diaz | e0f9063 | 2018-12-14 00:18:21 +0000 | [diff] [blame] | 12 | #include <drivers/ti/uart/uart_16550.h> |
Soby Mathew | 7abebe8 | 2016-08-08 12:38:52 +0100 | [diff] [blame] | 13 | |
Julius Werner | 4e406bf | 2017-09-18 16:57:51 -0700 | [diff] [blame] | 14 | /* |
| 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 Diaz | 659ea8e | 2018-03-22 20:13:44 +0000 | [diff] [blame] | 21 | .globl console_16550_core_flush |
Julius Werner | 4e406bf | 2017-09-18 16:57:51 -0700 | [diff] [blame] | 22 | |
| 23 | .globl console_16550_putc |
| 24 | .globl console_16550_getc |
Antonio Nino Diaz | 659ea8e | 2018-03-22 20:13:44 +0000 | [diff] [blame] | 25 | .globl console_16550_flush |
Soby Mathew | 7abebe8 | 2016-08-08 12:38:52 +0100 | [diff] [blame] | 26 | |
| 27 | /* ----------------------------------------------- |
Julius Werner | 4e406bf | 2017-09-18 16:57:51 -0700 | [diff] [blame] | 28 | * int console_16550_core_init(uintptr_t base_addr, |
Soby Mathew | 7abebe8 | 2016-08-08 12:38:52 +0100 | [diff] [blame] | 29 | * 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 Werner | 4e406bf | 2017-09-18 16:57:51 -0700 | [diff] [blame] | 37 | * Out: return 1 on success, 0 on error |
Soby Mathew | 7abebe8 | 2016-08-08 12:38:52 +0100 | [diff] [blame] | 38 | * Clobber list : x1, x2, x3 |
| 39 | * ----------------------------------------------- |
| 40 | */ |
Julius Werner | 4e406bf | 2017-09-18 16:57:51 -0700 | [diff] [blame] | 41 | func console_16550_core_init |
Soby Mathew | 7abebe8 | 2016-08-08 12:38:52 +0100 | [diff] [blame] | 42 | /* 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 Fair | 4d5853d | 2016-10-14 01:13:33 +0000 | [diff] [blame] | 70 | #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 Mathew | 7abebe8 | 2016-08-08 12:38:52 +0100 | [diff] [blame] | 74 | /* 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 Werner | 4e406bf | 2017-09-18 16:57:51 -0700 | [diff] [blame] | 81 | ret |
Soby Mathew | 7abebe8 | 2016-08-08 12:38:52 +0100 | [diff] [blame] | 82 | init_fail: |
Julius Werner | 4e406bf | 2017-09-18 16:57:51 -0700 | [diff] [blame] | 83 | mov w0, #0 |
Soby Mathew | 7abebe8 | 2016-08-08 12:38:52 +0100 | [diff] [blame] | 84 | ret |
Julius Werner | 4e406bf | 2017-09-18 16:57:51 -0700 | [diff] [blame] | 85 | endfunc console_16550_core_init |
| 86 | |
| 87 | #if MULTI_CONSOLE_API |
| 88 | .globl console_16550_register |
| 89 | |
| 90 | /* ----------------------------------------------- |
Antonio Nino Diaz | 992c5ac | 2018-10-08 13:26:48 +0100 | [diff] [blame] | 91 | * int console_16550_register(uintptr_t baseaddr, |
| 92 | * uint32_t clock, uint32_t baud, |
| 93 | * console_16550_t *console); |
Julius Werner | 4e406bf | 2017-09-18 16:57:51 -0700 | [diff] [blame] | 94 | * 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 | */ |
| 105 | func 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 Mathew | 58873ae | 2018-10-10 16:03:09 +0100 | [diff] [blame] | 116 | finish_console_register 16550 putc=1, getc=1, flush=1 |
Julius Werner | 4e406bf | 2017-09-18 16:57:51 -0700 | [diff] [blame] | 117 | |
| 118 | register_fail: |
| 119 | ret x7 |
| 120 | endfunc 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 Diaz | 659ea8e | 2018-03-22 20:13:44 +0000 | [diff] [blame] | 129 | .equ console_core_flush,console_16550_core_flush |
Julius Werner | 4e406bf | 2017-09-18 16:57:51 -0700 | [diff] [blame] | 130 | #endif |
Soby Mathew | 7abebe8 | 2016-08-08 12:38:52 +0100 | [diff] [blame] | 131 | |
| 132 | /* -------------------------------------------------------- |
Julius Werner | 4e406bf | 2017-09-18 16:57:51 -0700 | [diff] [blame] | 133 | * int console_16550_core_putc(int c, uintptr_t base_addr) |
Soby Mathew | 7abebe8 | 2016-08-08 12:38:52 +0100 | [diff] [blame] | 134 | * 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 Werner | 4e406bf | 2017-09-18 16:57:51 -0700 | [diff] [blame] | 142 | func console_16550_core_putc |
| 143 | #if ENABLE_ASSERTIONS |
| 144 | cmp x1, #0 |
| 145 | ASM_ASSERT(ne) |
| 146 | #endif /* ENABLE_ASSERTIONS */ |
Soby Mathew | 7abebe8 | 2016-08-08 12:38:52 +0100 | [diff] [blame] | 147 | |
| 148 | /* Prepend '\r' to '\n' */ |
| 149 | cmp w0, #0xA |
| 150 | b.ne 2f |
| 151 | /* Check if the transmit FIFO is full */ |
| 152 | 1: 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 Mathew | 7abebe8 | 2016-08-08 12:38:52 +0100 | [diff] [blame] | 158 | |
| 159 | /* Check if the transmit FIFO is full */ |
| 160 | 2: 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 Mathew | 7abebe8 | 2016-08-08 12:38:52 +0100 | [diff] [blame] | 165 | ret |
Julius Werner | 4e406bf | 2017-09-18 16:57:51 -0700 | [diff] [blame] | 166 | endfunc 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 | */ |
| 178 | func 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 |
| 185 | endfunc console_16550_putc |
Soby Mathew | 7abebe8 | 2016-08-08 12:38:52 +0100 | [diff] [blame] | 186 | |
| 187 | /* --------------------------------------------- |
Julius Werner | 4e406bf | 2017-09-18 16:57:51 -0700 | [diff] [blame] | 188 | * int console_16550_core_getc(uintptr_t base_addr) |
Soby Mathew | 7abebe8 | 2016-08-08 12:38:52 +0100 | [diff] [blame] | 189 | * Function to get a character from the console. |
| 190 | * It returns the character grabbed on success |
Julius Werner | 4e406bf | 2017-09-18 16:57:51 -0700 | [diff] [blame] | 191 | * or -1 on if no character is available. |
| 192 | * In : x0 - console base address |
| 193 | * Out : w0 - character if available, else -1 |
Soby Mathew | 7abebe8 | 2016-08-08 12:38:52 +0100 | [diff] [blame] | 194 | * Clobber list : x0, x1 |
| 195 | * --------------------------------------------- |
| 196 | */ |
Julius Werner | 4e406bf | 2017-09-18 16:57:51 -0700 | [diff] [blame] | 197 | func console_16550_core_getc |
| 198 | #if ENABLE_ASSERTIONS |
| 199 | cmp x0, #0 |
| 200 | ASM_ASSERT(ne) |
| 201 | #endif /* ENABLE_ASSERTIONS */ |
| 202 | |
Soby Mathew | 7abebe8 | 2016-08-08 12:38:52 +0100 | [diff] [blame] | 203 | /* Check if the receive FIFO is empty */ |
| 204 | 1: ldr w1, [x0, #UARTLSR] |
Julius Werner | 4e406bf | 2017-09-18 16:57:51 -0700 | [diff] [blame] | 205 | tbz w1, #UARTLSR_RDR_BIT, no_char |
Soby Mathew | 7abebe8 | 2016-08-08 12:38:52 +0100 | [diff] [blame] | 206 | ldr w0, [x0, #UARTRX] |
| 207 | ret |
Julius Werner | 4e406bf | 2017-09-18 16:57:51 -0700 | [diff] [blame] | 208 | no_char: |
| 209 | mov w0, #ERROR_NO_PENDING_CHAR |
Soby Mathew | 7abebe8 | 2016-08-08 12:38:52 +0100 | [diff] [blame] | 210 | ret |
Julius Werner | 4e406bf | 2017-09-18 16:57:51 -0700 | [diff] [blame] | 211 | endfunc 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 | */ |
| 223 | func 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 |
| 230 | endfunc console_16550_getc |
Antonio Nino Diaz | 56ec1d5 | 2017-02-08 15:58:12 +0000 | [diff] [blame] | 231 | |
| 232 | /* --------------------------------------------- |
Antonio Nino Diaz | 659ea8e | 2018-03-22 20:13:44 +0000 | [diff] [blame] | 233 | * int console_16550_core_flush(uintptr_t base_addr) |
Antonio Nino Diaz | 56ec1d5 | 2017-02-08 15:58:12 +0000 | [diff] [blame] | 234 | * 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 Diaz | 659ea8e | 2018-03-22 20:13:44 +0000 | [diff] [blame] | 241 | func 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 */ |
| 248 | 1: 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 Diaz | 56ec1d5 | 2017-02-08 15:58:12 +0000 | [diff] [blame] | 253 | mov w0, #0 |
| 254 | ret |
Antonio Nino Diaz | 659ea8e | 2018-03-22 20:13:44 +0000 | [diff] [blame] | 255 | endfunc 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 | */ |
| 266 | func 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 |
| 273 | endfunc console_16550_flush |