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);
174 serial_out(0, &com_port->mdr1);
178 while (!(serial_in(&com_port->lsr) & UART_LSR_TEMT))
181 serial_out(CONFIG_SYS_NS16550_IER, &com_port->ier);
182 #if defined(CONFIG_ARCH_OMAP2PLUS) || defined(CONFIG_OMAP_SERIAL)
183 serial_out(0x7, &com_port->mdr1); /* mode select reset TL16C750*/
186 serial_out(UART_MCRVAL, &com_port->mcr);
187 serial_out(ns16550_getfcr(com_port), &com_port->fcr);
188 /* initialize serial config to 8N1 before writing baudrate */
189 serial_out(UART_LCRVAL, &com_port->lcr);
190 if (baud_divisor != -1)
191 NS16550_setbrg(com_port, baud_divisor);
192 #if defined(CONFIG_ARCH_OMAP2PLUS) || defined(CONFIG_SOC_DA8XX) || \
193 defined(CONFIG_OMAP_SERIAL)
194 /* /16 is proper to hit 115200 with 48MHz */
195 serial_out(0, &com_port->mdr1);
197 #if defined(CONFIG_SOC_KEYSTONE)
198 serial_out(UART_REG_VAL_PWREMU_MGMT_UART_ENABLE, &com_port->regC);
202 #ifndef CONFIG_NS16550_MIN_FUNCTIONS
203 void NS16550_reinit(NS16550_t com_port, int baud_divisor)
205 serial_out(CONFIG_SYS_NS16550_IER, &com_port->ier);
206 NS16550_setbrg(com_port, 0);
207 serial_out(UART_MCRVAL, &com_port->mcr);
208 serial_out(ns16550_getfcr(com_port), &com_port->fcr);
209 NS16550_setbrg(com_port, baud_divisor);
211 #endif /* CONFIG_NS16550_MIN_FUNCTIONS */
213 void NS16550_putc(NS16550_t com_port, char c)
215 while ((serial_in(&com_port->lsr) & UART_LSR_THRE) == 0)
217 serial_out(c, &com_port->thr);
220 * Call watchdog_reset() upon newline. This is done here in putc
221 * since the environment code uses a single puts() to print the complete
222 * environment upon "printenv". So we can't put this watchdog call
229 #ifndef CONFIG_NS16550_MIN_FUNCTIONS
230 char NS16550_getc(NS16550_t com_port)
232 while ((serial_in(&com_port->lsr) & UART_LSR_DR) == 0) {
233 #if !defined(CONFIG_SPL_BUILD) && defined(CONFIG_USB_TTY)
234 extern void usbtty_poll(void);
239 return serial_in(&com_port->rbr);
242 int NS16550_tstc(NS16550_t com_port)
244 return (serial_in(&com_port->lsr) & UART_LSR_DR) != 0;
247 #endif /* CONFIG_NS16550_MIN_FUNCTIONS */
249 #ifdef CONFIG_DEBUG_UART_NS16550
251 #include <debug_uart.h>
253 static inline void _debug_uart_init(void)
255 struct NS16550 *com_port = (struct NS16550 *)CONFIG_DEBUG_UART_BASE;
259 * We copy the code from above because it is already horribly messy.
260 * Trying to refactor to nicely remove the duplication doesn't seem
261 * feasible. The better fix is to move all users of this driver to
264 baud_divisor = ns16550_calc_divisor(com_port, CONFIG_DEBUG_UART_CLOCK,
266 serial_dout(&com_port->ier, CONFIG_SYS_NS16550_IER);
267 serial_dout(&com_port->mcr, UART_MCRVAL);
268 serial_dout(&com_port->fcr, UART_FCR_DEFVAL);
270 serial_dout(&com_port->lcr, UART_LCR_BKSE | UART_LCRVAL);
271 serial_dout(&com_port->dll, baud_divisor & 0xff);
272 serial_dout(&com_port->dlm, (baud_divisor >> 8) & 0xff);
273 serial_dout(&com_port->lcr, UART_LCRVAL);
276 static inline int NS16550_read_baud_divisor(struct NS16550 *com_port)
280 serial_dout(&com_port->lcr, UART_LCR_BKSE | UART_LCRVAL);
281 ret = serial_din(&com_port->dll) & 0xff;
282 ret |= (serial_din(&com_port->dlm) & 0xff) << 8;
283 serial_dout(&com_port->lcr, UART_LCRVAL);
288 static inline void _debug_uart_putc(int ch)
290 struct NS16550 *com_port = (struct NS16550 *)CONFIG_DEBUG_UART_BASE;
292 while (!(serial_din(&com_port->lsr) & UART_LSR_THRE)) {
293 #ifdef CONFIG_DEBUG_UART_NS16550_CHECK_ENABLED
294 if (!NS16550_read_baud_divisor(com_port))
298 serial_dout(&com_port->thr, ch);
305 #if CONFIG_IS_ENABLED(DM_SERIAL)
306 static int ns16550_serial_putc(struct udevice *dev, const char ch)
308 struct NS16550 *const com_port = dev_get_priv(dev);
310 if (!(serial_in(&com_port->lsr) & UART_LSR_THRE))
312 serial_out(ch, &com_port->thr);
315 * Call watchdog_reset() upon newline. This is done here in putc
316 * since the environment code uses a single puts() to print the complete
317 * environment upon "printenv". So we can't put this watchdog call
326 static int ns16550_serial_pending(struct udevice *dev, bool input)
328 struct NS16550 *const com_port = dev_get_priv(dev);
331 return (serial_in(&com_port->lsr) & UART_LSR_DR) ? 1 : 0;
333 return (serial_in(&com_port->lsr) & UART_LSR_THRE) ? 0 : 1;
336 static int ns16550_serial_getc(struct udevice *dev)
338 struct NS16550 *const com_port = dev_get_priv(dev);
340 if (!(serial_in(&com_port->lsr) & UART_LSR_DR))
343 return serial_in(&com_port->rbr);
346 static int ns16550_serial_setbrg(struct udevice *dev, int baudrate)
348 struct NS16550 *const com_port = dev_get_priv(dev);
349 struct ns16550_platdata *plat = com_port->plat;
352 clock_divisor = ns16550_calc_divisor(com_port, plat->clock, baudrate);
354 NS16550_setbrg(com_port, clock_divisor);
359 static int ns16550_serial_setconfig(struct udevice *dev, uint serial_config)
361 struct NS16550 *const com_port = dev_get_priv(dev);
362 int lcr_val = UART_LCR_WLS_8;
363 uint parity = SERIAL_GET_PARITY(serial_config);
364 uint bits = SERIAL_GET_BITS(serial_config);
365 uint stop = SERIAL_GET_STOP(serial_config);
368 * only parity config is implemented, check if other serial settings
369 * are the default one.
371 if (bits != SERIAL_8_BITS || stop != SERIAL_ONE_STOP)
372 return -ENOTSUPP; /* not supported in driver*/
375 case SERIAL_PAR_NONE:
379 lcr_val |= UART_LCR_PEN;
381 case SERIAL_PAR_EVEN:
382 lcr_val |= UART_LCR_PEN | UART_LCR_EPS;
385 return -ENOTSUPP; /* not supported in driver*/
388 serial_out(lcr_val, &com_port->lcr);
392 static int ns16550_serial_getinfo(struct udevice *dev,
393 struct serial_device_info *info)
395 struct NS16550 *const com_port = dev_get_priv(dev);
396 struct ns16550_platdata *plat = com_port->plat;
398 info->type = SERIAL_CHIP_16550_COMPATIBLE;
399 #ifdef CONFIG_SYS_NS16550_PORT_MAPPED
400 info->addr_space = SERIAL_ADDRESS_SPACE_IO;
402 info->addr_space = SERIAL_ADDRESS_SPACE_MEMORY;
404 info->addr = plat->base;
405 info->reg_width = plat->reg_width;
406 info->reg_shift = plat->reg_shift;
407 info->reg_offset = plat->reg_offset;
411 int ns16550_serial_probe(struct udevice *dev)
413 struct NS16550 *const com_port = dev_get_priv(dev);
414 struct reset_ctl_bulk reset_bulk;
417 ret = reset_get_bulk(dev, &reset_bulk);
419 reset_deassert_bulk(&reset_bulk);
421 com_port->plat = dev_get_platdata(dev);
422 NS16550_init(com_port, -1);
427 #if CONFIG_IS_ENABLED(OF_CONTROL)
434 #if CONFIG_IS_ENABLED(OF_CONTROL) && !CONFIG_IS_ENABLED(OF_PLATDATA)
435 int ns16550_serial_ofdata_to_platdata(struct udevice *dev)
437 struct ns16550_platdata *plat = dev->platdata;
438 const u32 port_type = dev_get_driver_data(dev);
443 /* try Processor Local Bus device first */
444 addr = dev_read_addr_pci(dev);
445 if (addr == FDT_ADDR_T_NONE)
448 #ifdef CONFIG_SYS_NS16550_PORT_MAPPED
451 plat->base = (unsigned long)map_physmem(addr, 0, MAP_NOCACHE);
454 plat->reg_offset = dev_read_u32_default(dev, "reg-offset", 0);
455 plat->reg_shift = dev_read_u32_default(dev, "reg-shift", 0);
456 plat->reg_width = dev_read_u32_default(dev, "reg-io-width", 1);
458 err = clk_get_by_index(dev, 0, &clk);
460 err = clk_get_rate(&clk);
461 if (!IS_ERR_VALUE(err))
463 } else if (err != -ENOENT && err != -ENODEV && err != -ENOSYS) {
464 debug("ns16550 failed to get clock\n");
469 plat->clock = dev_read_u32_default(dev, "clock-frequency",
470 CONFIG_SYS_NS16550_CLK);
472 debug("ns16550 clock not defined\n");
476 plat->fcr = UART_FCR_DEFVAL;
477 if (port_type == PORT_JZ4780)
478 plat->fcr |= UART_FCR_UME;
484 const struct dm_serial_ops ns16550_serial_ops = {
485 .putc = ns16550_serial_putc,
486 .pending = ns16550_serial_pending,
487 .getc = ns16550_serial_getc,
488 .setbrg = ns16550_serial_setbrg,
489 .setconfig = ns16550_serial_setconfig,
490 .getinfo = ns16550_serial_getinfo,
493 #if CONFIG_IS_ENABLED(OF_CONTROL) && !CONFIG_IS_ENABLED(OF_PLATDATA)
495 * Please consider existing compatible strings before adding a new
496 * one to keep this table compact. Or you may add a generic "ns16550"
497 * compatible string to your dts.
499 static const struct udevice_id ns16550_serial_ids[] = {
500 { .compatible = "ns16550", .data = PORT_NS16550 },
501 { .compatible = "ns16550a", .data = PORT_NS16550 },
502 { .compatible = "ingenic,jz4780-uart", .data = PORT_JZ4780 },
503 { .compatible = "nvidia,tegra20-uart", .data = PORT_NS16550 },
504 { .compatible = "snps,dw-apb-uart", .data = PORT_NS16550 },
507 #endif /* OF_CONTROL && !OF_PLATDATA */
509 #if CONFIG_IS_ENABLED(SERIAL_PRESENT)
511 /* TODO(sjg@chromium.org): Integrate this into a macro like CONFIG_IS_ENABLED */
512 #if !defined(CONFIG_TPL_BUILD) || defined(CONFIG_TPL_DM_SERIAL)
513 U_BOOT_DRIVER(ns16550_serial) = {
514 .name = "ns16550_serial",
516 #if CONFIG_IS_ENABLED(OF_CONTROL) && !CONFIG_IS_ENABLED(OF_PLATDATA)
517 .of_match = ns16550_serial_ids,
518 .ofdata_to_platdata = ns16550_serial_ofdata_to_platdata,
519 .platdata_auto_alloc_size = sizeof(struct ns16550_platdata),
521 .priv_auto_alloc_size = sizeof(struct NS16550),
522 .probe = ns16550_serial_probe,
523 .ops = &ns16550_serial_ops,
524 #if !CONFIG_IS_ENABLED(OF_CONTROL)
525 .flags = DM_FLAG_PRE_RELOC,
529 #endif /* SERIAL_PRESENT */
531 #endif /* CONFIG_DM_SERIAL */