Skip to contentSkip to conversation

arm: microbit Non-Volatile Memory Controller

PATCH Applied Stefan Hajnoczi 5 participants 14 messages Jan 23, 2019 – Jan 25, 2019

v2 of 4 Applied Latest is v4 →

lore
Share & tools
lore
#0 lore raw

v2 cover arm: microbit Non-Volatile Memory Controller

v2:
* Add Patch 2 to call memory_region_flush_rom_device() from pflash devices [Peter]

This series adds the Non-Volatile Memory Controller, which controls access to the User Information Control Registers (UICR), Factory Information Control Registers (FICR), and flash memory.

This is the last piece of microbit work needed to make basic programs like Micropython "Hello world" work under QEMU.

Originally sent as part of Steffen's longer microbit device emulation series, I extracted this and deferred it until later because cleanups were necessary:

* Use memory_region_flush_rom_device() to dirty/invalidate memory [Peter] ^--- Paolo: I CCed you on this new memory API
* Fix device-introspect-test segfault due to missing owner when initializing FICR and UICR memory regions [Peter]
* Fix off-by-one assertion checks [Peter]
* Fix missing whitespace at end of comment [Peter]
* Clear UICR on reset - we'd need a block device for true non-volatility [Peter]

Stefan Hajnoczi (2):
memory: add memory_region_flush_rom_device()
pflash: flush rom device memory region

Steffen Görtz (3):
hw/nvram/nrf51_nvm: Add nRF51 non-volatile memories
arm: Instantiate NRF51 special NVM's and NVMC tests/microbit-test: Add tests for nRF51 NVMC

 hw/nvram/Makefile.objs       |   1 +
 include/exec/memory.h        |  18 ++
 include/hw/arm/nrf51_soc.h   |   2 +
 include/hw/nvram/nrf51_nvm.h |  64 ++++++
 exec.c                       |  12 ++
 hw/arm/nrf51_soc.c           |  41 ++--
 hw/block/pflash_cfi01.c      |   5 +
 hw/block/pflash_cfi02.c      |   8 +
 hw/nvram/nrf51_nvm.c         | 381 +++++++++++++++++++++++++++++++++++
 tests/microbit-test.c        |  97 +++++++++
 10 files changed, 617 insertions(+), 12 deletions(-)
 create mode 100644 include/hw/nvram/nrf51_nvm.h
 create mode 100644 hw/nvram/nrf51_nvm.c
Signature · 1 line
2.20.1
#1 lore raw

PATCH v2 1/5 memory: add memory_region_flush_rom_device()

ROM devices go via MemoryRegionOps->write() callbacks for write operations and do not dirty/invalidate that memory. Device emulation must be able to mark memory ranges that have been modified internally (e.g. using memory_region_get_ram_ptr()).

Introduce the memory_region_flush_rom_device() API for this purpose.

---
 include/exec/memory.h | 18 ++++++++++++++++++
 exec.c                | 12 ++++++++++++
 2 files changed, 30 insertions(+)
include/exec/memory.h +18 -0
index cd2f209b64..abe9cc79c0 100644
@@ -1344,6 +1344,24 @@ bool memory_region_snapshot_get_dirty(MemoryRegion *mr,
 void memory_region_reset_dirty(MemoryRegion *mr, hwaddr addr,
                                hwaddr size, unsigned client);
 
+/**
+ * memory_region_flush_rom_device: Mark a range of pages dirty and invalidate
+ *                                 TBs (for self-modifying code).
+ *
+ * The MemoryRegionOps->write() callback of a ROM device must use this function
+ * to mark byte ranges that have been modified internally, such as by directly
+ * accessing the memory returned by memory_region_get_ram_ptr().
+ *
+ * This function marks the range dirty and invalidates TBs so that TCG can
+ * detect self-modifying code.
+ *
+ * @mr: the region being flushed.
+ * @addr: the start, relative to the start of the region, of the range being
+ *        flushed.
+ * @size: the size, in bytes, of the range being flushed.
+ */
+void memory_region_flush_rom_device(MemoryRegion *mr, hwaddr addr, hwaddr size);
+
 /**
  * memory_region_set_readonly: Turn a memory region read-only (or read-write)
  *
exec.c +12 -0
index 895449f926..105ff21e74 100644
@@ -3162,6 +3162,18 @@ static void invalidate_and_set_dirty(MemoryRegion *mr, hwaddr addr,
     cpu_physical_memory_set_dirty_range(addr, length, dirty_log_mask);
 }
 
+void memory_region_flush_rom_device(MemoryRegion *mr, hwaddr addr, hwaddr size)
+{
+    /* In principle this function would work on other memory region types too,
+     * but the ROM device use case is the only one where this operation is
+     * necessary.  Other memory regions should use the
+     * address_space_read/write() APIs.
+     */
+    assert(memory_region_is_romd(mr));
+
+    invalidate_and_set_dirty(mr, addr, size);
+}
+
 static int memory_access_size(MemoryRegion *mr, unsigned l, hwaddr addr)
 {
     unsigned access_size_max = mr->ops->valid.max_access_size;
Signature · 1 line
2.20.1
#2 lore raw

PATCH v2 2/5 pflash: flush rom device memory region

In reply to Stefan Hajnoczi

pflash devices should mark the memory region dirty and invalidate TBs after directly writing to the RAM backing the ROM device.

Note that pflash_cfi01_get_memory() is used by several machine types to populate ROM contents directly. Callers are untouched by this patch because they only modify memory before the guest is started.

---
 hw/block/pflash_cfi01.c | 5 +++++
 hw/block/pflash_cfi02.c | 8 ++++++++
 2 files changed, 13 insertions(+)
hw/block/pflash_cfi01.c +5 -0
index bffb4c40e7..5301c11c18 100644
@@ -446,6 +446,7 @@ static inline void pflash_data_write(pflash_t *pfl, hwaddr offset,
         break;
     }
 
+    memory_region_flush_rom_device(&pfl->mem, offset, width);
 }
 
 static void pflash_write(pflash_t *pfl, hwaddr offset,
@@ -482,6 +483,8 @@ static void pflash_write(pflash_t *pfl, hwaddr offset,
             if (!pfl->ro) {
                 memset(p + offset, 0xff, pfl->sector_len);
                 pflash_update(pfl, offset, pfl->sector_len);
+                memory_region_flush_rom_device(&pfl->mem, offset,
+                                               pfl->sector_len);
             } else {
                 pfl->status |= 0x20; /* Block erase error */
             }
@@ -763,6 +766,8 @@ static void pflash_cfi01_realize(DeviceState *dev, Error **errp)
             error_setg(errp, "failed to read the initial flash content");
             return;
         }
+
+        memory_region_flush_rom_device(&pfl->mem, 0, total_len);
     }
 
     /* Default to devices being used at their maximum device width. This was
hw/block/pflash_cfi02.c +8 -0
index 0f8b7b8c7b..d04572eca4 100644
@@ -378,6 +378,8 @@ static void pflash_write (pflash_t *pfl, hwaddr offset,
                     pflash_update(pfl, offset, 4);
                     break;
                 }
+
+                memory_region_flush_rom_device(&pfl->orig_mem, offset, width);
             }
             pfl->status = 0x00 | ~(value & 0x80);
             /* Let's pretend write is immediate */
@@ -426,6 +428,8 @@ static void pflash_write (pflash_t *pfl, hwaddr offset,
             if (!pfl->ro) {
                 memset(pfl->storage, 0xFF, pfl->chip_len);
                 pflash_update(pfl, 0, pfl->chip_len);
+                memory_region_flush_rom_device(&pfl->orig_mem, 0,
+                                               pfl->chip_len);
             }
             pfl->status = 0x00;
             /* Let's wait 5 seconds before chip erase is done */
@@ -441,6 +445,8 @@ static void pflash_write (pflash_t *pfl, hwaddr offset,
             if (!pfl->ro) {
                 memset(p + offset, 0xFF, pfl->sector_len);
                 pflash_update(pfl, offset, pfl->sector_len);
+                memory_region_flush_rom_device(&pfl->orig_mem, offset,
+                                               pfl->sector_len);
             }
             pfl->status = 0x00;
             /* Let's wait 1/2 second before sector erase is done */
@@ -590,6 +596,8 @@ static void pflash_cfi02_realize(DeviceState *dev, Error **errp)
             error_setg(errp, "failed to read the initial flash content");
             return;
         }
+
+        memory_region_flush_rom_device(&pfl->orig_mem, 0, chip_len);
     }
 
     pflash_setup_mappings(pfl);
Signature · 1 line
2.20.1
#3 lore raw

Re: [Qemu-arm] [Qemu-devel] [PATCH v2 2/5] pflash: flush rom device memory region

Hi Stefan,

Quoted 35 lines · Stefan Hajnoczi

pflash devices should mark the memory region dirty and invalidate TBs after directly writing to the RAM backing the ROM device.

Note that pflash_cfi01_get_memory() is used by several machine types to populate ROM contents directly. Callers are untouched by this patch because they only modify memory before the guest is started.

---
 hw/block/pflash_cfi01.c | 5 +++++
 hw/block/pflash_cfi02.c | 8 ++++++++
 2 files changed, 13 insertions(+)
hw/block/pflash_cfi01.c +3 -0
index bffb4c40e7..5301c11c18 100644
@@ -446,6 +446,7 @@ static inline void pflash_data_write(pflash_t *pfl, hwaddr offset,
         break;
     }
 
+    memory_region_flush_rom_device(&pfl->mem, offset, width);
 }
 
 static void pflash_write(pflash_t *pfl, hwaddr offset,
@@ -482,6 +483,8 @@ static void pflash_write(pflash_t *pfl, hwaddr offset,
             if (!pfl->ro) {
                 memset(p + offset, 0xff, pfl->sector_len);
                 pflash_update(pfl, offset, pfl->sector_len);
+                memory_region_flush_rom_device(&pfl->mem, offset,
+                                               pfl->sector_len);
             } else {
                 pfl->status |= 0x20; /* Block erase error */
             }

hw/block/pflash_cfi01.c static void pflash_cfi01_realize(DeviceState *dev, Error **errp) · line 770

Quoted 5 lines
hw/block/pflash_cfi01.c +2 -0
@@ -763,6 +766,8 @@ static void pflash_cfi01_realize(DeviceState *dev, Error **errp)
             error_setg(errp, "failed to read the initial flash content");
             return;
         }
+
+        memory_region_flush_rom_device(&pfl->mem, 0, total_len);

The device realize() is also executed before the guest is started, is this call really necessary?

Quoted 35 lines
hw/block/pflash_cfi01.c +0 -0
     }
 
     /* Default to devices being used at their maximum device width. This was
hw/block/pflash_cfi02.c +6 -0
index 0f8b7b8c7b..d04572eca4 100644
@@ -378,6 +378,8 @@ static void pflash_write (pflash_t *pfl, hwaddr offset,
                     pflash_update(pfl, offset, 4);
                     break;
                 }
+
+                memory_region_flush_rom_device(&pfl->orig_mem, offset, width);
             }
             pfl->status = 0x00 | ~(value & 0x80);
             /* Let's pretend write is immediate */
@@ -426,6 +428,8 @@ static void pflash_write (pflash_t *pfl, hwaddr offset,
             if (!pfl->ro) {
                 memset(pfl->storage, 0xFF, pfl->chip_len);
                 pflash_update(pfl, 0, pfl->chip_len);
+                memory_region_flush_rom_device(&pfl->orig_mem, 0,
+                                               pfl->chip_len);
             }
             pfl->status = 0x00;
             /* Let's wait 5 seconds before chip erase is done */
@@ -441,6 +445,8 @@ static void pflash_write (pflash_t *pfl, hwaddr offset,
             if (!pfl->ro) {
                 memset(p + offset, 0xFF, pfl->sector_len);
                 pflash_update(pfl, offset, pfl->sector_len);
+                memory_region_flush_rom_device(&pfl->orig_mem, offset,
+                                               pfl->sector_len);
             }
             pfl->status = 0x00;
             /* Let's wait 1/2 second before sector erase is done */

hw/block/pflash_cfi02.c static void pflash_cfi02_realize(DeviceState *dev, Error **errp) · line 600

Quoted 5 lines
hw/block/pflash_cfi02.c +2 -0
@@ -590,6 +596,8 @@ static void pflash_cfi02_realize(DeviceState *dev, Error **errp)
             error_setg(errp, "failed to read the initial flash content");
             return;
         }
+
+        memory_region_flush_rom_device(&pfl->orig_mem, 0, chip_len);

Ditto.

Quoted 4 lines
hw/block/pflash_cfi02.c +0 -0
     }
 
     pflash_setup_mappings(pfl);
#4 lore raw

Re: [Qemu-arm] [Qemu-devel] [PATCH v2 2/5] pflash: flush rom device memory region

Quoted 7 lines · Philippe Mathieu-Daudé
Quoted 6 lines · Stefan Hajnoczi

pflash devices should mark the memory region dirty and invalidate TBs after directly writing to the RAM backing the ROM device.

Note that pflash_cfi01_get_memory() is used by several machine types to populate ROM contents directly. Callers are untouched by this patch because they only modify memory before the guest is started.

...

Quoted 9 lines

hw/block/pflash_cfi01.c static void pflash_cfi01_realize(DeviceState *dev, Error **errp) · line 770

Quoted 6 lines
hw/block/pflash_cfi01.c +2 -0
@@ -763,6 +766,8 @@ static void pflash_cfi01_realize(DeviceState *dev, Error **errp)
             error_setg(errp, "failed to read the initial flash content");
             return;
         }
+
+        memory_region_flush_rom_device(&pfl->mem, 0, total_len);

The device realize() is also executed before the guest is started, is this call really necessary?

My rationale was that machine init only happens before the guest is started while ->realize() is called by hotplug too.

That said, can pflash devices be hotplugged?

If you guys prefer not flushing from pflash ->realize() then I'll drop it.

Stefan

#5 lore raw

Re: [Qemu-devel] [PATCH v2 2/5] pflash: flush rom device memory region

Quoted 7 lines · Stefan Hajnoczi
Quoted 2 lines · Philippe Mathieu-Daudé

The device realize() is also executed before the guest is started, is this call really necessary?

My rationale was that machine init only happens before the guest is started while ->realize() is called by hotplug too.

Yes, but before realize the flash memory can't actually be visible to the guest, so there's no need to invalidate anything.

Quoted 1 line

That said, can pflash devices be hotplugged?

I guess you could have one inside a hotplugged device in theory; they can't be directly hotpluggged. (I bet that in practice there are leaks though if you did do that, and we don't have any devices that do so today.)

Quoted 2 lines

If you guys prefer not flushing from pflash ->realize() then I'll drop it.

I think it makes more sense not to do a flush in the realize method.

thanks
-- PMM

#6 lore raw

Re: [Qemu-arm] [Qemu-devel] [PATCH v2 2/5] pflash: flush rom device memory region

Quoted 19 lines · Peter Maydell
Quoted 7 lines · Stefan Hajnoczi
Quoted 2 lines · Philippe Mathieu-Daudé

The device realize() is also executed before the guest is started, is this call really necessary?

My rationale was that machine init only happens before the guest is started while ->realize() is called by hotplug too.

Yes, but before realize the flash memory can't actually be visible to the guest, so there's no need to invalidate anything.

Quoted 1 line

That said, can pflash devices be hotplugged?

I guess you could have one inside a hotplugged device in theory; they can't be directly hotpluggged. (I bet that in practice there are leaks though if you did do that, and we don't have any devices that do so today.)

Quoted 4 lines
Quoted 2 lines

If you guys prefer not flushing from pflash ->realize() then I'll drop it.

I think it makes more sense not to do a flush in the realize method.

Okay, I'll resend this patch.

Stefan

#7 lore raw

Re: [Qemu-arm] [Qemu-devel] [PATCH v2 2/5] pflash: flush rom device memory region

Quoted 13 lines · Stefan Hajnoczi
Quoted 12 lines · Peter Maydell
Quoted 7 lines · Stefan Hajnoczi
Quoted 2 lines · Philippe Mathieu-Daudé

The device realize() is also executed before the guest is started, is this call really necessary?

My rationale was that machine init only happens before the guest is started while ->realize() is called by hotplug too.

Yes, but before realize the flash memory can't actually be visible to the guest, so there's no need to invalidate anything.

Quoted 6 lines
Quoted 6 lines
Quoted 1 line

That said, can pflash devices be hotplugged?

I guess you could have one inside a hotplugged device in theory; they can't be directly hotpluggged. (I bet that in practice there are leaks though if you did do that, and we don't have any devices that do so today.)

Can we assume pflash aren't hotpluggable until someone find an use case and is willing to work on it? I can't find any practical example.

Quoted 6 lines
Quoted 4 lines
Quoted 2 lines

If you guys prefer not flushing from pflash ->realize() then I'll drop it.

I think it makes more sense not to do a flush in the realize method.

Okay, I'll resend this patch.

Thanks!

Phil.

#8 lore raw

PATCH v2 3/5 hw/nvram/nrf51_nvm: Add nRF51 non-volatile memories

In reply to Stefan Hajnoczi

From: Steffen Görtz <[email protected]>

The nRF51 contains three regions of non-volatile memory (NVM):
- CODE (R/W): contains code
- FICR (R): Factory information like code size, chip id etc.
- UICR (R/W): Changeable configuration data. Lock bits, Code protection configuration, Bootloader address, Nordic SoftRadio configuration, Firmware configuration.

Read and write access to the memories is managed by the Non-volatile memory controller.

Memory schema:
[ CPU ] -+- [ NVM, either FICR, UICR or CODE ]

          |      |
          \- [ NVMC ]

---
* Fix device-introspect-test segfault due to missing owner when initializing FICR and UICR memory regions [Peter]
* Fix off-by-one assertion checks [Peter]
* Fix missing whitespace at end of comment [Peter]
* Clear UICR on reset - we'd need a block device for true non-volatility [Peter]
* Use memory_region_flush_rom_device() to dirty/invalidate memory [Peter]

---
 hw/nvram/Makefile.objs       |   1 +
 include/hw/nvram/nrf51_nvm.h |  64 ++++++
 hw/nvram/nrf51_nvm.c         | 381 +++++++++++++++++++++++++++++++++++
 3 files changed, 446 insertions(+)
 create mode 100644 include/hw/nvram/nrf51_nvm.h
 create mode 100644 hw/nvram/nrf51_nvm.c
hw/nvram/Makefile.objs +1 -0
index b318e53a43..26f7b4ca35 100644
@@ -5,3 +5,4 @@ common-obj-y += fw_cfg.o
 common-obj-y += chrp_nvram.o
 common-obj-$(CONFIG_MAC_NVRAM) += mac_nvram.o
 obj-$(CONFIG_PSERIES) += spapr_nvram.o
+obj-$(CONFIG_NRF51_SOC) += nrf51_nvm.o
include/hw/nvram/nrf51_nvm.h new file +64 -0

64 lines not on this page · raw message

hw/nvram/nrf51_nvm.c new file +381 -0

381 lines not on this page · raw message

Signature · 1 line
2.20.1
#9 lore raw

PATCH v2 4/5 arm: Instantiate NRF51 special NVM's and NVMC

In reply to Stefan Hajnoczi

From: Steffen Görtz <[email protected]>

Instantiates UICR, FICR, FLASH and NVMC in nRF51 SOC.

---
 include/hw/arm/nrf51_soc.h |  2 ++
 hw/arm/nrf51_soc.c         | 41 +++++++++++++++++++++++++++-----------
 2 files changed, 31 insertions(+), 12 deletions(-)
include/hw/arm/nrf51_soc.h +2 -0
index e06f0304b4..c8c91cf9ff 100644
@@ -15,6 +15,7 @@
 #include "hw/char/nrf51_uart.h"
 #include "hw/misc/nrf51_rng.h"
 #include "hw/gpio/nrf51_gpio.h"
+#include "hw/nvram/nrf51_nvm.h"
 #include "hw/timer/nrf51_timer.h"
 
 #define TYPE_NRF51_SOC "nrf51-soc"
@@ -32,6 +33,7 @@ typedef struct NRF51State {
 
     NRF51UARTState uart;
     NRF51RNGState rng;
+    NRF51NVMState nvm;
     NRF51GPIOState gpio;
     NRF51TimerState timer[NRF51_NUM_TIMERS];
 
hw/arm/nrf51_soc.c +29 -12

71 lines not on this page · raw message

Signature · 1 line
2.20.1
#10 lore raw

PATCH v2 5/5 tests/microbit-test: Add tests for nRF51 NVMC

In reply to Stefan Hajnoczi

From: Steffen Görtz <[email protected]>

---
 tests/microbit-test.c | 97 +++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 97 insertions(+)
tests/microbit-test.c +97 -0
index 0c125535f6..d3edd38643 100644
@@ -20,8 +20,104 @@
 
 #include "hw/arm/nrf51.h"
 #include "hw/gpio/nrf51_gpio.h"
+#include "hw/nvram/nrf51_nvm.h"
 #include "hw/timer/nrf51_timer.h"
 
+#define FLASH_SIZE          (256 * NRF51_PAGE_SIZE)
+
+static void fill_and_erase(hwaddr base, hwaddr size, uint32_t address_reg)
+{
+    hwaddr i;
+
+    /* Erase Page */
+    writel(NRF51_NVMC_BASE + NRF51_NVMC_CONFIG, 0x02);
+    writel(NRF51_NVMC_BASE + address_reg, base);
+    writel(NRF51_NVMC_BASE + NRF51_NVMC_CONFIG, 0x00);
+
+    /* Check memory */
+    for (i = 0; i < size / 4; i++) {
+        g_assert_cmpuint(readl(base + i * 4), ==, 0xFFFFFFFF);
+    }
+
+    /* Fill memory */
+    writel(NRF51_NVMC_BASE + NRF51_NVMC_CONFIG, 0x01);
+    for (i = 0; i < size / 4; i++) {
+        writel(base + i * 4, i);
+        g_assert_cmpuint(readl(base + i * 4), ==, i);
+    }
+    writel(NRF51_NVMC_BASE + NRF51_NVMC_CONFIG, 0x00);
+}
+
+static void test_nrf51_nvmc(void)
+{
+    uint32_t value;
+    hwaddr i;
+
+    /* Test always ready */
+    value = readl(NRF51_NVMC_BASE + NRF51_NVMC_READY);
+    g_assert_cmpuint(value & 0x01, ==, 0x01);
+
+    /* Test write-read config register */
+    writel(NRF51_NVMC_BASE + NRF51_NVMC_CONFIG, 0x03);
+    g_assert_cmpuint(readl(NRF51_NVMC_BASE + NRF51_NVMC_CONFIG), ==, 0x03);
+    writel(NRF51_NVMC_BASE + NRF51_NVMC_CONFIG, 0x00);
+    g_assert_cmpuint(readl(NRF51_NVMC_BASE + NRF51_NVMC_CONFIG), ==, 0x00);
+
+    /* Test PCR0 */
+    fill_and_erase(NRF51_FLASH_BASE, NRF51_PAGE_SIZE, NRF51_NVMC_ERASEPCR0);
+    fill_and_erase(NRF51_FLASH_BASE + NRF51_PAGE_SIZE,
+                   NRF51_PAGE_SIZE, NRF51_NVMC_ERASEPCR0);
+
+    /* Test PCR1 */
+    fill_and_erase(NRF51_FLASH_BASE, NRF51_PAGE_SIZE, NRF51_NVMC_ERASEPCR1);
+    fill_and_erase(NRF51_FLASH_BASE + NRF51_PAGE_SIZE,
+                   NRF51_PAGE_SIZE, NRF51_NVMC_ERASEPCR1);
+
+    /* Erase all */
+    writel(NRF51_NVMC_BASE + NRF51_NVMC_CONFIG, 0x02);
+    writel(NRF51_NVMC_BASE + NRF51_NVMC_ERASEALL, 0x01);
+    writel(NRF51_NVMC_BASE + NRF51_NVMC_CONFIG, 0x00);
+
+    writel(NRF51_NVMC_BASE + NRF51_NVMC_CONFIG, 0x01);
+    for (i = 0; i < FLASH_SIZE / 4; i++) {
+        writel(NRF51_FLASH_BASE + i * 4, i);
+        g_assert_cmpuint(readl(NRF51_FLASH_BASE + i * 4), ==, i);
+    }
+    writel(NRF51_NVMC_BASE + NRF51_NVMC_CONFIG, 0x00);
+
+    writel(NRF51_NVMC_BASE + NRF51_NVMC_CONFIG, 0x02);
+    writel(NRF51_NVMC_BASE + NRF51_NVMC_ERASEALL, 0x01);
+    writel(NRF51_NVMC_BASE + NRF51_NVMC_CONFIG, 0x00);
+
+    for (i = 0; i < FLASH_SIZE / 4; i++) {
+        g_assert_cmpuint(readl(NRF51_FLASH_BASE + i * 4), ==, 0xFFFFFFFF);
+    }
+
+    /* Erase UICR */
+    writel(NRF51_NVMC_BASE + NRF51_NVMC_CONFIG, 0x02);
+    writel(NRF51_NVMC_BASE + NRF51_NVMC_ERASEUICR, 0x01);
+    writel(NRF51_NVMC_BASE + NRF51_NVMC_CONFIG, 0x00);
+
+    for (i = 0; i < NRF51_UICR_SIZE / 4; i++) {
+        g_assert_cmpuint(readl(NRF51_UICR_BASE + i * 4), ==, 0xFFFFFFFF);
+    }
+
+    writel(NRF51_NVMC_BASE + NRF51_NVMC_CONFIG, 0x01);
+    for (i = 0; i < NRF51_UICR_SIZE / 4; i++) {
+        writel(NRF51_UICR_BASE + i * 4, i);
+        g_assert_cmpuint(readl(NRF51_UICR_BASE + i * 4), ==, i);
+    }
+    writel(NRF51_NVMC_BASE + NRF51_NVMC_CONFIG, 0x00);
+
+    writel(NRF51_NVMC_BASE + NRF51_NVMC_CONFIG, 0x02);
+    writel(NRF51_NVMC_BASE + NRF51_NVMC_ERASEUICR, 0x01);
+    writel(NRF51_NVMC_BASE + NRF51_NVMC_CONFIG, 0x00);
+
+    for (i = 0; i < NRF51_UICR_SIZE / 4; i++) {
+        g_assert_cmpuint(readl(NRF51_UICR_BASE + i * 4), ==, 0xFFFFFFFF);
+    }
+}
+
 static void test_nrf51_gpio(void)
 {
     size_t i;
@@ -246,6 +342,7 @@ int main(int argc, char **argv)
     global_qtest = qtest_initf("-machine microbit");
 
     qtest_add_func("/microbit/nrf51/gpio", test_nrf51_gpio);
+    qtest_add_func("/microbit/nrf51/nvmc", test_nrf51_nvmc);
     qtest_add_func("/microbit/nrf51/timer", test_nrf51_timer);
 
     ret = g_test_run();
Signature · 1 line
2.20.1
#11 lore raw

Re: [Qemu-arm] [PATCH v2 5/5] tests/microbit-test: Add tests for nRF51 NVMC

Quoted 19 lines · Stefan Hajnoczi

From: Steffen Görtz <[email protected]>

---
 tests/microbit-test.c | 97 +++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 97 insertions(+)
tests/microbit-test.c +1 -0
index 0c125535f6..d3edd38643 100644
@@ -20,8 +20,104 @@
 
 #include "hw/arm/nrf51.h"
 #include "hw/gpio/nrf51_gpio.h"
+#include "hw/nvram/nrf51_nvm.h"
 #include "hw/timer/nrf51_timer.h"
 

tests/microbit-test.c line 35

Quoted 10 lines
tests/microbit-test.c +10 -0
+#define FLASH_SIZE          (256 * NRF51_PAGE_SIZE)
+
+static void fill_and_erase(hwaddr base, hwaddr size, uint32_t address_reg)
+{
+    hwaddr i;
+
+    /* Erase Page */
+    writel(NRF51_NVMC_BASE + NRF51_NVMC_CONFIG, 0x02);
+    writel(NRF51_NVMC_BASE + address_reg, base);
+    writel(NRF51_NVMC_BASE + NRF51_NVMC_CONFIG, 0x00);

I think you might have a conflict here with Julia's patch "tests/microbit-test: Make test independent of global_qtest". Could you please have a look (and e.g. include Julia's patches in your series)?

Thanks,
Thomas

#12 lore raw
In reply to Stefan Hajnoczi
Quoted 11 lines · Stefan Hajnoczi

v2:
* Add Patch 2 to call memory_region_flush_rom_device() from pflash devices [Peter]

This series adds the Non-Volatile Memory Controller, which controls access to the User Information Control Registers (UICR), Factory Information Control Registers (FICR), and flash memory.

This is the last piece of microbit work needed to make basic programs like Micropython "Hello world" work under QEMU.

I've applied patches 1, 3, 4 and 5 to target-arm.next, thanks. Philippe has review comments on 2 and it's not really microbit related anyway.

thanks
-- PMM

#13 lore raw
Quoted 11 lines · Peter Maydell
Quoted 9 lines · Stefan Hajnoczi

v2:
* Add Patch 2 to call memory_region_flush_rom_device() from pflash devices [Peter]

This series adds the Non-Volatile Memory Controller, which controls access to the User Information Control Registers (UICR), Factory Information Control Registers (FICR), and flash memory.

Quoted 6 lines
Quoted 2 lines

This is the last piece of microbit work needed to make basic programs like Micropython "Hello world" work under QEMU.

I've applied patches 1, 3, 4 and 5 to target-arm.next, thanks. Philippe has review comments on 2 and it's not really microbit related anyway.

As discussed on IRC, I'm dropping patch 5, because it conflicts with Julia's patch that drops the usage of global_qtest.

thanks
-- PMM