1 // SPDX-License-Identifier: GPL-2.0+
3 * Copyright (C) 2015-2016 Wills Wang <wills.wang@live.com>
12 #include <asm/addrspace.h>
13 #include <asm/types.h>
14 #include <dm/pinctrl.h>
15 #include <mach/ar71xx_regs.h>
17 #define AR933X_UART_DATA_REG 0x00
18 #define AR933X_UART_CS_REG 0x04
19 #define AR933X_UART_CLK_REG 0x08
21 #define AR933X_UART_DATA_TX_RX_MASK 0xff
22 #define AR933X_UART_DATA_RX_CSR BIT(8)
23 #define AR933X_UART_DATA_TX_CSR BIT(9)
24 #define AR933X_UART_CS_IF_MODE_S 2
25 #define AR933X_UART_CS_IF_MODE_M 0x3
26 #define AR933X_UART_CS_IF_MODE_DTE 1
27 #define AR933X_UART_CS_IF_MODE_DCE 2
28 #define AR933X_UART_CS_TX_RDY_ORIDE BIT(7)
29 #define AR933X_UART_CS_RX_RDY_ORIDE BIT(8)
30 #define AR933X_UART_CLK_STEP_M 0xffff
31 #define AR933X_UART_CLK_SCALE_M 0xfff
32 #define AR933X_UART_CLK_SCALE_S 16
33 #define AR933X_UART_CLK_STEP_S 0
35 struct ar933x_serial_priv {
40 * Baudrate algorithm come from Linux/drivers/tty/serial/ar933x_uart.c
41 * baudrate = (clk / (scale + 1)) * (step * (1 / 2^17))
43 static u32 ar933x_serial_get_baud(u32 clk, u32 scale, u32 step)
48 div = (2 << 16) * (scale + 1);
57 static void ar933x_serial_get_scale_step(u32 clk, u32 baud,
58 u32 *scale, u32 *step)
67 for (tscale = 0; tscale < AR933X_UART_CLK_SCALE_M; tscale++) {
71 tstep = baud * (tscale + 1);
75 if (tstep > AR933X_UART_CLK_STEP_M)
78 baudrate = ar933x_serial_get_baud(clk, tscale, tstep);
79 diff = abs(baudrate - baud);
80 if (diff < min_diff) {
88 static int ar933x_serial_setbrg(struct udevice *dev, int baudrate)
90 struct ar933x_serial_priv *priv = dev_get_priv(dev);
93 val = get_serial_clock();
94 ar933x_serial_get_scale_step(val, baudrate, &scale, &step);
96 val = (scale & AR933X_UART_CLK_SCALE_M)
97 << AR933X_UART_CLK_SCALE_S;
98 val |= (step & AR933X_UART_CLK_STEP_M)
99 << AR933X_UART_CLK_STEP_S;
100 writel(val, priv->regs + AR933X_UART_CLK_REG);
105 static int ar933x_serial_putc(struct udevice *dev, const char c)
107 struct ar933x_serial_priv *priv = dev_get_priv(dev);
110 data = readl(priv->regs + AR933X_UART_DATA_REG);
111 if (!(data & AR933X_UART_DATA_TX_CSR))
114 data = (u32)c | AR933X_UART_DATA_TX_CSR;
115 writel(data, priv->regs + AR933X_UART_DATA_REG);
120 static int ar933x_serial_getc(struct udevice *dev)
122 struct ar933x_serial_priv *priv = dev_get_priv(dev);
125 data = readl(priv->regs + AR933X_UART_DATA_REG);
126 if (!(data & AR933X_UART_DATA_RX_CSR))
129 writel(AR933X_UART_DATA_RX_CSR, priv->regs + AR933X_UART_DATA_REG);
130 return data & AR933X_UART_DATA_TX_RX_MASK;
133 static int ar933x_serial_pending(struct udevice *dev, bool input)
135 struct ar933x_serial_priv *priv = dev_get_priv(dev);
138 data = readl(priv->regs + AR933X_UART_DATA_REG);
140 return (data & AR933X_UART_DATA_RX_CSR) ? 1 : 0;
142 return (data & AR933X_UART_DATA_TX_CSR) ? 0 : 1;
145 static int ar933x_serial_probe(struct udevice *dev)
147 struct ar933x_serial_priv *priv = dev_get_priv(dev);
151 addr = devfdt_get_addr(dev);
152 if (addr == FDT_ADDR_T_NONE)
155 priv->regs = map_physmem(addr, AR933X_UART_SIZE,
159 * UART controller configuration:
164 * - set RX ready oride
165 * - set TX ready oride
167 val = (AR933X_UART_CS_IF_MODE_DCE << AR933X_UART_CS_IF_MODE_S) |
168 AR933X_UART_CS_TX_RDY_ORIDE | AR933X_UART_CS_RX_RDY_ORIDE;
169 writel(val, priv->regs + AR933X_UART_CS_REG);
173 static const struct dm_serial_ops ar933x_serial_ops = {
174 .putc = ar933x_serial_putc,
175 .pending = ar933x_serial_pending,
176 .getc = ar933x_serial_getc,
177 .setbrg = ar933x_serial_setbrg,
180 static const struct udevice_id ar933x_serial_ids[] = {
181 { .compatible = "qca,ar9330-uart" },
185 U_BOOT_DRIVER(serial_ar933x) = {
186 .name = "serial_ar933x",
188 .of_match = ar933x_serial_ids,
189 .priv_auto_alloc_size = sizeof(struct ar933x_serial_priv),
190 .probe = ar933x_serial_probe,
191 .ops = &ar933x_serial_ops,
194 #ifdef CONFIG_DEBUG_UART_AR933X
196 #include <debug_uart.h>
198 static inline void _debug_uart_init(void)
200 void __iomem *regs = (void *)CONFIG_DEBUG_UART_BASE;
201 u32 val, scale, step;
204 * UART controller configuration:
209 * - set RX ready oride
210 * - set TX ready oride
212 val = (AR933X_UART_CS_IF_MODE_DCE << AR933X_UART_CS_IF_MODE_S) |
213 AR933X_UART_CS_TX_RDY_ORIDE | AR933X_UART_CS_RX_RDY_ORIDE;
214 writel(val, regs + AR933X_UART_CS_REG);
216 ar933x_serial_get_scale_step(CONFIG_DEBUG_UART_CLOCK,
217 CONFIG_BAUDRATE, &scale, &step);
219 val = (scale & AR933X_UART_CLK_SCALE_M)
220 << AR933X_UART_CLK_SCALE_S;
221 val |= (step & AR933X_UART_CLK_STEP_M)
222 << AR933X_UART_CLK_STEP_S;
223 writel(val, regs + AR933X_UART_CLK_REG);
226 static inline void _debug_uart_putc(int c)
228 void __iomem *regs = (void *)CONFIG_DEBUG_UART_BASE;
232 data = readl(regs + AR933X_UART_DATA_REG);
233 } while (!(data & AR933X_UART_DATA_TX_CSR));
235 data = (u32)c | AR933X_UART_DATA_TX_CSR;
236 writel(data, regs + AR933X_UART_DATA_REG);