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 |
Caleb Connolly | fb47e06 | 2024-04-15 16:03:40 +0100 | [diff] [blame^] | 35 | /* |
| 36 | * This is documented on page 1817 of the apq8016e technical reference manual. |
| 37 | * section 6.2.5.3.26 |
| 38 | * |
| 39 | * The upper nybble contains the bit clock divider for the RX pin, the lower |
| 40 | * nybble defines the TX pin. In almost all cases these should be the same value. |
| 41 | * |
| 42 | * The baud rate is the core clock frequency divided by the fixed divider value |
| 43 | * programmed into this register (defined in calc_csr_bitrate()). |
| 44 | */ |
Ramon Fried | 2292ed3 | 2018-05-16 12:13:42 +0300 | [diff] [blame] | 45 | #define UARTDM_CSR 0xA0 |
Mateusz Kulikowski | 8aac697 | 2016-03-31 23:12:14 +0200 | [diff] [blame] | 46 | |
| 47 | #define UARTDM_SR 0xA4 /* Status register */ |
| 48 | #define UARTDM_SR_RX_READY (1 << 0) /* Word is the receiver FIFO */ |
| 49 | #define UARTDM_SR_TX_EMPTY (1 << 3) /* Transmitter underrun */ |
| 50 | #define UARTDM_SR_UART_OVERRUN (1 << 4) /* Receive overrun */ |
| 51 | |
| 52 | #define UARTDM_CR 0xA8 /* Command register */ |
| 53 | #define UARTDM_CR_CMD_RESET_ERR (3 << 4) /* Clear overrun error */ |
| 54 | #define UARTDM_CR_CMD_RESET_STALE_INT (8 << 4) /* Clears stale irq */ |
| 55 | #define UARTDM_CR_CMD_RESET_TX_READY (3 << 8) /* Clears TX Ready irq*/ |
| 56 | #define UARTDM_CR_CMD_FORCE_STALE (4 << 8) /* Causes stale event */ |
| 57 | #define UARTDM_CR_CMD_STALE_EVENT_DISABLE (6 << 8) /* Disable stale event */ |
| 58 | |
| 59 | #define UARTDM_IMR 0xB0 /* Interrupt mask register */ |
| 60 | #define UARTDM_ISR 0xB4 /* Interrupt status register */ |
| 61 | #define UARTDM_ISR_TX_READY 0x80 /* TX FIFO empty */ |
| 62 | |
| 63 | #define UARTDM_TF 0x100 /* UART Transmit FIFO register */ |
| 64 | #define UARTDM_RF 0x140 /* UART Receive FIFO register */ |
| 65 | |
Ramon Fried | 2292ed3 | 2018-05-16 12:13:42 +0300 | [diff] [blame] | 66 | #define MSM_BOOT_UART_DM_8_N_1_MODE 0x34 |
| 67 | #define MSM_BOOT_UART_DM_CMD_RESET_RX 0x10 |
| 68 | #define MSM_BOOT_UART_DM_CMD_RESET_TX 0x20 |
Mateusz Kulikowski | 8aac697 | 2016-03-31 23:12:14 +0200 | [diff] [blame] | 69 | |
| 70 | DECLARE_GLOBAL_DATA_PTR; |
| 71 | |
| 72 | struct msm_serial_data { |
| 73 | phys_addr_t base; |
| 74 | unsigned chars_cnt; /* number of buffered chars */ |
| 75 | uint32_t chars_buf; /* buffered chars */ |
Caleb Connolly | fb47e06 | 2024-04-15 16:03:40 +0100 | [diff] [blame^] | 76 | uint32_t clk_rate; /* core clock rate */ |
Mateusz Kulikowski | 8aac697 | 2016-03-31 23:12:14 +0200 | [diff] [blame] | 77 | }; |
| 78 | |
| 79 | static int msm_serial_fetch(struct udevice *dev) |
| 80 | { |
| 81 | struct msm_serial_data *priv = dev_get_priv(dev); |
| 82 | unsigned sr; |
| 83 | |
| 84 | if (priv->chars_cnt) |
| 85 | return priv->chars_cnt; |
| 86 | |
| 87 | /* Clear error in case of buffer overrun */ |
| 88 | if (readl(priv->base + UARTDM_SR) & UARTDM_SR_UART_OVERRUN) |
| 89 | writel(UARTDM_CR_CMD_RESET_ERR, priv->base + UARTDM_CR); |
| 90 | |
| 91 | /* We need to fetch new character */ |
| 92 | sr = readl(priv->base + UARTDM_SR); |
| 93 | |
| 94 | if (sr & UARTDM_SR_RX_READY) { |
| 95 | /* There are at least 4 bytes in fifo */ |
| 96 | priv->chars_buf = readl(priv->base + UARTDM_RF); |
| 97 | priv->chars_cnt = 4; |
| 98 | } else { |
| 99 | /* Check if there is anything in fifo */ |
| 100 | priv->chars_cnt = readl(priv->base + UARTDM_RXFS); |
| 101 | /* Extract number of characters in UART packing buffer*/ |
| 102 | priv->chars_cnt = (priv->chars_cnt >> |
| 103 | UARTDM_RXFS_BUF_SHIFT) & |
| 104 | UARTDM_RXFS_BUF_MASK; |
| 105 | if (!priv->chars_cnt) |
| 106 | return 0; |
| 107 | |
| 108 | /* There is at least one charcter, move it to fifo */ |
| 109 | writel(UARTDM_CR_CMD_FORCE_STALE, |
| 110 | priv->base + UARTDM_CR); |
| 111 | |
| 112 | priv->chars_buf = readl(priv->base + UARTDM_RF); |
| 113 | writel(UARTDM_CR_CMD_RESET_STALE_INT, |
| 114 | priv->base + UARTDM_CR); |
| 115 | writel(0x7, priv->base + UARTDM_DMRX); |
| 116 | } |
| 117 | |
| 118 | return priv->chars_cnt; |
| 119 | } |
| 120 | |
| 121 | static int msm_serial_getc(struct udevice *dev) |
| 122 | { |
| 123 | struct msm_serial_data *priv = dev_get_priv(dev); |
| 124 | char c; |
| 125 | |
| 126 | if (!msm_serial_fetch(dev)) |
| 127 | return -EAGAIN; |
| 128 | |
| 129 | c = priv->chars_buf & 0xFF; |
| 130 | priv->chars_buf >>= 8; |
| 131 | priv->chars_cnt--; |
| 132 | |
| 133 | return c; |
| 134 | } |
| 135 | |
| 136 | static int msm_serial_putc(struct udevice *dev, const char ch) |
| 137 | { |
| 138 | struct msm_serial_data *priv = dev_get_priv(dev); |
| 139 | |
| 140 | if (!(readl(priv->base + UARTDM_SR) & UARTDM_SR_TX_EMPTY) && |
| 141 | !(readl(priv->base + UARTDM_ISR) & UARTDM_ISR_TX_READY)) |
| 142 | return -EAGAIN; |
| 143 | |
| 144 | writel(UARTDM_CR_CMD_RESET_TX_READY, priv->base + UARTDM_CR); |
| 145 | |
| 146 | writel(1, priv->base + UARTDM_NCF_TX); |
| 147 | writel(ch, priv->base + UARTDM_TF); |
| 148 | |
| 149 | return 0; |
| 150 | } |
| 151 | |
| 152 | static int msm_serial_pending(struct udevice *dev, bool input) |
| 153 | { |
| 154 | if (input) { |
| 155 | if (msm_serial_fetch(dev)) |
| 156 | return 1; |
| 157 | } |
| 158 | |
| 159 | return 0; |
| 160 | } |
| 161 | |
| 162 | static const struct dm_serial_ops msm_serial_ops = { |
| 163 | .putc = msm_serial_putc, |
| 164 | .pending = msm_serial_pending, |
| 165 | .getc = msm_serial_getc, |
| 166 | }; |
| 167 | |
Caleb Connolly | fb47e06 | 2024-04-15 16:03:40 +0100 | [diff] [blame^] | 168 | static long msm_uart_clk_init(struct udevice *dev) |
Mateusz Kulikowski | 8aac697 | 2016-03-31 23:12:14 +0200 | [diff] [blame] | 169 | { |
Caleb Connolly | fb47e06 | 2024-04-15 16:03:40 +0100 | [diff] [blame^] | 170 | struct msm_serial_data *priv = dev_get_priv(dev); |
Stephen Warren | a962243 | 2016-06-17 09:44:00 -0600 | [diff] [blame] | 171 | struct clk clk; |
Mateusz Kulikowski | 8aac697 | 2016-03-31 23:12:14 +0200 | [diff] [blame] | 172 | int ret; |
Caleb Connolly | fb47e06 | 2024-04-15 16:03:40 +0100 | [diff] [blame^] | 173 | long rate; |
Mateusz Kulikowski | 8aac697 | 2016-03-31 23:12:14 +0200 | [diff] [blame] | 174 | |
Caleb Connolly | 7a9c7b9 | 2024-02-26 17:26:12 +0000 | [diff] [blame] | 175 | ret = clk_get_by_name(dev, "core", &clk); |
| 176 | if (ret < 0) { |
| 177 | pr_warn("%s: Failed to get clock: %d\n", __func__, ret); |
Caleb Connolly | fb47e06 | 2024-04-15 16:03:40 +0100 | [diff] [blame^] | 178 | return 0; |
Caleb Connolly | 7a9c7b9 | 2024-02-26 17:26:12 +0000 | [diff] [blame] | 179 | } |
Stephen Warren | a962243 | 2016-06-17 09:44:00 -0600 | [diff] [blame] | 180 | |
Caleb Connolly | fb47e06 | 2024-04-15 16:03:40 +0100 | [diff] [blame^] | 181 | rate = clk_set_rate(&clk, priv->clk_rate); |
Mateusz Kulikowski | 8aac697 | 2016-03-31 23:12:14 +0200 | [diff] [blame] | 182 | |
Caleb Connolly | fb47e06 | 2024-04-15 16:03:40 +0100 | [diff] [blame^] | 183 | return rate; |
| 184 | } |
| 185 | |
| 186 | static int calc_csr_bitrate(struct msm_serial_data *priv) |
| 187 | { |
| 188 | /* This table is from the TRE. See the definition of UARTDM_CSR */ |
| 189 | unsigned int csr_div_table[] = {24576, 12288, 6144, 3072, 1536, 768, 512, 384, |
| 190 | 256, 192, 128, 96, 64, 48, 32, 16}; |
| 191 | int i = ARRAY_SIZE(csr_div_table) - 1; |
| 192 | /* Currently we only support one baudrate */ |
| 193 | int baud = 115200; |
| 194 | |
| 195 | for (; i >= 0; i--) { |
| 196 | int x = priv->clk_rate / csr_div_table[i]; |
| 197 | |
| 198 | if (x == baud) |
| 199 | /* Duplicate the configuration for RX |
| 200 | * as the lower nybble only configures TX |
| 201 | */ |
| 202 | return i + (i << 4); |
| 203 | } |
| 204 | |
| 205 | return -EINVAL; |
Mateusz Kulikowski | 8aac697 | 2016-03-31 23:12:14 +0200 | [diff] [blame] | 206 | } |
| 207 | |
Ramon Fried | 2292ed3 | 2018-05-16 12:13:42 +0300 | [diff] [blame] | 208 | static void uart_dm_init(struct msm_serial_data *priv) |
| 209 | { |
Stephan Gerhold | 9a4569f | 2021-07-14 10:56:26 +0200 | [diff] [blame] | 210 | /* Delay initialization for a bit to let pins stabilize if necessary */ |
| 211 | mdelay(5); |
Caleb Connolly | fb47e06 | 2024-04-15 16:03:40 +0100 | [diff] [blame^] | 212 | int bitrate = calc_csr_bitrate(priv); |
| 213 | if (bitrate < 0) { |
| 214 | log_warning("Couldn't calculate bit clock divider! Using default\n"); |
| 215 | /* This happens to be the value used on MSM8916 for the hardcoded clockrate |
| 216 | * in clock-apq8016. It's at least a better guess than a value we *know* |
| 217 | * is wrong... |
| 218 | */ |
| 219 | bitrate = 0xCC; |
| 220 | } |
Stephan Gerhold | 9a4569f | 2021-07-14 10:56:26 +0200 | [diff] [blame] | 221 | |
Caleb Connolly | fb47e06 | 2024-04-15 16:03:40 +0100 | [diff] [blame^] | 222 | writel(bitrate, priv->base + UARTDM_CSR); |
Ramon Fried | 2292ed3 | 2018-05-16 12:13:42 +0300 | [diff] [blame] | 223 | writel(0x0, priv->base + UARTDM_MR1); |
| 224 | writel(MSM_BOOT_UART_DM_8_N_1_MODE, priv->base + UARTDM_MR2); |
| 225 | writel(MSM_BOOT_UART_DM_CMD_RESET_RX, priv->base + UARTDM_CR); |
| 226 | writel(MSM_BOOT_UART_DM_CMD_RESET_TX, priv->base + UARTDM_CR); |
Stephan Gerhold | 706d481 | 2021-06-28 10:40:09 +0200 | [diff] [blame] | 227 | |
| 228 | /* Make sure BAM/single character mode is disabled */ |
| 229 | writel(0x0, priv->base + UARTDM_DMEN); |
Ramon Fried | 2292ed3 | 2018-05-16 12:13:42 +0300 | [diff] [blame] | 230 | } |
Mateusz Kulikowski | 8aac697 | 2016-03-31 23:12:14 +0200 | [diff] [blame] | 231 | static int msm_serial_probe(struct udevice *dev) |
| 232 | { |
| 233 | struct msm_serial_data *priv = dev_get_priv(dev); |
Caleb Connolly | fb47e06 | 2024-04-15 16:03:40 +0100 | [diff] [blame^] | 234 | long rate; |
Mateusz Kulikowski | 8aac697 | 2016-03-31 23:12:14 +0200 | [diff] [blame] | 235 | |
Ramon Fried | 1f0f4a1 | 2018-05-16 12:13:38 +0300 | [diff] [blame] | 236 | /* No need to reinitialize the UART after relocation */ |
| 237 | if (gd->flags & GD_FLG_RELOC) |
| 238 | return 0; |
| 239 | |
Caleb Connolly | fb47e06 | 2024-04-15 16:03:40 +0100 | [diff] [blame^] | 240 | rate = msm_uart_clk_init(dev); |
| 241 | if (rate < 0) |
| 242 | return rate; |
| 243 | if (!rate) { |
| 244 | log_err("Got core clock rate of 0... Please fix your clock driver\n"); |
| 245 | return -EINVAL; |
| 246 | } |
| 247 | |
| 248 | /* Update the clock rate to the actual programmed rate returned by the |
| 249 | * clock driver |
| 250 | */ |
| 251 | priv->clk_rate = rate; |
Mateusz Kulikowski | 8aac697 | 2016-03-31 23:12:14 +0200 | [diff] [blame] | 252 | |
Ramon Fried | 2292ed3 | 2018-05-16 12:13:42 +0300 | [diff] [blame] | 253 | uart_dm_init(priv); |
Mateusz Kulikowski | 8aac697 | 2016-03-31 23:12:14 +0200 | [diff] [blame] | 254 | |
| 255 | return 0; |
| 256 | } |
| 257 | |
Simon Glass | aad29ae | 2020-12-03 16:55:21 -0700 | [diff] [blame] | 258 | static int msm_serial_of_to_plat(struct udevice *dev) |
Mateusz Kulikowski | 8aac697 | 2016-03-31 23:12:14 +0200 | [diff] [blame] | 259 | { |
| 260 | struct msm_serial_data *priv = dev_get_priv(dev); |
Caleb Connolly | fb47e06 | 2024-04-15 16:03:40 +0100 | [diff] [blame^] | 261 | int ret; |
Mateusz Kulikowski | 8aac697 | 2016-03-31 23:12:14 +0200 | [diff] [blame] | 262 | |
Masahiro Yamada | a89b4de | 2020-07-17 14:36:48 +0900 | [diff] [blame] | 263 | priv->base = dev_read_addr(dev); |
Mateusz Kulikowski | 8aac697 | 2016-03-31 23:12:14 +0200 | [diff] [blame] | 264 | if (priv->base == FDT_ADDR_T_NONE) |
| 265 | return -EINVAL; |
| 266 | |
Caleb Connolly | fb47e06 | 2024-04-15 16:03:40 +0100 | [diff] [blame^] | 267 | ret = dev_read_u32(dev, "clock-frequency", &priv->clk_rate); |
| 268 | if (ret < 0) { |
| 269 | log_debug("No clock frequency specified, using default rate\n"); |
| 270 | /* Default for APQ8016 */ |
| 271 | priv->clk_rate = 7372800; |
| 272 | } |
Robert Marko | 03a3602 | 2020-07-06 10:37:55 +0200 | [diff] [blame] | 273 | |
Mateusz Kulikowski | 8aac697 | 2016-03-31 23:12:14 +0200 | [diff] [blame] | 274 | return 0; |
| 275 | } |
| 276 | |
| 277 | static const struct udevice_id msm_serial_ids[] = { |
| 278 | { .compatible = "qcom,msm-uartdm-v1.4" }, |
| 279 | { } |
| 280 | }; |
| 281 | |
| 282 | U_BOOT_DRIVER(serial_msm) = { |
| 283 | .name = "serial_msm", |
| 284 | .id = UCLASS_SERIAL, |
| 285 | .of_match = msm_serial_ids, |
Simon Glass | aad29ae | 2020-12-03 16:55:21 -0700 | [diff] [blame] | 286 | .of_to_plat = msm_serial_of_to_plat, |
Simon Glass | 8a2b47f | 2020-12-03 16:55:17 -0700 | [diff] [blame] | 287 | .priv_auto = sizeof(struct msm_serial_data), |
Mateusz Kulikowski | 8aac697 | 2016-03-31 23:12:14 +0200 | [diff] [blame] | 288 | .probe = msm_serial_probe, |
| 289 | .ops = &msm_serial_ops, |
Caleb Connolly | 7a9c7b9 | 2024-02-26 17:26:12 +0000 | [diff] [blame] | 290 | .flags = DM_FLAG_PRE_RELOC, |
Mateusz Kulikowski | 8aac697 | 2016-03-31 23:12:14 +0200 | [diff] [blame] | 291 | }; |
Caleb Connolly | a86d23a | 2024-02-26 17:26:11 +0000 | [diff] [blame] | 292 | |
| 293 | #ifdef CONFIG_DEBUG_UART_MSM |
| 294 | |
| 295 | static struct msm_serial_data init_serial_data = { |
| 296 | .base = CONFIG_VAL(DEBUG_UART_BASE), |
Caleb Connolly | fb47e06 | 2024-04-15 16:03:40 +0100 | [diff] [blame^] | 297 | .clk_rate = CONFIG_VAL(DEBUG_UART_CLOCK), |
Caleb Connolly | a86d23a | 2024-02-26 17:26:11 +0000 | [diff] [blame] | 298 | }; |
| 299 | |
| 300 | #include <debug_uart.h> |
| 301 | |
| 302 | /* Uncomment to turn on UART clocks when debugging U-Boot as aboot on MSM8916 */ |
| 303 | //int apq8016_clk_init_uart(phys_addr_t gcc_base); |
| 304 | |
| 305 | static inline void _debug_uart_init(void) |
| 306 | { |
| 307 | /* Uncomment to turn on UART clocks when debugging U-Boot as aboot on MSM8916 */ |
| 308 | //apq8016_clk_init_uart(0x1800000); |
| 309 | uart_dm_init(&init_serial_data); |
| 310 | } |
| 311 | |
| 312 | static inline void _debug_uart_putc(int ch) |
| 313 | { |
| 314 | struct msm_serial_data *priv = &init_serial_data; |
| 315 | |
| 316 | while (!(readl(priv->base + UARTDM_SR) & UARTDM_SR_TX_EMPTY) && |
| 317 | !(readl(priv->base + UARTDM_ISR) & UARTDM_ISR_TX_READY)) |
| 318 | ; |
| 319 | |
| 320 | writel(UARTDM_CR_CMD_RESET_TX_READY, priv->base + UARTDM_CR); |
| 321 | |
| 322 | writel(1, priv->base + UARTDM_NCF_TX); |
| 323 | writel(ch, priv->base + UARTDM_TF); |
| 324 | } |
| 325 | |
| 326 | DEBUG_UART_FUNCS |
| 327 | |
| 328 | #endif |