Skip to contentSkip to conversation

Add NRF51 SOC non-volatile memory controller

RFC In review Steffen Görtz 2 participants 6 messages Jul 12, 2018 – Jul 18, 2018

lore
Share & tools
lore
#0 lore raw

v3 cover Add NRF51 SOC non-volatile memory controller

Add some non-volatile memories and a non-volatile memory controller for the nRF51.
Furthermore, a testsuite for the bbc:microbit and nrf51 soc was added.

Examination of the real device showed that
NVMs remained unchanged when the write/erase enabled
bits are not set in the controller, so we can
safely ignore all writes.
More: https://github.com/douzepouze/gsoc18-qemu/blob/master/notes.md#test-nvmc-behavior-out-of-micropython-repl

The CODE/FLASH NVM is not currently included in this peripheral. It is hosted in the SOC and must be read-only to provide an accurate model.

Steffen Görtz (2):
arm: Add NRF51 SOC non-volatile memory controller
tests: Add bbc:microbit / nRF51 test suite

 hw/nvram/Makefile.objs       |   1 +
 hw/nvram/nrf51_nvm.c         | 401 +++++++++++++++++++++++++++++++++++
 include/hw/arm/nrf51_soc.h   |   2 +-
 include/hw/nvram/nrf51_nvm.h |  56 +++++
 tests/Makefile.include       |   2 +
 tests/microbit-test.c        | 118 +++++++++++
 6 files changed, 579 insertions(+), 1 deletion(-)
 create mode 100644 hw/nvram/nrf51_nvm.c
 create mode 100644 include/hw/nvram/nrf51_nvm.h
 create mode 100644 tests/microbit-test.c
Signature · 1 line
2.18.0
#1 lore raw

RFC v3 1/2 arm: Add NRF51 SOC non-volatile memory controller

Changes in V3:
- NVMs consolidated in one file
- Removed bitfields
- Add VMState
- Add reset

Changes in V1/V2:
- Code style changes

---
 hw/nvram/Makefile.objs       |   1 +
 hw/nvram/nrf51_nvm.c         | 401 +++++++++++++++++++++++++++++++++++
 include/hw/arm/nrf51_soc.h   |   2 +-
 include/hw/nvram/nrf51_nvm.h |  56 +++++
 4 files changed, 459 insertions(+), 1 deletion(-)
 create mode 100644 hw/nvram/nrf51_nvm.c
 create mode 100644 include/hw/nvram/nrf51_nvm.h
hw/nvram/Makefile.objs +1 -0
index a912d25391..3f978e6212 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
hw/nvram/nrf51_nvm.c new file +401 -0

401 lines not on this page · raw message

include/hw/arm/nrf51_soc.h +1 -1
index 35dd71c3db..9c32d22abe 100644
@@ -1,5 +1,5 @@
 /*
- * Nordic Semiconductor nRF51  SoC
+ * Nordic Semiconductor nRF51 SoC
  *
  * Copyright 2018 Joel Stanley <[email protected]>
  *
include/hw/nvram/nrf51_nvm.h new file +56 -0

56 lines not on this page · raw message

Signature · 1 line
2.18.0
#2 lore raw

Re: [Qemu-devel] [RFC v3 1/2] arm: Add NRF51 SOC non-volatile memory controller

Quoted 11 lines · Steffen Görtz

Changes in V3:
- NVMs consolidated in one file
- Removed bitfields
- Add VMState
- Add reset

Changes in V1/V2:
- Code style changes

---

Everything above '---' goes into the commit description and is permanently in the git log. Changelogs are not useful in the git log since their context (the earlier revisions and the discussion) is missing. Please put changelogs below '---' as anything there doesn't get included in the commit description.

hw/nvram/nrf51_nvm.c line 100

Quoted 3 lines
hw/nvram/nrf51_nvm.c new file +3 -0
+static uint64_t ficr_read(void *opaque, hwaddr offset, unsigned int size)
+{
+    if (offset > (ARRAY_SIZE(ficr_content) - size)) {

Bug alert, the semantic types look suspicious:

bytes_t > num_elements_t - bytes_t

You cannot subtract a byte count from the number of elements in an array.

Did you mean:

  if (offset >= sizeof(ficr_content)) {

MemoryRegion was already declared with FICR_SIZE length, so nothing should ever call ficr_read() with an out-of-bounds offset.

Instead of defining FICR_SIZE, which could become out-of-sync with the definition of ficr_content[], I would use sizeof(ficr_content) as the MemoryRegion length. This way the code is guaranteed to be safe even if the size of ficr_content[] is changed in the future. No if statement would be necessary.

hw/nvram/nrf51_nvm.c line 209

Quoted 11 lines
hw/nvram/nrf51_nvm.c new file +11 -0
+static uint64_t uicr_read(void *opaque, hwaddr offset, unsigned int size)
+{
+    Nrf51NVMState *s = NRF51_NVM(opaque);
+
+    offset >>= 2;
+
+    if (offset >= ARRAY_SIZE(s->uicr_content)) {
+        qemu_log_mask(LOG_GUEST_ERROR,
+                "%s: bad read offset 0x%" HWADDR_PRIx "\n", __func__, offset);
+        return 0;
+    }

The same applies to NRF51_UICR_SIZE. It's simpler to use sizeof(s->uicr_content) instead of defining a separate constant and then using an if statement to check the offset.

Quoted 5 lines
hw/nvram/nrf51_nvm.c new file +5 -0
+static void nrf51_nvm_init(Object *obj)
+{
+    Nrf51NVMState *s = NRF51_NVM(obj);
+    SysBusDevice *sbd = SYS_BUS_DEVICE(obj);
+

hw/nvram/nrf51_nvm.c line 317

Quoted 10 lines
hw/nvram/nrf51_nvm.c new file +10 -0
+    memory_region_init_io(&s->mmio, obj, &io_ops, s, "nrf51_soc.nvmc",
+            NRF51_NVMC_SIZE);
+    sysbus_init_mmio(sbd, &s->mmio);
+
+    memory_region_init_io(&s->ficr, NULL, &ficr_ops, s, "nrf51_soc.ficr",
+            NRF51_FICR_SIZE);
+    memory_region_set_readonly(&s->ficr, true);
+    sysbus_init_mmio(sbd, &s->ficr);
+
+    memcpy(s->uicr_content, uicr_fixture, sizeof(s->uicr_content));

uicr_content[] persists across device reset? Doing it that way seems fine to me, but I wanted to check that it's what you intended.

hw/nvram/nrf51_nvm.c line 354

Quoted 7 lines
hw/nvram/nrf51_nvm.c new file +7 -0
+static void nrf51_nvm_reset(DeviceState *dev)
+{
+    Nrf51NVMState *s = NRF51_NVM(dev);
+
+    s->empty_page = g_malloc(s->page_size);
+    memset(s->empty_page, 0xFF, s->page_size);
+}

This function can be called multiple times and will leak the previous s->empty_page. It's easier to allocate s->empty_page in nrf51_nvm_realize() instead.

hw/nvram/nrf51_nvm.c line 359

Quoted 5 lines
hw/nvram/nrf51_nvm.c new file +5 -0
+
+
+static Property nrf51_nvm_properties[] = {
+    DEFINE_PROP_UINT16("page_size", Nrf51NVMState, page_size, 0x400),
+    DEFINE_PROP_UINT32("code_size", Nrf51NVMState, code_size, 0x100),

There is an intense battle between '-' and '_' for property names:

$ git grep 'DEFINE_PROP[^(]\+("[^"]\+_' | wc -l
234
$ git grep 'DEFINE_PROP[^(]\+("[^"]\+-' | wc -l
394

I like '-' is because -device foo,my-option=x is more consistent with commonly-used properties than -device foo,my_option=x, but there are plenty of properties with '_' lurking around too.

Up to you :).

include/hw/arm/nrf51_soc.h line 2

Quoted 8 lines
include/hw/arm/nrf51_soc.h +1 -1
index 35dd71c3db..9c32d22abe 100644
@@ -1,5 +1,5 @@
 /*
- * Nordic Semiconductor nRF51  SoC
+ * Nordic Semiconductor nRF51 SoC

Unrelated to this patch. Please drop.

#3 lore raw

RFC v3 2/2 tests: Add bbc:microbit / nRF51 test suite

In reply to Steffen Görtz

The microbit-test includes tests for the nRF51 NVMC peripheral and will host future nRF51 peripheral tests and board-level bbc:microbit tests.

---
 tests/Makefile.include |   2 +
 tests/microbit-test.c  | 118 +++++++++++++++++++++++++++++++++++++++++
 2 files changed, 120 insertions(+)
 create mode 100644 tests/microbit-test.c
tests/Makefile.include +2 -0
index 3c8bde4f90..622da9288f 100644
@@ -373,6 +373,7 @@ check-qtest-sparc64-y += tests/boot-serial-test$(EXESUF)
 
 check-qtest-arm-y = tests/tmp105-test$(EXESUF)
 check-qtest-arm-y += tests/ds1338-test$(EXESUF)
+check-qtest-arm-y += tests/microbit-test$(EXESUF)
 check-qtest-arm-y += tests/m25p80-test$(EXESUF)
 gcov-files-arm-y += hw/misc/tmp105.c
 check-qtest-arm-y += tests/virtio-blk-test$(EXESUF)
@@ -778,6 +779,7 @@ tests/bios-tables-test$(EXESUF): tests/bios-tables-test.o \
 tests/pxe-test$(EXESUF): tests/pxe-test.o tests/boot-sector.o $(libqos-obj-y)
 tests/tmp105-test$(EXESUF): tests/tmp105-test.o $(libqos-omap-obj-y)
 tests/ds1338-test$(EXESUF): tests/ds1338-test.o $(libqos-imx-obj-y)
+tests/microbit-test$(EXESUF): tests/microbit-test.o
 tests/m25p80-test$(EXESUF): tests/m25p80-test.o
 tests/i440fx-test$(EXESUF): tests/i440fx-test.o $(libqos-pc-obj-y)
 tests/q35-test$(EXESUF): tests/q35-test.o $(libqos-pc-obj-y)
tests/microbit-test.c new file +118 -0

118 lines not on this page · raw message

Signature · 1 line
2.18.0
#4 lore raw

Re: [Qemu-devel] [RFC v3 2/2] tests: Add bbc:microbit / nRF51 test suite

tests/microbit-test.c line 5

Quoted 11 lines · Steffen Görtz
tests/microbit-test.c new file +5 -0
new file mode 100644
index 0000000000..c502ee3976
@@ -0,0 +1,118 @@
+ /*
+ * QTest testcase for Microbit board using the Nordic Semiconductor nRF51 SoC.
+ *
+ *
+ * Copyright (c) 2018 Steffen Görtz <[email protected]>

Please add a license.

#5 lore raw
In reply to Steffen Görtz
Quoted 11 lines · Steffen Görtz

Add some non-volatile memories and a non-volatile memory controller for the nRF51.
Furthermore, a testsuite for the bbc:microbit and nrf51 soc was added.

Examination of the real device showed that
NVMs remained unchanged when the write/erase enabled
bits are not set in the controller, so we can
safely ignore all writes.
More: https://github.com/douzepouze/gsoc18-qemu/blob/master/notes.md#test-nvmc-behavior-out-of-micropython-repl

Quoted 7 lines

The CODE/FLASH NVM is not currently included in this peripheral. It is hosted in the SOC and must be read-only to provide an accurate model.

Steffen Görtz (2):
arm: Add NRF51 SOC non-volatile memory controller
tests: Add bbc:microbit / nRF51 test suite

Please include a changelog in the cover letter so that it's clear what has changed in this revision.