2 * Copyright (C) 2012-2015 Masahiro Yamada <yamada.masahiro@socionext.com>
4 * SPDX-License-Identifier: GPL-2.0+
8 #include <linux/serial_reg.h>
10 #include <dm/device.h>
11 #include <dm/platform_data/serial-uniphier.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;
39 #define uniphier_serial_port(dev) \
40 ((struct uniphier_serial_private_data *)dev_get_priv(dev))->membase
42 static int uniphier_serial_setbrg(struct udevice *dev, int baudrate)
44 struct uniphier_serial_platform_data *plat = dev_get_platdata(dev);
45 struct uniphier_serial __iomem *port = uniphier_serial_port(dev);
46 const unsigned int mode_x_div = 16;
49 divisor = DIV_ROUND_CLOSEST(plat->uartclk, mode_x_div * baudrate);
51 writel(divisor, &port->dlr);
56 static int uniphier_serial_getc(struct udevice *dev)
58 struct uniphier_serial __iomem *port = uniphier_serial_port(dev);
60 if (!(readl(&port->lsr) & UART_LSR_DR))
63 return readl(&port->rx);
66 static int uniphier_serial_putc(struct udevice *dev, const char c)
68 struct uniphier_serial __iomem *port = uniphier_serial_port(dev);
70 if (!(readl(&port->lsr) & UART_LSR_THRE))
78 static int uniphier_serial_pending(struct udevice *dev, bool input)
80 struct uniphier_serial __iomem *port = uniphier_serial_port(dev);
83 return readl(&port->lsr) & UART_LSR_DR;
85 return !(readl(&port->lsr) & UART_LSR_THRE);
88 static int uniphier_serial_probe(struct udevice *dev)
91 struct uniphier_serial_private_data *priv = dev_get_priv(dev);
92 struct uniphier_serial_platform_data *plat = dev_get_platdata(dev);
93 struct uniphier_serial __iomem *port;
95 port = map_sysmem(plat->base, sizeof(struct uniphier_serial));
101 tmp = readl(&port->lcr_mcr);
103 tmp |= UART_LCR_WLEN8 << LCR_SHIFT;
104 writel(tmp, &port->lcr_mcr);
109 static int uniphier_serial_remove(struct udevice *dev)
111 unmap_sysmem(uniphier_serial_port(dev));
116 #ifdef CONFIG_OF_CONTROL
117 static const struct udevice_id uniphier_uart_of_match[] = {
118 { .compatible = "socionext,uniphier-uart" },
122 static int uniphier_serial_ofdata_to_platdata(struct udevice *dev)
124 struct uniphier_serial_platform_data *plat = dev_get_platdata(dev);
125 DECLARE_GLOBAL_DATA_PTR;
127 plat->base = fdtdec_get_addr(gd->fdt_blob, dev->of_offset, "reg");
128 plat->uartclk = fdtdec_get_int(gd->fdt_blob, dev->of_offset,
129 "clock-frequency", 0);
135 static const struct dm_serial_ops uniphier_serial_ops = {
136 .setbrg = uniphier_serial_setbrg,
137 .getc = uniphier_serial_getc,
138 .putc = uniphier_serial_putc,
139 .pending = uniphier_serial_pending,
142 U_BOOT_DRIVER(uniphier_serial) = {
145 .of_match = of_match_ptr(uniphier_uart_of_match),
146 .ofdata_to_platdata = of_match_ptr(uniphier_serial_ofdata_to_platdata),
147 .probe = uniphier_serial_probe,
148 .remove = uniphier_serial_remove,
149 .priv_auto_alloc_size = sizeof(struct uniphier_serial_private_data),
150 .platdata_auto_alloc_size =
151 sizeof(struct uniphier_serial_platform_data),
152 .ops = &uniphier_serial_ops,
153 .flags = DM_FLAG_PRE_RELOC,