blob: 3f6860f39162df74c3d1f03d3f9aa90090216646 [file] [log] [blame]
wdenke85390d2002-04-01 14:29:03 +00001/*
2 * COM1 NS16550 support
Stefan Roese88fbf932010-04-15 16:07:28 +02003 * originally from linux source (arch/powerpc/boot/ns16550.c)
Jean-Christophe PLAGNIOL-VILLARD03836942008-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 Glass85d65312019-12-28 10:44:58 -07007#include <clock_legacy.h>
Tom Rinidec7ea02024-05-20 13:35:03 -06008#include <config.h>
Paul Burton8aadc562016-09-08 07:47:29 +01009#include <clk.h>
Simon Glass79a9da32014-09-04 16:27:34 -060010#include <dm.h>
11#include <errno.h>
Simon Glass0f2af882020-05-10 11:40:05 -060012#include <log.h>
wdenke85390d2002-04-01 14:29:03 +000013#include <ns16550.h>
Ley Foon Tana1763392018-06-14 18:45:22 +080014#include <reset.h>
Simon Glass79a9da32014-09-04 16:27:34 -060015#include <serial.h>
Simon Glassdbf9a082023-09-26 08:14:56 -060016#include <spl.h>
Ladislav Michlcc294422010-02-01 23:34:25 +010017#include <watchdog.h>
Simon Glass3ba929a2020-10-30 21:38:53 -060018#include <asm/global_data.h>
Simon Glassd66c5f72020-02-03 07:36:15 -070019#include <linux/err.h>
Graeme Russ14f06e62010-04-24 00:05:46 +100020#include <linux/types.h>
21#include <asm/io.h>
wdenke85390d2002-04-01 14:29:03 +000022
Simon Glass79a9da32014-09-04 16:27:34 -060023DECLARE_GLOBAL_DATA_PTR;
24
Detlev Zundel166fb542009-04-03 11:53:01 +020025#define UART_LCRVAL UART_LCR_8N1 /* 8 data, 1 stop, no parity */
26#define UART_MCRVAL (UART_MCR_DTR | \
27 UART_MCR_RTS) /* RTS/DTR */
Simon Glass79a9da32014-09-04 16:27:34 -060028
Simon Glassd945a492019-09-25 08:11:14 -060029#if !CONFIG_IS_ENABLED(DM_SERIAL)
Graeme Russ14f06e62010-04-24 00:05:46 +100030#ifdef CONFIG_SYS_NS16550_PORT_MAPPED
Simon Glassdd5497c2011-10-15 19:14:09 +000031#define serial_out(x, y) outb(x, (ulong)y)
32#define serial_in(y) inb((ulong)y)
Dave Aldridgea51bebc2011-09-01 22:47:14 +000033#elif defined(CONFIG_SYS_NS16550_MEM32) && (CONFIG_SYS_NS16550_REG_SIZE > 0)
Simon Glassdd5497c2011-10-15 19:14:09 +000034#define serial_out(x, y) out_be32(y, x)
35#define serial_in(y) in_be32(y)
Dave Aldridgea51bebc2011-09-01 22:47:14 +000036#elif defined(CONFIG_SYS_NS16550_MEM32) && (CONFIG_SYS_NS16550_REG_SIZE < 0)
Simon Glassdd5497c2011-10-15 19:14:09 +000037#define serial_out(x, y) out_le32(y, x)
38#define serial_in(y) in_le32(y)
Graeme Russ14f06e62010-04-24 00:05:46 +100039#else
Simon Glassdd5497c2011-10-15 19:14:09 +000040#define serial_out(x, y) writeb(x, y)
41#define serial_in(y) readb(y)
Graeme Russ14f06e62010-04-24 00:05:46 +100042#endif
Simon Glass79a9da32014-09-04 16:27:34 -060043#endif /* !CONFIG_DM_SERIAL */
wdenke85390d2002-04-01 14:29:03 +000044
Tom Rini84c0f692021-09-12 20:32:32 -040045#if defined(CONFIG_ARCH_KEYSTONE)
Vitaly Andrianov7bcf4d62014-04-04 13:16:53 -040046#define UART_REG_VAL_PWREMU_MGMT_UART_DISABLE 0
47#define UART_REG_VAL_PWREMU_MGMT_UART_ENABLE ((1 << 14) | (1 << 13) | (1 << 0))
Karicheri, Muralidharancbc08882014-04-09 15:38:46 -040048#undef UART_MCRVAL
49#ifdef CONFIG_SERIAL_HW_FLOW_CONTROL
50#define UART_MCRVAL (UART_MCR_RTS | UART_MCR_AFE)
51#else
52#define UART_MCRVAL (UART_MCR_RTS)
53#endif
Vitaly Andrianov7bcf4d62014-04-04 13:16:53 -040054#endif
55
Tom Rini364d0022023-01-10 11:19:45 -050056#ifndef CFG_SYS_NS16550_IER
57#define CFG_SYS_NS16550_IER 0x00
58#endif /* CFG_SYS_NS16550_IER */
Prafulla Wadaskar66216372010-10-27 21:58:31 +053059
Simon Glassb31fb122015-02-27 22:06:26 -070060static inline void serial_out_shift(void *addr, int shift, int value)
Simon Glass6aba4fd2015-01-26 18:27:08 -070061{
Simon Glass79a9da32014-09-04 16:27:34 -060062#ifdef CONFIG_SYS_NS16550_PORT_MAPPED
Simon Glass96e230b2014-10-10 07:49:13 -060063 outb(value, (ulong)addr);
Bernhard Messerklingerd8427e72018-02-15 09:02:26 +010064#elif defined(CONFIG_SYS_NS16550_MEM32) && defined(CONFIG_SYS_LITTLE_ENDIAN)
Simon Glass79a9da32014-09-04 16:27:34 -060065 out_le32(addr, value);
66#elif defined(CONFIG_SYS_NS16550_MEM32) && defined(CONFIG_SYS_BIG_ENDIAN)
67 out_be32(addr, value);
Simon Glass0b31ec72015-05-12 14:55:02 -060068#elif defined(CONFIG_SYS_NS16550_MEM32)
69 writel(value, addr);
Simon Glass79a9da32014-09-04 16:27:34 -060070#elif defined(CONFIG_SYS_BIG_ENDIAN)
Simon Glass6aba4fd2015-01-26 18:27:08 -070071 writeb(value, addr + (1 << shift) - 1);
Simon Glass79a9da32014-09-04 16:27:34 -060072#else
73 writeb(value, addr);
74#endif
75}
76
Simon Glassb31fb122015-02-27 22:06:26 -070077static inline int serial_in_shift(void *addr, int shift)
Simon Glass79a9da32014-09-04 16:27:34 -060078{
Simon Glass79a9da32014-09-04 16:27:34 -060079#ifdef CONFIG_SYS_NS16550_PORT_MAPPED
Simon Glass96e230b2014-10-10 07:49:13 -060080 return inb((ulong)addr);
Bernhard Messerklingerd8427e72018-02-15 09:02:26 +010081#elif defined(CONFIG_SYS_NS16550_MEM32) && defined(CONFIG_SYS_LITTLE_ENDIAN)
Simon Glass79a9da32014-09-04 16:27:34 -060082 return in_le32(addr);
83#elif defined(CONFIG_SYS_NS16550_MEM32) && defined(CONFIG_SYS_BIG_ENDIAN)
84 return in_be32(addr);
Simon Glass0b31ec72015-05-12 14:55:02 -060085#elif defined(CONFIG_SYS_NS16550_MEM32)
86 return readl(addr);
Simon Glass79a9da32014-09-04 16:27:34 -060087#elif defined(CONFIG_SYS_BIG_ENDIAN)
Axel Linb5c372d2015-02-28 15:55:36 +080088 return readb(addr + (1 << shift) - 1);
Simon Glass79a9da32014-09-04 16:27:34 -060089#else
90 return readb(addr);
91#endif
92}
93
Simon Glassd945a492019-09-25 08:11:14 -060094#if CONFIG_IS_ENABLED(DM_SERIAL)
Marek Vasut3e97cbb2016-05-25 02:13:03 +020095
Tom Rinidf6a2152022-11-16 13:10:28 -050096#ifndef CFG_SYS_NS16550_CLK
97#define CFG_SYS_NS16550_CLK 0
Marek Vasut3e97cbb2016-05-25 02:13:03 +020098#endif
99
Simon Glassf8b1a242019-12-19 17:58:18 -0700100/*
101 * Use this #ifdef for now since many platforms don't define in(), out(),
102 * out_le32(), etc. but we don't have #defines to indicate this.
103 *
104 * TODO(sjg@chromium.org): Add CONFIG options to indicate what I/O is available
105 * on a platform
106 */
107#ifdef CONFIG_NS16550_DYNAMIC
Simon Glassb75b15b2020-12-03 16:55:23 -0700108static void serial_out_dynamic(struct ns16550_plat *plat, u8 *addr,
Simon Glassf8b1a242019-12-19 17:58:18 -0700109 int value)
110{
111 if (plat->flags & NS16550_FLAG_IO) {
112 outb(value, addr);
113 } else if (plat->reg_width == 4) {
114 if (plat->flags & NS16550_FLAG_ENDIAN) {
115 if (plat->flags & NS16550_FLAG_BE)
116 out_be32(addr, value);
117 else
118 out_le32(addr, value);
119 } else {
120 writel(value, addr);
121 }
122 } else if (plat->flags & NS16550_FLAG_BE) {
123 writeb(value, addr + (1 << plat->reg_shift) - 1);
124 } else {
125 writeb(value, addr);
126 }
127}
128
Simon Glassb75b15b2020-12-03 16:55:23 -0700129static int serial_in_dynamic(struct ns16550_plat *plat, u8 *addr)
Simon Glassf8b1a242019-12-19 17:58:18 -0700130{
131 if (plat->flags & NS16550_FLAG_IO) {
132 return inb(addr);
133 } else if (plat->reg_width == 4) {
134 if (plat->flags & NS16550_FLAG_ENDIAN) {
135 if (plat->flags & NS16550_FLAG_BE)
136 return in_be32(addr);
137 else
138 return in_le32(addr);
139 } else {
140 return readl(addr);
141 }
142 } else if (plat->flags & NS16550_FLAG_BE) {
143 return readb(addr + (1 << plat->reg_shift) - 1);
144 } else {
145 return readb(addr);
146 }
147}
148#else
Simon Glassb75b15b2020-12-03 16:55:23 -0700149static inline void serial_out_dynamic(struct ns16550_plat *plat, u8 *addr,
Simon Glassf8b1a242019-12-19 17:58:18 -0700150 int value)
151{
152}
153
Simon Glassb75b15b2020-12-03 16:55:23 -0700154static inline int serial_in_dynamic(struct ns16550_plat *plat, u8 *addr)
Simon Glassf8b1a242019-12-19 17:58:18 -0700155{
156 return 0;
157}
158
159#endif /* CONFIG_NS16550_DYNAMIC */
160
Simon Glass119e7ef2020-12-22 19:30:18 -0700161static void ns16550_writeb(struct ns16550 *port, int offset, int value)
Simon Glass6aba4fd2015-01-26 18:27:08 -0700162{
Simon Glassb75b15b2020-12-03 16:55:23 -0700163 struct ns16550_plat *plat = port->plat;
Simon Glass6aba4fd2015-01-26 18:27:08 -0700164 unsigned char *addr;
165
166 offset *= 1 << plat->reg_shift;
Simon Glassf8b1a242019-12-19 17:58:18 -0700167 addr = (unsigned char *)plat->base + offset + plat->reg_offset;
Paul Burtonc8acc892016-05-17 07:43:26 +0100168
Simon Glassf8b1a242019-12-19 17:58:18 -0700169 if (IS_ENABLED(CONFIG_NS16550_DYNAMIC))
170 serial_out_dynamic(plat, addr, value);
171 else
172 serial_out_shift(addr, plat->reg_shift, value);
Simon Glass6aba4fd2015-01-26 18:27:08 -0700173}
174
Simon Glass119e7ef2020-12-22 19:30:18 -0700175static int ns16550_readb(struct ns16550 *port, int offset)
Simon Glass6aba4fd2015-01-26 18:27:08 -0700176{
Simon Glassb75b15b2020-12-03 16:55:23 -0700177 struct ns16550_plat *plat = port->plat;
Simon Glass6aba4fd2015-01-26 18:27:08 -0700178 unsigned char *addr;
179
180 offset *= 1 << plat->reg_shift;
Simon Glassf8b1a242019-12-19 17:58:18 -0700181 addr = (unsigned char *)plat->base + offset + plat->reg_offset;
Simon Glass6aba4fd2015-01-26 18:27:08 -0700182
Simon Glassf8b1a242019-12-19 17:58:18 -0700183 if (IS_ENABLED(CONFIG_NS16550_DYNAMIC))
184 return serial_in_dynamic(plat, addr);
185 else
186 return serial_in_shift(addr, plat->reg_shift);
Simon Glass6aba4fd2015-01-26 18:27:08 -0700187}
188
Simon Glass119e7ef2020-12-22 19:30:18 -0700189static u32 ns16550_getfcr(struct ns16550 *port)
Marek Vasutf523c9c2016-12-01 02:06:29 +0100190{
Simon Glassb75b15b2020-12-03 16:55:23 -0700191 struct ns16550_plat *plat = port->plat;
Marek Vasutf523c9c2016-12-01 02:06:29 +0100192
193 return plat->fcr;
194}
195
Simon Glass79a9da32014-09-04 16:27:34 -0600196/* We can clean these up once everything is moved to driver model */
197#define serial_out(value, addr) \
Simon Glassb31fb122015-02-27 22:06:26 -0700198 ns16550_writeb(com_port, \
199 (unsigned char *)addr - (unsigned char *)com_port, value)
Simon Glass79a9da32014-09-04 16:27:34 -0600200#define serial_in(addr) \
Simon Glassb31fb122015-02-27 22:06:26 -0700201 ns16550_readb(com_port, \
202 (unsigned char *)addr - (unsigned char *)com_port)
Marek Vasutf523c9c2016-12-01 02:06:29 +0100203#else
Simon Glass119e7ef2020-12-22 19:30:18 -0700204static u32 ns16550_getfcr(struct ns16550 *port)
Marek Vasutf523c9c2016-12-01 02:06:29 +0100205{
Heiko Schocher06f108e2017-01-18 08:05:49 +0100206 return UART_FCR_DEFVAL;
Marek Vasutf523c9c2016-12-01 02:06:29 +0100207}
Simon Glass79a9da32014-09-04 16:27:34 -0600208#endif
209
Simon Glass119e7ef2020-12-22 19:30:18 -0700210int ns16550_calc_divisor(struct ns16550 *port, int clock, int baudrate)
Simon Glasse98e01e2014-09-04 16:27:32 -0600211{
212 const unsigned int mode_x_div = 16;
213
Simon Glass27afb522015-01-26 18:27:09 -0700214 return DIV_ROUND_CLOSEST(clock, mode_x_div * baudrate);
215}
216
Simon Glass2b923982020-12-22 19:30:19 -0700217static void ns16550_setbrg(struct ns16550 *com_port, int baud_divisor)
Simon Glassc31ebfe2014-09-04 16:27:33 -0600218{
Simon Goldschmidtba2d0aa2018-11-02 21:28:08 +0100219 /* to keep serial format, read lcr before writing BKSE */
220 int lcr_val = serial_in(&com_port->lcr) & ~UART_LCR_BKSE;
221
222 serial_out(UART_LCR_BKSE | lcr_val, &com_port->lcr);
Simon Glassc31ebfe2014-09-04 16:27:33 -0600223 serial_out(baud_divisor & 0xff, &com_port->dll);
224 serial_out((baud_divisor >> 8) & 0xff, &com_port->dlm);
Simon Goldschmidtba2d0aa2018-11-02 21:28:08 +0100225 serial_out(lcr_val, &com_port->lcr);
Simon Glassc31ebfe2014-09-04 16:27:33 -0600226}
227
Simon Glass2b923982020-12-22 19:30:19 -0700228void ns16550_init(struct ns16550 *com_port, int baud_divisor)
wdenke85390d2002-04-01 14:29:03 +0000229{
Simon Glass7ec24132024-09-29 19:49:48 -0600230#if defined(CONFIG_XPL_BUILD) && defined(CONFIG_OMAP34XX)
Manfred Huberf9b8ae32013-03-29 02:52:36 +0000231 /*
Gregoire Gentil6b05d0a2014-11-10 11:04:10 -0800232 * On some OMAP3/OMAP4 devices when UART3 is configured for boot mode
233 * before SPL starts only THRE bit is set. We have to empty the
234 * transmitter before initialization starts.
Manfred Huberf9b8ae32013-03-29 02:52:36 +0000235 */
236 if ((serial_in(&com_port->lsr) & (UART_LSR_TEMT | UART_LSR_THRE))
237 == UART_LSR_THRE) {
Simon Glass79a9da32014-09-04 16:27:34 -0600238 if (baud_divisor != -1)
Simon Glass2b923982020-12-22 19:30:19 -0700239 ns16550_setbrg(com_port, baud_divisor);
Patrik Dahlström7e1bda92019-12-21 17:25:12 +0100240 else {
241 // Re-use old baud rate divisor to flush transmit reg.
242 const int dll = serial_in(&com_port->dll);
243 const int dlm = serial_in(&com_port->dlm);
244 const int divisor = dll | (dlm << 8);
Simon Glass2b923982020-12-22 19:30:19 -0700245 ns16550_setbrg(com_port, divisor);
Patrik Dahlström7e1bda92019-12-21 17:25:12 +0100246 }
Manfred Huberf9b8ae32013-03-29 02:52:36 +0000247 serial_out(0, &com_port->mdr1);
248 }
249#endif
250
Scott Wood6c6f0612012-09-18 18:19:05 -0500251 while (!(serial_in(&com_port->lsr) & UART_LSR_TEMT))
252 ;
253
Tom Rini364d0022023-01-10 11:19:45 -0500254 serial_out(CFG_SYS_NS16550_IER, &com_port->ier);
Lokesh Vutla42cb4b82018-08-27 15:55:24 +0530255#if defined(CONFIG_ARCH_OMAP2PLUS) || defined(CONFIG_OMAP_SERIAL)
Graeme Russ14f06e62010-04-24 00:05:46 +1000256 serial_out(0x7, &com_port->mdr1); /* mode select reset TL16C750*/
wdenk21136db2003-07-16 21:53:01 +0000257#endif
Ley Foon Tana1763392018-06-14 18:45:22 +0800258
Graeme Russ14f06e62010-04-24 00:05:46 +1000259 serial_out(UART_MCRVAL, &com_port->mcr);
Marek Vasutf523c9c2016-12-01 02:06:29 +0100260 serial_out(ns16550_getfcr(com_port), &com_port->fcr);
Simon Goldschmidtba2d0aa2018-11-02 21:28:08 +0100261 /* initialize serial config to 8N1 before writing baudrate */
262 serial_out(UART_LCRVAL, &com_port->lcr);
Simon Glass79a9da32014-09-04 16:27:34 -0600263 if (baud_divisor != -1)
Simon Glass2b923982020-12-22 19:30:19 -0700264 ns16550_setbrg(com_port, baud_divisor);
Lokesh Vutla42cb4b82018-08-27 15:55:24 +0530265#if defined(CONFIG_ARCH_OMAP2PLUS) || defined(CONFIG_SOC_DA8XX) || \
266 defined(CONFIG_OMAP_SERIAL)
Simon Glassdd5497c2011-10-15 19:14:09 +0000267 /* /16 is proper to hit 115200 with 48MHz */
268 serial_out(0, &com_port->mdr1);
Tom Rinif28c4342017-05-12 22:33:16 -0400269#endif
Tom Rini84c0f692021-09-12 20:32:32 -0400270#if defined(CONFIG_ARCH_KEYSTONE)
Vitaly Andrianov7bcf4d62014-04-04 13:16:53 -0400271 serial_out(UART_REG_VAL_PWREMU_MGMT_UART_ENABLE, &com_port->regC);
272#endif
wdenke85390d2002-04-01 14:29:03 +0000273}
274
Tom Rini10ac24f2022-11-16 13:10:26 -0500275#if !CONFIG_IS_ENABLED(NS16550_MIN_FUNCTIONS)
Simon Glass2b923982020-12-22 19:30:19 -0700276void ns16550_reinit(struct ns16550 *com_port, int baud_divisor)
wdenke85390d2002-04-01 14:29:03 +0000277{
Tom Rini364d0022023-01-10 11:19:45 -0500278 serial_out(CFG_SYS_NS16550_IER, &com_port->ier);
Simon Glass2b923982020-12-22 19:30:19 -0700279 ns16550_setbrg(com_port, 0);
Graeme Russ14f06e62010-04-24 00:05:46 +1000280 serial_out(UART_MCRVAL, &com_port->mcr);
Marek Vasutf523c9c2016-12-01 02:06:29 +0100281 serial_out(ns16550_getfcr(com_port), &com_port->fcr);
Simon Glass2b923982020-12-22 19:30:19 -0700282 ns16550_setbrg(com_port, baud_divisor);
wdenke85390d2002-04-01 14:29:03 +0000283}
Tom Rini10ac24f2022-11-16 13:10:26 -0500284#endif /* !CONFIG_IS_ENABLED(NS16550_MIN_FUNCTIONS) */
wdenke85390d2002-04-01 14:29:03 +0000285
Simon Glass2b923982020-12-22 19:30:19 -0700286void ns16550_putc(struct ns16550 *com_port, char c)
wdenke85390d2002-04-01 14:29:03 +0000287{
Simon Glassdd5497c2011-10-15 19:14:09 +0000288 while ((serial_in(&com_port->lsr) & UART_LSR_THRE) == 0)
289 ;
Graeme Russ14f06e62010-04-24 00:05:46 +1000290 serial_out(c, &com_port->thr);
Stefan Roese57b99882010-10-12 09:39:45 +0200291
292 /*
Rasmus Villemoes76aaf692024-05-28 13:13:21 +0200293 * Call schedule() upon newline. This is done here in putc
Stefan Roese57b99882010-10-12 09:39:45 +0200294 * since the environment code uses a single puts() to print the complete
Rasmus Villemoes76aaf692024-05-28 13:13:21 +0200295 * environment upon "printenv". So we can't put this schedule call
Stefan Roese57b99882010-10-12 09:39:45 +0200296 * in puts().
297 */
298 if (c == '\n')
Stefan Roese80877fa2022-09-02 14:10:46 +0200299 schedule();
wdenke85390d2002-04-01 14:29:03 +0000300}
301
Tom Rini10ac24f2022-11-16 13:10:26 -0500302#if !CONFIG_IS_ENABLED(NS16550_MIN_FUNCTIONS)
Simon Glass2b923982020-12-22 19:30:19 -0700303char ns16550_getc(struct ns16550 *com_port)
wdenke85390d2002-04-01 14:29:03 +0000304{
Graeme Russ14f06e62010-04-24 00:05:46 +1000305 while ((serial_in(&com_port->lsr) & UART_LSR_DR) == 0) {
Simon Glass7ec24132024-09-29 19:49:48 -0600306#if !defined(CONFIG_XPL_BUILD) && defined(CONFIG_USB_TTY)
wdenk29e7f5a2004-03-12 00:14:09 +0000307 extern void usbtty_poll(void);
308 usbtty_poll();
309#endif
Stefan Roese80877fa2022-09-02 14:10:46 +0200310 schedule();
wdenk29e7f5a2004-03-12 00:14:09 +0000311 }
Graeme Russ14f06e62010-04-24 00:05:46 +1000312 return serial_in(&com_port->rbr);
wdenke85390d2002-04-01 14:29:03 +0000313}
314
Simon Glass2b923982020-12-22 19:30:19 -0700315int ns16550_tstc(struct ns16550 *com_port)
wdenke85390d2002-04-01 14:29:03 +0000316{
Simon Glassdd5497c2011-10-15 19:14:09 +0000317 return (serial_in(&com_port->lsr) & UART_LSR_DR) != 0;
wdenke85390d2002-04-01 14:29:03 +0000318}
319
Tom Rini10ac24f2022-11-16 13:10:26 -0500320#endif /* !CONFIG_IS_ENABLED(NS16550_MIN_FUNCTIONS) */
Simon Glass79a9da32014-09-04 16:27:34 -0600321
Simon Glass27afb522015-01-26 18:27:09 -0700322#ifdef CONFIG_DEBUG_UART_NS16550
323
324#include <debug_uart.h>
325
Simon Glass60517d72015-10-18 19:51:23 -0600326static inline void _debug_uart_init(void)
Simon Glass27afb522015-01-26 18:27:09 -0700327{
Pali Rohár53a85a72022-05-06 11:05:16 +0200328 struct ns16550 *com_port = (struct ns16550 *)CONFIG_VAL(DEBUG_UART_BASE);
Simon Glass27afb522015-01-26 18:27:09 -0700329 int baud_divisor;
330
Pali Rohárb318d6e2022-06-23 14:13:56 +0200331 /* Wait until tx buffer is empty */
332 while (!(serial_din(&com_port->lsr) & UART_LSR_TEMT))
333 ;
334
Simon Glass27afb522015-01-26 18:27:09 -0700335 /*
336 * We copy the code from above because it is already horribly messy.
337 * Trying to refactor to nicely remove the duplication doesn't seem
338 * feasible. The better fix is to move all users of this driver to
339 * driver model.
340 */
Marek Vasut3b164a52016-05-25 02:13:16 +0200341 baud_divisor = ns16550_calc_divisor(com_port, CONFIG_DEBUG_UART_CLOCK,
342 CONFIG_BAUDRATE);
Tom Rini364d0022023-01-10 11:19:45 -0500343 serial_dout(&com_port->ier, CFG_SYS_NS16550_IER);
Lokesh Vutla771d69c2017-04-22 15:57:25 +0530344 serial_dout(&com_port->mcr, UART_MCRVAL);
345 serial_dout(&com_port->fcr, UART_FCR_DEFVAL);
346
347 serial_dout(&com_port->lcr, UART_LCR_BKSE | UART_LCRVAL);
348 serial_dout(&com_port->dll, baud_divisor & 0xff);
349 serial_dout(&com_port->dlm, (baud_divisor >> 8) & 0xff);
350 serial_dout(&com_port->lcr, UART_LCRVAL);
351}
352
Simon Glass119e7ef2020-12-22 19:30:18 -0700353static inline int NS16550_read_baud_divisor(struct ns16550 *com_port)
Simon Goldschmidte03ad212019-01-09 20:35:31 +0100354{
355 int ret;
356
357 serial_dout(&com_port->lcr, UART_LCR_BKSE | UART_LCRVAL);
358 ret = serial_din(&com_port->dll) & 0xff;
359 ret |= (serial_din(&com_port->dlm) & 0xff) << 8;
360 serial_dout(&com_port->lcr, UART_LCRVAL);
361
362 return ret;
363}
364
Lokesh Vutla771d69c2017-04-22 15:57:25 +0530365static inline void _debug_uart_putc(int ch)
366{
Pali Rohár53a85a72022-05-06 11:05:16 +0200367 struct ns16550 *com_port = (struct ns16550 *)CONFIG_VAL(DEBUG_UART_BASE);
Lokesh Vutla771d69c2017-04-22 15:57:25 +0530368
Simon Goldschmidte03ad212019-01-09 20:35:31 +0100369 while (!(serial_din(&com_port->lsr) & UART_LSR_THRE)) {
370#ifdef CONFIG_DEBUG_UART_NS16550_CHECK_ENABLED
371 if (!NS16550_read_baud_divisor(com_port))
372 return;
373#endif
374 }
Lokesh Vutla771d69c2017-04-22 15:57:25 +0530375 serial_dout(&com_port->thr, ch);
376}
377
378DEBUG_UART_FUNCS
379
380#endif
381
Simon Glassd945a492019-09-25 08:11:14 -0600382#if CONFIG_IS_ENABLED(DM_SERIAL)
Simon Glass79a9da32014-09-04 16:27:34 -0600383static int ns16550_serial_putc(struct udevice *dev, const char ch)
384{
Simon Glass119e7ef2020-12-22 19:30:18 -0700385 struct ns16550 *const com_port = dev_get_priv(dev);
Simon Glass79a9da32014-09-04 16:27:34 -0600386
387 if (!(serial_in(&com_port->lsr) & UART_LSR_THRE))
388 return -EAGAIN;
389 serial_out(ch, &com_port->thr);
390
391 /*
Rasmus Villemoes76aaf692024-05-28 13:13:21 +0200392 * Call schedule() upon newline. This is done here in putc
Simon Glass79a9da32014-09-04 16:27:34 -0600393 * since the environment code uses a single puts() to print the complete
Rasmus Villemoes76aaf692024-05-28 13:13:21 +0200394 * environment upon "printenv". So we can't put this schedule call
Simon Glass79a9da32014-09-04 16:27:34 -0600395 * in puts().
396 */
397 if (ch == '\n')
Stefan Roese80877fa2022-09-02 14:10:46 +0200398 schedule();
Simon Glass79a9da32014-09-04 16:27:34 -0600399
400 return 0;
401}
402
403static int ns16550_serial_pending(struct udevice *dev, bool input)
404{
Simon Glass119e7ef2020-12-22 19:30:18 -0700405 struct ns16550 *const com_port = dev_get_priv(dev);
Simon Glass79a9da32014-09-04 16:27:34 -0600406
407 if (input)
Mario Sixaed664a2018-01-15 11:09:49 +0100408 return (serial_in(&com_port->lsr) & UART_LSR_DR) ? 1 : 0;
Simon Glass79a9da32014-09-04 16:27:34 -0600409 else
Mario Sixaed664a2018-01-15 11:09:49 +0100410 return (serial_in(&com_port->lsr) & UART_LSR_THRE) ? 0 : 1;
Simon Glass79a9da32014-09-04 16:27:34 -0600411}
412
413static int ns16550_serial_getc(struct udevice *dev)
414{
Simon Glass119e7ef2020-12-22 19:30:18 -0700415 struct ns16550 *const com_port = dev_get_priv(dev);
Stefan Roese48b771c2017-08-16 17:37:15 +0200416
417 if (!(serial_in(&com_port->lsr) & UART_LSR_DR))
Simon Glass79a9da32014-09-04 16:27:34 -0600418 return -EAGAIN;
419
Stefan Roese48b771c2017-08-16 17:37:15 +0200420 return serial_in(&com_port->rbr);
Simon Glass79a9da32014-09-04 16:27:34 -0600421}
422
423static int ns16550_serial_setbrg(struct udevice *dev, int baudrate)
424{
Simon Glass119e7ef2020-12-22 19:30:18 -0700425 struct ns16550 *const com_port = dev_get_priv(dev);
Simon Glassb75b15b2020-12-03 16:55:23 -0700426 struct ns16550_plat *plat = com_port->plat;
Simon Glass79a9da32014-09-04 16:27:34 -0600427 int clock_divisor;
428
429 clock_divisor = ns16550_calc_divisor(com_port, plat->clock, baudrate);
430
Simon Glass2b923982020-12-22 19:30:19 -0700431 ns16550_setbrg(com_port, clock_divisor);
Simon Glass79a9da32014-09-04 16:27:34 -0600432
Simon Goldschmidtba2d0aa2018-11-02 21:28:08 +0100433 return 0;
434}
435
436static int ns16550_serial_setconfig(struct udevice *dev, uint serial_config)
437{
Simon Glass119e7ef2020-12-22 19:30:18 -0700438 struct ns16550 *const com_port = dev_get_priv(dev);
Simon Goldschmidtba2d0aa2018-11-02 21:28:08 +0100439 int lcr_val = UART_LCR_WLS_8;
440 uint parity = SERIAL_GET_PARITY(serial_config);
441 uint bits = SERIAL_GET_BITS(serial_config);
442 uint stop = SERIAL_GET_STOP(serial_config);
443
444 /*
445 * only parity config is implemented, check if other serial settings
446 * are the default one.
447 */
448 if (bits != SERIAL_8_BITS || stop != SERIAL_ONE_STOP)
449 return -ENOTSUPP; /* not supported in driver*/
450
451 switch (parity) {
452 case SERIAL_PAR_NONE:
453 /* no bits to add */
454 break;
455 case SERIAL_PAR_ODD:
456 lcr_val |= UART_LCR_PEN;
457 break;
458 case SERIAL_PAR_EVEN:
459 lcr_val |= UART_LCR_PEN | UART_LCR_EPS;
460 break;
461 default:
462 return -ENOTSUPP; /* not supported in driver*/
463 }
464
465 serial_out(lcr_val, &com_port->lcr);
Simon Glass79a9da32014-09-04 16:27:34 -0600466 return 0;
467}
468
Andy Shevchenkod778fc42018-11-20 23:52:36 +0200469static int ns16550_serial_getinfo(struct udevice *dev,
470 struct serial_device_info *info)
471{
Simon Glass119e7ef2020-12-22 19:30:18 -0700472 struct ns16550 *const com_port = dev_get_priv(dev);
Simon Glassb75b15b2020-12-03 16:55:23 -0700473 struct ns16550_plat *plat = com_port->plat;
Andy Shevchenkod778fc42018-11-20 23:52:36 +0200474
Simon Glassdbf9a082023-09-26 08:14:56 -0600475 /* save code size */
Simon Glassf218eeb2024-09-29 19:49:37 -0600476 if (!not_xpl())
Simon Glassdbf9a082023-09-26 08:14:56 -0600477 return -ENOSYS;
478
Andy Shevchenkod778fc42018-11-20 23:52:36 +0200479 info->type = SERIAL_CHIP_16550_COMPATIBLE;
480#ifdef CONFIG_SYS_NS16550_PORT_MAPPED
481 info->addr_space = SERIAL_ADDRESS_SPACE_IO;
482#else
483 info->addr_space = SERIAL_ADDRESS_SPACE_MEMORY;
484#endif
485 info->addr = plat->base;
Simon Glass4289c262023-09-26 08:14:58 -0600486 info->size = plat->size;
Andy Shevchenkod778fc42018-11-20 23:52:36 +0200487 info->reg_width = plat->reg_width;
488 info->reg_shift = plat->reg_shift;
489 info->reg_offset = plat->reg_offset;
Andy Shevchenkoee2c9222020-02-27 17:21:55 +0200490 info->clock = plat->clock;
491
Andy Shevchenkod778fc42018-11-20 23:52:36 +0200492 return 0;
493}
494
Simon Glass4289c262023-09-26 08:14:58 -0600495static int ns16550_serial_assign_base(struct ns16550_plat *plat,
496 fdt_addr_t base, fdt_size_t size)
Wolfgang Wallnerf89c7ec2020-03-02 14:41:14 +0100497{
Bin Menge92e09d2020-04-03 18:35:32 -0700498 if (base == FDT_ADDR_T_NONE)
Wolfgang Wallnerf89c7ec2020-03-02 14:41:14 +0100499 return -EINVAL;
500
501#ifdef CONFIG_SYS_NS16550_PORT_MAPPED
Bin Menge92e09d2020-04-03 18:35:32 -0700502 plat->base = base;
Wolfgang Wallnerf89c7ec2020-03-02 14:41:14 +0100503#else
Bin Menge92e09d2020-04-03 18:35:32 -0700504 plat->base = (unsigned long)map_physmem(base, 0, MAP_NOCACHE);
Wolfgang Wallnerf89c7ec2020-03-02 14:41:14 +0100505#endif
Simon Glass4289c262023-09-26 08:14:58 -0600506 plat->size = size;
Wolfgang Wallnerf89c7ec2020-03-02 14:41:14 +0100507
508 return 0;
509}
Wolfgang Wallnerf89c7ec2020-03-02 14:41:14 +0100510
Simon Glass79a9da32014-09-04 16:27:34 -0600511int ns16550_serial_probe(struct udevice *dev)
512{
Simon Glass95588622020-12-22 19:30:28 -0700513 struct ns16550_plat *plat = dev_get_plat(dev);
Simon Glass119e7ef2020-12-22 19:30:18 -0700514 struct ns16550 *const com_port = dev_get_priv(dev);
Ley Foon Tana1763392018-06-14 18:45:22 +0800515 struct reset_ctl_bulk reset_bulk;
Bin Menge92e09d2020-04-03 18:35:32 -0700516 fdt_addr_t addr;
Simon Glass4289c262023-09-26 08:14:58 -0600517 fdt_addr_t size;
Ley Foon Tana1763392018-06-14 18:45:22 +0800518 int ret;
519
Bin Menge92e09d2020-04-03 18:35:32 -0700520 /*
521 * If we are on PCI bus, either directly attached to a PCI root port,
Simon Glass71fa5b42020-12-03 16:55:18 -0700522 * or via a PCI bridge, assign plat->base before probing hardware.
Bin Menge92e09d2020-04-03 18:35:32 -0700523 */
524 if (device_is_on_pci_bus(dev)) {
Simon Glass4289c262023-09-26 08:14:58 -0600525 addr = devfdt_get_addr_pci(dev, &size);
526 ret = ns16550_serial_assign_base(plat, addr, size);
Bin Menge92e09d2020-04-03 18:35:32 -0700527 if (ret)
528 return ret;
529 }
Wolfgang Wallnerf89c7ec2020-03-02 14:41:14 +0100530
Ley Foon Tana1763392018-06-14 18:45:22 +0800531 ret = reset_get_bulk(dev, &reset_bulk);
532 if (!ret)
533 reset_deassert_bulk(&reset_bulk);
Simon Glass79a9da32014-09-04 16:27:34 -0600534
Simon Glassfa20e932020-12-03 16:55:20 -0700535 com_port->plat = dev_get_plat(dev);
Simon Glass2b923982020-12-22 19:30:19 -0700536 ns16550_init(com_port, -1);
Simon Glass79a9da32014-09-04 16:27:34 -0600537
538 return 0;
539}
540
Marek Vasut1a59eec2016-12-01 02:06:30 +0100541#if CONFIG_IS_ENABLED(OF_CONTROL)
542enum {
543 PORT_NS16550 = 0,
Marek Vasut92a744f2016-12-01 02:06:31 +0100544 PORT_JZ4780,
Marek Vasut1a59eec2016-12-01 02:06:30 +0100545};
546#endif
547
Simon Glass3580f6d2021-08-07 07:24:03 -0600548#if CONFIG_IS_ENABLED(OF_REAL)
Simon Glassaad29ae2020-12-03 16:55:21 -0700549int ns16550_serial_of_to_plat(struct udevice *dev)
Simon Glass79a9da32014-09-04 16:27:34 -0600550{
Simon Glass95588622020-12-22 19:30:28 -0700551 struct ns16550_plat *plat = dev_get_plat(dev);
Marek Vasut92a744f2016-12-01 02:06:31 +0100552 const u32 port_type = dev_get_driver_data(dev);
Simon Glass4289c262023-09-26 08:14:58 -0600553 fdt_size_t size = 0;
Bin Menge92e09d2020-04-03 18:35:32 -0700554 fdt_addr_t addr;
Masahiro Yamada09abe2b2016-09-26 20:45:27 +0900555 struct clk clk;
556 int err;
Simon Glass79a9da32014-09-04 16:27:34 -0600557
Simon Glassf218eeb2024-09-29 19:49:37 -0600558 addr = not_xpl() ? dev_read_addr_size(dev, &size) :
Simon Glass4289c262023-09-26 08:14:58 -0600559 dev_read_addr(dev);
560 err = ns16550_serial_assign_base(plat, addr, size);
Bin Menge92e09d2020-04-03 18:35:32 -0700561 if (err && !device_is_on_pci_bus(dev))
562 return err;
563
Philipp Tomsiche74958e2017-06-07 18:46:02 +0200564 plat->reg_offset = dev_read_u32_default(dev, "reg-offset", 0);
565 plat->reg_shift = dev_read_u32_default(dev, "reg-shift", 0);
Andy Shevchenko72fccfe2018-11-20 23:52:35 +0200566 plat->reg_width = dev_read_u32_default(dev, "reg-io-width", 1);
Paul Burton8aadc562016-09-08 07:47:29 +0100567
Paul Burton8aadc562016-09-08 07:47:29 +0100568 if (!plat->clock)
Jonas Karlmandba266f2024-08-04 15:09:52 +0000569 plat->clock = dev_read_u32_default(dev, "clock-frequency", 0);
570 if (!plat->clock) {
571 err = clk_get_by_index(dev, 0, &clk);
572 if (!err) {
573 err = clk_get_rate(&clk);
574 if (!IS_ERR_VALUE(err))
575 plat->clock = err;
576 } else if (err != -ENOENT && err != -ENODEV && err != -ENOSYS) {
577 debug("ns16550 failed to get clock\n");
578 return err;
579 }
580 }
Bin Meng48ab3df2021-02-03 22:42:25 +0800581 if (!plat->clock)
Tom Rinidf6a2152022-11-16 13:10:28 -0500582 plat->clock = CFG_SYS_NS16550_CLK;
Thomas Chou16b2e7e2015-11-19 21:48:05 +0800583 if (!plat->clock) {
584 debug("ns16550 clock not defined\n");
585 return -EINVAL;
586 }
Simon Glass79a9da32014-09-04 16:27:34 -0600587
Heiko Schocher06f108e2017-01-18 08:05:49 +0100588 plat->fcr = UART_FCR_DEFVAL;
Marek Vasut92a744f2016-12-01 02:06:31 +0100589 if (port_type == PORT_JZ4780)
590 plat->fcr |= UART_FCR_UME;
Marek Vasutf523c9c2016-12-01 02:06:29 +0100591
Simon Glass79a9da32014-09-04 16:27:34 -0600592 return 0;
593}
Simon Glass3bf04f32014-10-22 21:37:05 -0600594#endif
Simon Glass79a9da32014-09-04 16:27:34 -0600595
596const struct dm_serial_ops ns16550_serial_ops = {
597 .putc = ns16550_serial_putc,
598 .pending = ns16550_serial_pending,
599 .getc = ns16550_serial_getc,
600 .setbrg = ns16550_serial_setbrg,
Andy Shevchenkod778fc42018-11-20 23:52:36 +0200601 .setconfig = ns16550_serial_setconfig,
602 .getinfo = ns16550_serial_getinfo,
Simon Glass79a9da32014-09-04 16:27:34 -0600603};
Thomas Chou16b2e7e2015-11-19 21:48:05 +0800604
Simon Glass3580f6d2021-08-07 07:24:03 -0600605#if CONFIG_IS_ENABLED(OF_REAL)
Thomas Choudad53672015-12-14 20:45:09 +0800606/*
607 * Please consider existing compatible strings before adding a new
608 * one to keep this table compact. Or you may add a generic "ns16550"
609 * compatible string to your dts.
610 */
Thomas Chou16b2e7e2015-11-19 21:48:05 +0800611static const struct udevice_id ns16550_serial_ids[] = {
Marek Vasut1a59eec2016-12-01 02:06:30 +0100612 { .compatible = "ns16550", .data = PORT_NS16550 },
613 { .compatible = "ns16550a", .data = PORT_NS16550 },
Marek Vasut92a744f2016-12-01 02:06:31 +0100614 { .compatible = "ingenic,jz4780-uart", .data = PORT_JZ4780 },
Marek Vasut1a59eec2016-12-01 02:06:30 +0100615 { .compatible = "nvidia,tegra20-uart", .data = PORT_NS16550 },
616 { .compatible = "snps,dw-apb-uart", .data = PORT_NS16550 },
Thomas Chou16b2e7e2015-11-19 21:48:05 +0800617 {}
618};
Simon Glass3580f6d2021-08-07 07:24:03 -0600619#endif /* OF_REAL */
Thomas Chou16b2e7e2015-11-19 21:48:05 +0800620
Simon Glass9ebf3482015-12-13 21:36:59 -0700621#if CONFIG_IS_ENABLED(SERIAL_PRESENT)
Alexandru Gagniucb1ab1892017-03-27 12:54:19 -0700622
623/* TODO(sjg@chromium.org): Integrate this into a macro like CONFIG_IS_ENABLED */
624#if !defined(CONFIG_TPL_BUILD) || defined(CONFIG_TPL_DM_SERIAL)
Thomas Chou16b2e7e2015-11-19 21:48:05 +0800625U_BOOT_DRIVER(ns16550_serial) = {
626 .name = "ns16550_serial",
627 .id = UCLASS_SERIAL,
Simon Glass3580f6d2021-08-07 07:24:03 -0600628#if CONFIG_IS_ENABLED(OF_REAL)
Thomas Chou16b2e7e2015-11-19 21:48:05 +0800629 .of_match = ns16550_serial_ids,
Simon Glassaad29ae2020-12-03 16:55:21 -0700630 .of_to_plat = ns16550_serial_of_to_plat,
Simon Glassb75b15b2020-12-03 16:55:23 -0700631 .plat_auto = sizeof(struct ns16550_plat),
Thomas Chou16b2e7e2015-11-19 21:48:05 +0800632#endif
Simon Glass119e7ef2020-12-22 19:30:18 -0700633 .priv_auto = sizeof(struct ns16550),
Thomas Chou16b2e7e2015-11-19 21:48:05 +0800634 .probe = ns16550_serial_probe,
635 .ops = &ns16550_serial_ops,
Bin Mengbdb33d82018-10-24 06:36:36 -0700636#if !CONFIG_IS_ENABLED(OF_CONTROL)
Simon Glass6ef533e2015-12-04 08:58:38 -0700637 .flags = DM_FLAG_PRE_RELOC,
Bin Mengbdb33d82018-10-24 06:36:36 -0700638#endif
Thomas Chou16b2e7e2015-11-19 21:48:05 +0800639};
Walter Lozano48e5b042020-06-25 01:10:06 -0300640
Simon Glassdf65db82020-12-28 20:34:57 -0700641DM_DRIVER_ALIAS(ns16550_serial, ti_da830_uart)
Simon Glass9ebf3482015-12-13 21:36:59 -0700642#endif
Alexandru Gagniucb1ab1892017-03-27 12:54:19 -0700643#endif /* SERIAL_PRESENT */
644
Simon Glass79a9da32014-09-04 16:27:34 -0600645#endif /* CONFIG_DM_SERIAL */