3 * originally from linux source (arch/powerpc/boot/ns16550.c)
4 * modified to use CONFIG_SYS_ISA_MEM and new defines
7 #include <clock_legacy.h>
16 #include <linux/types.h>
19 DECLARE_GLOBAL_DATA_PTR;
21 #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 */
25 #if !CONFIG_IS_ENABLED(DM_SERIAL)
26 #ifdef CONFIG_SYS_NS16550_PORT_MAPPED
27 #define serial_out(x, y) outb(x, (ulong)y)
28 #define serial_in(y) inb((ulong)y)
29 #elif defined(CONFIG_SYS_NS16550_MEM32) && (CONFIG_SYS_NS16550_REG_SIZE > 0)
30 #define serial_out(x, y) out_be32(y, x)
31 #define serial_in(y) in_be32(y)
32 #elif defined(CONFIG_SYS_NS16550_MEM32) && (CONFIG_SYS_NS16550_REG_SIZE < 0)
33 #define serial_out(x, y) out_le32(y, x)
34 #define serial_in(y) in_le32(y)
36 #define serial_out(x, y) writeb(x, y)
37 #define serial_in(y) readb(y)
39 #endif /* !CONFIG_DM_SERIAL */
41 #if defined(CONFIG_SOC_KEYSTONE)
42 #define UART_REG_VAL_PWREMU_MGMT_UART_DISABLE 0
43 #define UART_REG_VAL_PWREMU_MGMT_UART_ENABLE ((1 << 14) | (1 << 13) | (1 << 0))
45 #ifdef CONFIG_SERIAL_HW_FLOW_CONTROL
46 #define UART_MCRVAL (UART_MCR_RTS | UART_MCR_AFE)
48 #define UART_MCRVAL (UART_MCR_RTS)
52 #ifndef CONFIG_SYS_NS16550_IER
53 #define CONFIG_SYS_NS16550_IER 0x00
54 #endif /* CONFIG_SYS_NS16550_IER */
56 static inline void serial_out_shift(void *addr, int shift, int value)
58 #ifdef CONFIG_SYS_NS16550_PORT_MAPPED
59 outb(value, (ulong)addr);
60 #elif defined(CONFIG_SYS_NS16550_MEM32) && defined(CONFIG_SYS_LITTLE_ENDIAN)
61 out_le32(addr, value);
62 #elif defined(CONFIG_SYS_NS16550_MEM32) && defined(CONFIG_SYS_BIG_ENDIAN)
63 out_be32(addr, value);
64 #elif defined(CONFIG_SYS_NS16550_MEM32)
66 #elif defined(CONFIG_SYS_BIG_ENDIAN)
67 writeb(value, addr + (1 << shift) - 1);
73 static inline int serial_in_shift(void *addr, int shift)
75 #ifdef CONFIG_SYS_NS16550_PORT_MAPPED
76 return inb((ulong)addr);
77 #elif defined(CONFIG_SYS_NS16550_MEM32) && defined(CONFIG_SYS_LITTLE_ENDIAN)
79 #elif defined(CONFIG_SYS_NS16550_MEM32) && defined(CONFIG_SYS_BIG_ENDIAN)
81 #elif defined(CONFIG_SYS_NS16550_MEM32)
83 #elif defined(CONFIG_SYS_BIG_ENDIAN)
84 return readb(addr + (1 << shift) - 1);
90 #if CONFIG_IS_ENABLED(DM_SERIAL)
92 #ifndef CONFIG_SYS_NS16550_CLK
93 #define CONFIG_SYS_NS16550_CLK 0
96 static void ns16550_writeb(NS16550_t port, int offset, int value)
98 struct ns16550_platdata *plat = port->plat;
101 offset *= 1 << plat->reg_shift;
102 addr = (unsigned char *)plat->base + offset;
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.
108 serial_out_shift(addr + plat->reg_offset, plat->reg_shift, value);
111 static int ns16550_readb(NS16550_t port, int offset)
113 struct ns16550_platdata *plat = port->plat;
116 offset *= 1 << plat->reg_shift;
117 addr = (unsigned char *)plat->base + offset;
119 return serial_in_shift(addr + plat->reg_offset, plat->reg_shift);
122 static u32 ns16550_getfcr(NS16550_t port)
124 struct ns16550_platdata *plat = port->plat;
129 /* We can clean these up once everything is moved to driver model */
130 #define serial_out(value, addr) \
131 ns16550_writeb(com_port, \
132 (unsigned char *)addr - (unsigned char *)com_port, value)
133 #define serial_in(addr) \
134 ns16550_readb(com_port, \
135 (unsigned char *)addr - (unsigned char *)com_port)
137 static u32 ns16550_getfcr(NS16550_t port)
139 return UART_FCR_DEFVAL;
143 int ns16550_calc_divisor(NS16550_t port, int clock, int baudrate)
145 const unsigned int mode_x_div = 16;
147 return DIV_ROUND_CLOSEST(clock, mode_x_div * baudrate);
150 static void NS16550_setbrg(NS16550_t com_port, int baud_divisor)
152 /* to keep serial format, read lcr before writing BKSE */
153 int lcr_val = serial_in(&com_port->lcr) & ~UART_LCR_BKSE;
155 serial_out(UART_LCR_BKSE | lcr_val, &com_port->lcr);
156 serial_out(baud_divisor & 0xff, &com_port->dll);
157 serial_out((baud_divisor >> 8) & 0xff, &com_port->dlm);
158 serial_out(lcr_val, &com_port->lcr);
161 void NS16550_init(NS16550_t com_port, int baud_divisor)
163 #if (defined(CONFIG_SPL_BUILD) && \
164 (defined(CONFIG_OMAP34XX) || defined(CONFIG_OMAP44XX)))
166 * 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.
170 if ((serial_in(&com_port->lsr) & (UART_LSR_TEMT | UART_LSR_THRE))
172 if (baud_divisor != -1)
173 NS16550_setbrg(com_port, baud_divisor);
175 // Re-use old baud rate divisor to flush transmit reg.
176 const int dll = serial_in(&com_port->dll);
177 const int dlm = serial_in(&com_port->dlm);
178 const int divisor = dll | (dlm << 8);
179 NS16550_setbrg(com_port, divisor);
181 serial_out(0, &com_port->mdr1);
185 while (!(serial_in(&com_port->lsr) & UART_LSR_TEMT))
188 serial_out(CONFIG_SYS_NS16550_IER, &com_port->ier);
189 #if defined(CONFIG_ARCH_OMAP2PLUS) || defined(CONFIG_OMAP_SERIAL)
190 serial_out(0x7, &com_port->mdr1); /* mode select reset TL16C750*/
193 serial_out(UART_MCRVAL, &com_port->mcr);
194 serial_out(ns16550_getfcr(com_port), &com_port->fcr);
195 /* initialize serial config to 8N1 before writing baudrate */
196 serial_out(UART_LCRVAL, &com_port->lcr);
197 if (baud_divisor != -1)
198 NS16550_setbrg(com_port, baud_divisor);
199 #if defined(CONFIG_ARCH_OMAP2PLUS) || defined(CONFIG_SOC_DA8XX) || \
200 defined(CONFIG_OMAP_SERIAL)
201 /* /16 is proper to hit 115200 with 48MHz */
202 serial_out(0, &com_port->mdr1);
204 #if defined(CONFIG_SOC_KEYSTONE)
205 serial_out(UART_REG_VAL_PWREMU_MGMT_UART_ENABLE, &com_port->regC);
209 #ifndef CONFIG_NS16550_MIN_FUNCTIONS
210 void NS16550_reinit(NS16550_t com_port, int baud_divisor)
212 serial_out(CONFIG_SYS_NS16550_IER, &com_port->ier);
213 NS16550_setbrg(com_port, 0);
214 serial_out(UART_MCRVAL, &com_port->mcr);
215 serial_out(ns16550_getfcr(com_port), &com_port->fcr);
216 NS16550_setbrg(com_port, baud_divisor);
218 #endif /* CONFIG_NS16550_MIN_FUNCTIONS */
220 void NS16550_putc(NS16550_t com_port, char c)
222 while ((serial_in(&com_port->lsr) & UART_LSR_THRE) == 0)
224 serial_out(c, &com_port->thr);
227 * Call watchdog_reset() upon newline. This is done here in putc
228 * since the environment code uses a single puts() to print the complete
229 * environment upon "printenv". So we can't put this watchdog call
236 #ifndef CONFIG_NS16550_MIN_FUNCTIONS
237 char NS16550_getc(NS16550_t com_port)
239 while ((serial_in(&com_port->lsr) & UART_LSR_DR) == 0) {
240 #if !defined(CONFIG_SPL_BUILD) && defined(CONFIG_USB_TTY)
241 extern void usbtty_poll(void);
246 return serial_in(&com_port->rbr);
249 int NS16550_tstc(NS16550_t com_port)
251 return (serial_in(&com_port->lsr) & UART_LSR_DR) != 0;
254 #endif /* CONFIG_NS16550_MIN_FUNCTIONS */
256 #ifdef CONFIG_DEBUG_UART_NS16550
258 #include <debug_uart.h>
260 static inline void _debug_uart_init(void)
262 struct NS16550 *com_port = (struct NS16550 *)CONFIG_DEBUG_UART_BASE;
266 * We copy the code from above because it is already horribly messy.
267 * Trying to refactor to nicely remove the duplication doesn't seem
268 * feasible. The better fix is to move all users of this driver to
271 baud_divisor = ns16550_calc_divisor(com_port, CONFIG_DEBUG_UART_CLOCK,
273 serial_dout(&com_port->ier, CONFIG_SYS_NS16550_IER);
274 serial_dout(&com_port->mcr, UART_MCRVAL);
275 serial_dout(&com_port->fcr, UART_FCR_DEFVAL);
277 serial_dout(&com_port->lcr, UART_LCR_BKSE | UART_LCRVAL);
278 serial_dout(&com_port->dll, baud_divisor & 0xff);
279 serial_dout(&com_port->dlm, (baud_divisor >> 8) & 0xff);
280 serial_dout(&com_port->lcr, UART_LCRVAL);
283 static inline int NS16550_read_baud_divisor(struct NS16550 *com_port)
287 serial_dout(&com_port->lcr, UART_LCR_BKSE | UART_LCRVAL);
288 ret = serial_din(&com_port->dll) & 0xff;
289 ret |= (serial_din(&com_port->dlm) & 0xff) << 8;
290 serial_dout(&com_port->lcr, UART_LCRVAL);
295 static inline void _debug_uart_putc(int ch)
297 struct NS16550 *com_port = (struct NS16550 *)CONFIG_DEBUG_UART_BASE;
299 while (!(serial_din(&com_port->lsr) & UART_LSR_THRE)) {
300 #ifdef CONFIG_DEBUG_UART_NS16550_CHECK_ENABLED
301 if (!NS16550_read_baud_divisor(com_port))
305 serial_dout(&com_port->thr, ch);
312 #if CONFIG_IS_ENABLED(DM_SERIAL)
313 static int ns16550_serial_putc(struct udevice *dev, const char ch)
315 struct NS16550 *const com_port = dev_get_priv(dev);
317 if (!(serial_in(&com_port->lsr) & UART_LSR_THRE))
319 serial_out(ch, &com_port->thr);
322 * Call watchdog_reset() upon newline. This is done here in putc
323 * since the environment code uses a single puts() to print the complete
324 * environment upon "printenv". So we can't put this watchdog call
333 static int ns16550_serial_pending(struct udevice *dev, bool input)
335 struct NS16550 *const com_port = dev_get_priv(dev);
338 return (serial_in(&com_port->lsr) & UART_LSR_DR) ? 1 : 0;
340 return (serial_in(&com_port->lsr) & UART_LSR_THRE) ? 0 : 1;
343 static int ns16550_serial_getc(struct udevice *dev)
345 struct NS16550 *const com_port = dev_get_priv(dev);
347 if (!(serial_in(&com_port->lsr) & UART_LSR_DR))
350 return serial_in(&com_port->rbr);
353 static int ns16550_serial_setbrg(struct udevice *dev, int baudrate)
355 struct NS16550 *const com_port = dev_get_priv(dev);
356 struct ns16550_platdata *plat = com_port->plat;
359 clock_divisor = ns16550_calc_divisor(com_port, plat->clock, baudrate);
361 NS16550_setbrg(com_port, clock_divisor);
366 static int ns16550_serial_setconfig(struct udevice *dev, uint serial_config)
368 struct NS16550 *const com_port = dev_get_priv(dev);
369 int lcr_val = UART_LCR_WLS_8;
370 uint parity = SERIAL_GET_PARITY(serial_config);
371 uint bits = SERIAL_GET_BITS(serial_config);
372 uint stop = SERIAL_GET_STOP(serial_config);
375 * only parity config is implemented, check if other serial settings
376 * are the default one.
378 if (bits != SERIAL_8_BITS || stop != SERIAL_ONE_STOP)
379 return -ENOTSUPP; /* not supported in driver*/
382 case SERIAL_PAR_NONE:
386 lcr_val |= UART_LCR_PEN;
388 case SERIAL_PAR_EVEN:
389 lcr_val |= UART_LCR_PEN | UART_LCR_EPS;
392 return -ENOTSUPP; /* not supported in driver*/
395 serial_out(lcr_val, &com_port->lcr);
399 static int ns16550_serial_getinfo(struct udevice *dev,
400 struct serial_device_info *info)
402 struct NS16550 *const com_port = dev_get_priv(dev);
403 struct ns16550_platdata *plat = com_port->plat;
405 info->type = SERIAL_CHIP_16550_COMPATIBLE;
406 #ifdef CONFIG_SYS_NS16550_PORT_MAPPED
407 info->addr_space = SERIAL_ADDRESS_SPACE_IO;
409 info->addr_space = SERIAL_ADDRESS_SPACE_MEMORY;
411 info->addr = plat->base;
412 info->reg_width = plat->reg_width;
413 info->reg_shift = plat->reg_shift;
414 info->reg_offset = plat->reg_offset;
418 int ns16550_serial_probe(struct udevice *dev)
420 struct NS16550 *const com_port = dev_get_priv(dev);
421 struct reset_ctl_bulk reset_bulk;
424 ret = reset_get_bulk(dev, &reset_bulk);
426 reset_deassert_bulk(&reset_bulk);
428 com_port->plat = dev_get_platdata(dev);
429 NS16550_init(com_port, -1);
434 #if CONFIG_IS_ENABLED(OF_CONTROL)
441 #if CONFIG_IS_ENABLED(OF_CONTROL) && !CONFIG_IS_ENABLED(OF_PLATDATA)
442 int ns16550_serial_ofdata_to_platdata(struct udevice *dev)
444 struct ns16550_platdata *plat = dev->platdata;
445 const u32 port_type = dev_get_driver_data(dev);
450 /* try Processor Local Bus device first */
451 addr = dev_read_addr_pci(dev);
452 if (addr == FDT_ADDR_T_NONE)
455 #ifdef CONFIG_SYS_NS16550_PORT_MAPPED
458 plat->base = (unsigned long)map_physmem(addr, 0, MAP_NOCACHE);
461 plat->reg_offset = dev_read_u32_default(dev, "reg-offset", 0);
462 plat->reg_shift = dev_read_u32_default(dev, "reg-shift", 0);
463 plat->reg_width = dev_read_u32_default(dev, "reg-io-width", 1);
465 err = clk_get_by_index(dev, 0, &clk);
467 err = clk_get_rate(&clk);
468 if (!IS_ERR_VALUE(err))
470 } else if (err != -ENOENT && err != -ENODEV && err != -ENOSYS) {
471 debug("ns16550 failed to get clock\n");
476 plat->clock = dev_read_u32_default(dev, "clock-frequency",
477 CONFIG_SYS_NS16550_CLK);
479 debug("ns16550 clock not defined\n");
483 plat->fcr = UART_FCR_DEFVAL;
484 if (port_type == PORT_JZ4780)
485 plat->fcr |= UART_FCR_UME;
491 const struct dm_serial_ops ns16550_serial_ops = {
492 .putc = ns16550_serial_putc,
493 .pending = ns16550_serial_pending,
494 .getc = ns16550_serial_getc,
495 .setbrg = ns16550_serial_setbrg,
496 .setconfig = ns16550_serial_setconfig,
497 .getinfo = ns16550_serial_getinfo,
500 #if CONFIG_IS_ENABLED(OF_CONTROL) && !CONFIG_IS_ENABLED(OF_PLATDATA)
502 * Please consider existing compatible strings before adding a new
503 * one to keep this table compact. Or you may add a generic "ns16550"
504 * compatible string to your dts.
506 static const struct udevice_id ns16550_serial_ids[] = {
507 { .compatible = "ns16550", .data = PORT_NS16550 },
508 { .compatible = "ns16550a", .data = PORT_NS16550 },
509 { .compatible = "ingenic,jz4780-uart", .data = PORT_JZ4780 },
510 { .compatible = "nvidia,tegra20-uart", .data = PORT_NS16550 },
511 { .compatible = "snps,dw-apb-uart", .data = PORT_NS16550 },
514 #endif /* OF_CONTROL && !OF_PLATDATA */
516 #if CONFIG_IS_ENABLED(SERIAL_PRESENT)
518 /* TODO(sjg@chromium.org): Integrate this into a macro like CONFIG_IS_ENABLED */
519 #if !defined(CONFIG_TPL_BUILD) || defined(CONFIG_TPL_DM_SERIAL)
520 U_BOOT_DRIVER(ns16550_serial) = {
521 .name = "ns16550_serial",
523 #if CONFIG_IS_ENABLED(OF_CONTROL) && !CONFIG_IS_ENABLED(OF_PLATDATA)
524 .of_match = ns16550_serial_ids,
525 .ofdata_to_platdata = ns16550_serial_ofdata_to_platdata,
526 .platdata_auto_alloc_size = sizeof(struct ns16550_platdata),
528 .priv_auto_alloc_size = sizeof(struct NS16550),
529 .probe = ns16550_serial_probe,
530 .ops = &ns16550_serial_ops,
531 #if !CONFIG_IS_ENABLED(OF_CONTROL)
532 .flags = DM_FLAG_PRE_RELOC,
536 #endif /* SERIAL_PRESENT */
538 #endif /* CONFIG_DM_SERIAL */