Tom Rini | 83d290c | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Mateusz Kulikowski | 142a20c | 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 | 142a20c | 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 | 336d461 | 2020-02-03 07:36:16 -0700 | [diff] [blame] | 15 | #include <malloc.h> |
Mateusz Kulikowski | 142a20c | 2016-03-31 23:12:14 +0200 | [diff] [blame] | 16 | #include <serial.h> |
| 17 | #include <watchdog.h> |
| 18 | #include <asm/io.h> |
| 19 | #include <linux/compiler.h> |
Ramon Fried | b460b88 | 2018-05-16 12:13:42 +0300 | [diff] [blame] | 20 | #include <dm/pinctrl.h> |
Mateusz Kulikowski | 142a20c | 2016-03-31 23:12:14 +0200 | [diff] [blame] | 21 | |
| 22 | /* Serial registers - this driver works in uartdm mode*/ |
| 23 | |
| 24 | #define UARTDM_DMRX 0x34 /* Max RX transfer length */ |
| 25 | #define UARTDM_NCF_TX 0x40 /* Number of chars to TX */ |
| 26 | |
| 27 | #define UARTDM_RXFS 0x50 /* RX channel status register */ |
| 28 | #define UARTDM_RXFS_BUF_SHIFT 0x7 /* Number of bytes in the packing buffer */ |
| 29 | #define UARTDM_RXFS_BUF_MASK 0x7 |
Ramon Fried | b460b88 | 2018-05-16 12:13:42 +0300 | [diff] [blame] | 30 | #define UARTDM_MR1 0x00 |
| 31 | #define UARTDM_MR2 0x04 |
| 32 | #define UARTDM_CSR 0xA0 |
Mateusz Kulikowski | 142a20c | 2016-03-31 23:12:14 +0200 | [diff] [blame] | 33 | |
| 34 | #define UARTDM_SR 0xA4 /* Status register */ |
| 35 | #define UARTDM_SR_RX_READY (1 << 0) /* Word is the receiver FIFO */ |
| 36 | #define UARTDM_SR_TX_EMPTY (1 << 3) /* Transmitter underrun */ |
| 37 | #define UARTDM_SR_UART_OVERRUN (1 << 4) /* Receive overrun */ |
| 38 | |
| 39 | #define UARTDM_CR 0xA8 /* Command register */ |
| 40 | #define UARTDM_CR_CMD_RESET_ERR (3 << 4) /* Clear overrun error */ |
| 41 | #define UARTDM_CR_CMD_RESET_STALE_INT (8 << 4) /* Clears stale irq */ |
| 42 | #define UARTDM_CR_CMD_RESET_TX_READY (3 << 8) /* Clears TX Ready irq*/ |
| 43 | #define UARTDM_CR_CMD_FORCE_STALE (4 << 8) /* Causes stale event */ |
| 44 | #define UARTDM_CR_CMD_STALE_EVENT_DISABLE (6 << 8) /* Disable stale event */ |
| 45 | |
| 46 | #define UARTDM_IMR 0xB0 /* Interrupt mask register */ |
| 47 | #define UARTDM_ISR 0xB4 /* Interrupt status register */ |
| 48 | #define UARTDM_ISR_TX_READY 0x80 /* TX FIFO empty */ |
| 49 | |
| 50 | #define UARTDM_TF 0x100 /* UART Transmit FIFO register */ |
| 51 | #define UARTDM_RF 0x140 /* UART Receive FIFO register */ |
| 52 | |
Ramon Fried | b460b88 | 2018-05-16 12:13:42 +0300 | [diff] [blame] | 53 | #define UART_DM_CLK_RX_TX_BIT_RATE 0xCC |
| 54 | #define MSM_BOOT_UART_DM_8_N_1_MODE 0x34 |
| 55 | #define MSM_BOOT_UART_DM_CMD_RESET_RX 0x10 |
| 56 | #define MSM_BOOT_UART_DM_CMD_RESET_TX 0x20 |
Mateusz Kulikowski | 142a20c | 2016-03-31 23:12:14 +0200 | [diff] [blame] | 57 | |
| 58 | DECLARE_GLOBAL_DATA_PTR; |
| 59 | |
| 60 | struct msm_serial_data { |
| 61 | phys_addr_t base; |
| 62 | unsigned chars_cnt; /* number of buffered chars */ |
| 63 | uint32_t chars_buf; /* buffered chars */ |
| 64 | }; |
| 65 | |
| 66 | static int msm_serial_fetch(struct udevice *dev) |
| 67 | { |
| 68 | struct msm_serial_data *priv = dev_get_priv(dev); |
| 69 | unsigned sr; |
| 70 | |
| 71 | if (priv->chars_cnt) |
| 72 | return priv->chars_cnt; |
| 73 | |
| 74 | /* Clear error in case of buffer overrun */ |
| 75 | if (readl(priv->base + UARTDM_SR) & UARTDM_SR_UART_OVERRUN) |
| 76 | writel(UARTDM_CR_CMD_RESET_ERR, priv->base + UARTDM_CR); |
| 77 | |
| 78 | /* We need to fetch new character */ |
| 79 | sr = readl(priv->base + UARTDM_SR); |
| 80 | |
| 81 | if (sr & UARTDM_SR_RX_READY) { |
| 82 | /* There are at least 4 bytes in fifo */ |
| 83 | priv->chars_buf = readl(priv->base + UARTDM_RF); |
| 84 | priv->chars_cnt = 4; |
| 85 | } else { |
| 86 | /* Check if there is anything in fifo */ |
| 87 | priv->chars_cnt = readl(priv->base + UARTDM_RXFS); |
| 88 | /* Extract number of characters in UART packing buffer*/ |
| 89 | priv->chars_cnt = (priv->chars_cnt >> |
| 90 | UARTDM_RXFS_BUF_SHIFT) & |
| 91 | UARTDM_RXFS_BUF_MASK; |
| 92 | if (!priv->chars_cnt) |
| 93 | return 0; |
| 94 | |
| 95 | /* There is at least one charcter, move it to fifo */ |
| 96 | writel(UARTDM_CR_CMD_FORCE_STALE, |
| 97 | priv->base + UARTDM_CR); |
| 98 | |
| 99 | priv->chars_buf = readl(priv->base + UARTDM_RF); |
| 100 | writel(UARTDM_CR_CMD_RESET_STALE_INT, |
| 101 | priv->base + UARTDM_CR); |
| 102 | writel(0x7, priv->base + UARTDM_DMRX); |
| 103 | } |
| 104 | |
| 105 | return priv->chars_cnt; |
| 106 | } |
| 107 | |
| 108 | static int msm_serial_getc(struct udevice *dev) |
| 109 | { |
| 110 | struct msm_serial_data *priv = dev_get_priv(dev); |
| 111 | char c; |
| 112 | |
| 113 | if (!msm_serial_fetch(dev)) |
| 114 | return -EAGAIN; |
| 115 | |
| 116 | c = priv->chars_buf & 0xFF; |
| 117 | priv->chars_buf >>= 8; |
| 118 | priv->chars_cnt--; |
| 119 | |
| 120 | return c; |
| 121 | } |
| 122 | |
| 123 | static int msm_serial_putc(struct udevice *dev, const char ch) |
| 124 | { |
| 125 | struct msm_serial_data *priv = dev_get_priv(dev); |
| 126 | |
| 127 | if (!(readl(priv->base + UARTDM_SR) & UARTDM_SR_TX_EMPTY) && |
| 128 | !(readl(priv->base + UARTDM_ISR) & UARTDM_ISR_TX_READY)) |
| 129 | return -EAGAIN; |
| 130 | |
| 131 | writel(UARTDM_CR_CMD_RESET_TX_READY, priv->base + UARTDM_CR); |
| 132 | |
| 133 | writel(1, priv->base + UARTDM_NCF_TX); |
| 134 | writel(ch, priv->base + UARTDM_TF); |
| 135 | |
| 136 | return 0; |
| 137 | } |
| 138 | |
| 139 | static int msm_serial_pending(struct udevice *dev, bool input) |
| 140 | { |
| 141 | if (input) { |
| 142 | if (msm_serial_fetch(dev)) |
| 143 | return 1; |
| 144 | } |
| 145 | |
| 146 | return 0; |
| 147 | } |
| 148 | |
| 149 | static const struct dm_serial_ops msm_serial_ops = { |
| 150 | .putc = msm_serial_putc, |
| 151 | .pending = msm_serial_pending, |
| 152 | .getc = msm_serial_getc, |
| 153 | }; |
| 154 | |
| 155 | static int msm_uart_clk_init(struct udevice *dev) |
| 156 | { |
Simon Glass | e160f7d | 2017-01-17 16:52:55 -0700 | [diff] [blame] | 157 | uint clk_rate = fdtdec_get_uint(gd->fdt_blob, dev_of_offset(dev), |
Mateusz Kulikowski | 142a20c | 2016-03-31 23:12:14 +0200 | [diff] [blame] | 158 | "clock-frequency", 115200); |
| 159 | uint clkd[2]; /* clk_id and clk_no */ |
| 160 | int clk_offset; |
Stephen Warren | 135aa95 | 2016-06-17 09:44:00 -0600 | [diff] [blame] | 161 | struct udevice *clk_dev; |
| 162 | struct clk clk; |
Mateusz Kulikowski | 142a20c | 2016-03-31 23:12:14 +0200 | [diff] [blame] | 163 | int ret; |
| 164 | |
Simon Glass | e160f7d | 2017-01-17 16:52:55 -0700 | [diff] [blame] | 165 | ret = fdtdec_get_int_array(gd->fdt_blob, dev_of_offset(dev), "clock", |
| 166 | clkd, 2); |
Mateusz Kulikowski | 142a20c | 2016-03-31 23:12:14 +0200 | [diff] [blame] | 167 | if (ret) |
| 168 | return ret; |
| 169 | |
| 170 | clk_offset = fdt_node_offset_by_phandle(gd->fdt_blob, clkd[0]); |
| 171 | if (clk_offset < 0) |
| 172 | return clk_offset; |
| 173 | |
Stephen Warren | 135aa95 | 2016-06-17 09:44:00 -0600 | [diff] [blame] | 174 | ret = uclass_get_device_by_of_offset(UCLASS_CLK, clk_offset, &clk_dev); |
Mateusz Kulikowski | 142a20c | 2016-03-31 23:12:14 +0200 | [diff] [blame] | 175 | if (ret) |
| 176 | return ret; |
| 177 | |
Stephen Warren | 135aa95 | 2016-06-17 09:44:00 -0600 | [diff] [blame] | 178 | clk.id = clkd[1]; |
| 179 | ret = clk_request(clk_dev, &clk); |
| 180 | if (ret < 0) |
| 181 | return ret; |
| 182 | |
| 183 | ret = clk_set_rate(&clk, clk_rate); |
| 184 | clk_free(&clk); |
Mateusz Kulikowski | 142a20c | 2016-03-31 23:12:14 +0200 | [diff] [blame] | 185 | if (ret < 0) |
| 186 | return ret; |
| 187 | |
| 188 | return 0; |
| 189 | } |
| 190 | |
Ramon Fried | b460b88 | 2018-05-16 12:13:42 +0300 | [diff] [blame] | 191 | static void uart_dm_init(struct msm_serial_data *priv) |
| 192 | { |
| 193 | writel(UART_DM_CLK_RX_TX_BIT_RATE, priv->base + UARTDM_CSR); |
| 194 | writel(0x0, priv->base + UARTDM_MR1); |
| 195 | writel(MSM_BOOT_UART_DM_8_N_1_MODE, priv->base + UARTDM_MR2); |
| 196 | writel(MSM_BOOT_UART_DM_CMD_RESET_RX, priv->base + UARTDM_CR); |
| 197 | writel(MSM_BOOT_UART_DM_CMD_RESET_TX, priv->base + UARTDM_CR); |
| 198 | } |
Mateusz Kulikowski | 142a20c | 2016-03-31 23:12:14 +0200 | [diff] [blame] | 199 | static int msm_serial_probe(struct udevice *dev) |
| 200 | { |
Ramon Fried | 11d59fe | 2018-05-16 12:13:37 +0300 | [diff] [blame] | 201 | int ret; |
Mateusz Kulikowski | 142a20c | 2016-03-31 23:12:14 +0200 | [diff] [blame] | 202 | struct msm_serial_data *priv = dev_get_priv(dev); |
| 203 | |
Ramon Fried | 7e5ad79 | 2018-05-16 12:13:38 +0300 | [diff] [blame] | 204 | /* No need to reinitialize the UART after relocation */ |
| 205 | if (gd->flags & GD_FLG_RELOC) |
| 206 | return 0; |
| 207 | |
Ramon Fried | 11d59fe | 2018-05-16 12:13:37 +0300 | [diff] [blame] | 208 | ret = msm_uart_clk_init(dev); |
| 209 | if (ret) |
| 210 | return ret; |
Mateusz Kulikowski | 142a20c | 2016-03-31 23:12:14 +0200 | [diff] [blame] | 211 | |
Ramon Fried | b460b88 | 2018-05-16 12:13:42 +0300 | [diff] [blame] | 212 | pinctrl_select_state(dev, "uart"); |
| 213 | uart_dm_init(priv); |
Mateusz Kulikowski | 142a20c | 2016-03-31 23:12:14 +0200 | [diff] [blame] | 214 | |
| 215 | return 0; |
| 216 | } |
| 217 | |
| 218 | static int msm_serial_ofdata_to_platdata(struct udevice *dev) |
| 219 | { |
| 220 | struct msm_serial_data *priv = dev_get_priv(dev); |
| 221 | |
Masahiro Yamada | 2548493 | 2020-07-17 14:36:48 +0900 | [diff] [blame^] | 222 | priv->base = dev_read_addr(dev); |
Mateusz Kulikowski | 142a20c | 2016-03-31 23:12:14 +0200 | [diff] [blame] | 223 | if (priv->base == FDT_ADDR_T_NONE) |
| 224 | return -EINVAL; |
| 225 | |
| 226 | return 0; |
| 227 | } |
| 228 | |
| 229 | static const struct udevice_id msm_serial_ids[] = { |
| 230 | { .compatible = "qcom,msm-uartdm-v1.4" }, |
| 231 | { } |
| 232 | }; |
| 233 | |
| 234 | U_BOOT_DRIVER(serial_msm) = { |
| 235 | .name = "serial_msm", |
| 236 | .id = UCLASS_SERIAL, |
| 237 | .of_match = msm_serial_ids, |
| 238 | .ofdata_to_platdata = msm_serial_ofdata_to_platdata, |
| 239 | .priv_auto_alloc_size = sizeof(struct msm_serial_data), |
| 240 | .probe = msm_serial_probe, |
| 241 | .ops = &msm_serial_ops, |
| 242 | }; |