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.
14 DECLARE_GLOBAL_DATA_PTR;
16 struct arc_serial_regs {
28 struct arc_serial_platdata {
29 struct arc_serial_regs *reg;
33 /* Bit definitions of STATUS register */
34 #define UART_RXEMPTY (1 << 5)
35 #define UART_OVERFLOW_ERR (1 << 1)
36 #define UART_TXEMPTY (1 << 7)
38 static int arc_serial_setbrg(struct udevice *dev, int baudrate)
40 struct arc_serial_platdata *plat = dev->platdata;
41 struct arc_serial_regs *const regs = plat->reg;
42 int arc_console_baud = gd->cpu_clk / (baudrate * 4) - 1;
44 writeb(arc_console_baud & 0xff, ®s->baudl);
48 * UART ISS(Instruction Set simulator) emulation has a subtle bug:
49 * A existing value of Baudh = 0 is used as a indication to startup
50 * it's internal state machine.
51 * Thus if baudh is set to 0, 2 times, it chokes.
52 * This happens with BAUD=115200 and the formaula above
53 * Until that is fixed, when running on ISS, we will set baudh to !0
55 if (gd->arch.running_on_hw)
56 writeb((arc_console_baud & 0xff00) >> 8, ®s->baudh);
58 writeb(1, ®s->baudh);
60 writeb((arc_console_baud & 0xff00) >> 8, ®s->baudh);
66 static int arc_serial_putc(struct udevice *dev, const char c)
68 struct arc_serial_platdata *plat = dev->platdata;
69 struct arc_serial_regs *const regs = plat->reg;
71 while (!(readb(®s->status) & UART_TXEMPTY))
74 writeb(c, ®s->data);
79 static int arc_serial_tstc(struct arc_serial_regs *const regs)
81 return !(readb(®s->status) & UART_RXEMPTY);
84 static int arc_serial_pending(struct udevice *dev, bool input)
86 struct arc_serial_platdata *plat = dev->platdata;
87 struct arc_serial_regs *const regs = plat->reg;
88 uint32_t status = readb(®s->status);
91 return status & UART_RXEMPTY ? 0 : 1;
93 return status & UART_TXEMPTY ? 0 : 1;
96 static int arc_serial_getc(struct udevice *dev)
98 struct arc_serial_platdata *plat = dev->platdata;
99 struct arc_serial_regs *const regs = plat->reg;
101 while (!arc_serial_tstc(regs))
104 /* Check for overflow errors */
105 if (readb(®s->status) & UART_OVERFLOW_ERR)
108 return readb(®s->data) & 0xFF;
111 static int arc_serial_probe(struct udevice *dev)
116 static const struct dm_serial_ops arc_serial_ops = {
117 .putc = arc_serial_putc,
118 .pending = arc_serial_pending,
119 .getc = arc_serial_getc,
120 .setbrg = arc_serial_setbrg,
123 static const struct udevice_id arc_serial_ids[] = {
124 { .compatible = "snps,arc-uart" },
128 static int arc_serial_ofdata_to_platdata(struct udevice *dev)
130 struct arc_serial_platdata *plat = dev_get_platdata(dev);
131 DECLARE_GLOBAL_DATA_PTR;
133 plat->reg = (struct arc_serial_regs *)dev_get_addr(dev);
134 plat->uartclk = fdtdec_get_int(gd->fdt_blob, dev->of_offset,
135 "clock-frequency", 0);
140 U_BOOT_DRIVER(serial_arc) = {
141 .name = "serial_arc",
143 .of_match = arc_serial_ids,
144 .ofdata_to_platdata = arc_serial_ofdata_to_platdata,
145 .probe = arc_serial_probe,
146 .ops = &arc_serial_ops,
147 .flags = DM_FLAG_PRE_RELOC,