blob: 6046efb51fe87b8711bf7e4c13e007ead20e3647 [file] [log] [blame]
Masahiro Yamada7f368552014-10-03 19:21:05 +09001/*
2 * Copyright (C) 2012-2014 Panasonic Corporation
3 * Author: Masahiro Yamada <yamada.m@jp.panasonic.com>
4 *
Masahiro Yamada7f368552014-10-03 19:21:05 +09005 * SPDX-License-Identifier: GPL-2.0+
6 */
7
Masahiro Yamada325b7082014-10-30 12:11:14 +09008#include <linux/serial_reg.h>
Masahiro Yamadad064cbf2014-10-23 22:26:10 +09009#include <asm/io.h>
10#include <asm/errno.h>
11#include <dm/device.h>
12#include <dm/platform_data/serial-uniphier.h>
Masahiro Yamada7f368552014-10-03 19:21:05 +090013#include <serial.h>
14
15#define UART_REG(x) \
16 u8 x; \
17 u8 postpad_##x[3];
18
19/*
20 * Note: Register map is slightly different from that of 16550.
21 */
22struct uniphier_serial {
23 UART_REG(rbr); /* 0x00 */
24 UART_REG(ier); /* 0x04 */
25 UART_REG(iir); /* 0x08 */
26 UART_REG(fcr); /* 0x0c */
27 u8 mcr; /* 0x10 */
28 u8 lcr;
29 u16 __postpad;
30 UART_REG(lsr); /* 0x14 */
31 UART_REG(msr); /* 0x18 */
32 u32 __none1;
33 u32 __none2;
34 u16 dlr;
35 u16 __postpad2;
36};
37
38#define thr rbr
39
Masahiro Yamadad064cbf2014-10-23 22:26:10 +090040struct uniphier_serial_private_data {
41 struct uniphier_serial __iomem *membase;
42};
Masahiro Yamada7f368552014-10-03 19:21:05 +090043
Masahiro Yamadad064cbf2014-10-23 22:26:10 +090044#define uniphier_serial_port(dev) \
45 ((struct uniphier_serial_private_data *)dev_get_priv(dev))->membase
46
Masahiro Yamadad9bc8fd2014-10-24 17:00:11 +090047static int uniphier_serial_setbrg(struct udevice *dev, int baudrate)
Masahiro Yamada7f368552014-10-03 19:21:05 +090048{
Masahiro Yamadad064cbf2014-10-23 22:26:10 +090049 struct uniphier_serial_platform_data *plat = dev_get_platdata(dev);
50 struct uniphier_serial __iomem *port = uniphier_serial_port(dev);
Masahiro Yamada7f368552014-10-03 19:21:05 +090051 const unsigned int mode_x_div = 16;
52 unsigned int divisor;
53
Masahiro Yamada325b7082014-10-30 12:11:14 +090054 writeb(UART_LCR_WLEN8, &port->lcr);
Masahiro Yamada7f368552014-10-03 19:21:05 +090055
Masahiro Yamadad064cbf2014-10-23 22:26:10 +090056 divisor = DIV_ROUND_CLOSEST(plat->uartclk, mode_x_div * baudrate);
Masahiro Yamada7f368552014-10-03 19:21:05 +090057
58 writew(divisor, &port->dlr);
Masahiro Yamadad064cbf2014-10-23 22:26:10 +090059
60 return 0;
Masahiro Yamada7f368552014-10-03 19:21:05 +090061}
62
Masahiro Yamadad064cbf2014-10-23 22:26:10 +090063static int uniphier_serial_getc(struct udevice *dev)
Masahiro Yamada7f368552014-10-03 19:21:05 +090064{
Masahiro Yamadad064cbf2014-10-23 22:26:10 +090065 struct uniphier_serial __iomem *port = uniphier_serial_port(dev);
Masahiro Yamada7f368552014-10-03 19:21:05 +090066
Masahiro Yamadad064cbf2014-10-23 22:26:10 +090067 if (!(readb(&port->lsr) & UART_LSR_DR))
68 return -EAGAIN;
Masahiro Yamada7f368552014-10-03 19:21:05 +090069
70 return readb(&port->rbr);
71}
72
Masahiro Yamadad064cbf2014-10-23 22:26:10 +090073static int uniphier_serial_putc(struct udevice *dev, const char c)
Masahiro Yamada7f368552014-10-03 19:21:05 +090074{
Masahiro Yamadad064cbf2014-10-23 22:26:10 +090075 struct uniphier_serial __iomem *port = uniphier_serial_port(dev);
Masahiro Yamada7f368552014-10-03 19:21:05 +090076
Masahiro Yamadad064cbf2014-10-23 22:26:10 +090077 if (!(readb(&port->lsr) & UART_LSR_THRE))
78 return -EAGAIN;
Masahiro Yamada7f368552014-10-03 19:21:05 +090079
80 writeb(c, &port->thr);
Masahiro Yamadad064cbf2014-10-23 22:26:10 +090081
82 return 0;
Masahiro Yamada7f368552014-10-03 19:21:05 +090083}
84
Masahiro Yamadabb721482014-10-24 17:00:10 +090085static int uniphier_serial_pending(struct udevice *dev, bool input)
86{
87 struct uniphier_serial __iomem *port = uniphier_serial_port(dev);
88
89 if (input)
90 return readb(&port->lsr) & UART_LSR_DR;
91 else
92 return !(readb(&port->lsr) & UART_LSR_THRE);
93}
94
Masahiro Yamadad9bc8fd2014-10-24 17:00:11 +090095static int uniphier_serial_probe(struct udevice *dev)
Masahiro Yamadad064cbf2014-10-23 22:26:10 +090096{
97 struct uniphier_serial_private_data *priv = dev_get_priv(dev);
98 struct uniphier_serial_platform_data *plat = dev_get_platdata(dev);
99
100 priv->membase = map_sysmem(plat->base, sizeof(struct uniphier_serial));
101
102 if (!priv->membase)
103 return -ENOMEM;
104
105 return 0;
106}
107
Masahiro Yamadad9bc8fd2014-10-24 17:00:11 +0900108static int uniphier_serial_remove(struct udevice *dev)
Masahiro Yamadad064cbf2014-10-23 22:26:10 +0900109{
110 unmap_sysmem(uniphier_serial_port(dev));
111
112 return 0;
113}
114
115#ifdef CONFIG_OF_CONTROL
116static const struct udevice_id uniphier_uart_of_match = {
117 { .compatible = "panasonic,uniphier-uart"},
118 {},
Masahiro Yamada7f368552014-10-03 19:21:05 +0900119};
120
Masahiro Yamadad064cbf2014-10-23 22:26:10 +0900121static int uniphier_serial_ofdata_to_platdata(struct udevice *dev)
Masahiro Yamada7f368552014-10-03 19:21:05 +0900122{
Masahiro Yamadad064cbf2014-10-23 22:26:10 +0900123 /*
124 * TODO: Masahiro Yamada (yamada.m@jp.panasonic.com)
125 *
126 * Implement conversion code from DTB to platform data
127 * when supporting CONFIG_OF_CONTROL on UniPhir platform.
128 */
Masahiro Yamada7f368552014-10-03 19:21:05 +0900129}
Masahiro Yamadad064cbf2014-10-23 22:26:10 +0900130#endif
Masahiro Yamada7f368552014-10-03 19:21:05 +0900131
Masahiro Yamadad064cbf2014-10-23 22:26:10 +0900132static const struct dm_serial_ops uniphier_serial_ops = {
133 .setbrg = uniphier_serial_setbrg,
134 .getc = uniphier_serial_getc,
135 .putc = uniphier_serial_putc,
Masahiro Yamadabb721482014-10-24 17:00:10 +0900136 .pending = uniphier_serial_pending,
Masahiro Yamadad064cbf2014-10-23 22:26:10 +0900137};
138
139U_BOOT_DRIVER(uniphier_serial) = {
140 .name = DRIVER_NAME,
141 .id = UCLASS_SERIAL,
142 .of_match = of_match_ptr(uniphier_uart_of_match),
143 .ofdata_to_platdata = of_match_ptr(uniphier_serial_ofdata_to_platdata),
144 .probe = uniphier_serial_probe,
145 .remove = uniphier_serial_remove,
146 .priv_auto_alloc_size = sizeof(struct uniphier_serial_private_data),
147 .platdata_auto_alloc_size =
148 sizeof(struct uniphier_serial_platform_data),
149 .ops = &uniphier_serial_ops,
150 .flags = DM_FLAG_PRE_RELOC,
151};