1 // SPDX-License-Identifier: GPL-2.0+
3 * Texas Instruments' OMAP serial driver
5 * Copyright (C) 2018 Texas Instruments Incorporated - http://www.ti.com/
6 * Lokesh Vutla <lokeshvutla@ti.com>
11 #include <dt-structs.h>
16 #include <linux/err.h>
18 #ifndef CFG_SYS_NS16550_CLK
19 #define CFG_SYS_NS16550_CLK 0
22 #ifdef CONFIG_DEBUG_UART_OMAP
24 #ifndef CFG_SYS_NS16550_IER
25 #define CFG_SYS_NS16550_IER 0x00
28 #define UART_MCRVAL 0x00
29 #define UART_LCRVAL UART_LCR_8N1
31 static inline void serial_out_shift(void *addr, int shift, int value)
33 #ifdef CONFIG_SYS_NS16550_PORT_MAPPED
34 outb(value, (ulong)addr);
35 #elif defined(CONFIG_SYS_NS16550_MEM32) && defined(CONFIG_SYS_LITTLE_ENDIAN)
36 out_le32(addr, value);
37 #elif defined(CONFIG_SYS_NS16550_MEM32) && defined(CONFIG_SYS_BIG_ENDIAN)
38 out_be32(addr, value);
39 #elif defined(CONFIG_SYS_NS16550_MEM32)
41 #elif defined(CONFIG_SYS_BIG_ENDIAN)
42 writeb(value, addr + (1 << shift) - 1);
48 static inline int serial_in_shift(void *addr, int shift)
50 #ifdef CONFIG_SYS_NS16550_PORT_MAPPED
51 return inb((ulong)addr);
52 #elif defined(CONFIG_SYS_NS16550_MEM32) && defined(CONFIG_SYS_LITTLE_ENDIAN)
54 #elif defined(CONFIG_SYS_NS16550_MEM32) && defined(CONFIG_SYS_BIG_ENDIAN)
56 #elif defined(CONFIG_SYS_NS16550_MEM32)
58 #elif defined(CONFIG_SYS_BIG_ENDIAN)
59 return readb(addr + (1 << shift) - 1);
65 #include <debug_uart.h>
67 static inline void _debug_uart_init(void)
69 struct ns16550 *com_port = (struct ns16550 *)CONFIG_VAL(DEBUG_UART_BASE);
72 baud_divisor = ns16550_calc_divisor(com_port, CONFIG_DEBUG_UART_CLOCK,
74 serial_dout(&com_port->ier, CFG_SYS_NS16550_IER);
75 serial_dout(&com_port->mdr1, 0x7);
76 serial_dout(&com_port->mcr, UART_MCRVAL);
77 serial_dout(&com_port->fcr, UART_FCR_DEFVAL);
79 serial_dout(&com_port->lcr, UART_LCR_BKSE | UART_LCRVAL);
80 serial_dout(&com_port->dll, baud_divisor & 0xff);
81 serial_dout(&com_port->dlm, (baud_divisor >> 8) & 0xff);
82 serial_dout(&com_port->lcr, UART_LCRVAL);
83 serial_dout(&com_port->mdr1, 0x0);
86 static inline void _debug_uart_putc(int ch)
88 struct ns16550 *com_port = (struct ns16550 *)CONFIG_VAL(DEBUG_UART_BASE);
90 while (!(serial_din(&com_port->lsr) & UART_LSR_THRE))
92 serial_dout(&com_port->thr, ch);
99 #if CONFIG_IS_ENABLED(DM_SERIAL)
101 #if CONFIG_IS_ENABLED(OF_REAL)
102 static int omap_serial_of_to_plat(struct udevice *dev)
104 struct ns16550_plat *plat = dev_get_plat(dev);
109 /* try Processor Local Bus device first */
110 addr = dev_read_addr(dev);
111 if (addr == FDT_ADDR_T_NONE)
114 plat->base = (unsigned long)map_physmem(addr, 0, MAP_NOCACHE);
116 plat->reg_offset = dev_read_u32_default(dev, "reg-offset", 0);
119 err = clk_get_by_index(dev, 0, &clk);
121 err = clk_get_rate(&clk);
122 if (!IS_ERR_VALUE(err))
124 } else if (err != -ENOENT && err != -ENODEV && err != -ENOSYS) {
125 debug("omap serial failed to get clock\n");
130 plat->clock = dev_read_u32_default(dev, "clock-frequency",
131 CFG_SYS_NS16550_CLK);
133 debug("omap serial clock not defined\n");
137 plat->fcr = UART_FCR_DEFVAL;
142 static const struct udevice_id omap_serial_ids[] = {
143 { .compatible = "ti,omap2-uart", },
144 { .compatible = "ti,omap3-uart", },
145 { .compatible = "ti,omap4-uart", },
146 { .compatible = "ti,am3352-uart", },
147 { .compatible = "ti,am4372-uart", },
148 { .compatible = "ti,dra742-uart", },
149 { .compatible = "ti,am654-uart", },
154 #if CONFIG_IS_ENABLED(SERIAL_PRESENT)
155 U_BOOT_DRIVER(omap_serial) = {
156 .name = "omap_serial",
158 #if CONFIG_IS_ENABLED(OF_REAL)
159 .of_match = omap_serial_ids,
160 .of_to_plat = omap_serial_of_to_plat,
161 .plat_auto = sizeof(struct ns16550_plat),
163 .priv_auto = sizeof(struct ns16550),
164 .probe = ns16550_serial_probe,
165 .ops = &ns16550_serial_ops,
166 #if !CONFIG_IS_ENABLED(OF_CONTROL)
167 .flags = DM_FLAG_PRE_RELOC,
171 #endif /* DM_SERIAL */