2 * Copyright (C) 2012-2015 Masahiro Yamada <yamada.masahiro@socionext.com>
4 * SPDX-License-Identifier: GPL-2.0+
8 #include <linux/serial_reg.h>
9 #include <linux/sizes.h>
10 #include <asm/errno.h>
11 #include <dm/device.h>
17 * Note: Register map is slightly different from that of 16550.
19 struct uniphier_serial {
20 u32 rx; /* In: Receive buffer */
21 #define tx rx /* Out: Transmit buffer */
22 u32 ier; /* Interrupt Enable Register */
23 u32 iir; /* In: Interrupt ID Register */
24 u32 char_fcr; /* Charactor / FIFO Control Register */
25 u32 lcr_mcr; /* Line/Modem Control Register */
27 #define LCR_MASK (0xff << (LCR_SHIFT))
28 u32 lsr; /* In: Line Status Register */
29 u32 msr; /* In: Modem Status Register */
32 u32 dlr; /* Divisor Latch Register */
35 struct uniphier_serial_private_data {
36 struct uniphier_serial __iomem *membase;
40 #define uniphier_serial_port(dev) \
41 ((struct uniphier_serial_private_data *)dev_get_priv(dev))->membase
43 static int uniphier_serial_setbrg(struct udevice *dev, int baudrate)
45 struct uniphier_serial_private_data *priv = dev_get_priv(dev);
46 struct uniphier_serial __iomem *port = uniphier_serial_port(dev);
47 const unsigned int mode_x_div = 16;
50 divisor = DIV_ROUND_CLOSEST(priv->uartclk, mode_x_div * baudrate);
52 writel(divisor, &port->dlr);
57 static int uniphier_serial_getc(struct udevice *dev)
59 struct uniphier_serial __iomem *port = uniphier_serial_port(dev);
61 if (!(readl(&port->lsr) & UART_LSR_DR))
64 return readl(&port->rx);
67 static int uniphier_serial_putc(struct udevice *dev, const char c)
69 struct uniphier_serial __iomem *port = uniphier_serial_port(dev);
71 if (!(readl(&port->lsr) & UART_LSR_THRE))
79 static int uniphier_serial_pending(struct udevice *dev, bool input)
81 struct uniphier_serial __iomem *port = uniphier_serial_port(dev);
84 return readl(&port->lsr) & UART_LSR_DR;
86 return !(readl(&port->lsr) & UART_LSR_THRE);
89 static int uniphier_serial_probe(struct udevice *dev)
91 DECLARE_GLOBAL_DATA_PTR;
92 struct uniphier_serial_private_data *priv = dev_get_priv(dev);
93 struct uniphier_serial __iomem *port;
97 base = dev_get_addr(dev);
98 if (base == FDT_ADDR_T_NONE)
101 port = map_sysmem(base, SZ_64);
105 priv->membase = port;
107 priv->uartclk = fdtdec_get_int(gd->fdt_blob, dev->of_offset,
108 "clock-frequency", 0);
110 tmp = readl(&port->lcr_mcr);
112 tmp |= UART_LCR_WLEN8 << LCR_SHIFT;
113 writel(tmp, &port->lcr_mcr);
118 static int uniphier_serial_remove(struct udevice *dev)
120 unmap_sysmem(uniphier_serial_port(dev));
125 static const struct udevice_id uniphier_uart_of_match[] = {
126 { .compatible = "socionext,uniphier-uart" },
130 static const struct dm_serial_ops uniphier_serial_ops = {
131 .setbrg = uniphier_serial_setbrg,
132 .getc = uniphier_serial_getc,
133 .putc = uniphier_serial_putc,
134 .pending = uniphier_serial_pending,
137 U_BOOT_DRIVER(uniphier_serial) = {
138 .name = "uniphier-uart",
140 .of_match = uniphier_uart_of_match,
141 .probe = uniphier_serial_probe,
142 .remove = uniphier_serial_remove,
143 .priv_auto_alloc_size = sizeof(struct uniphier_serial_private_data),
144 .ops = &uniphier_serial_ops,