2 * Copyright (C) 2013 Synopsys, Inc. (www.synopsys.com)
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License version 2 as
6 * published by the Free Software Foundation.
13 #include <asm/global_data.h>
15 DECLARE_GLOBAL_DATA_PTR;
17 struct arc_serial_regs {
29 struct arc_serial_plat {
30 struct arc_serial_regs *reg;
34 /* Bit definitions of STATUS register */
35 #define UART_RXEMPTY (1 << 5)
36 #define UART_OVERFLOW_ERR (1 << 1)
37 #define UART_TXEMPTY (1 << 7)
39 static int arc_serial_setbrg(struct udevice *dev, int baudrate)
41 struct arc_serial_plat *plat = dev_get_plat(dev);
42 struct arc_serial_regs *const regs = plat->reg;
43 int arc_console_baud = gd->cpu_clk / (baudrate * 4) - 1;
45 writeb(arc_console_baud & 0xff, ®s->baudl);
46 writeb((arc_console_baud & 0xff00) >> 8, ®s->baudh);
51 static int arc_serial_putc(struct udevice *dev, const char c)
53 struct arc_serial_plat *plat = dev_get_plat(dev);
54 struct arc_serial_regs *const regs = plat->reg;
56 if (!(readb(®s->status) & UART_TXEMPTY))
59 writeb(c, ®s->data);
64 static int arc_serial_tstc(struct arc_serial_regs *const regs)
66 return !(readb(®s->status) & UART_RXEMPTY);
69 static int arc_serial_pending(struct udevice *dev, bool input)
71 struct arc_serial_plat *plat = dev_get_plat(dev);
72 struct arc_serial_regs *const regs = plat->reg;
73 uint32_t status = readb(®s->status);
76 return status & UART_RXEMPTY ? 0 : 1;
78 return status & UART_TXEMPTY ? 0 : 1;
81 static int arc_serial_getc(struct udevice *dev)
83 struct arc_serial_plat *plat = dev_get_plat(dev);
84 struct arc_serial_regs *const regs = plat->reg;
86 if (!arc_serial_tstc(regs))
89 /* Check for overflow errors */
90 if (readb(®s->status) & UART_OVERFLOW_ERR)
93 return readb(®s->data) & 0xFF;
96 static int arc_serial_probe(struct udevice *dev)
101 static const struct dm_serial_ops arc_serial_ops = {
102 .putc = arc_serial_putc,
103 .pending = arc_serial_pending,
104 .getc = arc_serial_getc,
105 .setbrg = arc_serial_setbrg,
108 static const struct udevice_id arc_serial_ids[] = {
109 { .compatible = "snps,arc-uart" },
113 static int arc_serial_of_to_plat(struct udevice *dev)
115 struct arc_serial_plat *plat = dev_get_plat(dev);
116 DECLARE_GLOBAL_DATA_PTR;
118 plat->reg = dev_read_addr_ptr(dev);
119 plat->uartclk = fdtdec_get_int(gd->fdt_blob, dev_of_offset(dev),
120 "clock-frequency", 0);
125 U_BOOT_DRIVER(serial_arc) = {
126 .name = "serial_arc",
128 .of_match = arc_serial_ids,
129 .of_to_plat = arc_serial_of_to_plat,
130 .plat_auto = sizeof(struct arc_serial_plat),
131 .probe = arc_serial_probe,
132 .ops = &arc_serial_ops,
135 #ifdef CONFIG_DEBUG_ARC_SERIAL
136 #include <debug_uart.h>
138 static inline void _debug_uart_init(void)
140 struct arc_serial_regs *regs = (struct arc_serial_regs *)CONFIG_VAL(DEBUG_UART_BASE);
141 int arc_console_baud = CONFIG_DEBUG_UART_CLOCK / (CONFIG_BAUDRATE * 4) - 1;
143 writeb(arc_console_baud & 0xff, ®s->baudl);
144 writeb((arc_console_baud & 0xff00) >> 8, ®s->baudh);
147 static inline void _debug_uart_putc(int c)
149 struct arc_serial_regs *regs = (struct arc_serial_regs *)CONFIG_VAL(DEBUG_UART_BASE);
151 while (!(readb(®s->status) & UART_TXEMPTY))
154 writeb(c, ®s->data);