xref: /rk3399_ARM-atf/drivers/arm/pl011/aarch64/pl011_console.S (revision 73e052846ab4c45194d639ab65b523448a7d7edd)
19c94d3b3SSoby Mathew/*
2*73e05284SAntonio Nino Diaz * Copyright (c) 2013-2017, ARM Limited and Contributors. All rights reserved.
39c94d3b3SSoby Mathew *
49c94d3b3SSoby Mathew * Redistribution and use in source and binary forms, with or without
59c94d3b3SSoby Mathew * modification, are permitted provided that the following conditions are met:
69c94d3b3SSoby Mathew *
79c94d3b3SSoby Mathew * Redistributions of source code must retain the above copyright notice, this
89c94d3b3SSoby Mathew * list of conditions and the following disclaimer.
99c94d3b3SSoby Mathew *
109c94d3b3SSoby Mathew * Redistributions in binary form must reproduce the above copyright notice,
119c94d3b3SSoby Mathew * this list of conditions and the following disclaimer in the documentation
129c94d3b3SSoby Mathew * and/or other materials provided with the distribution.
139c94d3b3SSoby Mathew *
149c94d3b3SSoby Mathew * Neither the name of ARM nor the names of its contributors may be used
159c94d3b3SSoby Mathew * to endorse or promote products derived from this software without specific
169c94d3b3SSoby Mathew * prior written permission.
179c94d3b3SSoby Mathew *
189c94d3b3SSoby Mathew * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
199c94d3b3SSoby Mathew * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
209c94d3b3SSoby Mathew * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
219c94d3b3SSoby Mathew * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
229c94d3b3SSoby Mathew * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
239c94d3b3SSoby Mathew * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
249c94d3b3SSoby Mathew * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
259c94d3b3SSoby Mathew * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
269c94d3b3SSoby Mathew * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
279c94d3b3SSoby Mathew * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
289c94d3b3SSoby Mathew * POSSIBILITY OF SUCH DAMAGE.
299c94d3b3SSoby Mathew */
309c94d3b3SSoby Mathew#include <arch.h>
319c94d3b3SSoby Mathew#include <asm_macros.S>
329c94d3b3SSoby Mathew#include <pl011.h>
339c94d3b3SSoby Mathew
349c94d3b3SSoby Mathew/*
359c94d3b3SSoby Mathew * Pull in generic functions to provide backwards compatibility for
369c94d3b3SSoby Mathew * platform makefiles
379c94d3b3SSoby Mathew */
389c94d3b3SSoby Mathew#include "../../../console/aarch64/console.S"
399c94d3b3SSoby Mathew
409c94d3b3SSoby Mathew
419c94d3b3SSoby Mathew	.globl	console_core_init
429c94d3b3SSoby Mathew	.globl	console_core_putc
439c94d3b3SSoby Mathew	.globl	console_core_getc
44*73e05284SAntonio Nino Diaz	.globl	console_core_flush
459c94d3b3SSoby Mathew
469c94d3b3SSoby Mathew
479c94d3b3SSoby Mathew	/* -----------------------------------------------
489c94d3b3SSoby Mathew	 * int console_core_init(uintptr_t base_addr,
499c94d3b3SSoby Mathew	 * unsigned int uart_clk, unsigned int baud_rate)
509c94d3b3SSoby Mathew	 * Function to initialize the console without a
519c94d3b3SSoby Mathew	 * C Runtime to print debug information. This
529c94d3b3SSoby Mathew	 * function will be accessed by console_init and
539c94d3b3SSoby Mathew	 * crash reporting.
549c94d3b3SSoby Mathew	 * In: x0 - console base address
559c94d3b3SSoby Mathew	 *     w1 - Uart clock in Hz
569c94d3b3SSoby Mathew	 *     w2 - Baud rate
579c94d3b3SSoby Mathew	 * Out: return 1 on success else 0 on error
589c94d3b3SSoby Mathew	 * Clobber list : x1, x2, x3, x4
599c94d3b3SSoby Mathew	 * -----------------------------------------------
609c94d3b3SSoby Mathew	 */
619c94d3b3SSoby Mathewfunc console_core_init
629c94d3b3SSoby Mathew	/* Check the input base address */
639c94d3b3SSoby Mathew	cbz	x0, core_init_fail
649c94d3b3SSoby Mathew#if !PL011_GENERIC_UART
659c94d3b3SSoby Mathew	/* Check baud rate and uart clock for sanity */
669c94d3b3SSoby Mathew	cbz	w1, core_init_fail
679c94d3b3SSoby Mathew	cbz	w2, core_init_fail
689c94d3b3SSoby Mathew	/* Disable uart before programming */
699c94d3b3SSoby Mathew	ldr	w3, [x0, #UARTCR]
709c94d3b3SSoby Mathew	mov	w4, #PL011_UARTCR_UARTEN
719c94d3b3SSoby Mathew	bic	w3, w3, w4
729c94d3b3SSoby Mathew	str	w3, [x0, #UARTCR]
739c94d3b3SSoby Mathew	/* Program the baudrate */
749c94d3b3SSoby Mathew	/* Divisor =  (Uart clock * 4) / baudrate */
759c94d3b3SSoby Mathew	lsl	w1, w1, #2
769c94d3b3SSoby Mathew	udiv	w2, w1, w2
779c94d3b3SSoby Mathew	/* IBRD = Divisor >> 6 */
789c94d3b3SSoby Mathew	lsr	w1, w2, #6
799c94d3b3SSoby Mathew	/* Write the IBRD */
809c94d3b3SSoby Mathew	str	w1, [x0, #UARTIBRD]
819c94d3b3SSoby Mathew	/* FBRD = Divisor & 0x3F */
829c94d3b3SSoby Mathew	and	w1, w2, #0x3f
839c94d3b3SSoby Mathew	/* Write the FBRD */
849c94d3b3SSoby Mathew	str	w1, [x0, #UARTFBRD]
859c94d3b3SSoby Mathew	mov	w1, #PL011_LINE_CONTROL
869c94d3b3SSoby Mathew	str	w1, [x0, #UARTLCR_H]
879c94d3b3SSoby Mathew	/* Clear any pending errors */
889c94d3b3SSoby Mathew	str	wzr, [x0, #UARTECR]
899c94d3b3SSoby Mathew	/* Enable tx, rx, and uart overall */
909c94d3b3SSoby Mathew	mov	w1, #(PL011_UARTCR_RXE | PL011_UARTCR_TXE | PL011_UARTCR_UARTEN)
919c94d3b3SSoby Mathew	str	w1, [x0, #UARTCR]
929c94d3b3SSoby Mathew#endif
939c94d3b3SSoby Mathew	mov	w0, #1
949c94d3b3SSoby Mathew	ret
959c94d3b3SSoby Mathewcore_init_fail:
969c94d3b3SSoby Mathew	mov	w0, wzr
979c94d3b3SSoby Mathew	ret
989c94d3b3SSoby Mathewendfunc console_core_init
999c94d3b3SSoby Mathew
1009c94d3b3SSoby Mathew	/* --------------------------------------------------------
1019c94d3b3SSoby Mathew	 * int console_core_putc(int c, uintptr_t base_addr)
1029c94d3b3SSoby Mathew	 * Function to output a character over the console. It
1039c94d3b3SSoby Mathew	 * returns the character printed on success or -1 on error.
1049c94d3b3SSoby Mathew	 * In : w0 - character to be printed
1059c94d3b3SSoby Mathew	 *      x1 - console base address
1069c94d3b3SSoby Mathew	 * Out : return -1 on error else return character.
1079c94d3b3SSoby Mathew	 * Clobber list : x2
1089c94d3b3SSoby Mathew	 * --------------------------------------------------------
1099c94d3b3SSoby Mathew	 */
1109c94d3b3SSoby Mathewfunc console_core_putc
1119c94d3b3SSoby Mathew	/* Check the input parameter */
1129c94d3b3SSoby Mathew	cbz	x1, putc_error
1139c94d3b3SSoby Mathew	/* Prepend '\r' to '\n' */
1149c94d3b3SSoby Mathew	cmp	w0, #0xA
1159c94d3b3SSoby Mathew	b.ne	2f
1169c94d3b3SSoby Mathew1:
1179c94d3b3SSoby Mathew	/* Check if the transmit FIFO is full */
1189c94d3b3SSoby Mathew	ldr	w2, [x1, #UARTFR]
1199c94d3b3SSoby Mathew	tbnz	w2, #PL011_UARTFR_TXFF_BIT, 1b
1209c94d3b3SSoby Mathew	mov	w2, #0xD
1219c94d3b3SSoby Mathew	str	w2, [x1, #UARTDR]
1229c94d3b3SSoby Mathew2:
1239c94d3b3SSoby Mathew	/* Check if the transmit FIFO is full */
1249c94d3b3SSoby Mathew	ldr	w2, [x1, #UARTFR]
1259c94d3b3SSoby Mathew	tbnz	w2, #PL011_UARTFR_TXFF_BIT, 2b
1269c94d3b3SSoby Mathew	str	w0, [x1, #UARTDR]
1279c94d3b3SSoby Mathew	ret
1289c94d3b3SSoby Mathewputc_error:
1299c94d3b3SSoby Mathew	mov	w0, #-1
1309c94d3b3SSoby Mathew	ret
1319c94d3b3SSoby Mathewendfunc console_core_putc
1329c94d3b3SSoby Mathew
1339c94d3b3SSoby Mathew	/* ---------------------------------------------
1349c94d3b3SSoby Mathew	 * int console_core_getc(uintptr_t base_addr)
1359c94d3b3SSoby Mathew	 * Function to get a character from the console.
1369c94d3b3SSoby Mathew	 * It returns the character grabbed on success
1379c94d3b3SSoby Mathew	 * or -1 on error.
1389c94d3b3SSoby Mathew	 * In : x0 - console base address
1399c94d3b3SSoby Mathew	 * Clobber list : x0, x1
1409c94d3b3SSoby Mathew	 * ---------------------------------------------
1419c94d3b3SSoby Mathew	 */
1429c94d3b3SSoby Mathewfunc console_core_getc
1439c94d3b3SSoby Mathew	cbz	x0, getc_error
1449c94d3b3SSoby Mathew1:
1459c94d3b3SSoby Mathew	/* Check if the receive FIFO is empty */
1469c94d3b3SSoby Mathew	ldr	w1, [x0, #UARTFR]
1479c94d3b3SSoby Mathew	tbnz	w1, #PL011_UARTFR_RXFE_BIT, 1b
1489c94d3b3SSoby Mathew	ldr	w1, [x0, #UARTDR]
1499c94d3b3SSoby Mathew	mov	w0, w1
1509c94d3b3SSoby Mathew	ret
1519c94d3b3SSoby Mathewgetc_error:
1529c94d3b3SSoby Mathew	mov	w0, #-1
1539c94d3b3SSoby Mathew	ret
1549c94d3b3SSoby Mathewendfunc console_core_getc
155*73e05284SAntonio Nino Diaz
156*73e05284SAntonio Nino Diaz	/* ---------------------------------------------
157*73e05284SAntonio Nino Diaz	 * int console_core_flush(uintptr_t base_addr)
158*73e05284SAntonio Nino Diaz	 * Function to force a write of all buffered
159*73e05284SAntonio Nino Diaz	 * data that hasn't been output.
160*73e05284SAntonio Nino Diaz	 * In : x0 - console base address
161*73e05284SAntonio Nino Diaz	 * Out : return -1 on error else return 0.
162*73e05284SAntonio Nino Diaz	 * Clobber list : x0, x1
163*73e05284SAntonio Nino Diaz	 * ---------------------------------------------
164*73e05284SAntonio Nino Diaz	 */
165*73e05284SAntonio Nino Diazfunc console_core_flush
166*73e05284SAntonio Nino Diaz	cbz	x0, flush_error
167*73e05284SAntonio Nino Diaz
168*73e05284SAntonio Nino Diaz1:
169*73e05284SAntonio Nino Diaz	/* Loop until the transmit FIFO is empty */
170*73e05284SAntonio Nino Diaz	ldr	w1, [x0, #UARTFR]
171*73e05284SAntonio Nino Diaz	tbnz	w1, #PL011_UARTFR_BUSY_BIT, 1b
172*73e05284SAntonio Nino Diaz
173*73e05284SAntonio Nino Diaz	mov	w0, #0
174*73e05284SAntonio Nino Diaz	ret
175*73e05284SAntonio Nino Diazflush_error:
176*73e05284SAntonio Nino Diaz	mov	w0, #-1
177*73e05284SAntonio Nino Diaz	ret
178*73e05284SAntonio Nino Diazendfunc console_core_flush
179