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