blob: 78e8d9dc7357eedfd83ef321446adf5af3ee076d [file] [log] [blame]
Ryder Lee849b1162018-11-15 10:08:02 +08001// SPDX-License-Identifier: GPL-2.0
2/*
3 * MediaTek High-speed UART driver
4 *
5 * Copyright (C) 2018 MediaTek Inc.
6 * Author: Weijie Gao <weijie.gao@mediatek.com>
7 */
8
9#include <clk.h>
Tom Rini03de3052024-05-20 13:35:03 -060010#include <config.h>
Ryder Lee849b1162018-11-15 10:08:02 +080011#include <div64.h>
12#include <dm.h>
Weijie Gao3b17f2e2022-09-09 19:59:31 +080013#include <dm/device_compat.h>
Ryder Lee849b1162018-11-15 10:08:02 +080014#include <errno.h>
Simon Glassf7ae49f2020-05-10 11:40:05 -060015#include <log.h>
Ryder Lee849b1162018-11-15 10:08:02 +080016#include <serial.h>
17#include <watchdog.h>
Simon Glass401d1c42020-10-30 21:38:53 -060018#include <asm/global_data.h>
Ryder Lee849b1162018-11-15 10:08:02 +080019#include <asm/io.h>
20#include <asm/types.h>
Simon Glass61b29b82020-02-03 07:36:15 -070021#include <linux/err.h>
Simon Glass1e94b462023-09-14 18:21:46 -060022#include <linux/printk.h>
Ryder Lee849b1162018-11-15 10:08:02 +080023
24struct mtk_serial_regs {
25 u32 rbr;
26 u32 ier;
27 u32 fcr;
28 u32 lcr;
29 u32 mcr;
30 u32 lsr;
31 u32 msr;
32 u32 spr;
33 u32 mdr1;
34 u32 highspeed;
35 u32 sample_count;
36 u32 sample_point;
37 u32 fracdiv_l;
38 u32 fracdiv_m;
39 u32 escape_en;
40 u32 guard;
41 u32 rx_sel;
42};
43
44#define thr rbr
45#define iir fcr
46#define dll rbr
47#define dlm ier
48
49#define UART_LCR_WLS_8 0x03 /* 8 bit character length */
50#define UART_LCR_DLAB 0x80 /* Divisor latch access bit */
51
52#define UART_LSR_DR 0x01 /* Data ready */
53#define UART_LSR_THRE 0x20 /* Xmit holding register empty */
Weijie Gao99ced532019-09-25 17:45:17 +080054#define UART_LSR_TEMT 0x40 /* Xmitter empty */
55
56#define UART_MCR_DTR 0x01 /* DTR */
57#define UART_MCR_RTS 0x02 /* RTS */
58
59#define UART_FCR_FIFO_EN 0x01 /* Fifo enable */
60#define UART_FCR_RXSR 0x02 /* Receiver soft reset */
61#define UART_FCR_TXSR 0x04 /* Transmitter soft reset */
62
63#define UART_MCRVAL (UART_MCR_DTR | \
64 UART_MCR_RTS)
65
66/* Clear & enable FIFOs */
67#define UART_FCRVAL (UART_FCR_FIFO_EN | \
68 UART_FCR_RXSR | \
69 UART_FCR_TXSR)
Ryder Lee849b1162018-11-15 10:08:02 +080070
71/* the data is correct if the real baud is within 3%. */
72#define BAUD_ALLOW_MAX(baud) ((baud) + (baud) * 3 / 100)
73#define BAUD_ALLOW_MIX(baud) ((baud) - (baud) * 3 / 100)
74
Weijie Gao3b17f2e2022-09-09 19:59:31 +080075/* struct mtk_serial_priv - Structure holding all information used by the
76 * driver
77 * @regs: Register base of the serial port
78 * @clk: The baud clock device
Christian Marangi41d2cab2024-06-24 23:03:32 +020079 * @clk_bus: The bus clock device
Weijie Gao3b17f2e2022-09-09 19:59:31 +080080 * @fixed_clk_rate: Fallback fixed baud clock rate if baud clock
81 * device is not specified
82 * @force_highspeed: Force using high-speed mode
83 */
Ryder Lee849b1162018-11-15 10:08:02 +080084struct mtk_serial_priv {
85 struct mtk_serial_regs __iomem *regs;
Weijie Gao3b17f2e2022-09-09 19:59:31 +080086 struct clk clk;
Christian Marangi41d2cab2024-06-24 23:03:32 +020087 struct clk clk_bus;
Weijie Gao3b17f2e2022-09-09 19:59:31 +080088 u32 fixed_clk_rate;
Weijie Gao0b9f1ae2021-03-05 10:35:39 +080089 bool force_highspeed;
Ryder Lee849b1162018-11-15 10:08:02 +080090};
91
Weijie Gao3b17f2e2022-09-09 19:59:31 +080092static void _mtk_serial_setbrg(struct mtk_serial_priv *priv, int baud,
93 uint clk_rate)
Ryder Lee849b1162018-11-15 10:08:02 +080094{
Weijie Gao0b9f1ae2021-03-05 10:35:39 +080095 u32 quot, realbaud, samplecount = 1;
Ryder Lee849b1162018-11-15 10:08:02 +080096
Weijie Gao0b9f1ae2021-03-05 10:35:39 +080097 /* Special case for low baud clock */
Weijie Gao3b17f2e2022-09-09 19:59:31 +080098 if (baud <= 115200 && clk_rate == 12000000) {
Weijie Gao0b9f1ae2021-03-05 10:35:39 +080099 writel(3, &priv->regs->highspeed);
100
Weijie Gao3b17f2e2022-09-09 19:59:31 +0800101 quot = DIV_ROUND_CLOSEST(clk_rate, 256 * baud);
Weijie Gao0b9f1ae2021-03-05 10:35:39 +0800102 if (quot == 0)
103 quot = 1;
104
Weijie Gao3b17f2e2022-09-09 19:59:31 +0800105 samplecount = DIV_ROUND_CLOSEST(clk_rate, quot * baud);
Weijie Gao0b9f1ae2021-03-05 10:35:39 +0800106
Weijie Gao3b17f2e2022-09-09 19:59:31 +0800107 realbaud = clk_rate / samplecount / quot;
Weijie Gao0b9f1ae2021-03-05 10:35:39 +0800108 if (realbaud > BAUD_ALLOW_MAX(baud) ||
109 realbaud < BAUD_ALLOW_MIX(baud)) {
110 pr_info("baud %d can't be handled\n", baud);
111 }
112
113 goto set_baud;
114 }
115
116 if (priv->force_highspeed)
117 goto use_hs3;
Ryder Lee849b1162018-11-15 10:08:02 +0800118
119 if (baud <= 115200) {
120 writel(0, &priv->regs->highspeed);
Weijie Gao3b17f2e2022-09-09 19:59:31 +0800121 quot = DIV_ROUND_CLOSEST(clk_rate, 16 * baud);
Ryder Lee849b1162018-11-15 10:08:02 +0800122 } else if (baud <= 576000) {
123 writel(2, &priv->regs->highspeed);
124
125 /* Set to next lower baudrate supported */
126 if ((baud == 500000) || (baud == 576000))
127 baud = 460800;
Weijie Gao0b9f1ae2021-03-05 10:35:39 +0800128
Weijie Gao3b17f2e2022-09-09 19:59:31 +0800129 quot = DIV_ROUND_UP(clk_rate, 4 * baud);
Ryder Lee849b1162018-11-15 10:08:02 +0800130 } else {
Weijie Gao0b9f1ae2021-03-05 10:35:39 +0800131use_hs3:
Ryder Lee849b1162018-11-15 10:08:02 +0800132 writel(3, &priv->regs->highspeed);
Weijie Gao0b9f1ae2021-03-05 10:35:39 +0800133
Weijie Gao3b17f2e2022-09-09 19:59:31 +0800134 quot = DIV_ROUND_UP(clk_rate, 256 * baud);
135 samplecount = DIV_ROUND_CLOSEST(clk_rate, quot * baud);
Ryder Lee849b1162018-11-15 10:08:02 +0800136 }
137
Weijie Gao0b9f1ae2021-03-05 10:35:39 +0800138set_baud:
Ryder Lee849b1162018-11-15 10:08:02 +0800139 /* set divisor */
140 writel(UART_LCR_WLS_8 | UART_LCR_DLAB, &priv->regs->lcr);
141 writel(quot & 0xff, &priv->regs->dll);
142 writel((quot >> 8) & 0xff, &priv->regs->dlm);
143 writel(UART_LCR_WLS_8, &priv->regs->lcr);
144
Weijie Gao0b9f1ae2021-03-05 10:35:39 +0800145 /* set highspeed mode sample count & point */
146 writel(samplecount - 1, &priv->regs->sample_count);
147 writel((samplecount - 2) >> 1, &priv->regs->sample_point);
Ryder Lee849b1162018-11-15 10:08:02 +0800148}
149
Weijie Gao44fa6762019-09-25 17:45:18 +0800150static int _mtk_serial_putc(struct mtk_serial_priv *priv, const char ch)
151{
152 if (!(readl(&priv->regs->lsr) & UART_LSR_THRE))
153 return -EAGAIN;
154
155 writel(ch, &priv->regs->thr);
156
157 if (ch == '\n')
Stefan Roese29caf932022-09-02 14:10:46 +0200158 schedule();
Weijie Gao44fa6762019-09-25 17:45:18 +0800159
160 return 0;
161}
162
163static int _mtk_serial_getc(struct mtk_serial_priv *priv)
164{
165 if (!(readl(&priv->regs->lsr) & UART_LSR_DR))
166 return -EAGAIN;
167
168 return readl(&priv->regs->rbr);
169}
170
171static int _mtk_serial_pending(struct mtk_serial_priv *priv, bool input)
172{
173 if (input)
174 return (readl(&priv->regs->lsr) & UART_LSR_DR) ? 1 : 0;
175 else
176 return (readl(&priv->regs->lsr) & UART_LSR_THRE) ? 0 : 1;
177}
178
Tom Rini0478dac2022-12-04 10:14:13 -0500179#if CONFIG_IS_ENABLED(DM_SERIAL)
Ryder Lee849b1162018-11-15 10:08:02 +0800180static int mtk_serial_setbrg(struct udevice *dev, int baudrate)
181{
182 struct mtk_serial_priv *priv = dev_get_priv(dev);
Weijie Gao3b17f2e2022-09-09 19:59:31 +0800183 u32 clk_rate;
Ryder Lee849b1162018-11-15 10:08:02 +0800184
Weijie Gao3b17f2e2022-09-09 19:59:31 +0800185 clk_rate = clk_get_rate(&priv->clk);
186 if (IS_ERR_VALUE(clk_rate) || clk_rate == 0)
187 clk_rate = priv->fixed_clk_rate;
188
189 _mtk_serial_setbrg(priv, baudrate, clk_rate);
Ryder Lee849b1162018-11-15 10:08:02 +0800190
191 return 0;
192}
193
194static int mtk_serial_putc(struct udevice *dev, const char ch)
195{
196 struct mtk_serial_priv *priv = dev_get_priv(dev);
197
Weijie Gao44fa6762019-09-25 17:45:18 +0800198 return _mtk_serial_putc(priv, ch);
Ryder Lee849b1162018-11-15 10:08:02 +0800199}
200
201static int mtk_serial_getc(struct udevice *dev)
202{
203 struct mtk_serial_priv *priv = dev_get_priv(dev);
204
Weijie Gao44fa6762019-09-25 17:45:18 +0800205 return _mtk_serial_getc(priv);
Ryder Lee849b1162018-11-15 10:08:02 +0800206}
207
208static int mtk_serial_pending(struct udevice *dev, bool input)
209{
210 struct mtk_serial_priv *priv = dev_get_priv(dev);
211
Weijie Gao44fa6762019-09-25 17:45:18 +0800212 return _mtk_serial_pending(priv, input);
Ryder Lee849b1162018-11-15 10:08:02 +0800213}
214
215static int mtk_serial_probe(struct udevice *dev)
216{
217 struct mtk_serial_priv *priv = dev_get_priv(dev);
218
219 /* Disable interrupt */
220 writel(0, &priv->regs->ier);
221
Weijie Gao99ced532019-09-25 17:45:17 +0800222 writel(UART_MCRVAL, &priv->regs->mcr);
223 writel(UART_FCRVAL, &priv->regs->fcr);
224
Christian Marangi41d2cab2024-06-24 23:03:32 +0200225 clk_enable(&priv->clk);
226 if (priv->clk_bus.dev)
227 clk_enable(&priv->clk_bus);
228
Ryder Lee849b1162018-11-15 10:08:02 +0800229 return 0;
230}
231
Simon Glassd1998a92020-12-03 16:55:21 -0700232static int mtk_serial_of_to_plat(struct udevice *dev)
Ryder Lee849b1162018-11-15 10:08:02 +0800233{
234 struct mtk_serial_priv *priv = dev_get_priv(dev);
235 fdt_addr_t addr;
Ryder Lee849b1162018-11-15 10:08:02 +0800236 int err;
237
238 addr = dev_read_addr(dev);
239 if (addr == FDT_ADDR_T_NONE)
240 return -EINVAL;
241
242 priv->regs = map_physmem(addr, 0, MAP_NOCACHE);
243
Weijie Gao3b17f2e2022-09-09 19:59:31 +0800244 err = clk_get_by_index(dev, 0, &priv->clk);
245 if (err) {
246 err = dev_read_u32(dev, "clock-frequency", &priv->fixed_clk_rate);
247 if (err) {
248 dev_err(dev, "baud clock not defined\n");
249 return -EINVAL;
250 }
251 } else {
252 err = clk_get_rate(&priv->clk);
253 if (IS_ERR_VALUE(err)) {
254 dev_err(dev, "invalid baud clock\n");
255 return -EINVAL;
256 }
Ryder Lee849b1162018-11-15 10:08:02 +0800257 }
258
Christian Marangi41d2cab2024-06-24 23:03:32 +0200259 clk_get_by_name(dev, "bus", &priv->clk_bus);
260
Weijie Gao0b9f1ae2021-03-05 10:35:39 +0800261 priv->force_highspeed = dev_read_bool(dev, "mediatek,force-highspeed");
262
Ryder Lee849b1162018-11-15 10:08:02 +0800263 return 0;
264}
265
266static const struct dm_serial_ops mtk_serial_ops = {
267 .putc = mtk_serial_putc,
268 .pending = mtk_serial_pending,
269 .getc = mtk_serial_getc,
270 .setbrg = mtk_serial_setbrg,
271};
272
273static const struct udevice_id mtk_serial_ids[] = {
274 { .compatible = "mediatek,hsuart" },
275 { .compatible = "mediatek,mt6577-uart" },
276 { }
277};
278
279U_BOOT_DRIVER(serial_mtk) = {
280 .name = "serial_mtk",
281 .id = UCLASS_SERIAL,
282 .of_match = mtk_serial_ids,
Simon Glassd1998a92020-12-03 16:55:21 -0700283 .of_to_plat = mtk_serial_of_to_plat,
Simon Glass41575d82020-12-03 16:55:17 -0700284 .priv_auto = sizeof(struct mtk_serial_priv),
Ryder Lee849b1162018-11-15 10:08:02 +0800285 .probe = mtk_serial_probe,
286 .ops = &mtk_serial_ops,
287 .flags = DM_FLAG_PRE_RELOC,
288};
Weijie Gao44fa6762019-09-25 17:45:18 +0800289#else
290
291DECLARE_GLOBAL_DATA_PTR;
292
293#define DECLARE_HSUART_PRIV(port) \
294 static struct mtk_serial_priv mtk_hsuart##port = { \
Tom Rini91092132022-11-16 13:10:28 -0500295 .regs = (struct mtk_serial_regs *)CFG_SYS_NS16550_COM##port, \
296 .fixed_clk_rate = CFG_SYS_NS16550_CLK \
Weijie Gao44fa6762019-09-25 17:45:18 +0800297};
298
299#define DECLARE_HSUART_FUNCTIONS(port) \
300 static int mtk_serial##port##_init(void) \
301 { \
302 writel(0, &mtk_hsuart##port.regs->ier); \
303 writel(UART_MCRVAL, &mtk_hsuart##port.regs->mcr); \
304 writel(UART_FCRVAL, &mtk_hsuart##port.regs->fcr); \
Weijie Gao3b17f2e2022-09-09 19:59:31 +0800305 _mtk_serial_setbrg(&mtk_hsuart##port, gd->baudrate, \
306 mtk_hsuart##port.fixed_clk_rate); \
Weijie Gao44fa6762019-09-25 17:45:18 +0800307 return 0 ; \
308 } \
309 static void mtk_serial##port##_setbrg(void) \
310 { \
Weijie Gao3b17f2e2022-09-09 19:59:31 +0800311 _mtk_serial_setbrg(&mtk_hsuart##port, gd->baudrate, \
312 mtk_hsuart##port.fixed_clk_rate); \
Weijie Gao44fa6762019-09-25 17:45:18 +0800313 } \
314 static int mtk_serial##port##_getc(void) \
315 { \
316 int err; \
317 do { \
318 err = _mtk_serial_getc(&mtk_hsuart##port); \
319 if (err == -EAGAIN) \
Stefan Roese29caf932022-09-02 14:10:46 +0200320 schedule(); \
Weijie Gao44fa6762019-09-25 17:45:18 +0800321 } while (err == -EAGAIN); \
322 return err >= 0 ? err : 0; \
323 } \
324 static int mtk_serial##port##_tstc(void) \
325 { \
326 return _mtk_serial_pending(&mtk_hsuart##port, true); \
327 } \
328 static void mtk_serial##port##_putc(const char c) \
329 { \
330 int err; \
331 if (c == '\n') \
332 mtk_serial##port##_putc('\r'); \
333 do { \
334 err = _mtk_serial_putc(&mtk_hsuart##port, c); \
335 } while (err == -EAGAIN); \
336 } \
337 static void mtk_serial##port##_puts(const char *s) \
338 { \
339 while (*s) { \
340 mtk_serial##port##_putc(*s++); \
341 } \
342 }
343
344/* Serial device descriptor */
345#define INIT_HSUART_STRUCTURE(port, __name) { \
346 .name = __name, \
347 .start = mtk_serial##port##_init, \
348 .stop = NULL, \
349 .setbrg = mtk_serial##port##_setbrg, \
350 .getc = mtk_serial##port##_getc, \
351 .tstc = mtk_serial##port##_tstc, \
352 .putc = mtk_serial##port##_putc, \
353 .puts = mtk_serial##port##_puts, \
354}
355
356#define DECLARE_HSUART(port, __name) \
357 DECLARE_HSUART_PRIV(port); \
358 DECLARE_HSUART_FUNCTIONS(port); \
359 struct serial_device mtk_hsuart##port##_device = \
360 INIT_HSUART_STRUCTURE(port, __name);
361
362#if !defined(CONFIG_CONS_INDEX)
363#elif (CONFIG_CONS_INDEX < 1) || (CONFIG_CONS_INDEX > 6)
364#error "Invalid console index value."
365#endif
366
Tom Rini91092132022-11-16 13:10:28 -0500367#if CONFIG_CONS_INDEX == 1 && !defined(CFG_SYS_NS16550_COM1)
Weijie Gao44fa6762019-09-25 17:45:18 +0800368#error "Console port 1 defined but not configured."
Tom Rini91092132022-11-16 13:10:28 -0500369#elif CONFIG_CONS_INDEX == 2 && !defined(CFG_SYS_NS16550_COM2)
Weijie Gao44fa6762019-09-25 17:45:18 +0800370#error "Console port 2 defined but not configured."
Tom Rini91092132022-11-16 13:10:28 -0500371#elif CONFIG_CONS_INDEX == 3 && !defined(CFG_SYS_NS16550_COM3)
Weijie Gao44fa6762019-09-25 17:45:18 +0800372#error "Console port 3 defined but not configured."
Tom Rini91092132022-11-16 13:10:28 -0500373#elif CONFIG_CONS_INDEX == 4 && !defined(CFG_SYS_NS16550_COM4)
Weijie Gao44fa6762019-09-25 17:45:18 +0800374#error "Console port 4 defined but not configured."
Tom Rini91092132022-11-16 13:10:28 -0500375#elif CONFIG_CONS_INDEX == 5 && !defined(CFG_SYS_NS16550_COM5)
Weijie Gao44fa6762019-09-25 17:45:18 +0800376#error "Console port 5 defined but not configured."
Tom Rini91092132022-11-16 13:10:28 -0500377#elif CONFIG_CONS_INDEX == 6 && !defined(CFG_SYS_NS16550_COM6)
Weijie Gao44fa6762019-09-25 17:45:18 +0800378#error "Console port 6 defined but not configured."
379#endif
380
Tom Rini91092132022-11-16 13:10:28 -0500381#if defined(CFG_SYS_NS16550_COM1)
Weijie Gao44fa6762019-09-25 17:45:18 +0800382DECLARE_HSUART(1, "mtk-hsuart0");
383#endif
Tom Rini91092132022-11-16 13:10:28 -0500384#if defined(CFG_SYS_NS16550_COM2)
Weijie Gao44fa6762019-09-25 17:45:18 +0800385DECLARE_HSUART(2, "mtk-hsuart1");
386#endif
Tom Rini91092132022-11-16 13:10:28 -0500387#if defined(CFG_SYS_NS16550_COM3)
Weijie Gao44fa6762019-09-25 17:45:18 +0800388DECLARE_HSUART(3, "mtk-hsuart2");
389#endif
Tom Rini91092132022-11-16 13:10:28 -0500390#if defined(CFG_SYS_NS16550_COM4)
Weijie Gao44fa6762019-09-25 17:45:18 +0800391DECLARE_HSUART(4, "mtk-hsuart3");
392#endif
Tom Rini91092132022-11-16 13:10:28 -0500393#if defined(CFG_SYS_NS16550_COM5)
Weijie Gao44fa6762019-09-25 17:45:18 +0800394DECLARE_HSUART(5, "mtk-hsuart4");
395#endif
Tom Rini91092132022-11-16 13:10:28 -0500396#if defined(CFG_SYS_NS16550_COM6)
Weijie Gao44fa6762019-09-25 17:45:18 +0800397DECLARE_HSUART(6, "mtk-hsuart5");
398#endif
399
400__weak struct serial_device *default_serial_console(void)
401{
402#if CONFIG_CONS_INDEX == 1
403 return &mtk_hsuart1_device;
404#elif CONFIG_CONS_INDEX == 2
405 return &mtk_hsuart2_device;
406#elif CONFIG_CONS_INDEX == 3
407 return &mtk_hsuart3_device;
408#elif CONFIG_CONS_INDEX == 4
409 return &mtk_hsuart4_device;
410#elif CONFIG_CONS_INDEX == 5
411 return &mtk_hsuart5_device;
412#elif CONFIG_CONS_INDEX == 6
413 return &mtk_hsuart6_device;
414#else
415#error "Bad CONFIG_CONS_INDEX."
416#endif
417}
418
419void mtk_serial_initialize(void)
420{
Tom Rini91092132022-11-16 13:10:28 -0500421#if defined(CFG_SYS_NS16550_COM1)
Weijie Gao44fa6762019-09-25 17:45:18 +0800422 serial_register(&mtk_hsuart1_device);
423#endif
Tom Rini91092132022-11-16 13:10:28 -0500424#if defined(CFG_SYS_NS16550_COM2)
Weijie Gao44fa6762019-09-25 17:45:18 +0800425 serial_register(&mtk_hsuart2_device);
426#endif
Tom Rini91092132022-11-16 13:10:28 -0500427#if defined(CFG_SYS_NS16550_COM3)
Weijie Gao44fa6762019-09-25 17:45:18 +0800428 serial_register(&mtk_hsuart3_device);
429#endif
Tom Rini91092132022-11-16 13:10:28 -0500430#if defined(CFG_SYS_NS16550_COM4)
Weijie Gao44fa6762019-09-25 17:45:18 +0800431 serial_register(&mtk_hsuart4_device);
432#endif
Tom Rini91092132022-11-16 13:10:28 -0500433#if defined(CFG_SYS_NS16550_COM5)
Weijie Gao44fa6762019-09-25 17:45:18 +0800434 serial_register(&mtk_hsuart5_device);
435#endif
Tom Rini91092132022-11-16 13:10:28 -0500436#if defined(CFG_SYS_NS16550_COM6)
Weijie Gao44fa6762019-09-25 17:45:18 +0800437 serial_register(&mtk_hsuart6_device);
438#endif
439}
440
441#endif
Ryder Lee849b1162018-11-15 10:08:02 +0800442
443#ifdef CONFIG_DEBUG_UART_MTK
444
445#include <debug_uart.h>
446
447static inline void _debug_uart_init(void)
448{
449 struct mtk_serial_priv priv;
450
Weijie Gao0fd96bf2023-07-19 17:16:07 +0800451 memset(&priv, 0, sizeof(struct mtk_serial_priv));
Pali Rohárb62450c2022-05-27 22:15:24 +0200452 priv.regs = (void *) CONFIG_VAL(DEBUG_UART_BASE);
Weijie Gao3b17f2e2022-09-09 19:59:31 +0800453 priv.fixed_clk_rate = CONFIG_DEBUG_UART_CLOCK;
Ryder Lee849b1162018-11-15 10:08:02 +0800454
455 writel(0, &priv.regs->ier);
Weijie Gao99ced532019-09-25 17:45:17 +0800456 writel(UART_MCRVAL, &priv.regs->mcr);
457 writel(UART_FCRVAL, &priv.regs->fcr);
Ryder Lee849b1162018-11-15 10:08:02 +0800458
Weijie Gao3b17f2e2022-09-09 19:59:31 +0800459 _mtk_serial_setbrg(&priv, CONFIG_BAUDRATE, priv.fixed_clk_rate);
Ryder Lee849b1162018-11-15 10:08:02 +0800460}
461
462static inline void _debug_uart_putc(int ch)
463{
464 struct mtk_serial_regs __iomem *regs =
Pali Rohárb62450c2022-05-27 22:15:24 +0200465 (void *) CONFIG_VAL(DEBUG_UART_BASE);
Ryder Lee849b1162018-11-15 10:08:02 +0800466
467 while (!(readl(&regs->lsr) & UART_LSR_THRE))
468 ;
469
470 writel(ch, &regs->thr);
471}
472
473DEBUG_UART_FUNCS
474
Simon Glass61b29b82020-02-03 07:36:15 -0700475#endif