blob: a6bd27facffd91f7356f89077df1ad9a6ecbf95c [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 Yamada7f368552014-10-03 19:21:05 +09003 * Author: Masahiro Yamada <yamada.m@jp.panasonic.com>
4 *
Masahiro Yamada7f368552014-10-03 19:21:05 +09005 * SPDX-License-Identifier: GPL-2.0+
6 */
7
Masahiro Yamada325b7082014-10-30 12:11:14 +09008#include <linux/serial_reg.h>
Masahiro Yamadad064cbf2014-10-23 22:26:10 +09009#include <asm/io.h>
10#include <asm/errno.h>
11#include <dm/device.h>
12#include <dm/platform_data/serial-uniphier.h>
Masahiro Yamada7f368552014-10-03 19:21:05 +090013#include <serial.h>
Masahiro Yamada625177d2014-11-26 18:34:00 +090014#include <fdtdec.h>
Masahiro Yamada7f368552014-10-03 19:21:05 +090015
Masahiro Yamada7f368552014-10-03 19:21:05 +090016/*
17 * Note: Register map is slightly different from that of 16550.
18 */
19struct uniphier_serial {
Masahiro Yamadad0c47b32015-02-27 02:26:46 +090020 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 */
26#define LCR_SHIFT 8
27#define LCR_MASK (0xff << (LCR_SHIFT))
28 u32 lsr; /* In: Line Status Register */
29 u32 msr; /* In: Modem Status Register */
30 u32 __rsv0;
31 u32 __rsv1;
32 u32 dlr; /* Divisor Latch Register */
Masahiro Yamada7f368552014-10-03 19:21:05 +090033};
34
Masahiro Yamadad064cbf2014-10-23 22:26:10 +090035struct uniphier_serial_private_data {
36 struct uniphier_serial __iomem *membase;
37};
Masahiro Yamada7f368552014-10-03 19:21:05 +090038
Masahiro Yamadad064cbf2014-10-23 22:26:10 +090039#define uniphier_serial_port(dev) \
40 ((struct uniphier_serial_private_data *)dev_get_priv(dev))->membase
41
Masahiro Yamadad9bc8fd2014-10-24 17:00:11 +090042static int uniphier_serial_setbrg(struct udevice *dev, int baudrate)
Masahiro Yamada7f368552014-10-03 19:21:05 +090043{
Masahiro Yamadad064cbf2014-10-23 22:26:10 +090044 struct uniphier_serial_platform_data *plat = dev_get_platdata(dev);
45 struct uniphier_serial __iomem *port = uniphier_serial_port(dev);
Masahiro Yamada7f368552014-10-03 19:21:05 +090046 const unsigned int mode_x_div = 16;
47 unsigned int divisor;
Masahiro Yamada7f368552014-10-03 19:21:05 +090048
Masahiro Yamadad064cbf2014-10-23 22:26:10 +090049 divisor = DIV_ROUND_CLOSEST(plat->uartclk, mode_x_div * baudrate);
Masahiro Yamada7f368552014-10-03 19:21:05 +090050
Masahiro Yamadad0c47b32015-02-27 02:26:46 +090051 writel(divisor, &port->dlr);
Masahiro Yamadad064cbf2014-10-23 22:26:10 +090052
53 return 0;
Masahiro Yamada7f368552014-10-03 19:21:05 +090054}
55
Masahiro Yamadad064cbf2014-10-23 22:26:10 +090056static int uniphier_serial_getc(struct udevice *dev)
Masahiro Yamada7f368552014-10-03 19:21:05 +090057{
Masahiro Yamadad064cbf2014-10-23 22:26:10 +090058 struct uniphier_serial __iomem *port = uniphier_serial_port(dev);
Masahiro Yamada7f368552014-10-03 19:21:05 +090059
Masahiro Yamadad0c47b32015-02-27 02:26:46 +090060 if (!(readl(&port->lsr) & UART_LSR_DR))
Masahiro Yamadad064cbf2014-10-23 22:26:10 +090061 return -EAGAIN;
Masahiro Yamada7f368552014-10-03 19:21:05 +090062
Masahiro Yamadad0c47b32015-02-27 02:26:46 +090063 return readl(&port->rx);
Masahiro Yamada7f368552014-10-03 19:21:05 +090064}
65
Masahiro Yamadad064cbf2014-10-23 22:26:10 +090066static int uniphier_serial_putc(struct udevice *dev, const char c)
Masahiro Yamada7f368552014-10-03 19:21:05 +090067{
Masahiro Yamadad064cbf2014-10-23 22:26:10 +090068 struct uniphier_serial __iomem *port = uniphier_serial_port(dev);
Masahiro Yamada7f368552014-10-03 19:21:05 +090069
Masahiro Yamadad0c47b32015-02-27 02:26:46 +090070 if (!(readl(&port->lsr) & UART_LSR_THRE))
Masahiro Yamadad064cbf2014-10-23 22:26:10 +090071 return -EAGAIN;
Masahiro Yamada7f368552014-10-03 19:21:05 +090072
Masahiro Yamadad0c47b32015-02-27 02:26:46 +090073 writel(c, &port->tx);
Masahiro Yamadad064cbf2014-10-23 22:26:10 +090074
75 return 0;
Masahiro Yamada7f368552014-10-03 19:21:05 +090076}
77
Masahiro Yamadabb721482014-10-24 17:00:10 +090078static int uniphier_serial_pending(struct udevice *dev, bool input)
79{
80 struct uniphier_serial __iomem *port = uniphier_serial_port(dev);
81
82 if (input)
Masahiro Yamadad0c47b32015-02-27 02:26:46 +090083 return readl(&port->lsr) & UART_LSR_DR;
Masahiro Yamadabb721482014-10-24 17:00:10 +090084 else
Masahiro Yamadad0c47b32015-02-27 02:26:46 +090085 return !(readl(&port->lsr) & UART_LSR_THRE);
Masahiro Yamadabb721482014-10-24 17:00:10 +090086}
87
Masahiro Yamadad9bc8fd2014-10-24 17:00:11 +090088static int uniphier_serial_probe(struct udevice *dev)
Masahiro Yamadad064cbf2014-10-23 22:26:10 +090089{
Masahiro Yamada099cf772015-02-27 02:26:47 +090090 u32 tmp;
Masahiro Yamadad064cbf2014-10-23 22:26:10 +090091 struct uniphier_serial_private_data *priv = dev_get_priv(dev);
92 struct uniphier_serial_platform_data *plat = dev_get_platdata(dev);
Masahiro Yamada099cf772015-02-27 02:26:47 +090093 struct uniphier_serial __iomem *port;
Masahiro Yamadad064cbf2014-10-23 22:26:10 +090094
Masahiro Yamada099cf772015-02-27 02:26:47 +090095 port = map_sysmem(plat->base, sizeof(struct uniphier_serial));
96 if (!port)
Masahiro Yamadad064cbf2014-10-23 22:26:10 +090097 return -ENOMEM;
98
Masahiro Yamada099cf772015-02-27 02:26:47 +090099 priv->membase = port;
100
101 tmp = readl(&port->lcr_mcr);
102 tmp &= ~LCR_MASK;
103 tmp |= UART_LCR_WLEN8 << LCR_SHIFT;
104 writel(tmp, &port->lcr_mcr);
105
Masahiro Yamadad064cbf2014-10-23 22:26:10 +0900106 return 0;
107}
108
Masahiro Yamadad9bc8fd2014-10-24 17:00:11 +0900109static int uniphier_serial_remove(struct udevice *dev)
Masahiro Yamadad064cbf2014-10-23 22:26:10 +0900110{
111 unmap_sysmem(uniphier_serial_port(dev));
112
113 return 0;
114}
115
116#ifdef CONFIG_OF_CONTROL
Masahiro Yamada625177d2014-11-26 18:34:00 +0900117static const struct udevice_id uniphier_uart_of_match[] = {
118 { .compatible = "panasonic,uniphier-uart" },
Masahiro Yamadad064cbf2014-10-23 22:26:10 +0900119 {},
Masahiro Yamada7f368552014-10-03 19:21:05 +0900120};
121
Masahiro Yamadad064cbf2014-10-23 22:26:10 +0900122static int uniphier_serial_ofdata_to_platdata(struct udevice *dev)
Masahiro Yamada7f368552014-10-03 19:21:05 +0900123{
Masahiro Yamada625177d2014-11-26 18:34:00 +0900124 struct uniphier_serial_platform_data *plat = dev_get_platdata(dev);
125 DECLARE_GLOBAL_DATA_PTR;
126
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);
130
131 return 0;
Masahiro Yamada7f368552014-10-03 19:21:05 +0900132}
Masahiro Yamadad064cbf2014-10-23 22:26:10 +0900133#endif
Masahiro Yamada7f368552014-10-03 19:21:05 +0900134
Masahiro Yamadad064cbf2014-10-23 22:26:10 +0900135static const struct dm_serial_ops uniphier_serial_ops = {
136 .setbrg = uniphier_serial_setbrg,
137 .getc = uniphier_serial_getc,
138 .putc = uniphier_serial_putc,
Masahiro Yamadabb721482014-10-24 17:00:10 +0900139 .pending = uniphier_serial_pending,
Masahiro Yamadad064cbf2014-10-23 22:26:10 +0900140};
141
142U_BOOT_DRIVER(uniphier_serial) = {
143 .name = DRIVER_NAME,
144 .id = UCLASS_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,
154};