2 * Copyright (C) 2012-2014 Panasonic Corporation
3 * Author: Masahiro Yamada <yamada.m@jp.panasonic.com>
5 * SPDX-License-Identifier: GPL-2.0+
8 #include <linux/serial_reg.h>
10 #include <asm/errno.h>
11 #include <dm/device.h>
12 #include <dm/platform_data/serial-uniphier.h>
20 * Note: Register map is slightly different from that of 16550.
22 struct uniphier_serial {
23 UART_REG(rbr); /* 0x00 */
24 UART_REG(ier); /* 0x04 */
25 UART_REG(iir); /* 0x08 */
26 UART_REG(fcr); /* 0x0c */
30 UART_REG(lsr); /* 0x14 */
31 UART_REG(msr); /* 0x18 */
40 struct uniphier_serial_private_data {
41 struct uniphier_serial __iomem *membase;
44 #define uniphier_serial_port(dev) \
45 ((struct uniphier_serial_private_data *)dev_get_priv(dev))->membase
47 static int uniphier_serial_setbrg(struct udevice *dev, int baudrate)
49 struct uniphier_serial_platform_data *plat = dev_get_platdata(dev);
50 struct uniphier_serial __iomem *port = uniphier_serial_port(dev);
51 const unsigned int mode_x_div = 16;
54 writeb(UART_LCR_WLEN8, &port->lcr);
56 divisor = DIV_ROUND_CLOSEST(plat->uartclk, mode_x_div * baudrate);
58 writew(divisor, &port->dlr);
63 static int uniphier_serial_getc(struct udevice *dev)
65 struct uniphier_serial __iomem *port = uniphier_serial_port(dev);
67 if (!(readb(&port->lsr) & UART_LSR_DR))
70 return readb(&port->rbr);
73 static int uniphier_serial_putc(struct udevice *dev, const char c)
75 struct uniphier_serial __iomem *port = uniphier_serial_port(dev);
77 if (!(readb(&port->lsr) & UART_LSR_THRE))
80 writeb(c, &port->thr);
85 static int uniphier_serial_pending(struct udevice *dev, bool input)
87 struct uniphier_serial __iomem *port = uniphier_serial_port(dev);
90 return readb(&port->lsr) & UART_LSR_DR;
92 return !(readb(&port->lsr) & UART_LSR_THRE);
95 static int uniphier_serial_probe(struct udevice *dev)
97 struct uniphier_serial_private_data *priv = dev_get_priv(dev);
98 struct uniphier_serial_platform_data *plat = dev_get_platdata(dev);
100 priv->membase = map_sysmem(plat->base, sizeof(struct uniphier_serial));
108 static int uniphier_serial_remove(struct udevice *dev)
110 unmap_sysmem(uniphier_serial_port(dev));
115 #ifdef CONFIG_OF_CONTROL
116 static const struct udevice_id uniphier_uart_of_match = {
117 { .compatible = "panasonic,uniphier-uart"},
121 static int uniphier_serial_ofdata_to_platdata(struct udevice *dev)
124 * TODO: Masahiro Yamada (yamada.m@jp.panasonic.com)
126 * Implement conversion code from DTB to platform data
127 * when supporting CONFIG_OF_CONTROL on UniPhir platform.
132 static const struct dm_serial_ops uniphier_serial_ops = {
133 .setbrg = uniphier_serial_setbrg,
134 .getc = uniphier_serial_getc,
135 .putc = uniphier_serial_putc,
136 .pending = uniphier_serial_pending,
139 U_BOOT_DRIVER(uniphier_serial) = {
142 .of_match = of_match_ptr(uniphier_uart_of_match),
143 .ofdata_to_platdata = of_match_ptr(uniphier_serial_ofdata_to_platdata),
144 .probe = uniphier_serial_probe,
145 .remove = uniphier_serial_remove,
146 .priv_auto_alloc_size = sizeof(struct uniphier_serial_private_data),
147 .platdata_auto_alloc_size =
148 sizeof(struct uniphier_serial_platform_data),
149 .ops = &uniphier_serial_ops,
150 .flags = DM_FLAG_PRE_RELOC,