Skip to contentSkip to conversation

Add nRF51 RTC timer

PATCH New Kaido Kert 1 participant 1 message May 10, 2025 Arm/Amlogic Meson SoC support

lore
Share & tools
lore

Kaido Kert

author
#0 lore raw

PATCH 1/1 Add nRF51 RTC timer

Implements the RTC timer for nRF51. Implementation is based on existing nrf51_timer.
Event signaling through PPI peripheral is not implemented. Tests added in microbit board.

---
 hw/arm/nrf51_soc.c          |  13 ++
 hw/rtc/Kconfig              |   3 +
 hw/rtc/meson.build          |   1 +
 hw/rtc/nrf51_rtc.c          | 373 ++++++++++++++++++++++++++++++++++++
 hw/rtc/trace-events         |   4 +
 include/hw/arm/nrf51.h      |   1 +
 include/hw/arm/nrf51_soc.h  |   2 +
 include/hw/rtc/nrf51_rtc.h  |  68 +++++++
 tests/qtest/microbit-test.c | 249 ++++++++++++++++++++++++
 9 files changed, 714 insertions(+)
 create mode 100644 hw/rtc/nrf51_rtc.c
 create mode 100644 include/hw/rtc/nrf51_rtc.h
hw/arm/nrf51_soc.c +13 -0
index d8cc3214ed..f1e3aadf19 100644
@@ -118,6 +118,17 @@ static void nrf51_soc_realize(DeviceState *dev_soc, Error **errp)
                        qdev_get_gpio_in(DEVICE(&s->armv7m),
                        BASE_TO_IRQ(NRF51_RNG_BASE)));
 
+    /* RTC */
+    if (!sysbus_realize(SYS_BUS_DEVICE(&s->rtc), errp)) {
+        return;
+    }
+    mr = sysbus_mmio_get_region(SYS_BUS_DEVICE(&s->rtc), 0);
+    memory_region_add_subregion_overlap(&s->container, NRF51_RTC0_BASE, mr, 0);
+    sysbus_connect_irq(SYS_BUS_DEVICE(&s->rtc), 0,
+                       qdev_get_gpio_in(DEVICE(&s->armv7m),
+                       BASE_TO_IRQ(NRF51_RTC0_BASE)
+                       ));
+
     /* UICR, FICR, NVMC, FLASH */
     if (!object_property_set_uint(OBJECT(&s->nvm), "flash-size",
                                   s->flash_size, errp)) {
@@ -195,6 +206,8 @@ static void nrf51_soc_init(Object *obj)
 
     object_initialize_child(obj, "rng", &s->rng, TYPE_NRF51_RNG);
 
+    object_initialize_child(obj, "rtc", &s->rtc, TYPE_NRF51_RTC);
+
     object_initialize_child(obj, "nvm", &s->nvm, TYPE_NRF51_NVM);
 
     object_initialize_child(obj, "gpio", &s->gpio, TYPE_NRF51_GPIO);
hw/rtc/Kconfig +3 -0
index 315b0e4ecc..18306c5db2 100644
@@ -31,3 +31,6 @@ config RS5C372_RTC
     bool
     depends on I2C
     default y if I2C_DEVICES
+
+config NRF51_RTC
+    bool
hw/rtc/meson.build +1 -0
index 6c87864dc0..b959c9efe3 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_NRF51_SOC', if_true: files('nrf51_rtc.c'))
hw/rtc/nrf51_rtc.c new file +373 -0

373 lines not on this page · raw message

hw/rtc/trace-events +4 -0
index b9f2852d35..b9dd52e6d0 100644
@@ -36,6 +36,10 @@ m48txx_nvram_mem_write(uint32_t addr, uint32_t value) "mem write addr:0x%04x val
 goldfish_rtc_read(uint64_t addr, uint64_t value) "addr 0x%02" PRIx64 " value 0x%08" PRIx64
 goldfish_rtc_write(uint64_t addr, uint64_t value) "addr 0x%02" PRIx64 " value 0x%08" PRIx64
 
+# nrf51_rtc.c
+nrf51_rtc_read(uint64_t addr, uint64_t value) "addr 0x%" PRIx64 " value 0x%" PRIx64
+nrf51_rtc_write(uint64_t addr, uint64_t value) "addr 0x%" PRIx64 " value 0x%" PRIx64
+
 # 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
include/hw/arm/nrf51.h +1 -0
index de836beaa4..b20519bd0a 100644
@@ -28,6 +28,7 @@
 #define NRF51_UART_BASE       0x40002000
 #define NRF51_TWI_BASE        0x40003000
 #define NRF51_TIMER_BASE      0x40008000
+#define NRF51_RTC0_BASE       0x4000B000
 #define NRF51_RNG_BASE        0x4000D000
 #define NRF51_NVMC_BASE       0x4001E000
 #define NRF51_GPIO_BASE       0x50000000
include/hw/arm/nrf51_soc.h +2 -0
index f88ab1b7d3..633100d4d0 100644
@@ -16,6 +16,7 @@
 #include "hw/misc/nrf51_rng.h"
 #include "hw/gpio/nrf51_gpio.h"
 #include "hw/nvram/nrf51_nvm.h"
+#include "hw/rtc/nrf51_rtc.h"
 #include "hw/timer/nrf51_timer.h"
 #include "hw/clock.h"
 #include "qom/object.h"
@@ -34,6 +35,7 @@ struct NRF51State {
 
     NRF51UARTState uart;
     NRF51RNGState rng;
+    NRF51RTCState rtc;
     NRF51NVMState nvm;
     NRF51GPIOState gpio;
     NRF51TimerState timer[NRF51_NUM_TIMERS];
include/hw/rtc/nrf51_rtc.h new file +68 -0

68 lines not on this page · raw message

tests/qtest/microbit-test.c +249 -0

267 lines not on this page · raw message

Signature · 1 line
2.39.5 (Apple Git-154)