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 <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 /* enable fifo, DMA */ 70 mov w3, #(UARTFCR_FIFOEN | UARTFCR_DMAEN) 71 str w3, [x0, #UARTFCR] 72 /* DTR + RTS */ 73 mov w3, #3 74 str w3, [x0, #UARTMCR] 75 mov w0, #1 76 ret 77init_fail: 78 mov w0, #0 79 ret 80endfunc console_16550_core_init 81 82#if MULTI_CONSOLE_API 83 .globl console_16550_register 84 85 /* ----------------------------------------------- 86 * int console_16550_register(console_16550_t *console, 87 uintptr_t base, uint32_t clk, uint32_t baud) 88 * Function to initialize and register a new 16550 89 * console. Storage passed in for the console struct 90 * *must* be persistent (i.e. not from the stack). 91 * In: x0 - UART register base address 92 * w1 - UART clock in Hz 93 * w2 - Baud rate 94 * x3 - pointer to empty console_16550_t struct 95 * Out: return 1 on success, 0 on error 96 * Clobber list : x0, x1, x2, x6, x7, x14 97 * ----------------------------------------------- 98 */ 99func console_16550_register 100 mov x7, x30 101 mov x6, x3 102 cbz x6, register_fail 103 str x0, [x6, #CONSOLE_T_16550_BASE] 104 105 bl console_16550_core_init 106 cbz x0, register_fail 107 108 mov x0, x6 109 mov x30, x7 110 finish_console_register 16550 111 112register_fail: 113 ret x7 114endfunc console_16550_register 115#else 116 .globl console_core_init 117 .globl console_core_putc 118 .globl console_core_getc 119 .globl console_core_flush 120 .equ console_core_init,console_16550_core_init 121 .equ console_core_putc,console_16550_core_putc 122 .equ console_core_getc,console_16550_core_getc 123 .equ console_core_flush,console_16550_core_flush 124#endif 125 126 /* -------------------------------------------------------- 127 * int console_16550_core_putc(int c, uintptr_t base_addr) 128 * Function to output a character over the console. It 129 * returns the character printed on success or -1 on error. 130 * In : w0 - character to be printed 131 * x1 - console base address 132 * Out : return -1 on error else return character. 133 * Clobber list : x2 134 * -------------------------------------------------------- 135 */ 136func console_16550_core_putc 137#if ENABLE_ASSERTIONS 138 cmp x1, #0 139 ASM_ASSERT(ne) 140#endif /* ENABLE_ASSERTIONS */ 141 142 /* Prepend '\r' to '\n' */ 143 cmp w0, #0xA 144 b.ne 2f 145 /* Check if the transmit FIFO is full */ 1461: ldr w2, [x1, #UARTLSR] 147 and w2, w2, #(UARTLSR_TEMT | UARTLSR_THRE) 148 cmp w2, #(UARTLSR_TEMT | UARTLSR_THRE) 149 b.ne 1b 150 mov w2, #0xD /* '\r' */ 151 str w2, [x1, #UARTTX] 152 153 /* Check if the transmit FIFO is full */ 1542: ldr w2, [x1, #UARTLSR] 155 and w2, w2, #(UARTLSR_TEMT | UARTLSR_THRE) 156 cmp w2, #(UARTLSR_TEMT | UARTLSR_THRE) 157 b.ne 2b 158 str w0, [x1, #UARTTX] 159 ret 160endfunc console_16550_core_putc 161 162 /* -------------------------------------------------------- 163 * int console_16550_putc(int c, console_16550_t *console) 164 * Function to output a character over the console. It 165 * returns the character printed on success or -1 on error. 166 * In : w0 - character to be printed 167 * x1 - pointer to console_t structure 168 * Out : return -1 on error else return character. 169 * Clobber list : x2 170 * -------------------------------------------------------- 171 */ 172func console_16550_putc 173#if ENABLE_ASSERTIONS 174 cmp x1, #0 175 ASM_ASSERT(ne) 176#endif /* ENABLE_ASSERTIONS */ 177 ldr x1, [x1, #CONSOLE_T_16550_BASE] 178 b console_16550_core_putc 179endfunc console_16550_putc 180 181 /* --------------------------------------------- 182 * int console_16550_core_getc(uintptr_t base_addr) 183 * Function to get a character from the console. 184 * It returns the character grabbed on success 185 * or -1 on if no character is available. 186 * In : x0 - console base address 187 * Out : w0 - character if available, else -1 188 * Clobber list : x0, x1 189 * --------------------------------------------- 190 */ 191func console_16550_core_getc 192#if ENABLE_ASSERTIONS 193 cmp x0, #0 194 ASM_ASSERT(ne) 195#endif /* ENABLE_ASSERTIONS */ 196 197 /* Check if the receive FIFO is empty */ 1981: ldr w1, [x0, #UARTLSR] 199 tbz w1, #UARTLSR_RDR_BIT, no_char 200 ldr w0, [x0, #UARTRX] 201 ret 202no_char: 203 mov w0, #ERROR_NO_PENDING_CHAR 204 ret 205endfunc 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 : x0 - pointer to console_t stucture 213 * Out : w0 - character if available, else -1 214 * Clobber list : x0, x1 215 * --------------------------------------------- 216 */ 217func console_16550_getc 218#if ENABLE_ASSERTIONS 219 cmp x1, #0 220 ASM_ASSERT(ne) 221#endif /* ENABLE_ASSERTIONS */ 222 ldr x0, [x0, #CONSOLE_T_16550_BASE] 223 b console_16550_core_getc 224endfunc 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 : x0 - console base address 231 * Out : return -1 on error else return 0. 232 * Clobber list : x0, x1 233 * --------------------------------------------- 234 */ 235func console_16550_core_flush 236#if ENABLE_ASSERTIONS 237 cmp x0, #0 238 ASM_ASSERT(ne) 239#endif /* ENABLE_ASSERTIONS */ 240 241 /* Loop until the transmit FIFO is empty */ 2421: ldr w1, [x0, #UARTLSR] 243 and w1, w1, #(UARTLSR_TEMT | UARTLSR_THRE) 244 cmp w1, #(UARTLSR_TEMT | UARTLSR_THRE) 245 b.ne 1b 246 247 mov w0, #0 248 ret 249endfunc 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 : x0 - pointer to console_t structure 256 * Out : return -1 on error else return 0. 257 * Clobber list : x0, x1 258 * --------------------------------------------- 259 */ 260func console_16550_flush 261#if ENABLE_ASSERTIONS 262 cmp x0, #0 263 ASM_ASSERT(ne) 264#endif /* ENABLE_ASSERTIONS */ 265 ldr x0, [x0, #CONSOLE_T_16550_BASE] 266 b console_16550_core_flush 267endfunc console_16550_flush 268