Varun Wadekar | 68e4ee4 | 2017-11-15 15:48:51 -0800 | [diff] [blame] | 1 | /* |
Ambroise Vincent | 09a22e7 | 2019-05-29 14:04:16 +0100 | [diff] [blame] | 2 | * Copyright (c) 2017-2019, ARM Limited and Contributors. All rights reserved. |
Varun Wadekar | c5e665c | 2018-06-19 17:07:08 -0700 | [diff] [blame] | 3 | * Copyright (c) 2020, NVIDIA Corporation. All rights reserved. |
Varun Wadekar | 68e4ee4 | 2017-11-15 15:48:51 -0800 | [diff] [blame] | 4 | * |
| 5 | * SPDX-License-Identifier: BSD-3-Clause |
| 6 | */ |
| 7 | #include <asm_macros.S> |
Ambroise Vincent | 09a22e7 | 2019-05-29 14:04:16 +0100 | [diff] [blame] | 8 | #include <console_macros.S> |
Varun Wadekar | 68e4ee4 | 2017-11-15 15:48:51 -0800 | [diff] [blame] | 9 | |
| 10 | #define CONSOLE_NUM_BYTES_SHIFT 24 |
| 11 | #define CONSOLE_FLUSH_DATA_TO_PORT (1 << 26) |
| 12 | #define CONSOLE_RING_DOORBELL (1 << 31) |
| 13 | #define CONSOLE_IS_BUSY (1 << 31) |
Varun Wadekar | 3cb0d8d | 2019-09-13 16:31:09 -0700 | [diff] [blame] | 14 | #define CONSOLE_TIMEOUT 0xC000 /* 50 ms */ |
Varun Wadekar | 68e4ee4 | 2017-11-15 15:48:51 -0800 | [diff] [blame] | 15 | |
| 16 | /* |
| 17 | * This file contains a driver implementation to make use of the |
| 18 | * real console implementation provided by the SPE firmware running |
| 19 | * SoCs after Tegra186. |
| 20 | * |
| 21 | * This console is shared by multiple components and the SPE firmware |
| 22 | * finally displays everything on the UART port. |
| 23 | */ |
| 24 | |
Ambroise Vincent | 09a22e7 | 2019-05-29 14:04:16 +0100 | [diff] [blame] | 25 | .globl console_spe_core_init |
| 26 | .globl console_spe_core_putc |
| 27 | .globl console_spe_core_getc |
| 28 | .globl console_spe_core_flush |
| 29 | .globl console_spe_putc |
| 30 | .globl console_spe_getc |
| 31 | .globl console_spe_flush |
| 32 | .globl console_spe_register |
Varun Wadekar | 68e4ee4 | 2017-11-15 15:48:51 -0800 | [diff] [blame] | 33 | |
Varun Wadekar | c5e665c | 2018-06-19 17:07:08 -0700 | [diff] [blame] | 34 | .macro check_if_console_is_ready base, tmp1, tmp2, label |
| 35 | /* wait until spe is ready or timeout expires */ |
| 36 | mrs \tmp2, cntps_tval_el1 |
| 37 | 1: ldr \tmp1, [\base] |
| 38 | and \tmp1, \tmp1, #CONSOLE_IS_BUSY |
| 39 | cbz \tmp1, 2f |
| 40 | mrs \tmp1, cntps_tval_el1 |
| 41 | sub \tmp1, \tmp2, \tmp1 |
| 42 | cmp \tmp1, #CONSOLE_TIMEOUT |
| 43 | b.lt 1b |
| 44 | b \label |
| 45 | 2: |
| 46 | .endm |
| 47 | |
Ambroise Vincent | 09a22e7 | 2019-05-29 14:04:16 +0100 | [diff] [blame] | 48 | /* ------------------------------------------------- |
| 49 | * int console_spe_register(uintptr_t baseaddr, |
| 50 | * uint32_t clock, uint32_t baud, |
Andre Przywara | abe890f | 2020-01-25 00:58:35 +0000 | [diff] [blame] | 51 | * console_t *console); |
Ambroise Vincent | 09a22e7 | 2019-05-29 14:04:16 +0100 | [diff] [blame] | 52 | * Function to initialize and register a new spe |
| 53 | * console. Storage passed in for the console struct |
| 54 | * *must* be persistent (i.e. not from the stack). |
| 55 | * In: x0 - UART register base address |
| 56 | * w1 - UART clock in Hz |
Varun Wadekar | 68e4ee4 | 2017-11-15 15:48:51 -0800 | [diff] [blame] | 57 | * w2 - Baud rate |
Andre Przywara | abe890f | 2020-01-25 00:58:35 +0000 | [diff] [blame] | 58 | * x3 - pointer to empty console_t struct |
Ambroise Vincent | 09a22e7 | 2019-05-29 14:04:16 +0100 | [diff] [blame] | 59 | * Out: return 1 on success, 0 on error |
| 60 | * Clobber list : x0, x1, x2, x6, x7, x14 |
| 61 | * ------------------------------------------------- |
Varun Wadekar | 68e4ee4 | 2017-11-15 15:48:51 -0800 | [diff] [blame] | 62 | */ |
Ambroise Vincent | 09a22e7 | 2019-05-29 14:04:16 +0100 | [diff] [blame] | 63 | func console_spe_register |
Varun Wadekar | c5e665c | 2018-06-19 17:07:08 -0700 | [diff] [blame] | 64 | /* Check the input base address */ |
| 65 | cbz x0, register_fail |
| 66 | |
| 67 | /* Dont use clock or baud rate, so ok to overwrite them */ |
| 68 | check_if_console_is_ready x0, x1, x2, register_fail |
| 69 | |
Ambroise Vincent | 09a22e7 | 2019-05-29 14:04:16 +0100 | [diff] [blame] | 70 | cbz x3, register_fail |
Varun Wadekar | aeee490 | 2020-03-04 13:47:13 -0800 | [diff] [blame] | 71 | str x0, [x3, #CONSOLE_T_BASE] |
Ambroise Vincent | 09a22e7 | 2019-05-29 14:04:16 +0100 | [diff] [blame] | 72 | mov x0, x3 |
| 73 | finish_console_register spe putc=1, getc=1, flush=1 |
| 74 | |
| 75 | register_fail: |
Varun Wadekar | 68e4ee4 | 2017-11-15 15:48:51 -0800 | [diff] [blame] | 76 | mov w0, wzr |
| 77 | ret |
Ambroise Vincent | 09a22e7 | 2019-05-29 14:04:16 +0100 | [diff] [blame] | 78 | endfunc console_spe_register |
Varun Wadekar | 68e4ee4 | 2017-11-15 15:48:51 -0800 | [diff] [blame] | 79 | |
| 80 | /* -------------------------------------------------------- |
Ambroise Vincent | 09a22e7 | 2019-05-29 14:04:16 +0100 | [diff] [blame] | 81 | * int console_spe_core_putc(int c, uintptr_t base_addr) |
Varun Wadekar | 68e4ee4 | 2017-11-15 15:48:51 -0800 | [diff] [blame] | 82 | * Function to output a character over the console. It |
| 83 | * returns the character printed on success or -1 on error. |
| 84 | * In : w0 - character to be printed |
| 85 | * x1 - console base address |
| 86 | * Out : return -1 on error else return character. |
Varun Wadekar | c5e665c | 2018-06-19 17:07:08 -0700 | [diff] [blame] | 87 | * Clobber list : x2, x3 |
Varun Wadekar | 68e4ee4 | 2017-11-15 15:48:51 -0800 | [diff] [blame] | 88 | * -------------------------------------------------------- |
| 89 | */ |
Ambroise Vincent | 09a22e7 | 2019-05-29 14:04:16 +0100 | [diff] [blame] | 90 | func console_spe_core_putc |
Varun Wadekar | 68e4ee4 | 2017-11-15 15:48:51 -0800 | [diff] [blame] | 91 | /* Check the input parameter */ |
| 92 | cbz x1, putc_error |
| 93 | |
Varun Wadekar | cbdcbd1 | 2018-05-07 11:21:57 -0700 | [diff] [blame] | 94 | /* Prepend '\r' to '\n' */ |
| 95 | cmp w0, #0xA |
Varun Wadekar | c5e665c | 2018-06-19 17:07:08 -0700 | [diff] [blame] | 96 | b.ne not_eol |
Varun Wadekar | cbdcbd1 | 2018-05-07 11:21:57 -0700 | [diff] [blame] | 97 | |
Varun Wadekar | c5e665c | 2018-06-19 17:07:08 -0700 | [diff] [blame] | 98 | check_if_console_is_ready x1, x2, x3, putc_error |
Varun Wadekar | 68e4ee4 | 2017-11-15 15:48:51 -0800 | [diff] [blame] | 99 | |
| 100 | /* spe is ready */ |
Varun Wadekar | cbdcbd1 | 2018-05-07 11:21:57 -0700 | [diff] [blame] | 101 | mov w2, #0xD /* '\r' */ |
| 102 | and w2, w2, #0xFF |
Varun Wadekar | 3cb0d8d | 2019-09-13 16:31:09 -0700 | [diff] [blame] | 103 | mov w3, #(CONSOLE_RING_DOORBELL | (1 << CONSOLE_NUM_BYTES_SHIFT)) |
Varun Wadekar | cbdcbd1 | 2018-05-07 11:21:57 -0700 | [diff] [blame] | 104 | orr w2, w2, w3 |
| 105 | str w2, [x1] |
| 106 | |
Varun Wadekar | c5e665c | 2018-06-19 17:07:08 -0700 | [diff] [blame] | 107 | not_eol: |
| 108 | check_if_console_is_ready x1, x2, x3, putc_error |
Varun Wadekar | cbdcbd1 | 2018-05-07 11:21:57 -0700 | [diff] [blame] | 109 | |
| 110 | /* spe is ready */ |
Varun Wadekar | 68e4ee4 | 2017-11-15 15:48:51 -0800 | [diff] [blame] | 111 | mov w2, w0 |
| 112 | and w2, w2, #0xFF |
Varun Wadekar | 3cb0d8d | 2019-09-13 16:31:09 -0700 | [diff] [blame] | 113 | mov w3, #(CONSOLE_RING_DOORBELL | (1 << CONSOLE_NUM_BYTES_SHIFT)) |
Varun Wadekar | 68e4ee4 | 2017-11-15 15:48:51 -0800 | [diff] [blame] | 114 | orr w2, w2, w3 |
| 115 | str w2, [x1] |
| 116 | |
| 117 | ret |
| 118 | putc_error: |
| 119 | mov w0, #-1 |
| 120 | ret |
Ambroise Vincent | 09a22e7 | 2019-05-29 14:04:16 +0100 | [diff] [blame] | 121 | endfunc console_spe_core_putc |
| 122 | |
| 123 | /* -------------------------------------------------------- |
Andre Przywara | abe890f | 2020-01-25 00:58:35 +0000 | [diff] [blame] | 124 | * int console_spe_putc(int c, console_t *console) |
Ambroise Vincent | 09a22e7 | 2019-05-29 14:04:16 +0100 | [diff] [blame] | 125 | * Function to output a character over the console. It |
| 126 | * returns the character printed on success or -1 on error. |
| 127 | * In : w0 - character to be printed |
| 128 | * x1 - pointer to console_t structure |
| 129 | * Out : return -1 on error else return character. |
| 130 | * Clobber list : x2 |
| 131 | * -------------------------------------------------------- |
| 132 | */ |
| 133 | func console_spe_putc |
Varun Wadekar | aeee490 | 2020-03-04 13:47:13 -0800 | [diff] [blame] | 134 | ldr x1, [x1, #CONSOLE_T_BASE] |
Ambroise Vincent | 09a22e7 | 2019-05-29 14:04:16 +0100 | [diff] [blame] | 135 | b console_spe_core_putc |
| 136 | endfunc console_spe_putc |
Varun Wadekar | 68e4ee4 | 2017-11-15 15:48:51 -0800 | [diff] [blame] | 137 | |
| 138 | /* --------------------------------------------- |
Andre Przywara | abe890f | 2020-01-25 00:58:35 +0000 | [diff] [blame] | 139 | * int console_spe_getc(console_t *console) |
Varun Wadekar | 68e4ee4 | 2017-11-15 15:48:51 -0800 | [diff] [blame] | 140 | * Function to get a character from the console. |
| 141 | * It returns the character grabbed on success |
Ambroise Vincent | 09a22e7 | 2019-05-29 14:04:16 +0100 | [diff] [blame] | 142 | * or -1 if no character is available. |
| 143 | * In : x0 - pointer to console_t structure |
| 144 | * Out: w0 - character if available, else -1 |
Varun Wadekar | 68e4ee4 | 2017-11-15 15:48:51 -0800 | [diff] [blame] | 145 | * Clobber list : x0, x1 |
| 146 | * --------------------------------------------- |
| 147 | */ |
Ambroise Vincent | 09a22e7 | 2019-05-29 14:04:16 +0100 | [diff] [blame] | 148 | func console_spe_getc |
Varun Wadekar | 68e4ee4 | 2017-11-15 15:48:51 -0800 | [diff] [blame] | 149 | mov w0, #-1 |
| 150 | ret |
Ambroise Vincent | 09a22e7 | 2019-05-29 14:04:16 +0100 | [diff] [blame] | 151 | endfunc console_spe_getc |
Varun Wadekar | 68e4ee4 | 2017-11-15 15:48:51 -0800 | [diff] [blame] | 152 | |
Ambroise Vincent | 09a22e7 | 2019-05-29 14:04:16 +0100 | [diff] [blame] | 153 | /* ------------------------------------------------- |
| 154 | * int console_spe_core_flush(uintptr_t base_addr) |
Varun Wadekar | 68e4ee4 | 2017-11-15 15:48:51 -0800 | [diff] [blame] | 155 | * Function to force a write of all buffered |
| 156 | * data that hasn't been output. |
| 157 | * In : x0 - console base address |
| 158 | * Out : return -1 on error else return 0. |
| 159 | * Clobber list : x0, x1 |
Ambroise Vincent | 09a22e7 | 2019-05-29 14:04:16 +0100 | [diff] [blame] | 160 | * ------------------------------------------------- |
Varun Wadekar | 68e4ee4 | 2017-11-15 15:48:51 -0800 | [diff] [blame] | 161 | */ |
Ambroise Vincent | 09a22e7 | 2019-05-29 14:04:16 +0100 | [diff] [blame] | 162 | func console_spe_core_flush |
Varun Wadekar | 68e4ee4 | 2017-11-15 15:48:51 -0800 | [diff] [blame] | 163 | cbz x0, flush_error |
| 164 | |
| 165 | /* flush console */ |
Varun Wadekar | 3cb0d8d | 2019-09-13 16:31:09 -0700 | [diff] [blame] | 166 | mov w1, #(CONSOLE_RING_DOORBELL | CONSOLE_FLUSH_DATA_TO_PORT) |
Varun Wadekar | 68e4ee4 | 2017-11-15 15:48:51 -0800 | [diff] [blame] | 167 | str w1, [x0] |
| 168 | mov w0, #0 |
| 169 | ret |
| 170 | flush_error: |
| 171 | mov w0, #-1 |
| 172 | ret |
Ambroise Vincent | 09a22e7 | 2019-05-29 14:04:16 +0100 | [diff] [blame] | 173 | endfunc console_spe_core_flush |
| 174 | |
| 175 | /* --------------------------------------------- |
Andre Przywara | abe890f | 2020-01-25 00:58:35 +0000 | [diff] [blame] | 176 | * int console_spe_flush(console_t *console) |
Ambroise Vincent | 09a22e7 | 2019-05-29 14:04:16 +0100 | [diff] [blame] | 177 | * Function to force a write of all buffered |
| 178 | * data that hasn't been output. |
| 179 | * In : x0 - pointer to console_t structure |
| 180 | * Out : return -1 on error else return 0. |
| 181 | * Clobber list : x0, x1 |
| 182 | * --------------------------------------------- |
| 183 | */ |
| 184 | func console_spe_flush |
Varun Wadekar | aeee490 | 2020-03-04 13:47:13 -0800 | [diff] [blame] | 185 | ldr x0, [x0, #CONSOLE_T_BASE] |
Ambroise Vincent | 09a22e7 | 2019-05-29 14:04:16 +0100 | [diff] [blame] | 186 | b console_spe_core_flush |
| 187 | endfunc console_spe_flush |