1/*
2 * Copyright (c) 2018-2020, ARM Limited and Contributors. All rights reserved.
3 *
4 * SPDX-License-Identifier: BSD-3-Clause
5 */
6
7#include <asm_macros.S>
8#include <assert_macros.S>
9#include <console_macros.S>
10#include <drivers/amlogic/meson_console.h>
11
12	.globl console_meson_register
13	.globl console_meson_init
14	.globl console_meson_putc
15	.globl console_meson_getc
16	.globl console_meson_flush
17	.globl console_meson_core_putc
18	.globl console_meson_core_getc
19	.globl console_meson_core_flush
20
21	/* -----------------------------------------------
22	 * Hardware definitions
23	 * -----------------------------------------------
24	 */
25#define MESON_WFIFO_OFFSET			0x0
26#define MESON_RFIFO_OFFSET			0x4
27#define MESON_CONTROL_OFFSET			0x8
28#define MESON_STATUS_OFFSET			0xC
29#define MESON_MISC_OFFSET			0x10
30#define MESON_REG5_OFFSET			0x14
31
32#define MESON_CONTROL_CLR_ERROR_BIT		24
33#define MESON_CONTROL_RX_RESET_BIT		23
34#define MESON_CONTROL_TX_RESET_BIT		22
35#define MESON_CONTROL_RX_ENABLE_BIT		13
36#define MESON_CONTROL_TX_ENABLE_BIT		12
37
38#define MESON_STATUS_RX_EMPTY_BIT		20
39#define MESON_STATUS_TX_FULL_BIT		21
40#define MESON_STATUS_TX_EMPTY_BIT		22
41
42#define MESON_REG5_USE_XTAL_CLK_BIT		24
43#define MESON_REG5_USE_NEW_RATE_BIT		23
44#define MESON_REG5_NEW_BAUD_RATE_MASK		0x7FFFFF
45
46	/* -----------------------------------------------
47	 * int console_meson_register(uintptr_t base,
48	 *     uint32_t clk, uint32_t baud,
49	 *     console_t *console);
50	 * Function to initialize and register a new MESON
51	 * console. Storage passed in for the console struct
52	 * *must* be persistent (i.e. not from the stack).
53	 * In: x0 - UART register base address
54	 *     w1 - UART clock in Hz
55	 *     w2 - Baud rate
56	 *     x3 - pointer to empty console_t struct
57	 * Out: return 1 on success, 0 on error
58	 * Clobber list : x0, x1, x2, x6, x7, x14
59	 * -----------------------------------------------
60	 */
61func console_meson_register
62	mov	x7, x30
63	mov	x6, x3
64	cbz	x6, register_fail
65	str	x0, [x6, #CONSOLE_T_BASE]
66
67	bl	console_meson_init
68	cbz	x0, register_fail
69
70	mov	x0, x6
71	mov	x30, x7
72	finish_console_register meson putc=1, getc=1, flush=1
73
74register_fail:
75	ret	x7
76endfunc console_meson_register
77
78	/* -----------------------------------------------
79	 * int console_meson_init(uintptr_t base_addr,
80	 * unsigned int uart_clk, unsigned int baud_rate)
81	 * Function to initialize the console without a
82	 * C Runtime to print debug information. This
83	 * function will be accessed by console_init and
84	 * crash reporting.
85	 * In: x0 - console base address
86	 *     w1 - Uart clock in Hz
87	 *     w2 - Baud rate
88	 * Out: return 1 on success else 0 on error
89	 * Clobber list : x0-x3
90	 * -----------------------------------------------
91	 */
92func console_meson_init
93	cmp	w0, #0
94	beq	init_fail
95	mov_imm	w3, 24000000 /* TODO: This only works with a 24 MHz clock. */
96	cmp	w1, w3
97	bne	init_fail
98	cmp	w2, #0
99	beq	init_fail
100	/* Set baud rate: value = ((clock / 3) / baudrate) - 1 */
101	mov	w3, #3
102	udiv	w3, w1, w3
103	udiv	w3, w3, w2
104	sub	w3, w3, #1
105	orr	w3, w3, #((1 << MESON_REG5_USE_XTAL_CLK_BIT) | \
106			  (1 << MESON_REG5_USE_NEW_RATE_BIT))
107	str	w3, [x0, #MESON_REG5_OFFSET]
108	/* Reset UART and clear error flag */
109	ldr	w3, [x0, #MESON_CONTROL_OFFSET]
110	orr	w3, w3, #((1 << MESON_CONTROL_CLR_ERROR_BIT) | \
111			  (1 << MESON_CONTROL_RX_RESET_BIT) | \
112			  (1 << MESON_CONTROL_TX_RESET_BIT))
113	str	w3, [x0, #MESON_CONTROL_OFFSET]
114	bic	w3, w3, #((1 << MESON_CONTROL_CLR_ERROR_BIT) | \
115			  (1 << MESON_CONTROL_RX_RESET_BIT) | \
116			  (1 << MESON_CONTROL_TX_RESET_BIT))
117	str	w3, [x0, #MESON_CONTROL_OFFSET]
118	/* Enable transfer and receive FIFO */
119	orr	w3, w3, #((1 << MESON_CONTROL_RX_ENABLE_BIT) | \
120			  (1 << MESON_CONTROL_TX_ENABLE_BIT))
121	str	w3, [x0, #MESON_CONTROL_OFFSET]
122	/* Success */
123	mov	w0, #1
124	ret
125init_fail:
126	mov	w0, wzr
127	ret
128endfunc console_meson_init
129
130	/* --------------------------------------------------------
131	 * int console_meson_putc(int c, console_t *console)
132	 * Function to output a character over the console. It
133	 * returns the character printed on success or -1 on error.
134	 * In : w0 - character to be printed
135	 *      x1 - pointer to console_t structure
136	 * Out : return -1 on error else return character.
137	 * Clobber list : x2
138	 * --------------------------------------------------------
139	 */
140func console_meson_putc
141#if ENABLE_ASSERTIONS
142	cmp	x1, #0
143	ASM_ASSERT(ne)
144#endif /* ENABLE_ASSERTIONS */
145	ldr	x1, [x1, #CONSOLE_T_BASE]
146	b	console_meson_core_putc
147endfunc console_meson_putc
148
149	/* --------------------------------------------------------
150	 * int console_meson_core_putc(int c, uintptr_t base_addr)
151	 * Function to output a character over the console. It
152	 * returns the character printed on success or -1 on error.
153	 * In : w0 - character to be printed
154	 *      x1 - console base address
155	 * Out : return -1 on error else return character.
156	 * Clobber list : x2
157	 * --------------------------------------------------------
158	 */
159func console_meson_core_putc
160#if ENABLE_ASSERTIONS
161	cmp	x1, #0
162	ASM_ASSERT(ne)
163#endif
164	/* Prepend '\r' to '\n' */
165	cmp	w0, #0xA
166	b.ne	2f
167	/* Wait until the transmit FIFO isn't full */
1681:	ldr	w2, [x1, #MESON_STATUS_OFFSET]
169	tbnz	w2, #MESON_STATUS_TX_FULL_BIT, 1b
170	/* Write '\r' if needed */
171	mov	w2, #0xD
172	str	w2, [x1, #MESON_WFIFO_OFFSET]
173	/* Wait until the transmit FIFO isn't full */
1742:	ldr	w2, [x1, #MESON_STATUS_OFFSET]
175	tbnz	w2, #MESON_STATUS_TX_FULL_BIT, 2b
176	/* Write input character */
177	str	w0, [x1, #MESON_WFIFO_OFFSET]
178	ret
179endfunc console_meson_core_putc
180
181	/* ---------------------------------------------
182	 * int console_meson_getc(console_t *console)
183	 * Function to get a character from the console.
184	 * It returns the character grabbed on success
185	 * or -1 if no character is available.
186	 * In : x0 - pointer to console_t structure
187	 * Out: w0 - character if available, else -1
188	 * Clobber list : x0, x1
189	 * ---------------------------------------------
190	 */
191func console_meson_getc
192#if ENABLE_ASSERTIONS
193	cmp	x0, #0
194	ASM_ASSERT(ne)
195#endif /* ENABLE_ASSERTIONS */
196	ldr	x0, [x0, #CONSOLE_T_BASE]
197	b	console_meson_core_getc
198endfunc console_meson_getc
199
200	/* ---------------------------------------------
201	 * int console_meson_core_getc(uintptr_t base_addr)
202	 * Function to get a character from the console.
203	 * It returns the character grabbed on success
204	 * or -1 if no character is available.
205	 * In : x0 - console base address
206	 * Out: w0 - character if available, else -1
207	 * Clobber list : x0, x1
208	 * ---------------------------------------------
209	 */
210func console_meson_core_getc
211#if ENABLE_ASSERTIONS
212	cmp	x0, #0
213	ASM_ASSERT(ne)
214#endif
215	/* Is the receive FIFO empty? */
216	ldr	w1, [x0, #MESON_STATUS_OFFSET]
217	tbnz	w1, #MESON_STATUS_RX_EMPTY_BIT, 1f
218	/* Read one character from the RX FIFO */
219	ldr	w0, [x0, #MESON_RFIFO_OFFSET]
220	ret
2211:
222	mov	w0, #ERROR_NO_PENDING_CHAR
223	ret
224endfunc console_meson_core_getc
225
226	/* ---------------------------------------------
227	 * void console_meson_flush(console_t *console)
228	 * Function to force a write of all buffered
229	 * data that hasn't been output.
230	 * In : x0 - pointer to console_t structure
231	 * Out : void.
232	 * Clobber list : x0, x1
233	 * ---------------------------------------------
234	 */
235func console_meson_flush
236#if ENABLE_ASSERTIONS
237	cmp	x0, #0
238	ASM_ASSERT(ne)
239#endif /* ENABLE_ASSERTIONS */
240	ldr	x0, [x0, #CONSOLE_T_BASE]
241	b	console_meson_core_flush
242endfunc console_meson_flush
243
244	/* ---------------------------------------------
245	 * void console_meson_core_flush(uintptr_t base_addr)
246	 * Function to force a write of all buffered
247	 * data that hasn't been output.
248	 * In : x0 - console base address
249	 * Out : void.
250	 * Clobber list : x0, x1
251	 * ---------------------------------------------
252	 */
253func console_meson_core_flush
254#if ENABLE_ASSERTIONS
255	cmp	x0, #0
256	ASM_ASSERT(ne)
257#endif
258	/* Wait until the transmit FIFO is empty */
2591:	ldr	w1, [x0, #MESON_STATUS_OFFSET]
260	tbz	w1, #MESON_STATUS_TX_EMPTY_BIT, 1b
261	ret
262endfunc console_meson_core_flush
263