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