Skip to contentSkip to conversation

Basic skeleton of RP2040 Raspbery Pi Pico

RFC PATCH In review Alex Bennée 2 participants 12 messages Jan 10, 2022 – Jan 18, 2022 Arm/Amlogic Meson SoC support

lore
Share & tools
lore
#0 lore raw

cover Basic skeleton of RP2040 Raspbery Pi Pico

Hi,

This is a rather incomplete and basic model of the Pico Pi that I hacked up over the Christmas break. It took me a bit of faffing about to get it up and running but as it stands it will now run the first few hundred instructions of the RP2040 mask ROM before looping waiting for CLK_SYS_SELECTED to update (which it never will as it is currently unimplemented).

I don't know when I'll next get a chance to hack on it but I thought it was worth posting as a reference. If anyone has an inclination to take it further then at least the initial baby steps are done for you.

There is no rush to review this although I am curious if anyone has comments about how the model is put together.

It uses -kernel to load the user program (although it never gets there at the moment):

qemu-system-arm -d unimp,guest_errors -M raspi-pico \
--kernel ~/mysrc/pico-air.rust/pico-air.git/target/thumbv6m-none-eabi/debug/rp2040-project-template \
-display none -serial mon:stdio

Alex Bennée (6):
hw/arm: arm initial boilerplate for RP2040 SoC hw/arm: add boilerplate for machines based on the RP2040 hw/arm: wire-up memory from the Pico board and the SoC
pc-bios: add pipico mask rom (!upstream) hw/arm: add mask boot ROM logic
docs/devel: add some clarifying text for aliases

 docs/devel/memory.rst                   |  14 +-
 configs/devices/arm-softmmu/default.mak |   2 +
 include/hw/arm/rp2040.h                 |  39 +++++
 hw/arm/raspi_pico.c                     |  97 ++++++++++++
 hw/arm/rp2040.c                         | 201 ++++++++++++++++++++++++
 hw/arm/Kconfig                          |   6 +
 hw/arm/meson.build                      |   2 +
 pc-bios/pipico.rom                      | Bin 0 -> 16384 bytes
 8 files changed, 356 insertions(+), 5 deletions(-)
 create mode 100644 include/hw/arm/rp2040.h
 create mode 100644 hw/arm/raspi_pico.c
 create mode 100644 hw/arm/rp2040.c
 create mode 100644 pc-bios/pipico.rom
Signature · 1 line
2.30.2
#1 lore raw

RFC PATCH 1/6 hw/arm: arm initial boilerplate for RP2040 SoC

---
 configs/devices/arm-softmmu/default.mak |  1 +
 include/hw/arm/rp2040.h                 | 32 ++++++++++
 hw/arm/rp2040.c                         | 79 +++++++++++++++++++++++++
 hw/arm/Kconfig                          |  3 +
 hw/arm/meson.build                      |  1 +
 5 files changed, 116 insertions(+)
 create mode 100644 include/hw/arm/rp2040.h
 create mode 100644 hw/arm/rp2040.c
configs/devices/arm-softmmu/default.mak +1 -0
index 6985a25377..dce1c39aad 100644
@@ -32,6 +32,7 @@ CONFIG_NETDUINO2=y
 CONFIG_NETDUINOPLUS2=y
 CONFIG_MPS2=y
 CONFIG_RASPI=y
+CONFIG_RP2040=y
 CONFIG_DIGIC=y
 CONFIG_SABRELITE=y
 CONFIG_EMCRAFT_SF2=y
include/hw/arm/rp2040.h new file +32 -0
new file mode 100644
index 0000000000..6bf4a4e57e
@@ -0,0 +1,32 @@
+/*
+ * RP2040 SoC Emulation
+ *
+ * Copyright (c) 2021 Linaro Ltd
+ *
+ * SPDX-License-Identifier: GPL-2.0-or-later
+ */
+
+#ifndef _RP2040_H_
+#define _RP2040_H_
+
+#include "target/arm/cpu.h"
+#include "hw/arm/armv7m.h"
+#include "qom/object.h"
+
+#define TYPE_RP2040 "rp2040"
+OBJECT_DECLARE_TYPE(RP2040State, RP2040Class, RP2040)
+
+#define RP2040_NCPUS 2
+
+struct RP2040State {
+    /*< private >*/
+    DeviceState parent_obj;
+    /*< public >*/
+
+    ARMv7MState armv7m[RP2040_NCPUS];
+
+    MemoryRegion container;
+};
+
+
+#endif /* _RP2040_H_ */
hw/arm/rp2040.c new file +79 -0

79 lines not on this page · raw message

hw/arm/Kconfig +3 -0
index e652590943..1c5150c180 100644
@@ -203,6 +203,9 @@ config REALVIEW
     select DS1338 # I2C RTC+NVRAM
     select USB_OHCI
 
+config RP2040
+    bool
+
 config SBSA_REF
     bool
     imply PCI_DEVICES
hw/arm/meson.build +1 -0
index 721a8eb8be..9f1b040c57 100644
@@ -40,6 +40,7 @@ arm_ss.add(when: 'CONFIG_STRONGARM', if_true: files('strongarm.c'))
 arm_ss.add(when: 'CONFIG_ALLWINNER_A10', if_true: files('allwinner-a10.c', 'cubieboard.c'))
 arm_ss.add(when: 'CONFIG_ALLWINNER_H3', if_true: files('allwinner-h3.c', 'orangepi.c'))
 arm_ss.add(when: 'CONFIG_RASPI', if_true: files('bcm2835_peripherals.c', 'bcm2836.c', 'raspi.c'))
+arm_ss.add(when: 'CONFIG_RP2040', if_true: files('rp2040.c'))
 arm_ss.add(when: 'CONFIG_STM32F100_SOC', if_true: files('stm32f100_soc.c'))
 arm_ss.add(when: 'CONFIG_STM32F205_SOC', if_true: files('stm32f205_soc.c'))
 arm_ss.add(when: 'CONFIG_STM32F405_SOC', if_true: files('stm32f405_soc.c'))
Signature · 1 line
2.30.2
Reviewed-by on v1 1/6
#2 lore raw

Re: [RFC PATCH 1/6] hw/arm: arm initial boilerplate for RP2040 SoC

Quoted 11 lines · Alex Bennée
---
 configs/devices/arm-softmmu/default.mak |  1 +
 include/hw/arm/rp2040.h                 | 32 ++++++++++
 hw/arm/rp2040.c                         | 79 +++++++++++++++++++++++++
 hw/arm/Kconfig                          |  3 +
 hw/arm/meson.build                      |  1 +
 5 files changed, 116 insertions(+)
 create mode 100644 include/hw/arm/rp2040.h
 create mode 100644 hw/arm/rp2040.c

hw/arm/rp2040.c line 52

Quoted 10 lines
hw/arm/rp2040.c new file +10 -0
+static void rp2040_realize(DeviceState *dev, Error **errp)
+{
+    RP2040State *s = RP2040(dev);
+    Object *obj = OBJECT(dev);
+    int n;
+
+    for (n = 0; n < RP2040_NCPUS; n++) {
+        Object *cpuobj = OBJECT(&s->armv7m[n]);
+        if (!sysbus_realize(SYS_BUS_DEVICE(cpuobj), errp)) {
+            return;

Could we ever have the first CPU initialized and the 2nd failing? Because the error path wouldn't be clean (leaking CPU#0). Maybe ignore errp and use error_fatal?

Otherwise,

Quoted 3 lines
hw/arm/rp2040.c new file +3 -0
+        }
+    }
+}
#3 lore raw

RFC PATCH 2/6 hw/arm: add boilerplate for machines based on the RP2040

In reply to Alex Bennée

Currently we are only targeting the official RaspberryPi Pico although I suspect most RP2040 based boards will look broadly the same.

---
 configs/devices/arm-softmmu/default.mak |  1 +
 hw/arm/raspi_pico.c                     | 77 +++++++++++++++++++++++++
 hw/arm/Kconfig                          |  3 +
 hw/arm/meson.build                      |  1 +
 4 files changed, 82 insertions(+)
 create mode 100644 hw/arm/raspi_pico.c
configs/devices/arm-softmmu/default.mak +1 -0
index dce1c39aad..7322213127 100644
@@ -32,6 +32,7 @@ CONFIG_NETDUINO2=y
 CONFIG_NETDUINOPLUS2=y
 CONFIG_MPS2=y
 CONFIG_RASPI=y
+CONFIG_RASPI_PICO=y
 CONFIG_RP2040=y
 CONFIG_DIGIC=y
 CONFIG_SABRELITE=y
hw/arm/raspi_pico.c new file +77 -0

77 lines not on this page · raw message

hw/arm/Kconfig +3 -0
index 1c5150c180..288a03f428 100644
@@ -336,6 +336,9 @@ config RASPI
     select SDHCI
     select USB_DWC2
 
+config RASPI_PICO
+    bool
+
 config STM32F100_SOC
     bool
     select ARM_V7M
hw/arm/meson.build +1 -0
index 9f1b040c57..4913cd1a50 100644
@@ -41,6 +41,7 @@ arm_ss.add(when: 'CONFIG_ALLWINNER_A10', if_true: files('allwinner-a10.c', 'cubi
 arm_ss.add(when: 'CONFIG_ALLWINNER_H3', if_true: files('allwinner-h3.c', 'orangepi.c'))
 arm_ss.add(when: 'CONFIG_RASPI', if_true: files('bcm2835_peripherals.c', 'bcm2836.c', 'raspi.c'))
 arm_ss.add(when: 'CONFIG_RP2040', if_true: files('rp2040.c'))
+arm_ss.add(when: 'CONFIG_RASPI_PICO', if_true: files('raspi_pico.c'))
 arm_ss.add(when: 'CONFIG_STM32F100_SOC', if_true: files('stm32f100_soc.c'))
 arm_ss.add(when: 'CONFIG_STM32F205_SOC', if_true: files('stm32f205_soc.c'))
 arm_ss.add(when: 'CONFIG_STM32F405_SOC', if_true: files('stm32f405_soc.c'))
Signature · 1 line
2.30.2
Reviewed-by on v1 2/6
#4 lore raw

Re: [RFC PATCH 2/6] hw/arm: add boilerplate for machines based on the RP2040

Quoted 12 lines · Alex Bennée

Currently we are only targeting the official RaspberryPi Pico although I suspect most RP2040 based boards will look broadly the same.

---
 configs/devices/arm-softmmu/default.mak |  1 +
 hw/arm/raspi_pico.c                     | 77 +++++++++++++++++++++++++
 hw/arm/Kconfig                          |  3 +
 hw/arm/meson.build                      |  1 +
 4 files changed, 82 insertions(+)
 create mode 100644 hw/arm/raspi_pico.c

hw/arm/raspi_pico.c line 59

Quoted 10 lines
hw/arm/raspi_pico.c new file +10 -0
+static void pipico_machine_class_init(ObjectClass *oc, void *data)
+{
+    MachineClass *mc = MACHINE_CLASS(oc);
+
+    mc->desc = g_strdup_printf("Raspberry Pi Pico");
+    mc->init = pipico_machine_init;
+    mc->block_default_type = IF_PFLASH;
+    mc->no_parallel = 1;
+    mc->no_floppy = 1;
+    mc->no_cdrom = 1;

:)

Quoted 5 lines
hw/arm/raspi_pico.c new file +5 -0
+    mc->no_sdcard = 1;
+    mc->min_cpus = 2;
+    mc->default_cpus = 2;
+    mc->max_cpus = 2;
+};
#5 lore raw

RFC PATCH 3/6 hw/arm: wire-up memory from the Pico board and the SoC

In reply to Alex Bennée

All the memory aside from the external flash is a feature of the SoC itself. However the flash is part of the board and different RP2040 boards can choose to wire up different amounts of it.

For now add unimplemented devices for all the rp2040 peripheral blocks. Before we can boot more of the ROM we will need to model at least the SIO and CLOCKS blocks.

For now CPU#1 starts disabled as it needs a working CPUID register so it can identify itself before sleeping.

---
 include/hw/arm/rp2040.h |   9 +++-
 hw/arm/raspi_pico.c     |  20 +++++++
 hw/arm/rp2040.c         | 113 ++++++++++++++++++++++++++++++++++++++--
 3 files changed, 137 insertions(+), 5 deletions(-)
include/hw/arm/rp2040.h +8 -1
index 6bf4a4e57e..760cef98fc 100644
@@ -25,7 +25,14 @@ struct RP2040State {
 
     ARMv7MState armv7m[RP2040_NCPUS];
 
-    MemoryRegion container;
+    /* RP2040 regions */
+    MemoryRegion *memory; /* from board */
+    MemoryRegion memory_alias[RP2040_NCPUS - 1];
+
+    MemoryRegion rom;    /* internal mask rom */
+    MemoryRegion sram03; /* shared SRAM0-3 banks */
+    MemoryRegion sram4;  /* non-stripped SRAM4 */
+    MemoryRegion sram5;  /* non-stripped SRAM5 */
 };
 
 
hw/arm/raspi_pico.c +20 -0
index 9826f4d608..76839e93bf 100644
@@ -22,6 +22,7 @@ struct PiPicoMachineState {
     MachineState parent_obj;
     /*< public >*/
     RP2040State soc;
+    MemoryRegion flash;
 };
 typedef struct PiPicoMachineState PiPicoMachineState;
 
@@ -37,14 +38,33 @@ typedef struct PiPicoMachineClass PiPicoMachineClass;
 DECLARE_OBJ_CHECKERS(PiPicoMachineState, PiPicoMachineClass,
                      PIPICO_MACHINE, TYPE_PIPICO_MACHINE)
 
+#define RP2040_XIP_BASE   0x10000000
 
 static void pipico_machine_init(MachineState *machine)
 {
     PiPicoMachineState *s = PIPICO_MACHINE(machine);
+    MemoryRegion *sysmem = get_system_memory();
+    Error **errp = &error_fatal;
 
     /* Setup the SOC */
     object_initialize_child(OBJECT(machine), "soc", &s->soc, TYPE_RP2040);
+    object_property_set_link(OBJECT(&s->soc), "memory", OBJECT(sysmem), errp);
+
+    /*
+     * The flash device it external to the SoC and mounted on the
+     * PiPico board itself. We will "load" the actual contents with
+     * armv7_load_kernel later although we still rely on the SoC's
+     * mask ROM to get to it.
+     */
+    const uint32_t flash_size = 256 * KiB;
+    memory_region_init_rom(&s->flash, NULL, "pico.flash0", flash_size, errp);
+    memory_region_add_subregion(sysmem, RP2040_XIP_BASE, &s->flash);
+
+
     sysbus_realize(SYS_BUS_DEVICE(&s->soc), &error_fatal);
+
+    /* This assumes the "kernel" is positioned in the XIP Flash block */
+    armv7m_load_kernel(ARM_CPU(first_cpu), machine->kernel_filename, RP2040_XIP_BASE);
 }
 
 static void pipico_machine_class_init(ObjectClass *oc, void *data)
hw/arm/rp2040.c +109 -4

159 lines not on this page · raw message

Signature · 1 line
2.30.2
#6 lore raw

Re: [RFC PATCH 3/6] hw/arm: wire-up memory from the Pico board and the SoC

Quoted 34 lines · Alex Bennée

All the memory aside from the external flash is a feature of the SoC itself. However the flash is part of the board and different RP2040 boards can choose to wire up different amounts of it.

For now add unimplemented devices for all the rp2040 peripheral blocks. Before we can boot more of the ROM we will need to model at least the SIO and CLOCKS blocks.

For now CPU#1 starts disabled as it needs a working CPUID register so it can identify itself before sleeping.

---
 include/hw/arm/rp2040.h |   9 +++-
 hw/arm/raspi_pico.c     |  20 +++++++
 hw/arm/rp2040.c         | 113 ++++++++++++++++++++++++++++++++++++++--
 3 files changed, 137 insertions(+), 5 deletions(-)
hw/arm/rp2040.c +7 -1
+static Property rp2040_soc_properties[] = {
+    DEFINE_PROP_LINK("memory", RP2040State, memory, TYPE_MEMORY_REGION,
+                     MemoryRegion *),
+    DEFINE_PROP_END_OF_LIST(),
+};
+
 static void rp2040_class_init(ObjectClass *oc, void *data)
 {
     DeviceClass *dc = DEVICE_CLASS(oc);
@@ -61,14 +167,13 @@ static void rp2040_class_init(ObjectClass *oc, void *data)
 
     bc->cpu_type = ARM_CPU_TYPE_NAME("cortex-m0");
     dc->realize = rp2040_realize;
-    /* any props? */
+    device_class_set_props(dc, rp2040_soc_properties);
 };

hw/arm/rp2040.c static void rp2040_class_init(ObjectClass *oc, void *data) · line 179

Quoted 10 lines
hw/arm/rp2040.c +1 -2
 
 static const TypeInfo rp2040_types[] = {
     {
         .name           = TYPE_RP2040,
-        /* .parent         = TYPE_SYS_BUS_DEVICE, */
-        .parent         = TYPE_DEVICE,
+        .parent         = TYPE_SYS_BUS_DEVICE,
         .instance_size  = sizeof(RP2040State),
         .instance_init  = rp2040_init,
         .class_size     = sizeof(RP2040Class),

Squash that in patch #1 without any MemoryRegion property?

#7 lore raw

RFC PATCH 4/6 pc-bios: add pipico mask rom (!upstream)

In reply to Alex Bennée

The boot code for the RP2040 is fully open source and can be found at:

https://github.com/raspberrypi/pico-bootrom

it contains not only the initial boot code but a full USB stack (for programming) and a number of library functions that the user code might use. We really should be able to build this ourselves but until I figure out how to integrate the build this will do. This image was extracted from a B1 pipico by a friendly hardware hacker.

---
 pc-bios/pipico.rom | Bin 0 -> 16384 bytes
 1 file changed, 0 insertions(+), 0 deletions(-)
 create mode 100644 pc-bios/pipico.rom
pc-bios/pipico.rom new file binary +0 -0
new file mode 100644
index 0000000000000000000000000000000000000000..8eb47ee7873678c8e82aeae8db9157fddf5e9de4
GIT binary patch
Signature · 1 line
2.30.2
#8 lore raw

RFC PATCH 5/6 hw/arm: add mask boot ROM logic

In reply to Alex Bennée

The mask ROM contains the initial boot code to start the RP2040. It also contains some common sub-routines as well as the flash programming code for drag and drop programming. As a result we need to ensure the full version is available to any user supplied programs we might want to run.

---
 hw/arm/rp2040.c | 19 ++++++++++++++++++-
 1 file changed, 18 insertions(+), 1 deletion(-)
hw/arm/rp2040.c +18 -1
index c6cc9b7165..9a5bc20159 100644
@@ -8,10 +8,12 @@
 
 #include "qemu/osdep.h"
 #include "qemu/units.h"
+#include "qemu/datadir.h"
 #include "qapi/error.h"
 #include "hw/arm/armv7m.h"
 #include "hw/arm/rp2040.h"
 #include "hw/misc/unimp.h"
+#include "hw/loader.h"
 #include "hw/sysbus.h"
 #include "hw/qdev-properties.h"
 
@@ -33,6 +35,8 @@ typedef struct RP2040Class {
 #define RP2040_SRAM4_BASE 0x20040000
 #define RP2040_SRAM5_BASE 0x20041000
 
+#define RP2040_MASK_ROM   "pipico.rom"
+
 static void rp2040_init(Object *obj)
 {
     RP2040State *s = RP2040(obj);
@@ -61,6 +65,8 @@ static void rp2040_realize(DeviceState *dev, Error **errp)
     RP2040State *s = RP2040(dev);
     Object *obj = OBJECT(dev);
     int n;
+    g_autofree char *mask_rom = qemu_find_file(QEMU_FILE_TYPE_BIOS,
+                                               RP2040_MASK_ROM);
 
     if (!s->memory) {
         error_setg(errp, "%s: memory property was not set", __func__);
@@ -68,9 +74,20 @@ static void rp2040_realize(DeviceState *dev, Error **errp)
     }
 
     /* initialize internal 16 KB internal ROM */
-    memory_region_init_rom(&s->rom, obj, "rp2040.rom0", 16 * KiB, errp);
+    memory_region_init_rom(&s->rom, obj, "rp2040.rom", 16 * KiB, errp);
     memory_region_add_subregion(s->memory, 0, &s->rom);
 
+    if (!mask_rom) {
+        error_setg(errp, "%s: unable to find mask_rom %s",
+                   __func__, RP2040_MASK_ROM);
+        return;
+    }
+    n = load_image_targphys(mask_rom, 0x0, 16 * KiB);
+    if (n <= 0) {
+        error_setg(errp, "%s: failed to load mask rom image: %s",
+                   __func__, RP2040_MASK_ROM);
+    }
+
     /* SRAM (Main 256k bank + two 4k banks)*/
     memory_region_init_ram(&s->sram03, obj, "rp2040.sram03", 256 * KiB, errp);
     memory_region_add_subregion(s->memory, RP2040_SRAM_BASE, &s->sram03);
Signature · 1 line
2.30.2
#9 lore raw

RFC PATCH 6/6 docs/devel: add some clarifying text for aliases

In reply to Alex Bennée

We do mention the limitation of single parenthood for memory_region_add_subregion but lets also make it clear how aliases help solve that conundrum.

---
 docs/devel/memory.rst | 14 +++++++++-----
 1 file changed, 9 insertions(+), 5 deletions(-)
docs/devel/memory.rst +9 -5
index 5dc8a12682..69c5e3f914 100644
@@ -67,11 +67,15 @@ MemoryRegion):
 
   You initialize a pure container with memory_region_init().
 
-- alias: a subsection of another region.  Aliases allow a region to be
-  split apart into discontiguous regions.  Examples of uses are memory banks
-  used when the guest address space is smaller than the amount of RAM
-  addressed, or a memory controller that splits main memory to expose a "PCI
-  hole".  Aliases may point to any type of region, including other aliases,
+- alias: a subsection of another region. Aliases allow a region to be
+  split apart into discontiguous regions. Examples of uses are memory
+  banks used when the guest address space is smaller than the amount
+  of RAM addressed, or a memory controller that splits main memory to
+  expose a "PCI hole". You can also create aliases to avoid trying to
+  add the original region to multiple parents via
+  `memory_region_add_subregion`.
+
+  Aliases may point to any type of region, including other aliases,
   but an alias may not point back to itself, directly or indirectly.
   You initialize these with memory_region_init_alias().
 
Signature · 1 line
2.30.2
Reviewed-by on v1 6/6
#10 lore raw

Re: [RFC PATCH 6/6] docs/devel: add some clarifying text for aliases

Quoted 24 lines · Alex Bennée

We do mention the limitation of single parenthood for memory_region_add_subregion but lets also make it clear how aliases help solve that conundrum.

---
 docs/devel/memory.rst | 14 +++++++++-----
 1 file changed, 9 insertions(+), 5 deletions(-)
docs/devel/memory.rst +2 -5
index 5dc8a12682..69c5e3f914 100644
@@ -67,11 +67,15 @@ MemoryRegion):
 
   You initialize a pure container with memory_region_init().
 
-- alias: a subsection of another region.  Aliases allow a region to be
-  split apart into discontiguous regions.  Examples of uses are memory banks
-  used when the guest address space is smaller than the amount of RAM
-  addressed, or a memory controller that splits main memory to expose a "PCI
-  hole".  Aliases may point to any type of region, including other aliases,
+- alias: a subsection of another region. Aliases allow a region to be
+  split apart into discontiguous regions. Examples of uses are memory

docs/devel/memory.rst MemoryRegion): · line 81

Quoted 10 lines
docs/devel/memory.rst +7 -0
+  banks used when the guest address space is smaller than the amount
+  of RAM addressed, or a memory controller that splits main memory to
+  expose a "PCI hole". You can also create aliases to avoid trying to
+  add the original region to multiple parents via
+  `memory_region_add_subregion`.
+
+  Aliases may point to any type of region, including other aliases,
   but an alias may not point back to itself, directly or indirectly.
   You initialize these with memory_region_init_alias().
 
#11 lore raw

Re: [RFC PATCH 6/6] docs/devel: add some clarifying text for aliases

In reply to Alex Bennée
Quoted 8 lines · Alex Bennée

We do mention the limitation of single parenthood for memory_region_add_subregion but lets also make it clear how aliases help solve that conundrum.

---
 docs/devel/memory.rst | 14 +++++++++-----
 1 file changed, 9 insertions(+), 5 deletions(-)

Thanks, queued via memory-api.