Yann Gautier | 4b0c72a | 2018-07-16 10:54:09 +0200 | [diff] [blame] | 1 | /* |
Yann Gautier | f0f59d0 | 2023-03-29 14:23:49 +0200 | [diff] [blame] | 2 | * Copyright (c) 2018-2023, Arm Limited and Contributors. All rights reserved. |
Yann Gautier | 4b0c72a | 2018-07-16 10:54:09 +0200 | [diff] [blame] | 3 | * |
| 4 | * SPDX-License-Identifier: BSD-3-Clause |
| 5 | */ |
| 6 | #include <asm_macros.S> |
Yann Gautier | 4518c59 | 2018-11-15 09:51:06 +0100 | [diff] [blame] | 7 | #include <assert_macros.S> |
Yann Gautier | 4518c59 | 2018-11-15 09:51:06 +0100 | [diff] [blame] | 8 | #include <console_macros.S> |
Antonio Nino Diaz | e0f9063 | 2018-12-14 00:18:21 +0000 | [diff] [blame] | 9 | #include <drivers/st/stm32_console.h> |
| 10 | #include <drivers/st/stm32_uart_regs.h> |
Yann Gautier | 4b0c72a | 2018-07-16 10:54:09 +0200 | [diff] [blame] | 11 | |
Yann Gautier | 69035a8 | 2018-07-05 16:48:16 +0200 | [diff] [blame] | 12 | #define USART_TIMEOUT 0x1000 |
| 13 | |
Yann Gautier | 4518c59 | 2018-11-15 09:51:06 +0100 | [diff] [blame] | 14 | /* |
| 15 | * "core" functions are low-level implementations that don't require |
| 16 | * writeable memory and are thus safe to call in BL1 crash context. |
| 17 | */ |
| 18 | .globl console_stm32_core_init |
| 19 | .globl console_stm32_core_putc |
| 20 | .globl console_stm32_core_getc |
| 21 | .globl console_stm32_core_flush |
| 22 | |
| 23 | .globl console_stm32_putc |
| 24 | .globl console_stm32_flush |
| 25 | |
| 26 | |
Yann Gautier | 4b0c72a | 2018-07-16 10:54:09 +0200 | [diff] [blame] | 27 | |
| 28 | /* ----------------------------------------------------------------- |
| 29 | * int console_core_init(uintptr_t base_addr, |
| 30 | * unsigned int uart_clk, |
| 31 | * unsigned int baud_rate) |
| 32 | * |
| 33 | * Function to initialize the console without a C Runtime to print |
| 34 | * debug information. This function will be accessed by console_init |
| 35 | * and crash reporting. |
| 36 | * |
| 37 | * In: r0 - console base address |
| 38 | * r1 - Uart clock in Hz |
| 39 | * r2 - Baud rate |
| 40 | * Out: return 1 on success else 0 on error |
| 41 | * Clobber list : r1, r2, r3 |
| 42 | * ----------------------------------------------------------------- |
| 43 | */ |
Yann Gautier | 4518c59 | 2018-11-15 09:51:06 +0100 | [diff] [blame] | 44 | func console_stm32_core_init |
Yann Gautier | 69035a8 | 2018-07-05 16:48:16 +0200 | [diff] [blame] | 45 | /* Check the input base address */ |
| 46 | cmp r0, #0 |
| 47 | beq core_init_fail |
Yann Gautier | 9ee1f9c | 2021-09-09 17:34:53 +0200 | [diff] [blame] | 48 | #if !defined(IMAGE_BL2) |
Yann Gautier | 61f74bb | 2022-09-13 13:55:43 +0200 | [diff] [blame] | 49 | #if STM32MP_RECONFIGURE_CONSOLE |
| 50 | /* UART clock rate is set to 0 in BL32, skip init in that case */ |
| 51 | cmp r1, #0 |
| 52 | beq 1f |
| 53 | #else /* STM32MP_RECONFIGURE_CONSOLE */ |
Yann Gautier | 9ee1f9c | 2021-09-09 17:34:53 +0200 | [diff] [blame] | 54 | /* Skip UART initialization if it is already enabled */ |
| 55 | ldr r3, [r0, #USART_CR1] |
| 56 | ands r3, r3, #USART_CR1_UE |
| 57 | bne 1f |
Yann Gautier | 61f74bb | 2022-09-13 13:55:43 +0200 | [diff] [blame] | 58 | #endif /* STM32MP_RECONFIGURE_CONSOLE */ |
Yann Gautier | 9ee1f9c | 2021-09-09 17:34:53 +0200 | [diff] [blame] | 59 | #endif /* IMAGE_BL2 */ |
Yann Gautier | 69035a8 | 2018-07-05 16:48:16 +0200 | [diff] [blame] | 60 | /* Check baud rate and uart clock for sanity */ |
| 61 | cmp r1, #0 |
| 62 | beq core_init_fail |
| 63 | cmp r2, #0 |
| 64 | beq core_init_fail |
| 65 | /* Disable UART */ |
| 66 | ldr r3, [r0, #USART_CR1] |
| 67 | bic r3, r3, #USART_CR1_UE |
| 68 | str r3, [r0, #USART_CR1] |
| 69 | /* Configure UART */ |
| 70 | orr r3, r3, #(USART_CR1_TE | USART_CR1_FIFOEN) |
| 71 | str r3, [r0, #USART_CR1] |
| 72 | ldr r3, [r0, #USART_CR2] |
| 73 | bic r3, r3, #USART_CR2_STOP |
| 74 | str r3, [r0, #USART_CR2] |
| 75 | /* Divisor = (Uart clock + (baudrate / 2)) / baudrate */ |
Yann Gautier | 376b80b | 2022-02-28 17:29:49 +0100 | [diff] [blame] | 76 | lsr r3, r2, #1 |
Yann Gautier | 69035a8 | 2018-07-05 16:48:16 +0200 | [diff] [blame] | 77 | add r3, r1, r3 |
| 78 | udiv r3, r3, r2 |
Yann Gautier | 2934be5 | 2022-02-28 18:28:06 +0100 | [diff] [blame] | 79 | cmp r3, #16 |
| 80 | bhi 2f |
| 81 | /* Oversampling 8 */ |
| 82 | /* Divisor = (2 * Uart clock + (baudrate / 2)) / baudrate */ |
| 83 | lsr r3, r2, #1 |
| 84 | add r3, r3, r1, lsl #1 |
| 85 | udiv r3, r3, r2 |
| 86 | and r1, r3, #USART_BRR_DIV_FRACTION |
| 87 | lsr r1, r1, #1 |
| 88 | bic r3, r3, #USART_BRR_DIV_FRACTION |
| 89 | orr r3, r3, r1 |
| 90 | ldr r1, [r0, #USART_CR1] |
| 91 | orr r1, r1, #USART_CR1_OVER8 |
| 92 | str r1, [r0, #USART_CR1] |
| 93 | 2: |
Yann Gautier | 69035a8 | 2018-07-05 16:48:16 +0200 | [diff] [blame] | 94 | str r3, [r0, #USART_BRR] |
| 95 | /* Enable UART */ |
| 96 | ldr r3, [r0, #USART_CR1] |
| 97 | orr r3, r3, #USART_CR1_UE |
| 98 | str r3, [r0, #USART_CR1] |
| 99 | /* Check TEACK bit */ |
| 100 | mov r2, #USART_TIMEOUT |
| 101 | teack_loop: |
| 102 | subs r2, r2, #1 |
| 103 | beq core_init_fail |
| 104 | ldr r3, [r0, #USART_ISR] |
| 105 | tst r3, #USART_ISR_TEACK |
| 106 | beq teack_loop |
Yann Gautier | 9ee1f9c | 2021-09-09 17:34:53 +0200 | [diff] [blame] | 107 | 1: |
Yann Gautier | 69035a8 | 2018-07-05 16:48:16 +0200 | [diff] [blame] | 108 | mov r0, #1 |
| 109 | bx lr |
| 110 | core_init_fail: |
| 111 | mov r0, #0 |
Yann Gautier | 4b0c72a | 2018-07-16 10:54:09 +0200 | [diff] [blame] | 112 | bx lr |
Yann Gautier | 4518c59 | 2018-11-15 09:51:06 +0100 | [diff] [blame] | 113 | endfunc console_stm32_core_init |
| 114 | |
Yann Gautier | 4518c59 | 2018-11-15 09:51:06 +0100 | [diff] [blame] | 115 | .globl console_stm32_register |
| 116 | |
| 117 | /* ------------------------------------------------------- |
| 118 | * int console_stm32_register(uintptr_t baseaddr, |
| 119 | * uint32_t clock, uint32_t baud, |
Andre Przywara | 678c6fa | 2020-01-25 00:58:35 +0000 | [diff] [blame] | 120 | * console_t *console); |
Yann Gautier | 4518c59 | 2018-11-15 09:51:06 +0100 | [diff] [blame] | 121 | * Function to initialize and register a new STM32 |
| 122 | * console. Storage passed in for the console struct |
| 123 | * *must* be persistent (i.e. not from the stack). |
| 124 | * In: r0 - UART register base address |
| 125 | * r1 - UART clock in Hz |
| 126 | * r2 - Baud rate |
Andre Przywara | 678c6fa | 2020-01-25 00:58:35 +0000 | [diff] [blame] | 127 | * r3 - pointer to empty console_t struct |
Yann Gautier | 4518c59 | 2018-11-15 09:51:06 +0100 | [diff] [blame] | 128 | * Out: return 1 on success, 0 on error |
| 129 | * Clobber list : r0, r1, r2 |
| 130 | * ------------------------------------------------------- |
| 131 | */ |
| 132 | func console_stm32_register |
| 133 | push {r4, lr} |
| 134 | mov r4, r3 |
| 135 | cmp r4, #0 |
| 136 | beq register_fail |
Andre Przywara | 678c6fa | 2020-01-25 00:58:35 +0000 | [diff] [blame] | 137 | str r0, [r4, #CONSOLE_T_BASE] |
Yann Gautier | 4518c59 | 2018-11-15 09:51:06 +0100 | [diff] [blame] | 138 | |
| 139 | bl console_stm32_core_init |
| 140 | cmp r0, #0 |
| 141 | beq register_fail |
| 142 | |
| 143 | mov r0, r4 |
| 144 | pop {r4, lr} |
| 145 | finish_console_register stm32 putc=1, getc=0, flush=1 |
| 146 | |
| 147 | register_fail: |
| 148 | pop {r4, pc} |
| 149 | endfunc console_stm32_register |
Yann Gautier | 4b0c72a | 2018-07-16 10:54:09 +0200 | [diff] [blame] | 150 | |
| 151 | /* --------------------------------------------------------------- |
| 152 | * int console_core_putc(int c, uintptr_t base_addr) |
| 153 | * |
| 154 | * Function to output a character over the console. It returns the |
| 155 | * character printed on success or -1 on error. |
| 156 | * |
| 157 | * In : r0 - character to be printed |
| 158 | * r1 - console base address |
| 159 | * Out : return -1 on error else return character. |
| 160 | * Clobber list : r2 |
| 161 | * --------------------------------------------------------------- |
| 162 | */ |
Yann Gautier | 4518c59 | 2018-11-15 09:51:06 +0100 | [diff] [blame] | 163 | func console_stm32_core_putc |
Yann Gautier | 69035a8 | 2018-07-05 16:48:16 +0200 | [diff] [blame] | 164 | /* Check the input parameter */ |
| 165 | cmp r1, #0 |
| 166 | beq putc_error |
Yann Gautier | a30e5f7 | 2019-09-04 11:55:10 +0200 | [diff] [blame] | 167 | |
Yann Gautier | 69035a8 | 2018-07-05 16:48:16 +0200 | [diff] [blame] | 168 | /* Check Transmit Data Register Empty */ |
Yann Gautier | a30e5f7 | 2019-09-04 11:55:10 +0200 | [diff] [blame] | 169 | txe_loop: |
Yann Gautier | 69035a8 | 2018-07-05 16:48:16 +0200 | [diff] [blame] | 170 | ldr r2, [r1, #USART_ISR] |
| 171 | tst r2, #USART_ISR_TXE |
Yann Gautier | a30e5f7 | 2019-09-04 11:55:10 +0200 | [diff] [blame] | 172 | beq txe_loop |
Yann Gautier | 69035a8 | 2018-07-05 16:48:16 +0200 | [diff] [blame] | 173 | str r0, [r1, #USART_TDR] |
| 174 | /* Check transmit complete flag */ |
Yann Gautier | a30e5f7 | 2019-09-04 11:55:10 +0200 | [diff] [blame] | 175 | tc_loop: |
Yann Gautier | 69035a8 | 2018-07-05 16:48:16 +0200 | [diff] [blame] | 176 | ldr r2, [r1, #USART_ISR] |
| 177 | tst r2, #USART_ISR_TC |
Yann Gautier | a30e5f7 | 2019-09-04 11:55:10 +0200 | [diff] [blame] | 178 | beq tc_loop |
Yann Gautier | 69035a8 | 2018-07-05 16:48:16 +0200 | [diff] [blame] | 179 | bx lr |
| 180 | putc_error: |
| 181 | mov r0, #-1 |
Yann Gautier | 4b0c72a | 2018-07-16 10:54:09 +0200 | [diff] [blame] | 182 | bx lr |
Yann Gautier | 4518c59 | 2018-11-15 09:51:06 +0100 | [diff] [blame] | 183 | endfunc console_stm32_core_putc |
| 184 | |
| 185 | /* ------------------------------------------------------------ |
Andre Przywara | 678c6fa | 2020-01-25 00:58:35 +0000 | [diff] [blame] | 186 | * int console_stm32_putc(int c, console_t *console) |
Yann Gautier | 4518c59 | 2018-11-15 09:51:06 +0100 | [diff] [blame] | 187 | * Function to output a character over the console. It |
| 188 | * returns the character printed on success or -1 on error. |
| 189 | * In: r0 - character to be printed |
| 190 | * r1 - pointer to console_t structure |
| 191 | * Out : return -1 on error else return character. |
| 192 | * Clobber list: r2 |
| 193 | * ------------------------------------------------------------ |
| 194 | */ |
| 195 | func console_stm32_putc |
| 196 | #if ENABLE_ASSERTIONS |
| 197 | cmp r1, #0 |
| 198 | ASM_ASSERT(ne) |
| 199 | #endif /* ENABLE_ASSERTIONS */ |
Andre Przywara | 678c6fa | 2020-01-25 00:58:35 +0000 | [diff] [blame] | 200 | ldr r1, [r1, #CONSOLE_T_BASE] |
Yann Gautier | 4518c59 | 2018-11-15 09:51:06 +0100 | [diff] [blame] | 201 | b console_stm32_core_putc |
| 202 | endfunc console_stm32_putc |
Yann Gautier | 4b0c72a | 2018-07-16 10:54:09 +0200 | [diff] [blame] | 203 | |
| 204 | /* ----------------------------------------------------------- |
| 205 | * int console_core_getc(uintptr_t base_addr) |
| 206 | * |
| 207 | * Function to get a character from the console. |
| 208 | * It returns the character grabbed on success or -1 on error. |
| 209 | * |
| 210 | * In : r0 - console base address |
| 211 | * Out : return -1. |
| 212 | * Clobber list : r0, r1 |
| 213 | * ----------------------------------------------------------- |
| 214 | */ |
Yann Gautier | 4518c59 | 2018-11-15 09:51:06 +0100 | [diff] [blame] | 215 | func console_stm32_core_getc |
Yann Gautier | 4b0c72a | 2018-07-16 10:54:09 +0200 | [diff] [blame] | 216 | /* Not supported */ |
| 217 | mov r0, #-1 |
| 218 | bx lr |
Yann Gautier | 4518c59 | 2018-11-15 09:51:06 +0100 | [diff] [blame] | 219 | endfunc console_stm32_core_getc |
Yann Gautier | 4b0c72a | 2018-07-16 10:54:09 +0200 | [diff] [blame] | 220 | |
| 221 | /* --------------------------------------------------------------- |
Jimmy Brisson | 39f9eee | 2020-08-05 13:44:05 -0500 | [diff] [blame] | 222 | * void console_core_flush(uintptr_t base_addr) |
Yann Gautier | 4b0c72a | 2018-07-16 10:54:09 +0200 | [diff] [blame] | 223 | * |
| 224 | * Function to force a write of all buffered data that hasn't been |
| 225 | * output. |
| 226 | * |
| 227 | * In : r0 - console base address |
Jimmy Brisson | 39f9eee | 2020-08-05 13:44:05 -0500 | [diff] [blame] | 228 | * Out : void. |
Yann Gautier | 4b0c72a | 2018-07-16 10:54:09 +0200 | [diff] [blame] | 229 | * Clobber list : r0, r1 |
| 230 | * --------------------------------------------------------------- |
| 231 | */ |
Yann Gautier | 4518c59 | 2018-11-15 09:51:06 +0100 | [diff] [blame] | 232 | func console_stm32_core_flush |
Jimmy Brisson | 39f9eee | 2020-08-05 13:44:05 -0500 | [diff] [blame] | 233 | #if ENABLE_ASSERTIONS |
Yann Gautier | 69035a8 | 2018-07-05 16:48:16 +0200 | [diff] [blame] | 234 | cmp r0, #0 |
Jimmy Brisson | 39f9eee | 2020-08-05 13:44:05 -0500 | [diff] [blame] | 235 | ASM_ASSERT(ne) |
| 236 | #endif /* ENABLE_ASSERTIONS */ |
Yann Gautier | f0f59d0 | 2023-03-29 14:23:49 +0200 | [diff] [blame] | 237 | /* Skip flush if UART is not enabled */ |
| 238 | ldr r1, [r0, #USART_CR1] |
Yann Gautier | 3437747 | 2023-06-28 17:52:08 +0200 | [diff] [blame] | 239 | tst r1, #USART_CR1_UE |
Yann Gautier | f0f59d0 | 2023-03-29 14:23:49 +0200 | [diff] [blame] | 240 | beq 1f |
Yann Gautier | 69035a8 | 2018-07-05 16:48:16 +0200 | [diff] [blame] | 241 | /* Check Transmit Data Register Empty */ |
| 242 | txe_loop_3: |
| 243 | ldr r1, [r0, #USART_ISR] |
| 244 | tst r1, #USART_ISR_TXE |
| 245 | beq txe_loop_3 |
Yann Gautier | f0f59d0 | 2023-03-29 14:23:49 +0200 | [diff] [blame] | 246 | 1: |
Yann Gautier | 4b0c72a | 2018-07-16 10:54:09 +0200 | [diff] [blame] | 247 | bx lr |
Yann Gautier | 4518c59 | 2018-11-15 09:51:06 +0100 | [diff] [blame] | 248 | endfunc console_stm32_core_flush |
| 249 | |
| 250 | /* ------------------------------------------------------ |
Jimmy Brisson | 39f9eee | 2020-08-05 13:44:05 -0500 | [diff] [blame] | 251 | * void console_stm32_flush(console_t *console) |
Yann Gautier | 4518c59 | 2018-11-15 09:51:06 +0100 | [diff] [blame] | 252 | * Function to force a write of all buffered |
| 253 | * data that hasn't been output. |
| 254 | * In : r0 - pointer to console_t structure |
Jimmy Brisson | 39f9eee | 2020-08-05 13:44:05 -0500 | [diff] [blame] | 255 | * Out : void. |
Yann Gautier | 4518c59 | 2018-11-15 09:51:06 +0100 | [diff] [blame] | 256 | * Clobber list: r0, r1 |
| 257 | * ------------------------------------------------------ |
| 258 | */ |
| 259 | func console_stm32_flush |
| 260 | #if ENABLE_ASSERTIONS |
| 261 | cmp r0, #0 |
| 262 | ASM_ASSERT(ne) |
| 263 | #endif /* ENABLE_ASSERTIONS */ |
Andre Przywara | 678c6fa | 2020-01-25 00:58:35 +0000 | [diff] [blame] | 264 | ldr r0, [r0, #CONSOLE_T_BASE] |
Yann Gautier | 4518c59 | 2018-11-15 09:51:06 +0100 | [diff] [blame] | 265 | b console_stm32_core_flush |
| 266 | endfunc console_stm32_flush |