blob: 68895bde8eb30eeeea6bf32b9f16ba09931928e5 [file] [log] [blame]
Masahiro Yamada7f368552014-10-03 19:21:05 +09001/*
Masahiro Yamada4e3d8402016-07-19 21:56:13 +09002 * Copyright (C) 2012-2015 Panasonic Corporation
3 * Copyright (C) 2015-2016 Socionext Inc.
4 * Author: Masahiro Yamada <yamada.masahiro@socionext.com>
Masahiro Yamada7f368552014-10-03 19:21:05 +09005 *
Masahiro Yamada7f368552014-10-03 19:21:05 +09006 * SPDX-License-Identifier: GPL-2.0+
7 */
8
Simon Glass4af0d7e2017-05-17 17:18:07 -06009#include <common.h>
Simon Glass9d922452017-05-17 17:18:03 -060010#include <dm.h>
Masahiro Yamadaf6e7f072015-05-29 17:30:00 +090011#include <linux/io.h>
Masahiro Yamada325b7082014-10-30 12:11:14 +090012#include <linux/serial_reg.h>
Masahiro Yamadab37a1cc2016-03-24 22:32:38 +090013#include <linux/sizes.h>
Masahiro Yamada1221ce42016-09-21 11:28:55 +090014#include <linux/errno.h>
Masahiro Yamada7f368552014-10-03 19:21:05 +090015#include <serial.h>
Masahiro Yamada625177d2014-11-26 18:34:00 +090016#include <fdtdec.h>
Masahiro Yamada7f368552014-10-03 19:21:05 +090017
Masahiro Yamada7f368552014-10-03 19:21:05 +090018/*
19 * Note: Register map is slightly different from that of 16550.
20 */
21struct uniphier_serial {
Masahiro Yamadad0c47b32015-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 Yamada7f368552014-10-03 19:21:05 +090035};
36
Masahiro Yamadad064cbf2014-10-23 22:26:10 +090037struct uniphier_serial_private_data {
38 struct uniphier_serial __iomem *membase;
Masahiro Yamada6d99cfa2015-08-28 20:13:19 +090039 unsigned int uartclk;
Masahiro Yamadad064cbf2014-10-23 22:26:10 +090040};
Masahiro Yamada7f368552014-10-03 19:21:05 +090041
Masahiro Yamadad064cbf2014-10-23 22:26:10 +090042#define uniphier_serial_port(dev) \
43 ((struct uniphier_serial_private_data *)dev_get_priv(dev))->membase
44
Masahiro Yamadad9bc8fd2014-10-24 17:00:11 +090045static int uniphier_serial_setbrg(struct udevice *dev, int baudrate)
Masahiro Yamada7f368552014-10-03 19:21:05 +090046{
Masahiro Yamada6d99cfa2015-08-28 20:13:19 +090047 struct uniphier_serial_private_data *priv = dev_get_priv(dev);
Masahiro Yamadad064cbf2014-10-23 22:26:10 +090048 struct uniphier_serial __iomem *port = uniphier_serial_port(dev);
Masahiro Yamada7f368552014-10-03 19:21:05 +090049 const unsigned int mode_x_div = 16;
50 unsigned int divisor;
Masahiro Yamada7f368552014-10-03 19:21:05 +090051
Masahiro Yamada6d99cfa2015-08-28 20:13:19 +090052 divisor = DIV_ROUND_CLOSEST(priv->uartclk, mode_x_div * baudrate);
Masahiro Yamada7f368552014-10-03 19:21:05 +090053
Masahiro Yamadad0c47b32015-02-27 02:26:46 +090054 writel(divisor, &port->dlr);
Masahiro Yamadad064cbf2014-10-23 22:26:10 +090055
56 return 0;
Masahiro Yamada7f368552014-10-03 19:21:05 +090057}
58
Masahiro Yamadad064cbf2014-10-23 22:26:10 +090059static int uniphier_serial_getc(struct udevice *dev)
Masahiro Yamada7f368552014-10-03 19:21:05 +090060{
Masahiro Yamadad064cbf2014-10-23 22:26:10 +090061 struct uniphier_serial __iomem *port = uniphier_serial_port(dev);
Masahiro Yamada7f368552014-10-03 19:21:05 +090062
Masahiro Yamadad0c47b32015-02-27 02:26:46 +090063 if (!(readl(&port->lsr) & UART_LSR_DR))
Masahiro Yamadad064cbf2014-10-23 22:26:10 +090064 return -EAGAIN;
Masahiro Yamada7f368552014-10-03 19:21:05 +090065
Masahiro Yamadad0c47b32015-02-27 02:26:46 +090066 return readl(&port->rx);
Masahiro Yamada7f368552014-10-03 19:21:05 +090067}
68
Masahiro Yamadad064cbf2014-10-23 22:26:10 +090069static int uniphier_serial_putc(struct udevice *dev, const char c)
Masahiro Yamada7f368552014-10-03 19:21:05 +090070{
Masahiro Yamadad064cbf2014-10-23 22:26:10 +090071 struct uniphier_serial __iomem *port = uniphier_serial_port(dev);
Masahiro Yamada7f368552014-10-03 19:21:05 +090072
Masahiro Yamadad0c47b32015-02-27 02:26:46 +090073 if (!(readl(&port->lsr) & UART_LSR_THRE))
Masahiro Yamadad064cbf2014-10-23 22:26:10 +090074 return -EAGAIN;
Masahiro Yamada7f368552014-10-03 19:21:05 +090075
Masahiro Yamadad0c47b32015-02-27 02:26:46 +090076 writel(c, &port->tx);
Masahiro Yamadad064cbf2014-10-23 22:26:10 +090077
78 return 0;
Masahiro Yamada7f368552014-10-03 19:21:05 +090079}
80
Masahiro Yamadabb721482014-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 Yamadad0c47b32015-02-27 02:26:46 +090086 return readl(&port->lsr) & UART_LSR_DR;
Masahiro Yamadabb721482014-10-24 17:00:10 +090087 else
Masahiro Yamadad0c47b32015-02-27 02:26:46 +090088 return !(readl(&port->lsr) & UART_LSR_THRE);
Masahiro Yamadabb721482014-10-24 17:00:10 +090089}
90
Masahiro Yamadad9bc8fd2014-10-24 17:00:11 +090091static int uniphier_serial_probe(struct udevice *dev)
Masahiro Yamadad064cbf2014-10-23 22:26:10 +090092{
Masahiro Yamada6d99cfa2015-08-28 20:13:19 +090093 DECLARE_GLOBAL_DATA_PTR;
Masahiro Yamadad064cbf2014-10-23 22:26:10 +090094 struct uniphier_serial_private_data *priv = dev_get_priv(dev);
Masahiro Yamada099cf772015-02-27 02:26:47 +090095 struct uniphier_serial __iomem *port;
Masahiro Yamada6d99cfa2015-08-28 20:13:19 +090096 fdt_addr_t base;
Masahiro Yamada6d99cfa2015-08-28 20:13:19 +090097 u32 tmp;
Masahiro Yamadad064cbf2014-10-23 22:26:10 +090098
Simon Glassa821c4a2017-05-17 17:18:05 -060099 base = devfdt_get_addr(dev);
Masahiro Yamadab37a1cc2016-03-24 22:32:38 +0900100 if (base == FDT_ADDR_T_NONE)
101 return -EINVAL;
Masahiro Yamada6d99cfa2015-08-28 20:13:19 +0900102
Masahiro Yamada4e3d8402016-07-19 21:56:13 +0900103 port = devm_ioremap(dev, base, SZ_64);
Masahiro Yamada099cf772015-02-27 02:26:47 +0900104 if (!port)
Masahiro Yamadad064cbf2014-10-23 22:26:10 +0900105 return -ENOMEM;
106
Masahiro Yamada099cf772015-02-27 02:26:47 +0900107 priv->membase = port;
108
Simon Glasse160f7d2017-01-17 16:52:55 -0700109 priv->uartclk = fdtdec_get_int(gd->fdt_blob, dev_of_offset(dev),
Masahiro Yamada6d99cfa2015-08-28 20:13:19 +0900110 "clock-frequency", 0);
111
Masahiro Yamada099cf772015-02-27 02:26:47 +0900112 tmp = readl(&port->lcr_mcr);
113 tmp &= ~LCR_MASK;
114 tmp |= UART_LCR_WLEN8 << LCR_SHIFT;
115 writel(tmp, &port->lcr_mcr);
116
Masahiro Yamadad064cbf2014-10-23 22:26:10 +0900117 return 0;
118}
119
Masahiro Yamada625177d2014-11-26 18:34:00 +0900120static const struct udevice_id uniphier_uart_of_match[] = {
Masahiro Yamada6462cde2015-03-11 15:54:46 +0900121 { .compatible = "socionext,uniphier-uart" },
122 { /* sentinel */ }
Masahiro Yamada7f368552014-10-03 19:21:05 +0900123};
124
Masahiro Yamadad064cbf2014-10-23 22:26:10 +0900125static const struct dm_serial_ops uniphier_serial_ops = {
126 .setbrg = uniphier_serial_setbrg,
127 .getc = uniphier_serial_getc,
128 .putc = uniphier_serial_putc,
Masahiro Yamadabb721482014-10-24 17:00:10 +0900129 .pending = uniphier_serial_pending,
Masahiro Yamadad064cbf2014-10-23 22:26:10 +0900130};
131
132U_BOOT_DRIVER(uniphier_serial) = {
Masahiro Yamada6d99cfa2015-08-28 20:13:19 +0900133 .name = "uniphier-uart",
Masahiro Yamadad064cbf2014-10-23 22:26:10 +0900134 .id = UCLASS_SERIAL,
Masahiro Yamada6d99cfa2015-08-28 20:13:19 +0900135 .of_match = uniphier_uart_of_match,
Masahiro Yamadad064cbf2014-10-23 22:26:10 +0900136 .probe = uniphier_serial_probe,
Masahiro Yamadad064cbf2014-10-23 22:26:10 +0900137 .priv_auto_alloc_size = sizeof(struct uniphier_serial_private_data),
Masahiro Yamadad064cbf2014-10-23 22:26:10 +0900138 .ops = &uniphier_serial_ops,
Masahiro Yamadad064cbf2014-10-23 22:26:10 +0900139};