1 // SPDX-License-Identifier: GPL-2.0
3 * MediaTek High-speed UART driver
5 * Copyright (C) 2018 MediaTek Inc.
6 * Author: Weijie Gao <weijie.gao@mediatek.com>
17 #include <asm/types.h>
19 struct mtk_serial_regs {
44 #define UART_LCR_WLS_8 0x03 /* 8 bit character length */
45 #define UART_LCR_DLAB 0x80 /* Divisor latch access bit */
47 #define UART_LSR_DR 0x01 /* Data ready */
48 #define UART_LSR_THRE 0x20 /* Xmit holding register empty */
49 #define UART_LSR_TEMT 0x40 /* Xmitter empty */
51 #define UART_MCR_DTR 0x01 /* DTR */
52 #define UART_MCR_RTS 0x02 /* RTS */
54 #define UART_FCR_FIFO_EN 0x01 /* Fifo enable */
55 #define UART_FCR_RXSR 0x02 /* Receiver soft reset */
56 #define UART_FCR_TXSR 0x04 /* Transmitter soft reset */
58 #define UART_MCRVAL (UART_MCR_DTR | \
61 /* Clear & enable FIFOs */
62 #define UART_FCRVAL (UART_FCR_FIFO_EN | \
66 /* the data is correct if the real baud is within 3%. */
67 #define BAUD_ALLOW_MAX(baud) ((baud) + (baud) * 3 / 100)
68 #define BAUD_ALLOW_MIX(baud) ((baud) - (baud) * 3 / 100)
70 struct mtk_serial_priv {
71 struct mtk_serial_regs __iomem *regs;
75 static void _mtk_serial_setbrg(struct mtk_serial_priv *priv, int baud)
77 bool support_clk12m_baud115200;
78 u32 quot, samplecount, realbaud;
80 if ((baud <= 115200) && (priv->clock == 12000000))
81 support_clk12m_baud115200 = true;
83 support_clk12m_baud115200 = false;
86 writel(0, &priv->regs->highspeed);
87 quot = DIV_ROUND_CLOSEST(priv->clock, 16 * baud);
89 if (support_clk12m_baud115200) {
90 writel(3, &priv->regs->highspeed);
91 quot = DIV_ROUND_CLOSEST(priv->clock, 256 * baud);
95 samplecount = DIV_ROUND_CLOSEST(priv->clock,
97 if (samplecount != 0) {
98 realbaud = priv->clock / samplecount / quot;
99 if ((realbaud > BAUD_ALLOW_MAX(baud)) ||
100 (realbaud < BAUD_ALLOW_MIX(baud))) {
101 pr_info("baud %d can't be handled\n",
105 pr_info("samplecount is 0\n");
108 } else if (baud <= 576000) {
109 writel(2, &priv->regs->highspeed);
111 /* Set to next lower baudrate supported */
112 if ((baud == 500000) || (baud == 576000))
114 quot = DIV_ROUND_UP(priv->clock, 4 * baud);
116 writel(3, &priv->regs->highspeed);
117 quot = DIV_ROUND_UP(priv->clock, 256 * baud);
121 writel(UART_LCR_WLS_8 | UART_LCR_DLAB, &priv->regs->lcr);
122 writel(quot & 0xff, &priv->regs->dll);
123 writel((quot >> 8) & 0xff, &priv->regs->dlm);
124 writel(UART_LCR_WLS_8, &priv->regs->lcr);
129 tmp = DIV_ROUND_CLOSEST(priv->clock, quot * baud);
130 writel(tmp - 1, &priv->regs->sample_count);
131 writel((tmp - 2) >> 1, &priv->regs->sample_point);
133 writel(0, &priv->regs->sample_count);
134 writel(0xff, &priv->regs->sample_point);
137 if (support_clk12m_baud115200) {
138 writel(samplecount - 1, &priv->regs->sample_count);
139 writel((samplecount - 2) >> 1, &priv->regs->sample_point);
143 static int mtk_serial_setbrg(struct udevice *dev, int baudrate)
145 struct mtk_serial_priv *priv = dev_get_priv(dev);
147 _mtk_serial_setbrg(priv, baudrate);
152 static int mtk_serial_putc(struct udevice *dev, const char ch)
154 struct mtk_serial_priv *priv = dev_get_priv(dev);
156 if (!(readl(&priv->regs->lsr) & UART_LSR_THRE))
159 writel(ch, &priv->regs->thr);
167 static int mtk_serial_getc(struct udevice *dev)
169 struct mtk_serial_priv *priv = dev_get_priv(dev);
171 if (!(readl(&priv->regs->lsr) & UART_LSR_DR))
174 return readl(&priv->regs->rbr);
177 static int mtk_serial_pending(struct udevice *dev, bool input)
179 struct mtk_serial_priv *priv = dev_get_priv(dev);
182 return (readl(&priv->regs->lsr) & UART_LSR_DR) ? 1 : 0;
184 return (readl(&priv->regs->lsr) & UART_LSR_THRE) ? 0 : 1;
187 static int mtk_serial_probe(struct udevice *dev)
189 struct mtk_serial_priv *priv = dev_get_priv(dev);
191 /* Disable interrupt */
192 writel(0, &priv->regs->ier);
194 writel(UART_MCRVAL, &priv->regs->mcr);
195 writel(UART_FCRVAL, &priv->regs->fcr);
200 static int mtk_serial_ofdata_to_platdata(struct udevice *dev)
202 struct mtk_serial_priv *priv = dev_get_priv(dev);
207 addr = dev_read_addr(dev);
208 if (addr == FDT_ADDR_T_NONE)
211 priv->regs = map_physmem(addr, 0, MAP_NOCACHE);
213 err = clk_get_by_index(dev, 0, &clk);
215 err = clk_get_rate(&clk);
216 if (!IS_ERR_VALUE(err))
218 } else if (err != -ENOENT && err != -ENODEV && err != -ENOSYS) {
219 debug("mtk_serial: failed to get clock\n");
224 priv->clock = dev_read_u32_default(dev, "clock-frequency", 0);
227 debug("mtk_serial: clock not defined\n");
234 static const struct dm_serial_ops mtk_serial_ops = {
235 .putc = mtk_serial_putc,
236 .pending = mtk_serial_pending,
237 .getc = mtk_serial_getc,
238 .setbrg = mtk_serial_setbrg,
241 static const struct udevice_id mtk_serial_ids[] = {
242 { .compatible = "mediatek,hsuart" },
243 { .compatible = "mediatek,mt6577-uart" },
247 U_BOOT_DRIVER(serial_mtk) = {
248 .name = "serial_mtk",
250 .of_match = mtk_serial_ids,
251 .ofdata_to_platdata = mtk_serial_ofdata_to_platdata,
252 .priv_auto_alloc_size = sizeof(struct mtk_serial_priv),
253 .probe = mtk_serial_probe,
254 .ops = &mtk_serial_ops,
255 .flags = DM_FLAG_PRE_RELOC,
258 #ifdef CONFIG_DEBUG_UART_MTK
260 #include <debug_uart.h>
262 static inline void _debug_uart_init(void)
264 struct mtk_serial_priv priv;
266 priv.regs = (void *) CONFIG_DEBUG_UART_BASE;
267 priv.clock = CONFIG_DEBUG_UART_CLOCK;
269 writel(0, &priv.regs->ier);
270 writel(UART_MCRVAL, &priv.regs->mcr);
271 writel(UART_FCRVAL, &priv.regs->fcr);
273 _mtk_serial_setbrg(&priv, CONFIG_BAUDRATE);
276 static inline void _debug_uart_putc(int ch)
278 struct mtk_serial_regs __iomem *regs =
279 (void *) CONFIG_DEBUG_UART_BASE;
281 while (!(readl(®s->lsr) & UART_LSR_THRE))
284 writel(ch, ®s->thr);