Skip to contentSkip to conversation

arm: Add nRF51 SoC and micro:bit machine

PATCH Superseded Joel Stanley 3 participants 9 messages Aug 16, 2018 – Aug 29, 2018

v5 of 6 Superseded Latest is v6 →

lore
Share & tools
lore
#0 lore raw

v5 cover arm: Add nRF51 SoC and micro:bit machine

v5: Change back to ARMv7M from ARMMProfile
v4: Fix number of IRQs
v3: Rebase on Stefan's cortex-m0 series
v2: Addresses review from Peter and Stefan

Based-on: [email protected]

This short series implements a minimal definition of the Nordic Semiconductor nRF51, a Cortex-M0 ARM SoC, and the BBC micro:bit, a machine that will use this SoC.

This work will serve as the base for our Google Summer of Code and Outreachy interns who will work on implementing a number of features on top of this base.

I've tested this with a microbit micropython firmware, and checked that it starts running by looking at it with gdb.

I chose to keep the nrf51 and the microbit seperate, to not confuse the peripherals that are on the microbit but are not part of the nrf51, and vice versa.

Joel Stanley (3):
MAINTAINERS: Add NRF51 entry
arm: Add Nordic Semiconductor nRF51 SoC
arm: Add BBC micro:bit machine

 MAINTAINERS                     |   8 +++
 default-configs/arm-softmmu.mak |   1 +
 hw/arm/Makefile.objs            |   1 +
 hw/arm/microbit.c               |  54 +++++++++++++++
 hw/arm/nrf51_soc.c              | 119 ++++++++++++++++++++++++++++++++
 include/hw/arm/nrf51_soc.h      |  42 +++++++++++
 6 files changed, 225 insertions(+)
 create mode 100644 hw/arm/microbit.c
 create mode 100644 hw/arm/nrf51_soc.c
 create mode 100644 include/hw/arm/nrf51_soc.h
Signature · 1 line
2.17.1
#1 lore raw

PATCH v5 1/3 MAINTAINERS: Add NRF51 entry

This contains the NRF51, and the machine that uses it, the BBC micro:bit.

---
v3:
fix spelling of mailing list
add stefan's reviewed-by

---
 MAINTAINERS | 8 ++++++++
 1 file changed, 8 insertions(+)
MAINTAINERS +8 -0
index c48d9271cf15..5a0d2e327d4a 100644
@@ -656,6 +656,14 @@ F: include/hw/*/*aspeed*
 F: hw/net/ftgmac100.c
 F: include/hw/net/ftgmac100.h
 
+NRF51
+M: Joel Stanley <[email protected]>
+L: [email protected]
+S: Maintained
+F: hw/arm/nrf51_soc.c
+F: hw/arm/microbit.c
+F: include/hw/arm/nrf51_soc.h
+
 CRIS Machines
 -------------
 Axis Dev88
Signature · 1 line
2.17.1
#2 lore raw

PATCH v5 2/3 arm: Add Nordic Semiconductor nRF51 SoC

In reply to Joel Stanley

The nRF51 is a Cortex-M0 microcontroller with an on-board radio module, plus other common ARM SoC peripherals.

http://infocenter.nordicsemi.com/pdf/nRF51_RM_v3.0.pdf

This defines a basic model of the CPU and memory, with no peripherals implemented at this stage.

---
v2:
put memory as struct fileds in state structure
pass OBJECT(s) as owner, not NULL
Add missing addresses for ficr
Fix flash and sram sizes for microbit
Embed cpu object in state object an initalise it without use of armv7m_init Link to datasheet
v3:
rebase nrf51 on m0 changes
remove unused kernel_filename
clarify flash and sram size
make flash and sram size properties of the soc state
v4:
set the number of interrupts to 32
v5:
move back to armv7m calls, as v4 of Stefan's patch removed the m_profile changes

---
 default-configs/arm-softmmu.mak |   1 +
 hw/arm/Makefile.objs            |   1 +
 hw/arm/nrf51_soc.c              | 119 ++++++++++++++++++++++++++++++++
 include/hw/arm/nrf51_soc.h      |  42 +++++++++++
 4 files changed, 163 insertions(+)
 create mode 100644 hw/arm/nrf51_soc.c
 create mode 100644 include/hw/arm/nrf51_soc.h
default-configs/arm-softmmu.mak +1 -0
index 311584fd74eb..2ff27c2e1d5a 100644
@@ -101,6 +101,7 @@ CONFIG_STM32F2XX_SYSCFG=y
 CONFIG_STM32F2XX_ADC=y
 CONFIG_STM32F2XX_SPI=y
 CONFIG_STM32F205_SOC=y
+CONFIG_NRF51_SOC=y
 
 CONFIG_CMSDK_APB_TIMER=y
 CONFIG_CMSDK_APB_UART=y
hw/arm/Makefile.objs +1 -0
index 2902f47b4c4c..ae4e20373b9e 100644
@@ -37,3 +37,4 @@ obj-$(CONFIG_IOTKIT) += iotkit.o
 obj-$(CONFIG_FSL_IMX7) += fsl-imx7.o mcimx7d-sabre.o
 obj-$(CONFIG_ARM_SMMUV3) += smmu-common.o smmuv3.o
 obj-$(CONFIG_FSL_IMX6UL) += fsl-imx6ul.o mcimx6ul-evk.o
+obj-$(CONFIG_NRF51_SOC) += nrf51_soc.o
hw/arm/nrf51_soc.c new file +119 -0

119 lines not on this page · raw message

include/hw/arm/nrf51_soc.h new file +42 -0

42 lines not on this page · raw message

Signature · 1 line
2.17.1
#3 lore raw

Re: [Qemu-arm] [PATCH v5 2/3] arm: Add Nordic Semiconductor nRF51 SoC

Quoted 90 lines · Joel Stanley

The nRF51 is a Cortex-M0 microcontroller with an on-board radio module, plus other common ARM SoC peripherals.

http://infocenter.nordicsemi.com/pdf/nRF51_RM_v3.0.pdf

This defines a basic model of the CPU and memory, with no peripherals implemented at this stage.

---
v2:
put memory as struct fileds in state structure
pass OBJECT(s) as owner, not NULL
Add missing addresses for ficr
Fix flash and sram sizes for microbit
Embed cpu object in state object an initalise it without use of armv7m_init Link to datasheet
v3:
rebase nrf51 on m0 changes
remove unused kernel_filename
clarify flash and sram size
make flash and sram size properties of the soc state
v4:
set the number of interrupts to 32
v5:
move back to armv7m calls, as v4 of Stefan's patch removed the m_profile changes

---
 default-configs/arm-softmmu.mak |   1 +
 hw/arm/Makefile.objs            |   1 +
 hw/arm/nrf51_soc.c              | 119 ++++++++++++++++++++++++++++++++
 include/hw/arm/nrf51_soc.h      |  42 +++++++++++
 4 files changed, 163 insertions(+)
 create mode 100644 hw/arm/nrf51_soc.c
 create mode 100644 include/hw/arm/nrf51_soc.h
default-configs/arm-softmmu.mak +1 -0
index 311584fd74eb..2ff27c2e1d5a 100644
@@ -101,6 +101,7 @@ CONFIG_STM32F2XX_SYSCFG=y
 CONFIG_STM32F2XX_ADC=y
 CONFIG_STM32F2XX_SPI=y
 CONFIG_STM32F205_SOC=y
+CONFIG_NRF51_SOC=y
 
 CONFIG_CMSDK_APB_TIMER=y
 CONFIG_CMSDK_APB_UART=y
hw/arm/Makefile.objs +1 -0
index 2902f47b4c4c..ae4e20373b9e 100644
@@ -37,3 +37,4 @@ obj-$(CONFIG_IOTKIT) += iotkit.o
 obj-$(CONFIG_FSL_IMX7) += fsl-imx7.o mcimx7d-sabre.o
 obj-$(CONFIG_ARM_SMMUV3) += smmu-common.o smmuv3.o
 obj-$(CONFIG_FSL_IMX6UL) += fsl-imx6ul.o mcimx6ul-evk.o
+obj-$(CONFIG_NRF51_SOC) += nrf51_soc.o
hw/arm/nrf51_soc.c new file +27 -0
new file mode 100644
index 000000000000..9f9649c7807d
@@ -0,0 +1,119 @@
+/*
+ * Nordic Semiconductor nRF51 SoC
+ * http://infocenter.nordicsemi.com/pdf/nRF51_RM_v3.0.1.pdf
+ *
+ * Copyright 2018 Joel Stanley <[email protected]>
+ *
+ * This code is licensed under the GPL version 2 or later.  See
+ * the COPYING file in the top-level directory.
+ */
+
+#include "qemu/osdep.h"
+#include "qapi/error.h"
+#include "qemu-common.h"
+#include "hw/arm/arm.h"
+#include "hw/sysbus.h"
+#include "hw/boards.h"
+#include "hw/devices.h"
+#include "hw/misc/unimp.h"
+#include "exec/address-spaces.h"
+#include "sysemu/sysemu.h"
+#include "qemu/log.h"
+#include "cpu.h"
+
+#include "hw/arm/nrf51_soc.h"
+
+#define IOMEM_BASE      0x40000000
+#define IOMEM_SIZE      0x20000000

hw/arm/nrf51_soc.c line 37

Quoted 10 lines
hw/arm/nrf51_soc.c new file +10 -0
+
+#define FICR_BASE       0x10000000
+#define FICR_SIZE       0x000000fc
+
+#define FLASH_BASE      0x00000000
+#define SRAM_BASE       0x20000000
+
+/* The size and base is for the NRF51822 part. If other parts
+ * are supported in the future, add a sub-class of NRF51SoC for
+ * the specific variants */

/* and */ both on a line of their own for multiline comments, please.

Quoted 6 lines
hw/arm/nrf51_soc.c new file +6 -0
+#define NRF51822_FLASH_SIZE     (256 * 1024)
+#define NRF51822_SRAM_SIZE      (16 * 1024)
+
+static void nrf51_soc_realize(DeviceState *dev_soc, Error **errp)
+{
+    NRF51State *s = NRF51_SOC(dev_soc);

hw/arm/nrf51_soc.c line 53

Quoted 10 lines
hw/arm/nrf51_soc.c new file +10 -0
+    Error *err = NULL;
+
+    if (!s->board_memory) {
+        error_setg(errp, "memory property was not set");
+        return;
+    }
+
+    object_property_set_link(OBJECT(&s->cpu), OBJECT(&s->container), "memory",
+            &err);
+    object_property_set_bool(OBJECT(&s->cpu), true, "realized", &err);

You need to check whether you get an error after the first call; you can't stack up multiple calls with a single error check like this. See the comment in include/qapi/error.h (this is a violation of the final "Do *not*" in that comment).

hw/arm/nrf51_soc.c line 63

Quoted 10 lines
hw/arm/nrf51_soc.c new file +10 -0
+
+    memory_region_add_subregion_overlap(&s->container, 0, s->board_memory, -1);
+
+    memory_region_init_ram(&s->flash, OBJECT(s), "nrf51.flash", s->flash_size,
+            &err);
+    if (err) {
+        error_propagate(errp, err);
+        return;
+    }
+    memory_region_set_readonly(&s->flash, true);

memory_region_init_rom() is equivalent to memory_region_init_ram() + memory_region_set_readonly().

hw/arm/nrf51_soc.c line 66

Quoted 3 lines
hw/arm/nrf51_soc.c new file +3 -0
+    memory_region_add_subregion(&s->container, FLASH_BASE, &s->flash);
+
+    memory_region_init_ram(&s->sram, NULL, "nrf51.sram", s->sram_size, &err);

Assuming this is the main (largest) ram block in the system, it should be created with memory_region_allocate_system_memory().

hw/arm/nrf51_soc.c line 75

Quoted 9 lines
hw/arm/nrf51_soc.c new file +9 -0
+    if (err) {
+        error_propagate(errp, err);
+        return;
+    }
+    memory_region_add_subregion(&s->container, SRAM_BASE, &s->sram);
+
+    create_unimplemented_device("nrf51_soc.io", IOMEM_BASE, IOMEM_SIZE);
+    create_unimplemented_device("nrf51_soc.ficr", FICR_BASE, FICR_SIZE);
+    create_unimplemented_device("nrf51_soc.private", 0xF0000000, 0x10000000);

This looks like some base/size #defines for the 'private' region would be useful.

hw/arm/nrf51_soc.c line 86

Quoted 11 lines
hw/arm/nrf51_soc.c new file +11 -0
+}
+
+static void nrf51_soc_init(Object *obj)
+{
+    NRF51State *s = NRF51_SOC(obj);
+
+    memory_region_init(&s->container, obj, "nrf51-container", UINT64_MAX);
+
+    object_initialize(&s->cpu, sizeof(s->cpu), TYPE_ARMV7M);
+    object_property_add_child(OBJECT(s), "armv6m", OBJECT(&s->cpu), &error_abort);
+    qdev_set_parent_bus(DEVICE(&s->cpu), sysbus_get_default());

Use sysbus_init_child_obj() rather than this sequence of 3 calls.

hw/arm/nrf51_soc.c line 97

Quoted 11 lines
hw/arm/nrf51_soc.c new file +11 -0
+    qdev_prop_set_string(DEVICE(&s->cpu), "cpu-type", ARM_CPU_TYPE_NAME("cortex-m0"));
+    qdev_prop_set_uint32(DEVICE(&s->cpu), "num-irq", 32);
+}
+
+static Property nrf51_soc_properties[] = {
+    DEFINE_PROP_LINK("memory", NRF51State, board_memory, TYPE_MEMORY_REGION,
+                     MemoryRegion *),
+    DEFINE_PROP_UINT32("sram-size", NRF51State, sram_size, NRF51822_SRAM_SIZE),
+    DEFINE_PROP_UINT32("flash-size", NRF51State, flash_size, NRF51822_FLASH_SIZE),
+    DEFINE_PROP_END_OF_LIST(),
+};

Ah, I was wondering how flash_size was handled when I looked at patch 3.

Instead of your patch 3 board model code reaching into the internals of the NRF51State struct to pull out the flash_size field, the board code should set this property on the object it creates, and then it knows how big the flash it's asked for is and can pass that value also to the armv7m_load_kernel() function.

Quoted 6 lines
include/hw/arm/nrf51_soc.h new file +3 -0
@@ -0,0 +1,42 @@
+/*
+ * Nordic Semiconductor nRF51  SoC
+ *

include/hw/arm/nrf51_soc.h line 13

Quoted 10 lines
include/hw/arm/nrf51_soc.h new file +10 -0
+ * Copyright 2018 Joel Stanley <[email protected]>
+ *
+ * This code is licensed under the GPL version 2 or later.  See
+ * the COPYING file in the top-level directory.
+ */
+
+#ifndef NRF51_SOC_H
+#define NRF51_SOC_H
+
+#include "qemu/osdep.h"

Our convention with osdep.h is that:
* all .c files must include it as their first header
* .h files must never include it

thanks
-- PMM

#4 lore raw

Re: [Qemu-devel] [PATCH v5 2/3] arm: Add Nordic Semiconductor nRF51 SoC

Hi Peter,

Quoted 10 lines · Peter Maydell

hw/arm/nrf51_soc.c line 97

Quoted 7 lines
hw/arm/nrf51_soc.c new file +7 -0
+static Property nrf51_soc_properties[] = {
+    DEFINE_PROP_LINK("memory", NRF51State, board_memory, TYPE_MEMORY_REGION,
+                     MemoryRegion *),
+    DEFINE_PROP_UINT32("sram-size", NRF51State, sram_size, NRF51822_SRAM_SIZE),
+    DEFINE_PROP_UINT32("flash-size", NRF51State, flash_size, NRF51822_FLASH_SIZE),
+    DEFINE_PROP_END_OF_LIST(),
+};

Ah, I was wondering how flash_size was handled when I looked at patch 3.

Quoted 5 lines

Instead of your patch 3 board model code reaching into the internals of the NRF51State struct to pull out the flash_size field, the board code should set this property on the object it creates, and then it knows how big the flash it's asked for is and can pass that value also to the armv7m_load_kernel() function.

Thanks for the review. I've sorted out the other comments relating to this patch, but I wanted to discuss this one.

I agree that it would be neater to do this. I didn't as the flash is part of the NRF51822 SoC, opposed to some external flash that is on the microbit board and connected to the SoC. This is mentioned in the comment at the start of the file:

/*
* The size and base is for the NRF51822 part. If other parts
* are supported in the future, add a sub-class of NRF51SoC for
* the specific variants
*/

What would you prefer we do here?

Cheers,

Joel

#5 lore raw

Re: [Qemu-arm] [PATCH v5 2/3] arm: Add Nordic Semiconductor nRF51 SoC

Quoted 10 lines · Joel Stanley

I agree that it would be neater to do this. I didn't as the flash is part of the NRF51822 SoC, opposed to some external flash that is on the microbit board and connected to the SoC. This is mentioned in the comment at the start of the file:

/*
* The size and base is for the NRF51822 part. If other parts
* are supported in the future, add a sub-class of NRF51SoC for
* the specific variants
*/

Oh, right. I'd assumed it wasn't fixed because it was specified as a property on the object.

Quoted 1 line

What would you prefer we do here?

I don't see anything that seems like the really obvious clean thing, so I suggest just doing something that seems reasonable. I think Steffen's patchset also had a change in this area, which might affect the decision.

thanks
-- PMM

Reviewed-by on v5 2/3
#6 lore raw

Re: [Qemu-arm] [PATCH v5 2/3] arm: Add Nordic Semiconductor nRF51 SoC

In reply to Joel Stanley
Quoted 27 lines · Joel Stanley

The nRF51 is a Cortex-M0 microcontroller with an on-board radio module, plus other common ARM SoC peripherals.

http://infocenter.nordicsemi.com/pdf/nRF51_RM_v3.0.pdf

This defines a basic model of the CPU and memory, with no peripherals implemented at this stage.

---
v2:
put memory as struct fileds in state structure
pass OBJECT(s) as owner, not NULL
Add missing addresses for ficr
Fix flash and sram sizes for microbit
Embed cpu object in state object an initalise it without use of armv7m_init Link to datasheet
v3:
rebase nrf51 on m0 changes
remove unused kernel_filename
clarify flash and sram size
make flash and sram size properties of the soc state
v4:
set the number of interrupts to 32
v5:
move back to armv7m calls, as v4 of Stefan's patch removed the m_profile changes

Quoted 8 lines
---
 default-configs/arm-softmmu.mak |   1 +
 hw/arm/Makefile.objs            |   1 +
 hw/arm/nrf51_soc.c              | 119 ++++++++++++++++++++++++++++++++
 include/hw/arm/nrf51_soc.h      |  42 +++++++++++
 4 files changed, 163 insertions(+)
 create mode 100644 hw/arm/nrf51_soc.c
 create mode 100644 include/hw/arm/nrf51_soc.h

Besides what has already been discussed:

#7 lore raw

PATCH v5 3/3 arm: Add BBC micro:bit machine

In reply to Joel Stanley

This adds the base for a machine model of the BBC micro:bit:

https://en.wikipedia.org/wiki/Micro_Bit

This is a system with a nRF51 SoC containing the main processor, with various peripherals on board.

---
v2:
- Instead of setting kernel filename property, load the image directly
- Add link to hardware overview website
v3:
- Rebase microbit on m0 changes
- Remove hard-coded flash size and retrieve from the soc
- Add Stefan's reviewed-by
v5:
- move back to armv7m calls, as v4 of Stefan's patch removed the m_profile changes

---
 hw/arm/Makefile.objs |  2 +-
 hw/arm/microbit.c    | 54 ++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 55 insertions(+), 1 deletion(-)
 create mode 100644 hw/arm/microbit.c
hw/arm/Makefile.objs +1 -1
index ae4e20373b9e..5f88062c666d 100644
@@ -37,4 +37,4 @@ obj-$(CONFIG_IOTKIT) += iotkit.o
 obj-$(CONFIG_FSL_IMX7) += fsl-imx7.o mcimx7d-sabre.o
 obj-$(CONFIG_ARM_SMMUV3) += smmu-common.o smmuv3.o
 obj-$(CONFIG_FSL_IMX6UL) += fsl-imx6ul.o mcimx6ul-evk.o
-obj-$(CONFIG_NRF51_SOC) += nrf51_soc.o
+obj-$(CONFIG_NRF51_SOC) += nrf51_soc.o microbit.o
hw/arm/microbit.c new file +54 -0

54 lines not on this page · raw message

Signature · 1 line
2.17.1
#8 lore raw
In reply to Joel Stanley
Quoted 10 lines · Joel Stanley

v5: Change back to ARMv7M from ARMMProfile
v4: Fix number of IRQs
v3: Rebase on Stefan's cortex-m0 series
v2: Addresses review from Peter and Stefan

Based-on: [email protected]

This short series implements a minimal definition of the Nordic Semiconductor nRF51, a Cortex-M0 ARM SoC, and the BBC micro:bit, a machine that will use this SoC.

Hi Joel; it looks like my review of v4 crossed in the mail with your sending out v5. I think that my comments on the v4 patch 3 still applies to v5, though (as does my reviwed-by tag on patch 1).

thanks
-- PMM