blob: 8f3e4dd44f15352447ba1f695d3024d7c747bfac [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>
Simon Glass401d1c42020-10-30 21:38:53 -060013#include <asm/global_data.h>
Alexey Brodkin22a240c2013-12-13 10:35:11 +040014
15DECLARE_GLOBAL_DATA_PTR;
16
17struct arc_serial_regs {
18 unsigned int id0;
19 unsigned int id1;
20 unsigned int id2;
21 unsigned int id3;
22 unsigned int data;
23 unsigned int status;
24 unsigned int baudl;
25 unsigned int baudh;
26};
27
Alexey Brodkin01496c42015-03-17 14:55:14 +030028
Simon Glass8a8d24b2020-12-03 16:55:23 -070029struct arc_serial_plat {
Alexey Brodkin01496c42015-03-17 14:55:14 +030030 struct arc_serial_regs *reg;
31 unsigned int uartclk;
32};
33
Alexey Brodkin22a240c2013-12-13 10:35:11 +040034/* Bit definitions of STATUS register */
35#define UART_RXEMPTY (1 << 5)
36#define UART_OVERFLOW_ERR (1 << 1)
37#define UART_TXEMPTY (1 << 7)
38
Alexey Brodkin01496c42015-03-17 14:55:14 +030039static int arc_serial_setbrg(struct udevice *dev, int baudrate)
Alexey Brodkin22a240c2013-12-13 10:35:11 +040040{
Simon Glass0fd3d912020-12-22 19:30:28 -070041 struct arc_serial_plat *plat = dev_get_plat(dev);
Alexey Brodkin01496c42015-03-17 14:55:14 +030042 struct arc_serial_regs *const regs = plat->reg;
43 int arc_console_baud = gd->cpu_clk / (baudrate * 4) - 1;
Alexey Brodkin22a240c2013-12-13 10:35:11 +040044
Alexey Brodkin94b54002014-02-08 10:10:02 +040045 writeb(arc_console_baud & 0xff, &regs->baudl);
Alexey Brodkin94b54002014-02-08 10:10:02 +040046 writeb((arc_console_baud & 0xff00) >> 8, &regs->baudh);
Alexey Brodkin22a240c2013-12-13 10:35:11 +040047
Alexey Brodkin22a240c2013-12-13 10:35:11 +040048 return 0;
49}
50
Alexey Brodkin01496c42015-03-17 14:55:14 +030051static int arc_serial_putc(struct udevice *dev, const char c)
Alexey Brodkin22a240c2013-12-13 10:35:11 +040052{
Simon Glass0fd3d912020-12-22 19:30:28 -070053 struct arc_serial_plat *plat = dev_get_plat(dev);
Alexey Brodkin01496c42015-03-17 14:55:14 +030054 struct arc_serial_regs *const regs = plat->reg;
55
Alexey Brodkin94b54002014-02-08 10:10:02 +040056 while (!(readb(&regs->status) & UART_TXEMPTY))
Alexey Brodkin22a240c2013-12-13 10:35:11 +040057 ;
58
Alexey Brodkin94b54002014-02-08 10:10:02 +040059 writeb(c, &regs->data);
Alexey Brodkin01496c42015-03-17 14:55:14 +030060
61 return 0;
Alexey Brodkin22a240c2013-12-13 10:35:11 +040062}
63
Alexey Brodkin01496c42015-03-17 14:55:14 +030064static int arc_serial_tstc(struct arc_serial_regs *const regs)
Alexey Brodkin22a240c2013-12-13 10:35:11 +040065{
Alexey Brodkin94b54002014-02-08 10:10:02 +040066 return !(readb(&regs->status) & UART_RXEMPTY);
Alexey Brodkin22a240c2013-12-13 10:35:11 +040067}
68
Alexey Brodkin01496c42015-03-17 14:55:14 +030069static int arc_serial_pending(struct udevice *dev, bool input)
Alexey Brodkin22a240c2013-12-13 10:35:11 +040070{
Simon Glass0fd3d912020-12-22 19:30:28 -070071 struct arc_serial_plat *plat = dev_get_plat(dev);
Alexey Brodkin01496c42015-03-17 14:55:14 +030072 struct arc_serial_regs *const regs = plat->reg;
73 uint32_t status = readb(&regs->status);
74
75 if (input)
76 return status & UART_RXEMPTY ? 0 : 1;
77 else
78 return status & UART_TXEMPTY ? 0 : 1;
79}
80
81static int arc_serial_getc(struct udevice *dev)
82{
Simon Glass0fd3d912020-12-22 19:30:28 -070083 struct arc_serial_plat *plat = dev_get_plat(dev);
Alexey Brodkin01496c42015-03-17 14:55:14 +030084 struct arc_serial_regs *const regs = plat->reg;
85
86 while (!arc_serial_tstc(regs))
Alexey Brodkin22a240c2013-12-13 10:35:11 +040087 ;
88
89 /* Check for overflow errors */
Alexey Brodkin94b54002014-02-08 10:10:02 +040090 if (readb(&regs->status) & UART_OVERFLOW_ERR)
Alexey Brodkin22a240c2013-12-13 10:35:11 +040091 return 0;
92
Alexey Brodkin94b54002014-02-08 10:10:02 +040093 return readb(&regs->data) & 0xFF;
Alexey Brodkin22a240c2013-12-13 10:35:11 +040094}
95
Alexey Brodkin01496c42015-03-17 14:55:14 +030096static int arc_serial_probe(struct udevice *dev)
97{
98 return 0;
99}
100
101static 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,
Alexey Brodkin22a240c2013-12-13 10:35:11 +0400106};
107
Alexey Brodkin01496c42015-03-17 14:55:14 +0300108static const struct udevice_id arc_serial_ids[] = {
109 { .compatible = "snps,arc-uart" },
110 { }
111};
112
Simon Glassd1998a92020-12-03 16:55:21 -0700113static int arc_serial_of_to_plat(struct udevice *dev)
Alexey Brodkin22a240c2013-12-13 10:35:11 +0400114{
Simon Glass8a8d24b2020-12-03 16:55:23 -0700115 struct arc_serial_plat *plat = dev_get_plat(dev);
Alexey Brodkin01496c42015-03-17 14:55:14 +0300116 DECLARE_GLOBAL_DATA_PTR;
117
Masahiro Yamada8613c8d2020-07-17 14:36:46 +0900118 plat->reg = dev_read_addr_ptr(dev);
Simon Glasse160f7d2017-01-17 16:52:55 -0700119 plat->uartclk = fdtdec_get_int(gd->fdt_blob, dev_of_offset(dev),
Alexey Brodkin01496c42015-03-17 14:55:14 +0300120 "clock-frequency", 0);
121
122 return 0;
Alexey Brodkin22a240c2013-12-13 10:35:11 +0400123}
124
Alexey Brodkin01496c42015-03-17 14:55:14 +0300125U_BOOT_DRIVER(serial_arc) = {
126 .name = "serial_arc",
127 .id = UCLASS_SERIAL,
128 .of_match = arc_serial_ids,
Simon Glassd1998a92020-12-03 16:55:21 -0700129 .of_to_plat = arc_serial_of_to_plat,
Simon Glass8a8d24b2020-12-03 16:55:23 -0700130 .plat_auto = sizeof(struct arc_serial_plat),
Alexey Brodkin01496c42015-03-17 14:55:14 +0300131 .probe = arc_serial_probe,
132 .ops = &arc_serial_ops,
Alexey Brodkin01496c42015-03-17 14:55:14 +0300133};
Alexey Brodkin54705012018-05-21 16:42:07 +0300134
135#ifdef CONFIG_DEBUG_ARC_SERIAL
136#include <debug_uart.h>
137
138static inline void _debug_uart_init(void)
139{
140 struct arc_serial_regs *regs = (struct arc_serial_regs *)CONFIG_DEBUG_UART_BASE;
141 int arc_console_baud = CONFIG_DEBUG_UART_CLOCK / (CONFIG_BAUDRATE * 4) - 1;
142
143 writeb(arc_console_baud & 0xff, &regs->baudl);
144 writeb((arc_console_baud & 0xff00) >> 8, &regs->baudh);
145}
146
147static inline void _debug_uart_putc(int c)
148{
149 struct arc_serial_regs *regs = (struct arc_serial_regs *)CONFIG_DEBUG_UART_BASE;
150
151 while (!(readb(&regs->status) & UART_TXEMPTY))
152 ;
153
154 writeb(c, &regs->data);
155}
156
157DEBUG_UART_FUNCS
158
159#endif