Skip to contentSkip to conversation

hw/rtc: add K230 RTC.

PATCH New Leo Cheng 1 participant 5 messages Aug 8 1 message not in the archive

Subsystems (2)
lore
Share & tools
lore

A message that is not in the archive (sent off-list or not yet received): [email protected]

Leo Cheng

author
#1 lore raw

PATCH 1/5 hw/gpio: add K230 GPIO controller.

Model the Synopsys DW_apb_gpio controller on the Kendryte K230: direction, output, per-pin edge/level interrupts with EOI and output loopback in EXT_PORTA. Each pin drives its own PLIC line. Register layout from the K230 TRM v0.3.1. Covered by tests/qtest/k230-gpio-test.

---
 hw/gpio/Kconfig              |   3 +
 hw/gpio/k230_gpio.c          | 270 +++++++++++++++++++++++++++++++++++
 hw/gpio/meson.build          |   1 +
 hw/gpio/trace-events         |   4 +
 include/hw/gpio/k230_gpio.h  |  68 +++++++++
 tests/qtest/k230-gpio-test.c | 143 +++++++++++++++++++
 6 files changed, 489 insertions(+)
 create mode 100644 hw/gpio/k230_gpio.c
 create mode 100644 include/hw/gpio/k230_gpio.h
 create mode 100644 tests/qtest/k230-gpio-test.c
hw/gpio/Kconfig +3 -0
index a209294..07ef5a2 100644
@@ -30,3 +30,6 @@ config PCF8574
 
 config ZAURUS_SCOOP
     bool
+
+config K230_GPIO
+    bool
hw/gpio/k230_gpio.c new file +270 -0

270 lines not on this page · raw message

hw/gpio/meson.build +1 -0
index 6a67ee9..94b5d9e 100644
@@ -19,3 +19,4 @@ system_ss.add(when: 'CONFIG_ASPEED_SOC', if_true: files('aspeed_gpio.c'))
 system_ss.add(when: 'CONFIG_ASPEED_SOC', if_true: files('aspeed_sgpio.c'))
 system_ss.add(when: 'CONFIG_SIFIVE_GPIO', if_true: files('sifive_gpio.c'))
 system_ss.add(when: 'CONFIG_PCF8574', if_true: files('pcf8574.c'))
+system_ss.add(when: 'CONFIG_K230_GPIO', if_true: files('k230_gpio.c'))
hw/gpio/trace-events +4 -0
index cea896b..ac5d7fd 100644
@@ -46,3 +46,7 @@ stm32l4x5_gpio_read(char *gpio, uint64_t addr) "GPIO%s addr: 0x%" PRIx64 " "
 stm32l4x5_gpio_write(char *gpio, uint64_t addr, uint64_t data) "GPIO%s addr: 0x%" PRIx64 " val: 0x%" PRIx64 ""
 stm32l4x5_gpio_update_idr(char *gpio, uint32_t old_idr, uint32_t new_idr) "GPIO%s from: 0x%x to: 0x%x"
 stm32l4x5_gpio_pins(char *gpio, uint16_t disconnected, uint16_t high) "GPIO%s disconnected pins: 0x%x levels: 0x%x"
+
+# k230_gpio.c
+k230_gpio_read(uint64_t addr, uint32_t data) "K230 GPIO read: [0x%" PRIx64 "] -> 0x%" PRIx32
+k230_gpio_write(uint64_t addr, uint64_t data) "K230 GPIO write: [0x%" PRIx64 "] <- 0x%" PRIx64
include/hw/gpio/k230_gpio.h new file +68 -0

68 lines not on this page · raw message

tests/qtest/k230-gpio-test.c new file +143 -0

143 lines not on this page · raw message

Signature · 1 line
2.43.0
author
#2 lore raw

PATCH 2/5 hw/rtc: add K230 RTC.

In reply to a message not in the archive

Model the K230 RTC: date/time keeping with leap-year carry, write protection, a masked comparator alarm, a TICK_EN periodic tick interrupt, and a live sub-second COUNT derived from virtual time. The alarm is re-evaluated on TIME/ALARM/INT_CTRL writes and the year comparison ignores the DATE leap flag. Register layout from the K230 TRM v0.3.1. Covered by tests/qtest/k230-rtc-test.

---
 hw/rtc/Kconfig              |   3 +
 hw/rtc/k230_rtc.c           | 306 ++++++++++++++++++++++++++++++++++++
 hw/rtc/meson.build          |   1 +
 hw/rtc/trace-events         |   5 +
 include/hw/rtc/k230_rtc.h   |  61 +++++++
 tests/qtest/k230-rtc-test.c | 188 ++++++++++++++++++++++
 6 files changed, 564 insertions(+)
 create mode 100644 hw/rtc/k230_rtc.c
 create mode 100644 include/hw/rtc/k230_rtc.h
 create mode 100644 tests/qtest/k230-rtc-test.c
hw/rtc/Kconfig +3 -0
index 315b0e4..d764807 100644
@@ -31,3 +31,6 @@ config RS5C372_RTC
     bool
     depends on I2C
     default y if I2C_DEVICES
+
+config K230_RTC
+    bool
hw/rtc/k230_rtc.c new file +306 -0

306 lines not on this page · raw message

hw/rtc/meson.build +1 -0
index 6c87864..3f3529c 100644
@@ -14,3 +14,4 @@ system_ss.add(when: 'CONFIG_LS7A_RTC', if_true: files('ls7a_rtc.c'))
 system_ss.add(when: 'CONFIG_ALLWINNER_H3', if_true: files('allwinner-rtc.c'))
 system_ss.add(when: 'CONFIG_MC146818RTC', if_true: files('mc146818rtc.c'))
 system_ss.add(when: 'CONFIG_RS5C372_RTC', if_true: files('rs5c372.c'))
+system_ss.add(when: 'CONFIG_K230_RTC', if_true: files('k230_rtc.c'))
hw/rtc/trace-events +5 -0
index d2f3621..beee3b1 100644
@@ -43,3 +43,8 @@ goldfish_rtc_write(uint64_t addr, uint64_t value) "addr 0x%02" PRIx64 " value 0x
 # rs5c372.c
 rs5c372_recv(uint32_t addr, uint8_t value) "[0x%" PRIx32 "] -> 0x%02" PRIx8
 rs5c372_send(uint32_t addr, uint8_t value) "[0x%" PRIx32 "] <- 0x%02" PRIx8
+
+# k230_rtc.c
+k230_rtc_read(uint64_t addr, uint32_t data) "K230 RTC read: [0x%" PRIx64 "] -> 0x%" PRIx32
+k230_rtc_write(uint64_t addr, uint64_t data) "K230 RTC write: [0x%" PRIx64 "] <- 0x%" PRIx64
+k230_rtc_alarm(void) "K230 RTC alarm"
include/hw/rtc/k230_rtc.h new file +61 -0

61 lines not on this page · raw message

tests/qtest/k230-rtc-test.c new file +188 -0

188 lines not on this page · raw message

Signature · 1 line
2.43.0
author
#3 lore raw

PATCH 3/5 hw/misc: add K230 IOMUX and mailbox/IPCM.

In reply to a message not in the archive

IOMUX is the K230 pin-configuration controller (64 pins, one 32-bit register per pin) with the field layout of the upstream Linux pinctrl-k230 driver. The mailbox/IPCM block at 0x91104000 is an inter-core interrupt-notify mailbox plus a hardware spinlock array: a write to a CPU2DSP/DSP2CPU SET register rings the doorbell IRQ, CLEAR acks, and the 0xA0 array is 128 test-and-set locks. Message payloads travel through shared memory, so no data registers are modelled. Register semantics from the Canaan k230_sdk. Covered by the k230-iomux and k230-mailbox qtests.

---
 hw/misc/Kconfig                 |   6 +
 hw/misc/k230_iomux.c            | 101 ++++++++++++++++
 hw/misc/k230_mailbox.c          | 206 ++++++++++++++++++++++++++++++++
 hw/misc/meson.build             |   2 +
 hw/misc/trace-events            |  10 ++
 include/hw/misc/k230_iomux.h    |  44 +++++++
 include/hw/misc/k230_mailbox.h  |  61 ++++++++++
 tests/qtest/k230-iomux-test.c   |  62 ++++++++++
 tests/qtest/k230-mailbox-test.c |  89 ++++++++++++++
 9 files changed, 581 insertions(+)
 create mode 100644 hw/misc/k230_iomux.c
 create mode 100644 hw/misc/k230_mailbox.c
 create mode 100644 include/hw/misc/k230_iomux.h
 create mode 100644 include/hw/misc/k230_mailbox.h
 create mode 100644 tests/qtest/k230-iomux-test.c
 create mode 100644 tests/qtest/k230-mailbox-test.c
hw/misc/Kconfig +6 -0
index 1543ee6..742ea2f 100644
@@ -186,6 +186,12 @@ config AUX
 config UNIMP
     bool
 
+config K230_IOMUX
+    bool
+
+config K230_MAILBOX
+    bool
+
 config LED
     bool
 
hw/misc/k230_iomux.c new file +101 -0

101 lines not on this page · raw message

hw/misc/k230_mailbox.c new file +206 -0

206 lines not on this page · raw message

hw/misc/meson.build +2 -0
index 23265f6..c0a678b 100644
@@ -6,6 +6,8 @@ system_ss.add(when: 'CONFIG_ISA_TESTDEV', if_true: files('pc-testdev.c'))
 system_ss.add(when: 'CONFIG_PCI_TESTDEV', if_true: files('pci-testdev.c'))
 system_ss.add(when: 'CONFIG_IOMMU_TESTDEV', if_true: files('iommu-testdev.c'))
 system_ss.add(when: 'CONFIG_UNIMP', if_true: files('unimp.c'))
+system_ss.add(when: 'CONFIG_K230_IOMUX', if_true: files('k230_iomux.c'))
+system_ss.add(when: 'CONFIG_K230_MAILBOX', if_true: files('k230_mailbox.c'))
 system_ss.add(when: 'CONFIG_EMPTY_SLOT', if_true: files('empty_slot.c'))
 system_ss.add(when: 'CONFIG_LED', if_true: files('led.c'))
 system_ss.add(when: 'CONFIG_PVPANIC_COMMON', if_true: files('pvpanic.c'))
hw/misc/trace-events +10 -0
index c9a868b..5bf28dd 100644
@@ -442,3 +442,13 @@ iommu_testdev_dma_read(uint64_t gva, uint32_t len) "gva=0x%" PRIx64 " len=%u"
 iommu_testdev_dma_verify(uint32_t expected, uint32_t actual) "expected=0x%x actual=0x%x"
 iommu_testdev_dma_result(uint32_t result) "DMA completed result=0x%x"
 iommu_testdev_dma_armed(bool armed) "armed=%d"
+
+# k230_iomux.c
+k230_iomux_read(uint64_t addr, uint32_t val) "addr 0x%" PRIx64 " val 0x%08x"
+k230_iomux_write(uint64_t addr, uint64_t val) "addr 0x%" PRIx64 " val 0x%" PRIx64
+
+# k230_mailbox.c
+k230_mailbox_read(uint64_t addr, uint32_t val) "addr 0x%" PRIx64 " val 0x%08x"
+k230_mailbox_write(uint64_t addr, uint64_t val) "addr 0x%" PRIx64 " val 0x%" PRIx64
+k230_mailbox_lock_read(int num, uint32_t old) "lock %d old %u"
+k230_mailbox_lock_write(int num, uint64_t val) "lock %d val 0x%" PRIx64
include/hw/misc/k230_iomux.h new file +44 -0

44 lines not on this page · raw message

include/hw/misc/k230_mailbox.h new file +61 -0

61 lines not on this page · raw message

tests/qtest/k230-iomux-test.c new file +62 -0

62 lines not on this page · raw message

tests/qtest/k230-mailbox-test.c new file +89 -0

89 lines not on this page · raw message

Signature · 1 line
2.43.0
author
#4 lore raw

PATCH 4/5 hw/sd: add K230 SD/eMMC (DesignWare dw_mmc).

In reply to a message not in the archive

Model the register interface of the DesignWare dw_mmc host used by the K230: self-clearing CTRL reset bits, a self-clearing CMD.START that raises command completion, W1C RINTSTS and masked-interrupt delivery. The FIFO/DMA data path is not modelled; this is enough for a driver to probe. Register map from the DesignWare dw_mmc driver. Covered by tests/qtest/k230-dwmmc-test.

---
 hw/sd/Kconfig                 |   3 +
 hw/sd/k230_dwmmc.c            | 139 ++++++++++++++++++++++++++++++++++
 hw/sd/meson.build             |   1 +
 hw/sd/trace-events            |   4 +
 include/hw/sd/k230_dwmmc.h    |  61 +++++++++++++++
 tests/qtest/k230-dwmmc-test.c |  79 +++++++++++++++++++
 6 files changed, 287 insertions(+)
 create mode 100644 hw/sd/k230_dwmmc.c
 create mode 100644 include/hw/sd/k230_dwmmc.h
 create mode 100644 tests/qtest/k230-dwmmc-test.c
hw/sd/Kconfig +3 -0
index 633b9af..15f1fd9 100644
@@ -23,3 +23,6 @@ config SDHCI_PCI
 config CADENCE_SDHCI
     bool
     select SDHCI
+
+config K230_DWMMC
+    bool
hw/sd/k230_dwmmc.c new file +139 -0

139 lines not on this page · raw message

hw/sd/meson.build +1 -0
index b43d45b..d642de3 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_DWMMC', if_true: files('k230_dwmmc.c'))
hw/sd/trace-events +4 -0
index c6811f3..8537b25 100644
@@ -75,3 +75,7 @@ pl181_data_engine_idle(void) "data engine idle"
 # aspeed_sdhci.c
 aspeed_sdhci_read(uint64_t addr, uint32_t size, uint64_t data) "@0x%" PRIx64 " size %u: 0x%" PRIx64
 aspeed_sdhci_write(uint64_t addr, uint32_t size, uint64_t data) "@0x%" PRIx64 " size %u: 0x%" PRIx64
+
+# k230_dwmmc.c
+k230_dwmmc_read(uint64_t addr, uint32_t val) "addr 0x%" PRIx64 " val 0x%08x"
+k230_dwmmc_write(uint64_t addr, uint64_t val) "addr 0x%" PRIx64 " val 0x%" PRIx64
include/hw/sd/k230_dwmmc.h new file +61 -0

61 lines not on this page · raw message

tests/qtest/k230-dwmmc-test.c new file +79 -0

79 lines not on this page · raw message

Signature · 1 line
2.43.0
author
#5 lore raw

PATCH 5/5 hw/riscv: wire K230 peripherals into the machine.

In reply to a message not in the archive

Instantiate the GPIO, RTC, IOMUX, mailbox and SD/eMMC models on the k230 machine, add two SiFive PWM instances, and register the qtests. Also add a satellite-safety fault-injection qtest (watchdog timeout, protected-register tamper, GPIO interrupt storm/loss) exercising the modelled devices, update the machine documentation and MAINTAINERS.

---
 MAINTAINERS                    |  12 +++-
 docs/system/riscv/k230.rst     |   6 ++
 hw/riscv/Kconfig               |   7 ++
 hw/riscv/k230.c                |  89 +++++++++++++++++++----
 include/hw/riscv/k230.h        |  18 +++++
 tests/qtest/k230-pwm-test.c    |  43 +++++++++++
 tests/qtest/k230-safety-test.c | 127 +++++++++++++++++++++++++++++++++
 tests/qtest/meson.build        |   2 +-
 8 files changed, 290 insertions(+), 14 deletions(-)
 create mode 100644 tests/qtest/k230-pwm-test.c
 create mode 100644 tests/qtest/k230-safety-test.c
MAINTAINERS +11 -1
index 6171cc7..987a58f 100644
@@ -1825,10 +1825,20 @@ L: [email protected]
 S: Maintained
 F: docs/system/riscv/k230.rst
 F: hw/riscv/k230.c
+F: hw/gpio/k230_gpio.c
+F: hw/rtc/k230_rtc.c
+F: hw/misc/k230_iomux.c
+F: hw/misc/k230_mailbox.c
+F: hw/sd/k230_dwmmc.c
 F: hw/watchdog/k230_wdt.c
 F: include/hw/riscv/k230.h
+F: include/hw/gpio/k230_gpio.h
+F: include/hw/rtc/k230_rtc.h
+F: include/hw/misc/k230_iomux.h
+F: include/hw/misc/k230_mailbox.h
+F: include/hw/sd/k230_dwmmc.h
 F: include/hw/watchdog/k230_wdt.h
-F: tests/qtest/k230-wdt-test.c
+F: tests/qtest/k230-*-test.c
 
 RX Machines
 -----------
docs/system/riscv/k230.rst +6 -0
index cea8202..7adafaf 100644
@@ -20,6 +20,12 @@ The ``k230`` machine supports the following devices:
 * Platform-Level Interrupt Controller (PLIC)
 * 2 K230 Watchdog Timer
 * 5 UART
+* 2 GPIO controllers (DesignWare APB GPIO, one PLIC line per pin)
+* 2 PWM controllers (SiFive PWM)
+* RTC (date/time, alarm and periodic tick interrupts)
+* IOMUX pin-configuration controller
+* Mailbox / IPCM block (inter-core doorbell interrupts + hardware spinlock)
+* SD/eMMC host (DesignWare dw_mmc, register model)
 
 Boot options
 ------------
hw/riscv/Kconfig +7 -0
index de37c08..82c9d7a 100644
@@ -106,6 +106,7 @@ config SIFIVE_U
     select SIFIVE_U_OTP
     select SIFIVE_U_PRCI
     select SIFIVE_PWM
+    select K230_RTC
     select SSI_M25P80
     select SSI_SD
     select UNIMP
@@ -162,3 +163,9 @@ config K230
     select SERIAL_MM
     select UNIMP
     select K230_WDT
+    select K230_GPIO
+    select SIFIVE_PWM
+    select K230_RTC
+    select K230_IOMUX
+    select K230_MAILBOX
+    select K230_DWMMC
hw/riscv/k230.c +77 -12

113 lines not on this page · raw message

include/hw/riscv/k230.h +18 -0
index 592e1c2..e045f96 100644
@@ -18,6 +18,12 @@
 #include "hw/core/boards.h"
 #include "hw/riscv/riscv_hart.h"
 #include "hw/watchdog/k230_wdt.h"
+#include "hw/gpio/k230_gpio.h"
+#include "hw/timer/sifive_pwm.h"
+#include "hw/rtc/k230_rtc.h"
+#include "hw/misc/k230_iomux.h"
+#include "hw/misc/k230_mailbox.h"
+#include "hw/sd/k230_dwmmc.h"
 
 #define C908_CPU_HARTID   (0)
 
@@ -33,6 +39,12 @@ typedef struct K230SoCState {
     RISCVHartArrayState c908_cpu; /* Small core */
 
     K230WdtState wdt[2];
+    K230GpioState gpio[2];
+    SiFivePwmState pwm[2];
+    K230RtcState rtc;
+    K230IomuxState iomux;
+    K230MailboxState mailbox;
+    K230DwmmcState sd[2];
     MemoryRegion sram;
     MemoryRegion bootrom;
 
@@ -127,8 +139,14 @@ enum {
     K230_UART2_IRQ  = 18,
     K230_UART3_IRQ  = 19,
     K230_UART4_IRQ  = 20,
+    K230_GPIO0_IRQ  = 32,   /* per-pin: 32..63 */
+    K230_GPIO1_IRQ  = 64,   /* per-pin: 64..95 */
+    K230_RTC_IRQ    = 175,  /* shared PMU/RTC */
     K230_WDT0_IRQ   = 107,
     K230_WDT1_IRQ   = 108,
+    K230_MAILBOX_IRQ = 109,  /* IPCM doorbell 109..112; ch0/ch1 use 109/110 */
+    K230_SD0_IRQ    = 142,
+    K230_SD1_IRQ    = 144,
 };
 
 #define K230_UART_COUNT 5
tests/qtest/k230-pwm-test.c new file +43 -0

43 lines not on this page · raw message

tests/qtest/k230-safety-test.c new file +127 -0

127 lines not on this page · raw message

tests/qtest/meson.build +1 -1
index 56ff860..be9e9ce 100644
@@ -297,7 +297,7 @@ 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-gpio-test', 'k230-pwm-test', 'k230-rtc-test', 'k230-iomux-test', 'k230-mailbox-test', 'k230-safety-test', 'k230-dwmmc-test'] : [])
 
 qtests_hexagon = ['boot-serial-test']
 
Signature · 1 line
2.43.0