Stephan Gerhold | 14fdf07 | 2021-12-01 20:01:11 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2021, Stephan Gerhold <stephan@gerhold.net> |
| 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 | */ |
| 48 | func 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 |
| 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 : x0 - unused |
| 62 | * x1 - base address |
| 63 | * Out: void |
| 64 | * Clobber list : x1, x2, x3 |
| 65 | * ----------------------------------------------------------- |
| 66 | */ |
| 67 | func console_uartdm_core_init |
| 68 | /* Reset receiver */ |
| 69 | mov w3, #UART_DM_CR_RESET_RX |
| 70 | str w3, [x1, #UART_DM_CR] |
| 71 | |
| 72 | /* Reset transmitter */ |
| 73 | mov w3, #UART_DM_CR_RESET_TX |
| 74 | str w3, [x1, #UART_DM_CR] |
| 75 | |
| 76 | /* |
| 77 | * Disable BAM/DMA modes but enable single-character mode for TX. |
| 78 | * The single character mode allows simplifying the putc implementation |
| 79 | * since characters can be written directly to the FIFO instead of |
| 80 | * having to initiate a new transfer and waiting for its completion. |
| 81 | */ |
| 82 | mov w3, #UART_DM_DMEN_TX_SC |
| 83 | str w3, [x1, #UART_DM_DMEN] |
| 84 | |
| 85 | /* Enable transmitter */ |
| 86 | mov w3, #UART_DM_CR_TX_ENABLE |
| 87 | str w3, [x1, #UART_DM_CR] |
| 88 | |
| 89 | ret |
| 90 | endfunc console_uartdm_core_init |
| 91 | |
| 92 | /* ----------------------------------------------------------- |
| 93 | * int console_uartdm_putc(int c, console_t *console) |
| 94 | * Function to output a character over the console. |
| 95 | * In : w0 - character to be printed |
| 96 | * x1 - pointer to console_t struct |
| 97 | * Out: w0 - printed character on success, < 0 on error. |
| 98 | * Clobber list : x0, x1, x2 |
| 99 | * ----------------------------------------------------------- |
| 100 | */ |
| 101 | func console_uartdm_putc |
| 102 | ldr x1, [x1, #CONSOLE_T_BASE] |
| 103 | b console_uartdm_core_putc |
| 104 | endfunc console_uartdm_putc |
| 105 | |
| 106 | /* ----------------------------------------------------------- |
| 107 | * int console_uartdm_core_putc(int c, uintptr_t base_addr) |
| 108 | * Function to output a character over the console. |
| 109 | * In : w0 - character to be printed |
| 110 | * x1 - base address |
| 111 | * Out: w0 - printed character on success, < 0 on error. |
| 112 | * Clobber list : x2 |
| 113 | * ----------------------------------------------------------- |
| 114 | */ |
| 115 | func console_uartdm_core_putc |
| 116 | 1: /* Loop until TX FIFO has space */ |
| 117 | ldr w2, [x1, #UART_DM_SR] |
| 118 | tbz w2, #UART_DM_SR_TXRDY_BIT, 1b |
| 119 | |
| 120 | /* Write character to FIFO */ |
| 121 | str w0, [x1, #UART_DM_TF] |
| 122 | ret |
| 123 | endfunc console_uartdm_core_putc |
| 124 | |
| 125 | /* ----------------------------------------------------------- |
| 126 | * void console_uartdm_flush(console_t *console) |
| 127 | * Function to force a write of all buffered data |
| 128 | * that has not been output. |
| 129 | * In : x0 - pointer to console_t struct |
| 130 | * Out: void |
| 131 | * Clobber list : x0, x1, x2, x3, x4, x5 |
| 132 | * ----------------------------------------------------------- |
| 133 | */ |
| 134 | func console_uartdm_flush |
| 135 | ldr x1, [x0, #CONSOLE_T_BASE] |
| 136 | b console_uartdm_core_flush |
| 137 | endfunc console_uartdm_flush |
| 138 | |
| 139 | /* ----------------------------------------------------------- |
| 140 | * void console_uartdm_core_flush(unused, uintptr_t base_addr) |
| 141 | * Function to force a write of all buffered data |
| 142 | * that has not been output. |
| 143 | * In : x0 - unused |
| 144 | * x1 - base address |
| 145 | * Out: void |
| 146 | * Clobber list : x2 |
| 147 | * ----------------------------------------------------------- |
| 148 | */ |
| 149 | func console_uartdm_core_flush |
| 150 | 1: /* Loop until TX FIFO is empty */ |
| 151 | ldr w2, [x1, #UART_DM_SR] |
| 152 | tbz w2, #UART_DM_SR_TXEMT_BIT, 1b |
| 153 | ret |
| 154 | endfunc console_uartdm_core_flush |