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