1/* 2 * Copyright (c) 2021-2023, Stephan Gerhold <[email protected]> 3 * 4 * Based on aarch64/skeleton_console.S: 5 * Copyright (c) 2015-2020, ARM Limited and Contributors. All rights reserved. 6 * 7 * SPDX-License-Identifier: BSD-3-Clause 8 */ 9 10#include <asm_macros.S> 11#include <console_macros.S> 12 13/* UART DM registers */ 14#define UART_DM_DMEN 0x03c /* DMA / data packing */ 15#define UART_DM_SR 0x0a4 /* status register */ 16#define UART_DM_CR 0x0a8 /* command register */ 17#define UART_DM_TF 0x100 /* transmit FIFO */ 18 19#define UART_DM_DMEN_TX_SC BIT_32(4) /* TX single character mode */ 20 21#define UART_DM_SR_TXRDY_BIT 2 /* TX FIFO has space */ 22#define UART_DM_SR_TXEMT_BIT 3 /* TX FIFO is empty */ 23 24#define UART_DM_CR_RESET_RX (U(0x01) << 4) /* reset receiver */ 25#define UART_DM_CR_RESET_TX (U(0x02) << 4) /* reset transmitter */ 26#define UART_DM_CR_TX_ENABLE BIT_32(2) /* enable transmitter */ 27 28 .globl console_uartdm_register 29 .globl console_uartdm_core_init 30 .globl console_uartdm_putc 31 .globl console_uartdm_core_putc 32 .globl console_uartdm_flush 33 .globl console_uartdm_core_flush 34 35 /* ----------------------------------------------------------- 36 * int console_uartdm_register(console_t *console, 37 * uintptr_t base_addr) 38 * Function to initialize and register the console. The caller 39 * needs to pass an empty console_t structure in which *MUST* 40 * be allocated in persistent memory (e.g. a global or static 41 * local variable, *NOT* on the stack). 42 * In : x0 - pointer to empty console_t structure 43 * x1 - base address 44 * Out: x0 - 1 on success, 0 on error 45 * Clobber list : x0 - x7 46 * ----------------------------------------------------------- 47 */ 48func console_uartdm_register 49 str x1, [x0, #CONSOLE_T_BASE] 50 mov x7, lr 51 bl console_uartdm_core_init 52 mov lr, x7 53 54 /* Register the new console */ 55 finish_console_register uartdm putc=1, flush=1 56endfunc console_uartdm_register 57 58 /* ----------------------------------------------------------- 59 * void console_uartdm_core_init(unused, uintptr_t base_addr) 60 * Function to initialize the console. 61 * In : x0 - unused 62 * x1 - base address 63 * Out: void 64 * Clobber list : x1, x2, x3 65 * ----------------------------------------------------------- 66 */ 67func console_uartdm_core_init 68 /* 69 * Try to flush remaining characters from the TX FIFO before resetting 70 * the transmitter. Unfortunately there is no good way to check if 71 * the transmitter is actually enabled (and will finish eventually), 72 * so use a timeout to avoid looping forever. 73 */ 74 mov w2, #65536 751: 76 ldr w3, [x1, #UART_DM_SR] 77 tbnz w3, #UART_DM_SR_TXEMT_BIT, 2f 78 subs w2, w2, #1 79 b.ne 1b 80 /* Timeout */ 81 822: /* Reset receiver */ 83 mov w3, #UART_DM_CR_RESET_RX 84 str w3, [x1, #UART_DM_CR] 85 86 /* Reset transmitter */ 87 mov w3, #UART_DM_CR_RESET_TX 88 str w3, [x1, #UART_DM_CR] 89 90 /* 91 * Disable BAM/DMA modes but enable single-character mode for TX. 92 * The single character mode allows simplifying the putc implementation 93 * since characters can be written directly to the FIFO instead of 94 * having to initiate a new transfer and waiting for its completion. 95 */ 96 mov w3, #UART_DM_DMEN_TX_SC 97 str w3, [x1, #UART_DM_DMEN] 98 99 /* Enable transmitter */ 100 mov w3, #UART_DM_CR_TX_ENABLE 101 str w3, [x1, #UART_DM_CR] 102 103 ret 104endfunc console_uartdm_core_init 105 106 /* ----------------------------------------------------------- 107 * int console_uartdm_putc(int c, console_t *console) 108 * Function to output a character over the console. 109 * In : w0 - character to be printed 110 * x1 - pointer to console_t struct 111 * Out: w0 - printed character on success, < 0 on error. 112 * Clobber list : x0, x1, x2 113 * ----------------------------------------------------------- 114 */ 115func console_uartdm_putc 116 ldr x1, [x1, #CONSOLE_T_BASE] 117 b console_uartdm_core_putc 118endfunc console_uartdm_putc 119 120 /* ----------------------------------------------------------- 121 * int console_uartdm_core_putc(int c, uintptr_t base_addr) 122 * Function to output a character over the console. 123 * In : w0 - character to be printed 124 * x1 - base address 125 * Out: w0 - printed character on success, < 0 on error. 126 * Clobber list : x2 127 * ----------------------------------------------------------- 128 */ 129func console_uartdm_core_putc 130 cmp w0, #'\n' 131 b.ne 2f 132 1331: /* Loop until TX FIFO has space */ 134 ldr w2, [x1, #UART_DM_SR] 135 tbz w2, #UART_DM_SR_TXRDY_BIT, 1b 136 137 /* Prepend '\r' to '\n' */ 138 mov w2, #'\r' 139 str w2, [x1, #UART_DM_TF] 140 1412: /* Loop until TX FIFO has space */ 142 ldr w2, [x1, #UART_DM_SR] 143 tbz w2, #UART_DM_SR_TXRDY_BIT, 2b 144 145 /* Write character to FIFO */ 146 str w0, [x1, #UART_DM_TF] 147 ret 148endfunc console_uartdm_core_putc 149 150 /* ----------------------------------------------------------- 151 * void console_uartdm_flush(console_t *console) 152 * Function to force a write of all buffered data 153 * that has not been output. 154 * In : x0 - pointer to console_t struct 155 * Out: void 156 * Clobber list : x0, x1, x2, x3, x4, x5 157 * ----------------------------------------------------------- 158 */ 159func console_uartdm_flush 160 ldr x1, [x0, #CONSOLE_T_BASE] 161 b console_uartdm_core_flush 162endfunc console_uartdm_flush 163 164 /* ----------------------------------------------------------- 165 * void console_uartdm_core_flush(unused, uintptr_t base_addr) 166 * Function to force a write of all buffered data 167 * that has not been output. 168 * In : x0 - unused 169 * x1 - base address 170 * Out: void 171 * Clobber list : x2 172 * ----------------------------------------------------------- 173 */ 174func console_uartdm_core_flush 1751: /* Loop until TX FIFO is empty */ 176 ldr w2, [x1, #UART_DM_SR] 177 tbz w2, #UART_DM_SR_TXEMT_BIT, 1b 178 ret 179endfunc console_uartdm_core_flush 180