[Qemu-devel] [PATCH v3 12/13] arm: Instantiate NRF51 Timers

Steffen Görtz posted 13 patches 7 years, 3 months ago
There is a newer version of this series
[Qemu-devel] [PATCH v3 12/13] arm: Instantiate NRF51 Timers
Posted by Steffen Görtz 7 years, 3 months ago
Instantiates TIMER0 - TIMER2

Signed-off-by: Steffen Görtz <contrib@steffen-goertz.de>
---
 hw/arm/nrf51_soc.c         | 27 +++++++++++++++++++++++++++
 include/hw/arm/nrf51_soc.h |  4 ++++
 2 files changed, 31 insertions(+)

diff --git a/hw/arm/nrf51_soc.c b/hw/arm/nrf51_soc.c
index 695b335dc5..c29b80c0ca 100644
--- a/hw/arm/nrf51_soc.c
+++ b/hw/arm/nrf51_soc.c
@@ -40,6 +40,8 @@ static void nrf51_soc_realize(DeviceState *dev_soc, Error **errp)
     NRF51State *s = NRF51_SOC(dev_soc);
     MemoryRegion *mr;
     Error *err = NULL;
+    uint8_t i = 0;
+    hwaddr base_addr = 0;
 
     if (!s->board_memory) {
         error_setg(errp, "memory property was not set");
@@ -141,6 +143,22 @@ static void nrf51_soc_realize(DeviceState *dev_soc, Error **errp)
     /* Pass all GPIOs to the SOC layer so they are available to the board */
     qdev_pass_gpios(DEVICE(&s->gpio), dev_soc, NULL);
 
+    /* TIMER */
+    for (i = 0; i < NRF51_NUM_TIMERS; i++) {
+        object_property_set_bool(OBJECT(&s->timer[i]), true, "realized", &err);
+        if (err) {
+            error_propagate(errp, err);
+            return;
+        }
+
+        base_addr = NRF51_TIMER_BASE + i * 0x1000;
+
+        sysbus_mmio_map(SYS_BUS_DEVICE(&s->timer[i]), 0, base_addr);
+        sysbus_connect_irq(SYS_BUS_DEVICE(&s->timer[i]), 0,
+                           qdev_get_gpio_in(DEVICE(&s->cpu),
+                                            BASE_TO_IRQ(base_addr)));
+    }
+
 
     create_unimplemented_device("nrf51_soc.io", NRF51_IOMEM_BASE,
                                 NRF51_IOMEM_SIZE);
@@ -150,6 +168,8 @@ static void nrf51_soc_realize(DeviceState *dev_soc, Error **errp)
 
 static void nrf51_soc_init(Object *obj)
 {
+    uint8_t i = 0;
+
     NRF51State *s = NRF51_SOC(obj);
 
     memory_region_init(&s->container, obj, "nrf51-container", UINT64_MAX);
@@ -173,6 +193,13 @@ static void nrf51_soc_init(Object *obj)
     sysbus_init_child_obj(obj, "gpio", &s->gpio, sizeof(s->gpio),
                           TYPE_NRF51_GPIO);
 
+    for (i = 0; i < NRF51_NUM_TIMERS; i++) {
+        sysbus_init_child_obj(obj, "timer[*]", &s->timer[i],
+                              sizeof(s->timer[i]), TYPE_NRF51_TIMER);
+
+    }
+
+
 }
 
 static Property nrf51_soc_properties[] = {
diff --git a/include/hw/arm/nrf51_soc.h b/include/hw/arm/nrf51_soc.h
index d4a48ccf91..89525dcb39 100644
--- a/include/hw/arm/nrf51_soc.h
+++ b/include/hw/arm/nrf51_soc.h
@@ -16,11 +16,14 @@
 #include "hw/misc/nrf51_rng.h"
 #include "hw/nvram/nrf51_nvm.h"
 #include "hw/gpio/nrf51_gpio.h"
+#include "hw/timer/nrf51_timer.h"
 
 #define TYPE_NRF51_SOC "nrf51-soc"
 #define NRF51_SOC(obj) \
     OBJECT_CHECK(NRF51State, (obj), TYPE_NRF51_SOC)
 
+#define NRF51_NUM_TIMERS 3
+
 typedef struct NRF51State {
     /*< private >*/
     SysBusDevice parent_obj;
@@ -32,6 +35,7 @@ typedef struct NRF51State {
     NRF51RNGState rng;
     NRF51NVMState nvm;
     NRF51GPIOState gpio;
+    NRF51TimerState timer[NRF51_NUM_TIMERS];
 
     MemoryRegion iomem;
     MemoryRegion sram;
-- 
2.19.1


Re: [Qemu-devel] [PATCH v3 12/13] arm: Instantiate NRF51 Timers
Posted by Stefan Hajnoczi 7 years, 3 months ago
On Tue, Oct 30, 2018 at 08:25:26PM -0400, Steffen Görtz wrote:
> +    /* TIMER */
> +    for (i = 0; i < NRF51_NUM_TIMERS; i++) {
> +        object_property_set_bool(OBJECT(&s->timer[i]), true, "realized", &err);
> +        if (err) {
> +            error_propagate(errp, err);
> +            return;
> +        }
> +
> +        base_addr = NRF51_TIMER_BASE + i * 0x1000;

s/0x1000/NRF51_TIMER_SIZE/

Aside from that:

Reviewed-by: Stefan Hajnoczi <stefanha@redhat.com>
Re: [Qemu-devel] [PATCH v3 12/13] arm: Instantiate NRF51 Timers
Posted by Steffen Görtz 7 years, 3 months ago
Hi Stefan,
> 
> Indentation is off here.  One way of formatting it:
> 
>                 address_space_write(&s->as, i * NRF51_PAGE_SIZE,
>                                     MEMTXATTRS_UNSPECIFIED, s->empty_page,
> 				    NRF51_PAGE_SIZE);

Good catch.

>> +static void nrf51_nvm_reset(DeviceState *dev)
>> +{
>> +    NRF51NVMState *s = NRF51_NVM(dev);
>> +
>> +    memset(s->uicr_content, '\0', sizeof(s->uicr_content));
>> +}
> 
> We will zero UICR.  Does UICR come zero-initialized on a real micro:bit?
> 
> I remember there was an issue with .hex files that set UICR values.
> Will nrf51_nvm_reset() overwrite values from .hex files when the generic
> loader devices is used (-device loader,file=test.hex)?
> 
UICR comes 0xFF initialized ([1] 8.1) and yes we had a conflict with the js-runtime.
I now moved the memset to init just before mapping the region:

memset(s->uicr_content, 0xFF, sizeof(s->uicr_content));
memory_region_init_io(&s->uicr, NULL, &uicr_ops, s, "nrf51_soc.uicr",
                      sizeof(s->uicr_content));
ysbus_init_mmio(sbd, &s->uicr);

That should do the trick.
A reset without loading of a new firmware blob will not change the (flash) user memory any longer, which should match the real device.

Best,
Steffen


[1] http://infocenter.nordicsemi.com/pdf/nRF51_RM_v3.0.pdf