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