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 | /* |
| 63 | * Wait for the TX FIFO to be empty. If wait for 20ms, the TX FIFO is |
| 64 | * still not empty, TX FIFO will reset by all means. |
| 65 | */ |
| 66 | mov w1, #20 /* max time out 20ms */ |
| 67 | 2: |
| 68 | /* Check whether TX FIFO is empty */ |
| 69 | ldr w3, [x0, #UART_STATUS_REG] |
| 70 | and w3, w3, #UARTLSR_TXFIFOEMPTY |
| 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 |
| 199 | * or -1 on error. |
| 200 | * In : w0 - console base address |
| 201 | * Out : return -1 on error else return character. |
| 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 |
Konstantin Porotchkin | ee4fa95 | 2018-10-08 16:50:54 +0300 | [diff] [blame] | 206 | mov w0, #-1 |
| 207 | ret |
Konstantin Porotchkin | d8e3957 | 2018-11-14 17:15:08 +0200 | [diff] [blame] | 208 | endfunc console_a3700_core_getc |
| 209 | |
| 210 | /* --------------------------------------------- |
Andre Przywara | 0342f40 | 2020-01-25 00:58:35 +0000 | [diff] [blame] | 211 | * int console_a3700_getc(console_t *console) |
Konstantin Porotchkin | d8e3957 | 2018-11-14 17:15:08 +0200 | [diff] [blame] | 212 | * Function to get a character from the console. |
| 213 | * It returns the character grabbed on success |
| 214 | * or -1 on if no character is available. |
| 215 | * In : x0 - pointer to console_t structure |
| 216 | * Out : w0 - character if available, else -1 |
| 217 | * Clobber list : x0, x1 |
| 218 | * --------------------------------------------- |
| 219 | */ |
| 220 | func console_a3700_getc |
Andre Przywara | 0342f40 | 2020-01-25 00:58:35 +0000 | [diff] [blame] | 221 | ldr x0, [x0, #CONSOLE_T_BASE] |
Konstantin Porotchkin | d8e3957 | 2018-11-14 17:15:08 +0200 | [diff] [blame] | 222 | b console_a3700_core_getc |
| 223 | endfunc console_a3700_getc |
Konstantin Porotchkin | ee4fa95 | 2018-10-08 16:50:54 +0300 | [diff] [blame] | 224 | |
| 225 | /* --------------------------------------------- |
Jimmy Brisson | 39f9eee | 2020-08-05 13:44:05 -0500 | [diff] [blame] | 226 | * void console_a3700_core_flush(uintptr_t base_addr) |
Konstantin Porotchkin | ee4fa95 | 2018-10-08 16:50:54 +0300 | [diff] [blame] | 227 | * Function to force a write of all buffered |
| 228 | * data that hasn't been output. |
| 229 | * In : x0 - console base address |
Jimmy Brisson | 39f9eee | 2020-08-05 13:44:05 -0500 | [diff] [blame] | 230 | * Out : void. |
Konstantin Porotchkin | ee4fa95 | 2018-10-08 16:50:54 +0300 | [diff] [blame] | 231 | * Clobber list : x0, x1 |
| 232 | * --------------------------------------------- |
| 233 | */ |
Konstantin Porotchkin | d8e3957 | 2018-11-14 17:15:08 +0200 | [diff] [blame] | 234 | func console_a3700_core_flush |
Konstantin Porotchkin | ee4fa95 | 2018-10-08 16:50:54 +0300 | [diff] [blame] | 235 | ret |
Konstantin Porotchkin | d8e3957 | 2018-11-14 17:15:08 +0200 | [diff] [blame] | 236 | endfunc console_a3700_core_flush |
| 237 | |
| 238 | /* --------------------------------------------- |
Jimmy Brisson | 39f9eee | 2020-08-05 13:44:05 -0500 | [diff] [blame] | 239 | * void console_a3700_flush(console_t *console) |
Konstantin Porotchkin | d8e3957 | 2018-11-14 17:15:08 +0200 | [diff] [blame] | 240 | * Function to force a write of all buffered |
| 241 | * data that hasn't been output. |
| 242 | * In : x0 - pointer to console_t structure |
Jimmy Brisson | 39f9eee | 2020-08-05 13:44:05 -0500 | [diff] [blame] | 243 | * Out : void. |
Konstantin Porotchkin | d8e3957 | 2018-11-14 17:15:08 +0200 | [diff] [blame] | 244 | * Clobber list : x0, x1 |
| 245 | * --------------------------------------------- |
| 246 | */ |
| 247 | func console_a3700_flush |
Andre Przywara | 0342f40 | 2020-01-25 00:58:35 +0000 | [diff] [blame] | 248 | ldr x0, [x0, #CONSOLE_T_BASE] |
Konstantin Porotchkin | d8e3957 | 2018-11-14 17:15:08 +0200 | [diff] [blame] | 249 | b console_a3700_core_flush |
| 250 | endfunc console_a3700_flush |
| 251 | |