Skip to contentSkip to conversation

hw/ssi: Add DesignWare SSI standard PIO support for K230

PATCH Superseded Kangjie Huang 4 participants 13 messages Aug 1 – Aug 20

Subsystems (2)

v2 of 4 Superseded Latest is v4 →

lore
Share & tools
lore
#0 lore raw

v2 cover hw/ssi: Add DesignWare SSI standard PIO support for K230

Hello,

This series adds a reusable Synopsys DesignWare SSI controller model and uses it to model the three SSI instances on the Kendryte K230 machine.

This series supersedes the previous K230-specific SSI series:

https://lore.kernel.org/qemu-devel/[email protected]/

Following review of v1, the controller has been split into a reusable DesignWare SSI model and a K230 integration layer. The controller no longer depends on K230 types, addresses, or HI_SYS state. K230-specific instance profiles, MMIO mapping, PLIC routing, and flash attachment now live in the K230 machine code.

The v1 implementation combined DesignWare SSI behaviour and K230-specific SoC integration in one device. That made the reuse boundary unclear for other SoCs using the same IP. This revision makes that boundary explicit:
the generic model implements the documented controller behaviour, while the K230 machine supplies only its instance configuration and SoC connections.

The implemented controller scope is deliberately limited to Standard single-line SPI PIO: configurable chip selects and FIFO depth, four TMOD modes, FIFO and status handling, Standard interrupt state, and migration state for the implemented data path. Enhanced SPI, internal DMA/IDMA, and XIP transactions are deferred to follow-up series.

K230 has nine SSI interrupt source lines per instance. This series wires all nine lines to preserve the documented PLIC topology. TXU, DONE, and AXIE remain deasserted because their transfer engines are outside the Standard PIO scope modeled here.

The final patch adds an optional M25P80-compatible SPI NOR flash on logical spi0 CS0. It supports and tests Standard 1-1-1 accesses only; the enhanced SPI and IDMA boot paths are outside the scope of this series.

Changes since v1:

* Replace the K230-specific controller type with a reusable DesignWare SSI model configured through num-cs, fifo-depth, and imr-reset properties.
* Keep K230 instance profiles, MMIO mapping, PLIC routing, and flash attachment in the K230 machine code.
* Limit this revision to Standard PIO, FIFO, interrupt, and 1-1-1 SPI NOR support; defer enhanced SPI, internal DMA/IDMA, and XIP to follow-up series.

Testing on the current V2 development tree:

* riscv64-softmmu build succeeds.
* K230 SSI qtest: 14/14 pass, covering register/reset contracts, Standard PIO TMOD modes, FIFO behaviour, interrupt state, PLIC routing, Standard 1-1-1 flash identification and fixed-address reads, and unsupported-register semantics.
* U-Boot Standard SPI payload boot: U-Boot loads OpenSBI, Linux, initramfs, and the DTB from a W25Q256 flash and reaches a Linux initramfs shell. Standard sf probe, erase, write, readback, and compare also pass.
* Linux 5.10.4 Standard SPI PIO: dw_spi_mmio and spi-nor probe successfully; the MTD test partition completes erase, write, readback, and byte-wise comparison (K230_MTD_TEST_PASS mode=standard).

Kangjie Huang (5):
hw/ssi: Add Synopsys DesignWare SSI standard PIO controller hw/ssi: Add DesignWare SSI standard interrupt support hw/riscv/k230: Instantiate DesignWare SSI controllers hw/riscv/k230: Route SSI interrupts to the PLIC hw/riscv/k230: Attach a standard SPI NOR flash

 docs/system/riscv/k230.rst     |    2 +
 hw/riscv/Kconfig               |    2 +
 hw/riscv/k230.c                |  178 +++++-
 hw/ssi/Kconfig                 |    4 +
 hw/ssi/dw_ssi.c                | 1083 ++++++++++++++++++++++++++++++++
 hw/ssi/meson.build             |    1 +
 include/hw/riscv/k230.h        |    7 +
 include/hw/ssi/dw_ssi.h        |   78 +++
 tests/qtest/k230-dw-ssi-test.c |  569 +++++++++++++++++
 tests/qtest/meson.build        |    4 +-
 10 files changed, 1912 insertions(+), 16 deletions(-)
 create mode 100644 hw/ssi/dw_ssi.c
 create mode 100644 include/hw/ssi/dw_ssi.h
 create mode 100644 tests/qtest/k230-dw-ssi-test.c
Signature · 1 line
2.43.0
#1 lore raw

PATCH v2 1/5 hw/ssi: Add Synopsys DesignWare SSI standard PIO controller

Add a reusable SysBus model for the Synopsys DesignWare SSI controller.

Implement the Standard SPI register subset, configurable chip-select and FIFO resources, the four Standard PIO transfer modes, reset handling, chip-select GPIOs, and RAZ/WI handling for unsupported enhanced, DMA, and XIP registers.

Add num-cs, fifo-depth, and imr-reset properties. Save only Standard PIO state in VMState and reject migration between devices with different resource profiles.

The controller is not instantiated by this patch.

---
 hw/ssi/Kconfig          |   4 +
 hw/ssi/dw_ssi.c         | 956 ++++++++++++++++++++++++++++++++++++++++
 hw/ssi/meson.build      |   1 +
 include/hw/ssi/dw_ssi.h |  62 +++
 4 files changed, 1023 insertions(+)
 create mode 100644 hw/ssi/dw_ssi.c
 create mode 100644 include/hw/ssi/dw_ssi.h
hw/ssi/Kconfig +4 -0
index 1bd56463c1..c48cd2ff6d 100644
@@ -6,6 +6,10 @@ config SIFIVE_SPI
     bool
     select SSI
 
+config DW_SSI
+    bool
+    select SSI
+
 config SSI
     bool
 
hw/ssi/dw_ssi.c new file +956 -0

956 lines not on this page · raw message

hw/ssi/meson.build +1 -0
index 6afb1ea200..447780846c 100644
@@ -1,6 +1,7 @@
 system_ss.add(when: 'CONFIG_ALLWINNER_A10_SPI', if_true: files('allwinner-a10-spi.c'))
 system_ss.add(when: 'CONFIG_ASPEED_SOC', if_true: files('aspeed_smc.c'))
 system_ss.add(when: 'CONFIG_MSF2', if_true: files('mss-spi.c'))
+system_ss.add(when: 'CONFIG_DW_SSI', if_true: files('dw_ssi.c'))
 system_ss.add(when: 'CONFIG_NPCM7XX', if_true: files('npcm7xx_fiu.c', 'npcm_pspi.c'))
 system_ss.add(when: 'CONFIG_PL022', if_true: files('pl022.c'))
 system_ss.add(when: 'CONFIG_SIFIVE_SPI', if_true: files('sifive_spi.c'))
include/hw/ssi/dw_ssi.h new file +62 -0

62 lines not on this page · raw message

Signature · 1 line
2.43.0
#2 lore raw

Re: [PATCH v2 1/5] hw/ssi: Add Synopsys DesignWare SSI standard PIO controller

Quoted 44 lines · Kangjie Huang

Add a reusable SysBus model for the Synopsys DesignWare SSI controller.

Implement the Standard SPI register subset, configurable chip-select and FIFO resources, the four Standard PIO transfer modes, reset handling, chip-select GPIOs, and RAZ/WI handling for unsupported enhanced, DMA, and XIP registers.

Add num-cs, fifo-depth, and imr-reset properties. Save only Standard PIO state in VMState and reject migration between devices with different resource profiles.

The controller is not instantiated by this patch.

---
 hw/ssi/Kconfig          |   4 +
 hw/ssi/dw_ssi.c         | 956 ++++++++++++++++++++++++++++++++++++++++
 hw/ssi/meson.build      |   1 +
 include/hw/ssi/dw_ssi.h |  62 +++
 4 files changed, 1023 insertions(+)
 create mode 100644 hw/ssi/dw_ssi.c
 create mode 100644 include/hw/ssi/dw_ssi.h
hw/ssi/Kconfig +4 -0
index 1bd56463c1..c48cd2ff6d 100644
@@ -6,6 +6,10 @@ config SIFIVE_SPI
     bool
     select SSI
 
+config DW_SSI
+    bool
+    select SSI
+
 config SSI
     bool
 
hw/ssi/dw_ssi.c new file +0 -0
new file mode 100644
index 0000000000..f9f0e23730

hw/ssi/dw_ssi.c line 9

Quoted 9 lines
hw/ssi/dw_ssi.c new file +9 -0
@@ -0,0 +1,956 @@
+/*
+ * Synopsys DesignWare SSI
+ *
+ * Copyright (c) 2026 Kangjie Huang <[email protected]>
+ *
+ * SPDX-License-Identifier: GPL-2.0-or-later
+ *
+ * Emulates the DesignWare SSI controller in Standard SPI mode,
+ * covering the PIO/FIFO data path and chip selects.

Could you share a stable public link to the DesignWare SSI datasheet here?

Thanks,
Chao

Quoted 1,030 lines
hw/ssi/dw_ssi.c new file +947 -0
+ */
+
+#include "qemu/osdep.h"
+#include "hw/core/registerfields.h"
+#include "hw/core/qdev-properties.h"
+#include "hw/core/irq.h"
+#include "hw/ssi/dw_ssi.h"
+#include "migration/vmstate.h"
+#include "qapi/error.h"
+#include "qemu/bitops.h"
+#include "qemu/log.h"
+#include "qemu/module.h"
+
+/* DWC SSI 1.03 derivative (TRM v0.3.1 section 12.3). */
+#define DW_SSI_CTRLR0_RESET            0x00004007
+#define DW_SSI_SR_RESET                0x00000006
+#define DW_SSI_IDR_RESET               0xa1b2c3d5
+#define DW_SSI_VERSION                 0x3130332a
+#define DW_SSI_PIO_TX_BATCH            64
+
+enum {
+    DW_SSI_TMOD_TR,
+    DW_SSI_TMOD_TO,
+    DW_SSI_TMOD_RO,
+    DW_SSI_TMOD_EEPROM_READ,
+};
+
+REG32(CTRLR0, 0x000)
+    FIELD(CTRLR0, DFS, 0, 5)
+    FIELD(CTRLR0, FRF, 6, 2)
+    FIELD(CTRLR0, SCPH, 8, 1)
+    FIELD(CTRLR0, SCPOL, 9, 1)
+    FIELD(CTRLR0, TMOD, 10, 2)
+    FIELD(CTRLR0, SLV_OE, 12, 1)
+    FIELD(CTRLR0, SRL, 13, 1)
+    FIELD(CTRLR0, SSTE, 14, 1)
+    FIELD(CTRLR0, CFS, 16, 4)
+    FIELD(CTRLR0, SPI_FRF, 22, 2)
+    FIELD(CTRLR0, SPI_HYPERBUS_EN, 24, 1)
+    FIELD(CTRLR0, SPI_DWS_EN, 25, 1)
+REG32(CTRLR1, 0x004)
+    FIELD(CTRLR1, NDF, 0, 16)
+REG32(SSIENR, 0x008)
+    FIELD(SSIENR, SSIC_EN, 0, 1)
+REG32(MWCR, 0x00c)
+    FIELD(MWCR, MWMOD, 0, 1)
+    FIELD(MWCR, MDD, 1, 1)
+    FIELD(MWCR, MHS, 2, 1)
+REG32(SER, 0x010)
+REG32(BAUDR, 0x014)
+    FIELD(BAUDR, SCKDV, 1, 15)
+REG32(TXFTLR, 0x018)
+    FIELD(TXFTLR, TFT, 0, 8)
+    FIELD(TXFTLR, TXFTHR, 16, 11)
+REG32(RXFTLR, 0x01c)
+    FIELD(RXFTLR, RFT, 0, 8)
+REG32(TXFLR, 0x020)
+    FIELD(TXFLR, TXTFL, 0, 9)
+REG32(RXFLR, 0x024)
+    FIELD(RXFLR, RXTFL, 0, 9)
+REG32(SR, 0x028)
+    FIELD(SR, CMPLTD_DF, 15, 17)
+    FIELD(SR, DCOL, 6, 1)
+    FIELD(SR, TXE, 5, 1)
+    FIELD(SR, BUSY, 0, 1)
+    FIELD(SR, TFNF, 1, 1)
+    FIELD(SR, TFE, 2, 1)
+    FIELD(SR, RFNE, 3, 1)
+    FIELD(SR, RFF, 4, 1)
+REG32(IMR, 0x02c)
+    FIELD(IMR, DONEM, 11, 1)
+    FIELD(IMR, SPITEM, 10, 1)
+    FIELD(IMR, AXIEM, 8, 1)
+    FIELD(IMR, TXUIM, 7, 1)
+    FIELD(IMR, XRXOIM, 6, 1)
+    FIELD(IMR, MSTIM, 5, 1)
+    FIELD(IMR, RXOIM, 3, 1)
+    FIELD(IMR, RXUIM, 2, 1)
+    FIELD(IMR, TXOIM, 1, 1)
+    FIELD(IMR, TXEIM, 0, 1)
+    FIELD(IMR, RXFIM, 4, 1)
+REG32(ISR, 0x030)
+    FIELD(ISR, DONES, 11, 1)
+    FIELD(ISR, SPITES, 10, 1)
+    FIELD(ISR, AXIES, 8, 1)
+    FIELD(ISR, TXUIS, 7, 1)
+    FIELD(ISR, XRXOIS, 6, 1)
+    FIELD(ISR, MSTIS, 5, 1)
+    FIELD(ISR, RXOIS, 3, 1)
+    FIELD(ISR, RXUIS, 2, 1)
+    FIELD(ISR, TXOIS, 1, 1)
+    FIELD(ISR, TXEIS, 0, 1)
+    FIELD(ISR, RXFIS, 4, 1)
+REG32(RISR, 0x034)
+    FIELD(RISR, DONER, 11, 1)
+    FIELD(RISR, SPITER, 10, 1)
+    FIELD(RISR, AXIER, 8, 1)
+    FIELD(RISR, TXUIR, 7, 1)
+    FIELD(RISR, XRXOIR, 6, 1)
+    FIELD(RISR, MSTIR, 5, 1)
+    FIELD(RISR, RXOIR, 3, 1)
+    FIELD(RISR, RXUIR, 2, 1)
+    FIELD(RISR, TXOIR, 1, 1)
+    FIELD(RISR, TXEIR, 0, 1)
+    FIELD(RISR, RXFIR, 4, 1)
+REG32(TXEICR, 0x038)
+    FIELD(TXEICR, TXEICR, 0, 1)
+REG32(RXOICR, 0x03c)
+    FIELD(RXOICR, RXOICR, 0, 1)
+REG32(RXUICR, 0x040)
+    FIELD(RXUICR, RXUICR, 0, 1)
+REG32(MSTICR, 0x044)
+    FIELD(MSTICR, MSTICR, 0, 1)
+REG32(ICR, 0x048)
+    FIELD(ICR, ICR, 0, 1)
+REG32(DMACR, 0x04c)
+    FIELD(DMACR, RDMAE, 0, 1)
+    FIELD(DMACR, TDMAE, 1, 1)
+    FIELD(DMACR, IDMAE, 2, 1)
+    FIELD(DMACR, ATW, 3, 2)
+    FIELD(DMACR, AINC, 6, 1)
+    FIELD(DMACR, ACACHE, 8, 4)
+    FIELD(DMACR, APROT, 12, 3)
+    FIELD(DMACR, AID, 15, 4)
+REG32(AXIAWLEN, 0x050)
+    FIELD(AXIAWLEN, AWLEN, 8, 8)
+REG32(AXIARLEN, 0x054)
+    FIELD(AXIARLEN, ARLEN, 8, 8)
+REG32(IDR, 0x058)
+    FIELD(IDR, IDCODE, 0, 32)
+REG32(SSIC_VERSION_ID, 0x05c)
+    FIELD(SSIC_VERSION_ID, VERSION_ID, 0, 32)
+REG32(DR0, 0x060)
+REG32(DR_END, 0x0ec)
+REG32(RX_SAMPLE_DELAY, 0x0f0)
+    FIELD(RX_SAMPLE_DELAY, RSD, 0, 8)
+    FIELD(RX_SAMPLE_DELAY, SE, 16, 1)
+REG32(SPI_CTRLR0, 0x0f4)
+    FIELD(SPI_CTRLR0, CLK_STRETCH_EN, 30, 1)
+    FIELD(SPI_CTRLR0, XIP_PREFETCH_EN, 29, 1)
+    FIELD(SPI_CTRLR0, XIP_MBL, 26, 2)
+    FIELD(SPI_CTRLR0, SPI_RXDS_SIG_EN, 25, 1)
+    FIELD(SPI_CTRLR0, SPI_DM_EN, 24, 1)
+    FIELD(SPI_CTRLR0, SSIC_XIP_CONT_XFER_EN, 21, 1)
+    FIELD(SPI_CTRLR0, XIP_INST_EN, 20, 1)
+    FIELD(SPI_CTRLR0, XIP_DFS_HC, 19, 1)
+    FIELD(SPI_CTRLR0, SPI_RXDS_EN, 18, 1)
+    FIELD(SPI_CTRLR0, INST_DDR_EN, 17, 1)
+    FIELD(SPI_CTRLR0, SPI_DDR_EN, 16, 1)
+    FIELD(SPI_CTRLR0, WAIT_CYCLES, 11, 5)
+    FIELD(SPI_CTRLR0, INST_L, 8, 2)
+    FIELD(SPI_CTRLR0, XIP_MD_BIT_EN, 7, 1)
+    FIELD(SPI_CTRLR0, ADDR_L, 2, 4)
+    FIELD(SPI_CTRLR0, TRANS_TYPE, 0, 2)
+REG32(DDR_DRIVE_EDGE, 0x0f8)
+    FIELD(DDR_DRIVE_EDGE, TDE, 0, 8)
+REG32(XIP_MODE_BITS, 0x0fc)
+    FIELD(XIP_MODE_BITS, XIP_MD_BITS, 0, 16)
+REG32(XIP_INCR_INST, 0x100)
+    FIELD(XIP_INCR_INST, INCR_INST, 0, 16)
+REG32(XIP_WRAP_INST, 0x104)
+    FIELD(XIP_WRAP_INST, WRAP_INST, 0, 16)
+REG32(XIP_CTRL, 0x108)
+REG32(XIP_SER, 0x10c)
+REG32(XRXOICR, 0x110)
+REG32(XIP_CNT_TIME_OUT, 0x114)
+REG32(SPI_CTRLR1, 0x118)
+REG32(SPITECR, 0x11c)
+REG32(SPIDR, 0x120)
+    FIELD(SPIDR, SPI_INST, 0, 16)
+REG32(SPIAR, 0x124)
+    FIELD(SPIAR, SDAR, 0, 32)
+REG32(AXIAR0, 0x128)
+    FIELD(AXIAR0, AXIAR_0_31, 0, 32)
+REG32(AXIAR1, 0x12c)
+    FIELD(AXIAR1, AXIAR_32_63, 0, 32)
+REG32(AXIECR, 0x130)
+    FIELD(AXIECR, AXIECR, 0, 1)
+REG32(DONECR, 0x134)
+    FIELD(DONECR, DONECR, 0, 1)
+REG32(RSVD_138, 0x138)
+REG32(RSVD_13C, 0x13c)
+REG32(XIP_WRITE_INCR_INST, 0x140)
+REG32(XIP_WRITE_WRAP_INST, 0x144)
+REG32(XIP_WRITE_CTRL, 0x148)
+
+#define DW_SSI_CTRLR0_STANDARD_WRITABLE_MASK \
+    (R_CTRLR0_DFS_MASK | \
+     R_CTRLR0_SCPH_MASK | \
+     R_CTRLR0_SCPOL_MASK | \
+     R_CTRLR0_TMOD_MASK | \
+     R_CTRLR0_SLV_OE_MASK | \
+     R_CTRLR0_SRL_MASK | \
+     R_CTRLR0_SSTE_MASK | \
+     R_CTRLR0_CFS_MASK)
+
+#define DW_SSI_CTRLR1_WRITABLE_MASK R_CTRLR1_NDF_MASK
+#define DW_SSI_MWCR_WRITABLE_MASK \
+    (R_MWCR_MWMOD_MASK | R_MWCR_MDD_MASK)
+#define DW_SSI_BAUDR_WRITABLE_MASK R_BAUDR_SCKDV_MASK
+#define DW_SSI_TXFTLR_WRITABLE_MASK \
+    (R_TXFTLR_TFT_MASK | R_TXFTLR_TXFTHR_MASK)
+#define DW_SSI_RXFTLR_WRITABLE_MASK R_RXFTLR_RFT_MASK
+#define DW_SSI_IMR_WRITABLE_MASK \
+    (R_IMR_TXEIM_MASK | R_IMR_TXOIM_MASK | R_IMR_RXUIM_MASK | \
+     R_IMR_RXOIM_MASK | R_IMR_RXFIM_MASK | R_IMR_MSTIM_MASK | \
+     R_IMR_TXUIM_MASK)
+#define DW_SSI_RX_SAMPLE_DELAY_WRITABLE_MASK \
+    (R_RX_SAMPLE_DELAY_RSD_MASK | R_RX_SAMPLE_DELAY_SE_MASK)
+
+static bool dw_ssi_validate_config(DwSsiState *s, Error **errp)
+{
+    DeviceState *dev = DEVICE(s);
+
+    if (s->cfg.num_cs == 0 || s->cfg.num_cs > 8) {
+        error_setg(errp, "%s: num-cs must be in range 1..8",
+                   dev->canonical_path);
+        return false;
+    }
+
+    if (s->cfg.fifo_depth < 2 || s->cfg.fifo_depth > 256) {
+        error_setg(errp, "%s: fifo-depth must be in range 2..256",
+                   dev->canonical_path);
+        return false;
+    }
+
+    if (s->cfg.imr_reset & ~DW_SSI_IMR_WRITABLE_MASK) {
+        error_setg(errp, "%s: imr-reset contains unsupported bits",
+                   dev->canonical_path);
+        return false;
+    }
+
+    return true;
+}
+
+static bool dw_ssi_fifo_threshold_valid(const DwSsiState *s, uint32_t value)
+{
+    return value < s->cfg.fifo_depth;
+}
+
+static void dw_ssi_write_masked(DwSsiState *s, unsigned int reg,
+                                     uint32_t value, uint32_t mask)
+{
+    s->regs[reg] = (s->regs[reg] & ~mask) | (value & mask);
+}
+
+static uint32_t dw_ssi_frame_masked(DwSsiState *s)
+{
+    unsigned int bits = FIELD_EX32(s->regs[R_CTRLR0], CTRLR0, DFS) + 1;
+
+    return bits == 32 ? UINT32_MAX : MAKE_64BIT_MASK(0, bits);
+}
+
+static bool dw_ssi_is_enabled(const DwSsiState *s)
+{
+    return FIELD_EX32(s->regs[R_SSIENR], SSIENR, SSIC_EN);
+}
+
+static void dw_ssi_deselect(DwSsiState *s)
+{
+    if (s->active_cs < 0) {
+        return;
+    }
+
+    qemu_irq_raise(s->cs_lines[s->active_cs]);
+    s->active_cs = -1;
+}
+
+static void dw_ssi_select(DwSsiState *s, unsigned cs)
+{
+    if (cs >= s->cfg.num_cs) {
+        qemu_log_mask(LOG_GUEST_ERROR,
+                      "%s: invalid chip select %u\n",
+                      DEVICE(s)->canonical_path, cs);
+        dw_ssi_deselect(s);
+        return;
+    }
+
+    if (s->active_cs == cs) {
+        return;
+    }
+
+    dw_ssi_deselect(s);
+    qemu_irq_lower(s->cs_lines[cs]);
+    s->active_cs = cs;
+}
+
+static void dw_ssi_update_cs(DwSsiState *s)
+{
+    uint32_t ser = s->regs[R_SER];
+
+    if (!dw_ssi_is_enabled(s) || !ser) {
+        dw_ssi_deselect(s);
+        return;
+    }
+
+    if (ser & (ser - 1)) {
+        qemu_log_mask(LOG_GUEST_ERROR,
+                      "%s: multiple chip selects enabled: 0x%x\n",
+                      DEVICE(s)->canonical_path, ser);
+        dw_ssi_deselect(s);
+        return;
+    }
+
+    dw_ssi_select(s, ctz32(ser));
+}
+
+static void dw_ssi_abort_transfer(DwSsiState *s)
+{
+    dw_ssi_deselect(s);
+    fifo32_reset(&s->tx_fifo);
+    fifo32_reset(&s->rx_fifo);
+    s->phase = DW_SSI_PHASE_IDLE;
+    s->remaining_frames = 0;
+}
+
+static uint32_t dw_ssi_status(DwSsiState *s)
+{
+    uint32_t tx_used = fifo32_num_used(&s->tx_fifo);
+    uint32_t rx_used = fifo32_num_used(&s->rx_fifo);
+    uint32_t sr = 0;
+
+    sr = FIELD_DP32(sr, SR, BUSY, s->phase != DW_SSI_PHASE_IDLE);
+    sr = FIELD_DP32(sr, SR, TFNF, tx_used < s->cfg.fifo_depth);
+    sr = FIELD_DP32(sr, SR, TFE, tx_used == 0);
+    sr = FIELD_DP32(sr, SR, RFNE, rx_used != 0);
+    sr = FIELD_DP32(sr, SR, RFF,
+                    rx_used == s->cfg.fifo_depth);
+
+    return sr;
+}
+
+static void dw_ssi_run_transfer(DwSsiState *s);
+
+static void dw_ssi_push_tx(DwSsiState *s, uint32_t tx)
+{
+    if (!dw_ssi_is_enabled(s)) {
+        return;
+    }
+
+    if (fifo32_is_full(&s->tx_fifo)) {
+        return;
+    }
+
+    fifo32_push(&s->tx_fifo, tx);
+
+    if (s->phase != DW_SSI_PHASE_STANDARD_TX_ONLY) {
+        dw_ssi_run_transfer(s);
+    }
+}
+
+static uint32_t dw_ssi_send_frame(DwSsiState *s,
+                                        uint32_t tx)
+{
+    uint32_t mask = dw_ssi_frame_masked(s);
+    uint32_t rx;
+
+    tx &= mask;
+
+    if (FIELD_EX32(s->regs[R_CTRLR0], CTRLR0, SRL)) {
+        rx = tx;
+    } else {
+        rx = ssi_transfer(s->spi, tx);
+    }
+
+    return rx & mask;
+}
+
+static void dw_ssi_run_transfer(DwSsiState *s)
+{
+    uint32_t tmod;
+
+    if (!dw_ssi_is_enabled(s) || s->active_cs < 0) {
+        return;
+    }
+
+    /* SCKDV=0 disables sclk_out: no transfer can start. */
+    if (FIELD_EX32(s->regs[R_BAUDR], BAUDR, SCKDV) == 0) {
+        return;
+    }
+
+    /*
+     * TRM: the controller starts shifting only after the TX FIFO
+     * occupancy exceeds TXFTLR.TXFTHR.  Transfers already in flight
+     * are not interrupted when the FIFO temporarily drops below the
+     * level.
+     */
+    if (s->phase == DW_SSI_PHASE_IDLE &&
+        fifo32_num_used(&s->tx_fifo) <=
+            FIELD_EX32(s->regs[R_TXFTLR], TXFTLR, TXFTHR)) {
+        return;
+    }
+
+    tmod = FIELD_EX32(s->regs[R_CTRLR0], CTRLR0, TMOD);
+
+    switch (tmod) {
+    case DW_SSI_TMOD_TR:
+        if (s->phase == DW_SSI_PHASE_IDLE) {
+            s->phase = DW_SSI_PHASE_STANDARD_TR;
+        }
+
+        while (!fifo32_is_empty(&s->tx_fifo)) {
+            uint32_t tx = fifo32_pop(&s->tx_fifo);
+            uint32_t rx = dw_ssi_send_frame(s, tx);
+
+            if (!fifo32_is_full(&s->rx_fifo)) {
+                fifo32_push(&s->rx_fifo, rx);
+            } else {
+                qemu_log_mask(LOG_GUEST_ERROR,
+                              "%s: RX FIFO full, dropping frame\n",
+                              DEVICE(s)->canonical_path);
+                break;
+            }
+        }
+
+        if (fifo32_is_empty(&s->tx_fifo)) {
+            s->phase = DW_SSI_PHASE_IDLE;
+        }
+        break;
+    case DW_SSI_TMOD_TO: {
+        unsigned int frames = 0;
+
+        if (fifo32_is_empty(&s->tx_fifo)) {
+            s->phase = DW_SSI_PHASE_IDLE;
+            break;
+        }
+
+        s->phase = DW_SSI_PHASE_STANDARD_TX_ONLY;
+        while (!fifo32_is_empty(&s->tx_fifo) &&
+               frames < DW_SSI_PIO_TX_BATCH) {
+            uint32_t tx = fifo32_pop(&s->tx_fifo);
+
+            dw_ssi_send_frame(s, tx);
+            frames++;
+        }
+        if (fifo32_is_empty(&s->tx_fifo)) {
+            s->phase = DW_SSI_PHASE_IDLE;
+        }
+        break;
+    }
+    case DW_SSI_TMOD_RO:
+        switch (s->phase) {
+        case DW_SSI_PHASE_IDLE:
+            if (fifo32_is_empty(&s->tx_fifo)) {
+                break;
+            }
+            fifo32_pop(&s->tx_fifo);
+            s->phase = DW_SSI_PHASE_RX_ONLY;
+            s->remaining_frames = FIELD_EX32(s->regs[R_CTRLR1], CTRLR1,
+                                              NDF) + 1;
+            /* Fall through. */
+        case DW_SSI_PHASE_RX_ONLY:
+            while (!fifo32_is_full(&s->rx_fifo) &&
+                   s->remaining_frames > 0) {
+                uint32_t rx = dw_ssi_send_frame(s, 0x00);
+
+                fifo32_push(&s->rx_fifo, rx);
+                s->remaining_frames--;
+            }
+            if (s->remaining_frames == 0) {
+                s->phase = DW_SSI_PHASE_IDLE;
+            }
+            break;
+        }
+        break;
+    case DW_SSI_TMOD_EEPROM_READ:
+        switch (s->phase) {
+        case DW_SSI_PHASE_IDLE:
+            if (fifo32_is_empty(&s->tx_fifo)) {
+                break;
+            }
+            s->phase = DW_SSI_PHASE_EEPROM_COMMAND;
+            /* Fall through. */
+        case DW_SSI_PHASE_EEPROM_COMMAND:
+            if (fifo32_is_empty(&s->tx_fifo)) {
+                break;
+            }
+            while (!fifo32_is_empty(&s->tx_fifo)) {
+                uint32_t tx = fifo32_pop(&s->tx_fifo);
+
+                dw_ssi_send_frame(s, tx);
+            }
+            s->phase = DW_SSI_PHASE_EEPROM_DATA;
+            s->remaining_frames = FIELD_EX32(s->regs[R_CTRLR1], CTRLR1,
+                                              NDF) + 1;
+            /* Fall through. */
+        case DW_SSI_PHASE_EEPROM_DATA:
+            while (!fifo32_is_full(&s->rx_fifo) &&
+                   s->remaining_frames > 0) {
+                uint32_t rx = dw_ssi_send_frame(s, 0x00);
+
+                fifo32_push(&s->rx_fifo, rx);
+                s->remaining_frames--;
+            }
+            if (s->remaining_frames == 0) {
+                s->phase = DW_SSI_PHASE_IDLE;
+            }
+            break;
+        }
+        break;
+    default:
+        g_assert_not_reached();
+    }
+}
+
+static bool dw_ssi_is_dr(hwaddr addr)
+{
+    return addr >= A_DR0 && addr <= A_DR_END &&
+           (addr & 0x3) == 0;
+}
+
+static bool dw_ssi_is_unsupported_offset(hwaddr addr)
+{
+    switch (addr) {
+    /* Enhanced SPI extensions. */
+    case A_SPI_CTRLR0:
+    case A_DDR_DRIVE_EDGE:
+
+    /* XIP extensions. */
+    case A_XIP_MODE_BITS:
+    case A_XIP_INCR_INST:
+    case A_XIP_WRAP_INST:
+    case A_XIP_CTRL:
+    case A_XIP_SER:
+    case A_XRXOICR:
+    case A_XIP_CNT_TIME_OUT:
+    case A_SPI_CTRLR1:
+    case A_SPITECR:
+    case A_RSVD_138:
+    case A_RSVD_13C:
+    case A_XIP_WRITE_INCR_INST:
+    case A_XIP_WRITE_WRAP_INST:
+    case A_XIP_WRITE_CTRL:
+
+    /* Internal-AXI DMA/IDMA extensions. */
+    case A_DMACR:
+    case A_AXIAWLEN:
+    case A_AXIARLEN:
+    case A_SPIDR:
+    case A_SPIAR:
+    case A_AXIAR0:
+    case A_AXIAR1:
+    case A_AXIECR:
+    case A_DONECR:
+
+    /* SSI unsupported extensions. */
+    case A_RX_SAMPLE_DELAY:
+        return true;
+    default:
+        return false;
+    }
+}
+
+static bool dw_ssi_write_requires_disabled(hwaddr addr)
+{
+    switch (addr) {
+    case A_CTRLR0:
+    case A_CTRLR1:
+    case A_MWCR:
+    case A_BAUDR:
+        return true;
+    default:
+        return false;
+    }
+}
+
+static uint32_t dw_ssi_unsupported_read(DwSsiState *s,
+                                        hwaddr addr)
+{
+    qemu_log_mask(LOG_UNIMP,
+                  "%s: unsupported read at 0x%" HWADDR_PRIx "\n",
+                  DEVICE(s)->canonical_path, addr);
+    return 0;
+}
+
+static void dw_ssi_unsupported_write(DwSsiState *s,
+                                     hwaddr addr,
+                                     uint32_t value)
+{
+    qemu_log_mask(LOG_UNIMP,
+                  "%s: unsupported write at 0x%" HWADDR_PRIx
+                  " value 0x%08x\n",
+                  DEVICE(s)->canonical_path, addr, value);
+}
+
+static uint64_t dw_ssi_read(void *opaque, hwaddr addr, unsigned int size)
+{
+    DwSsiState *s = DW_SSI(opaque);
+    uint32_t value = 0;
+
+    if (dw_ssi_is_dr(addr)) {
+        if (!fifo32_is_empty(&s->rx_fifo)) {
+            value = fifo32_pop(&s->rx_fifo) & dw_ssi_frame_masked(s);
+        } else {
+            value = 0;
+        }
+
+        dw_ssi_run_transfer(s);
+        return value;
+    }
+
+    if (dw_ssi_is_unsupported_offset(addr)) {
+        value = dw_ssi_unsupported_read(s, addr);
+        return value;
+    }
+
+    switch (addr) {
+    case A_CTRLR0:
+    case A_CTRLR1:
+    case A_SSIENR:
+    case A_MWCR:
+    case A_BAUDR:
+    case A_TXFTLR:
+    case A_RXFTLR:
+    case A_IMR:
+    case A_IDR:
+    case A_SSIC_VERSION_ID:
+        value = s->regs[addr / sizeof(uint32_t)];
+        break;
+
+    case A_SER:
+        value = s->regs[R_SER] & MAKE_64BIT_MASK(0, s->cfg.num_cs);
+        break;
+    case A_TXFLR:
+        value = fifo32_num_used(&s->tx_fifo);
+        if (s->phase == DW_SSI_PHASE_STANDARD_TX_ONLY) {
+            dw_ssi_run_transfer(s);
+        }
+        break;
+    case A_RXFLR:
+        value = fifo32_num_used(&s->rx_fifo);
+        break;
+    case A_SR:
+        value = dw_ssi_status(s);
+        if (s->phase == DW_SSI_PHASE_STANDARD_TX_ONLY) {
+            dw_ssi_run_transfer(s);
+        }
+        break;
+    default:
+        if (addr >= DW_SSI_REGS_SIZE || (addr & 0x3) != 0) {
+            qemu_log_mask(LOG_GUEST_ERROR,
+                          "%s: bad read offset 0x%" HWADDR_PRIx "\n",
+                          DEVICE(s)->canonical_path, addr);
+        }
+        break;
+    }
+
+    return value;
+}
+
+static void dw_ssi_write(void *opaque, hwaddr addr,
+                              uint64_t value, unsigned int size)
+{
+    DwSsiState *s = DW_SSI(opaque);
+
+    if (dw_ssi_is_dr(addr)) {
+        dw_ssi_push_tx(s, value);
+        return;
+    }
+
+    if (dw_ssi_is_unsupported_offset(addr)) {
+        dw_ssi_unsupported_write(s, addr, value);
+        return;
+    }
+
+    if (dw_ssi_write_requires_disabled(addr) &&
+        dw_ssi_is_enabled(s)) {
+        qemu_log_mask(LOG_GUEST_ERROR,
+                      "%s: write to offset 0x%" HWADDR_PRIx
+                      " while SSI is enabled\n",
+                      DEVICE(s)->canonical_path, addr);
+        return;
+    }
+
+    switch (addr) {
+    case A_CTRLR0:
+        value &= ~R_CTRLR0_SPI_FRF_MASK;
+        dw_ssi_write_masked(s, R_CTRLR0, value,
+                                 DW_SSI_CTRLR0_STANDARD_WRITABLE_MASK);
+        break;
+    case A_CTRLR1:
+        dw_ssi_write_masked(s, R_CTRLR1, value,
+                                 DW_SSI_CTRLR1_WRITABLE_MASK);
+        break;
+    case A_SSIENR: {
+        bool old_enabled = dw_ssi_is_enabled(s);
+        bool new_enabled = value & R_SSIENR_SSIC_EN_MASK;
+
+        if (old_enabled == new_enabled) {
+            return;
+        }
+
+        s->regs[R_SSIENR] = FIELD_DP32(0, SSIENR, SSIC_EN, new_enabled);
+        if (!new_enabled) {
+            dw_ssi_abort_transfer(s);
+            return;
+        }
+
+        dw_ssi_update_cs(s);
+        dw_ssi_run_transfer(s);
+        break;
+    }
+    case A_MWCR:
+        dw_ssi_write_masked(s, R_MWCR, value,
+                                 DW_SSI_MWCR_WRITABLE_MASK);
+        break;
+    case A_SER: {
+        uint32_t old_ser = s->regs[R_SER];
+        uint32_t new_ser = value & MAKE_64BIT_MASK(0, s->cfg.num_cs);
+
+        /*
+         * Changing the selected slave terminates a pending transaction:
+         * the FIFOs are flushed only by clearing SSIENR (TRM), but a
+         * stale RO/EEPROM phase must not resume on the new chip select.
+         */
+        if (old_ser != new_ser && s->phase != DW_SSI_PHASE_IDLE) {
+            s->phase = DW_SSI_PHASE_IDLE;
+            s->remaining_frames = 0;
+        }
+        s->regs[R_SER] = new_ser;
+
+        dw_ssi_update_cs(s);
+        dw_ssi_run_transfer(s);
+        break;
+    }
+    case A_BAUDR:
+        dw_ssi_write_masked(s, R_BAUDR, value,
+                                 DW_SSI_BAUDR_WRITABLE_MASK);
+        break;
+    case A_TXFTLR:
+        if (!dw_ssi_fifo_threshold_valid(
+                s, FIELD_EX32(value, TXFTLR, TFT)) ||
+            !dw_ssi_fifo_threshold_valid(
+                s, FIELD_EX32(value, TXFTLR, TXFTHR))) {
+            qemu_log_mask(LOG_GUEST_ERROR,
+                          "%s: invalid txftlr value %x\n",
+                          DEVICE(s)->canonical_path, (unsigned int)value);
+            break;
+        }
+        dw_ssi_write_masked(s, R_TXFTLR, value,
+                                 DW_SSI_TXFTLR_WRITABLE_MASK);
+        dw_ssi_run_transfer(s);
+        break;
+    case A_RXFTLR:
+        if (!dw_ssi_fifo_threshold_valid(
+                s, FIELD_EX32(value, RXFTLR, RFT))) {
+            qemu_log_mask(LOG_GUEST_ERROR,
+                          "%s: invalid rxftlr value %x\n",
+                          DEVICE(s)->canonical_path, (unsigned int)value);
+            break;
+        }
+        dw_ssi_write_masked(s, R_RXFTLR, value,
+                                 DW_SSI_RXFTLR_WRITABLE_MASK);
+        break;
+    case A_TXFLR:
+    case A_RXFLR:
+    case A_SR:
+        break;
+    case A_IMR:
+        dw_ssi_write_masked(s, R_IMR, value,
+                                 DW_SSI_IMR_WRITABLE_MASK);
+        break;
+    case A_IDR:
+    case A_SSIC_VERSION_ID:
+        break;
+    default:
+        if (addr >= DW_SSI_REGS_SIZE || (addr & 0x3) != 0) {
+            qemu_log_mask(LOG_GUEST_ERROR,
+                          "%s: bad write offset 0x%" HWADDR_PRIx "\n",
+                          DEVICE(s)->canonical_path, addr);
+        }
+        break;
+    }
+}
+
+static const MemoryRegionOps dw_ssi_ops = {
+    .read = dw_ssi_read,
+    .write = dw_ssi_write,
+    .endianness = DEVICE_LITTLE_ENDIAN,
+    .impl = {
+        .min_access_size = 4,
+        .max_access_size = 4,
+        .unaligned = false,
+    },
+    .valid = {
+        .min_access_size = 4,
+        .max_access_size = 4,
+        .unaligned = false,
+    },
+};
+
+static void dw_ssi_enter_reset(Object *obj, ResetType type)
+{
+    DwSsiState *s = DW_SSI(obj);
+
+    memset(s->regs, 0, sizeof(s->regs));
+    fifo32_reset(&s->tx_fifo);
+    fifo32_reset(&s->rx_fifo);
+    s->phase = DW_SSI_PHASE_IDLE;
+    s->remaining_frames = 0;
+
+    s->regs[R_CTRLR0] = DW_SSI_CTRLR0_RESET;
+    s->regs[R_SR] = DW_SSI_SR_RESET;
+    s->regs[R_IMR] = s->cfg.imr_reset & DW_SSI_IMR_WRITABLE_MASK;
+    s->regs[R_IDR] = DW_SSI_IDR_RESET;
+    s->regs[R_SSIC_VERSION_ID] = DW_SSI_VERSION;
+}
+
+static void dw_ssi_hold_reset(Object *obj, ResetType type)
+{
+    DwSsiState *s = DW_SSI(obj);
+    s->active_cs = -1;
+
+    if (s->cs_lines) {
+        for (int i = 0; i < s->cfg.num_cs; i++) {
+            qemu_irq_raise(s->cs_lines[i]);
+        }
+    }
+}
+
+static int dw_ssi_post_load(void *opaque, int version_id)
+{
+    DwSsiState *s = opaque;
+    uint32_t ndf = FIELD_EX32(s->regs[R_CTRLR1], CTRLR1, NDF) + 1;
+
+    if (s->active_cs < -1 || s->active_cs >= (int)s->cfg.num_cs) {
+        return -EINVAL;
+    }
+    if (s->phase > DW_SSI_PHASE_STANDARD_TR) {
+        return -EINVAL;
+    }
+    if (s->phase == DW_SSI_PHASE_RX_ONLY ||
+        s->phase == DW_SSI_PHASE_EEPROM_DATA) {
+        if (s->remaining_frames == 0 || s->remaining_frames > ndf) {
+            return -EINVAL;
+        }
+    } else if (s->remaining_frames != 0) {
+        return -EINVAL;
+    }
+    if (s->active_cs >= 0 &&
+        (!dw_ssi_is_enabled(s) ||
+         s->regs[R_SER] != BIT(s->active_cs))) {
+        return -EINVAL;
+    }
+
+    for (int i = 0; i < s->cfg.num_cs; i++) {
+        qemu_irq_raise(s->cs_lines[i]);
+    }
+    if (s->active_cs >= 0) {
+        qemu_irq_lower(s->cs_lines[s->active_cs]);
+    }
+
+    return 0;
+}
+
+static const VMStateDescription vmstate_dw_ssi = {
+    .name = TYPE_DW_SSI,
+    .version_id = 1,
+    .minimum_version_id = 1,
+    .post_load = dw_ssi_post_load,
+    .fields = (const VMStateField[]) {
+        VMSTATE_UINT32_EQUAL(cfg.num_cs, DwSsiState),
+        VMSTATE_UINT32_EQUAL(cfg.fifo_depth, DwSsiState),
+        VMSTATE_UINT32_EQUAL(cfg.imr_reset, DwSsiState),
+        VMSTATE_UINT32_ARRAY(regs, DwSsiState, DW_SSI_NUM_REGS),
+        VMSTATE_FIFO32(tx_fifo, DwSsiState),
+        VMSTATE_FIFO32(rx_fifo, DwSsiState),
+        VMSTATE_UINT32(phase, DwSsiState),
+        VMSTATE_UINT32(remaining_frames, DwSsiState),
+        VMSTATE_INT32(active_cs, DwSsiState),
+        VMSTATE_END_OF_LIST()
+    },
+};
+
+static void dw_ssi_init(Object *obj)
+{
+    DwSsiState *s = DW_SSI(obj);
+    DeviceState *dev = DEVICE(obj);
+    SysBusDevice *sbd = SYS_BUS_DEVICE(obj);
+
+    s->spi = ssi_create_bus(dev, "spi");
+
+    memory_region_init_io(&s->mmio, obj, &dw_ssi_ops, s,
+                          TYPE_DW_SSI, DW_SSI_MMIO_SIZE);
+    sysbus_init_mmio(sbd, &s->mmio);
+
+    s->active_cs = -1;
+}
+
+static void dw_ssi_realize(DeviceState *dev, Error **errp)
+{
+    DwSsiState *s = DW_SSI(dev);
+
+    if (!dw_ssi_validate_config(s, errp)) {
+        return;
+    }
+
+    s->cs_lines = g_new0(qemu_irq, s->cfg.num_cs);
+    qdev_init_gpio_out_named(dev, s->cs_lines, "cs", s->cfg.num_cs);
+
+    fifo32_create(&s->tx_fifo, s->cfg.fifo_depth);
+    fifo32_create(&s->rx_fifo, s->cfg.fifo_depth);
+}
+
+static void dw_ssi_finalize(Object *obj)
+{
+    DwSsiState *s = DW_SSI(obj);
+
+    fifo32_destroy(&s->tx_fifo);
+    fifo32_destroy(&s->rx_fifo);
+    g_free(s->cs_lines);
+}
+
+static const Property dw_ssi_properties[] = {
+    DEFINE_PROP_UINT32("num-cs", DwSsiState, cfg.num_cs, 1),
+    DEFINE_PROP_UINT32("fifo-depth", DwSsiState, cfg.fifo_depth, 256),
+    DEFINE_PROP_UINT32("imr-reset", DwSsiState,
+                       cfg.imr_reset, 0x0000003f),
+};
+
+static void dw_ssi_class_init(ObjectClass *klass, const void *data)
+{
+    DeviceClass *dc = DEVICE_CLASS(klass);
+    ResettableClass *rc = RESETTABLE_CLASS(klass);
+
+    dc->realize = dw_ssi_realize;
+    dc->vmsd = &vmstate_dw_ssi;
+    device_class_set_props(dc, dw_ssi_properties);
+    rc->phases.enter = dw_ssi_enter_reset;
+    rc->phases.hold = dw_ssi_hold_reset;
+}
+
+static const TypeInfo dw_ssi_info = {
+    .name = TYPE_DW_SSI,
+    .parent = TYPE_SYS_BUS_DEVICE,
+    .instance_size = sizeof(DwSsiState),
+    .instance_init = dw_ssi_init,
+    .instance_finalize = dw_ssi_finalize,
+    .class_init = dw_ssi_class_init,
+};
+
+static void dw_ssi_register_types(void)
+{
+    type_register_static(&dw_ssi_info);
+}
+
+type_init(dw_ssi_register_types)
hw/ssi/meson.build +1 -0
index 6afb1ea200..447780846c 100644
@@ -1,6 +1,7 @@
 system_ss.add(when: 'CONFIG_ALLWINNER_A10_SPI', if_true: files('allwinner-a10-spi.c'))
 system_ss.add(when: 'CONFIG_ASPEED_SOC', if_true: files('aspeed_smc.c'))
 system_ss.add(when: 'CONFIG_MSF2', if_true: files('mss-spi.c'))
+system_ss.add(when: 'CONFIG_DW_SSI', if_true: files('dw_ssi.c'))
 system_ss.add(when: 'CONFIG_NPCM7XX', if_true: files('npcm7xx_fiu.c', 'npcm_pspi.c'))
 system_ss.add(when: 'CONFIG_PL022', if_true: files('pl022.c'))
 system_ss.add(when: 'CONFIG_SIFIVE_SPI', if_true: files('sifive_spi.c'))
include/hw/ssi/dw_ssi.h new file +62 -0
new file mode 100644
index 0000000000..a74e906ef8
@@ -0,0 +1,62 @@
+/*
+ * Synopsys DesignWare SSI
+ *
+ * Copyright (c) 2026 Kangjie Huang <[email protected]>
+ *
+ * SPDX-License-Identifier: GPL-2.0-or-later
+ *
+ * Emulates the DesignWare SSI controller in Standard SPI mode,
+ * covering the PIO/FIFO data path and chip selects.
+ */
+
+#ifndef HW_SSI_DW_SSI_H
+#define HW_SSI_DW_SSI_H
+
+#include "hw/core/sysbus.h"
+#include "hw/ssi/ssi.h"
+#include "qemu/fifo32.h"
+#include "qom/object.h"
+
+#define TYPE_DW_SSI "designware-ssi"
+OBJECT_DECLARE_SIMPLE_TYPE(DwSsiState, DW_SSI)
+
+#define DW_SSI_MMIO_SIZE 0x1000
+#define DW_SSI_REGS_SIZE 0x14c
+#define DW_SSI_NUM_REGS \
+    (DW_SSI_REGS_SIZE / sizeof(uint32_t))
+
+typedef enum DwSsiPhase {
+    DW_SSI_PHASE_IDLE,
+    DW_SSI_PHASE_STANDARD_TX_ONLY,
+    DW_SSI_PHASE_RX_ONLY,
+    DW_SSI_PHASE_EEPROM_COMMAND,
+    DW_SSI_PHASE_EEPROM_DATA,
+    DW_SSI_PHASE_STANDARD_TR,
+} DwSsiPhase;
+
+typedef struct DwSsiConfig {
+    uint32_t num_cs;
+    uint32_t fifo_depth;
+    uint32_t imr_reset;
+} DwSsiConfig;
+
+struct DwSsiState {
+    SysBusDevice parent_obj;
+
+    MemoryRegion mmio;
+    SSIBus *spi;
+
+    qemu_irq *cs_lines;
+
+    Fifo32 tx_fifo;
+    Fifo32 rx_fifo;
+    uint32_t regs[DW_SSI_NUM_REGS];
+
+    DwSsiConfig cfg;
+
+    uint32_t phase;
+    uint32_t remaining_frames;
+
+    int active_cs;
+};
+#endif /* HW_SSI_DW_SSI_H */
Signature · 1 line
2.43.0
#3 lore raw

Re: [PATCH v2 1/5] hw/ssi: Add Synopsys DesignWare SSI standard PIO controller

Hi

Quoted 1 line · Chao Liu <[email protected]> 于2026年8月3日周一 10:10写道:

Could you share a stable public link to the DesignWare SSI datasheet here?

Sorry, but I don't have a stable public link to share. Synopsys doesn't publish the DesignWare SSI databook for public download; it's gated behind myDesignWare registration.

As for the other Synopsys peripherals, I haven't seen any public links to Synopsys IP documentation either - not for the existing QEMU models (DWC2/DWC3 USB, DesignWare I2C, DesignWare PCIe host, etc.) or anywhere else.

I actually referenced for this implementation:

- K230 TRM (Canaan official; includes the DWC SSI 1.01a register
descriptions): https://github.com/revyos/external-docs/blob/master/K230/en-us/K230_Technical_Reference_Manual_V0.3.1_20241118.pdf

- Intel Arria 10 HPS TRM, Chapter 20 (Intel official; DW APB SSI
registers) https://www.intel.com/content/www/us/en/docs/programmable/683711/21-2/hard-processor-system-technical-reference.html

- Intel HPS Technical Reference Manual:
https://www.intel.com/content/www/us/en/docs/programmable/683222/22-4/hard-processor-system-technical-reference.html

- Linux kernel DW SPI driver,
- DT binding:
https://github.com/torvalds/linux/blob/master/Documentation/devicetree/bindings/spi/snps,dw-apb-ssi.yaml
- Driver header (PSSI/HSSI CTRLR0 ):
https://github.com/torvalds/linux/blob/master/drivers/spi/spi-dw.h
- Driver core
https://github.com/torvalds/linux/blob/master/drivers/spi/spi-dw-core.c

#4 lore raw

Re: [PATCH v2 1/5] hw/ssi: Add Synopsys DesignWare SSI standard PIO controller

In reply to Kangjie Huang
Quoted 14 lines · Kangjie Huang

Add a reusable SysBus model for the Synopsys DesignWare SSI controller.

Implement the Standard SPI register subset, configurable chip-select and FIFO resources, the four Standard PIO transfer modes, reset handling, chip-select GPIOs, and RAZ/WI handling for unsupported enhanced, DMA, and XIP registers.

Add num-cs, fifo-depth, and imr-reset properties. Save only Standard PIO state in VMState and reject migration between devices with different resource profiles.

The controller is not instantiated by this patch.

<snip>

Quoted 62 lines
---
 hw/ssi/Kconfig          |   4 +
 hw/ssi/dw_ssi.c         | 956 ++++++++++++++++++++++++++++++++++++++++
 hw/ssi/meson.build      |   1 +
 include/hw/ssi/dw_ssi.h |  62 +++
 4 files changed, 1023 insertions(+)
 create mode 100644 hw/ssi/dw_ssi.c
 create mode 100644 include/hw/ssi/dw_ssi.h
hw/ssi/Kconfig +4 -0
index 1bd56463c1..c48cd2ff6d 100644
@@ -6,6 +6,10 @@ config SIFIVE_SPI
     bool
     select SSI
 
+config DW_SSI
+    bool
+    select SSI
+
 config SSI
     bool
 
hw/ssi/dw_ssi.c new file +32 -0
new file mode 100644
index 0000000000..f9f0e23730
@@ -0,0 +1,956 @@
+/*
+ * Synopsys DesignWare SSI
+ *
+ * Copyright (c) 2026 Kangjie Huang <[email protected]>
+ *
+ * SPDX-License-Identifier: GPL-2.0-or-later
+ *
+ * Emulates the DesignWare SSI controller in Standard SPI mode,
+ * covering the PIO/FIFO data path and chip selects.
+ */
+
+#include "qemu/osdep.h"
+#include "hw/core/registerfields.h"
+#include "hw/core/qdev-properties.h"
+#include "hw/core/irq.h"
+#include "hw/ssi/dw_ssi.h"
+#include "migration/vmstate.h"
+#include "qapi/error.h"
+#include "qemu/bitops.h"
+#include "qemu/log.h"
+#include "qemu/module.h"
+
+/* DWC SSI 1.03 derivative (TRM v0.3.1 section 12.3). */
+#define DW_SSI_CTRLR0_RESET            0x00004007
+#define DW_SSI_SR_RESET                0x00000006
+#define DW_SSI_IDR_RESET               0xa1b2c3d5
+#define DW_SSI_VERSION                 0x3130332a
+#define DW_SSI_PIO_TX_BATCH            64
+
+enum {
+    DW_SSI_TMOD_TR,
+    DW_SSI_TMOD_TO,

hw/ssi/dw_ssi.c line 42

Quoted 10 lines
hw/ssi/dw_ssi.c new file +10 -0
+    DW_SSI_TMOD_RO,
+    DW_SSI_TMOD_EEPROM_READ,
+};
+
+REG32(CTRLR0, 0x000)
+    FIELD(CTRLR0, DFS, 0, 5)
+    FIELD(CTRLR0, FRF, 6, 2)
+    FIELD(CTRLR0, SCPH, 8, 1)
+    FIELD(CTRLR0, SCPOL, 9, 1)
+    FIELD(CTRLR0, TMOD, 10, 2)

There are 2 variants of designware spi, one is called dw-apb-ssi and another is called dwc-ssi. If you look at their u-boot drivers, you can see that they differ in the location of the TMOD bits in the CTRLR0. dw-apb-ssi has it at bits 8 and 9, while dwc-ssi has it at 10/11. This implementation is for the dwc-ssi version (which is the newer one).

Would it be possible to have the naming be consistent (DWC_SSI, dwc_ssi.c) everywhere? There are some places throught the series where the file names/Kconfig symbols/function names are named dw_ssi.

One could argue that this model could retain the generic designware spi name and support both versions, but I don't think that's necessary.

Regards
Anirudh Srinivasan

#5 lore raw

Re: [PATCH v2 1/5] hw/ssi: Add Synopsys DesignWare SSI standard PIO controller

Quoted 6 lines · Anirudh Srinivasan <[email protected]> 于2026年8月5日周三 02:39写道:

There are 2 variants of designware spi, one is called dw-apb-ssi and another is called dwc-ssi. If you look at their u-boot drivers, you can see that they differ in the location of the TMOD bits in the CTRLR0. dw-apb-ssi has it at bits 8 and 9, while dwc-ssi has it at 10/11. This implementation is for the dwc-ssi version (which is the newer one).

Quoted 6 lines

Would it be possible to have the naming be consistent (DWC_SSI, dwc_ssi.c) everywhere? There are some places throught the series where the file names/Kconfig symbols/function names are named dw_ssi.

One could argue that this model could retain the generic designware spi name and support both versions, but I don't think that's necessary.

I will make the naming consistently use dwc_ssi throughout the next revision of the series.

I compared the dw-apb-ssi and dwc-ssi , as well as their Linux and U-Boot driver implementations, and confirmed that the two variants have distinct guest-visible register ABIs. As you note, CTRLR0.TMOD is located at bits [9:8] in dw-apb-ssi and at bits [11:10] in DWC SSI. Also,The layouts and supported ranges of other fields, including DFS, FRF and SCPH/SCPOL, also differ. Linux and U-Boot likewise use explicit capability checks to select the appropriate register field encodings.

Supporting dw-apb-ssi would require variant-specific register handling and tests. But QEMU currently has no concrete dw-apb-ssi machine/SoC consumer, my understanding is that this series should remain focused on dwc-ssi, with dw-apb-ssi support added together with a concrete consumer and its tests in the future. Please let me know if I have misunderstood.

Regards,
Kangjie

#6 lore raw

Re: [PATCH v2 1/5] hw/ssi: Add Synopsys DesignWare SSI standard PIO controller

Hi,

Quoted 27 lines · Kangjie Huang
Quoted 6 lines · Anirudh Srinivasan <[email protected]> 于2026年8月5日周三 02:39写道:

There are 2 variants of designware spi, one is called dw-apb-ssi and another is called dwc-ssi. If you look at their u-boot drivers, you can see that they differ in the location of the TMOD bits in the CTRLR0. dw-apb-ssi has it at bits 8 and 9, while dwc-ssi has it at 10/11. This implementation is for the dwc-ssi version (which is the newer one).

Quoted 6 lines

Would it be possible to have the naming be consistent (DWC_SSI, dwc_ssi.c) everywhere? There are some places throught the series where the file names/Kconfig symbols/function names are named dw_ssi.

One could argue that this model could retain the generic designware spi name and support both versions, but I don't think that's necessary.

I will make the naming consistently use dwc_ssi throughout the next revision of the series.

I compared the dw-apb-ssi and dwc-ssi , as well as their Linux and U-Boot driver implementations, and confirmed that the two variants have distinct guest-visible register ABIs. As you note, CTRLR0.TMOD is located at bits [9:8] in dw-apb-ssi and at bits [11:10] in DWC SSI. Also,The layouts and supported ranges of other fields, including DFS, FRF and SCPH/SCPOL, also differ. Linux and U-Boot likewise use explicit capability checks to select the appropriate register field encodings.

Quoted 5 lines

Supporting dw-apb-ssi would require variant-specific register handling and tests. But QEMU currently has no concrete dw-apb-ssi machine/SoC consumer, my understanding is that this series should remain focused on dwc-ssi, with dw-apb-ssi support added together with a concrete consumer and its tests in the future. Please let me know if I have misunderstood.

Sounds good. I wouldn't worry about supporting dw-apb-ssi.

Quoted 3 lines

Regards,
Kangjie

#7 lore raw

Re: [PATCH v2 1/5] hw/ssi: Add Synopsys DesignWare SSI standard PIO controller

Hi,

Quoted 2 lines · Anirudh Srinivasan <[email protected]> 于2026年8月5日周三 23:15写道:

Sounds good. I wouldn't worry about supporting dw-apb-ssi.

Thank you. I will fix in the next version.
Regards,
Kangjie

#8 lore raw

Re: [PATCH v2 1/5] hw/ssi: Add Synopsys DesignWare SSI standard PIO controller

In reply to Kangjie Huang
Quoted 720 lines · Kangjie Huang

Add a reusable SysBus model for the Synopsys DesignWare SSI controller.

Implement the Standard SPI register subset, configurable chip-select and FIFO resources, the four Standard PIO transfer modes, reset handling, chip-select GPIOs, and RAZ/WI handling for unsupported enhanced, DMA, and XIP registers.

Add num-cs, fifo-depth, and imr-reset properties. Save only Standard PIO state in VMState and reject migration between devices with different resource profiles.

The controller is not instantiated by this patch.

---
  hw/ssi/Kconfig          |   4 +
  hw/ssi/dw_ssi.c         | 956 ++++++++++++++++++++++++++++++++++++++++
  hw/ssi/meson.build      |   1 +
  include/hw/ssi/dw_ssi.h |  62 +++
  4 files changed, 1023 insertions(+)

create mode 100644 hw/ssi/dw_ssi.c
create mode 100644 include/hw/ssi/dw_ssi.h

hw/ssi/Kconfig +4 -0
index 1bd56463c1..c48cd2ff6d 100644
@@ -6,6 +6,10 @@ config SIFIVE_SPI
      bool
      select SSI
 
+config DW_SSI
+    bool
+    select SSI
+
  config SSI
      bool
 
hw/ssi/dw_ssi.c new file +676 -0
new file mode 100644
index 0000000000..f9f0e23730
@@ -0,0 +1,956 @@
+/*
+ * Synopsys DesignWare SSI
+ *
+ * Copyright (c) 2026 Kangjie Huang <[email protected]>
+ *
+ * SPDX-License-Identifier: GPL-2.0-or-later
+ *
+ * Emulates the DesignWare SSI controller in Standard SPI mode,
+ * covering the PIO/FIFO data path and chip selects.
+ */
+
+#include "qemu/osdep.h"
+#include "hw/core/registerfields.h"
+#include "hw/core/qdev-properties.h"
+#include "hw/core/irq.h"
+#include "hw/ssi/dw_ssi.h"
+#include "migration/vmstate.h"
+#include "qapi/error.h"
+#include "qemu/bitops.h"
+#include "qemu/log.h"
+#include "qemu/module.h"
+
+/* DWC SSI 1.03 derivative (TRM v0.3.1 section 12.3). */
+#define DW_SSI_CTRLR0_RESET            0x00004007
+#define DW_SSI_SR_RESET                0x00000006
+#define DW_SSI_IDR_RESET               0xa1b2c3d5
+#define DW_SSI_VERSION                 0x3130332a
+#define DW_SSI_PIO_TX_BATCH            64
+
+enum {
+    DW_SSI_TMOD_TR,
+    DW_SSI_TMOD_TO,
+    DW_SSI_TMOD_RO,
+    DW_SSI_TMOD_EEPROM_READ,
+};
+
+REG32(CTRLR0, 0x000)
+    FIELD(CTRLR0, DFS, 0, 5)
+    FIELD(CTRLR0, FRF, 6, 2)
+    FIELD(CTRLR0, SCPH, 8, 1)
+    FIELD(CTRLR0, SCPOL, 9, 1)
+    FIELD(CTRLR0, TMOD, 10, 2)
+    FIELD(CTRLR0, SLV_OE, 12, 1)
+    FIELD(CTRLR0, SRL, 13, 1)
+    FIELD(CTRLR0, SSTE, 14, 1)
+    FIELD(CTRLR0, CFS, 16, 4)
+    FIELD(CTRLR0, SPI_FRF, 22, 2)
+    FIELD(CTRLR0, SPI_HYPERBUS_EN, 24, 1)
+    FIELD(CTRLR0, SPI_DWS_EN, 25, 1)
+REG32(CTRLR1, 0x004)
+    FIELD(CTRLR1, NDF, 0, 16)
+REG32(SSIENR, 0x008)
+    FIELD(SSIENR, SSIC_EN, 0, 1)
+REG32(MWCR, 0x00c)
+    FIELD(MWCR, MWMOD, 0, 1)
+    FIELD(MWCR, MDD, 1, 1)
+    FIELD(MWCR, MHS, 2, 1)
+REG32(SER, 0x010)
+REG32(BAUDR, 0x014)
+    FIELD(BAUDR, SCKDV, 1, 15)
+REG32(TXFTLR, 0x018)
+    FIELD(TXFTLR, TFT, 0, 8)
+    FIELD(TXFTLR, TXFTHR, 16, 11)
+REG32(RXFTLR, 0x01c)
+    FIELD(RXFTLR, RFT, 0, 8)
+REG32(TXFLR, 0x020)
+    FIELD(TXFLR, TXTFL, 0, 9)
+REG32(RXFLR, 0x024)
+    FIELD(RXFLR, RXTFL, 0, 9)
+REG32(SR, 0x028)
+    FIELD(SR, CMPLTD_DF, 15, 17)
+    FIELD(SR, DCOL, 6, 1)
+    FIELD(SR, TXE, 5, 1)
+    FIELD(SR, BUSY, 0, 1)
+    FIELD(SR, TFNF, 1, 1)
+    FIELD(SR, TFE, 2, 1)
+    FIELD(SR, RFNE, 3, 1)
+    FIELD(SR, RFF, 4, 1)
+REG32(IMR, 0x02c)
+    FIELD(IMR, DONEM, 11, 1)
+    FIELD(IMR, SPITEM, 10, 1)
+    FIELD(IMR, AXIEM, 8, 1)
+    FIELD(IMR, TXUIM, 7, 1)
+    FIELD(IMR, XRXOIM, 6, 1)
+    FIELD(IMR, MSTIM, 5, 1)
+    FIELD(IMR, RXOIM, 3, 1)
+    FIELD(IMR, RXUIM, 2, 1)
+    FIELD(IMR, TXOIM, 1, 1)
+    FIELD(IMR, TXEIM, 0, 1)
+    FIELD(IMR, RXFIM, 4, 1)
+REG32(ISR, 0x030)
+    FIELD(ISR, DONES, 11, 1)
+    FIELD(ISR, SPITES, 10, 1)
+    FIELD(ISR, AXIES, 8, 1)
+    FIELD(ISR, TXUIS, 7, 1)
+    FIELD(ISR, XRXOIS, 6, 1)
+    FIELD(ISR, MSTIS, 5, 1)
+    FIELD(ISR, RXOIS, 3, 1)
+    FIELD(ISR, RXUIS, 2, 1)
+    FIELD(ISR, TXOIS, 1, 1)
+    FIELD(ISR, TXEIS, 0, 1)
+    FIELD(ISR, RXFIS, 4, 1)
+REG32(RISR, 0x034)
+    FIELD(RISR, DONER, 11, 1)
+    FIELD(RISR, SPITER, 10, 1)
+    FIELD(RISR, AXIER, 8, 1)
+    FIELD(RISR, TXUIR, 7, 1)
+    FIELD(RISR, XRXOIR, 6, 1)
+    FIELD(RISR, MSTIR, 5, 1)
+    FIELD(RISR, RXOIR, 3, 1)
+    FIELD(RISR, RXUIR, 2, 1)
+    FIELD(RISR, TXOIR, 1, 1)
+    FIELD(RISR, TXEIR, 0, 1)
+    FIELD(RISR, RXFIR, 4, 1)
+REG32(TXEICR, 0x038)
+    FIELD(TXEICR, TXEICR, 0, 1)
+REG32(RXOICR, 0x03c)
+    FIELD(RXOICR, RXOICR, 0, 1)
+REG32(RXUICR, 0x040)
+    FIELD(RXUICR, RXUICR, 0, 1)
+REG32(MSTICR, 0x044)
+    FIELD(MSTICR, MSTICR, 0, 1)
+REG32(ICR, 0x048)
+    FIELD(ICR, ICR, 0, 1)
+REG32(DMACR, 0x04c)
+    FIELD(DMACR, RDMAE, 0, 1)
+    FIELD(DMACR, TDMAE, 1, 1)
+    FIELD(DMACR, IDMAE, 2, 1)
+    FIELD(DMACR, ATW, 3, 2)
+    FIELD(DMACR, AINC, 6, 1)
+    FIELD(DMACR, ACACHE, 8, 4)
+    FIELD(DMACR, APROT, 12, 3)
+    FIELD(DMACR, AID, 15, 4)
+REG32(AXIAWLEN, 0x050)
+    FIELD(AXIAWLEN, AWLEN, 8, 8)
+REG32(AXIARLEN, 0x054)
+    FIELD(AXIARLEN, ARLEN, 8, 8)
+REG32(IDR, 0x058)
+    FIELD(IDR, IDCODE, 0, 32)
+REG32(SSIC_VERSION_ID, 0x05c)
+    FIELD(SSIC_VERSION_ID, VERSION_ID, 0, 32)
+REG32(DR0, 0x060)
+REG32(DR_END, 0x0ec)
+REG32(RX_SAMPLE_DELAY, 0x0f0)
+    FIELD(RX_SAMPLE_DELAY, RSD, 0, 8)
+    FIELD(RX_SAMPLE_DELAY, SE, 16, 1)
+REG32(SPI_CTRLR0, 0x0f4)
+    FIELD(SPI_CTRLR0, CLK_STRETCH_EN, 30, 1)
+    FIELD(SPI_CTRLR0, XIP_PREFETCH_EN, 29, 1)
+    FIELD(SPI_CTRLR0, XIP_MBL, 26, 2)
+    FIELD(SPI_CTRLR0, SPI_RXDS_SIG_EN, 25, 1)
+    FIELD(SPI_CTRLR0, SPI_DM_EN, 24, 1)
+    FIELD(SPI_CTRLR0, SSIC_XIP_CONT_XFER_EN, 21, 1)
+    FIELD(SPI_CTRLR0, XIP_INST_EN, 20, 1)
+    FIELD(SPI_CTRLR0, XIP_DFS_HC, 19, 1)
+    FIELD(SPI_CTRLR0, SPI_RXDS_EN, 18, 1)
+    FIELD(SPI_CTRLR0, INST_DDR_EN, 17, 1)
+    FIELD(SPI_CTRLR0, SPI_DDR_EN, 16, 1)
+    FIELD(SPI_CTRLR0, WAIT_CYCLES, 11, 5)
+    FIELD(SPI_CTRLR0, INST_L, 8, 2)
+    FIELD(SPI_CTRLR0, XIP_MD_BIT_EN, 7, 1)
+    FIELD(SPI_CTRLR0, ADDR_L, 2, 4)
+    FIELD(SPI_CTRLR0, TRANS_TYPE, 0, 2)
+REG32(DDR_DRIVE_EDGE, 0x0f8)
+    FIELD(DDR_DRIVE_EDGE, TDE, 0, 8)
+REG32(XIP_MODE_BITS, 0x0fc)
+    FIELD(XIP_MODE_BITS, XIP_MD_BITS, 0, 16)
+REG32(XIP_INCR_INST, 0x100)
+    FIELD(XIP_INCR_INST, INCR_INST, 0, 16)
+REG32(XIP_WRAP_INST, 0x104)
+    FIELD(XIP_WRAP_INST, WRAP_INST, 0, 16)
+REG32(XIP_CTRL, 0x108)
+REG32(XIP_SER, 0x10c)
+REG32(XRXOICR, 0x110)
+REG32(XIP_CNT_TIME_OUT, 0x114)
+REG32(SPI_CTRLR1, 0x118)
+REG32(SPITECR, 0x11c)
+REG32(SPIDR, 0x120)
+    FIELD(SPIDR, SPI_INST, 0, 16)
+REG32(SPIAR, 0x124)
+    FIELD(SPIAR, SDAR, 0, 32)
+REG32(AXIAR0, 0x128)
+    FIELD(AXIAR0, AXIAR_0_31, 0, 32)
+REG32(AXIAR1, 0x12c)
+    FIELD(AXIAR1, AXIAR_32_63, 0, 32)
+REG32(AXIECR, 0x130)
+    FIELD(AXIECR, AXIECR, 0, 1)
+REG32(DONECR, 0x134)
+    FIELD(DONECR, DONECR, 0, 1)
+REG32(RSVD_138, 0x138)
+REG32(RSVD_13C, 0x13c)
+REG32(XIP_WRITE_INCR_INST, 0x140)
+REG32(XIP_WRITE_WRAP_INST, 0x144)
+REG32(XIP_WRITE_CTRL, 0x148)
+
+#define DW_SSI_CTRLR0_STANDARD_WRITABLE_MASK \
+    (R_CTRLR0_DFS_MASK | \
+     R_CTRLR0_SCPH_MASK | \
+     R_CTRLR0_SCPOL_MASK | \
+     R_CTRLR0_TMOD_MASK | \
+     R_CTRLR0_SLV_OE_MASK | \
+     R_CTRLR0_SRL_MASK | \
+     R_CTRLR0_SSTE_MASK | \
+     R_CTRLR0_CFS_MASK)
+
+#define DW_SSI_CTRLR1_WRITABLE_MASK R_CTRLR1_NDF_MASK
+#define DW_SSI_MWCR_WRITABLE_MASK \
+    (R_MWCR_MWMOD_MASK | R_MWCR_MDD_MASK)
+#define DW_SSI_BAUDR_WRITABLE_MASK R_BAUDR_SCKDV_MASK
+#define DW_SSI_TXFTLR_WRITABLE_MASK \
+    (R_TXFTLR_TFT_MASK | R_TXFTLR_TXFTHR_MASK)
+#define DW_SSI_RXFTLR_WRITABLE_MASK R_RXFTLR_RFT_MASK
+#define DW_SSI_IMR_WRITABLE_MASK \
+    (R_IMR_TXEIM_MASK | R_IMR_TXOIM_MASK | R_IMR_RXUIM_MASK | \
+     R_IMR_RXOIM_MASK | R_IMR_RXFIM_MASK | R_IMR_MSTIM_MASK | \
+     R_IMR_TXUIM_MASK)
+#define DW_SSI_RX_SAMPLE_DELAY_WRITABLE_MASK \
+    (R_RX_SAMPLE_DELAY_RSD_MASK | R_RX_SAMPLE_DELAY_SE_MASK)
+
+static bool dw_ssi_validate_config(DwSsiState *s, Error **errp)
+{
+    DeviceState *dev = DEVICE(s);
+
+    if (s->cfg.num_cs == 0 || s->cfg.num_cs > 8) {
+        error_setg(errp, "%s: num-cs must be in range 1..8",
+                   dev->canonical_path);
+        return false;
+    }
+
+    if (s->cfg.fifo_depth < 2 || s->cfg.fifo_depth > 256) {
+        error_setg(errp, "%s: fifo-depth must be in range 2..256",
+                   dev->canonical_path);
+        return false;
+    }
+
+    if (s->cfg.imr_reset & ~DW_SSI_IMR_WRITABLE_MASK) {
+        error_setg(errp, "%s: imr-reset contains unsupported bits",
+                   dev->canonical_path);
+        return false;
+    }
+
+    return true;
+}
+
+static bool dw_ssi_fifo_threshold_valid(const DwSsiState *s, uint32_t value)
+{
+    return value < s->cfg.fifo_depth;
+}
+
+static void dw_ssi_write_masked(DwSsiState *s, unsigned int reg,
+                                     uint32_t value, uint32_t mask)
+{
+    s->regs[reg] = (s->regs[reg] & ~mask) | (value & mask);
+}
+
+static uint32_t dw_ssi_frame_masked(DwSsiState *s)
+{
+    unsigned int bits = FIELD_EX32(s->regs[R_CTRLR0], CTRLR0, DFS) + 1;
+
+    return bits == 32 ? UINT32_MAX : MAKE_64BIT_MASK(0, bits);
+}
+
+static bool dw_ssi_is_enabled(const DwSsiState *s)
+{
+    return FIELD_EX32(s->regs[R_SSIENR], SSIENR, SSIC_EN);
+}
+
+static void dw_ssi_deselect(DwSsiState *s)
+{
+    if (s->active_cs < 0) {
+        return;
+    }
+
+    qemu_irq_raise(s->cs_lines[s->active_cs]);
+    s->active_cs = -1;
+}
+
+static void dw_ssi_select(DwSsiState *s, unsigned cs)
+{
+    if (cs >= s->cfg.num_cs) {
+        qemu_log_mask(LOG_GUEST_ERROR,
+                      "%s: invalid chip select %u\n",
+                      DEVICE(s)->canonical_path, cs);
+        dw_ssi_deselect(s);
+        return;
+    }
+
+    if (s->active_cs == cs) {
+        return;
+    }
+
+    dw_ssi_deselect(s);
+    qemu_irq_lower(s->cs_lines[cs]);
+    s->active_cs = cs;
+}
+
+static void dw_ssi_update_cs(DwSsiState *s)
+{
+    uint32_t ser = s->regs[R_SER];
+
+    if (!dw_ssi_is_enabled(s) || !ser) {
+        dw_ssi_deselect(s);
+        return;
+    }
+
+    if (ser & (ser - 1)) {
+        qemu_log_mask(LOG_GUEST_ERROR,
+                      "%s: multiple chip selects enabled: 0x%x\n",
+                      DEVICE(s)->canonical_path, ser);
+        dw_ssi_deselect(s);
+        return;
+    }
+
+    dw_ssi_select(s, ctz32(ser));
+}
+
+static void dw_ssi_abort_transfer(DwSsiState *s)
+{
+    dw_ssi_deselect(s);
+    fifo32_reset(&s->tx_fifo);
+    fifo32_reset(&s->rx_fifo);
+    s->phase = DW_SSI_PHASE_IDLE;
+    s->remaining_frames = 0;
+}
+
+static uint32_t dw_ssi_status(DwSsiState *s)
+{
+    uint32_t tx_used = fifo32_num_used(&s->tx_fifo);
+    uint32_t rx_used = fifo32_num_used(&s->rx_fifo);
+    uint32_t sr = 0;
+
+    sr = FIELD_DP32(sr, SR, BUSY, s->phase != DW_SSI_PHASE_IDLE);
+    sr = FIELD_DP32(sr, SR, TFNF, tx_used < s->cfg.fifo_depth);
+    sr = FIELD_DP32(sr, SR, TFE, tx_used == 0);
+    sr = FIELD_DP32(sr, SR, RFNE, rx_used != 0);
+    sr = FIELD_DP32(sr, SR, RFF,
+                    rx_used == s->cfg.fifo_depth);
+
+    return sr;
+}
+
+static void dw_ssi_run_transfer(DwSsiState *s);
+
+static void dw_ssi_push_tx(DwSsiState *s, uint32_t tx)
+{
+    if (!dw_ssi_is_enabled(s)) {
+        return;
+    }
+
+    if (fifo32_is_full(&s->tx_fifo)) {
+        return;
+    }
+
+    fifo32_push(&s->tx_fifo, tx);
+
+    if (s->phase != DW_SSI_PHASE_STANDARD_TX_ONLY) {
+        dw_ssi_run_transfer(s);
+    }
+}
+
+static uint32_t dw_ssi_send_frame(DwSsiState *s,
+                                        uint32_t tx)
+{
+    uint32_t mask = dw_ssi_frame_masked(s);
+    uint32_t rx;
+
+    tx &= mask;
+
+    if (FIELD_EX32(s->regs[R_CTRLR0], CTRLR0, SRL)) {
+        rx = tx;
+    } else {
+        rx = ssi_transfer(s->spi, tx);
+    }
+
+    return rx & mask;
+}
+
+static void dw_ssi_run_transfer(DwSsiState *s)
+{
+    uint32_t tmod;
+
+    if (!dw_ssi_is_enabled(s) || s->active_cs < 0) {
+        return;
+    }
+
+    /* SCKDV=0 disables sclk_out: no transfer can start. */
+    if (FIELD_EX32(s->regs[R_BAUDR], BAUDR, SCKDV) == 0) {
+        return;
+    }
+
+    /*
+     * TRM: the controller starts shifting only after the TX FIFO
+     * occupancy exceeds TXFTLR.TXFTHR.  Transfers already in flight
+     * are not interrupted when the FIFO temporarily drops below the
+     * level.
+     */
+    if (s->phase == DW_SSI_PHASE_IDLE &&
+        fifo32_num_used(&s->tx_fifo) <=
+            FIELD_EX32(s->regs[R_TXFTLR], TXFTLR, TXFTHR)) {
+        return;
+    }
+
+    tmod = FIELD_EX32(s->regs[R_CTRLR0], CTRLR0, TMOD);
+
+    switch (tmod) {
+    case DW_SSI_TMOD_TR:
+        if (s->phase == DW_SSI_PHASE_IDLE) {
+            s->phase = DW_SSI_PHASE_STANDARD_TR;
+        }
+
+        while (!fifo32_is_empty(&s->tx_fifo)) {
+            uint32_t tx = fifo32_pop(&s->tx_fifo);
+            uint32_t rx = dw_ssi_send_frame(s, tx);
+
+            if (!fifo32_is_full(&s->rx_fifo)) {
+                fifo32_push(&s->rx_fifo, rx);
+            } else {
+                qemu_log_mask(LOG_GUEST_ERROR,
+                              "%s: RX FIFO full, dropping frame\n",
+                              DEVICE(s)->canonical_path);
+                break;
+            }
+        }
+
+        if (fifo32_is_empty(&s->tx_fifo)) {
+            s->phase = DW_SSI_PHASE_IDLE;
+        }
+        break;
+    case DW_SSI_TMOD_TO: {
+        unsigned int frames = 0;
+
+        if (fifo32_is_empty(&s->tx_fifo)) {
+            s->phase = DW_SSI_PHASE_IDLE;
+            break;
+        }
+
+        s->phase = DW_SSI_PHASE_STANDARD_TX_ONLY;
+        while (!fifo32_is_empty(&s->tx_fifo) &&
+               frames < DW_SSI_PIO_TX_BATCH) {
+            uint32_t tx = fifo32_pop(&s->tx_fifo);
+
+            dw_ssi_send_frame(s, tx);
+            frames++;
+        }
+        if (fifo32_is_empty(&s->tx_fifo)) {
+            s->phase = DW_SSI_PHASE_IDLE;
+        }
+        break;
+    }
+    case DW_SSI_TMOD_RO:
+        switch (s->phase) {
+        case DW_SSI_PHASE_IDLE:
+            if (fifo32_is_empty(&s->tx_fifo)) {
+                break;
+            }
+            fifo32_pop(&s->tx_fifo);
+            s->phase = DW_SSI_PHASE_RX_ONLY;
+            s->remaining_frames = FIELD_EX32(s->regs[R_CTRLR1], CTRLR1,
+                                              NDF) + 1;
+            /* Fall through. */
+        case DW_SSI_PHASE_RX_ONLY:
+            while (!fifo32_is_full(&s->rx_fifo) &&
+                   s->remaining_frames > 0) {
+                uint32_t rx = dw_ssi_send_frame(s, 0x00);
+
+                fifo32_push(&s->rx_fifo, rx);
+                s->remaining_frames--;
+            }
+            if (s->remaining_frames == 0) {
+                s->phase = DW_SSI_PHASE_IDLE;
+            }
+            break;
+        }
+        break;
+    case DW_SSI_TMOD_EEPROM_READ:
+        switch (s->phase) {
+        case DW_SSI_PHASE_IDLE:
+            if (fifo32_is_empty(&s->tx_fifo)) {
+                break;
+            }
+            s->phase = DW_SSI_PHASE_EEPROM_COMMAND;
+            /* Fall through. */
+        case DW_SSI_PHASE_EEPROM_COMMAND:
+            if (fifo32_is_empty(&s->tx_fifo)) {
+                break;
+            }
+            while (!fifo32_is_empty(&s->tx_fifo)) {
+                uint32_t tx = fifo32_pop(&s->tx_fifo);
+
+                dw_ssi_send_frame(s, tx);
+            }
+            s->phase = DW_SSI_PHASE_EEPROM_DATA;
+            s->remaining_frames = FIELD_EX32(s->regs[R_CTRLR1], CTRLR1,
+                                              NDF) + 1;
+            /* Fall through. */
+        case DW_SSI_PHASE_EEPROM_DATA:
+            while (!fifo32_is_full(&s->rx_fifo) &&
+                   s->remaining_frames > 0) {
+                uint32_t rx = dw_ssi_send_frame(s, 0x00);
+
+                fifo32_push(&s->rx_fifo, rx);
+                s->remaining_frames--;
+            }
+            if (s->remaining_frames == 0) {
+                s->phase = DW_SSI_PHASE_IDLE;
+            }
+            break;
+        }
+        break;
+    default:
+        g_assert_not_reached();
+    }
+}
+
+static bool dw_ssi_is_dr(hwaddr addr)
+{
+    return addr >= A_DR0 && addr <= A_DR_END &&
+           (addr & 0x3) == 0;
+}
+
+static bool dw_ssi_is_unsupported_offset(hwaddr addr)
+{
+    switch (addr) {
+    /* Enhanced SPI extensions. */
+    case A_SPI_CTRLR0:
+    case A_DDR_DRIVE_EDGE:
+
+    /* XIP extensions. */
+    case A_XIP_MODE_BITS:
+    case A_XIP_INCR_INST:
+    case A_XIP_WRAP_INST:
+    case A_XIP_CTRL:
+    case A_XIP_SER:
+    case A_XRXOICR:
+    case A_XIP_CNT_TIME_OUT:
+    case A_SPI_CTRLR1:
+    case A_SPITECR:
+    case A_RSVD_138:
+    case A_RSVD_13C:
+    case A_XIP_WRITE_INCR_INST:
+    case A_XIP_WRITE_WRAP_INST:
+    case A_XIP_WRITE_CTRL:
+
+    /* Internal-AXI DMA/IDMA extensions. */
+    case A_DMACR:
+    case A_AXIAWLEN:
+    case A_AXIARLEN:
+    case A_SPIDR:
+    case A_SPIAR:
+    case A_AXIAR0:
+    case A_AXIAR1:
+    case A_AXIECR:
+    case A_DONECR:
+
+    /* SSI unsupported extensions. */
+    case A_RX_SAMPLE_DELAY:
+        return true;
+    default:
+        return false;
+    }
+}
+
+static bool dw_ssi_write_requires_disabled(hwaddr addr)
+{
+    switch (addr) {
+    case A_CTRLR0:
+    case A_CTRLR1:
+    case A_MWCR:
+    case A_BAUDR:
+        return true;
+    default:
+        return false;
+    }
+}
+
+static uint32_t dw_ssi_unsupported_read(DwSsiState *s,
+                                        hwaddr addr)
+{
+    qemu_log_mask(LOG_UNIMP,
+                  "%s: unsupported read at 0x%" HWADDR_PRIx "\n",
+                  DEVICE(s)->canonical_path, addr);
+    return 0;
+}
+
+static void dw_ssi_unsupported_write(DwSsiState *s,
+                                     hwaddr addr,
+                                     uint32_t value)
+{
+    qemu_log_mask(LOG_UNIMP,
+                  "%s: unsupported write at 0x%" HWADDR_PRIx
+                  " value 0x%08x\n",
+                  DEVICE(s)->canonical_path, addr, value);
+}
+
+static uint64_t dw_ssi_read(void *opaque, hwaddr addr, unsigned int size)
+{
+    DwSsiState *s = DW_SSI(opaque);
+    uint32_t value = 0;
+
+    if (dw_ssi_is_dr(addr)) {
+        if (!fifo32_is_empty(&s->rx_fifo)) {
+            value = fifo32_pop(&s->rx_fifo) & dw_ssi_frame_masked(s);
+        } else {
+            value = 0;
+        }
+
+        dw_ssi_run_transfer(s);
+        return value;
+    }
+
+    if (dw_ssi_is_unsupported_offset(addr)) {
+        value = dw_ssi_unsupported_read(s, addr);
+        return value;
+    }
+
+    switch (addr) {
+    case A_CTRLR0:
+    case A_CTRLR1:
+    case A_SSIENR:
+    case A_MWCR:
+    case A_BAUDR:
+    case A_TXFTLR:
+    case A_RXFTLR:
+    case A_IMR:
+    case A_IDR:
+    case A_SSIC_VERSION_ID:
+        value = s->regs[addr / sizeof(uint32_t)];
+        break;
+
+    case A_SER:
+        value = s->regs[R_SER] & MAKE_64BIT_MASK(0, s->cfg.num_cs);
+        break;
+    case A_TXFLR:
+        value = fifo32_num_used(&s->tx_fifo);
+        if (s->phase == DW_SSI_PHASE_STANDARD_TX_ONLY) {
+            dw_ssi_run_transfer(s);
+        }
+        break;
+    case A_RXFLR:
+        value = fifo32_num_used(&s->rx_fifo);
+        break;
+    case A_SR:
+        value = dw_ssi_status(s);
+        if (s->phase == DW_SSI_PHASE_STANDARD_TX_ONLY) {
+            dw_ssi_run_transfer(s);
+        }
+        break;
+    default:
+        if (addr >= DW_SSI_REGS_SIZE || (addr & 0x3) != 0) {
+            qemu_log_mask(LOG_GUEST_ERROR,
+                          "%s: bad read offset 0x%" HWADDR_PRIx "\n",
+                          DEVICE(s)->canonical_path, addr);
+        }
+        break;
+    }
+
+    return value;
+}
+
+static void dw_ssi_write(void *opaque, hwaddr addr,
+                              uint64_t value, unsigned int size)
+{
+    DwSsiState *s = DW_SSI(opaque);
+
+    if (dw_ssi_is_dr(addr)) {
+        dw_ssi_push_tx(s, value);
+        return;
+    }
+
+    if (dw_ssi_is_unsupported_offset(addr)) {
+        dw_ssi_unsupported_write(s, addr, value);
+        return;
+    }
+
+    if (dw_ssi_write_requires_disabled(addr) &&
+        dw_ssi_is_enabled(s)) {

hw/ssi/dw_ssi.c line 686

Quoted 10 lines
hw/ssi/dw_ssi.c new file +10 -0
+        qemu_log_mask(LOG_GUEST_ERROR,
+                      "%s: write to offset 0x%" HWADDR_PRIx
+                      " while SSI is enabled\n",
+                      DEVICE(s)->canonical_path, addr);
+        return;
+    }
+
+    switch (addr) {
+    case A_CTRLR0:
+        value &= ~R_CTRLR0_SPI_FRF_MASK;

Why does the SPI_FRF field need to be explicity cleared? It will be cleared in dw_ssi_write_masked since it is not contained in the DW_SSI_CTRLR0_STANDARD_WRITABLE_MASK .

Can you also print an unimplemented warning if a non-zero value is written to the SPI_FRF field. Currently configuring the device to enhanced mode is silently ignored.

Quoted 350 lines
hw/ssi/dw_ssi.c new file +270 -0
+        dw_ssi_write_masked(s, R_CTRLR0, value,
+                                 DW_SSI_CTRLR0_STANDARD_WRITABLE_MASK);
+        break;
+    case A_CTRLR1:
+        dw_ssi_write_masked(s, R_CTRLR1, value,
+                                 DW_SSI_CTRLR1_WRITABLE_MASK);
+        break;
+    case A_SSIENR: {
+        bool old_enabled = dw_ssi_is_enabled(s);
+        bool new_enabled = value & R_SSIENR_SSIC_EN_MASK;
+
+        if (old_enabled == new_enabled) {
+            return;
+        }
+
+        s->regs[R_SSIENR] = FIELD_DP32(0, SSIENR, SSIC_EN, new_enabled);
+        if (!new_enabled) {
+            dw_ssi_abort_transfer(s);
+            return;
+        }
+
+        dw_ssi_update_cs(s);
+        dw_ssi_run_transfer(s);
+        break;
+    }
+    case A_MWCR:
+        dw_ssi_write_masked(s, R_MWCR, value,
+                                 DW_SSI_MWCR_WRITABLE_MASK);
+        break;
+    case A_SER: {
+        uint32_t old_ser = s->regs[R_SER];
+        uint32_t new_ser = value & MAKE_64BIT_MASK(0, s->cfg.num_cs);
+
+        /*
+         * Changing the selected slave terminates a pending transaction:
+         * the FIFOs are flushed only by clearing SSIENR (TRM), but a
+         * stale RO/EEPROM phase must not resume on the new chip select.
+         */
+        if (old_ser != new_ser && s->phase != DW_SSI_PHASE_IDLE) {
+            s->phase = DW_SSI_PHASE_IDLE;
+            s->remaining_frames = 0;
+        }
+        s->regs[R_SER] = new_ser;
+
+        dw_ssi_update_cs(s);
+        dw_ssi_run_transfer(s);
+        break;
+    }
+    case A_BAUDR:
+        dw_ssi_write_masked(s, R_BAUDR, value,
+                                 DW_SSI_BAUDR_WRITABLE_MASK);
+        break;
+    case A_TXFTLR:
+        if (!dw_ssi_fifo_threshold_valid(
+                s, FIELD_EX32(value, TXFTLR, TFT)) ||
+            !dw_ssi_fifo_threshold_valid(
+                s, FIELD_EX32(value, TXFTLR, TXFTHR))) {
+            qemu_log_mask(LOG_GUEST_ERROR,
+                          "%s: invalid txftlr value %x\n",
+                          DEVICE(s)->canonical_path, (unsigned int)value);
+            break;
+        }
+        dw_ssi_write_masked(s, R_TXFTLR, value,
+                                 DW_SSI_TXFTLR_WRITABLE_MASK);
+        dw_ssi_run_transfer(s);
+        break;
+    case A_RXFTLR:
+        if (!dw_ssi_fifo_threshold_valid(
+                s, FIELD_EX32(value, RXFTLR, RFT))) {
+            qemu_log_mask(LOG_GUEST_ERROR,
+                          "%s: invalid rxftlr value %x\n",
+                          DEVICE(s)->canonical_path, (unsigned int)value);
+            break;
+        }
+        dw_ssi_write_masked(s, R_RXFTLR, value,
+                                 DW_SSI_RXFTLR_WRITABLE_MASK);
+        break;
+    case A_TXFLR:
+    case A_RXFLR:
+    case A_SR:
+        break;
+    case A_IMR:
+        dw_ssi_write_masked(s, R_IMR, value,
+                                 DW_SSI_IMR_WRITABLE_MASK);
+        break;
+    case A_IDR:
+    case A_SSIC_VERSION_ID:
+        break;
+    default:
+        if (addr >= DW_SSI_REGS_SIZE || (addr & 0x3) != 0) {
+            qemu_log_mask(LOG_GUEST_ERROR,
+                          "%s: bad write offset 0x%" HWADDR_PRIx "\n",
+                          DEVICE(s)->canonical_path, addr);
+        }
+        break;
+    }
+}
+
+static const MemoryRegionOps dw_ssi_ops = {
+    .read = dw_ssi_read,
+    .write = dw_ssi_write,
+    .endianness = DEVICE_LITTLE_ENDIAN,
+    .impl = {
+        .min_access_size = 4,
+        .max_access_size = 4,
+        .unaligned = false,
+    },
+    .valid = {
+        .min_access_size = 4,
+        .max_access_size = 4,
+        .unaligned = false,
+    },
+};
+
+static void dw_ssi_enter_reset(Object *obj, ResetType type)
+{
+    DwSsiState *s = DW_SSI(obj);
+
+    memset(s->regs, 0, sizeof(s->regs));
+    fifo32_reset(&s->tx_fifo);
+    fifo32_reset(&s->rx_fifo);
+    s->phase = DW_SSI_PHASE_IDLE;
+    s->remaining_frames = 0;
+
+    s->regs[R_CTRLR0] = DW_SSI_CTRLR0_RESET;
+    s->regs[R_SR] = DW_SSI_SR_RESET;
+    s->regs[R_IMR] = s->cfg.imr_reset & DW_SSI_IMR_WRITABLE_MASK;
+    s->regs[R_IDR] = DW_SSI_IDR_RESET;
+    s->regs[R_SSIC_VERSION_ID] = DW_SSI_VERSION;
+}
+
+static void dw_ssi_hold_reset(Object *obj, ResetType type)
+{
+    DwSsiState *s = DW_SSI(obj);
+    s->active_cs = -1;
+
+    if (s->cs_lines) {
+        for (int i = 0; i < s->cfg.num_cs; i++) {
+            qemu_irq_raise(s->cs_lines[i]);
+        }
+    }
+}
+
+static int dw_ssi_post_load(void *opaque, int version_id)
+{
+    DwSsiState *s = opaque;
+    uint32_t ndf = FIELD_EX32(s->regs[R_CTRLR1], CTRLR1, NDF) + 1;
+
+    if (s->active_cs < -1 || s->active_cs >= (int)s->cfg.num_cs) {
+        return -EINVAL;
+    }
+    if (s->phase > DW_SSI_PHASE_STANDARD_TR) {
+        return -EINVAL;
+    }
+    if (s->phase == DW_SSI_PHASE_RX_ONLY ||
+        s->phase == DW_SSI_PHASE_EEPROM_DATA) {
+        if (s->remaining_frames == 0 || s->remaining_frames > ndf) {
+            return -EINVAL;
+        }
+    } else if (s->remaining_frames != 0) {
+        return -EINVAL;
+    }
+    if (s->active_cs >= 0 &&
+        (!dw_ssi_is_enabled(s) ||
+         s->regs[R_SER] != BIT(s->active_cs))) {
+        return -EINVAL;
+    }
+
+    for (int i = 0; i < s->cfg.num_cs; i++) {
+        qemu_irq_raise(s->cs_lines[i]);
+    }
+    if (s->active_cs >= 0) {
+        qemu_irq_lower(s->cs_lines[s->active_cs]);
+    }
+
+    return 0;
+}
+
+static const VMStateDescription vmstate_dw_ssi = {
+    .name = TYPE_DW_SSI,
+    .version_id = 1,
+    .minimum_version_id = 1,
+    .post_load = dw_ssi_post_load,
+    .fields = (const VMStateField[]) {
+        VMSTATE_UINT32_EQUAL(cfg.num_cs, DwSsiState),
+        VMSTATE_UINT32_EQUAL(cfg.fifo_depth, DwSsiState),
+        VMSTATE_UINT32_EQUAL(cfg.imr_reset, DwSsiState),
+        VMSTATE_UINT32_ARRAY(regs, DwSsiState, DW_SSI_NUM_REGS),
+        VMSTATE_FIFO32(tx_fifo, DwSsiState),
+        VMSTATE_FIFO32(rx_fifo, DwSsiState),
+        VMSTATE_UINT32(phase, DwSsiState),
+        VMSTATE_UINT32(remaining_frames, DwSsiState),
+        VMSTATE_INT32(active_cs, DwSsiState),
+        VMSTATE_END_OF_LIST()
+    },
+};
+
+static void dw_ssi_init(Object *obj)
+{
+    DwSsiState *s = DW_SSI(obj);
+    DeviceState *dev = DEVICE(obj);
+    SysBusDevice *sbd = SYS_BUS_DEVICE(obj);
+
+    s->spi = ssi_create_bus(dev, "spi");
+
+    memory_region_init_io(&s->mmio, obj, &dw_ssi_ops, s,
+                          TYPE_DW_SSI, DW_SSI_MMIO_SIZE);
+    sysbus_init_mmio(sbd, &s->mmio);
+
+    s->active_cs = -1;
+}
+
+static void dw_ssi_realize(DeviceState *dev, Error **errp)
+{
+    DwSsiState *s = DW_SSI(dev);
+
+    if (!dw_ssi_validate_config(s, errp)) {
+        return;
+    }
+
+    s->cs_lines = g_new0(qemu_irq, s->cfg.num_cs);
+    qdev_init_gpio_out_named(dev, s->cs_lines, "cs", s->cfg.num_cs);
+
+    fifo32_create(&s->tx_fifo, s->cfg.fifo_depth);
+    fifo32_create(&s->rx_fifo, s->cfg.fifo_depth);
+}
+
+static void dw_ssi_finalize(Object *obj)
+{
+    DwSsiState *s = DW_SSI(obj);
+
+    fifo32_destroy(&s->tx_fifo);
+    fifo32_destroy(&s->rx_fifo);
+    g_free(s->cs_lines);
+}
+
+static const Property dw_ssi_properties[] = {
+    DEFINE_PROP_UINT32("num-cs", DwSsiState, cfg.num_cs, 1),
+    DEFINE_PROP_UINT32("fifo-depth", DwSsiState, cfg.fifo_depth, 256),
+    DEFINE_PROP_UINT32("imr-reset", DwSsiState,
+                       cfg.imr_reset, 0x0000003f),
+};
+
+static void dw_ssi_class_init(ObjectClass *klass, const void *data)
+{
+    DeviceClass *dc = DEVICE_CLASS(klass);
+    ResettableClass *rc = RESETTABLE_CLASS(klass);
+
+    dc->realize = dw_ssi_realize;
+    dc->vmsd = &vmstate_dw_ssi;
+    device_class_set_props(dc, dw_ssi_properties);
+    rc->phases.enter = dw_ssi_enter_reset;
+    rc->phases.hold = dw_ssi_hold_reset;
+}
+
+static const TypeInfo dw_ssi_info = {
+    .name = TYPE_DW_SSI,
+    .parent = TYPE_SYS_BUS_DEVICE,
+    .instance_size = sizeof(DwSsiState),
+    .instance_init = dw_ssi_init,
+    .instance_finalize = dw_ssi_finalize,
+    .class_init = dw_ssi_class_init,
+};
+
+static void dw_ssi_register_types(void)
+{
+    type_register_static(&dw_ssi_info);
+}
+
+type_init(dw_ssi_register_types)
hw/ssi/meson.build +1 -0
index 6afb1ea200..447780846c 100644
@@ -1,6 +1,7 @@
  system_ss.add(when: 'CONFIG_ALLWINNER_A10_SPI', if_true: files('allwinner-a10-spi.c'))
  system_ss.add(when: 'CONFIG_ASPEED_SOC', if_true: files('aspeed_smc.c'))
  system_ss.add(when: 'CONFIG_MSF2', if_true: files('mss-spi.c'))
+system_ss.add(when: 'CONFIG_DW_SSI', if_true: files('dw_ssi.c'))
  system_ss.add(when: 'CONFIG_NPCM7XX', if_true: files('npcm7xx_fiu.c', 'npcm_pspi.c'))
  system_ss.add(when: 'CONFIG_PL022', if_true: files('pl022.c'))
  system_ss.add(when: 'CONFIG_SIFIVE_SPI', if_true: files('sifive_spi.c'))
include/hw/ssi/dw_ssi.h new file +62 -0
new file mode 100644
index 0000000000..a74e906ef8
@@ -0,0 +1,62 @@
+/*
+ * Synopsys DesignWare SSI
+ *
+ * Copyright (c) 2026 Kangjie Huang <[email protected]>
+ *
+ * SPDX-License-Identifier: GPL-2.0-or-later
+ *
+ * Emulates the DesignWare SSI controller in Standard SPI mode,
+ * covering the PIO/FIFO data path and chip selects.
+ */
+
+#ifndef HW_SSI_DW_SSI_H
+#define HW_SSI_DW_SSI_H
+
+#include "hw/core/sysbus.h"
+#include "hw/ssi/ssi.h"
+#include "qemu/fifo32.h"
+#include "qom/object.h"
+
+#define TYPE_DW_SSI "designware-ssi"
+OBJECT_DECLARE_SIMPLE_TYPE(DwSsiState, DW_SSI)
+
+#define DW_SSI_MMIO_SIZE 0x1000
+#define DW_SSI_REGS_SIZE 0x14c
+#define DW_SSI_NUM_REGS \
+    (DW_SSI_REGS_SIZE / sizeof(uint32_t))
+
+typedef enum DwSsiPhase {
+    DW_SSI_PHASE_IDLE,
+    DW_SSI_PHASE_STANDARD_TX_ONLY,
+    DW_SSI_PHASE_RX_ONLY,
+    DW_SSI_PHASE_EEPROM_COMMAND,
+    DW_SSI_PHASE_EEPROM_DATA,
+    DW_SSI_PHASE_STANDARD_TR,
+} DwSsiPhase;
+
+typedef struct DwSsiConfig {
+    uint32_t num_cs;
+    uint32_t fifo_depth;
+    uint32_t imr_reset;
+} DwSsiConfig;
+
+struct DwSsiState {
+    SysBusDevice parent_obj;
+
+    MemoryRegion mmio;
+    SSIBus *spi;
+
+    qemu_irq *cs_lines;
+
+    Fifo32 tx_fifo;
+    Fifo32 rx_fifo;
+    uint32_t regs[DW_SSI_NUM_REGS];
+
+    DwSsiConfig cfg;
+
+    uint32_t phase;
+    uint32_t remaining_frames;
+
+    int active_cs;
+};
+#endif /* HW_SSI_DW_SSI_H */
#9 lore raw

PATCH v2 2/5 hw/ssi: Add DesignWare SSI standard interrupt support

In reply to Kangjie Huang

Implement raw and masked SSI interrupt status, threshold interrupts, and the documented read-clear registers.

TXE and RXF are derived from FIFO levels. TXO, RXO, and RXU are latched causes. Expose the nine physical interrupt outputs used by the K230 SSI integration.

TXU, DONE, and AXIE depend on transfer engines outside the Standard PIO scope and remain deasserted in this model.

---
 hw/ssi/dw_ssi.c         | 129 +++++++++++++++++++++++++++++++++++++++-
 include/hw/ssi/dw_ssi.h |  18 +++++-
 2 files changed, 145 insertions(+), 2 deletions(-)
hw/ssi/dw_ssi.c +128 -1
index f9f0e23730..57bca1687b 100644
@@ -6,7 +6,7 @@
  * SPDX-License-Identifier: GPL-2.0-or-later
  *
  * Emulates the DesignWare SSI controller in Standard SPI mode,
- * covering the PIO/FIFO data path and chip selects.
+ * covering the PIO/FIFO data path, interrupt outputs and chip selects.
  */
 
 #include "qemu/osdep.h"
@@ -26,6 +26,9 @@
 #define DW_SSI_IDR_RESET               0xa1b2c3d5
 #define DW_SSI_VERSION                 0x3130332a
 #define DW_SSI_PIO_TX_BATCH            64
+#define DW_SSI_IRQ_VALID_MASK          0x000009bf
+#define DW_SSI_IMPLEMENTED_LATCHED_IRQ_MASK \
+    (R_RISR_TXOIR_MASK | R_RISR_RXOIR_MASK | R_RISR_RXUIR_MASK)
 
 enum {
     DW_SSI_TMOD_TR,
@@ -217,6 +220,22 @@ REG32(XIP_WRITE_CTRL, 0x148)
 #define DW_SSI_RX_SAMPLE_DELAY_WRITABLE_MASK \
     (R_RX_SAMPLE_DELAY_RSD_MASK | R_RX_SAMPLE_DELAY_SE_MASK)
 
+/*
+ * TXU, DONE and AXIE depend on transfer engines outside Standard PIO.
+ * Their physical outputs are exposed but remain deasserted in this model.
+ */
+static const uint32_t dw_ssi_irq_status_mask[DW_SSI_IRQ_COUNT] = {
+    [DW_SSI_IRQ_TXE] = R_RISR_TXEIR_MASK,
+    [DW_SSI_IRQ_TXO] = R_RISR_TXOIR_MASK,
+    [DW_SSI_IRQ_RXF] = R_RISR_RXFIR_MASK,
+    [DW_SSI_IRQ_RXO] = R_RISR_RXOIR_MASK,
+    [DW_SSI_IRQ_TXU] = R_RISR_TXUIR_MASK,
+    [DW_SSI_IRQ_RXU] = R_RISR_RXUIR_MASK,
+    [DW_SSI_IRQ_MST] = R_RISR_MSTIR_MASK,
+    [DW_SSI_IRQ_DONE] = R_RISR_DONER_MASK,
+    [DW_SSI_IRQ_AXIE] = R_RISR_AXIER_MASK,
+};
+
 static bool dw_ssi_validate_config(DwSsiState *s, Error **errp)
 {
     DeviceState *dev = DEVICE(s);
@@ -253,6 +272,45 @@ static void dw_ssi_write_masked(DwSsiState *s, unsigned int reg,
     s->regs[reg] = (s->regs[reg] & ~mask) | (value & mask);
 }
 
+static uint32_t dw_ssi_irq_raw_status(DwSsiState *s)
+{
+    uint32_t status = s->irq_latched;
+    uint32_t tx_used = fifo32_num_used(&s->tx_fifo);
+    uint32_t rx_used = fifo32_num_used(&s->rx_fifo);
+    uint32_t tx_threshold =
+        FIELD_EX32(s->regs[R_TXFTLR], TXFTLR, TFT);
+    uint32_t rx_threshold =
+        FIELD_EX32(s->regs[R_RXFTLR], RXFTLR, RFT);
+
+    if (tx_used <= tx_threshold) {
+        status |= R_RISR_TXEIR_MASK;
+    }
+    if (rx_used > rx_threshold) {
+        status |= R_RISR_RXFIR_MASK;
+    }
+    return status & DW_SSI_IRQ_VALID_MASK;
+}
+
+static void dw_ssi_update_irq(DwSsiState *s)
+{
+    uint32_t status = dw_ssi_irq_raw_status(s) &
+                      s->regs[R_IMR] & DW_SSI_IRQ_VALID_MASK;
+
+    for (int i = 0; i < DW_SSI_IRQ_COUNT; i++) {
+        qemu_set_irq(s->irqs[i], !!(status & dw_ssi_irq_status_mask[i]));
+    }
+}
+
+static uint32_t dw_ssi_irq_read_clear(DwSsiState *s,
+                                            uint32_t clear_mask)
+{
+    uint32_t active = s->irq_latched & clear_mask;
+
+    s->irq_latched &= ~clear_mask;
+    dw_ssi_update_irq(s);
+    return !!active;
+}
+
 static uint32_t dw_ssi_frame_masked(DwSsiState *s)
 {
     unsigned int bits = FIELD_EX32(s->regs[R_CTRLR0], CTRLR0, DFS) + 1;
@@ -321,6 +379,7 @@ static void dw_ssi_abort_transfer(DwSsiState *s)
     fifo32_reset(&s->rx_fifo);
     s->phase = DW_SSI_PHASE_IDLE;
     s->remaining_frames = 0;
+    dw_ssi_update_irq(s);
 }
 
 static uint32_t dw_ssi_status(DwSsiState *s)
@@ -348,6 +407,8 @@ static void dw_ssi_push_tx(DwSsiState *s, uint32_t tx)
     }
 
     if (fifo32_is_full(&s->tx_fifo)) {
+        s->irq_latched |= R_RISR_TXOIR_MASK;
+        dw_ssi_update_irq(s);
         return;
     }
 
@@ -356,6 +417,7 @@ static void dw_ssi_push_tx(DwSsiState *s, uint32_t tx)
     if (s->phase != DW_SSI_PHASE_STANDARD_TX_ONLY) {
         dw_ssi_run_transfer(s);
     }
+    dw_ssi_update_irq(s);
 }
 
 static uint32_t dw_ssi_send_frame(DwSsiState *s,
@@ -415,6 +477,7 @@ static void dw_ssi_run_transfer(DwSsiState *s)
             if (!fifo32_is_full(&s->rx_fifo)) {
                 fifo32_push(&s->rx_fifo, rx);
             } else {
+                s->irq_latched |= R_RISR_RXOIR_MASK;
                 qemu_log_mask(LOG_GUEST_ERROR,
                               "%s: RX FIFO full, dropping frame\n",
                               DEVICE(s)->canonical_path);
@@ -602,9 +665,11 @@ static uint64_t dw_ssi_read(void *opaque, hwaddr addr, unsigned int size)
             value = fifo32_pop(&s->rx_fifo) & dw_ssi_frame_masked(s);
         } else {
             value = 0;
+            s->irq_latched |= R_RISR_RXUIR_MASK;
         }
 
         dw_ssi_run_transfer(s);
+        dw_ssi_update_irq(s);
         return value;
     }
 
@@ -634,6 +699,7 @@ static uint64_t dw_ssi_read(void *opaque, hwaddr addr, unsigned int size)
         value = fifo32_num_used(&s->tx_fifo);
         if (s->phase == DW_SSI_PHASE_STANDARD_TX_ONLY) {
             dw_ssi_run_transfer(s);
+            dw_ssi_update_irq(s);
         }
         break;
     case A_RXFLR:
@@ -643,8 +709,35 @@ static uint64_t dw_ssi_read(void *opaque, hwaddr addr, unsigned int size)
         value = dw_ssi_status(s);
         if (s->phase == DW_SSI_PHASE_STANDARD_TX_ONLY) {
             dw_ssi_run_transfer(s);
+            dw_ssi_update_irq(s);
         }
         break;
+    case A_ISR:
+        value = dw_ssi_irq_raw_status(s) & s->regs[R_IMR] &
+                DW_SSI_IRQ_VALID_MASK;
+        break;
+    case A_RISR:
+        value = dw_ssi_irq_raw_status(s);
+        break;
+    case A_TXEICR:
+        value = dw_ssi_irq_read_clear(
+            s, R_RISR_TXOIR_MASK | R_RISR_TXUIR_MASK);
+        break;
+    case A_RXOICR:
+        value = dw_ssi_irq_read_clear(s, R_RISR_RXOIR_MASK);
+        break;
+    case A_RXUICR:
+        value = dw_ssi_irq_read_clear(s, R_RISR_RXUIR_MASK);
+        break;
+    case A_MSTICR:
+        value = dw_ssi_irq_read_clear(s, R_RISR_MSTIR_MASK);
+        break;
+    case A_ICR:
+        value = dw_ssi_irq_read_clear(
+            s, R_RISR_TXOIR_MASK | R_RISR_RXUIR_MASK |
+               R_RISR_RXOIR_MASK | R_RISR_MSTIR_MASK);
+        break;
+
     default:
         if (addr >= DW_SSI_REGS_SIZE || (addr & 0x3) != 0) {
             qemu_log_mask(LOG_GUEST_ERROR,
@@ -707,6 +800,7 @@ static void dw_ssi_write(void *opaque, hwaddr addr,
 
         dw_ssi_update_cs(s);
         dw_ssi_run_transfer(s);
+        dw_ssi_update_irq(s);
         break;
     }
     case A_MWCR:
@@ -730,6 +824,7 @@ static void dw_ssi_write(void *opaque, hwaddr addr,
 
         dw_ssi_update_cs(s);
         dw_ssi_run_transfer(s);
+        dw_ssi_update_irq(s);
         break;
     }
     case A_BAUDR:
@@ -749,6 +844,7 @@ static void dw_ssi_write(void *opaque, hwaddr addr,
         dw_ssi_write_masked(s, R_TXFTLR, value,
                                  DW_SSI_TXFTLR_WRITABLE_MASK);
         dw_ssi_run_transfer(s);
+        dw_ssi_update_irq(s);
         break;
     case A_RXFTLR:
         if (!dw_ssi_fifo_threshold_valid(
@@ -760,6 +856,7 @@ static void dw_ssi_write(void *opaque, hwaddr addr,
         }
         dw_ssi_write_masked(s, R_RXFTLR, value,
                                  DW_SSI_RXFTLR_WRITABLE_MASK);
+        dw_ssi_update_irq(s);
         break;
     case A_TXFLR:
     case A_RXFLR:
@@ -768,6 +865,16 @@ static void dw_ssi_write(void *opaque, hwaddr addr,
     case A_IMR:
         dw_ssi_write_masked(s, R_IMR, value,
                                  DW_SSI_IMR_WRITABLE_MASK);
+        dw_ssi_update_irq(s);
+        break;
+    case A_ISR:
+    case A_RISR:
+        break;
+    case A_TXEICR:
+    case A_RXOICR:
+    case A_RXUICR:
+    case A_MSTICR:
+    case A_ICR:
         break;
     case A_IDR:
     case A_SSIC_VERSION_ID:
@@ -807,12 +914,15 @@ static void dw_ssi_enter_reset(Object *obj, ResetType type)
     fifo32_reset(&s->rx_fifo);
     s->phase = DW_SSI_PHASE_IDLE;
     s->remaining_frames = 0;
+    s->irq_latched = 0;
 
     s->regs[R_CTRLR0] = DW_SSI_CTRLR0_RESET;
     s->regs[R_SR] = DW_SSI_SR_RESET;
     s->regs[R_IMR] = s->cfg.imr_reset & DW_SSI_IMR_WRITABLE_MASK;
     s->regs[R_IDR] = DW_SSI_IDR_RESET;
     s->regs[R_SSIC_VERSION_ID] = DW_SSI_VERSION;
+
+    dw_ssi_update_irq(s);
 }
 
 static void dw_ssi_hold_reset(Object *obj, ResetType type)
@@ -827,6 +937,13 @@ static void dw_ssi_hold_reset(Object *obj, ResetType type)
     }
 }
 
+static void dw_ssi_exit_reset(Object *obj, ResetType type)
+{
+    DwSsiState *s = DW_SSI(obj);
+
+    dw_ssi_update_irq(s);
+}
+
 static int dw_ssi_post_load(void *opaque, int version_id)
 {
     DwSsiState *s = opaque;
@@ -835,6 +952,9 @@ static int dw_ssi_post_load(void *opaque, int version_id)
     if (s->active_cs < -1 || s->active_cs >= (int)s->cfg.num_cs) {
         return -EINVAL;
     }
+    if (s->irq_latched & ~DW_SSI_IMPLEMENTED_LATCHED_IRQ_MASK) {
+        return -EINVAL;
+    }
     if (s->phase > DW_SSI_PHASE_STANDARD_TR) {
         return -EINVAL;
     }
@@ -859,6 +979,7 @@ static int dw_ssi_post_load(void *opaque, int version_id)
         qemu_irq_lower(s->cs_lines[s->active_cs]);
     }
 
+    dw_ssi_update_irq(s);
     return 0;
 }
 
@@ -874,6 +995,7 @@ static const VMStateDescription vmstate_dw_ssi = {
         VMSTATE_UINT32_ARRAY(regs, DwSsiState, DW_SSI_NUM_REGS),
         VMSTATE_FIFO32(tx_fifo, DwSsiState),
         VMSTATE_FIFO32(rx_fifo, DwSsiState),
+        VMSTATE_UINT32(irq_latched, DwSsiState),
         VMSTATE_UINT32(phase, DwSsiState),
         VMSTATE_UINT32(remaining_frames, DwSsiState),
         VMSTATE_INT32(active_cs, DwSsiState),
@@ -893,6 +1015,10 @@ static void dw_ssi_init(Object *obj)
                           TYPE_DW_SSI, DW_SSI_MMIO_SIZE);
     sysbus_init_mmio(sbd, &s->mmio);
 
+    for (int i = 0; i < DW_SSI_IRQ_COUNT; i++) {
+        sysbus_init_irq(sbd, &s->irqs[i]);
+    }
+
     s->active_cs = -1;
 }
 
@@ -937,6 +1063,7 @@ static void dw_ssi_class_init(ObjectClass *klass, const void *data)
     device_class_set_props(dc, dw_ssi_properties);
     rc->phases.enter = dw_ssi_enter_reset;
     rc->phases.hold = dw_ssi_hold_reset;
+    rc->phases.exit = dw_ssi_exit_reset;
 }
 
 static const TypeInfo dw_ssi_info = {
include/hw/ssi/dw_ssi.h +17 -1
index a74e906ef8..4d4e1fa4c8 100644
@@ -6,7 +6,7 @@
  * SPDX-License-Identifier: GPL-2.0-or-later
  *
  * Emulates the DesignWare SSI controller in Standard SPI mode,
- * covering the PIO/FIFO data path and chip selects.
+ * covering the PIO/FIFO data path, interrupt outputs and chip selects.
  */
 
 #ifndef HW_SSI_DW_SSI_H
@@ -25,6 +25,20 @@ OBJECT_DECLARE_SIMPLE_TYPE(DwSsiState, DW_SSI)
 #define DW_SSI_NUM_REGS \
     (DW_SSI_REGS_SIZE / sizeof(uint32_t))
 
+/* SSI GPIO output ordering differs from RISR/ISR bit ordering. */
+typedef enum DwSsiIrq {
+    DW_SSI_IRQ_TXE,
+    DW_SSI_IRQ_TXO,
+    DW_SSI_IRQ_RXF,
+    DW_SSI_IRQ_RXO,
+    DW_SSI_IRQ_TXU,
+    DW_SSI_IRQ_RXU,
+    DW_SSI_IRQ_MST,
+    DW_SSI_IRQ_DONE,
+    DW_SSI_IRQ_AXIE,
+    DW_SSI_IRQ_COUNT,
+} DwSsiIrq;
+
 typedef enum DwSsiPhase {
     DW_SSI_PHASE_IDLE,
     DW_SSI_PHASE_STANDARD_TX_ONLY,
@@ -47,6 +61,7 @@ struct DwSsiState {
     SSIBus *spi;
 
     qemu_irq *cs_lines;
+    qemu_irq irqs[DW_SSI_IRQ_COUNT];
 
     Fifo32 tx_fifo;
     Fifo32 rx_fifo;
@@ -54,6 +69,7 @@ struct DwSsiState {
 
     DwSsiConfig cfg;
 
+    uint32_t irq_latched;
     uint32_t phase;
     uint32_t remaining_frames;
 
Signature · 1 line
2.43.0
#10 lore raw

PATCH v2 3/5 hw/riscv/k230: Instantiate DesignWare SSI controllers

In reply to Kangjie Huang

Instantiate the three DesignWare SSI profiles used by K230 and map their controller MMIO regions at the documented addresses.

Configure the QSPI0, QSPI1, and SPI-OPI instances with their chip-select, FIFO-depth, and interrupt-mask reset profiles.

Add K230 SSI qtests for Standard PIO transfers, register masks, FIFO behaviour, interrupt state, unsupported-register semantics, and reset profiles. K230 is used as the hardware carrier for the generic controller tests; PLIC routing is added by the next patch.

---
 docs/system/riscv/k230.rst     |   1 +
 hw/riscv/Kconfig               |   1 +
 hw/riscv/k230.c                |  79 ++++--
 include/hw/riscv/k230.h        |   2 +
 tests/qtest/k230-dw-ssi-test.c | 469 +++++++++++++++++++++++++++++++++
 tests/qtest/meson.build        |   4 +-
 6 files changed, 540 insertions(+), 16 deletions(-)
 create mode 100644 tests/qtest/k230-dw-ssi-test.c
docs/system/riscv/k230.rst +1 -0
index cea8202e55..7a49cac95b 100644
@@ -20,6 +20,7 @@ The ``k230`` machine supports the following devices:
 * Platform-Level Interrupt Controller (PLIC)
 * 2 K230 Watchdog Timer
 * 5 UART
+* 3 K230 SSI controllers for SPI only
 
 Boot options
 ------------
hw/riscv/Kconfig +1 -0
index de37c08cae..60a260691c 100644
@@ -162,3 +162,4 @@ config K230
     select SERIAL_MM
     select UNIMP
     select K230_WDT
+    select DW_SSI
hw/riscv/k230.c +64 -15

124 lines not on this page · raw message

include/hw/riscv/k230.h +2 -0
index 592e1c26bf..99996a93b1 100644
@@ -17,6 +17,7 @@
 
 #include "hw/core/boards.h"
 #include "hw/riscv/riscv_hart.h"
+#include "hw/ssi/dw_ssi.h"
 #include "hw/watchdog/k230_wdt.h"
 
 #define C908_CPU_HARTID   (0)
@@ -33,6 +34,7 @@ typedef struct K230SoCState {
     RISCVHartArrayState c908_cpu; /* Small core */
 
     K230WdtState wdt[2];
+    DwSsiState dw_ssi[3];
     MemoryRegion sram;
     MemoryRegion bootrom;
 
tests/qtest/k230-dw-ssi-test.c new file +469 -0

469 lines not on this page · raw message

tests/qtest/meson.build +3 -1
index 56ff860e21..f5bcc7bece 100644
@@ -297,7 +297,8 @@ qtests_riscv64 = ['riscv-csr-test'] + \
   (config_all_devices.has_key('CONFIG_IOMMU_TESTDEV') and
    config_all_devices.has_key('CONFIG_RISCV_IOMMU') ?
    ['iommu-riscv-test'] : []) + \
-  (config_all_devices.has_key('CONFIG_K230') ? ['k230-wdt-test'] : [])
+  (config_all_devices.has_key('CONFIG_K230') ?
+   ['k230-wdt-test', 'k230-dw-ssi-test'] : [])
 
 qtests_hexagon = ['boot-serial-test']
 
@@ -400,6 +401,7 @@ qtests = {
                        [gio],
   'erst-test': files('erst-test.c'),
   'ivshmem-test': [rt, '../../contrib/ivshmem-server/ivshmem-server.c'],
+  'k230-dw-ssi-test': files('k230-dw-ssi-test.c'),
   'migration-test': test_migration_files + migration_tls_files + migration_colo_files,
   'pxe-test': files('boot-sector.c'),
   'pnv-xive2-test': files('pnv-xive2-common.c', 'pnv-xive2-flush-sync.c',
Signature · 1 line
2.43.0
#11 lore raw

PATCH v2 4/5 hw/riscv/k230: Route SSI interrupts to the PLIC

In reply to Kangjie Huang

Connect the nine interrupt outputs of each K230 SSI controller to the documented PLIC source range.

The TXU, DONE, and AXIE lines are wired to preserve the physical K230 interrupt topology, but remain inactive until their transfer engines are modeled.

Extend qtests to cover TXE routing for all instances and RXU routing and instance isolation.

---
 hw/riscv/k230.c                | 36 +++++++++++++++++++++++++++
 include/hw/riscv/k230.h        |  3 +++
 tests/qtest/k230-dw-ssi-test.c | 45 ++++++++++++++++++++++++++++++++++
 3 files changed, 84 insertions(+)
hw/riscv/k230.c +36 -0
index 4c321002cd..d856864839 100644
@@ -104,6 +104,21 @@ static const MemMapEntry memmap[] = {
     [K230_DEV_CLINT] =        { 0xF04000000, 0x00400000 },
 };
 
+typedef struct K230SsiRoute {
+    unsigned int ssi_index;
+    unsigned int irq_base;
+} K230SsiRoute;
+
+/*
+ * SDK numbering follows the address map: spi0 is the SPI-OPI instance,
+ * while spi1 and spi2 are QSPI0 and QSPI1.
+ */
+static const K230SsiRoute k230_ssi_routes[] = {
+    { .ssi_index = 2, .irq_base = K230_SPI0_IRQ_BASE },
+    { .ssi_index = 0, .irq_base = K230_SPI1_IRQ_BASE },
+    { .ssi_index = 1, .irq_base = K230_SPI2_IRQ_BASE },
+};
+
 typedef struct K230DwSsiProfile {
     uint32_t num_cs;
     uint32_t fifo_depth;
@@ -193,6 +208,25 @@ static void k230_create_uart(MemoryRegion *sys_mem, DeviceState *plic,
                    399193, serial_hd(index), DEVICE_LITTLE_ENDIAN);
 }
 
+static void k230_connect_ssi_irqs(K230SoCState *s)
+{
+    for (size_t route_idx = 0;
+         route_idx < ARRAY_SIZE(k230_ssi_routes); route_idx++) {
+        const K230SsiRoute *route = &k230_ssi_routes[route_idx];
+        SysBusDevice *ssi;
+
+        g_assert(route->ssi_index < ARRAY_SIZE(s->dw_ssi));
+        g_assert(route->irq_base + DW_SSI_IRQ_COUNT <=
+                 K230_PLIC_NUM_SOURCES);
+
+        ssi = SYS_BUS_DEVICE(&s->dw_ssi[route->ssi_index]);
+        for (unsigned int i = 0; i < DW_SSI_IRQ_COUNT; i++) {
+            sysbus_connect_irq(ssi, i,
+                qdev_get_gpio_in(DEVICE(s->c908_plic), route->irq_base + i));
+        }
+    }
+}
+
 static void k230_soc_realize(DeviceState *dev, Error **errp)
 {
     K230SoCState *s = RISCV_K230_SOC(dev);
@@ -247,6 +281,8 @@ static void k230_soc_realize(DeviceState *dev, Error **errp)
         }
     }
 
+    k230_connect_ssi_irqs(s);
+
     sysbus_mmio_map(SYS_BUS_DEVICE(&s->wdt[0]), 0, memmap[K230_DEV_WDT0].base);
     sysbus_connect_irq(SYS_BUS_DEVICE(&s->wdt[0]), 0,
                        qdev_get_gpio_in(DEVICE(s->c908_plic), K230_WDT0_IRQ));
include/hw/riscv/k230.h +3 -0
index 99996a93b1..275b6b62c3 100644
@@ -131,6 +131,9 @@ enum {
     K230_UART4_IRQ  = 20,
     K230_WDT0_IRQ   = 107,
     K230_WDT1_IRQ   = 108,
+    K230_SPI0_IRQ_BASE = 146,
+    K230_SPI1_IRQ_BASE = 155,
+    K230_SPI2_IRQ_BASE = 164,
 };
 
 #define K230_UART_COUNT 5
tests/qtest/k230-dw-ssi-test.c +45 -0

95 lines not on this page · raw message

Signature · 1 line
2.43.0
#12 lore raw

PATCH v2 5/5 hw/riscv/k230: Attach a standard SPI NOR flash

In reply to Kangjie Huang

Add the optional spi-flash machine property and attach the selected M25P80-compatible flash device to logical spi0 chip select 0.

Use the supplied MTD backend when present and retain the erased-flash default otherwise. Document the machine option and add qtests for Standard 1-1-1 JEDEC identification and fixed-address reads.

Standard 1-1-1 transfers were also exercised manually through the K230 SDK U-Boot and Linux SPI paths against the attached flash.

The enhanced SPI and IDMA boot paths are outside the scope of this series.

---
 docs/system/riscv/k230.rst     |  1 +
 hw/riscv/Kconfig               |  1 +
 hw/riscv/k230.c                | 63 ++++++++++++++++++++++++++++++++++
 include/hw/riscv/k230.h        |  2 ++
 tests/qtest/k230-dw-ssi-test.c | 55 +++++++++++++++++++++++++++++
 5 files changed, 122 insertions(+)
docs/system/riscv/k230.rst +1 -0
index 7a49cac95b..620ef263de 100644
@@ -21,6 +21,7 @@ The ``k230`` machine supports the following devices:
 * 2 K230 Watchdog Timer
 * 5 UART
 * 3 K230 SSI controllers for SPI only
+* Optional SPI NOR flash on spi0 CS0
 
 Boot options
 ------------
hw/riscv/Kconfig +1 -0
index 60a260691c..9d59465ad6 100644
@@ -163,3 +163,4 @@ config K230
     select UNIMP
     select K230_WDT
     select DW_SSI
+    select SSI_M25P80
hw/riscv/k230.c +63 -0

99 lines not on this page · raw message

include/hw/riscv/k230.h +2 -0
index 275b6b62c3..80fb4c0e69 100644
@@ -52,6 +52,8 @@ typedef struct K230MachineState {
     /*< public >*/
     K230SoCState soc;
     Notifier machine_done;
+
+    char *spi_flash_model;
 } K230MachineState;
 
 enum {
tests/qtest/k230-dw-ssi-test.c +55 -0

66 lines not on this page · raw message

Signature · 1 line
2.43.0