Stephan Gerhold | b68e4e9 | 2022-08-28 15:18:55 +0200 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2021-2023, Stephan Gerhold <stephan@gerhold.net> |
| 3 | * |
| 4 | * Based on aarch32/skeleton_console.S: |
| 5 | * Copyright (c) 2016-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_32(2) /* TX FIFO has space */ |
| 22 | #define UART_DM_SR_TXEMT BIT_32(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 : r0 - pointer to empty console_t structure |
| 43 | * r1 - base address |
| 44 | * Out: r0 - 1 on success, 0 on error |
| 45 | * Clobber list : r0 - r7 |
| 46 | * ----------------------------------------------------------- |
| 47 | */ |
| 48 | func console_uartdm_register |
| 49 | str r1, [r0, #CONSOLE_T_BASE] |
| 50 | mov r7, lr |
| 51 | bl console_uartdm_core_init |
| 52 | mov lr, r7 |
| 53 | |
| 54 | /* Register the new console */ |
| 55 | finish_console_register uartdm putc=1, flush=1 |
| 56 | endfunc console_uartdm_register |
| 57 | |
| 58 | /* ----------------------------------------------------------- |
| 59 | * void console_uartdm_core_init(unused, uintptr_t base_addr) |
| 60 | * Function to initialize the console. |
| 61 | * In : r0 - unused |
| 62 | * r1 - base address |
| 63 | * Out: void |
| 64 | * Clobber list : r1, r2, r3 |
| 65 | * ----------------------------------------------------------- |
| 66 | */ |
| 67 | func 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 r2, #65536 |
| 75 | 1: |
| 76 | ldr r3, [r1, #UART_DM_SR] |
| 77 | tst r3, #UART_DM_SR_TXEMT |
| 78 | bne 2f |
| 79 | subs r2, r2, #1 |
| 80 | bne 1b |
| 81 | /* Timeout */ |
| 82 | |
| 83 | 2: /* Reset receiver */ |
| 84 | mov r3, #UART_DM_CR_RESET_RX |
| 85 | str r3, [r1, #UART_DM_CR] |
| 86 | |
| 87 | /* Reset transmitter */ |
| 88 | mov r3, #UART_DM_CR_RESET_TX |
| 89 | str r3, [r1, #UART_DM_CR] |
| 90 | |
| 91 | /* |
| 92 | * Disable BAM/DMA modes but enable single-character mode for TX. |
| 93 | * The single character mode allows simplifying the putc implementation |
| 94 | * since characters can be written directly to the FIFO instead of |
| 95 | * having to initiate a new transfer and waiting for its completion. |
| 96 | */ |
| 97 | mov r3, #UART_DM_DMEN_TX_SC |
| 98 | str r3, [r1, #UART_DM_DMEN] |
| 99 | |
| 100 | /* Enable transmitter */ |
| 101 | mov r3, #UART_DM_CR_TX_ENABLE |
| 102 | str r3, [r1, #UART_DM_CR] |
| 103 | |
| 104 | bx lr |
| 105 | endfunc console_uartdm_core_init |
| 106 | |
| 107 | /* ----------------------------------------------------------- |
| 108 | * int console_uartdm_putc(int c, console_t *console) |
| 109 | * Function to output a character over the console. |
| 110 | * In : r0 - character to be printed |
| 111 | * r1 - pointer to console_t struct |
| 112 | * Out: r0 - printed character on success, < 0 on error. |
| 113 | * Clobber list : r0, r1, r2 |
| 114 | * ----------------------------------------------------------- |
| 115 | */ |
| 116 | func console_uartdm_putc |
| 117 | ldr r1, [r1, #CONSOLE_T_BASE] |
| 118 | b console_uartdm_core_putc |
| 119 | endfunc console_uartdm_putc |
| 120 | |
| 121 | /* ----------------------------------------------------------- |
| 122 | * int console_uartdm_core_putc(int c, uintptr_t base_addr) |
| 123 | * Function to output a character over the console. |
| 124 | * In : r0 - character to be printed |
| 125 | * r1 - base address |
| 126 | * Out: r0 - printed character on success, < 0 on error. |
| 127 | * Clobber list : r2 |
| 128 | * ----------------------------------------------------------- |
| 129 | */ |
| 130 | func console_uartdm_core_putc |
| 131 | cmp r0, #'\n' |
| 132 | bne 2f |
| 133 | |
| 134 | 1: /* Loop until TX FIFO has space */ |
| 135 | ldr r2, [r1, #UART_DM_SR] |
| 136 | tst r2, #UART_DM_SR_TXRDY |
| 137 | beq 1b |
| 138 | |
| 139 | /* Prepend '\r' to '\n' */ |
| 140 | mov r2, #'\r' |
| 141 | str r2, [r1, #UART_DM_TF] |
| 142 | |
| 143 | 2: /* Loop until TX FIFO has space */ |
| 144 | ldr r2, [r1, #UART_DM_SR] |
| 145 | tst r2, #UART_DM_SR_TXRDY |
| 146 | beq 2b |
| 147 | |
| 148 | /* Write character to FIFO */ |
| 149 | str r0, [r1, #UART_DM_TF] |
| 150 | bx lr |
| 151 | endfunc console_uartdm_core_putc |
| 152 | |
| 153 | /* ----------------------------------------------------------- |
| 154 | * void console_uartdm_flush(console_t *console) |
| 155 | * Function to force a write of all buffered data |
| 156 | * that has not been output. |
| 157 | * In : r0 - pointer to console_t struct |
| 158 | * Out: void |
| 159 | * Clobber list : r0, r1, r2, r3, r4, r5 |
| 160 | * ----------------------------------------------------------- |
| 161 | */ |
| 162 | func console_uartdm_flush |
| 163 | ldr r1, [r0, #CONSOLE_T_BASE] |
| 164 | b console_uartdm_core_flush |
| 165 | endfunc console_uartdm_flush |
| 166 | |
| 167 | /* ----------------------------------------------------------- |
| 168 | * void console_uartdm_core_flush(unused, uintptr_t base_addr) |
| 169 | * Function to force a write of all buffered data |
| 170 | * that has not been output. |
| 171 | * In : r0 - unused |
| 172 | * r1 - base address |
| 173 | * Out: void |
| 174 | * Clobber list : r2 |
| 175 | * ----------------------------------------------------------- |
| 176 | */ |
| 177 | func console_uartdm_core_flush |
| 178 | 1: /* Loop until TX FIFO is empty */ |
| 179 | ldr r2, [r1, #UART_DM_SR] |
| 180 | tst r2, #UART_DM_SR_TXEMT |
| 181 | beq 1b |
| 182 | bx lr |
| 183 | endfunc console_uartdm_core_flush |