blob: c2fc8a901e257b34c62c3baebed277ef00ad23d3 [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>
Simon Glass3ba929a2020-10-30 21:38:53 -060013#include <asm/global_data.h>
Alexey Brodkinc51eb112013-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 Brodkine458cc12015-03-17 14:55:14 +030028
Simon Glassb75b15b2020-12-03 16:55:23 -070029struct arc_serial_plat {
Alexey Brodkine458cc12015-03-17 14:55:14 +030030 struct arc_serial_regs *reg;
31 unsigned int uartclk;
32};
33
Alexey Brodkinc51eb112013-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 Brodkine458cc12015-03-17 14:55:14 +030039static int arc_serial_setbrg(struct udevice *dev, int baudrate)
Alexey Brodkinc51eb112013-12-13 10:35:11 +040040{
Simon Glass95588622020-12-22 19:30:28 -070041 struct arc_serial_plat *plat = dev_get_plat(dev);
Alexey Brodkine458cc12015-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 Brodkinc51eb112013-12-13 10:35:11 +040044
Alexey Brodkin326d8372014-02-08 10:10:02 +040045 writeb(arc_console_baud & 0xff, &regs->baudl);
Alexey Brodkin326d8372014-02-08 10:10:02 +040046 writeb((arc_console_baud & 0xff00) >> 8, &regs->baudh);
Alexey Brodkinc51eb112013-12-13 10:35:11 +040047
Alexey Brodkinc51eb112013-12-13 10:35:11 +040048 return 0;
49}
50
Alexey Brodkine458cc12015-03-17 14:55:14 +030051static int arc_serial_putc(struct udevice *dev, const char c)
Alexey Brodkinc51eb112013-12-13 10:35:11 +040052{
Simon Glass95588622020-12-22 19:30:28 -070053 struct arc_serial_plat *plat = dev_get_plat(dev);
Alexey Brodkine458cc12015-03-17 14:55:14 +030054 struct arc_serial_regs *const regs = plat->reg;
55
Pali Rohár241f12d2022-12-11 00:31:21 +010056 if (!(readb(&regs->status) & UART_TXEMPTY))
57 return -EAGAIN;
Alexey Brodkinc51eb112013-12-13 10:35:11 +040058
Alexey Brodkin326d8372014-02-08 10:10:02 +040059 writeb(c, &regs->data);
Alexey Brodkine458cc12015-03-17 14:55:14 +030060
61 return 0;
Alexey Brodkinc51eb112013-12-13 10:35:11 +040062}
63
Alexey Brodkine458cc12015-03-17 14:55:14 +030064static int arc_serial_tstc(struct arc_serial_regs *const regs)
Alexey Brodkinc51eb112013-12-13 10:35:11 +040065{
Alexey Brodkin326d8372014-02-08 10:10:02 +040066 return !(readb(&regs->status) & UART_RXEMPTY);
Alexey Brodkinc51eb112013-12-13 10:35:11 +040067}
68
Alexey Brodkine458cc12015-03-17 14:55:14 +030069static int arc_serial_pending(struct udevice *dev, bool input)
70{
Simon Glass95588622020-12-22 19:30:28 -070071 struct arc_serial_plat *plat = dev_get_plat(dev);
Alexey Brodkine458cc12015-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)
Alexey Brodkinc51eb112013-12-13 10:35:11 +040082{
Simon Glass95588622020-12-22 19:30:28 -070083 struct arc_serial_plat *plat = dev_get_plat(dev);
Alexey Brodkine458cc12015-03-17 14:55:14 +030084 struct arc_serial_regs *const regs = plat->reg;
85
Pali Rohár241f12d2022-12-11 00:31:21 +010086 if (!arc_serial_tstc(regs))
87 return -EAGAIN;
Alexey Brodkinc51eb112013-12-13 10:35:11 +040088
89 /* Check for overflow errors */
Alexey Brodkin326d8372014-02-08 10:10:02 +040090 if (readb(&regs->status) & UART_OVERFLOW_ERR)
Alexey Brodkinc51eb112013-12-13 10:35:11 +040091 return 0;
92
Alexey Brodkin326d8372014-02-08 10:10:02 +040093 return readb(&regs->data) & 0xFF;
Alexey Brodkinc51eb112013-12-13 10:35:11 +040094}
95
Alexey Brodkine458cc12015-03-17 14:55:14 +030096static int arc_serial_probe(struct udevice *dev)
Alexey Brodkinc51eb112013-12-13 10:35:11 +040097{
Alexey Brodkine458cc12015-03-17 14:55:14 +030098 return 0;
Alexey Brodkinc51eb112013-12-13 10:35:11 +040099}
100
Alexey Brodkine458cc12015-03-17 14:55:14 +0300101static 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,
106};
107
108static const struct udevice_id arc_serial_ids[] = {
109 { .compatible = "snps,arc-uart" },
110 { }
111};
112
Simon Glassaad29ae2020-12-03 16:55:21 -0700113static int arc_serial_of_to_plat(struct udevice *dev)
Alexey Brodkinc51eb112013-12-13 10:35:11 +0400114{
Simon Glassb75b15b2020-12-03 16:55:23 -0700115 struct arc_serial_plat *plat = dev_get_plat(dev);
Alexey Brodkine458cc12015-03-17 14:55:14 +0300116 DECLARE_GLOBAL_DATA_PTR;
117
Masahiro Yamada1096ae12020-07-17 14:36:46 +0900118 plat->reg = dev_read_addr_ptr(dev);
Simon Glassdd79d6e2017-01-17 16:52:55 -0700119 plat->uartclk = fdtdec_get_int(gd->fdt_blob, dev_of_offset(dev),
Alexey Brodkine458cc12015-03-17 14:55:14 +0300120 "clock-frequency", 0);
121
122 return 0;
Alexey Brodkinc51eb112013-12-13 10:35:11 +0400123}
Alexey Brodkine458cc12015-03-17 14:55:14 +0300124
125U_BOOT_DRIVER(serial_arc) = {
126 .name = "serial_arc",
127 .id = UCLASS_SERIAL,
128 .of_match = arc_serial_ids,
Simon Glassaad29ae2020-12-03 16:55:21 -0700129 .of_to_plat = arc_serial_of_to_plat,
Simon Glassb75b15b2020-12-03 16:55:23 -0700130 .plat_auto = sizeof(struct arc_serial_plat),
Alexey Brodkine458cc12015-03-17 14:55:14 +0300131 .probe = arc_serial_probe,
132 .ops = &arc_serial_ops,
Alexey Brodkine458cc12015-03-17 14:55:14 +0300133};
Alexey Brodkineede1672018-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{
Pali Rohár8864b352022-05-27 22:15:24 +0200140 struct arc_serial_regs *regs = (struct arc_serial_regs *)CONFIG_VAL(DEBUG_UART_BASE);
Alexey Brodkineede1672018-05-21 16:42:07 +0300141 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{
Pali Rohár8864b352022-05-27 22:15:24 +0200149 struct arc_serial_regs *regs = (struct arc_serial_regs *)CONFIG_VAL(DEBUG_UART_BASE);
Alexey Brodkineede1672018-05-21 16:42:07 +0300150
151 while (!(readb(&regs->status) & UART_TXEMPTY))
152 ;
153
154 writeb(c, &regs->data);
155}
156
157DEBUG_UART_FUNCS
158
159#endif