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