xref: /qemu/hw/arm/nrf51_soc.c (revision bb42c4cb2aad60a5309228c95eda9381f080e50a)
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 
24659b85e4SSteffen 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  */
32659b85e4SSteffen Görtz #define NRF51822_FLASH_SIZE     (256 * NRF51_PAGE_SIZE)
33659b85e4SSteffen 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     }
68659b85e4SSteffen 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     }
75659b85e4SSteffen 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);
84659b85e4SSteffen 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),
87659b85e4SSteffen Görtz                        BASE_TO_IRQ(NRF51_UART_BASE)));
88b0014913SJulia Suvorova 
89f30890deSSteffen Görtz     /* RNG */
90f30890deSSteffen Görtz     object_property_set_bool(OBJECT(&s->rng), true, "realized", &err);
91f30890deSSteffen Görtz     if (err) {
92f30890deSSteffen Görtz         error_propagate(errp, err);
93f30890deSSteffen Görtz         return;
94f30890deSSteffen Görtz     }
95f30890deSSteffen Görtz 
96f30890deSSteffen Görtz     mr = sysbus_mmio_get_region(SYS_BUS_DEVICE(&s->rng), 0);
97f30890deSSteffen Görtz     memory_region_add_subregion_overlap(&s->container, NRF51_RNG_BASE, mr, 0);
98f30890deSSteffen Görtz     sysbus_connect_irq(SYS_BUS_DEVICE(&s->rng), 0,
99f30890deSSteffen Görtz                        qdev_get_gpio_in(DEVICE(&s->cpu),
100f30890deSSteffen Görtz                        BASE_TO_IRQ(NRF51_RNG_BASE)));
101f30890deSSteffen Görtz 
102*bb42c4cbSSteffen Görtz     /* GPIO */
103*bb42c4cbSSteffen Görtz     object_property_set_bool(OBJECT(&s->gpio), true, "realized", &err);
104*bb42c4cbSSteffen Görtz     if (err) {
105*bb42c4cbSSteffen Görtz         error_propagate(errp, err);
106*bb42c4cbSSteffen Görtz         return;
107*bb42c4cbSSteffen Görtz     }
108*bb42c4cbSSteffen Görtz 
109*bb42c4cbSSteffen Görtz     mr = sysbus_mmio_get_region(SYS_BUS_DEVICE(&s->gpio), 0);
110*bb42c4cbSSteffen Görtz     memory_region_add_subregion_overlap(&s->container, NRF51_GPIO_BASE, mr, 0);
111*bb42c4cbSSteffen Görtz 
112*bb42c4cbSSteffen Görtz     /* Pass all GPIOs to the SOC layer so they are available to the board */
113*bb42c4cbSSteffen Görtz     qdev_pass_gpios(DEVICE(&s->gpio), dev_soc, NULL);
114*bb42c4cbSSteffen Görtz 
115659b85e4SSteffen Görtz     create_unimplemented_device("nrf51_soc.io", NRF51_IOMEM_BASE,
116659b85e4SSteffen Görtz                                 NRF51_IOMEM_SIZE);
117659b85e4SSteffen Görtz     create_unimplemented_device("nrf51_soc.ficr", NRF51_FICR_BASE,
118659b85e4SSteffen Görtz                                 NRF51_FICR_SIZE);
119673b2d42SJoel Stanley     create_unimplemented_device("nrf51_soc.private",
120659b85e4SSteffen Görtz                                 NRF51_PRIVATE_BASE, NRF51_PRIVATE_SIZE);
121673b2d42SJoel Stanley }
122673b2d42SJoel Stanley 
123673b2d42SJoel Stanley static void nrf51_soc_init(Object *obj)
124673b2d42SJoel Stanley {
125673b2d42SJoel Stanley     NRF51State *s = NRF51_SOC(obj);
126673b2d42SJoel Stanley 
127673b2d42SJoel Stanley     memory_region_init(&s->container, obj, "nrf51-container", UINT64_MAX);
128673b2d42SJoel Stanley 
129673b2d42SJoel Stanley     sysbus_init_child_obj(OBJECT(s), "armv6m", OBJECT(&s->cpu), sizeof(s->cpu),
130673b2d42SJoel Stanley                           TYPE_ARMV7M);
131673b2d42SJoel Stanley     qdev_prop_set_string(DEVICE(&s->cpu), "cpu-type",
132673b2d42SJoel Stanley                          ARM_CPU_TYPE_NAME("cortex-m0"));
133673b2d42SJoel Stanley     qdev_prop_set_uint32(DEVICE(&s->cpu), "num-irq", 32);
134b0014913SJulia Suvorova 
135b0014913SJulia Suvorova     sysbus_init_child_obj(obj, "uart", &s->uart, sizeof(s->uart),
136b0014913SJulia Suvorova                            TYPE_NRF51_UART);
137b0014913SJulia Suvorova     object_property_add_alias(obj, "serial0", OBJECT(&s->uart), "chardev",
138b0014913SJulia Suvorova                               &error_abort);
139f30890deSSteffen Görtz 
140f30890deSSteffen Görtz     sysbus_init_child_obj(obj, "rng", &s->rng, sizeof(s->rng),
141f30890deSSteffen Görtz                            TYPE_NRF51_RNG);
142*bb42c4cbSSteffen Görtz 
143*bb42c4cbSSteffen Görtz     sysbus_init_child_obj(obj, "gpio", &s->gpio, sizeof(s->gpio),
144*bb42c4cbSSteffen Görtz                           TYPE_NRF51_GPIO);
145673b2d42SJoel Stanley }
146673b2d42SJoel Stanley 
147673b2d42SJoel Stanley static Property nrf51_soc_properties[] = {
148673b2d42SJoel Stanley     DEFINE_PROP_LINK("memory", NRF51State, board_memory, TYPE_MEMORY_REGION,
149673b2d42SJoel Stanley                      MemoryRegion *),
150673b2d42SJoel Stanley     DEFINE_PROP_UINT32("sram-size", NRF51State, sram_size, NRF51822_SRAM_SIZE),
151673b2d42SJoel Stanley     DEFINE_PROP_UINT32("flash-size", NRF51State, flash_size,
152673b2d42SJoel Stanley                        NRF51822_FLASH_SIZE),
153673b2d42SJoel Stanley     DEFINE_PROP_END_OF_LIST(),
154673b2d42SJoel Stanley };
155673b2d42SJoel Stanley 
156673b2d42SJoel Stanley static void nrf51_soc_class_init(ObjectClass *klass, void *data)
157673b2d42SJoel Stanley {
158673b2d42SJoel Stanley     DeviceClass *dc = DEVICE_CLASS(klass);
159673b2d42SJoel Stanley 
160673b2d42SJoel Stanley     dc->realize = nrf51_soc_realize;
161673b2d42SJoel Stanley     dc->props = nrf51_soc_properties;
162673b2d42SJoel Stanley }
163673b2d42SJoel Stanley 
164673b2d42SJoel Stanley static const TypeInfo nrf51_soc_info = {
165673b2d42SJoel Stanley     .name          = TYPE_NRF51_SOC,
166673b2d42SJoel Stanley     .parent        = TYPE_SYS_BUS_DEVICE,
167673b2d42SJoel Stanley     .instance_size = sizeof(NRF51State),
168673b2d42SJoel Stanley     .instance_init = nrf51_soc_init,
169673b2d42SJoel Stanley     .class_init    = nrf51_soc_class_init,
170673b2d42SJoel Stanley };
171673b2d42SJoel Stanley 
172673b2d42SJoel Stanley static void nrf51_soc_types(void)
173673b2d42SJoel Stanley {
174673b2d42SJoel Stanley     type_register_static(&nrf51_soc_info);
175673b2d42SJoel Stanley }
176673b2d42SJoel Stanley type_init(nrf51_soc_types)
177