xref: /rk3399_ARM-atf/drivers/arm/pl011/aarch64/pl011_console.S (revision 3d2690d6e212134dc3df8716f43033597851dfe0)
19c94d3b3SSoby Mathew/*
273e05284SAntonio Nino Diaz * Copyright (c) 2013-2017, ARM Limited and Contributors. All rights reserved.
39c94d3b3SSoby Mathew *
482cb2c1aSdp-arm * SPDX-License-Identifier: BSD-3-Clause
59c94d3b3SSoby Mathew */
69c94d3b3SSoby Mathew#include <arch.h>
79c94d3b3SSoby Mathew#include <asm_macros.S>
84a0c4571SJulius Werner#include <assert_macros.S>
9*3d2690d6SMichalis Pappas#include <console_macros.S>
109c94d3b3SSoby Mathew#include <pl011.h>
119c94d3b3SSoby Mathew
129c94d3b3SSoby Mathew/*
139c94d3b3SSoby Mathew * Pull in generic functions to provide backwards compatibility for
149c94d3b3SSoby Mathew * platform makefiles
159c94d3b3SSoby Mathew */
169c94d3b3SSoby Mathew#include "../../../console/aarch64/console.S"
179c94d3b3SSoby Mathew
184a0c4571SJulius Werner	/*
194a0c4571SJulius Werner	 * "core" functions are low-level implementations that don't require
204a0c4571SJulius Werner	 * writable memory and are thus safe to call in BL1 crash context.
214a0c4571SJulius Werner	 */
224a0c4571SJulius Werner	.globl console_pl011_core_init
234a0c4571SJulius Werner	.globl console_pl011_core_putc
244a0c4571SJulius Werner	.globl console_pl011_core_getc
254a0c4571SJulius Werner	.globl console_pl011_core_flush
269c94d3b3SSoby Mathew
274a0c4571SJulius Werner	.globl	console_pl011_putc
284a0c4571SJulius Werner	.globl	console_pl011_getc
294a0c4571SJulius Werner	.globl	console_pl011_flush
309c94d3b3SSoby Mathew
319c94d3b3SSoby Mathew	/* -----------------------------------------------
324a0c4571SJulius Werner	 * int console_pl011_core_init(uintptr_t base_addr,
339c94d3b3SSoby Mathew	 * unsigned int uart_clk, unsigned int baud_rate)
349c94d3b3SSoby Mathew	 * Function to initialize the console without a
359c94d3b3SSoby Mathew	 * C Runtime to print debug information. This
369c94d3b3SSoby Mathew	 * function will be accessed by console_init and
379c94d3b3SSoby Mathew	 * crash reporting.
389c94d3b3SSoby Mathew	 * In: x0 - console base address
399c94d3b3SSoby Mathew	 *     w1 - Uart clock in Hz
409c94d3b3SSoby Mathew	 *     w2 - Baud rate
419c94d3b3SSoby Mathew	 * Out: return 1 on success else 0 on error
429c94d3b3SSoby Mathew	 * Clobber list : x1, x2, x3, x4
439c94d3b3SSoby Mathew	 * -----------------------------------------------
449c94d3b3SSoby Mathew	 */
454a0c4571SJulius Wernerfunc console_pl011_core_init
469c94d3b3SSoby Mathew	/* Check the input base address */
479c94d3b3SSoby Mathew	cbz	x0, core_init_fail
489c94d3b3SSoby Mathew#if !PL011_GENERIC_UART
499c94d3b3SSoby Mathew	/* Check baud rate and uart clock for sanity */
509c94d3b3SSoby Mathew	cbz	w1, core_init_fail
519c94d3b3SSoby Mathew	cbz	w2, core_init_fail
529c94d3b3SSoby Mathew	/* Disable uart before programming */
539c94d3b3SSoby Mathew	ldr	w3, [x0, #UARTCR]
549c94d3b3SSoby Mathew	mov	w4, #PL011_UARTCR_UARTEN
559c94d3b3SSoby Mathew	bic	w3, w3, w4
569c94d3b3SSoby Mathew	str	w3, [x0, #UARTCR]
579c94d3b3SSoby Mathew	/* Program the baudrate */
589c94d3b3SSoby Mathew	/* Divisor =  (Uart clock * 4) / baudrate */
599c94d3b3SSoby Mathew	lsl	w1, w1, #2
609c94d3b3SSoby Mathew	udiv	w2, w1, w2
619c94d3b3SSoby Mathew	/* IBRD = Divisor >> 6 */
629c94d3b3SSoby Mathew	lsr	w1, w2, #6
639c94d3b3SSoby Mathew	/* Write the IBRD */
649c94d3b3SSoby Mathew	str	w1, [x0, #UARTIBRD]
659c94d3b3SSoby Mathew	/* FBRD = Divisor & 0x3F */
669c94d3b3SSoby Mathew	and	w1, w2, #0x3f
679c94d3b3SSoby Mathew	/* Write the FBRD */
689c94d3b3SSoby Mathew	str	w1, [x0, #UARTFBRD]
699c94d3b3SSoby Mathew	mov	w1, #PL011_LINE_CONTROL
709c94d3b3SSoby Mathew	str	w1, [x0, #UARTLCR_H]
719c94d3b3SSoby Mathew	/* Clear any pending errors */
729c94d3b3SSoby Mathew	str	wzr, [x0, #UARTECR]
739c94d3b3SSoby Mathew	/* Enable tx, rx, and uart overall */
749c94d3b3SSoby Mathew	mov	w1, #(PL011_UARTCR_RXE | PL011_UARTCR_TXE | PL011_UARTCR_UARTEN)
759c94d3b3SSoby Mathew	str	w1, [x0, #UARTCR]
769c94d3b3SSoby Mathew#endif
779c94d3b3SSoby Mathew	mov	w0, #1
789c94d3b3SSoby Mathew	ret
799c94d3b3SSoby Mathewcore_init_fail:
809c94d3b3SSoby Mathew	mov	w0, wzr
819c94d3b3SSoby Mathew	ret
824a0c4571SJulius Wernerendfunc console_pl011_core_init
834a0c4571SJulius Werner
844a0c4571SJulius Werner#if MULTI_CONSOLE_API
854a0c4571SJulius Werner	.globl console_pl011_register
864a0c4571SJulius Werner
874a0c4571SJulius Werner	/* -----------------------------------------------
884a0c4571SJulius Werner	 * int console_pl011_register(console_pl011_t *console,
894a0c4571SJulius Werner		uintptr_t base, uint32_t clk, uint32_t baud)
904a0c4571SJulius Werner	 * Function to initialize and register a new PL011
914a0c4571SJulius Werner	 * console. Storage passed in for the console struct
924a0c4571SJulius Werner	 * *must* be persistent (i.e. not from the stack).
934a0c4571SJulius Werner	 * In: x0 - UART register base address
944a0c4571SJulius Werner	 *     w1 - UART clock in Hz
954a0c4571SJulius Werner	 *     w2 - Baud rate
964a0c4571SJulius Werner	 *     x3 - pointer to empty console_pl011_t struct
974a0c4571SJulius Werner	 * Out: return 1 on success, 0 on error
984a0c4571SJulius Werner	 * Clobber list : x0, x1, x2, x6, x7, x14
994a0c4571SJulius Werner	 * -----------------------------------------------
1004a0c4571SJulius Werner	 */
1014a0c4571SJulius Wernerfunc console_pl011_register
1024a0c4571SJulius Werner	mov	x7, x30
1034a0c4571SJulius Werner	mov	x6, x3
1044a0c4571SJulius Werner	cbz	x6, register_fail
1054a0c4571SJulius Werner	str	x0, [x6, #CONSOLE_T_PL011_BASE]
1064a0c4571SJulius Werner
1074a0c4571SJulius Werner	bl	console_pl011_core_init
1084a0c4571SJulius Werner	cbz	x0, register_fail
1094a0c4571SJulius Werner
1104a0c4571SJulius Werner	mov	x0, x6
1114a0c4571SJulius Werner	mov	x30, x7
1124a0c4571SJulius Werner	finish_console_register pl011
1134a0c4571SJulius Werner
1144a0c4571SJulius Wernerregister_fail:
1154a0c4571SJulius Werner	ret	x7
1164a0c4571SJulius Wernerendfunc console_pl011_register
1174a0c4571SJulius Werner#else
1184a0c4571SJulius Werner	.globl console_core_init
1194a0c4571SJulius Werner	.globl console_core_putc
1204a0c4571SJulius Werner	.globl console_core_getc
1214a0c4571SJulius Werner	.globl console_core_flush
1224a0c4571SJulius Werner	.equ console_core_init,console_pl011_core_init
1234a0c4571SJulius Werner	.equ console_core_putc,console_pl011_core_putc
1244a0c4571SJulius Werner	.equ console_core_getc,console_pl011_core_getc
1254a0c4571SJulius Werner	.equ console_core_flush,console_pl011_core_flush
1264a0c4571SJulius Werner#endif
1279c94d3b3SSoby Mathew
1289c94d3b3SSoby Mathew	/* --------------------------------------------------------
1294a0c4571SJulius Werner	 * int console_pl011_core_putc(int c, uintptr_t base_addr)
1309c94d3b3SSoby Mathew	 * Function to output a character over the console. It
1319c94d3b3SSoby Mathew	 * returns the character printed on success or -1 on error.
1329c94d3b3SSoby Mathew	 * In : w0 - character to be printed
1339c94d3b3SSoby Mathew	 *      x1 - console base address
1349c94d3b3SSoby Mathew	 * Out : return -1 on error else return character.
1359c94d3b3SSoby Mathew	 * Clobber list : x2
1369c94d3b3SSoby Mathew	 * --------------------------------------------------------
1379c94d3b3SSoby Mathew	 */
1384a0c4571SJulius Wernerfunc console_pl011_core_putc
1394a0c4571SJulius Werner#if ENABLE_ASSERTIONS
1404a0c4571SJulius Werner	cmp	x1, #0
1414a0c4571SJulius Werner	ASM_ASSERT(ne)
1424a0c4571SJulius Werner#endif /* ENABLE_ASSERTIONS */
1434a0c4571SJulius Werner
1449c94d3b3SSoby Mathew	/* Prepend '\r' to '\n' */
1459c94d3b3SSoby Mathew	cmp	w0, #0xA
1469c94d3b3SSoby Mathew	b.ne	2f
1479c94d3b3SSoby Mathew1:
1489c94d3b3SSoby Mathew	/* Check if the transmit FIFO is full */
1499c94d3b3SSoby Mathew	ldr	w2, [x1, #UARTFR]
1509c94d3b3SSoby Mathew	tbnz	w2, #PL011_UARTFR_TXFF_BIT, 1b
1519c94d3b3SSoby Mathew	mov	w2, #0xD
1529c94d3b3SSoby Mathew	str	w2, [x1, #UARTDR]
1539c94d3b3SSoby Mathew2:
1549c94d3b3SSoby Mathew	/* Check if the transmit FIFO is full */
1559c94d3b3SSoby Mathew	ldr	w2, [x1, #UARTFR]
1569c94d3b3SSoby Mathew	tbnz	w2, #PL011_UARTFR_TXFF_BIT, 2b
1579c94d3b3SSoby Mathew	str	w0, [x1, #UARTDR]
1589c94d3b3SSoby Mathew	ret
1594a0c4571SJulius Wernerendfunc console_pl011_core_putc
1604a0c4571SJulius Werner
1614a0c4571SJulius Werner	/* --------------------------------------------------------
1624a0c4571SJulius Werner	 * int console_pl011_putc(int c, console_pl011_t *console)
1634a0c4571SJulius Werner	 * Function to output a character over the console. It
1644a0c4571SJulius Werner	 * returns the character printed on success or -1 on error.
1654a0c4571SJulius Werner	 * In : w0 - character to be printed
1664a0c4571SJulius Werner	 *      x1 - pointer to console_t structure
1674a0c4571SJulius Werner	 * Out : return -1 on error else return character.
1684a0c4571SJulius Werner	 * Clobber list : x2
1694a0c4571SJulius Werner	 * --------------------------------------------------------
1704a0c4571SJulius Werner	 */
1714a0c4571SJulius Wernerfunc console_pl011_putc
1724a0c4571SJulius Werner#if ENABLE_ASSERTIONS
1734a0c4571SJulius Werner	cmp	x1, #0
1744a0c4571SJulius Werner	ASM_ASSERT(ne)
1754a0c4571SJulius Werner#endif /* ENABLE_ASSERTIONS */
1764a0c4571SJulius Werner	ldr	x1, [x1, #CONSOLE_T_PL011_BASE]
1774a0c4571SJulius Werner	b	console_pl011_core_putc
1784a0c4571SJulius Wernerendfunc console_pl011_putc
1799c94d3b3SSoby Mathew
1809c94d3b3SSoby Mathew	/* ---------------------------------------------
1814a0c4571SJulius Werner	 * int console_pl011_core_getc(uintptr_t base_addr)
1829c94d3b3SSoby Mathew	 * Function to get a character from the console.
1839c94d3b3SSoby Mathew	 * It returns the character grabbed on success
1844a0c4571SJulius Werner	 * or -1 if no character is available.
1859c94d3b3SSoby Mathew	 * In : x0 - console base address
1864a0c4571SJulius Werner	 * Out: w0 - character if available, else -1
1879c94d3b3SSoby Mathew	 * Clobber list : x0, x1
1889c94d3b3SSoby Mathew	 * ---------------------------------------------
1899c94d3b3SSoby Mathew	 */
1904a0c4571SJulius Wernerfunc console_pl011_core_getc
1914a0c4571SJulius Werner#if ENABLE_ASSERTIONS
1924a0c4571SJulius Werner	cmp	x0, #0
1934a0c4571SJulius Werner	ASM_ASSERT(ne)
1944a0c4571SJulius Werner#endif /* ENABLE_ASSERTIONS */
1954a0c4571SJulius Werner
1969c94d3b3SSoby Mathew	/* Check if the receive FIFO is empty */
1979c94d3b3SSoby Mathew	ldr	w1, [x0, #UARTFR]
1984a0c4571SJulius Werner	tbnz	w1, #PL011_UARTFR_RXFE_BIT, no_char
1999c94d3b3SSoby Mathew	ldr	w1, [x0, #UARTDR]
2009c94d3b3SSoby Mathew	mov	w0, w1
2019c94d3b3SSoby Mathew	ret
2024a0c4571SJulius Wernerno_char:
2034a0c4571SJulius Werner	mov	w0, #ERROR_NO_PENDING_CHAR
2049c94d3b3SSoby Mathew	ret
2054a0c4571SJulius Wernerendfunc console_pl011_core_getc
20673e05284SAntonio Nino Diaz
20773e05284SAntonio Nino Diaz	/* ---------------------------------------------
2084a0c4571SJulius Werner	 * int console_pl011_getc(console_pl011_t *console)
2094a0c4571SJulius Werner	 * Function to get a character from the console.
2104a0c4571SJulius Werner	 * It returns the character grabbed on success
2114a0c4571SJulius Werner	 * or -1 if no character is available.
2124a0c4571SJulius Werner	 * In : x0 - pointer to console_t structure
2134a0c4571SJulius Werner	 * Out: w0 - character if available, else -1
2144a0c4571SJulius Werner	 * Clobber list : x0, x1
2154a0c4571SJulius Werner	 * ---------------------------------------------
2164a0c4571SJulius Werner	 */
2174a0c4571SJulius Wernerfunc console_pl011_getc
2184a0c4571SJulius Werner#if ENABLE_ASSERTIONS
2194a0c4571SJulius Werner	cmp	x0, #0
2204a0c4571SJulius Werner	ASM_ASSERT(ne)
2214a0c4571SJulius Werner#endif /* ENABLE_ASSERTIONS */
2224a0c4571SJulius Werner	ldr	x0, [x0, #CONSOLE_T_PL011_BASE]
2234a0c4571SJulius Werner	b	console_pl011_core_getc
2244a0c4571SJulius Wernerendfunc console_pl011_getc
2254a0c4571SJulius Werner
2264a0c4571SJulius Werner	/* ---------------------------------------------
2274a0c4571SJulius Werner	 * int console_pl011_core_flush(uintptr_t base_addr)
22873e05284SAntonio Nino Diaz	 * Function to force a write of all buffered
22973e05284SAntonio Nino Diaz	 * data that hasn't been output.
23073e05284SAntonio Nino Diaz	 * In : x0 - console base address
23173e05284SAntonio Nino Diaz	 * Out : return -1 on error else return 0.
23273e05284SAntonio Nino Diaz	 * Clobber list : x0, x1
23373e05284SAntonio Nino Diaz	 * ---------------------------------------------
23473e05284SAntonio Nino Diaz	 */
2354a0c4571SJulius Wernerfunc console_pl011_core_flush
2364a0c4571SJulius Werner#if ENABLE_ASSERTIONS
2374a0c4571SJulius Werner	cmp	x0, #0
2384a0c4571SJulius Werner	ASM_ASSERT(ne)
2394a0c4571SJulius Werner#endif /* ENABLE_ASSERTIONS */
24073e05284SAntonio Nino Diaz1:
24173e05284SAntonio Nino Diaz	/* Loop until the transmit FIFO is empty */
24273e05284SAntonio Nino Diaz	ldr	w1, [x0, #UARTFR]
24373e05284SAntonio Nino Diaz	tbnz	w1, #PL011_UARTFR_BUSY_BIT, 1b
24473e05284SAntonio Nino Diaz
24573e05284SAntonio Nino Diaz	mov	w0, #0
24673e05284SAntonio Nino Diaz	ret
2474a0c4571SJulius Wernerendfunc console_pl011_core_flush
2484a0c4571SJulius Werner
2494a0c4571SJulius Werner	/* ---------------------------------------------
2504a0c4571SJulius Werner	 * int console_pl011_flush(console_pl011_t *console)
2514a0c4571SJulius Werner	 * Function to force a write of all buffered
2524a0c4571SJulius Werner	 * data that hasn't been output.
2534a0c4571SJulius Werner	 * In : x0 - pointer to console_t structure
2544a0c4571SJulius Werner	 * Out : return -1 on error else return 0.
2554a0c4571SJulius Werner	 * Clobber list : x0, x1
2564a0c4571SJulius Werner	 * ---------------------------------------------
2574a0c4571SJulius Werner	 */
2584a0c4571SJulius Wernerfunc console_pl011_flush
2594a0c4571SJulius Werner#if ENABLE_ASSERTIONS
2604a0c4571SJulius Werner	cmp	x0, #0
2614a0c4571SJulius Werner	ASM_ASSERT(ne)
2624a0c4571SJulius Werner#endif /* ENABLE_ASSERTIONS */
2634a0c4571SJulius Werner	ldr	x0, [x0, #CONSOLE_T_PL011_BASE]
2644a0c4571SJulius Werner	b	console_pl011_core_flush
2654a0c4571SJulius Wernerendfunc console_pl011_flush
266