1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
|
// SPDX-License-Identifier: GPL-2.0
/*
* UART driver for MediaTek MT7620 and earlier SoCs
*
* Copyright (C) 2020 MediaTek Inc.
* Author: Weijie Gao <weijie.gao@mediatek.com>
*/
#include <clk.h>
#include <div64.h>
#include <dm.h>
#include <errno.h>
#include <log.h>
#include <reset.h>
#include <serial.h>
#include <watchdog.h>
#include <asm/io.h>
#include <asm/types.h>
#include <asm/addrspace.h>
#include <dm/device_compat.h>
#include <linux/err.h>
#if CONFIG_IS_ENABLED(OF_PLATDATA)
#include <dt-structs.h>
#endif
struct mt7620_serial_regs {
u32 rbr;
u32 thr;
u32 ier;
u32 iir;
u32 fcr;
u32 lcr;
u32 mcr;
u32 lsr;
u32 msr;
u32 scratch;
u32 dl;
u32 dll;
u32 dlm;
u32 ifctl;
};
#define UART_LCR_WLS_8 0x03 /* 8 bit character length */
#define UART_LSR_DR 0x01 /* Data ready */
#define UART_LSR_THRE 0x20 /* Xmit holding register empty */
#define UART_LSR_TEMT 0x40 /* Xmitter empty */
#define UART_MCR_DTR 0x01 /* DTR */
#define UART_MCR_RTS 0x02 /* RTS */
#define UART_FCR_FIFO_EN 0x01 /* Fifo enable */
#define UART_FCR_RXSR 0x02 /* Receiver soft reset */
#define UART_FCR_TXSR 0x04 /* Transmitter soft reset */
#define UART_MCRVAL (UART_MCR_DTR | \
UART_MCR_RTS)
/* Clear & enable FIFOs */
#define UART_FCRVAL (UART_FCR_FIFO_EN | \
UART_FCR_RXSR | \
UART_FCR_TXSR)
struct mt7620_serial_plat {
#if CONFIG_IS_ENABLED(OF_PLATDATA)
struct dtd_serial_mt7620 dtplat;
#endif
struct mt7620_serial_regs __iomem *regs;
u32 clock;
};
static void _mt7620_serial_setbrg(struct mt7620_serial_plat *plat, int baud)
{
u32 quot;
/* set divisor */
quot = DIV_ROUND_CLOSEST(plat->clock, 16 * baud);
writel(quot, &plat->regs->dl);
/* set character length and stop bits */
writel(UART_LCR_WLS_8, &plat->regs->lcr);
}
static int mt7620_serial_setbrg(struct udevice *dev, int baudrate)
{
struct mt7620_serial_plat *plat = dev_get_plat(dev);
_mt7620_serial_setbrg(plat, baudrate);
return 0;
}
static int mt7620_serial_putc(struct udevice *dev, const char ch)
{
struct mt7620_serial_plat *plat = dev_get_plat(dev);
if (!(readl(&plat->regs->lsr) & UART_LSR_THRE))
return -EAGAIN;
writel(ch, &plat->regs->thr);
if (ch == '\n')
schedule();
return 0;
}
static int mt7620_serial_getc(struct udevice *dev)
{
struct mt7620_serial_plat *plat = dev_get_plat(dev);
if (!(readl(&plat->regs->lsr) & UART_LSR_DR))
return -EAGAIN;
return readl(&plat->regs->rbr);
}
static int mt7620_serial_pending(struct udevice *dev, bool input)
{
struct mt7620_serial_plat *plat = dev_get_plat(dev);
if (input)
return (readl(&plat->regs->lsr) & UART_LSR_DR) ? 1 : 0;
return (readl(&plat->regs->lsr) & UART_LSR_THRE) ? 0 : 1;
}
static int mt7620_serial_probe(struct udevice *dev)
{
struct mt7620_serial_plat *plat = dev_get_plat(dev);
#if CONFIG_IS_ENABLED(OF_PLATDATA)
plat->regs = (void __iomem *)KSEG1ADDR(plat->dtplat.reg[0]);
plat->clock = plat->dtplat.clock_frequency;
#endif
/* Disable interrupt */
writel(0, &plat->regs->ier);
writel(UART_MCRVAL, &plat->regs->mcr);
writel(UART_FCRVAL, &plat->regs->fcr);
return 0;
}
#if CONFIG_IS_ENABLED(OF_REAL)
static int mt7620_serial_of_to_plat(struct udevice *dev)
{
struct mt7620_serial_plat *plat = dev_get_plat(dev);
struct reset_ctl reset_uart;
struct clk clk;
int err;
err = reset_get_by_index(dev, 0, &reset_uart);
if (!err)
reset_deassert(&reset_uart);
plat->regs = dev_remap_addr_index(dev, 0);
if (!plat->regs) {
dev_err(dev, "mt7620_serial: unable to map UART registers\n");
return -EINVAL;
}
err = clk_get_by_index(dev, 0, &clk);
if (!err) {
err = clk_get_rate(&clk);
if (!IS_ERR_VALUE(err))
plat->clock = err;
} else if (err != -ENOENT && err != -ENODEV && err != -ENOSYS) {
dev_err(dev, "mt7620_serial: failed to get clock\n");
return err;
}
if (!plat->clock)
plat->clock = dev_read_u32_default(dev, "clock-frequency", 0);
if (!plat->clock) {
dev_err(dev, "mt7620_serial: clock not defined\n");
return -EINVAL;
}
return 0;
}
static const struct udevice_id mt7620_serial_ids[] = {
{ .compatible = "mediatek,mt7620-uart" },
{ }
};
#endif
static const struct dm_serial_ops mt7620_serial_ops = {
.putc = mt7620_serial_putc,
.pending = mt7620_serial_pending,
.getc = mt7620_serial_getc,
.setbrg = mt7620_serial_setbrg,
};
U_BOOT_DRIVER(serial_mt7620) = {
.name = "serial_mt7620",
.id = UCLASS_SERIAL,
#if CONFIG_IS_ENABLED(OF_REAL)
.of_match = mt7620_serial_ids,
.of_to_plat = mt7620_serial_of_to_plat,
#endif
.plat_auto = sizeof(struct mt7620_serial_plat),
.probe = mt7620_serial_probe,
.ops = &mt7620_serial_ops,
.flags = DM_FLAG_PRE_RELOC,
};
DM_DRIVER_ALIAS(serial_mt7620, mediatek_mt7620_uart);
#ifdef CONFIG_DEBUG_UART_MT7620
#include <debug_uart.h>
static inline void _debug_uart_init(void)
{
struct mt7620_serial_plat plat;
plat.regs = (void *)CONFIG_VAL(DEBUG_UART_BASE);
plat.clock = CONFIG_DEBUG_UART_CLOCK;
writel(0, &plat.regs->ier);
writel(UART_MCRVAL, &plat.regs->mcr);
writel(UART_FCRVAL, &plat.regs->fcr);
_mt7620_serial_setbrg(&plat, CONFIG_BAUDRATE);
}
static inline void _debug_uart_putc(int ch)
{
struct mt7620_serial_regs __iomem *regs =
(void *)CONFIG_VAL(DEBUG_UART_BASE);
while (!(readl(®s->lsr) & UART_LSR_THRE))
;
writel(ch, ®s->thr);
}
DEBUG_UART_FUNCS
#endif
|