1 // SPDX-License-Identifier: GPL-2.0+
12 #include <asm/global_data.h>
13 #include <linux/bitops.h>
14 #include <mach/pic32.h>
15 #include <dt-bindings/clock/microchip,clock.h>
17 DECLARE_GLOBAL_DATA_PTR;
19 /* UART Control Registers */
21 #define U_MODCLR (U_MOD + _CLR_OFFSET)
22 #define U_MODSET (U_MOD + _SET_OFFSET)
24 #define U_STACLR (U_STA + _CLR_OFFSET)
25 #define U_STASET (U_STA + _SET_OFFSET)
31 #define UART_ENABLE BIT(15)
34 #define UART_RX_ENABLE BIT(12)
35 #define UART_TX_BRK BIT(11)
36 #define UART_TX_ENABLE BIT(10)
37 #define UART_TX_FULL BIT(9)
38 #define UART_TX_EMPTY BIT(8)
39 #define UART_RX_OVER BIT(1)
40 #define UART_RX_DATA_AVAIL BIT(0)
42 struct pic32_uart_priv {
48 * Initialize the serial port with the given baudrate.
49 * The settings are always 8 data bits, no parity, 1 stop bit, no start bits.
51 static int pic32_serial_init(void __iomem *base, ulong clk, u32 baudrate)
53 u32 div = DIV_ROUND_CLOSEST(clk, baudrate * 16);
55 /* wait for TX FIFO to empty */
56 wait_for_bit_le32(base + U_STA, UART_TX_EMPTY,
57 true, CONFIG_SYS_HZ, false);
60 writel(UART_TX_BRK, base + U_STASET);
62 /* disable and clear mode */
63 writel(0, base + U_MOD);
64 writel(0, base + U_STA);
66 /* set baud rate generator */
67 writel(div - 1, base + U_BRG);
69 /* enable the UART for TX and RX */
70 writel(UART_TX_ENABLE | UART_RX_ENABLE, base + U_STASET);
73 writel(UART_ENABLE, base + U_MODSET);
77 /* Check whether any char pending in RX fifo */
78 static int pic32_uart_pending_input(void __iomem *base)
80 /* check if rx buffer overrun error has occurred */
81 if (readl(base + U_STA) & UART_RX_OVER) {
84 /* clear overrun error to keep receiving */
85 writel(UART_RX_OVER, base + U_STACLR);
88 /* In PIC32 there is no way to know number of outstanding
89 * chars in rx-fifo. Only it can be known whether there is any.
91 return readl(base + U_STA) & UART_RX_DATA_AVAIL;
94 static int pic32_uart_pending(struct udevice *dev, bool input)
96 struct pic32_uart_priv *priv = dev_get_priv(dev);
99 return pic32_uart_pending_input(priv->base);
101 return !(readl(priv->base + U_STA) & UART_TX_EMPTY);
104 static int pic32_uart_setbrg(struct udevice *dev, int baudrate)
106 struct pic32_uart_priv *priv = dev_get_priv(dev);
108 return pic32_serial_init(priv->base, priv->uartclk, baudrate);
111 static int pic32_uart_putc(struct udevice *dev, const char ch)
113 struct pic32_uart_priv *priv = dev_get_priv(dev);
115 /* Check if Tx FIFO is full */
116 if (readl(priv->base + U_STA) & UART_TX_FULL)
119 /* pump the char to tx buffer */
120 writel(ch, priv->base + U_TXR);
125 static int pic32_uart_getc(struct udevice *dev)
127 struct pic32_uart_priv *priv = dev_get_priv(dev);
129 /* return error if RX fifo is empty */
130 if (!pic32_uart_pending_input(priv->base))
133 /* read the character from rx buffer */
134 return readl(priv->base + U_RXR) & 0xff;
137 static int pic32_uart_probe(struct udevice *dev)
139 struct pic32_uart_priv *priv = dev_get_priv(dev);
146 addr = fdtdec_get_addr_size(gd->fdt_blob, dev_of_offset(dev), "reg",
148 if (addr == FDT_ADDR_T_NONE)
151 priv->base = ioremap(addr, size);
154 ret = clk_get_by_index(dev, 0, &clk);
157 priv->uartclk = clk_get_rate(&clk);
159 /* initialize serial */
160 return pic32_serial_init(priv->base, priv->uartclk, CONFIG_BAUDRATE);
163 static const struct dm_serial_ops pic32_uart_ops = {
164 .putc = pic32_uart_putc,
165 .pending = pic32_uart_pending,
166 .getc = pic32_uart_getc,
167 .setbrg = pic32_uart_setbrg,
170 static const struct udevice_id pic32_uart_ids[] = {
171 { .compatible = "microchip,pic32mzda-uart" },
175 U_BOOT_DRIVER(pic32_serial) = {
176 .name = "pic32-uart",
178 .of_match = pic32_uart_ids,
179 .probe = pic32_uart_probe,
180 .ops = &pic32_uart_ops,
181 .priv_auto = sizeof(struct pic32_uart_priv),
184 #ifdef CONFIG_DEBUG_UART_PIC32
185 #include <debug_uart.h>
187 static inline void _debug_uart_init(void)
189 void __iomem *base = (void __iomem *)CONFIG_VAL(DEBUG_UART_BASE);
191 pic32_serial_init(base, CONFIG_DEBUG_UART_CLOCK, CONFIG_BAUDRATE);
194 static inline void _debug_uart_putc(int ch)
196 writel(ch, CONFIG_VAL(DEBUG_UART_BASE) + U_TXR);