blob: a92d2b1de882f05c570377610b6ca74fed3524f9 [file] [log] [blame]
wdenke85390d2002-04-01 14:29:03 +00001/*
2 * COM1 NS16550 support
Stefan Roesea47a12b2010-04-15 16:07:28 +02003 * originally from linux source (arch/powerpc/boot/ns16550.c)
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +02004 * modified to use CONFIG_SYS_ISA_MEM and new defines
wdenke85390d2002-04-01 14:29:03 +00005 */
6
Simon Glassd96c2602019-12-28 10:44:58 -07007#include <clock_legacy.h>
Simon Glassfa54eb12014-09-04 16:27:32 -06008#include <common.h>
Paul Burton50fce1d2016-09-08 07:47:29 +01009#include <clk.h>
Simon Glass12e431b2014-09-04 16:27:34 -060010#include <dm.h>
11#include <errno.h>
wdenke85390d2002-04-01 14:29:03 +000012#include <ns16550.h>
Ley Foon Tanb051eec2018-06-14 18:45:22 +080013#include <reset.h>
Simon Glass12e431b2014-09-04 16:27:34 -060014#include <serial.h>
Ladislav Michla1b322a2010-02-01 23:34:25 +010015#include <watchdog.h>
Graeme Russ167cdad2010-04-24 00:05:46 +100016#include <linux/types.h>
17#include <asm/io.h>
wdenke85390d2002-04-01 14:29:03 +000018
Simon Glass12e431b2014-09-04 16:27:34 -060019DECLARE_GLOBAL_DATA_PTR;
20
Detlev Zundel200779e2009-04-03 11:53:01 +020021#define UART_LCRVAL UART_LCR_8N1 /* 8 data, 1 stop, no parity */
22#define UART_MCRVAL (UART_MCR_DTR | \
23 UART_MCR_RTS) /* RTS/DTR */
Simon Glass12e431b2014-09-04 16:27:34 -060024
Simon Glass2e2c5142019-09-25 08:11:14 -060025#if !CONFIG_IS_ENABLED(DM_SERIAL)
Graeme Russ167cdad2010-04-24 00:05:46 +100026#ifdef CONFIG_SYS_NS16550_PORT_MAPPED
Simon Glassf8df9d02011-10-15 19:14:09 +000027#define serial_out(x, y) outb(x, (ulong)y)
28#define serial_in(y) inb((ulong)y)
Dave Aldridge79df1202011-09-01 22:47:14 +000029#elif defined(CONFIG_SYS_NS16550_MEM32) && (CONFIG_SYS_NS16550_REG_SIZE > 0)
Simon Glassf8df9d02011-10-15 19:14:09 +000030#define serial_out(x, y) out_be32(y, x)
31#define serial_in(y) in_be32(y)
Dave Aldridge79df1202011-09-01 22:47:14 +000032#elif defined(CONFIG_SYS_NS16550_MEM32) && (CONFIG_SYS_NS16550_REG_SIZE < 0)
Simon Glassf8df9d02011-10-15 19:14:09 +000033#define serial_out(x, y) out_le32(y, x)
34#define serial_in(y) in_le32(y)
Graeme Russ167cdad2010-04-24 00:05:46 +100035#else
Simon Glassf8df9d02011-10-15 19:14:09 +000036#define serial_out(x, y) writeb(x, y)
37#define serial_in(y) readb(y)
Graeme Russ167cdad2010-04-24 00:05:46 +100038#endif
Simon Glass12e431b2014-09-04 16:27:34 -060039#endif /* !CONFIG_DM_SERIAL */
wdenke85390d2002-04-01 14:29:03 +000040
Khoronzhuk, Ivan7c387642014-07-16 00:59:25 +030041#if defined(CONFIG_SOC_KEYSTONE)
Vitaly Andrianovef509b92014-04-04 13:16:53 -040042#define UART_REG_VAL_PWREMU_MGMT_UART_DISABLE 0
43#define UART_REG_VAL_PWREMU_MGMT_UART_ENABLE ((1 << 14) | (1 << 13) | (1 << 0))
Karicheri, Muralidharand57dee52014-04-09 15:38:46 -040044#undef UART_MCRVAL
45#ifdef CONFIG_SERIAL_HW_FLOW_CONTROL
46#define UART_MCRVAL (UART_MCR_RTS | UART_MCR_AFE)
47#else
48#define UART_MCRVAL (UART_MCR_RTS)
49#endif
Vitaly Andrianovef509b92014-04-04 13:16:53 -040050#endif
51
Prafulla Wadaskara160ea02010-10-27 21:58:31 +053052#ifndef CONFIG_SYS_NS16550_IER
53#define CONFIG_SYS_NS16550_IER 0x00
54#endif /* CONFIG_SYS_NS16550_IER */
55
Simon Glass363e6da2015-02-27 22:06:26 -070056static inline void serial_out_shift(void *addr, int shift, int value)
Simon Glass76571672015-01-26 18:27:08 -070057{
58#ifdef CONFIG_SYS_NS16550_PORT_MAPPED
59 outb(value, (ulong)addr);
Bernhard Messerklinger78b7d372018-02-15 09:02:26 +010060#elif defined(CONFIG_SYS_NS16550_MEM32) && defined(CONFIG_SYS_LITTLE_ENDIAN)
Simon Glass76571672015-01-26 18:27:08 -070061 out_le32(addr, value);
62#elif defined(CONFIG_SYS_NS16550_MEM32) && defined(CONFIG_SYS_BIG_ENDIAN)
63 out_be32(addr, value);
Simon Glass90914002015-05-12 14:55:02 -060064#elif defined(CONFIG_SYS_NS16550_MEM32)
65 writel(value, addr);
Simon Glass76571672015-01-26 18:27:08 -070066#elif defined(CONFIG_SYS_BIG_ENDIAN)
67 writeb(value, addr + (1 << shift) - 1);
68#else
69 writeb(value, addr);
70#endif
71}
72
Simon Glass363e6da2015-02-27 22:06:26 -070073static inline int serial_in_shift(void *addr, int shift)
Simon Glass76571672015-01-26 18:27:08 -070074{
75#ifdef CONFIG_SYS_NS16550_PORT_MAPPED
76 return inb((ulong)addr);
Bernhard Messerklinger78b7d372018-02-15 09:02:26 +010077#elif defined(CONFIG_SYS_NS16550_MEM32) && defined(CONFIG_SYS_LITTLE_ENDIAN)
Simon Glass76571672015-01-26 18:27:08 -070078 return in_le32(addr);
79#elif defined(CONFIG_SYS_NS16550_MEM32) && defined(CONFIG_SYS_BIG_ENDIAN)
80 return in_be32(addr);
Simon Glass90914002015-05-12 14:55:02 -060081#elif defined(CONFIG_SYS_NS16550_MEM32)
82 return readl(addr);
Simon Glass76571672015-01-26 18:27:08 -070083#elif defined(CONFIG_SYS_BIG_ENDIAN)
Axel Lin20379c12015-02-28 15:55:36 +080084 return readb(addr + (1 << shift) - 1);
Simon Glass76571672015-01-26 18:27:08 -070085#else
86 return readb(addr);
87#endif
88}
89
Simon Glass2e2c5142019-09-25 08:11:14 -060090#if CONFIG_IS_ENABLED(DM_SERIAL)
Marek Vasutfa4ce722016-05-25 02:13:03 +020091
92#ifndef CONFIG_SYS_NS16550_CLK
93#define CONFIG_SYS_NS16550_CLK 0
94#endif
95
Simon Glass12e431b2014-09-04 16:27:34 -060096static void ns16550_writeb(NS16550_t port, int offset, int value)
97{
98 struct ns16550_platdata *plat = port->plat;
99 unsigned char *addr;
100
101 offset *= 1 << plat->reg_shift;
Paul Burtondf8ec552016-05-17 07:43:26 +0100102 addr = (unsigned char *)plat->base + offset;
103
Simon Glass12e431b2014-09-04 16:27:34 -0600104 /*
105 * As far as we know it doesn't make sense to support selection of
106 * these options at run-time, so use the existing CONFIG options.
107 */
Michal Simek59b35dd2016-02-16 16:17:49 +0100108 serial_out_shift(addr + plat->reg_offset, plat->reg_shift, value);
Simon Glass12e431b2014-09-04 16:27:34 -0600109}
110
111static int ns16550_readb(NS16550_t port, int offset)
112{
113 struct ns16550_platdata *plat = port->plat;
114 unsigned char *addr;
115
116 offset *= 1 << plat->reg_shift;
Paul Burtondf8ec552016-05-17 07:43:26 +0100117 addr = (unsigned char *)plat->base + offset;
Simon Glass76571672015-01-26 18:27:08 -0700118
Michal Simek59b35dd2016-02-16 16:17:49 +0100119 return serial_in_shift(addr + plat->reg_offset, plat->reg_shift);
Simon Glass12e431b2014-09-04 16:27:34 -0600120}
121
Marek Vasut65f83802016-12-01 02:06:29 +0100122static u32 ns16550_getfcr(NS16550_t port)
123{
124 struct ns16550_platdata *plat = port->plat;
125
126 return plat->fcr;
127}
128
Simon Glass12e431b2014-09-04 16:27:34 -0600129/* We can clean these up once everything is moved to driver model */
130#define serial_out(value, addr) \
Simon Glass363e6da2015-02-27 22:06:26 -0700131 ns16550_writeb(com_port, \
132 (unsigned char *)addr - (unsigned char *)com_port, value)
Simon Glass12e431b2014-09-04 16:27:34 -0600133#define serial_in(addr) \
Simon Glass363e6da2015-02-27 22:06:26 -0700134 ns16550_readb(com_port, \
135 (unsigned char *)addr - (unsigned char *)com_port)
Marek Vasut65f83802016-12-01 02:06:29 +0100136#else
137static u32 ns16550_getfcr(NS16550_t port)
138{
Heiko Schocher17fa0322017-01-18 08:05:49 +0100139 return UART_FCR_DEFVAL;
Marek Vasut65f83802016-12-01 02:06:29 +0100140}
Simon Glass12e431b2014-09-04 16:27:34 -0600141#endif
142
Marek Vasut03c6f172016-05-25 02:13:16 +0200143int ns16550_calc_divisor(NS16550_t port, int clock, int baudrate)
Simon Glassfa54eb12014-09-04 16:27:32 -0600144{
145 const unsigned int mode_x_div = 16;
146
Simon Glass21d00432015-01-26 18:27:09 -0700147 return DIV_ROUND_CLOSEST(clock, mode_x_div * baudrate);
148}
149
Simon Glass8bbe33c2014-09-04 16:27:33 -0600150static void NS16550_setbrg(NS16550_t com_port, int baud_divisor)
151{
Simon Goldschmidt9ad3b042018-11-02 21:28:08 +0100152 /* to keep serial format, read lcr before writing BKSE */
153 int lcr_val = serial_in(&com_port->lcr) & ~UART_LCR_BKSE;
154
155 serial_out(UART_LCR_BKSE | lcr_val, &com_port->lcr);
Simon Glass8bbe33c2014-09-04 16:27:33 -0600156 serial_out(baud_divisor & 0xff, &com_port->dll);
157 serial_out((baud_divisor >> 8) & 0xff, &com_port->dlm);
Simon Goldschmidt9ad3b042018-11-02 21:28:08 +0100158 serial_out(lcr_val, &com_port->lcr);
Simon Glass8bbe33c2014-09-04 16:27:33 -0600159}
160
Simon Glassf8df9d02011-10-15 19:14:09 +0000161void NS16550_init(NS16550_t com_port, int baud_divisor)
wdenke85390d2002-04-01 14:29:03 +0000162{
Gregoire Gentil956a8ba2014-11-10 11:04:10 -0800163#if (defined(CONFIG_SPL_BUILD) && \
164 (defined(CONFIG_OMAP34XX) || defined(CONFIG_OMAP44XX)))
Manfred Huberfd2aeac2013-03-29 02:52:36 +0000165 /*
Gregoire Gentil956a8ba2014-11-10 11:04:10 -0800166 * On some OMAP3/OMAP4 devices when UART3 is configured for boot mode
167 * before SPL starts only THRE bit is set. We have to empty the
168 * transmitter before initialization starts.
Manfred Huberfd2aeac2013-03-29 02:52:36 +0000169 */
170 if ((serial_in(&com_port->lsr) & (UART_LSR_TEMT | UART_LSR_THRE))
171 == UART_LSR_THRE) {
Simon Glass12e431b2014-09-04 16:27:34 -0600172 if (baud_divisor != -1)
173 NS16550_setbrg(com_port, baud_divisor);
Manfred Huberfd2aeac2013-03-29 02:52:36 +0000174 serial_out(0, &com_port->mdr1);
175 }
176#endif
177
Scott Woodcb55b332012-09-18 18:19:05 -0500178 while (!(serial_in(&com_port->lsr) & UART_LSR_TEMT))
179 ;
180
Prafulla Wadaskara160ea02010-10-27 21:58:31 +0530181 serial_out(CONFIG_SYS_NS16550_IER, &com_port->ier);
Lokesh Vutla5d754192018-08-27 15:55:24 +0530182#if defined(CONFIG_ARCH_OMAP2PLUS) || defined(CONFIG_OMAP_SERIAL)
Graeme Russ167cdad2010-04-24 00:05:46 +1000183 serial_out(0x7, &com_port->mdr1); /* mode select reset TL16C750*/
wdenk945af8d2003-07-16 21:53:01 +0000184#endif
Ley Foon Tanb051eec2018-06-14 18:45:22 +0800185
Graeme Russ167cdad2010-04-24 00:05:46 +1000186 serial_out(UART_MCRVAL, &com_port->mcr);
Marek Vasut65f83802016-12-01 02:06:29 +0100187 serial_out(ns16550_getfcr(com_port), &com_port->fcr);
Simon Goldschmidt9ad3b042018-11-02 21:28:08 +0100188 /* initialize serial config to 8N1 before writing baudrate */
189 serial_out(UART_LCRVAL, &com_port->lcr);
Simon Glass12e431b2014-09-04 16:27:34 -0600190 if (baud_divisor != -1)
191 NS16550_setbrg(com_port, baud_divisor);
Lokesh Vutla5d754192018-08-27 15:55:24 +0530192#if defined(CONFIG_ARCH_OMAP2PLUS) || defined(CONFIG_SOC_DA8XX) || \
193 defined(CONFIG_OMAP_SERIAL)
Simon Glassf8df9d02011-10-15 19:14:09 +0000194 /* /16 is proper to hit 115200 with 48MHz */
195 serial_out(0, &com_port->mdr1);
Tom Rini89024dd2017-05-12 22:33:16 -0400196#endif
Khoronzhuk, Ivan7c387642014-07-16 00:59:25 +0300197#if defined(CONFIG_SOC_KEYSTONE)
Vitaly Andrianovef509b92014-04-04 13:16:53 -0400198 serial_out(UART_REG_VAL_PWREMU_MGMT_UART_ENABLE, &com_port->regC);
199#endif
wdenke85390d2002-04-01 14:29:03 +0000200}
201
Ron Madridf5675aa2009-02-18 14:30:44 -0800202#ifndef CONFIG_NS16550_MIN_FUNCTIONS
Simon Glassf8df9d02011-10-15 19:14:09 +0000203void NS16550_reinit(NS16550_t com_port, int baud_divisor)
wdenke85390d2002-04-01 14:29:03 +0000204{
Prafulla Wadaskara160ea02010-10-27 21:58:31 +0530205 serial_out(CONFIG_SYS_NS16550_IER, &com_port->ier);
Simon Glass8bbe33c2014-09-04 16:27:33 -0600206 NS16550_setbrg(com_port, 0);
Graeme Russ167cdad2010-04-24 00:05:46 +1000207 serial_out(UART_MCRVAL, &com_port->mcr);
Marek Vasut65f83802016-12-01 02:06:29 +0100208 serial_out(ns16550_getfcr(com_port), &com_port->fcr);
Simon Glass8bbe33c2014-09-04 16:27:33 -0600209 NS16550_setbrg(com_port, baud_divisor);
wdenke85390d2002-04-01 14:29:03 +0000210}
Ron Madridf5675aa2009-02-18 14:30:44 -0800211#endif /* CONFIG_NS16550_MIN_FUNCTIONS */
wdenke85390d2002-04-01 14:29:03 +0000212
Simon Glassf8df9d02011-10-15 19:14:09 +0000213void NS16550_putc(NS16550_t com_port, char c)
wdenke85390d2002-04-01 14:29:03 +0000214{
Simon Glassf8df9d02011-10-15 19:14:09 +0000215 while ((serial_in(&com_port->lsr) & UART_LSR_THRE) == 0)
216 ;
Graeme Russ167cdad2010-04-24 00:05:46 +1000217 serial_out(c, &com_port->thr);
Stefan Roese1a2d9b32010-10-12 09:39:45 +0200218
219 /*
220 * Call watchdog_reset() upon newline. This is done here in putc
221 * since the environment code uses a single puts() to print the complete
222 * environment upon "printenv". So we can't put this watchdog call
223 * in puts().
224 */
225 if (c == '\n')
226 WATCHDOG_RESET();
wdenke85390d2002-04-01 14:29:03 +0000227}
228
Ron Madridf5675aa2009-02-18 14:30:44 -0800229#ifndef CONFIG_NS16550_MIN_FUNCTIONS
Simon Glassf8df9d02011-10-15 19:14:09 +0000230char NS16550_getc(NS16550_t com_port)
wdenke85390d2002-04-01 14:29:03 +0000231{
Graeme Russ167cdad2010-04-24 00:05:46 +1000232 while ((serial_in(&com_port->lsr) & UART_LSR_DR) == 0) {
Marek Vasutf2041382012-09-15 10:25:19 +0200233#if !defined(CONFIG_SPL_BUILD) && defined(CONFIG_USB_TTY)
wdenk232c1502004-03-12 00:14:09 +0000234 extern void usbtty_poll(void);
235 usbtty_poll();
236#endif
Ladislav Michla1b322a2010-02-01 23:34:25 +0100237 WATCHDOG_RESET();
wdenk232c1502004-03-12 00:14:09 +0000238 }
Graeme Russ167cdad2010-04-24 00:05:46 +1000239 return serial_in(&com_port->rbr);
wdenke85390d2002-04-01 14:29:03 +0000240}
241
Simon Glassf8df9d02011-10-15 19:14:09 +0000242int NS16550_tstc(NS16550_t com_port)
wdenke85390d2002-04-01 14:29:03 +0000243{
Simon Glassf8df9d02011-10-15 19:14:09 +0000244 return (serial_in(&com_port->lsr) & UART_LSR_DR) != 0;
wdenke85390d2002-04-01 14:29:03 +0000245}
246
Ron Madridf5675aa2009-02-18 14:30:44 -0800247#endif /* CONFIG_NS16550_MIN_FUNCTIONS */
Simon Glass12e431b2014-09-04 16:27:34 -0600248
Simon Glass21d00432015-01-26 18:27:09 -0700249#ifdef CONFIG_DEBUG_UART_NS16550
250
251#include <debug_uart.h>
252
Simon Glass97b05972015-10-18 19:51:23 -0600253static inline void _debug_uart_init(void)
Simon Glass21d00432015-01-26 18:27:09 -0700254{
255 struct NS16550 *com_port = (struct NS16550 *)CONFIG_DEBUG_UART_BASE;
256 int baud_divisor;
257
258 /*
259 * We copy the code from above because it is already horribly messy.
260 * Trying to refactor to nicely remove the duplication doesn't seem
261 * feasible. The better fix is to move all users of this driver to
262 * driver model.
263 */
Marek Vasut03c6f172016-05-25 02:13:16 +0200264 baud_divisor = ns16550_calc_divisor(com_port, CONFIG_DEBUG_UART_CLOCK,
265 CONFIG_BAUDRATE);
Simon Glass6e780c72015-06-23 15:39:06 -0600266 serial_dout(&com_port->ier, CONFIG_SYS_NS16550_IER);
267 serial_dout(&com_port->mcr, UART_MCRVAL);
Heiko Schocher17fa0322017-01-18 08:05:49 +0100268 serial_dout(&com_port->fcr, UART_FCR_DEFVAL);
Simon Glass21d00432015-01-26 18:27:09 -0700269
Simon Glass6e780c72015-06-23 15:39:06 -0600270 serial_dout(&com_port->lcr, UART_LCR_BKSE | UART_LCRVAL);
271 serial_dout(&com_port->dll, baud_divisor & 0xff);
272 serial_dout(&com_port->dlm, (baud_divisor >> 8) & 0xff);
273 serial_dout(&com_port->lcr, UART_LCRVAL);
Simon Glass21d00432015-01-26 18:27:09 -0700274}
275
Simon Goldschmidtc4448bd2019-01-09 20:35:31 +0100276static inline int NS16550_read_baud_divisor(struct NS16550 *com_port)
277{
278 int ret;
279
280 serial_dout(&com_port->lcr, UART_LCR_BKSE | UART_LCRVAL);
281 ret = serial_din(&com_port->dll) & 0xff;
282 ret |= (serial_din(&com_port->dlm) & 0xff) << 8;
283 serial_dout(&com_port->lcr, UART_LCRVAL);
284
285 return ret;
286}
287
Simon Glass21d00432015-01-26 18:27:09 -0700288static inline void _debug_uart_putc(int ch)
289{
290 struct NS16550 *com_port = (struct NS16550 *)CONFIG_DEBUG_UART_BASE;
291
Simon Goldschmidtc4448bd2019-01-09 20:35:31 +0100292 while (!(serial_din(&com_port->lsr) & UART_LSR_THRE)) {
293#ifdef CONFIG_DEBUG_UART_NS16550_CHECK_ENABLED
294 if (!NS16550_read_baud_divisor(com_port))
295 return;
296#endif
297 }
Simon Glass6e780c72015-06-23 15:39:06 -0600298 serial_dout(&com_port->thr, ch);
Simon Glass21d00432015-01-26 18:27:09 -0700299}
300
301DEBUG_UART_FUNCS
302
303#endif
304
Simon Glass2e2c5142019-09-25 08:11:14 -0600305#if CONFIG_IS_ENABLED(DM_SERIAL)
Simon Glass12e431b2014-09-04 16:27:34 -0600306static int ns16550_serial_putc(struct udevice *dev, const char ch)
307{
308 struct NS16550 *const com_port = dev_get_priv(dev);
309
310 if (!(serial_in(&com_port->lsr) & UART_LSR_THRE))
311 return -EAGAIN;
312 serial_out(ch, &com_port->thr);
313
314 /*
315 * Call watchdog_reset() upon newline. This is done here in putc
316 * since the environment code uses a single puts() to print the complete
317 * environment upon "printenv". So we can't put this watchdog call
318 * in puts().
319 */
320 if (ch == '\n')
321 WATCHDOG_RESET();
322
323 return 0;
324}
325
326static int ns16550_serial_pending(struct udevice *dev, bool input)
327{
328 struct NS16550 *const com_port = dev_get_priv(dev);
329
330 if (input)
Mario Six4dbf9be2018-01-15 11:09:49 +0100331 return (serial_in(&com_port->lsr) & UART_LSR_DR) ? 1 : 0;
Simon Glass12e431b2014-09-04 16:27:34 -0600332 else
Mario Six4dbf9be2018-01-15 11:09:49 +0100333 return (serial_in(&com_port->lsr) & UART_LSR_THRE) ? 0 : 1;
Simon Glass12e431b2014-09-04 16:27:34 -0600334}
335
336static int ns16550_serial_getc(struct udevice *dev)
337{
Stefan Roese7fded0c2017-08-16 17:37:15 +0200338 struct NS16550 *const com_port = dev_get_priv(dev);
339
340 if (!(serial_in(&com_port->lsr) & UART_LSR_DR))
Simon Glass12e431b2014-09-04 16:27:34 -0600341 return -EAGAIN;
342
Stefan Roese7fded0c2017-08-16 17:37:15 +0200343 return serial_in(&com_port->rbr);
Simon Glass12e431b2014-09-04 16:27:34 -0600344}
345
346static int ns16550_serial_setbrg(struct udevice *dev, int baudrate)
347{
348 struct NS16550 *const com_port = dev_get_priv(dev);
349 struct ns16550_platdata *plat = com_port->plat;
350 int clock_divisor;
351
352 clock_divisor = ns16550_calc_divisor(com_port, plat->clock, baudrate);
353
354 NS16550_setbrg(com_port, clock_divisor);
355
356 return 0;
357}
358
Simon Goldschmidt9ad3b042018-11-02 21:28:08 +0100359static int ns16550_serial_setconfig(struct udevice *dev, uint serial_config)
360{
361 struct NS16550 *const com_port = dev_get_priv(dev);
362 int lcr_val = UART_LCR_WLS_8;
363 uint parity = SERIAL_GET_PARITY(serial_config);
364 uint bits = SERIAL_GET_BITS(serial_config);
365 uint stop = SERIAL_GET_STOP(serial_config);
366
367 /*
368 * only parity config is implemented, check if other serial settings
369 * are the default one.
370 */
371 if (bits != SERIAL_8_BITS || stop != SERIAL_ONE_STOP)
372 return -ENOTSUPP; /* not supported in driver*/
373
374 switch (parity) {
375 case SERIAL_PAR_NONE:
376 /* no bits to add */
377 break;
378 case SERIAL_PAR_ODD:
379 lcr_val |= UART_LCR_PEN;
380 break;
381 case SERIAL_PAR_EVEN:
382 lcr_val |= UART_LCR_PEN | UART_LCR_EPS;
383 break;
384 default:
385 return -ENOTSUPP; /* not supported in driver*/
386 }
387
388 serial_out(lcr_val, &com_port->lcr);
389 return 0;
390}
391
Andy Shevchenko50bf7d02018-11-20 23:52:36 +0200392static int ns16550_serial_getinfo(struct udevice *dev,
393 struct serial_device_info *info)
394{
395 struct NS16550 *const com_port = dev_get_priv(dev);
396 struct ns16550_platdata *plat = com_port->plat;
397
398 info->type = SERIAL_CHIP_16550_COMPATIBLE;
399#ifdef CONFIG_SYS_NS16550_PORT_MAPPED
400 info->addr_space = SERIAL_ADDRESS_SPACE_IO;
401#else
402 info->addr_space = SERIAL_ADDRESS_SPACE_MEMORY;
403#endif
404 info->addr = plat->base;
405 info->reg_width = plat->reg_width;
406 info->reg_shift = plat->reg_shift;
407 info->reg_offset = plat->reg_offset;
408 return 0;
409}
410
Simon Glass12e431b2014-09-04 16:27:34 -0600411int ns16550_serial_probe(struct udevice *dev)
412{
413 struct NS16550 *const com_port = dev_get_priv(dev);
Ley Foon Tanb051eec2018-06-14 18:45:22 +0800414 struct reset_ctl_bulk reset_bulk;
415 int ret;
416
417 ret = reset_get_bulk(dev, &reset_bulk);
418 if (!ret)
419 reset_deassert_bulk(&reset_bulk);
Simon Glass12e431b2014-09-04 16:27:34 -0600420
Simon Glass11c1a872014-10-22 21:37:05 -0600421 com_port->plat = dev_get_platdata(dev);
Simon Glass12e431b2014-09-04 16:27:34 -0600422 NS16550_init(com_port, -1);
423
424 return 0;
425}
426
Marek Vasut79fd9282016-12-01 02:06:30 +0100427#if CONFIG_IS_ENABLED(OF_CONTROL)
428enum {
429 PORT_NS16550 = 0,
Marek Vasut0b060ee2016-12-01 02:06:31 +0100430 PORT_JZ4780,
Marek Vasut79fd9282016-12-01 02:06:30 +0100431};
432#endif
433
Simon Glassb2927fb2016-07-04 11:58:23 -0600434#if CONFIG_IS_ENABLED(OF_CONTROL) && !CONFIG_IS_ENABLED(OF_PLATDATA)
Simon Glass12e431b2014-09-04 16:27:34 -0600435int ns16550_serial_ofdata_to_platdata(struct udevice *dev)
436{
Simon Glass12e431b2014-09-04 16:27:34 -0600437 struct ns16550_platdata *plat = dev->platdata;
Marek Vasut0b060ee2016-12-01 02:06:31 +0100438 const u32 port_type = dev_get_driver_data(dev);
Simon Glass12e431b2014-09-04 16:27:34 -0600439 fdt_addr_t addr;
Masahiro Yamada021abf62016-09-26 20:45:27 +0900440 struct clk clk;
441 int err;
Simon Glass12e431b2014-09-04 16:27:34 -0600442
Bin Meng3db886a2014-12-31 16:05:12 +0800443 /* try Processor Local Bus device first */
Simon Glass33c215a2019-09-15 12:08:58 -0600444 addr = dev_read_addr_pci(dev);
Simon Glass12e431b2014-09-04 16:27:34 -0600445 if (addr == FDT_ADDR_T_NONE)
446 return -EINVAL;
447
Paul Burtondf8ec552016-05-17 07:43:26 +0100448#ifdef CONFIG_SYS_NS16550_PORT_MAPPED
Simon Glass167efe02014-10-22 21:37:04 -0600449 plat->base = addr;
Paul Burtondf8ec552016-05-17 07:43:26 +0100450#else
451 plat->base = (unsigned long)map_physmem(addr, 0, MAP_NOCACHE);
452#endif
453
Philipp Tomsich3d404792017-06-07 18:46:02 +0200454 plat->reg_offset = dev_read_u32_default(dev, "reg-offset", 0);
455 plat->reg_shift = dev_read_u32_default(dev, "reg-shift", 0);
Andy Shevchenko4e720772018-11-20 23:52:35 +0200456 plat->reg_width = dev_read_u32_default(dev, "reg-io-width", 1);
Paul Burton50fce1d2016-09-08 07:47:29 +0100457
458 err = clk_get_by_index(dev, 0, &clk);
459 if (!err) {
460 err = clk_get_rate(&clk);
461 if (!IS_ERR_VALUE(err))
462 plat->clock = err;
Alexandre Courbotab895d62016-09-30 17:37:00 +0900463 } else if (err != -ENOENT && err != -ENODEV && err != -ENOSYS) {
Paul Burton50fce1d2016-09-08 07:47:29 +0100464 debug("ns16550 failed to get clock\n");
465 return err;
466 }
467
468 if (!plat->clock)
Philipp Tomsich3d404792017-06-07 18:46:02 +0200469 plat->clock = dev_read_u32_default(dev, "clock-frequency",
470 CONFIG_SYS_NS16550_CLK);
Thomas Chou8e62d322015-11-19 21:48:05 +0800471 if (!plat->clock) {
472 debug("ns16550 clock not defined\n");
473 return -EINVAL;
474 }
Simon Glass12e431b2014-09-04 16:27:34 -0600475
Heiko Schocher17fa0322017-01-18 08:05:49 +0100476 plat->fcr = UART_FCR_DEFVAL;
Marek Vasut0b060ee2016-12-01 02:06:31 +0100477 if (port_type == PORT_JZ4780)
478 plat->fcr |= UART_FCR_UME;
Marek Vasut65f83802016-12-01 02:06:29 +0100479
Simon Glass12e431b2014-09-04 16:27:34 -0600480 return 0;
481}
Simon Glass11c1a872014-10-22 21:37:05 -0600482#endif
Simon Glass12e431b2014-09-04 16:27:34 -0600483
484const struct dm_serial_ops ns16550_serial_ops = {
485 .putc = ns16550_serial_putc,
486 .pending = ns16550_serial_pending,
487 .getc = ns16550_serial_getc,
488 .setbrg = ns16550_serial_setbrg,
Andy Shevchenko50bf7d02018-11-20 23:52:36 +0200489 .setconfig = ns16550_serial_setconfig,
490 .getinfo = ns16550_serial_getinfo,
Simon Glass12e431b2014-09-04 16:27:34 -0600491};
Thomas Chou8e62d322015-11-19 21:48:05 +0800492
Alexandru Gagniuc6f8c3512017-03-27 12:54:19 -0700493#if CONFIG_IS_ENABLED(OF_CONTROL) && !CONFIG_IS_ENABLED(OF_PLATDATA)
Thomas Choucc4228f2015-12-14 20:45:09 +0800494/*
495 * Please consider existing compatible strings before adding a new
496 * one to keep this table compact. Or you may add a generic "ns16550"
497 * compatible string to your dts.
498 */
Thomas Chou8e62d322015-11-19 21:48:05 +0800499static const struct udevice_id ns16550_serial_ids[] = {
Marek Vasut79fd9282016-12-01 02:06:30 +0100500 { .compatible = "ns16550", .data = PORT_NS16550 },
501 { .compatible = "ns16550a", .data = PORT_NS16550 },
Marek Vasut0b060ee2016-12-01 02:06:31 +0100502 { .compatible = "ingenic,jz4780-uart", .data = PORT_JZ4780 },
Marek Vasut79fd9282016-12-01 02:06:30 +0100503 { .compatible = "nvidia,tegra20-uart", .data = PORT_NS16550 },
504 { .compatible = "snps,dw-apb-uart", .data = PORT_NS16550 },
Thomas Chou8e62d322015-11-19 21:48:05 +0800505 {}
506};
Alexandru Gagniuc6f8c3512017-03-27 12:54:19 -0700507#endif /* OF_CONTROL && !OF_PLATDATA */
Thomas Chou8e62d322015-11-19 21:48:05 +0800508
Simon Glassb7e29832015-12-13 21:36:59 -0700509#if CONFIG_IS_ENABLED(SERIAL_PRESENT)
Alexandru Gagniuc6f8c3512017-03-27 12:54:19 -0700510
511/* TODO(sjg@chromium.org): Integrate this into a macro like CONFIG_IS_ENABLED */
512#if !defined(CONFIG_TPL_BUILD) || defined(CONFIG_TPL_DM_SERIAL)
Thomas Chou8e62d322015-11-19 21:48:05 +0800513U_BOOT_DRIVER(ns16550_serial) = {
514 .name = "ns16550_serial",
515 .id = UCLASS_SERIAL,
Alexandru Gagniuc6f8c3512017-03-27 12:54:19 -0700516#if CONFIG_IS_ENABLED(OF_CONTROL) && !CONFIG_IS_ENABLED(OF_PLATDATA)
Thomas Chou8e62d322015-11-19 21:48:05 +0800517 .of_match = ns16550_serial_ids,
518 .ofdata_to_platdata = ns16550_serial_ofdata_to_platdata,
519 .platdata_auto_alloc_size = sizeof(struct ns16550_platdata),
520#endif
521 .priv_auto_alloc_size = sizeof(struct NS16550),
522 .probe = ns16550_serial_probe,
523 .ops = &ns16550_serial_ops,
Bin Meng46879192018-10-24 06:36:36 -0700524#if !CONFIG_IS_ENABLED(OF_CONTROL)
Simon Glassb7e5a642015-12-04 08:58:38 -0700525 .flags = DM_FLAG_PRE_RELOC,
Bin Meng46879192018-10-24 06:36:36 -0700526#endif
Thomas Chou8e62d322015-11-19 21:48:05 +0800527};
Simon Glassb7e29832015-12-13 21:36:59 -0700528#endif
Alexandru Gagniuc6f8c3512017-03-27 12:54:19 -0700529#endif /* SERIAL_PRESENT */
530
Simon Glass12e431b2014-09-04 16:27:34 -0600531#endif /* CONFIG_DM_SERIAL */