xref: /rk3399_ARM-atf/drivers/ti/uart/aarch64/16550_console.S (revision 4f8053dd288b0f76b9f2b74c49f745116d46f97c)
19c94d3b3SSoby Mathew/*
2*4f8053ddSAntonio Nino Diaz * Copyright (c) 2015-2018, ARM Limited and Contributors. All rights reserved.
39c94d3b3SSoby Mathew *
482cb2c1aSdp-arm * SPDX-License-Identifier: BSD-3-Clause
59c94d3b3SSoby Mathew */
69c94d3b3SSoby Mathew
79c94d3b3SSoby Mathew#include <arch.h>
89c94d3b3SSoby Mathew#include <asm_macros.S>
936c42ca1SJulius Werner#include <assert_macros.S>
1036c42ca1SJulius Werner#include <console_macros.S>
119c94d3b3SSoby Mathew#include <uart_16550.h>
129c94d3b3SSoby Mathew
1336c42ca1SJulius Werner	/*
1436c42ca1SJulius Werner	 * "core" functions are low-level implementations that don't require
1536c42ca1SJulius Werner	 * writable memory and are thus safe to call in BL1 crash context.
1636c42ca1SJulius Werner	 */
1736c42ca1SJulius Werner	.globl console_16550_core_init
1836c42ca1SJulius Werner	.globl console_16550_core_putc
1936c42ca1SJulius Werner	.globl console_16550_core_getc
20*4f8053ddSAntonio Nino Diaz	.globl console_16550_core_flush
2136c42ca1SJulius Werner
2236c42ca1SJulius Werner	.globl console_16550_putc
2336c42ca1SJulius Werner	.globl console_16550_getc
24*4f8053ddSAntonio Nino Diaz	.globl console_16550_flush
259c94d3b3SSoby Mathew
269c94d3b3SSoby Mathew	/* -----------------------------------------------
2736c42ca1SJulius Werner	 * int console_16550_core_init(uintptr_t base_addr,
289c94d3b3SSoby Mathew	 * unsigned int uart_clk, unsigned int baud_rate)
299c94d3b3SSoby Mathew	 * Function to initialize the console without a
309c94d3b3SSoby Mathew	 * C Runtime to print debug information. This
319c94d3b3SSoby Mathew	 * function will be accessed by console_init and
329c94d3b3SSoby Mathew	 * crash reporting.
339c94d3b3SSoby Mathew	 * In: x0 - console base address
349c94d3b3SSoby Mathew	 *     w1 - Uart clock in Hz
359c94d3b3SSoby Mathew	 *     w2 - Baud rate
3636c42ca1SJulius Werner	 * Out: return 1 on success, 0 on error
379c94d3b3SSoby Mathew	 * Clobber list : x1, x2, x3
389c94d3b3SSoby Mathew	 * -----------------------------------------------
399c94d3b3SSoby Mathew	 */
4036c42ca1SJulius Wernerfunc console_16550_core_init
419c94d3b3SSoby Mathew	/* Check the input base address */
429c94d3b3SSoby Mathew	cbz	x0, init_fail
439c94d3b3SSoby Mathew	/* Check baud rate and uart clock for sanity */
449c94d3b3SSoby Mathew	cbz	w1, init_fail
459c94d3b3SSoby Mathew	cbz	w2, init_fail
469c94d3b3SSoby Mathew
479c94d3b3SSoby Mathew	/* Program the baudrate */
489c94d3b3SSoby Mathew	/* Divisor =  Uart clock / (16 * baudrate) */
499c94d3b3SSoby Mathew	lsl	w2, w2, #4
509c94d3b3SSoby Mathew	udiv	w2, w1, w2
519c94d3b3SSoby Mathew	and	w1, w2, #0xff		/* w1 = DLL */
529c94d3b3SSoby Mathew	lsr	w2, w2, #8
539c94d3b3SSoby Mathew	and	w2, w2, #0xff		/* w2 = DLLM */
549c94d3b3SSoby Mathew	ldr	w3, [x0, #UARTLCR]
559c94d3b3SSoby Mathew	orr	w3, w3, #UARTLCR_DLAB
569c94d3b3SSoby Mathew	str	w3, [x0, #UARTLCR]	/* enable DLL, DLLM programming */
579c94d3b3SSoby Mathew	str	w1, [x0, #UARTDLL]	/* program DLL */
589c94d3b3SSoby Mathew	str	w2, [x0, #UARTDLLM]	/* program DLLM */
599c94d3b3SSoby Mathew	mov	w2, #~UARTLCR_DLAB
609c94d3b3SSoby Mathew	and	w3, w3, w2
619c94d3b3SSoby Mathew	str	w3, [x0, #UARTLCR]	/* disable DLL, DLLM programming */
629c94d3b3SSoby Mathew
639c94d3b3SSoby Mathew	/* 8n1 */
649c94d3b3SSoby Mathew	mov	w3, #3
659c94d3b3SSoby Mathew	str	w3, [x0, #UARTLCR]
669c94d3b3SSoby Mathew	/* no interrupt */
679c94d3b3SSoby Mathew	mov	w3, #0
689c94d3b3SSoby Mathew	str	w3, [x0, #UARTIER]
699c94d3b3SSoby Mathew	/* enable fifo, DMA */
709c94d3b3SSoby Mathew	mov	w3, #(UARTFCR_FIFOEN | UARTFCR_DMAEN)
719c94d3b3SSoby Mathew	str	w3, [x0, #UARTFCR]
729c94d3b3SSoby Mathew	/* DTR + RTS */
739c94d3b3SSoby Mathew	mov	w3, #3
749c94d3b3SSoby Mathew	str	w3, [x0, #UARTMCR]
759c94d3b3SSoby Mathew	mov	w0, #1
769c94d3b3SSoby Mathew	ret
7736c42ca1SJulius Wernerinit_fail:
7836c42ca1SJulius Werner	mov	w0, #0
7936c42ca1SJulius Werner	ret
8036c42ca1SJulius Wernerendfunc console_16550_core_init
8136c42ca1SJulius Werner
8236c42ca1SJulius Werner#if MULTI_CONSOLE_API
8336c42ca1SJulius Werner	.globl console_16550_register
8436c42ca1SJulius Werner
8536c42ca1SJulius Werner	/* -----------------------------------------------
8636c42ca1SJulius Werner	 * int console_16550_register(console_16550_t *console,
8736c42ca1SJulius Werner		uintptr_t base, uint32_t clk, uint32_t baud)
8836c42ca1SJulius Werner	 * Function to initialize and register a new 16550
8936c42ca1SJulius Werner	 * console. Storage passed in for the console struct
9036c42ca1SJulius Werner	 * *must* be persistent (i.e. not from the stack).
9136c42ca1SJulius Werner	 * In: x0 - UART register base address
9236c42ca1SJulius Werner	 *     w1 - UART clock in Hz
9336c42ca1SJulius Werner	 *     w2 - Baud rate
9436c42ca1SJulius Werner	 *     x3 - pointer to empty console_16550_t struct
9536c42ca1SJulius Werner	 * Out: return 1 on success, 0 on error
9636c42ca1SJulius Werner	 * Clobber list : x0, x1, x2, x6, x7, x14
9736c42ca1SJulius Werner	 * -----------------------------------------------
9836c42ca1SJulius Werner	 */
9936c42ca1SJulius Wernerfunc console_16550_register
10036c42ca1SJulius Werner	mov	x7, x30
10136c42ca1SJulius Werner	mov	x6, x3
10236c42ca1SJulius Werner	cbz	x6, register_fail
10336c42ca1SJulius Werner	str	x0, [x6, #CONSOLE_T_16550_BASE]
10436c42ca1SJulius Werner
10536c42ca1SJulius Werner	bl	console_16550_core_init
10636c42ca1SJulius Werner	cbz	x0, register_fail
10736c42ca1SJulius Werner
10836c42ca1SJulius Werner	mov	x0, x6
10936c42ca1SJulius Werner	mov	x30, x7
11036c42ca1SJulius Werner	finish_console_register 16550
11136c42ca1SJulius Werner
11236c42ca1SJulius Wernerregister_fail:
11336c42ca1SJulius Werner	ret	x7
11436c42ca1SJulius Wernerendfunc console_16550_register
11536c42ca1SJulius Werner#else
11636c42ca1SJulius Werner	.globl console_core_init
11736c42ca1SJulius Werner	.globl console_core_putc
11836c42ca1SJulius Werner	.globl console_core_getc
11936c42ca1SJulius Werner	.globl console_core_flush
12036c42ca1SJulius Werner	.equ console_core_init,console_16550_core_init
12136c42ca1SJulius Werner	.equ console_core_putc,console_16550_core_putc
12236c42ca1SJulius Werner	.equ console_core_getc,console_16550_core_getc
123*4f8053ddSAntonio Nino Diaz	.equ console_core_flush,console_16550_core_flush
12436c42ca1SJulius Werner#endif
1259c94d3b3SSoby Mathew
1269c94d3b3SSoby Mathew	/* --------------------------------------------------------
12736c42ca1SJulius Werner	 * int console_16550_core_putc(int c, uintptr_t base_addr)
1289c94d3b3SSoby Mathew	 * Function to output a character over the console. It
1299c94d3b3SSoby Mathew	 * returns the character printed on success or -1 on error.
1309c94d3b3SSoby Mathew	 * In : w0 - character to be printed
1319c94d3b3SSoby Mathew	 *      x1 - console base address
1329c94d3b3SSoby Mathew	 * Out : return -1 on error else return character.
1339c94d3b3SSoby Mathew	 * Clobber list : x2
1349c94d3b3SSoby Mathew	 * --------------------------------------------------------
1359c94d3b3SSoby Mathew	 */
13636c42ca1SJulius Wernerfunc console_16550_core_putc
13736c42ca1SJulius Werner#if ENABLE_ASSERTIONS
13836c42ca1SJulius Werner	cmp	x1, #0
13936c42ca1SJulius Werner	ASM_ASSERT(ne)
14036c42ca1SJulius Werner#endif /* ENABLE_ASSERTIONS */
1419c94d3b3SSoby Mathew
1429c94d3b3SSoby Mathew	/* Prepend '\r' to '\n' */
1439c94d3b3SSoby Mathew	cmp	w0, #0xA
1449c94d3b3SSoby Mathew	b.ne	2f
1459c94d3b3SSoby Mathew	/* Check if the transmit FIFO is full */
1469c94d3b3SSoby Mathew1:	ldr	w2, [x1, #UARTLSR]
1479c94d3b3SSoby Mathew	and	w2, w2, #(UARTLSR_TEMT | UARTLSR_THRE)
1489c94d3b3SSoby Mathew	cmp	w2, #(UARTLSR_TEMT | UARTLSR_THRE)
1499c94d3b3SSoby Mathew	b.ne	1b
1509c94d3b3SSoby Mathew	mov	w2, #0xD		/* '\r' */
1519c94d3b3SSoby Mathew	str	w2, [x1, #UARTTX]
1529c94d3b3SSoby Mathew
1539c94d3b3SSoby Mathew	/* Check if the transmit FIFO is full */
1549c94d3b3SSoby Mathew2:	ldr	w2, [x1, #UARTLSR]
1559c94d3b3SSoby Mathew	and	w2, w2, #(UARTLSR_TEMT | UARTLSR_THRE)
1569c94d3b3SSoby Mathew	cmp	w2, #(UARTLSR_TEMT | UARTLSR_THRE)
1579c94d3b3SSoby Mathew	b.ne	2b
1589c94d3b3SSoby Mathew	str	w0, [x1, #UARTTX]
1599c94d3b3SSoby Mathew	ret
16036c42ca1SJulius Wernerendfunc console_16550_core_putc
16136c42ca1SJulius Werner
16236c42ca1SJulius Werner	/* --------------------------------------------------------
16336c42ca1SJulius Werner	 * int console_16550_putc(int c, console_16550_t *console)
16436c42ca1SJulius Werner	 * Function to output a character over the console. It
16536c42ca1SJulius Werner	 * returns the character printed on success or -1 on error.
16636c42ca1SJulius Werner	 * In : w0 - character to be printed
16736c42ca1SJulius Werner	 *      x1 - pointer to console_t structure
16836c42ca1SJulius Werner	 * Out : return -1 on error else return character.
16936c42ca1SJulius Werner	 * Clobber list : x2
17036c42ca1SJulius Werner	 * --------------------------------------------------------
17136c42ca1SJulius Werner	 */
17236c42ca1SJulius Wernerfunc console_16550_putc
17336c42ca1SJulius Werner#if ENABLE_ASSERTIONS
17436c42ca1SJulius Werner	cmp	x1, #0
17536c42ca1SJulius Werner	ASM_ASSERT(ne)
17636c42ca1SJulius Werner#endif /* ENABLE_ASSERTIONS */
17736c42ca1SJulius Werner	ldr	x1, [x1, #CONSOLE_T_16550_BASE]
17836c42ca1SJulius Werner	b	console_16550_core_putc
17936c42ca1SJulius Wernerendfunc console_16550_putc
1809c94d3b3SSoby Mathew
1819c94d3b3SSoby Mathew	/* ---------------------------------------------
18236c42ca1SJulius Werner	 * int console_16550_core_getc(uintptr_t base_addr)
1839c94d3b3SSoby Mathew	 * Function to get a character from the console.
1849c94d3b3SSoby Mathew	 * It returns the character grabbed on success
18536c42ca1SJulius Werner	 * or -1 on if no character is available.
18636c42ca1SJulius Werner	 * In :  x0 - console base address
18736c42ca1SJulius Werner	 * Out : w0 - character if available, else -1
1889c94d3b3SSoby Mathew	 * Clobber list : x0, x1
1899c94d3b3SSoby Mathew	 * ---------------------------------------------
1909c94d3b3SSoby Mathew	 */
19136c42ca1SJulius Wernerfunc console_16550_core_getc
19236c42ca1SJulius Werner#if ENABLE_ASSERTIONS
19336c42ca1SJulius Werner	cmp	x0, #0
19436c42ca1SJulius Werner	ASM_ASSERT(ne)
19536c42ca1SJulius Werner#endif /* ENABLE_ASSERTIONS */
19636c42ca1SJulius Werner
1979c94d3b3SSoby Mathew	/* Check if the receive FIFO is empty */
1989c94d3b3SSoby Mathew1:	ldr	w1, [x0, #UARTLSR]
19936c42ca1SJulius Werner	tbz	w1, #UARTLSR_RDR_BIT, no_char
2009c94d3b3SSoby Mathew	ldr	w0, [x0, #UARTRX]
2019c94d3b3SSoby Mathew	ret
20236c42ca1SJulius Wernerno_char:
20336c42ca1SJulius Werner	mov	w0, #ERROR_NO_PENDING_CHAR
2049c94d3b3SSoby Mathew	ret
20536c42ca1SJulius Wernerendfunc console_16550_core_getc
20636c42ca1SJulius Werner
20736c42ca1SJulius Werner	/* ---------------------------------------------
20836c42ca1SJulius Werner	 * int console_16550_getc(console_16550_t *console)
20936c42ca1SJulius Werner	 * Function to get a character from the console.
21036c42ca1SJulius Werner	 * It returns the character grabbed on success
21136c42ca1SJulius Werner	 * or -1 on if no character is available.
21236c42ca1SJulius Werner	 * In :  x0 - pointer to console_t stucture
21336c42ca1SJulius Werner	 * Out : w0 - character if available, else -1
21436c42ca1SJulius Werner	 * Clobber list : x0, x1
21536c42ca1SJulius Werner	 * ---------------------------------------------
21636c42ca1SJulius Werner	 */
21736c42ca1SJulius Wernerfunc console_16550_getc
21836c42ca1SJulius Werner#if ENABLE_ASSERTIONS
21936c42ca1SJulius Werner	cmp	x1, #0
22036c42ca1SJulius Werner	ASM_ASSERT(ne)
22136c42ca1SJulius Werner#endif /* ENABLE_ASSERTIONS */
22236c42ca1SJulius Werner	ldr	x0, [x0, #CONSOLE_T_16550_BASE]
22336c42ca1SJulius Werner	b	console_16550_core_getc
22436c42ca1SJulius Wernerendfunc console_16550_getc
225ad4c2ec6SAntonio Nino Diaz
226ad4c2ec6SAntonio Nino Diaz	/* ---------------------------------------------
227*4f8053ddSAntonio Nino Diaz	 * int console_16550_core_flush(uintptr_t base_addr)
228ad4c2ec6SAntonio Nino Diaz	 * Function to force a write of all buffered
229ad4c2ec6SAntonio Nino Diaz	 * data that hasn't been output.
230ad4c2ec6SAntonio Nino Diaz	 * In : x0 - console base address
231ad4c2ec6SAntonio Nino Diaz	 * Out : return -1 on error else return 0.
232ad4c2ec6SAntonio Nino Diaz	 * Clobber list : x0, x1
233ad4c2ec6SAntonio Nino Diaz	 * ---------------------------------------------
234ad4c2ec6SAntonio Nino Diaz	 */
235*4f8053ddSAntonio Nino Diazfunc console_16550_core_flush
236*4f8053ddSAntonio Nino Diaz#if ENABLE_ASSERTIONS
237*4f8053ddSAntonio Nino Diaz	cmp	x0, #0
238*4f8053ddSAntonio Nino Diaz	ASM_ASSERT(ne)
239*4f8053ddSAntonio Nino Diaz#endif /* ENABLE_ASSERTIONS */
240*4f8053ddSAntonio Nino Diaz
241*4f8053ddSAntonio Nino Diaz	/* Loop until the transmit FIFO is empty */
242*4f8053ddSAntonio Nino Diaz1:	ldr	w1, [x0, #UARTLSR]
243*4f8053ddSAntonio Nino Diaz	and	w1, w1, #(UARTLSR_TEMT | UARTLSR_THRE)
244*4f8053ddSAntonio Nino Diaz	cmp	w1, #(UARTLSR_TEMT | UARTLSR_THRE)
245*4f8053ddSAntonio Nino Diaz	b.ne	1b
246*4f8053ddSAntonio Nino Diaz
247ad4c2ec6SAntonio Nino Diaz	mov	w0, #0
248ad4c2ec6SAntonio Nino Diaz	ret
249*4f8053ddSAntonio Nino Diazendfunc console_16550_core_flush
250*4f8053ddSAntonio Nino Diaz
251*4f8053ddSAntonio Nino Diaz	/* ---------------------------------------------
252*4f8053ddSAntonio Nino Diaz	 * int console_16550_flush(console_pl011_t *console)
253*4f8053ddSAntonio Nino Diaz	 * Function to force a write of all buffered
254*4f8053ddSAntonio Nino Diaz	 * data that hasn't been output.
255*4f8053ddSAntonio Nino Diaz	 * In : x0 - pointer to console_t structure
256*4f8053ddSAntonio Nino Diaz	 * Out : return -1 on error else return 0.
257*4f8053ddSAntonio Nino Diaz	 * Clobber list : x0, x1
258*4f8053ddSAntonio Nino Diaz	 * ---------------------------------------------
259*4f8053ddSAntonio Nino Diaz	 */
260*4f8053ddSAntonio Nino Diazfunc console_16550_flush
261*4f8053ddSAntonio Nino Diaz#if ENABLE_ASSERTIONS
262*4f8053ddSAntonio Nino Diaz	cmp	x0, #0
263*4f8053ddSAntonio Nino Diaz	ASM_ASSERT(ne)
264*4f8053ddSAntonio Nino Diaz#endif /* ENABLE_ASSERTIONS */
265*4f8053ddSAntonio Nino Diaz	ldr	x0, [x0, #CONSOLE_T_16550_BASE]
266*4f8053ddSAntonio Nino Diaz	b	console_16550_core_flush
267*4f8053ddSAntonio Nino Diazendfunc console_16550_flush
268