2018-05-06 21:58:06 +00:00
|
|
|
// SPDX-License-Identifier: GPL-2.0+
|
2014-10-03 10:21:05 +00:00
|
|
|
/*
|
2016-07-19 12:56:13 +00:00
|
|
|
* Copyright (C) 2012-2015 Panasonic Corporation
|
|
|
|
* Copyright (C) 2015-2016 Socionext Inc.
|
|
|
|
* Author: Masahiro Yamada <yamada.masahiro@socionext.com>
|
2014-10-03 10:21:05 +00:00
|
|
|
*/
|
|
|
|
|
2017-05-17 23:18:07 +00:00
|
|
|
#include <common.h>
|
2017-05-17 23:18:03 +00:00
|
|
|
#include <dm.h>
|
2020-07-09 16:12:06 +00:00
|
|
|
#include <linux/bitfield.h>
|
|
|
|
#include <linux/bitops.h>
|
2018-06-19 07:11:45 +00:00
|
|
|
#include <linux/bug.h>
|
2015-05-29 08:30:00 +00:00
|
|
|
#include <linux/io.h>
|
2014-10-30 03:11:14 +00:00
|
|
|
#include <linux/serial_reg.h>
|
2016-03-24 13:32:38 +00:00
|
|
|
#include <linux/sizes.h>
|
2016-09-21 02:28:55 +00:00
|
|
|
#include <linux/errno.h>
|
2014-10-03 10:21:05 +00:00
|
|
|
#include <serial.h>
|
2014-11-26 09:34:00 +00:00
|
|
|
#include <fdtdec.h>
|
2014-10-03 10:21:05 +00:00
|
|
|
|
2020-07-09 16:12:06 +00:00
|
|
|
#define UNIPHIER_UART_REGSHIFT 2
|
|
|
|
|
|
|
|
#define UNIPHIER_UART_RX (0 << (UNIPHIER_UART_REGSHIFT))
|
|
|
|
#define UNIPHIER_UART_TX UNIPHIER_UART_RX
|
|
|
|
/* bit[15:8] = CHAR, bit[7:0] = FCR */
|
|
|
|
#define UNIPHIER_UART_CHAR_FCR (3 << (UNIPHIER_UART_REGSHIFT))
|
2020-07-09 16:12:08 +00:00
|
|
|
#define UNIPHIER_UART_FCR_MASK GENMASK(7, 0)
|
2020-07-09 16:12:06 +00:00
|
|
|
/* bit[15:8] = LCR, bit[7:0] = MCR */
|
|
|
|
#define UNIPHIER_UART_LCR_MCR (4 << (UNIPHIER_UART_REGSHIFT))
|
|
|
|
#define UNIPHIER_UART_LCR_MASK GENMASK(15, 8)
|
|
|
|
#define UNIPHIER_UART_LSR (5 << (UNIPHIER_UART_REGSHIFT))
|
|
|
|
/* Divisor Latch Register */
|
|
|
|
#define UNIPHIER_UART_DLR (9 << (UNIPHIER_UART_REGSHIFT))
|
2014-10-03 10:21:05 +00:00
|
|
|
|
2018-06-19 07:11:44 +00:00
|
|
|
struct uniphier_serial_priv {
|
2020-07-09 16:12:06 +00:00
|
|
|
void __iomem *membase;
|
2015-08-28 11:13:19 +00:00
|
|
|
unsigned int uartclk;
|
2014-10-23 13:26:10 +00:00
|
|
|
};
|
|
|
|
|
2014-10-24 08:00:11 +00:00
|
|
|
static int uniphier_serial_setbrg(struct udevice *dev, int baudrate)
|
2014-10-03 10:21:05 +00:00
|
|
|
{
|
2018-06-19 07:11:44 +00:00
|
|
|
struct uniphier_serial_priv *priv = dev_get_priv(dev);
|
2020-07-09 16:12:06 +00:00
|
|
|
static const unsigned int mode_x_div = 16;
|
2014-10-03 10:21:05 +00:00
|
|
|
unsigned int divisor;
|
|
|
|
|
2015-08-28 11:13:19 +00:00
|
|
|
divisor = DIV_ROUND_CLOSEST(priv->uartclk, mode_x_div * baudrate);
|
2014-10-03 10:21:05 +00:00
|
|
|
|
2020-07-09 16:12:07 +00:00
|
|
|
/* flush the trasmitter before changing hw setting */
|
|
|
|
while (!(readl(priv->membase + UNIPHIER_UART_LSR) & UART_LSR_TEMT))
|
|
|
|
;
|
|
|
|
|
2020-07-09 16:12:06 +00:00
|
|
|
writel(divisor, priv->membase + UNIPHIER_UART_DLR);
|
2014-10-03 10:21:05 +00:00
|
|
|
|
2014-10-23 13:26:10 +00:00
|
|
|
return 0;
|
2014-10-03 10:21:05 +00:00
|
|
|
}
|
|
|
|
|
2014-10-23 13:26:10 +00:00
|
|
|
static int uniphier_serial_getc(struct udevice *dev)
|
2014-10-03 10:21:05 +00:00
|
|
|
{
|
2020-07-09 16:12:06 +00:00
|
|
|
struct uniphier_serial_priv *priv = dev_get_priv(dev);
|
2014-10-03 10:21:05 +00:00
|
|
|
|
2020-07-09 16:12:06 +00:00
|
|
|
if (!(readl(priv->membase + UNIPHIER_UART_LSR) & UART_LSR_DR))
|
2014-10-23 13:26:10 +00:00
|
|
|
return -EAGAIN;
|
2014-10-03 10:21:05 +00:00
|
|
|
|
2020-07-09 16:12:06 +00:00
|
|
|
return readl(priv->membase + UNIPHIER_UART_RX);
|
2014-10-03 10:21:05 +00:00
|
|
|
}
|
|
|
|
|
2014-10-23 13:26:10 +00:00
|
|
|
static int uniphier_serial_putc(struct udevice *dev, const char c)
|
2014-10-03 10:21:05 +00:00
|
|
|
{
|
2020-07-09 16:12:06 +00:00
|
|
|
struct uniphier_serial_priv *priv = dev_get_priv(dev);
|
2014-10-03 10:21:05 +00:00
|
|
|
|
2020-07-09 16:12:06 +00:00
|
|
|
if (!(readl(priv->membase + UNIPHIER_UART_LSR) & UART_LSR_THRE))
|
2014-10-23 13:26:10 +00:00
|
|
|
return -EAGAIN;
|
2014-10-03 10:21:05 +00:00
|
|
|
|
2020-07-09 16:12:06 +00:00
|
|
|
writel(c, priv->membase + UNIPHIER_UART_TX);
|
2014-10-23 13:26:10 +00:00
|
|
|
|
|
|
|
return 0;
|
2014-10-03 10:21:05 +00:00
|
|
|
}
|
|
|
|
|
2014-10-24 08:00:10 +00:00
|
|
|
static int uniphier_serial_pending(struct udevice *dev, bool input)
|
|
|
|
{
|
2020-07-09 16:12:06 +00:00
|
|
|
struct uniphier_serial_priv *priv = dev_get_priv(dev);
|
2014-10-24 08:00:10 +00:00
|
|
|
|
|
|
|
if (input)
|
2020-07-09 16:12:06 +00:00
|
|
|
return readl(priv->membase + UNIPHIER_UART_LSR) & UART_LSR_DR;
|
2014-10-24 08:00:10 +00:00
|
|
|
else
|
2020-07-09 16:12:06 +00:00
|
|
|
return !(readl(priv->membase + UNIPHIER_UART_LSR) & UART_LSR_THRE);
|
2014-10-24 08:00:10 +00:00
|
|
|
}
|
|
|
|
|
2018-06-19 07:11:45 +00:00
|
|
|
/*
|
|
|
|
* SPL does not have enough memory footprint for the clock driver.
|
|
|
|
* Hardcode clock frequency for each SoC.
|
|
|
|
*/
|
|
|
|
struct uniphier_serial_clk_data {
|
|
|
|
const char *compatible;
|
|
|
|
unsigned int clk_rate;
|
|
|
|
};
|
|
|
|
|
|
|
|
static const struct uniphier_serial_clk_data uniphier_serial_clk_data[] = {
|
|
|
|
{ .compatible = "socionext,uniphier-ld4", .clk_rate = 36864000 },
|
|
|
|
{ .compatible = "socionext,uniphier-pro4", .clk_rate = 73728000 },
|
|
|
|
{ .compatible = "socionext,uniphier-sld8", .clk_rate = 80000000 },
|
|
|
|
{ .compatible = "socionext,uniphier-pro5", .clk_rate = 73728000 },
|
|
|
|
{ .compatible = "socionext,uniphier-pxs2", .clk_rate = 88888888 },
|
|
|
|
{ .compatible = "socionext,uniphier-ld6b", .clk_rate = 88888888 },
|
|
|
|
{ .compatible = "socionext,uniphier-ld11", .clk_rate = 58823529 },
|
|
|
|
{ .compatible = "socionext,uniphier-ld20", .clk_rate = 58823529 },
|
|
|
|
{ .compatible = "socionext,uniphier-pxs3", .clk_rate = 58823529 },
|
|
|
|
{ /* sentinel */ },
|
|
|
|
};
|
|
|
|
|
2014-10-24 08:00:11 +00:00
|
|
|
static int uniphier_serial_probe(struct udevice *dev)
|
2014-10-23 13:26:10 +00:00
|
|
|
{
|
2018-06-19 07:11:44 +00:00
|
|
|
struct uniphier_serial_priv *priv = dev_get_priv(dev);
|
2018-06-19 07:11:45 +00:00
|
|
|
const struct uniphier_serial_clk_data *clk_data;
|
|
|
|
ofnode root_node;
|
2015-08-28 11:13:19 +00:00
|
|
|
fdt_addr_t base;
|
|
|
|
u32 tmp;
|
2014-10-03 10:21:05 +00:00
|
|
|
|
2020-07-17 05:36:48 +00:00
|
|
|
base = dev_read_addr(dev);
|
2016-03-24 13:32:38 +00:00
|
|
|
if (base == FDT_ADDR_T_NONE)
|
|
|
|
return -EINVAL;
|
2015-08-28 11:13:19 +00:00
|
|
|
|
2020-07-09 16:12:06 +00:00
|
|
|
priv->membase = devm_ioremap(dev, base, SZ_64);
|
|
|
|
if (!priv->membase)
|
2014-10-23 13:26:10 +00:00
|
|
|
return -ENOMEM;
|
2014-10-03 10:21:05 +00:00
|
|
|
|
2018-06-19 07:11:45 +00:00
|
|
|
root_node = ofnode_path("/");
|
|
|
|
clk_data = uniphier_serial_clk_data;
|
|
|
|
while (clk_data->compatible) {
|
|
|
|
if (ofnode_device_is_compatible(root_node,
|
|
|
|
clk_data->compatible))
|
|
|
|
break;
|
|
|
|
clk_data++;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (WARN_ON(!clk_data->compatible))
|
|
|
|
return -ENOTSUPP;
|
|
|
|
|
|
|
|
priv->uartclk = clk_data->clk_rate;
|
2015-08-28 11:13:19 +00:00
|
|
|
|
2020-07-30 09:28:07 +00:00
|
|
|
/* flush the trasmitter before changing hw setting */
|
2020-07-09 16:12:07 +00:00
|
|
|
while (!(readl(priv->membase + UNIPHIER_UART_LSR) & UART_LSR_TEMT))
|
|
|
|
;
|
|
|
|
|
2020-07-09 16:12:08 +00:00
|
|
|
/* enable FIFO */
|
|
|
|
tmp = readl(priv->membase + UNIPHIER_UART_CHAR_FCR);
|
|
|
|
tmp &= ~UNIPHIER_UART_FCR_MASK;
|
|
|
|
tmp |= FIELD_PREP(UNIPHIER_UART_FCR_MASK, UART_FCR_ENABLE_FIFO);
|
|
|
|
writel(tmp, priv->membase + UNIPHIER_UART_CHAR_FCR);
|
|
|
|
|
2020-07-09 16:12:06 +00:00
|
|
|
tmp = readl(priv->membase + UNIPHIER_UART_LCR_MCR);
|
|
|
|
tmp &= ~UNIPHIER_UART_LCR_MASK;
|
|
|
|
tmp |= FIELD_PREP(UNIPHIER_UART_LCR_MASK, UART_LCR_WLEN8);
|
|
|
|
writel(tmp, priv->membase + UNIPHIER_UART_LCR_MCR);
|
2015-02-26 17:26:47 +00:00
|
|
|
|
2014-10-23 13:26:10 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2014-11-26 09:34:00 +00:00
|
|
|
static const struct udevice_id uniphier_uart_of_match[] = {
|
2015-03-11 06:54:46 +00:00
|
|
|
{ .compatible = "socionext,uniphier-uart" },
|
|
|
|
{ /* sentinel */ }
|
2014-10-23 13:26:10 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
static const struct dm_serial_ops uniphier_serial_ops = {
|
|
|
|
.setbrg = uniphier_serial_setbrg,
|
|
|
|
.getc = uniphier_serial_getc,
|
|
|
|
.putc = uniphier_serial_putc,
|
2014-10-24 08:00:10 +00:00
|
|
|
.pending = uniphier_serial_pending,
|
2014-10-23 13:26:10 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
U_BOOT_DRIVER(uniphier_serial) = {
|
2015-08-28 11:13:19 +00:00
|
|
|
.name = "uniphier-uart",
|
2014-10-23 13:26:10 +00:00
|
|
|
.id = UCLASS_SERIAL,
|
2015-08-28 11:13:19 +00:00
|
|
|
.of_match = uniphier_uart_of_match,
|
2014-10-23 13:26:10 +00:00
|
|
|
.probe = uniphier_serial_probe,
|
2020-12-03 23:55:17 +00:00
|
|
|
.priv_auto = sizeof(struct uniphier_serial_priv),
|
2014-10-23 13:26:10 +00:00
|
|
|
.ops = &uniphier_serial_ops,
|
|
|
|
};
|