1 /*
2  * Copyright 2014, General Dynamics C4 Systems
3  *
4  * SPDX-License-Identifier: GPL-2.0-only
5  */
6 
7 #include <config.h>
8 #include <stdint.h>
9 #include <util.h>
10 #include <machine/io.h>
11 
12 #define ULCON       0x0000 /* line control */
13 #define UCON        0x0004 /* control */
14 #define UFCON       0x0008 /* fifo control */
15 #define UMCON       0x000C /* modem control */
16 #define UTRSTAT     0x0010 /* TX/RX status */
17 #define UERSTAT     0x0014 /* RX error status */
18 #define UFSTAT      0x0018 /* FIFO status */
19 #define UMSTAT      0x001C /* modem status */
20 #define UTXH        0x0020 /* TX buffer */
21 #define URXH        0x0024 /* RX buffer */
22 #define UBRDIV      0x0028 /* baud rate divisor */
23 #define UFRACVAL    0x002C /* divisor fractional value */
24 #define UINTP       0x0030 /* interrupt pending */
25 #define UINTSP      0x0034 /* interrupt source pending */
26 #define UINTM       0x0038 /* interrupt mask */
27 
28 /* UTRSTAT */
29 #define TX_EMPTY        BIT(2)
30 #define TXBUF_EMPTY     BIT(1)
31 #define RXBUF_READY     BIT(0)
32 
33 #define UART_REG(X) ((volatile uint32_t *)(UART_PPTR + (X)))
34 
35 #ifdef CONFIG_PRINTING
uart_drv_putchar(unsigned char c)36 void uart_drv_putchar(unsigned char c)
37 {
38     while ((*UART_REG(UTRSTAT) & TXBUF_EMPTY) == 0);
39     *UART_REG(UTXH) = (c & 0xff);
40 }
41 #endif /* CONFIG_PRINTING */
42 
43 #ifdef CONFIG_DEBUG_BUILD
uart_drv_getchar(void)44 unsigned char uart_drv_getchar(void)
45 {
46     if ((*UART_REG(UTRSTAT) & RXBUF_READY)) {
47         return (unsigned char) * UART_REG(URXH);
48     } else {
49         return -1;
50     }
51 }
52 #endif /* CONFIG_DEBUG_BUILD */
53