2 * Copyright (C) 2012-2014 Panasonic Corporation
3 * Author: Masahiro Yamada <yamada.m@jp.panasonic.com>
5 * Based on serial_ns16550.c
7 * Rob Taylor, Flying Pig Systems. robt@flyingpig.com.
9 * SPDX-License-Identifier: GPL-2.0+
20 * Note: Register map is slightly different from that of 16550.
22 struct uniphier_serial {
23 UART_REG(rbr); /* 0x00 */
24 UART_REG(ier); /* 0x04 */
25 UART_REG(iir); /* 0x08 */
26 UART_REG(fcr); /* 0x0c */
30 UART_REG(lsr); /* 0x14 */
31 UART_REG(msr); /* 0x18 */
41 * These are the definitions for the Line Control Register
43 #define UART_LCR_WLS_8 0x03 /* 8 bit character length */
46 * These are the definitions for the Line Status Register
48 #define UART_LSR_DR 0x01 /* Data ready */
49 #define UART_LSR_THRE 0x20 /* Xmit holding register empty */
51 DECLARE_GLOBAL_DATA_PTR;
53 static void uniphier_serial_init(struct uniphier_serial *port)
55 const unsigned int mode_x_div = 16;
58 writeb(UART_LCR_WLS_8, &port->lcr);
60 divisor = DIV_ROUND_CLOSEST(CONFIG_SYS_UNIPHIER_UART_CLK,
61 mode_x_div * gd->baudrate);
63 writew(divisor, &port->dlr);
66 static void uniphier_serial_setbrg(struct uniphier_serial *port)
68 uniphier_serial_init(port);
71 static int uniphier_serial_tstc(struct uniphier_serial *port)
73 return (readb(&port->lsr) & UART_LSR_DR) != 0;
76 static int uniphier_serial_getc(struct uniphier_serial *port)
78 while (!uniphier_serial_tstc(port))
81 return readb(&port->rbr);
84 static void uniphier_serial_putc(struct uniphier_serial *port, const char c)
87 uniphier_serial_putc(port, '\r');
89 while (!(readb(&port->lsr) & UART_LSR_THRE))
92 writeb(c, &port->thr);
95 static struct uniphier_serial *serial_ports[4] = {
96 #ifdef CONFIG_SYS_UNIPHIER_SERIAL_BASE0
97 (struct uniphier_serial *)CONFIG_SYS_UNIPHIER_SERIAL_BASE0,
101 #ifdef CONFIG_SYS_UNIPHIER_SERIAL_BASE1
102 (struct uniphier_serial *)CONFIG_SYS_UNIPHIER_SERIAL_BASE1,
106 #ifdef CONFIG_SYS_UNIPHIER_SERIAL_BASE2
107 (struct uniphier_serial *)CONFIG_SYS_UNIPHIER_SERIAL_BASE2,
111 #ifdef CONFIG_SYS_UNIPHIER_SERIAL_BASE3
112 (struct uniphier_serial *)CONFIG_SYS_UNIPHIER_SERIAL_BASE3,
118 /* Multi serial device functions */
119 #define DECLARE_ESERIAL_FUNCTIONS(port) \
120 static int eserial##port##_init(void) \
122 uniphier_serial_init(serial_ports[port]); \
125 static void eserial##port##_setbrg(void) \
127 uniphier_serial_setbrg(serial_ports[port]); \
129 static int eserial##port##_getc(void) \
131 return uniphier_serial_getc(serial_ports[port]); \
133 static int eserial##port##_tstc(void) \
135 return uniphier_serial_tstc(serial_ports[port]); \
137 static void eserial##port##_putc(const char c) \
139 uniphier_serial_putc(serial_ports[port], c); \
142 /* Serial device descriptor */
143 #define INIT_ESERIAL_STRUCTURE(port, __name) { \
145 .start = eserial##port##_init, \
147 .setbrg = eserial##port##_setbrg, \
148 .getc = eserial##port##_getc, \
149 .tstc = eserial##port##_tstc, \
150 .putc = eserial##port##_putc, \
151 .puts = default_serial_puts, \
154 #if defined(CONFIG_SYS_UNIPHIER_SERIAL_BASE0)
155 DECLARE_ESERIAL_FUNCTIONS(0);
156 struct serial_device uniphier_serial0_device =
157 INIT_ESERIAL_STRUCTURE(0, "ttyS0");
159 #if defined(CONFIG_SYS_UNIPHIER_SERIAL_BASE1)
160 DECLARE_ESERIAL_FUNCTIONS(1);
161 struct serial_device uniphier_serial1_device =
162 INIT_ESERIAL_STRUCTURE(1, "ttyS1");
164 #if defined(CONFIG_SYS_UNIPHIER_SERIAL_BASE2)
165 DECLARE_ESERIAL_FUNCTIONS(2);
166 struct serial_device uniphier_serial2_device =
167 INIT_ESERIAL_STRUCTURE(2, "ttyS2");
169 #if defined(CONFIG_SYS_UNIPHIER_SERIAL_BASE3)
170 DECLARE_ESERIAL_FUNCTIONS(3);
171 struct serial_device uniphier_serial3_device =
172 INIT_ESERIAL_STRUCTURE(3, "ttyS3");
175 __weak struct serial_device *default_serial_console(void)
177 #if defined(CONFIG_SYS_UNIPHIER_SERIAL_BASE0)
178 return &uniphier_serial0_device;
179 #elif defined(CONFIG_SYS_UNIPHIER_SERIAL_BASE1)
180 return &uniphier_serial1_device;
181 #elif defined(CONFIG_SYS_UNIPHIER_SERIAL_BASE2)
182 return &uniphier_serial2_device;
183 #elif defined(CONFIG_SYS_UNIPHIER_SERIAL_BASE3)
184 return &uniphier_serial3_device;
186 #error "No uniphier serial ports configured."
190 void uniphier_serial_initialize(void)
192 #if defined(CONFIG_SYS_UNIPHIER_SERIAL_BASE0)
193 serial_register(&uniphier_serial0_device);
195 #if defined(CONFIG_SYS_UNIPHIER_SERIAL_BASE1)
196 serial_register(&uniphier_serial1_device);
198 #if defined(CONFIG_SYS_UNIPHIER_SERIAL_BASE2)
199 serial_register(&uniphier_serial2_device);
201 #if defined(CONFIG_SYS_UNIPHIER_SERIAL_BASE3)
202 serial_register(&uniphier_serial3_device);