blob: edb9203858814bd41f8d2460c3c702e59137b5d6 [file] [log] [blame]
Masahiro Yamada7f368552014-10-03 19:21:05 +09001/*
Masahiro Yamadaf6e7f072015-05-29 17:30:00 +09002 * Copyright (C) 2012-2015 Masahiro Yamada <yamada.masahiro@socionext.com>
Masahiro Yamada7f368552014-10-03 19:21:05 +09003 *
Masahiro Yamada7f368552014-10-03 19:21:05 +09004 * SPDX-License-Identifier: GPL-2.0+
5 */
6
Masahiro Yamadaf6e7f072015-05-29 17:30:00 +09007#include <linux/io.h>
Masahiro Yamada325b7082014-10-30 12:11:14 +09008#include <linux/serial_reg.h>
Masahiro Yamadad064cbf2014-10-23 22:26:10 +09009#include <asm/errno.h>
10#include <dm/device.h>
Joe Hershberger0eb25b62015-03-22 17:08:59 -050011#include <mapmem.h>
Masahiro Yamada7f368552014-10-03 19:21:05 +090012#include <serial.h>
Masahiro Yamada625177d2014-11-26 18:34:00 +090013#include <fdtdec.h>
Masahiro Yamada7f368552014-10-03 19:21:05 +090014
Masahiro Yamada7f368552014-10-03 19:21:05 +090015/*
16 * Note: Register map is slightly different from that of 16550.
17 */
18struct uniphier_serial {
Masahiro Yamadad0c47b32015-02-27 02:26:46 +090019 u32 rx; /* In: Receive buffer */
20#define tx rx /* Out: Transmit buffer */
21 u32 ier; /* Interrupt Enable Register */
22 u32 iir; /* In: Interrupt ID Register */
23 u32 char_fcr; /* Charactor / FIFO Control Register */
24 u32 lcr_mcr; /* Line/Modem Control Register */
25#define LCR_SHIFT 8
26#define LCR_MASK (0xff << (LCR_SHIFT))
27 u32 lsr; /* In: Line Status Register */
28 u32 msr; /* In: Modem Status Register */
29 u32 __rsv0;
30 u32 __rsv1;
31 u32 dlr; /* Divisor Latch Register */
Masahiro Yamada7f368552014-10-03 19:21:05 +090032};
33
Masahiro Yamadad064cbf2014-10-23 22:26:10 +090034struct uniphier_serial_private_data {
35 struct uniphier_serial __iomem *membase;
Masahiro Yamada6d99cfa2015-08-28 20:13:19 +090036 unsigned int uartclk;
Masahiro Yamadad064cbf2014-10-23 22:26:10 +090037};
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 Yamada6d99cfa2015-08-28 20:13:19 +090044 struct uniphier_serial_private_data *priv = dev_get_priv(dev);
Masahiro Yamadad064cbf2014-10-23 22:26:10 +090045 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 Yamada6d99cfa2015-08-28 20:13:19 +090049 divisor = DIV_ROUND_CLOSEST(priv->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 Yamada6d99cfa2015-08-28 20:13:19 +090090 DECLARE_GLOBAL_DATA_PTR;
Masahiro Yamadad064cbf2014-10-23 22:26:10 +090091 struct uniphier_serial_private_data *priv = dev_get_priv(dev);
Masahiro Yamada099cf772015-02-27 02:26:47 +090092 struct uniphier_serial __iomem *port;
Masahiro Yamada6d99cfa2015-08-28 20:13:19 +090093 fdt_addr_t base;
94 fdt_size_t size;
95 u32 tmp;
Masahiro Yamadad064cbf2014-10-23 22:26:10 +090096
Masahiro Yamada6d99cfa2015-08-28 20:13:19 +090097 base = fdtdec_get_addr_size(gd->fdt_blob, dev->of_offset, "reg", &size);
98
99 port = map_sysmem(base, size);
Masahiro Yamada099cf772015-02-27 02:26:47 +0900100 if (!port)
Masahiro Yamadad064cbf2014-10-23 22:26:10 +0900101 return -ENOMEM;
102
Masahiro Yamada099cf772015-02-27 02:26:47 +0900103 priv->membase = port;
104
Masahiro Yamada6d99cfa2015-08-28 20:13:19 +0900105 priv->uartclk = fdtdec_get_int(gd->fdt_blob, dev->of_offset,
106 "clock-frequency", 0);
107
Masahiro Yamada099cf772015-02-27 02:26:47 +0900108 tmp = readl(&port->lcr_mcr);
109 tmp &= ~LCR_MASK;
110 tmp |= UART_LCR_WLEN8 << LCR_SHIFT;
111 writel(tmp, &port->lcr_mcr);
112
Masahiro Yamadad064cbf2014-10-23 22:26:10 +0900113 return 0;
114}
115
Masahiro Yamadad9bc8fd2014-10-24 17:00:11 +0900116static int uniphier_serial_remove(struct udevice *dev)
Masahiro Yamadad064cbf2014-10-23 22:26:10 +0900117{
118 unmap_sysmem(uniphier_serial_port(dev));
119
120 return 0;
121}
122
Masahiro Yamada625177d2014-11-26 18:34:00 +0900123static const struct udevice_id uniphier_uart_of_match[] = {
Masahiro Yamada6462cde2015-03-11 15:54:46 +0900124 { .compatible = "socionext,uniphier-uart" },
125 { /* sentinel */ }
Masahiro Yamada7f368552014-10-03 19:21:05 +0900126};
127
Masahiro Yamadad064cbf2014-10-23 22:26:10 +0900128static const struct dm_serial_ops uniphier_serial_ops = {
129 .setbrg = uniphier_serial_setbrg,
130 .getc = uniphier_serial_getc,
131 .putc = uniphier_serial_putc,
Masahiro Yamadabb721482014-10-24 17:00:10 +0900132 .pending = uniphier_serial_pending,
Masahiro Yamadad064cbf2014-10-23 22:26:10 +0900133};
134
135U_BOOT_DRIVER(uniphier_serial) = {
Masahiro Yamada6d99cfa2015-08-28 20:13:19 +0900136 .name = "uniphier-uart",
Masahiro Yamadad064cbf2014-10-23 22:26:10 +0900137 .id = UCLASS_SERIAL,
Masahiro Yamada6d99cfa2015-08-28 20:13:19 +0900138 .of_match = uniphier_uart_of_match,
Masahiro Yamadad064cbf2014-10-23 22:26:10 +0900139 .probe = uniphier_serial_probe,
140 .remove = uniphier_serial_remove,
141 .priv_auto_alloc_size = sizeof(struct uniphier_serial_private_data),
Masahiro Yamadad064cbf2014-10-23 22:26:10 +0900142 .ops = &uniphier_serial_ops,
Masahiro Yamadad064cbf2014-10-23 22:26:10 +0900143};