xref: /qemu/hw/arm/microbit.c (revision db1015e92e04835c9eb50c29625fe566d1202dbd)
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"
1412ec8bd5SPeter 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"
20a27bd6c7SMarkus Armbruster #include "hw/qdev-properties.h"
21*db1015e9SEduardo Habkost #include "qom/object.h"
22b148ed46SJoel Stanley 
23*db1015e9SEduardo Habkost struct MicrobitMachineState {
24b148ed46SJoel Stanley     MachineState parent;
25b148ed46SJoel Stanley 
26b148ed46SJoel Stanley     NRF51State nrf51;
279d68bf56SSteffen Görtz     MicrobitI2CState i2c;
28*db1015e9SEduardo Habkost };
29*db1015e9SEduardo Habkost typedef struct MicrobitMachineState MicrobitMachineState;
30b148ed46SJoel Stanley 
31b148ed46SJoel Stanley #define TYPE_MICROBIT_MACHINE MACHINE_TYPE_NAME("microbit")
32b148ed46SJoel Stanley 
33b148ed46SJoel Stanley #define MICROBIT_MACHINE(obj) \
34b148ed46SJoel Stanley     OBJECT_CHECK(MicrobitMachineState, obj, TYPE_MICROBIT_MACHINE)
35b148ed46SJoel Stanley 
36b148ed46SJoel Stanley static void microbit_init(MachineState *machine)
37b148ed46SJoel Stanley {
38b148ed46SJoel Stanley     MicrobitMachineState *s = MICROBIT_MACHINE(machine);
39b148ed46SJoel Stanley     MemoryRegion *system_memory = get_system_memory();
409d68bf56SSteffen Görtz     MemoryRegion *mr;
41b148ed46SJoel Stanley 
425a147c8cSMarkus Armbruster     object_initialize_child(OBJECT(machine), "nrf51", &s->nrf51,
43b148ed46SJoel Stanley                             TYPE_NRF51_SOC);
44b0014913SJulia Suvorova     qdev_prop_set_chr(DEVICE(&s->nrf51), "serial0", serial_hd(0));
455325cc34SMarkus Armbruster     object_property_set_link(OBJECT(&s->nrf51), "memory",
465325cc34SMarkus Armbruster                              OBJECT(system_memory), &error_fatal);
47e9a82986SMarkus Armbruster     sysbus_realize(SYS_BUS_DEVICE(&s->nrf51), &error_fatal);
48b148ed46SJoel Stanley 
499d68bf56SSteffen Görtz     /*
509d68bf56SSteffen Görtz      * Overlap the TWI stub device into the SoC.  This is a microbit-specific
519d68bf56SSteffen Görtz      * hack until we implement the nRF51 TWI controller properly and the
529d68bf56SSteffen Görtz      * magnetometer/accelerometer devices.
539d68bf56SSteffen Görtz      */
545a147c8cSMarkus Armbruster     object_initialize_child(OBJECT(machine), "microbit.twi", &s->i2c,
555a147c8cSMarkus Armbruster                             TYPE_MICROBIT_I2C);
56e9a82986SMarkus Armbruster     sysbus_realize(SYS_BUS_DEVICE(&s->i2c), &error_fatal);
57e9a82986SMarkus Armbruster     mr = sysbus_mmio_get_region(SYS_BUS_DEVICE(&s->i2c), 0);
589d68bf56SSteffen Görtz     memory_region_add_subregion_overlap(&s->nrf51.container, NRF51_TWI_BASE,
599d68bf56SSteffen Görtz                                         mr, -1);
609d68bf56SSteffen Görtz 
61b148ed46SJoel Stanley     armv7m_load_kernel(ARM_CPU(first_cpu), machine->kernel_filename,
62e9a82986SMarkus Armbruster                        s->nrf51.flash_size);
63b148ed46SJoel Stanley }
64b148ed46SJoel Stanley 
65b148ed46SJoel Stanley static void microbit_machine_class_init(ObjectClass *oc, void *data)
66b148ed46SJoel Stanley {
67b148ed46SJoel Stanley     MachineClass *mc = MACHINE_CLASS(oc);
68b148ed46SJoel Stanley 
69b148ed46SJoel Stanley     mc->desc = "BBC micro:bit";
70b148ed46SJoel Stanley     mc->init = microbit_init;
71b148ed46SJoel Stanley     mc->max_cpus = 1;
72b148ed46SJoel Stanley }
73b148ed46SJoel Stanley 
74b148ed46SJoel Stanley static const TypeInfo microbit_info = {
75b148ed46SJoel Stanley     .name = TYPE_MICROBIT_MACHINE,
76b148ed46SJoel Stanley     .parent = TYPE_MACHINE,
77b148ed46SJoel Stanley     .instance_size = sizeof(MicrobitMachineState),
78b148ed46SJoel Stanley     .class_init = microbit_machine_class_init,
79b148ed46SJoel Stanley };
80b148ed46SJoel Stanley 
81b148ed46SJoel Stanley static void microbit_machine_init(void)
82b148ed46SJoel Stanley {
83b148ed46SJoel Stanley     type_register_static(&microbit_info);
84b148ed46SJoel Stanley }
85b148ed46SJoel Stanley 
86b148ed46SJoel Stanley type_init(microbit_machine_init);
87