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
346
347
348
349
350
351
352
353
354
355
356
357
|
/*
* QEMU model of the SiFive SPI Controller
*
* Copyright (c) 2021 Wind River Systems, Inc.
*
* Author:
* Bin Meng <bin.meng@windriver.com>
*
* This program is free software; you can redistribute it and/or modify it
* under the terms and conditions of the GNU General Public License,
* version 2 or later, as published by the Free Software Foundation.
*
* This program is distributed in the hope it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
* more details.
*
* You should have received a copy of the GNU General Public License along with
* this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "qemu/osdep.h"
#include "hw/irq.h"
#include "hw/qdev-properties.h"
#include "hw/sysbus.h"
#include "hw/ssi/ssi.h"
#include "qemu/fifo8.h"
#include "qemu/log.h"
#include "qemu/module.h"
#include "hw/ssi/sifive_spi.h"
#define R_SCKDIV (0x00 / 4)
#define R_SCKMODE (0x04 / 4)
#define R_CSID (0x10 / 4)
#define R_CSDEF (0x14 / 4)
#define R_CSMODE (0x18 / 4)
#define R_DELAY0 (0x28 / 4)
#define R_DELAY1 (0x2C / 4)
#define R_FMT (0x40 / 4)
#define R_TXDATA (0x48 / 4)
#define R_RXDATA (0x4C / 4)
#define R_TXMARK (0x50 / 4)
#define R_RXMARK (0x54 / 4)
#define R_FCTRL (0x60 / 4)
#define R_FFMT (0x64 / 4)
#define R_IE (0x70 / 4)
#define R_IP (0x74 / 4)
#define FMT_DIR (1 << 3)
#define TXDATA_FULL (1 << 31)
#define RXDATA_EMPTY (1 << 31)
#define IE_TXWM (1 << 0)
#define IE_RXWM (1 << 1)
#define IP_TXWM (1 << 0)
#define IP_RXWM (1 << 1)
#define FIFO_CAPACITY 8
static void sifive_spi_txfifo_reset(SiFiveSPIState *s)
{
fifo8_reset(&s->tx_fifo);
s->regs[R_TXDATA] &= ~TXDATA_FULL;
s->regs[R_IP] &= ~IP_TXWM;
}
static void sifive_spi_rxfifo_reset(SiFiveSPIState *s)
{
fifo8_reset(&s->rx_fifo);
s->regs[R_RXDATA] |= RXDATA_EMPTY;
s->regs[R_IP] &= ~IP_RXWM;
}
static void sifive_spi_update_cs(SiFiveSPIState *s)
{
int i;
for (i = 0; i < s->num_cs; i++) {
if (s->regs[R_CSDEF] & (1 << i)) {
qemu_set_irq(s->cs_lines[i], !(s->regs[R_CSMODE]));
}
}
}
static void sifive_spi_update_irq(SiFiveSPIState *s)
{
int level;
if (fifo8_num_used(&s->tx_fifo) < s->regs[R_TXMARK]) {
s->regs[R_IP] |= IP_TXWM;
} else {
s->regs[R_IP] &= ~IP_TXWM;
}
if (fifo8_num_used(&s->rx_fifo) > s->regs[R_RXMARK]) {
s->regs[R_IP] |= IP_RXWM;
} else {
s->regs[R_IP] &= ~IP_RXWM;
}
level = s->regs[R_IP] & s->regs[R_IE] ? 1 : 0;
qemu_set_irq(s->irq, level);
}
static void sifive_spi_reset(DeviceState *d)
{
SiFiveSPIState *s = SIFIVE_SPI(d);
memset(s->regs, 0, sizeof(s->regs));
/* The reset value is high for all implemented CS pins */
s->regs[R_CSDEF] = (1 << s->num_cs) - 1;
/* Populate register with their default value */
s->regs[R_SCKDIV] = 0x03;
s->regs[R_DELAY0] = 0x1001;
s->regs[R_DELAY1] = 0x01;
sifive_spi_txfifo_reset(s);
sifive_spi_rxfifo_reset(s);
sifive_spi_update_cs(s);
sifive_spi_update_irq(s);
}
static void sifive_spi_flush_txfifo(SiFiveSPIState *s)
{
uint8_t tx;
uint8_t rx;
while (!fifo8_is_empty(&s->tx_fifo)) {
tx = fifo8_pop(&s->tx_fifo);
rx = ssi_transfer(s->spi, tx);
if (!fifo8_is_full(&s->rx_fifo)) {
if (!(s->regs[R_FMT] & FMT_DIR)) {
fifo8_push(&s->rx_fifo, rx);
}
}
}
}
static bool sifive_spi_is_bad_reg(hwaddr addr, bool allow_reserved)
{
bool bad;
switch (addr) {
/* reserved offsets */
case 0x08:
case 0x0C:
case 0x1C:
case 0x20:
case 0x24:
case 0x30:
case 0x34:
case 0x38:
case 0x3C:
case 0x44:
case 0x58:
case 0x5C:
case 0x68:
case 0x6C:
bad = allow_reserved ? false : true;
break;
default:
bad = false;
}
if (addr >= (SIFIVE_SPI_REG_NUM << 2)) {
bad = true;
}
return bad;
}
static uint64_t sifive_spi_read(void *opaque, hwaddr addr, unsigned int size)
{
SiFiveSPIState *s = opaque;
uint32_t r;
if (sifive_spi_is_bad_reg(addr, true)) {
qemu_log_mask(LOG_GUEST_ERROR, "%s: bad read at address 0x%"
HWADDR_PRIx "\n", __func__, addr);
return 0;
}
addr >>= 2;
switch (addr) {
case R_TXDATA:
if (fifo8_is_full(&s->tx_fifo)) {
return TXDATA_FULL;
}
r = 0;
break;
case R_RXDATA:
if (fifo8_is_empty(&s->rx_fifo)) {
return RXDATA_EMPTY;
}
r = fifo8_pop(&s->rx_fifo);
break;
default:
r = s->regs[addr];
break;
}
sifive_spi_update_irq(s);
return r;
}
static void sifive_spi_write(void *opaque, hwaddr addr,
uint64_t val64, unsigned int size)
{
SiFiveSPIState *s = opaque;
uint32_t value = val64;
if (sifive_spi_is_bad_reg(addr, false)) {
qemu_log_mask(LOG_GUEST_ERROR, "%s: bad write at addr=0x%"
HWADDR_PRIx " value=0x%x\n", __func__, addr, value);
return;
}
addr >>= 2;
switch (addr) {
case R_CSID:
if (value >= s->num_cs) {
qemu_log_mask(LOG_GUEST_ERROR, "%s: invalid csid %d\n",
__func__, value);
} else {
s->regs[R_CSID] = value;
sifive_spi_update_cs(s);
}
break;
case R_CSDEF:
if (value >= (1 << s->num_cs)) {
qemu_log_mask(LOG_GUEST_ERROR, "%s: invalid csdef %x\n",
__func__, value);
} else {
s->regs[R_CSDEF] = value;
}
break;
case R_CSMODE:
if (value > 3) {
qemu_log_mask(LOG_GUEST_ERROR, "%s: invalid csmode %x\n",
__func__, value);
} else {
s->regs[R_CSMODE] = value;
sifive_spi_update_cs(s);
}
break;
case R_TXDATA:
if (!fifo8_is_full(&s->tx_fifo)) {
fifo8_push(&s->tx_fifo, (uint8_t)value);
sifive_spi_flush_txfifo(s);
}
break;
case R_RXDATA:
case R_IP:
qemu_log_mask(LOG_GUEST_ERROR,
"%s: invalid write to read-only reigster 0x%"
HWADDR_PRIx " with 0x%x\n", __func__, addr << 2, value);
break;
case R_TXMARK:
case R_RXMARK:
if (value >= FIFO_CAPACITY) {
qemu_log_mask(LOG_GUEST_ERROR, "%s: invalid watermark %d\n",
__func__, value);
} else {
s->regs[addr] = value;
}
break;
case R_FCTRL:
case R_FFMT:
qemu_log_mask(LOG_UNIMP,
"%s: direct-map flash interface unimplemented\n",
__func__);
break;
default:
s->regs[addr] = value;
break;
}
sifive_spi_update_irq(s);
}
static const MemoryRegionOps sifive_spi_ops = {
.read = sifive_spi_read,
.write = sifive_spi_write,
.endianness = DEVICE_LITTLE_ENDIAN,
.valid = {
.min_access_size = 4,
.max_access_size = 4
}
};
static void sifive_spi_realize(DeviceState *dev, Error **errp)
{
SysBusDevice *sbd = SYS_BUS_DEVICE(dev);
SiFiveSPIState *s = SIFIVE_SPI(dev);
int i;
s->spi = ssi_create_bus(dev, "spi");
sysbus_init_irq(sbd, &s->irq);
s->cs_lines = g_new0(qemu_irq, s->num_cs);
for (i = 0; i < s->num_cs; i++) {
sysbus_init_irq(sbd, &s->cs_lines[i]);
}
memory_region_init_io(&s->mmio, OBJECT(s), &sifive_spi_ops, s,
TYPE_SIFIVE_SPI, 0x1000);
sysbus_init_mmio(sbd, &s->mmio);
fifo8_create(&s->tx_fifo, FIFO_CAPACITY);
fifo8_create(&s->rx_fifo, FIFO_CAPACITY);
}
static Property sifive_spi_properties[] = {
DEFINE_PROP_UINT32("num-cs", SiFiveSPIState, num_cs, 1),
DEFINE_PROP_END_OF_LIST(),
};
static void sifive_spi_class_init(ObjectClass *klass, void *data)
{
DeviceClass *dc = DEVICE_CLASS(klass);
device_class_set_props(dc, sifive_spi_properties);
dc->reset = sifive_spi_reset;
dc->realize = sifive_spi_realize;
}
static const TypeInfo sifive_spi_info = {
.name = TYPE_SIFIVE_SPI,
.parent = TYPE_SYS_BUS_DEVICE,
.instance_size = sizeof(SiFiveSPIState),
.class_init = sifive_spi_class_init,
};
static void sifive_spi_register_types(void)
{
type_register_static(&sifive_spi_info);
}
type_init(sifive_spi_register_types)
|