1/* 2 * Copyright (C) 2016 Marvell International Ltd. 3 * 4 * SPDX-License-Identifier: BSD-3-Clause 5 * https://spdx.org/licenses 6 */ 7 8#include <arch.h> 9#include <asm_macros.S> 10#include <console_macros.S> 11#include <drivers/marvell/uart/a3700_console.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_a3700_core_putc 18 .globl console_a3700_core_init 19 .globl console_a3700_core_getc 20 .globl console_a3700_core_flush 21 22 .globl console_a3700_putc 23 .globl console_a3700_getc 24 .globl console_a3700_flush 25 26 /* ----------------------------------------------- 27 * int console_a3700_core_init(unsigned long 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 37 * Clobber list : x1, x2, x3 38 * ----------------------------------------------- 39 */ 40func console_a3700_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 w2, w2, #0x3ff 52 53 ldr w3, [x0, #UART_BAUD_REG] 54 bic w3, w3, 0x3ff 55 orr w3, w3, w2 56 str w3, [x0, #UART_BAUD_REG]/* set baud rate divisor */ 57 58 /* Set UART to default 16X scheme */ 59 mov w3, #0 60 str w3, [x0, #UART_POSSR_REG] 61 62 /* 63 * Wait for the TX (THR and TSR) to be empty. If wait for 20ms, the TX FIFO is 64 * still not empty, TX FIFO will reset by all means. 65 */ 66 mov w1, #20 /* max time out 20ms */ 672: 68 /* Check whether TX (THR and TSR) is empty */ 69 ldr w3, [x0, #UART_STATUS_REG] 70 and w3, w3, #UARTLSR_TXEMPTY 71 cmp w3, #0 72 b.ne 4f 73 74 /* Delay */ 75 mov w2, #30000 763: 77 sub w2, w2, #1 78 cmp w2, #0 79 b.ne 3b 80 81 /* Check whether 10ms is waited */ 82 sub w1, w1, #1 83 cmp w1, #0 84 b.ne 2b 85 864: 87 /* Reset FIFO */ 88 mov w3, #UART_CTRL_RXFIFO_RESET 89 orr w3, w3, #UART_CTRL_TXFIFO_RESET 90 str w3, [x0, #UART_CTRL_REG] 91 92 /* Delay */ 93 mov w2, #2000 941: 95 sub w2, w2, #1 96 cmp w2, #0 97 b.ne 1b 98 99 /* No Parity, 1 Stop */ 100 mov w3, #0 101 str w3, [x0, #UART_CTRL_REG] 102 103 mov w0, #1 104 ret 105init_fail: 106 mov w0, #0 107 ret 108endfunc console_a3700_core_init 109 110 .globl console_a3700_register 111 112 /* ----------------------------------------------- 113 * int console_a3700_register(console_t *console, 114 uintptr_t base, uint32_t clk, uint32_t baud) 115 * Function to initialize and register a new a3700 116 * console. Storage passed in for the console struct 117 * *must* be persistent (i.e. not from the stack). 118 * In: x0 - UART register base address 119 * w1 - UART clock in Hz 120 * w2 - Baud rate 121 * x3 - pointer to empty console_t struct 122 * Out: return 1 on success, 0 on error 123 * Clobber list : x0, x1, x2, x6, x7, x14 124 * ----------------------------------------------- 125 */ 126func console_a3700_register 127 mov x7, x30 128 mov x6, x3 129 cbz x6, register_fail 130 str x0, [x6, #CONSOLE_T_BASE] 131 132 bl console_a3700_core_init 133 cbz x0, register_fail 134 135 mov x0, x6 136 mov x30, x7 137 finish_console_register a3700, putc=1, getc=1, flush=1 138 139register_fail: 140 ret x7 141endfunc console_a3700_register 142 143 /* -------------------------------------------------------- 144 * int console_a3700_core_putc(int c, unsigned int base_addr) 145 * Function to output a character over the console. It 146 * returns the character printed on success or -1 on error. 147 * In : w0 - character to be printed 148 * x1 - console base address 149 * Out : return -1 on error else return character. 150 * Clobber list : x2 151 * -------------------------------------------------------- 152 */ 153func console_a3700_core_putc 154 /* Check the input parameter */ 155 cbz x1, putc_error 156 157 /* Prepend '\r' to '\n' */ 158 cmp w0, #0xA 159 b.ne 2f 160 /* Check if the transmit FIFO is full */ 1611: ldr w2, [x1, #UART_STATUS_REG] 162 and w2, w2, #UARTLSR_TXFIFOFULL 163 cmp w2, #UARTLSR_TXFIFOFULL 164 b.eq 1b 165 mov w2, #0xD /* '\r' */ 166 str w2, [x1, #UART_TX_REG] 167 168 /* Check if the transmit FIFO is full */ 1692: ldr w2, [x1, #UART_STATUS_REG] 170 and w2, w2, #UARTLSR_TXFIFOFULL 171 cmp w2, #UARTLSR_TXFIFOFULL 172 b.eq 2b 173 str w0, [x1, #UART_TX_REG] 174 ret 175putc_error: 176 mov w0, #-1 177 ret 178endfunc console_a3700_core_putc 179 180 /* -------------------------------------------------------- 181 * int console_a3700_putc(int c, console_t *console) 182 * Function to output a character over the console. It 183 * returns the character printed on success or -1 on error. 184 * In : w0 - character to be printed 185 * x1 - pointer to console_t structure 186 * Out : return -1 on error else return character. 187 * Clobber list : x2 188 * -------------------------------------------------------- 189 */ 190func console_a3700_putc 191 ldr x1, [x1, #CONSOLE_T_BASE] 192 b console_a3700_core_putc 193endfunc console_a3700_putc 194 195 /* --------------------------------------------- 196 * int console_a3700_core_getc(void) 197 * Function to get a character from the console. 198 * It returns the character grabbed on success 199 * or -1 if no character is available. 200 * In : w0 - console base address 201 * Out : w0 - character if available, else -1 202 * Clobber list : x0, x1 203 * --------------------------------------------- 204 */ 205func console_a3700_core_getc 206 /* Check if there is a pending character */ 207 ldr w1, [x0, #UART_STATUS_REG] 208 and w1, w1, #UARTLSR_RXRDY 209 cmp w1, #UARTLSR_RXRDY 210 b.ne getc_no_char 211 ldr w0, [x0, #UART_RX_REG] 212 and w0, w0, #0xff 213 ret 214getc_no_char: 215 mov w0, #ERROR_NO_PENDING_CHAR 216 ret 217endfunc console_a3700_core_getc 218 219 /* --------------------------------------------- 220 * int console_a3700_getc(console_t *console) 221 * Function to get a character from the console. 222 * It returns the character grabbed on success 223 * or -1 on if no character is available. 224 * In : x0 - pointer to console_t structure 225 * Out : w0 - character if available, else -1 226 * Clobber list : x0, x1 227 * --------------------------------------------- 228 */ 229func console_a3700_getc 230 ldr x0, [x0, #CONSOLE_T_BASE] 231 b console_a3700_core_getc 232endfunc console_a3700_getc 233 234 /* --------------------------------------------- 235 * void console_a3700_core_flush(uintptr_t base_addr) 236 * Function to force a write of all buffered 237 * data that hasn't been output. 238 * In : x0 - console base address 239 * Out : void. 240 * Clobber list : x0, x1 241 * --------------------------------------------- 242 */ 243func console_a3700_core_flush 244 /* Wait for the TX (THR and TSR) to be empty */ 2451: ldr w1, [x0, #UART_STATUS_REG] 246 and w1, w1, #UARTLSR_TXEMPTY 247 cmp w1, #UARTLSR_TXEMPTY 248 b.ne 1b 249 ret 250endfunc console_a3700_core_flush 251 252 /* --------------------------------------------- 253 * void console_a3700_flush(console_t *console) 254 * Function to force a write of all buffered 255 * data that hasn't been output. 256 * In : x0 - pointer to console_t structure 257 * Out : void. 258 * Clobber list : x0, x1 259 * --------------------------------------------- 260 */ 261func console_a3700_flush 262 ldr x0, [x0, #CONSOLE_T_BASE] 263 b console_a3700_core_flush 264endfunc console_a3700_flush 265 266