2 * Copyright (c) 2018, ARM Limited and Contributors. All rights reserved.
4 * SPDX-License-Identifier: BSD-3-Clause
6 #include <asm_macros.S>
7 #include <assert_macros.S>
8 #define USE_FINISH_CONSOLE_REG_2
9 #include <console_macros.S>
10 #include <stm32_console.h>
11 #include <stm32_uart_regs.h>
13 #define USART_TIMEOUT 0x1000
16 * "core" functions are low-level implementations that don't require
17 * writeable memory and are thus safe to call in BL1 crash context.
19 .globl console_stm32_core_init
20 .globl console_stm32_core_putc
21 .globl console_stm32_core_getc
22 .globl console_stm32_core_flush
24 .globl console_stm32_putc
25 .globl console_stm32_flush
29 /* -----------------------------------------------------------------
30 * int console_core_init(uintptr_t base_addr,
31 * unsigned int uart_clk,
32 * unsigned int baud_rate)
34 * Function to initialize the console without a C Runtime to print
35 * debug information. This function will be accessed by console_init
36 * and crash reporting.
38 * In: r0 - console base address
39 * r1 - Uart clock in Hz
41 * Out: return 1 on success else 0 on error
42 * Clobber list : r1, r2, r3
43 * -----------------------------------------------------------------
45 func console_stm32_core_init
46 /* Check the input base address */
49 #if defined(IMAGE_BL2)
50 /* Check baud rate and uart clock for sanity */
56 ldr r3, [r0, #USART_CR1]
57 bic r3, r3, #USART_CR1_UE
58 str r3, [r0, #USART_CR1]
60 orr r3, r3, #(USART_CR1_TE | USART_CR1_FIFOEN)
61 str r3, [r0, #USART_CR1]
62 ldr r3, [r0, #USART_CR2]
63 bic r3, r3, #USART_CR2_STOP
64 str r3, [r0, #USART_CR2]
65 /* Divisor = (Uart clock + (baudrate / 2)) / baudrate */
69 str r3, [r0, #USART_BRR]
71 ldr r3, [r0, #USART_CR1]
72 orr r3, r3, #USART_CR1_UE
73 str r3, [r0, #USART_CR1]
75 mov r2, #USART_TIMEOUT
79 ldr r3, [r0, #USART_ISR]
80 tst r3, #USART_ISR_TEACK
82 #endif /* IMAGE_BL2 */
88 endfunc console_stm32_core_init
90 .globl console_stm32_register
92 /* -------------------------------------------------------
93 * int console_stm32_register(uintptr_t baseaddr,
94 * uint32_t clock, uint32_t baud,
95 * struct console_stm32 *console);
96 * Function to initialize and register a new STM32
97 * console. Storage passed in for the console struct
98 * *must* be persistent (i.e. not from the stack).
99 * In: r0 - UART register base address
100 * r1 - UART clock in Hz
102 * r3 - pointer to empty console_stm32 struct
103 * Out: return 1 on success, 0 on error
104 * Clobber list : r0, r1, r2
105 * -------------------------------------------------------
107 func console_stm32_register
112 str r0, [r4, #CONSOLE_T_STM32_BASE]
114 bl console_stm32_core_init
120 finish_console_register stm32 putc=1, getc=0, flush=1
124 endfunc console_stm32_register
126 /* ---------------------------------------------------------------
127 * int console_core_putc(int c, uintptr_t base_addr)
129 * Function to output a character over the console. It returns the
130 * character printed on success or -1 on error.
132 * In : r0 - character to be printed
133 * r1 - console base address
134 * Out : return -1 on error else return character.
136 * ---------------------------------------------------------------
138 func console_stm32_core_putc
139 /* Check the input parameter */
142 /* Prepend '\r' to '\n' */
146 /* Check Transmit Data Register Empty */
148 ldr r2, [r1, #USART_ISR]
149 tst r2, #USART_ISR_TXE
152 str r2, [r1, #USART_TDR]
153 /* Check transmit complete flag */
155 ldr r2, [r1, #USART_ISR]
156 tst r2, #USART_ISR_TC
159 /* Check Transmit Data Register Empty */
161 ldr r2, [r1, #USART_ISR]
162 tst r2, #USART_ISR_TXE
164 str r0, [r1, #USART_TDR]
165 /* Check transmit complete flag */
167 ldr r2, [r1, #USART_ISR]
168 tst r2, #USART_ISR_TC
174 endfunc console_stm32_core_putc
176 /* ------------------------------------------------------------
177 * int console_stm32_putc(int c, struct console_stm32 *console)
178 * Function to output a character over the console. It
179 * returns the character printed on success or -1 on error.
180 * In: r0 - character to be printed
181 * r1 - pointer to console_t structure
182 * Out : return -1 on error else return character.
184 * ------------------------------------------------------------
186 func console_stm32_putc
187 #if ENABLE_ASSERTIONS
190 #endif /* ENABLE_ASSERTIONS */
191 ldr r1, [r1, #CONSOLE_T_STM32_BASE]
192 b console_stm32_core_putc
193 endfunc console_stm32_putc
195 /* -----------------------------------------------------------
196 * int console_core_getc(uintptr_t base_addr)
198 * Function to get a character from the console.
199 * It returns the character grabbed on success or -1 on error.
201 * In : r0 - console base address
203 * Clobber list : r0, r1
204 * -----------------------------------------------------------
206 func console_stm32_core_getc
210 endfunc console_stm32_core_getc
212 /* ---------------------------------------------------------------
213 * int console_core_flush(uintptr_t base_addr)
215 * Function to force a write of all buffered data that hasn't been
218 * In : r0 - console base address
219 * Out : return -1 on error else return 0.
220 * Clobber list : r0, r1
221 * ---------------------------------------------------------------
223 func console_stm32_core_flush
226 /* Check Transmit Data Register Empty */
228 ldr r1, [r0, #USART_ISR]
229 tst r1, #USART_ISR_TXE
236 endfunc console_stm32_core_flush
238 /* ------------------------------------------------------
239 * int console_stm32_flush(struct console_stm32 *console)
240 * Function to force a write of all buffered
241 * data that hasn't been output.
242 * In : r0 - pointer to console_t structure
243 * Out : return -1 on error else return 0.
244 * Clobber list: r0, r1
245 * ------------------------------------------------------
247 func console_stm32_flush
248 #if ENABLE_ASSERTIONS
251 #endif /* ENABLE_ASSERTIONS */
252 ldr r0, [r0, #CONSOLE_T_STM32_BASE]
253 b console_stm32_core_flush
254 endfunc console_stm32_flush