xref: /qemu/hw/arm/nrf51_soc.c (revision 659b85e4133127b342ed191e9d97dc6ad7626113)
1673b2d42SJoel Stanley /*
2673b2d42SJoel Stanley  * Nordic Semiconductor nRF51 SoC
3673b2d42SJoel Stanley  * http://infocenter.nordicsemi.com/pdf/nRF51_RM_v3.0.1.pdf
4673b2d42SJoel Stanley  *
5673b2d42SJoel Stanley  * Copyright 2018 Joel Stanley <joel@jms.id.au>
6673b2d42SJoel Stanley  *
7673b2d42SJoel Stanley  * This code is licensed under the GPL version 2 or later.  See
8673b2d42SJoel Stanley  * the COPYING file in the top-level directory.
9673b2d42SJoel Stanley  */
10673b2d42SJoel Stanley 
11673b2d42SJoel Stanley #include "qemu/osdep.h"
12673b2d42SJoel Stanley #include "qapi/error.h"
13673b2d42SJoel Stanley #include "qemu-common.h"
14673b2d42SJoel Stanley #include "hw/arm/arm.h"
15673b2d42SJoel Stanley #include "hw/sysbus.h"
16673b2d42SJoel Stanley #include "hw/boards.h"
17673b2d42SJoel Stanley #include "hw/devices.h"
18673b2d42SJoel Stanley #include "hw/misc/unimp.h"
19673b2d42SJoel Stanley #include "exec/address-spaces.h"
20673b2d42SJoel Stanley #include "sysemu/sysemu.h"
21673b2d42SJoel Stanley #include "qemu/log.h"
22673b2d42SJoel Stanley #include "cpu.h"
23673b2d42SJoel Stanley 
24*659b85e4SSteffen Görtz #include "hw/arm/nrf51.h"
25673b2d42SJoel Stanley #include "hw/arm/nrf51_soc.h"
26673b2d42SJoel Stanley 
27673b2d42SJoel Stanley /*
28673b2d42SJoel Stanley  * The size and base is for the NRF51822 part. If other parts
29673b2d42SJoel Stanley  * are supported in the future, add a sub-class of NRF51SoC for
30673b2d42SJoel Stanley  * the specific variants
31673b2d42SJoel Stanley  */
32*659b85e4SSteffen Görtz #define NRF51822_FLASH_SIZE     (256 * NRF51_PAGE_SIZE)
33*659b85e4SSteffen Görtz #define NRF51822_SRAM_SIZE      (16 * NRF51_PAGE_SIZE)
34673b2d42SJoel Stanley 
35b0014913SJulia Suvorova #define BASE_TO_IRQ(base) ((base >> 12) & 0x1F)
36b0014913SJulia Suvorova 
37673b2d42SJoel Stanley static void nrf51_soc_realize(DeviceState *dev_soc, Error **errp)
38673b2d42SJoel Stanley {
39673b2d42SJoel Stanley     NRF51State *s = NRF51_SOC(dev_soc);
40b0014913SJulia Suvorova     MemoryRegion *mr;
41673b2d42SJoel Stanley     Error *err = NULL;
42673b2d42SJoel Stanley 
43673b2d42SJoel Stanley     if (!s->board_memory) {
44673b2d42SJoel Stanley         error_setg(errp, "memory property was not set");
45673b2d42SJoel Stanley         return;
46673b2d42SJoel Stanley     }
47673b2d42SJoel Stanley 
48673b2d42SJoel Stanley     object_property_set_link(OBJECT(&s->cpu), OBJECT(&s->container), "memory",
49673b2d42SJoel Stanley             &err);
50673b2d42SJoel Stanley     if (err) {
51673b2d42SJoel Stanley         error_propagate(errp, err);
52673b2d42SJoel Stanley         return;
53673b2d42SJoel Stanley     }
54673b2d42SJoel Stanley     object_property_set_bool(OBJECT(&s->cpu), true, "realized", &err);
55673b2d42SJoel Stanley     if (err) {
56673b2d42SJoel Stanley         error_propagate(errp, err);
57673b2d42SJoel Stanley         return;
58673b2d42SJoel Stanley     }
59673b2d42SJoel Stanley 
60673b2d42SJoel Stanley     memory_region_add_subregion_overlap(&s->container, 0, s->board_memory, -1);
61673b2d42SJoel Stanley 
62673b2d42SJoel Stanley     memory_region_init_rom(&s->flash, OBJECT(s), "nrf51.flash", s->flash_size,
63673b2d42SJoel Stanley             &err);
64673b2d42SJoel Stanley     if (err) {
65673b2d42SJoel Stanley         error_propagate(errp, err);
66673b2d42SJoel Stanley         return;
67673b2d42SJoel Stanley     }
68*659b85e4SSteffen Görtz     memory_region_add_subregion(&s->container, NRF51_FLASH_BASE, &s->flash);
69673b2d42SJoel Stanley 
70673b2d42SJoel Stanley     memory_region_init_ram(&s->sram, NULL, "nrf51.sram", s->sram_size, &err);
71673b2d42SJoel Stanley     if (err) {
72673b2d42SJoel Stanley         error_propagate(errp, err);
73673b2d42SJoel Stanley         return;
74673b2d42SJoel Stanley     }
75*659b85e4SSteffen Görtz     memory_region_add_subregion(&s->container, NRF51_SRAM_BASE, &s->sram);
76673b2d42SJoel Stanley 
77b0014913SJulia Suvorova     /* UART */
78b0014913SJulia Suvorova     object_property_set_bool(OBJECT(&s->uart), true, "realized", &err);
79b0014913SJulia Suvorova     if (err) {
80b0014913SJulia Suvorova         error_propagate(errp, err);
81b0014913SJulia Suvorova         return;
82b0014913SJulia Suvorova     }
83b0014913SJulia Suvorova     mr = sysbus_mmio_get_region(SYS_BUS_DEVICE(&s->uart), 0);
84*659b85e4SSteffen Görtz     memory_region_add_subregion_overlap(&s->container, NRF51_UART_BASE, mr, 0);
85b0014913SJulia Suvorova     sysbus_connect_irq(SYS_BUS_DEVICE(&s->uart), 0,
86b0014913SJulia Suvorova                        qdev_get_gpio_in(DEVICE(&s->cpu),
87*659b85e4SSteffen Görtz                        BASE_TO_IRQ(NRF51_UART_BASE)));
88b0014913SJulia Suvorova 
89*659b85e4SSteffen Görtz     create_unimplemented_device("nrf51_soc.io", NRF51_IOMEM_BASE,
90*659b85e4SSteffen Görtz                                 NRF51_IOMEM_SIZE);
91*659b85e4SSteffen Görtz     create_unimplemented_device("nrf51_soc.ficr", NRF51_FICR_BASE,
92*659b85e4SSteffen Görtz                                 NRF51_FICR_SIZE);
93673b2d42SJoel Stanley     create_unimplemented_device("nrf51_soc.private",
94*659b85e4SSteffen Görtz                                 NRF51_PRIVATE_BASE, NRF51_PRIVATE_SIZE);
95673b2d42SJoel Stanley }
96673b2d42SJoel Stanley 
97673b2d42SJoel Stanley static void nrf51_soc_init(Object *obj)
98673b2d42SJoel Stanley {
99673b2d42SJoel Stanley     NRF51State *s = NRF51_SOC(obj);
100673b2d42SJoel Stanley 
101673b2d42SJoel Stanley     memory_region_init(&s->container, obj, "nrf51-container", UINT64_MAX);
102673b2d42SJoel Stanley 
103673b2d42SJoel Stanley     sysbus_init_child_obj(OBJECT(s), "armv6m", OBJECT(&s->cpu), sizeof(s->cpu),
104673b2d42SJoel Stanley                           TYPE_ARMV7M);
105673b2d42SJoel Stanley     qdev_prop_set_string(DEVICE(&s->cpu), "cpu-type",
106673b2d42SJoel Stanley                          ARM_CPU_TYPE_NAME("cortex-m0"));
107673b2d42SJoel Stanley     qdev_prop_set_uint32(DEVICE(&s->cpu), "num-irq", 32);
108b0014913SJulia Suvorova 
109b0014913SJulia Suvorova     sysbus_init_child_obj(obj, "uart", &s->uart, sizeof(s->uart),
110b0014913SJulia Suvorova                            TYPE_NRF51_UART);
111b0014913SJulia Suvorova     object_property_add_alias(obj, "serial0", OBJECT(&s->uart), "chardev",
112b0014913SJulia Suvorova                               &error_abort);
113673b2d42SJoel Stanley }
114673b2d42SJoel Stanley 
115673b2d42SJoel Stanley static Property nrf51_soc_properties[] = {
116673b2d42SJoel Stanley     DEFINE_PROP_LINK("memory", NRF51State, board_memory, TYPE_MEMORY_REGION,
117673b2d42SJoel Stanley                      MemoryRegion *),
118673b2d42SJoel Stanley     DEFINE_PROP_UINT32("sram-size", NRF51State, sram_size, NRF51822_SRAM_SIZE),
119673b2d42SJoel Stanley     DEFINE_PROP_UINT32("flash-size", NRF51State, flash_size,
120673b2d42SJoel Stanley                        NRF51822_FLASH_SIZE),
121673b2d42SJoel Stanley     DEFINE_PROP_END_OF_LIST(),
122673b2d42SJoel Stanley };
123673b2d42SJoel Stanley 
124673b2d42SJoel Stanley static void nrf51_soc_class_init(ObjectClass *klass, void *data)
125673b2d42SJoel Stanley {
126673b2d42SJoel Stanley     DeviceClass *dc = DEVICE_CLASS(klass);
127673b2d42SJoel Stanley 
128673b2d42SJoel Stanley     dc->realize = nrf51_soc_realize;
129673b2d42SJoel Stanley     dc->props = nrf51_soc_properties;
130673b2d42SJoel Stanley }
131673b2d42SJoel Stanley 
132673b2d42SJoel Stanley static const TypeInfo nrf51_soc_info = {
133673b2d42SJoel Stanley     .name          = TYPE_NRF51_SOC,
134673b2d42SJoel Stanley     .parent        = TYPE_SYS_BUS_DEVICE,
135673b2d42SJoel Stanley     .instance_size = sizeof(NRF51State),
136673b2d42SJoel Stanley     .instance_init = nrf51_soc_init,
137673b2d42SJoel Stanley     .class_init    = nrf51_soc_class_init,
138673b2d42SJoel Stanley };
139673b2d42SJoel Stanley 
140673b2d42SJoel Stanley static void nrf51_soc_types(void)
141673b2d42SJoel Stanley {
142673b2d42SJoel Stanley     type_register_static(&nrf51_soc_info);
143673b2d42SJoel Stanley }
144673b2d42SJoel Stanley type_init(nrf51_soc_types)
145