• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1/*
2 * Copyright (c) 2013-2020, 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 <console_macros.S>
10#include <drivers/arm/pl011.h>
11
12	/*
13	 * "core" functions are low-level implementations that don't require
14	 * writable memory and are thus safe to call in BL1 crash context.
15	 */
16	.globl console_pl011_core_init
17	.globl console_pl011_core_putc
18	.globl console_pl011_core_getc
19	.globl console_pl011_core_flush
20
21	.globl	console_pl011_putc
22	.globl	console_pl011_getc
23	.globl	console_pl011_flush
24
25	/* -----------------------------------------------
26	 * int console_pl011_core_init(uintptr_t base_addr,
27	 * unsigned int uart_clk, unsigned int baud_rate)
28	 * Function to initialize the console without a
29	 * C Runtime to print debug information. This
30	 * function will be accessed by console_init and
31	 * crash reporting.
32	 * In: x0 - console base address
33	 *     w1 - Uart clock in Hz
34	 *     w2 - Baud rate
35	 * Out: return 1 on success else 0 on error
36	 * Clobber list : x1, x2, x3, x4
37	 * -----------------------------------------------
38	 */
39func console_pl011_core_init
40	/* Check the input base address */
41	cbz	x0, core_init_fail
42#if !PL011_GENERIC_UART
43	/* Check baud rate and uart clock for sanity */
44	cbz	w1, core_init_fail
45	cbz	w2, core_init_fail
46	/* Disable uart before programming */
47	ldr	w3, [x0, #UARTCR]
48	mov	w4, #PL011_UARTCR_UARTEN
49	bic	w3, w3, w4
50	str	w3, [x0, #UARTCR]
51	/* Program the baudrate */
52	/* Divisor =  (Uart clock * 4) / baudrate */
53	lsl	w1, w1, #2
54	udiv	w2, w1, w2
55	/* IBRD = Divisor >> 6 */
56	lsr	w1, w2, #6
57	/* Write the IBRD */
58	str	w1, [x0, #UARTIBRD]
59	/* FBRD = Divisor & 0x3F */
60	and	w1, w2, #0x3f
61	/* Write the FBRD */
62	str	w1, [x0, #UARTFBRD]
63	mov	w1, #PL011_LINE_CONTROL
64	str	w1, [x0, #UARTLCR_H]
65	/* Clear any pending errors */
66	str	wzr, [x0, #UARTECR]
67	/* Enable tx, rx, and uart overall */
68	mov	w1, #(PL011_UARTCR_RXE | PL011_UARTCR_TXE | PL011_UARTCR_UARTEN)
69	str	w1, [x0, #UARTCR]
70#endif
71	mov	w0, #1
72	ret
73core_init_fail:
74	mov	w0, wzr
75	ret
76endfunc console_pl011_core_init
77
78	.globl console_pl011_register
79
80	/* -----------------------------------------------
81	 * int console_pl011_register(uintptr_t baseaddr,
82	 *     uint32_t clock, uint32_t baud,
83	 *     console_t *console);
84	 * Function to initialize and register a new PL011
85	 * console. Storage passed in for the console struct
86	 * *must* be persistent (i.e. not from the stack).
87	 * In: x0 - UART register base address
88	 *     w1 - UART clock in Hz
89	 *     w2 - Baud rate
90	 *     x3 - pointer to empty console_t struct
91	 * Out: return 1 on success, 0 on error
92	 * Clobber list : x0, x1, x2, x6, x7, x14
93	 * -----------------------------------------------
94	 */
95func console_pl011_register
96	mov	x7, x30
97	mov	x6, x3
98	cbz	x6, register_fail
99	str	x0, [x6, #CONSOLE_T_BASE]
100
101	bl	console_pl011_core_init
102	cbz	x0, register_fail
103
104	mov	x0, x6
105	mov	x30, x7
106	finish_console_register pl011 putc=1, getc=1, flush=1
107
108register_fail:
109	ret	x7
110endfunc console_pl011_register
111
112	/* --------------------------------------------------------
113	 * int console_pl011_core_putc(int c, uintptr_t base_addr)
114	 * Function to output a character over the console. It
115	 * returns the character printed on success or -1 on error.
116	 * In : w0 - character to be printed
117	 *      x1 - console base address
118	 * Out : return -1 on error else return character.
119	 * Clobber list : x2
120	 * --------------------------------------------------------
121	 */
122func console_pl011_core_putc
123#if ENABLE_ASSERTIONS
124	cmp	x1, #0
125	ASM_ASSERT(ne)
126#endif /* ENABLE_ASSERTIONS */
127
128	/* Prepend '\r' to '\n' */
129	cmp	w0, #0xA
130	b.ne	2f
1311:
132	/* Check if the transmit FIFO is full */
133	ldr	w2, [x1, #UARTFR]
134	tbnz	w2, #PL011_UARTFR_TXFF_BIT, 1b
135	mov	w2, #0xD
136	str	w2, [x1, #UARTDR]
1372:
138	/* Check if the transmit FIFO is full */
139	ldr	w2, [x1, #UARTFR]
140	tbnz	w2, #PL011_UARTFR_TXFF_BIT, 2b
141	str	w0, [x1, #UARTDR]
142	ret
143endfunc console_pl011_core_putc
144
145	/* --------------------------------------------------------
146	 * int console_pl011_putc(int c, console_t *console)
147	 * Function to output a character over the console. It
148	 * returns the character printed on success or -1 on error.
149	 * In : w0 - character to be printed
150	 *      x1 - pointer to console_t structure
151	 * Out : return -1 on error else return character.
152	 * Clobber list : x2
153	 * --------------------------------------------------------
154	 */
155func console_pl011_putc
156#if ENABLE_ASSERTIONS
157	cmp	x1, #0
158	ASM_ASSERT(ne)
159#endif /* ENABLE_ASSERTIONS */
160	ldr	x1, [x1, #CONSOLE_T_BASE]
161	b	console_pl011_core_putc
162endfunc console_pl011_putc
163
164	/* ---------------------------------------------
165	 * int console_pl011_core_getc(uintptr_t base_addr)
166	 * Function to get a character from the console.
167	 * It returns the character grabbed on success
168	 * or -1 if no character is available.
169	 * In : x0 - console base address
170	 * Out: w0 - character if available, else -1
171	 * Clobber list : x0, x1
172	 * ---------------------------------------------
173	 */
174func console_pl011_core_getc
175#if ENABLE_ASSERTIONS
176	cmp	x0, #0
177	ASM_ASSERT(ne)
178#endif /* ENABLE_ASSERTIONS */
179
180	/* Check if the receive FIFO is empty */
181	ldr	w1, [x0, #UARTFR]
182	tbnz	w1, #PL011_UARTFR_RXFE_BIT, no_char
183	ldr	w1, [x0, #UARTDR]
184	mov	w0, w1
185	ret
186no_char:
187	mov	w0, #ERROR_NO_PENDING_CHAR
188	ret
189endfunc console_pl011_core_getc
190
191	/* ---------------------------------------------
192	 * int console_pl011_getc(console_t *console)
193	 * Function to get a character from the console.
194	 * It returns the character grabbed on success
195	 * or -1 if no character is available.
196	 * In : x0 - pointer to console_t structure
197	 * Out: w0 - character if available, else -1
198	 * Clobber list : x0, x1
199	 * ---------------------------------------------
200	 */
201func console_pl011_getc
202#if ENABLE_ASSERTIONS
203	cmp	x0, #0
204	ASM_ASSERT(ne)
205#endif /* ENABLE_ASSERTIONS */
206	ldr	x0, [x0, #CONSOLE_T_BASE]
207	b	console_pl011_core_getc
208endfunc console_pl011_getc
209
210	/* ---------------------------------------------
211	 * void console_pl011_core_flush(uintptr_t base_addr)
212	 * Function to force a write of all buffered
213	 * data that hasn't been output.
214	 * In : x0 - console base address
215	 * Out : void.
216	 * Clobber list : x0, x1
217	 * ---------------------------------------------
218	 */
219func console_pl011_core_flush
220#if ENABLE_ASSERTIONS
221	cmp	x0, #0
222	ASM_ASSERT(ne)
223#endif /* ENABLE_ASSERTIONS */
2241:
225	/* Loop until the transmit FIFO is empty */
226	ldr	w1, [x0, #UARTFR]
227	tbnz	w1, #PL011_UARTFR_BUSY_BIT, 1b
228	ret
229endfunc console_pl011_core_flush
230
231	/* ---------------------------------------------
232	 * void console_pl011_flush(console_t *console)
233	 * Function to force a write of all buffered
234	 * data that hasn't been output.
235	 * In : x0 - pointer to console_t structure
236	 * Out : void
237	 * Clobber list : x0, x1
238	 * ---------------------------------------------
239	 */
240func console_pl011_flush
241#if ENABLE_ASSERTIONS
242	cmp	x0, #0
243	ASM_ASSERT(ne)
244#endif /* ENABLE_ASSERTIONS */
245	ldr	x0, [x0, #CONSOLE_T_BASE]
246	b	console_pl011_core_flush
247endfunc console_pl011_flush
248