-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy path0001-hw-ssi-allwinner-sun6i-spi-allwinner-sun6i-spi-suppo.patch
397 lines (394 loc) · 11.3 KB
/
0001-hw-ssi-allwinner-sun6i-spi-allwinner-sun6i-spi-suppo.patch
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
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
From 4884ce8218689bfe0f9560dbaad7ebc253314458 Mon Sep 17 00:00:00 2001
From: LuHui <[email protected]>
Date: Sun, 5 Mar 2023 21:24:55 +0800
Subject: [PATCH 1/4] hw/ssi/allwinner-sun6i-spi: allwinner sun6i spi support
Signed-off-by: LuHui <[email protected]>
---
hw/ssi/Kconfig | 4 +
hw/ssi/allwinner-sun6i-spi.c | 306 +++++++++++++++++++++++++++
hw/ssi/meson.build | 1 +
include/hw/ssi/allwinner-sun6i-spi.h | 40 ++++
4 files changed, 351 insertions(+)
create mode 100644 hw/ssi/allwinner-sun6i-spi.c
create mode 100644 include/hw/ssi/allwinner-sun6i-spi.h
diff --git a/hw/ssi/Kconfig b/hw/ssi/Kconfig
index 7d90a02181..d2d2e7cb15 100644
--- a/hw/ssi/Kconfig
+++ b/hw/ssi/Kconfig
@@ -20,3 +20,7 @@ config XILINX_SPIPS
config STM32F2XX_SPI
bool
select SSI
+
+config AW_SUN6I_SPI
+ bool
+ select SSI
diff --git a/hw/ssi/allwinner-sun6i-spi.c b/hw/ssi/allwinner-sun6i-spi.c
new file mode 100644
index 0000000000..cc18d7627f
--- /dev/null
+++ b/hw/ssi/allwinner-sun6i-spi.c
@@ -0,0 +1,306 @@
+/*
+ * allwinner sun6i spi control emulation.
+ *
+ * Copyright (C) 2023 Lu Hui <[email protected]>
+ *
+ * some code from ./pl022.c:
+ * Copyright (c) 2007 CodeSourcery.
+ * Written by Paul Brook
+ * some code from ./sifive_spi.c
+ * Copyright (c) 2021 Wind River Systems, Inc.
+ * Bin Meng <[email protected]>
+ * some code from mainline uboot:
+ * drivers/spi/spi-sunxi.c
+ *
+ * This code is licensed under the GPL.
+ */
+
+#include "qemu/osdep.h"
+#include "qemu/units.h"
+#include "hw/sysbus.h"
+#include "migration/vmstate.h"
+#include "hw/qdev-properties.h"
+#include "hw/qdev-properties-system.h"
+#include "hw/irq.h"
+#include "hw/ssi/allwinner-sun6i-spi.h"
+#include "hw/ssi/ssi.h"
+#include "qemu/log.h"
+#include "qemu/module.h"
+
+/* #define DEBUG_AW_SUN6I_SPI 1 */
+
+#ifdef DEBUG_AW_SUN6I_SPI
+#define DPRINTF(fmt, ...) \
+do { printf("aw_sun6i_spi: " fmt , ## __VA_ARGS__); } while (0)
+#else
+#define DPRINTF(fmt, ...) do {} while (0)
+#endif
+
+/* sun6i spi registers */
+#define SUN6I_GLB_CTL_REG 0x04
+#define SUN6I_TFR_CTL_REG 0x08
+#define SUN6I_INT_CTL_REG 0x10
+#define SUN6I_FIFO_CTL_REG 0x18
+#define SUN6I_FIFO_STA_REG 0x1c
+#define SUN6I_CLK_CTL_REG 0x24
+#define SUN6I_BURST_CNT_REG 0x30
+#define SUN6I_SEND_CNT_REG 0x34
+#define SUN6I_BURST_CTL_REG 0x38
+#define SUN6I_TXDATA_REG 0x200
+#define SUN6I_RXDATA_REG 0x300
+
+static void aw_sun6i_spi_update_ss(AwSun6iSpiState *s, int ss_active,
+ bool ss_level)
+{
+ if (ss_active == s->ss_active) {
+ if (s->ss_level == ss_level) {
+ return;
+ }
+ }
+ int i;
+ /* deassert all */
+ for (i = 0; i < 4; i++) {
+ qemu_irq_raise(s->ss_lines[i]);
+ }
+ s->ss_level = ss_level;
+ s->ss_active = ss_active;
+ if (s->ss_active >= 0 && s->ss_active < 4) {
+ if (s->ss_level) {
+ qemu_irq_raise(s->ss_lines[s->ss_active]);
+ DPRINTF("%s: deassert ss %d\n", __func__, s->ss_active);
+ } else {
+ qemu_irq_lower(s->ss_lines[s->ss_active]);
+ DPRINTF("%s: assert ss %d\n", __func__, s->ss_active);
+ }
+ }
+}
+
+static void aw_sun6i_spi_reset_tx_fifo(AwSun6iSpiState *s)
+{
+ DPRINTF("%s: tx fifo reset\n", __func__);
+ fifo8_reset(&s->tx_fifo);
+}
+
+static void aw_sun6i_spi_reset_rx_fifo(AwSun6iSpiState *s)
+{
+ DPRINTF("%s: rx fifo reset\n", __func__);
+ fifo8_reset(&s->rx_fifo);
+}
+
+static void aw_sun6i_spi_xfer(AwSun6iSpiState *s)
+{
+ if (s->start_burst == 0) {
+ return;
+ }
+ DPRINTF("%s: spi xfer start\n", __func__);
+ int i;
+ uint8_t rx;
+ uint8_t tx;
+ /*
+ * act a 'smart spi controller'
+ * because mainline uboot:
+ * arch/arm/mach-sunxi/spl_spi_sunxi.c
+ * doesnt do any assert or deassert cs line
+ * so, spi controller need do it.
+ */
+ if (s->send_bytes < s->burst_bytes) {
+ aw_sun6i_spi_update_ss(s, s->ss_active, 0);
+ }
+ for (i = 0; i < s->burst_bytes; i++) {
+ if (i >= s->send_bytes) {
+ DPRINTF("%s: fill dummy byte to tx fifo\n", __func__);
+ tx = 0xFF;
+ } else if (!fifo8_is_empty(&s->tx_fifo)) {
+ tx = fifo8_pop(&s->tx_fifo);
+ }
+ DPRINTF("%s: tx [%d] %02x\n", __func__, i, tx);
+ rx = ssi_transfer(s->spi, tx);
+ if (!fifo8_is_full(&s->rx_fifo)) {
+ fifo8_push(&s->rx_fifo, rx);
+ DPRINTF("%s: rx [%d] %02x \n", __func__, i, rx);
+ }
+ }
+ if (s->send_bytes < s->burst_bytes) {
+ aw_sun6i_spi_update_ss(s, s->ss_active, 1);
+ }
+ DPRINTF("%s: spi xfer end\n", __func__);
+ s->start_burst = 0;
+}
+
+static uint64_t aw_sun6i_spi_read(void *opaque, hwaddr offset,
+ unsigned size)
+{
+ AwSun6iSpiState *s = (AwSun6iSpiState *)opaque;
+ uint32_t val = 0x0;
+
+ unsigned sz;
+ switch (offset) {
+ case SUN6I_GLB_CTL_REG:
+ return 0x0;
+ case SUN6I_TFR_CTL_REG:
+ if (s->ss_active >= 0) {
+ val |= s->ss_active << 4;
+ }
+ val |= s->ss_level << 7;
+ val |= s->start_burst << 31;
+ return val;
+ case SUN6I_FIFO_CTL_REG:
+ return 0x0;
+ case SUN6I_BURST_CNT_REG:
+ val |= s->burst_bytes;
+ return val;
+ case SUN6I_SEND_CNT_REG:
+ val |= s->send_bytes;
+ return val;
+ case SUN6I_BURST_CTL_REG:
+ val |= s->send_bytes;
+ return val;
+ case SUN6I_FIFO_STA_REG:
+ val |= extract32(fifo8_num_used(&s->rx_fifo), 0, 7) << 0;
+ val |= extract32(fifo8_num_used(&s->tx_fifo), 0, 7) << 16;
+ return val;
+ case SUN6I_RXDATA_REG:
+ sz = 0;
+ for (sz = 0; sz < size; sz++) {
+ if (!fifo8_is_empty(&s->rx_fifo)) {
+ val |= fifo8_pop(&s->rx_fifo) << (sz * 8);
+ }
+ }
+ DPRINTF("%s: read rxfifo, data: %08x\n", __func__, val);
+ return val;
+ default:
+ return 0x0;
+ }
+}
+
+static void aw_sun6i_spi_do_reset(void *opaque) {
+ AwSun6iSpiState *s = (AwSun6iSpiState *)opaque;
+
+ DPRINTF("%s: reset spi controller\n", __func__);
+ s->ss_active = 0;
+ s->ss_level = 1;
+ aw_sun6i_spi_update_ss(s, 0, 1);
+ s->start_burst = 0;
+ s->burst_bytes = 0;
+ s->send_bytes = 0;
+ aw_sun6i_spi_reset_rx_fifo(s);
+ aw_sun6i_spi_reset_tx_fifo(s);
+}
+
+static void aw_sun6i_spi_write(void *opaque, hwaddr offset,
+ uint64_t value, unsigned size)
+{
+ AwSun6iSpiState *s = (AwSun6iSpiState *)opaque;
+
+ int ss_active;
+ bool ss_level;
+ switch (offset) {
+ case SUN6I_GLB_CTL_REG:
+ if (test_bit(31, (void *)&value)) {
+ aw_sun6i_spi_do_reset(s);
+ }
+ break;
+ case SUN6I_TFR_CTL_REG:
+ ss_active = extract32(value, 4, 2);
+ ss_level = test_bit(7, (void *)&value);
+ aw_sun6i_spi_update_ss(s, ss_active, ss_level);
+ s->start_burst = test_bit(31, (void *)&value);
+ aw_sun6i_spi_xfer(s);
+ break;
+ case SUN6I_FIFO_CTL_REG:
+ if (test_bit(32, (void *)&value)) {
+ aw_sun6i_spi_reset_tx_fifo(s);
+ }
+ if (test_bit(15, (void *)&value)) {
+ aw_sun6i_spi_reset_rx_fifo(s);
+ }
+ break;
+ case SUN6I_BURST_CNT_REG:
+ s->burst_bytes = value;
+ break;
+ case SUN6I_SEND_CNT_REG:
+ s->send_bytes = value;
+ break;
+ case SUN6I_BURST_CTL_REG:
+ s->send_bytes = value;
+ break;
+ case SUN6I_TXDATA_REG:
+ if (!fifo8_is_full(&s->tx_fifo)) {
+ fifo8_push(&s->tx_fifo, (uint8_t)value);
+ DPRINTF("%s: write txfifo, data: %02x\n", __func__, (uint8_t)value);
+ }
+ break;
+ default:
+ break;
+ }
+}
+
+static void aw_sun6i_spi_reset(DeviceState *dev)
+{
+ AwSun6iSpiState *s = AW_SUN6I_SPI(dev);
+
+ aw_sun6i_spi_do_reset(s);
+}
+
+static const MemoryRegionOps aw_sun6i_spi_ops = {
+ .read = aw_sun6i_spi_read,
+ .write = aw_sun6i_spi_write,
+ .endianness = DEVICE_NATIVE_ENDIAN,
+ .valid = {
+ .min_access_size = 1,
+ .max_access_size = 4,
+ },
+ .impl.min_access_size = 1,
+};
+
+static Property aw_sun6i_spi_properties[] = {
+ DEFINE_PROP_UINT8("fifo_depth", AwSun6iSpiState, fifo_depth, 64),
+ DEFINE_PROP_END_OF_LIST()
+};
+
+static const VMStateDescription vmstate_aw_sun6i_spi = {
+ .name = "aw_sun6i_spi",
+ .version_id = 1,
+ .minimum_version_id = 1,
+};
+
+static void aw_sun6i_spi_realize(DeviceState *dev, Error **errp)
+{
+ SysBusDevice *sbd = SYS_BUS_DEVICE(dev);
+ AwSun6iSpiState *s = AW_SUN6I_SPI(dev);
+
+ memory_region_init_io(&s->iomem, OBJECT(s), &aw_sun6i_spi_ops, s,
+ "aw_sun6i_spi", 4 * KiB);
+ int i;
+ for (i = 0; i < 4; i++) {
+ sysbus_init_irq(sbd, &s->ss_lines[i]);
+ }
+ sysbus_init_mmio(sbd, &s->iomem);
+ s->spi = ssi_create_bus(dev, "ssi");
+ fifo8_create(&s->tx_fifo, s->fifo_depth);
+ fifo8_create(&s->rx_fifo, s->fifo_depth);
+}
+
+static void aw_sun6i_spi_class_init(ObjectClass *klass, void *data)
+{
+ DeviceClass *dc = DEVICE_CLASS(klass);
+
+ dc->reset = aw_sun6i_spi_reset;
+ dc->vmsd = &vmstate_aw_sun6i_spi;
+ dc->realize = aw_sun6i_spi_realize;
+ device_class_set_props(dc, aw_sun6i_spi_properties);
+}
+
+static const TypeInfo aw_sun6i_spi_info = {
+ .name = TYPE_AW_SUN6I_SPI,
+ .parent = TYPE_SYS_BUS_DEVICE,
+ .instance_size = sizeof(AwSun6iSpiState),
+ .class_init = aw_sun6i_spi_class_init,
+};
+
+static void aw_sun6i_spi_register_types(void)
+{
+ type_register_static(&aw_sun6i_spi_info);
+}
+
+type_init(aw_sun6i_spi_register_types)
diff --git a/hw/ssi/meson.build b/hw/ssi/meson.build
index 904a47161a..31eb783240 100644
--- a/hw/ssi/meson.build
+++ b/hw/ssi/meson.build
@@ -11,3 +11,4 @@ softmmu_ss.add(when: 'CONFIG_XLNX_VERSAL', if_true: files('xlnx-versal-ospi.c'))
softmmu_ss.add(when: 'CONFIG_IMX', if_true: files('imx_spi.c'))
softmmu_ss.add(when: 'CONFIG_OMAP', if_true: files('omap_spi.c'))
softmmu_ss.add(when: 'CONFIG_IBEX', if_true: files('ibex_spi_host.c'))
+softmmu_ss.add(when: 'CONFIG_AW_SUN6I_SPI', if_true: files('allwinner-sun6i-spi.c'))
diff --git a/include/hw/ssi/allwinner-sun6i-spi.h b/include/hw/ssi/allwinner-sun6i-spi.h
new file mode 100644
index 0000000000..4edac250d3
--- /dev/null
+++ b/include/hw/ssi/allwinner-sun6i-spi.h
@@ -0,0 +1,40 @@
+/*
+ * Allwinner sun6i spi control emulation.
+ */
+
+#ifndef HW_SSI_AW_SUN6I_SPI_H
+#define HW_SSI_AW_SUN6I_SPI_H
+
+#include "qemu/fifo8.h"
+#include "hw/sysbus.h"
+#include "qom/object.h"
+
+#define TYPE_AW_SUN6I_SPI "aw-f1c100s-spi"
+OBJECT_DECLARE_SIMPLE_TYPE(AwSun6iSpiState, AW_SUN6I_SPI)
+
+struct AwSun6iSpiState {
+ SysBusDevice parent_obj;
+ MemoryRegion iomem;
+ SSIBus *spi;
+
+ int8_t ss_active;
+ bool ss_level;
+ qemu_irq ss_lines[4];
+
+ bool start_burst;
+ uint8_t burst_bytes;
+ uint8_t send_bytes;
+
+ bool irq_enable;
+ bool trans_complete;
+ bool txfifo_overflow;
+ bool rxfifo_overflow;
+ bool txfifo_empty;
+ bool rxfifo_ready;
+
+ Fifo8 tx_fifo;
+ Fifo8 rx_fifo;
+ uint8_t fifo_depth;
+};
+
+#endif
--
2.35.7