blob: 74547eb692b19eedfbe9de2aabb30aafaebe3595 [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>
Joe Hershberger0eb25b62015-03-22 17:08:59 -050014#include <mapmem.h>
Masahiro Yamada7f368552014-10-03 19:21:05 +090015#include <serial.h>
Masahiro Yamada625177d2014-11-26 18:34:00 +090016#include <fdtdec.h>
Masahiro Yamada7f368552014-10-03 19:21:05 +090017
Masahiro Yamada7f368552014-10-03 19:21:05 +090018/*
19 * Note: Register map is slightly different from that of 16550.
20 */
21struct uniphier_serial {
Masahiro Yamadad0c47b32015-02-27 02:26:46 +090022 u32 rx; /* In: Receive buffer */
23#define tx rx /* Out: Transmit buffer */
24 u32 ier; /* Interrupt Enable Register */
25 u32 iir; /* In: Interrupt ID Register */
26 u32 char_fcr; /* Charactor / FIFO Control Register */
27 u32 lcr_mcr; /* Line/Modem Control Register */
28#define LCR_SHIFT 8
29#define LCR_MASK (0xff << (LCR_SHIFT))
30 u32 lsr; /* In: Line Status Register */
31 u32 msr; /* In: Modem Status Register */
32 u32 __rsv0;
33 u32 __rsv1;
34 u32 dlr; /* Divisor Latch Register */
Masahiro Yamada7f368552014-10-03 19:21:05 +090035};
36
Masahiro Yamadad064cbf2014-10-23 22:26:10 +090037struct uniphier_serial_private_data {
38 struct uniphier_serial __iomem *membase;
39};
Masahiro Yamada7f368552014-10-03 19:21:05 +090040
Masahiro Yamadad064cbf2014-10-23 22:26:10 +090041#define uniphier_serial_port(dev) \
42 ((struct uniphier_serial_private_data *)dev_get_priv(dev))->membase
43
Masahiro Yamadad9bc8fd2014-10-24 17:00:11 +090044static int uniphier_serial_setbrg(struct udevice *dev, int baudrate)
Masahiro Yamada7f368552014-10-03 19:21:05 +090045{
Masahiro Yamadad064cbf2014-10-23 22:26:10 +090046 struct uniphier_serial_platform_data *plat = dev_get_platdata(dev);
47 struct uniphier_serial __iomem *port = uniphier_serial_port(dev);
Masahiro Yamada7f368552014-10-03 19:21:05 +090048 const unsigned int mode_x_div = 16;
49 unsigned int divisor;
Masahiro Yamada7f368552014-10-03 19:21:05 +090050
Masahiro Yamadad064cbf2014-10-23 22:26:10 +090051 divisor = DIV_ROUND_CLOSEST(plat->uartclk, mode_x_div * baudrate);
Masahiro Yamada7f368552014-10-03 19:21:05 +090052
Masahiro Yamadad0c47b32015-02-27 02:26:46 +090053 writel(divisor, &port->dlr);
Masahiro Yamadad064cbf2014-10-23 22:26:10 +090054
55 return 0;
Masahiro Yamada7f368552014-10-03 19:21:05 +090056}
57
Masahiro Yamadad064cbf2014-10-23 22:26:10 +090058static int uniphier_serial_getc(struct udevice *dev)
Masahiro Yamada7f368552014-10-03 19:21:05 +090059{
Masahiro Yamadad064cbf2014-10-23 22:26:10 +090060 struct uniphier_serial __iomem *port = uniphier_serial_port(dev);
Masahiro Yamada7f368552014-10-03 19:21:05 +090061
Masahiro Yamadad0c47b32015-02-27 02:26:46 +090062 if (!(readl(&port->lsr) & UART_LSR_DR))
Masahiro Yamadad064cbf2014-10-23 22:26:10 +090063 return -EAGAIN;
Masahiro Yamada7f368552014-10-03 19:21:05 +090064
Masahiro Yamadad0c47b32015-02-27 02:26:46 +090065 return readl(&port->rx);
Masahiro Yamada7f368552014-10-03 19:21:05 +090066}
67
Masahiro Yamadad064cbf2014-10-23 22:26:10 +090068static int uniphier_serial_putc(struct udevice *dev, const char c)
Masahiro Yamada7f368552014-10-03 19:21:05 +090069{
Masahiro Yamadad064cbf2014-10-23 22:26:10 +090070 struct uniphier_serial __iomem *port = uniphier_serial_port(dev);
Masahiro Yamada7f368552014-10-03 19:21:05 +090071
Masahiro Yamadad0c47b32015-02-27 02:26:46 +090072 if (!(readl(&port->lsr) & UART_LSR_THRE))
Masahiro Yamadad064cbf2014-10-23 22:26:10 +090073 return -EAGAIN;
Masahiro Yamada7f368552014-10-03 19:21:05 +090074
Masahiro Yamadad0c47b32015-02-27 02:26:46 +090075 writel(c, &port->tx);
Masahiro Yamadad064cbf2014-10-23 22:26:10 +090076
77 return 0;
Masahiro Yamada7f368552014-10-03 19:21:05 +090078}
79
Masahiro Yamadabb721482014-10-24 17:00:10 +090080static int uniphier_serial_pending(struct udevice *dev, bool input)
81{
82 struct uniphier_serial __iomem *port = uniphier_serial_port(dev);
83
84 if (input)
Masahiro Yamadad0c47b32015-02-27 02:26:46 +090085 return readl(&port->lsr) & UART_LSR_DR;
Masahiro Yamadabb721482014-10-24 17:00:10 +090086 else
Masahiro Yamadad0c47b32015-02-27 02:26:46 +090087 return !(readl(&port->lsr) & UART_LSR_THRE);
Masahiro Yamadabb721482014-10-24 17:00:10 +090088}
89
Masahiro Yamadad9bc8fd2014-10-24 17:00:11 +090090static int uniphier_serial_probe(struct udevice *dev)
Masahiro Yamadad064cbf2014-10-23 22:26:10 +090091{
Masahiro Yamada099cf772015-02-27 02:26:47 +090092 u32 tmp;
Masahiro Yamadad064cbf2014-10-23 22:26:10 +090093 struct uniphier_serial_private_data *priv = dev_get_priv(dev);
94 struct uniphier_serial_platform_data *plat = dev_get_platdata(dev);
Masahiro Yamada099cf772015-02-27 02:26:47 +090095 struct uniphier_serial __iomem *port;
Masahiro Yamadad064cbf2014-10-23 22:26:10 +090096
Masahiro Yamada099cf772015-02-27 02:26:47 +090097 port = map_sysmem(plat->base, sizeof(struct uniphier_serial));
98 if (!port)
Masahiro Yamadad064cbf2014-10-23 22:26:10 +090099 return -ENOMEM;
100
Masahiro Yamada099cf772015-02-27 02:26:47 +0900101 priv->membase = port;
102
103 tmp = readl(&port->lcr_mcr);
104 tmp &= ~LCR_MASK;
105 tmp |= UART_LCR_WLEN8 << LCR_SHIFT;
106 writel(tmp, &port->lcr_mcr);
107
Masahiro Yamadad064cbf2014-10-23 22:26:10 +0900108 return 0;
109}
110
Masahiro Yamadad9bc8fd2014-10-24 17:00:11 +0900111static int uniphier_serial_remove(struct udevice *dev)
Masahiro Yamadad064cbf2014-10-23 22:26:10 +0900112{
113 unmap_sysmem(uniphier_serial_port(dev));
114
115 return 0;
116}
117
118#ifdef CONFIG_OF_CONTROL
Masahiro Yamada625177d2014-11-26 18:34:00 +0900119static const struct udevice_id uniphier_uart_of_match[] = {
Masahiro Yamada6462cde2015-03-11 15:54:46 +0900120 { .compatible = "socionext,uniphier-uart" },
121 { /* sentinel */ }
Masahiro Yamada7f368552014-10-03 19:21:05 +0900122};
123
Masahiro Yamadad064cbf2014-10-23 22:26:10 +0900124static int uniphier_serial_ofdata_to_platdata(struct udevice *dev)
Masahiro Yamada7f368552014-10-03 19:21:05 +0900125{
Masahiro Yamada625177d2014-11-26 18:34:00 +0900126 struct uniphier_serial_platform_data *plat = dev_get_platdata(dev);
127 DECLARE_GLOBAL_DATA_PTR;
128
129 plat->base = fdtdec_get_addr(gd->fdt_blob, dev->of_offset, "reg");
130 plat->uartclk = fdtdec_get_int(gd->fdt_blob, dev->of_offset,
131 "clock-frequency", 0);
132
133 return 0;
Masahiro Yamada7f368552014-10-03 19:21:05 +0900134}
Masahiro Yamadad064cbf2014-10-23 22:26:10 +0900135#endif
Masahiro Yamada7f368552014-10-03 19:21:05 +0900136
Masahiro Yamadad064cbf2014-10-23 22:26:10 +0900137static const struct dm_serial_ops uniphier_serial_ops = {
138 .setbrg = uniphier_serial_setbrg,
139 .getc = uniphier_serial_getc,
140 .putc = uniphier_serial_putc,
Masahiro Yamadabb721482014-10-24 17:00:10 +0900141 .pending = uniphier_serial_pending,
Masahiro Yamadad064cbf2014-10-23 22:26:10 +0900142};
143
144U_BOOT_DRIVER(uniphier_serial) = {
145 .name = DRIVER_NAME,
146 .id = UCLASS_SERIAL,
147 .of_match = of_match_ptr(uniphier_uart_of_match),
148 .ofdata_to_platdata = of_match_ptr(uniphier_serial_ofdata_to_platdata),
149 .probe = uniphier_serial_probe,
150 .remove = uniphier_serial_remove,
151 .priv_auto_alloc_size = sizeof(struct uniphier_serial_private_data),
152 .platdata_auto_alloc_size =
153 sizeof(struct uniphier_serial_platform_data),
154 .ops = &uniphier_serial_ops,
155 .flags = DM_FLAG_PRE_RELOC,
156};