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: x0 - console base address 34 * w1 - Uart clock in Hz 35 * w2 - Baud rate 36 * Out: return 1 on success, 0 on error 37 * Clobber list : x1, x2, x3 38 * ----------------------------------------------- 39 */ 40func console_16550_core_init 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] 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 */ 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 80 ret 81init_fail: 82 mov w0, #0 83 ret 84endfunc console_16550_core_init 85 86 .globl console_16550_register 87 88 /* ----------------------------------------------- 89 * int console_16550_register(uintptr_t baseaddr, 90 * uint32_t clock, uint32_t baud, 91 * console_16550_t *console); 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). 95 * In: x0 - UART register base address 96 * w1 - UART clock in Hz 97 * w2 - Baud rate 98 * x3 - pointer to empty console_16550_t struct 99 * Out: return 1 on success, 0 on error 100 * Clobber list : x0, x1, x2, x6, x7, x14 101 * ----------------------------------------------- 102 */ 103func console_16550_register 104 mov x7, x30 105 mov x6, x3 106 cbz x6, register_fail 107 str x0, [x6, #CONSOLE_T_16550_BASE] 108 109 bl console_16550_core_init 110 cbz x0, register_fail 111 112 mov x0, x6 113 mov x30, x7 114 finish_console_register 16550 putc=1, getc=1, flush=1 115 116register_fail: 117 ret x7 118endfunc console_16550_register 119 120 /* -------------------------------------------------------- 121 * int console_16550_core_putc(int c, uintptr_t base_addr) 122 * Function to output a character over the console. It 123 * returns the character printed on success or -1 on error. 124 * In : w0 - character to be printed 125 * x1 - console base address 126 * Out : return -1 on error else return character. 127 * Clobber list : x2 128 * -------------------------------------------------------- 129 */ 130func console_16550_core_putc 131#if ENABLE_ASSERTIONS 132 cmp x1, #0 133 ASM_ASSERT(ne) 134#endif /* ENABLE_ASSERTIONS */ 135 136 /* Prepend '\r' to '\n' */ 137 cmp w0, #0xA 138 b.ne 2f 139 /* Check if the transmit FIFO is full */ 1401: ldr w2, [x1, #UARTLSR] 141 and w2, w2, #(UARTLSR_TEMT | UARTLSR_THRE) 142 cmp w2, #(UARTLSR_TEMT | UARTLSR_THRE) 143 b.ne 1b 144 mov w2, #0xD /* '\r' */ 145 str w2, [x1, #UARTTX] 146 147 /* Check if the transmit FIFO is full */ 1482: ldr w2, [x1, #UARTLSR] 149 and w2, w2, #(UARTLSR_TEMT | UARTLSR_THRE) 150 cmp w2, #(UARTLSR_TEMT | UARTLSR_THRE) 151 b.ne 2b 152 str w0, [x1, #UARTTX] 153 ret 154endfunc console_16550_core_putc 155 156 /* -------------------------------------------------------- 157 * int console_16550_putc(int c, console_16550_t *console) 158 * Function to output a character over the console. It 159 * returns the character printed on success or -1 on error. 160 * In : w0 - character to be printed 161 * x1 - pointer to console_t structure 162 * Out : return -1 on error else return character. 163 * Clobber list : x2 164 * -------------------------------------------------------- 165 */ 166func console_16550_putc 167#if ENABLE_ASSERTIONS 168 cmp x1, #0 169 ASM_ASSERT(ne) 170#endif /* ENABLE_ASSERTIONS */ 171 ldr x1, [x1, #CONSOLE_T_16550_BASE] 172 b console_16550_core_putc 173endfunc console_16550_putc 174 175 /* --------------------------------------------- 176 * int console_16550_core_getc(uintptr_t base_addr) 177 * Function to get a character from the console. 178 * It returns the character grabbed on success 179 * or -1 on if no character is available. 180 * In : x0 - console base address 181 * Out : w0 - character if available, else -1 182 * Clobber list : x0, x1 183 * --------------------------------------------- 184 */ 185func console_16550_core_getc 186#if ENABLE_ASSERTIONS 187 cmp x0, #0 188 ASM_ASSERT(ne) 189#endif /* ENABLE_ASSERTIONS */ 190 191 /* Check if the receive FIFO is empty */ 1921: ldr w1, [x0, #UARTLSR] 193 tbz w1, #UARTLSR_RDR_BIT, no_char 194 ldr w0, [x0, #UARTRX] 195 ret 196no_char: 197 mov w0, #ERROR_NO_PENDING_CHAR 198 ret 199endfunc console_16550_core_getc 200 201 /* --------------------------------------------- 202 * int console_16550_getc(console_16550_t *console) 203 * Function to get a character from the console. 204 * It returns the character grabbed on success 205 * or -1 on if no character is available. 206 * In : x0 - pointer to console_t stucture 207 * Out : w0 - character if available, else -1 208 * Clobber list : x0, x1 209 * --------------------------------------------- 210 */ 211func console_16550_getc 212#if ENABLE_ASSERTIONS 213 cmp x1, #0 214 ASM_ASSERT(ne) 215#endif /* ENABLE_ASSERTIONS */ 216 ldr x0, [x0, #CONSOLE_T_16550_BASE] 217 b console_16550_core_getc 218endfunc console_16550_getc 219 220 /* --------------------------------------------- 221 * int console_16550_core_flush(uintptr_t base_addr) 222 * Function to force a write of all buffered 223 * data that hasn't been output. 224 * In : x0 - console base address 225 * Out : return -1 on error else return 0. 226 * Clobber list : x0, x1 227 * --------------------------------------------- 228 */ 229func console_16550_core_flush 230#if ENABLE_ASSERTIONS 231 cmp x0, #0 232 ASM_ASSERT(ne) 233#endif /* ENABLE_ASSERTIONS */ 234 235 /* Loop until the transmit FIFO is empty */ 2361: ldr w1, [x0, #UARTLSR] 237 and w1, w1, #(UARTLSR_TEMT | UARTLSR_THRE) 238 cmp w1, #(UARTLSR_TEMT | UARTLSR_THRE) 239 b.ne 1b 240 241 mov w0, #0 242 ret 243endfunc console_16550_core_flush 244 245 /* --------------------------------------------- 246 * int console_16550_flush(console_pl011_t *console) 247 * Function to force a write of all buffered 248 * data that hasn't been output. 249 * In : x0 - pointer to console_t structure 250 * Out : return -1 on error else return 0. 251 * Clobber list : x0, x1 252 * --------------------------------------------- 253 */ 254func console_16550_flush 255#if ENABLE_ASSERTIONS 256 cmp x0, #0 257 ASM_ASSERT(ne) 258#endif /* ENABLE_ASSERTIONS */ 259 ldr x0, [x0, #CONSOLE_T_16550_BASE] 260 b console_16550_core_flush 261endfunc console_16550_flush 262