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>
17 #include <asm/global_data.h>
18 #include <linux/err.h>
19 #include <linux/types.h>
22 DECLARE_GLOBAL_DATA_PTR;
24 #define UART_LCRVAL UART_LCR_8N1 /* 8 data, 1 stop, no parity */
25 #define UART_MCRVAL (UART_MCR_DTR | \
26 UART_MCR_RTS) /* RTS/DTR */
28 #if !CONFIG_IS_ENABLED(DM_SERIAL)
29 #ifdef CONFIG_SYS_NS16550_PORT_MAPPED
30 #define serial_out(x, y) outb(x, (ulong)y)
31 #define serial_in(y) inb((ulong)y)
32 #elif defined(CONFIG_SYS_NS16550_MEM32) && (CONFIG_SYS_NS16550_REG_SIZE > 0)
33 #define serial_out(x, y) out_be32(y, x)
34 #define serial_in(y) in_be32(y)
35 #elif defined(CONFIG_SYS_NS16550_MEM32) && (CONFIG_SYS_NS16550_REG_SIZE < 0)
36 #define serial_out(x, y) out_le32(y, x)
37 #define serial_in(y) in_le32(y)
39 #define serial_out(x, y) writeb(x, y)
40 #define serial_in(y) readb(y)
42 #endif /* !CONFIG_DM_SERIAL */
44 #if defined(CONFIG_ARCH_KEYSTONE)
45 #define UART_REG_VAL_PWREMU_MGMT_UART_DISABLE 0
46 #define UART_REG_VAL_PWREMU_MGMT_UART_ENABLE ((1 << 14) | (1 << 13) | (1 << 0))
48 #ifdef CONFIG_SERIAL_HW_FLOW_CONTROL
49 #define UART_MCRVAL (UART_MCR_RTS | UART_MCR_AFE)
51 #define UART_MCRVAL (UART_MCR_RTS)
55 #ifndef CONFIG_SYS_NS16550_IER
56 #define CONFIG_SYS_NS16550_IER 0x00
57 #endif /* CONFIG_SYS_NS16550_IER */
59 static inline void serial_out_shift(void *addr, int shift, int value)
61 #ifdef CONFIG_SYS_NS16550_PORT_MAPPED
62 outb(value, (ulong)addr);
63 #elif defined(CONFIG_SYS_NS16550_MEM32) && defined(CONFIG_SYS_LITTLE_ENDIAN)
64 out_le32(addr, value);
65 #elif defined(CONFIG_SYS_NS16550_MEM32) && defined(CONFIG_SYS_BIG_ENDIAN)
66 out_be32(addr, value);
67 #elif defined(CONFIG_SYS_NS16550_MEM32)
69 #elif defined(CONFIG_SYS_BIG_ENDIAN)
70 writeb(value, addr + (1 << shift) - 1);
76 static inline int serial_in_shift(void *addr, int shift)
78 #ifdef CONFIG_SYS_NS16550_PORT_MAPPED
79 return inb((ulong)addr);
80 #elif defined(CONFIG_SYS_NS16550_MEM32) && defined(CONFIG_SYS_LITTLE_ENDIAN)
82 #elif defined(CONFIG_SYS_NS16550_MEM32) && defined(CONFIG_SYS_BIG_ENDIAN)
84 #elif defined(CONFIG_SYS_NS16550_MEM32)
86 #elif defined(CONFIG_SYS_BIG_ENDIAN)
87 return readb(addr + (1 << shift) - 1);
93 #if CONFIG_IS_ENABLED(DM_SERIAL)
95 #ifndef CONFIG_SYS_NS16550_CLK
96 #define CONFIG_SYS_NS16550_CLK 0
100 * Use this #ifdef for now since many platforms don't define in(), out(),
101 * out_le32(), etc. but we don't have #defines to indicate this.
103 * TODO(sjg@chromium.org): Add CONFIG options to indicate what I/O is available
106 #ifdef CONFIG_NS16550_DYNAMIC
107 static void serial_out_dynamic(struct ns16550_plat *plat, u8 *addr,
110 if (plat->flags & NS16550_FLAG_IO) {
112 } else if (plat->reg_width == 4) {
113 if (plat->flags & NS16550_FLAG_ENDIAN) {
114 if (plat->flags & NS16550_FLAG_BE)
115 out_be32(addr, value);
117 out_le32(addr, value);
121 } else if (plat->flags & NS16550_FLAG_BE) {
122 writeb(value, addr + (1 << plat->reg_shift) - 1);
128 static int serial_in_dynamic(struct ns16550_plat *plat, u8 *addr)
130 if (plat->flags & NS16550_FLAG_IO) {
132 } else if (plat->reg_width == 4) {
133 if (plat->flags & NS16550_FLAG_ENDIAN) {
134 if (plat->flags & NS16550_FLAG_BE)
135 return in_be32(addr);
137 return in_le32(addr);
141 } else if (plat->flags & NS16550_FLAG_BE) {
142 return readb(addr + (1 << plat->reg_shift) - 1);
148 static inline void serial_out_dynamic(struct ns16550_plat *plat, u8 *addr,
153 static inline int serial_in_dynamic(struct ns16550_plat *plat, u8 *addr)
158 #endif /* CONFIG_NS16550_DYNAMIC */
160 static void ns16550_writeb(struct ns16550 *port, int offset, int value)
162 struct ns16550_plat *plat = port->plat;
165 offset *= 1 << plat->reg_shift;
166 addr = (unsigned char *)plat->base + offset + plat->reg_offset;
168 if (IS_ENABLED(CONFIG_NS16550_DYNAMIC))
169 serial_out_dynamic(plat, addr, value);
171 serial_out_shift(addr, plat->reg_shift, value);
174 static int ns16550_readb(struct ns16550 *port, int offset)
176 struct ns16550_plat *plat = port->plat;
179 offset *= 1 << plat->reg_shift;
180 addr = (unsigned char *)plat->base + offset + plat->reg_offset;
182 if (IS_ENABLED(CONFIG_NS16550_DYNAMIC))
183 return serial_in_dynamic(plat, addr);
185 return serial_in_shift(addr, plat->reg_shift);
188 static u32 ns16550_getfcr(struct ns16550 *port)
190 struct ns16550_plat *plat = port->plat;
195 /* We can clean these up once everything is moved to driver model */
196 #define serial_out(value, addr) \
197 ns16550_writeb(com_port, \
198 (unsigned char *)addr - (unsigned char *)com_port, value)
199 #define serial_in(addr) \
200 ns16550_readb(com_port, \
201 (unsigned char *)addr - (unsigned char *)com_port)
203 static u32 ns16550_getfcr(struct ns16550 *port)
205 return UART_FCR_DEFVAL;
209 int ns16550_calc_divisor(struct ns16550 *port, int clock, int baudrate)
211 const unsigned int mode_x_div = 16;
213 return DIV_ROUND_CLOSEST(clock, mode_x_div * baudrate);
216 static void ns16550_setbrg(struct ns16550 *com_port, int baud_divisor)
218 /* to keep serial format, read lcr before writing BKSE */
219 int lcr_val = serial_in(&com_port->lcr) & ~UART_LCR_BKSE;
221 serial_out(UART_LCR_BKSE | lcr_val, &com_port->lcr);
222 serial_out(baud_divisor & 0xff, &com_port->dll);
223 serial_out((baud_divisor >> 8) & 0xff, &com_port->dlm);
224 serial_out(lcr_val, &com_port->lcr);
227 void ns16550_init(struct ns16550 *com_port, int baud_divisor)
229 #if (defined(CONFIG_SPL_BUILD) && \
230 (defined(CONFIG_OMAP34XX) || defined(CONFIG_OMAP44XX)))
232 * 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.
236 if ((serial_in(&com_port->lsr) & (UART_LSR_TEMT | UART_LSR_THRE))
238 if (baud_divisor != -1)
239 ns16550_setbrg(com_port, baud_divisor);
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);
245 ns16550_setbrg(com_port, divisor);
247 serial_out(0, &com_port->mdr1);
251 while (!(serial_in(&com_port->lsr) & UART_LSR_TEMT))
254 serial_out(CONFIG_SYS_NS16550_IER, &com_port->ier);
255 #if defined(CONFIG_ARCH_OMAP2PLUS) || defined(CONFIG_OMAP_SERIAL)
256 serial_out(0x7, &com_port->mdr1); /* mode select reset TL16C750*/
259 serial_out(UART_MCRVAL, &com_port->mcr);
260 serial_out(ns16550_getfcr(com_port), &com_port->fcr);
261 /* initialize serial config to 8N1 before writing baudrate */
262 serial_out(UART_LCRVAL, &com_port->lcr);
263 if (baud_divisor != -1)
264 ns16550_setbrg(com_port, baud_divisor);
265 #if defined(CONFIG_ARCH_OMAP2PLUS) || defined(CONFIG_SOC_DA8XX) || \
266 defined(CONFIG_OMAP_SERIAL)
267 /* /16 is proper to hit 115200 with 48MHz */
268 serial_out(0, &com_port->mdr1);
270 #if defined(CONFIG_ARCH_KEYSTONE)
271 serial_out(UART_REG_VAL_PWREMU_MGMT_UART_ENABLE, &com_port->regC);
275 #ifndef CONFIG_NS16550_MIN_FUNCTIONS
276 void ns16550_reinit(struct ns16550 *com_port, int baud_divisor)
278 serial_out(CONFIG_SYS_NS16550_IER, &com_port->ier);
279 ns16550_setbrg(com_port, 0);
280 serial_out(UART_MCRVAL, &com_port->mcr);
281 serial_out(ns16550_getfcr(com_port), &com_port->fcr);
282 ns16550_setbrg(com_port, baud_divisor);
284 #endif /* CONFIG_NS16550_MIN_FUNCTIONS */
286 void ns16550_putc(struct ns16550 *com_port, char c)
288 while ((serial_in(&com_port->lsr) & UART_LSR_THRE) == 0)
290 serial_out(c, &com_port->thr);
293 * Call watchdog_reset() upon newline. This is done here in putc
294 * since the environment code uses a single puts() to print the complete
295 * environment upon "printenv". So we can't put this watchdog call
302 #ifndef CONFIG_NS16550_MIN_FUNCTIONS
303 char ns16550_getc(struct ns16550 *com_port)
305 while ((serial_in(&com_port->lsr) & UART_LSR_DR) == 0) {
306 #if !defined(CONFIG_SPL_BUILD) && defined(CONFIG_USB_TTY)
307 extern void usbtty_poll(void);
312 return serial_in(&com_port->rbr);
315 int ns16550_tstc(struct ns16550 *com_port)
317 return (serial_in(&com_port->lsr) & UART_LSR_DR) != 0;
320 #endif /* CONFIG_NS16550_MIN_FUNCTIONS */
322 #ifdef CONFIG_DEBUG_UART_NS16550
324 #include <debug_uart.h>
326 static inline void _debug_uart_init(void)
328 struct ns16550 *com_port = (struct ns16550 *)CONFIG_DEBUG_UART_BASE;
332 * We copy the code from above because it is already horribly messy.
333 * Trying to refactor to nicely remove the duplication doesn't seem
334 * feasible. The better fix is to move all users of this driver to
337 baud_divisor = ns16550_calc_divisor(com_port, CONFIG_DEBUG_UART_CLOCK,
339 serial_dout(&com_port->ier, CONFIG_SYS_NS16550_IER);
340 serial_dout(&com_port->mcr, UART_MCRVAL);
341 serial_dout(&com_port->fcr, UART_FCR_DEFVAL);
343 serial_dout(&com_port->lcr, UART_LCR_BKSE | UART_LCRVAL);
344 serial_dout(&com_port->dll, baud_divisor & 0xff);
345 serial_dout(&com_port->dlm, (baud_divisor >> 8) & 0xff);
346 serial_dout(&com_port->lcr, UART_LCRVAL);
349 static inline int NS16550_read_baud_divisor(struct ns16550 *com_port)
353 serial_dout(&com_port->lcr, UART_LCR_BKSE | UART_LCRVAL);
354 ret = serial_din(&com_port->dll) & 0xff;
355 ret |= (serial_din(&com_port->dlm) & 0xff) << 8;
356 serial_dout(&com_port->lcr, UART_LCRVAL);
361 static inline void _debug_uart_putc(int ch)
363 struct ns16550 *com_port = (struct ns16550 *)CONFIG_DEBUG_UART_BASE;
365 while (!(serial_din(&com_port->lsr) & UART_LSR_THRE)) {
366 #ifdef CONFIG_DEBUG_UART_NS16550_CHECK_ENABLED
367 if (!NS16550_read_baud_divisor(com_port))
371 serial_dout(&com_port->thr, ch);
378 #if CONFIG_IS_ENABLED(DM_SERIAL)
379 static int ns16550_serial_putc(struct udevice *dev, const char ch)
381 struct ns16550 *const com_port = dev_get_priv(dev);
383 if (!(serial_in(&com_port->lsr) & UART_LSR_THRE))
385 serial_out(ch, &com_port->thr);
388 * Call watchdog_reset() upon newline. This is done here in putc
389 * since the environment code uses a single puts() to print the complete
390 * environment upon "printenv". So we can't put this watchdog call
399 static int ns16550_serial_pending(struct udevice *dev, bool input)
401 struct ns16550 *const com_port = dev_get_priv(dev);
404 return (serial_in(&com_port->lsr) & UART_LSR_DR) ? 1 : 0;
406 return (serial_in(&com_port->lsr) & UART_LSR_THRE) ? 0 : 1;
409 static int ns16550_serial_getc(struct udevice *dev)
411 struct ns16550 *const com_port = dev_get_priv(dev);
413 if (!(serial_in(&com_port->lsr) & UART_LSR_DR))
416 return serial_in(&com_port->rbr);
419 static int ns16550_serial_setbrg(struct udevice *dev, int baudrate)
421 struct ns16550 *const com_port = dev_get_priv(dev);
422 struct ns16550_plat *plat = com_port->plat;
425 clock_divisor = ns16550_calc_divisor(com_port, plat->clock, baudrate);
427 ns16550_setbrg(com_port, clock_divisor);
432 static int ns16550_serial_setconfig(struct udevice *dev, uint serial_config)
434 struct ns16550 *const com_port = dev_get_priv(dev);
435 int lcr_val = UART_LCR_WLS_8;
436 uint parity = SERIAL_GET_PARITY(serial_config);
437 uint bits = SERIAL_GET_BITS(serial_config);
438 uint stop = SERIAL_GET_STOP(serial_config);
441 * only parity config is implemented, check if other serial settings
442 * are the default one.
444 if (bits != SERIAL_8_BITS || stop != SERIAL_ONE_STOP)
445 return -ENOTSUPP; /* not supported in driver*/
448 case SERIAL_PAR_NONE:
452 lcr_val |= UART_LCR_PEN;
454 case SERIAL_PAR_EVEN:
455 lcr_val |= UART_LCR_PEN | UART_LCR_EPS;
458 return -ENOTSUPP; /* not supported in driver*/
461 serial_out(lcr_val, &com_port->lcr);
465 static int ns16550_serial_getinfo(struct udevice *dev,
466 struct serial_device_info *info)
468 struct ns16550 *const com_port = dev_get_priv(dev);
469 struct ns16550_plat *plat = com_port->plat;
471 info->type = SERIAL_CHIP_16550_COMPATIBLE;
472 #ifdef CONFIG_SYS_NS16550_PORT_MAPPED
473 info->addr_space = SERIAL_ADDRESS_SPACE_IO;
475 info->addr_space = SERIAL_ADDRESS_SPACE_MEMORY;
477 info->addr = plat->base;
478 info->reg_width = plat->reg_width;
479 info->reg_shift = plat->reg_shift;
480 info->reg_offset = plat->reg_offset;
481 info->clock = plat->clock;
486 static int ns16550_serial_assign_base(struct ns16550_plat *plat, fdt_addr_t base)
488 if (base == FDT_ADDR_T_NONE)
491 #ifdef CONFIG_SYS_NS16550_PORT_MAPPED
494 plat->base = (unsigned long)map_physmem(base, 0, MAP_NOCACHE);
500 int ns16550_serial_probe(struct udevice *dev)
502 struct ns16550_plat *plat = dev_get_plat(dev);
503 struct ns16550 *const com_port = dev_get_priv(dev);
504 struct reset_ctl_bulk reset_bulk;
509 * If we are on PCI bus, either directly attached to a PCI root port,
510 * or via a PCI bridge, assign plat->base before probing hardware.
512 if (device_is_on_pci_bus(dev)) {
513 addr = devfdt_get_addr_pci(dev);
514 ret = ns16550_serial_assign_base(plat, addr);
519 ret = reset_get_bulk(dev, &reset_bulk);
521 reset_deassert_bulk(&reset_bulk);
523 com_port->plat = dev_get_plat(dev);
524 ns16550_init(com_port, -1);
529 #if CONFIG_IS_ENABLED(OF_CONTROL)
536 #if CONFIG_IS_ENABLED(OF_REAL)
537 int ns16550_serial_of_to_plat(struct udevice *dev)
539 struct ns16550_plat *plat = dev_get_plat(dev);
540 const u32 port_type = dev_get_driver_data(dev);
545 addr = dev_read_addr(dev);
546 err = ns16550_serial_assign_base(plat, addr);
547 if (err && !device_is_on_pci_bus(dev))
550 plat->reg_offset = dev_read_u32_default(dev, "reg-offset", 0);
551 plat->reg_shift = dev_read_u32_default(dev, "reg-shift", 0);
552 plat->reg_width = dev_read_u32_default(dev, "reg-io-width", 1);
554 err = clk_get_by_index(dev, 0, &clk);
556 err = clk_get_rate(&clk);
557 if (!IS_ERR_VALUE(err))
559 } else if (err != -ENOENT && err != -ENODEV && err != -ENOSYS) {
560 debug("ns16550 failed to get clock\n");
565 plat->clock = dev_read_u32_default(dev, "clock-frequency",
566 CONFIG_SYS_NS16550_CLK);
568 plat->clock = CONFIG_SYS_NS16550_CLK;
570 debug("ns16550 clock not defined\n");
574 plat->fcr = UART_FCR_DEFVAL;
575 if (port_type == PORT_JZ4780)
576 plat->fcr |= UART_FCR_UME;
582 const struct dm_serial_ops ns16550_serial_ops = {
583 .putc = ns16550_serial_putc,
584 .pending = ns16550_serial_pending,
585 .getc = ns16550_serial_getc,
586 .setbrg = ns16550_serial_setbrg,
587 .setconfig = ns16550_serial_setconfig,
588 .getinfo = ns16550_serial_getinfo,
591 #if CONFIG_IS_ENABLED(OF_REAL)
593 * Please consider existing compatible strings before adding a new
594 * one to keep this table compact. Or you may add a generic "ns16550"
595 * compatible string to your dts.
597 static const struct udevice_id ns16550_serial_ids[] = {
598 { .compatible = "ns16550", .data = PORT_NS16550 },
599 { .compatible = "ns16550a", .data = PORT_NS16550 },
600 { .compatible = "ingenic,jz4780-uart", .data = PORT_JZ4780 },
601 { .compatible = "nvidia,tegra20-uart", .data = PORT_NS16550 },
602 { .compatible = "snps,dw-apb-uart", .data = PORT_NS16550 },
607 #if CONFIG_IS_ENABLED(SERIAL_PRESENT)
609 /* TODO(sjg@chromium.org): Integrate this into a macro like CONFIG_IS_ENABLED */
610 #if !defined(CONFIG_TPL_BUILD) || defined(CONFIG_TPL_DM_SERIAL)
611 U_BOOT_DRIVER(ns16550_serial) = {
612 .name = "ns16550_serial",
614 #if CONFIG_IS_ENABLED(OF_REAL)
615 .of_match = ns16550_serial_ids,
616 .of_to_plat = ns16550_serial_of_to_plat,
617 .plat_auto = sizeof(struct ns16550_plat),
619 .priv_auto = sizeof(struct ns16550),
620 .probe = ns16550_serial_probe,
621 .ops = &ns16550_serial_ops,
622 #if !CONFIG_IS_ENABLED(OF_CONTROL)
623 .flags = DM_FLAG_PRE_RELOC,
627 DM_DRIVER_ALIAS(ns16550_serial, rockchip_rk3328_uart)
628 DM_DRIVER_ALIAS(ns16550_serial, rockchip_rk3368_uart)
629 DM_DRIVER_ALIAS(ns16550_serial, ti_da830_uart)
631 #endif /* SERIAL_PRESENT */
633 #endif /* CONFIG_DM_SERIAL */