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 <linux/err.h>
18 #include <linux/types.h>
21 DECLARE_GLOBAL_DATA_PTR;
23 #define UART_LCRVAL UART_LCR_8N1 /* 8 data, 1 stop, no parity */
24 #define UART_MCRVAL (UART_MCR_DTR | \
25 UART_MCR_RTS) /* RTS/DTR */
27 #if !CONFIG_IS_ENABLED(DM_SERIAL)
28 #ifdef CONFIG_SYS_NS16550_PORT_MAPPED
29 #define serial_out(x, y) outb(x, (ulong)y)
30 #define serial_in(y) inb((ulong)y)
31 #elif defined(CONFIG_SYS_NS16550_MEM32) && (CONFIG_SYS_NS16550_REG_SIZE > 0)
32 #define serial_out(x, y) out_be32(y, x)
33 #define serial_in(y) in_be32(y)
34 #elif defined(CONFIG_SYS_NS16550_MEM32) && (CONFIG_SYS_NS16550_REG_SIZE < 0)
35 #define serial_out(x, y) out_le32(y, x)
36 #define serial_in(y) in_le32(y)
38 #define serial_out(x, y) writeb(x, y)
39 #define serial_in(y) readb(y)
41 #endif /* !CONFIG_DM_SERIAL */
43 #if defined(CONFIG_SOC_KEYSTONE)
44 #define UART_REG_VAL_PWREMU_MGMT_UART_DISABLE 0
45 #define UART_REG_VAL_PWREMU_MGMT_UART_ENABLE ((1 << 14) | (1 << 13) | (1 << 0))
47 #ifdef CONFIG_SERIAL_HW_FLOW_CONTROL
48 #define UART_MCRVAL (UART_MCR_RTS | UART_MCR_AFE)
50 #define UART_MCRVAL (UART_MCR_RTS)
54 #ifndef CONFIG_SYS_NS16550_IER
55 #define CONFIG_SYS_NS16550_IER 0x00
56 #endif /* CONFIG_SYS_NS16550_IER */
58 static inline void serial_out_shift(void *addr, int shift, int value)
60 #ifdef CONFIG_SYS_NS16550_PORT_MAPPED
61 outb(value, (ulong)addr);
62 #elif defined(CONFIG_SYS_NS16550_MEM32) && defined(CONFIG_SYS_LITTLE_ENDIAN)
63 out_le32(addr, value);
64 #elif defined(CONFIG_SYS_NS16550_MEM32) && defined(CONFIG_SYS_BIG_ENDIAN)
65 out_be32(addr, value);
66 #elif defined(CONFIG_SYS_NS16550_MEM32)
68 #elif defined(CONFIG_SYS_BIG_ENDIAN)
69 writeb(value, addr + (1 << shift) - 1);
75 static inline int serial_in_shift(void *addr, int shift)
77 #ifdef CONFIG_SYS_NS16550_PORT_MAPPED
78 return inb((ulong)addr);
79 #elif defined(CONFIG_SYS_NS16550_MEM32) && defined(CONFIG_SYS_LITTLE_ENDIAN)
81 #elif defined(CONFIG_SYS_NS16550_MEM32) && defined(CONFIG_SYS_BIG_ENDIAN)
83 #elif defined(CONFIG_SYS_NS16550_MEM32)
85 #elif defined(CONFIG_SYS_BIG_ENDIAN)
86 return readb(addr + (1 << shift) - 1);
92 #if CONFIG_IS_ENABLED(DM_SERIAL)
94 #ifndef CONFIG_SYS_NS16550_CLK
95 #define CONFIG_SYS_NS16550_CLK 0
99 * Use this #ifdef for now since many platforms don't define in(), out(),
100 * out_le32(), etc. but we don't have #defines to indicate this.
105 #ifdef CONFIG_NS16550_DYNAMIC
106 static void serial_out_dynamic(struct ns16550_platdata *plat, u8 *addr,
109 if (plat->flags & NS16550_FLAG_IO) {
111 } else if (plat->reg_width == 4) {
112 if (plat->flags & NS16550_FLAG_ENDIAN) {
113 if (plat->flags & NS16550_FLAG_BE)
114 out_be32(addr, value);
116 out_le32(addr, value);
120 } else if (plat->flags & NS16550_FLAG_BE) {
121 writeb(value, addr + (1 << plat->reg_shift) - 1);
127 static int serial_in_dynamic(struct ns16550_platdata *plat, u8 *addr)
129 if (plat->flags & NS16550_FLAG_IO) {
131 } else if (plat->reg_width == 4) {
132 if (plat->flags & NS16550_FLAG_ENDIAN) {
133 if (plat->flags & NS16550_FLAG_BE)
134 return in_be32(addr);
136 return in_le32(addr);
140 } else if (plat->flags & NS16550_FLAG_BE) {
141 return readb(addr + (1 << plat->reg_shift) - 1);
147 static inline void serial_out_dynamic(struct ns16550_platdata *plat, u8 *addr,
152 static inline int serial_in_dynamic(struct ns16550_platdata *plat, u8 *addr)
157 #endif /* CONFIG_NS16550_DYNAMIC */
159 static void ns16550_writeb(NS16550_t port, int offset, int value)
161 struct ns16550_platdata *plat = port->plat;
164 offset *= 1 << plat->reg_shift;
165 addr = (unsigned char *)plat->base + offset + plat->reg_offset;
167 if (IS_ENABLED(CONFIG_NS16550_DYNAMIC))
168 serial_out_dynamic(plat, addr, value);
170 serial_out_shift(addr, plat->reg_shift, value);
173 static int ns16550_readb(NS16550_t port, int offset)
175 struct ns16550_platdata *plat = port->plat;
178 offset *= 1 << plat->reg_shift;
179 addr = (unsigned char *)plat->base + offset + plat->reg_offset;
181 if (IS_ENABLED(CONFIG_NS16550_DYNAMIC))
182 return serial_in_dynamic(plat, addr);
184 return serial_in_shift(addr, plat->reg_shift);
187 static u32 ns16550_getfcr(NS16550_t port)
189 struct ns16550_platdata *plat = port->plat;
194 /* We can clean these up once everything is moved to driver model */
195 #define serial_out(value, addr) \
196 ns16550_writeb(com_port, \
197 (unsigned char *)addr - (unsigned char *)com_port, value)
198 #define serial_in(addr) \
199 ns16550_readb(com_port, \
200 (unsigned char *)addr - (unsigned char *)com_port)
202 static u32 ns16550_getfcr(NS16550_t port)
204 return UART_FCR_DEFVAL;
208 int ns16550_calc_divisor(NS16550_t port, int clock, int baudrate)
210 const unsigned int mode_x_div = 16;
212 return DIV_ROUND_CLOSEST(clock, mode_x_div * baudrate);
215 static void NS16550_setbrg(NS16550_t com_port, int baud_divisor)
217 /* to keep serial format, read lcr before writing BKSE */
218 int lcr_val = serial_in(&com_port->lcr) & ~UART_LCR_BKSE;
220 serial_out(UART_LCR_BKSE | lcr_val, &com_port->lcr);
221 serial_out(baud_divisor & 0xff, &com_port->dll);
222 serial_out((baud_divisor >> 8) & 0xff, &com_port->dlm);
223 serial_out(lcr_val, &com_port->lcr);
226 void NS16550_init(NS16550_t com_port, int baud_divisor)
228 #if (defined(CONFIG_SPL_BUILD) && \
229 (defined(CONFIG_OMAP34XX) || defined(CONFIG_OMAP44XX)))
231 * On some OMAP3/OMAP4 devices when UART3 is configured for boot mode
232 * before SPL starts only THRE bit is set. We have to empty the
233 * transmitter before initialization starts.
235 if ((serial_in(&com_port->lsr) & (UART_LSR_TEMT | UART_LSR_THRE))
237 if (baud_divisor != -1)
238 NS16550_setbrg(com_port, baud_divisor);
240 // Re-use old baud rate divisor to flush transmit reg.
241 const int dll = serial_in(&com_port->dll);
242 const int dlm = serial_in(&com_port->dlm);
243 const int divisor = dll | (dlm << 8);
244 NS16550_setbrg(com_port, divisor);
246 serial_out(0, &com_port->mdr1);
250 while (!(serial_in(&com_port->lsr) & UART_LSR_TEMT))
253 serial_out(CONFIG_SYS_NS16550_IER, &com_port->ier);
254 #if defined(CONFIG_ARCH_OMAP2PLUS) || defined(CONFIG_OMAP_SERIAL)
255 serial_out(0x7, &com_port->mdr1); /* mode select reset TL16C750*/
258 serial_out(UART_MCRVAL, &com_port->mcr);
259 serial_out(ns16550_getfcr(com_port), &com_port->fcr);
260 /* initialize serial config to 8N1 before writing baudrate */
261 serial_out(UART_LCRVAL, &com_port->lcr);
262 if (baud_divisor != -1)
263 NS16550_setbrg(com_port, baud_divisor);
264 #if defined(CONFIG_ARCH_OMAP2PLUS) || defined(CONFIG_SOC_DA8XX) || \
265 defined(CONFIG_OMAP_SERIAL)
266 /* /16 is proper to hit 115200 with 48MHz */
267 serial_out(0, &com_port->mdr1);
269 #if defined(CONFIG_SOC_KEYSTONE)
270 serial_out(UART_REG_VAL_PWREMU_MGMT_UART_ENABLE, &com_port->regC);
274 #ifndef CONFIG_NS16550_MIN_FUNCTIONS
275 void NS16550_reinit(NS16550_t com_port, int baud_divisor)
277 serial_out(CONFIG_SYS_NS16550_IER, &com_port->ier);
278 NS16550_setbrg(com_port, 0);
279 serial_out(UART_MCRVAL, &com_port->mcr);
280 serial_out(ns16550_getfcr(com_port), &com_port->fcr);
281 NS16550_setbrg(com_port, baud_divisor);
283 #endif /* CONFIG_NS16550_MIN_FUNCTIONS */
285 void NS16550_putc(NS16550_t com_port, char c)
287 while ((serial_in(&com_port->lsr) & UART_LSR_THRE) == 0)
289 serial_out(c, &com_port->thr);
292 * Call watchdog_reset() upon newline. This is done here in putc
293 * since the environment code uses a single puts() to print the complete
294 * environment upon "printenv". So we can't put this watchdog call
301 #ifndef CONFIG_NS16550_MIN_FUNCTIONS
302 char NS16550_getc(NS16550_t com_port)
304 while ((serial_in(&com_port->lsr) & UART_LSR_DR) == 0) {
305 #if !defined(CONFIG_SPL_BUILD) && defined(CONFIG_USB_TTY)
306 extern void usbtty_poll(void);
311 return serial_in(&com_port->rbr);
314 int NS16550_tstc(NS16550_t com_port)
316 return (serial_in(&com_port->lsr) & UART_LSR_DR) != 0;
319 #endif /* CONFIG_NS16550_MIN_FUNCTIONS */
321 #ifdef CONFIG_DEBUG_UART_NS16550
323 #include <debug_uart.h>
325 static inline void _debug_uart_init(void)
327 struct NS16550 *com_port = (struct NS16550 *)CONFIG_DEBUG_UART_BASE;
331 * We copy the code from above because it is already horribly messy.
332 * Trying to refactor to nicely remove the duplication doesn't seem
333 * feasible. The better fix is to move all users of this driver to
336 baud_divisor = ns16550_calc_divisor(com_port, CONFIG_DEBUG_UART_CLOCK,
338 serial_dout(&com_port->ier, CONFIG_SYS_NS16550_IER);
339 serial_dout(&com_port->mcr, UART_MCRVAL);
340 serial_dout(&com_port->fcr, UART_FCR_DEFVAL);
342 serial_dout(&com_port->lcr, UART_LCR_BKSE | UART_LCRVAL);
343 serial_dout(&com_port->dll, baud_divisor & 0xff);
344 serial_dout(&com_port->dlm, (baud_divisor >> 8) & 0xff);
345 serial_dout(&com_port->lcr, UART_LCRVAL);
348 static inline int NS16550_read_baud_divisor(struct NS16550 *com_port)
352 serial_dout(&com_port->lcr, UART_LCR_BKSE | UART_LCRVAL);
353 ret = serial_din(&com_port->dll) & 0xff;
354 ret |= (serial_din(&com_port->dlm) & 0xff) << 8;
355 serial_dout(&com_port->lcr, UART_LCRVAL);
360 static inline void _debug_uart_putc(int ch)
362 struct NS16550 *com_port = (struct NS16550 *)CONFIG_DEBUG_UART_BASE;
364 while (!(serial_din(&com_port->lsr) & UART_LSR_THRE)) {
365 #ifdef CONFIG_DEBUG_UART_NS16550_CHECK_ENABLED
366 if (!NS16550_read_baud_divisor(com_port))
370 serial_dout(&com_port->thr, ch);
377 #if CONFIG_IS_ENABLED(DM_SERIAL)
378 static int ns16550_serial_putc(struct udevice *dev, const char ch)
380 struct NS16550 *const com_port = dev_get_priv(dev);
382 if (!(serial_in(&com_port->lsr) & UART_LSR_THRE))
384 serial_out(ch, &com_port->thr);
387 * Call watchdog_reset() upon newline. This is done here in putc
388 * since the environment code uses a single puts() to print the complete
389 * environment upon "printenv". So we can't put this watchdog call
398 static int ns16550_serial_pending(struct udevice *dev, bool input)
400 struct NS16550 *const com_port = dev_get_priv(dev);
403 return (serial_in(&com_port->lsr) & UART_LSR_DR) ? 1 : 0;
405 return (serial_in(&com_port->lsr) & UART_LSR_THRE) ? 0 : 1;
408 static int ns16550_serial_getc(struct udevice *dev)
410 struct NS16550 *const com_port = dev_get_priv(dev);
412 if (!(serial_in(&com_port->lsr) & UART_LSR_DR))
415 return serial_in(&com_port->rbr);
418 static int ns16550_serial_setbrg(struct udevice *dev, int baudrate)
420 struct NS16550 *const com_port = dev_get_priv(dev);
421 struct ns16550_platdata *plat = com_port->plat;
424 clock_divisor = ns16550_calc_divisor(com_port, plat->clock, baudrate);
426 NS16550_setbrg(com_port, clock_divisor);
431 static int ns16550_serial_setconfig(struct udevice *dev, uint serial_config)
433 struct NS16550 *const com_port = dev_get_priv(dev);
434 int lcr_val = UART_LCR_WLS_8;
435 uint parity = SERIAL_GET_PARITY(serial_config);
436 uint bits = SERIAL_GET_BITS(serial_config);
437 uint stop = SERIAL_GET_STOP(serial_config);
440 * only parity config is implemented, check if other serial settings
441 * are the default one.
443 if (bits != SERIAL_8_BITS || stop != SERIAL_ONE_STOP)
444 return -ENOTSUPP; /* not supported in driver*/
447 case SERIAL_PAR_NONE:
451 lcr_val |= UART_LCR_PEN;
453 case SERIAL_PAR_EVEN:
454 lcr_val |= UART_LCR_PEN | UART_LCR_EPS;
457 return -ENOTSUPP; /* not supported in driver*/
460 serial_out(lcr_val, &com_port->lcr);
464 static int ns16550_serial_getinfo(struct udevice *dev,
465 struct serial_device_info *info)
467 struct NS16550 *const com_port = dev_get_priv(dev);
468 struct ns16550_platdata *plat = com_port->plat;
470 info->type = SERIAL_CHIP_16550_COMPATIBLE;
471 #ifdef CONFIG_SYS_NS16550_PORT_MAPPED
472 info->addr_space = SERIAL_ADDRESS_SPACE_IO;
474 info->addr_space = SERIAL_ADDRESS_SPACE_MEMORY;
476 info->addr = plat->base;
477 info->reg_width = plat->reg_width;
478 info->reg_shift = plat->reg_shift;
479 info->reg_offset = plat->reg_offset;
480 info->clock = plat->clock;
485 static int ns16550_serial_assign_base(struct ns16550_platdata *plat, ulong base)
487 if (base == FDT_ADDR_T_NONE)
490 #ifdef CONFIG_SYS_NS16550_PORT_MAPPED
493 plat->base = (unsigned long)map_physmem(base, 0, MAP_NOCACHE);
499 int ns16550_serial_probe(struct udevice *dev)
501 struct ns16550_platdata *plat = dev->plat;
502 struct NS16550 *const com_port = dev_get_priv(dev);
503 struct reset_ctl_bulk reset_bulk;
508 * If we are on PCI bus, either directly attached to a PCI root port,
509 * or via a PCI bridge, assign plat->base before probing hardware.
511 if (device_is_on_pci_bus(dev)) {
512 addr = devfdt_get_addr_pci(dev);
513 ret = ns16550_serial_assign_base(plat, addr);
518 ret = reset_get_bulk(dev, &reset_bulk);
520 reset_deassert_bulk(&reset_bulk);
522 com_port->plat = dev_get_plat(dev);
523 NS16550_init(com_port, -1);
528 #if CONFIG_IS_ENABLED(OF_CONTROL)
535 #if CONFIG_IS_ENABLED(OF_CONTROL) && !CONFIG_IS_ENABLED(OF_PLATDATA)
536 int ns16550_serial_of_to_plat(struct udevice *dev)
538 struct ns16550_platdata *plat = dev->plat;
539 const u32 port_type = dev_get_driver_data(dev);
544 addr = dev_read_addr(dev);
545 err = ns16550_serial_assign_base(plat, addr);
546 if (err && !device_is_on_pci_bus(dev))
549 plat->reg_offset = dev_read_u32_default(dev, "reg-offset", 0);
550 plat->reg_shift = dev_read_u32_default(dev, "reg-shift", 0);
551 plat->reg_width = dev_read_u32_default(dev, "reg-io-width", 1);
553 err = clk_get_by_index(dev, 0, &clk);
555 err = clk_get_rate(&clk);
556 if (!IS_ERR_VALUE(err))
558 } else if (err != -ENOENT && err != -ENODEV && err != -ENOSYS) {
559 debug("ns16550 failed to get clock\n");
564 plat->clock = dev_read_u32_default(dev, "clock-frequency",
565 CONFIG_SYS_NS16550_CLK);
567 debug("ns16550 clock not defined\n");
571 plat->fcr = UART_FCR_DEFVAL;
572 if (port_type == PORT_JZ4780)
573 plat->fcr |= UART_FCR_UME;
579 const struct dm_serial_ops ns16550_serial_ops = {
580 .putc = ns16550_serial_putc,
581 .pending = ns16550_serial_pending,
582 .getc = ns16550_serial_getc,
583 .setbrg = ns16550_serial_setbrg,
584 .setconfig = ns16550_serial_setconfig,
585 .getinfo = ns16550_serial_getinfo,
588 #if CONFIG_IS_ENABLED(OF_CONTROL) && !CONFIG_IS_ENABLED(OF_PLATDATA)
590 * Please consider existing compatible strings before adding a new
591 * one to keep this table compact. Or you may add a generic "ns16550"
592 * compatible string to your dts.
594 static const struct udevice_id ns16550_serial_ids[] = {
595 { .compatible = "ns16550", .data = PORT_NS16550 },
596 { .compatible = "ns16550a", .data = PORT_NS16550 },
597 { .compatible = "ingenic,jz4780-uart", .data = PORT_JZ4780 },
598 { .compatible = "nvidia,tegra20-uart", .data = PORT_NS16550 },
599 { .compatible = "snps,dw-apb-uart", .data = PORT_NS16550 },
602 #endif /* OF_CONTROL && !OF_PLATDATA */
604 #if CONFIG_IS_ENABLED(SERIAL_PRESENT)
607 #if !defined(CONFIG_TPL_BUILD) || defined(CONFIG_TPL_DM_SERIAL)
608 U_BOOT_DRIVER(ns16550_serial) = {
609 .name = "ns16550_serial",
611 #if CONFIG_IS_ENABLED(OF_CONTROL) && !CONFIG_IS_ENABLED(OF_PLATDATA)
612 .of_match = ns16550_serial_ids,
613 .of_to_plat = ns16550_serial_of_to_plat,
614 .plat_auto = sizeof(struct ns16550_platdata),
616 .priv_auto = sizeof(struct NS16550),
617 .probe = ns16550_serial_probe,
618 .ops = &ns16550_serial_ops,
619 #if !CONFIG_IS_ENABLED(OF_CONTROL)
620 .flags = DM_FLAG_PRE_RELOC,
624 U_BOOT_DRIVER_ALIAS(ns16550_serial, rockchip_rk3328_uart)
625 U_BOOT_DRIVER_ALIAS(ns16550_serial, rockchip_rk3368_uart)
626 U_BOOT_DRIVER_ALIAS(ns16550_serial, ti_da830_uart)
628 #endif /* SERIAL_PRESENT */
630 #endif /* CONFIG_DM_SERIAL */