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 |
Pali Rohár | 622d5da | 2021-11-15 10:51:28 +0100 | [diff] [blame] | 37 | * Clobber list : x1, x2, x3, x4 |
Konstantin Porotchkin | ee4fa95 | 2018-10-08 16:50:54 +0300 | [diff] [blame] | 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 | |
Konstantin Porotchkin | ee4fa95 | 2018-10-08 16:50:54 +0300 | [diff] [blame] | 47 | /* |
Pali Rohár | c000296 | 2021-02-16 11:56:24 +0100 | [diff] [blame] | 48 | * Wait for the TX (THR and TSR) to be empty. If wait for 3ms, the TX FIFO is |
Konstantin Porotchkin | ee4fa95 | 2018-10-08 16:50:54 +0300 | [diff] [blame] | 49 | * still not empty, TX FIFO will reset by all means. |
| 50 | */ |
Pali Rohár | 622d5da | 2021-11-15 10:51:28 +0100 | [diff] [blame] | 51 | mov w4, #30 /* max time out 30 * 100 us */ |
Konstantin Porotchkin | ee4fa95 | 2018-10-08 16:50:54 +0300 | [diff] [blame] | 52 | 2: |
Pali Rohár | 10d330f | 2021-01-18 12:52:55 +0100 | [diff] [blame] | 53 | /* Check whether TX (THR and TSR) is empty */ |
Konstantin Porotchkin | ee4fa95 | 2018-10-08 16:50:54 +0300 | [diff] [blame] | 54 | ldr w3, [x0, #UART_STATUS_REG] |
Pali Rohár | 10d330f | 2021-01-18 12:52:55 +0100 | [diff] [blame] | 55 | and w3, w3, #UARTLSR_TXEMPTY |
Konstantin Porotchkin | ee4fa95 | 2018-10-08 16:50:54 +0300 | [diff] [blame] | 56 | cmp w3, #0 |
| 57 | b.ne 4f |
| 58 | |
| 59 | /* Delay */ |
Pali Rohár | 622d5da | 2021-11-15 10:51:28 +0100 | [diff] [blame] | 60 | mov w3, #60000 /* 60000 cycles of below 3 instructions on 1200 MHz CPU ~~ 100 us */ |
Konstantin Porotchkin | ee4fa95 | 2018-10-08 16:50:54 +0300 | [diff] [blame] | 61 | 3: |
Pali Rohár | 622d5da | 2021-11-15 10:51:28 +0100 | [diff] [blame] | 62 | sub w3, w3, #1 |
| 63 | cmp w3, #0 |
Konstantin Porotchkin | ee4fa95 | 2018-10-08 16:50:54 +0300 | [diff] [blame] | 64 | b.ne 3b |
| 65 | |
Pali Rohár | 46179c3 | 2021-02-16 11:49:11 +0100 | [diff] [blame] | 66 | /* Check whether wait timeout expired */ |
Pali Rohár | 622d5da | 2021-11-15 10:51:28 +0100 | [diff] [blame] | 67 | sub w4, w4, #1 |
| 68 | cmp w4, #0 |
Konstantin Porotchkin | ee4fa95 | 2018-10-08 16:50:54 +0300 | [diff] [blame] | 69 | b.ne 2b |
| 70 | |
| 71 | 4: |
Pali Rohár | 82602c9 | 2021-11-15 12:24:56 +0100 | [diff] [blame] | 72 | /* Reset UART via North Bridge Peripheral */ |
| 73 | mov_imm x4, MVEBU_NB_RESET_REG |
| 74 | ldr w3, [x4] |
| 75 | bic w3, w3, #MVEBU_NB_RESET_UART_N |
| 76 | str w3, [x4] |
| 77 | orr w3, w3, #MVEBU_NB_RESET_UART_N |
| 78 | str w3, [x4] |
| 79 | |
Konstantin Porotchkin | ee4fa95 | 2018-10-08 16:50:54 +0300 | [diff] [blame] | 80 | /* Reset FIFO */ |
| 81 | mov w3, #UART_CTRL_RXFIFO_RESET |
| 82 | orr w3, w3, #UART_CTRL_TXFIFO_RESET |
| 83 | str w3, [x0, #UART_CTRL_REG] |
| 84 | |
| 85 | /* Delay */ |
Pali Rohár | 622d5da | 2021-11-15 10:51:28 +0100 | [diff] [blame] | 86 | mov w3, #2000 |
Konstantin Porotchkin | ee4fa95 | 2018-10-08 16:50:54 +0300 | [diff] [blame] | 87 | 1: |
Pali Rohár | 622d5da | 2021-11-15 10:51:28 +0100 | [diff] [blame] | 88 | sub w3, w3, #1 |
| 89 | cmp w3, #0 |
Konstantin Porotchkin | ee4fa95 | 2018-10-08 16:50:54 +0300 | [diff] [blame] | 90 | b.ne 1b |
| 91 | |
Pali Rohár | f658b89 | 2021-11-15 10:53:21 +0100 | [diff] [blame] | 92 | /* Program the baudrate */ |
| 93 | /* Divisor = Round(Uartclock / (16 * baudrate)) */ |
| 94 | lsl w2, w2, #4 |
| 95 | add w1, w1, w2, lsr #1 |
| 96 | udiv w2, w1, w2 |
| 97 | and w2, w2, #0x3ff /* clear all other bits to use default clock */ |
| 98 | |
| 99 | str w2, [x0, #UART_BAUD_REG]/* set baud rate divisor */ |
| 100 | |
| 101 | /* Set UART to default 16X scheme */ |
| 102 | mov w3, #0 |
| 103 | str w3, [x0, #UART_POSSR_REG] |
| 104 | |
Konstantin Porotchkin | ee4fa95 | 2018-10-08 16:50:54 +0300 | [diff] [blame] | 105 | /* No Parity, 1 Stop */ |
| 106 | mov w3, #0 |
| 107 | str w3, [x0, #UART_CTRL_REG] |
| 108 | |
| 109 | mov w0, #1 |
| 110 | ret |
| 111 | init_fail: |
| 112 | mov w0, #0 |
| 113 | ret |
Konstantin Porotchkin | d8e3957 | 2018-11-14 17:15:08 +0200 | [diff] [blame] | 114 | endfunc console_a3700_core_init |
| 115 | |
| 116 | .globl console_a3700_register |
| 117 | |
| 118 | /* ----------------------------------------------- |
Andre Przywara | 0342f40 | 2020-01-25 00:58:35 +0000 | [diff] [blame] | 119 | * int console_a3700_register(console_t *console, |
Konstantin Porotchkin | d8e3957 | 2018-11-14 17:15:08 +0200 | [diff] [blame] | 120 | uintptr_t base, uint32_t clk, uint32_t baud) |
| 121 | * Function to initialize and register a new a3700 |
| 122 | * console. Storage passed in for the console struct |
| 123 | * *must* be persistent (i.e. not from the stack). |
| 124 | * In: x0 - UART register base address |
| 125 | * w1 - UART clock in Hz |
| 126 | * w2 - Baud rate |
Andre Przywara | 0342f40 | 2020-01-25 00:58:35 +0000 | [diff] [blame] | 127 | * x3 - pointer to empty console_t struct |
Konstantin Porotchkin | d8e3957 | 2018-11-14 17:15:08 +0200 | [diff] [blame] | 128 | * Out: return 1 on success, 0 on error |
Pali Rohár | 622d5da | 2021-11-15 10:51:28 +0100 | [diff] [blame] | 129 | * Clobber list : x0, x1, x2, x3, x4, x6, x7, x14 |
Konstantin Porotchkin | d8e3957 | 2018-11-14 17:15:08 +0200 | [diff] [blame] | 130 | * ----------------------------------------------- |
| 131 | */ |
| 132 | func console_a3700_register |
| 133 | mov x7, x30 |
| 134 | mov x6, x3 |
| 135 | cbz x6, register_fail |
Andre Przywara | 0342f40 | 2020-01-25 00:58:35 +0000 | [diff] [blame] | 136 | str x0, [x6, #CONSOLE_T_BASE] |
Konstantin Porotchkin | d8e3957 | 2018-11-14 17:15:08 +0200 | [diff] [blame] | 137 | |
| 138 | bl console_a3700_core_init |
| 139 | cbz x0, register_fail |
| 140 | |
| 141 | mov x0, x6 |
| 142 | mov x30, x7 |
| 143 | finish_console_register a3700, putc=1, getc=1, flush=1 |
| 144 | |
| 145 | register_fail: |
| 146 | ret x7 |
| 147 | endfunc console_a3700_register |
Konstantin Porotchkin | ee4fa95 | 2018-10-08 16:50:54 +0300 | [diff] [blame] | 148 | |
| 149 | /* -------------------------------------------------------- |
Konstantin Porotchkin | d8e3957 | 2018-11-14 17:15:08 +0200 | [diff] [blame] | 150 | * int console_a3700_core_putc(int c, unsigned int base_addr) |
Konstantin Porotchkin | ee4fa95 | 2018-10-08 16:50:54 +0300 | [diff] [blame] | 151 | * Function to output a character over the console. It |
| 152 | * returns the character printed on success or -1 on error. |
| 153 | * In : w0 - character to be printed |
| 154 | * x1 - console base address |
| 155 | * Out : return -1 on error else return character. |
| 156 | * Clobber list : x2 |
| 157 | * -------------------------------------------------------- |
| 158 | */ |
Konstantin Porotchkin | d8e3957 | 2018-11-14 17:15:08 +0200 | [diff] [blame] | 159 | func console_a3700_core_putc |
Konstantin Porotchkin | ee4fa95 | 2018-10-08 16:50:54 +0300 | [diff] [blame] | 160 | /* Check the input parameter */ |
| 161 | cbz x1, putc_error |
| 162 | |
| 163 | /* Prepend '\r' to '\n' */ |
| 164 | cmp w0, #0xA |
| 165 | b.ne 2f |
| 166 | /* Check if the transmit FIFO is full */ |
| 167 | 1: ldr w2, [x1, #UART_STATUS_REG] |
| 168 | and w2, w2, #UARTLSR_TXFIFOFULL |
| 169 | cmp w2, #UARTLSR_TXFIFOFULL |
| 170 | b.eq 1b |
| 171 | mov w2, #0xD /* '\r' */ |
| 172 | str w2, [x1, #UART_TX_REG] |
| 173 | |
| 174 | /* Check if the transmit FIFO is full */ |
| 175 | 2: ldr w2, [x1, #UART_STATUS_REG] |
| 176 | and w2, w2, #UARTLSR_TXFIFOFULL |
| 177 | cmp w2, #UARTLSR_TXFIFOFULL |
| 178 | b.eq 2b |
| 179 | str w0, [x1, #UART_TX_REG] |
| 180 | ret |
| 181 | putc_error: |
| 182 | mov w0, #-1 |
| 183 | ret |
Konstantin Porotchkin | d8e3957 | 2018-11-14 17:15:08 +0200 | [diff] [blame] | 184 | endfunc console_a3700_core_putc |
| 185 | |
| 186 | /* -------------------------------------------------------- |
Andre Przywara | 0342f40 | 2020-01-25 00:58:35 +0000 | [diff] [blame] | 187 | * int console_a3700_putc(int c, console_t *console) |
Konstantin Porotchkin | d8e3957 | 2018-11-14 17:15:08 +0200 | [diff] [blame] | 188 | * Function to output a character over the console. It |
| 189 | * returns the character printed on success or -1 on error. |
| 190 | * In : w0 - character to be printed |
| 191 | * x1 - pointer to console_t structure |
| 192 | * Out : return -1 on error else return character. |
| 193 | * Clobber list : x2 |
| 194 | * -------------------------------------------------------- |
| 195 | */ |
| 196 | func console_a3700_putc |
Andre Przywara | 0342f40 | 2020-01-25 00:58:35 +0000 | [diff] [blame] | 197 | ldr x1, [x1, #CONSOLE_T_BASE] |
Konstantin Porotchkin | d8e3957 | 2018-11-14 17:15:08 +0200 | [diff] [blame] | 198 | b console_a3700_core_putc |
| 199 | endfunc console_a3700_putc |
Konstantin Porotchkin | ee4fa95 | 2018-10-08 16:50:54 +0300 | [diff] [blame] | 200 | |
| 201 | /* --------------------------------------------- |
Konstantin Porotchkin | d8e3957 | 2018-11-14 17:15:08 +0200 | [diff] [blame] | 202 | * int console_a3700_core_getc(void) |
Konstantin Porotchkin | ee4fa95 | 2018-10-08 16:50:54 +0300 | [diff] [blame] | 203 | * Function to get a character from the console. |
| 204 | * It returns the character grabbed on success |
Pali Rohár | 6291f55 | 2021-01-18 12:39:25 +0100 | [diff] [blame] | 205 | * or -1 if no character is available. |
Konstantin Porotchkin | ee4fa95 | 2018-10-08 16:50:54 +0300 | [diff] [blame] | 206 | * In : w0 - console base address |
Pali Rohár | 6291f55 | 2021-01-18 12:39:25 +0100 | [diff] [blame] | 207 | * Out : w0 - character if available, else -1 |
Konstantin Porotchkin | ee4fa95 | 2018-10-08 16:50:54 +0300 | [diff] [blame] | 208 | * Clobber list : x0, x1 |
| 209 | * --------------------------------------------- |
| 210 | */ |
Konstantin Porotchkin | d8e3957 | 2018-11-14 17:15:08 +0200 | [diff] [blame] | 211 | func console_a3700_core_getc |
Pali Rohár | 6291f55 | 2021-01-18 12:39:25 +0100 | [diff] [blame] | 212 | /* Check if there is a pending character */ |
| 213 | ldr w1, [x0, #UART_STATUS_REG] |
| 214 | and w1, w1, #UARTLSR_RXRDY |
| 215 | cmp w1, #UARTLSR_RXRDY |
| 216 | b.ne getc_no_char |
| 217 | ldr w0, [x0, #UART_RX_REG] |
| 218 | and w0, w0, #0xff |
| 219 | ret |
| 220 | getc_no_char: |
| 221 | mov w0, #ERROR_NO_PENDING_CHAR |
Konstantin Porotchkin | ee4fa95 | 2018-10-08 16:50:54 +0300 | [diff] [blame] | 222 | ret |
Konstantin Porotchkin | d8e3957 | 2018-11-14 17:15:08 +0200 | [diff] [blame] | 223 | endfunc console_a3700_core_getc |
| 224 | |
| 225 | /* --------------------------------------------- |
Andre Przywara | 0342f40 | 2020-01-25 00:58:35 +0000 | [diff] [blame] | 226 | * int console_a3700_getc(console_t *console) |
Konstantin Porotchkin | d8e3957 | 2018-11-14 17:15:08 +0200 | [diff] [blame] | 227 | * Function to get a character from the console. |
| 228 | * It returns the character grabbed on success |
| 229 | * or -1 on if no character is available. |
| 230 | * In : x0 - pointer to console_t structure |
| 231 | * Out : w0 - character if available, else -1 |
| 232 | * Clobber list : x0, x1 |
| 233 | * --------------------------------------------- |
| 234 | */ |
| 235 | func console_a3700_getc |
Andre Przywara | 0342f40 | 2020-01-25 00:58:35 +0000 | [diff] [blame] | 236 | ldr x0, [x0, #CONSOLE_T_BASE] |
Konstantin Porotchkin | d8e3957 | 2018-11-14 17:15:08 +0200 | [diff] [blame] | 237 | b console_a3700_core_getc |
| 238 | endfunc console_a3700_getc |
Konstantin Porotchkin | ee4fa95 | 2018-10-08 16:50:54 +0300 | [diff] [blame] | 239 | |
| 240 | /* --------------------------------------------- |
Jimmy Brisson | 39f9eee | 2020-08-05 13:44:05 -0500 | [diff] [blame] | 241 | * void console_a3700_core_flush(uintptr_t base_addr) |
Konstantin Porotchkin | ee4fa95 | 2018-10-08 16:50:54 +0300 | [diff] [blame] | 242 | * Function to force a write of all buffered |
| 243 | * data that hasn't been output. |
| 244 | * In : x0 - console base address |
Jimmy Brisson | 39f9eee | 2020-08-05 13:44:05 -0500 | [diff] [blame] | 245 | * Out : void. |
Konstantin Porotchkin | ee4fa95 | 2018-10-08 16:50:54 +0300 | [diff] [blame] | 246 | * Clobber list : x0, x1 |
| 247 | * --------------------------------------------- |
| 248 | */ |
Konstantin Porotchkin | d8e3957 | 2018-11-14 17:15:08 +0200 | [diff] [blame] | 249 | func console_a3700_core_flush |
Pali Rohár | 10d330f | 2021-01-18 12:52:55 +0100 | [diff] [blame] | 250 | /* Wait for the TX (THR and TSR) to be empty */ |
Pali Rohár | f758ceb | 2020-12-23 19:23:26 +0100 | [diff] [blame] | 251 | 1: ldr w1, [x0, #UART_STATUS_REG] |
Pali Rohár | 10d330f | 2021-01-18 12:52:55 +0100 | [diff] [blame] | 252 | and w1, w1, #UARTLSR_TXEMPTY |
| 253 | cmp w1, #UARTLSR_TXEMPTY |
Pali Rohár | f758ceb | 2020-12-23 19:23:26 +0100 | [diff] [blame] | 254 | b.ne 1b |
Konstantin Porotchkin | ee4fa95 | 2018-10-08 16:50:54 +0300 | [diff] [blame] | 255 | ret |
Konstantin Porotchkin | d8e3957 | 2018-11-14 17:15:08 +0200 | [diff] [blame] | 256 | endfunc console_a3700_core_flush |
| 257 | |
| 258 | /* --------------------------------------------- |
Jimmy Brisson | 39f9eee | 2020-08-05 13:44:05 -0500 | [diff] [blame] | 259 | * void console_a3700_flush(console_t *console) |
Konstantin Porotchkin | d8e3957 | 2018-11-14 17:15:08 +0200 | [diff] [blame] | 260 | * Function to force a write of all buffered |
| 261 | * data that hasn't been output. |
| 262 | * In : x0 - pointer to console_t structure |
Jimmy Brisson | 39f9eee | 2020-08-05 13:44:05 -0500 | [diff] [blame] | 263 | * Out : void. |
Konstantin Porotchkin | d8e3957 | 2018-11-14 17:15:08 +0200 | [diff] [blame] | 264 | * Clobber list : x0, x1 |
| 265 | * --------------------------------------------- |
| 266 | */ |
| 267 | func console_a3700_flush |
Andre Przywara | 0342f40 | 2020-01-25 00:58:35 +0000 | [diff] [blame] | 268 | ldr x0, [x0, #CONSOLE_T_BASE] |
Konstantin Porotchkin | d8e3957 | 2018-11-14 17:15:08 +0200 | [diff] [blame] | 269 | b console_a3700_core_flush |
| 270 | endfunc console_a3700_flush |
| 271 | |