Skip to contentSkip to conversation

hw/riscv: add SDHCI support for K230

PATCH In review Xin Xie 3 participants 8 messages Jul 29 – Aug 14

Subsystems (2)
lore
Share & tools
lore

Xin Xie

author
#0 lore raw

cover hw/riscv: add SDHCI support for K230

The Kendryte K230 contains two Synopsys DesignWare Core Mobile Storage Host Controllers. The K230 machine currently exposes their MMIO ranges as unimplemented devices, so software cannot access SD storage.

This series adds a K230 SDHCIv3 model based on the generic SDHCI model. The model reuses the generic commands, PIO, SDMA and ADMA2 data paths and provides the K230 0x1000-byte register layout. It implements the documented capability and reset values, read-only Preset Value and extension pointer registers, writable backing storage for most PHY and vendor registers, and an always-ready PHY_PWRGOOD status bit required by the K230 SDK reset sequence. It is worth noting that K230 documentation describes an SDHCI v4.20 controller, while the generic QEMU SDHCI does not support ADMA3. So this model reports v3 and does not advertise ADMA3.

Testing:
- scripts/checkpatch.pl: 0 errors, 0 warnings, 747 lines checked
- qemu:qtest-riscv64/k230-sdhci-test: 3 subtests passed
- Linux direct boot: both controllers detected, r/w and e2fsck passed

Xin Xie (3):
hw/sd: add Kendryte K230 SDHCI controller
hw/riscv: enable SDHCI controllers on K230
tests/qtest: add K230 SDHCI tests

 MAINTAINERS                   |   3 +
 docs/system/riscv/k230.rst    |   6 +-
 hw/riscv/Kconfig              |   1 +
 hw/riscv/k230.c               |  34 +++-
 hw/sd/Kconfig                 |   4 +
 hw/sd/k230_sdhci.c            | 311 ++++++++++++++++++++++++++++++++++
 hw/sd/meson.build             |   1 +
 include/hw/riscv/k230.h       |   4 +
 include/hw/sd/k230_sdhci.h    |  82 +++++++++
 tests/qtest/k230-sdhci-test.c | 214 +++++++++++++++++++++++
 tests/qtest/meson.build       |   5 +-
 11 files changed, 656 insertions(+), 9 deletions(-)
 create mode 100644 hw/sd/k230_sdhci.c
 create mode 100644 include/hw/sd/k230_sdhci.h
 create mode 100644 tests/qtest/k230-sdhci-test.c
Signature · 1 line
2.43.0
author
#1 lore raw

PATCH 1/3 hw/sd: add Kendryte K230 SDHCI controller

Add a minimal K230 SDHCI model based on the generic SDHCI model. Reuse the generic commands and data paths while providing the extended register layout and documented reset values.

K230 SDHCI controller contains the standard SDHCI register block followed by preset, extension pointer, PHY and vendor-specific registers. This model implements the preset and extension pointer registers as read-only, retain writes to most PHY and vendor-specific registers, and keep PHY_PWRGOOD asserted so that the K230 SDK reset sequence can complete.

The SDHCI version is set to 3 due to the absence of ADMA3 support in the generic model.

---
 MAINTAINERS                |   2 +
 hw/sd/Kconfig              |   4 +
 hw/sd/k230_sdhci.c         | 311 +++++++++++++++++++++++++++++++++++++
 hw/sd/meson.build          |   1 +
 include/hw/sd/k230_sdhci.h |  82 ++++++++++
 5 files changed, 400 insertions(+)
 create mode 100644 hw/sd/k230_sdhci.c
 create mode 100644 include/hw/sd/k230_sdhci.h
MAINTAINERS +2 -0
index 6171cc7494..06285352c4 100644
@@ -1826,8 +1826,10 @@ S: Maintained
 F: docs/system/riscv/k230.rst
 F: hw/riscv/k230.c
 F: hw/watchdog/k230_wdt.c
+F: hw/sd/k230_sdhci.c
 F: include/hw/riscv/k230.h
 F: include/hw/watchdog/k230_wdt.h
+F: include/hw/sd/k230_sdhci.h
 F: tests/qtest/k230-wdt-test.c
 
 RX Machines
hw/sd/Kconfig +4 -0
index 633b9afec9..e9a5d63b4d 100644
@@ -23,3 +23,7 @@ config SDHCI_PCI
 config CADENCE_SDHCI
     bool
     select SDHCI
+
+config K230_SDHCI
+    bool
+    select SDHCI
hw/sd/k230_sdhci.c new file +311 -0

311 lines not on this page · raw message

hw/sd/meson.build +1 -0
index b43d45bc56..a109635233 100644
@@ -10,3 +10,4 @@ system_ss.add(when: 'CONFIG_ASPEED_SOC', if_true: files('aspeed_sdhci.c'))
 system_ss.add(when: 'CONFIG_ALLWINNER_H3', if_true: files('allwinner-sdhost.c'))
 system_ss.add(when: 'CONFIG_NPCM7XX', if_true: files('npcm7xx_sdhci.c'))
 system_ss.add(when: 'CONFIG_CADENCE_SDHCI', if_true: files('cadence_sdhci.c'))
+system_ss.add(when: 'CONFIG_K230_SDHCI', if_true: files('k230_sdhci.c'))
include/hw/sd/k230_sdhci.h new file +82 -0

82 lines not on this page · raw message

Signature · 1 line
2.43.0
Reviewed-by on v1 1/3
#2 lore raw

Re: [PATCH v1 1/3] hw/sd: add Kendryte K230 SDHCI controller

Quoted 10 lines · Xin Xie

Add a minimal K230 SDHCI model based on the generic SDHCI model. Reuse the generic commands and data paths while providing the extended register layout and documented reset values.

K230 SDHCI controller contains the standard SDHCI register block followed by preset, extension pointer, PHY and vendor-specific registers. This model implements the preset and extension pointer registers as read-only, retain writes to most PHY and vendor-specific registers, and keep PHY_PWRGOOD asserted so that the K230 SDK reset sequence can complete.

Quoted 5 lines

The SDHCI version is set to 3 due to the absence of ADMA3 support in the generic model.

---

Quoted 450 lines
  MAINTAINERS                |   2 +
  hw/sd/Kconfig              |   4 +
  hw/sd/k230_sdhci.c         | 311 +++++++++++++++++++++++++++++++++++++
  hw/sd/meson.build          |   1 +
  include/hw/sd/k230_sdhci.h |  82 ++++++++++
  5 files changed, 400 insertions(+)

create mode 100644 hw/sd/k230_sdhci.c
create mode 100644 include/hw/sd/k230_sdhci.h

MAINTAINERS +2 -0
index 6171cc7494..06285352c4 100644
@@ -1826,8 +1826,10 @@ S: Maintained
  F: docs/system/riscv/k230.rst
  F: hw/riscv/k230.c
  F: hw/watchdog/k230_wdt.c
+F: hw/sd/k230_sdhci.c
  F: include/hw/riscv/k230.h
  F: include/hw/watchdog/k230_wdt.h
+F: include/hw/sd/k230_sdhci.h
  F: tests/qtest/k230-wdt-test.c
 
  RX Machines
hw/sd/Kconfig +4 -0
index 633b9afec9..e9a5d63b4d 100644
@@ -23,3 +23,7 @@ config SDHCI_PCI
  config CADENCE_SDHCI
      bool
      select SDHCI
+
+config K230_SDHCI
+    bool
+    select SDHCI
hw/sd/k230_sdhci.c new file +311 -0
new file mode 100644
index 0000000000..34c972a2f7
@@ -0,0 +1,311 @@
+/*
+ * Kendryte K230 SDHCI controller
+ *
+ * Copyright (c) 2026 Xin Xie <[email protected]>
+ *
+ * SPDX-License-Identifier: GPL-2.0-or-later
+ */
+
+#include "qemu/osdep.h"
+#include "qemu/log.h"
+#include "hw/sd/k230_sdhci.h"
+#include "hw/sd/sdhci-internal.h"
+#include "migration/vmstate.h"
+
+/*
+ * The TRM describes an SDHCI v4.20 DWC MSHC, including the v4 block-count
+ * mode, ADMA3, and extension-area registers.  QEMU's generic controller
+ * supports up to v3 and does not implement those v4 transfer semantics.
+ */
+#define K230_SDHCI_SPEC_VERSION 3
+
+/*
+ * K230's Capabilities registers describe more functionality than the generic
+ * QEMU SDHCI data path currently implements.  Do not expose the following TRM
+ * capabilities to the guest:
+ *
+ * - ASYNC_INT requires an SDIO card-interrupt input and asynchronous interrupt
+ *   signalling while the SD clock is stopped.  SDBus has no such input.
+ * - TIMER_RETUNING and RETUNING_MODE require a re-tuning timer and generation
+ *   of a re-tuning request.  The generic controller only completes an initial
+ *   command-based tuning operation; it never schedules periodic re-tuning.
+ * - ADMA3 requires the SDHCI v4.20 ADMA3 descriptor and command semantics,
+ *   whereas the generic controller implements SDMA and ADMA2 only.
+ */
+#define K230_SDHCI_CAPABILITIES_UNSUPPORTED \
+    (R_SDHC_CAPAB_ASYNC_INT_MASK | \
+     R_SDHC_CAPAB_TIMER_RETUNING_MASK | \
+     R_SDHC_CAPAB_RETUNING_MODE_MASK | \
+     R_SDHC_CAPAB_ADMA3_MASK)
+#define K230_SDHCI_CAPAREG_IMPLEMENTED \
+    (K230_SDHCI_CAPAREG_RESET & ~K230_SDHCI_CAPABILITIES_UNSUPPORTED)
+
+#define K230_LO8(value)     ((value) & 0xff)
+#define K230_HI8(value)     (((value) >> 8) & 0xff)
+
+static const uint8_t k230_sdhci_preset_reset[K230_SDHCI_PRESET_SIZE] = {
+    [K230_SDHCI_PRESET_INIT - K230_SDHCI_PRESET_BASE] =
+        K230_LO8(K230_SDHCI_PRESET_INIT_RESET),
+    [K230_SDHCI_PRESET_INIT - K230_SDHCI_PRESET_BASE + 1] =
+        K230_HI8(K230_SDHCI_PRESET_INIT_RESET),
+    [K230_SDHCI_PRESET_DEFAULT_SPEED - K230_SDHCI_PRESET_BASE] =
+        K230_LO8(K230_SDHCI_PRESET_DEFAULT_SPEED_RESET),
+    [K230_SDHCI_PRESET_DEFAULT_SPEED - K230_SDHCI_PRESET_BASE + 1] =
+        K230_HI8(K230_SDHCI_PRESET_DEFAULT_SPEED_RESET),
+    [K230_SDHCI_PRESET_HIGH_SPEED - K230_SDHCI_PRESET_BASE] =
+        K230_LO8(K230_SDHCI_PRESET_HIGH_SPEED_RESET),
+    [K230_SDHCI_PRESET_HIGH_SPEED - K230_SDHCI_PRESET_BASE + 1] =
+        K230_HI8(K230_SDHCI_PRESET_HIGH_SPEED_RESET),
+    [K230_SDHCI_PRESET_SDR12 - K230_SDHCI_PRESET_BASE] =
+        K230_LO8(K230_SDHCI_PRESET_SDR12_RESET),
+    [K230_SDHCI_PRESET_SDR12 - K230_SDHCI_PRESET_BASE + 1] =
+        K230_HI8(K230_SDHCI_PRESET_SDR12_RESET),
+    [K230_SDHCI_PRESET_SDR25 - K230_SDHCI_PRESET_BASE] =
+        K230_LO8(K230_SDHCI_PRESET_SDR25_RESET),
+    [K230_SDHCI_PRESET_SDR25 - K230_SDHCI_PRESET_BASE + 1] =
+        K230_HI8(K230_SDHCI_PRESET_SDR25_RESET),
+    [K230_SDHCI_PRESET_SDR50 - K230_SDHCI_PRESET_BASE] =
+        K230_LO8(K230_SDHCI_PRESET_SDR50_RESET),
+    [K230_SDHCI_PRESET_SDR50 - K230_SDHCI_PRESET_BASE + 1] =
+        K230_HI8(K230_SDHCI_PRESET_SDR50_RESET),
+    [K230_SDHCI_PRESET_SDR104 - K230_SDHCI_PRESET_BASE] =
+        K230_LO8(K230_SDHCI_PRESET_SDR104_RESET),
+    [K230_SDHCI_PRESET_SDR104 - K230_SDHCI_PRESET_BASE + 1] =
+        K230_HI8(K230_SDHCI_PRESET_SDR104_RESET),
+    [K230_SDHCI_PRESET_DDR50 - K230_SDHCI_PRESET_BASE] =
+        K230_LO8(K230_SDHCI_PRESET_DDR50_RESET),
+    [K230_SDHCI_PRESET_DDR50 - K230_SDHCI_PRESET_BASE + 1] =
+        K230_HI8(K230_SDHCI_PRESET_DDR50_RESET),
+};
+
+static uint64_t k230_sdhci_preset_read(void *opaque, hwaddr addr,
+                                      unsigned int size)
+{
+    return ldn_le_p(&k230_sdhci_preset_reset[addr], size);
+}
+
+static void k230_sdhci_preset_write(void *opaque, hwaddr addr, uint64_t value,
+                                   unsigned int size)
+{
+    qemu_log_mask(LOG_GUEST_ERROR,
+                  "K230 SDHCI: write to read-only Preset Value register "
+                  "0x%03" HWADDR_PRIx "\n",
+                  K230_SDHCI_PRESET_BASE + addr);
+}
+
+static const MemoryRegionOps k230_sdhci_preset_ops = {
+    .read = k230_sdhci_preset_read,
+    .write = k230_sdhci_preset_write,
+    .endianness = DEVICE_LITTLE_ENDIAN,
+    .valid = {
+        .min_access_size = 1,
+        .max_access_size = 4,
+        .unaligned = false,
+    },
+};
+
+static const uint8_t k230_sdhci_pointer_reset[K230_SDHCI_POINTER_SIZE] = {
+    [K230_SDHCI_P_UHS2_SETTINGS - K230_SDHCI_POINTER_BASE] =
+        K230_LO8(K230_SDHCI_P_UHS2_SETTINGS_RESET),
+    [K230_SDHCI_P_UHS2_SETTINGS - K230_SDHCI_POINTER_BASE + 1] =
+        K230_HI8(K230_SDHCI_P_UHS2_SETTINGS_RESET),
+    [K230_SDHCI_P_UHS2_HOST_CAPAB - K230_SDHCI_POINTER_BASE] =
+        K230_LO8(K230_SDHCI_P_UHS2_HOST_CAPAB_RESET),
+    [K230_SDHCI_P_UHS2_HOST_CAPAB - K230_SDHCI_POINTER_BASE + 1] =
+        K230_HI8(K230_SDHCI_P_UHS2_HOST_CAPAB_RESET),
+    [K230_SDHCI_P_UHS2_TEST - K230_SDHCI_POINTER_BASE] =
+        K230_LO8(K230_SDHCI_P_UHS2_TEST_RESET),
+    [K230_SDHCI_P_UHS2_TEST - K230_SDHCI_POINTER_BASE + 1] =
+        K230_HI8(K230_SDHCI_P_UHS2_TEST_RESET),
+    [K230_SDHCI_P_EMBEDDED_CNTRL - K230_SDHCI_POINTER_BASE] =
+        K230_LO8(K230_SDHCI_P_EMBEDDED_CNTRL_RESET),
+    [K230_SDHCI_P_EMBEDDED_CNTRL - K230_SDHCI_POINTER_BASE + 1] =
+        K230_HI8(K230_SDHCI_P_EMBEDDED_CNTRL_RESET),
+    [K230_SDHCI_P_VENDOR_AREA1 - K230_SDHCI_POINTER_BASE] =
+        K230_LO8(K230_SDHCI_P_VENDOR_AREA1_RESET),
+    [K230_SDHCI_P_VENDOR_AREA1 - K230_SDHCI_POINTER_BASE + 1] =
+        K230_HI8(K230_SDHCI_P_VENDOR_AREA1_RESET),
+    [K230_SDHCI_P_VENDOR_AREA2 - K230_SDHCI_POINTER_BASE] =
+        K230_LO8(K230_SDHCI_P_VENDOR_AREA2_RESET),
+    [K230_SDHCI_P_VENDOR_AREA2 - K230_SDHCI_POINTER_BASE + 1] =
+        K230_HI8(K230_SDHCI_P_VENDOR_AREA2_RESET),
+};
+
+static uint64_t k230_sdhci_pointer_read(void *opaque, hwaddr addr,
+                                       unsigned int size)
+{
+    return ldn_le_p(&k230_sdhci_pointer_reset[addr], size);
+}
+
+static void k230_sdhci_pointer_write(void *opaque, hwaddr addr, uint64_t value,
+                                    unsigned int size)
+{
+    qemu_log_mask(LOG_GUEST_ERROR,
+                  "K230 SDHCI: write to read-only pointer register "
+                  "0x%03" HWADDR_PRIx "\n",
+                  K230_SDHCI_POINTER_BASE + addr);
+}
+
+static const MemoryRegionOps k230_sdhci_pointer_ops = {
+    .read = k230_sdhci_pointer_read,
+    .write = k230_sdhci_pointer_write,
+    .endianness = DEVICE_LITTLE_ENDIAN,
+    .valid = {
+        .min_access_size = 1,
+        .max_access_size = 4,
+        .unaligned = false,
+    },
+};
+
+/*
+ * K230 uses the standard SDHCI register set at offset zero, followed by
+ * UHS-II, DWC MSHC PHY, Embedded Control, and vendor-specific blocks. Most
+ * side effects of analogue PHY behaviors not simulated.
+ *
+ * PHY_PWRGOOD is the sole synthesized status bit.  Real hardware asserts it
+ * after PHY power and reset sequencing; the SDK driver polls it during every
+ * full controller reset.  A virtual PHY has no settling interval or failure
+ * condition, so the bit is permanently asserted and cannot be cleared by a
+ * guest write.
+ */
+static uint64_t k230_sdhci_fallback_read(void *opaque, hwaddr addr,
+                                        unsigned int size)
+{
+    K230SDHCIState *s = opaque;
+    uint64_t value = ldn_le_p(&s->fallback_regs[addr], size);
+
+    if (ranges_overlap(addr, size, K230_SDHCI_PHY_CNFG,
+                       sizeof(uint32_t)) && addr <= K230_SDHCI_PHY_CNFG) {
+        unsigned int shift = (K230_SDHCI_PHY_CNFG - addr) * 8;
+
+        value |= (uint64_t)K230_SDHCI_PHY_CNFG_PWRGOOD << shift;
+    }
+
+    return value;
+}
+
+static void k230_sdhci_fallback_write(void *opaque, hwaddr addr,
+                                     uint64_t value, unsigned int size)
+{
+    K230SDHCIState *s = opaque;
+
+    stn_le_p(&s->fallback_regs[addr], size, value);
+
+    /* PWRGOOD reflects PHY state and is not software writable. */
+    s->fallback_regs[K230_SDHCI_PHY_CNFG] |=
+        K230_SDHCI_PHY_CNFG_PWRGOOD;
+}
+
+static const MemoryRegionOps k230_sdhci_fallback_ops = {
+    .read = k230_sdhci_fallback_read,
+    .write = k230_sdhci_fallback_write,
+    .endianness = DEVICE_LITTLE_ENDIAN,
+    .valid = {
+        .min_access_size = 1,
+        .max_access_size = 4,
+        .unaligned = false,
+    },
+};
+
+static void k230_sdhci_reset(DeviceState *dev)
+{
+    K230SDHCIState *s = K230_SDHCI(dev);
+
+    memset(s->fallback_regs, 0, sizeof(s->fallback_regs));
+    s->fallback_regs[K230_SDHCI_PHY_CNFG] =
+        K230_SDHCI_PHY_CNFG_PWRGOOD;
+    device_cold_reset(DEVICE(&s->sdhci));
+}
+
+static void k230_sdhci_realize(DeviceState *dev, Error **errp)
+{
+    K230SDHCIState *s = K230_SDHCI(dev);
+    SysBusDevice *sbd = SYS_BUS_DEVICE(dev);
+    SysBusDevice *sdhci_sbd = SYS_BUS_DEVICE(&s->sdhci);
+
+    memory_region_init(&s->container, OBJECT(s), "k230.sdhci-container",
+                       K230_SDHCI_REG_SIZE);
+    sysbus_init_mmio(sbd, &s->container);
+
+    memory_region_init_io(&s->iomem_fallback, OBJECT(s),
+                          &k230_sdhci_fallback_ops, s,
+                          "k230.sdhci-fallback",
+                          K230_SDHCI_REG_SIZE);
+    memory_region_add_subregion(&s->container, 0, &s->iomem_fallback);
+
+    if (!sysbus_realize(sdhci_sbd, errp)) {
+        return;
+    }
+    memory_region_add_subregion_overlap(
+        &s->container, 0, sysbus_mmio_get_region(sdhci_sbd, 0), 1);
+
+    memory_region_init_io(&s->iomem_preset, OBJECT(s),
+                          &k230_sdhci_preset_ops, s,
+                          "k230.sdhci-preset", K230_SDHCI_PRESET_SIZE);
+    memory_region_add_subregion_overlap(&s->container,
+                                        K230_SDHCI_PRESET_BASE,
+                                        &s->iomem_preset, 2);
+
+    memory_region_init_io(&s->iomem_pointer, OBJECT(s),
+                          &k230_sdhci_pointer_ops, s,
+                          "k230.sdhci-pointers", K230_SDHCI_POINTER_SIZE);
+    memory_region_add_subregion_overlap(&s->container,
+                                        K230_SDHCI_POINTER_BASE,
+                                        &s->iomem_pointer, 2);
+
+    sysbus_pass_irq(sbd, sdhci_sbd);
+    s->bus = qdev_get_child_bus(DEVICE(sdhci_sbd), "sd-bus");
+}
+
+static const VMStateDescription vmstate_k230_sdhci = {
+    .name = TYPE_K230_SDHCI,
+    .version_id = 1,
+    .minimum_version_id = 1,
+    .fields = (const VMStateField[]) {
+        VMSTATE_UINT8_ARRAY(fallback_regs, K230SDHCIState,
+                            K230_SDHCI_REG_SIZE),
+        VMSTATE_END_OF_LIST()
+    },
+};
+
+static void k230_sdhci_instance_init(Object *obj)
+{
+    K230SDHCIState *s = K230_SDHCI(obj);
+
+    object_initialize_child(obj, "generic-sdhci", &s->sdhci,
+                            TYPE_SYSBUS_SDHCI);
+
+    object_property_set_uint(OBJECT(&s->sdhci), "sd-spec-version",
+                             K230_SDHCI_SPEC_VERSION,
+                             &error_abort);
+    object_property_set_uint(OBJECT(&s->sdhci), "capareg",
+                             K230_SDHCI_CAPAREG_IMPLEMENTED,
+                             &error_abort);
+    object_property_set_uint(OBJECT(&s->sdhci), "uhs", UHS_I, &error_abort);
+}
+
+static void k230_sdhci_class_init(ObjectClass *klass, const void *data)
+{
+    DeviceClass *dc = DEVICE_CLASS(klass);
+
+    dc->desc = "Kendryte K230 SDHCI Controller";
+    dc->realize = k230_sdhci_realize;
+    device_class_set_legacy_reset(dc, k230_sdhci_reset);
+    dc->vmsd = &vmstate_k230_sdhci;
+    set_bit(DEVICE_CATEGORY_STORAGE, dc->categories);
+}
+
+static const TypeInfo k230_sdhci_type_info = {
+    .name = TYPE_K230_SDHCI,
+    .parent = TYPE_SYS_BUS_DEVICE,
+    .instance_size = sizeof(K230SDHCIState),
+    .instance_init = k230_sdhci_instance_init,
+    .class_init = k230_sdhci_class_init,
+};
+
+static void k230_sdhci_register_types(void)
+{
+    type_register_static(&k230_sdhci_type_info);
+}
+
+type_init(k230_sdhci_register_types)
hw/sd/meson.build +1 -0
index b43d45bc56..a109635233 100644
@@ -10,3 +10,4 @@ system_ss.add(when: 'CONFIG_ASPEED_SOC', if_true: files('aspeed_sdhci.c'))
  system_ss.add(when: 'CONFIG_ALLWINNER_H3', if_true: files('allwinner-sdhost.c'))
  system_ss.add(when: 'CONFIG_NPCM7XX', if_true: files('npcm7xx_sdhci.c'))
  system_ss.add(when: 'CONFIG_CADENCE_SDHCI', if_true: files('cadence_sdhci.c'))
+system_ss.add(when: 'CONFIG_K230_SDHCI', if_true: files('k230_sdhci.c'))
include/hw/sd/k230_sdhci.h new file +82 -0
new file mode 100644
index 0000000000..a8389f1de4
@@ -0,0 +1,82 @@
+/*
+ * Kendryte K230 SDHCI controller
+ *
+ * Copyright (c) 2026 Xin Xie <[email protected]>
+ *
+ * SPDX-License-Identifier: GPL-2.0-or-later
+ */
+
+#ifndef HW_SD_K230_SDHCI_H
+#define HW_SD_K230_SDHCI_H
+
+#include "qemu/bitops.h"
+#include "hw/sd/sdhci.h"
+#include "qom/object.h"
+
+#define TYPE_K230_SDHCI "k230.sdhci"
+OBJECT_DECLARE_SIMPLE_TYPE(K230SDHCIState, K230_SDHCI)
+
+#define K230_SDHCI_REG_SIZE             0x1000
+
+/* K230 TRM reset values for Capabilities Registers 1 and 2. */
+#define K230_SDHCI_CAPABILITIES1_RESET  0x256ec881ULL
+#define K230_SDHCI_CAPABILITIES2_RESET  0x0800a177ULL
+#define K230_SDHCI_CAPAREG_RESET \
+    ((K230_SDHCI_CAPABILITIES2_RESET << 32) \
+    | K230_SDHCI_CAPABILITIES1_RESET)
+
+/* SDHCI Preset Value registers. */
+#define K230_SDHCI_PRESET_BASE           0x060
+#define K230_SDHCI_PRESET_SIZE           0x010
+#define K230_SDHCI_PRESET_INIT           0x060
+#define K230_SDHCI_PRESET_DEFAULT_SPEED  0x062
+#define K230_SDHCI_PRESET_HIGH_SPEED     0x064
+#define K230_SDHCI_PRESET_SDR12          0x066
+#define K230_SDHCI_PRESET_SDR25          0x068
+#define K230_SDHCI_PRESET_SDR50          0x06a
+#define K230_SDHCI_PRESET_SDR104         0x06c
+#define K230_SDHCI_PRESET_DDR50          0x06e
+#define K230_SDHCI_PRESET_INIT_RESET          0x00fa
+#define K230_SDHCI_PRESET_DEFAULT_SPEED_RESET 0x0004
+#define K230_SDHCI_PRESET_HIGH_SPEED_RESET    0x0002
+#define K230_SDHCI_PRESET_SDR12_RESET         0x0004
+#define K230_SDHCI_PRESET_SDR25_RESET         0x0002
+#define K230_SDHCI_PRESET_SDR50_RESET         0x0001
+#define K230_SDHCI_PRESET_SDR104_RESET        0x0000
+#define K230_SDHCI_PRESET_DDR50_RESET         0x0002
+
+/* SDHCI v4 extension-area pointer registers. */
+#define K230_SDHCI_POINTER_BASE         0x0e0
+#define K230_SDHCI_POINTER_SIZE         0x00c
+#define K230_SDHCI_P_UHS2_SETTINGS      0x0e0
+#define K230_SDHCI_P_UHS2_HOST_CAPAB    0x0e2
+#define K230_SDHCI_P_UHS2_TEST          0x0e4
+#define K230_SDHCI_P_EMBEDDED_CNTRL     0x0e6
+#define K230_SDHCI_P_VENDOR_AREA1       0x0e8
+#define K230_SDHCI_P_VENDOR_AREA2       0x0ea
+#define K230_SDHCI_P_UHS2_SETTINGS_RESET    0x0000
+#define K230_SDHCI_P_UHS2_HOST_CAPAB_RESET  0x0f58
+#define K230_SDHCI_P_UHS2_TEST_RESET        0x0f68
+#define K230_SDHCI_P_EMBEDDED_CNTRL_RESET   0x0f6c
+#define K230_SDHCI_P_VENDOR_AREA1_RESET     0x0500
+#define K230_SDHCI_P_VENDOR_AREA2_RESET     0x0384
+
+/* DWC MSHC PHY register block. */
+#define K230_SDHCI_PHY_CNFG             0x300
+#define K230_SDHCI_PHY_CNFG_RSTN        BIT(0)
+#define K230_SDHCI_PHY_CNFG_PWRGOOD     BIT(1)
+
+struct K230SDHCIState {
+    SysBusDevice parent_obj;
+
+    MemoryRegion container;
+    MemoryRegion iomem_fallback;
+    MemoryRegion iomem_preset;
+    MemoryRegion iomem_pointer;
+    BusState *bus;
+    uint8_t fallback_regs[K230_SDHCI_REG_SIZE];
+
+    SDHCIState sdhci;
+};
+
+#endif
#3 lore raw

Re: [PATCH v1 1/3] hw/sd: add Kendryte K230 SDHCI controller

In reply to Xin Xie
Quoted 11 lines · Xin Xie

Add a minimal K230 SDHCI model based on the generic SDHCI model. Reuse the generic commands and data paths while providing the extended register layout and documented reset values.

K230 SDHCI controller contains the standard SDHCI register block followed by preset, extension pointer, PHY and vendor-specific registers. This model implements the preset and extension pointer registers as read-only, retain writes to most PHY and vendor-specific registers, and keep PHY_PWRGOOD asserted so that the K230 SDK reset sequence can complete.

Please split the k230_sdhci model into 2 separate models:

- One is generic Synopsys DesignWare Mobile Storage Host Controller IP model (hw/sd/dw_mshc.c)
- Optional Canann K230 specific wrapper glue model to the dw_mshc IP (if the generic Synopsys DesignWare MSHC IP model, or plus some unimp region, is good enough to make the software happy, no need to create this one)

Quoted 76 lines

The SDHCI version is set to 3 due to the absence of ADMA3 support in the generic model.

---
 MAINTAINERS                |   2 +
 hw/sd/Kconfig              |   4 +
 hw/sd/k230_sdhci.c         | 311 +++++++++++++++++++++++++++++++++++++
 hw/sd/meson.build          |   1 +
 include/hw/sd/k230_sdhci.h |  82 ++++++++++
 5 files changed, 400 insertions(+)
 create mode 100644 hw/sd/k230_sdhci.c
 create mode 100644 include/hw/sd/k230_sdhci.h
MAINTAINERS +2 -0
index 6171cc7494..06285352c4 100644
@@ -1826,8 +1826,10 @@ S: Maintained
 F: docs/system/riscv/k230.rst
 F: hw/riscv/k230.c
 F: hw/watchdog/k230_wdt.c
+F: hw/sd/k230_sdhci.c
 F: include/hw/riscv/k230.h
 F: include/hw/watchdog/k230_wdt.h
+F: include/hw/sd/k230_sdhci.h
 F: tests/qtest/k230-wdt-test.c
 
 RX Machines
hw/sd/Kconfig +4 -0
index 633b9afec9..e9a5d63b4d 100644
@@ -23,3 +23,7 @@ config SDHCI_PCI
 config CADENCE_SDHCI
     bool
     select SDHCI
+
+config K230_SDHCI
+    bool
+    select SDHCI
hw/sd/k230_sdhci.c new file +29 -0
new file mode 100644
index 0000000000..34c972a2f7
@@ -0,0 +1,311 @@
+/*
+ * Kendryte K230 SDHCI controller
+ *
+ * Copyright (c) 2026 Xin Xie <[email protected]>
+ *
+ * SPDX-License-Identifier: GPL-2.0-or-later
+ */
+
+#include "qemu/osdep.h"
+#include "qemu/log.h"
+#include "hw/sd/k230_sdhci.h"
+#include "hw/sd/sdhci-internal.h"
+#include "migration/vmstate.h"
+
+/*
+ * The TRM describes an SDHCI v4.20 DWC MSHC, including the v4 block-count
+ * mode, ADMA3, and extension-area registers.  QEMU's generic controller
+ * supports up to v3 and does not implement those v4 transfer semantics.
+ */
+#define K230_SDHCI_SPEC_VERSION 3
+
+/*
+ * K230's Capabilities registers describe more functionality than the generic
+ * QEMU SDHCI data path currently implements.  Do not expose the following TRM
+ * capabilities to the guest:
+ *
+ * - ASYNC_INT requires an SDIO card-interrupt input and asynchronous interrupt
+ *   signalling while the SD clock is stopped.  SDBus has no such input.
+ * - TIMER_RETUNING and RETUNING_MODE require a re-tuning timer and generation

hw/sd/k230_sdhci.c line 39

Quoted 10 lines
hw/sd/k230_sdhci.c new file +10 -0
+ *   of a re-tuning request.  The generic controller only completes an initial
+ *   command-based tuning operation; it never schedules periodic re-tuning.
+ * - ADMA3 requires the SDHCI v4.20 ADMA3 descriptor and command semantics,
+ *   whereas the generic controller implements SDMA and ADMA2 only.
+ */
+#define K230_SDHCI_CAPABILITIES_UNSUPPORTED \
+    (R_SDHC_CAPAB_ASYNC_INT_MASK | \
+     R_SDHC_CAPAB_TIMER_RETUNING_MASK | \
+     R_SDHC_CAPAB_RETUNING_MODE_MASK | \
+     R_SDHC_CAPAB_ADMA3_MASK)

The reset value of the capability does not agree with the TRM. Please confirm the value on the real hardware, or just use the value from the TRM.

Quoted 341 lines
hw/sd/k230_sdhci.c new file +272 -0
+#define K230_SDHCI_CAPAREG_IMPLEMENTED \
+    (K230_SDHCI_CAPAREG_RESET & ~K230_SDHCI_CAPABILITIES_UNSUPPORTED)
+
+#define K230_LO8(value)     ((value) & 0xff)
+#define K230_HI8(value)     (((value) >> 8) & 0xff)
+
+static const uint8_t k230_sdhci_preset_reset[K230_SDHCI_PRESET_SIZE] = {
+    [K230_SDHCI_PRESET_INIT - K230_SDHCI_PRESET_BASE] =
+        K230_LO8(K230_SDHCI_PRESET_INIT_RESET),
+    [K230_SDHCI_PRESET_INIT - K230_SDHCI_PRESET_BASE + 1] =
+        K230_HI8(K230_SDHCI_PRESET_INIT_RESET),
+    [K230_SDHCI_PRESET_DEFAULT_SPEED - K230_SDHCI_PRESET_BASE] =
+        K230_LO8(K230_SDHCI_PRESET_DEFAULT_SPEED_RESET),
+    [K230_SDHCI_PRESET_DEFAULT_SPEED - K230_SDHCI_PRESET_BASE + 1] =
+        K230_HI8(K230_SDHCI_PRESET_DEFAULT_SPEED_RESET),
+    [K230_SDHCI_PRESET_HIGH_SPEED - K230_SDHCI_PRESET_BASE] =
+        K230_LO8(K230_SDHCI_PRESET_HIGH_SPEED_RESET),
+    [K230_SDHCI_PRESET_HIGH_SPEED - K230_SDHCI_PRESET_BASE + 1] =
+        K230_HI8(K230_SDHCI_PRESET_HIGH_SPEED_RESET),
+    [K230_SDHCI_PRESET_SDR12 - K230_SDHCI_PRESET_BASE] =
+        K230_LO8(K230_SDHCI_PRESET_SDR12_RESET),
+    [K230_SDHCI_PRESET_SDR12 - K230_SDHCI_PRESET_BASE + 1] =
+        K230_HI8(K230_SDHCI_PRESET_SDR12_RESET),
+    [K230_SDHCI_PRESET_SDR25 - K230_SDHCI_PRESET_BASE] =
+        K230_LO8(K230_SDHCI_PRESET_SDR25_RESET),
+    [K230_SDHCI_PRESET_SDR25 - K230_SDHCI_PRESET_BASE + 1] =
+        K230_HI8(K230_SDHCI_PRESET_SDR25_RESET),
+    [K230_SDHCI_PRESET_SDR50 - K230_SDHCI_PRESET_BASE] =
+        K230_LO8(K230_SDHCI_PRESET_SDR50_RESET),
+    [K230_SDHCI_PRESET_SDR50 - K230_SDHCI_PRESET_BASE + 1] =
+        K230_HI8(K230_SDHCI_PRESET_SDR50_RESET),
+    [K230_SDHCI_PRESET_SDR104 - K230_SDHCI_PRESET_BASE] =
+        K230_LO8(K230_SDHCI_PRESET_SDR104_RESET),
+    [K230_SDHCI_PRESET_SDR104 - K230_SDHCI_PRESET_BASE + 1] =
+        K230_HI8(K230_SDHCI_PRESET_SDR104_RESET),
+    [K230_SDHCI_PRESET_DDR50 - K230_SDHCI_PRESET_BASE] =
+        K230_LO8(K230_SDHCI_PRESET_DDR50_RESET),
+    [K230_SDHCI_PRESET_DDR50 - K230_SDHCI_PRESET_BASE + 1] =
+        K230_HI8(K230_SDHCI_PRESET_DDR50_RESET),
+};
+
+static uint64_t k230_sdhci_preset_read(void *opaque, hwaddr addr,
+                                      unsigned int size)
+{
+    return ldn_le_p(&k230_sdhci_preset_reset[addr], size);
+}
+
+static void k230_sdhci_preset_write(void *opaque, hwaddr addr, uint64_t value,
+                                   unsigned int size)
+{
+    qemu_log_mask(LOG_GUEST_ERROR,
+                  "K230 SDHCI: write to read-only Preset Value register "
+                  "0x%03" HWADDR_PRIx "\n",
+                  K230_SDHCI_PRESET_BASE + addr);
+}
+
+static const MemoryRegionOps k230_sdhci_preset_ops = {
+    .read = k230_sdhci_preset_read,
+    .write = k230_sdhci_preset_write,
+    .endianness = DEVICE_LITTLE_ENDIAN,
+    .valid = {
+        .min_access_size = 1,
+        .max_access_size = 4,
+        .unaligned = false,
+    },
+};
+
+static const uint8_t k230_sdhci_pointer_reset[K230_SDHCI_POINTER_SIZE] = {
+    [K230_SDHCI_P_UHS2_SETTINGS - K230_SDHCI_POINTER_BASE] =
+        K230_LO8(K230_SDHCI_P_UHS2_SETTINGS_RESET),
+    [K230_SDHCI_P_UHS2_SETTINGS - K230_SDHCI_POINTER_BASE + 1] =
+        K230_HI8(K230_SDHCI_P_UHS2_SETTINGS_RESET),
+    [K230_SDHCI_P_UHS2_HOST_CAPAB - K230_SDHCI_POINTER_BASE] =
+        K230_LO8(K230_SDHCI_P_UHS2_HOST_CAPAB_RESET),
+    [K230_SDHCI_P_UHS2_HOST_CAPAB - K230_SDHCI_POINTER_BASE + 1] =
+        K230_HI8(K230_SDHCI_P_UHS2_HOST_CAPAB_RESET),
+    [K230_SDHCI_P_UHS2_TEST - K230_SDHCI_POINTER_BASE] =
+        K230_LO8(K230_SDHCI_P_UHS2_TEST_RESET),
+    [K230_SDHCI_P_UHS2_TEST - K230_SDHCI_POINTER_BASE + 1] =
+        K230_HI8(K230_SDHCI_P_UHS2_TEST_RESET),
+    [K230_SDHCI_P_EMBEDDED_CNTRL - K230_SDHCI_POINTER_BASE] =
+        K230_LO8(K230_SDHCI_P_EMBEDDED_CNTRL_RESET),
+    [K230_SDHCI_P_EMBEDDED_CNTRL - K230_SDHCI_POINTER_BASE + 1] =
+        K230_HI8(K230_SDHCI_P_EMBEDDED_CNTRL_RESET),
+    [K230_SDHCI_P_VENDOR_AREA1 - K230_SDHCI_POINTER_BASE] =
+        K230_LO8(K230_SDHCI_P_VENDOR_AREA1_RESET),
+    [K230_SDHCI_P_VENDOR_AREA1 - K230_SDHCI_POINTER_BASE + 1] =
+        K230_HI8(K230_SDHCI_P_VENDOR_AREA1_RESET),
+    [K230_SDHCI_P_VENDOR_AREA2 - K230_SDHCI_POINTER_BASE] =
+        K230_LO8(K230_SDHCI_P_VENDOR_AREA2_RESET),
+    [K230_SDHCI_P_VENDOR_AREA2 - K230_SDHCI_POINTER_BASE + 1] =
+        K230_HI8(K230_SDHCI_P_VENDOR_AREA2_RESET),
+};
+
+static uint64_t k230_sdhci_pointer_read(void *opaque, hwaddr addr,
+                                       unsigned int size)
+{
+    return ldn_le_p(&k230_sdhci_pointer_reset[addr], size);
+}
+
+static void k230_sdhci_pointer_write(void *opaque, hwaddr addr, uint64_t value,
+                                    unsigned int size)
+{
+    qemu_log_mask(LOG_GUEST_ERROR,
+                  "K230 SDHCI: write to read-only pointer register "
+                  "0x%03" HWADDR_PRIx "\n",
+                  K230_SDHCI_POINTER_BASE + addr);
+}
+
+static const MemoryRegionOps k230_sdhci_pointer_ops = {
+    .read = k230_sdhci_pointer_read,
+    .write = k230_sdhci_pointer_write,
+    .endianness = DEVICE_LITTLE_ENDIAN,
+    .valid = {
+        .min_access_size = 1,
+        .max_access_size = 4,
+        .unaligned = false,
+    },
+};
+
+/*
+ * K230 uses the standard SDHCI register set at offset zero, followed by
+ * UHS-II, DWC MSHC PHY, Embedded Control, and vendor-specific blocks. Most
+ * side effects of analogue PHY behaviors not simulated.
+ *
+ * PHY_PWRGOOD is the sole synthesized status bit.  Real hardware asserts it
+ * after PHY power and reset sequencing; the SDK driver polls it during every
+ * full controller reset.  A virtual PHY has no settling interval or failure
+ * condition, so the bit is permanently asserted and cannot be cleared by a
+ * guest write.
+ */
+static uint64_t k230_sdhci_fallback_read(void *opaque, hwaddr addr,
+                                        unsigned int size)
+{
+    K230SDHCIState *s = opaque;
+    uint64_t value = ldn_le_p(&s->fallback_regs[addr], size);
+
+    if (ranges_overlap(addr, size, K230_SDHCI_PHY_CNFG,
+                       sizeof(uint32_t)) && addr <= K230_SDHCI_PHY_CNFG) {
+        unsigned int shift = (K230_SDHCI_PHY_CNFG - addr) * 8;
+
+        value |= (uint64_t)K230_SDHCI_PHY_CNFG_PWRGOOD << shift;
+    }
+
+    return value;
+}
+
+static void k230_sdhci_fallback_write(void *opaque, hwaddr addr,
+                                     uint64_t value, unsigned int size)
+{
+    K230SDHCIState *s = opaque;
+
+    stn_le_p(&s->fallback_regs[addr], size, value);
+
+    /* PWRGOOD reflects PHY state and is not software writable. */
+    s->fallback_regs[K230_SDHCI_PHY_CNFG] |=
+        K230_SDHCI_PHY_CNFG_PWRGOOD;
+}
+
+static const MemoryRegionOps k230_sdhci_fallback_ops = {
+    .read = k230_sdhci_fallback_read,
+    .write = k230_sdhci_fallback_write,
+    .endianness = DEVICE_LITTLE_ENDIAN,
+    .valid = {
+        .min_access_size = 1,
+        .max_access_size = 4,
+        .unaligned = false,
+    },
+};
+
+static void k230_sdhci_reset(DeviceState *dev)
+{
+    K230SDHCIState *s = K230_SDHCI(dev);
+
+    memset(s->fallback_regs, 0, sizeof(s->fallback_regs));
+    s->fallback_regs[K230_SDHCI_PHY_CNFG] =
+        K230_SDHCI_PHY_CNFG_PWRGOOD;
+    device_cold_reset(DEVICE(&s->sdhci));
+}
+
+static void k230_sdhci_realize(DeviceState *dev, Error **errp)
+{
+    K230SDHCIState *s = K230_SDHCI(dev);
+    SysBusDevice *sbd = SYS_BUS_DEVICE(dev);
+    SysBusDevice *sdhci_sbd = SYS_BUS_DEVICE(&s->sdhci);
+
+    memory_region_init(&s->container, OBJECT(s), "k230.sdhci-container",
+                       K230_SDHCI_REG_SIZE);
+    sysbus_init_mmio(sbd, &s->container);
+
+    memory_region_init_io(&s->iomem_fallback, OBJECT(s),
+                          &k230_sdhci_fallback_ops, s,
+                          "k230.sdhci-fallback",
+                          K230_SDHCI_REG_SIZE);
+    memory_region_add_subregion(&s->container, 0, &s->iomem_fallback);
+
+    if (!sysbus_realize(sdhci_sbd, errp)) {
+        return;
+    }
+    memory_region_add_subregion_overlap(
+        &s->container, 0, sysbus_mmio_get_region(sdhci_sbd, 0), 1);
+
+    memory_region_init_io(&s->iomem_preset, OBJECT(s),
+                          &k230_sdhci_preset_ops, s,
+                          "k230.sdhci-preset", K230_SDHCI_PRESET_SIZE);
+    memory_region_add_subregion_overlap(&s->container,
+                                        K230_SDHCI_PRESET_BASE,
+                                        &s->iomem_preset, 2);
+
+    memory_region_init_io(&s->iomem_pointer, OBJECT(s),
+                          &k230_sdhci_pointer_ops, s,
+                          "k230.sdhci-pointers", K230_SDHCI_POINTER_SIZE);
+    memory_region_add_subregion_overlap(&s->container,
+                                        K230_SDHCI_POINTER_BASE,
+                                        &s->iomem_pointer, 2);
+
+    sysbus_pass_irq(sbd, sdhci_sbd);
+    s->bus = qdev_get_child_bus(DEVICE(sdhci_sbd), "sd-bus");
+}
+
+static const VMStateDescription vmstate_k230_sdhci = {
+    .name = TYPE_K230_SDHCI,
+    .version_id = 1,
+    .minimum_version_id = 1,
+    .fields = (const VMStateField[]) {
+        VMSTATE_UINT8_ARRAY(fallback_regs, K230SDHCIState,
+                            K230_SDHCI_REG_SIZE),
+        VMSTATE_END_OF_LIST()
+    },
+};
+
+static void k230_sdhci_instance_init(Object *obj)
+{
+    K230SDHCIState *s = K230_SDHCI(obj);
+
+    object_initialize_child(obj, "generic-sdhci", &s->sdhci,
+                            TYPE_SYSBUS_SDHCI);
+
+    object_property_set_uint(OBJECT(&s->sdhci), "sd-spec-version",
+                             K230_SDHCI_SPEC_VERSION,
+                             &error_abort);
+    object_property_set_uint(OBJECT(&s->sdhci), "capareg",
+                             K230_SDHCI_CAPAREG_IMPLEMENTED,
+                             &error_abort);
+    object_property_set_uint(OBJECT(&s->sdhci), "uhs", UHS_I, &error_abort);
+}
+
+static void k230_sdhci_class_init(ObjectClass *klass, const void *data)
+{
+    DeviceClass *dc = DEVICE_CLASS(klass);
+
+    dc->desc = "Kendryte K230 SDHCI Controller";
+    dc->realize = k230_sdhci_realize;
+    device_class_set_legacy_reset(dc, k230_sdhci_reset);
+    dc->vmsd = &vmstate_k230_sdhci;
+    set_bit(DEVICE_CATEGORY_STORAGE, dc->categories);
+}
+
+static const TypeInfo k230_sdhci_type_info = {
+    .name = TYPE_K230_SDHCI,
+    .parent = TYPE_SYS_BUS_DEVICE,
+    .instance_size = sizeof(K230SDHCIState),
+    .instance_init = k230_sdhci_instance_init,
+    .class_init = k230_sdhci_class_init,
+};
+
+static void k230_sdhci_register_types(void)
+{
+    type_register_static(&k230_sdhci_type_info);
+}
+
+type_init(k230_sdhci_register_types)
hw/sd/meson.build +1 -0
index b43d45bc56..a109635233 100644
@@ -10,3 +10,4 @@ system_ss.add(when: 'CONFIG_ASPEED_SOC', if_true: files('aspeed_sdhci.c'))
 system_ss.add(when: 'CONFIG_ALLWINNER_H3', if_true: files('allwinner-sdhost.c'))
 system_ss.add(when: 'CONFIG_NPCM7XX', if_true: files('npcm7xx_sdhci.c'))
 system_ss.add(when: 'CONFIG_CADENCE_SDHCI', if_true: files('cadence_sdhci.c'))
+system_ss.add(when: 'CONFIG_K230_SDHCI', if_true: files('k230_sdhci.c'))
include/hw/sd/k230_sdhci.h new file +54 -0
new file mode 100644
index 0000000000..a8389f1de4
@@ -0,0 +1,82 @@
+/*
+ * Kendryte K230 SDHCI controller
+ *
+ * Copyright (c) 2026 Xin Xie <[email protected]>
+ *
+ * SPDX-License-Identifier: GPL-2.0-or-later
+ */
+
+#ifndef HW_SD_K230_SDHCI_H
+#define HW_SD_K230_SDHCI_H
+
+#include "qemu/bitops.h"
+#include "hw/sd/sdhci.h"
+#include "qom/object.h"
+
+#define TYPE_K230_SDHCI "k230.sdhci"
+OBJECT_DECLARE_SIMPLE_TYPE(K230SDHCIState, K230_SDHCI)
+
+#define K230_SDHCI_REG_SIZE             0x1000
+
+/* K230 TRM reset values for Capabilities Registers 1 and 2. */
+#define K230_SDHCI_CAPABILITIES1_RESET  0x256ec881ULL
+#define K230_SDHCI_CAPABILITIES2_RESET  0x0800a177ULL
+#define K230_SDHCI_CAPAREG_RESET \
+    ((K230_SDHCI_CAPABILITIES2_RESET << 32) \
+    | K230_SDHCI_CAPABILITIES1_RESET)
+
+/* SDHCI Preset Value registers. */
+#define K230_SDHCI_PRESET_BASE           0x060
+#define K230_SDHCI_PRESET_SIZE           0x010
+#define K230_SDHCI_PRESET_INIT           0x060
+#define K230_SDHCI_PRESET_DEFAULT_SPEED  0x062
+#define K230_SDHCI_PRESET_HIGH_SPEED     0x064
+#define K230_SDHCI_PRESET_SDR12          0x066
+#define K230_SDHCI_PRESET_SDR25          0x068
+#define K230_SDHCI_PRESET_SDR50          0x06a
+#define K230_SDHCI_PRESET_SDR104         0x06c
+#define K230_SDHCI_PRESET_DDR50          0x06e
+#define K230_SDHCI_PRESET_INIT_RESET          0x00fa
+#define K230_SDHCI_PRESET_DEFAULT_SPEED_RESET 0x0004
+#define K230_SDHCI_PRESET_HIGH_SPEED_RESET    0x0002
+#define K230_SDHCI_PRESET_SDR12_RESET         0x0004
+#define K230_SDHCI_PRESET_SDR25_RESET         0x0002
+#define K230_SDHCI_PRESET_SDR50_RESET         0x0001
+#define K230_SDHCI_PRESET_SDR104_RESET        0x0000
+#define K230_SDHCI_PRESET_DDR50_RESET         0x0002
+
+/* SDHCI v4 extension-area pointer registers. */
+#define K230_SDHCI_POINTER_BASE         0x0e0
+#define K230_SDHCI_POINTER_SIZE         0x00c
+#define K230_SDHCI_P_UHS2_SETTINGS      0x0e0
+#define K230_SDHCI_P_UHS2_HOST_CAPAB    0x0e2
+#define K230_SDHCI_P_UHS2_TEST          0x0e4
+#define K230_SDHCI_P_EMBEDDED_CNTRL     0x0e6

include/hw/sd/k230_sdhci.h line 64

Quoted 10 lines
include/hw/sd/k230_sdhci.h new file +10 -0
+#define K230_SDHCI_P_VENDOR_AREA1       0x0e8
+#define K230_SDHCI_P_VENDOR_AREA2       0x0ea
+#define K230_SDHCI_P_UHS2_SETTINGS_RESET    0x0000
+#define K230_SDHCI_P_UHS2_HOST_CAPAB_RESET  0x0f58
+#define K230_SDHCI_P_UHS2_TEST_RESET        0x0f68
+#define K230_SDHCI_P_EMBEDDED_CNTRL_RESET   0x0f6c
+#define K230_SDHCI_P_VENDOR_AREA1_RESET     0x0500
+#define K230_SDHCI_P_VENDOR_AREA2_RESET     0x0384
+
+/* DWC MSHC PHY register block. */

nits: remove the ending period.
Please fix this globally in this patch series.

Quoted 8 lines
include/hw/sd/k230_sdhci.h new file +8 -0
+#define K230_SDHCI_PHY_CNFG             0x300
+#define K230_SDHCI_PHY_CNFG_RSTN        BIT(0)
+#define K230_SDHCI_PHY_CNFG_PWRGOOD     BIT(1)
+
+struct K230SDHCIState {
+    SysBusDevice parent_obj;
+
+    MemoryRegion container;

include/hw/sd/k230_sdhci.h line 82

Quoted 10 lines
include/hw/sd/k230_sdhci.h new file +10 -0
+    MemoryRegion iomem_fallback;
+    MemoryRegion iomem_preset;
+    MemoryRegion iomem_pointer;
+    BusState *bus;
+    uint8_t fallback_regs[K230_SDHCI_REG_SIZE];
+
+    SDHCIState sdhci;
+};
+
+#endif

Regards,
Bin

author
#4 lore raw

PATCH 2/3 hw/riscv: enable SDHCI controllers on K230

In reply to Xin Xie

Replace the two unimplemented K230 SD controller regions with K230 SDHCI devices. Map them at 0x91580000 and 0x91581000 and connect their interrupts to PLIC inputs 142 and 144 respectively.

Create an SD card on each controller when a corresponding -drive if=sd,index=N backend is provided.

Update the K230 machine documentation to list the controllers as supported and remove the instructions that disabled the SDHCI device tree nodes before booting Linux.

---
 docs/system/riscv/k230.rst |  6 ++----
 hw/riscv/Kconfig           |  1 +
 hw/riscv/k230.c            | 34 ++++++++++++++++++++++++++++++----
 include/hw/riscv/k230.h    |  4 ++++
 4 files changed, 37 insertions(+), 8 deletions(-)
docs/system/riscv/k230.rst +2 -4
index cea8202e55..6e8286a0df 100644
@@ -20,6 +20,7 @@ The ``k230`` machine supports the following devices:
 * Platform-Level Interrupt Controller (PLIC)
 * 2 K230 Watchdog Timer
 * 5 UART
+* 2 SDHCI controllers
 
 Boot options
 ------------
@@ -100,14 +101,11 @@ Press Enter to stop autoboot. At the U-Boot prompt, run these commands:
    K230# fdt resize 8192
    K230# fdt set /chosen linux,initrd-start <0x0 0xa100000>
    K230# fdt set /chosen linux,initrd-end <0x0 ${INITRD_END}>
-   K230# fdt set /soc/sdhci0@91580000 status disabled
-   K230# fdt set /soc/sdhci1@91581000 status disabled
    K230# bootm 0xc100000 - 0xa000000
 
 Use ``setenv`` so ``bootm`` writes the kernel command line into
 ``/chosen/bootargs``. The ``fdt`` commands select the loaded DTB, add space for
-edits, describe the initrd range in ``/chosen``, and disable SDHCI nodes because
-this machine does not emulate those controllers yet. Replace ``${INITRD_END}``
+edits, and describe the initrd range in ``/chosen``. Replace ``${INITRD_END}``
 with the host-calculated value above when typing the command. ``cma=0`` avoids
 the SDK kernel reserving too much of the little-core memory window for initramfs
 boot.
hw/riscv/Kconfig +1 -0
index de37c08cae..e4a0616c16 100644
@@ -162,3 +162,4 @@ config K230
     select SERIAL_MM
     select UNIMP
     select K230_WDT
+    select K230_SDHCI
hw/riscv/k230.c +30 -4
index 656f28190c..7ea69084ae 100644
@@ -110,6 +110,8 @@ static void k230_soc_init(Object *obj)
     object_initialize_child(obj, "c908-cpu", cpu0, TYPE_RISCV_HART_ARRAY);
     object_initialize_child(obj, "k230-wdt0", &s->wdt[0], TYPE_K230_WDT);
     object_initialize_child(obj, "k230-wdt1", &s->wdt[1], TYPE_K230_WDT);
+    object_initialize_child(obj, "sdhci0", &s->sdhci[0], TYPE_K230_SDHCI);
+    object_initialize_child(obj, "sdhci1", &s->sdhci[1], TYPE_K230_SDHCI);
 
     qdev_prop_set_uint32(DEVICE(cpu0), "hartid-base", 0);
     qdev_prop_set_string(DEVICE(cpu0), "cpu-type", TYPE_RISCV_CPU_THEAD_C908);
@@ -343,11 +345,21 @@ static void k230_soc_realize(DeviceState *dev, Error **errp)
     create_unimplemented_device("usb1", memmap[K230_DEV_USB1].base,
                                 memmap[K230_DEV_USB1].size);
 
-    create_unimplemented_device("sd0", memmap[K230_DEV_SD0].base,
-                                memmap[K230_DEV_SD0].size);
+    for (int i = 0; i < 2; i++) {
+        if (!sysbus_realize(SYS_BUS_DEVICE(&s->sdhci[i]), errp)) {
+            return;
+        }
+    }
+
+    sysbus_mmio_map(SYS_BUS_DEVICE(&s->sdhci[0]), 0,
+                    memmap[K230_DEV_SD0].base);
+    sysbus_connect_irq(SYS_BUS_DEVICE(&s->sdhci[0]), 0,
+                       qdev_get_gpio_in(s->c908_plic, K230_SD0_IRQ));
 
-    create_unimplemented_device("sd1", memmap[K230_DEV_SD1].base,
-                                memmap[K230_DEV_SD1].size);
+    sysbus_mmio_map(SYS_BUS_DEVICE(&s->sdhci[1]), 0,
+                    memmap[K230_DEV_SD1].base);
+    sysbus_connect_irq(SYS_BUS_DEVICE(&s->sdhci[1]), 0,
+                       qdev_get_gpio_in(s->c908_plic, K230_SD1_IRQ));
 
     create_unimplemented_device("qspi0", memmap[K230_DEV_QSPI0].base,
                                 memmap[K230_DEV_QSPI0].size);
@@ -490,6 +502,20 @@ static void k230_machine_init(MachineState *machine)
                             TYPE_RISCV_K230_SOC);
     qdev_realize(DEVICE(&s->soc), NULL, &error_fatal);
 
+    for (int i = 0; i < 2; i++) {
+        DriveInfo *dinfo = drive_get(IF_SD, 0, i);
+        DeviceState *card;
+
+        if (!dinfo) {
+            continue;
+        }
+
+        card = qdev_new(TYPE_SD_CARD);
+        qdev_prop_set_drive_err(card, "drive", blk_by_legacy_dinfo(dinfo),
+                                &error_fatal);
+        qdev_realize_and_unref(card, s->soc.sdhci[i].bus, &error_fatal);
+    }
+
     /* Data Memory */
     memory_region_add_subregion(sys_mem, memmap[K230_DEV_DDRC].base,
                                 machine->ram);
include/hw/riscv/k230.h +4 -0
index 592e1c26bf..8e73366e56 100644
@@ -18,6 +18,7 @@
 #include "hw/core/boards.h"
 #include "hw/riscv/riscv_hart.h"
 #include "hw/watchdog/k230_wdt.h"
+#include "hw/sd/k230_sdhci.h"
 
 #define C908_CPU_HARTID   (0)
 
@@ -33,6 +34,7 @@ typedef struct K230SoCState {
     RISCVHartArrayState c908_cpu; /* Small core */
 
     K230WdtState wdt[2];
+    K230SDHCIState sdhci[2];
     MemoryRegion sram;
     MemoryRegion bootrom;
 
@@ -129,6 +131,8 @@ enum {
     K230_UART4_IRQ  = 20,
     K230_WDT0_IRQ   = 107,
     K230_WDT1_IRQ   = 108,
+    K230_SD0_IRQ    = 142,
+    K230_SD1_IRQ    = 144,
 };
 
 #define K230_UART_COUNT 5
Signature · 1 line
2.43.0
#5 lore raw

Re: [PATCH v1 2/3] hw/riscv: enable SDHCI controllers on K230

Quoted 102 lines · Xin Xie

Replace the two unimplemented K230 SD controller regions with K230 SDHCI devices. Map them at 0x91580000 and 0x91581000 and connect their interrupts to PLIC inputs 142 and 144 respectively.

Create an SD card on each controller when a corresponding -drive if=sd,index=N backend is provided.

Update the K230 machine documentation to list the controllers as supported and remove the instructions that disabled the SDHCI device tree nodes before booting Linux.

---
  docs/system/riscv/k230.rst |  6 ++----
  hw/riscv/Kconfig           |  1 +
  hw/riscv/k230.c            | 34 ++++++++++++++++++++++++++++++----
  include/hw/riscv/k230.h    |  4 ++++
  4 files changed, 37 insertions(+), 8 deletions(-)
docs/system/riscv/k230.rst +2 -4
index cea8202e55..6e8286a0df 100644
@@ -20,6 +20,7 @@ The ``k230`` machine supports the following devices:
  * Platform-Level Interrupt Controller (PLIC)
  * 2 K230 Watchdog Timer
  * 5 UART
+* 2 SDHCI controllers
 
  Boot options
  ------------
@@ -100,14 +101,11 @@ Press Enter to stop autoboot. At the U-Boot prompt, run these commands:
     K230# fdt resize 8192
     K230# fdt set /chosen linux,initrd-start <0x0 0xa100000>
     K230# fdt set /chosen linux,initrd-end <0x0 ${INITRD_END}>
-   K230# fdt set /soc/sdhci0@91580000 status disabled
-   K230# fdt set /soc/sdhci1@91581000 status disabled
     K230# bootm 0xc100000 - 0xa000000
 
  Use ``setenv`` so ``bootm`` writes the kernel command line into
  ``/chosen/bootargs``. The ``fdt`` commands select the loaded DTB, add space for
-edits, describe the initrd range in ``/chosen``, and disable SDHCI nodes because
-this machine does not emulate those controllers yet. Replace ``${INITRD_END}``
+edits, and describe the initrd range in ``/chosen``. Replace ``${INITRD_END}``
  with the host-calculated value above when typing the command. ``cma=0`` avoids
  the SDK kernel reserving too much of the little-core memory window for initramfs
  boot.
hw/riscv/Kconfig +1 -0
index de37c08cae..e4a0616c16 100644
@@ -162,3 +162,4 @@ config K230
      select SERIAL_MM
      select UNIMP
      select K230_WDT
+    select K230_SDHCI
hw/riscv/k230.c +19 -4
index 656f28190c..7ea69084ae 100644
@@ -110,6 +110,8 @@ static void k230_soc_init(Object *obj)
      object_initialize_child(obj, "c908-cpu", cpu0, TYPE_RISCV_HART_ARRAY);
      object_initialize_child(obj, "k230-wdt0", &s->wdt[0], TYPE_K230_WDT);
      object_initialize_child(obj, "k230-wdt1", &s->wdt[1], TYPE_K230_WDT);
+    object_initialize_child(obj, "sdhci0", &s->sdhci[0], TYPE_K230_SDHCI);
+    object_initialize_child(obj, "sdhci1", &s->sdhci[1], TYPE_K230_SDHCI);
 
      qdev_prop_set_uint32(DEVICE(cpu0), "hartid-base", 0);
      qdev_prop_set_string(DEVICE(cpu0), "cpu-type", TYPE_RISCV_CPU_THEAD_C908);
@@ -343,11 +345,21 @@ static void k230_soc_realize(DeviceState *dev, Error **errp)
      create_unimplemented_device("usb1", memmap[K230_DEV_USB1].base,
                                  memmap[K230_DEV_USB1].size);
 
-    create_unimplemented_device("sd0", memmap[K230_DEV_SD0].base,
-                                memmap[K230_DEV_SD0].size);
+    for (int i = 0; i < 2; i++) {
+        if (!sysbus_realize(SYS_BUS_DEVICE(&s->sdhci[i]), errp)) {
+            return;
+        }
+    }
+
+    sysbus_mmio_map(SYS_BUS_DEVICE(&s->sdhci[0]), 0,
+                    memmap[K230_DEV_SD0].base);
+    sysbus_connect_irq(SYS_BUS_DEVICE(&s->sdhci[0]), 0,
+                       qdev_get_gpio_in(s->c908_plic, K230_SD0_IRQ));
 
-    create_unimplemented_device("sd1", memmap[K230_DEV_SD1].base,
-                                memmap[K230_DEV_SD1].size);
+    sysbus_mmio_map(SYS_BUS_DEVICE(&s->sdhci[1]), 0,
+                    memmap[K230_DEV_SD1].base);
+    sysbus_connect_irq(SYS_BUS_DEVICE(&s->sdhci[1]), 0,
+                       qdev_get_gpio_in(s->c908_plic, K230_SD1_IRQ));
 
      create_unimplemented_device("qspi0", memmap[K230_DEV_QSPI0].base,
                                  memmap[K230_DEV_QSPI0].size);
@@ -490,6 +502,20 @@ static void k230_machine_init(MachineState *machine)
                              TYPE_RISCV_K230_SOC);
      qdev_realize(DEVICE(&s->soc), NULL, &error_fatal);
 
+    for (int i = 0; i < 2; i++) {
+        DriveInfo *dinfo = drive_get(IF_SD, 0, i);
+        DeviceState *card;

hw/riscv/k230.c static void k230_machine_init(MachineState *machine) · line 517

Quoted 10 lines
hw/riscv/k230.c +10 -0
+
+        if (!dinfo) {
+            continue;
+        }
+
+        card = qdev_new(TYPE_SD_CARD);
+        qdev_prop_set_drive_err(card, "drive", blk_by_legacy_dinfo(dinfo),
+                                &error_fatal);
+        qdev_realize_and_unref(card, s->soc.sdhci[i].bus, &error_fatal);
+    }

I think this should be done in k230_soc_realize() together with the realize() step of the other parts of the soc.

LGTM otherwise. Thanks,

Daniel

Quoted 33 lines
hw/riscv/k230.c +1 -0
+
      /* Data Memory */
      memory_region_add_subregion(sys_mem, memmap[K230_DEV_DDRC].base,
                                  machine->ram);
include/hw/riscv/k230.h +4 -0
index 592e1c26bf..8e73366e56 100644
@@ -18,6 +18,7 @@
  #include "hw/core/boards.h"
  #include "hw/riscv/riscv_hart.h"
  #include "hw/watchdog/k230_wdt.h"
+#include "hw/sd/k230_sdhci.h"
 
  #define C908_CPU_HARTID   (0)
 
@@ -33,6 +34,7 @@ typedef struct K230SoCState {
      RISCVHartArrayState c908_cpu; /* Small core */
 
      K230WdtState wdt[2];
+    K230SDHCIState sdhci[2];
      MemoryRegion sram;
      MemoryRegion bootrom;
 
@@ -129,6 +131,8 @@ enum {
      K230_UART4_IRQ  = 20,
      K230_WDT0_IRQ   = 107,
      K230_WDT1_IRQ   = 108,
+    K230_SD0_IRQ    = 142,
+    K230_SD1_IRQ    = 144,
  };
 
  #define K230_UART_COUNT 5
author
#6 lore raw

PATCH 3/3 tests/qtest: add K230 SDHCI tests

In reply to Xin Xie

Add qtests covering the K230-specific SDHCI register layout, capability masking, preset values, extension pointers, PHY power status and per-controller vendor register storage.

Verify card detection and interrupt delivery through the K230 PLIC. Exercise basic PIO reads and writes with an attached SD card image and check that written data reaches the backing file.

---
 MAINTAINERS                   |   1 +
 tests/qtest/k230-sdhci-test.c | 214 ++++++++++++++++++++++++++++++++++
 tests/qtest/meson.build       |   5 +-
 3 files changed, 219 insertions(+), 1 deletion(-)
 create mode 100644 tests/qtest/k230-sdhci-test.c
MAINTAINERS +1 -0
index 06285352c4..99c4c3294b 100644
@@ -1831,6 +1831,7 @@ F: include/hw/riscv/k230.h
 F: include/hw/watchdog/k230_wdt.h
 F: include/hw/sd/k230_sdhci.h
 F: tests/qtest/k230-wdt-test.c
+F: tests/qtest/k230-sdhci-test.c
 
 RX Machines
 -----------
tests/qtest/k230-sdhci-test.c new file +214 -0

214 lines not on this page · raw message

tests/qtest/meson.build +4 -1
index 56ff860e21..0600b1e807 100644
@@ -297,7 +297,10 @@ 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-sdhci-test',
+  ] : [])
 
 qtests_hexagon = ['boot-serial-test']
 
Signature · 1 line
2.43.0
Reviewed-by on v1 3/3
#7 lore raw

Re: [PATCH v1 3/3] tests/qtest: add K230 SDHCI tests

Quoted 10 lines · Xin Xie

Add qtests covering the K230-specific SDHCI register layout, capability masking, preset values, extension pointers, PHY power status and per-controller vendor register storage.

Verify card detection and interrupt delivery through the K230 PLIC. Exercise basic PIO reads and writes with an attached SD card image and check that written data reaches the backing file.

---

Quoted 254 lines
  MAINTAINERS                   |   1 +
  tests/qtest/k230-sdhci-test.c | 214 ++++++++++++++++++++++++++++++++++
  tests/qtest/meson.build       |   5 +-
  3 files changed, 219 insertions(+), 1 deletion(-)

create mode 100644 tests/qtest/k230-sdhci-test.c

MAINTAINERS +1 -0
index 06285352c4..99c4c3294b 100644
@@ -1831,6 +1831,7 @@ F: include/hw/riscv/k230.h
  F: include/hw/watchdog/k230_wdt.h
  F: include/hw/sd/k230_sdhci.h
  F: tests/qtest/k230-wdt-test.c
+F: tests/qtest/k230-sdhci-test.c
 
  RX Machines
  -----------
tests/qtest/k230-sdhci-test.c new file +214 -0
new file mode 100644
index 0000000000..5eedb130c3
@@ -0,0 +1,214 @@
+/*
+ * QTest testcase for the Kendryte K230 SDHCI controllers
+ *
+ * Copyright (c) 2026 Xin Xie <[email protected]>
+ *
+ * SPDX-License-Identifier: GPL-2.0-or-later
+ */
+
+#include "qemu/osdep.h"
+#include "qemu/timer.h"
+#include "qemu/units.h"
+#include "libqtest.h"
+#include "libqos/sdhci-cmd.h"
+#include "hw/sd/k230_sdhci.h"
+#include "hw/sd/sdhci-internal.h"
+
+#define K230_SD0_BASE          0x91580000
+#define K230_SD1_BASE          0x91581000
+#define K230_PLIC_PENDING      0xf00001000ULL
+#define K230_SD1_IRQ           144
+
+#define K230_SD_TEST_IMAGE_SIZE (2 * MiB)
+#define K230_SD_TEST_BLOCK_SIZE 512
+
+static void test_registers(void)
+{
+    QTestState *qts = qtest_init("-machine k230");
+    uint64_t capabilities;
+    uint64_t expected = K230_SDHCI_CAPAREG_RESET;
+
+    /* Only capabilities unsupported by the QEMU model are removed. */
+    expected = FIELD_DP64(expected, SDHC_CAPAB, ASYNC_INT, 0);
+    expected = FIELD_DP64(expected, SDHC_CAPAB, TIMER_RETUNING, 0);
+    expected = FIELD_DP64(expected, SDHC_CAPAB, RETUNING_MODE, 0);
+    expected = FIELD_DP64(expected, SDHC_CAPAB, ADMA3, 0);
+    capabilities = qtest_readq(qts, K230_SD0_BASE + SDHC_CAPAB);
+    g_assert_cmphex(capabilities, ==, expected);
+
+    /* Preset Value registers use the TRM reset values and are read-only. */
+    g_assert(qtest_readw(qts, K230_SD0_BASE + K230_SDHCI_PRESET_INIT) ==
+             K230_SDHCI_PRESET_INIT_RESET);
+    g_assert(qtest_readw(qts, K230_SD0_BASE +
+                         K230_SDHCI_PRESET_DEFAULT_SPEED) ==
+             K230_SDHCI_PRESET_DEFAULT_SPEED_RESET);
+    g_assert(qtest_readw(qts, K230_SD0_BASE +
+                         K230_SDHCI_PRESET_HIGH_SPEED) ==
+             K230_SDHCI_PRESET_HIGH_SPEED_RESET);
+    g_assert(qtest_readw(qts, K230_SD0_BASE + K230_SDHCI_PRESET_SDR12) ==
+             K230_SDHCI_PRESET_SDR12_RESET);
+    g_assert(qtest_readw(qts, K230_SD0_BASE + K230_SDHCI_PRESET_SDR25) ==
+             K230_SDHCI_PRESET_SDR25_RESET);
+    g_assert(qtest_readw(qts, K230_SD0_BASE + K230_SDHCI_PRESET_SDR50) ==
+             K230_SDHCI_PRESET_SDR50_RESET);
+    g_assert(qtest_readw(qts, K230_SD0_BASE + K230_SDHCI_PRESET_SDR104) ==
+             K230_SDHCI_PRESET_SDR104_RESET);
+    g_assert(qtest_readw(qts, K230_SD0_BASE + K230_SDHCI_PRESET_DDR50) ==
+             K230_SDHCI_PRESET_DDR50_RESET);
+
+    qtest_writew(qts, K230_SD0_BASE + K230_SDHCI_PRESET_SDR50, 0xffff);
+    g_assert(qtest_readw(qts, K230_SD0_BASE + K230_SDHCI_PRESET_SDR50) ==
+             K230_SDHCI_PRESET_SDR50_RESET);
+
+    /* Extension-area pointers use their documented read-only reset values. */
+    g_assert(qtest_readw(qts, K230_SD0_BASE + K230_SDHCI_P_UHS2_SETTINGS) ==
+             K230_SDHCI_P_UHS2_SETTINGS_RESET);
+    g_assert(qtest_readw(qts, K230_SD0_BASE + K230_SDHCI_P_UHS2_HOST_CAPAB) ==
+             K230_SDHCI_P_UHS2_HOST_CAPAB_RESET);
+    g_assert(qtest_readw(qts, K230_SD0_BASE + K230_SDHCI_P_UHS2_TEST) ==
+             K230_SDHCI_P_UHS2_TEST_RESET);
+    g_assert(qtest_readw(qts, K230_SD0_BASE + K230_SDHCI_P_EMBEDDED_CNTRL) ==
+             K230_SDHCI_P_EMBEDDED_CNTRL_RESET);
+    g_assert(qtest_readw(qts, K230_SD0_BASE + K230_SDHCI_P_VENDOR_AREA1) ==
+             K230_SDHCI_P_VENDOR_AREA1_RESET);
+    g_assert(qtest_readw(qts, K230_SD0_BASE + K230_SDHCI_P_VENDOR_AREA2) ==
+             K230_SDHCI_P_VENDOR_AREA2_RESET);
+
+    qtest_writew(qts, K230_SD0_BASE + K230_SDHCI_P_VENDOR_AREA1, 0xffff);
+    g_assert(qtest_readw(qts, K230_SD0_BASE + K230_SDHCI_P_VENDOR_AREA1) ==
+             K230_SDHCI_P_VENDOR_AREA1_RESET);
+
+    /* The virtual PHY is always ready and software cannot clear PWRGOOD. */
+    g_assert((qtest_readl(qts, K230_SD0_BASE + K230_SDHCI_PHY_CNFG) &
+              K230_SDHCI_PHY_CNFG_PWRGOOD) == K230_SDHCI_PHY_CNFG_PWRGOOD);
+    qtest_writel(qts, K230_SD0_BASE + K230_SDHCI_PHY_CNFG, 0);
+    g_assert(qtest_readl(qts, K230_SD0_BASE + K230_SDHCI_PHY_CNFG) ==
+             K230_SDHCI_PHY_CNFG_PWRGOOD);
+
+    /* Vendor storage is writable and private to each controller instance. */
+    qtest_writel(qts, K230_SD0_BASE + K230_SDHCI_P_VENDOR_AREA1_RESET,
+                 0x89abcdef);
+    g_assert(qtest_readl(qts, K230_SD0_BASE +
+                         K230_SDHCI_P_VENDOR_AREA1_RESET) ==
+             0x89abcdef);
+    g_assert(qtest_readl(qts, K230_SD1_BASE +
+                         K230_SDHCI_P_VENDOR_AREA1_RESET) == 0);
+
+    qtest_quit(qts);
+}
+
+static void test_card_detect(void)
+{
+    g_autofree char *path = NULL;
+    int fd = g_file_open_tmp("k230-sd-XXXXXX.img", &path, NULL);
+    QTestState *qts;
+
+    g_assert(fd >= 0);
+    g_assert(ftruncate(fd, K230_SD_TEST_IMAGE_SIZE) == 0);
+    close(fd);
+
+    qts = qtest_initf("-machine k230 "
+                      "-drive if=sd,index=1,format=raw,file=%s", path);
+
+    /* Card insertion becomes visible after the generic insertion delay. */
+    qtest_clock_step(qts, NANOSECONDS_PER_SECOND);
+    g_assert((qtest_readl(qts, K230_SD1_BASE + SDHC_PRNSTS) &
+              SDHC_CARD_PRESENT) == SDHC_CARD_PRESENT);
+    g_assert((qtest_readl(qts, K230_SD0_BASE + SDHC_PRNSTS) &
+              SDHC_CARD_PRESENT) == 0);
+
+    /* Route a command-complete event through SDHCI to the K230 PLIC. */
+    qtest_writew(qts, K230_SD1_BASE + SDHC_NORINTSTSEN,
+                 SDHC_NISEN_CMDCMP);
+    qtest_writew(qts, K230_SD1_BASE + SDHC_NORINTSIGEN,
+                 SDHC_NISEN_CMDCMP);
+    g_assert((qtest_readw(qts, K230_SD1_BASE + SDHC_NORINTSTSEN) &
+              SDHC_NISEN_CMDCMP) != 0);
+    g_assert((qtest_readw(qts, K230_SD1_BASE + SDHC_NORINTSIGEN) &
+              SDHC_NISEN_CMDCMP) != 0);
+    qtest_writew(qts, K230_SD1_BASE + SDHC_CLKCON,
+                 SDHC_CLOCK_INT_EN | SDHC_CLOCK_SDCLK_EN);
+    qtest_writew(qts, K230_SD1_BASE + SDHC_CMDREG,
+                 SDHC_APP_CMD | SDHC_CMD_RESPONSE);
+    g_assert((qtest_readw(qts, K230_SD1_BASE + SDHC_NORINTSTS) &
+              SDHC_NIS_CMDCMP) != 0);
+    g_assert((qtest_readl(qts, K230_PLIC_PENDING +
+                          K230_SD1_IRQ / 32 * sizeof(uint32_t)) &
+              BIT(K230_SD1_IRQ % 32)) != 0);
+
+    qtest_quit(qts);
+    unlink(path);
+}
+
+static void k230_sdhci_select_card(QTestState *qts)
+{
+    uint16_t rca;
+
+    /* Reset the controller and enable its internal and SD clocks. */
+    qtest_writeb(qts, K230_SD1_BASE + SDHC_SWRST, SDHC_RESET_ALL);
+    qtest_writew(qts, K230_SD1_BASE + SDHC_CLKCON,
+                 SDHC_CLOCK_INT_EN | SDHC_CLOCK_INT_STABLE |
+                 SDHC_CLOCK_SDCLK_EN);
+
+    /* Run the SD identification sequence and select the assigned RCA. */
+    sdhci_cmd_regs(qts, K230_SD1_BASE, 0, 0, 0, 0, SDHC_APP_CMD);
+    sdhci_cmd_regs(qts, K230_SD1_BASE, 0, 0, 0x41200000, 0, 41 << 8);
+    sdhci_cmd_regs(qts, K230_SD1_BASE, 0, 0, 0, 0, SDHC_ALL_SEND_CID);
+    sdhci_cmd_regs(qts, K230_SD1_BASE, 0, 0, 0, 0,
+                   SDHC_SEND_RELATIVE_ADDR | SDHC_CMD_RESPONSE);
+    rca = qtest_readl(qts, K230_SD1_BASE + SDHC_RSPREG0) >> 16;
+    sdhci_cmd_regs(qts, K230_SD1_BASE, 0, 0, rca << 16, 0,
+                   SDHC_SELECT_DESELECT_CARD);
+}
+
+static void test_card_io(void)
+{
+    static const char read_data[] = "K230 SDHCI read";
+    static const char write_data[] = "K230 SDHCI write";
+    g_autofree char *path = NULL;
+    char buffer[K230_SD_TEST_BLOCK_SIZE];
+    int fd = g_file_open_tmp("k230-sd-XXXXXX.img", &path, NULL);
+    QTestState *qts;
+
+    /* Seed block zero so the controller read can be checked independently. */
+    g_assert(fd >= 0);
+    g_assert(ftruncate(fd, K230_SD_TEST_IMAGE_SIZE) == 0);
+    g_assert(pwrite(fd, read_data, sizeof(read_data), 0) == sizeof(read_data));
+    close(fd);
+
+    qts = qtest_initf("-machine k230 "
+                      "-drive if=sd,index=1,format=raw,auto-read-only=off,"
+                      "file=%s", path);
+    k230_sdhci_select_card(qts);
+
+    /* Read block zero through the SDHCI PIO data port. */
+    memset(buffer, 0, sizeof(buffer));
+    g_assert(sdhci_read_cmd(qts, K230_SD1_BASE, buffer,
+                            sizeof(read_data)) == sizeof(read_data));
+    g_assert(memcmp(buffer, read_data, sizeof(read_data)) == 0);
+
+    /* Write through SDHCI, then verify the data reached the backing image. */
+    sdhci_write_cmd(qts, K230_SD1_BASE, write_data, sizeof(write_data),
+                    K230_SD_TEST_BLOCK_SIZE);
+    qtest_quit(qts);
+
+    fd = open(path, O_RDONLY);
+    g_assert(fd >= 0);
+    memset(buffer, 0, sizeof(buffer));
+    g_assert(pread(fd, buffer, sizeof(write_data), 0) == sizeof(write_data));
+    close(fd);
+    g_assert(memcmp(buffer, write_data, sizeof(write_data)) == 0);
+
+    unlink(path);
+}
+
+int main(int argc, char *argv[])
+{
+    g_test_init(&argc, &argv, NULL);
+
+    qtest_add_func("/k230-sdhci/registers", test_registers);
+    qtest_add_func("/k230-sdhci/card-detect", test_card_detect);
+    qtest_add_func("/k230-sdhci/card-io", test_card_io);
+
+    return g_test_run();
+}
tests/qtest/meson.build +4 -1
index 56ff860e21..0600b1e807 100644
@@ -297,7 +297,10 @@ 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-sdhci-test',
+  ] : [])
 
  qtests_hexagon = ['boot-serial-test']