blob: a566ba7a47d5a880cd9b16c00216aa90afeeb2d3 [file] [log] [blame]
Tom Rini83d290c2018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Masahiro Yamada7f368552014-10-03 19:21:05 +09002/*
Masahiro Yamada4e3d8402016-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 Yamada7f368552014-10-03 19:21:05 +09006 */
7
Simon Glass9d922452017-05-17 17:18:03 -06008#include <dm.h>
Masahiro Yamada70434ab2020-07-10 01:12:06 +09009#include <linux/bitfield.h>
10#include <linux/bitops.h>
Masahiro Yamada41bacb52018-06-19 16:11:45 +090011#include <linux/bug.h>
Masahiro Yamadaf6e7f072015-05-29 17:30:00 +090012#include <linux/io.h>
Masahiro Yamada325b7082014-10-30 12:11:14 +090013#include <linux/serial_reg.h>
Masahiro Yamadab37a1cc2016-03-24 22:32:38 +090014#include <linux/sizes.h>
Masahiro Yamada1221ce42016-09-21 11:28:55 +090015#include <linux/errno.h>
Masahiro Yamada7f368552014-10-03 19:21:05 +090016#include <serial.h>
Masahiro Yamada625177d2014-11-26 18:34:00 +090017#include <fdtdec.h>
Masahiro Yamada7f368552014-10-03 19:21:05 +090018
Masahiro Yamada70434ab2020-07-10 01:12:06 +090019#define UNIPHIER_UART_REGSHIFT 2
20
21#define UNIPHIER_UART_RX (0 << (UNIPHIER_UART_REGSHIFT))
22#define UNIPHIER_UART_TX UNIPHIER_UART_RX
23/* bit[15:8] = CHAR, bit[7:0] = FCR */
24#define UNIPHIER_UART_CHAR_FCR (3 << (UNIPHIER_UART_REGSHIFT))
Masahiro Yamadab0535152020-07-10 01:12:08 +090025#define UNIPHIER_UART_FCR_MASK GENMASK(7, 0)
Masahiro Yamada70434ab2020-07-10 01:12:06 +090026/* bit[15:8] = LCR, bit[7:0] = MCR */
27#define UNIPHIER_UART_LCR_MCR (4 << (UNIPHIER_UART_REGSHIFT))
28#define UNIPHIER_UART_LCR_MASK GENMASK(15, 8)
29#define UNIPHIER_UART_LSR (5 << (UNIPHIER_UART_REGSHIFT))
30/* Divisor Latch Register */
31#define UNIPHIER_UART_DLR (9 << (UNIPHIER_UART_REGSHIFT))
Masahiro Yamada7f368552014-10-03 19:21:05 +090032
Masahiro Yamada157736a2018-06-19 16:11:44 +090033struct uniphier_serial_priv {
Masahiro Yamada70434ab2020-07-10 01:12:06 +090034 void __iomem *membase;
Masahiro Yamada6d99cfa2015-08-28 20:13:19 +090035 unsigned int uartclk;
Masahiro Yamadad064cbf2014-10-23 22:26:10 +090036};
Masahiro Yamada7f368552014-10-03 19:21:05 +090037
Masahiro Yamadad9bc8fd2014-10-24 17:00:11 +090038static int uniphier_serial_setbrg(struct udevice *dev, int baudrate)
Masahiro Yamada7f368552014-10-03 19:21:05 +090039{
Masahiro Yamada157736a2018-06-19 16:11:44 +090040 struct uniphier_serial_priv *priv = dev_get_priv(dev);
Masahiro Yamada70434ab2020-07-10 01:12:06 +090041 static const unsigned int mode_x_div = 16;
Masahiro Yamada7f368552014-10-03 19:21:05 +090042 unsigned int divisor;
Masahiro Yamada7f368552014-10-03 19:21:05 +090043
Masahiro Yamada6d99cfa2015-08-28 20:13:19 +090044 divisor = DIV_ROUND_CLOSEST(priv->uartclk, mode_x_div * baudrate);
Masahiro Yamada7f368552014-10-03 19:21:05 +090045
Masahiro Yamada26f7a7d2020-07-10 01:12:07 +090046 /* flush the trasmitter before changing hw setting */
47 while (!(readl(priv->membase + UNIPHIER_UART_LSR) & UART_LSR_TEMT))
48 ;
49
Masahiro Yamada70434ab2020-07-10 01:12:06 +090050 writel(divisor, priv->membase + UNIPHIER_UART_DLR);
Masahiro Yamadad064cbf2014-10-23 22:26:10 +090051
52 return 0;
Masahiro Yamada7f368552014-10-03 19:21:05 +090053}
54
Masahiro Yamadad064cbf2014-10-23 22:26:10 +090055static int uniphier_serial_getc(struct udevice *dev)
Masahiro Yamada7f368552014-10-03 19:21:05 +090056{
Masahiro Yamada70434ab2020-07-10 01:12:06 +090057 struct uniphier_serial_priv *priv = dev_get_priv(dev);
Masahiro Yamada7f368552014-10-03 19:21:05 +090058
Masahiro Yamada70434ab2020-07-10 01:12:06 +090059 if (!(readl(priv->membase + UNIPHIER_UART_LSR) & UART_LSR_DR))
Masahiro Yamadad064cbf2014-10-23 22:26:10 +090060 return -EAGAIN;
Masahiro Yamada7f368552014-10-03 19:21:05 +090061
Masahiro Yamada70434ab2020-07-10 01:12:06 +090062 return readl(priv->membase + UNIPHIER_UART_RX);
Masahiro Yamada7f368552014-10-03 19:21:05 +090063}
64
Masahiro Yamadad064cbf2014-10-23 22:26:10 +090065static int uniphier_serial_putc(struct udevice *dev, const char c)
Masahiro Yamada7f368552014-10-03 19:21:05 +090066{
Masahiro Yamada70434ab2020-07-10 01:12:06 +090067 struct uniphier_serial_priv *priv = dev_get_priv(dev);
Masahiro Yamada7f368552014-10-03 19:21:05 +090068
Masahiro Yamada70434ab2020-07-10 01:12:06 +090069 if (!(readl(priv->membase + UNIPHIER_UART_LSR) & UART_LSR_THRE))
Masahiro Yamadad064cbf2014-10-23 22:26:10 +090070 return -EAGAIN;
Masahiro Yamada7f368552014-10-03 19:21:05 +090071
Masahiro Yamada70434ab2020-07-10 01:12:06 +090072 writel(c, priv->membase + UNIPHIER_UART_TX);
Masahiro Yamadad064cbf2014-10-23 22:26:10 +090073
74 return 0;
Masahiro Yamada7f368552014-10-03 19:21:05 +090075}
76
Masahiro Yamadabb721482014-10-24 17:00:10 +090077static int uniphier_serial_pending(struct udevice *dev, bool input)
78{
Masahiro Yamada70434ab2020-07-10 01:12:06 +090079 struct uniphier_serial_priv *priv = dev_get_priv(dev);
Masahiro Yamadabb721482014-10-24 17:00:10 +090080
81 if (input)
Masahiro Yamada70434ab2020-07-10 01:12:06 +090082 return readl(priv->membase + UNIPHIER_UART_LSR) & UART_LSR_DR;
Masahiro Yamadabb721482014-10-24 17:00:10 +090083 else
Masahiro Yamada70434ab2020-07-10 01:12:06 +090084 return !(readl(priv->membase + UNIPHIER_UART_LSR) & UART_LSR_THRE);
Masahiro Yamadabb721482014-10-24 17:00:10 +090085}
86
Masahiro Yamada41bacb52018-06-19 16:11:45 +090087/*
88 * SPL does not have enough memory footprint for the clock driver.
89 * Hardcode clock frequency for each SoC.
90 */
91struct uniphier_serial_clk_data {
92 const char *compatible;
93 unsigned int clk_rate;
94};
95
96static const struct uniphier_serial_clk_data uniphier_serial_clk_data[] = {
97 { .compatible = "socionext,uniphier-ld4", .clk_rate = 36864000 },
98 { .compatible = "socionext,uniphier-pro4", .clk_rate = 73728000 },
99 { .compatible = "socionext,uniphier-sld8", .clk_rate = 80000000 },
100 { .compatible = "socionext,uniphier-pro5", .clk_rate = 73728000 },
101 { .compatible = "socionext,uniphier-pxs2", .clk_rate = 88888888 },
102 { .compatible = "socionext,uniphier-ld6b", .clk_rate = 88888888 },
103 { .compatible = "socionext,uniphier-ld11", .clk_rate = 58823529 },
104 { .compatible = "socionext,uniphier-ld20", .clk_rate = 58823529 },
105 { .compatible = "socionext,uniphier-pxs3", .clk_rate = 58823529 },
106 { /* sentinel */ },
107};
108
Masahiro Yamadad9bc8fd2014-10-24 17:00:11 +0900109static int uniphier_serial_probe(struct udevice *dev)
Masahiro Yamadad064cbf2014-10-23 22:26:10 +0900110{
Masahiro Yamada157736a2018-06-19 16:11:44 +0900111 struct uniphier_serial_priv *priv = dev_get_priv(dev);
Masahiro Yamada41bacb52018-06-19 16:11:45 +0900112 const struct uniphier_serial_clk_data *clk_data;
113 ofnode root_node;
Masahiro Yamada6d99cfa2015-08-28 20:13:19 +0900114 fdt_addr_t base;
Masahiro Yamada6d99cfa2015-08-28 20:13:19 +0900115 u32 tmp;
Masahiro Yamadad064cbf2014-10-23 22:26:10 +0900116
Masahiro Yamada25484932020-07-17 14:36:48 +0900117 base = dev_read_addr(dev);
Masahiro Yamadab37a1cc2016-03-24 22:32:38 +0900118 if (base == FDT_ADDR_T_NONE)
119 return -EINVAL;
Masahiro Yamada6d99cfa2015-08-28 20:13:19 +0900120
Masahiro Yamada70434ab2020-07-10 01:12:06 +0900121 priv->membase = devm_ioremap(dev, base, SZ_64);
122 if (!priv->membase)
Masahiro Yamadad064cbf2014-10-23 22:26:10 +0900123 return -ENOMEM;
124
Masahiro Yamada41bacb52018-06-19 16:11:45 +0900125 root_node = ofnode_path("/");
126 clk_data = uniphier_serial_clk_data;
127 while (clk_data->compatible) {
128 if (ofnode_device_is_compatible(root_node,
129 clk_data->compatible))
130 break;
131 clk_data++;
132 }
133
134 if (WARN_ON(!clk_data->compatible))
135 return -ENOTSUPP;
136
137 priv->uartclk = clk_data->clk_rate;
Masahiro Yamada6d99cfa2015-08-28 20:13:19 +0900138
Masahiro Yamada26f0c862020-07-30 18:28:07 +0900139 /* flush the trasmitter before changing hw setting */
Masahiro Yamada26f7a7d2020-07-10 01:12:07 +0900140 while (!(readl(priv->membase + UNIPHIER_UART_LSR) & UART_LSR_TEMT))
141 ;
142
Masahiro Yamadab0535152020-07-10 01:12:08 +0900143 /* enable FIFO */
144 tmp = readl(priv->membase + UNIPHIER_UART_CHAR_FCR);
145 tmp &= ~UNIPHIER_UART_FCR_MASK;
146 tmp |= FIELD_PREP(UNIPHIER_UART_FCR_MASK, UART_FCR_ENABLE_FIFO);
147 writel(tmp, priv->membase + UNIPHIER_UART_CHAR_FCR);
148
Masahiro Yamada70434ab2020-07-10 01:12:06 +0900149 tmp = readl(priv->membase + UNIPHIER_UART_LCR_MCR);
150 tmp &= ~UNIPHIER_UART_LCR_MASK;
151 tmp |= FIELD_PREP(UNIPHIER_UART_LCR_MASK, UART_LCR_WLEN8);
152 writel(tmp, priv->membase + UNIPHIER_UART_LCR_MCR);
Masahiro Yamada099cf772015-02-27 02:26:47 +0900153
Masahiro Yamadad064cbf2014-10-23 22:26:10 +0900154 return 0;
155}
156
Masahiro Yamada625177d2014-11-26 18:34:00 +0900157static const struct udevice_id uniphier_uart_of_match[] = {
Masahiro Yamada6462cde2015-03-11 15:54:46 +0900158 { .compatible = "socionext,uniphier-uart" },
159 { /* sentinel */ }
Masahiro Yamada7f368552014-10-03 19:21:05 +0900160};
161
Masahiro Yamadad064cbf2014-10-23 22:26:10 +0900162static const struct dm_serial_ops uniphier_serial_ops = {
163 .setbrg = uniphier_serial_setbrg,
164 .getc = uniphier_serial_getc,
165 .putc = uniphier_serial_putc,
Masahiro Yamadabb721482014-10-24 17:00:10 +0900166 .pending = uniphier_serial_pending,
Masahiro Yamadad064cbf2014-10-23 22:26:10 +0900167};
168
169U_BOOT_DRIVER(uniphier_serial) = {
Masahiro Yamada6d99cfa2015-08-28 20:13:19 +0900170 .name = "uniphier-uart",
Masahiro Yamadad064cbf2014-10-23 22:26:10 +0900171 .id = UCLASS_SERIAL,
Masahiro Yamada6d99cfa2015-08-28 20:13:19 +0900172 .of_match = uniphier_uart_of_match,
Masahiro Yamadad064cbf2014-10-23 22:26:10 +0900173 .probe = uniphier_serial_probe,
Simon Glass41575d82020-12-03 16:55:17 -0700174 .priv_auto = sizeof(struct uniphier_serial_priv),
Masahiro Yamadad064cbf2014-10-23 22:26:10 +0900175 .ops = &uniphier_serial_ops,
Masahiro Yamadad064cbf2014-10-23 22:26:10 +0900176};