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
|
|
|
*
|
|
|
|
* SPDX-License-Identifier: GPL-2.0+
|
|
|
|
*/
|
|
|
|
|
2017-05-17 23:18:07 +00:00
|
|
|
#include <common.h>
|
2017-05-17 23:18:03 +00:00
|
|
|
#include <dm.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
|
|
|
|
|
|
|
/*
|
|
|
|
* Note: Register map is slightly different from that of 16550.
|
|
|
|
*/
|
|
|
|
struct uniphier_serial {
|
2015-02-26 17:26:46 +00:00
|
|
|
u32 rx; /* In: Receive buffer */
|
|
|
|
#define tx rx /* Out: Transmit buffer */
|
|
|
|
u32 ier; /* Interrupt Enable Register */
|
|
|
|
u32 iir; /* In: Interrupt ID Register */
|
|
|
|
u32 char_fcr; /* Charactor / FIFO Control Register */
|
|
|
|
u32 lcr_mcr; /* Line/Modem Control Register */
|
|
|
|
#define LCR_SHIFT 8
|
|
|
|
#define LCR_MASK (0xff << (LCR_SHIFT))
|
|
|
|
u32 lsr; /* In: Line Status Register */
|
|
|
|
u32 msr; /* In: Modem Status Register */
|
|
|
|
u32 __rsv0;
|
|
|
|
u32 __rsv1;
|
|
|
|
u32 dlr; /* Divisor Latch Register */
|
2014-10-03 10:21:05 +00:00
|
|
|
};
|
|
|
|
|
2014-10-23 13:26:10 +00:00
|
|
|
struct uniphier_serial_private_data {
|
|
|
|
struct uniphier_serial __iomem *membase;
|
2015-08-28 11:13:19 +00:00
|
|
|
unsigned int uartclk;
|
2014-10-23 13:26:10 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
#define uniphier_serial_port(dev) \
|
|
|
|
((struct uniphier_serial_private_data *)dev_get_priv(dev))->membase
|
2014-10-03 10:21:05 +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
|
|
|
{
|
2015-08-28 11:13:19 +00:00
|
|
|
struct uniphier_serial_private_data *priv = dev_get_priv(dev);
|
2014-10-23 13:26:10 +00:00
|
|
|
struct uniphier_serial __iomem *port = uniphier_serial_port(dev);
|
2014-10-03 10:21:05 +00:00
|
|
|
const unsigned int mode_x_div = 16;
|
|
|
|
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
|
|
|
|
2015-02-26 17:26:46 +00:00
|
|
|
writel(divisor, &port->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
|
|
|
{
|
2014-10-23 13:26:10 +00:00
|
|
|
struct uniphier_serial __iomem *port = uniphier_serial_port(dev);
|
2014-10-03 10:21:05 +00:00
|
|
|
|
2015-02-26 17:26:46 +00:00
|
|
|
if (!(readl(&port->lsr) & UART_LSR_DR))
|
2014-10-23 13:26:10 +00:00
|
|
|
return -EAGAIN;
|
2014-10-03 10:21:05 +00:00
|
|
|
|
2015-02-26 17:26:46 +00:00
|
|
|
return readl(&port->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
|
|
|
{
|
2014-10-23 13:26:10 +00:00
|
|
|
struct uniphier_serial __iomem *port = uniphier_serial_port(dev);
|
2014-10-03 10:21:05 +00:00
|
|
|
|
2015-02-26 17:26:46 +00:00
|
|
|
if (!(readl(&port->lsr) & UART_LSR_THRE))
|
2014-10-23 13:26:10 +00:00
|
|
|
return -EAGAIN;
|
2014-10-03 10:21:05 +00:00
|
|
|
|
2015-02-26 17:26:46 +00:00
|
|
|
writel(c, &port->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)
|
|
|
|
{
|
|
|
|
struct uniphier_serial __iomem *port = uniphier_serial_port(dev);
|
|
|
|
|
|
|
|
if (input)
|
2015-02-26 17:26:46 +00:00
|
|
|
return readl(&port->lsr) & UART_LSR_DR;
|
2014-10-24 08:00:10 +00:00
|
|
|
else
|
2015-02-26 17:26:46 +00:00
|
|
|
return !(readl(&port->lsr) & UART_LSR_THRE);
|
2014-10-24 08:00:10 +00:00
|
|
|
}
|
|
|
|
|
2014-10-24 08:00:11 +00:00
|
|
|
static int uniphier_serial_probe(struct udevice *dev)
|
2014-10-23 13:26:10 +00:00
|
|
|
{
|
2015-08-28 11:13:19 +00:00
|
|
|
DECLARE_GLOBAL_DATA_PTR;
|
2014-10-23 13:26:10 +00:00
|
|
|
struct uniphier_serial_private_data *priv = dev_get_priv(dev);
|
2015-02-26 17:26:47 +00:00
|
|
|
struct uniphier_serial __iomem *port;
|
2015-08-28 11:13:19 +00:00
|
|
|
fdt_addr_t base;
|
|
|
|
u32 tmp;
|
2014-10-03 10:21:05 +00:00
|
|
|
|
2017-05-17 23:18:05 +00:00
|
|
|
base = devfdt_get_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
|
|
|
|
2016-07-19 12:56:13 +00:00
|
|
|
port = devm_ioremap(dev, base, SZ_64);
|
2015-02-26 17:26:47 +00:00
|
|
|
if (!port)
|
2014-10-23 13:26:10 +00:00
|
|
|
return -ENOMEM;
|
2014-10-03 10:21:05 +00:00
|
|
|
|
2015-02-26 17:26:47 +00:00
|
|
|
priv->membase = port;
|
|
|
|
|
2017-01-17 23:52:55 +00:00
|
|
|
priv->uartclk = fdtdec_get_int(gd->fdt_blob, dev_of_offset(dev),
|
2015-08-28 11:13:19 +00:00
|
|
|
"clock-frequency", 0);
|
|
|
|
|
2015-02-26 17:26:47 +00:00
|
|
|
tmp = readl(&port->lcr_mcr);
|
|
|
|
tmp &= ~LCR_MASK;
|
|
|
|
tmp |= UART_LCR_WLEN8 << LCR_SHIFT;
|
|
|
|
writel(tmp, &port->lcr_mcr);
|
|
|
|
|
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,
|
|
|
|
.priv_auto_alloc_size = sizeof(struct uniphier_serial_private_data),
|
|
|
|
.ops = &uniphier_serial_ops,
|
|
|
|
};
|