blob: 54e596c4ed624d9b952f7ebf3f06d607cd9d1c90 [file] [log] [blame]
Alexey Brodkin22a240c2013-12-13 10:35:11 +04001/*
2 * Copyright (C) 2013 Synopsys, Inc. (www.synopsys.com)
3 *
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.
7 *
8 */
9
10#include <common.h>
Alexey Brodkin01496c42015-03-17 14:55:14 +030011#include <dm.h>
Alexey Brodkin22a240c2013-12-13 10:35:11 +040012#include <serial.h>
13
14DECLARE_GLOBAL_DATA_PTR;
15
16struct arc_serial_regs {
17 unsigned int id0;
18 unsigned int id1;
19 unsigned int id2;
20 unsigned int id3;
21 unsigned int data;
22 unsigned int status;
23 unsigned int baudl;
24 unsigned int baudh;
25};
26
Alexey Brodkin01496c42015-03-17 14:55:14 +030027
28struct arc_serial_platdata {
29 struct arc_serial_regs *reg;
30 unsigned int uartclk;
31};
32
Alexey Brodkin22a240c2013-12-13 10:35:11 +040033/* Bit definitions of STATUS register */
34#define UART_RXEMPTY (1 << 5)
35#define UART_OVERFLOW_ERR (1 << 1)
36#define UART_TXEMPTY (1 << 7)
37
Alexey Brodkin01496c42015-03-17 14:55:14 +030038static int arc_serial_setbrg(struct udevice *dev, int baudrate)
Alexey Brodkin22a240c2013-12-13 10:35:11 +040039{
Alexey Brodkin01496c42015-03-17 14:55:14 +030040 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;
Alexey Brodkin22a240c2013-12-13 10:35:11 +040043
Alexey Brodkin94b54002014-02-08 10:10:02 +040044 writeb(arc_console_baud & 0xff, &regs->baudl);
Alexey Brodkin1d568c72014-02-08 10:10:01 +040045
46#ifdef CONFIG_ARC
47 /*
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
54 */
55 if (gd->arch.running_on_hw)
Alexey Brodkin94b54002014-02-08 10:10:02 +040056 writeb((arc_console_baud & 0xff00) >> 8, &regs->baudh);
Alexey Brodkin1d568c72014-02-08 10:10:01 +040057 else
Alexey Brodkin94b54002014-02-08 10:10:02 +040058 writeb(1, &regs->baudh);
Alexey Brodkin1d568c72014-02-08 10:10:01 +040059#else
Alexey Brodkin94b54002014-02-08 10:10:02 +040060 writeb((arc_console_baud & 0xff00) >> 8, &regs->baudh);
Alexey Brodkin1d568c72014-02-08 10:10:01 +040061#endif
Alexey Brodkin22a240c2013-12-13 10:35:11 +040062
Alexey Brodkin22a240c2013-12-13 10:35:11 +040063 return 0;
64}
65
Alexey Brodkin01496c42015-03-17 14:55:14 +030066static int arc_serial_putc(struct udevice *dev, const char c)
Alexey Brodkin22a240c2013-12-13 10:35:11 +040067{
Alexey Brodkin01496c42015-03-17 14:55:14 +030068 struct arc_serial_platdata *plat = dev->platdata;
69 struct arc_serial_regs *const regs = plat->reg;
70
Alexey Brodkin22a240c2013-12-13 10:35:11 +040071 if (c == '\n')
Alexey Brodkin01496c42015-03-17 14:55:14 +030072 arc_serial_putc(dev, '\r');
Alexey Brodkin22a240c2013-12-13 10:35:11 +040073
Alexey Brodkin94b54002014-02-08 10:10:02 +040074 while (!(readb(&regs->status) & UART_TXEMPTY))
Alexey Brodkin22a240c2013-12-13 10:35:11 +040075 ;
76
Alexey Brodkin94b54002014-02-08 10:10:02 +040077 writeb(c, &regs->data);
Alexey Brodkin01496c42015-03-17 14:55:14 +030078
79 return 0;
Alexey Brodkin22a240c2013-12-13 10:35:11 +040080}
81
Alexey Brodkin01496c42015-03-17 14:55:14 +030082static int arc_serial_tstc(struct arc_serial_regs *const regs)
Alexey Brodkin22a240c2013-12-13 10:35:11 +040083{
Alexey Brodkin94b54002014-02-08 10:10:02 +040084 return !(readb(&regs->status) & UART_RXEMPTY);
Alexey Brodkin22a240c2013-12-13 10:35:11 +040085}
86
Alexey Brodkin01496c42015-03-17 14:55:14 +030087static int arc_serial_pending(struct udevice *dev, bool input)
Alexey Brodkin22a240c2013-12-13 10:35:11 +040088{
Alexey Brodkin01496c42015-03-17 14:55:14 +030089 struct arc_serial_platdata *plat = dev->platdata;
90 struct arc_serial_regs *const regs = plat->reg;
91 uint32_t status = readb(&regs->status);
92
93 if (input)
94 return status & UART_RXEMPTY ? 0 : 1;
95 else
96 return status & UART_TXEMPTY ? 0 : 1;
97}
98
99static int arc_serial_getc(struct udevice *dev)
100{
101 struct arc_serial_platdata *plat = dev->platdata;
102 struct arc_serial_regs *const regs = plat->reg;
103
104 while (!arc_serial_tstc(regs))
Alexey Brodkin22a240c2013-12-13 10:35:11 +0400105 ;
106
107 /* Check for overflow errors */
Alexey Brodkin94b54002014-02-08 10:10:02 +0400108 if (readb(&regs->status) & UART_OVERFLOW_ERR)
Alexey Brodkin22a240c2013-12-13 10:35:11 +0400109 return 0;
110
Alexey Brodkin94b54002014-02-08 10:10:02 +0400111 return readb(&regs->data) & 0xFF;
Alexey Brodkin22a240c2013-12-13 10:35:11 +0400112}
113
Alexey Brodkin01496c42015-03-17 14:55:14 +0300114static int arc_serial_probe(struct udevice *dev)
115{
116 return 0;
117}
118
119static const struct dm_serial_ops arc_serial_ops = {
120 .putc = arc_serial_putc,
121 .pending = arc_serial_pending,
122 .getc = arc_serial_getc,
123 .setbrg = arc_serial_setbrg,
Alexey Brodkin22a240c2013-12-13 10:35:11 +0400124};
125
Alexey Brodkin01496c42015-03-17 14:55:14 +0300126static const struct udevice_id arc_serial_ids[] = {
127 { .compatible = "snps,arc-uart" },
128 { }
129};
130
131static int arc_serial_ofdata_to_platdata(struct udevice *dev)
Alexey Brodkin22a240c2013-12-13 10:35:11 +0400132{
Alexey Brodkin01496c42015-03-17 14:55:14 +0300133 struct arc_serial_platdata *plat = dev_get_platdata(dev);
134 DECLARE_GLOBAL_DATA_PTR;
135
136 plat->reg = (struct arc_serial_regs *)fdtdec_get_addr(gd->fdt_blob,
137 dev->of_offset, "reg");
138 plat->uartclk = fdtdec_get_int(gd->fdt_blob, dev->of_offset,
139 "clock-frequency", 0);
140
141 return 0;
Alexey Brodkin22a240c2013-12-13 10:35:11 +0400142}
143
Alexey Brodkin01496c42015-03-17 14:55:14 +0300144U_BOOT_DRIVER(serial_arc) = {
145 .name = "serial_arc",
146 .id = UCLASS_SERIAL,
147 .of_match = arc_serial_ids,
148 .ofdata_to_platdata = arc_serial_ofdata_to_platdata,
149 .probe = arc_serial_probe,
150 .ops = &arc_serial_ops,
151 .flags = DM_FLAG_PRE_RELOC,
152};