1/* 2 * Copyright (c) 2013-2019, ARM Limited and Contributors. All rights reserved. 3 * 4 * SPDX-License-Identifier: BSD-3-Clause 5 */ 6#include <arch.h> 7#include <asm_macros.S> 8#include <assert_macros.S> 9#include <console_macros.S> 10#include <drivers/arm/pl011.h> 11 12 /* 13 * "core" functions are low-level implementations that don't require 14 * writable memory and are thus safe to call in BL1 crash context. 15 */ 16 .globl console_pl011_core_init 17 .globl console_pl011_core_putc 18 .globl console_pl011_core_getc 19 .globl console_pl011_core_flush 20 21 .globl console_pl011_putc 22 .globl console_pl011_getc 23 .globl console_pl011_flush 24 25 /* ----------------------------------------------- 26 * int console_pl011_core_init(uintptr_t base_addr, 27 * unsigned int uart_clk, unsigned int baud_rate) 28 * Function to initialize the console without a 29 * C Runtime to print debug information. This 30 * function will be accessed by console_init and 31 * crash reporting. 32 * In: x0 - console base address 33 * w1 - Uart clock in Hz 34 * w2 - Baud rate 35 * Out: return 1 on success else 0 on error 36 * Clobber list : x1, x2, x3, x4 37 * ----------------------------------------------- 38 */ 39func console_pl011_core_init 40 /* Check the input base address */ 41 cbz x0, core_init_fail 42#if !PL011_GENERIC_UART 43 /* Check baud rate and uart clock for sanity */ 44 cbz w1, core_init_fail 45 cbz w2, core_init_fail 46 /* Disable uart before programming */ 47 ldr w3, [x0, #UARTCR] 48 mov w4, #PL011_UARTCR_UARTEN 49 bic w3, w3, w4 50 str w3, [x0, #UARTCR] 51 /* Program the baudrate */ 52 /* Divisor = (Uart clock * 4) / baudrate */ 53 lsl w1, w1, #2 54 udiv w2, w1, w2 55 /* IBRD = Divisor >> 6 */ 56 lsr w1, w2, #6 57 /* Write the IBRD */ 58 str w1, [x0, #UARTIBRD] 59 /* FBRD = Divisor & 0x3F */ 60 and w1, w2, #0x3f 61 /* Write the FBRD */ 62 str w1, [x0, #UARTFBRD] 63 mov w1, #PL011_LINE_CONTROL 64 str w1, [x0, #UARTLCR_H] 65 /* Clear any pending errors */ 66 str wzr, [x0, #UARTECR] 67 /* Enable tx, rx, and uart overall */ 68 mov w1, #(PL011_UARTCR_RXE | PL011_UARTCR_TXE | PL011_UARTCR_UARTEN) 69 str w1, [x0, #UARTCR] 70#endif 71 mov w0, #1 72 ret 73core_init_fail: 74 mov w0, wzr 75 ret 76endfunc console_pl011_core_init 77 78#if MULTI_CONSOLE_API 79 .globl console_pl011_register 80 81 /* ----------------------------------------------- 82 * int console_pl011_register(uintptr_t baseaddr, 83 * uint32_t clock, uint32_t baud, 84 * console_pl011_t *console); 85 * Function to initialize and register a new PL011 86 * console. Storage passed in for the console struct 87 * *must* be persistent (i.e. not from the stack). 88 * In: x0 - UART register base address 89 * w1 - UART clock in Hz 90 * w2 - Baud rate 91 * x3 - pointer to empty console_pl011_t struct 92 * Out: return 1 on success, 0 on error 93 * Clobber list : x0, x1, x2, x6, x7, x14 94 * ----------------------------------------------- 95 */ 96func console_pl011_register 97 mov x7, x30 98 mov x6, x3 99 cbz x6, register_fail 100 str x0, [x6, #CONSOLE_T_PL011_BASE] 101 102 bl console_pl011_core_init 103 cbz x0, register_fail 104 105 mov x0, x6 106 mov x30, x7 107 finish_console_register pl011 putc=1, getc=1, flush=1 108 109register_fail: 110 ret x7 111endfunc console_pl011_register 112#else 113 .globl console_core_init 114 .globl console_core_putc 115 .globl console_core_getc 116 .globl console_core_flush 117 .equ console_core_init,console_pl011_core_init 118 .equ console_core_putc,console_pl011_core_putc 119 .equ console_core_getc,console_pl011_core_getc 120 .equ console_core_flush,console_pl011_core_flush 121#endif 122 123 /* -------------------------------------------------------- 124 * int console_pl011_core_putc(int c, uintptr_t base_addr) 125 * Function to output a character over the console. It 126 * returns the character printed on success or -1 on error. 127 * In : w0 - character to be printed 128 * x1 - console base address 129 * Out : return -1 on error else return character. 130 * Clobber list : x2 131 * -------------------------------------------------------- 132 */ 133func console_pl011_core_putc 134#if ENABLE_ASSERTIONS 135 cmp x1, #0 136 ASM_ASSERT(ne) 137#endif /* ENABLE_ASSERTIONS */ 138 139 /* Prepend '\r' to '\n' */ 140 cmp w0, #0xA 141 b.ne 2f 1421: 143 /* Check if the transmit FIFO is full */ 144 ldr w2, [x1, #UARTFR] 145 tbnz w2, #PL011_UARTFR_TXFF_BIT, 1b 146 mov w2, #0xD 147 str w2, [x1, #UARTDR] 1482: 149 /* Check if the transmit FIFO is full */ 150 ldr w2, [x1, #UARTFR] 151 tbnz w2, #PL011_UARTFR_TXFF_BIT, 2b 152 str w0, [x1, #UARTDR] 153 ret 154endfunc console_pl011_core_putc 155 156 /* -------------------------------------------------------- 157 * int console_pl011_putc(int c, console_pl011_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_pl011_putc 167#if ENABLE_ASSERTIONS 168 cmp x1, #0 169 ASM_ASSERT(ne) 170#endif /* ENABLE_ASSERTIONS */ 171 ldr x1, [x1, #CONSOLE_T_PL011_BASE] 172 b console_pl011_core_putc 173endfunc console_pl011_putc 174 175 /* --------------------------------------------- 176 * int console_pl011_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 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_pl011_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 */ 192 ldr w1, [x0, #UARTFR] 193 tbnz w1, #PL011_UARTFR_RXFE_BIT, no_char 194 ldr w1, [x0, #UARTDR] 195 mov w0, w1 196 ret 197no_char: 198 mov w0, #ERROR_NO_PENDING_CHAR 199 ret 200endfunc console_pl011_core_getc 201 202 /* --------------------------------------------- 203 * int console_pl011_getc(console_pl011_t *console) 204 * Function to get a character from the console. 205 * It returns the character grabbed on success 206 * or -1 if no character is available. 207 * In : x0 - pointer to console_t structure 208 * Out: w0 - character if available, else -1 209 * Clobber list : x0, x1 210 * --------------------------------------------- 211 */ 212func console_pl011_getc 213#if ENABLE_ASSERTIONS 214 cmp x0, #0 215 ASM_ASSERT(ne) 216#endif /* ENABLE_ASSERTIONS */ 217 ldr x0, [x0, #CONSOLE_T_PL011_BASE] 218 b console_pl011_core_getc 219endfunc console_pl011_getc 220 221 /* --------------------------------------------- 222 * int console_pl011_core_flush(uintptr_t base_addr) 223 * Function to force a write of all buffered 224 * data that hasn't been output. 225 * In : x0 - console base address 226 * Out : return -1 on error else return 0. 227 * Clobber list : x0, x1 228 * --------------------------------------------- 229 */ 230func console_pl011_core_flush 231#if ENABLE_ASSERTIONS 232 cmp x0, #0 233 ASM_ASSERT(ne) 234#endif /* ENABLE_ASSERTIONS */ 2351: 236 /* Loop until the transmit FIFO is empty */ 237 ldr w1, [x0, #UARTFR] 238 tbnz w1, #PL011_UARTFR_BUSY_BIT, 1b 239 240 mov w0, #0 241 ret 242endfunc console_pl011_core_flush 243 244 /* --------------------------------------------- 245 * int console_pl011_flush(console_pl011_t *console) 246 * Function to force a write of all buffered 247 * data that hasn't been output. 248 * In : x0 - pointer to console_t structure 249 * Out : return -1 on error else return 0. 250 * Clobber list : x0, x1 251 * --------------------------------------------- 252 */ 253func console_pl011_flush 254#if ENABLE_ASSERTIONS 255 cmp x0, #0 256 ASM_ASSERT(ne) 257#endif /* ENABLE_ASSERTIONS */ 258 ldr x0, [x0, #CONSOLE_T_PL011_BASE] 259 b console_pl011_core_flush 260endfunc console_pl011_flush 261