1 // SPDX-License-Identifier: GPL-2.0+
5 * (C) Copyright 2015 Mateusz Kulikowski <mateusz.kulikowski@gmail.com>
7 * UART will work in Data Mover mode.
8 * Based on Linux driver.
18 #include <linux/compiler.h>
19 #include <dm/pinctrl.h>
21 /* Serial registers - this driver works in uartdm mode*/
23 #define UARTDM_DMRX 0x34 /* Max RX transfer length */
24 #define UARTDM_NCF_TX 0x40 /* Number of chars to TX */
26 #define UARTDM_RXFS 0x50 /* RX channel status register */
27 #define UARTDM_RXFS_BUF_SHIFT 0x7 /* Number of bytes in the packing buffer */
28 #define UARTDM_RXFS_BUF_MASK 0x7
29 #define UARTDM_MR1 0x00
30 #define UARTDM_MR2 0x04
31 #define UARTDM_CSR 0xA0
33 #define UARTDM_SR 0xA4 /* Status register */
34 #define UARTDM_SR_RX_READY (1 << 0) /* Word is the receiver FIFO */
35 #define UARTDM_SR_TX_EMPTY (1 << 3) /* Transmitter underrun */
36 #define UARTDM_SR_UART_OVERRUN (1 << 4) /* Receive overrun */
38 #define UARTDM_CR 0xA8 /* Command register */
39 #define UARTDM_CR_CMD_RESET_ERR (3 << 4) /* Clear overrun error */
40 #define UARTDM_CR_CMD_RESET_STALE_INT (8 << 4) /* Clears stale irq */
41 #define UARTDM_CR_CMD_RESET_TX_READY (3 << 8) /* Clears TX Ready irq*/
42 #define UARTDM_CR_CMD_FORCE_STALE (4 << 8) /* Causes stale event */
43 #define UARTDM_CR_CMD_STALE_EVENT_DISABLE (6 << 8) /* Disable stale event */
45 #define UARTDM_IMR 0xB0 /* Interrupt mask register */
46 #define UARTDM_ISR 0xB4 /* Interrupt status register */
47 #define UARTDM_ISR_TX_READY 0x80 /* TX FIFO empty */
49 #define UARTDM_TF 0x100 /* UART Transmit FIFO register */
50 #define UARTDM_RF 0x140 /* UART Receive FIFO register */
52 #define UART_DM_CLK_RX_TX_BIT_RATE 0xCC
53 #define MSM_BOOT_UART_DM_8_N_1_MODE 0x34
54 #define MSM_BOOT_UART_DM_CMD_RESET_RX 0x10
55 #define MSM_BOOT_UART_DM_CMD_RESET_TX 0x20
57 DECLARE_GLOBAL_DATA_PTR;
59 struct msm_serial_data {
61 unsigned chars_cnt; /* number of buffered chars */
62 uint32_t chars_buf; /* buffered chars */
65 static int msm_serial_fetch(struct udevice *dev)
67 struct msm_serial_data *priv = dev_get_priv(dev);
71 return priv->chars_cnt;
73 /* Clear error in case of buffer overrun */
74 if (readl(priv->base + UARTDM_SR) & UARTDM_SR_UART_OVERRUN)
75 writel(UARTDM_CR_CMD_RESET_ERR, priv->base + UARTDM_CR);
77 /* We need to fetch new character */
78 sr = readl(priv->base + UARTDM_SR);
80 if (sr & UARTDM_SR_RX_READY) {
81 /* There are at least 4 bytes in fifo */
82 priv->chars_buf = readl(priv->base + UARTDM_RF);
85 /* Check if there is anything in fifo */
86 priv->chars_cnt = readl(priv->base + UARTDM_RXFS);
87 /* Extract number of characters in UART packing buffer*/
88 priv->chars_cnt = (priv->chars_cnt >>
89 UARTDM_RXFS_BUF_SHIFT) &
94 /* There is at least one charcter, move it to fifo */
95 writel(UARTDM_CR_CMD_FORCE_STALE,
96 priv->base + UARTDM_CR);
98 priv->chars_buf = readl(priv->base + UARTDM_RF);
99 writel(UARTDM_CR_CMD_RESET_STALE_INT,
100 priv->base + UARTDM_CR);
101 writel(0x7, priv->base + UARTDM_DMRX);
104 return priv->chars_cnt;
107 static int msm_serial_getc(struct udevice *dev)
109 struct msm_serial_data *priv = dev_get_priv(dev);
112 if (!msm_serial_fetch(dev))
115 c = priv->chars_buf & 0xFF;
116 priv->chars_buf >>= 8;
122 static int msm_serial_putc(struct udevice *dev, const char ch)
124 struct msm_serial_data *priv = dev_get_priv(dev);
126 if (!(readl(priv->base + UARTDM_SR) & UARTDM_SR_TX_EMPTY) &&
127 !(readl(priv->base + UARTDM_ISR) & UARTDM_ISR_TX_READY))
130 writel(UARTDM_CR_CMD_RESET_TX_READY, priv->base + UARTDM_CR);
132 writel(1, priv->base + UARTDM_NCF_TX);
133 writel(ch, priv->base + UARTDM_TF);
138 static int msm_serial_pending(struct udevice *dev, bool input)
141 if (msm_serial_fetch(dev))
148 static const struct dm_serial_ops msm_serial_ops = {
149 .putc = msm_serial_putc,
150 .pending = msm_serial_pending,
151 .getc = msm_serial_getc,
154 static int msm_uart_clk_init(struct udevice *dev)
156 uint clk_rate = fdtdec_get_uint(gd->fdt_blob, dev_of_offset(dev),
157 "clock-frequency", 115200);
158 uint clkd[2]; /* clk_id and clk_no */
160 struct udevice *clk_dev;
164 ret = fdtdec_get_int_array(gd->fdt_blob, dev_of_offset(dev), "clock",
169 clk_offset = fdt_node_offset_by_phandle(gd->fdt_blob, clkd[0]);
173 ret = uclass_get_device_by_of_offset(UCLASS_CLK, clk_offset, &clk_dev);
178 ret = clk_request(clk_dev, &clk);
182 ret = clk_set_rate(&clk, clk_rate);
190 static void uart_dm_init(struct msm_serial_data *priv)
192 writel(UART_DM_CLK_RX_TX_BIT_RATE, priv->base + UARTDM_CSR);
193 writel(0x0, priv->base + UARTDM_MR1);
194 writel(MSM_BOOT_UART_DM_8_N_1_MODE, priv->base + UARTDM_MR2);
195 writel(MSM_BOOT_UART_DM_CMD_RESET_RX, priv->base + UARTDM_CR);
196 writel(MSM_BOOT_UART_DM_CMD_RESET_TX, priv->base + UARTDM_CR);
198 static int msm_serial_probe(struct udevice *dev)
201 struct msm_serial_data *priv = dev_get_priv(dev);
203 /* No need to reinitialize the UART after relocation */
204 if (gd->flags & GD_FLG_RELOC)
207 ret = msm_uart_clk_init(dev);
211 pinctrl_select_state(dev, "uart");
217 static int msm_serial_ofdata_to_platdata(struct udevice *dev)
219 struct msm_serial_data *priv = dev_get_priv(dev);
221 priv->base = devfdt_get_addr(dev);
222 if (priv->base == FDT_ADDR_T_NONE)
228 static const struct udevice_id msm_serial_ids[] = {
229 { .compatible = "qcom,msm-uartdm-v1.4" },
233 U_BOOT_DRIVER(serial_msm) = {
234 .name = "serial_msm",
236 .of_match = msm_serial_ids,
237 .ofdata_to_platdata = msm_serial_ofdata_to_platdata,
238 .priv_auto_alloc_size = sizeof(struct msm_serial_data),
239 .probe = msm_serial_probe,
240 .ops = &msm_serial_ops,