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"
21db1015e9SEduardo Habkost #include "qom/object.h"
22b148ed46SJoel Stanley
23db1015e9SEduardo Habkost struct MicrobitMachineState {
24b148ed46SJoel Stanley MachineState parent;
25b148ed46SJoel Stanley
26b148ed46SJoel Stanley NRF51State nrf51;
279d68bf56SSteffen Görtz MicrobitI2CState i2c;
28db1015e9SEduardo Habkost };
29b148ed46SJoel Stanley
30b148ed46SJoel Stanley #define TYPE_MICROBIT_MACHINE MACHINE_TYPE_NAME("microbit")
31b148ed46SJoel Stanley
OBJECT_DECLARE_SIMPLE_TYPE(MicrobitMachineState,MICROBIT_MACHINE)328063396bSEduardo Habkost OBJECT_DECLARE_SIMPLE_TYPE(MicrobitMachineState, 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
405a147c8cSMarkus Armbruster object_initialize_child(OBJECT(machine), "nrf51", &s->nrf51,
41b148ed46SJoel Stanley TYPE_NRF51_SOC);
42b0014913SJulia Suvorova qdev_prop_set_chr(DEVICE(&s->nrf51), "serial0", serial_hd(0));
435325cc34SMarkus Armbruster object_property_set_link(OBJECT(&s->nrf51), "memory",
445325cc34SMarkus Armbruster OBJECT(system_memory), &error_fatal);
45e9a82986SMarkus Armbruster sysbus_realize(SYS_BUS_DEVICE(&s->nrf51), &error_fatal);
46b148ed46SJoel Stanley
479d68bf56SSteffen Görtz /*
489d68bf56SSteffen Görtz * Overlap the TWI stub device into the SoC. This is a microbit-specific
499d68bf56SSteffen Görtz * hack until we implement the nRF51 TWI controller properly and the
509d68bf56SSteffen Görtz * magnetometer/accelerometer devices.
519d68bf56SSteffen Görtz */
525a147c8cSMarkus Armbruster object_initialize_child(OBJECT(machine), "microbit.twi", &s->i2c,
535a147c8cSMarkus Armbruster TYPE_MICROBIT_I2C);
54e9a82986SMarkus Armbruster sysbus_realize(SYS_BUS_DEVICE(&s->i2c), &error_fatal);
55e9a82986SMarkus Armbruster mr = sysbus_mmio_get_region(SYS_BUS_DEVICE(&s->i2c), 0);
569d68bf56SSteffen Görtz memory_region_add_subregion_overlap(&s->nrf51.container, NRF51_TWI_BASE,
579d68bf56SSteffen Görtz mr, -1);
589d68bf56SSteffen Görtz
59b148ed46SJoel Stanley armv7m_load_kernel(ARM_CPU(first_cpu), machine->kernel_filename,
60*761c532aSPeter Maydell 0, s->nrf51.flash_size);
61b148ed46SJoel Stanley }
62b148ed46SJoel Stanley
microbit_machine_class_init(ObjectClass * oc,void * data)63b148ed46SJoel Stanley static void microbit_machine_class_init(ObjectClass *oc, void *data)
64b148ed46SJoel Stanley {
65b148ed46SJoel Stanley MachineClass *mc = MACHINE_CLASS(oc);
66b148ed46SJoel Stanley
67fd8f71b9SPhilippe Mathieu-Daudé mc->desc = "BBC micro:bit (Cortex-M0)";
68b148ed46SJoel Stanley mc->init = microbit_init;
69b148ed46SJoel Stanley mc->max_cpus = 1;
70b148ed46SJoel Stanley }
71b148ed46SJoel Stanley
72b148ed46SJoel Stanley static const TypeInfo microbit_info = {
73b148ed46SJoel Stanley .name = TYPE_MICROBIT_MACHINE,
74b148ed46SJoel Stanley .parent = TYPE_MACHINE,
75b148ed46SJoel Stanley .instance_size = sizeof(MicrobitMachineState),
76b148ed46SJoel Stanley .class_init = microbit_machine_class_init,
77b148ed46SJoel Stanley };
78b148ed46SJoel Stanley
microbit_machine_init(void)79b148ed46SJoel Stanley static void microbit_machine_init(void)
80b148ed46SJoel Stanley {
81b148ed46SJoel Stanley type_register_static(µbit_info);
82b148ed46SJoel Stanley }
83b148ed46SJoel Stanley
84b148ed46SJoel Stanley type_init(microbit_machine_init);
85