Konstantin Porotchkin | ee4fa95 | 2018-10-08 16:50:54 +0300 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2016 Marvell International Ltd. |
| 3 | * |
| 4 | * SPDX-License-Identifier: BSD-3-Clause |
| 5 | * https://spdx.org/licenses |
| 6 | */ |
| 7 | |
Konstantin Porotchkin | d8e3957 | 2018-11-14 17:15:08 +0200 | [diff] [blame] | 8 | #include <arch.h> |
Konstantin Porotchkin | ee4fa95 | 2018-10-08 16:50:54 +0300 | [diff] [blame] | 9 | #include <asm_macros.S> |
Konstantin Porotchkin | d8e3957 | 2018-11-14 17:15:08 +0200 | [diff] [blame] | 10 | #include <console_macros.S> |
Antonio Nino Diaz | e0f9063 | 2018-12-14 00:18:21 +0000 | [diff] [blame] | 11 | #include <drivers/marvell/uart/a3700_console.h> |
Konstantin Porotchkin | ee4fa95 | 2018-10-08 16:50:54 +0300 | [diff] [blame] | 12 | |
Konstantin Porotchkin | d8e3957 | 2018-11-14 17:15:08 +0200 | [diff] [blame] | 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 |
Konstantin Porotchkin | ee4fa95 | 2018-10-08 16:50:54 +0300 | [diff] [blame] | 25 | |
| 26 | /* ----------------------------------------------- |
Konstantin Porotchkin | d8e3957 | 2018-11-14 17:15:08 +0200 | [diff] [blame] | 27 | * int console_a3700_core_init(unsigned long base_addr, |
Konstantin Porotchkin | ee4fa95 | 2018-10-08 16:50:54 +0300 | [diff] [blame] | 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 | */ |
Konstantin Porotchkin | d8e3957 | 2018-11-14 17:15:08 +0200 | [diff] [blame] | 40 | func console_a3700_core_init |
Konstantin Porotchkin | ee4fa95 | 2018-10-08 16:50:54 +0300 | [diff] [blame] | 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 | /* |
Pali Rohár | 10d330f | 2021-01-18 12:52:55 +0100 | [diff] [blame] | 63 | * Wait for the TX (THR and TSR) to be empty. If wait for 20ms, the TX FIFO is |
Konstantin Porotchkin | ee4fa95 | 2018-10-08 16:50:54 +0300 | [diff] [blame] | 64 | * still not empty, TX FIFO will reset by all means. |
| 65 | */ |
| 66 | mov w1, #20 /* max time out 20ms */ |
| 67 | 2: |
Pali Rohár | 10d330f | 2021-01-18 12:52:55 +0100 | [diff] [blame] | 68 | /* Check whether TX (THR and TSR) is empty */ |
Konstantin Porotchkin | ee4fa95 | 2018-10-08 16:50:54 +0300 | [diff] [blame] | 69 | ldr w3, [x0, #UART_STATUS_REG] |
Pali Rohár | 10d330f | 2021-01-18 12:52:55 +0100 | [diff] [blame] | 70 | and w3, w3, #UARTLSR_TXEMPTY |
Konstantin Porotchkin | ee4fa95 | 2018-10-08 16:50:54 +0300 | [diff] [blame] | 71 | cmp w3, #0 |
| 72 | b.ne 4f |
| 73 | |
| 74 | /* Delay */ |
| 75 | mov w2, #30000 |
| 76 | 3: |
| 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 | |
| 86 | 4: |
| 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 |
| 94 | 1: |
| 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 |
| 105 | init_fail: |
| 106 | mov w0, #0 |
| 107 | ret |
Konstantin Porotchkin | d8e3957 | 2018-11-14 17:15:08 +0200 | [diff] [blame] | 108 | endfunc console_a3700_core_init |
| 109 | |
| 110 | .globl console_a3700_register |
| 111 | |
| 112 | /* ----------------------------------------------- |
Andre Przywara | 0342f40 | 2020-01-25 00:58:35 +0000 | [diff] [blame] | 113 | * int console_a3700_register(console_t *console, |
Konstantin Porotchkin | d8e3957 | 2018-11-14 17:15:08 +0200 | [diff] [blame] | 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 |
Andre Przywara | 0342f40 | 2020-01-25 00:58:35 +0000 | [diff] [blame] | 121 | * x3 - pointer to empty console_t struct |
Konstantin Porotchkin | d8e3957 | 2018-11-14 17:15:08 +0200 | [diff] [blame] | 122 | * Out: return 1 on success, 0 on error |
| 123 | * Clobber list : x0, x1, x2, x6, x7, x14 |
| 124 | * ----------------------------------------------- |
| 125 | */ |
| 126 | func console_a3700_register |
| 127 | mov x7, x30 |
| 128 | mov x6, x3 |
| 129 | cbz x6, register_fail |
Andre Przywara | 0342f40 | 2020-01-25 00:58:35 +0000 | [diff] [blame] | 130 | str x0, [x6, #CONSOLE_T_BASE] |
Konstantin Porotchkin | d8e3957 | 2018-11-14 17:15:08 +0200 | [diff] [blame] | 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 | |
| 139 | register_fail: |
| 140 | ret x7 |
| 141 | endfunc console_a3700_register |
Konstantin Porotchkin | ee4fa95 | 2018-10-08 16:50:54 +0300 | [diff] [blame] | 142 | |
| 143 | /* -------------------------------------------------------- |
Konstantin Porotchkin | d8e3957 | 2018-11-14 17:15:08 +0200 | [diff] [blame] | 144 | * int console_a3700_core_putc(int c, unsigned int base_addr) |
Konstantin Porotchkin | ee4fa95 | 2018-10-08 16:50:54 +0300 | [diff] [blame] | 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 | */ |
Konstantin Porotchkin | d8e3957 | 2018-11-14 17:15:08 +0200 | [diff] [blame] | 153 | func console_a3700_core_putc |
Konstantin Porotchkin | ee4fa95 | 2018-10-08 16:50:54 +0300 | [diff] [blame] | 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 */ |
| 161 | 1: 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 */ |
| 169 | 2: 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 |
| 175 | putc_error: |
| 176 | mov w0, #-1 |
| 177 | ret |
Konstantin Porotchkin | d8e3957 | 2018-11-14 17:15:08 +0200 | [diff] [blame] | 178 | endfunc console_a3700_core_putc |
| 179 | |
| 180 | /* -------------------------------------------------------- |
Andre Przywara | 0342f40 | 2020-01-25 00:58:35 +0000 | [diff] [blame] | 181 | * int console_a3700_putc(int c, console_t *console) |
Konstantin Porotchkin | d8e3957 | 2018-11-14 17:15:08 +0200 | [diff] [blame] | 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 | */ |
| 190 | func console_a3700_putc |
Andre Przywara | 0342f40 | 2020-01-25 00:58:35 +0000 | [diff] [blame] | 191 | ldr x1, [x1, #CONSOLE_T_BASE] |
Konstantin Porotchkin | d8e3957 | 2018-11-14 17:15:08 +0200 | [diff] [blame] | 192 | b console_a3700_core_putc |
| 193 | endfunc console_a3700_putc |
Konstantin Porotchkin | ee4fa95 | 2018-10-08 16:50:54 +0300 | [diff] [blame] | 194 | |
| 195 | /* --------------------------------------------- |
Konstantin Porotchkin | d8e3957 | 2018-11-14 17:15:08 +0200 | [diff] [blame] | 196 | * int console_a3700_core_getc(void) |
Konstantin Porotchkin | ee4fa95 | 2018-10-08 16:50:54 +0300 | [diff] [blame] | 197 | * Function to get a character from the console. |
| 198 | * It returns the character grabbed on success |
Pali Rohár | 6291f55 | 2021-01-18 12:39:25 +0100 | [diff] [blame] | 199 | * or -1 if no character is available. |
Konstantin Porotchkin | ee4fa95 | 2018-10-08 16:50:54 +0300 | [diff] [blame] | 200 | * In : w0 - console base address |
Pali Rohár | 6291f55 | 2021-01-18 12:39:25 +0100 | [diff] [blame] | 201 | * Out : w0 - character if available, else -1 |
Konstantin Porotchkin | ee4fa95 | 2018-10-08 16:50:54 +0300 | [diff] [blame] | 202 | * Clobber list : x0, x1 |
| 203 | * --------------------------------------------- |
| 204 | */ |
Konstantin Porotchkin | d8e3957 | 2018-11-14 17:15:08 +0200 | [diff] [blame] | 205 | func console_a3700_core_getc |
Pali Rohár | 6291f55 | 2021-01-18 12:39:25 +0100 | [diff] [blame] | 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 |
| 214 | getc_no_char: |
| 215 | mov w0, #ERROR_NO_PENDING_CHAR |
Konstantin Porotchkin | ee4fa95 | 2018-10-08 16:50:54 +0300 | [diff] [blame] | 216 | ret |
Konstantin Porotchkin | d8e3957 | 2018-11-14 17:15:08 +0200 | [diff] [blame] | 217 | endfunc console_a3700_core_getc |
| 218 | |
| 219 | /* --------------------------------------------- |
Andre Przywara | 0342f40 | 2020-01-25 00:58:35 +0000 | [diff] [blame] | 220 | * int console_a3700_getc(console_t *console) |
Konstantin Porotchkin | d8e3957 | 2018-11-14 17:15:08 +0200 | [diff] [blame] | 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 | */ |
| 229 | func console_a3700_getc |
Andre Przywara | 0342f40 | 2020-01-25 00:58:35 +0000 | [diff] [blame] | 230 | ldr x0, [x0, #CONSOLE_T_BASE] |
Konstantin Porotchkin | d8e3957 | 2018-11-14 17:15:08 +0200 | [diff] [blame] | 231 | b console_a3700_core_getc |
| 232 | endfunc console_a3700_getc |
Konstantin Porotchkin | ee4fa95 | 2018-10-08 16:50:54 +0300 | [diff] [blame] | 233 | |
| 234 | /* --------------------------------------------- |
Jimmy Brisson | 39f9eee | 2020-08-05 13:44:05 -0500 | [diff] [blame] | 235 | * void console_a3700_core_flush(uintptr_t base_addr) |
Konstantin Porotchkin | ee4fa95 | 2018-10-08 16:50:54 +0300 | [diff] [blame] | 236 | * Function to force a write of all buffered |
| 237 | * data that hasn't been output. |
| 238 | * In : x0 - console base address |
Jimmy Brisson | 39f9eee | 2020-08-05 13:44:05 -0500 | [diff] [blame] | 239 | * Out : void. |
Konstantin Porotchkin | ee4fa95 | 2018-10-08 16:50:54 +0300 | [diff] [blame] | 240 | * Clobber list : x0, x1 |
| 241 | * --------------------------------------------- |
| 242 | */ |
Konstantin Porotchkin | d8e3957 | 2018-11-14 17:15:08 +0200 | [diff] [blame] | 243 | func console_a3700_core_flush |
Pali Rohár | 10d330f | 2021-01-18 12:52:55 +0100 | [diff] [blame] | 244 | /* Wait for the TX (THR and TSR) to be empty */ |
Pali Rohár | f758ceb | 2020-12-23 19:23:26 +0100 | [diff] [blame] | 245 | 1: ldr w1, [x0, #UART_STATUS_REG] |
Pali Rohár | 10d330f | 2021-01-18 12:52:55 +0100 | [diff] [blame] | 246 | and w1, w1, #UARTLSR_TXEMPTY |
| 247 | cmp w1, #UARTLSR_TXEMPTY |
Pali Rohár | f758ceb | 2020-12-23 19:23:26 +0100 | [diff] [blame] | 248 | b.ne 1b |
Konstantin Porotchkin | ee4fa95 | 2018-10-08 16:50:54 +0300 | [diff] [blame] | 249 | ret |
Konstantin Porotchkin | d8e3957 | 2018-11-14 17:15:08 +0200 | [diff] [blame] | 250 | endfunc console_a3700_core_flush |
| 251 | |
| 252 | /* --------------------------------------------- |
Jimmy Brisson | 39f9eee | 2020-08-05 13:44:05 -0500 | [diff] [blame] | 253 | * void console_a3700_flush(console_t *console) |
Konstantin Porotchkin | d8e3957 | 2018-11-14 17:15:08 +0200 | [diff] [blame] | 254 | * Function to force a write of all buffered |
| 255 | * data that hasn't been output. |
| 256 | * In : x0 - pointer to console_t structure |
Jimmy Brisson | 39f9eee | 2020-08-05 13:44:05 -0500 | [diff] [blame] | 257 | * Out : void. |
Konstantin Porotchkin | d8e3957 | 2018-11-14 17:15:08 +0200 | [diff] [blame] | 258 | * Clobber list : x0, x1 |
| 259 | * --------------------------------------------- |
| 260 | */ |
| 261 | func console_a3700_flush |
Andre Przywara | 0342f40 | 2020-01-25 00:58:35 +0000 | [diff] [blame] | 262 | ldr x0, [x0, #CONSOLE_T_BASE] |
Konstantin Porotchkin | d8e3957 | 2018-11-14 17:15:08 +0200 | [diff] [blame] | 263 | b console_a3700_core_flush |
| 264 | endfunc console_a3700_flush |
| 265 | |