blob: 01cc415efddeee6ea1eb786ba4050cbb1f9b0cb5 [file] [log] [blame]
developer90af58f2018-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 Rinidec7ea02024-05-20 13:35:03 -060010#include <config.h>
developer90af58f2018-11-15 10:08:02 +080011#include <div64.h>
12#include <dm.h>
Christian Marangi83add962024-06-24 23:03:33 +020013#include <dm/device.h>
developer0dc720a2022-09-09 19:59:31 +080014#include <dm/device_compat.h>
developer90af58f2018-11-15 10:08:02 +080015#include <errno.h>
Simon Glass0f2af882020-05-10 11:40:05 -060016#include <log.h>
developer90af58f2018-11-15 10:08:02 +080017#include <serial.h>
18#include <watchdog.h>
Simon Glass3ba929a2020-10-30 21:38:53 -060019#include <asm/global_data.h>
developer90af58f2018-11-15 10:08:02 +080020#include <asm/io.h>
21#include <asm/types.h>
Simon Glassd66c5f72020-02-03 07:36:15 -070022#include <linux/err.h>
Simon Glassbdd5f812023-09-14 18:21:46 -060023#include <linux/printk.h>
developer90af58f2018-11-15 10:08:02 +080024
25struct mtk_serial_regs {
26 u32 rbr;
27 u32 ier;
28 u32 fcr;
29 u32 lcr;
30 u32 mcr;
31 u32 lsr;
32 u32 msr;
developer2c587372025-05-23 17:25:55 +080033 u32 scr;
34 u32 autobaud_en;
developer90af58f2018-11-15 10:08:02 +080035 u32 highspeed;
36 u32 sample_count;
37 u32 sample_point;
developer2c587372025-05-23 17:25:55 +080038 u32 autobaud_reg;
39 u32 ratefix_ad;
40 u32 autobaud_sample;
41 u32 guard;
42 u32 escape_dat;
43 u32 escape_en;
44 u32 sleep_en;
45 u32 dma_en;
46 u32 rxtri_ad;
developer90af58f2018-11-15 10:08:02 +080047 u32 fracdiv_l;
48 u32 fracdiv_m;
developer2c587372025-05-23 17:25:55 +080049 u32 fcr_rd;
developer90af58f2018-11-15 10:08:02 +080050};
51
52#define thr rbr
53#define iir fcr
54#define dll rbr
55#define dlm ier
56
57#define UART_LCR_WLS_8 0x03 /* 8 bit character length */
58#define UART_LCR_DLAB 0x80 /* Divisor latch access bit */
59
60#define UART_LSR_DR 0x01 /* Data ready */
61#define UART_LSR_THRE 0x20 /* Xmit holding register empty */
developer67d2b612019-09-25 17:45:17 +080062#define UART_LSR_TEMT 0x40 /* Xmitter empty */
63
64#define UART_MCR_DTR 0x01 /* DTR */
65#define UART_MCR_RTS 0x02 /* RTS */
66
67#define UART_FCR_FIFO_EN 0x01 /* Fifo enable */
68#define UART_FCR_RXSR 0x02 /* Receiver soft reset */
69#define UART_FCR_TXSR 0x04 /* Transmitter soft reset */
70
71#define UART_MCRVAL (UART_MCR_DTR | \
72 UART_MCR_RTS)
73
74/* Clear & enable FIFOs */
75#define UART_FCRVAL (UART_FCR_FIFO_EN | \
76 UART_FCR_RXSR | \
77 UART_FCR_TXSR)
developer90af58f2018-11-15 10:08:02 +080078
79/* the data is correct if the real baud is within 3%. */
80#define BAUD_ALLOW_MAX(baud) ((baud) + (baud) * 3 / 100)
81#define BAUD_ALLOW_MIX(baud) ((baud) - (baud) * 3 / 100)
82
developer0dc720a2022-09-09 19:59:31 +080083/* struct mtk_serial_priv - Structure holding all information used by the
84 * driver
85 * @regs: Register base of the serial port
86 * @clk: The baud clock device
Christian Marangifb438682024-06-24 23:03:32 +020087 * @clk_bus: The bus clock device
developer0dc720a2022-09-09 19:59:31 +080088 * @fixed_clk_rate: Fallback fixed baud clock rate if baud clock
89 * device is not specified
90 * @force_highspeed: Force using high-speed mode
Christian Marangi83add962024-06-24 23:03:33 +020091 * @upstream_highspeed_logic: Apply upstream high-speed logic
developer0dc720a2022-09-09 19:59:31 +080092 */
developer90af58f2018-11-15 10:08:02 +080093struct mtk_serial_priv {
94 struct mtk_serial_regs __iomem *regs;
developer0dc720a2022-09-09 19:59:31 +080095 struct clk clk;
Christian Marangifb438682024-06-24 23:03:32 +020096 struct clk clk_bus;
developer0dc720a2022-09-09 19:59:31 +080097 u32 fixed_clk_rate;
developerdc457732021-03-05 10:35:39 +080098 bool force_highspeed;
Christian Marangi83add962024-06-24 23:03:33 +020099 bool upstream_highspeed_logic;
developer90af58f2018-11-15 10:08:02 +0800100};
101
developer9dd3ea02025-05-23 17:26:02 +0800102static const unsigned short fraction_l_mapping[] = {
103 0, 1, 0x5, 0x15, 0x55, 0x57, 0x57, 0x77, 0x7F, 0xFF, 0xFF
104};
105
106static const unsigned short fraction_m_mapping[] = {
107 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 3
108};
109
developer0dc720a2022-09-09 19:59:31 +0800110static void _mtk_serial_setbrg(struct mtk_serial_priv *priv, int baud,
111 uint clk_rate)
developer90af58f2018-11-15 10:08:02 +0800112{
developer9dd3ea02025-05-23 17:26:02 +0800113 u32 quot, realbaud, samplecount = 1, fraction, frac_l = 0, frac_m = 0;
developer90af58f2018-11-15 10:08:02 +0800114
developerdc457732021-03-05 10:35:39 +0800115 /* Special case for low baud clock */
developer0dc720a2022-09-09 19:59:31 +0800116 if (baud <= 115200 && clk_rate == 12000000) {
developerdc457732021-03-05 10:35:39 +0800117 writel(3, &priv->regs->highspeed);
developer90af58f2018-11-15 10:08:02 +0800118
developer0dc720a2022-09-09 19:59:31 +0800119 quot = DIV_ROUND_CLOSEST(clk_rate, 256 * baud);
developerdc457732021-03-05 10:35:39 +0800120 if (quot == 0)
121 quot = 1;
developer90af58f2018-11-15 10:08:02 +0800122
developer0dc720a2022-09-09 19:59:31 +0800123 samplecount = DIV_ROUND_CLOSEST(clk_rate, quot * baud);
developer90af58f2018-11-15 10:08:02 +0800124
developer0dc720a2022-09-09 19:59:31 +0800125 realbaud = clk_rate / samplecount / quot;
developerdc457732021-03-05 10:35:39 +0800126 if (realbaud > BAUD_ALLOW_MAX(baud) ||
127 realbaud < BAUD_ALLOW_MIX(baud)) {
128 pr_info("baud %d can't be handled\n", baud);
developer90af58f2018-11-15 10:08:02 +0800129 }
developerdc457732021-03-05 10:35:39 +0800130
131 goto set_baud;
132 }
133
Christian Marangi83add962024-06-24 23:03:33 +0200134 /*
135 * Upstream linux use highspeed for anything >= 115200 and lowspeed
136 * for < 115200. Simulate this if we are using the upstream compatible.
137 */
138 if (priv->force_highspeed ||
139 (priv->upstream_highspeed_logic && baud >= 115200))
developerdc457732021-03-05 10:35:39 +0800140 goto use_hs3;
141
142 if (baud <= 115200) {
143 writel(0, &priv->regs->highspeed);
developer0dc720a2022-09-09 19:59:31 +0800144 quot = DIV_ROUND_CLOSEST(clk_rate, 16 * baud);
developer90af58f2018-11-15 10:08:02 +0800145 } else if (baud <= 576000) {
146 writel(2, &priv->regs->highspeed);
147
148 /* Set to next lower baudrate supported */
149 if ((baud == 500000) || (baud == 576000))
150 baud = 460800;
developerdc457732021-03-05 10:35:39 +0800151
developer0dc720a2022-09-09 19:59:31 +0800152 quot = DIV_ROUND_UP(clk_rate, 4 * baud);
developer90af58f2018-11-15 10:08:02 +0800153 } else {
developerdc457732021-03-05 10:35:39 +0800154use_hs3:
developer90af58f2018-11-15 10:08:02 +0800155 writel(3, &priv->regs->highspeed);
developerdc457732021-03-05 10:35:39 +0800156
developer0dc720a2022-09-09 19:59:31 +0800157 quot = DIV_ROUND_UP(clk_rate, 256 * baud);
developer9dd3ea02025-05-23 17:26:02 +0800158 samplecount = clk_rate / (quot * baud);
159
160 fraction = ((clk_rate * 100) / quot / baud) % 100;
161 fraction = DIV_ROUND_CLOSEST(fraction, 10);
162
163 frac_l = fraction_l_mapping[fraction];
164 frac_m = fraction_m_mapping[fraction];
developer90af58f2018-11-15 10:08:02 +0800165 }
166
developerdc457732021-03-05 10:35:39 +0800167set_baud:
developer90af58f2018-11-15 10:08:02 +0800168 /* set divisor */
169 writel(UART_LCR_WLS_8 | UART_LCR_DLAB, &priv->regs->lcr);
170 writel(quot & 0xff, &priv->regs->dll);
171 writel((quot >> 8) & 0xff, &priv->regs->dlm);
172 writel(UART_LCR_WLS_8, &priv->regs->lcr);
173
developerdc457732021-03-05 10:35:39 +0800174 /* set highspeed mode sample count & point */
175 writel(samplecount - 1, &priv->regs->sample_count);
developer9dd3ea02025-05-23 17:26:02 +0800176 writel((samplecount >> 1) - 1, &priv->regs->sample_point);
177
178 /* set baudrate fraction compensation */
179 writel(frac_l, &priv->regs->fracdiv_l);
180 writel(frac_m, &priv->regs->fracdiv_m);
developer90af58f2018-11-15 10:08:02 +0800181}
182
developer77c7c732019-09-25 17:45:18 +0800183static int _mtk_serial_putc(struct mtk_serial_priv *priv, const char ch)
184{
185 if (!(readl(&priv->regs->lsr) & UART_LSR_THRE))
186 return -EAGAIN;
187
188 writel(ch, &priv->regs->thr);
189
190 if (ch == '\n')
Stefan Roese80877fa2022-09-02 14:10:46 +0200191 schedule();
developer77c7c732019-09-25 17:45:18 +0800192
193 return 0;
194}
195
196static int _mtk_serial_getc(struct mtk_serial_priv *priv)
197{
198 if (!(readl(&priv->regs->lsr) & UART_LSR_DR))
199 return -EAGAIN;
200
201 return readl(&priv->regs->rbr);
202}
203
204static int _mtk_serial_pending(struct mtk_serial_priv *priv, bool input)
205{
206 if (input)
207 return (readl(&priv->regs->lsr) & UART_LSR_DR) ? 1 : 0;
208 else
209 return (readl(&priv->regs->lsr) & UART_LSR_THRE) ? 0 : 1;
210}
211
Tom Rini952cc382022-12-04 10:14:13 -0500212#if CONFIG_IS_ENABLED(DM_SERIAL)
developer90af58f2018-11-15 10:08:02 +0800213static int mtk_serial_setbrg(struct udevice *dev, int baudrate)
214{
215 struct mtk_serial_priv *priv = dev_get_priv(dev);
developer0dc720a2022-09-09 19:59:31 +0800216 u32 clk_rate;
developer90af58f2018-11-15 10:08:02 +0800217
developer0dc720a2022-09-09 19:59:31 +0800218 clk_rate = clk_get_rate(&priv->clk);
219 if (IS_ERR_VALUE(clk_rate) || clk_rate == 0)
220 clk_rate = priv->fixed_clk_rate;
221
222 _mtk_serial_setbrg(priv, baudrate, clk_rate);
developer90af58f2018-11-15 10:08:02 +0800223
224 return 0;
225}
226
227static int mtk_serial_putc(struct udevice *dev, const char ch)
228{
229 struct mtk_serial_priv *priv = dev_get_priv(dev);
230
developer77c7c732019-09-25 17:45:18 +0800231 return _mtk_serial_putc(priv, ch);
developer90af58f2018-11-15 10:08:02 +0800232}
233
234static int mtk_serial_getc(struct udevice *dev)
235{
236 struct mtk_serial_priv *priv = dev_get_priv(dev);
237
developer77c7c732019-09-25 17:45:18 +0800238 return _mtk_serial_getc(priv);
developer90af58f2018-11-15 10:08:02 +0800239}
240
241static int mtk_serial_pending(struct udevice *dev, bool input)
242{
243 struct mtk_serial_priv *priv = dev_get_priv(dev);
244
developer77c7c732019-09-25 17:45:18 +0800245 return _mtk_serial_pending(priv, input);
developer90af58f2018-11-15 10:08:02 +0800246}
247
248static int mtk_serial_probe(struct udevice *dev)
249{
250 struct mtk_serial_priv *priv = dev_get_priv(dev);
251
252 /* Disable interrupt */
253 writel(0, &priv->regs->ier);
254
developer67d2b612019-09-25 17:45:17 +0800255 writel(UART_MCRVAL, &priv->regs->mcr);
256 writel(UART_FCRVAL, &priv->regs->fcr);
257
Christian Marangifb438682024-06-24 23:03:32 +0200258 clk_enable(&priv->clk);
259 if (priv->clk_bus.dev)
260 clk_enable(&priv->clk_bus);
261
developer90af58f2018-11-15 10:08:02 +0800262 return 0;
263}
264
Simon Glassaad29ae2020-12-03 16:55:21 -0700265static int mtk_serial_of_to_plat(struct udevice *dev)
developer90af58f2018-11-15 10:08:02 +0800266{
267 struct mtk_serial_priv *priv = dev_get_priv(dev);
268 fdt_addr_t addr;
developer90af58f2018-11-15 10:08:02 +0800269 int err;
270
271 addr = dev_read_addr(dev);
272 if (addr == FDT_ADDR_T_NONE)
273 return -EINVAL;
274
275 priv->regs = map_physmem(addr, 0, MAP_NOCACHE);
276
developer0dc720a2022-09-09 19:59:31 +0800277 err = clk_get_by_index(dev, 0, &priv->clk);
278 if (err) {
279 err = dev_read_u32(dev, "clock-frequency", &priv->fixed_clk_rate);
280 if (err) {
281 dev_err(dev, "baud clock not defined\n");
282 return -EINVAL;
283 }
284 } else {
285 err = clk_get_rate(&priv->clk);
286 if (IS_ERR_VALUE(err)) {
287 dev_err(dev, "invalid baud clock\n");
288 return -EINVAL;
289 }
developer90af58f2018-11-15 10:08:02 +0800290 }
291
Christian Marangifb438682024-06-24 23:03:32 +0200292 clk_get_by_name(dev, "bus", &priv->clk_bus);
293
developerdc457732021-03-05 10:35:39 +0800294 priv->force_highspeed = dev_read_bool(dev, "mediatek,force-highspeed");
Christian Marangi83add962024-06-24 23:03:33 +0200295 priv->upstream_highspeed_logic =
296 device_is_compatible(dev, "mediatek,mt6577-uart");
developerdc457732021-03-05 10:35:39 +0800297
developer90af58f2018-11-15 10:08:02 +0800298 return 0;
299}
300
301static const struct dm_serial_ops mtk_serial_ops = {
302 .putc = mtk_serial_putc,
303 .pending = mtk_serial_pending,
304 .getc = mtk_serial_getc,
305 .setbrg = mtk_serial_setbrg,
306};
307
308static const struct udevice_id mtk_serial_ids[] = {
309 { .compatible = "mediatek,hsuart" },
310 { .compatible = "mediatek,mt6577-uart" },
311 { }
312};
313
314U_BOOT_DRIVER(serial_mtk) = {
315 .name = "serial_mtk",
316 .id = UCLASS_SERIAL,
317 .of_match = mtk_serial_ids,
Simon Glassaad29ae2020-12-03 16:55:21 -0700318 .of_to_plat = mtk_serial_of_to_plat,
Simon Glass8a2b47f2020-12-03 16:55:17 -0700319 .priv_auto = sizeof(struct mtk_serial_priv),
developer90af58f2018-11-15 10:08:02 +0800320 .probe = mtk_serial_probe,
321 .ops = &mtk_serial_ops,
322 .flags = DM_FLAG_PRE_RELOC,
323};
developer77c7c732019-09-25 17:45:18 +0800324#else
325
326DECLARE_GLOBAL_DATA_PTR;
327
328#define DECLARE_HSUART_PRIV(port) \
329 static struct mtk_serial_priv mtk_hsuart##port = { \
Tom Rinidf6a2152022-11-16 13:10:28 -0500330 .regs = (struct mtk_serial_regs *)CFG_SYS_NS16550_COM##port, \
331 .fixed_clk_rate = CFG_SYS_NS16550_CLK \
developer77c7c732019-09-25 17:45:18 +0800332};
developer90af58f2018-11-15 10:08:02 +0800333
developer77c7c732019-09-25 17:45:18 +0800334#define DECLARE_HSUART_FUNCTIONS(port) \
335 static int mtk_serial##port##_init(void) \
336 { \
337 writel(0, &mtk_hsuart##port.regs->ier); \
338 writel(UART_MCRVAL, &mtk_hsuart##port.regs->mcr); \
339 writel(UART_FCRVAL, &mtk_hsuart##port.regs->fcr); \
developer0dc720a2022-09-09 19:59:31 +0800340 _mtk_serial_setbrg(&mtk_hsuart##port, gd->baudrate, \
341 mtk_hsuart##port.fixed_clk_rate); \
developer77c7c732019-09-25 17:45:18 +0800342 return 0 ; \
343 } \
344 static void mtk_serial##port##_setbrg(void) \
345 { \
developer0dc720a2022-09-09 19:59:31 +0800346 _mtk_serial_setbrg(&mtk_hsuart##port, gd->baudrate, \
347 mtk_hsuart##port.fixed_clk_rate); \
developer77c7c732019-09-25 17:45:18 +0800348 } \
349 static int mtk_serial##port##_getc(void) \
350 { \
351 int err; \
352 do { \
353 err = _mtk_serial_getc(&mtk_hsuart##port); \
354 if (err == -EAGAIN) \
Stefan Roese80877fa2022-09-02 14:10:46 +0200355 schedule(); \
developer77c7c732019-09-25 17:45:18 +0800356 } while (err == -EAGAIN); \
357 return err >= 0 ? err : 0; \
358 } \
359 static int mtk_serial##port##_tstc(void) \
360 { \
361 return _mtk_serial_pending(&mtk_hsuart##port, true); \
362 } \
363 static void mtk_serial##port##_putc(const char c) \
364 { \
365 int err; \
366 if (c == '\n') \
367 mtk_serial##port##_putc('\r'); \
368 do { \
369 err = _mtk_serial_putc(&mtk_hsuart##port, c); \
370 } while (err == -EAGAIN); \
371 } \
372 static void mtk_serial##port##_puts(const char *s) \
373 { \
374 while (*s) { \
375 mtk_serial##port##_putc(*s++); \
376 } \
377 }
378
379/* Serial device descriptor */
380#define INIT_HSUART_STRUCTURE(port, __name) { \
381 .name = __name, \
382 .start = mtk_serial##port##_init, \
383 .stop = NULL, \
384 .setbrg = mtk_serial##port##_setbrg, \
385 .getc = mtk_serial##port##_getc, \
386 .tstc = mtk_serial##port##_tstc, \
387 .putc = mtk_serial##port##_putc, \
388 .puts = mtk_serial##port##_puts, \
389}
390
391#define DECLARE_HSUART(port, __name) \
392 DECLARE_HSUART_PRIV(port); \
393 DECLARE_HSUART_FUNCTIONS(port); \
394 struct serial_device mtk_hsuart##port##_device = \
395 INIT_HSUART_STRUCTURE(port, __name);
396
397#if !defined(CONFIG_CONS_INDEX)
398#elif (CONFIG_CONS_INDEX < 1) || (CONFIG_CONS_INDEX > 6)
399#error "Invalid console index value."
400#endif
401
Tom Rinidf6a2152022-11-16 13:10:28 -0500402#if CONFIG_CONS_INDEX == 1 && !defined(CFG_SYS_NS16550_COM1)
developer77c7c732019-09-25 17:45:18 +0800403#error "Console port 1 defined but not configured."
Tom Rinidf6a2152022-11-16 13:10:28 -0500404#elif CONFIG_CONS_INDEX == 2 && !defined(CFG_SYS_NS16550_COM2)
developer77c7c732019-09-25 17:45:18 +0800405#error "Console port 2 defined but not configured."
Tom Rinidf6a2152022-11-16 13:10:28 -0500406#elif CONFIG_CONS_INDEX == 3 && !defined(CFG_SYS_NS16550_COM3)
developer77c7c732019-09-25 17:45:18 +0800407#error "Console port 3 defined but not configured."
Tom Rinidf6a2152022-11-16 13:10:28 -0500408#elif CONFIG_CONS_INDEX == 4 && !defined(CFG_SYS_NS16550_COM4)
developer77c7c732019-09-25 17:45:18 +0800409#error "Console port 4 defined but not configured."
Tom Rinidf6a2152022-11-16 13:10:28 -0500410#elif CONFIG_CONS_INDEX == 5 && !defined(CFG_SYS_NS16550_COM5)
developer77c7c732019-09-25 17:45:18 +0800411#error "Console port 5 defined but not configured."
Tom Rinidf6a2152022-11-16 13:10:28 -0500412#elif CONFIG_CONS_INDEX == 6 && !defined(CFG_SYS_NS16550_COM6)
developer77c7c732019-09-25 17:45:18 +0800413#error "Console port 6 defined but not configured."
414#endif
415
Tom Rinidf6a2152022-11-16 13:10:28 -0500416#if defined(CFG_SYS_NS16550_COM1)
developer77c7c732019-09-25 17:45:18 +0800417DECLARE_HSUART(1, "mtk-hsuart0");
418#endif
Tom Rinidf6a2152022-11-16 13:10:28 -0500419#if defined(CFG_SYS_NS16550_COM2)
developer77c7c732019-09-25 17:45:18 +0800420DECLARE_HSUART(2, "mtk-hsuart1");
421#endif
Tom Rinidf6a2152022-11-16 13:10:28 -0500422#if defined(CFG_SYS_NS16550_COM3)
developer77c7c732019-09-25 17:45:18 +0800423DECLARE_HSUART(3, "mtk-hsuart2");
424#endif
Tom Rinidf6a2152022-11-16 13:10:28 -0500425#if defined(CFG_SYS_NS16550_COM4)
developer77c7c732019-09-25 17:45:18 +0800426DECLARE_HSUART(4, "mtk-hsuart3");
427#endif
Tom Rinidf6a2152022-11-16 13:10:28 -0500428#if defined(CFG_SYS_NS16550_COM5)
developer77c7c732019-09-25 17:45:18 +0800429DECLARE_HSUART(5, "mtk-hsuart4");
430#endif
Tom Rinidf6a2152022-11-16 13:10:28 -0500431#if defined(CFG_SYS_NS16550_COM6)
developer77c7c732019-09-25 17:45:18 +0800432DECLARE_HSUART(6, "mtk-hsuart5");
433#endif
434
435__weak struct serial_device *default_serial_console(void)
436{
437#if CONFIG_CONS_INDEX == 1
438 return &mtk_hsuart1_device;
439#elif CONFIG_CONS_INDEX == 2
440 return &mtk_hsuart2_device;
441#elif CONFIG_CONS_INDEX == 3
442 return &mtk_hsuart3_device;
443#elif CONFIG_CONS_INDEX == 4
444 return &mtk_hsuart4_device;
445#elif CONFIG_CONS_INDEX == 5
446 return &mtk_hsuart5_device;
447#elif CONFIG_CONS_INDEX == 6
448 return &mtk_hsuart6_device;
449#else
450#error "Bad CONFIG_CONS_INDEX."
451#endif
452}
453
454void mtk_serial_initialize(void)
455{
Tom Rinidf6a2152022-11-16 13:10:28 -0500456#if defined(CFG_SYS_NS16550_COM1)
developer77c7c732019-09-25 17:45:18 +0800457 serial_register(&mtk_hsuart1_device);
458#endif
Tom Rinidf6a2152022-11-16 13:10:28 -0500459#if defined(CFG_SYS_NS16550_COM2)
developer77c7c732019-09-25 17:45:18 +0800460 serial_register(&mtk_hsuart2_device);
461#endif
Tom Rinidf6a2152022-11-16 13:10:28 -0500462#if defined(CFG_SYS_NS16550_COM3)
developer77c7c732019-09-25 17:45:18 +0800463 serial_register(&mtk_hsuart3_device);
464#endif
Tom Rinidf6a2152022-11-16 13:10:28 -0500465#if defined(CFG_SYS_NS16550_COM4)
developer77c7c732019-09-25 17:45:18 +0800466 serial_register(&mtk_hsuart4_device);
467#endif
Tom Rinidf6a2152022-11-16 13:10:28 -0500468#if defined(CFG_SYS_NS16550_COM5)
developer77c7c732019-09-25 17:45:18 +0800469 serial_register(&mtk_hsuart5_device);
470#endif
Tom Rinidf6a2152022-11-16 13:10:28 -0500471#if defined(CFG_SYS_NS16550_COM6)
developer77c7c732019-09-25 17:45:18 +0800472 serial_register(&mtk_hsuart6_device);
473#endif
474}
475
476#endif
477
developer90af58f2018-11-15 10:08:02 +0800478#ifdef CONFIG_DEBUG_UART_MTK
479
480#include <debug_uart.h>
481
482static inline void _debug_uart_init(void)
483{
484 struct mtk_serial_priv priv;
485
developer5a83d2b2023-07-19 17:16:07 +0800486 memset(&priv, 0, sizeof(struct mtk_serial_priv));
Pali Rohár8864b352022-05-27 22:15:24 +0200487 priv.regs = (void *) CONFIG_VAL(DEBUG_UART_BASE);
developer0dc720a2022-09-09 19:59:31 +0800488 priv.fixed_clk_rate = CONFIG_DEBUG_UART_CLOCK;
developer90af58f2018-11-15 10:08:02 +0800489
490 writel(0, &priv.regs->ier);
developer67d2b612019-09-25 17:45:17 +0800491 writel(UART_MCRVAL, &priv.regs->mcr);
492 writel(UART_FCRVAL, &priv.regs->fcr);
developer90af58f2018-11-15 10:08:02 +0800493
developer0dc720a2022-09-09 19:59:31 +0800494 _mtk_serial_setbrg(&priv, CONFIG_BAUDRATE, priv.fixed_clk_rate);
developer90af58f2018-11-15 10:08:02 +0800495}
496
497static inline void _debug_uart_putc(int ch)
498{
499 struct mtk_serial_regs __iomem *regs =
Pali Rohár8864b352022-05-27 22:15:24 +0200500 (void *) CONFIG_VAL(DEBUG_UART_BASE);
developer90af58f2018-11-15 10:08:02 +0800501
502 while (!(readl(&regs->lsr) & UART_LSR_THRE))
503 ;
504
505 writel(ch, &regs->thr);
506}
507
508DEBUG_UART_FUNCS
509
Simon Glassd66c5f72020-02-03 07:36:15 -0700510#endif