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>
15 #include <linux/err.h>
17 #ifndef CONFIG_SYS_NS16550_CLK
18 #define CONFIG_SYS_NS16550_CLK 0
21 #ifdef CONFIG_DEBUG_UART_OMAP
23 #ifndef CONFIG_SYS_NS16550_IER
24 #define CONFIG_SYS_NS16550_IER 0x00
27 #define UART_MCRVAL 0x00
28 #define UART_LCRVAL UART_LCR_8N1
30 static inline void serial_out_shift(void *addr, int shift, int value)
32 #ifdef CONFIG_SYS_NS16550_PORT_MAPPED
33 outb(value, (ulong)addr);
34 #elif defined(CONFIG_SYS_NS16550_MEM32) && defined(CONFIG_SYS_LITTLE_ENDIAN)
35 out_le32(addr, value);
36 #elif defined(CONFIG_SYS_NS16550_MEM32) && defined(CONFIG_SYS_BIG_ENDIAN)
37 out_be32(addr, value);
38 #elif defined(CONFIG_SYS_NS16550_MEM32)
40 #elif defined(CONFIG_SYS_BIG_ENDIAN)
41 writeb(value, addr + (1 << shift) - 1);
47 static inline int serial_in_shift(void *addr, int shift)
49 #ifdef CONFIG_SYS_NS16550_PORT_MAPPED
50 return inb((ulong)addr);
51 #elif defined(CONFIG_SYS_NS16550_MEM32) && defined(CONFIG_SYS_LITTLE_ENDIAN)
53 #elif defined(CONFIG_SYS_NS16550_MEM32) && defined(CONFIG_SYS_BIG_ENDIAN)
55 #elif defined(CONFIG_SYS_NS16550_MEM32)
57 #elif defined(CONFIG_SYS_BIG_ENDIAN)
58 return readb(addr + (1 << shift) - 1);
64 #include <debug_uart.h>
66 static inline void _debug_uart_init(void)
68 struct NS16550 *com_port = (struct NS16550 *)CONFIG_DEBUG_UART_BASE;
71 baud_divisor = ns16550_calc_divisor(com_port, CONFIG_DEBUG_UART_CLOCK,
73 serial_dout(&com_port->ier, CONFIG_SYS_NS16550_IER);
74 serial_dout(&com_port->mdr1, 0x7);
75 serial_dout(&com_port->mcr, UART_MCRVAL);
76 serial_dout(&com_port->fcr, UART_FCR_DEFVAL);
78 serial_dout(&com_port->lcr, UART_LCR_BKSE | UART_LCRVAL);
79 serial_dout(&com_port->dll, baud_divisor & 0xff);
80 serial_dout(&com_port->dlm, (baud_divisor >> 8) & 0xff);
81 serial_dout(&com_port->lcr, UART_LCRVAL);
82 serial_dout(&com_port->mdr1, 0x0);
85 static inline void _debug_uart_putc(int ch)
87 struct NS16550 *com_port = (struct NS16550 *)CONFIG_DEBUG_UART_BASE;
89 while (!(serial_din(&com_port->lsr) & UART_LSR_THRE))
91 serial_dout(&com_port->thr, ch);
98 #if CONFIG_IS_ENABLED(DM_SERIAL)
100 #if CONFIG_IS_ENABLED(OF_CONTROL) && !CONFIG_IS_ENABLED(OF_PLATDATA)
101 static int omap_serial_ofdata_to_platdata(struct udevice *dev)
103 struct ns16550_platdata *plat = dev->platdata;
108 /* try Processor Local Bus device first */
109 addr = dev_read_addr(dev);
110 if (addr == FDT_ADDR_T_NONE)
113 plat->base = (unsigned long)map_physmem(addr, 0, MAP_NOCACHE);
115 plat->reg_offset = dev_read_u32_default(dev, "reg-offset", 0);
118 err = clk_get_by_index(dev, 0, &clk);
120 err = clk_get_rate(&clk);
121 if (!IS_ERR_VALUE(err))
123 } else if (err != -ENOENT && err != -ENODEV && err != -ENOSYS) {
124 debug("omap serial failed to get clock\n");
129 plat->clock = dev_read_u32_default(dev, "clock-frequency",
130 CONFIG_SYS_NS16550_CLK);
132 debug("omap serial clock not defined\n");
136 plat->fcr = UART_FCR_DEFVAL;
141 static const struct udevice_id omap_serial_ids[] = {
142 { .compatible = "ti,omap2-uart", },
143 { .compatible = "ti,omap3-uart", },
144 { .compatible = "ti,omap4-uart", },
145 { .compatible = "ti,am3352-uart", },
146 { .compatible = "ti,am4372-uart", },
147 { .compatible = "ti,dra742-uart", },
148 { .compatible = "ti,am654-uart", },
151 #endif /* OF_CONTROL && !OF_PLATDATA */
153 #if CONFIG_IS_ENABLED(SERIAL_PRESENT)
154 U_BOOT_DRIVER(omap_serial) = {
155 .name = "omap_serial",
157 #if CONFIG_IS_ENABLED(OF_CONTROL) && !CONFIG_IS_ENABLED(OF_PLATDATA)
158 .of_match = omap_serial_ids,
159 .ofdata_to_platdata = omap_serial_ofdata_to_platdata,
160 .platdata_auto_alloc_size = sizeof(struct ns16550_platdata),
162 .priv_auto_alloc_size = sizeof(struct NS16550),
163 .probe = ns16550_serial_probe,
164 .ops = &ns16550_serial_ops,
165 #if !CONFIG_IS_ENABLED(OF_CONTROL)
166 .flags = DM_FLAG_PRE_RELOC,
170 #endif /* DM_SERIAL */