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
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
|
/*
* StarFive JH7100 Serial Port driver
* This driver ported from NS16550 Serial Port driver:
* drivers/serial/ns16550.c
* (which is originally from linux source (arch/powerpc/boot/ns16550.c))
*
* Modified for StarFive JH7100 SoC by
* Wei Fu <wefu@redhat.com>
*
* SPDX-License-Identifier: GPL-2.0+
*/
#include <common.h>
#include <clk.h>
#include <debug_uart.h>
#include <dm.h>
#include <errno.h>
#include <fdtdec.h>
#include <log.h>
#include <watchdog.h>
#include <asm/io.h>
#include <linux/compiler.h>
#include <serial.h>
#include <linux/err.h>
#define UART_REG(x) u32 x
typedef struct uart_starfive {
UART_REG(rbr); /* 0 */
UART_REG(ier); /* 1 */
UART_REG(fcr); /* 2 */
UART_REG(lcr); /* 3 */
UART_REG(mcr); /* 4 */
UART_REG(lsr); /* 5 */
UART_REG(msr); /* 6 */
UART_REG(spr); /* 7 */
} uart_starfive_t;
struct starfive_uart_platdata {
unsigned long clock;
struct uart_starfive *regs;
};
#define thr rbr
#define iir fcr
#define dll rbr
#define dlh ier
DECLARE_GLOBAL_DATA_PTR;
/* TXCTRL register */
#define UART_TXEN 0x1
#define UART_TXWM(x) (((x) & 0xffff) << 16)
/* RXCTRL register */
#define UART_RXEN 0x1
#define UART_RXWM(x) (((x) & 0xffff) << 16)
/* IP register */
#define UART_IP_TXWM 0x1
#define UART_IP_RXWM 0x2
#define UART_TXFIFO_FULL 0x80000000
#define UART_RXFIFO_EMPTY 0x80000000
#define SER_RBR 0x00
#define SER_THR 0x00
#define SER_DLL 0x00
#define SER_DLH 0x04
#define SER_IER 0x04
#define SER_IIR 0x08
#define SER_FCR 0x08
#define SER_LCR 0x0C
#define SER_MCR 0x10
#define SER_LSR 0x14
#define SER_MSR 0x18
#define SER_SCR 0x1C
//UART_LCR bit define
#define LCR_WORD_LEN5 (0 << 0)
#define LCR_WORD_LEN6 BIT(0)
#define LCR_WORD_LEN7 (2 << 0)
#define LCR_WORD_LEN8 (3 << 0)
#define LCR_STOP_1BIT (0 << 2)
#define LCR_STOP_2BIT BIT(2)
#define LCR_PE BIT(3)
#define LCR_EPS BIT(4)
#define LCR_SP BIT(5)
#define LCR_BC BIT(6)
#define LCR_DLAB BIT(7)
//IER, interrupt enable reg
#define IER_ERBFI BIT(0) //rx interrupt enable
#define IER_ETBEI BIT(1) //tx interrupt enable
#define IER_ELSI BIT(2)
#define IER_EDSSI BIT(3)
#define IER_PTIME BIT(7)
//IIR, interrupt identify reg
#define INTR_ID_RLSI 0x06
#define INTR_ID_DATAVL 0x04
#define INTR_ID_TIMEOUT 0x0C
#define INTR_ID_THR 0x02
//FCR
#define RCVR_TRIG_1C (0x00 << 0x06) //1 char
#define RCVR_TRIG_QFL (0x01 << 0x06) //1/4 FIFO
#define RCVR_TRIG_HFL (0x02 << 0x06) //1/2 FIFO
#define RCVR_TRIG_LTF (0x03 << 0x06) //2 less than full
#define TXEM_TRIG_EMT (0x00 << 0x04) //FIFO empty
#define TXEM_TRIG_2C (0x00 << 0x04) //2 char
#define TXEM_TRIG_QFL (0x02 << 0x04) //1/4 FIFO
#define TXEM_TRIG_HFL (0x03 << 0x04) //1/2 FIFO
#define DMA_MOD1 (0x01 << 0x03)
#define DMA_MOD0 (0x00 << 0x03)
#define TXFIFO_RST BIT(2)
#define RXFIFO_RST BIT(1)
#define FIFO_ENA BIT(0)
//LSR
#define DATA_RDY BIT(0) //at least one byte available
#define RCV_OVERUN BIT(1)
#define ERR_PARITY BIT(2)
#define ERR_FRAMING BIT(3)
#define ID_BREAK BIT(4) //break interrupt bit
#define ID_THRE BIT(5) //in FIFO THRE mode, it is the xmit fifo full flag !!!
#define ID_TEMPT BIT(6)
#define ERR_RPE BIT(7)
/* MCR */
#define MCR_DTR BIT(0) /* dtr output */
#define MCR_RTS BIT(1) /* rts output */
#define MCR_OUT1 BIT(2) /* output #1 */
#define MCR_OUT2 BIT(3) /* output #2 */
#define MCR_LOOP BIT(4) /* loop back */
#define MCR_AFCE BIT(5) /* auto flow control enable */
static void _ser_clrrxtmo(struct uart_starfive *regs)
{
u32 reg32_val;
while(1) {
reg32_val = readl(®s->iir);
if((reg32_val & INTR_ID_TIMEOUT) != INTR_ID_TIMEOUT)
break;
reg32_val = readl(®s->rbr);
}
}
/* Set up the baud rate in gd struct */
static void _starfive_serial_setbrg(struct uart_starfive *regs,
unsigned long clock, unsigned long baud)
{
u64 baud_value = ((clock / baud) >> 4);
writel((baud_value & 0xFF), ®s->dll);
writel(((baud_value >> 8) & 0xFF), ®s->dlh);
}
static void _starfive_serial_init(struct uart_starfive *regs)
{
u32 datab, stopb, par, mcr;
u8 databits = 8;
u8 stopbits = 1;
u8 parity = 0;
u8 flow_ctl = 0;
if (databits >= 5 && databits <= 8)
datab = (databits - 5);
else
return;
if (stopbits == 1)
stopb = LCR_STOP_1BIT;
else if (stopbits == 2)
stopb = LCR_STOP_2BIT;
else
return;
par = (parity == 0) ? 0 : (parity == 1) ? LCR_PE : (LCR_PE | LCR_EPS);
writel(LCR_DLAB, ®s->lcr);
writel(datab | stopb | par, ®s->lcr);
//enable fifo and reset fifo, 1-byte int trig!
writel((RCVR_TRIG_1C | TXEM_TRIG_EMT | TXFIFO_RST | RXFIFO_RST |
FIFO_ENA), ®s->fcr);
mcr = (flow_ctl == 0) ? 0 : MCR_RTS | MCR_AFCE;
writel(mcr, ®s->mcr);
//dis the ser interrupt
writel(0, ®s->ier);
_ser_clrrxtmo(regs);
}
static int _starfive_serial_putc(struct uart_starfive *regs, const char c)
{
if (readl(®s->lsr) & ID_TEMPT) {
writel(c, ®s->thr);
return 0;
}
return -EAGAIN;
}
static int _starfive_serial_getc(struct uart_starfive *regs)
{
/* Wait here until the the FIFO is not full */
if (readl(®s->lsr) & DATA_RDY)
return readl(®s->rbr);
return -EAGAIN;
}
static int starfive_serial_setbrg(struct udevice *dev, int baudrate)
{
int ret;
u32 clock = 0;
struct clk clk;
struct starfive_uart_platdata *platdata = dev_get_plat(dev);
ret = clk_get_by_index(dev, 0, &clk);
if (IS_ERR_VALUE(ret)) {
debug("StarFive UART failed to get clock\n");
ret = dev_read_u32(dev, "clock-frequency", &clock);
if (IS_ERR_VALUE(ret)) {
debug("StarFive UART clock not defined\n");
return 0;
}
} else {
clock = clk_get_rate(&clk);
if (IS_ERR_VALUE(clock)) {
debug("StarFive UART clock get rate failed\n");
return 0;
}
}
platdata->clock = clock;
_starfive_serial_setbrg(platdata->regs, platdata->clock, baudrate);
return 0;
}
static int starfive_serial_probe(struct udevice *dev)
{
struct starfive_uart_platdata *platdata = dev_get_plat(dev);
/* No need to reinitialize the UART after relocation */
if (gd->flags & GD_FLG_RELOC)
return 0;
_starfive_serial_init(platdata->regs);
return 0;
}
static int starfive_serial_getc(struct udevice *dev)
{
int c;
struct starfive_uart_platdata *platdata = dev_get_plat(dev);
struct uart_starfive *regs = platdata->regs;
while ((c = _starfive_serial_getc(regs)) == -EAGAIN) ;
return c;
}
static int starfive_serial_putc(struct udevice *dev, const char ch)
{
int rc;
struct starfive_uart_platdata *platdata = dev_get_plat(dev);
while ((rc = _starfive_serial_putc(platdata->regs, ch)) == -EAGAIN) ;
return rc;
}
static int starfive_serial_pending(struct udevice *dev, bool input)
{
struct starfive_uart_platdata *platdata = dev_get_plat(dev);
struct uart_starfive *regs = platdata->regs;
if (input)
return (readl(®s->lsr) & DATA_RDY) ? 1 : 0;
else
return (readl(®s->lsr) & ID_THRE) ? 0 : 1;
}
static int starfive_serial_of_to_plat(struct udevice *dev)
{
struct starfive_uart_platdata *platdata = dev_get_plat(dev);
platdata->regs = (struct uart_starfive *)dev_read_addr(dev);
if (IS_ERR(platdata->regs))
return PTR_ERR(platdata->regs);
return 0;
}
static const struct dm_serial_ops starfive_serial_ops = {
.putc = starfive_serial_putc,
.getc = starfive_serial_getc,
.pending = starfive_serial_pending,
.setbrg = starfive_serial_setbrg,
};
static const struct udevice_id starfive_serial_ids[] = {
{ .compatible = "starfive,uart0" },
{ }
};
U_BOOT_DRIVER(serial_starfive) = {
.name = "serial_starfive",
.id = UCLASS_SERIAL,
.of_match = starfive_serial_ids,
.of_to_plat = starfive_serial_of_to_plat,
.plat_auto = sizeof(struct starfive_uart_platdata),
.probe = starfive_serial_probe,
.ops = &starfive_serial_ops,
};
#ifdef CONFIG_DEBUG_UART_STARFIVE
static inline void _debug_uart_init(void)
{
struct uart_starfive *regs =
(struct uart_starfive *)CONFIG_DEBUG_UART_BASE;
_starfive_serial_setbrg(regs, CONFIG_DEBUG_UART_CLOCK,
CONFIG_BAUDRATE);
_starfive_serial_init(regs);
}
static inline void _debug_uart_putc(int ch)
{
struct uart_starfive *regs =
(struct uart_starfive *)CONFIG_DEBUG_UART_BASE;
while (_starfive_serial_putc(regs, ch) == -EAGAIN)
WATCHDOG_RESET();
}
DEBUG_UART_FUNCS
#endif
|