blob: 3f3d4152134af0a1706535e0b11e21849442ef84 [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
8#include <common.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
40/*
41 * These are the definitions for the Line Control Register
42 */
43#define UART_LCR_WLS_8 0x03 /* 8 bit character length */
44
45/*
46 * These are the definitions for the Line Status Register
47 */
48#define UART_LSR_DR 0x01 /* Data ready */
49#define UART_LSR_THRE 0x20 /* Xmit holding register empty */
50
Masahiro Yamadad064cbf2014-10-23 22:26:10 +090051struct uniphier_serial_private_data {
52 struct uniphier_serial __iomem *membase;
53};
Masahiro Yamada7f368552014-10-03 19:21:05 +090054
Masahiro Yamadad064cbf2014-10-23 22:26:10 +090055#define uniphier_serial_port(dev) \
56 ((struct uniphier_serial_private_data *)dev_get_priv(dev))->membase
57
Masahiro Yamadad9bc8fd2014-10-24 17:00:11 +090058static int uniphier_serial_setbrg(struct udevice *dev, int baudrate)
Masahiro Yamada7f368552014-10-03 19:21:05 +090059{
Masahiro Yamadad064cbf2014-10-23 22:26:10 +090060 struct uniphier_serial_platform_data *plat = dev_get_platdata(dev);
61 struct uniphier_serial __iomem *port = uniphier_serial_port(dev);
Masahiro Yamada7f368552014-10-03 19:21:05 +090062 const unsigned int mode_x_div = 16;
63 unsigned int divisor;
64
65 writeb(UART_LCR_WLS_8, &port->lcr);
66
Masahiro Yamadad064cbf2014-10-23 22:26:10 +090067 divisor = DIV_ROUND_CLOSEST(plat->uartclk, mode_x_div * baudrate);
Masahiro Yamada7f368552014-10-03 19:21:05 +090068
69 writew(divisor, &port->dlr);
Masahiro Yamadad064cbf2014-10-23 22:26:10 +090070
71 return 0;
Masahiro Yamada7f368552014-10-03 19:21:05 +090072}
73
Masahiro Yamadad064cbf2014-10-23 22:26:10 +090074static int uniphier_serial_getc(struct udevice *dev)
Masahiro Yamada7f368552014-10-03 19:21:05 +090075{
Masahiro Yamadad064cbf2014-10-23 22:26:10 +090076 struct uniphier_serial __iomem *port = uniphier_serial_port(dev);
Masahiro Yamada7f368552014-10-03 19:21:05 +090077
Masahiro Yamadad064cbf2014-10-23 22:26:10 +090078 if (!(readb(&port->lsr) & UART_LSR_DR))
79 return -EAGAIN;
Masahiro Yamada7f368552014-10-03 19:21:05 +090080
81 return readb(&port->rbr);
82}
83
Masahiro Yamadad064cbf2014-10-23 22:26:10 +090084static int uniphier_serial_putc(struct udevice *dev, const char c)
Masahiro Yamada7f368552014-10-03 19:21:05 +090085{
Masahiro Yamadad064cbf2014-10-23 22:26:10 +090086 struct uniphier_serial __iomem *port = uniphier_serial_port(dev);
Masahiro Yamada7f368552014-10-03 19:21:05 +090087
Masahiro Yamadad064cbf2014-10-23 22:26:10 +090088 if (!(readb(&port->lsr) & UART_LSR_THRE))
89 return -EAGAIN;
Masahiro Yamada7f368552014-10-03 19:21:05 +090090
91 writeb(c, &port->thr);
Masahiro Yamadad064cbf2014-10-23 22:26:10 +090092
93 return 0;
Masahiro Yamada7f368552014-10-03 19:21:05 +090094}
95
Masahiro Yamadabb721482014-10-24 17:00:10 +090096static int uniphier_serial_pending(struct udevice *dev, bool input)
97{
98 struct uniphier_serial __iomem *port = uniphier_serial_port(dev);
99
100 if (input)
101 return readb(&port->lsr) & UART_LSR_DR;
102 else
103 return !(readb(&port->lsr) & UART_LSR_THRE);
104}
105
Masahiro Yamadad9bc8fd2014-10-24 17:00:11 +0900106static int uniphier_serial_probe(struct udevice *dev)
Masahiro Yamadad064cbf2014-10-23 22:26:10 +0900107{
108 struct uniphier_serial_private_data *priv = dev_get_priv(dev);
109 struct uniphier_serial_platform_data *plat = dev_get_platdata(dev);
110
111 priv->membase = map_sysmem(plat->base, sizeof(struct uniphier_serial));
112
113 if (!priv->membase)
114 return -ENOMEM;
115
116 return 0;
117}
118
Masahiro Yamadad9bc8fd2014-10-24 17:00:11 +0900119static int uniphier_serial_remove(struct udevice *dev)
Masahiro Yamadad064cbf2014-10-23 22:26:10 +0900120{
121 unmap_sysmem(uniphier_serial_port(dev));
122
123 return 0;
124}
125
126#ifdef CONFIG_OF_CONTROL
127static const struct udevice_id uniphier_uart_of_match = {
128 { .compatible = "panasonic,uniphier-uart"},
129 {},
Masahiro Yamada7f368552014-10-03 19:21:05 +0900130};
131
Masahiro Yamadad064cbf2014-10-23 22:26:10 +0900132static int uniphier_serial_ofdata_to_platdata(struct udevice *dev)
Masahiro Yamada7f368552014-10-03 19:21:05 +0900133{
Masahiro Yamadad064cbf2014-10-23 22:26:10 +0900134 /*
135 * TODO: Masahiro Yamada (yamada.m@jp.panasonic.com)
136 *
137 * Implement conversion code from DTB to platform data
138 * when supporting CONFIG_OF_CONTROL on UniPhir platform.
139 */
Masahiro Yamada7f368552014-10-03 19:21:05 +0900140}
Masahiro Yamadad064cbf2014-10-23 22:26:10 +0900141#endif
Masahiro Yamada7f368552014-10-03 19:21:05 +0900142
Masahiro Yamadad064cbf2014-10-23 22:26:10 +0900143static const struct dm_serial_ops uniphier_serial_ops = {
144 .setbrg = uniphier_serial_setbrg,
145 .getc = uniphier_serial_getc,
146 .putc = uniphier_serial_putc,
Masahiro Yamadabb721482014-10-24 17:00:10 +0900147 .pending = uniphier_serial_pending,
Masahiro Yamadad064cbf2014-10-23 22:26:10 +0900148};
149
150U_BOOT_DRIVER(uniphier_serial) = {
151 .name = DRIVER_NAME,
152 .id = UCLASS_SERIAL,
153 .of_match = of_match_ptr(uniphier_uart_of_match),
154 .ofdata_to_platdata = of_match_ptr(uniphier_serial_ofdata_to_platdata),
155 .probe = uniphier_serial_probe,
156 .remove = uniphier_serial_remove,
157 .priv_auto_alloc_size = sizeof(struct uniphier_serial_private_data),
158 .platdata_auto_alloc_size =
159 sizeof(struct uniphier_serial_platform_data),
160 .ops = &uniphier_serial_ops,
161 .flags = DM_FLAG_PRE_RELOC,
162};