blob: 0cc1aadce4a5b677b5137995059850cb8f4984fc [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Mateusz Kulikowski8aac6972016-03-31 23:12:14 +02002/*
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 Kulikowski8aac6972016-03-31 23:12:14 +02009 */
10
11#include <common.h>
12#include <clk.h>
13#include <dm.h>
14#include <errno.h>
Simon Glass9bc15642020-02-03 07:36:16 -070015#include <malloc.h>
Mateusz Kulikowski8aac6972016-03-31 23:12:14 +020016#include <serial.h>
17#include <watchdog.h>
18#include <asm/io.h>
19#include <linux/compiler.h>
Ramon Fried2292ed32018-05-16 12:13:42 +030020#include <dm/pinctrl.h>
Mateusz Kulikowski8aac6972016-03-31 23:12:14 +020021
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 Fried2292ed32018-05-16 12:13:42 +030030#define UARTDM_MR1 0x00
31#define UARTDM_MR2 0x04
32#define UARTDM_CSR 0xA0
Mateusz Kulikowski8aac6972016-03-31 23:12:14 +020033
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 Fried2292ed32018-05-16 12:13:42 +030053#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 Kulikowski8aac6972016-03-31 23:12:14 +020057
58DECLARE_GLOBAL_DATA_PTR;
59
60struct 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
66static 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
108static 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
123static 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
139static 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
149static 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
155static int msm_uart_clk_init(struct udevice *dev)
156{
Simon Glassdd79d6e2017-01-17 16:52:55 -0700157 uint clk_rate = fdtdec_get_uint(gd->fdt_blob, dev_of_offset(dev),
Mateusz Kulikowski8aac6972016-03-31 23:12:14 +0200158 "clock-frequency", 115200);
159 uint clkd[2]; /* clk_id and clk_no */
160 int clk_offset;
Stephen Warrena9622432016-06-17 09:44:00 -0600161 struct udevice *clk_dev;
162 struct clk clk;
Mateusz Kulikowski8aac6972016-03-31 23:12:14 +0200163 int ret;
164
Simon Glassdd79d6e2017-01-17 16:52:55 -0700165 ret = fdtdec_get_int_array(gd->fdt_blob, dev_of_offset(dev), "clock",
166 clkd, 2);
Mateusz Kulikowski8aac6972016-03-31 23:12:14 +0200167 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 Warrena9622432016-06-17 09:44:00 -0600174 ret = uclass_get_device_by_of_offset(UCLASS_CLK, clk_offset, &clk_dev);
Mateusz Kulikowski8aac6972016-03-31 23:12:14 +0200175 if (ret)
176 return ret;
177
Stephen Warrena9622432016-06-17 09:44:00 -0600178 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 Kulikowski8aac6972016-03-31 23:12:14 +0200185 if (ret < 0)
186 return ret;
187
188 return 0;
189}
190
Ramon Fried2292ed32018-05-16 12:13:42 +0300191static 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 Kulikowski8aac6972016-03-31 23:12:14 +0200199static int msm_serial_probe(struct udevice *dev)
200{
Ramon Fried5550dd92018-05-16 12:13:37 +0300201 int ret;
Mateusz Kulikowski8aac6972016-03-31 23:12:14 +0200202 struct msm_serial_data *priv = dev_get_priv(dev);
203
Ramon Fried1f0f4a12018-05-16 12:13:38 +0300204 /* No need to reinitialize the UART after relocation */
205 if (gd->flags & GD_FLG_RELOC)
206 return 0;
207
Ramon Fried5550dd92018-05-16 12:13:37 +0300208 ret = msm_uart_clk_init(dev);
209 if (ret)
210 return ret;
Mateusz Kulikowski8aac6972016-03-31 23:12:14 +0200211
Ramon Fried2292ed32018-05-16 12:13:42 +0300212 pinctrl_select_state(dev, "uart");
213 uart_dm_init(priv);
Mateusz Kulikowski8aac6972016-03-31 23:12:14 +0200214
215 return 0;
216}
217
218static int msm_serial_ofdata_to_platdata(struct udevice *dev)
219{
220 struct msm_serial_data *priv = dev_get_priv(dev);
221
Simon Glassba1dea42017-05-17 17:18:05 -0600222 priv->base = devfdt_get_addr(dev);
Mateusz Kulikowski8aac6972016-03-31 23:12:14 +0200223 if (priv->base == FDT_ADDR_T_NONE)
224 return -EINVAL;
225
226 return 0;
227}
228
229static const struct udevice_id msm_serial_ids[] = {
230 { .compatible = "qcom,msm-uartdm-v1.4" },
231 { }
232};
233
234U_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};