1 // SPDX-License-Identifier: GPL-2.0
3 * UART driver for MediaTek MT7620 and earlier SoCs
5 * Copyright (C) 2020 MediaTek Inc.
6 * Author: Weijie Gao <weijie.gao@mediatek.com>
18 #include <asm/types.h>
19 #include <asm/addrspace.h>
20 #include <dm/device_compat.h>
21 #include <linux/err.h>
23 #if CONFIG_IS_ENABLED(OF_PLATDATA)
24 #include <dt-structs.h>
27 struct mt7620_serial_regs {
44 #define UART_LCR_WLS_8 0x03 /* 8 bit character length */
46 #define UART_LSR_DR 0x01 /* Data ready */
47 #define UART_LSR_THRE 0x20 /* Xmit holding register empty */
48 #define UART_LSR_TEMT 0x40 /* Xmitter empty */
50 #define UART_MCR_DTR 0x01 /* DTR */
51 #define UART_MCR_RTS 0x02 /* RTS */
53 #define UART_FCR_FIFO_EN 0x01 /* Fifo enable */
54 #define UART_FCR_RXSR 0x02 /* Receiver soft reset */
55 #define UART_FCR_TXSR 0x04 /* Transmitter soft reset */
57 #define UART_MCRVAL (UART_MCR_DTR | \
60 /* Clear & enable FIFOs */
61 #define UART_FCRVAL (UART_FCR_FIFO_EN | \
65 struct mt7620_serial_plat {
66 #if CONFIG_IS_ENABLED(OF_PLATDATA)
67 struct dtd_serial_mt7620 dtplat;
70 struct mt7620_serial_regs __iomem *regs;
74 static void _mt7620_serial_setbrg(struct mt7620_serial_plat *plat, int baud)
79 quot = DIV_ROUND_CLOSEST(plat->clock, 16 * baud);
80 writel(quot, &plat->regs->dl);
82 /* set character length and stop bits */
83 writel(UART_LCR_WLS_8, &plat->regs->lcr);
86 static int mt7620_serial_setbrg(struct udevice *dev, int baudrate)
88 struct mt7620_serial_plat *plat = dev_get_plat(dev);
90 _mt7620_serial_setbrg(plat, baudrate);
95 static int mt7620_serial_putc(struct udevice *dev, const char ch)
97 struct mt7620_serial_plat *plat = dev_get_plat(dev);
99 if (!(readl(&plat->regs->lsr) & UART_LSR_THRE))
102 writel(ch, &plat->regs->thr);
110 static int mt7620_serial_getc(struct udevice *dev)
112 struct mt7620_serial_plat *plat = dev_get_plat(dev);
114 if (!(readl(&plat->regs->lsr) & UART_LSR_DR))
117 return readl(&plat->regs->rbr);
120 static int mt7620_serial_pending(struct udevice *dev, bool input)
122 struct mt7620_serial_plat *plat = dev_get_plat(dev);
125 return (readl(&plat->regs->lsr) & UART_LSR_DR) ? 1 : 0;
127 return (readl(&plat->regs->lsr) & UART_LSR_THRE) ? 0 : 1;
130 static int mt7620_serial_probe(struct udevice *dev)
132 struct mt7620_serial_plat *plat = dev_get_plat(dev);
134 #if CONFIG_IS_ENABLED(OF_PLATDATA)
135 plat->regs = (void __iomem *)KSEG1ADDR(plat->dtplat.reg[0]);
136 plat->clock = plat->dtplat.clock_frequency;
139 /* Disable interrupt */
140 writel(0, &plat->regs->ier);
142 writel(UART_MCRVAL, &plat->regs->mcr);
143 writel(UART_FCRVAL, &plat->regs->fcr);
148 #if CONFIG_IS_ENABLED(OF_REAL)
149 static int mt7620_serial_of_to_plat(struct udevice *dev)
151 struct mt7620_serial_plat *plat = dev_get_plat(dev);
152 struct reset_ctl reset_uart;
156 err = reset_get_by_index(dev, 0, &reset_uart);
158 reset_deassert(&reset_uart);
160 plat->regs = dev_remap_addr_index(dev, 0);
162 dev_err(dev, "mt7620_serial: unable to map UART registers\n");
166 err = clk_get_by_index(dev, 0, &clk);
168 err = clk_get_rate(&clk);
169 if (!IS_ERR_VALUE(err))
171 } else if (err != -ENOENT && err != -ENODEV && err != -ENOSYS) {
172 dev_err(dev, "mt7620_serial: failed to get clock\n");
177 plat->clock = dev_read_u32_default(dev, "clock-frequency", 0);
180 dev_err(dev, "mt7620_serial: clock not defined\n");
187 static const struct udevice_id mt7620_serial_ids[] = {
188 { .compatible = "mediatek,mt7620-uart" },
193 static const struct dm_serial_ops mt7620_serial_ops = {
194 .putc = mt7620_serial_putc,
195 .pending = mt7620_serial_pending,
196 .getc = mt7620_serial_getc,
197 .setbrg = mt7620_serial_setbrg,
200 U_BOOT_DRIVER(serial_mt7620) = {
201 .name = "serial_mt7620",
203 #if CONFIG_IS_ENABLED(OF_REAL)
204 .of_match = mt7620_serial_ids,
205 .of_to_plat = mt7620_serial_of_to_plat,
207 .plat_auto = sizeof(struct mt7620_serial_plat),
208 .probe = mt7620_serial_probe,
209 .ops = &mt7620_serial_ops,
210 .flags = DM_FLAG_PRE_RELOC,
213 DM_DRIVER_ALIAS(serial_mt7620, mediatek_mt7620_uart);
215 #ifdef CONFIG_DEBUG_UART_MT7620
217 #include <debug_uart.h>
219 static inline void _debug_uart_init(void)
221 struct mt7620_serial_plat plat;
223 plat.regs = (void *)CONFIG_DEBUG_UART_BASE;
224 plat.clock = CONFIG_DEBUG_UART_CLOCK;
226 writel(0, &plat.regs->ier);
227 writel(UART_MCRVAL, &plat.regs->mcr);
228 writel(UART_FCRVAL, &plat.regs->fcr);
230 _mt7620_serial_setbrg(&plat, CONFIG_BAUDRATE);
233 static inline void _debug_uart_putc(int ch)
235 struct mt7620_serial_regs __iomem *regs =
236 (void *)CONFIG_DEBUG_UART_BASE;
238 while (!(readl(®s->lsr) & UART_LSR_THRE))
241 writel(ch, ®s->thr);