xref: /qemu/hw/arm/microbit.c (revision a27bd6c779badb8d76e4430d810ef710a1b98f4e)
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"
20*a27bd6c7SMarkus Armbruster #include "hw/qdev-properties.h"
21b148ed46SJoel Stanley 
22b148ed46SJoel Stanley typedef struct {
23b148ed46SJoel Stanley     MachineState parent;
24b148ed46SJoel Stanley 
25b148ed46SJoel Stanley     NRF51State nrf51;
269d68bf56SSteffen Görtz     MicrobitI2CState i2c;
27b148ed46SJoel Stanley } MicrobitMachineState;
28b148ed46SJoel Stanley 
29b148ed46SJoel Stanley #define TYPE_MICROBIT_MACHINE MACHINE_TYPE_NAME("microbit")
30b148ed46SJoel Stanley 
31b148ed46SJoel Stanley #define MICROBIT_MACHINE(obj) \
32b148ed46SJoel Stanley     OBJECT_CHECK(MicrobitMachineState, obj, TYPE_MICROBIT_MACHINE)
33b148ed46SJoel Stanley 
34b148ed46SJoel Stanley static void microbit_init(MachineState *machine)
35b148ed46SJoel Stanley {
36b148ed46SJoel Stanley     MicrobitMachineState *s = MICROBIT_MACHINE(machine);
37b148ed46SJoel Stanley     MemoryRegion *system_memory = get_system_memory();
389d68bf56SSteffen Görtz     MemoryRegion *mr;
39b148ed46SJoel Stanley     Object *soc = OBJECT(&s->nrf51);
409d68bf56SSteffen Görtz     Object *i2c = OBJECT(&s->i2c);
41b148ed46SJoel Stanley 
42b148ed46SJoel Stanley     sysbus_init_child_obj(OBJECT(machine), "nrf51", soc, sizeof(s->nrf51),
43b148ed46SJoel Stanley                           TYPE_NRF51_SOC);
44b0014913SJulia Suvorova     qdev_prop_set_chr(DEVICE(&s->nrf51), "serial0", serial_hd(0));
45b148ed46SJoel Stanley     object_property_set_link(soc, OBJECT(system_memory), "memory",
46b148ed46SJoel Stanley                              &error_fatal);
47b148ed46SJoel Stanley     object_property_set_bool(soc, true, "realized", &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      */
549d68bf56SSteffen Görtz     sysbus_init_child_obj(OBJECT(machine), "microbit.twi", i2c,
559d68bf56SSteffen Görtz                           sizeof(s->i2c), TYPE_MICROBIT_I2C);
569d68bf56SSteffen Görtz     object_property_set_bool(i2c, true, "realized", &error_fatal);
579d68bf56SSteffen Görtz     mr = sysbus_mmio_get_region(SYS_BUS_DEVICE(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,
62b148ed46SJoel Stanley                        NRF51_SOC(soc)->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