blob: c1b303ffcba70c93ccd504d1cec3b902be40ae91 [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>
Simon Glass61b29b82020-02-03 07:36:15 -070016#include <linux/err.h>
Graeme Russ167cdad2010-04-24 00:05:46 +100017#include <linux/types.h>
18#include <asm/io.h>
wdenke85390d2002-04-01 14:29:03 +000019
Simon Glass12e431b2014-09-04 16:27:34 -060020DECLARE_GLOBAL_DATA_PTR;
21
Detlev Zundel200779e2009-04-03 11:53:01 +020022#define UART_LCRVAL UART_LCR_8N1 /* 8 data, 1 stop, no parity */
23#define UART_MCRVAL (UART_MCR_DTR | \
24 UART_MCR_RTS) /* RTS/DTR */
Simon Glass12e431b2014-09-04 16:27:34 -060025
Simon Glass2e2c5142019-09-25 08:11:14 -060026#if !CONFIG_IS_ENABLED(DM_SERIAL)
Graeme Russ167cdad2010-04-24 00:05:46 +100027#ifdef CONFIG_SYS_NS16550_PORT_MAPPED
Simon Glassf8df9d02011-10-15 19:14:09 +000028#define serial_out(x, y) outb(x, (ulong)y)
29#define serial_in(y) inb((ulong)y)
Dave Aldridge79df1202011-09-01 22:47:14 +000030#elif defined(CONFIG_SYS_NS16550_MEM32) && (CONFIG_SYS_NS16550_REG_SIZE > 0)
Simon Glassf8df9d02011-10-15 19:14:09 +000031#define serial_out(x, y) out_be32(y, x)
32#define serial_in(y) in_be32(y)
Dave Aldridge79df1202011-09-01 22:47:14 +000033#elif defined(CONFIG_SYS_NS16550_MEM32) && (CONFIG_SYS_NS16550_REG_SIZE < 0)
Simon Glassf8df9d02011-10-15 19:14:09 +000034#define serial_out(x, y) out_le32(y, x)
35#define serial_in(y) in_le32(y)
Graeme Russ167cdad2010-04-24 00:05:46 +100036#else
Simon Glassf8df9d02011-10-15 19:14:09 +000037#define serial_out(x, y) writeb(x, y)
38#define serial_in(y) readb(y)
Graeme Russ167cdad2010-04-24 00:05:46 +100039#endif
Simon Glass12e431b2014-09-04 16:27:34 -060040#endif /* !CONFIG_DM_SERIAL */
wdenke85390d2002-04-01 14:29:03 +000041
Khoronzhuk, Ivan7c387642014-07-16 00:59:25 +030042#if defined(CONFIG_SOC_KEYSTONE)
Vitaly Andrianovef509b92014-04-04 13:16:53 -040043#define UART_REG_VAL_PWREMU_MGMT_UART_DISABLE 0
44#define UART_REG_VAL_PWREMU_MGMT_UART_ENABLE ((1 << 14) | (1 << 13) | (1 << 0))
Karicheri, Muralidharand57dee52014-04-09 15:38:46 -040045#undef UART_MCRVAL
46#ifdef CONFIG_SERIAL_HW_FLOW_CONTROL
47#define UART_MCRVAL (UART_MCR_RTS | UART_MCR_AFE)
48#else
49#define UART_MCRVAL (UART_MCR_RTS)
50#endif
Vitaly Andrianovef509b92014-04-04 13:16:53 -040051#endif
52
Prafulla Wadaskara160ea02010-10-27 21:58:31 +053053#ifndef CONFIG_SYS_NS16550_IER
54#define CONFIG_SYS_NS16550_IER 0x00
55#endif /* CONFIG_SYS_NS16550_IER */
56
Simon Glass363e6da2015-02-27 22:06:26 -070057static inline void serial_out_shift(void *addr, int shift, int value)
Simon Glass76571672015-01-26 18:27:08 -070058{
59#ifdef CONFIG_SYS_NS16550_PORT_MAPPED
60 outb(value, (ulong)addr);
Bernhard Messerklinger78b7d372018-02-15 09:02:26 +010061#elif defined(CONFIG_SYS_NS16550_MEM32) && defined(CONFIG_SYS_LITTLE_ENDIAN)
Simon Glass76571672015-01-26 18:27:08 -070062 out_le32(addr, value);
63#elif defined(CONFIG_SYS_NS16550_MEM32) && defined(CONFIG_SYS_BIG_ENDIAN)
64 out_be32(addr, value);
Simon Glass90914002015-05-12 14:55:02 -060065#elif defined(CONFIG_SYS_NS16550_MEM32)
66 writel(value, addr);
Simon Glass76571672015-01-26 18:27:08 -070067#elif defined(CONFIG_SYS_BIG_ENDIAN)
68 writeb(value, addr + (1 << shift) - 1);
69#else
70 writeb(value, addr);
71#endif
72}
73
Simon Glass363e6da2015-02-27 22:06:26 -070074static inline int serial_in_shift(void *addr, int shift)
Simon Glass76571672015-01-26 18:27:08 -070075{
76#ifdef CONFIG_SYS_NS16550_PORT_MAPPED
77 return inb((ulong)addr);
Bernhard Messerklinger78b7d372018-02-15 09:02:26 +010078#elif defined(CONFIG_SYS_NS16550_MEM32) && defined(CONFIG_SYS_LITTLE_ENDIAN)
Simon Glass76571672015-01-26 18:27:08 -070079 return in_le32(addr);
80#elif defined(CONFIG_SYS_NS16550_MEM32) && defined(CONFIG_SYS_BIG_ENDIAN)
81 return in_be32(addr);
Simon Glass90914002015-05-12 14:55:02 -060082#elif defined(CONFIG_SYS_NS16550_MEM32)
83 return readl(addr);
Simon Glass76571672015-01-26 18:27:08 -070084#elif defined(CONFIG_SYS_BIG_ENDIAN)
Axel Lin20379c12015-02-28 15:55:36 +080085 return readb(addr + (1 << shift) - 1);
Simon Glass76571672015-01-26 18:27:08 -070086#else
87 return readb(addr);
88#endif
89}
90
Simon Glass2e2c5142019-09-25 08:11:14 -060091#if CONFIG_IS_ENABLED(DM_SERIAL)
Marek Vasutfa4ce722016-05-25 02:13:03 +020092
93#ifndef CONFIG_SYS_NS16550_CLK
94#define CONFIG_SYS_NS16550_CLK 0
95#endif
96
Simon Glass62cbde42019-12-19 17:58:18 -070097/*
98 * Use this #ifdef for now since many platforms don't define in(), out(),
99 * out_le32(), etc. but we don't have #defines to indicate this.
100 *
101 * TODO(sjg@chromium.org): Add CONFIG options to indicate what I/O is available
102 * on a platform
103 */
104#ifdef CONFIG_NS16550_DYNAMIC
105static void serial_out_dynamic(struct ns16550_platdata *plat, u8 *addr,
106 int value)
107{
108 if (plat->flags & NS16550_FLAG_IO) {
109 outb(value, addr);
110 } else if (plat->reg_width == 4) {
111 if (plat->flags & NS16550_FLAG_ENDIAN) {
112 if (plat->flags & NS16550_FLAG_BE)
113 out_be32(addr, value);
114 else
115 out_le32(addr, value);
116 } else {
117 writel(value, addr);
118 }
119 } else if (plat->flags & NS16550_FLAG_BE) {
120 writeb(value, addr + (1 << plat->reg_shift) - 1);
121 } else {
122 writeb(value, addr);
123 }
124}
125
126static int serial_in_dynamic(struct ns16550_platdata *plat, u8 *addr)
127{
128 if (plat->flags & NS16550_FLAG_IO) {
129 return inb(addr);
130 } else if (plat->reg_width == 4) {
131 if (plat->flags & NS16550_FLAG_ENDIAN) {
132 if (plat->flags & NS16550_FLAG_BE)
133 return in_be32(addr);
134 else
135 return in_le32(addr);
136 } else {
137 return readl(addr);
138 }
139 } else if (plat->flags & NS16550_FLAG_BE) {
140 return readb(addr + (1 << plat->reg_shift) - 1);
141 } else {
142 return readb(addr);
143 }
144}
145#else
146static inline void serial_out_dynamic(struct ns16550_platdata *plat, u8 *addr,
147 int value)
148{
149}
150
151static inline int serial_in_dynamic(struct ns16550_platdata *plat, u8 *addr)
152{
153 return 0;
154}
155
156#endif /* CONFIG_NS16550_DYNAMIC */
157
Simon Glass12e431b2014-09-04 16:27:34 -0600158static void ns16550_writeb(NS16550_t port, int offset, int value)
159{
160 struct ns16550_platdata *plat = port->plat;
161 unsigned char *addr;
162
163 offset *= 1 << plat->reg_shift;
Simon Glass62cbde42019-12-19 17:58:18 -0700164 addr = (unsigned char *)plat->base + offset + plat->reg_offset;
Paul Burtondf8ec552016-05-17 07:43:26 +0100165
Simon Glass62cbde42019-12-19 17:58:18 -0700166 if (IS_ENABLED(CONFIG_NS16550_DYNAMIC))
167 serial_out_dynamic(plat, addr, value);
168 else
169 serial_out_shift(addr, plat->reg_shift, value);
Simon Glass12e431b2014-09-04 16:27:34 -0600170}
171
172static int ns16550_readb(NS16550_t port, int offset)
173{
174 struct ns16550_platdata *plat = port->plat;
175 unsigned char *addr;
176
177 offset *= 1 << plat->reg_shift;
Simon Glass62cbde42019-12-19 17:58:18 -0700178 addr = (unsigned char *)plat->base + offset + plat->reg_offset;
Simon Glass76571672015-01-26 18:27:08 -0700179
Simon Glass62cbde42019-12-19 17:58:18 -0700180 if (IS_ENABLED(CONFIG_NS16550_DYNAMIC))
181 return serial_in_dynamic(plat, addr);
182 else
183 return serial_in_shift(addr, plat->reg_shift);
Simon Glass12e431b2014-09-04 16:27:34 -0600184}
185
Marek Vasut65f83802016-12-01 02:06:29 +0100186static u32 ns16550_getfcr(NS16550_t port)
187{
188 struct ns16550_platdata *plat = port->plat;
189
190 return plat->fcr;
191}
192
Simon Glass12e431b2014-09-04 16:27:34 -0600193/* We can clean these up once everything is moved to driver model */
194#define serial_out(value, addr) \
Simon Glass363e6da2015-02-27 22:06:26 -0700195 ns16550_writeb(com_port, \
196 (unsigned char *)addr - (unsigned char *)com_port, value)
Simon Glass12e431b2014-09-04 16:27:34 -0600197#define serial_in(addr) \
Simon Glass363e6da2015-02-27 22:06:26 -0700198 ns16550_readb(com_port, \
199 (unsigned char *)addr - (unsigned char *)com_port)
Marek Vasut65f83802016-12-01 02:06:29 +0100200#else
201static u32 ns16550_getfcr(NS16550_t port)
202{
Heiko Schocher17fa0322017-01-18 08:05:49 +0100203 return UART_FCR_DEFVAL;
Marek Vasut65f83802016-12-01 02:06:29 +0100204}
Simon Glass12e431b2014-09-04 16:27:34 -0600205#endif
206
Marek Vasut03c6f172016-05-25 02:13:16 +0200207int ns16550_calc_divisor(NS16550_t port, int clock, int baudrate)
Simon Glassfa54eb12014-09-04 16:27:32 -0600208{
209 const unsigned int mode_x_div = 16;
210
Simon Glass21d00432015-01-26 18:27:09 -0700211 return DIV_ROUND_CLOSEST(clock, mode_x_div * baudrate);
212}
213
Simon Glass8bbe33c2014-09-04 16:27:33 -0600214static void NS16550_setbrg(NS16550_t com_port, int baud_divisor)
215{
Simon Goldschmidt9ad3b042018-11-02 21:28:08 +0100216 /* to keep serial format, read lcr before writing BKSE */
217 int lcr_val = serial_in(&com_port->lcr) & ~UART_LCR_BKSE;
218
219 serial_out(UART_LCR_BKSE | lcr_val, &com_port->lcr);
Simon Glass8bbe33c2014-09-04 16:27:33 -0600220 serial_out(baud_divisor & 0xff, &com_port->dll);
221 serial_out((baud_divisor >> 8) & 0xff, &com_port->dlm);
Simon Goldschmidt9ad3b042018-11-02 21:28:08 +0100222 serial_out(lcr_val, &com_port->lcr);
Simon Glass8bbe33c2014-09-04 16:27:33 -0600223}
224
Simon Glassf8df9d02011-10-15 19:14:09 +0000225void NS16550_init(NS16550_t com_port, int baud_divisor)
wdenke85390d2002-04-01 14:29:03 +0000226{
Gregoire Gentil956a8ba2014-11-10 11:04:10 -0800227#if (defined(CONFIG_SPL_BUILD) && \
228 (defined(CONFIG_OMAP34XX) || defined(CONFIG_OMAP44XX)))
Manfred Huberfd2aeac2013-03-29 02:52:36 +0000229 /*
Gregoire Gentil956a8ba2014-11-10 11:04:10 -0800230 * On some OMAP3/OMAP4 devices when UART3 is configured for boot mode
231 * before SPL starts only THRE bit is set. We have to empty the
232 * transmitter before initialization starts.
Manfred Huberfd2aeac2013-03-29 02:52:36 +0000233 */
234 if ((serial_in(&com_port->lsr) & (UART_LSR_TEMT | UART_LSR_THRE))
235 == UART_LSR_THRE) {
Simon Glass12e431b2014-09-04 16:27:34 -0600236 if (baud_divisor != -1)
237 NS16550_setbrg(com_port, baud_divisor);
Patrik Dahlström1c166062019-12-21 17:25:12 +0100238 else {
239 // Re-use old baud rate divisor to flush transmit reg.
240 const int dll = serial_in(&com_port->dll);
241 const int dlm = serial_in(&com_port->dlm);
242 const int divisor = dll | (dlm << 8);
243 NS16550_setbrg(com_port, divisor);
244 }
Manfred Huberfd2aeac2013-03-29 02:52:36 +0000245 serial_out(0, &com_port->mdr1);
246 }
247#endif
248
Scott Woodcb55b332012-09-18 18:19:05 -0500249 while (!(serial_in(&com_port->lsr) & UART_LSR_TEMT))
250 ;
251
Prafulla Wadaskara160ea02010-10-27 21:58:31 +0530252 serial_out(CONFIG_SYS_NS16550_IER, &com_port->ier);
Lokesh Vutla5d754192018-08-27 15:55:24 +0530253#if defined(CONFIG_ARCH_OMAP2PLUS) || defined(CONFIG_OMAP_SERIAL)
Graeme Russ167cdad2010-04-24 00:05:46 +1000254 serial_out(0x7, &com_port->mdr1); /* mode select reset TL16C750*/
wdenk945af8d2003-07-16 21:53:01 +0000255#endif
Ley Foon Tanb051eec2018-06-14 18:45:22 +0800256
Graeme Russ167cdad2010-04-24 00:05:46 +1000257 serial_out(UART_MCRVAL, &com_port->mcr);
Marek Vasut65f83802016-12-01 02:06:29 +0100258 serial_out(ns16550_getfcr(com_port), &com_port->fcr);
Simon Goldschmidt9ad3b042018-11-02 21:28:08 +0100259 /* initialize serial config to 8N1 before writing baudrate */
260 serial_out(UART_LCRVAL, &com_port->lcr);
Simon Glass12e431b2014-09-04 16:27:34 -0600261 if (baud_divisor != -1)
262 NS16550_setbrg(com_port, baud_divisor);
Lokesh Vutla5d754192018-08-27 15:55:24 +0530263#if defined(CONFIG_ARCH_OMAP2PLUS) || defined(CONFIG_SOC_DA8XX) || \
264 defined(CONFIG_OMAP_SERIAL)
Simon Glassf8df9d02011-10-15 19:14:09 +0000265 /* /16 is proper to hit 115200 with 48MHz */
266 serial_out(0, &com_port->mdr1);
Tom Rini89024dd2017-05-12 22:33:16 -0400267#endif
Khoronzhuk, Ivan7c387642014-07-16 00:59:25 +0300268#if defined(CONFIG_SOC_KEYSTONE)
Vitaly Andrianovef509b92014-04-04 13:16:53 -0400269 serial_out(UART_REG_VAL_PWREMU_MGMT_UART_ENABLE, &com_port->regC);
270#endif
wdenke85390d2002-04-01 14:29:03 +0000271}
272
Ron Madridf5675aa2009-02-18 14:30:44 -0800273#ifndef CONFIG_NS16550_MIN_FUNCTIONS
Simon Glassf8df9d02011-10-15 19:14:09 +0000274void NS16550_reinit(NS16550_t com_port, int baud_divisor)
wdenke85390d2002-04-01 14:29:03 +0000275{
Prafulla Wadaskara160ea02010-10-27 21:58:31 +0530276 serial_out(CONFIG_SYS_NS16550_IER, &com_port->ier);
Simon Glass8bbe33c2014-09-04 16:27:33 -0600277 NS16550_setbrg(com_port, 0);
Graeme Russ167cdad2010-04-24 00:05:46 +1000278 serial_out(UART_MCRVAL, &com_port->mcr);
Marek Vasut65f83802016-12-01 02:06:29 +0100279 serial_out(ns16550_getfcr(com_port), &com_port->fcr);
Simon Glass8bbe33c2014-09-04 16:27:33 -0600280 NS16550_setbrg(com_port, baud_divisor);
wdenke85390d2002-04-01 14:29:03 +0000281}
Ron Madridf5675aa2009-02-18 14:30:44 -0800282#endif /* CONFIG_NS16550_MIN_FUNCTIONS */
wdenke85390d2002-04-01 14:29:03 +0000283
Simon Glassf8df9d02011-10-15 19:14:09 +0000284void NS16550_putc(NS16550_t com_port, char c)
wdenke85390d2002-04-01 14:29:03 +0000285{
Simon Glassf8df9d02011-10-15 19:14:09 +0000286 while ((serial_in(&com_port->lsr) & UART_LSR_THRE) == 0)
287 ;
Graeme Russ167cdad2010-04-24 00:05:46 +1000288 serial_out(c, &com_port->thr);
Stefan Roese1a2d9b32010-10-12 09:39:45 +0200289
290 /*
291 * Call watchdog_reset() upon newline. This is done here in putc
292 * since the environment code uses a single puts() to print the complete
293 * environment upon "printenv". So we can't put this watchdog call
294 * in puts().
295 */
296 if (c == '\n')
297 WATCHDOG_RESET();
wdenke85390d2002-04-01 14:29:03 +0000298}
299
Ron Madridf5675aa2009-02-18 14:30:44 -0800300#ifndef CONFIG_NS16550_MIN_FUNCTIONS
Simon Glassf8df9d02011-10-15 19:14:09 +0000301char NS16550_getc(NS16550_t com_port)
wdenke85390d2002-04-01 14:29:03 +0000302{
Graeme Russ167cdad2010-04-24 00:05:46 +1000303 while ((serial_in(&com_port->lsr) & UART_LSR_DR) == 0) {
Marek Vasutf2041382012-09-15 10:25:19 +0200304#if !defined(CONFIG_SPL_BUILD) && defined(CONFIG_USB_TTY)
wdenk232c1502004-03-12 00:14:09 +0000305 extern void usbtty_poll(void);
306 usbtty_poll();
307#endif
Ladislav Michla1b322a2010-02-01 23:34:25 +0100308 WATCHDOG_RESET();
wdenk232c1502004-03-12 00:14:09 +0000309 }
Graeme Russ167cdad2010-04-24 00:05:46 +1000310 return serial_in(&com_port->rbr);
wdenke85390d2002-04-01 14:29:03 +0000311}
312
Simon Glassf8df9d02011-10-15 19:14:09 +0000313int NS16550_tstc(NS16550_t com_port)
wdenke85390d2002-04-01 14:29:03 +0000314{
Simon Glassf8df9d02011-10-15 19:14:09 +0000315 return (serial_in(&com_port->lsr) & UART_LSR_DR) != 0;
wdenke85390d2002-04-01 14:29:03 +0000316}
317
Ron Madridf5675aa2009-02-18 14:30:44 -0800318#endif /* CONFIG_NS16550_MIN_FUNCTIONS */
Simon Glass12e431b2014-09-04 16:27:34 -0600319
Simon Glass21d00432015-01-26 18:27:09 -0700320#ifdef CONFIG_DEBUG_UART_NS16550
321
322#include <debug_uart.h>
323
Simon Glass97b05972015-10-18 19:51:23 -0600324static inline void _debug_uart_init(void)
Simon Glass21d00432015-01-26 18:27:09 -0700325{
326 struct NS16550 *com_port = (struct NS16550 *)CONFIG_DEBUG_UART_BASE;
327 int baud_divisor;
328
329 /*
330 * We copy the code from above because it is already horribly messy.
331 * Trying to refactor to nicely remove the duplication doesn't seem
332 * feasible. The better fix is to move all users of this driver to
333 * driver model.
334 */
Marek Vasut03c6f172016-05-25 02:13:16 +0200335 baud_divisor = ns16550_calc_divisor(com_port, CONFIG_DEBUG_UART_CLOCK,
336 CONFIG_BAUDRATE);
Simon Glass6e780c72015-06-23 15:39:06 -0600337 serial_dout(&com_port->ier, CONFIG_SYS_NS16550_IER);
338 serial_dout(&com_port->mcr, UART_MCRVAL);
Heiko Schocher17fa0322017-01-18 08:05:49 +0100339 serial_dout(&com_port->fcr, UART_FCR_DEFVAL);
Simon Glass21d00432015-01-26 18:27:09 -0700340
Simon Glass6e780c72015-06-23 15:39:06 -0600341 serial_dout(&com_port->lcr, UART_LCR_BKSE | UART_LCRVAL);
342 serial_dout(&com_port->dll, baud_divisor & 0xff);
343 serial_dout(&com_port->dlm, (baud_divisor >> 8) & 0xff);
344 serial_dout(&com_port->lcr, UART_LCRVAL);
Simon Glass21d00432015-01-26 18:27:09 -0700345}
346
Simon Goldschmidtc4448bd2019-01-09 20:35:31 +0100347static inline int NS16550_read_baud_divisor(struct NS16550 *com_port)
348{
349 int ret;
350
351 serial_dout(&com_port->lcr, UART_LCR_BKSE | UART_LCRVAL);
352 ret = serial_din(&com_port->dll) & 0xff;
353 ret |= (serial_din(&com_port->dlm) & 0xff) << 8;
354 serial_dout(&com_port->lcr, UART_LCRVAL);
355
356 return ret;
357}
358
Simon Glass21d00432015-01-26 18:27:09 -0700359static inline void _debug_uart_putc(int ch)
360{
361 struct NS16550 *com_port = (struct NS16550 *)CONFIG_DEBUG_UART_BASE;
362
Simon Goldschmidtc4448bd2019-01-09 20:35:31 +0100363 while (!(serial_din(&com_port->lsr) & UART_LSR_THRE)) {
364#ifdef CONFIG_DEBUG_UART_NS16550_CHECK_ENABLED
365 if (!NS16550_read_baud_divisor(com_port))
366 return;
367#endif
368 }
Simon Glass6e780c72015-06-23 15:39:06 -0600369 serial_dout(&com_port->thr, ch);
Simon Glass21d00432015-01-26 18:27:09 -0700370}
371
372DEBUG_UART_FUNCS
373
374#endif
375
Simon Glass2e2c5142019-09-25 08:11:14 -0600376#if CONFIG_IS_ENABLED(DM_SERIAL)
Simon Glass12e431b2014-09-04 16:27:34 -0600377static int ns16550_serial_putc(struct udevice *dev, const char ch)
378{
379 struct NS16550 *const com_port = dev_get_priv(dev);
380
381 if (!(serial_in(&com_port->lsr) & UART_LSR_THRE))
382 return -EAGAIN;
383 serial_out(ch, &com_port->thr);
384
385 /*
386 * Call watchdog_reset() upon newline. This is done here in putc
387 * since the environment code uses a single puts() to print the complete
388 * environment upon "printenv". So we can't put this watchdog call
389 * in puts().
390 */
391 if (ch == '\n')
392 WATCHDOG_RESET();
393
394 return 0;
395}
396
397static int ns16550_serial_pending(struct udevice *dev, bool input)
398{
399 struct NS16550 *const com_port = dev_get_priv(dev);
400
401 if (input)
Mario Six4dbf9be2018-01-15 11:09:49 +0100402 return (serial_in(&com_port->lsr) & UART_LSR_DR) ? 1 : 0;
Simon Glass12e431b2014-09-04 16:27:34 -0600403 else
Mario Six4dbf9be2018-01-15 11:09:49 +0100404 return (serial_in(&com_port->lsr) & UART_LSR_THRE) ? 0 : 1;
Simon Glass12e431b2014-09-04 16:27:34 -0600405}
406
407static int ns16550_serial_getc(struct udevice *dev)
408{
Stefan Roese7fded0c2017-08-16 17:37:15 +0200409 struct NS16550 *const com_port = dev_get_priv(dev);
410
411 if (!(serial_in(&com_port->lsr) & UART_LSR_DR))
Simon Glass12e431b2014-09-04 16:27:34 -0600412 return -EAGAIN;
413
Stefan Roese7fded0c2017-08-16 17:37:15 +0200414 return serial_in(&com_port->rbr);
Simon Glass12e431b2014-09-04 16:27:34 -0600415}
416
417static int ns16550_serial_setbrg(struct udevice *dev, int baudrate)
418{
419 struct NS16550 *const com_port = dev_get_priv(dev);
420 struct ns16550_platdata *plat = com_port->plat;
421 int clock_divisor;
422
423 clock_divisor = ns16550_calc_divisor(com_port, plat->clock, baudrate);
424
425 NS16550_setbrg(com_port, clock_divisor);
426
427 return 0;
428}
429
Simon Goldschmidt9ad3b042018-11-02 21:28:08 +0100430static int ns16550_serial_setconfig(struct udevice *dev, uint serial_config)
431{
432 struct NS16550 *const com_port = dev_get_priv(dev);
433 int lcr_val = UART_LCR_WLS_8;
434 uint parity = SERIAL_GET_PARITY(serial_config);
435 uint bits = SERIAL_GET_BITS(serial_config);
436 uint stop = SERIAL_GET_STOP(serial_config);
437
438 /*
439 * only parity config is implemented, check if other serial settings
440 * are the default one.
441 */
442 if (bits != SERIAL_8_BITS || stop != SERIAL_ONE_STOP)
443 return -ENOTSUPP; /* not supported in driver*/
444
445 switch (parity) {
446 case SERIAL_PAR_NONE:
447 /* no bits to add */
448 break;
449 case SERIAL_PAR_ODD:
450 lcr_val |= UART_LCR_PEN;
451 break;
452 case SERIAL_PAR_EVEN:
453 lcr_val |= UART_LCR_PEN | UART_LCR_EPS;
454 break;
455 default:
456 return -ENOTSUPP; /* not supported in driver*/
457 }
458
459 serial_out(lcr_val, &com_port->lcr);
460 return 0;
461}
462
Andy Shevchenko50bf7d02018-11-20 23:52:36 +0200463static int ns16550_serial_getinfo(struct udevice *dev,
464 struct serial_device_info *info)
465{
466 struct NS16550 *const com_port = dev_get_priv(dev);
467 struct ns16550_platdata *plat = com_port->plat;
468
469 info->type = SERIAL_CHIP_16550_COMPATIBLE;
470#ifdef CONFIG_SYS_NS16550_PORT_MAPPED
471 info->addr_space = SERIAL_ADDRESS_SPACE_IO;
472#else
473 info->addr_space = SERIAL_ADDRESS_SPACE_MEMORY;
474#endif
475 info->addr = plat->base;
476 info->reg_width = plat->reg_width;
477 info->reg_shift = plat->reg_shift;
478 info->reg_offset = plat->reg_offset;
479 return 0;
480}
481
Wolfgang Wallner720f9e12020-03-02 14:41:14 +0100482#if CONFIG_IS_ENABLED(OF_CONTROL) && !CONFIG_IS_ENABLED(OF_PLATDATA)
483static int ns1655_serial_set_base_addr(struct udevice *dev)
484{
485 fdt_addr_t addr;
486 struct ns16550_platdata *plat;
487
488 plat = dev_get_platdata(dev);
489
490 addr = dev_read_addr_pci(dev);
491 if (addr == FDT_ADDR_T_NONE)
492 return -EINVAL;
493
494#ifdef CONFIG_SYS_NS16550_PORT_MAPPED
495 plat->base = addr;
496#else
497 plat->base = (unsigned long)map_physmem(addr, 0, MAP_NOCACHE);
498#endif
499
500 return 0;
501}
502#endif
503
Simon Glass12e431b2014-09-04 16:27:34 -0600504int ns16550_serial_probe(struct udevice *dev)
505{
506 struct NS16550 *const com_port = dev_get_priv(dev);
Ley Foon Tanb051eec2018-06-14 18:45:22 +0800507 struct reset_ctl_bulk reset_bulk;
508 int ret;
509
Wolfgang Wallner720f9e12020-03-02 14:41:14 +0100510#if CONFIG_IS_ENABLED(OF_CONTROL) && !CONFIG_IS_ENABLED(OF_PLATDATA)
511 ret = ns1655_serial_set_base_addr(dev);
512 if (ret)
513 return ret;
514#endif
515
Ley Foon Tanb051eec2018-06-14 18:45:22 +0800516 ret = reset_get_bulk(dev, &reset_bulk);
517 if (!ret)
518 reset_deassert_bulk(&reset_bulk);
Simon Glass12e431b2014-09-04 16:27:34 -0600519
Simon Glass11c1a872014-10-22 21:37:05 -0600520 com_port->plat = dev_get_platdata(dev);
Simon Glass12e431b2014-09-04 16:27:34 -0600521 NS16550_init(com_port, -1);
522
523 return 0;
524}
525
Marek Vasut79fd9282016-12-01 02:06:30 +0100526#if CONFIG_IS_ENABLED(OF_CONTROL)
527enum {
528 PORT_NS16550 = 0,
Marek Vasut0b060ee2016-12-01 02:06:31 +0100529 PORT_JZ4780,
Marek Vasut79fd9282016-12-01 02:06:30 +0100530};
531#endif
532
Simon Glassb2927fb2016-07-04 11:58:23 -0600533#if CONFIG_IS_ENABLED(OF_CONTROL) && !CONFIG_IS_ENABLED(OF_PLATDATA)
Simon Glass12e431b2014-09-04 16:27:34 -0600534int ns16550_serial_ofdata_to_platdata(struct udevice *dev)
535{
Simon Glass12e431b2014-09-04 16:27:34 -0600536 struct ns16550_platdata *plat = dev->platdata;
Marek Vasut0b060ee2016-12-01 02:06:31 +0100537 const u32 port_type = dev_get_driver_data(dev);
Masahiro Yamada021abf62016-09-26 20:45:27 +0900538 struct clk clk;
539 int err;
Simon Glass12e431b2014-09-04 16:27:34 -0600540
Philipp Tomsich3d404792017-06-07 18:46:02 +0200541 plat->reg_offset = dev_read_u32_default(dev, "reg-offset", 0);
542 plat->reg_shift = dev_read_u32_default(dev, "reg-shift", 0);
Andy Shevchenko4e720772018-11-20 23:52:35 +0200543 plat->reg_width = dev_read_u32_default(dev, "reg-io-width", 1);
Paul Burton50fce1d2016-09-08 07:47:29 +0100544
545 err = clk_get_by_index(dev, 0, &clk);
546 if (!err) {
547 err = clk_get_rate(&clk);
548 if (!IS_ERR_VALUE(err))
549 plat->clock = err;
Alexandre Courbotab895d62016-09-30 17:37:00 +0900550 } else if (err != -ENOENT && err != -ENODEV && err != -ENOSYS) {
Paul Burton50fce1d2016-09-08 07:47:29 +0100551 debug("ns16550 failed to get clock\n");
552 return err;
553 }
554
555 if (!plat->clock)
Philipp Tomsich3d404792017-06-07 18:46:02 +0200556 plat->clock = dev_read_u32_default(dev, "clock-frequency",
557 CONFIG_SYS_NS16550_CLK);
Thomas Chou8e62d322015-11-19 21:48:05 +0800558 if (!plat->clock) {
559 debug("ns16550 clock not defined\n");
560 return -EINVAL;
561 }
Simon Glass12e431b2014-09-04 16:27:34 -0600562
Heiko Schocher17fa0322017-01-18 08:05:49 +0100563 plat->fcr = UART_FCR_DEFVAL;
Marek Vasut0b060ee2016-12-01 02:06:31 +0100564 if (port_type == PORT_JZ4780)
565 plat->fcr |= UART_FCR_UME;
Marek Vasut65f83802016-12-01 02:06:29 +0100566
Simon Glass12e431b2014-09-04 16:27:34 -0600567 return 0;
568}
Simon Glass11c1a872014-10-22 21:37:05 -0600569#endif
Simon Glass12e431b2014-09-04 16:27:34 -0600570
571const struct dm_serial_ops ns16550_serial_ops = {
572 .putc = ns16550_serial_putc,
573 .pending = ns16550_serial_pending,
574 .getc = ns16550_serial_getc,
575 .setbrg = ns16550_serial_setbrg,
Andy Shevchenko50bf7d02018-11-20 23:52:36 +0200576 .setconfig = ns16550_serial_setconfig,
577 .getinfo = ns16550_serial_getinfo,
Simon Glass12e431b2014-09-04 16:27:34 -0600578};
Thomas Chou8e62d322015-11-19 21:48:05 +0800579
Alexandru Gagniuc6f8c3512017-03-27 12:54:19 -0700580#if CONFIG_IS_ENABLED(OF_CONTROL) && !CONFIG_IS_ENABLED(OF_PLATDATA)
Thomas Choucc4228f2015-12-14 20:45:09 +0800581/*
582 * Please consider existing compatible strings before adding a new
583 * one to keep this table compact. Or you may add a generic "ns16550"
584 * compatible string to your dts.
585 */
Thomas Chou8e62d322015-11-19 21:48:05 +0800586static const struct udevice_id ns16550_serial_ids[] = {
Marek Vasut79fd9282016-12-01 02:06:30 +0100587 { .compatible = "ns16550", .data = PORT_NS16550 },
588 { .compatible = "ns16550a", .data = PORT_NS16550 },
Marek Vasut0b060ee2016-12-01 02:06:31 +0100589 { .compatible = "ingenic,jz4780-uart", .data = PORT_JZ4780 },
Marek Vasut79fd9282016-12-01 02:06:30 +0100590 { .compatible = "nvidia,tegra20-uart", .data = PORT_NS16550 },
591 { .compatible = "snps,dw-apb-uart", .data = PORT_NS16550 },
Thomas Chou8e62d322015-11-19 21:48:05 +0800592 {}
593};
Alexandru Gagniuc6f8c3512017-03-27 12:54:19 -0700594#endif /* OF_CONTROL && !OF_PLATDATA */
Thomas Chou8e62d322015-11-19 21:48:05 +0800595
Simon Glassb7e29832015-12-13 21:36:59 -0700596#if CONFIG_IS_ENABLED(SERIAL_PRESENT)
Alexandru Gagniuc6f8c3512017-03-27 12:54:19 -0700597
598/* TODO(sjg@chromium.org): Integrate this into a macro like CONFIG_IS_ENABLED */
599#if !defined(CONFIG_TPL_BUILD) || defined(CONFIG_TPL_DM_SERIAL)
Thomas Chou8e62d322015-11-19 21:48:05 +0800600U_BOOT_DRIVER(ns16550_serial) = {
601 .name = "ns16550_serial",
602 .id = UCLASS_SERIAL,
Alexandru Gagniuc6f8c3512017-03-27 12:54:19 -0700603#if CONFIG_IS_ENABLED(OF_CONTROL) && !CONFIG_IS_ENABLED(OF_PLATDATA)
Thomas Chou8e62d322015-11-19 21:48:05 +0800604 .of_match = ns16550_serial_ids,
605 .ofdata_to_platdata = ns16550_serial_ofdata_to_platdata,
606 .platdata_auto_alloc_size = sizeof(struct ns16550_platdata),
607#endif
608 .priv_auto_alloc_size = sizeof(struct NS16550),
609 .probe = ns16550_serial_probe,
610 .ops = &ns16550_serial_ops,
Bin Meng46879192018-10-24 06:36:36 -0700611#if !CONFIG_IS_ENABLED(OF_CONTROL)
Simon Glassb7e5a642015-12-04 08:58:38 -0700612 .flags = DM_FLAG_PRE_RELOC,
Bin Meng46879192018-10-24 06:36:36 -0700613#endif
Thomas Chou8e62d322015-11-19 21:48:05 +0800614};
Simon Glassb7e29832015-12-13 21:36:59 -0700615#endif
Alexandru Gagniuc6f8c3512017-03-27 12:54:19 -0700616#endif /* SERIAL_PRESENT */
617
Simon Glass12e431b2014-09-04 16:27:34 -0600618#endif /* CONFIG_DM_SERIAL */