blob: c7f46e5598116db68188eb7a925f87786db913bb [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Masahiro Yamadab2890732014-10-03 19:21:05 +09002/*
Masahiro Yamadafa1f73f2016-07-19 21:56:13 +09003 * Copyright (C) 2012-2015 Panasonic Corporation
4 * Copyright (C) 2015-2016 Socionext Inc.
5 * Author: Masahiro Yamada <yamada.masahiro@socionext.com>
Masahiro Yamadab2890732014-10-03 19:21:05 +09006 */
7
Simon Glass51a3ec32017-05-17 17:18:07 -06008#include <common.h>
Simon Glass11c89f32017-05-17 17:18:03 -06009#include <dm.h>
Masahiro Yamada66f4a9a2018-06-19 16:11:45 +090010#include <linux/bug.h>
Masahiro Yamada663a23f2015-05-29 17:30:00 +090011#include <linux/io.h>
Masahiro Yamada9caae5e2014-10-30 12:11:14 +090012#include <linux/serial_reg.h>
Masahiro Yamada6ee016e2016-03-24 22:32:38 +090013#include <linux/sizes.h>
Masahiro Yamada56a931c2016-09-21 11:28:55 +090014#include <linux/errno.h>
Masahiro Yamadab2890732014-10-03 19:21:05 +090015#include <serial.h>
Masahiro Yamadaf8cd9b02014-11-26 18:34:00 +090016#include <fdtdec.h>
Masahiro Yamadab2890732014-10-03 19:21:05 +090017
Masahiro Yamadab2890732014-10-03 19:21:05 +090018/*
19 * Note: Register map is slightly different from that of 16550.
20 */
21struct uniphier_serial {
Masahiro Yamadae7c3a892015-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 Yamadab2890732014-10-03 19:21:05 +090035};
36
Masahiro Yamada8b5b1ee2018-06-19 16:11:44 +090037struct uniphier_serial_priv {
Masahiro Yamada50c12542014-10-23 22:26:10 +090038 struct uniphier_serial __iomem *membase;
Masahiro Yamada25318dc2015-08-28 20:13:19 +090039 unsigned int uartclk;
Masahiro Yamada50c12542014-10-23 22:26:10 +090040};
41
42#define uniphier_serial_port(dev) \
Masahiro Yamada8b5b1ee2018-06-19 16:11:44 +090043 ((struct uniphier_serial_priv *)dev_get_priv(dev))->membase
Masahiro Yamadab2890732014-10-03 19:21:05 +090044
Masahiro Yamada0bf0c6d2014-10-24 17:00:11 +090045static int uniphier_serial_setbrg(struct udevice *dev, int baudrate)
Masahiro Yamadab2890732014-10-03 19:21:05 +090046{
Masahiro Yamada8b5b1ee2018-06-19 16:11:44 +090047 struct uniphier_serial_priv *priv = dev_get_priv(dev);
Masahiro Yamada50c12542014-10-23 22:26:10 +090048 struct uniphier_serial __iomem *port = uniphier_serial_port(dev);
Masahiro Yamadab2890732014-10-03 19:21:05 +090049 const unsigned int mode_x_div = 16;
50 unsigned int divisor;
Masahiro Yamadab2890732014-10-03 19:21:05 +090051
Masahiro Yamada25318dc2015-08-28 20:13:19 +090052 divisor = DIV_ROUND_CLOSEST(priv->uartclk, mode_x_div * baudrate);
Masahiro Yamadab2890732014-10-03 19:21:05 +090053
Masahiro Yamadae7c3a892015-02-27 02:26:46 +090054 writel(divisor, &port->dlr);
Masahiro Yamadab2890732014-10-03 19:21:05 +090055
Masahiro Yamada50c12542014-10-23 22:26:10 +090056 return 0;
Masahiro Yamadab2890732014-10-03 19:21:05 +090057}
58
Masahiro Yamada50c12542014-10-23 22:26:10 +090059static int uniphier_serial_getc(struct udevice *dev)
Masahiro Yamadab2890732014-10-03 19:21:05 +090060{
Masahiro Yamada50c12542014-10-23 22:26:10 +090061 struct uniphier_serial __iomem *port = uniphier_serial_port(dev);
Masahiro Yamadab2890732014-10-03 19:21:05 +090062
Masahiro Yamadae7c3a892015-02-27 02:26:46 +090063 if (!(readl(&port->lsr) & UART_LSR_DR))
Masahiro Yamada50c12542014-10-23 22:26:10 +090064 return -EAGAIN;
Masahiro Yamadab2890732014-10-03 19:21:05 +090065
Masahiro Yamadae7c3a892015-02-27 02:26:46 +090066 return readl(&port->rx);
Masahiro Yamadab2890732014-10-03 19:21:05 +090067}
68
Masahiro Yamada50c12542014-10-23 22:26:10 +090069static int uniphier_serial_putc(struct udevice *dev, const char c)
Masahiro Yamadab2890732014-10-03 19:21:05 +090070{
Masahiro Yamada50c12542014-10-23 22:26:10 +090071 struct uniphier_serial __iomem *port = uniphier_serial_port(dev);
Masahiro Yamadab2890732014-10-03 19:21:05 +090072
Masahiro Yamadae7c3a892015-02-27 02:26:46 +090073 if (!(readl(&port->lsr) & UART_LSR_THRE))
Masahiro Yamada50c12542014-10-23 22:26:10 +090074 return -EAGAIN;
Masahiro Yamadab2890732014-10-03 19:21:05 +090075
Masahiro Yamadae7c3a892015-02-27 02:26:46 +090076 writel(c, &port->tx);
Masahiro Yamada50c12542014-10-23 22:26:10 +090077
78 return 0;
Masahiro Yamadab2890732014-10-03 19:21:05 +090079}
80
Masahiro Yamadad11fbd52014-10-24 17:00:10 +090081static int uniphier_serial_pending(struct udevice *dev, bool input)
82{
83 struct uniphier_serial __iomem *port = uniphier_serial_port(dev);
84
85 if (input)
Masahiro Yamadae7c3a892015-02-27 02:26:46 +090086 return readl(&port->lsr) & UART_LSR_DR;
Masahiro Yamadad11fbd52014-10-24 17:00:10 +090087 else
Masahiro Yamadae7c3a892015-02-27 02:26:46 +090088 return !(readl(&port->lsr) & UART_LSR_THRE);
Masahiro Yamadad11fbd52014-10-24 17:00:10 +090089}
90
Masahiro Yamada66f4a9a2018-06-19 16:11:45 +090091/*
92 * SPL does not have enough memory footprint for the clock driver.
93 * Hardcode clock frequency for each SoC.
94 */
95struct uniphier_serial_clk_data {
96 const char *compatible;
97 unsigned int clk_rate;
98};
99
100static const struct uniphier_serial_clk_data uniphier_serial_clk_data[] = {
101 { .compatible = "socionext,uniphier-ld4", .clk_rate = 36864000 },
102 { .compatible = "socionext,uniphier-pro4", .clk_rate = 73728000 },
103 { .compatible = "socionext,uniphier-sld8", .clk_rate = 80000000 },
104 { .compatible = "socionext,uniphier-pro5", .clk_rate = 73728000 },
105 { .compatible = "socionext,uniphier-pxs2", .clk_rate = 88888888 },
106 { .compatible = "socionext,uniphier-ld6b", .clk_rate = 88888888 },
107 { .compatible = "socionext,uniphier-ld11", .clk_rate = 58823529 },
108 { .compatible = "socionext,uniphier-ld20", .clk_rate = 58823529 },
109 { .compatible = "socionext,uniphier-pxs3", .clk_rate = 58823529 },
110 { /* sentinel */ },
111};
112
Masahiro Yamada0bf0c6d2014-10-24 17:00:11 +0900113static int uniphier_serial_probe(struct udevice *dev)
Masahiro Yamada50c12542014-10-23 22:26:10 +0900114{
Masahiro Yamada8b5b1ee2018-06-19 16:11:44 +0900115 struct uniphier_serial_priv *priv = dev_get_priv(dev);
Masahiro Yamadafef7fcd2015-02-27 02:26:47 +0900116 struct uniphier_serial __iomem *port;
Masahiro Yamada66f4a9a2018-06-19 16:11:45 +0900117 const struct uniphier_serial_clk_data *clk_data;
118 ofnode root_node;
Masahiro Yamada25318dc2015-08-28 20:13:19 +0900119 fdt_addr_t base;
Masahiro Yamada25318dc2015-08-28 20:13:19 +0900120 u32 tmp;
Masahiro Yamadab2890732014-10-03 19:21:05 +0900121
Simon Glassba1dea42017-05-17 17:18:05 -0600122 base = devfdt_get_addr(dev);
Masahiro Yamada6ee016e2016-03-24 22:32:38 +0900123 if (base == FDT_ADDR_T_NONE)
124 return -EINVAL;
Masahiro Yamada25318dc2015-08-28 20:13:19 +0900125
Masahiro Yamadafa1f73f2016-07-19 21:56:13 +0900126 port = devm_ioremap(dev, base, SZ_64);
Masahiro Yamadafef7fcd2015-02-27 02:26:47 +0900127 if (!port)
Masahiro Yamada50c12542014-10-23 22:26:10 +0900128 return -ENOMEM;
Masahiro Yamadab2890732014-10-03 19:21:05 +0900129
Masahiro Yamadafef7fcd2015-02-27 02:26:47 +0900130 priv->membase = port;
131
Masahiro Yamada66f4a9a2018-06-19 16:11:45 +0900132 root_node = ofnode_path("/");
133 clk_data = uniphier_serial_clk_data;
134 while (clk_data->compatible) {
135 if (ofnode_device_is_compatible(root_node,
136 clk_data->compatible))
137 break;
138 clk_data++;
139 }
140
141 if (WARN_ON(!clk_data->compatible))
142 return -ENOTSUPP;
143
144 priv->uartclk = clk_data->clk_rate;
Masahiro Yamada25318dc2015-08-28 20:13:19 +0900145
Masahiro Yamadafef7fcd2015-02-27 02:26:47 +0900146 tmp = readl(&port->lcr_mcr);
147 tmp &= ~LCR_MASK;
148 tmp |= UART_LCR_WLEN8 << LCR_SHIFT;
149 writel(tmp, &port->lcr_mcr);
150
Masahiro Yamada50c12542014-10-23 22:26:10 +0900151 return 0;
152}
Masahiro Yamadab2890732014-10-03 19:21:05 +0900153
Masahiro Yamadaf8cd9b02014-11-26 18:34:00 +0900154static const struct udevice_id uniphier_uart_of_match[] = {
Masahiro Yamadad5f83a42015-03-11 15:54:46 +0900155 { .compatible = "socionext,uniphier-uart" },
156 { /* sentinel */ }
Masahiro Yamada50c12542014-10-23 22:26:10 +0900157};
158
Masahiro Yamada50c12542014-10-23 22:26:10 +0900159static const struct dm_serial_ops uniphier_serial_ops = {
160 .setbrg = uniphier_serial_setbrg,
161 .getc = uniphier_serial_getc,
162 .putc = uniphier_serial_putc,
Masahiro Yamadad11fbd52014-10-24 17:00:10 +0900163 .pending = uniphier_serial_pending,
Masahiro Yamada50c12542014-10-23 22:26:10 +0900164};
165
166U_BOOT_DRIVER(uniphier_serial) = {
Masahiro Yamada25318dc2015-08-28 20:13:19 +0900167 .name = "uniphier-uart",
Masahiro Yamada50c12542014-10-23 22:26:10 +0900168 .id = UCLASS_SERIAL,
Masahiro Yamada25318dc2015-08-28 20:13:19 +0900169 .of_match = uniphier_uart_of_match,
Masahiro Yamada50c12542014-10-23 22:26:10 +0900170 .probe = uniphier_serial_probe,
Masahiro Yamada8b5b1ee2018-06-19 16:11:44 +0900171 .priv_auto_alloc_size = sizeof(struct uniphier_serial_priv),
Masahiro Yamada50c12542014-10-23 22:26:10 +0900172 .ops = &uniphier_serial_ops,
Masahiro Yamada50c12542014-10-23 22:26:10 +0900173};