blob: a472e0b3683399cfe6035595f6786254725b6fa7 [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>
Stephan Gerhold9a4569f2021-07-14 10:56:26 +020021#include <linux/delay.h>
Ramon Fried2292ed32018-05-16 12:13:42 +030022#include <dm/pinctrl.h>
Mateusz Kulikowski8aac6972016-03-31 23:12:14 +020023
24/* Serial registers - this driver works in uartdm mode*/
25
26#define UARTDM_DMRX 0x34 /* Max RX transfer length */
Stephan Gerhold706d4812021-06-28 10:40:09 +020027#define UARTDM_DMEN 0x3C /* DMA/data-packing mode */
Mateusz Kulikowski8aac6972016-03-31 23:12:14 +020028#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 Fried2292ed32018-05-16 12:13:42 +030033#define UARTDM_MR1 0x00
34#define UARTDM_MR2 0x04
Caleb Connollyfb47e062024-04-15 16:03:40 +010035/*
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 Fried2292ed32018-05-16 12:13:42 +030045#define UARTDM_CSR 0xA0
Mateusz Kulikowski8aac6972016-03-31 23:12:14 +020046
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
Sumit Garg4f219db2024-04-12 15:24:34 +053066#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
69#define MSM_UART_MR1_RX_RDY_CTL BIT(7)
Mateusz Kulikowski8aac6972016-03-31 23:12:14 +020070
71DECLARE_GLOBAL_DATA_PTR;
72
73struct msm_serial_data {
74 phys_addr_t base;
75 unsigned chars_cnt; /* number of buffered chars */
76 uint32_t chars_buf; /* buffered chars */
Caleb Connollyfb47e062024-04-15 16:03:40 +010077 uint32_t clk_rate; /* core clock rate */
Mateusz Kulikowski8aac6972016-03-31 23:12:14 +020078};
79
80static int msm_serial_fetch(struct udevice *dev)
81{
82 struct msm_serial_data *priv = dev_get_priv(dev);
83 unsigned sr;
84
85 if (priv->chars_cnt)
86 return priv->chars_cnt;
87
88 /* Clear error in case of buffer overrun */
89 if (readl(priv->base + UARTDM_SR) & UARTDM_SR_UART_OVERRUN)
90 writel(UARTDM_CR_CMD_RESET_ERR, priv->base + UARTDM_CR);
91
92 /* We need to fetch new character */
93 sr = readl(priv->base + UARTDM_SR);
94
95 if (sr & UARTDM_SR_RX_READY) {
96 /* There are at least 4 bytes in fifo */
97 priv->chars_buf = readl(priv->base + UARTDM_RF);
98 priv->chars_cnt = 4;
99 } else {
100 /* Check if there is anything in fifo */
101 priv->chars_cnt = readl(priv->base + UARTDM_RXFS);
102 /* Extract number of characters in UART packing buffer*/
103 priv->chars_cnt = (priv->chars_cnt >>
104 UARTDM_RXFS_BUF_SHIFT) &
105 UARTDM_RXFS_BUF_MASK;
106 if (!priv->chars_cnt)
107 return 0;
108
109 /* There is at least one charcter, move it to fifo */
110 writel(UARTDM_CR_CMD_FORCE_STALE,
111 priv->base + UARTDM_CR);
112
113 priv->chars_buf = readl(priv->base + UARTDM_RF);
114 writel(UARTDM_CR_CMD_RESET_STALE_INT,
115 priv->base + UARTDM_CR);
116 writel(0x7, priv->base + UARTDM_DMRX);
117 }
118
119 return priv->chars_cnt;
120}
121
122static int msm_serial_getc(struct udevice *dev)
123{
124 struct msm_serial_data *priv = dev_get_priv(dev);
125 char c;
126
127 if (!msm_serial_fetch(dev))
128 return -EAGAIN;
129
130 c = priv->chars_buf & 0xFF;
131 priv->chars_buf >>= 8;
132 priv->chars_cnt--;
133
134 return c;
135}
136
137static int msm_serial_putc(struct udevice *dev, const char ch)
138{
139 struct msm_serial_data *priv = dev_get_priv(dev);
140
141 if (!(readl(priv->base + UARTDM_SR) & UARTDM_SR_TX_EMPTY) &&
142 !(readl(priv->base + UARTDM_ISR) & UARTDM_ISR_TX_READY))
143 return -EAGAIN;
144
145 writel(UARTDM_CR_CMD_RESET_TX_READY, priv->base + UARTDM_CR);
146
147 writel(1, priv->base + UARTDM_NCF_TX);
148 writel(ch, priv->base + UARTDM_TF);
149
150 return 0;
151}
152
153static int msm_serial_pending(struct udevice *dev, bool input)
154{
155 if (input) {
156 if (msm_serial_fetch(dev))
157 return 1;
158 }
159
160 return 0;
161}
162
163static const struct dm_serial_ops msm_serial_ops = {
164 .putc = msm_serial_putc,
165 .pending = msm_serial_pending,
166 .getc = msm_serial_getc,
167};
168
Caleb Connollyfb47e062024-04-15 16:03:40 +0100169static long msm_uart_clk_init(struct udevice *dev)
Mateusz Kulikowski8aac6972016-03-31 23:12:14 +0200170{
Caleb Connollyfb47e062024-04-15 16:03:40 +0100171 struct msm_serial_data *priv = dev_get_priv(dev);
Stephen Warrena9622432016-06-17 09:44:00 -0600172 struct clk clk;
Mateusz Kulikowski8aac6972016-03-31 23:12:14 +0200173 int ret;
Caleb Connollyfb47e062024-04-15 16:03:40 +0100174 long rate;
Mateusz Kulikowski8aac6972016-03-31 23:12:14 +0200175
Caleb Connolly7a9c7b92024-02-26 17:26:12 +0000176 ret = clk_get_by_name(dev, "core", &clk);
177 if (ret < 0) {
178 pr_warn("%s: Failed to get clock: %d\n", __func__, ret);
Caleb Connollyfb47e062024-04-15 16:03:40 +0100179 return 0;
Caleb Connolly7a9c7b92024-02-26 17:26:12 +0000180 }
Stephen Warrena9622432016-06-17 09:44:00 -0600181
Caleb Connollyfb47e062024-04-15 16:03:40 +0100182 rate = clk_set_rate(&clk, priv->clk_rate);
Mateusz Kulikowski8aac6972016-03-31 23:12:14 +0200183
Caleb Connollyfb47e062024-04-15 16:03:40 +0100184 return rate;
185}
186
187static int calc_csr_bitrate(struct msm_serial_data *priv)
188{
189 /* This table is from the TRE. See the definition of UARTDM_CSR */
190 unsigned int csr_div_table[] = {24576, 12288, 6144, 3072, 1536, 768, 512, 384,
191 256, 192, 128, 96, 64, 48, 32, 16};
192 int i = ARRAY_SIZE(csr_div_table) - 1;
193 /* Currently we only support one baudrate */
194 int baud = 115200;
195
196 for (; i >= 0; i--) {
197 int x = priv->clk_rate / csr_div_table[i];
198
199 if (x == baud)
200 /* Duplicate the configuration for RX
201 * as the lower nybble only configures TX
202 */
203 return i + (i << 4);
204 }
205
206 return -EINVAL;
Mateusz Kulikowski8aac6972016-03-31 23:12:14 +0200207}
208
Ramon Fried2292ed32018-05-16 12:13:42 +0300209static void uart_dm_init(struct msm_serial_data *priv)
210{
Stephan Gerhold9a4569f2021-07-14 10:56:26 +0200211 /* Delay initialization for a bit to let pins stabilize if necessary */
212 mdelay(5);
Caleb Connollyfb47e062024-04-15 16:03:40 +0100213 int bitrate = calc_csr_bitrate(priv);
214 if (bitrate < 0) {
215 log_warning("Couldn't calculate bit clock divider! Using default\n");
216 /* This happens to be the value used on MSM8916 for the hardcoded clockrate
217 * in clock-apq8016. It's at least a better guess than a value we *know*
218 * is wrong...
219 */
220 bitrate = 0xCC;
221 }
Stephan Gerhold9a4569f2021-07-14 10:56:26 +0200222
Caleb Connollyfb47e062024-04-15 16:03:40 +0100223 writel(bitrate, priv->base + UARTDM_CSR);
Sumit Garg4f219db2024-04-12 15:24:34 +0530224 /* Enable RS232 flow control to support RS232 db9 connector */
225 writel(MSM_UART_MR1_RX_RDY_CTL, priv->base + UARTDM_MR1);
Ramon Fried2292ed32018-05-16 12:13:42 +0300226 writel(MSM_BOOT_UART_DM_8_N_1_MODE, priv->base + UARTDM_MR2);
227 writel(MSM_BOOT_UART_DM_CMD_RESET_RX, priv->base + UARTDM_CR);
228 writel(MSM_BOOT_UART_DM_CMD_RESET_TX, priv->base + UARTDM_CR);
Stephan Gerhold706d4812021-06-28 10:40:09 +0200229
230 /* Make sure BAM/single character mode is disabled */
231 writel(0x0, priv->base + UARTDM_DMEN);
Ramon Fried2292ed32018-05-16 12:13:42 +0300232}
Mateusz Kulikowski8aac6972016-03-31 23:12:14 +0200233static int msm_serial_probe(struct udevice *dev)
234{
235 struct msm_serial_data *priv = dev_get_priv(dev);
Caleb Connollyfb47e062024-04-15 16:03:40 +0100236 long rate;
Mateusz Kulikowski8aac6972016-03-31 23:12:14 +0200237
Ramon Fried1f0f4a12018-05-16 12:13:38 +0300238 /* No need to reinitialize the UART after relocation */
239 if (gd->flags & GD_FLG_RELOC)
240 return 0;
241
Caleb Connollyfb47e062024-04-15 16:03:40 +0100242 rate = msm_uart_clk_init(dev);
243 if (rate < 0)
244 return rate;
245 if (!rate) {
246 log_err("Got core clock rate of 0... Please fix your clock driver\n");
247 return -EINVAL;
248 }
249
250 /* Update the clock rate to the actual programmed rate returned by the
251 * clock driver
252 */
253 priv->clk_rate = rate;
Mateusz Kulikowski8aac6972016-03-31 23:12:14 +0200254
Ramon Fried2292ed32018-05-16 12:13:42 +0300255 uart_dm_init(priv);
Mateusz Kulikowski8aac6972016-03-31 23:12:14 +0200256
257 return 0;
258}
259
Simon Glassaad29ae2020-12-03 16:55:21 -0700260static int msm_serial_of_to_plat(struct udevice *dev)
Mateusz Kulikowski8aac6972016-03-31 23:12:14 +0200261{
262 struct msm_serial_data *priv = dev_get_priv(dev);
Caleb Connollyfb47e062024-04-15 16:03:40 +0100263 int ret;
Mateusz Kulikowski8aac6972016-03-31 23:12:14 +0200264
Masahiro Yamadaa89b4de2020-07-17 14:36:48 +0900265 priv->base = dev_read_addr(dev);
Mateusz Kulikowski8aac6972016-03-31 23:12:14 +0200266 if (priv->base == FDT_ADDR_T_NONE)
267 return -EINVAL;
268
Caleb Connollyfb47e062024-04-15 16:03:40 +0100269 ret = dev_read_u32(dev, "clock-frequency", &priv->clk_rate);
270 if (ret < 0) {
271 log_debug("No clock frequency specified, using default rate\n");
272 /* Default for APQ8016 */
273 priv->clk_rate = 7372800;
274 }
Robert Marko03a36022020-07-06 10:37:55 +0200275
Mateusz Kulikowski8aac6972016-03-31 23:12:14 +0200276 return 0;
277}
278
279static const struct udevice_id msm_serial_ids[] = {
280 { .compatible = "qcom,msm-uartdm-v1.4" },
281 { }
282};
283
284U_BOOT_DRIVER(serial_msm) = {
285 .name = "serial_msm",
286 .id = UCLASS_SERIAL,
287 .of_match = msm_serial_ids,
Simon Glassaad29ae2020-12-03 16:55:21 -0700288 .of_to_plat = msm_serial_of_to_plat,
Simon Glass8a2b47f2020-12-03 16:55:17 -0700289 .priv_auto = sizeof(struct msm_serial_data),
Mateusz Kulikowski8aac6972016-03-31 23:12:14 +0200290 .probe = msm_serial_probe,
291 .ops = &msm_serial_ops,
Caleb Connolly7a9c7b92024-02-26 17:26:12 +0000292 .flags = DM_FLAG_PRE_RELOC,
Mateusz Kulikowski8aac6972016-03-31 23:12:14 +0200293};
Caleb Connollya86d23a2024-02-26 17:26:11 +0000294
295#ifdef CONFIG_DEBUG_UART_MSM
296
297static struct msm_serial_data init_serial_data = {
298 .base = CONFIG_VAL(DEBUG_UART_BASE),
Caleb Connollyfb47e062024-04-15 16:03:40 +0100299 .clk_rate = CONFIG_VAL(DEBUG_UART_CLOCK),
Caleb Connollya86d23a2024-02-26 17:26:11 +0000300};
301
302#include <debug_uart.h>
303
304/* Uncomment to turn on UART clocks when debugging U-Boot as aboot on MSM8916 */
Sumit Gargbf06b692024-04-12 15:24:33 +0530305//int apq8016_clk_init_uart(phys_addr_t gcc_base, unsigned long id);
Caleb Connollya86d23a2024-02-26 17:26:11 +0000306
307static inline void _debug_uart_init(void)
308{
Sumit Gargbf06b692024-04-12 15:24:33 +0530309 /*
310 * Uncomment to turn on UART clocks when debugging U-Boot as aboot
311 * on MSM8916. Supported debug UART clock IDs:
312 * - db410c: GCC_BLSP1_UART2_APPS_CLK
313 * - HMIBSC: GCC_BLSP1_UART1_APPS_CLK
314 */
315 //apq8016_clk_init_uart(0x1800000, <uart_clk_id>);
Caleb Connollya86d23a2024-02-26 17:26:11 +0000316 uart_dm_init(&init_serial_data);
317}
318
319static inline void _debug_uart_putc(int ch)
320{
321 struct msm_serial_data *priv = &init_serial_data;
322
323 while (!(readl(priv->base + UARTDM_SR) & UARTDM_SR_TX_EMPTY) &&
324 !(readl(priv->base + UARTDM_ISR) & UARTDM_ISR_TX_READY))
325 ;
326
327 writel(UARTDM_CR_CMD_RESET_TX_READY, priv->base + UARTDM_CR);
328
329 writel(1, priv->base + UARTDM_NCF_TX);
330 writel(ch, priv->base + UARTDM_TF);
331}
332
333DEBUG_UART_FUNCS
334
335#endif