xref: /qemu/hw/arm/microbit.c (revision 12ec8bd51e81a025a444585359f019f49b57a702)
1b148ed46SJoel Stanley /*
2b148ed46SJoel Stanley  * BBC micro:bit machine
3b148ed46SJoel Stanley  * http://tech.microbit.org/hardware/
4b148ed46SJoel Stanley  *
5b148ed46SJoel Stanley  * Copyright 2018 Joel Stanley <joel@jms.id.au>
6b148ed46SJoel Stanley  *
7b148ed46SJoel Stanley  * This code is licensed under the GPL version 2 or later.  See
8b148ed46SJoel Stanley  * the COPYING file in the top-level directory.
9b148ed46SJoel Stanley  */
10b148ed46SJoel Stanley 
11b148ed46SJoel Stanley #include "qemu/osdep.h"
12b148ed46SJoel Stanley #include "qapi/error.h"
13b148ed46SJoel Stanley #include "hw/boards.h"
14*12ec8bd5SPeter Maydell #include "hw/arm/boot.h"
15b0014913SJulia Suvorova #include "sysemu/sysemu.h"
16b148ed46SJoel Stanley #include "exec/address-spaces.h"
17b148ed46SJoel Stanley 
18b148ed46SJoel Stanley #include "hw/arm/nrf51_soc.h"
199d68bf56SSteffen Görtz #include "hw/i2c/microbit_i2c.h"
20b148ed46SJoel Stanley 
21b148ed46SJoel Stanley typedef struct {
22b148ed46SJoel Stanley     MachineState parent;
23b148ed46SJoel Stanley 
24b148ed46SJoel Stanley     NRF51State nrf51;
259d68bf56SSteffen Görtz     MicrobitI2CState i2c;
26b148ed46SJoel Stanley } MicrobitMachineState;
27b148ed46SJoel Stanley 
28b148ed46SJoel Stanley #define TYPE_MICROBIT_MACHINE MACHINE_TYPE_NAME("microbit")
29b148ed46SJoel Stanley 
30b148ed46SJoel Stanley #define MICROBIT_MACHINE(obj) \
31b148ed46SJoel Stanley     OBJECT_CHECK(MicrobitMachineState, obj, TYPE_MICROBIT_MACHINE)
32b148ed46SJoel Stanley 
33b148ed46SJoel Stanley static void microbit_init(MachineState *machine)
34b148ed46SJoel Stanley {
35b148ed46SJoel Stanley     MicrobitMachineState *s = MICROBIT_MACHINE(machine);
36b148ed46SJoel Stanley     MemoryRegion *system_memory = get_system_memory();
379d68bf56SSteffen Görtz     MemoryRegion *mr;
38b148ed46SJoel Stanley     Object *soc = OBJECT(&s->nrf51);
399d68bf56SSteffen Görtz     Object *i2c = OBJECT(&s->i2c);
40b148ed46SJoel Stanley 
41b148ed46SJoel Stanley     sysbus_init_child_obj(OBJECT(machine), "nrf51", soc, sizeof(s->nrf51),
42b148ed46SJoel Stanley                           TYPE_NRF51_SOC);
43b0014913SJulia Suvorova     qdev_prop_set_chr(DEVICE(&s->nrf51), "serial0", serial_hd(0));
44b148ed46SJoel Stanley     object_property_set_link(soc, OBJECT(system_memory), "memory",
45b148ed46SJoel Stanley                              &error_fatal);
46b148ed46SJoel Stanley     object_property_set_bool(soc, true, "realized", &error_fatal);
47b148ed46SJoel Stanley 
489d68bf56SSteffen Görtz     /*
499d68bf56SSteffen Görtz      * Overlap the TWI stub device into the SoC.  This is a microbit-specific
509d68bf56SSteffen Görtz      * hack until we implement the nRF51 TWI controller properly and the
519d68bf56SSteffen Görtz      * magnetometer/accelerometer devices.
529d68bf56SSteffen Görtz      */
539d68bf56SSteffen Görtz     sysbus_init_child_obj(OBJECT(machine), "microbit.twi", i2c,
549d68bf56SSteffen Görtz                           sizeof(s->i2c), TYPE_MICROBIT_I2C);
559d68bf56SSteffen Görtz     object_property_set_bool(i2c, true, "realized", &error_fatal);
569d68bf56SSteffen Görtz     mr = sysbus_mmio_get_region(SYS_BUS_DEVICE(i2c), 0);
579d68bf56SSteffen Görtz     memory_region_add_subregion_overlap(&s->nrf51.container, NRF51_TWI_BASE,
589d68bf56SSteffen Görtz                                         mr, -1);
599d68bf56SSteffen Görtz 
60b148ed46SJoel Stanley     armv7m_load_kernel(ARM_CPU(first_cpu), machine->kernel_filename,
61b148ed46SJoel Stanley                        NRF51_SOC(soc)->flash_size);
62b148ed46SJoel Stanley }
63b148ed46SJoel Stanley 
64b148ed46SJoel Stanley static void microbit_machine_class_init(ObjectClass *oc, void *data)
65b148ed46SJoel Stanley {
66b148ed46SJoel Stanley     MachineClass *mc = MACHINE_CLASS(oc);
67b148ed46SJoel Stanley 
68b148ed46SJoel Stanley     mc->desc = "BBC micro:bit";
69b148ed46SJoel Stanley     mc->init = microbit_init;
70b148ed46SJoel Stanley     mc->max_cpus = 1;
71b148ed46SJoel Stanley }
72b148ed46SJoel Stanley 
73b148ed46SJoel Stanley static const TypeInfo microbit_info = {
74b148ed46SJoel Stanley     .name = TYPE_MICROBIT_MACHINE,
75b148ed46SJoel Stanley     .parent = TYPE_MACHINE,
76b148ed46SJoel Stanley     .instance_size = sizeof(MicrobitMachineState),
77b148ed46SJoel Stanley     .class_init = microbit_machine_class_init,
78b148ed46SJoel Stanley };
79b148ed46SJoel Stanley 
80b148ed46SJoel Stanley static void microbit_machine_init(void)
81b148ed46SJoel Stanley {
82b148ed46SJoel Stanley     type_register_static(&microbit_info);
83b148ed46SJoel Stanley }
84b148ed46SJoel Stanley 
85b148ed46SJoel Stanley type_init(microbit_machine_init);
86