1 // SPDX-License-Identifier: GPL-2.0+
3 * (C) Copyright 2016 Beniamino Galvani <b.galvani@gmail.com>
10 #include <linux/bitops.h>
11 #include <linux/compiler.h>
22 struct meson_serial_plat {
23 struct meson_uart *reg;
26 /* AML_UART_STATUS bits */
27 #define AML_UART_PARITY_ERR BIT(16)
28 #define AML_UART_FRAME_ERR BIT(17)
29 #define AML_UART_TX_FIFO_WERR BIT(18)
30 #define AML_UART_RX_EMPTY BIT(20)
31 #define AML_UART_TX_FULL BIT(21)
32 #define AML_UART_TX_EMPTY BIT(22)
33 #define AML_UART_XMIT_BUSY BIT(25)
34 #define AML_UART_ERR (AML_UART_PARITY_ERR | \
35 AML_UART_FRAME_ERR | \
36 AML_UART_TX_FIFO_WERR)
38 /* AML_UART_CONTROL bits */
39 #define AML_UART_TX_EN BIT(12)
40 #define AML_UART_RX_EN BIT(13)
41 #define AML_UART_TX_RST BIT(22)
42 #define AML_UART_RX_RST BIT(23)
43 #define AML_UART_CLR_ERR BIT(24)
45 static void meson_serial_init(struct meson_uart *uart)
49 val = readl(&uart->control);
50 val |= (AML_UART_RX_RST | AML_UART_TX_RST | AML_UART_CLR_ERR);
51 writel(val, &uart->control);
52 val &= ~(AML_UART_RX_RST | AML_UART_TX_RST | AML_UART_CLR_ERR);
53 writel(val, &uart->control);
54 val |= (AML_UART_RX_EN | AML_UART_TX_EN);
55 writel(val, &uart->control);
58 static int meson_serial_probe(struct udevice *dev)
60 struct meson_serial_plat *plat = dev_get_plat(dev);
61 struct meson_uart *const uart = plat->reg;
63 meson_serial_init(uart);
68 static void meson_serial_rx_error(struct udevice *dev)
70 struct meson_serial_plat *plat = dev_get_plat(dev);
71 struct meson_uart *const uart = plat->reg;
72 u32 val = readl(&uart->control);
75 val |= AML_UART_CLR_ERR;
76 writel(val, &uart->control);
77 val &= ~AML_UART_CLR_ERR;
78 writel(val, &uart->control);
80 /* Remove spurious byte from fifo */
84 static int meson_serial_getc(struct udevice *dev)
86 struct meson_serial_plat *plat = dev_get_plat(dev);
87 struct meson_uart *const uart = plat->reg;
88 uint32_t status = readl(&uart->status);
90 if (status & AML_UART_RX_EMPTY)
93 if (status & AML_UART_ERR) {
94 meson_serial_rx_error(dev);
98 return readl(&uart->rfifo) & 0xff;
101 static int meson_serial_putc(struct udevice *dev, const char ch)
103 struct meson_serial_plat *plat = dev_get_plat(dev);
104 struct meson_uart *const uart = plat->reg;
106 if (readl(&uart->status) & AML_UART_TX_FULL)
109 writel(ch, &uart->wfifo);
114 static int meson_serial_pending(struct udevice *dev, bool input)
116 struct meson_serial_plat *plat = dev_get_plat(dev);
117 struct meson_uart *const uart = plat->reg;
118 uint32_t status = readl(&uart->status);
121 if (status & AML_UART_RX_EMPTY)
125 * Handle and drop any RX error here to avoid
126 * returning true here when an error byte is in the FIFO
128 if (status & AML_UART_ERR) {
129 meson_serial_rx_error(dev);
135 return !(status & AML_UART_TX_FULL);
139 static int meson_serial_of_to_plat(struct udevice *dev)
141 struct meson_serial_plat *plat = dev_get_plat(dev);
144 addr = dev_read_addr(dev);
145 if (addr == FDT_ADDR_T_NONE)
148 plat->reg = (struct meson_uart *)addr;
153 static const struct dm_serial_ops meson_serial_ops = {
154 .putc = meson_serial_putc,
155 .pending = meson_serial_pending,
156 .getc = meson_serial_getc,
159 static const struct udevice_id meson_serial_ids[] = {
160 { .compatible = "amlogic,meson-uart" },
161 { .compatible = "amlogic,meson-gx-uart" },
165 U_BOOT_DRIVER(serial_meson) = {
166 .name = "serial_meson",
168 .of_match = meson_serial_ids,
169 .probe = meson_serial_probe,
170 .ops = &meson_serial_ops,
171 .of_to_plat = meson_serial_of_to_plat,
172 .plat_auto = sizeof(struct meson_serial_plat),
175 #ifdef CONFIG_DEBUG_UART_MESON
177 #include <debug_uart.h>
179 static inline void _debug_uart_init(void)
183 static inline void _debug_uart_putc(int ch)
185 struct meson_uart *regs = (struct meson_uart *)CONFIG_DEBUG_UART_BASE;
187 while (readl(®s->status) & AML_UART_TX_FULL)
190 writel(ch, ®s->wfifo);