blob: 27e4b92c39974fe1f5b111d5c6c3b61e7b2bc3d2 [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
Tom Riniabb9a042024-05-18 20:20:43 -06008#include <common.h>
Simon Glass11c89f32017-05-17 17:18:03 -06009#include <dm.h>
Masahiro Yamadaf2d670a2020-07-10 01:12:06 +090010#include <linux/bitfield.h>
11#include <linux/bitops.h>
Masahiro Yamada66f4a9a2018-06-19 16:11:45 +090012#include <linux/bug.h>
Masahiro Yamada663a23f2015-05-29 17:30:00 +090013#include <linux/io.h>
Masahiro Yamada9caae5e2014-10-30 12:11:14 +090014#include <linux/serial_reg.h>
Masahiro Yamada6ee016e2016-03-24 22:32:38 +090015#include <linux/sizes.h>
Masahiro Yamada56a931c2016-09-21 11:28:55 +090016#include <linux/errno.h>
Masahiro Yamadab2890732014-10-03 19:21:05 +090017#include <serial.h>
Masahiro Yamadaf8cd9b02014-11-26 18:34:00 +090018#include <fdtdec.h>
Masahiro Yamadab2890732014-10-03 19:21:05 +090019
Masahiro Yamadaf2d670a2020-07-10 01:12:06 +090020#define UNIPHIER_UART_REGSHIFT 2
21
22#define UNIPHIER_UART_RX (0 << (UNIPHIER_UART_REGSHIFT))
23#define UNIPHIER_UART_TX UNIPHIER_UART_RX
24/* bit[15:8] = CHAR, bit[7:0] = FCR */
25#define UNIPHIER_UART_CHAR_FCR (3 << (UNIPHIER_UART_REGSHIFT))
Masahiro Yamadac346d422020-07-10 01:12:08 +090026#define UNIPHIER_UART_FCR_MASK GENMASK(7, 0)
Masahiro Yamadaf2d670a2020-07-10 01:12:06 +090027/* bit[15:8] = LCR, bit[7:0] = MCR */
28#define UNIPHIER_UART_LCR_MCR (4 << (UNIPHIER_UART_REGSHIFT))
29#define UNIPHIER_UART_LCR_MASK GENMASK(15, 8)
30#define UNIPHIER_UART_LSR (5 << (UNIPHIER_UART_REGSHIFT))
31/* Divisor Latch Register */
32#define UNIPHIER_UART_DLR (9 << (UNIPHIER_UART_REGSHIFT))
Masahiro Yamadab2890732014-10-03 19:21:05 +090033
Masahiro Yamada8b5b1ee2018-06-19 16:11:44 +090034struct uniphier_serial_priv {
Masahiro Yamadaf2d670a2020-07-10 01:12:06 +090035 void __iomem *membase;
Masahiro Yamada25318dc2015-08-28 20:13:19 +090036 unsigned int uartclk;
Masahiro Yamada50c12542014-10-23 22:26:10 +090037};
38
Masahiro Yamada0bf0c6d2014-10-24 17:00:11 +090039static int uniphier_serial_setbrg(struct udevice *dev, int baudrate)
Masahiro Yamadab2890732014-10-03 19:21:05 +090040{
Masahiro Yamada8b5b1ee2018-06-19 16:11:44 +090041 struct uniphier_serial_priv *priv = dev_get_priv(dev);
Masahiro Yamadaf2d670a2020-07-10 01:12:06 +090042 static const unsigned int mode_x_div = 16;
Masahiro Yamadab2890732014-10-03 19:21:05 +090043 unsigned int divisor;
Masahiro Yamadab2890732014-10-03 19:21:05 +090044
Masahiro Yamada25318dc2015-08-28 20:13:19 +090045 divisor = DIV_ROUND_CLOSEST(priv->uartclk, mode_x_div * baudrate);
Masahiro Yamadab2890732014-10-03 19:21:05 +090046
Masahiro Yamadab8daeda2020-07-10 01:12:07 +090047 /* flush the trasmitter before changing hw setting */
48 while (!(readl(priv->membase + UNIPHIER_UART_LSR) & UART_LSR_TEMT))
49 ;
50
Masahiro Yamadaf2d670a2020-07-10 01:12:06 +090051 writel(divisor, priv->membase + UNIPHIER_UART_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 Yamadaf2d670a2020-07-10 01:12:06 +090058 struct uniphier_serial_priv *priv = dev_get_priv(dev);
Masahiro Yamadab2890732014-10-03 19:21:05 +090059
Masahiro Yamadaf2d670a2020-07-10 01:12:06 +090060 if (!(readl(priv->membase + UNIPHIER_UART_LSR) & UART_LSR_DR))
Masahiro Yamada50c12542014-10-23 22:26:10 +090061 return -EAGAIN;
Masahiro Yamadab2890732014-10-03 19:21:05 +090062
Masahiro Yamadaf2d670a2020-07-10 01:12:06 +090063 return readl(priv->membase + UNIPHIER_UART_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 Yamadaf2d670a2020-07-10 01:12:06 +090068 struct uniphier_serial_priv *priv = dev_get_priv(dev);
Masahiro Yamadab2890732014-10-03 19:21:05 +090069
Masahiro Yamadaf2d670a2020-07-10 01:12:06 +090070 if (!(readl(priv->membase + UNIPHIER_UART_LSR) & UART_LSR_THRE))
Masahiro Yamada50c12542014-10-23 22:26:10 +090071 return -EAGAIN;
Masahiro Yamadab2890732014-10-03 19:21:05 +090072
Masahiro Yamadaf2d670a2020-07-10 01:12:06 +090073 writel(c, priv->membase + UNIPHIER_UART_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{
Masahiro Yamadaf2d670a2020-07-10 01:12:06 +090080 struct uniphier_serial_priv *priv = dev_get_priv(dev);
Masahiro Yamadad11fbd52014-10-24 17:00:10 +090081
82 if (input)
Masahiro Yamadaf2d670a2020-07-10 01:12:06 +090083 return readl(priv->membase + UNIPHIER_UART_LSR) & UART_LSR_DR;
Masahiro Yamadad11fbd52014-10-24 17:00:10 +090084 else
Masahiro Yamadaf2d670a2020-07-10 01:12:06 +090085 return !(readl(priv->membase + UNIPHIER_UART_LSR) & UART_LSR_THRE);
Masahiro Yamadad11fbd52014-10-24 17:00:10 +090086}
87
Masahiro Yamada66f4a9a2018-06-19 16:11:45 +090088/*
89 * SPL does not have enough memory footprint for the clock driver.
90 * Hardcode clock frequency for each SoC.
91 */
92struct uniphier_serial_clk_data {
93 const char *compatible;
94 unsigned int clk_rate;
95};
96
97static const struct uniphier_serial_clk_data uniphier_serial_clk_data[] = {
98 { .compatible = "socionext,uniphier-ld4", .clk_rate = 36864000 },
99 { .compatible = "socionext,uniphier-pro4", .clk_rate = 73728000 },
100 { .compatible = "socionext,uniphier-sld8", .clk_rate = 80000000 },
101 { .compatible = "socionext,uniphier-pro5", .clk_rate = 73728000 },
102 { .compatible = "socionext,uniphier-pxs2", .clk_rate = 88888888 },
103 { .compatible = "socionext,uniphier-ld6b", .clk_rate = 88888888 },
104 { .compatible = "socionext,uniphier-ld11", .clk_rate = 58823529 },
105 { .compatible = "socionext,uniphier-ld20", .clk_rate = 58823529 },
106 { .compatible = "socionext,uniphier-pxs3", .clk_rate = 58823529 },
107 { /* sentinel */ },
108};
109
Masahiro Yamada0bf0c6d2014-10-24 17:00:11 +0900110static int uniphier_serial_probe(struct udevice *dev)
Masahiro Yamada50c12542014-10-23 22:26:10 +0900111{
Masahiro Yamada8b5b1ee2018-06-19 16:11:44 +0900112 struct uniphier_serial_priv *priv = dev_get_priv(dev);
Masahiro Yamada66f4a9a2018-06-19 16:11:45 +0900113 const struct uniphier_serial_clk_data *clk_data;
114 ofnode root_node;
Masahiro Yamada25318dc2015-08-28 20:13:19 +0900115 fdt_addr_t base;
Masahiro Yamada25318dc2015-08-28 20:13:19 +0900116 u32 tmp;
Masahiro Yamadab2890732014-10-03 19:21:05 +0900117
Masahiro Yamadaa89b4de2020-07-17 14:36:48 +0900118 base = dev_read_addr(dev);
Masahiro Yamada6ee016e2016-03-24 22:32:38 +0900119 if (base == FDT_ADDR_T_NONE)
120 return -EINVAL;
Masahiro Yamada25318dc2015-08-28 20:13:19 +0900121
Masahiro Yamadaf2d670a2020-07-10 01:12:06 +0900122 priv->membase = devm_ioremap(dev, base, SZ_64);
123 if (!priv->membase)
Masahiro Yamada50c12542014-10-23 22:26:10 +0900124 return -ENOMEM;
Masahiro Yamadab2890732014-10-03 19:21:05 +0900125
Masahiro Yamada66f4a9a2018-06-19 16:11:45 +0900126 root_node = ofnode_path("/");
127 clk_data = uniphier_serial_clk_data;
128 while (clk_data->compatible) {
129 if (ofnode_device_is_compatible(root_node,
130 clk_data->compatible))
131 break;
132 clk_data++;
133 }
134
135 if (WARN_ON(!clk_data->compatible))
136 return -ENOTSUPP;
137
138 priv->uartclk = clk_data->clk_rate;
Masahiro Yamada25318dc2015-08-28 20:13:19 +0900139
Masahiro Yamadac3d25592020-07-30 18:28:07 +0900140 /* flush the trasmitter before changing hw setting */
Masahiro Yamadab8daeda2020-07-10 01:12:07 +0900141 while (!(readl(priv->membase + UNIPHIER_UART_LSR) & UART_LSR_TEMT))
142 ;
143
Masahiro Yamadac346d422020-07-10 01:12:08 +0900144 /* enable FIFO */
145 tmp = readl(priv->membase + UNIPHIER_UART_CHAR_FCR);
146 tmp &= ~UNIPHIER_UART_FCR_MASK;
147 tmp |= FIELD_PREP(UNIPHIER_UART_FCR_MASK, UART_FCR_ENABLE_FIFO);
148 writel(tmp, priv->membase + UNIPHIER_UART_CHAR_FCR);
149
Masahiro Yamadaf2d670a2020-07-10 01:12:06 +0900150 tmp = readl(priv->membase + UNIPHIER_UART_LCR_MCR);
151 tmp &= ~UNIPHIER_UART_LCR_MASK;
152 tmp |= FIELD_PREP(UNIPHIER_UART_LCR_MASK, UART_LCR_WLEN8);
153 writel(tmp, priv->membase + UNIPHIER_UART_LCR_MCR);
Masahiro Yamadafef7fcd2015-02-27 02:26:47 +0900154
Masahiro Yamada50c12542014-10-23 22:26:10 +0900155 return 0;
156}
Masahiro Yamadab2890732014-10-03 19:21:05 +0900157
Masahiro Yamadaf8cd9b02014-11-26 18:34:00 +0900158static const struct udevice_id uniphier_uart_of_match[] = {
Masahiro Yamadad5f83a42015-03-11 15:54:46 +0900159 { .compatible = "socionext,uniphier-uart" },
160 { /* sentinel */ }
Masahiro Yamada50c12542014-10-23 22:26:10 +0900161};
162
Masahiro Yamada50c12542014-10-23 22:26:10 +0900163static const struct dm_serial_ops uniphier_serial_ops = {
164 .setbrg = uniphier_serial_setbrg,
165 .getc = uniphier_serial_getc,
166 .putc = uniphier_serial_putc,
Masahiro Yamadad11fbd52014-10-24 17:00:10 +0900167 .pending = uniphier_serial_pending,
Masahiro Yamada50c12542014-10-23 22:26:10 +0900168};
169
170U_BOOT_DRIVER(uniphier_serial) = {
Masahiro Yamada25318dc2015-08-28 20:13:19 +0900171 .name = "uniphier-uart",
Masahiro Yamada50c12542014-10-23 22:26:10 +0900172 .id = UCLASS_SERIAL,
Masahiro Yamada25318dc2015-08-28 20:13:19 +0900173 .of_match = uniphier_uart_of_match,
Masahiro Yamada50c12542014-10-23 22:26:10 +0900174 .probe = uniphier_serial_probe,
Simon Glass8a2b47f2020-12-03 16:55:17 -0700175 .priv_auto = sizeof(struct uniphier_serial_priv),
Masahiro Yamada50c12542014-10-23 22:26:10 +0900176 .ops = &uniphier_serial_ops,
Masahiro Yamada50c12542014-10-23 22:26:10 +0900177};