blob: a2f1b356295ddd42bb30352d8d9d6ab34cfa3012 [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>
Simon Glasse98e01e2014-09-04 16:27:32 -06008#include <common.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>
wdenke85390d2002-04-01 14:29:03 +000012#include <ns16550.h>
Ley Foon Tana1763392018-06-14 18:45:22 +080013#include <reset.h>
Simon Glass79a9da32014-09-04 16:27:34 -060014#include <serial.h>
Ladislav Michlcc294422010-02-01 23:34:25 +010015#include <watchdog.h>
Simon Glassd66c5f72020-02-03 07:36:15 -070016#include <linux/err.h>
Graeme Russ14f06e62010-04-24 00:05:46 +100017#include <linux/types.h>
18#include <asm/io.h>
wdenke85390d2002-04-01 14:29:03 +000019
Simon Glass79a9da32014-09-04 16:27:34 -060020DECLARE_GLOBAL_DATA_PTR;
21
Detlev Zundel166fb542009-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 Glass79a9da32014-09-04 16:27:34 -060025
Simon Glassd945a492019-09-25 08:11:14 -060026#if !CONFIG_IS_ENABLED(DM_SERIAL)
Graeme Russ14f06e62010-04-24 00:05:46 +100027#ifdef CONFIG_SYS_NS16550_PORT_MAPPED
Simon Glassdd5497c2011-10-15 19:14:09 +000028#define serial_out(x, y) outb(x, (ulong)y)
29#define serial_in(y) inb((ulong)y)
Dave Aldridgea51bebc2011-09-01 22:47:14 +000030#elif defined(CONFIG_SYS_NS16550_MEM32) && (CONFIG_SYS_NS16550_REG_SIZE > 0)
Simon Glassdd5497c2011-10-15 19:14:09 +000031#define serial_out(x, y) out_be32(y, x)
32#define serial_in(y) in_be32(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_le32(y, x)
35#define serial_in(y) in_le32(y)
Graeme Russ14f06e62010-04-24 00:05:46 +100036#else
Simon Glassdd5497c2011-10-15 19:14:09 +000037#define serial_out(x, y) writeb(x, y)
38#define serial_in(y) readb(y)
Graeme Russ14f06e62010-04-24 00:05:46 +100039#endif
Simon Glass79a9da32014-09-04 16:27:34 -060040#endif /* !CONFIG_DM_SERIAL */
wdenke85390d2002-04-01 14:29:03 +000041
Khoronzhuk, Ivan80902982014-07-16 00:59:25 +030042#if defined(CONFIG_SOC_KEYSTONE)
Vitaly Andrianov7bcf4d62014-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, Muralidharancbc08882014-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 Andrianov7bcf4d62014-04-04 13:16:53 -040051#endif
52
Prafulla Wadaskar66216372010-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 Glassb31fb122015-02-27 22:06:26 -070057static inline void serial_out_shift(void *addr, int shift, int value)
Simon Glass6aba4fd2015-01-26 18:27:08 -070058{
Simon Glass79a9da32014-09-04 16:27:34 -060059#ifdef CONFIG_SYS_NS16550_PORT_MAPPED
Simon Glass96e230b2014-10-10 07:49:13 -060060 outb(value, (ulong)addr);
Bernhard Messerklingerd8427e72018-02-15 09:02:26 +010061#elif defined(CONFIG_SYS_NS16550_MEM32) && defined(CONFIG_SYS_LITTLE_ENDIAN)
Simon Glass79a9da32014-09-04 16:27:34 -060062 out_le32(addr, value);
63#elif defined(CONFIG_SYS_NS16550_MEM32) && defined(CONFIG_SYS_BIG_ENDIAN)
64 out_be32(addr, value);
Simon Glass0b31ec72015-05-12 14:55:02 -060065#elif defined(CONFIG_SYS_NS16550_MEM32)
66 writel(value, addr);
Simon Glass79a9da32014-09-04 16:27:34 -060067#elif defined(CONFIG_SYS_BIG_ENDIAN)
Simon Glass6aba4fd2015-01-26 18:27:08 -070068 writeb(value, addr + (1 << shift) - 1);
Simon Glass79a9da32014-09-04 16:27:34 -060069#else
70 writeb(value, addr);
71#endif
72}
73
Simon Glassb31fb122015-02-27 22:06:26 -070074static inline int serial_in_shift(void *addr, int shift)
Simon Glass79a9da32014-09-04 16:27:34 -060075{
Simon Glass79a9da32014-09-04 16:27:34 -060076#ifdef CONFIG_SYS_NS16550_PORT_MAPPED
Simon Glass96e230b2014-10-10 07:49:13 -060077 return inb((ulong)addr);
Bernhard Messerklingerd8427e72018-02-15 09:02:26 +010078#elif defined(CONFIG_SYS_NS16550_MEM32) && defined(CONFIG_SYS_LITTLE_ENDIAN)
Simon Glass79a9da32014-09-04 16:27:34 -060079 return in_le32(addr);
80#elif defined(CONFIG_SYS_NS16550_MEM32) && defined(CONFIG_SYS_BIG_ENDIAN)
81 return in_be32(addr);
Simon Glass0b31ec72015-05-12 14:55:02 -060082#elif defined(CONFIG_SYS_NS16550_MEM32)
83 return readl(addr);
Simon Glass79a9da32014-09-04 16:27:34 -060084#elif defined(CONFIG_SYS_BIG_ENDIAN)
Axel Linb5c372d2015-02-28 15:55:36 +080085 return readb(addr + (1 << shift) - 1);
Simon Glass79a9da32014-09-04 16:27:34 -060086#else
87 return readb(addr);
88#endif
89}
90
Simon Glassd945a492019-09-25 08:11:14 -060091#if CONFIG_IS_ENABLED(DM_SERIAL)
Marek Vasut3e97cbb2016-05-25 02:13:03 +020092
93#ifndef CONFIG_SYS_NS16550_CLK
94#define CONFIG_SYS_NS16550_CLK 0
95#endif
96
Simon Glassf8b1a242019-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 Glass6aba4fd2015-01-26 18:27:08 -0700158static 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 Glassf8b1a242019-12-19 17:58:18 -0700164 addr = (unsigned char *)plat->base + offset + plat->reg_offset;
Paul Burtonc8acc892016-05-17 07:43:26 +0100165
Simon Glassf8b1a242019-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 Glass6aba4fd2015-01-26 18:27:08 -0700170}
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 Glassf8b1a242019-12-19 17:58:18 -0700178 addr = (unsigned char *)plat->base + offset + plat->reg_offset;
Simon Glass6aba4fd2015-01-26 18:27:08 -0700179
Simon Glassf8b1a242019-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 Glass6aba4fd2015-01-26 18:27:08 -0700184}
185
Marek Vasutf523c9c2016-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 Glass79a9da32014-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 Glassb31fb122015-02-27 22:06:26 -0700195 ns16550_writeb(com_port, \
196 (unsigned char *)addr - (unsigned char *)com_port, value)
Simon Glass79a9da32014-09-04 16:27:34 -0600197#define serial_in(addr) \
Simon Glassb31fb122015-02-27 22:06:26 -0700198 ns16550_readb(com_port, \
199 (unsigned char *)addr - (unsigned char *)com_port)
Marek Vasutf523c9c2016-12-01 02:06:29 +0100200#else
201static u32 ns16550_getfcr(NS16550_t port)
202{
Heiko Schocher06f108e2017-01-18 08:05:49 +0100203 return UART_FCR_DEFVAL;
Marek Vasutf523c9c2016-12-01 02:06:29 +0100204}
Simon Glass79a9da32014-09-04 16:27:34 -0600205#endif
206
Marek Vasut3b164a52016-05-25 02:13:16 +0200207int ns16550_calc_divisor(NS16550_t port, int clock, int baudrate)
Simon Glasse98e01e2014-09-04 16:27:32 -0600208{
209 const unsigned int mode_x_div = 16;
210
Simon Glass27afb522015-01-26 18:27:09 -0700211 return DIV_ROUND_CLOSEST(clock, mode_x_div * baudrate);
212}
213
Simon Glassc31ebfe2014-09-04 16:27:33 -0600214static void NS16550_setbrg(NS16550_t com_port, int baud_divisor)
215{
Simon Goldschmidtba2d0aa2018-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 Glassc31ebfe2014-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 Goldschmidtba2d0aa2018-11-02 21:28:08 +0100222 serial_out(lcr_val, &com_port->lcr);
Simon Glassc31ebfe2014-09-04 16:27:33 -0600223}
224
Simon Glassdd5497c2011-10-15 19:14:09 +0000225void NS16550_init(NS16550_t com_port, int baud_divisor)
wdenke85390d2002-04-01 14:29:03 +0000226{
Gregoire Gentil6b05d0a2014-11-10 11:04:10 -0800227#if (defined(CONFIG_SPL_BUILD) && \
228 (defined(CONFIG_OMAP34XX) || defined(CONFIG_OMAP44XX)))
Manfred Huberf9b8ae32013-03-29 02:52:36 +0000229 /*
Gregoire Gentil6b05d0a2014-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 Huberf9b8ae32013-03-29 02:52:36 +0000233 */
234 if ((serial_in(&com_port->lsr) & (UART_LSR_TEMT | UART_LSR_THRE))
235 == UART_LSR_THRE) {
Simon Glass79a9da32014-09-04 16:27:34 -0600236 if (baud_divisor != -1)
237 NS16550_setbrg(com_port, baud_divisor);
Patrik Dahlström7e1bda92019-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 Huberf9b8ae32013-03-29 02:52:36 +0000245 serial_out(0, &com_port->mdr1);
246 }
247#endif
248
Scott Wood6c6f0612012-09-18 18:19:05 -0500249 while (!(serial_in(&com_port->lsr) & UART_LSR_TEMT))
250 ;
251
Prafulla Wadaskar66216372010-10-27 21:58:31 +0530252 serial_out(CONFIG_SYS_NS16550_IER, &com_port->ier);
Lokesh Vutla42cb4b82018-08-27 15:55:24 +0530253#if defined(CONFIG_ARCH_OMAP2PLUS) || defined(CONFIG_OMAP_SERIAL)
Graeme Russ14f06e62010-04-24 00:05:46 +1000254 serial_out(0x7, &com_port->mdr1); /* mode select reset TL16C750*/
wdenk21136db2003-07-16 21:53:01 +0000255#endif
Ley Foon Tana1763392018-06-14 18:45:22 +0800256
Graeme Russ14f06e62010-04-24 00:05:46 +1000257 serial_out(UART_MCRVAL, &com_port->mcr);
Marek Vasutf523c9c2016-12-01 02:06:29 +0100258 serial_out(ns16550_getfcr(com_port), &com_port->fcr);
Simon Goldschmidtba2d0aa2018-11-02 21:28:08 +0100259 /* initialize serial config to 8N1 before writing baudrate */
260 serial_out(UART_LCRVAL, &com_port->lcr);
Simon Glass79a9da32014-09-04 16:27:34 -0600261 if (baud_divisor != -1)
262 NS16550_setbrg(com_port, baud_divisor);
Lokesh Vutla42cb4b82018-08-27 15:55:24 +0530263#if defined(CONFIG_ARCH_OMAP2PLUS) || defined(CONFIG_SOC_DA8XX) || \
264 defined(CONFIG_OMAP_SERIAL)
Simon Glassdd5497c2011-10-15 19:14:09 +0000265 /* /16 is proper to hit 115200 with 48MHz */
266 serial_out(0, &com_port->mdr1);
Tom Rinif28c4342017-05-12 22:33:16 -0400267#endif
Khoronzhuk, Ivan80902982014-07-16 00:59:25 +0300268#if defined(CONFIG_SOC_KEYSTONE)
Vitaly Andrianov7bcf4d62014-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 Madriddfa028a2009-02-18 14:30:44 -0800273#ifndef CONFIG_NS16550_MIN_FUNCTIONS
Simon Glassdd5497c2011-10-15 19:14:09 +0000274void NS16550_reinit(NS16550_t com_port, int baud_divisor)
wdenke85390d2002-04-01 14:29:03 +0000275{
Prafulla Wadaskar66216372010-10-27 21:58:31 +0530276 serial_out(CONFIG_SYS_NS16550_IER, &com_port->ier);
Simon Glassc31ebfe2014-09-04 16:27:33 -0600277 NS16550_setbrg(com_port, 0);
Graeme Russ14f06e62010-04-24 00:05:46 +1000278 serial_out(UART_MCRVAL, &com_port->mcr);
Marek Vasutf523c9c2016-12-01 02:06:29 +0100279 serial_out(ns16550_getfcr(com_port), &com_port->fcr);
Simon Glassc31ebfe2014-09-04 16:27:33 -0600280 NS16550_setbrg(com_port, baud_divisor);
wdenke85390d2002-04-01 14:29:03 +0000281}
Ron Madriddfa028a2009-02-18 14:30:44 -0800282#endif /* CONFIG_NS16550_MIN_FUNCTIONS */
wdenke85390d2002-04-01 14:29:03 +0000283
Simon Glassdd5497c2011-10-15 19:14:09 +0000284void NS16550_putc(NS16550_t com_port, char c)
wdenke85390d2002-04-01 14:29:03 +0000285{
Simon Glassdd5497c2011-10-15 19:14:09 +0000286 while ((serial_in(&com_port->lsr) & UART_LSR_THRE) == 0)
287 ;
Graeme Russ14f06e62010-04-24 00:05:46 +1000288 serial_out(c, &com_port->thr);
Stefan Roese57b99882010-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 Madriddfa028a2009-02-18 14:30:44 -0800300#ifndef CONFIG_NS16550_MIN_FUNCTIONS
Simon Glassdd5497c2011-10-15 19:14:09 +0000301char NS16550_getc(NS16550_t com_port)
wdenke85390d2002-04-01 14:29:03 +0000302{
Graeme Russ14f06e62010-04-24 00:05:46 +1000303 while ((serial_in(&com_port->lsr) & UART_LSR_DR) == 0) {
Marek Vasut9e1fca92012-09-15 10:25:19 +0200304#if !defined(CONFIG_SPL_BUILD) && defined(CONFIG_USB_TTY)
wdenk29e7f5a2004-03-12 00:14:09 +0000305 extern void usbtty_poll(void);
306 usbtty_poll();
307#endif
Ladislav Michlcc294422010-02-01 23:34:25 +0100308 WATCHDOG_RESET();
wdenk29e7f5a2004-03-12 00:14:09 +0000309 }
Graeme Russ14f06e62010-04-24 00:05:46 +1000310 return serial_in(&com_port->rbr);
wdenke85390d2002-04-01 14:29:03 +0000311}
312
Simon Glassdd5497c2011-10-15 19:14:09 +0000313int NS16550_tstc(NS16550_t com_port)
wdenke85390d2002-04-01 14:29:03 +0000314{
Simon Glassdd5497c2011-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 Madriddfa028a2009-02-18 14:30:44 -0800318#endif /* CONFIG_NS16550_MIN_FUNCTIONS */
Simon Glass79a9da32014-09-04 16:27:34 -0600319
Simon Glass27afb522015-01-26 18:27:09 -0700320#ifdef CONFIG_DEBUG_UART_NS16550
321
322#include <debug_uart.h>
323
Simon Glass60517d72015-10-18 19:51:23 -0600324static inline void _debug_uart_init(void)
Simon Glass27afb522015-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 Vasut3b164a52016-05-25 02:13:16 +0200335 baud_divisor = ns16550_calc_divisor(com_port, CONFIG_DEBUG_UART_CLOCK,
336 CONFIG_BAUDRATE);
Simon Glass92465162015-06-23 15:39:06 -0600337 serial_dout(&com_port->ier, CONFIG_SYS_NS16550_IER);
Lokesh Vutla771d69c2017-04-22 15:57:25 +0530338 serial_dout(&com_port->mcr, UART_MCRVAL);
339 serial_dout(&com_port->fcr, UART_FCR_DEFVAL);
340
341 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);
345}
346
Simon Goldschmidte03ad212019-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
Lokesh Vutla771d69c2017-04-22 15:57:25 +0530359static inline void _debug_uart_putc(int ch)
360{
361 struct NS16550 *com_port = (struct NS16550 *)CONFIG_DEBUG_UART_BASE;
362
Simon Goldschmidte03ad212019-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 }
Lokesh Vutla771d69c2017-04-22 15:57:25 +0530369 serial_dout(&com_port->thr, ch);
370}
371
372DEBUG_UART_FUNCS
373
374#endif
375
Simon Glassd945a492019-09-25 08:11:14 -0600376#if CONFIG_IS_ENABLED(DM_SERIAL)
Simon Glass79a9da32014-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 Sixaed664a2018-01-15 11:09:49 +0100402 return (serial_in(&com_port->lsr) & UART_LSR_DR) ? 1 : 0;
Simon Glass79a9da32014-09-04 16:27:34 -0600403 else
Mario Sixaed664a2018-01-15 11:09:49 +0100404 return (serial_in(&com_port->lsr) & UART_LSR_THRE) ? 0 : 1;
Simon Glass79a9da32014-09-04 16:27:34 -0600405}
406
407static int ns16550_serial_getc(struct udevice *dev)
408{
Stefan Roese48b771c2017-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 Glass79a9da32014-09-04 16:27:34 -0600412 return -EAGAIN;
413
Stefan Roese48b771c2017-08-16 17:37:15 +0200414 return serial_in(&com_port->rbr);
Simon Glass79a9da32014-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
Simon Goldschmidtba2d0aa2018-11-02 21:28:08 +0100427 return 0;
428}
429
430static 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);
Simon Glass79a9da32014-09-04 16:27:34 -0600460 return 0;
461}
462
Andy Shevchenkod778fc42018-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
Bin Menge92e09d2020-04-03 18:35:32 -0700482static int ns16550_serial_assign_base(struct ns16550_platdata *plat, ulong base)
Wolfgang Wallnerf89c7ec2020-03-02 14:41:14 +0100483{
Bin Menge92e09d2020-04-03 18:35:32 -0700484 if (base == FDT_ADDR_T_NONE)
Wolfgang Wallnerf89c7ec2020-03-02 14:41:14 +0100485 return -EINVAL;
486
487#ifdef CONFIG_SYS_NS16550_PORT_MAPPED
Bin Menge92e09d2020-04-03 18:35:32 -0700488 plat->base = base;
Wolfgang Wallnerf89c7ec2020-03-02 14:41:14 +0100489#else
Bin Menge92e09d2020-04-03 18:35:32 -0700490 plat->base = (unsigned long)map_physmem(base, 0, MAP_NOCACHE);
Wolfgang Wallnerf89c7ec2020-03-02 14:41:14 +0100491#endif
492
493 return 0;
494}
Wolfgang Wallnerf89c7ec2020-03-02 14:41:14 +0100495
Simon Glass79a9da32014-09-04 16:27:34 -0600496int ns16550_serial_probe(struct udevice *dev)
497{
Bin Menge92e09d2020-04-03 18:35:32 -0700498 struct ns16550_platdata *plat = dev->platdata;
Simon Glass79a9da32014-09-04 16:27:34 -0600499 struct NS16550 *const com_port = dev_get_priv(dev);
Ley Foon Tana1763392018-06-14 18:45:22 +0800500 struct reset_ctl_bulk reset_bulk;
Bin Menge92e09d2020-04-03 18:35:32 -0700501 fdt_addr_t addr;
Ley Foon Tana1763392018-06-14 18:45:22 +0800502 int ret;
503
Bin Menge92e09d2020-04-03 18:35:32 -0700504 /*
505 * If we are on PCI bus, either directly attached to a PCI root port,
506 * or via a PCI bridge, assign platdata->base before probing hardware.
507 */
508 if (device_is_on_pci_bus(dev)) {
509 addr = devfdt_get_addr_pci(dev);
510 ret = ns16550_serial_assign_base(plat, addr);
511 if (ret)
512 return ret;
513 }
Wolfgang Wallnerf89c7ec2020-03-02 14:41:14 +0100514
Ley Foon Tana1763392018-06-14 18:45:22 +0800515 ret = reset_get_bulk(dev, &reset_bulk);
516 if (!ret)
517 reset_deassert_bulk(&reset_bulk);
Simon Glass79a9da32014-09-04 16:27:34 -0600518
Simon Glass3bf04f32014-10-22 21:37:05 -0600519 com_port->plat = dev_get_platdata(dev);
Simon Glass79a9da32014-09-04 16:27:34 -0600520 NS16550_init(com_port, -1);
521
522 return 0;
523}
524
Marek Vasut1a59eec2016-12-01 02:06:30 +0100525#if CONFIG_IS_ENABLED(OF_CONTROL)
526enum {
527 PORT_NS16550 = 0,
Marek Vasut92a744f2016-12-01 02:06:31 +0100528 PORT_JZ4780,
Marek Vasut1a59eec2016-12-01 02:06:30 +0100529};
530#endif
531
Simon Glass18798be2016-07-04 11:58:23 -0600532#if CONFIG_IS_ENABLED(OF_CONTROL) && !CONFIG_IS_ENABLED(OF_PLATDATA)
Simon Glass79a9da32014-09-04 16:27:34 -0600533int ns16550_serial_ofdata_to_platdata(struct udevice *dev)
534{
Simon Glass79a9da32014-09-04 16:27:34 -0600535 struct ns16550_platdata *plat = dev->platdata;
Marek Vasut92a744f2016-12-01 02:06:31 +0100536 const u32 port_type = dev_get_driver_data(dev);
Bin Menge92e09d2020-04-03 18:35:32 -0700537 fdt_addr_t addr;
Masahiro Yamada09abe2b2016-09-26 20:45:27 +0900538 struct clk clk;
539 int err;
Simon Glass79a9da32014-09-04 16:27:34 -0600540
Bin Menge92e09d2020-04-03 18:35:32 -0700541 addr = dev_read_addr(dev);
542 err = ns16550_serial_assign_base(plat, addr);
543 if (err && !device_is_on_pci_bus(dev))
544 return err;
545
Philipp Tomsiche74958e2017-06-07 18:46:02 +0200546 plat->reg_offset = dev_read_u32_default(dev, "reg-offset", 0);
547 plat->reg_shift = dev_read_u32_default(dev, "reg-shift", 0);
Andy Shevchenko72fccfe2018-11-20 23:52:35 +0200548 plat->reg_width = dev_read_u32_default(dev, "reg-io-width", 1);
Paul Burton8aadc562016-09-08 07:47:29 +0100549
550 err = clk_get_by_index(dev, 0, &clk);
551 if (!err) {
552 err = clk_get_rate(&clk);
553 if (!IS_ERR_VALUE(err))
554 plat->clock = err;
Alexandre Courboteaa24e7f2016-09-30 17:37:00 +0900555 } else if (err != -ENOENT && err != -ENODEV && err != -ENOSYS) {
Paul Burton8aadc562016-09-08 07:47:29 +0100556 debug("ns16550 failed to get clock\n");
557 return err;
558 }
559
560 if (!plat->clock)
Philipp Tomsiche74958e2017-06-07 18:46:02 +0200561 plat->clock = dev_read_u32_default(dev, "clock-frequency",
562 CONFIG_SYS_NS16550_CLK);
Thomas Chou16b2e7e2015-11-19 21:48:05 +0800563 if (!plat->clock) {
564 debug("ns16550 clock not defined\n");
565 return -EINVAL;
566 }
Simon Glass79a9da32014-09-04 16:27:34 -0600567
Heiko Schocher06f108e2017-01-18 08:05:49 +0100568 plat->fcr = UART_FCR_DEFVAL;
Marek Vasut92a744f2016-12-01 02:06:31 +0100569 if (port_type == PORT_JZ4780)
570 plat->fcr |= UART_FCR_UME;
Marek Vasutf523c9c2016-12-01 02:06:29 +0100571
Simon Glass79a9da32014-09-04 16:27:34 -0600572 return 0;
573}
Simon Glass3bf04f32014-10-22 21:37:05 -0600574#endif
Simon Glass79a9da32014-09-04 16:27:34 -0600575
576const struct dm_serial_ops ns16550_serial_ops = {
577 .putc = ns16550_serial_putc,
578 .pending = ns16550_serial_pending,
579 .getc = ns16550_serial_getc,
580 .setbrg = ns16550_serial_setbrg,
Andy Shevchenkod778fc42018-11-20 23:52:36 +0200581 .setconfig = ns16550_serial_setconfig,
582 .getinfo = ns16550_serial_getinfo,
Simon Glass79a9da32014-09-04 16:27:34 -0600583};
Thomas Chou16b2e7e2015-11-19 21:48:05 +0800584
Alexandru Gagniucb1ab1892017-03-27 12:54:19 -0700585#if CONFIG_IS_ENABLED(OF_CONTROL) && !CONFIG_IS_ENABLED(OF_PLATDATA)
Thomas Choudad53672015-12-14 20:45:09 +0800586/*
587 * Please consider existing compatible strings before adding a new
588 * one to keep this table compact. Or you may add a generic "ns16550"
589 * compatible string to your dts.
590 */
Thomas Chou16b2e7e2015-11-19 21:48:05 +0800591static const struct udevice_id ns16550_serial_ids[] = {
Marek Vasut1a59eec2016-12-01 02:06:30 +0100592 { .compatible = "ns16550", .data = PORT_NS16550 },
593 { .compatible = "ns16550a", .data = PORT_NS16550 },
Marek Vasut92a744f2016-12-01 02:06:31 +0100594 { .compatible = "ingenic,jz4780-uart", .data = PORT_JZ4780 },
Marek Vasut1a59eec2016-12-01 02:06:30 +0100595 { .compatible = "nvidia,tegra20-uart", .data = PORT_NS16550 },
596 { .compatible = "snps,dw-apb-uart", .data = PORT_NS16550 },
Thomas Chou16b2e7e2015-11-19 21:48:05 +0800597 {}
598};
Alexandru Gagniucb1ab1892017-03-27 12:54:19 -0700599#endif /* OF_CONTROL && !OF_PLATDATA */
Thomas Chou16b2e7e2015-11-19 21:48:05 +0800600
Simon Glass9ebf3482015-12-13 21:36:59 -0700601#if CONFIG_IS_ENABLED(SERIAL_PRESENT)
Alexandru Gagniucb1ab1892017-03-27 12:54:19 -0700602
603/* TODO(sjg@chromium.org): Integrate this into a macro like CONFIG_IS_ENABLED */
604#if !defined(CONFIG_TPL_BUILD) || defined(CONFIG_TPL_DM_SERIAL)
Thomas Chou16b2e7e2015-11-19 21:48:05 +0800605U_BOOT_DRIVER(ns16550_serial) = {
606 .name = "ns16550_serial",
607 .id = UCLASS_SERIAL,
Alexandru Gagniucb1ab1892017-03-27 12:54:19 -0700608#if CONFIG_IS_ENABLED(OF_CONTROL) && !CONFIG_IS_ENABLED(OF_PLATDATA)
Thomas Chou16b2e7e2015-11-19 21:48:05 +0800609 .of_match = ns16550_serial_ids,
610 .ofdata_to_platdata = ns16550_serial_ofdata_to_platdata,
611 .platdata_auto_alloc_size = sizeof(struct ns16550_platdata),
612#endif
613 .priv_auto_alloc_size = sizeof(struct NS16550),
614 .probe = ns16550_serial_probe,
615 .ops = &ns16550_serial_ops,
Bin Mengbdb33d82018-10-24 06:36:36 -0700616#if !CONFIG_IS_ENABLED(OF_CONTROL)
Simon Glass6ef533e2015-12-04 08:58:38 -0700617 .flags = DM_FLAG_PRE_RELOC,
Bin Mengbdb33d82018-10-24 06:36:36 -0700618#endif
Thomas Chou16b2e7e2015-11-19 21:48:05 +0800619};
Simon Glass9ebf3482015-12-13 21:36:59 -0700620#endif
Alexandru Gagniucb1ab1892017-03-27 12:54:19 -0700621#endif /* SERIAL_PRESENT */
622
Simon Glass79a9da32014-09-04 16:27:34 -0600623#endif /* CONFIG_DM_SERIAL */