blob: edb9203858814bd41f8d2460c3c702e59137b5d6 [file] [log] [blame]
Masahiro Yamadab2890732014-10-03 19:21:05 +09001/*
Masahiro Yamada663a23f2015-05-29 17:30:00 +09002 * Copyright (C) 2012-2015 Masahiro Yamada <yamada.masahiro@socionext.com>
Masahiro Yamadab2890732014-10-03 19:21:05 +09003 *
Masahiro Yamadab2890732014-10-03 19:21:05 +09004 * SPDX-License-Identifier: GPL-2.0+
5 */
6
Masahiro Yamada663a23f2015-05-29 17:30:00 +09007#include <linux/io.h>
Masahiro Yamada9caae5e2014-10-30 12:11:14 +09008#include <linux/serial_reg.h>
Masahiro Yamada50c12542014-10-23 22:26:10 +09009#include <asm/errno.h>
10#include <dm/device.h>
Joe Hershberger65b905b2015-03-22 17:08:59 -050011#include <mapmem.h>
Masahiro Yamadab2890732014-10-03 19:21:05 +090012#include <serial.h>
Masahiro Yamadaf8cd9b02014-11-26 18:34:00 +090013#include <fdtdec.h>
Masahiro Yamadab2890732014-10-03 19:21:05 +090014
Masahiro Yamadab2890732014-10-03 19:21:05 +090015/*
16 * Note: Register map is slightly different from that of 16550.
17 */
18struct uniphier_serial {
Masahiro Yamadae7c3a892015-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 Yamadab2890732014-10-03 19:21:05 +090032};
33
Masahiro Yamada50c12542014-10-23 22:26:10 +090034struct uniphier_serial_private_data {
35 struct uniphier_serial __iomem *membase;
Masahiro Yamada25318dc2015-08-28 20:13:19 +090036 unsigned int uartclk;
Masahiro Yamada50c12542014-10-23 22:26:10 +090037};
38
39#define uniphier_serial_port(dev) \
40 ((struct uniphier_serial_private_data *)dev_get_priv(dev))->membase
Masahiro Yamadab2890732014-10-03 19:21:05 +090041
Masahiro Yamada0bf0c6d2014-10-24 17:00:11 +090042static int uniphier_serial_setbrg(struct udevice *dev, int baudrate)
Masahiro Yamadab2890732014-10-03 19:21:05 +090043{
Masahiro Yamada25318dc2015-08-28 20:13:19 +090044 struct uniphier_serial_private_data *priv = dev_get_priv(dev);
Masahiro Yamada50c12542014-10-23 22:26:10 +090045 struct uniphier_serial __iomem *port = uniphier_serial_port(dev);
Masahiro Yamadab2890732014-10-03 19:21:05 +090046 const unsigned int mode_x_div = 16;
47 unsigned int divisor;
Masahiro Yamadab2890732014-10-03 19:21:05 +090048
Masahiro Yamada25318dc2015-08-28 20:13:19 +090049 divisor = DIV_ROUND_CLOSEST(priv->uartclk, mode_x_div * baudrate);
Masahiro Yamadab2890732014-10-03 19:21:05 +090050
Masahiro Yamadae7c3a892015-02-27 02:26:46 +090051 writel(divisor, &port->dlr);
Masahiro Yamadab2890732014-10-03 19:21:05 +090052
Masahiro Yamada50c12542014-10-23 22:26:10 +090053 return 0;
Masahiro Yamadab2890732014-10-03 19:21:05 +090054}
55
Masahiro Yamada50c12542014-10-23 22:26:10 +090056static int uniphier_serial_getc(struct udevice *dev)
Masahiro Yamadab2890732014-10-03 19:21:05 +090057{
Masahiro Yamada50c12542014-10-23 22:26:10 +090058 struct uniphier_serial __iomem *port = uniphier_serial_port(dev);
Masahiro Yamadab2890732014-10-03 19:21:05 +090059
Masahiro Yamadae7c3a892015-02-27 02:26:46 +090060 if (!(readl(&port->lsr) & UART_LSR_DR))
Masahiro Yamada50c12542014-10-23 22:26:10 +090061 return -EAGAIN;
Masahiro Yamadab2890732014-10-03 19:21:05 +090062
Masahiro Yamadae7c3a892015-02-27 02:26:46 +090063 return readl(&port->rx);
Masahiro Yamadab2890732014-10-03 19:21:05 +090064}
65
Masahiro Yamada50c12542014-10-23 22:26:10 +090066static int uniphier_serial_putc(struct udevice *dev, const char c)
Masahiro Yamadab2890732014-10-03 19:21:05 +090067{
Masahiro Yamada50c12542014-10-23 22:26:10 +090068 struct uniphier_serial __iomem *port = uniphier_serial_port(dev);
Masahiro Yamadab2890732014-10-03 19:21:05 +090069
Masahiro Yamadae7c3a892015-02-27 02:26:46 +090070 if (!(readl(&port->lsr) & UART_LSR_THRE))
Masahiro Yamada50c12542014-10-23 22:26:10 +090071 return -EAGAIN;
Masahiro Yamadab2890732014-10-03 19:21:05 +090072
Masahiro Yamadae7c3a892015-02-27 02:26:46 +090073 writel(c, &port->tx);
Masahiro Yamada50c12542014-10-23 22:26:10 +090074
75 return 0;
Masahiro Yamadab2890732014-10-03 19:21:05 +090076}
77
Masahiro Yamadad11fbd52014-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 Yamadae7c3a892015-02-27 02:26:46 +090083 return readl(&port->lsr) & UART_LSR_DR;
Masahiro Yamadad11fbd52014-10-24 17:00:10 +090084 else
Masahiro Yamadae7c3a892015-02-27 02:26:46 +090085 return !(readl(&port->lsr) & UART_LSR_THRE);
Masahiro Yamadad11fbd52014-10-24 17:00:10 +090086}
87
Masahiro Yamada0bf0c6d2014-10-24 17:00:11 +090088static int uniphier_serial_probe(struct udevice *dev)
Masahiro Yamada50c12542014-10-23 22:26:10 +090089{
Masahiro Yamada25318dc2015-08-28 20:13:19 +090090 DECLARE_GLOBAL_DATA_PTR;
Masahiro Yamada50c12542014-10-23 22:26:10 +090091 struct uniphier_serial_private_data *priv = dev_get_priv(dev);
Masahiro Yamadafef7fcd2015-02-27 02:26:47 +090092 struct uniphier_serial __iomem *port;
Masahiro Yamada25318dc2015-08-28 20:13:19 +090093 fdt_addr_t base;
94 fdt_size_t size;
95 u32 tmp;
Masahiro Yamadab2890732014-10-03 19:21:05 +090096
Masahiro Yamada25318dc2015-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 Yamadafef7fcd2015-02-27 02:26:47 +0900100 if (!port)
Masahiro Yamada50c12542014-10-23 22:26:10 +0900101 return -ENOMEM;
Masahiro Yamadab2890732014-10-03 19:21:05 +0900102
Masahiro Yamadafef7fcd2015-02-27 02:26:47 +0900103 priv->membase = port;
104
Masahiro Yamada25318dc2015-08-28 20:13:19 +0900105 priv->uartclk = fdtdec_get_int(gd->fdt_blob, dev->of_offset,
106 "clock-frequency", 0);
107
Masahiro Yamadafef7fcd2015-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 Yamada50c12542014-10-23 22:26:10 +0900113 return 0;
114}
Masahiro Yamadab2890732014-10-03 19:21:05 +0900115
Masahiro Yamada0bf0c6d2014-10-24 17:00:11 +0900116static int uniphier_serial_remove(struct udevice *dev)
Masahiro Yamadab2890732014-10-03 19:21:05 +0900117{
Masahiro Yamada50c12542014-10-23 22:26:10 +0900118 unmap_sysmem(uniphier_serial_port(dev));
119
120 return 0;
Masahiro Yamadab2890732014-10-03 19:21:05 +0900121}
122
Masahiro Yamadaf8cd9b02014-11-26 18:34:00 +0900123static const struct udevice_id uniphier_uart_of_match[] = {
Masahiro Yamadad5f83a42015-03-11 15:54:46 +0900124 { .compatible = "socionext,uniphier-uart" },
125 { /* sentinel */ }
Masahiro Yamada50c12542014-10-23 22:26:10 +0900126};
127
Masahiro Yamada50c12542014-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 Yamadad11fbd52014-10-24 17:00:10 +0900132 .pending = uniphier_serial_pending,
Masahiro Yamada50c12542014-10-23 22:26:10 +0900133};
134
135U_BOOT_DRIVER(uniphier_serial) = {
Masahiro Yamada25318dc2015-08-28 20:13:19 +0900136 .name = "uniphier-uart",
Masahiro Yamada50c12542014-10-23 22:26:10 +0900137 .id = UCLASS_SERIAL,
Masahiro Yamada25318dc2015-08-28 20:13:19 +0900138 .of_match = uniphier_uart_of_match,
Masahiro Yamada50c12542014-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 Yamada50c12542014-10-23 22:26:10 +0900142 .ops = &uniphier_serial_ops,
Masahiro Yamada50c12542014-10-23 22:26:10 +0900143};