Anup Patel | 4b1d68f | 2018-12-15 11:35:15 +0530 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
| 2 | /* |
| 3 | * Copyright (C) 2018 Anup Patel <anup@brainfault.org> |
| 4 | */ |
| 5 | |
Jagan Teki | 9ee724f | 2019-05-08 19:56:16 +0530 | [diff] [blame] | 6 | #include <clk.h> |
Anup Patel | 4b1d68f | 2018-12-15 11:35:15 +0530 | [diff] [blame] | 7 | #include <debug_uart.h> |
| 8 | #include <dm.h> |
| 9 | #include <errno.h> |
| 10 | #include <fdtdec.h> |
Simon Glass | 0f2af88 | 2020-05-10 11:40:05 -0600 | [diff] [blame] | 11 | #include <log.h> |
Anup Patel | 4b1d68f | 2018-12-15 11:35:15 +0530 | [diff] [blame] | 12 | #include <watchdog.h> |
Simon Glass | 3ba929a | 2020-10-30 21:38:53 -0600 | [diff] [blame] | 13 | #include <asm/global_data.h> |
Anup Patel | 4b1d68f | 2018-12-15 11:35:15 +0530 | [diff] [blame] | 14 | #include <asm/io.h> |
| 15 | #include <linux/compiler.h> |
| 16 | #include <serial.h> |
Simon Glass | d66c5f7 | 2020-02-03 07:36:15 -0700 | [diff] [blame] | 17 | #include <linux/err.h> |
Anup Patel | 4b1d68f | 2018-12-15 11:35:15 +0530 | [diff] [blame] | 18 | |
| 19 | DECLARE_GLOBAL_DATA_PTR; |
| 20 | |
| 21 | #define UART_TXFIFO_FULL 0x80000000 |
| 22 | #define UART_RXFIFO_EMPTY 0x80000000 |
| 23 | #define UART_RXFIFO_DATA 0x000000ff |
| 24 | #define UART_TXCTRL_TXEN 0x1 |
| 25 | #define UART_RXCTRL_RXEN 0x1 |
| 26 | |
Sagar Shrikant Kadam | 024d060 | 2019-07-09 05:23:44 -0700 | [diff] [blame] | 27 | /* IP register */ |
| 28 | #define UART_IP_RXWM 0x2 |
| 29 | |
Anup Patel | 4b1d68f | 2018-12-15 11:35:15 +0530 | [diff] [blame] | 30 | struct uart_sifive { |
| 31 | u32 txfifo; |
| 32 | u32 rxfifo; |
| 33 | u32 txctrl; |
| 34 | u32 rxctrl; |
| 35 | u32 ie; |
| 36 | u32 ip; |
| 37 | u32 div; |
| 38 | }; |
| 39 | |
Simon Glass | b75b15b | 2020-12-03 16:55:23 -0700 | [diff] [blame] | 40 | struct sifive_uart_plat { |
Atish Patra | bc922d4 | 2019-02-25 08:15:02 +0000 | [diff] [blame] | 41 | unsigned long clock; |
Anup Patel | 4b1d68f | 2018-12-15 11:35:15 +0530 | [diff] [blame] | 42 | struct uart_sifive *regs; |
| 43 | }; |
| 44 | |
Atish Patra | bc922d4 | 2019-02-25 08:15:02 +0000 | [diff] [blame] | 45 | /** |
| 46 | * Find minimum divisor divides in_freq to max_target_hz; |
| 47 | * Based on uart driver n SiFive FSBL. |
| 48 | * |
| 49 | * f_baud = f_in / (div + 1) => div = (f_in / f_baud) - 1 |
| 50 | * The nearest integer solution requires rounding up as to not exceed |
| 51 | * max_target_hz. |
| 52 | * div = ceil(f_in / f_baud) - 1 |
| 53 | * = floor((f_in - 1 + f_baud) / f_baud) - 1 |
| 54 | * This should not overflow as long as (f_in - 1 + f_baud) does not exceed |
| 55 | * 2^32 - 1, which is unlikely since we represent frequencies in kHz. |
| 56 | */ |
| 57 | static inline unsigned int uart_min_clk_divisor(unsigned long in_freq, |
| 58 | unsigned long max_target_hz) |
| 59 | { |
| 60 | unsigned long quotient = |
| 61 | (in_freq + max_target_hz - 1) / (max_target_hz); |
| 62 | /* Avoid underflow */ |
| 63 | if (quotient == 0) |
| 64 | return 0; |
| 65 | else |
| 66 | return quotient - 1; |
| 67 | } |
| 68 | |
Anup Patel | 4b1d68f | 2018-12-15 11:35:15 +0530 | [diff] [blame] | 69 | /* Set up the baud rate in gd struct */ |
| 70 | static void _sifive_serial_setbrg(struct uart_sifive *regs, |
| 71 | unsigned long clock, unsigned long baud) |
| 72 | { |
Atish Patra | bc922d4 | 2019-02-25 08:15:02 +0000 | [diff] [blame] | 73 | writel((uart_min_clk_divisor(clock, baud)), ®s->div); |
Anup Patel | 4b1d68f | 2018-12-15 11:35:15 +0530 | [diff] [blame] | 74 | } |
| 75 | |
| 76 | static void _sifive_serial_init(struct uart_sifive *regs) |
| 77 | { |
| 78 | writel(UART_TXCTRL_TXEN, ®s->txctrl); |
| 79 | writel(UART_RXCTRL_RXEN, ®s->rxctrl); |
| 80 | writel(0, ®s->ie); |
| 81 | } |
| 82 | |
| 83 | static int _sifive_serial_putc(struct uart_sifive *regs, const char c) |
| 84 | { |
| 85 | if (readl(®s->txfifo) & UART_TXFIFO_FULL) |
| 86 | return -EAGAIN; |
| 87 | |
| 88 | writel(c, ®s->txfifo); |
| 89 | |
| 90 | return 0; |
| 91 | } |
| 92 | |
| 93 | static int _sifive_serial_getc(struct uart_sifive *regs) |
| 94 | { |
| 95 | int ch = readl(®s->rxfifo); |
| 96 | |
| 97 | if (ch & UART_RXFIFO_EMPTY) |
| 98 | return -EAGAIN; |
| 99 | ch &= UART_RXFIFO_DATA; |
| 100 | |
Sagar Shrikant Kadam | 024d060 | 2019-07-09 05:23:44 -0700 | [diff] [blame] | 101 | return ch; |
Anup Patel | 4b1d68f | 2018-12-15 11:35:15 +0530 | [diff] [blame] | 102 | } |
| 103 | |
| 104 | static int sifive_serial_setbrg(struct udevice *dev, int baudrate) |
| 105 | { |
Atish Patra | 574fa69 | 2019-02-25 08:15:08 +0000 | [diff] [blame] | 106 | int ret; |
Anup Patel | 4b1d68f | 2018-12-15 11:35:15 +0530 | [diff] [blame] | 107 | struct clk clk; |
Simon Glass | b75b15b | 2020-12-03 16:55:23 -0700 | [diff] [blame] | 108 | struct sifive_uart_plat *plat = dev_get_plat(dev); |
Atish Patra | 574fa69 | 2019-02-25 08:15:08 +0000 | [diff] [blame] | 109 | u32 clock = 0; |
Anup Patel | 4b1d68f | 2018-12-15 11:35:15 +0530 | [diff] [blame] | 110 | |
Atish Patra | 574fa69 | 2019-02-25 08:15:08 +0000 | [diff] [blame] | 111 | ret = clk_get_by_index(dev, 0, &clk); |
| 112 | if (IS_ERR_VALUE(ret)) { |
Anup Patel | 4b1d68f | 2018-12-15 11:35:15 +0530 | [diff] [blame] | 113 | debug("SiFive UART failed to get clock\n"); |
Atish Patra | 574fa69 | 2019-02-25 08:15:08 +0000 | [diff] [blame] | 114 | ret = dev_read_u32(dev, "clock-frequency", &clock); |
| 115 | if (IS_ERR_VALUE(ret)) { |
| 116 | debug("SiFive UART clock not defined\n"); |
| 117 | return 0; |
| 118 | } |
| 119 | } else { |
| 120 | clock = clk_get_rate(&clk); |
| 121 | if (IS_ERR_VALUE(clock)) { |
| 122 | debug("SiFive UART clock get rate failed\n"); |
| 123 | return 0; |
| 124 | } |
Anup Patel | 4b1d68f | 2018-12-15 11:35:15 +0530 | [diff] [blame] | 125 | } |
Simon Glass | 71fa5b4 | 2020-12-03 16:55:18 -0700 | [diff] [blame] | 126 | plat->clock = clock; |
| 127 | _sifive_serial_setbrg(plat->regs, plat->clock, baudrate); |
Anup Patel | 4b1d68f | 2018-12-15 11:35:15 +0530 | [diff] [blame] | 128 | |
| 129 | return 0; |
| 130 | } |
| 131 | |
| 132 | static int sifive_serial_probe(struct udevice *dev) |
| 133 | { |
Simon Glass | b75b15b | 2020-12-03 16:55:23 -0700 | [diff] [blame] | 134 | struct sifive_uart_plat *plat = dev_get_plat(dev); |
Anup Patel | 4b1d68f | 2018-12-15 11:35:15 +0530 | [diff] [blame] | 135 | |
| 136 | /* No need to reinitialize the UART after relocation */ |
| 137 | if (gd->flags & GD_FLG_RELOC) |
| 138 | return 0; |
| 139 | |
Simon Glass | 71fa5b4 | 2020-12-03 16:55:18 -0700 | [diff] [blame] | 140 | _sifive_serial_init(plat->regs); |
Anup Patel | 4b1d68f | 2018-12-15 11:35:15 +0530 | [diff] [blame] | 141 | |
| 142 | return 0; |
| 143 | } |
| 144 | |
| 145 | static int sifive_serial_getc(struct udevice *dev) |
| 146 | { |
| 147 | int c; |
Simon Glass | b75b15b | 2020-12-03 16:55:23 -0700 | [diff] [blame] | 148 | struct sifive_uart_plat *plat = dev_get_plat(dev); |
Simon Glass | 71fa5b4 | 2020-12-03 16:55:18 -0700 | [diff] [blame] | 149 | struct uart_sifive *regs = plat->regs; |
Anup Patel | 4b1d68f | 2018-12-15 11:35:15 +0530 | [diff] [blame] | 150 | |
Anup Patel | 4b1d68f | 2018-12-15 11:35:15 +0530 | [diff] [blame] | 151 | while ((c = _sifive_serial_getc(regs)) == -EAGAIN) ; |
| 152 | |
| 153 | return c; |
| 154 | } |
| 155 | |
| 156 | static int sifive_serial_putc(struct udevice *dev, const char ch) |
| 157 | { |
| 158 | int rc; |
Simon Glass | b75b15b | 2020-12-03 16:55:23 -0700 | [diff] [blame] | 159 | struct sifive_uart_plat *plat = dev_get_plat(dev); |
Anup Patel | 4b1d68f | 2018-12-15 11:35:15 +0530 | [diff] [blame] | 160 | |
Simon Glass | 71fa5b4 | 2020-12-03 16:55:18 -0700 | [diff] [blame] | 161 | while ((rc = _sifive_serial_putc(plat->regs, ch)) == -EAGAIN) ; |
Anup Patel | 4b1d68f | 2018-12-15 11:35:15 +0530 | [diff] [blame] | 162 | |
| 163 | return rc; |
| 164 | } |
| 165 | |
| 166 | static int sifive_serial_pending(struct udevice *dev, bool input) |
| 167 | { |
Simon Glass | b75b15b | 2020-12-03 16:55:23 -0700 | [diff] [blame] | 168 | struct sifive_uart_plat *plat = dev_get_plat(dev); |
Simon Glass | 71fa5b4 | 2020-12-03 16:55:18 -0700 | [diff] [blame] | 169 | struct uart_sifive *regs = plat->regs; |
Anup Patel | 4b1d68f | 2018-12-15 11:35:15 +0530 | [diff] [blame] | 170 | |
Sagar Shrikant Kadam | 024d060 | 2019-07-09 05:23:44 -0700 | [diff] [blame] | 171 | if (input) |
| 172 | return (readl(®s->ip) & UART_IP_RXWM); |
| 173 | else |
Anup Patel | 4b1d68f | 2018-12-15 11:35:15 +0530 | [diff] [blame] | 174 | return !!(readl(®s->txfifo) & UART_TXFIFO_FULL); |
Anup Patel | 4b1d68f | 2018-12-15 11:35:15 +0530 | [diff] [blame] | 175 | } |
| 176 | |
Simon Glass | aad29ae | 2020-12-03 16:55:21 -0700 | [diff] [blame] | 177 | static int sifive_serial_of_to_plat(struct udevice *dev) |
Anup Patel | 4b1d68f | 2018-12-15 11:35:15 +0530 | [diff] [blame] | 178 | { |
Simon Glass | b75b15b | 2020-12-03 16:55:23 -0700 | [diff] [blame] | 179 | struct sifive_uart_plat *plat = dev_get_plat(dev); |
Anup Patel | 4b1d68f | 2018-12-15 11:35:15 +0530 | [diff] [blame] | 180 | |
Bin Meng | b934fd2 | 2021-01-31 20:36:00 +0800 | [diff] [blame] | 181 | plat->regs = (struct uart_sifive *)(uintptr_t)dev_read_addr(dev); |
Simon Glass | 71fa5b4 | 2020-12-03 16:55:18 -0700 | [diff] [blame] | 182 | if (IS_ERR(plat->regs)) |
| 183 | return PTR_ERR(plat->regs); |
Anup Patel | 4b1d68f | 2018-12-15 11:35:15 +0530 | [diff] [blame] | 184 | |
| 185 | return 0; |
| 186 | } |
| 187 | |
| 188 | static const struct dm_serial_ops sifive_serial_ops = { |
| 189 | .putc = sifive_serial_putc, |
| 190 | .getc = sifive_serial_getc, |
| 191 | .pending = sifive_serial_pending, |
| 192 | .setbrg = sifive_serial_setbrg, |
| 193 | }; |
| 194 | |
| 195 | static const struct udevice_id sifive_serial_ids[] = { |
| 196 | { .compatible = "sifive,uart0" }, |
| 197 | { } |
| 198 | }; |
| 199 | |
| 200 | U_BOOT_DRIVER(serial_sifive) = { |
| 201 | .name = "serial_sifive", |
| 202 | .id = UCLASS_SERIAL, |
| 203 | .of_match = sifive_serial_ids, |
Simon Glass | aad29ae | 2020-12-03 16:55:21 -0700 | [diff] [blame] | 204 | .of_to_plat = sifive_serial_of_to_plat, |
Simon Glass | b75b15b | 2020-12-03 16:55:23 -0700 | [diff] [blame] | 205 | .plat_auto = sizeof(struct sifive_uart_plat), |
Anup Patel | 4b1d68f | 2018-12-15 11:35:15 +0530 | [diff] [blame] | 206 | .probe = sifive_serial_probe, |
| 207 | .ops = &sifive_serial_ops, |
| 208 | }; |
| 209 | |
| 210 | #ifdef CONFIG_DEBUG_UART_SIFIVE |
| 211 | static inline void _debug_uart_init(void) |
| 212 | { |
| 213 | struct uart_sifive *regs = |
Pali Rohár | 8864b35 | 2022-05-27 22:15:24 +0200 | [diff] [blame] | 214 | (struct uart_sifive *)CONFIG_VAL(DEBUG_UART_BASE); |
Anup Patel | 4b1d68f | 2018-12-15 11:35:15 +0530 | [diff] [blame] | 215 | |
| 216 | _sifive_serial_setbrg(regs, CONFIG_DEBUG_UART_CLOCK, |
| 217 | CONFIG_BAUDRATE); |
| 218 | _sifive_serial_init(regs); |
| 219 | } |
| 220 | |
| 221 | static inline void _debug_uart_putc(int ch) |
| 222 | { |
| 223 | struct uart_sifive *regs = |
Pali Rohár | 8864b35 | 2022-05-27 22:15:24 +0200 | [diff] [blame] | 224 | (struct uart_sifive *)CONFIG_VAL(DEBUG_UART_BASE); |
Anup Patel | 4b1d68f | 2018-12-15 11:35:15 +0530 | [diff] [blame] | 225 | |
| 226 | while (_sifive_serial_putc(regs, ch) == -EAGAIN) |
Stefan Roese | 80877fa | 2022-09-02 14:10:46 +0200 | [diff] [blame] | 227 | schedule(); |
Anup Patel | 4b1d68f | 2018-12-15 11:35:15 +0530 | [diff] [blame] | 228 | } |
| 229 | |
| 230 | DEBUG_UART_FUNCS |
| 231 | |
| 232 | #endif |