blob: 98e3b812e0b2a0fe44094acfc6036cdac14d490f [file] [log] [blame]
Masahiro Yamada7f368552014-10-03 19:21:05 +09001/*
Masahiro Yamadad0c47b32015-02-27 02:26:46 +09002 * Copyright (C) 2012-2015 Panasonic Corporation
Masahiro Yamada6462cde2015-03-11 15:54:46 +09003 * Copyright (C) 2015 Socionext Inc.
4 * Author: Masahiro Yamada <yamada.masahiro@socionext.com>
Masahiro Yamada7f368552014-10-03 19:21:05 +09005 *
Masahiro Yamada7f368552014-10-03 19:21:05 +09006 * SPDX-License-Identifier: GPL-2.0+
7 */
8
Masahiro Yamada325b7082014-10-30 12:11:14 +09009#include <linux/serial_reg.h>
Masahiro Yamadad064cbf2014-10-23 22:26:10 +090010#include <asm/io.h>
11#include <asm/errno.h>
12#include <dm/device.h>
13#include <dm/platform_data/serial-uniphier.h>
Masahiro Yamada7f368552014-10-03 19:21:05 +090014#include <serial.h>
Masahiro Yamada625177d2014-11-26 18:34:00 +090015#include <fdtdec.h>
Masahiro Yamada7f368552014-10-03 19:21:05 +090016
Masahiro Yamada7f368552014-10-03 19:21:05 +090017/*
18 * Note: Register map is slightly different from that of 16550.
19 */
20struct uniphier_serial {
Masahiro Yamadad0c47b32015-02-27 02:26:46 +090021 u32 rx; /* In: Receive buffer */
22#define tx rx /* Out: Transmit buffer */
23 u32 ier; /* Interrupt Enable Register */
24 u32 iir; /* In: Interrupt ID Register */
25 u32 char_fcr; /* Charactor / FIFO Control Register */
26 u32 lcr_mcr; /* Line/Modem Control Register */
27#define LCR_SHIFT 8
28#define LCR_MASK (0xff << (LCR_SHIFT))
29 u32 lsr; /* In: Line Status Register */
30 u32 msr; /* In: Modem Status Register */
31 u32 __rsv0;
32 u32 __rsv1;
33 u32 dlr; /* Divisor Latch Register */
Masahiro Yamada7f368552014-10-03 19:21:05 +090034};
35
Masahiro Yamadad064cbf2014-10-23 22:26:10 +090036struct uniphier_serial_private_data {
37 struct uniphier_serial __iomem *membase;
38};
Masahiro Yamada7f368552014-10-03 19:21:05 +090039
Masahiro Yamadad064cbf2014-10-23 22:26:10 +090040#define uniphier_serial_port(dev) \
41 ((struct uniphier_serial_private_data *)dev_get_priv(dev))->membase
42
Masahiro Yamadad9bc8fd2014-10-24 17:00:11 +090043static int uniphier_serial_setbrg(struct udevice *dev, int baudrate)
Masahiro Yamada7f368552014-10-03 19:21:05 +090044{
Masahiro Yamadad064cbf2014-10-23 22:26:10 +090045 struct uniphier_serial_platform_data *plat = dev_get_platdata(dev);
46 struct uniphier_serial __iomem *port = uniphier_serial_port(dev);
Masahiro Yamada7f368552014-10-03 19:21:05 +090047 const unsigned int mode_x_div = 16;
48 unsigned int divisor;
Masahiro Yamada7f368552014-10-03 19:21:05 +090049
Masahiro Yamadad064cbf2014-10-23 22:26:10 +090050 divisor = DIV_ROUND_CLOSEST(plat->uartclk, mode_x_div * baudrate);
Masahiro Yamada7f368552014-10-03 19:21:05 +090051
Masahiro Yamadad0c47b32015-02-27 02:26:46 +090052 writel(divisor, &port->dlr);
Masahiro Yamadad064cbf2014-10-23 22:26:10 +090053
54 return 0;
Masahiro Yamada7f368552014-10-03 19:21:05 +090055}
56
Masahiro Yamadad064cbf2014-10-23 22:26:10 +090057static int uniphier_serial_getc(struct udevice *dev)
Masahiro Yamada7f368552014-10-03 19:21:05 +090058{
Masahiro Yamadad064cbf2014-10-23 22:26:10 +090059 struct uniphier_serial __iomem *port = uniphier_serial_port(dev);
Masahiro Yamada7f368552014-10-03 19:21:05 +090060
Masahiro Yamadad0c47b32015-02-27 02:26:46 +090061 if (!(readl(&port->lsr) & UART_LSR_DR))
Masahiro Yamadad064cbf2014-10-23 22:26:10 +090062 return -EAGAIN;
Masahiro Yamada7f368552014-10-03 19:21:05 +090063
Masahiro Yamadad0c47b32015-02-27 02:26:46 +090064 return readl(&port->rx);
Masahiro Yamada7f368552014-10-03 19:21:05 +090065}
66
Masahiro Yamadad064cbf2014-10-23 22:26:10 +090067static int uniphier_serial_putc(struct udevice *dev, const char c)
Masahiro Yamada7f368552014-10-03 19:21:05 +090068{
Masahiro Yamadad064cbf2014-10-23 22:26:10 +090069 struct uniphier_serial __iomem *port = uniphier_serial_port(dev);
Masahiro Yamada7f368552014-10-03 19:21:05 +090070
Masahiro Yamadad0c47b32015-02-27 02:26:46 +090071 if (!(readl(&port->lsr) & UART_LSR_THRE))
Masahiro Yamadad064cbf2014-10-23 22:26:10 +090072 return -EAGAIN;
Masahiro Yamada7f368552014-10-03 19:21:05 +090073
Masahiro Yamadad0c47b32015-02-27 02:26:46 +090074 writel(c, &port->tx);
Masahiro Yamadad064cbf2014-10-23 22:26:10 +090075
76 return 0;
Masahiro Yamada7f368552014-10-03 19:21:05 +090077}
78
Masahiro Yamadabb721482014-10-24 17:00:10 +090079static int uniphier_serial_pending(struct udevice *dev, bool input)
80{
81 struct uniphier_serial __iomem *port = uniphier_serial_port(dev);
82
83 if (input)
Masahiro Yamadad0c47b32015-02-27 02:26:46 +090084 return readl(&port->lsr) & UART_LSR_DR;
Masahiro Yamadabb721482014-10-24 17:00:10 +090085 else
Masahiro Yamadad0c47b32015-02-27 02:26:46 +090086 return !(readl(&port->lsr) & UART_LSR_THRE);
Masahiro Yamadabb721482014-10-24 17:00:10 +090087}
88
Masahiro Yamadad9bc8fd2014-10-24 17:00:11 +090089static int uniphier_serial_probe(struct udevice *dev)
Masahiro Yamadad064cbf2014-10-23 22:26:10 +090090{
Masahiro Yamada099cf772015-02-27 02:26:47 +090091 u32 tmp;
Masahiro Yamadad064cbf2014-10-23 22:26:10 +090092 struct uniphier_serial_private_data *priv = dev_get_priv(dev);
93 struct uniphier_serial_platform_data *plat = dev_get_platdata(dev);
Masahiro Yamada099cf772015-02-27 02:26:47 +090094 struct uniphier_serial __iomem *port;
Masahiro Yamadad064cbf2014-10-23 22:26:10 +090095
Masahiro Yamada099cf772015-02-27 02:26:47 +090096 port = map_sysmem(plat->base, sizeof(struct uniphier_serial));
97 if (!port)
Masahiro Yamadad064cbf2014-10-23 22:26:10 +090098 return -ENOMEM;
99
Masahiro Yamada099cf772015-02-27 02:26:47 +0900100 priv->membase = port;
101
102 tmp = readl(&port->lcr_mcr);
103 tmp &= ~LCR_MASK;
104 tmp |= UART_LCR_WLEN8 << LCR_SHIFT;
105 writel(tmp, &port->lcr_mcr);
106
Masahiro Yamadad064cbf2014-10-23 22:26:10 +0900107 return 0;
108}
109
Masahiro Yamadad9bc8fd2014-10-24 17:00:11 +0900110static int uniphier_serial_remove(struct udevice *dev)
Masahiro Yamadad064cbf2014-10-23 22:26:10 +0900111{
112 unmap_sysmem(uniphier_serial_port(dev));
113
114 return 0;
115}
116
117#ifdef CONFIG_OF_CONTROL
Masahiro Yamada625177d2014-11-26 18:34:00 +0900118static const struct udevice_id uniphier_uart_of_match[] = {
Masahiro Yamada6462cde2015-03-11 15:54:46 +0900119 { .compatible = "socionext,uniphier-uart" },
120 { /* sentinel */ }
Masahiro Yamada7f368552014-10-03 19:21:05 +0900121};
122
Masahiro Yamadad064cbf2014-10-23 22:26:10 +0900123static int uniphier_serial_ofdata_to_platdata(struct udevice *dev)
Masahiro Yamada7f368552014-10-03 19:21:05 +0900124{
Masahiro Yamada625177d2014-11-26 18:34:00 +0900125 struct uniphier_serial_platform_data *plat = dev_get_platdata(dev);
126 DECLARE_GLOBAL_DATA_PTR;
127
128 plat->base = fdtdec_get_addr(gd->fdt_blob, dev->of_offset, "reg");
129 plat->uartclk = fdtdec_get_int(gd->fdt_blob, dev->of_offset,
130 "clock-frequency", 0);
131
132 return 0;
Masahiro Yamada7f368552014-10-03 19:21:05 +0900133}
Masahiro Yamadad064cbf2014-10-23 22:26:10 +0900134#endif
Masahiro Yamada7f368552014-10-03 19:21:05 +0900135
Masahiro Yamadad064cbf2014-10-23 22:26:10 +0900136static const struct dm_serial_ops uniphier_serial_ops = {
137 .setbrg = uniphier_serial_setbrg,
138 .getc = uniphier_serial_getc,
139 .putc = uniphier_serial_putc,
Masahiro Yamadabb721482014-10-24 17:00:10 +0900140 .pending = uniphier_serial_pending,
Masahiro Yamadad064cbf2014-10-23 22:26:10 +0900141};
142
143U_BOOT_DRIVER(uniphier_serial) = {
144 .name = DRIVER_NAME,
145 .id = UCLASS_SERIAL,
146 .of_match = of_match_ptr(uniphier_uart_of_match),
147 .ofdata_to_platdata = of_match_ptr(uniphier_serial_ofdata_to_platdata),
148 .probe = uniphier_serial_probe,
149 .remove = uniphier_serial_remove,
150 .priv_auto_alloc_size = sizeof(struct uniphier_serial_private_data),
151 .platdata_auto_alloc_size =
152 sizeof(struct uniphier_serial_platform_data),
153 .ops = &uniphier_serial_ops,
154 .flags = DM_FLAG_PRE_RELOC,
155};