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);
45 writeb((arc_console_baud & 0xff00) >> 8, ®s->baudh);
50 static int arc_serial_putc(struct udevice *dev, const char c)
52 struct arc_serial_platdata *plat = dev->platdata;
53 struct arc_serial_regs *const regs = plat->reg;
55 while (!(readb(®s->status) & UART_TXEMPTY))
58 writeb(c, ®s->data);
63 static int arc_serial_tstc(struct arc_serial_regs *const regs)
65 return !(readb(®s->status) & UART_RXEMPTY);
68 static int arc_serial_pending(struct udevice *dev, bool input)
70 struct arc_serial_platdata *plat = dev->platdata;
71 struct arc_serial_regs *const regs = plat->reg;
72 uint32_t status = readb(®s->status);
75 return status & UART_RXEMPTY ? 0 : 1;
77 return status & UART_TXEMPTY ? 0 : 1;
80 static int arc_serial_getc(struct udevice *dev)
82 struct arc_serial_platdata *plat = dev->platdata;
83 struct arc_serial_regs *const regs = plat->reg;
85 while (!arc_serial_tstc(regs))
88 /* Check for overflow errors */
89 if (readb(®s->status) & UART_OVERFLOW_ERR)
92 return readb(®s->data) & 0xFF;
95 static int arc_serial_probe(struct udevice *dev)
100 static const struct dm_serial_ops arc_serial_ops = {
101 .putc = arc_serial_putc,
102 .pending = arc_serial_pending,
103 .getc = arc_serial_getc,
104 .setbrg = arc_serial_setbrg,
107 static const struct udevice_id arc_serial_ids[] = {
108 { .compatible = "snps,arc-uart" },
112 static int arc_serial_ofdata_to_platdata(struct udevice *dev)
114 struct arc_serial_platdata *plat = dev_get_platdata(dev);
115 DECLARE_GLOBAL_DATA_PTR;
117 plat->reg = (struct arc_serial_regs *)dev_get_addr(dev);
118 plat->uartclk = fdtdec_get_int(gd->fdt_blob, dev->of_offset,
119 "clock-frequency", 0);
124 U_BOOT_DRIVER(serial_arc) = {
125 .name = "serial_arc",
127 .of_match = arc_serial_ids,
128 .ofdata_to_platdata = arc_serial_ofdata_to_platdata,
129 .probe = arc_serial_probe,
130 .ops = &arc_serial_ops,
131 .flags = DM_FLAG_PRE_RELOC,