blob: 8878079666de67d43e5423a780918909c61a1213 [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
Simon Glass51a3ec32017-05-17 17:18:07 -06008#include <common.h>
Simon Glass11c89f32017-05-17 17:18:03 -06009#include <dm.h>
Masahiro Yamada663a23f2015-05-29 17:30:00 +090010#include <linux/io.h>
Masahiro Yamada9caae5e2014-10-30 12:11:14 +090011#include <linux/serial_reg.h>
Masahiro Yamada6ee016e2016-03-24 22:32:38 +090012#include <linux/sizes.h>
Masahiro Yamada56a931c2016-09-21 11:28:55 +090013#include <linux/errno.h>
Masahiro Yamadab2890732014-10-03 19:21:05 +090014#include <serial.h>
Masahiro Yamadaf8cd9b02014-11-26 18:34:00 +090015#include <fdtdec.h>
Masahiro Yamadab2890732014-10-03 19:21:05 +090016
Masahiro Yamadab2890732014-10-03 19:21:05 +090017/*
18 * Note: Register map is slightly different from that of 16550.
19 */
20struct uniphier_serial {
Masahiro Yamadae7c3a892015-02-27 02:26:46 +090021 u32 rx; /* In: Receive buffer */
22#define tx rx /* Out: Transmit buffer */
23 u32 ier; /* Interrupt Enable Register */
24 u32 iir; /* In: Interrupt ID Register */
25 u32 char_fcr; /* Charactor / FIFO Control Register */
26 u32 lcr_mcr; /* Line/Modem Control Register */
27#define LCR_SHIFT 8
28#define LCR_MASK (0xff << (LCR_SHIFT))
29 u32 lsr; /* In: Line Status Register */
30 u32 msr; /* In: Modem Status Register */
31 u32 __rsv0;
32 u32 __rsv1;
33 u32 dlr; /* Divisor Latch Register */
Masahiro Yamadab2890732014-10-03 19:21:05 +090034};
35
Masahiro Yamada50c12542014-10-23 22:26:10 +090036struct uniphier_serial_private_data {
37 struct uniphier_serial __iomem *membase;
Masahiro Yamada25318dc2015-08-28 20:13:19 +090038 unsigned int uartclk;
Masahiro Yamada50c12542014-10-23 22:26:10 +090039};
40
41#define uniphier_serial_port(dev) \
42 ((struct uniphier_serial_private_data *)dev_get_priv(dev))->membase
Masahiro Yamadab2890732014-10-03 19:21:05 +090043
Masahiro Yamada0bf0c6d2014-10-24 17:00:11 +090044static int uniphier_serial_setbrg(struct udevice *dev, int baudrate)
Masahiro Yamadab2890732014-10-03 19:21:05 +090045{
Masahiro Yamada25318dc2015-08-28 20:13:19 +090046 struct uniphier_serial_private_data *priv = dev_get_priv(dev);
Masahiro Yamada50c12542014-10-23 22:26:10 +090047 struct uniphier_serial __iomem *port = uniphier_serial_port(dev);
Masahiro Yamadab2890732014-10-03 19:21:05 +090048 const unsigned int mode_x_div = 16;
49 unsigned int divisor;
Masahiro Yamadab2890732014-10-03 19:21:05 +090050
Masahiro Yamada25318dc2015-08-28 20:13:19 +090051 divisor = DIV_ROUND_CLOSEST(priv->uartclk, mode_x_div * baudrate);
Masahiro Yamadab2890732014-10-03 19:21:05 +090052
Masahiro Yamadae7c3a892015-02-27 02:26:46 +090053 writel(divisor, &port->dlr);
Masahiro Yamadab2890732014-10-03 19:21:05 +090054
Masahiro Yamada50c12542014-10-23 22:26:10 +090055 return 0;
Masahiro Yamadab2890732014-10-03 19:21:05 +090056}
57
Masahiro Yamada50c12542014-10-23 22:26:10 +090058static int uniphier_serial_getc(struct udevice *dev)
Masahiro Yamadab2890732014-10-03 19:21:05 +090059{
Masahiro Yamada50c12542014-10-23 22:26:10 +090060 struct uniphier_serial __iomem *port = uniphier_serial_port(dev);
Masahiro Yamadab2890732014-10-03 19:21:05 +090061
Masahiro Yamadae7c3a892015-02-27 02:26:46 +090062 if (!(readl(&port->lsr) & UART_LSR_DR))
Masahiro Yamada50c12542014-10-23 22:26:10 +090063 return -EAGAIN;
Masahiro Yamadab2890732014-10-03 19:21:05 +090064
Masahiro Yamadae7c3a892015-02-27 02:26:46 +090065 return readl(&port->rx);
Masahiro Yamadab2890732014-10-03 19:21:05 +090066}
67
Masahiro Yamada50c12542014-10-23 22:26:10 +090068static int uniphier_serial_putc(struct udevice *dev, const char c)
Masahiro Yamadab2890732014-10-03 19:21:05 +090069{
Masahiro Yamada50c12542014-10-23 22:26:10 +090070 struct uniphier_serial __iomem *port = uniphier_serial_port(dev);
Masahiro Yamadab2890732014-10-03 19:21:05 +090071
Masahiro Yamadae7c3a892015-02-27 02:26:46 +090072 if (!(readl(&port->lsr) & UART_LSR_THRE))
Masahiro Yamada50c12542014-10-23 22:26:10 +090073 return -EAGAIN;
Masahiro Yamadab2890732014-10-03 19:21:05 +090074
Masahiro Yamadae7c3a892015-02-27 02:26:46 +090075 writel(c, &port->tx);
Masahiro Yamada50c12542014-10-23 22:26:10 +090076
77 return 0;
Masahiro Yamadab2890732014-10-03 19:21:05 +090078}
79
Masahiro Yamadad11fbd52014-10-24 17:00:10 +090080static int uniphier_serial_pending(struct udevice *dev, bool input)
81{
82 struct uniphier_serial __iomem *port = uniphier_serial_port(dev);
83
84 if (input)
Masahiro Yamadae7c3a892015-02-27 02:26:46 +090085 return readl(&port->lsr) & UART_LSR_DR;
Masahiro Yamadad11fbd52014-10-24 17:00:10 +090086 else
Masahiro Yamadae7c3a892015-02-27 02:26:46 +090087 return !(readl(&port->lsr) & UART_LSR_THRE);
Masahiro Yamadad11fbd52014-10-24 17:00:10 +090088}
89
Masahiro Yamada0bf0c6d2014-10-24 17:00:11 +090090static int uniphier_serial_probe(struct udevice *dev)
Masahiro Yamada50c12542014-10-23 22:26:10 +090091{
Masahiro Yamada25318dc2015-08-28 20:13:19 +090092 DECLARE_GLOBAL_DATA_PTR;
Masahiro Yamada50c12542014-10-23 22:26:10 +090093 struct uniphier_serial_private_data *priv = dev_get_priv(dev);
Masahiro Yamadafef7fcd2015-02-27 02:26:47 +090094 struct uniphier_serial __iomem *port;
Masahiro Yamada25318dc2015-08-28 20:13:19 +090095 fdt_addr_t base;
Masahiro Yamada25318dc2015-08-28 20:13:19 +090096 u32 tmp;
Masahiro Yamadab2890732014-10-03 19:21:05 +090097
Simon Glassba1dea42017-05-17 17:18:05 -060098 base = devfdt_get_addr(dev);
Masahiro Yamada6ee016e2016-03-24 22:32:38 +090099 if (base == FDT_ADDR_T_NONE)
100 return -EINVAL;
Masahiro Yamada25318dc2015-08-28 20:13:19 +0900101
Masahiro Yamadafa1f73f2016-07-19 21:56:13 +0900102 port = devm_ioremap(dev, base, SZ_64);
Masahiro Yamadafef7fcd2015-02-27 02:26:47 +0900103 if (!port)
Masahiro Yamada50c12542014-10-23 22:26:10 +0900104 return -ENOMEM;
Masahiro Yamadab2890732014-10-03 19:21:05 +0900105
Masahiro Yamadafef7fcd2015-02-27 02:26:47 +0900106 priv->membase = port;
107
Simon Glassdd79d6e2017-01-17 16:52:55 -0700108 priv->uartclk = fdtdec_get_int(gd->fdt_blob, dev_of_offset(dev),
Masahiro Yamada25318dc2015-08-28 20:13:19 +0900109 "clock-frequency", 0);
110
Masahiro Yamadafef7fcd2015-02-27 02:26:47 +0900111 tmp = readl(&port->lcr_mcr);
112 tmp &= ~LCR_MASK;
113 tmp |= UART_LCR_WLEN8 << LCR_SHIFT;
114 writel(tmp, &port->lcr_mcr);
115
Masahiro Yamada50c12542014-10-23 22:26:10 +0900116 return 0;
117}
Masahiro Yamadab2890732014-10-03 19:21:05 +0900118
Masahiro Yamadaf8cd9b02014-11-26 18:34:00 +0900119static const struct udevice_id uniphier_uart_of_match[] = {
Masahiro Yamadad5f83a42015-03-11 15:54:46 +0900120 { .compatible = "socionext,uniphier-uart" },
121 { /* sentinel */ }
Masahiro Yamada50c12542014-10-23 22:26:10 +0900122};
123
Masahiro Yamada50c12542014-10-23 22:26:10 +0900124static const struct dm_serial_ops uniphier_serial_ops = {
125 .setbrg = uniphier_serial_setbrg,
126 .getc = uniphier_serial_getc,
127 .putc = uniphier_serial_putc,
Masahiro Yamadad11fbd52014-10-24 17:00:10 +0900128 .pending = uniphier_serial_pending,
Masahiro Yamada50c12542014-10-23 22:26:10 +0900129};
130
131U_BOOT_DRIVER(uniphier_serial) = {
Masahiro Yamada25318dc2015-08-28 20:13:19 +0900132 .name = "uniphier-uart",
Masahiro Yamada50c12542014-10-23 22:26:10 +0900133 .id = UCLASS_SERIAL,
Masahiro Yamada25318dc2015-08-28 20:13:19 +0900134 .of_match = uniphier_uart_of_match,
Masahiro Yamada50c12542014-10-23 22:26:10 +0900135 .probe = uniphier_serial_probe,
Masahiro Yamada50c12542014-10-23 22:26:10 +0900136 .priv_auto_alloc_size = sizeof(struct uniphier_serial_private_data),
Masahiro Yamada50c12542014-10-23 22:26:10 +0900137 .ops = &uniphier_serial_ops,
Masahiro Yamada50c12542014-10-23 22:26:10 +0900138};