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> |
| 10 | #include <console_macros.S> |
Soby Mathew | 7abebe8 | 2016-08-08 12:38:52 +0100 | [diff] [blame] | 11 | #include <uart_16550.h> |
| 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 | |
| 86 | #if MULTI_CONSOLE_API |
| 87 | .globl console_16550_register |
| 88 | |
| 89 | /* ----------------------------------------------- |
Antonio Nino Diaz | 992c5ac | 2018-10-08 13:26:48 +0100 | [diff] [blame] | 90 | * int console_16550_register(uintptr_t baseaddr, |
| 91 | * uint32_t clock, uint32_t baud, |
| 92 | * console_16550_t *console); |
Julius Werner | 4e406bf | 2017-09-18 16:57:51 -0700 | [diff] [blame] | 93 | * Function to initialize and register a new 16550 |
| 94 | * console. Storage passed in for the console struct |
| 95 | * *must* be persistent (i.e. not from the stack). |
| 96 | * In: x0 - UART register base address |
| 97 | * w1 - UART clock in Hz |
| 98 | * w2 - Baud rate |
| 99 | * x3 - pointer to empty console_16550_t struct |
| 100 | * Out: return 1 on success, 0 on error |
| 101 | * Clobber list : x0, x1, x2, x6, x7, x14 |
| 102 | * ----------------------------------------------- |
| 103 | */ |
| 104 | func console_16550_register |
| 105 | mov x7, x30 |
| 106 | mov x6, x3 |
| 107 | cbz x6, register_fail |
| 108 | str x0, [x6, #CONSOLE_T_16550_BASE] |
| 109 | |
| 110 | bl console_16550_core_init |
| 111 | cbz x0, register_fail |
| 112 | |
| 113 | mov x0, x6 |
| 114 | mov x30, x7 |
| 115 | finish_console_register 16550 |
| 116 | |
| 117 | register_fail: |
| 118 | ret x7 |
| 119 | endfunc console_16550_register |
| 120 | #else |
| 121 | .globl console_core_init |
| 122 | .globl console_core_putc |
| 123 | .globl console_core_getc |
| 124 | .globl console_core_flush |
| 125 | .equ console_core_init,console_16550_core_init |
| 126 | .equ console_core_putc,console_16550_core_putc |
| 127 | .equ console_core_getc,console_16550_core_getc |
Antonio Nino Diaz | 659ea8e | 2018-03-22 20:13:44 +0000 | [diff] [blame] | 128 | .equ console_core_flush,console_16550_core_flush |
Julius Werner | 4e406bf | 2017-09-18 16:57:51 -0700 | [diff] [blame] | 129 | #endif |
Soby Mathew | 7abebe8 | 2016-08-08 12:38:52 +0100 | [diff] [blame] | 130 | |
| 131 | /* -------------------------------------------------------- |
Julius Werner | 4e406bf | 2017-09-18 16:57:51 -0700 | [diff] [blame] | 132 | * int console_16550_core_putc(int c, uintptr_t base_addr) |
Soby Mathew | 7abebe8 | 2016-08-08 12:38:52 +0100 | [diff] [blame] | 133 | * Function to output a character over the console. It |
| 134 | * returns the character printed on success or -1 on error. |
| 135 | * In : w0 - character to be printed |
| 136 | * x1 - console base address |
| 137 | * Out : return -1 on error else return character. |
| 138 | * Clobber list : x2 |
| 139 | * -------------------------------------------------------- |
| 140 | */ |
Julius Werner | 4e406bf | 2017-09-18 16:57:51 -0700 | [diff] [blame] | 141 | func console_16550_core_putc |
| 142 | #if ENABLE_ASSERTIONS |
| 143 | cmp x1, #0 |
| 144 | ASM_ASSERT(ne) |
| 145 | #endif /* ENABLE_ASSERTIONS */ |
Soby Mathew | 7abebe8 | 2016-08-08 12:38:52 +0100 | [diff] [blame] | 146 | |
| 147 | /* Prepend '\r' to '\n' */ |
| 148 | cmp w0, #0xA |
| 149 | b.ne 2f |
| 150 | /* Check if the transmit FIFO is full */ |
| 151 | 1: ldr w2, [x1, #UARTLSR] |
| 152 | and w2, w2, #(UARTLSR_TEMT | UARTLSR_THRE) |
| 153 | cmp w2, #(UARTLSR_TEMT | UARTLSR_THRE) |
| 154 | b.ne 1b |
| 155 | mov w2, #0xD /* '\r' */ |
| 156 | str w2, [x1, #UARTTX] |
Soby Mathew | 7abebe8 | 2016-08-08 12:38:52 +0100 | [diff] [blame] | 157 | |
| 158 | /* Check if the transmit FIFO is full */ |
| 159 | 2: ldr w2, [x1, #UARTLSR] |
| 160 | and w2, w2, #(UARTLSR_TEMT | UARTLSR_THRE) |
| 161 | cmp w2, #(UARTLSR_TEMT | UARTLSR_THRE) |
| 162 | b.ne 2b |
| 163 | str w0, [x1, #UARTTX] |
Soby Mathew | 7abebe8 | 2016-08-08 12:38:52 +0100 | [diff] [blame] | 164 | ret |
Julius Werner | 4e406bf | 2017-09-18 16:57:51 -0700 | [diff] [blame] | 165 | endfunc console_16550_core_putc |
| 166 | |
| 167 | /* -------------------------------------------------------- |
| 168 | * int console_16550_putc(int c, console_16550_t *console) |
| 169 | * Function to output a character over the console. It |
| 170 | * returns the character printed on success or -1 on error. |
| 171 | * In : w0 - character to be printed |
| 172 | * x1 - pointer to console_t structure |
| 173 | * Out : return -1 on error else return character. |
| 174 | * Clobber list : x2 |
| 175 | * -------------------------------------------------------- |
| 176 | */ |
| 177 | func console_16550_putc |
| 178 | #if ENABLE_ASSERTIONS |
| 179 | cmp x1, #0 |
| 180 | ASM_ASSERT(ne) |
| 181 | #endif /* ENABLE_ASSERTIONS */ |
| 182 | ldr x1, [x1, #CONSOLE_T_16550_BASE] |
| 183 | b console_16550_core_putc |
| 184 | endfunc console_16550_putc |
Soby Mathew | 7abebe8 | 2016-08-08 12:38:52 +0100 | [diff] [blame] | 185 | |
| 186 | /* --------------------------------------------- |
Julius Werner | 4e406bf | 2017-09-18 16:57:51 -0700 | [diff] [blame] | 187 | * int console_16550_core_getc(uintptr_t base_addr) |
Soby Mathew | 7abebe8 | 2016-08-08 12:38:52 +0100 | [diff] [blame] | 188 | * Function to get a character from the console. |
| 189 | * It returns the character grabbed on success |
Julius Werner | 4e406bf | 2017-09-18 16:57:51 -0700 | [diff] [blame] | 190 | * or -1 on if no character is available. |
| 191 | * In : x0 - console base address |
| 192 | * Out : w0 - character if available, else -1 |
Soby Mathew | 7abebe8 | 2016-08-08 12:38:52 +0100 | [diff] [blame] | 193 | * Clobber list : x0, x1 |
| 194 | * --------------------------------------------- |
| 195 | */ |
Julius Werner | 4e406bf | 2017-09-18 16:57:51 -0700 | [diff] [blame] | 196 | func console_16550_core_getc |
| 197 | #if ENABLE_ASSERTIONS |
| 198 | cmp x0, #0 |
| 199 | ASM_ASSERT(ne) |
| 200 | #endif /* ENABLE_ASSERTIONS */ |
| 201 | |
Soby Mathew | 7abebe8 | 2016-08-08 12:38:52 +0100 | [diff] [blame] | 202 | /* Check if the receive FIFO is empty */ |
| 203 | 1: ldr w1, [x0, #UARTLSR] |
Julius Werner | 4e406bf | 2017-09-18 16:57:51 -0700 | [diff] [blame] | 204 | tbz w1, #UARTLSR_RDR_BIT, no_char |
Soby Mathew | 7abebe8 | 2016-08-08 12:38:52 +0100 | [diff] [blame] | 205 | ldr w0, [x0, #UARTRX] |
| 206 | ret |
Julius Werner | 4e406bf | 2017-09-18 16:57:51 -0700 | [diff] [blame] | 207 | no_char: |
| 208 | mov w0, #ERROR_NO_PENDING_CHAR |
Soby Mathew | 7abebe8 | 2016-08-08 12:38:52 +0100 | [diff] [blame] | 209 | ret |
Julius Werner | 4e406bf | 2017-09-18 16:57:51 -0700 | [diff] [blame] | 210 | endfunc console_16550_core_getc |
| 211 | |
| 212 | /* --------------------------------------------- |
| 213 | * int console_16550_getc(console_16550_t *console) |
| 214 | * Function to get a character from the console. |
| 215 | * It returns the character grabbed on success |
| 216 | * or -1 on if no character is available. |
| 217 | * In : x0 - pointer to console_t stucture |
| 218 | * Out : w0 - character if available, else -1 |
| 219 | * Clobber list : x0, x1 |
| 220 | * --------------------------------------------- |
| 221 | */ |
| 222 | func console_16550_getc |
| 223 | #if ENABLE_ASSERTIONS |
| 224 | cmp x1, #0 |
| 225 | ASM_ASSERT(ne) |
| 226 | #endif /* ENABLE_ASSERTIONS */ |
| 227 | ldr x0, [x0, #CONSOLE_T_16550_BASE] |
| 228 | b console_16550_core_getc |
| 229 | endfunc console_16550_getc |
Antonio Nino Diaz | 56ec1d5 | 2017-02-08 15:58:12 +0000 | [diff] [blame] | 230 | |
| 231 | /* --------------------------------------------- |
Antonio Nino Diaz | 659ea8e | 2018-03-22 20:13:44 +0000 | [diff] [blame] | 232 | * int console_16550_core_flush(uintptr_t base_addr) |
Antonio Nino Diaz | 56ec1d5 | 2017-02-08 15:58:12 +0000 | [diff] [blame] | 233 | * Function to force a write of all buffered |
| 234 | * data that hasn't been output. |
| 235 | * In : x0 - console base address |
| 236 | * Out : return -1 on error else return 0. |
| 237 | * Clobber list : x0, x1 |
| 238 | * --------------------------------------------- |
| 239 | */ |
Antonio Nino Diaz | 659ea8e | 2018-03-22 20:13:44 +0000 | [diff] [blame] | 240 | func console_16550_core_flush |
| 241 | #if ENABLE_ASSERTIONS |
| 242 | cmp x0, #0 |
| 243 | ASM_ASSERT(ne) |
| 244 | #endif /* ENABLE_ASSERTIONS */ |
| 245 | |
| 246 | /* Loop until the transmit FIFO is empty */ |
| 247 | 1: ldr w1, [x0, #UARTLSR] |
| 248 | and w1, w1, #(UARTLSR_TEMT | UARTLSR_THRE) |
| 249 | cmp w1, #(UARTLSR_TEMT | UARTLSR_THRE) |
| 250 | b.ne 1b |
| 251 | |
Antonio Nino Diaz | 56ec1d5 | 2017-02-08 15:58:12 +0000 | [diff] [blame] | 252 | mov w0, #0 |
| 253 | ret |
Antonio Nino Diaz | 659ea8e | 2018-03-22 20:13:44 +0000 | [diff] [blame] | 254 | endfunc console_16550_core_flush |
| 255 | |
| 256 | /* --------------------------------------------- |
| 257 | * int console_16550_flush(console_pl011_t *console) |
| 258 | * Function to force a write of all buffered |
| 259 | * data that hasn't been output. |
| 260 | * In : x0 - pointer to console_t structure |
| 261 | * Out : return -1 on error else return 0. |
| 262 | * Clobber list : x0, x1 |
| 263 | * --------------------------------------------- |
| 264 | */ |
| 265 | func console_16550_flush |
| 266 | #if ENABLE_ASSERTIONS |
| 267 | cmp x0, #0 |
| 268 | ASM_ASSERT(ne) |
| 269 | #endif /* ENABLE_ASSERTIONS */ |
| 270 | ldr x0, [x0, #CONSOLE_T_16550_BASE] |
| 271 | b console_16550_core_flush |
| 272 | endfunc console_16550_flush |