Heiko Stuebner | 73c3712 | 2019-03-07 10:26:19 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2015-2018, ARM Limited and Contributors. All rights reserved. |
| 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 | /* ------------------------------------------------------- |
| 92 | * int console_stm32_register(uintptr_t baseaddr, |
| 93 | * uint32_t clock, uint32_t baud, |
| 94 | * struct console_stm32 *console); |
| 95 | * Function to initialize and register a new STM32 |
| 96 | * console. Storage passed in for the console struct |
| 97 | * *must* be persistent (i.e. not from the stack). |
| 98 | * In: r0 - UART register base address |
| 99 | * r1 - UART clock in Hz |
| 100 | * r2 - Baud rate |
| 101 | * r3 - pointer to empty console_stm32 struct |
| 102 | * Out: return 1 on success, 0 on error |
| 103 | * Clobber list : r0, r1, r2 |
| 104 | * ------------------------------------------------------- |
| 105 | */ |
| 106 | func console_16550_register |
| 107 | push {r4, lr} |
| 108 | mov r4, r3 |
| 109 | cmp r4, #0 |
| 110 | beq register_fail |
| 111 | str r0, [r4, #CONSOLE_T_16550_BASE] |
| 112 | |
| 113 | bl console_16550_core_init |
| 114 | cmp r0, #0 |
| 115 | beq register_fail |
| 116 | |
| 117 | mov r0, r4 |
| 118 | pop {r4, lr} |
| 119 | finish_console_register 16550 putc=1, getc=1, flush=1 |
| 120 | |
| 121 | register_fail: |
| 122 | pop {r4, pc} |
| 123 | endfunc console_16550_register |
| 124 | |
| 125 | /* -------------------------------------------------------- |
| 126 | * int console_16550_core_putc(int c, uintptr_t base_addr) |
| 127 | * Function to output a character over the console. It |
| 128 | * returns the character printed on success or -1 on error. |
| 129 | * In : r0 - character to be printed |
| 130 | * r1 - console base address |
| 131 | * Out : return -1 on error else return character. |
| 132 | * Clobber list : r2 |
| 133 | * -------------------------------------------------------- |
| 134 | */ |
| 135 | func console_16550_core_putc |
| 136 | #if ENABLE_ASSERTIONS |
| 137 | cmp r1, #0 |
| 138 | ASM_ASSERT(ne) |
| 139 | #endif /* ENABLE_ASSERTIONS */ |
| 140 | |
| 141 | /* Prepend '\r' to '\n' */ |
| 142 | cmp r0, #0xA |
| 143 | bne 2f |
| 144 | /* Check if the transmit FIFO is full */ |
| 145 | 1: ldr r2, [r1, #UARTLSR] |
| 146 | and r2, r2, #(UARTLSR_TEMT | UARTLSR_THRE) |
| 147 | cmp r2, #(UARTLSR_TEMT | UARTLSR_THRE) |
| 148 | bne 1b |
| 149 | mov r2, #0xD /* '\r' */ |
| 150 | str r2, [r1, #UARTTX] |
| 151 | |
| 152 | /* Check if the transmit FIFO is full */ |
| 153 | 2: ldr r2, [r1, #UARTLSR] |
| 154 | and r2, r2, #(UARTLSR_TEMT | UARTLSR_THRE) |
| 155 | cmp r2, #(UARTLSR_TEMT | UARTLSR_THRE) |
| 156 | bne 2b |
| 157 | str r0, [r1, #UARTTX] |
| 158 | bx lr |
| 159 | endfunc console_16550_core_putc |
| 160 | |
| 161 | /* -------------------------------------------------------- |
| 162 | * int console_16550_putc(int c, console_16550_t *console) |
| 163 | * Function to output a character over the console. It |
| 164 | * returns the character printed on success or -1 on error. |
| 165 | * In : r0 - character to be printed |
| 166 | * r1 - pointer to console_t structure |
| 167 | * Out : return -1 on error else return character. |
| 168 | * Clobber list : r2 |
| 169 | * -------------------------------------------------------- |
| 170 | */ |
| 171 | func console_16550_putc |
| 172 | #if ENABLE_ASSERTIONS |
| 173 | cmp r1, #0 |
| 174 | ASM_ASSERT(ne) |
| 175 | #endif /* ENABLE_ASSERTIONS */ |
| 176 | ldr r1, [r1, #CONSOLE_T_16550_BASE] |
| 177 | b console_16550_core_putc |
| 178 | endfunc console_16550_putc |
| 179 | |
| 180 | /* --------------------------------------------- |
| 181 | * int console_16550_core_getc(uintptr_t base_addr) |
| 182 | * Function to get a character from the console. |
| 183 | * It returns the character grabbed on success |
| 184 | * or -1 on if no character is available. |
| 185 | * In : r0 - console base address |
| 186 | * Clobber list : r0, r1 |
| 187 | * --------------------------------------------- |
| 188 | */ |
| 189 | func console_16550_core_getc |
| 190 | #if ENABLE_ASSERTIONS |
| 191 | cmp r0, #0 |
| 192 | ASM_ASSERT(ne) |
| 193 | #endif /* ENABLE_ASSERTIONS */ |
| 194 | |
| 195 | /* Check if the receive FIFO is empty */ |
| 196 | 1: ldr r1, [r0, #UARTLSR] |
| 197 | tst r1, #UARTLSR_RDR_BIT |
| 198 | beq no_char |
| 199 | ldr r1, [r0, #UARTRX] |
| 200 | mov r0, r1 |
| 201 | bx lr |
| 202 | no_char: |
| 203 | mov r0, #ERROR_NO_PENDING_CHAR |
| 204 | bx lr |
| 205 | endfunc console_16550_core_getc |
| 206 | |
| 207 | /* --------------------------------------------- |
| 208 | * int console_16550_getc(console_16550_t *console) |
| 209 | * Function to get a character from the console. |
| 210 | * It returns the character grabbed on success |
| 211 | * or -1 on if no character is available. |
| 212 | * In : r0 - pointer to console_t stucture |
| 213 | * Out : r0 - character if available, else -1 |
| 214 | * Clobber list : r0, r1 |
| 215 | * --------------------------------------------- |
| 216 | */ |
| 217 | func console_16550_getc |
| 218 | #if ENABLE_ASSERTIONS |
| 219 | cmp r0, #0 |
| 220 | ASM_ASSERT(ne) |
| 221 | #endif /* ENABLE_ASSERTIONS */ |
| 222 | ldr r0, [r0, #CONSOLE_T_16550_BASE] |
| 223 | b console_16550_core_getc |
| 224 | endfunc console_16550_getc |
| 225 | |
| 226 | /* --------------------------------------------- |
| 227 | * int console_16550_core_flush(uintptr_t base_addr) |
| 228 | * Function to force a write of all buffered |
| 229 | * data that hasn't been output. |
| 230 | * In : r0 - console base address |
| 231 | * Out : return -1 on error else return 0. |
| 232 | * Clobber list : r0, r1 |
| 233 | * --------------------------------------------- |
| 234 | */ |
| 235 | func console_16550_core_flush |
| 236 | #if ENABLE_ASSERTIONS |
| 237 | cmp r0, #0 |
| 238 | ASM_ASSERT(ne) |
| 239 | #endif /* ENABLE_ASSERTIONS */ |
| 240 | |
| 241 | /* Loop until the transmit FIFO is empty */ |
| 242 | 1: ldr r1, [r0, #UARTLSR] |
| 243 | and r1, r1, #(UARTLSR_TEMT | UARTLSR_THRE) |
| 244 | cmp r1, #(UARTLSR_TEMT | UARTLSR_THRE) |
| 245 | bne 1b |
| 246 | |
| 247 | mov r0, #0 |
| 248 | bx lr |
| 249 | endfunc console_16550_core_flush |
| 250 | |
| 251 | /* --------------------------------------------- |
| 252 | * int console_16550_flush(console_pl011_t *console) |
| 253 | * Function to force a write of all buffered |
| 254 | * data that hasn't been output. |
| 255 | * In : r0 - pointer to console_t structure |
| 256 | * Out : return -1 on error else return 0. |
| 257 | * Clobber list : r0, r1 |
| 258 | * --------------------------------------------- |
| 259 | */ |
| 260 | func console_16550_flush |
| 261 | #if ENABLE_ASSERTIONS |
| 262 | cmp r0, #0 |
| 263 | ASM_ASSERT(ne) |
| 264 | #endif /* ENABLE_ASSERTIONS */ |
| 265 | ldr r0, [r0, #CONSOLE_T_16550_BASE] |
| 266 | b console_16550_core_flush |
| 267 | endfunc console_16550_flush |