blob: 98e3b812e0b2a0fe44094acfc6036cdac14d490f [file] [log] [blame]
Masahiro Yamadab2890732014-10-03 19:21:05 +09001/*
Masahiro Yamadae7c3a892015-02-27 02:26:46 +09002 * Copyright (C) 2012-2015 Panasonic Corporation
Masahiro Yamadad5f83a42015-03-11 15:54:46 +09003 * Copyright (C) 2015 Socionext Inc.
4 * Author: Masahiro Yamada <yamada.masahiro@socionext.com>
Masahiro Yamadab2890732014-10-03 19:21:05 +09005 *
Masahiro Yamadab2890732014-10-03 19:21:05 +09006 * SPDX-License-Identifier: GPL-2.0+
7 */
8
Masahiro Yamada9caae5e2014-10-30 12:11:14 +09009#include <linux/serial_reg.h>
Masahiro Yamada50c12542014-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 Yamadab2890732014-10-03 19:21:05 +090014#include <serial.h>
Masahiro Yamadaf8cd9b02014-11-26 18:34:00 +090015#include <fdtdec.h>
Masahiro Yamadab2890732014-10-03 19:21:05 +090016
Masahiro Yamadab2890732014-10-03 19:21:05 +090017/*
18 * Note: Register map is slightly different from that of 16550.
19 */
20struct uniphier_serial {
Masahiro Yamadae7c3a892015-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 Yamadab2890732014-10-03 19:21:05 +090034};
35
Masahiro Yamada50c12542014-10-23 22:26:10 +090036struct uniphier_serial_private_data {
37 struct uniphier_serial __iomem *membase;
38};
39
40#define uniphier_serial_port(dev) \
41 ((struct uniphier_serial_private_data *)dev_get_priv(dev))->membase
Masahiro Yamadab2890732014-10-03 19:21:05 +090042
Masahiro Yamada0bf0c6d2014-10-24 17:00:11 +090043static int uniphier_serial_setbrg(struct udevice *dev, int baudrate)
Masahiro Yamadab2890732014-10-03 19:21:05 +090044{
Masahiro Yamada50c12542014-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 Yamadab2890732014-10-03 19:21:05 +090047 const unsigned int mode_x_div = 16;
48 unsigned int divisor;
Masahiro Yamadab2890732014-10-03 19:21:05 +090049
Masahiro Yamada50c12542014-10-23 22:26:10 +090050 divisor = DIV_ROUND_CLOSEST(plat->uartclk, mode_x_div * baudrate);
Masahiro Yamadab2890732014-10-03 19:21:05 +090051
Masahiro Yamadae7c3a892015-02-27 02:26:46 +090052 writel(divisor, &port->dlr);
Masahiro Yamadab2890732014-10-03 19:21:05 +090053
Masahiro Yamada50c12542014-10-23 22:26:10 +090054 return 0;
Masahiro Yamadab2890732014-10-03 19:21:05 +090055}
56
Masahiro Yamada50c12542014-10-23 22:26:10 +090057static int uniphier_serial_getc(struct udevice *dev)
Masahiro Yamadab2890732014-10-03 19:21:05 +090058{
Masahiro Yamada50c12542014-10-23 22:26:10 +090059 struct uniphier_serial __iomem *port = uniphier_serial_port(dev);
Masahiro Yamadab2890732014-10-03 19:21:05 +090060
Masahiro Yamadae7c3a892015-02-27 02:26:46 +090061 if (!(readl(&port->lsr) & UART_LSR_DR))
Masahiro Yamada50c12542014-10-23 22:26:10 +090062 return -EAGAIN;
Masahiro Yamadab2890732014-10-03 19:21:05 +090063
Masahiro Yamadae7c3a892015-02-27 02:26:46 +090064 return readl(&port->rx);
Masahiro Yamadab2890732014-10-03 19:21:05 +090065}
66
Masahiro Yamada50c12542014-10-23 22:26:10 +090067static int uniphier_serial_putc(struct udevice *dev, const char c)
Masahiro Yamadab2890732014-10-03 19:21:05 +090068{
Masahiro Yamada50c12542014-10-23 22:26:10 +090069 struct uniphier_serial __iomem *port = uniphier_serial_port(dev);
Masahiro Yamadab2890732014-10-03 19:21:05 +090070
Masahiro Yamadae7c3a892015-02-27 02:26:46 +090071 if (!(readl(&port->lsr) & UART_LSR_THRE))
Masahiro Yamada50c12542014-10-23 22:26:10 +090072 return -EAGAIN;
Masahiro Yamadab2890732014-10-03 19:21:05 +090073
Masahiro Yamadae7c3a892015-02-27 02:26:46 +090074 writel(c, &port->tx);
Masahiro Yamada50c12542014-10-23 22:26:10 +090075
76 return 0;
Masahiro Yamadab2890732014-10-03 19:21:05 +090077}
78
Masahiro Yamadad11fbd52014-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 Yamadae7c3a892015-02-27 02:26:46 +090084 return readl(&port->lsr) & UART_LSR_DR;
Masahiro Yamadad11fbd52014-10-24 17:00:10 +090085 else
Masahiro Yamadae7c3a892015-02-27 02:26:46 +090086 return !(readl(&port->lsr) & UART_LSR_THRE);
Masahiro Yamadad11fbd52014-10-24 17:00:10 +090087}
88
Masahiro Yamada0bf0c6d2014-10-24 17:00:11 +090089static int uniphier_serial_probe(struct udevice *dev)
Masahiro Yamada50c12542014-10-23 22:26:10 +090090{
Masahiro Yamadafef7fcd2015-02-27 02:26:47 +090091 u32 tmp;
Masahiro Yamada50c12542014-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 Yamadafef7fcd2015-02-27 02:26:47 +090094 struct uniphier_serial __iomem *port;
Masahiro Yamadab2890732014-10-03 19:21:05 +090095
Masahiro Yamadafef7fcd2015-02-27 02:26:47 +090096 port = map_sysmem(plat->base, sizeof(struct uniphier_serial));
97 if (!port)
Masahiro Yamada50c12542014-10-23 22:26:10 +090098 return -ENOMEM;
Masahiro Yamadab2890732014-10-03 19:21:05 +090099
Masahiro Yamadafef7fcd2015-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 Yamada50c12542014-10-23 22:26:10 +0900107 return 0;
108}
Masahiro Yamadab2890732014-10-03 19:21:05 +0900109
Masahiro Yamada0bf0c6d2014-10-24 17:00:11 +0900110static int uniphier_serial_remove(struct udevice *dev)
Masahiro Yamadab2890732014-10-03 19:21:05 +0900111{
Masahiro Yamada50c12542014-10-23 22:26:10 +0900112 unmap_sysmem(uniphier_serial_port(dev));
113
114 return 0;
Masahiro Yamadab2890732014-10-03 19:21:05 +0900115}
116
Masahiro Yamada50c12542014-10-23 22:26:10 +0900117#ifdef CONFIG_OF_CONTROL
Masahiro Yamadaf8cd9b02014-11-26 18:34:00 +0900118static const struct udevice_id uniphier_uart_of_match[] = {
Masahiro Yamadad5f83a42015-03-11 15:54:46 +0900119 { .compatible = "socionext,uniphier-uart" },
120 { /* sentinel */ }
Masahiro Yamada50c12542014-10-23 22:26:10 +0900121};
122
123static int uniphier_serial_ofdata_to_platdata(struct udevice *dev)
Masahiro Yamadab2890732014-10-03 19:21:05 +0900124{
Masahiro Yamadaf8cd9b02014-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 Yamadab2890732014-10-03 19:21:05 +0900133}
Masahiro Yamada50c12542014-10-23 22:26:10 +0900134#endif
135
136static const struct dm_serial_ops uniphier_serial_ops = {
137 .setbrg = uniphier_serial_setbrg,
138 .getc = uniphier_serial_getc,
139 .putc = uniphier_serial_putc,
Masahiro Yamadad11fbd52014-10-24 17:00:10 +0900140 .pending = uniphier_serial_pending,
Masahiro Yamada50c12542014-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};