xref: /rk3399_ARM-atf/drivers/st/uart/aarch32/stm32_console.S (revision 8244d2260da69e49e97a6d19aa18c84b4da0a3e4)
1/*
2 * Copyright (c) 2018, ARM Limited and Contributors. All rights reserved.
3 *
4 * SPDX-License-Identifier: BSD-3-Clause
5 */
6#include <asm_macros.S>
7#include <stm32_uart_regs.h>
8
9#define USART_TIMEOUT		0x1000
10
11	.globl	console_core_init
12	.globl	console_core_putc
13	.globl	console_core_getc
14	.globl	console_core_flush
15
16	/* -----------------------------------------------------------------
17	 * int console_core_init(uintptr_t base_addr,
18	 *			 unsigned int uart_clk,
19	 *			 unsigned int baud_rate)
20	 *
21	 * Function to initialize the console without a C Runtime to print
22	 * debug information. This function will be accessed by console_init
23	 * and crash reporting.
24	 *
25	 * In: r0 - console base address
26	 *     r1 - Uart clock in Hz
27	 *     r2 - Baud rate
28	 * Out: return 1 on success else 0 on error
29	 * Clobber list : r1, r2, r3
30	 * -----------------------------------------------------------------
31	 */
32func console_core_init
33	/* Check the input base address */
34	cmp	r0, #0
35	beq	core_init_fail
36#if defined(IMAGE_BL2)
37	/* Check baud rate and uart clock for sanity */
38	cmp	r1, #0
39	beq	core_init_fail
40	cmp	r2, #0
41	beq	core_init_fail
42	/* Disable UART */
43	ldr	r3, [r0, #USART_CR1]
44	bic	r3, r3, #USART_CR1_UE
45	str	r3, [r0, #USART_CR1]
46	/* Configure UART */
47	orr	r3, r3, #(USART_CR1_TE | USART_CR1_FIFOEN)
48	str	r3, [r0, #USART_CR1]
49	ldr	r3, [r0, #USART_CR2]
50	bic	r3, r3, #USART_CR2_STOP
51	str	r3, [r0, #USART_CR2]
52	/* Divisor =  (Uart clock + (baudrate / 2)) / baudrate */
53	lsl	r3, r2, #1
54	add	r3, r1, r3
55	udiv	r3, r3, r2
56	str	r3, [r0, #USART_BRR]
57	/* Enable UART */
58	ldr	r3, [r0, #USART_CR1]
59	orr	r3, r3, #USART_CR1_UE
60	str	r3, [r0, #USART_CR1]
61	/* Check TEACK bit */
62	mov	r2, #USART_TIMEOUT
63teack_loop:
64	subs	r2, r2, #1
65	beq	core_init_fail
66	ldr	r3, [r0, #USART_ISR]
67	tst	r3, #USART_ISR_TEACK
68	beq	teack_loop
69#endif /* IMAGE_BL2 */
70	mov	r0, #1
71	bx	lr
72core_init_fail:
73	mov	r0, #0
74	bx	lr
75endfunc console_core_init
76
77	/* ---------------------------------------------------------------
78	 * int console_core_putc(int c, uintptr_t base_addr)
79	 *
80	 * Function to output a character over the console. It returns the
81	 * character printed on success or -1 on error.
82	 *
83	 * In : r0 - character to be printed
84	 *      r1 - console base address
85	 * Out : return -1 on error else return character.
86	 * Clobber list : r2
87	 * ---------------------------------------------------------------
88	 */
89func console_core_putc
90	/* Check the input parameter */
91	cmp	r1, #0
92	beq	putc_error
93	/* Prepend '\r' to '\n' */
94	cmp	r0, #0xA
95	bne	2f
961:
97	/* Check Transmit Data Register Empty */
98txe_loop_1:
99	ldr	r2, [r1, #USART_ISR]
100	tst	r2, #USART_ISR_TXE
101	beq	txe_loop_1
102	mov	r2, #0xD
103	str	r2, [r1, #USART_TDR]
104	/* Check transmit complete flag */
105tc_loop_1:
106	ldr	r2, [r1, #USART_ISR]
107	tst	r2, #USART_ISR_TC
108	beq	tc_loop_1
1092:
110	/* Check Transmit Data Register Empty */
111txe_loop_2:
112	ldr	r2, [r1, #USART_ISR]
113	tst	r2, #USART_ISR_TXE
114	beq	txe_loop_2
115	str	r0, [r1, #USART_TDR]
116	/* Check transmit complete flag */
117tc_loop_2:
118	ldr	r2, [r1, #USART_ISR]
119	tst	r2, #USART_ISR_TC
120	beq	tc_loop_2
121	bx	lr
122putc_error:
123	mov	r0, #-1
124	bx	lr
125endfunc console_core_putc
126
127	/* -----------------------------------------------------------
128	 * int console_core_getc(uintptr_t base_addr)
129	 *
130	 * Function to get a character from the console.
131	 * It returns the character grabbed on success or -1 on error.
132	 *
133	 * In : r0 - console base address
134	 * Out : return -1.
135	 * Clobber list : r0, r1
136	 * -----------------------------------------------------------
137	 */
138func console_core_getc
139	/* Not supported */
140	mov	r0, #-1
141	bx	lr
142endfunc console_core_getc
143
144	/* ---------------------------------------------------------------
145	 * int console_core_flush(uintptr_t base_addr)
146	 *
147	 * Function to force a write of all buffered data that hasn't been
148	 * output.
149	 *
150	 * In : r0 - console base address
151	 * Out : return -1 on error else return 0.
152	 * Clobber list : r0, r1
153	 * ---------------------------------------------------------------
154	 */
155func console_core_flush
156	cmp	r0, #0
157	beq	flush_error
158	/* Check Transmit Data Register Empty */
159txe_loop_3:
160	ldr	r1, [r0, #USART_ISR]
161	tst	r1, #USART_ISR_TXE
162	beq	txe_loop_3
163	mov	r0, #0
164	bx	lr
165flush_error:
166	mov	r0, #-1
167	bx	lr
168endfunc console_core_flush
169