xref: /rk3399_ARM-atf/drivers/arm/pl011/aarch32/pl011_console.S (revision 0b32628eddfef95b51909816f625e6f37cd21cb8)
1/*
2 * Copyright (c) 2016-2017, ARM Limited and Contributors. All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are met:
6 *
7 * Redistributions of source code must retain the above copyright notice, this
8 * list of conditions and the following disclaimer.
9 *
10 * Redistributions in binary form must reproduce the above copyright notice,
11 * this list of conditions and the following disclaimer in the documentation
12 * and/or other materials provided with the distribution.
13 *
14 * Neither the name of ARM nor the names of its contributors may be used
15 * to endorse or promote products derived from this software without specific
16 * prior written permission.
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
22 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28 * POSSIBILITY OF SUCH DAMAGE.
29 */
30#include <arch.h>
31#include <asm_macros.S>
32#include <pl011.h>
33
34/*
35 * Pull in generic functions to provide backwards compatibility for
36 * platform makefiles
37 */
38#include "../../../console/aarch32/console.S"
39
40	.globl	console_core_init
41	.globl	console_core_putc
42	.globl	console_core_getc
43	.globl	console_core_flush
44
45
46	/* -----------------------------------------------
47	 * int console_core_init(uintptr_t base_addr,
48	 * unsigned int uart_clk, unsigned int baud_rate)
49	 * Function to initialize the console without a
50	 * C Runtime to print debug information. This
51	 * function will be accessed by console_init and
52	 * crash reporting.
53	 * In: r0 - console base address
54	 *     r1 - Uart clock in Hz
55	 *     r2 - Baud rate
56	 * Out: return 1 on success else 0 on error
57	 * Clobber list : r1, r2, r3
58	 * -----------------------------------------------
59	 */
60func console_core_init
61	/* Check the input base address */
62	cmp	r0, #0
63	beq	core_init_fail
64#if !PL011_GENERIC_UART
65	/* Check baud rate and uart clock for sanity */
66	cmp	r1, #0
67	beq	core_init_fail
68	cmp	r2, #0
69	beq	core_init_fail
70	/* Disable the UART before initialization */
71	ldr	r3, [r0, #UARTCR]
72	bic	r3, r3, #PL011_UARTCR_UARTEN
73	str	r3, [r0, #UARTCR]
74	/* Program the baudrate */
75	/* Divisor =  (Uart clock * 4) / baudrate */
76	lsl	r1, r1, #2
77	udiv	r2, r1, r2
78	/* IBRD = Divisor >> 6 */
79	lsr	r1, r2, #6
80	/* Write the IBRD */
81	str	r1, [r0, #UARTIBRD]
82	/* FBRD = Divisor & 0x3F */
83	and	r1, r2, #0x3f
84	/* Write the FBRD */
85	str	r1, [r0, #UARTFBRD]
86	mov	r1, #PL011_LINE_CONTROL
87	str	r1, [r0, #UARTLCR_H]
88	/* Clear any pending errors */
89	mov	r1, #0
90	str	r1, [r0, #UARTECR]
91	/* Enable tx, rx, and uart overall */
92	ldr	r1, =(PL011_UARTCR_RXE | PL011_UARTCR_TXE | PL011_UARTCR_UARTEN)
93	str	r1, [r0, #UARTCR]
94#endif
95	mov	r0, #1
96	bx	lr
97core_init_fail:
98	mov	r0, #0
99	bx	lr
100endfunc console_core_init
101
102	/* --------------------------------------------------------
103	 * int console_core_putc(int c, uintptr_t base_addr)
104	 * Function to output a character over the console. It
105	 * returns the character printed on success or -1 on error.
106	 * In : r0 - character to be printed
107	 *      r1 - console base address
108	 * Out : return -1 on error else return character.
109	 * Clobber list : r2
110	 * --------------------------------------------------------
111	 */
112func console_core_putc
113	/* Check the input parameter */
114	cmp	r1, #0
115	beq	putc_error
116	/* Prepend '\r' to '\n' */
117	cmp	r0, #0xA
118	bne	2f
1191:
120	/* Check if the transmit FIFO is full */
121	ldr	r2, [r1, #UARTFR]
122	tst	r2, #PL011_UARTFR_TXFF
123	bne	1b
124	mov	r2, #0xD
125	str	r2, [r1, #UARTDR]
1262:
127	/* Check if the transmit FIFO is full */
128	ldr	r2, [r1, #UARTFR]
129	tst	r2, #PL011_UARTFR_TXFF
130	bne	2b
131	str	r0, [r1, #UARTDR]
132	bx	lr
133putc_error:
134	mov	r0, #-1
135	bx	lr
136endfunc console_core_putc
137
138	/* ---------------------------------------------
139	 * int console_core_getc(uintptr_t base_addr)
140	 * Function to get a character from the console.
141	 * It returns the character grabbed on success
142	 * or -1 on error.
143	 * In : r0 - console base address
144	 * Clobber list : r0, r1
145	 * ---------------------------------------------
146	 */
147func console_core_getc
148	cmp	r0, #0
149	beq	getc_error
1501:
151	/* Check if the receive FIFO is empty */
152	ldr	r1, [r0, #UARTFR]
153	tst	r1, #PL011_UARTFR_RXFE
154	bne	1b
155	ldr	r1, [r0, #UARTDR]
156	mov	r0, r1
157	bx	lr
158getc_error:
159	mov	r0, #-1
160	bx	lr
161endfunc console_core_getc
162
163	/* ---------------------------------------------
164	 * int console_core_flush(uintptr_t base_addr)
165	 * Function to force a write of all buffered
166	 * data that hasn't been output.
167	 * In : r0 - console base address
168	 * Out : return -1 on error else return 0.
169	 * Clobber list : r0, r1
170	 * ---------------------------------------------
171	 */
172func console_core_flush
173	cmp	r0, #0
174	beq	flush_error
175
1761:
177	/* Loop while the transmit FIFO is busy */
178	ldr	r1, [r0, #UARTFR]
179	tst	r1, #PL011_UARTFR_BUSY
180	bne	1b
181
182	mov	r0, #0
183	bx	lr
184flush_error:
185	mov	r0, #-1
186	bx	lr
187endfunc console_core_flush
188