1*83d290c5STom Rini // SPDX-License-Identifier: GPL-2.0+
29e160ee8SPaul Thacker /*
39e160ee8SPaul Thacker * (c) 2015 Paul Thacker <paul.thacker@microchip.com>
49e160ee8SPaul Thacker *
59e160ee8SPaul Thacker */
69e160ee8SPaul Thacker #include <common.h>
79e160ee8SPaul Thacker #include <clk.h>
89e160ee8SPaul Thacker #include <dm.h>
99e160ee8SPaul Thacker #include <serial.h>
109e160ee8SPaul Thacker #include <wait_bit.h>
119e160ee8SPaul Thacker #include <mach/pic32.h>
129e160ee8SPaul Thacker #include <dt-bindings/clock/microchip,clock.h>
139e160ee8SPaul Thacker
149e160ee8SPaul Thacker DECLARE_GLOBAL_DATA_PTR;
159e160ee8SPaul Thacker
169e160ee8SPaul Thacker /* UART Control Registers */
179e160ee8SPaul Thacker #define U_MOD 0x00
189e160ee8SPaul Thacker #define U_MODCLR (U_MOD + _CLR_OFFSET)
199e160ee8SPaul Thacker #define U_MODSET (U_MOD + _SET_OFFSET)
209e160ee8SPaul Thacker #define U_STA 0x10
219e160ee8SPaul Thacker #define U_STACLR (U_STA + _CLR_OFFSET)
229e160ee8SPaul Thacker #define U_STASET (U_STA + _SET_OFFSET)
239e160ee8SPaul Thacker #define U_TXR 0x20
249e160ee8SPaul Thacker #define U_RXR 0x30
259e160ee8SPaul Thacker #define U_BRG 0x40
269e160ee8SPaul Thacker
279e160ee8SPaul Thacker /* U_MOD bits */
289e160ee8SPaul Thacker #define UART_ENABLE BIT(15)
299e160ee8SPaul Thacker
309e160ee8SPaul Thacker /* U_STA bits */
319e160ee8SPaul Thacker #define UART_RX_ENABLE BIT(12)
329e160ee8SPaul Thacker #define UART_TX_BRK BIT(11)
339e160ee8SPaul Thacker #define UART_TX_ENABLE BIT(10)
349e160ee8SPaul Thacker #define UART_TX_FULL BIT(9)
359e160ee8SPaul Thacker #define UART_TX_EMPTY BIT(8)
369e160ee8SPaul Thacker #define UART_RX_OVER BIT(1)
379e160ee8SPaul Thacker #define UART_RX_DATA_AVAIL BIT(0)
389e160ee8SPaul Thacker
399e160ee8SPaul Thacker struct pic32_uart_priv {
409e160ee8SPaul Thacker void __iomem *base;
419e160ee8SPaul Thacker ulong uartclk;
429e160ee8SPaul Thacker };
439e160ee8SPaul Thacker
449e160ee8SPaul Thacker /*
459e160ee8SPaul Thacker * Initialize the serial port with the given baudrate.
469e160ee8SPaul Thacker * The settings are always 8 data bits, no parity, 1 stop bit, no start bits.
479e160ee8SPaul Thacker */
pic32_serial_init(void __iomem * base,ulong clk,u32 baudrate)489e160ee8SPaul Thacker static int pic32_serial_init(void __iomem *base, ulong clk, u32 baudrate)
499e160ee8SPaul Thacker {
509e160ee8SPaul Thacker u32 div = DIV_ROUND_CLOSEST(clk, baudrate * 16);
519e160ee8SPaul Thacker
529e160ee8SPaul Thacker /* wait for TX FIFO to empty */
5348263504SÁlvaro Fernández Rojas wait_for_bit_le32(base + U_STA, UART_TX_EMPTY,
549e160ee8SPaul Thacker true, CONFIG_SYS_HZ, false);
559e160ee8SPaul Thacker
569e160ee8SPaul Thacker /* send break */
579e160ee8SPaul Thacker writel(UART_TX_BRK, base + U_STASET);
589e160ee8SPaul Thacker
599e160ee8SPaul Thacker /* disable and clear mode */
609e160ee8SPaul Thacker writel(0, base + U_MOD);
619e160ee8SPaul Thacker writel(0, base + U_STA);
629e160ee8SPaul Thacker
639e160ee8SPaul Thacker /* set baud rate generator */
649e160ee8SPaul Thacker writel(div - 1, base + U_BRG);
659e160ee8SPaul Thacker
669e160ee8SPaul Thacker /* enable the UART for TX and RX */
679e160ee8SPaul Thacker writel(UART_TX_ENABLE | UART_RX_ENABLE, base + U_STASET);
689e160ee8SPaul Thacker
699e160ee8SPaul Thacker /* enable the UART */
709e160ee8SPaul Thacker writel(UART_ENABLE, base + U_MODSET);
719e160ee8SPaul Thacker return 0;
729e160ee8SPaul Thacker }
739e160ee8SPaul Thacker
749e160ee8SPaul Thacker /* Check whether any char pending in RX fifo */
pic32_uart_pending_input(void __iomem * base)759e160ee8SPaul Thacker static int pic32_uart_pending_input(void __iomem *base)
769e160ee8SPaul Thacker {
779e160ee8SPaul Thacker /* check if rx buffer overrun error has occurred */
789e160ee8SPaul Thacker if (readl(base + U_STA) & UART_RX_OVER) {
799e160ee8SPaul Thacker readl(base + U_RXR);
809e160ee8SPaul Thacker
819e160ee8SPaul Thacker /* clear overrun error to keep receiving */
829e160ee8SPaul Thacker writel(UART_RX_OVER, base + U_STACLR);
839e160ee8SPaul Thacker }
849e160ee8SPaul Thacker
859e160ee8SPaul Thacker /* In PIC32 there is no way to know number of outstanding
869e160ee8SPaul Thacker * chars in rx-fifo. Only it can be known whether there is any.
879e160ee8SPaul Thacker */
889e160ee8SPaul Thacker return readl(base + U_STA) & UART_RX_DATA_AVAIL;
899e160ee8SPaul Thacker }
909e160ee8SPaul Thacker
pic32_uart_pending(struct udevice * dev,bool input)919e160ee8SPaul Thacker static int pic32_uart_pending(struct udevice *dev, bool input)
929e160ee8SPaul Thacker {
939e160ee8SPaul Thacker struct pic32_uart_priv *priv = dev_get_priv(dev);
949e160ee8SPaul Thacker
959e160ee8SPaul Thacker if (input)
969e160ee8SPaul Thacker return pic32_uart_pending_input(priv->base);
979e160ee8SPaul Thacker
989e160ee8SPaul Thacker return !(readl(priv->base + U_STA) & UART_TX_EMPTY);
999e160ee8SPaul Thacker }
1009e160ee8SPaul Thacker
pic32_uart_setbrg(struct udevice * dev,int baudrate)1019e160ee8SPaul Thacker static int pic32_uart_setbrg(struct udevice *dev, int baudrate)
1029e160ee8SPaul Thacker {
1039e160ee8SPaul Thacker struct pic32_uart_priv *priv = dev_get_priv(dev);
1049e160ee8SPaul Thacker
1059e160ee8SPaul Thacker return pic32_serial_init(priv->base, priv->uartclk, baudrate);
1069e160ee8SPaul Thacker }
1079e160ee8SPaul Thacker
pic32_uart_putc(struct udevice * dev,const char ch)1089e160ee8SPaul Thacker static int pic32_uart_putc(struct udevice *dev, const char ch)
1099e160ee8SPaul Thacker {
1109e160ee8SPaul Thacker struct pic32_uart_priv *priv = dev_get_priv(dev);
1119e160ee8SPaul Thacker
1129e160ee8SPaul Thacker /* Check if Tx FIFO is full */
1139e160ee8SPaul Thacker if (readl(priv->base + U_STA) & UART_TX_FULL)
1149e160ee8SPaul Thacker return -EAGAIN;
1159e160ee8SPaul Thacker
1169e160ee8SPaul Thacker /* pump the char to tx buffer */
1179e160ee8SPaul Thacker writel(ch, priv->base + U_TXR);
1189e160ee8SPaul Thacker
1199e160ee8SPaul Thacker return 0;
1209e160ee8SPaul Thacker }
1219e160ee8SPaul Thacker
pic32_uart_getc(struct udevice * dev)1229e160ee8SPaul Thacker static int pic32_uart_getc(struct udevice *dev)
1239e160ee8SPaul Thacker {
1249e160ee8SPaul Thacker struct pic32_uart_priv *priv = dev_get_priv(dev);
1259e160ee8SPaul Thacker
1269e160ee8SPaul Thacker /* return error if RX fifo is empty */
1279e160ee8SPaul Thacker if (!pic32_uart_pending_input(priv->base))
1289e160ee8SPaul Thacker return -EAGAIN;
1299e160ee8SPaul Thacker
1309e160ee8SPaul Thacker /* read the character from rx buffer */
1319e160ee8SPaul Thacker return readl(priv->base + U_RXR) & 0xff;
1329e160ee8SPaul Thacker }
1339e160ee8SPaul Thacker
pic32_uart_probe(struct udevice * dev)1349e160ee8SPaul Thacker static int pic32_uart_probe(struct udevice *dev)
1359e160ee8SPaul Thacker {
1369e160ee8SPaul Thacker struct pic32_uart_priv *priv = dev_get_priv(dev);
137135aa950SStephen Warren struct clk clk;
1389e160ee8SPaul Thacker fdt_addr_t addr;
1399e160ee8SPaul Thacker fdt_size_t size;
1409e160ee8SPaul Thacker int ret;
1419e160ee8SPaul Thacker
1429e160ee8SPaul Thacker /* get address */
143e160f7d4SSimon Glass addr = fdtdec_get_addr_size(gd->fdt_blob, dev_of_offset(dev), "reg",
144e160f7d4SSimon Glass &size);
1459e160ee8SPaul Thacker if (addr == FDT_ADDR_T_NONE)
1469e160ee8SPaul Thacker return -EINVAL;
1479e160ee8SPaul Thacker
1489e160ee8SPaul Thacker priv->base = ioremap(addr, size);
1499e160ee8SPaul Thacker
1509e160ee8SPaul Thacker /* get clock rate */
151135aa950SStephen Warren ret = clk_get_by_index(dev, 0, &clk);
1529e160ee8SPaul Thacker if (ret < 0)
1539e160ee8SPaul Thacker return ret;
154135aa950SStephen Warren priv->uartclk = clk_get_rate(&clk);
155135aa950SStephen Warren clk_free(&clk);
1569e160ee8SPaul Thacker
1579e160ee8SPaul Thacker /* initialize serial */
1589e160ee8SPaul Thacker return pic32_serial_init(priv->base, priv->uartclk, CONFIG_BAUDRATE);
1599e160ee8SPaul Thacker }
1609e160ee8SPaul Thacker
1619e160ee8SPaul Thacker static const struct dm_serial_ops pic32_uart_ops = {
1629e160ee8SPaul Thacker .putc = pic32_uart_putc,
1639e160ee8SPaul Thacker .pending = pic32_uart_pending,
1649e160ee8SPaul Thacker .getc = pic32_uart_getc,
1659e160ee8SPaul Thacker .setbrg = pic32_uart_setbrg,
1669e160ee8SPaul Thacker };
1679e160ee8SPaul Thacker
1689e160ee8SPaul Thacker static const struct udevice_id pic32_uart_ids[] = {
1699e160ee8SPaul Thacker { .compatible = "microchip,pic32mzda-uart" },
1709e160ee8SPaul Thacker {}
1719e160ee8SPaul Thacker };
1729e160ee8SPaul Thacker
1739e160ee8SPaul Thacker U_BOOT_DRIVER(pic32_serial) = {
1749e160ee8SPaul Thacker .name = "pic32-uart",
1759e160ee8SPaul Thacker .id = UCLASS_SERIAL,
1769e160ee8SPaul Thacker .of_match = pic32_uart_ids,
1779e160ee8SPaul Thacker .probe = pic32_uart_probe,
1789e160ee8SPaul Thacker .ops = &pic32_uart_ops,
1799e160ee8SPaul Thacker .priv_auto_alloc_size = sizeof(struct pic32_uart_priv),
1809e160ee8SPaul Thacker };
1819e160ee8SPaul Thacker
1829e160ee8SPaul Thacker #ifdef CONFIG_DEBUG_UART_PIC32
1839e160ee8SPaul Thacker #include <debug_uart.h>
1849e160ee8SPaul Thacker
_debug_uart_init(void)1859e160ee8SPaul Thacker static inline void _debug_uart_init(void)
1869e160ee8SPaul Thacker {
1879e160ee8SPaul Thacker void __iomem *base = (void __iomem *)CONFIG_DEBUG_UART_BASE;
1889e160ee8SPaul Thacker
1899e160ee8SPaul Thacker pic32_serial_init(base, CONFIG_DEBUG_UART_CLOCK, CONFIG_BAUDRATE);
1909e160ee8SPaul Thacker }
1919e160ee8SPaul Thacker
_debug_uart_putc(int ch)1929e160ee8SPaul Thacker static inline void _debug_uart_putc(int ch)
1939e160ee8SPaul Thacker {
1949e160ee8SPaul Thacker writel(ch, CONFIG_DEBUG_UART_BASE + U_TXR);
1959e160ee8SPaul Thacker }
1969e160ee8SPaul Thacker
1979e160ee8SPaul Thacker DEBUG_UART_FUNCS
1989e160ee8SPaul Thacker #endif
199