xref: /rk3399_ARM-atf/drivers/ti/uart/aarch64/16550_console.S (revision 82cb2c1ad9897473743f08437d0a3995bed561b9)
19c94d3b3SSoby Mathew/*
2ad4c2ec6SAntonio Nino Diaz * Copyright (c) 2015-2017, ARM Limited and Contributors. All rights reserved.
39c94d3b3SSoby Mathew *
4*82cb2c1aSdp-arm * SPDX-License-Identifier: BSD-3-Clause
59c94d3b3SSoby Mathew */
69c94d3b3SSoby Mathew
79c94d3b3SSoby Mathew#include <arch.h>
89c94d3b3SSoby Mathew#include <asm_macros.S>
99c94d3b3SSoby Mathew#include <uart_16550.h>
109c94d3b3SSoby Mathew
119c94d3b3SSoby Mathew	.globl	console_core_init
129c94d3b3SSoby Mathew	.globl	console_core_putc
139c94d3b3SSoby Mathew	.globl	console_core_getc
14ad4c2ec6SAntonio Nino Diaz	.globl	console_core_flush
159c94d3b3SSoby Mathew
169c94d3b3SSoby Mathew	/* -----------------------------------------------
179c94d3b3SSoby Mathew	 * int console_core_init(unsigned long base_addr,
189c94d3b3SSoby Mathew	 * unsigned int uart_clk, unsigned int baud_rate)
199c94d3b3SSoby Mathew	 * Function to initialize the console without a
209c94d3b3SSoby Mathew	 * C Runtime to print debug information. This
219c94d3b3SSoby Mathew	 * function will be accessed by console_init and
229c94d3b3SSoby Mathew	 * crash reporting.
239c94d3b3SSoby Mathew	 * In: x0 - console base address
249c94d3b3SSoby Mathew	 *     w1 - Uart clock in Hz
259c94d3b3SSoby Mathew	 *     w2 - Baud rate
269c94d3b3SSoby Mathew	 * Out: return 1 on success
279c94d3b3SSoby Mathew	 * Clobber list : x1, x2, x3
289c94d3b3SSoby Mathew	 * -----------------------------------------------
299c94d3b3SSoby Mathew	 */
309c94d3b3SSoby Mathewfunc console_core_init
319c94d3b3SSoby Mathew	/* Check the input base address */
329c94d3b3SSoby Mathew	cbz	x0, init_fail
339c94d3b3SSoby Mathew	/* Check baud rate and uart clock for sanity */
349c94d3b3SSoby Mathew	cbz	w1, init_fail
359c94d3b3SSoby Mathew	cbz	w2, init_fail
369c94d3b3SSoby Mathew
379c94d3b3SSoby Mathew	/* Program the baudrate */
389c94d3b3SSoby Mathew	/* Divisor =  Uart clock / (16 * baudrate) */
399c94d3b3SSoby Mathew	lsl	w2, w2, #4
409c94d3b3SSoby Mathew	udiv	w2, w1, w2
419c94d3b3SSoby Mathew	and	w1, w2, #0xff		/* w1 = DLL */
429c94d3b3SSoby Mathew	lsr	w2, w2, #8
439c94d3b3SSoby Mathew	and	w2, w2, #0xff		/* w2 = DLLM */
449c94d3b3SSoby Mathew	ldr	w3, [x0, #UARTLCR]
459c94d3b3SSoby Mathew	orr	w3, w3, #UARTLCR_DLAB
469c94d3b3SSoby Mathew	str	w3, [x0, #UARTLCR]	/* enable DLL, DLLM programming */
479c94d3b3SSoby Mathew	str	w1, [x0, #UARTDLL]	/* program DLL */
489c94d3b3SSoby Mathew	str	w2, [x0, #UARTDLLM]	/* program DLLM */
499c94d3b3SSoby Mathew	mov	w2, #~UARTLCR_DLAB
509c94d3b3SSoby Mathew	and	w3, w3, w2
519c94d3b3SSoby Mathew	str	w3, [x0, #UARTLCR]	/* disable DLL, DLLM programming */
529c94d3b3SSoby Mathew
539c94d3b3SSoby Mathew	/* 8n1 */
549c94d3b3SSoby Mathew	mov	w3, #3
559c94d3b3SSoby Mathew	str	w3, [x0, #UARTLCR]
569c94d3b3SSoby Mathew	/* no interrupt */
579c94d3b3SSoby Mathew	mov	w3, #0
589c94d3b3SSoby Mathew	str	w3, [x0, #UARTIER]
599c94d3b3SSoby Mathew	/* enable fifo, DMA */
609c94d3b3SSoby Mathew	mov	w3, #(UARTFCR_FIFOEN | UARTFCR_DMAEN)
619c94d3b3SSoby Mathew	str	w3, [x0, #UARTFCR]
629c94d3b3SSoby Mathew	/* DTR + RTS */
639c94d3b3SSoby Mathew	mov	w3, #3
649c94d3b3SSoby Mathew	str	w3, [x0, #UARTMCR]
659c94d3b3SSoby Mathew	mov	w0, #1
669c94d3b3SSoby Mathewinit_fail:
679c94d3b3SSoby Mathew	ret
689c94d3b3SSoby Mathewendfunc console_core_init
699c94d3b3SSoby Mathew
709c94d3b3SSoby Mathew	/* --------------------------------------------------------
719c94d3b3SSoby Mathew	 * int console_core_putc(int c, unsigned int base_addr)
729c94d3b3SSoby Mathew	 * Function to output a character over the console. It
739c94d3b3SSoby Mathew	 * returns the character printed on success or -1 on error.
749c94d3b3SSoby Mathew	 * In : w0 - character to be printed
759c94d3b3SSoby Mathew	 *      x1 - console base address
769c94d3b3SSoby Mathew	 * Out : return -1 on error else return character.
779c94d3b3SSoby Mathew	 * Clobber list : x2
789c94d3b3SSoby Mathew	 * --------------------------------------------------------
799c94d3b3SSoby Mathew	 */
809c94d3b3SSoby Mathewfunc console_core_putc
819c94d3b3SSoby Mathew	/* Check the input parameter */
829c94d3b3SSoby Mathew	cbz	x1, putc_error
839c94d3b3SSoby Mathew
849c94d3b3SSoby Mathew	/* Prepend '\r' to '\n' */
859c94d3b3SSoby Mathew	cmp	w0, #0xA
869c94d3b3SSoby Mathew	b.ne	2f
879c94d3b3SSoby Mathew	/* Check if the transmit FIFO is full */
889c94d3b3SSoby Mathew1:	ldr	w2, [x1, #UARTLSR]
899c94d3b3SSoby Mathew	and	w2, w2, #(UARTLSR_TEMT | UARTLSR_THRE)
909c94d3b3SSoby Mathew	cmp	w2, #(UARTLSR_TEMT | UARTLSR_THRE)
919c94d3b3SSoby Mathew	b.ne	1b
929c94d3b3SSoby Mathew	mov	w2, #0xD		/* '\r' */
939c94d3b3SSoby Mathew	str	w2, [x1, #UARTTX]
949c94d3b3SSoby Mathew
959c94d3b3SSoby Mathew	/* Check if the transmit FIFO is full */
969c94d3b3SSoby Mathew2:	ldr	w2, [x1, #UARTLSR]
979c94d3b3SSoby Mathew	and	w2, w2, #(UARTLSR_TEMT | UARTLSR_THRE)
989c94d3b3SSoby Mathew	cmp	w2, #(UARTLSR_TEMT | UARTLSR_THRE)
999c94d3b3SSoby Mathew	b.ne	2b
1009c94d3b3SSoby Mathew	str	w0, [x1, #UARTTX]
1019c94d3b3SSoby Mathew	ret
1029c94d3b3SSoby Mathewputc_error:
1039c94d3b3SSoby Mathew	mov	w0, #-1
1049c94d3b3SSoby Mathew	ret
1059c94d3b3SSoby Mathewendfunc console_core_putc
1069c94d3b3SSoby Mathew
1079c94d3b3SSoby Mathew	/* ---------------------------------------------
1089c94d3b3SSoby Mathew	 * int console_core_getc(void)
1099c94d3b3SSoby Mathew	 * Function to get a character from the console.
1109c94d3b3SSoby Mathew	 * It returns the character grabbed on success
1119c94d3b3SSoby Mathew	 * or -1 on error.
1129c94d3b3SSoby Mathew	 * In : w0 - console base address
1139c94d3b3SSoby Mathew	 * Out : return -1 on error else return character.
1149c94d3b3SSoby Mathew	 * Clobber list : x0, x1
1159c94d3b3SSoby Mathew	 * ---------------------------------------------
1169c94d3b3SSoby Mathew	 */
1179c94d3b3SSoby Mathewfunc console_core_getc
1189c94d3b3SSoby Mathew	/* Check if the receive FIFO is empty */
1199c94d3b3SSoby Mathew1:	ldr	w1, [x0, #UARTLSR]
120861ac52aSNishanth Menon	tbz	w1, #UARTLSR_RDR_BIT, 1b
1219c94d3b3SSoby Mathew	ldr	w0, [x0, #UARTRX]
1229c94d3b3SSoby Mathew	ret
1239c94d3b3SSoby Mathewgetc_error:
1249c94d3b3SSoby Mathew	mov	w0, #-1
1259c94d3b3SSoby Mathew	ret
1269c94d3b3SSoby Mathewendfunc console_core_getc
127ad4c2ec6SAntonio Nino Diaz
128ad4c2ec6SAntonio Nino Diaz	/* ---------------------------------------------
129ad4c2ec6SAntonio Nino Diaz	 * int console_core_flush(uintptr_t base_addr)
130ad4c2ec6SAntonio Nino Diaz	 * Function to force a write of all buffered
131ad4c2ec6SAntonio Nino Diaz	 * data that hasn't been output.
132ad4c2ec6SAntonio Nino Diaz	 * In : x0 - console base address
133ad4c2ec6SAntonio Nino Diaz	 * Out : return -1 on error else return 0.
134ad4c2ec6SAntonio Nino Diaz	 * Clobber list : x0, x1
135ad4c2ec6SAntonio Nino Diaz	 * ---------------------------------------------
136ad4c2ec6SAntonio Nino Diaz	 */
137ad4c2ec6SAntonio Nino Diazfunc console_core_flush
138ad4c2ec6SAntonio Nino Diaz	/* Placeholder */
139ad4c2ec6SAntonio Nino Diaz	mov	w0, #0
140ad4c2ec6SAntonio Nino Diaz	ret
141ad4c2ec6SAntonio Nino Diazendfunc console_core_flush
142