1 // SPDX-License-Identifier: GPL-2.0+
9 #include <linux/kernel.h>
10 #include <linux/bitops.h>
11 #include <linux/compiler.h>
21 u32 reg5; /* New baud control register */
24 struct meson_serial_plat {
25 struct meson_uart *reg;
28 /* AML_UART_STATUS bits */
29 #define AML_UART_PARITY_ERR BIT(16)
30 #define AML_UART_FRAME_ERR BIT(17)
31 #define AML_UART_TX_FIFO_WERR BIT(18)
32 #define AML_UART_RX_EMPTY BIT(20)
33 #define AML_UART_TX_FULL BIT(21)
34 #define AML_UART_TX_EMPTY BIT(22)
35 #define AML_UART_XMIT_BUSY BIT(25)
36 #define AML_UART_ERR (AML_UART_PARITY_ERR | \
37 AML_UART_FRAME_ERR | \
38 AML_UART_TX_FIFO_WERR)
40 /* AML_UART_CONTROL bits */
41 #define AML_UART_TX_EN BIT(12)
42 #define AML_UART_RX_EN BIT(13)
43 #define AML_UART_TX_RST BIT(22)
44 #define AML_UART_RX_RST BIT(23)
45 #define AML_UART_CLR_ERR BIT(24)
47 /* AML_UART_REG5 bits */
48 #define AML_UART_REG5_XTAL_DIV2 BIT(27)
49 #define AML_UART_REG5_XTAL_CLK_SEL BIT(26) /* default 0 (div by 3), 1 for no div */
50 #define AML_UART_REG5_USE_XTAL_CLK BIT(24) /* default 1 (use crystal as clock source) */
51 #define AML_UART_REG5_USE_NEW_BAUD BIT(23) /* default 1 (use new baud rate register) */
52 #define AML_UART_REG5_BAUD_MASK 0x7fffff
54 static u32 meson_calc_baud_divisor(ulong src_rate, u32 baud)
57 * Usually src_rate is 24 MHz (from crystal) as clock source for serial
58 * device. Since 8 Mb/s is the maximum supported baud rate, use div by 3
59 * to derive baud rate. This choice is used also in meson_serial_setbrg.
61 return DIV_ROUND_CLOSEST(src_rate / 3, baud) - 1;
64 static void meson_serial_set_baud(struct meson_uart *uart, ulong src_rate, u32 baud)
67 * Set crystal divided by 3 (regardless of device tree clock property)
68 * as clock source and the corresponding divisor to approximate baud
70 u32 divisor = meson_calc_baud_divisor(src_rate, baud);
71 u32 val = AML_UART_REG5_USE_XTAL_CLK | AML_UART_REG5_USE_NEW_BAUD |
72 (divisor & AML_UART_REG5_BAUD_MASK);
73 writel(val, &uart->reg5);
76 static void meson_serial_init(struct meson_uart *uart)
80 val = readl(&uart->control);
81 val |= (AML_UART_RX_RST | AML_UART_TX_RST | AML_UART_CLR_ERR);
82 writel(val, &uart->control);
83 val &= ~(AML_UART_RX_RST | AML_UART_TX_RST | AML_UART_CLR_ERR);
84 writel(val, &uart->control);
85 val |= (AML_UART_RX_EN | AML_UART_TX_EN);
86 writel(val, &uart->control);
89 static int meson_serial_probe(struct udevice *dev)
91 struct meson_serial_plat *plat = dev_get_plat(dev);
92 struct meson_uart *const uart = plat->reg;
94 int ret = clk_get_by_name(dev, "baud", &per_clk);
98 ulong rate = clk_get_rate(&per_clk);
100 meson_serial_set_baud(uart, rate, CONFIG_BAUDRATE);
101 meson_serial_init(uart);
106 static void meson_serial_rx_error(struct udevice *dev)
108 struct meson_serial_plat *plat = dev_get_plat(dev);
109 struct meson_uart *const uart = plat->reg;
110 u32 val = readl(&uart->control);
113 val |= AML_UART_CLR_ERR;
114 writel(val, &uart->control);
115 val &= ~AML_UART_CLR_ERR;
116 writel(val, &uart->control);
118 /* Remove spurious byte from fifo */
122 static int meson_serial_getc(struct udevice *dev)
124 struct meson_serial_plat *plat = dev_get_plat(dev);
125 struct meson_uart *const uart = plat->reg;
126 uint32_t status = readl(&uart->status);
128 if (status & AML_UART_RX_EMPTY)
131 if (status & AML_UART_ERR) {
132 meson_serial_rx_error(dev);
136 return readl(&uart->rfifo) & 0xff;
139 static int meson_serial_putc(struct udevice *dev, const char ch)
141 struct meson_serial_plat *plat = dev_get_plat(dev);
142 struct meson_uart *const uart = plat->reg;
144 if (readl(&uart->status) & AML_UART_TX_FULL)
147 writel(ch, &uart->wfifo);
152 static int meson_serial_setbrg(struct udevice *dev, const int baud)
155 * Change device baud rate if baud is reasonable (considering a 23 bit
156 * counter with an 8 MHz clock input) and the actual baud
157 * rate is within 2% of the requested value (2% is arbitrary).
159 if (baud < 1 || baud > 8000000)
162 struct meson_serial_plat *const plat = dev_get_plat(dev);
163 struct meson_uart *const uart = plat->reg;
165 int ret = clk_get_by_name(dev, "baud", &per_clk);
169 ulong rate = clk_get_rate(&per_clk);
170 u32 divisor = meson_calc_baud_divisor(rate, baud);
171 u32 calc_baud = (rate / 3) / (divisor + 1);
172 u32 calc_err = baud > calc_baud ? baud - calc_baud : calc_baud - baud;
174 if (((calc_err * 100) / baud) > 2)
177 meson_serial_set_baud(uart, rate, baud);
182 static int meson_serial_pending(struct udevice *dev, bool input)
184 struct meson_serial_plat *plat = dev_get_plat(dev);
185 struct meson_uart *const uart = plat->reg;
186 uint32_t status = readl(&uart->status);
189 if (status & AML_UART_RX_EMPTY)
193 * Handle and drop any RX error here to avoid
194 * returning true here when an error byte is in the FIFO
196 if (status & AML_UART_ERR) {
197 meson_serial_rx_error(dev);
203 if (status & AML_UART_TX_EMPTY)
210 static int meson_serial_of_to_plat(struct udevice *dev)
212 struct meson_serial_plat *plat = dev_get_plat(dev);
215 addr = dev_read_addr(dev);
216 if (addr == FDT_ADDR_T_NONE)
219 plat->reg = (struct meson_uart *)addr;
224 static const struct dm_serial_ops meson_serial_ops = {
225 .putc = meson_serial_putc,
226 .pending = meson_serial_pending,
227 .getc = meson_serial_getc,
228 .setbrg = meson_serial_setbrg,
231 static const struct udevice_id meson_serial_ids[] = {
232 { .compatible = "amlogic,meson-uart" },
233 { .compatible = "amlogic,meson-gx-uart" },
234 { .compatible = "amlogic,meson-a1-uart" },
238 U_BOOT_DRIVER(serial_meson) = {
239 .name = "serial_meson",
241 .of_match = meson_serial_ids,
242 .probe = meson_serial_probe,
243 .ops = &meson_serial_ops,
244 .of_to_plat = meson_serial_of_to_plat,
245 .plat_auto = sizeof(struct meson_serial_plat),
248 #ifdef CONFIG_DEBUG_UART_MESON
250 #include <debug_uart.h>
252 static inline void _debug_uart_init(void)
256 static inline void _debug_uart_putc(int ch)
258 struct meson_uart *regs = (struct meson_uart *)CONFIG_VAL(DEBUG_UART_BASE);
260 while (readl(®s->status) & AML_UART_TX_FULL)
263 writel(ch, ®s->wfifo);