1/*
2 * Copyright (c) 2018-2020, ARM Limited and Contributors. All rights reserved.
3 *
4 * SPDX-License-Identifier: BSD-3-Clause
5 */
6
7#include <arch.h>
8#include <asm_macros.S>
9#include <console_macros.S>
10#include <assert_macros.S>
11#include "imx_uart.h"
12
13#define URXD  0x0  /* Receiver Register */
14#define UTXD  0x40 /* Transmitter Register */
15#define USR2  0x98 /* UART Status Register 2 */
16#define UTS   0xb4 /* UART Test Register (mx31) */
17#define  URXD_RX_DATA    (0xFF)
18
19	.globl	console_imx_uart_register
20	.globl	console_imx_uart_init
21	.globl	console_imx_uart_putc
22	.globl	console_imx_uart_getc
23	.globl	console_imx_uart_flush
24
25func console_imx_uart_register
26	mov	x7, x30
27	mov	x6, x3
28	cbz	x6, register_fail
29	str	x0, [x6, #CONSOLE_T_BASE]
30
31	bl	console_imx_uart_init
32	cbz	x0, register_fail
33
34	mov	x0, x6
35	mov	x30, x7
36	finish_console_register imx_uart putc=1, getc=ENABLE_CONSOLE_GETC, flush=1
37
38register_fail:
39	ret	x7
40endfunc console_imx_uart_register
41
42func console_imx_uart_init
43	mov	w0, #1
44	ret
45endfunc console_imx_uart_init
46
47func console_imx_uart_putc
48	ldr	x1, [x1, #CONSOLE_T_BASE]
49	cbz	x1, putc_error
50
51	/* Prepare '\r' to '\n' */
52	cmp	w0, #0xA
53	b.ne	2f
541:
55	/* Check if the transmit FIFO is full */
56	ldr	w2, [x1, #UTS]
57	tbnz	w2, #4, 1b
58	mov	w2, #0xD
59	str	w2, [x1, #UTXD]
602:
61	/* Check if the transmit FIFO is full */
62	ldr	w2, [x1, #UTS]
63	tbnz	w2, #4, 2b
64	str	w0, [x1, #UTXD]
65	ret
66putc_error:
67	mov	w0, #-1
68	ret
69endfunc console_imx_uart_putc
70
71func console_imx_uart_getc
72	ldr	x0, [x0, #CONSOLE_T_BASE]
73	cbz	x0, getc_error
741:
75	ldr	w1, [x0, #UTS]
76	tbnz	w1, #5, 1b
77
78	ldr	w1, [x0, #URXD]
79	and	w0, w1, #URXD_RX_DATA
80
81	ret
82getc_error:
83	mov	w0, #-1
84	ret
85endfunc console_imx_uart_getc
86
87func console_imx_uart_flush
88	ldr	x0, [x0, #CONSOLE_T_BASE]
89	cbz	x0, flush_exit
901:
91	/* Wait for the transmit complete bit */
92	ldr	w1, [x0, #USR2]
93	tbz	w1, #3, 1b
94
95flush_exit:
96	ret
97endfunc console_imx_uart_flush
98