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