Skip to contentSkip to conversation

RISC-V: NEORV32 CPU, peripherials, and machine

PATCH In review Michael Levit 3 participants 12 messages Nov 9, 2025 – Nov 13, 2025

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

v4 cover RISC-V: NEORV32 CPU, peripherials, and machine

This v4 reworks the initial NEORV32 submissions.

The series introduces:
* a minimal NEORV32 RV32 CPU type and vendor CSR hook,
* the SYSINFO MMIO block,
* a small UART device,
* an SPI controller with command-mode chip-select,
* and the 'neorv32' RISC-V board wiring the above, plus docs.

Tested by booting the NEORV32 bootloader as -bios and chaining into a Hello World from an MTD-backed SPI flash image, with UART on stdio.

Changes since v3:
* Clean-up and fix documentation build errors
* Fixed extra blank lines

Thanks Daniel for the review and for providing the documentation fix suggestions.

Changes since v2:
* Clean-up all errors and most of the warnings generated by scripts/checkpatch.pl
* Sync with master, fix compile error
* No intentional functional changes; only file organization and clarity.
* Kept default.mak entry off by default (n).

Patch layout
============

  1/5  target/riscv: add NEORV32 RV32 CPU type and vendor CSR hooks
  2/5  hw/misc: add NEORV32 SYSINFO block (CLK/MISC/SOC/CACHE)
  3/5  hw/char: add NEORV32 UART (CTRL/DATA, fifo, chardev)
  4/5  hw/ssi: add NEORV32 SPI controller (SSI master, CS command)
  5/5  hw/riscv: introduce 'neorv32' board, docs, and riscv32 device config

Quick usage
===========

  $ ./configure --target-list=riscv32-softmmu --enable-debug --enable-fdt
  $ make -j$(nproc)

Prepare a flash image (64MiB) and place your app at 4MiB offset:

  $ dd if=/dev/zero of=$HOME/flash_contents.bin bs=1 count=$((0x04000000))
  $ dd if=/path/to/neorv32_exe.bin of=$HOME/flash_contents.bin \\
       bs=1 seek=$((0x00400000)) conv=notrunc

Run bootloader and chain-load your app:

  $ ./build/qemu-system-riscv32 -nographic -machine neorv32 \\
      -bios /path/to/neorv32/bootloader/neorv32_raw_exe.bin \\
      -drive file=$HOME/flash_contents.bin,if=mtd,format=raw

Debugging:

  $ ... -s -S   # gdbstub on :1234, start paused

Thanks for reviewing!
Michael

Michael (5):
target/riscv: add NEORV32 RV32 CPU type and vendor CSR hooks hw/misc: add NEORV32 SYSINFO block (CLK/MISC/SOC/CACHE) hw/char: add NEORV32 UART (CTRL/DATA, fifo, chardev) hw/ssi: add NEORV32 SPI controller (SSI master, CS command) hw/riscv: introduce 'neorv32' board, docs, and riscv32 device config

 configs/devices/riscv32-softmmu/default.mak |   1 +
 docs/system/riscv/neorv32.rst               | 112 +++++
 docs/system/target-riscv.rst                |   1 +
 hw/char/Kconfig                             |   3 +
 hw/char/meson.build                         |   1 +
 hw/char/neorv32_uart.c                      | 285 ++++++++++++
 hw/misc/Kconfig                             |   2 +
 hw/misc/meson.build                         |   1 +
 hw/misc/neorv32_sysinfo.c                   | 201 ++++++++
 hw/misc/neorv32_sysinfo.h                   |  88 ++++
 hw/misc/neorv32_sysinfo_rtl.h               | 239 ++++++++++
 hw/riscv/Kconfig                            |   8 +
 hw/riscv/meson.build                        |   1 +
 hw/riscv/neorv32.c                          | 215 +++++++++
 hw/ssi/Kconfig                              |   4 +
 hw/ssi/meson.build                          |   1 +
 hw/ssi/neorv32_spi.c                        | 478 ++++++++++++++++++++
 include/hw/char/neorv32_uart.h              |  54 +++
 include/hw/riscv/neorv32.h                  |  54 +++
 include/hw/ssi/neorv32_spi.h                |  57 +++
 target/riscv/cpu-qom.h                      |   1 +
 target/riscv/cpu.c                          |  17 +
 target/riscv/cpu.h                          |   3 +
 target/riscv/cpu_cfg.h                      |   1 +
 target/riscv/cpu_cfg_fields.h.inc           |   1 +
 target/riscv/cpu_vendorid.h                 |   2 +
 target/riscv/meson.build                    |   1 +
 target/riscv/neorv32_csr.c                  |  40 ++
 28 files changed, 1872 insertions(+)
 create mode 100644 docs/system/riscv/neorv32.rst
 create mode 100644 hw/char/neorv32_uart.c
 create mode 100644 hw/misc/neorv32_sysinfo.c
 create mode 100644 hw/misc/neorv32_sysinfo.h
 create mode 100644 hw/misc/neorv32_sysinfo_rtl.h
 create mode 100644 hw/riscv/neorv32.c
 create mode 100644 hw/ssi/neorv32_spi.c
 create mode 100644 include/hw/char/neorv32_uart.h
 create mode 100644 include/hw/riscv/neorv32.h
 create mode 100644 include/hw/ssi/neorv32_spi.h
 create mode 100644 target/riscv/neorv32_csr.c
Signature · 1 line
2.51.1
#1 lore raw

PATCH v4 1/5 target/riscv: add NEORV32 RV32 CPU type and vendor CSR hooks

From: Michael <[email protected]>

Introduce NEORV32 RV32 CPU type under target/riscv, wire NEORV32 vendor ID, and add a vendor CSR (CSR_MXISA) guarded by mvendorid match, plus meson glue.

---
 target/riscv/cpu-qom.h            |  1 +
 target/riscv/cpu.c                | 17 +++++++++++++
 target/riscv/cpu.h                |  3 +++
 target/riscv/cpu_cfg.h            |  1 +
 target/riscv/cpu_cfg_fields.h.inc |  1 +
 target/riscv/cpu_vendorid.h       |  2 ++
 target/riscv/meson.build          |  1 +
 target/riscv/neorv32_csr.c        | 40 +++++++++++++++++++++++++++++++
 8 files changed, 66 insertions(+)
 create mode 100644 target/riscv/neorv32_csr.c
target/riscv/cpu-qom.h +1 -0
index 75f4e43408..e0c1f5888f 100644
@@ -43,6 +43,7 @@
 #define TYPE_RISCV_CPU_RVA23U64         RISCV_CPU_TYPE_NAME("rva23u64")
 #define TYPE_RISCV_CPU_RVA23S64         RISCV_CPU_TYPE_NAME("rva23s64")
 #define TYPE_RISCV_CPU_IBEX             RISCV_CPU_TYPE_NAME("lowrisc-ibex")
+#define TYPE_RISCV_CPU_NEORV32          RISCV_CPU_TYPE_NAME("neorv32")
 #define TYPE_RISCV_CPU_SHAKTI_C         RISCV_CPU_TYPE_NAME("shakti-c")
 #define TYPE_RISCV_CPU_SIFIVE_E         RISCV_CPU_TYPE_NAME("sifive-e")
 #define TYPE_RISCV_CPU_SIFIVE_E31       RISCV_CPU_TYPE_NAME("sifive-e31")
target/riscv/cpu.c +17 -0
index 73d4280d7c..67260ed9d7 100644
@@ -233,6 +233,7 @@ const RISCVIsaExtData isa_edata_arr[] = {
     ISA_EXT_DATA_ENTRY(svrsw60t59b, PRIV_VERSION_1_13_0, ext_svrsw60t59b),
     ISA_EXT_DATA_ENTRY(svukte, PRIV_VERSION_1_13_0, ext_svukte),
     ISA_EXT_DATA_ENTRY(svvptc, PRIV_VERSION_1_13_0, ext_svvptc),
+    ISA_EXT_DATA_ENTRY(xneorv32xisa, PRIV_VERSION_1_10_0, ext_xneorv32xisa),
     ISA_EXT_DATA_ENTRY(xtheadba, PRIV_VERSION_1_11_0, ext_xtheadba),
     ISA_EXT_DATA_ENTRY(xtheadbb, PRIV_VERSION_1_11_0, ext_xtheadbb),
     ISA_EXT_DATA_ENTRY(xtheadbs, PRIV_VERSION_1_11_0, ext_xtheadbs),
@@ -1366,6 +1367,7 @@ const RISCVCPUMultiExtConfig riscv_cpu_vendor_exts[] = {
     MULTI_EXT_CFG_BOOL("xtheadmempair", ext_xtheadmempair, false),
     MULTI_EXT_CFG_BOOL("xtheadsync", ext_xtheadsync, false),
     MULTI_EXT_CFG_BOOL("xventanacondops", ext_XVentanaCondOps, false),
+    MULTI_EXT_CFG_BOOL("xneorv32xisa", ext_xneorv32xisa, false),
 
     { },
 };
@@ -3075,6 +3077,21 @@ static const TypeInfo riscv_cpu_type_infos[] = {
         .misa_mxl_max = MXL_RV32,
         .misa_ext = RVE
     ),
+    DEFINE_RISCV_CPU(TYPE_RISCV_CPU_NEORV32, TYPE_RISCV_VENDOR_CPU,
+        .misa_mxl_max = MXL_RV32,
+        .misa_ext = RVI | RVM | RVA | RVC | RVU,
+        .priv_spec = PRIV_VERSION_1_10_0,
+
+        .cfg.max_satp_mode = VM_1_10_MBARE,
+        .cfg.ext_zifencei = true,
+        .cfg.ext_zicsr = true,
+        .cfg.pmp = true,
+        .cfg.pmp_regions = 16,
+        .cfg.mvendorid = NEORV32_VENDOR_ID,
+#ifndef CONFIG_USER_ONLY
+        .custom_csrs = neorv32_csr_list
+#endif
+    ),
 #endif
 
 #if (defined(TARGET_RISCV64) && !defined(CONFIG_USER_ONLY))
target/riscv/cpu.h +3 -0
index 36e7f10037..6a9918a25a 100644
@@ -985,5 +985,8 @@ const char *satp_mode_str(uint8_t satp_mode, bool is_32_bit);
 /* In th_csr.c */
 extern const RISCVCSR th_csr_list[];
 
+/* Implemented in neorv32_csr.c */
+extern const RISCVCSR neorv32_csr_list[];
+
 const char *priv_spec_to_str(int priv_version);
 #endif /* RISCV_CPU_H */
target/riscv/cpu_cfg.h +1 -0
index aa28dc8d7e..9ad38506e4 100644
@@ -64,5 +64,6 @@ MATERIALISE_EXT_PREDICATE(xtheadmemidx)
 MATERIALISE_EXT_PREDICATE(xtheadmempair)
 MATERIALISE_EXT_PREDICATE(xtheadsync)
 MATERIALISE_EXT_PREDICATE(XVentanaCondOps)
+MATERIALISE_EXT_PREDICATE(xneorv32xisa)
 
 #endif
target/riscv/cpu_cfg_fields.h.inc +1 -0
index a154ecdc79..b84e1bd287 100644
@@ -147,6 +147,7 @@ BOOL_FIELD(ext_xtheadmemidx)
 BOOL_FIELD(ext_xtheadmempair)
 BOOL_FIELD(ext_xtheadsync)
 BOOL_FIELD(ext_XVentanaCondOps)
+BOOL_FIELD(ext_xneorv32xisa)
 
 BOOL_FIELD(mmu)
 BOOL_FIELD(pmp)
target/riscv/cpu_vendorid.h +2 -0
index 96b6b9c2cb..66a8f30b81 100644
@@ -7,4 +7,6 @@
 #define VEYRON_V1_MIMPID        0x111
 #define VEYRON_V1_MVENDORID     0x61f
 
+#define NEORV32_VENDOR_ID       0xF0000001
+
 #endif /*  TARGET_RISCV_CPU_VENDORID_H */
target/riscv/meson.build +1 -0
index fdefe88ccd..44e706ad3f 100644
@@ -40,6 +40,7 @@ riscv_system_ss.add(files(
   'th_csr.c',
   'time_helper.c',
   'riscv-qmp-cmds.c',
+  'neorv32_csr.c',
 ))
 
 subdir('tcg')
target/riscv/neorv32_csr.c new file +40 -0
new file mode 100644
index 0000000000..3b0f0cab05
@@ -0,0 +1,40 @@
+/*
+ * NEORV32-specific CSR.
+ *
+ * Copyright (c) 2025 Michael Levit
+ * SPDX-License-Identifier: GPL-2.0-or-later
+ */
+
+#include "qemu/osdep.h"
+#include "cpu.h"
+#include "cpu_vendorid.h"
+
+#define    CSR_MXISA    (0xfc0)
+
+static RISCVException smode(CPURISCVState *env, int csrno)
+{
+    return RISCV_EXCP_NONE;
+}
+
+static RISCVException read_neorv32_xisa(CPURISCVState *env, int csrno,
+                                       target_ulong *val)
+{
+    /* We don't support any extension for now on QEMU */
+    *val = 0x00;
+    return RISCV_EXCP_NONE;
+}
+
+static bool test_neorv32_mvendorid(RISCVCPU *cpu)
+{
+    return cpu->cfg.mvendorid == NEORV32_VENDOR_ID;
+}
+
+const RISCVCSR neorv32_csr_list[] = {
+    {
+        .csrno = CSR_MXISA,
+        .insertion_test = test_neorv32_mvendorid,
+        .csr_ops = { "neorv32.xisa", smode, read_neorv32_xisa }
+    },
+    { }
+};
+
Signature · 1 line
2.51.1
#2 lore raw

PATCH v4 2/5 hw/misc: add NEORV32 SYSINFO block (CLK/MISC/SOC/CACHE)

In reply to Michael Levit

From: Michael <[email protected]>

Add a minimal SYSINFO MMIO device compatible with NEORV32 SDK expectations:
CLK (rw), MISC/SOC/CACHE (ro) composed from constants. Includes Kconfig/meson.

---
 hw/misc/Kconfig               |   2 +
 hw/misc/meson.build           |   1 +
 hw/misc/neorv32_sysinfo.c     | 201 ++++++++++++++++++++++++++++
 hw/misc/neorv32_sysinfo.h     |  88 +++++++++++++
 hw/misc/neorv32_sysinfo_rtl.h | 239 ++++++++++++++++++++++++++++++++++
 5 files changed, 531 insertions(+)
 create mode 100644 hw/misc/neorv32_sysinfo.c
 create mode 100644 hw/misc/neorv32_sysinfo.h
 create mode 100644 hw/misc/neorv32_sysinfo_rtl.h
hw/misc/Kconfig +2 -0
index fccd735c24..dba77acb63 100644
@@ -240,4 +240,6 @@ config IOSB
 config XLNX_VERSAL_TRNG
     bool
 
+config NEORV32_SYSINFO_QEMU
+    bool
 source macio/Kconfig
hw/misc/meson.build +1 -0
index b1d8d8e5d2..4ea46ec2d1 100644
@@ -34,6 +34,7 @@ system_ss.add(when: 'CONFIG_SIFIVE_E_PRCI', if_true: files('sifive_e_prci.c'))
 system_ss.add(when: 'CONFIG_SIFIVE_E_AON', if_true: files('sifive_e_aon.c'))
 system_ss.add(when: 'CONFIG_SIFIVE_U_OTP', if_true: files('sifive_u_otp.c'))
 system_ss.add(when: 'CONFIG_SIFIVE_U_PRCI', if_true: files('sifive_u_prci.c'))
+system_ss.add(when: 'CONFIG_NEORV32_SYSINFO_QEMU', if_true: files('neorv32_sysinfo.c'))
 
 subdir('macio')
 
hw/misc/neorv32_sysinfo.c new file +201 -0

201 lines not on this page · raw message

hw/misc/neorv32_sysinfo.h new file +88 -0

88 lines not on this page · raw message

hw/misc/neorv32_sysinfo_rtl.h new file +239 -0

239 lines not on this page · raw message

Signature · 1 line
2.51.1
#3 lore raw

PATCH v4 3/5 hw/char: add NEORV32 UART (CTRL/DATA, fifo, chardev)

In reply to Michael Levit

From: Michael <[email protected]>

Add NEORV32 UART device: CTRL/DATA registers, RX FIFO, TX to chardev. Includes Kconfig/meson and public header.

---
 hw/char/Kconfig                |   3 +
 hw/char/meson.build            |   1 +
 hw/char/neorv32_uart.c         | 285 +++++++++++++++++++++++++++++++++
 include/hw/char/neorv32_uart.h |  54 +++++++
 4 files changed, 343 insertions(+)
 create mode 100644 hw/char/neorv32_uart.c
 create mode 100644 include/hw/char/neorv32_uart.h
hw/char/Kconfig +3 -0
index 020c0a84bb..1fd39c2b30 100644
@@ -95,3 +95,6 @@ config IP_OCTAL_232
     bool
     default y
     depends on IPACK
+
+config NEORV32_UART
+    bool
hw/char/meson.build +1 -0
index a9e1dc26c0..2f5bf827a7 100644
@@ -31,6 +31,7 @@ system_ss.add(when: 'CONFIG_OMAP', if_true: files('omap_uart.c'))
 system_ss.add(when: 'CONFIG_RASPI', if_true: files('bcm2835_aux.c'))
 system_ss.add(when: 'CONFIG_RENESAS_SCI', if_true: files('renesas_sci.c'))
 system_ss.add(when: 'CONFIG_SIFIVE_UART', if_true: files('sifive_uart.c'))
+system_ss.add(when: 'CONFIG_NEORV32_UART', if_true: files('neorv32_uart.c'))
 system_ss.add(when: 'CONFIG_SH_SCI', if_true: files('sh_serial.c'))
 system_ss.add(when: 'CONFIG_STM32F2XX_USART', if_true: files('stm32f2xx_usart.c'))
 system_ss.add(when: 'CONFIG_STM32L4X5_USART', if_true: files('stm32l4x5_usart.c'))
hw/char/neorv32_uart.c new file +285 -0

285 lines not on this page · raw message

include/hw/char/neorv32_uart.h new file +54 -0

54 lines not on this page · raw message

Signature · 1 line
2.51.1
#4 lore raw

Re: [PATCH v4 3/5] hw/char: add NEORV32 UART (CTRL/DATA, fifo, chardev)

Quoted 55 lines · Michael Levit

From: Michael <[email protected]>

Add NEORV32 UART device: CTRL/DATA registers, RX FIFO, TX to chardev. Includes Kconfig/meson and public header.

---
  hw/char/Kconfig                |   3 +
  hw/char/meson.build            |   1 +
  hw/char/neorv32_uart.c         | 285 +++++++++++++++++++++++++++++++++
  include/hw/char/neorv32_uart.h |  54 +++++++
  4 files changed, 343 insertions(+)

create mode 100644 hw/char/neorv32_uart.c create mode 100644 include/hw/char/neorv32_uart.h

hw/char/Kconfig +3 -0
index 020c0a84bb..1fd39c2b30 100644
@@ -95,3 +95,6 @@ config IP_OCTAL_232
      bool
      default y
      depends on IPACK
+
+config NEORV32_UART
+    bool
hw/char/meson.build +1 -0
index a9e1dc26c0..2f5bf827a7 100644
@@ -31,6 +31,7 @@ system_ss.add(when: 'CONFIG_OMAP', if_true: files('omap_uart.c'))
  system_ss.add(when: 'CONFIG_RASPI', if_true: files('bcm2835_aux.c'))
  system_ss.add(when: 'CONFIG_RENESAS_SCI', if_true: files('renesas_sci.c'))
  system_ss.add(when: 'CONFIG_SIFIVE_UART', if_true: files('sifive_uart.c'))
+system_ss.add(when: 'CONFIG_NEORV32_UART', if_true: files('neorv32_uart.c'))
  system_ss.add(when: 'CONFIG_SH_SCI', if_true: files('sh_serial.c'))
  system_ss.add(when: 'CONFIG_STM32F2XX_USART', if_true: files('stm32f2xx_usart.c'))
  system_ss.add(when: 'CONFIG_STM32L4X5_USART', if_true: files('stm32l4x5_usart.c'))
hw/char/neorv32_uart.c new file +11 -0
new file mode 100644
index 0000000000..a21066d194
@@ -0,0 +1,285 @@
+/*
+ * Neorv32-specific UART.
+ *
+ * Copyright (c) 2025 Michael Levit
+ * SPDX-License-Identifier: GPL-2.0-or-later
+ */
+
+
+#include "qemu/osdep.h"
+#include "qapi/error.h"
+#include "qemu/log.h"

hw/char/neorv32_uart.c line 21

Quoted 10 lines
hw/char/neorv32_uart.c new file +10 -0
+#include "migration/vmstate.h"
+#include "chardev/char.h"
+#include "chardev/char-fe.h"
+#include "hw/irq.h"
+#include "hw/char/neorv32_uart.h"
+#include "hw/qdev-properties-system.h"
+
+#define NEORV32_UART_IO_REGION_SIZE  (32)
+
+static Property neorv32_uart_properties[] = {

This should be static const Property now.

Quoted 254 lines
hw/char/neorv32_uart.c new file +254 -0
+    DEFINE_PROP_CHR("chardev", Neorv32UARTState, chr),
+};
+
+enum {
+    NEORV32_UART_CTRL = 0,  /**< offset 0: control register */
+    NEORV32_UART_DATA = 4  /**< offset 4: data register  */
+};
+
+/** UART control register bits */
+enum NEORV32_UART_CTRL_enum {
+    UART_CTRL_EN            = 0,  /* enable */
+    UART_CTRL_SIM_MODE      = 1,  /* sim override */
+    UART_CTRL_HWFC_EN       = 2,  /* RTS/CTS */
+    UART_CTRL_PRSC_LSB      = 3,  /* prescaler sel LSB */
+    UART_CTRL_PRSC_MSB      = 5,  /* prescaler sel MSB */
+    UART_CTRL_BAUD_LSB      = 6,  /* baud div LSB */
+    UART_CTRL_BAUD_MSB      = 15, /* baud div MSB */
+    UART_CTRL_RX_NEMPTY     = 16, /* RX not empty */
+    UART_CTRL_RX_FULL       = 17, /* RX full */
+    UART_CTRL_TX_EMPTY      = 18, /* TX empty */
+    UART_CTRL_TX_NFULL      = 19, /* TX not full */
+    UART_CTRL_IRQ_RX_NEMPTY = 20, /* IRQ on RX not empty */
+    UART_CTRL_IRQ_RX_FULL   = 21, /* IRQ on RX full */
+    UART_CTRL_IRQ_TX_EMPTY  = 22, /* IRQ on TX empty */
+    UART_CTRL_IRQ_TX_NFULL  = 23, /* IRQ on TX not full */
+
+    UART_CTRL_RX_OVER       = 30, /* RX overflow */
+    UART_CTRL_TX_BUSY       = 31  /* TX busy or not empty */
+};
+
+/**  bits */
+enum NEORV32_UART_DATA_enum {
+    UART_DATA_RTX_LSB = 0, /**< (r/w): UART rx/tx data, LSB */
+    UART_DATA_RTX_MSB = 7, /**< (r/w): UART rx/tx data, MSB */
+
+    UART_DATA_RX_FIFO_SIZE_LSB = 8, /**< (r/-): log2(RX FIFO size), LSB */
+    UART_DATA_RX_FIFO_SIZE_MSB = 11, /**< (r/-): log2(RX FIFO size), MSB */
+
+    UART_DATA_TX_FIFO_SIZE_LSB = 12, /**< (r/-): log2(RX FIFO size), LSB */
+    UART_DATA_TX_FIFO_SIZE_MSB = 15, /**< (r/-): log2(RX FIFO size), MSB */
+};
+/**@}*/
+
+static void neorv32_uart_update_irq(Neorv32UARTState *s)
+{
+    int cond = 0;
+    if ((s->ie & NEORV32_UART_IE_TXWM) ||
+       ((s->ie & NEORV32_UART_IE_RXWM) && s->rx_fifo_len)) {
+        cond = 1;
+    }
+    if (cond) {
+        qemu_irq_raise(s->irq);
+    } else {
+        qemu_irq_lower(s->irq);
+    }
+}
+
+static uint64_t
+neorv32_uart_read(void *opaque, hwaddr addr, unsigned int size)
+{
+  Neorv32UARTState *s = opaque;
+  unsigned char r;
+
+    switch (addr) {
+    case NEORV32_UART_CTRL:
+        if (s->rx_fifo_len) {
+            s->CTRL |= (1 << UART_CTRL_RX_NEMPTY); /* set data available */
+        } else {
+            s->CTRL &= ~(1 << UART_CTRL_RX_NEMPTY); /* clear data available */
+        }
+        /* TODO: assuming here TX is always avalable, fix it. */
+        s->CTRL |= (1 << UART_CTRL_TX_NFULL); /* set TX not full */
+
+        return s->CTRL;
+
+    case NEORV32_UART_DATA:
+        if (s->rx_fifo_len) {
+            r = s->rx_fifo[0];
+            memmove(s->rx_fifo, s->rx_fifo + 1, s->rx_fifo_len - 1);
+            s->rx_fifo_len--;
+            qemu_chr_fe_accept_input(&s->chr);
+            s->DATA = r;
+
+            neorv32_uart_update_irq(s); /* TODO: check if need to call */
+            return r;
+        }
+    }
+
+    qemu_log_mask(LOG_GUEST_ERROR, "%s: bad read: addr = 0x%x\n",
+    __func__, (int)addr);
+    return 0;
+}
+
+
+
+static void
+neorv32_uart_write(void *opaque, hwaddr addr, uint64_t val64, unsigned int size)
+{
+    Neorv32UARTState *s = opaque;
+    uint32_t value = val64;
+    unsigned char ch = value;
+
+    /* TODO: check if need to update data and control bits */
+    switch (addr) {
+    case NEORV32_UART_CTRL:
+        s->CTRL = value;
+        /* TODO: check if need to call, depending on IRQ flags */
+        /* neorv32_uart_update_irq(s); */
+        return;
+    case NEORV32_UART_DATA:
+        s->DATA = value;
+        qemu_chr_fe_write(&s->chr, &ch, 1);
+        /* neorv32_uart_update_irq(s); TODO: check if need to call */
+    return;
+    }
+
+    qemu_log_mask(LOG_GUEST_ERROR, "%s: bad write: addr = 0x%x v = 0x%x\n",
+    __func__, (int)addr, (int)value);
+}
+
+static const MemoryRegionOps neorv32_uart_ops = {
+    .read = neorv32_uart_read,
+    .write = neorv32_uart_write,
+    .endianness = DEVICE_NATIVE_ENDIAN,
+    .valid = {
+    .min_access_size = 4,
+    .max_access_size = 4
+    }
+};
+
+static void neorv32_uart_init(Object *obj)
+{
+    SysBusDevice *sbd = SYS_BUS_DEVICE(obj);
+    Neorv32UARTState *s = NEORV32_UART(obj);
+
+    memory_region_init_io(&s->mmio, OBJECT(s), &neorv32_uart_ops, s,
+    TYPE_NEORV32_UART, NEORV32_UART_IO_REGION_SIZE);
+    sysbus_init_mmio(sbd, &s->mmio);
+    sysbus_init_irq(sbd, &s->irq);
+}
+
+
+static void neorv32_uart_rx(void *opaque, const uint8_t *buf, int size)
+{
+    Neorv32UARTState *s = opaque;
+
+    /* Got a byte.  */
+    if (s->rx_fifo_len >= sizeof(s->rx_fifo)) {
+        printf("WARNING: UART dropped char.\n");
+        return;
+    }
+    s->rx_fifo[s->rx_fifo_len++] = *buf;
+
+    neorv32_uart_update_irq(s);
+}
+
+static int neorv32_uart_can_rx(void *opaque)
+{
+    Neorv32UARTState *s = opaque;
+
+    return s->rx_fifo_len < sizeof(s->rx_fifo);
+}
+
+static void neorv32_uart_event(void *opaque, QEMUChrEvent event)
+{
+}
+
+static int  neorv32_uart_be_change(void *opaque)
+{
+    Neorv32UARTState *s = opaque;
+
+    qemu_chr_fe_set_handlers(&s->chr, neorv32_uart_can_rx, neorv32_uart_rx,
+    neorv32_uart_event, neorv32_uart_be_change, s,
+    NULL, true);
+
+    return 0;
+}
+
+static void neorv32_uart_realize(DeviceState *dev, Error **errp)
+{
+    Neorv32UARTState *s = NEORV32_UART(dev);
+
+    qemu_chr_fe_set_handlers(&s->chr, neorv32_uart_can_rx, neorv32_uart_rx,
+    neorv32_uart_event, neorv32_uart_be_change, s,
+    NULL, true);
+}
+
+static const VMStateDescription vmstate_neorv32_uart = {
+    .name = TYPE_NEORV32_UART,
+    .version_id = 1,
+    .minimum_version_id = 1,
+    .fields = (VMStateField[]) {
+    VMSTATE_UINT8_ARRAY(rx_fifo, Neorv32UARTState,
+    NEORV32_UART_RX_FIFO_SIZE),
+    VMSTATE_UINT8(rx_fifo_len, Neorv32UARTState),
+    VMSTATE_UINT32(ie, Neorv32UARTState),
+    VMSTATE_END_OF_LIST()
+    },
+};
+
+static void neorv32_uart_reset_enter(Object *obj, ResetType type)
+{
+    Neorv32UARTState *s = NEORV32_UART(obj);
+    s->rx_fifo_len = 0;
+    s->ie = 0;
+}
+
+static void neorv32_uart_reset_hold(Object *obj, ResetType type)
+{
+    Neorv32UARTState *s = NEORV32_UART(obj);
+    qemu_irq_lower(s->irq);
+}
+
+static void neorv32_uart_class_init(ObjectClass *oc, const void *data)
+{
+    DeviceClass *dc = DEVICE_CLASS(oc);
+    ResettableClass *rc = RESETTABLE_CLASS(oc);
+
+    dc->realize = neorv32_uart_realize;
+    dc->vmsd = &vmstate_neorv32_uart;
+    rc->phases.enter = neorv32_uart_reset_enter;
+    rc->phases.hold = neorv32_uart_reset_hold;
+    device_class_set_props(dc, neorv32_uart_properties);
+    set_bit(DEVICE_CATEGORY_INPUT, dc->categories);
+}
+
+static const TypeInfo neorv32_uart_info = {
+    .name = TYPE_NEORV32_UART,
+    .parent = TYPE_SYS_BUS_DEVICE,
+    .instance_size = sizeof(Neorv32UARTState),
+    .instance_init = neorv32_uart_init,
+    .class_init = neorv32_uart_class_init,
+};
+
+static void neorv32_uart_register_types(void)
+{
+    type_register_static(&neorv32_uart_info);
+}
+
+type_init(neorv32_uart_register_types)
+/*
+ * Create UART device.
+ */
+Neorv32UARTState *neorv32_uart_create(MemoryRegion *address_space, hwaddr base,
+  Chardev *chr)
+{
+    DeviceState *dev;
+    SysBusDevice *s;
+    bool succed = false;
+
+    dev = qdev_new("riscv.neorv32.uart");
+
+    qdev_prop_set_chr(dev, "chardev", chr);
+    s = SYS_BUS_DEVICE(dev);

hw/char/neorv32_uart.c line 285

Quoted 10 lines
hw/char/neorv32_uart.c new file +10 -0
+    succed = sysbus_realize_and_unref(s, &error_fatal);
+
+    if (succed) {
+        memory_region_add_subregion(address_space, base,
+        sysbus_mmio_get_region(s, 0));
+        return NEORV32_UART(dev);
+    } else {
+        return NULL;
+    }
+} /* neorv32_uart_create */

New devices should never have their own global init functions such as neorv32_uart_create(): this needs to be done by the board instead.

Quoted 41 lines
include/hw/char/neorv32_uart.h new file +35 -0
new file mode 100644
index 0000000000..fa33906724
@@ -0,0 +1,54 @@
+/*
+ * Neorv32-specific UART.
+ *
+ * Copyright (c) 2025 Michael Levit
+ * SPDX-License-Identifier: GPL-2.0-or-later
+ */
+
+#ifndef HW_NEORV32_UART_H
+#define HW_NEORV32_UART_H
+
+#include "chardev/char-fe.h"
+#include "hw/qdev-properties.h"
+#include "hw/sysbus.h"
+#include "qom/object.h"
+
+#define TYPE_NEORV32_UART "riscv.neorv32.uart"
+OBJECT_DECLARE_SIMPLE_TYPE(Neorv32UARTState, NEORV32_UART)
+
+#define QEMU_UART_DATA_RX_FIFO_SIZE_LSB  8  /* log2 RX FIFO size LSB */
+#define QEMU_UART_DATA_RX_FIFO_SIZE_MSB  11  /* log2 RX FIFO size MSB */
+
+#define NEORV32_UART_RX_FIFO_SIZE  32 /* in HW it is 2048 + 256 = _MSB + _LSB */
+
+enum {
+    NEORV32_UART_IE_TXWM = 1, /* Transmit watermark interrupt enable */
+    NEORV32_UART_IE_RXWM = 2  /* Receive watermark interrupt enable */
+};
+
+enum {
+    NEORV32_UART_IP_TXWM = 1, /* Transmit watermark interrupt pending */
+    NEORV32_UART_IP_RXWM = 2  /* Receive watermark interrupt pending */
+};
+
+
+

include/hw/char/neorv32_uart.h line 45

Quoted 10 lines
include/hw/char/neorv32_uart.h new file +10 -0
+struct Neorv32UARTState {
+    /*< private >*/
+    SysBusDevice parent_obj;
+
+    /*< public >*/
+    qemu_irq irq;
+    MemoryRegion mmio;
+    CharFrontend chr;
+    uint8_t rx_fifo[NEORV32_UART_RX_FIFO_SIZE];
+    uint8_t rx_fifo_len;

Rather than re-invent your own FIFO, can you make use of the existing Fifo8? It has a much richer API, and as long as you don't use the deprecated _bufptr() APIs, it will aggressively defend against underflow/overflow.

include/hw/char/neorv32_uart.h line 52

Quoted 7 lines
include/hw/char/neorv32_uart.h new file +7 -0
+    uint32_t ie; /* interrupt enable */
+    uint32_t CTRL;
+    uint32_t DATA;
+};
+
+Neorv32UARTState *neorv32_uart_create(MemoryRegion *address_space, hwaddr base,
+    Chardev *chr);

And again, new devices should not have their own global init functions.

include/hw/char/neorv32_uart.h line 54

Quoted 1 line
include/hw/char/neorv32_uart.h new file +1 -0
+#endif /* HW_NEORV32_UART_H */

ATB,

Mark.

#5 lore raw

Re: [PATCH v4 3/5] hw/char: add NEORV32 UART (CTRL/DATA, fifo, chardev)

Quoted 4 lines · Mark Cave-Ayland
Quoted 4 lines
include/hw/char/neorv32_uart.h new file +4 -0
+struct Neorv32UARTState {
+    /*< private >*/
+    SysBusDevice parent_obj;
+
Quoted 8 lines
Quoted 6 lines

+    /*< public >*/
+    qemu_irq irq;
+    MemoryRegion mmio;
+    CharFrontend chr;
+    uint8_t rx_fifo[NEORV32_UART_RX_FIFO_SIZE];
+    uint8_t rx_fifo_len;

Rather than re-invent your own FIFO, can you make use of the existing Fifo8? It has a much richer API, and as long as you don't use the deprecated _bufptr() APIs, it will aggressively defend against underflow/overflow.

This looks like it is possible even with the configurable UART size, since that is exactly how the FIFO is implemented for the SPI device introduced in the following patch.

ATB,

Mark.

#6 lore raw

PATCH v4 4/5 hw/ssi: add NEORV32 SPI controller (SSI master, CS command)

In reply to Michael Levit

From: Michael <[email protected]>

Add NEORV32 SPI controller: CTRL/DATA, tiny TX/RX FIFOs, command-mode CS (active-low), SSI master bus, and helper to attach n25q flash when an MTD drive is provided. Includes Kconfig/meson and public header.

---
 hw/ssi/Kconfig               |   4 +
 hw/ssi/meson.build           |   1 +
 hw/ssi/neorv32_spi.c         | 478 +++++++++++++++++++++++++++++++++++
 include/hw/ssi/neorv32_spi.h |  57 +++++
 4 files changed, 540 insertions(+)
 create mode 100644 hw/ssi/neorv32_spi.c
 create mode 100644 include/hw/ssi/neorv32_spi.h
hw/ssi/Kconfig +4 -0
index 1bd56463c1..5b1a03f3c4 100644
@@ -32,3 +32,7 @@ config PNV_SPI
 config ALLWINNER_A10_SPI
     bool
     select SSI
+
+config NEORV32_SPI
+    bool
+    select SSI
hw/ssi/meson.build +1 -0
index 6afb1ea200..5139cc1ca0 100644
@@ -13,3 +13,4 @@ system_ss.add(when: 'CONFIG_IMX', if_true: files('imx_spi.c'))
 system_ss.add(when: 'CONFIG_IBEX', if_true: files('ibex_spi_host.c'))
 system_ss.add(when: 'CONFIG_BCM2835_SPI', if_true: files('bcm2835_spi.c'))
 system_ss.add(when: 'CONFIG_PNV_SPI', if_true: files('pnv_spi.c'))
+system_ss.add(when: 'CONFIG_NEORV32_SPI', if_true: files('neorv32_spi.c'))
hw/ssi/neorv32_spi.c new file +478 -0

478 lines not on this page · raw message

include/hw/ssi/neorv32_spi.h new file +57 -0

57 lines not on this page · raw message

Signature · 1 line
2.51.1
#7 lore raw

Re: [PATCH v4 4/5] hw/ssi: add NEORV32 SPI controller (SSI master, CS command)

Quoted 449 lines · Michael Levit

From: Michael <[email protected]>

Add NEORV32 SPI controller: CTRL/DATA, tiny TX/RX FIFOs, command-mode CS (active-low), SSI master bus, and helper to attach n25q flash when an MTD drive is provided. Includes Kconfig/meson and public header.

---
  hw/ssi/Kconfig               |   4 +
  hw/ssi/meson.build           |   1 +
  hw/ssi/neorv32_spi.c         | 478 +++++++++++++++++++++++++++++++++++
  include/hw/ssi/neorv32_spi.h |  57 +++++
  4 files changed, 540 insertions(+)

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

hw/ssi/Kconfig +4 -0
index 1bd56463c1..5b1a03f3c4 100644
@@ -32,3 +32,7 @@ config PNV_SPI
  config ALLWINNER_A10_SPI
      bool
      select SSI
+
+config NEORV32_SPI
+    bool
+    select SSI
hw/ssi/meson.build +1 -0
index 6afb1ea200..5139cc1ca0 100644
@@ -13,3 +13,4 @@ system_ss.add(when: 'CONFIG_IMX', if_true: files('imx_spi.c'))
  system_ss.add(when: 'CONFIG_IBEX', if_true: files('ibex_spi_host.c'))
  system_ss.add(when: 'CONFIG_BCM2835_SPI', if_true: files('bcm2835_spi.c'))
  system_ss.add(when: 'CONFIG_PNV_SPI', if_true: files('pnv_spi.c'))
+system_ss.add(when: 'CONFIG_NEORV32_SPI', if_true: files('neorv32_spi.c'))
hw/ssi/neorv32_spi.c new file +406 -0
new file mode 100644
index 0000000000..954788a5de
@@ -0,0 +1,478 @@
+/*
+ * QEMU implementation of the Neorv32 SPI block.
+ *
+ * Copyright (c) 2025 Michael Levit
+ * SPDX-License-Identifier: GPL-2.0-or-later
+ */
+
+#include "qemu/osdep.h"
+#include "hw/irq.h"
+#include "hw/qdev-properties.h"
+#include "hw/sysbus.h"
+#include "hw/ssi/ssi.h"
+#include "qemu/fifo8.h"
+#include "qemu/log.h"
+#include "qemu/module.h"
+#include "trace/trace-root.h"
+#include "qapi/error.h"
+#include "hw/irq.h"
+#include "hw/ssi/neorv32_spi.h"
+#include "system/blockdev.h"
+
+/* SPI control register bits */
+enum NEORV32_SPI_CTRL_enum {
+  SPI_CTRL_EN           =  0, /* (r/w): SPI unit enable */
+  SPI_CTRL_CPHA         =  1, /* (r/w): Clock phase */
+  SPI_CTRL_CPOL         =  2, /* (r/w): Clock polarity */
+  SPI_CTRL_PRSC0        =  3, /* (r/w): Clock prescaler select bit 0 */
+  SPI_CTRL_PRSC1        =  4, /* (r/w): Clock prescaler select bit 1 */
+  SPI_CTRL_PRSC2        =  5, /* (r/w): Clock prescaler select bit 2 */
+  SPI_CTRL_CDIV0        =  6, /* (r/w): Clock divider bit 0 */
+  SPI_CTRL_CDIV1        =  7, /* (r/w): Clock divider bit 1 */
+  SPI_CTRL_CDIV2        =  8, /* (r/w): Clock divider bit 2 */
+  SPI_CTRL_CDIV3        =  9, /* (r/w): Clock divider bit 3 */
+
+  SPI_CTRL_RX_AVAIL     = 16, /* (r/-): RX FIFO data available (not empty) */
+  SPI_CTRL_TX_EMPTY     = 17, /* (r/-): TX FIFO empty */
+  SPI_CTRL_TX_FULL      = 18, /* (r/-): TX FIFO full */
+
+  SPI_CTRL_FIFO_LSB     = 24, /* (r/-): log2(FIFO size), LSB */
+  SPI_CTRL_FIFO_MSB     = 27, /* (r/-): log2(FIFO size), MSB */
+
+  SPI_CS_ACTIVE         = 30, /* (r/-): At least one CS line is active */
+  SPI_CTRL_BUSY         = 31  /* (r/-): serial PHY busy or TX FIFO not empty */
+};
+
+/* SPI data register bits */
+enum NEORV32_SPI_DATA_enum {
+  SPI_DATA_LSB  =  0, /* (r/w): Data byte LSB */
+  SPI_DATA_CSEN =  3, /* (-/w): Chip select enable (command-mode) */
+  SPI_DATA_MSB  =  7, /* (r/w): Data byte MSB */
+  SPI_DATA_CMD  = 31  /* (-/w): 1=command, 0=data */
+};
+
+/* Register offsets */
+#define NEORV32_SPI_CTRL          0x00
+#define NEORV32_SPI_DATA          0x04
+#define NEORV32_SPI_MMIO_SIZE     0x8  /* ctrl + data (8 bytes total) */
+/* Various constants */
+#define NEORV32_SPI_MAX_CS_LINES  7
+#define NEORV32_SPI_FIFO_CAPACITY 8
+
+/* Utility functions to get/set bits in ctrl register */
+static inline bool get_ctrl_bit(NEORV32SPIState *s, int bit)
+{
+    return (s->ctrl & (1 << bit)) != 0;
+}
+
+static inline void set_ctrl_bit(NEORV32SPIState *s, int bit, bool val)
+{
+    if (val) {
+        s->ctrl |= (1 << bit);
+    } else {
+        s->ctrl &= ~(1 << bit);
+    }
+}
+
+static inline bool get_data_bit(uint32_t v, int bit)
+{
+    return (v >> bit) & 1;
+}
+
+/* Update read-only status bits in CTRL register */
+static void neorv32_spi_update_status(NEORV32SPIState *s)
+{
+    /* RX_AVAIL: set if RX FIFO not empty */
+    set_ctrl_bit(s, SPI_CTRL_RX_AVAIL, !fifo8_is_empty(&s->rx_fifo));
+
+    /* TX_EMPTY: set if TX FIFO empty */
+    set_ctrl_bit(s, SPI_CTRL_TX_EMPTY, fifo8_is_empty(&s->tx_fifo));
+
+    /* TX_FULL: set if TX FIFO full */
+    set_ctrl_bit(s, SPI_CTRL_TX_FULL, fifo8_is_full(&s->tx_fifo));
+
+
+    /*
+     * BUSY: We'll consider SPI busy if TX FIFO is not empty
+     * or currently shifting data.
+     * For simplicity, if TX is not empty we say busy.
+     */
+    bool busy = !fifo8_is_empty(&s->tx_fifo);
+    set_ctrl_bit(s, SPI_CTRL_BUSY, busy);
+
+    /* Update CS status */
+    if (s->cmd_cs_active) {
+        s->ctrl |= (1u << SPI_CS_ACTIVE);
+    } else {
+        s->ctrl &= ~(1u << SPI_CS_ACTIVE);
+    }
+
+}
+
+/* Update chip select lines based on command-mode CS (active-low on the wire) */
+static void neorv32_spi_update_cs(NEORV32SPIState *s)
+{
+    /* Check that input valid */
+    if (!s->cs_lines || s->num_cs <= 0) {
+        return;
+    }
+
+    /* Deassert all CS lines (inactive = high) */
+    for (int i = 0; i < s->num_cs; i++) {
+        qemu_set_irq(s->cs_lines[i], 1);
+    }
+
+    /* If DATA command says CS active, assert selected line (low = active) */
+    if (s->cmd_cs_active) {
+        int cs_idx = s->current_cs;
+        if (cs_idx < 0 || cs_idx >= s->num_cs) {
+            /* Out of range: keep all deasserted, but warn once per event */
+            qemu_log_mask(LOG_GUEST_ERROR,
+                          "%s: CS index %d out of range (num_cs=%d)\n",
+                          __func__, cs_idx, s->num_cs);
+            return;
+        }
+        /* Active-low when enabled */
+        qemu_set_irq(s->cs_lines[cs_idx], 0);
+    }
+
+}
+
+/* Update IRQ based on conditions */
+static void neorv32_spi_update_irq(NEORV32SPIState *s)
+{
+    /*
+     * Conditions for IRQ:
+     * IRQ if RX data available and IRQ_RX_AVAIL is set:
+     *    if (!RX FIFO empty && SPI_CTRL_IRQ_RX_AVAIL set)
+     *
+     * IRQ if TX empty and IRQ_TX_EMPTY is set:
+     *    if (TX empty && SPI_CTRL_IRQ_TX_EMPTY set)
+     *
+     * IRQ if TX < half full and IRQ_TX_HALF is set:
+     *    if (TX < half full && SPI_CTRL_IRQ_TX_HALF set)
+     */
+
+    bool rx_irq = !fifo8_is_empty(&s->rx_fifo);
+    bool tx_empty_irq = fifo8_is_empty(&s->tx_fifo);
+    int  used = fifo8_num_used(&s->tx_fifo);
+    bool tx_half_irq = (used < (s->fifo_capacity / 2));
+
+    bool irq_level = rx_irq || tx_empty_irq || tx_half_irq;
+    qemu_set_irq(s->irq, irq_level ? 1 : 0);
+}
+
+/*
+ * Flush the TX FIFO to the SPI bus:
+ * For each byte in TX FIFO, send it out via ssi_transfer.
+ * If direction is not explicitly given, we assume:
+ *   - On write to DATA, we push to TX FIFO and then transfer out.
+ *   - On receiving data back from ssi_transfer, we push it into RX FIFO
+ *     if SPI is enabled.
+ */
+static void neorv32_spi_flush_txfifo(NEORV32SPIState *s)
+{
+    if (!get_ctrl_bit(s, SPI_CTRL_EN)) {
+        /* SPI not enabled, do nothing */
+        return;
+    }
+
+    while (!fifo8_is_empty(&s->tx_fifo)) {
+        uint8_t tx = fifo8_pop(&s->tx_fifo);
+        uint8_t rx = ssi_transfer(s->bus, tx);
+
+        /* Push received byte into RX FIFO if not full */
+        if (!fifo8_is_full(&s->rx_fifo)) {
+            fifo8_push(&s->rx_fifo, rx);
+        }
+    }
+}
+
+/* Reset the device state */
+static void neorv32_spi_reset(DeviceState *d)
+{
+    NEORV32SPIState *s = NEORV32_SPI(d);
+
+    s->ctrl = 0;
+    s->data = 0;
+
+    /* Reset FIFOs */
+    fifo8_reset(&s->tx_fifo);
+    fifo8_reset(&s->rx_fifo);
+
+    neorv32_spi_update_status(s);
+    neorv32_spi_update_cs(s);
+    neorv32_spi_update_irq(s);
+}
+
+/* MMIO read handler */
+static uint64_t neorv32_spi_read(void *opaque, hwaddr addr, unsigned int size)
+{
+    NEORV32SPIState *s = opaque;
+    uint32_t r = 0;
+
+    switch (addr) {
+    case NEORV32_SPI_CTRL:
+        /* Return the current CTRL register value (including status bits) */
+        neorv32_spi_update_status(s);
+        r = s->ctrl;
+        break;
+
+    case NEORV32_SPI_DATA:
+        /* If RX FIFO is empty, return some default, else pop from RX FIFO */
+        if (fifo8_is_empty(&s->rx_fifo)) {
+            /*
+             * No data available,
+             * could return 0xFFFFFFFF or 0x00000000 as "no data"
+             */
+            r = 0x00000000;
+        } else {
+            r = fifo8_pop(&s->rx_fifo);
+        }
+        break;
+
+    default:
+        qemu_log_mask(LOG_GUEST_ERROR, "%s: bad read at address 0x%"
+                       HWADDR_PRIx "\n", __func__, addr);
+        break;
+    }
+
+    neorv32_spi_update_status(s);
+    neorv32_spi_update_irq(s);
+
+    return r;
+}
+
+/* MMIO write handler */
+static void neorv32_spi_write(void *opaque, hwaddr addr,
+                              uint64_t val64, unsigned int size)
+{
+    NEORV32SPIState *s = opaque;
+    uint32_t value = val64;
+
+    switch (addr) {
+    case NEORV32_SPI_CTRL: {
+
+        /*
+         * Writing control register:
+         * Some bits are read-only (e.g., status bits).
+         * We should mask them out or ignore writes to them.
+         * For simplicity, we overwrite ctrl except for RO bits.
+         */
+
+
+        /*
+         * Save old RO bits: RX_AVAIL, TX_EMPTY, TX_NHALF, TX_FULL, BUSY
+         * and FIFO size bits
+         */
+        uint32_t ro_mask = ((1 << SPI_CTRL_BUSY)      |
+                            (1 << SPI_CTRL_TX_EMPTY)  |
+                            (1 << SPI_CTRL_TX_FULL)   |
+                            (1 << SPI_CTRL_RX_AVAIL));
+
+        /*
+         * FIFO size bits might be hardwired read-only.
+         * Assume we do not change them:
+         * FIFO size: bits [SPI_CTRL_FIFO_LSB..SPI_CTRL_FIFO_MSB],
+         * here assume read-only.
+         */
+        uint32_t fifo_size_mask = 0;
+        for (int b = SPI_CTRL_FIFO_LSB; b <= SPI_CTRL_FIFO_MSB; b++) {
+            fifo_size_mask |= (1 << b);
+        }
+        ro_mask |= fifo_size_mask;
+
+        uint32_t ro_bits = s->ctrl & ro_mask;
+        s->ctrl = (value & ~ro_mask) | ro_bits;
+
+        neorv32_spi_update_cs(s);
+        break;
+    } /* NEORV32_SPI_CTRL */
+
+    case NEORV32_SPI_DATA:
+    {
+        /* If CMD=1, this write is a command, not payload */
+        const bool is_cmd = get_data_bit(value, SPI_DATA_CMD);
+
+        if (is_cmd) {
+            /*
+             * DATA command format:
+             *   bit 31: CMD = 1
+             *   bit  3: CSEN (1=assert CS, 0=deassert All)
+             *   bits [2:0]: CS index (0..7) when asserting
+             */
+            const bool csen = get_data_bit(value, SPI_DATA_CSEN);
+            const int  cs_index = (int)(value & 0x7);
+
+            if (csen) {
+                /* Select and assert a single CS */
+                s->current_cs    = cs_index;  /* range checking  update_cs() */
+                s->cmd_cs_active = true;
+            } else {
+                /* Deassert all CS lines */
+                s->cmd_cs_active = false;
+            }
+
+            /* Drive the wires */
+            neorv32_spi_update_cs(s);
+            /* Update status (SPI_CS_ACTIVE is read-only status bit) */
+            neorv32_spi_update_status(s);
+            neorv32_spi_update_irq(s);
+            break; /* no FIFO push on command */
+        }
+
+        /* Writing DATA puts a byte into TX FIFO if not full */
+        if (!fifo8_is_full(&s->tx_fifo)) {
+            uint8_t tx_byte = (uint8_t)value;
+
+            fifo8_push(&s->tx_fifo, tx_byte);
+            /* After pushing data, flush TX to SPI bus */
+            neorv32_spi_flush_txfifo(s);
+        } else {
+            qemu_log_mask(LOG_GUEST_ERROR,
+                          "%s: TX FIFO full, cannot write 0x%x\n",
+                          __func__, value);
+        }
+        break;
+    } /* NEORV32_SPI_DATA */
+
+    default:
+        qemu_log_mask(LOG_GUEST_ERROR, "%s: bad write at address 0x%"
+                      HWADDR_PRIx " value=0x%x\n", __func__, addr, value);
+        break;
+
+    } /* switch (addr) */
+
+    neorv32_spi_update_status(s);
+    neorv32_spi_update_irq(s);
+} /* neorv32_spi_write */
+
+static const MemoryRegionOps neorv32_spi_ops = {
+    .read = neorv32_spi_read,
+    .write = neorv32_spi_write,
+    .endianness = DEVICE_LITTLE_ENDIAN,
+    .valid = {
+        .min_access_size = 4,
+        .max_access_size = 4,
+    },
+};
+
+static void neorv32_spi_init(Object *obj)
+{
+    NEORV32SPIState *s = NEORV32_SPI(obj);
+    s->ctrl          = 0;
+    s->data          = 0;
+    s->fifo_capacity = NEORV32_SPI_FIFO_CAPACITY;
+    s->num_cs        = NEORV32_SPI_MAX_CS_LINES; /* Default to 1 CS line */
+    s->cmd_cs_active = false;
+    s->current_cs    = 0; /* Use CS0 by default */
+}
+
+/* Realize the device */
+static void neorv32_spi_realize(DeviceState *dev, Error **errp)
+{
+    NEORV32SPIState *s = NEORV32_SPI(dev);
+    SysBusDevice *sbd = SYS_BUS_DEVICE(dev);
+
+    /* Create the SSI master bus */
+    s->bus = ssi_create_bus(dev, "neorv32-spi-bus");
+
+    /* 1) IRQ inputs: first the main IRQ, then each CS line */
+    sysbus_init_irq(sbd, &s->irq);
+    s->cs_lines = g_new0(qemu_irq, s->num_cs);
+    for (int i = 0; i < s->num_cs; i++) {
+        sysbus_init_irq(sbd, &s->cs_lines[i]);
+        qemu_set_irq(s->cs_lines[i], 1);  /* deassert CS (high) */
+    }
+
+    /* 2) Now map the MMIO region */
+    memory_region_init_io(&s->mmio, OBJECT(s), &neorv32_spi_ops, s,
+                          TYPE_NEORV32_SPI, NEORV32_SPI_MMIO_SIZE);
+    sysbus_init_mmio(sbd, &s->mmio);
+
+
+    /* Initialize FIFOs */
+    fifo8_create(&s->tx_fifo, s->fifo_capacity);
+    fifo8_create(&s->rx_fifo, s->fifo_capacity);
+
+    /*
+     * Set FIFO size bits (log2 of FIFO size = 3 for capacity=8)
+     *
+     * FIFO size bits: from SPI_CTRL_FIFO_LSB to SPI_CTRL_FIFO_MSB
+     * We'll store a value of 3 (log2(8)=3)
+     */
+    int fifo_size_log2 = 3;
+    for (int b = SPI_CTRL_FIFO_LSB; b <= SPI_CTRL_FIFO_MSB; b++) {
+        int shift = b - SPI_CTRL_FIFO_LSB;

hw/ssi/neorv32_spi.c line 416

Quoted 10 lines
hw/ssi/neorv32_spi.c new file +10 -0
+        if (fifo_size_log2 & (1 << shift)) {
+            s->ctrl |= (1 << b);
+        } else {
+            s->ctrl &= ~(1 << b);
+        }
+    }
+}
+
+/* Device properties can be added if needed. For now, none. */
+static Property neorv32_spi_properties[] = {

Similar to as before: this should be static const Property.

Quoted 51 lines
hw/ssi/neorv32_spi.c new file +51 -0
+    DEFINE_PROP_UINT32("num-cs", NEORV32SPIState, num_cs, 1),
+};
+
+static void neorv32_spi_class_init(ObjectClass *klass, const void *data)
+{
+    DeviceClass *dc = DEVICE_CLASS(klass);
+
+    device_class_set_props(dc, neorv32_spi_properties);
+    device_class_set_legacy_reset(dc, neorv32_spi_reset);
+    dc->realize = neorv32_spi_realize;
+}
+
+static const TypeInfo neorv32_spi_type_info = {
+    .name           = TYPE_NEORV32_SPI,
+    .parent         = TYPE_SYS_BUS_DEVICE,
+    .instance_size  = sizeof(NEORV32SPIState),
+    .instance_init  = neorv32_spi_init,
+    .class_init     = neorv32_spi_class_init,
+};
+
+static void neorv32_spi_register_types(void)
+{
+    type_register_static(&neorv32_spi_type_info);
+}
+
+type_init(neorv32_spi_register_types)
+
+
+
+NEORV32SPIState *neorv32_spi_create(MemoryRegion *sys_mem, hwaddr base_addr)
+{
+    /* Allocate and initialize the SPI state object */
+    NEORV32SPIState *s = g_new0(NEORV32SPIState, 1);
+    object_initialize(&s->parent_obj, sizeof(*s), TYPE_NEORV32_SPI);
+    SysBusDevice *sbd = SYS_BUS_DEVICE(&s->parent_obj);
+
+    /* Realize the SPI controller (sets up mmio, irq, SSI bus, cs_lines) */
+    sysbus_realize_and_unref(sbd, &error_fatal);
+
+    /* Map the MMIO region into the system address space */
+    sysbus_mmio_map(sbd, 0, base_addr);
+
+    /* Attach an SPI flash to SPI0 if a drive image is provided */
+    DriveInfo *dinfo = drive_get(IF_MTD, 0, 0);
+    if (dinfo) {
+        /* Create the flash device and bind the MTD backend */
+        DeviceState *flash = qdev_new("n25q512a11");
+        qdev_prop_set_drive_err(flash, "drive",
+                                blk_by_legacy_dinfo(dinfo),
+                                &error_fatal);
+

hw/ssi/neorv32_spi.c line 477

Quoted 10 lines
hw/ssi/neorv32_spi.c new file +10 -0
+        /* Realize flash on the same SSI bus created on  controller realize */
+        qdev_realize_and_unref(flash, BUS(s->bus), &error_fatal);
+
+        /* Retrieve and wire the flash's CS input line to CS0 output */
+        qemu_irq flash_cs = qdev_get_gpio_in_named(flash, SSI_GPIO_CS, 0);
+        sysbus_connect_irq(sbd, 1, flash_cs);
+    }
+
+    return s;
+}

And again this separate neorv32_spi_create() function should not be included as part of the device. The initialisation and wiring needs to be done as part of the board code.

Quoted 51 lines
include/hw/ssi/neorv32_spi.h new file +45 -0
new file mode 100644
index 0000000000..8b94d2f1cf
@@ -0,0 +1,57 @@
+/*
+ * QEMU implementation of the Neorv32 SPI block.
+ *
+ * Copyright (c) 2025 Michael Levit
+ * SPDX-License-Identifier: GPL-2.0-or-later
+ */
+
+#ifndef NEORV32_SPI_H
+#define NEORV32_SPI_H
+
+#include "qemu/osdep.h"
+#include "hw/sysbus.h"
+
+#define TYPE_NEORV32_SPI "neorv32.spi"
+#define NEORV32_SPI(obj) OBJECT_CHECK(NEORV32SPIState, (obj), TYPE_NEORV32_SPI)
+
+typedef struct  NEORV32SPIState {
+    SysBusDevice parent_obj;
+
+    /* Memory-mapped registers */
+    MemoryRegion mmio;
+
+    /* IRQ line */
+    qemu_irq irq;
+
+    /* SPI bus (master) */
+    SSIBus *bus;
+
+    /* Chip selects (assume up to 3 CS lines) */
+    qemu_irq *cs_lines;
+    uint32_t num_cs;
+
+    /*
+     * Registers:
+     * Assume:
+     * 0x00: CTRL (r/w)
+     * 0x04: DATA (r/w)
+     */
+    uint32_t ctrl;
+    uint32_t data;
+
+    /* FIFOs */
+    Fifo8 tx_fifo;
+    Fifo8 rx_fifo;
+

include/hw/ssi/neorv32_spi.h line 55

Quoted 10 lines
include/hw/ssi/neorv32_spi.h new file +10 -0
+    /* FIFO capacity */
+    int fifo_capacity;
+    /* Track CS state driven by command writes */
+    bool cmd_cs_active;  /* true = CS asserted (active-low on wire) */
+    int  current_cs;     /* which CS line is active; default 0 for now */
+} NEORV32SPIState;
+
+
+
+NEORV32SPIState *neorv32_spi_create(MemoryRegion *sys_mem, hwaddr base_addr);

... which means this should also go.

include/hw/ssi/neorv32_spi.h line 57

Quoted 1 line
include/hw/ssi/neorv32_spi.h new file +1 -0
+#endif /* NEORV32_SPI_H */

ATB,

Mark.

#8 lore raw

PATCH v4 5/5 hw/riscv: introduce 'neorv32' board, docs, and riscv32 device config

In reply to Michael Levit

From: Michael <[email protected]>

Introduce the 'neorv32' board wiring IMEM/DMEM/BOOTROM, SYSINFO, UART0, and SPI0; add docs/system/riscv/neorv32.rst and riscv32-softmmu device config entry.

---
 configs/devices/riscv32-softmmu/default.mak |   1 +
 docs/system/riscv/neorv32.rst               | 112 ++++++++++
 docs/system/target-riscv.rst                |   1 +
 hw/riscv/Kconfig                            |   8 +
 hw/riscv/meson.build                        |   1 +
 hw/riscv/neorv32.c                          | 215 ++++++++++++++++++++
 include/hw/riscv/neorv32.h                  |  54 +++++
 7 files changed, 392 insertions(+)
 create mode 100644 docs/system/riscv/neorv32.rst
 create mode 100644 hw/riscv/neorv32.c
 create mode 100644 include/hw/riscv/neorv32.h
configs/devices/riscv32-softmmu/default.mak +1 -0
index c2cd86ce05..4fdc94ab48 100644
@@ -10,3 +10,4 @@
 # CONFIG_SIFIVE_U=n
 # CONFIG_RISCV_VIRT=n
 # CONFIG_OPENTITAN=n
+# CONFIG_NEORV32=n
docs/system/riscv/neorv32.rst new file +112 -0

112 lines not on this page · raw message

docs/system/target-riscv.rst +1 -0
index 89b2cb732c..fbd8270022 100644
@@ -68,6 +68,7 @@ undocumented; you can get a complete list by running
 
    riscv/microblaze-v-generic
    riscv/microchip-icicle-kit
+   riscv/neorv32
    riscv/shakti-c
    riscv/sifive_u
    riscv/virt
hw/riscv/Kconfig +8 -0
index fc9c35bd98..976acd2a1b 100644
@@ -128,3 +128,11 @@ config XIANGSHAN_KUNMINGHU
     select RISCV_APLIC
     select RISCV_IMSIC
     select SERIAL_MM
+
+config NEORV32
+    bool
+    default y
+    depends on RISCV32
+    select NEORV32_UART
+    select NEORV32_SPI
+    select NEORV32_SYSINFO_QEMU
hw/riscv/meson.build +1 -0
index 2a8d5b136c..b8788e2035 100644
@@ -14,5 +14,6 @@ riscv_ss.add(when: 'CONFIG_RISCV_IOMMU', if_true: files(
 	'riscv-iommu.c', 'riscv-iommu-pci.c', 'riscv-iommu-sys.c', 'riscv-iommu-hpm.c'))
 riscv_ss.add(when: 'CONFIG_MICROBLAZE_V', if_true: files('microblaze-v-generic.c'))
 riscv_ss.add(when: 'CONFIG_XIANGSHAN_KUNMINGHU', if_true: files('xiangshan_kmh.c'))
+riscv_ss.add(when: 'CONFIG_NEORV32', if_true: files('neorv32.c'))
 
 hw_arch += {'riscv': riscv_ss}
hw/riscv/neorv32.c new file +215 -0

215 lines not on this page · raw message

include/hw/riscv/neorv32.h new file +54 -0

54 lines not on this page · raw message

Signature · 1 line
2.51.1
#9 lore raw
In reply to Michael Levit
Quoted 10 lines · Michael Levit

This v4 reworks the initial NEORV32 submissions.

The series introduces:
* a minimal NEORV32 RV32 CPU type and vendor CSR hook,
* the SYSINFO MMIO block,
* a small UART device,
* an SPI controller with command-mode chip-select,
* and the 'neorv32' RISC-V board wiring the above, plus docs.

Quoted 2 lines

Tested by booting the NEORV32 bootloader as -bios and chaining into a Hello World from an MTD-backed SPI flash image, with UART on stdio.

Hi!

Are these binaries available publically somewhere on the internet? If so, could you please add a test in tests/functional/riscv32 that make sure that the machine is basically working, so we don't face any regressions in the future?

Thanks,
Thomas

#10 lore raw
Quoted 3 lines

Are these binaries available publically somewhere on the internet?

Currently, as far as I know, there are no prebuilt binaries — only source files for the bootloader and firmware examples in the NEORV32 repository. I compile the binaries and prepare the flash image locally.

Quoted 6 lines

If so,
could you please add a test in tests/functional/riscv32 that make sure that
the machine is basically working, so we don't face any regressions in the future?

I can upload the generated image to my GitHub repository, or alternatively to some QEMU-related storage if there’s a preferred location? The test image is around 4 MB, plus a few kilobytes for the bootloader. I will add the test of course.

Thanks for the review and feedback!

#11 lore raw
Quoted 8 lines · Michael Levit

Are these binaries available publically somewhere on the internet?

Currently, as far as I know, there are no prebuilt binaries — only source files for the bootloader and firmware examples in the NEORV32 repository. I compile the binaries and prepare the flash image locally.

Quoted 8 lines

If so,
could you please add a test in tests/functional/riscv32 that make sure that the machine is basically working, so we don't face any regressions in the future?

I can upload the generated image to my GitHub repository, or alternatively to some QEMU-related storage if there’s a preferred location?

The QEMU project does not maintain a storage for such third party binaries, so if you could put them on your GitHub repo, that would be great!

Thanks,
Thomas

Quoted 5 lines

The test image is around 4 MB, plus a few kilobytes for the bootloader. I will add the test of course.

Thanks for the review and feedback!