master
c 86 lines 2.57 KB
Raw
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/core/boards.h"
14 #include "hw/arm/boot.h"
15 #include "hw/arm/machines-qom.h"
16 #include "system/system.h"
17 #include "system/address-spaces.h"
18
19 #include "hw/arm/nrf51_soc.h"
20 #include "hw/i2c/microbit_i2c.h"
21 #include "hw/core/qdev-properties.h"
22 #include "qom/object.h"
23
24 struct MicrobitMachineState {
25 MachineState parent;
26
27 NRF51State nrf51;
28 MicrobitI2CState i2c;
29 };
30
31 #define TYPE_MICROBIT_MACHINE MACHINE_TYPE_NAME("microbit")
32
33 OBJECT_DECLARE_SIMPLE_TYPE(MicrobitMachineState, MICROBIT_MACHINE)
34
35 static void microbit_init(MachineState *machine)
36 {
37 MicrobitMachineState *s = MICROBIT_MACHINE(machine);
38 MemoryRegion *system_memory = get_system_memory();
39 MemoryRegion *mr;
40
41 object_initialize_child(OBJECT(machine), "nrf51", &s->nrf51,
42 TYPE_NRF51_SOC);
43 qdev_prop_set_chr(DEVICE(&s->nrf51), "serial0", serial_hd(0));
44 object_property_set_link(OBJECT(&s->nrf51), "memory",
45 OBJECT(system_memory), &error_fatal);
46 sysbus_realize(SYS_BUS_DEVICE(&s->nrf51), &error_fatal);
47
48 /*
49 * Overlap the TWI stub device into the SoC. This is a microbit-specific
50 * hack until we implement the nRF51 TWI controller properly and the
51 * magnetometer/accelerometer devices.
52 */
53 object_initialize_child(OBJECT(machine), "microbit.twi", &s->i2c,
54 TYPE_MICROBIT_I2C);
55 sysbus_realize(SYS_BUS_DEVICE(&s->i2c), &error_fatal);
56 mr = sysbus_mmio_get_region(SYS_BUS_DEVICE(&s->i2c), 0);
57 memory_region_add_subregion_overlap(&s->nrf51.container, NRF51_TWI_BASE,
58 mr, -1);
59
60 armv7m_load_kernel(s->nrf51.armv7m.cpu, machine->kernel_filename,
61 0, s->nrf51.flash_size);
62 }
63
64 static void microbit_machine_class_init(ObjectClass *oc, const void *data)
65 {
66 MachineClass *mc = MACHINE_CLASS(oc);
67
68 mc->desc = "BBC micro:bit (Cortex-M0)";
69 mc->init = microbit_init;
70 mc->max_cpus = 1;
71 }
72
73 static const TypeInfo microbit_info = {
74 .name = TYPE_MICROBIT_MACHINE,
75 .parent = TYPE_MACHINE,
76 .instance_size = sizeof(MicrobitMachineState),
77 .class_init = microbit_machine_class_init,
78 .interfaces = arm_machine_interfaces,
79 };
80
81 static void microbit_machine_init(void)
82 {
83 type_register_static(&microbit_info);
84 }
85
86 type_init(microbit_machine_init);