xref: /rk3399_ARM-atf/drivers/cadence/uart/aarch64/cdns_console.S (revision ff2743e544f0f82381ebb9dff8f14eacb837d2e0)
1/*
2 * Copyright (c) 2016-2017, ARM Limited and Contributors. All rights reserved.
3 *
4 * SPDX-License-Identifier: BSD-3-Clause
5 */
6#include <arch.h>
7#include <asm_macros.S>
8#include <assert_macros.S>
9#include <cadence/cdns_uart.h>
10
11	/*
12	 * "core" functions are low-level implementations that don't require
13	 * writable memory and are thus safe to call in BL1 crash context.
14	 */
15	.globl console_cdns_core_init
16	.globl console_cdns_core_putc
17	.globl console_cdns_core_getc
18
19	.globl  console_cdns_putc
20	.globl  console_cdns_getc
21
22	/* -----------------------------------------------
23	 * int console_cdns_core_init(uintptr_t base_addr)
24	 * Function to initialize the console without a
25	 * C Runtime to print debug information. This
26	 * function will be accessed by console_init and
27	 * crash reporting.
28	 * We assume that the bootloader already set up
29	 * the HW (baud, ...) and only enable the trans-
30	 * mitter and receiver here.
31	 * In: x0 - console base address
32	 * Out: return 1 on success else 0 on error
33	 * Clobber list : x1, x2, x3
34	 * -----------------------------------------------
35	 */
36func console_cdns_core_init
37	/* Check the input base address */
38	cbz	x0, core_init_fail
39
40	/* RX/TX enabled & reset */
41	mov	w3, #(R_UART_CR_TX_EN | R_UART_CR_RX_EN | R_UART_CR_TXRST | R_UART_CR_RXRST)
42	str	w3, [x0, #R_UART_CR]
43
44	mov	w0, #1
45	ret
46core_init_fail:
47	mov	w0, wzr
48	ret
49endfunc console_cdns_core_init
50
51#if MULTI_CONSOLE_API
52	.globl console_cdns_register
53
54	/* -----------------------------------------------
55	 * int console_cdns_register(console_cdns_t *console,
56		uintptr_t base, uint32_t clk, uint32_t baud)
57	 * Function to initialize and register a new CDNS
58	 * console. Storage passed in for the console struct
59	 * *must* be persistent (i.e. not from the stack).
60	 * In: x0 - UART register base address
61	 *     x1 - pointer to empty console_cdns_t struct
62	 * Out: return 1 on success, 0 on error
63	 * Clobber list : x0, x1, x2, x6, x7, x14
64	 * -----------------------------------------------
65	 */
66func console_cdns_register
67	mov	x7, x30
68	mov	x6, x1
69	cbz	x6, register_fail
70	str	x0, [x6, #CONSOLE_T_CDNS_BASE]
71
72	bl	console_cdns_core_init
73	cbz	x0, register_fail
74
75	mov	x0, x6
76	mov	x30, v7
77	finish_console_register cdns
78
79register_fail:
80	ret	x7
81endfunc console_cdns_register
82#else
83	.globl console_core_init
84	.globl console_core_putc
85	.globl console_core_getc
86	.globl console_core_flush
87	.equ console_core_init,console_cdns_core_init
88	.equ console_core_putc,console_cdns_core_putc
89	.equ console_core_getc,console_cdns_core_getc
90#endif
91
92	/* --------------------------------------------------------
93	 * int console_cdns_core_putc(int c, uintptr_t base_addr)
94	 * Function to output a character over the console. It
95	 * returns the character printed on success or -1 on error.
96	 * In : w0 - character to be printed
97	 *      x1 - console base address
98	 * Out : return -1 on error else return character.
99	 * Clobber list : x2
100	 * --------------------------------------------------------
101	 */
102func console_cdns_core_putc
103#if ENABLE_ASSERTIONS
104	cmp	x1, #0
105	ASM_ASSERT(ne)
106#endif /* ENABLE_ASSERTIONS */
107
108	/* Prepend '\r' to '\n' */
109	cmp	w0, #0xA
110	b.ne	2f
1111:
112	/* Check if the transmit FIFO is full */
113	ldr	w2, [x1, #R_UART_SR]
114	tbnz	w2, #UART_SR_INTR_TFUL_BIT, 1b
115	mov	w2, #0xD
116	str	w2, [x1, #R_UART_TX]
1172:
118	/* Check if the transmit FIFO is full */
119	ldr	w2, [x1, #R_UART_SR]
120	tbnz	w2, #UART_SR_INTR_TFUL_BIT, 2b
121	str	w0, [x1, #R_UART_TX]
122	ret
123endfunc console_cdns_core_putc
124
125	/* --------------------------------------------------------
126	 * int console_cdns_putc(int c, console_cdns_t *cdns)
127	 * Function to output a character over the console. It
128	 * returns the character printed on success or -1 on error.
129	 * In : w0 - character to be printed
130	 *      x1 - pointer to console_t structure
131	 * Out : return -1 on error else return character.
132	 * Clobber list : x2
133	 * --------------------------------------------------------
134	 */
135func console_cdns_putc
136#if ENABLE_ASSERTIONS
137	cmp	x1, #0
138	ASM_ASSERT(ne)
139#endif /* ENABLE_ASSERTIONS */
140	ldr	x1, [x1, #CONSOLE_T_CDNS_BASE]
141	b	console_cdns_core_putc
142endfunc console_cdns_putc
143
144	/* ---------------------------------------------
145	 * int console_cdns_core_getc(uintptr_t base_addr)
146	 * Function to get a character from the console.
147	 * It returns the character grabbed on success
148	 * or -1 if no character is available.
149	 * In : x0 - console base address
150	 * Out: w0 - character if available, else -1
151	 * Clobber list : x0, x1
152	 * ---------------------------------------------
153	 */
154func console_cdns_core_getc
155#if ENABLE_ASSERTIONS
156	cmp	x0, #0
157	ASM_ASSERT(ne)
158#endif /* ENABLE_ASSERTIONS */
159
160	/* Check if the receive FIFO is empty */
161	ldr	w1, [x0, #R_UART_SR]
162	tbnz	w1, #UART_SR_INTR_REMPTY_BIT, no_char
163	ldr	w1, [x0, #R_UART_RX]
164	mov	w0, w1
165	ret
166no_char:
167	mov	w0, #ERROR_NO_PENDING_CHAR
168	ret
169endfunc console_cdns_core_getc
170
171	/* ---------------------------------------------
172	 * int console_cdns_getc(console_cdns_t *console)
173	 * Function to get a character from the console.
174	 * It returns the character grabbed on success
175	 * or -1 if no character is available.
176	 * In : x0 - pointer to console_t structure
177	 * Out: w0 - character if available, else -1
178	 * Clobber list : x0, x1
179	 * ---------------------------------------------
180	 */
181func console_cdns_getc
182#if ENABLE_ASSERTIONS
183	cmp	x0, #0
184	ASM_ASSERT(ne)
185#endif /* ENABLE_ASSERTIONS */
186	ldr	x0, [x0, #CONSOLE_T_CDNS_BASE]
187	b	console_cdns_core_getc
188endfunc console_cdns_getc
189
190	/* ---------------------------------------------
191	 * int console_core_flush(uintptr_t base_addr)
192	 * DEPRECATED: Not used with MULTI_CONSOLE_API!
193	 * Function to force a write of all buffered
194	 * data that hasn't been output.
195	 * In : x0 - console base address
196	 * Out : return -1 on error else return 0.
197	 * Clobber list : x0, x1
198	 * ---------------------------------------------
199	 */
200func console_core_flush
201	/* Placeholder */
202	mov	w0, #0
203	ret
204endfunc console_core_flush
205