blob: d8dd5c1104ba269173199f1b2e12da8071b8e8cf [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>
Simon Glass3ba929a2020-10-30 21:38:53 -060018#include <asm/global_data.h>
Mateusz Kulikowski8aac6972016-03-31 23:12:14 +020019#include <asm/io.h>
20#include <linux/compiler.h>
Ramon Fried2292ed32018-05-16 12:13:42 +030021#include <dm/pinctrl.h>
Mateusz Kulikowski8aac6972016-03-31 23:12:14 +020022
23/* Serial registers - this driver works in uartdm mode*/
24
25#define UARTDM_DMRX 0x34 /* Max RX transfer length */
Stephan Gerhold706d4812021-06-28 10:40:09 +020026#define UARTDM_DMEN 0x3C /* DMA/data-packing mode */
Mateusz Kulikowski8aac6972016-03-31 23:12:14 +020027#define UARTDM_NCF_TX 0x40 /* Number of chars to TX */
28
29#define UARTDM_RXFS 0x50 /* RX channel status register */
30#define UARTDM_RXFS_BUF_SHIFT 0x7 /* Number of bytes in the packing buffer */
31#define UARTDM_RXFS_BUF_MASK 0x7
Ramon Fried2292ed32018-05-16 12:13:42 +030032#define UARTDM_MR1 0x00
33#define UARTDM_MR2 0x04
34#define UARTDM_CSR 0xA0
Mateusz Kulikowski8aac6972016-03-31 23:12:14 +020035
36#define UARTDM_SR 0xA4 /* Status register */
37#define UARTDM_SR_RX_READY (1 << 0) /* Word is the receiver FIFO */
38#define UARTDM_SR_TX_EMPTY (1 << 3) /* Transmitter underrun */
39#define UARTDM_SR_UART_OVERRUN (1 << 4) /* Receive overrun */
40
41#define UARTDM_CR 0xA8 /* Command register */
42#define UARTDM_CR_CMD_RESET_ERR (3 << 4) /* Clear overrun error */
43#define UARTDM_CR_CMD_RESET_STALE_INT (8 << 4) /* Clears stale irq */
44#define UARTDM_CR_CMD_RESET_TX_READY (3 << 8) /* Clears TX Ready irq*/
45#define UARTDM_CR_CMD_FORCE_STALE (4 << 8) /* Causes stale event */
46#define UARTDM_CR_CMD_STALE_EVENT_DISABLE (6 << 8) /* Disable stale event */
47
48#define UARTDM_IMR 0xB0 /* Interrupt mask register */
49#define UARTDM_ISR 0xB4 /* Interrupt status register */
50#define UARTDM_ISR_TX_READY 0x80 /* TX FIFO empty */
51
52#define UARTDM_TF 0x100 /* UART Transmit FIFO register */
53#define UARTDM_RF 0x140 /* UART Receive FIFO register */
54
Ramon Fried2292ed32018-05-16 12:13:42 +030055#define UART_DM_CLK_RX_TX_BIT_RATE 0xCC
56#define MSM_BOOT_UART_DM_8_N_1_MODE 0x34
57#define MSM_BOOT_UART_DM_CMD_RESET_RX 0x10
58#define MSM_BOOT_UART_DM_CMD_RESET_TX 0x20
Mateusz Kulikowski8aac6972016-03-31 23:12:14 +020059
60DECLARE_GLOBAL_DATA_PTR;
61
62struct msm_serial_data {
63 phys_addr_t base;
64 unsigned chars_cnt; /* number of buffered chars */
65 uint32_t chars_buf; /* buffered chars */
Robert Marko03a36022020-07-06 10:37:55 +020066 uint32_t clk_bit_rate; /* data mover mode bit rate register value */
Mateusz Kulikowski8aac6972016-03-31 23:12:14 +020067};
68
69static int msm_serial_fetch(struct udevice *dev)
70{
71 struct msm_serial_data *priv = dev_get_priv(dev);
72 unsigned sr;
73
74 if (priv->chars_cnt)
75 return priv->chars_cnt;
76
77 /* Clear error in case of buffer overrun */
78 if (readl(priv->base + UARTDM_SR) & UARTDM_SR_UART_OVERRUN)
79 writel(UARTDM_CR_CMD_RESET_ERR, priv->base + UARTDM_CR);
80
81 /* We need to fetch new character */
82 sr = readl(priv->base + UARTDM_SR);
83
84 if (sr & UARTDM_SR_RX_READY) {
85 /* There are at least 4 bytes in fifo */
86 priv->chars_buf = readl(priv->base + UARTDM_RF);
87 priv->chars_cnt = 4;
88 } else {
89 /* Check if there is anything in fifo */
90 priv->chars_cnt = readl(priv->base + UARTDM_RXFS);
91 /* Extract number of characters in UART packing buffer*/
92 priv->chars_cnt = (priv->chars_cnt >>
93 UARTDM_RXFS_BUF_SHIFT) &
94 UARTDM_RXFS_BUF_MASK;
95 if (!priv->chars_cnt)
96 return 0;
97
98 /* There is at least one charcter, move it to fifo */
99 writel(UARTDM_CR_CMD_FORCE_STALE,
100 priv->base + UARTDM_CR);
101
102 priv->chars_buf = readl(priv->base + UARTDM_RF);
103 writel(UARTDM_CR_CMD_RESET_STALE_INT,
104 priv->base + UARTDM_CR);
105 writel(0x7, priv->base + UARTDM_DMRX);
106 }
107
108 return priv->chars_cnt;
109}
110
111static int msm_serial_getc(struct udevice *dev)
112{
113 struct msm_serial_data *priv = dev_get_priv(dev);
114 char c;
115
116 if (!msm_serial_fetch(dev))
117 return -EAGAIN;
118
119 c = priv->chars_buf & 0xFF;
120 priv->chars_buf >>= 8;
121 priv->chars_cnt--;
122
123 return c;
124}
125
126static int msm_serial_putc(struct udevice *dev, const char ch)
127{
128 struct msm_serial_data *priv = dev_get_priv(dev);
129
130 if (!(readl(priv->base + UARTDM_SR) & UARTDM_SR_TX_EMPTY) &&
131 !(readl(priv->base + UARTDM_ISR) & UARTDM_ISR_TX_READY))
132 return -EAGAIN;
133
134 writel(UARTDM_CR_CMD_RESET_TX_READY, priv->base + UARTDM_CR);
135
136 writel(1, priv->base + UARTDM_NCF_TX);
137 writel(ch, priv->base + UARTDM_TF);
138
139 return 0;
140}
141
142static int msm_serial_pending(struct udevice *dev, bool input)
143{
144 if (input) {
145 if (msm_serial_fetch(dev))
146 return 1;
147 }
148
149 return 0;
150}
151
152static const struct dm_serial_ops msm_serial_ops = {
153 .putc = msm_serial_putc,
154 .pending = msm_serial_pending,
155 .getc = msm_serial_getc,
156};
157
158static int msm_uart_clk_init(struct udevice *dev)
159{
Simon Glassdd79d6e2017-01-17 16:52:55 -0700160 uint clk_rate = fdtdec_get_uint(gd->fdt_blob, dev_of_offset(dev),
Mateusz Kulikowski8aac6972016-03-31 23:12:14 +0200161 "clock-frequency", 115200);
162 uint clkd[2]; /* clk_id and clk_no */
163 int clk_offset;
Stephen Warrena9622432016-06-17 09:44:00 -0600164 struct udevice *clk_dev;
165 struct clk clk;
Mateusz Kulikowski8aac6972016-03-31 23:12:14 +0200166 int ret;
167
Simon Glassdd79d6e2017-01-17 16:52:55 -0700168 ret = fdtdec_get_int_array(gd->fdt_blob, dev_of_offset(dev), "clock",
169 clkd, 2);
Mateusz Kulikowski8aac6972016-03-31 23:12:14 +0200170 if (ret)
171 return ret;
172
173 clk_offset = fdt_node_offset_by_phandle(gd->fdt_blob, clkd[0]);
174 if (clk_offset < 0)
175 return clk_offset;
176
Stephen Warrena9622432016-06-17 09:44:00 -0600177 ret = uclass_get_device_by_of_offset(UCLASS_CLK, clk_offset, &clk_dev);
Mateusz Kulikowski8aac6972016-03-31 23:12:14 +0200178 if (ret)
179 return ret;
180
Stephen Warrena9622432016-06-17 09:44:00 -0600181 clk.id = clkd[1];
182 ret = clk_request(clk_dev, &clk);
183 if (ret < 0)
184 return ret;
185
186 ret = clk_set_rate(&clk, clk_rate);
187 clk_free(&clk);
Mateusz Kulikowski8aac6972016-03-31 23:12:14 +0200188 if (ret < 0)
189 return ret;
190
191 return 0;
192}
193
Ramon Fried2292ed32018-05-16 12:13:42 +0300194static void uart_dm_init(struct msm_serial_data *priv)
195{
Robert Marko03a36022020-07-06 10:37:55 +0200196 writel(priv->clk_bit_rate, priv->base + UARTDM_CSR);
Ramon Fried2292ed32018-05-16 12:13:42 +0300197 writel(0x0, priv->base + UARTDM_MR1);
198 writel(MSM_BOOT_UART_DM_8_N_1_MODE, priv->base + UARTDM_MR2);
199 writel(MSM_BOOT_UART_DM_CMD_RESET_RX, priv->base + UARTDM_CR);
200 writel(MSM_BOOT_UART_DM_CMD_RESET_TX, priv->base + UARTDM_CR);
Stephan Gerhold706d4812021-06-28 10:40:09 +0200201
202 /* Make sure BAM/single character mode is disabled */
203 writel(0x0, priv->base + UARTDM_DMEN);
Ramon Fried2292ed32018-05-16 12:13:42 +0300204}
Mateusz Kulikowski8aac6972016-03-31 23:12:14 +0200205static int msm_serial_probe(struct udevice *dev)
206{
Ramon Fried5550dd92018-05-16 12:13:37 +0300207 int ret;
Mateusz Kulikowski8aac6972016-03-31 23:12:14 +0200208 struct msm_serial_data *priv = dev_get_priv(dev);
209
Ramon Fried1f0f4a12018-05-16 12:13:38 +0300210 /* No need to reinitialize the UART after relocation */
211 if (gd->flags & GD_FLG_RELOC)
212 return 0;
213
Ramon Fried5550dd92018-05-16 12:13:37 +0300214 ret = msm_uart_clk_init(dev);
215 if (ret)
216 return ret;
Mateusz Kulikowski8aac6972016-03-31 23:12:14 +0200217
Ramon Fried2292ed32018-05-16 12:13:42 +0300218 pinctrl_select_state(dev, "uart");
219 uart_dm_init(priv);
Mateusz Kulikowski8aac6972016-03-31 23:12:14 +0200220
221 return 0;
222}
223
Simon Glassaad29ae2020-12-03 16:55:21 -0700224static int msm_serial_of_to_plat(struct udevice *dev)
Mateusz Kulikowski8aac6972016-03-31 23:12:14 +0200225{
226 struct msm_serial_data *priv = dev_get_priv(dev);
227
Masahiro Yamadaa89b4de2020-07-17 14:36:48 +0900228 priv->base = dev_read_addr(dev);
Mateusz Kulikowski8aac6972016-03-31 23:12:14 +0200229 if (priv->base == FDT_ADDR_T_NONE)
230 return -EINVAL;
231
Robert Marko03a36022020-07-06 10:37:55 +0200232 priv->clk_bit_rate = fdtdec_get_int(gd->fdt_blob, dev_of_offset(dev),
233 "bit-rate", UART_DM_CLK_RX_TX_BIT_RATE);
234
Mateusz Kulikowski8aac6972016-03-31 23:12:14 +0200235 return 0;
236}
237
238static const struct udevice_id msm_serial_ids[] = {
239 { .compatible = "qcom,msm-uartdm-v1.4" },
240 { }
241};
242
243U_BOOT_DRIVER(serial_msm) = {
244 .name = "serial_msm",
245 .id = UCLASS_SERIAL,
246 .of_match = msm_serial_ids,
Simon Glassaad29ae2020-12-03 16:55:21 -0700247 .of_to_plat = msm_serial_of_to_plat,
Simon Glass8a2b47f2020-12-03 16:55:17 -0700248 .priv_auto = sizeof(struct msm_serial_data),
Mateusz Kulikowski8aac6972016-03-31 23:12:14 +0200249 .probe = msm_serial_probe,
250 .ops = &msm_serial_ops,
251};