blob: 70dbc6d6b53f50eaaceba28d47e528fa95363fac [file] [log] [blame]
Alexey Brodkinc51eb112013-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 Brodkine458cc12015-03-17 14:55:14 +030011#include <dm.h>
Alexey Brodkinc51eb112013-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 Brodkine458cc12015-03-17 14:55:14 +030027
28struct arc_serial_platdata {
29 struct arc_serial_regs *reg;
30 unsigned int uartclk;
31};
32
Alexey Brodkinc51eb112013-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 Brodkine458cc12015-03-17 14:55:14 +030038static int arc_serial_setbrg(struct udevice *dev, int baudrate)
Alexey Brodkinc51eb112013-12-13 10:35:11 +040039{
Alexey Brodkine458cc12015-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 Brodkinc51eb112013-12-13 10:35:11 +040043
Alexey Brodkin326d8372014-02-08 10:10:02 +040044 writeb(arc_console_baud & 0xff, &regs->baudl);
Alexey Brodkin326d8372014-02-08 10:10:02 +040045 writeb((arc_console_baud & 0xff00) >> 8, &regs->baudh);
Alexey Brodkinc51eb112013-12-13 10:35:11 +040046
Alexey Brodkinc51eb112013-12-13 10:35:11 +040047 return 0;
48}
49
Alexey Brodkine458cc12015-03-17 14:55:14 +030050static int arc_serial_putc(struct udevice *dev, const char c)
Alexey Brodkinc51eb112013-12-13 10:35:11 +040051{
Alexey Brodkine458cc12015-03-17 14:55:14 +030052 struct arc_serial_platdata *plat = dev->platdata;
53 struct arc_serial_regs *const regs = plat->reg;
54
Alexey Brodkin326d8372014-02-08 10:10:02 +040055 while (!(readb(&regs->status) & UART_TXEMPTY))
Alexey Brodkinc51eb112013-12-13 10:35:11 +040056 ;
57
Alexey Brodkin326d8372014-02-08 10:10:02 +040058 writeb(c, &regs->data);
Alexey Brodkine458cc12015-03-17 14:55:14 +030059
60 return 0;
Alexey Brodkinc51eb112013-12-13 10:35:11 +040061}
62
Alexey Brodkine458cc12015-03-17 14:55:14 +030063static int arc_serial_tstc(struct arc_serial_regs *const regs)
Alexey Brodkinc51eb112013-12-13 10:35:11 +040064{
Alexey Brodkin326d8372014-02-08 10:10:02 +040065 return !(readb(&regs->status) & UART_RXEMPTY);
Alexey Brodkinc51eb112013-12-13 10:35:11 +040066}
67
Alexey Brodkine458cc12015-03-17 14:55:14 +030068static int arc_serial_pending(struct udevice *dev, bool input)
69{
70 struct arc_serial_platdata *plat = dev->platdata;
71 struct arc_serial_regs *const regs = plat->reg;
72 uint32_t status = readb(&regs->status);
73
74 if (input)
75 return status & UART_RXEMPTY ? 0 : 1;
76 else
77 return status & UART_TXEMPTY ? 0 : 1;
78}
79
80static int arc_serial_getc(struct udevice *dev)
Alexey Brodkinc51eb112013-12-13 10:35:11 +040081{
Alexey Brodkine458cc12015-03-17 14:55:14 +030082 struct arc_serial_platdata *plat = dev->platdata;
83 struct arc_serial_regs *const regs = plat->reg;
84
85 while (!arc_serial_tstc(regs))
Alexey Brodkinc51eb112013-12-13 10:35:11 +040086 ;
87
88 /* Check for overflow errors */
Alexey Brodkin326d8372014-02-08 10:10:02 +040089 if (readb(&regs->status) & UART_OVERFLOW_ERR)
Alexey Brodkinc51eb112013-12-13 10:35:11 +040090 return 0;
91
Alexey Brodkin326d8372014-02-08 10:10:02 +040092 return readb(&regs->data) & 0xFF;
Alexey Brodkinc51eb112013-12-13 10:35:11 +040093}
94
Alexey Brodkine458cc12015-03-17 14:55:14 +030095static int arc_serial_probe(struct udevice *dev)
Alexey Brodkinc51eb112013-12-13 10:35:11 +040096{
Alexey Brodkine458cc12015-03-17 14:55:14 +030097 return 0;
Alexey Brodkinc51eb112013-12-13 10:35:11 +040098}
99
Alexey Brodkine458cc12015-03-17 14:55:14 +0300100static 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,
105};
106
107static const struct udevice_id arc_serial_ids[] = {
108 { .compatible = "snps,arc-uart" },
109 { }
110};
111
112static int arc_serial_ofdata_to_platdata(struct udevice *dev)
Alexey Brodkinc51eb112013-12-13 10:35:11 +0400113{
Alexey Brodkine458cc12015-03-17 14:55:14 +0300114 struct arc_serial_platdata *plat = dev_get_platdata(dev);
115 DECLARE_GLOBAL_DATA_PTR;
116
Simon Glassba1dea42017-05-17 17:18:05 -0600117 plat->reg = (struct arc_serial_regs *)devfdt_get_addr(dev);
Simon Glassdd79d6e2017-01-17 16:52:55 -0700118 plat->uartclk = fdtdec_get_int(gd->fdt_blob, dev_of_offset(dev),
Alexey Brodkine458cc12015-03-17 14:55:14 +0300119 "clock-frequency", 0);
120
121 return 0;
Alexey Brodkinc51eb112013-12-13 10:35:11 +0400122}
Alexey Brodkine458cc12015-03-17 14:55:14 +0300123
124U_BOOT_DRIVER(serial_arc) = {
125 .name = "serial_arc",
126 .id = UCLASS_SERIAL,
127 .of_match = arc_serial_ids,
128 .ofdata_to_platdata = arc_serial_ofdata_to_platdata,
Alexey Brodkin1232f0d2019-01-22 18:18:51 +0300129 .platdata_auto_alloc_size = sizeof(struct arc_serial_platdata),
Alexey Brodkine458cc12015-03-17 14:55:14 +0300130 .probe = arc_serial_probe,
131 .ops = &arc_serial_ops,
Alexey Brodkine458cc12015-03-17 14:55:14 +0300132};
Alexey Brodkineede1672018-05-21 16:42:07 +0300133
134#ifdef CONFIG_DEBUG_ARC_SERIAL
135#include <debug_uart.h>
136
137static inline void _debug_uart_init(void)
138{
139 struct arc_serial_regs *regs = (struct arc_serial_regs *)CONFIG_DEBUG_UART_BASE;
140 int arc_console_baud = CONFIG_DEBUG_UART_CLOCK / (CONFIG_BAUDRATE * 4) - 1;
141
142 writeb(arc_console_baud & 0xff, &regs->baudl);
143 writeb((arc_console_baud & 0xff00) >> 8, &regs->baudh);
144}
145
146static inline void _debug_uart_putc(int c)
147{
148 struct arc_serial_regs *regs = (struct arc_serial_regs *)CONFIG_DEBUG_UART_BASE;
149
150 while (!(readb(&regs->status) & UART_TXEMPTY))
151 ;
152
153 writeb(c, &regs->data);
154}
155
156DEBUG_UART_FUNCS
157
158#endif