Ryder Lee | 849b116 | 2018-11-15 10:08:02 +0800 | [diff] [blame] | 1 | // 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> |
| 10 | #include <common.h> |
| 11 | #include <div64.h> |
| 12 | #include <dm.h> |
| 13 | #include <errno.h> |
| 14 | #include <serial.h> |
| 15 | #include <watchdog.h> |
| 16 | #include <asm/io.h> |
| 17 | #include <asm/types.h> |
Simon Glass | 61b29b8 | 2020-02-03 07:36:15 -0700 | [diff] [blame^] | 18 | #include <linux/err.h> |
Ryder Lee | 849b116 | 2018-11-15 10:08:02 +0800 | [diff] [blame] | 19 | |
| 20 | struct mtk_serial_regs { |
| 21 | u32 rbr; |
| 22 | u32 ier; |
| 23 | u32 fcr; |
| 24 | u32 lcr; |
| 25 | u32 mcr; |
| 26 | u32 lsr; |
| 27 | u32 msr; |
| 28 | u32 spr; |
| 29 | u32 mdr1; |
| 30 | u32 highspeed; |
| 31 | u32 sample_count; |
| 32 | u32 sample_point; |
| 33 | u32 fracdiv_l; |
| 34 | u32 fracdiv_m; |
| 35 | u32 escape_en; |
| 36 | u32 guard; |
| 37 | u32 rx_sel; |
| 38 | }; |
| 39 | |
| 40 | #define thr rbr |
| 41 | #define iir fcr |
| 42 | #define dll rbr |
| 43 | #define dlm ier |
| 44 | |
| 45 | #define UART_LCR_WLS_8 0x03 /* 8 bit character length */ |
| 46 | #define UART_LCR_DLAB 0x80 /* Divisor latch access bit */ |
| 47 | |
| 48 | #define UART_LSR_DR 0x01 /* Data ready */ |
| 49 | #define UART_LSR_THRE 0x20 /* Xmit holding register empty */ |
Weijie Gao | 99ced53 | 2019-09-25 17:45:17 +0800 | [diff] [blame] | 50 | #define UART_LSR_TEMT 0x40 /* Xmitter empty */ |
| 51 | |
| 52 | #define UART_MCR_DTR 0x01 /* DTR */ |
| 53 | #define UART_MCR_RTS 0x02 /* RTS */ |
| 54 | |
| 55 | #define UART_FCR_FIFO_EN 0x01 /* Fifo enable */ |
| 56 | #define UART_FCR_RXSR 0x02 /* Receiver soft reset */ |
| 57 | #define UART_FCR_TXSR 0x04 /* Transmitter soft reset */ |
| 58 | |
| 59 | #define UART_MCRVAL (UART_MCR_DTR | \ |
| 60 | UART_MCR_RTS) |
| 61 | |
| 62 | /* Clear & enable FIFOs */ |
| 63 | #define UART_FCRVAL (UART_FCR_FIFO_EN | \ |
| 64 | UART_FCR_RXSR | \ |
| 65 | UART_FCR_TXSR) |
Ryder Lee | 849b116 | 2018-11-15 10:08:02 +0800 | [diff] [blame] | 66 | |
| 67 | /* the data is correct if the real baud is within 3%. */ |
| 68 | #define BAUD_ALLOW_MAX(baud) ((baud) + (baud) * 3 / 100) |
| 69 | #define BAUD_ALLOW_MIX(baud) ((baud) - (baud) * 3 / 100) |
| 70 | |
| 71 | struct mtk_serial_priv { |
| 72 | struct mtk_serial_regs __iomem *regs; |
| 73 | u32 clock; |
| 74 | }; |
| 75 | |
| 76 | static void _mtk_serial_setbrg(struct mtk_serial_priv *priv, int baud) |
| 77 | { |
| 78 | bool support_clk12m_baud115200; |
| 79 | u32 quot, samplecount, realbaud; |
| 80 | |
| 81 | if ((baud <= 115200) && (priv->clock == 12000000)) |
| 82 | support_clk12m_baud115200 = true; |
| 83 | else |
| 84 | support_clk12m_baud115200 = false; |
| 85 | |
| 86 | if (baud <= 115200) { |
| 87 | writel(0, &priv->regs->highspeed); |
| 88 | quot = DIV_ROUND_CLOSEST(priv->clock, 16 * baud); |
| 89 | |
| 90 | if (support_clk12m_baud115200) { |
| 91 | writel(3, &priv->regs->highspeed); |
| 92 | quot = DIV_ROUND_CLOSEST(priv->clock, 256 * baud); |
| 93 | if (quot == 0) |
| 94 | quot = 1; |
| 95 | |
| 96 | samplecount = DIV_ROUND_CLOSEST(priv->clock, |
| 97 | quot * baud); |
| 98 | if (samplecount != 0) { |
| 99 | realbaud = priv->clock / samplecount / quot; |
| 100 | if ((realbaud > BAUD_ALLOW_MAX(baud)) || |
| 101 | (realbaud < BAUD_ALLOW_MIX(baud))) { |
| 102 | pr_info("baud %d can't be handled\n", |
| 103 | baud); |
| 104 | } |
| 105 | } else { |
| 106 | pr_info("samplecount is 0\n"); |
| 107 | } |
| 108 | } |
| 109 | } else if (baud <= 576000) { |
| 110 | writel(2, &priv->regs->highspeed); |
| 111 | |
| 112 | /* Set to next lower baudrate supported */ |
| 113 | if ((baud == 500000) || (baud == 576000)) |
| 114 | baud = 460800; |
| 115 | quot = DIV_ROUND_UP(priv->clock, 4 * baud); |
| 116 | } else { |
| 117 | writel(3, &priv->regs->highspeed); |
| 118 | quot = DIV_ROUND_UP(priv->clock, 256 * baud); |
| 119 | } |
| 120 | |
| 121 | /* set divisor */ |
| 122 | writel(UART_LCR_WLS_8 | UART_LCR_DLAB, &priv->regs->lcr); |
| 123 | writel(quot & 0xff, &priv->regs->dll); |
| 124 | writel((quot >> 8) & 0xff, &priv->regs->dlm); |
| 125 | writel(UART_LCR_WLS_8, &priv->regs->lcr); |
| 126 | |
| 127 | if (baud > 460800) { |
| 128 | u32 tmp; |
| 129 | |
| 130 | tmp = DIV_ROUND_CLOSEST(priv->clock, quot * baud); |
| 131 | writel(tmp - 1, &priv->regs->sample_count); |
| 132 | writel((tmp - 2) >> 1, &priv->regs->sample_point); |
| 133 | } else { |
| 134 | writel(0, &priv->regs->sample_count); |
| 135 | writel(0xff, &priv->regs->sample_point); |
| 136 | } |
| 137 | |
| 138 | if (support_clk12m_baud115200) { |
| 139 | writel(samplecount - 1, &priv->regs->sample_count); |
| 140 | writel((samplecount - 2) >> 1, &priv->regs->sample_point); |
| 141 | } |
| 142 | } |
| 143 | |
Weijie Gao | 44fa676 | 2019-09-25 17:45:18 +0800 | [diff] [blame] | 144 | static int _mtk_serial_putc(struct mtk_serial_priv *priv, const char ch) |
| 145 | { |
| 146 | if (!(readl(&priv->regs->lsr) & UART_LSR_THRE)) |
| 147 | return -EAGAIN; |
| 148 | |
| 149 | writel(ch, &priv->regs->thr); |
| 150 | |
| 151 | if (ch == '\n') |
| 152 | WATCHDOG_RESET(); |
| 153 | |
| 154 | return 0; |
| 155 | } |
| 156 | |
| 157 | static int _mtk_serial_getc(struct mtk_serial_priv *priv) |
| 158 | { |
| 159 | if (!(readl(&priv->regs->lsr) & UART_LSR_DR)) |
| 160 | return -EAGAIN; |
| 161 | |
| 162 | return readl(&priv->regs->rbr); |
| 163 | } |
| 164 | |
| 165 | static int _mtk_serial_pending(struct mtk_serial_priv *priv, bool input) |
| 166 | { |
| 167 | if (input) |
| 168 | return (readl(&priv->regs->lsr) & UART_LSR_DR) ? 1 : 0; |
| 169 | else |
| 170 | return (readl(&priv->regs->lsr) & UART_LSR_THRE) ? 0 : 1; |
| 171 | } |
| 172 | |
| 173 | #if defined(CONFIG_DM_SERIAL) && \ |
| 174 | (!defined(CONFIG_SPL_BUILD) || defined(CONFIG_SPL_DM)) |
Ryder Lee | 849b116 | 2018-11-15 10:08:02 +0800 | [diff] [blame] | 175 | static int mtk_serial_setbrg(struct udevice *dev, int baudrate) |
| 176 | { |
| 177 | struct mtk_serial_priv *priv = dev_get_priv(dev); |
| 178 | |
| 179 | _mtk_serial_setbrg(priv, baudrate); |
| 180 | |
| 181 | return 0; |
| 182 | } |
| 183 | |
| 184 | static int mtk_serial_putc(struct udevice *dev, const char ch) |
| 185 | { |
| 186 | struct mtk_serial_priv *priv = dev_get_priv(dev); |
| 187 | |
Weijie Gao | 44fa676 | 2019-09-25 17:45:18 +0800 | [diff] [blame] | 188 | return _mtk_serial_putc(priv, ch); |
Ryder Lee | 849b116 | 2018-11-15 10:08:02 +0800 | [diff] [blame] | 189 | } |
| 190 | |
| 191 | static int mtk_serial_getc(struct udevice *dev) |
| 192 | { |
| 193 | struct mtk_serial_priv *priv = dev_get_priv(dev); |
| 194 | |
Weijie Gao | 44fa676 | 2019-09-25 17:45:18 +0800 | [diff] [blame] | 195 | return _mtk_serial_getc(priv); |
Ryder Lee | 849b116 | 2018-11-15 10:08:02 +0800 | [diff] [blame] | 196 | } |
| 197 | |
| 198 | static int mtk_serial_pending(struct udevice *dev, bool input) |
| 199 | { |
| 200 | struct mtk_serial_priv *priv = dev_get_priv(dev); |
| 201 | |
Weijie Gao | 44fa676 | 2019-09-25 17:45:18 +0800 | [diff] [blame] | 202 | return _mtk_serial_pending(priv, input); |
Ryder Lee | 849b116 | 2018-11-15 10:08:02 +0800 | [diff] [blame] | 203 | } |
| 204 | |
| 205 | static int mtk_serial_probe(struct udevice *dev) |
| 206 | { |
| 207 | struct mtk_serial_priv *priv = dev_get_priv(dev); |
| 208 | |
| 209 | /* Disable interrupt */ |
| 210 | writel(0, &priv->regs->ier); |
| 211 | |
Weijie Gao | 99ced53 | 2019-09-25 17:45:17 +0800 | [diff] [blame] | 212 | writel(UART_MCRVAL, &priv->regs->mcr); |
| 213 | writel(UART_FCRVAL, &priv->regs->fcr); |
| 214 | |
Ryder Lee | 849b116 | 2018-11-15 10:08:02 +0800 | [diff] [blame] | 215 | return 0; |
| 216 | } |
| 217 | |
| 218 | static int mtk_serial_ofdata_to_platdata(struct udevice *dev) |
| 219 | { |
| 220 | struct mtk_serial_priv *priv = dev_get_priv(dev); |
| 221 | fdt_addr_t addr; |
| 222 | struct clk clk; |
| 223 | int err; |
| 224 | |
| 225 | addr = dev_read_addr(dev); |
| 226 | if (addr == FDT_ADDR_T_NONE) |
| 227 | return -EINVAL; |
| 228 | |
| 229 | priv->regs = map_physmem(addr, 0, MAP_NOCACHE); |
| 230 | |
| 231 | err = clk_get_by_index(dev, 0, &clk); |
| 232 | if (!err) { |
| 233 | err = clk_get_rate(&clk); |
| 234 | if (!IS_ERR_VALUE(err)) |
| 235 | priv->clock = err; |
| 236 | } else if (err != -ENOENT && err != -ENODEV && err != -ENOSYS) { |
| 237 | debug("mtk_serial: failed to get clock\n"); |
| 238 | return err; |
| 239 | } |
| 240 | |
| 241 | if (!priv->clock) |
| 242 | priv->clock = dev_read_u32_default(dev, "clock-frequency", 0); |
| 243 | |
| 244 | if (!priv->clock) { |
| 245 | debug("mtk_serial: clock not defined\n"); |
| 246 | return -EINVAL; |
| 247 | } |
| 248 | |
| 249 | return 0; |
| 250 | } |
| 251 | |
| 252 | static const struct dm_serial_ops mtk_serial_ops = { |
| 253 | .putc = mtk_serial_putc, |
| 254 | .pending = mtk_serial_pending, |
| 255 | .getc = mtk_serial_getc, |
| 256 | .setbrg = mtk_serial_setbrg, |
| 257 | }; |
| 258 | |
| 259 | static const struct udevice_id mtk_serial_ids[] = { |
| 260 | { .compatible = "mediatek,hsuart" }, |
| 261 | { .compatible = "mediatek,mt6577-uart" }, |
| 262 | { } |
| 263 | }; |
| 264 | |
| 265 | U_BOOT_DRIVER(serial_mtk) = { |
| 266 | .name = "serial_mtk", |
| 267 | .id = UCLASS_SERIAL, |
| 268 | .of_match = mtk_serial_ids, |
| 269 | .ofdata_to_platdata = mtk_serial_ofdata_to_platdata, |
| 270 | .priv_auto_alloc_size = sizeof(struct mtk_serial_priv), |
| 271 | .probe = mtk_serial_probe, |
| 272 | .ops = &mtk_serial_ops, |
| 273 | .flags = DM_FLAG_PRE_RELOC, |
| 274 | }; |
Weijie Gao | 44fa676 | 2019-09-25 17:45:18 +0800 | [diff] [blame] | 275 | #else |
| 276 | |
| 277 | DECLARE_GLOBAL_DATA_PTR; |
| 278 | |
| 279 | #define DECLARE_HSUART_PRIV(port) \ |
| 280 | static struct mtk_serial_priv mtk_hsuart##port = { \ |
| 281 | .regs = (struct mtk_serial_regs *)CONFIG_SYS_NS16550_COM##port, \ |
| 282 | .clock = CONFIG_SYS_NS16550_CLK \ |
| 283 | }; |
| 284 | |
| 285 | #define DECLARE_HSUART_FUNCTIONS(port) \ |
| 286 | static int mtk_serial##port##_init(void) \ |
| 287 | { \ |
| 288 | writel(0, &mtk_hsuart##port.regs->ier); \ |
| 289 | writel(UART_MCRVAL, &mtk_hsuart##port.regs->mcr); \ |
| 290 | writel(UART_FCRVAL, &mtk_hsuart##port.regs->fcr); \ |
| 291 | _mtk_serial_setbrg(&mtk_hsuart##port, gd->baudrate); \ |
| 292 | return 0 ; \ |
| 293 | } \ |
| 294 | static void mtk_serial##port##_setbrg(void) \ |
| 295 | { \ |
| 296 | _mtk_serial_setbrg(&mtk_hsuart##port, gd->baudrate); \ |
| 297 | } \ |
| 298 | static int mtk_serial##port##_getc(void) \ |
| 299 | { \ |
| 300 | int err; \ |
| 301 | do { \ |
| 302 | err = _mtk_serial_getc(&mtk_hsuart##port); \ |
| 303 | if (err == -EAGAIN) \ |
| 304 | WATCHDOG_RESET(); \ |
| 305 | } while (err == -EAGAIN); \ |
| 306 | return err >= 0 ? err : 0; \ |
| 307 | } \ |
| 308 | static int mtk_serial##port##_tstc(void) \ |
| 309 | { \ |
| 310 | return _mtk_serial_pending(&mtk_hsuart##port, true); \ |
| 311 | } \ |
| 312 | static void mtk_serial##port##_putc(const char c) \ |
| 313 | { \ |
| 314 | int err; \ |
| 315 | if (c == '\n') \ |
| 316 | mtk_serial##port##_putc('\r'); \ |
| 317 | do { \ |
| 318 | err = _mtk_serial_putc(&mtk_hsuart##port, c); \ |
| 319 | } while (err == -EAGAIN); \ |
| 320 | } \ |
| 321 | static void mtk_serial##port##_puts(const char *s) \ |
| 322 | { \ |
| 323 | while (*s) { \ |
| 324 | mtk_serial##port##_putc(*s++); \ |
| 325 | } \ |
| 326 | } |
| 327 | |
| 328 | /* Serial device descriptor */ |
| 329 | #define INIT_HSUART_STRUCTURE(port, __name) { \ |
| 330 | .name = __name, \ |
| 331 | .start = mtk_serial##port##_init, \ |
| 332 | .stop = NULL, \ |
| 333 | .setbrg = mtk_serial##port##_setbrg, \ |
| 334 | .getc = mtk_serial##port##_getc, \ |
| 335 | .tstc = mtk_serial##port##_tstc, \ |
| 336 | .putc = mtk_serial##port##_putc, \ |
| 337 | .puts = mtk_serial##port##_puts, \ |
| 338 | } |
| 339 | |
| 340 | #define DECLARE_HSUART(port, __name) \ |
| 341 | DECLARE_HSUART_PRIV(port); \ |
| 342 | DECLARE_HSUART_FUNCTIONS(port); \ |
| 343 | struct serial_device mtk_hsuart##port##_device = \ |
| 344 | INIT_HSUART_STRUCTURE(port, __name); |
| 345 | |
| 346 | #if !defined(CONFIG_CONS_INDEX) |
| 347 | #elif (CONFIG_CONS_INDEX < 1) || (CONFIG_CONS_INDEX > 6) |
| 348 | #error "Invalid console index value." |
| 349 | #endif |
| 350 | |
| 351 | #if CONFIG_CONS_INDEX == 1 && !defined(CONFIG_SYS_NS16550_COM1) |
| 352 | #error "Console port 1 defined but not configured." |
| 353 | #elif CONFIG_CONS_INDEX == 2 && !defined(CONFIG_SYS_NS16550_COM2) |
| 354 | #error "Console port 2 defined but not configured." |
| 355 | #elif CONFIG_CONS_INDEX == 3 && !defined(CONFIG_SYS_NS16550_COM3) |
| 356 | #error "Console port 3 defined but not configured." |
| 357 | #elif CONFIG_CONS_INDEX == 4 && !defined(CONFIG_SYS_NS16550_COM4) |
| 358 | #error "Console port 4 defined but not configured." |
| 359 | #elif CONFIG_CONS_INDEX == 5 && !defined(CONFIG_SYS_NS16550_COM5) |
| 360 | #error "Console port 5 defined but not configured." |
| 361 | #elif CONFIG_CONS_INDEX == 6 && !defined(CONFIG_SYS_NS16550_COM6) |
| 362 | #error "Console port 6 defined but not configured." |
| 363 | #endif |
| 364 | |
| 365 | #if defined(CONFIG_SYS_NS16550_COM1) |
| 366 | DECLARE_HSUART(1, "mtk-hsuart0"); |
| 367 | #endif |
| 368 | #if defined(CONFIG_SYS_NS16550_COM2) |
| 369 | DECLARE_HSUART(2, "mtk-hsuart1"); |
| 370 | #endif |
| 371 | #if defined(CONFIG_SYS_NS16550_COM3) |
| 372 | DECLARE_HSUART(3, "mtk-hsuart2"); |
| 373 | #endif |
| 374 | #if defined(CONFIG_SYS_NS16550_COM4) |
| 375 | DECLARE_HSUART(4, "mtk-hsuart3"); |
| 376 | #endif |
| 377 | #if defined(CONFIG_SYS_NS16550_COM5) |
| 378 | DECLARE_HSUART(5, "mtk-hsuart4"); |
| 379 | #endif |
| 380 | #if defined(CONFIG_SYS_NS16550_COM6) |
| 381 | DECLARE_HSUART(6, "mtk-hsuart5"); |
| 382 | #endif |
| 383 | |
| 384 | __weak struct serial_device *default_serial_console(void) |
| 385 | { |
| 386 | #if CONFIG_CONS_INDEX == 1 |
| 387 | return &mtk_hsuart1_device; |
| 388 | #elif CONFIG_CONS_INDEX == 2 |
| 389 | return &mtk_hsuart2_device; |
| 390 | #elif CONFIG_CONS_INDEX == 3 |
| 391 | return &mtk_hsuart3_device; |
| 392 | #elif CONFIG_CONS_INDEX == 4 |
| 393 | return &mtk_hsuart4_device; |
| 394 | #elif CONFIG_CONS_INDEX == 5 |
| 395 | return &mtk_hsuart5_device; |
| 396 | #elif CONFIG_CONS_INDEX == 6 |
| 397 | return &mtk_hsuart6_device; |
| 398 | #else |
| 399 | #error "Bad CONFIG_CONS_INDEX." |
| 400 | #endif |
| 401 | } |
| 402 | |
| 403 | void mtk_serial_initialize(void) |
| 404 | { |
| 405 | #if defined(CONFIG_SYS_NS16550_COM1) |
| 406 | serial_register(&mtk_hsuart1_device); |
| 407 | #endif |
| 408 | #if defined(CONFIG_SYS_NS16550_COM2) |
| 409 | serial_register(&mtk_hsuart2_device); |
| 410 | #endif |
| 411 | #if defined(CONFIG_SYS_NS16550_COM3) |
| 412 | serial_register(&mtk_hsuart3_device); |
| 413 | #endif |
| 414 | #if defined(CONFIG_SYS_NS16550_COM4) |
| 415 | serial_register(&mtk_hsuart4_device); |
| 416 | #endif |
| 417 | #if defined(CONFIG_SYS_NS16550_COM5) |
| 418 | serial_register(&mtk_hsuart5_device); |
| 419 | #endif |
| 420 | #if defined(CONFIG_SYS_NS16550_COM6) |
| 421 | serial_register(&mtk_hsuart6_device); |
| 422 | #endif |
| 423 | } |
| 424 | |
| 425 | #endif |
Ryder Lee | 849b116 | 2018-11-15 10:08:02 +0800 | [diff] [blame] | 426 | |
| 427 | #ifdef CONFIG_DEBUG_UART_MTK |
| 428 | |
| 429 | #include <debug_uart.h> |
| 430 | |
| 431 | static inline void _debug_uart_init(void) |
| 432 | { |
| 433 | struct mtk_serial_priv priv; |
| 434 | |
| 435 | priv.regs = (void *) CONFIG_DEBUG_UART_BASE; |
| 436 | priv.clock = CONFIG_DEBUG_UART_CLOCK; |
| 437 | |
| 438 | writel(0, &priv.regs->ier); |
Weijie Gao | 99ced53 | 2019-09-25 17:45:17 +0800 | [diff] [blame] | 439 | writel(UART_MCRVAL, &priv.regs->mcr); |
| 440 | writel(UART_FCRVAL, &priv.regs->fcr); |
Ryder Lee | 849b116 | 2018-11-15 10:08:02 +0800 | [diff] [blame] | 441 | |
| 442 | _mtk_serial_setbrg(&priv, CONFIG_BAUDRATE); |
| 443 | } |
| 444 | |
| 445 | static inline void _debug_uart_putc(int ch) |
| 446 | { |
| 447 | struct mtk_serial_regs __iomem *regs = |
| 448 | (void *) CONFIG_DEBUG_UART_BASE; |
| 449 | |
| 450 | while (!(readl(®s->lsr) & UART_LSR_THRE)) |
| 451 | ; |
| 452 | |
| 453 | writel(ch, ®s->thr); |
| 454 | } |
| 455 | |
| 456 | DEBUG_UART_FUNCS |
| 457 | |
Simon Glass | 61b29b8 | 2020-02-03 07:36:15 -0700 | [diff] [blame^] | 458 | #endif |