Tom Rini | 10e4779 | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Mateusz Kulikowski | 8aac697 | 2016-03-31 23:12:14 +0200 | [diff] [blame] | 2 | /* |
| 3 | * Qualcomm UART driver |
| 4 | * |
| 5 | * (C) Copyright 2015 Mateusz Kulikowski <mateusz.kulikowski@gmail.com> |
| 6 | * |
| 7 | * UART will work in Data Mover mode. |
| 8 | * Based on Linux driver. |
Mateusz Kulikowski | 8aac697 | 2016-03-31 23:12:14 +0200 | [diff] [blame] | 9 | */ |
| 10 | |
| 11 | #include <common.h> |
| 12 | #include <clk.h> |
| 13 | #include <dm.h> |
| 14 | #include <errno.h> |
Simon Glass | 9bc1564 | 2020-02-03 07:36:16 -0700 | [diff] [blame] | 15 | #include <malloc.h> |
Mateusz Kulikowski | 8aac697 | 2016-03-31 23:12:14 +0200 | [diff] [blame] | 16 | #include <serial.h> |
| 17 | #include <watchdog.h> |
Simon Glass | 3ba929a | 2020-10-30 21:38:53 -0600 | [diff] [blame] | 18 | #include <asm/global_data.h> |
Mateusz Kulikowski | 8aac697 | 2016-03-31 23:12:14 +0200 | [diff] [blame] | 19 | #include <asm/io.h> |
| 20 | #include <linux/compiler.h> |
Stephan Gerhold | 9a4569f | 2021-07-14 10:56:26 +0200 | [diff] [blame] | 21 | #include <linux/delay.h> |
Ramon Fried | 2292ed3 | 2018-05-16 12:13:42 +0300 | [diff] [blame] | 22 | #include <dm/pinctrl.h> |
Mateusz Kulikowski | 8aac697 | 2016-03-31 23:12:14 +0200 | [diff] [blame] | 23 | |
| 24 | /* Serial registers - this driver works in uartdm mode*/ |
| 25 | |
| 26 | #define UARTDM_DMRX 0x34 /* Max RX transfer length */ |
Stephan Gerhold | 706d481 | 2021-06-28 10:40:09 +0200 | [diff] [blame] | 27 | #define UARTDM_DMEN 0x3C /* DMA/data-packing mode */ |
Mateusz Kulikowski | 8aac697 | 2016-03-31 23:12:14 +0200 | [diff] [blame] | 28 | #define UARTDM_NCF_TX 0x40 /* Number of chars to TX */ |
| 29 | |
| 30 | #define UARTDM_RXFS 0x50 /* RX channel status register */ |
| 31 | #define UARTDM_RXFS_BUF_SHIFT 0x7 /* Number of bytes in the packing buffer */ |
| 32 | #define UARTDM_RXFS_BUF_MASK 0x7 |
Ramon Fried | 2292ed3 | 2018-05-16 12:13:42 +0300 | [diff] [blame] | 33 | #define UARTDM_MR1 0x00 |
| 34 | #define UARTDM_MR2 0x04 |
| 35 | #define UARTDM_CSR 0xA0 |
Mateusz Kulikowski | 8aac697 | 2016-03-31 23:12:14 +0200 | [diff] [blame] | 36 | |
| 37 | #define UARTDM_SR 0xA4 /* Status register */ |
| 38 | #define UARTDM_SR_RX_READY (1 << 0) /* Word is the receiver FIFO */ |
| 39 | #define UARTDM_SR_TX_EMPTY (1 << 3) /* Transmitter underrun */ |
| 40 | #define UARTDM_SR_UART_OVERRUN (1 << 4) /* Receive overrun */ |
| 41 | |
| 42 | #define UARTDM_CR 0xA8 /* Command register */ |
| 43 | #define UARTDM_CR_CMD_RESET_ERR (3 << 4) /* Clear overrun error */ |
| 44 | #define UARTDM_CR_CMD_RESET_STALE_INT (8 << 4) /* Clears stale irq */ |
| 45 | #define UARTDM_CR_CMD_RESET_TX_READY (3 << 8) /* Clears TX Ready irq*/ |
| 46 | #define UARTDM_CR_CMD_FORCE_STALE (4 << 8) /* Causes stale event */ |
| 47 | #define UARTDM_CR_CMD_STALE_EVENT_DISABLE (6 << 8) /* Disable stale event */ |
| 48 | |
| 49 | #define UARTDM_IMR 0xB0 /* Interrupt mask register */ |
| 50 | #define UARTDM_ISR 0xB4 /* Interrupt status register */ |
| 51 | #define UARTDM_ISR_TX_READY 0x80 /* TX FIFO empty */ |
| 52 | |
| 53 | #define UARTDM_TF 0x100 /* UART Transmit FIFO register */ |
| 54 | #define UARTDM_RF 0x140 /* UART Receive FIFO register */ |
| 55 | |
Ramon Fried | 2292ed3 | 2018-05-16 12:13:42 +0300 | [diff] [blame] | 56 | #define UART_DM_CLK_RX_TX_BIT_RATE 0xCC |
| 57 | #define MSM_BOOT_UART_DM_8_N_1_MODE 0x34 |
| 58 | #define MSM_BOOT_UART_DM_CMD_RESET_RX 0x10 |
| 59 | #define MSM_BOOT_UART_DM_CMD_RESET_TX 0x20 |
Mateusz Kulikowski | 8aac697 | 2016-03-31 23:12:14 +0200 | [diff] [blame] | 60 | |
| 61 | DECLARE_GLOBAL_DATA_PTR; |
| 62 | |
| 63 | struct msm_serial_data { |
| 64 | phys_addr_t base; |
| 65 | unsigned chars_cnt; /* number of buffered chars */ |
| 66 | uint32_t chars_buf; /* buffered chars */ |
Robert Marko | 03a3602 | 2020-07-06 10:37:55 +0200 | [diff] [blame] | 67 | uint32_t clk_bit_rate; /* data mover mode bit rate register value */ |
Mateusz Kulikowski | 8aac697 | 2016-03-31 23:12:14 +0200 | [diff] [blame] | 68 | }; |
| 69 | |
| 70 | static int msm_serial_fetch(struct udevice *dev) |
| 71 | { |
| 72 | struct msm_serial_data *priv = dev_get_priv(dev); |
| 73 | unsigned sr; |
| 74 | |
| 75 | if (priv->chars_cnt) |
| 76 | return priv->chars_cnt; |
| 77 | |
| 78 | /* Clear error in case of buffer overrun */ |
| 79 | if (readl(priv->base + UARTDM_SR) & UARTDM_SR_UART_OVERRUN) |
| 80 | writel(UARTDM_CR_CMD_RESET_ERR, priv->base + UARTDM_CR); |
| 81 | |
| 82 | /* We need to fetch new character */ |
| 83 | sr = readl(priv->base + UARTDM_SR); |
| 84 | |
| 85 | if (sr & UARTDM_SR_RX_READY) { |
| 86 | /* There are at least 4 bytes in fifo */ |
| 87 | priv->chars_buf = readl(priv->base + UARTDM_RF); |
| 88 | priv->chars_cnt = 4; |
| 89 | } else { |
| 90 | /* Check if there is anything in fifo */ |
| 91 | priv->chars_cnt = readl(priv->base + UARTDM_RXFS); |
| 92 | /* Extract number of characters in UART packing buffer*/ |
| 93 | priv->chars_cnt = (priv->chars_cnt >> |
| 94 | UARTDM_RXFS_BUF_SHIFT) & |
| 95 | UARTDM_RXFS_BUF_MASK; |
| 96 | if (!priv->chars_cnt) |
| 97 | return 0; |
| 98 | |
| 99 | /* There is at least one charcter, move it to fifo */ |
| 100 | writel(UARTDM_CR_CMD_FORCE_STALE, |
| 101 | priv->base + UARTDM_CR); |
| 102 | |
| 103 | priv->chars_buf = readl(priv->base + UARTDM_RF); |
| 104 | writel(UARTDM_CR_CMD_RESET_STALE_INT, |
| 105 | priv->base + UARTDM_CR); |
| 106 | writel(0x7, priv->base + UARTDM_DMRX); |
| 107 | } |
| 108 | |
| 109 | return priv->chars_cnt; |
| 110 | } |
| 111 | |
| 112 | static int msm_serial_getc(struct udevice *dev) |
| 113 | { |
| 114 | struct msm_serial_data *priv = dev_get_priv(dev); |
| 115 | char c; |
| 116 | |
| 117 | if (!msm_serial_fetch(dev)) |
| 118 | return -EAGAIN; |
| 119 | |
| 120 | c = priv->chars_buf & 0xFF; |
| 121 | priv->chars_buf >>= 8; |
| 122 | priv->chars_cnt--; |
| 123 | |
| 124 | return c; |
| 125 | } |
| 126 | |
| 127 | static int msm_serial_putc(struct udevice *dev, const char ch) |
| 128 | { |
| 129 | struct msm_serial_data *priv = dev_get_priv(dev); |
| 130 | |
| 131 | if (!(readl(priv->base + UARTDM_SR) & UARTDM_SR_TX_EMPTY) && |
| 132 | !(readl(priv->base + UARTDM_ISR) & UARTDM_ISR_TX_READY)) |
| 133 | return -EAGAIN; |
| 134 | |
| 135 | writel(UARTDM_CR_CMD_RESET_TX_READY, priv->base + UARTDM_CR); |
| 136 | |
| 137 | writel(1, priv->base + UARTDM_NCF_TX); |
| 138 | writel(ch, priv->base + UARTDM_TF); |
| 139 | |
| 140 | return 0; |
| 141 | } |
| 142 | |
| 143 | static int msm_serial_pending(struct udevice *dev, bool input) |
| 144 | { |
| 145 | if (input) { |
| 146 | if (msm_serial_fetch(dev)) |
| 147 | return 1; |
| 148 | } |
| 149 | |
| 150 | return 0; |
| 151 | } |
| 152 | |
| 153 | static const struct dm_serial_ops msm_serial_ops = { |
| 154 | .putc = msm_serial_putc, |
| 155 | .pending = msm_serial_pending, |
| 156 | .getc = msm_serial_getc, |
| 157 | }; |
| 158 | |
| 159 | static int msm_uart_clk_init(struct udevice *dev) |
| 160 | { |
Simon Glass | dd79d6e | 2017-01-17 16:52:55 -0700 | [diff] [blame] | 161 | uint clk_rate = fdtdec_get_uint(gd->fdt_blob, dev_of_offset(dev), |
Mateusz Kulikowski | 8aac697 | 2016-03-31 23:12:14 +0200 | [diff] [blame] | 162 | "clock-frequency", 115200); |
Stephen Warren | a962243 | 2016-06-17 09:44:00 -0600 | [diff] [blame] | 163 | struct clk clk; |
Mateusz Kulikowski | 8aac697 | 2016-03-31 23:12:14 +0200 | [diff] [blame] | 164 | int ret; |
| 165 | |
Caleb Connolly | 7a9c7b9 | 2024-02-26 17:26:12 +0000 | [diff] [blame] | 166 | ret = clk_get_by_name(dev, "core", &clk); |
| 167 | if (ret < 0) { |
| 168 | pr_warn("%s: Failed to get clock: %d\n", __func__, ret); |
Stephen Warren | a962243 | 2016-06-17 09:44:00 -0600 | [diff] [blame] | 169 | return ret; |
Caleb Connolly | 7a9c7b9 | 2024-02-26 17:26:12 +0000 | [diff] [blame] | 170 | } |
Stephen Warren | a962243 | 2016-06-17 09:44:00 -0600 | [diff] [blame] | 171 | |
| 172 | ret = clk_set_rate(&clk, clk_rate); |
Mateusz Kulikowski | 8aac697 | 2016-03-31 23:12:14 +0200 | [diff] [blame] | 173 | if (ret < 0) |
| 174 | return ret; |
| 175 | |
| 176 | return 0; |
| 177 | } |
| 178 | |
Ramon Fried | 2292ed3 | 2018-05-16 12:13:42 +0300 | [diff] [blame] | 179 | static void uart_dm_init(struct msm_serial_data *priv) |
| 180 | { |
Stephan Gerhold | 9a4569f | 2021-07-14 10:56:26 +0200 | [diff] [blame] | 181 | /* Delay initialization for a bit to let pins stabilize if necessary */ |
| 182 | mdelay(5); |
| 183 | |
Robert Marko | 03a3602 | 2020-07-06 10:37:55 +0200 | [diff] [blame] | 184 | writel(priv->clk_bit_rate, priv->base + UARTDM_CSR); |
Ramon Fried | 2292ed3 | 2018-05-16 12:13:42 +0300 | [diff] [blame] | 185 | writel(0x0, priv->base + UARTDM_MR1); |
| 186 | writel(MSM_BOOT_UART_DM_8_N_1_MODE, priv->base + UARTDM_MR2); |
| 187 | writel(MSM_BOOT_UART_DM_CMD_RESET_RX, priv->base + UARTDM_CR); |
| 188 | writel(MSM_BOOT_UART_DM_CMD_RESET_TX, priv->base + UARTDM_CR); |
Stephan Gerhold | 706d481 | 2021-06-28 10:40:09 +0200 | [diff] [blame] | 189 | |
| 190 | /* Make sure BAM/single character mode is disabled */ |
| 191 | writel(0x0, priv->base + UARTDM_DMEN); |
Ramon Fried | 2292ed3 | 2018-05-16 12:13:42 +0300 | [diff] [blame] | 192 | } |
Mateusz Kulikowski | 8aac697 | 2016-03-31 23:12:14 +0200 | [diff] [blame] | 193 | static int msm_serial_probe(struct udevice *dev) |
| 194 | { |
Ramon Fried | 5550dd9 | 2018-05-16 12:13:37 +0300 | [diff] [blame] | 195 | int ret; |
Mateusz Kulikowski | 8aac697 | 2016-03-31 23:12:14 +0200 | [diff] [blame] | 196 | struct msm_serial_data *priv = dev_get_priv(dev); |
| 197 | |
Ramon Fried | 1f0f4a1 | 2018-05-16 12:13:38 +0300 | [diff] [blame] | 198 | /* No need to reinitialize the UART after relocation */ |
| 199 | if (gd->flags & GD_FLG_RELOC) |
| 200 | return 0; |
| 201 | |
Ramon Fried | 5550dd9 | 2018-05-16 12:13:37 +0300 | [diff] [blame] | 202 | ret = msm_uart_clk_init(dev); |
| 203 | if (ret) |
| 204 | return ret; |
Mateusz Kulikowski | 8aac697 | 2016-03-31 23:12:14 +0200 | [diff] [blame] | 205 | |
Ramon Fried | 2292ed3 | 2018-05-16 12:13:42 +0300 | [diff] [blame] | 206 | uart_dm_init(priv); |
Mateusz Kulikowski | 8aac697 | 2016-03-31 23:12:14 +0200 | [diff] [blame] | 207 | |
| 208 | return 0; |
| 209 | } |
| 210 | |
Simon Glass | aad29ae | 2020-12-03 16:55:21 -0700 | [diff] [blame] | 211 | static int msm_serial_of_to_plat(struct udevice *dev) |
Mateusz Kulikowski | 8aac697 | 2016-03-31 23:12:14 +0200 | [diff] [blame] | 212 | { |
| 213 | struct msm_serial_data *priv = dev_get_priv(dev); |
| 214 | |
Masahiro Yamada | a89b4de | 2020-07-17 14:36:48 +0900 | [diff] [blame] | 215 | priv->base = dev_read_addr(dev); |
Mateusz Kulikowski | 8aac697 | 2016-03-31 23:12:14 +0200 | [diff] [blame] | 216 | if (priv->base == FDT_ADDR_T_NONE) |
| 217 | return -EINVAL; |
| 218 | |
Wolfgang Denk | 9d328a6 | 2021-09-27 17:42:38 +0200 | [diff] [blame] | 219 | priv->clk_bit_rate = fdtdec_get_int(gd->fdt_blob, dev_of_offset(dev), |
Robert Marko | 03a3602 | 2020-07-06 10:37:55 +0200 | [diff] [blame] | 220 | "bit-rate", UART_DM_CLK_RX_TX_BIT_RATE); |
| 221 | |
Mateusz Kulikowski | 8aac697 | 2016-03-31 23:12:14 +0200 | [diff] [blame] | 222 | return 0; |
| 223 | } |
| 224 | |
| 225 | static const struct udevice_id msm_serial_ids[] = { |
| 226 | { .compatible = "qcom,msm-uartdm-v1.4" }, |
| 227 | { } |
| 228 | }; |
| 229 | |
| 230 | U_BOOT_DRIVER(serial_msm) = { |
| 231 | .name = "serial_msm", |
| 232 | .id = UCLASS_SERIAL, |
| 233 | .of_match = msm_serial_ids, |
Simon Glass | aad29ae | 2020-12-03 16:55:21 -0700 | [diff] [blame] | 234 | .of_to_plat = msm_serial_of_to_plat, |
Simon Glass | 8a2b47f | 2020-12-03 16:55:17 -0700 | [diff] [blame] | 235 | .priv_auto = sizeof(struct msm_serial_data), |
Mateusz Kulikowski | 8aac697 | 2016-03-31 23:12:14 +0200 | [diff] [blame] | 236 | .probe = msm_serial_probe, |
| 237 | .ops = &msm_serial_ops, |
Caleb Connolly | 7a9c7b9 | 2024-02-26 17:26:12 +0000 | [diff] [blame] | 238 | .flags = DM_FLAG_PRE_RELOC, |
Mateusz Kulikowski | 8aac697 | 2016-03-31 23:12:14 +0200 | [diff] [blame] | 239 | }; |
Caleb Connolly | a86d23a | 2024-02-26 17:26:11 +0000 | [diff] [blame] | 240 | |
| 241 | #ifdef CONFIG_DEBUG_UART_MSM |
| 242 | |
| 243 | static struct msm_serial_data init_serial_data = { |
| 244 | .base = CONFIG_VAL(DEBUG_UART_BASE), |
| 245 | .clk_rate = 7372800, |
| 246 | }; |
| 247 | |
| 248 | #include <debug_uart.h> |
| 249 | |
| 250 | /* Uncomment to turn on UART clocks when debugging U-Boot as aboot on MSM8916 */ |
| 251 | //int apq8016_clk_init_uart(phys_addr_t gcc_base); |
| 252 | |
| 253 | static inline void _debug_uart_init(void) |
| 254 | { |
| 255 | /* Uncomment to turn on UART clocks when debugging U-Boot as aboot on MSM8916 */ |
| 256 | //apq8016_clk_init_uart(0x1800000); |
| 257 | uart_dm_init(&init_serial_data); |
| 258 | } |
| 259 | |
| 260 | static inline void _debug_uart_putc(int ch) |
| 261 | { |
| 262 | struct msm_serial_data *priv = &init_serial_data; |
| 263 | |
| 264 | while (!(readl(priv->base + UARTDM_SR) & UARTDM_SR_TX_EMPTY) && |
| 265 | !(readl(priv->base + UARTDM_ISR) & UARTDM_ISR_TX_READY)) |
| 266 | ; |
| 267 | |
| 268 | writel(UARTDM_CR_CMD_RESET_TX_READY, priv->base + UARTDM_CR); |
| 269 | |
| 270 | writel(1, priv->base + UARTDM_NCF_TX); |
| 271 | writel(ch, priv->base + UARTDM_TF); |
| 272 | } |
| 273 | |
| 274 | DEBUG_UART_FUNCS |
| 275 | |
| 276 | #endif |