xref: /qemu/hw/arm/bcm2836.c (revision ca1d323ca504e68af2c1b19dc99ec73054359507)
1bad56236SAndrew Baumann /*
2bad56236SAndrew Baumann  * Raspberry Pi emulation (c) 2012 Gregory Estrade
3bad56236SAndrew Baumann  * Upstreaming code cleanup [including bcm2835_*] (c) 2013 Jan Petrous
4bad56236SAndrew Baumann  *
5bad56236SAndrew Baumann  * Rasperry Pi 2 emulation and refactoring Copyright (c) 2015, Microsoft
6bad56236SAndrew Baumann  * Written by Andrew Baumann
7bad56236SAndrew Baumann  *
86111a0c0SPhilippe Mathieu-Daudé  * This work is licensed under the terms of the GNU GPL, version 2 or later.
96111a0c0SPhilippe Mathieu-Daudé  * See the COPYING file in the top-level directory.
10bad56236SAndrew Baumann  */
11bad56236SAndrew Baumann 
12c964b660SPeter Maydell #include "qemu/osdep.h"
13da34e65cSMarkus Armbruster #include "qapi/error.h"
140b8fa32fSMarkus Armbruster #include "qemu/module.h"
15bad56236SAndrew Baumann #include "hw/arm/bcm2836.h"
16bad56236SAndrew Baumann #include "hw/arm/raspi_platform.h"
17bad56236SAndrew Baumann #include "hw/sysbus.h"
18bad56236SAndrew Baumann 
19a91179e7SPhilippe Mathieu-Daudé struct BCM283XClass {
2058b35028SPhilippe Mathieu-Daudé     /*< private >*/
2158b35028SPhilippe Mathieu-Daudé     DeviceClass parent_class;
2258b35028SPhilippe Mathieu-Daudé     /*< public >*/
230fd74f03SPeter Maydell     const char *name;
24210f4784SPeter Maydell     const char *cpu_type;
2525ea2884SPhilippe Mathieu-Daudé     unsigned core_count;
26d0567e94SPhilippe Mathieu-Daudé     hwaddr peri_base; /* Peripheral base address seen by the CPU */
27d0567e94SPhilippe Mathieu-Daudé     hwaddr ctrl_base; /* Interrupt controller and mailboxes etc. */
281bcb4d16SPeter Maydell     int clusterid;
29a91179e7SPhilippe Mathieu-Daudé };
3058b35028SPhilippe Mathieu-Daudé 
3196c741d7SPhilippe Mathieu-Daudé static Property bcm2836_enabled_cores_property =
3296c741d7SPhilippe Mathieu-Daudé     DEFINE_PROP_UINT32("enabled-cpus", BCM283XState, enabled_cpus, 0);
3396c741d7SPhilippe Mathieu-Daudé 
34bad56236SAndrew Baumann static void bcm2836_init(Object *obj)
35bad56236SAndrew Baumann {
36926dcdf0SPeter Maydell     BCM283XState *s = BCM283X(obj);
37210f4784SPeter Maydell     BCM283XClass *bc = BCM283X_GET_CLASS(obj);
38210f4784SPeter Maydell     int n;
39210f4784SPeter Maydell 
4025ea2884SPhilippe Mathieu-Daudé     for (n = 0; n < bc->core_count; n++) {
415e5e9ed6SPhilippe Mathieu-Daudé         object_initialize_child(obj, "cpu[*]", &s->cpu[n].core,
4234d1a4f5SPhilippe Mathieu-Daudé                                 bc->cpu_type);
43210f4784SPeter Maydell     }
4496c741d7SPhilippe Mathieu-Daudé     if (bc->core_count > 1) {
4596c741d7SPhilippe Mathieu-Daudé         qdev_property_add_static(DEVICE(obj), &bcm2836_enabled_cores_property);
4696c741d7SPhilippe Mathieu-Daudé         qdev_prop_set_uint32(DEVICE(obj), "enabled-cpus", bc->core_count);
4796c741d7SPhilippe Mathieu-Daudé     }
48bad56236SAndrew Baumann 
49f5600924SPhilippe Mathieu-Daudé     if (bc->ctrl_base) {
50f5600924SPhilippe Mathieu-Daudé         object_initialize_child(obj, "control", &s->control,
51f5600924SPhilippe Mathieu-Daudé                                 TYPE_BCM2836_CONTROL);
52f5600924SPhilippe Mathieu-Daudé     }
53bad56236SAndrew Baumann 
54db873cc5SMarkus Armbruster     object_initialize_child(obj, "peripherals", &s->peripherals,
55db873cc5SMarkus Armbruster                             TYPE_BCM2835_PERIPHERALS);
56f0afa731SStephen Warren     object_property_add_alias(obj, "board-rev", OBJECT(&s->peripherals),
57d2623129SMarkus Armbruster                               "board-rev");
58f802ff1eSDaniel Bertalan     object_property_add_alias(obj, "command-line", OBJECT(&s->peripherals),
59f802ff1eSDaniel Bertalan                               "command-line");
605e9c2a8dSGrégory ESTRADE     object_property_add_alias(obj, "vcram-size", OBJECT(&s->peripherals),
61d2623129SMarkus Armbruster                               "vcram-size");
62bad56236SAndrew Baumann }
63bad56236SAndrew Baumann 
64f5600924SPhilippe Mathieu-Daudé static bool bcm283x_common_realize(DeviceState *dev, Error **errp)
65bad56236SAndrew Baumann {
66926dcdf0SPeter Maydell     BCM283XState *s = BCM283X(dev);
671bcb4d16SPeter Maydell     BCM283XClass *bc = BCM283X_GET_CLASS(dev);
68bad56236SAndrew Baumann     Object *obj;
69bad56236SAndrew Baumann 
70bad56236SAndrew Baumann     /* common peripherals from bcm2835 */
71bad56236SAndrew Baumann 
724d21fcd5SMarkus Armbruster     obj = object_property_get_link(OBJECT(dev), "ram", &error_abort);
73bad56236SAndrew Baumann 
74d2623129SMarkus Armbruster     object_property_add_const_link(OBJECT(&s->peripherals), "ram", obj);
75bad56236SAndrew Baumann 
76668f62ecSMarkus Armbruster     if (!sysbus_realize(SYS_BUS_DEVICE(&s->peripherals), errp)) {
77f5600924SPhilippe Mathieu-Daudé         return false;
78bad56236SAndrew Baumann     }
79bad56236SAndrew Baumann 
80a55b53a2SAndrew Baumann     object_property_add_alias(OBJECT(s), "sd-bus", OBJECT(&s->peripherals),
81d2623129SMarkus Armbruster                               "sd-bus");
82a55b53a2SAndrew Baumann 
83bad56236SAndrew Baumann     sysbus_mmio_map_overlap(SYS_BUS_DEVICE(&s->peripherals), 0,
8434d1a4f5SPhilippe Mathieu-Daudé                             bc->peri_base, 1);
85f5600924SPhilippe Mathieu-Daudé     return true;
86f5600924SPhilippe Mathieu-Daudé }
87f5600924SPhilippe Mathieu-Daudé 
88df6cf08dSPhilippe Mathieu-Daudé static void bcm2835_realize(DeviceState *dev, Error **errp)
89df6cf08dSPhilippe Mathieu-Daudé {
90df6cf08dSPhilippe Mathieu-Daudé     BCM283XState *s = BCM283X(dev);
91df6cf08dSPhilippe Mathieu-Daudé 
92df6cf08dSPhilippe Mathieu-Daudé     if (!bcm283x_common_realize(dev, errp)) {
93df6cf08dSPhilippe Mathieu-Daudé         return;
94df6cf08dSPhilippe Mathieu-Daudé     }
95df6cf08dSPhilippe Mathieu-Daudé 
96df6cf08dSPhilippe Mathieu-Daudé     if (!qdev_realize(DEVICE(&s->cpu[0].core), NULL, errp)) {
97df6cf08dSPhilippe Mathieu-Daudé         return;
98df6cf08dSPhilippe Mathieu-Daudé     }
99df6cf08dSPhilippe Mathieu-Daudé 
100df6cf08dSPhilippe Mathieu-Daudé     /* Connect irq/fiq outputs from the interrupt controller. */
101df6cf08dSPhilippe Mathieu-Daudé     sysbus_connect_irq(SYS_BUS_DEVICE(&s->peripherals), 0,
102df6cf08dSPhilippe Mathieu-Daudé             qdev_get_gpio_in(DEVICE(&s->cpu[0].core), ARM_CPU_IRQ));
103df6cf08dSPhilippe Mathieu-Daudé     sysbus_connect_irq(SYS_BUS_DEVICE(&s->peripherals), 1,
104df6cf08dSPhilippe Mathieu-Daudé             qdev_get_gpio_in(DEVICE(&s->cpu[0].core), ARM_CPU_FIQ));
105df6cf08dSPhilippe Mathieu-Daudé }
106df6cf08dSPhilippe Mathieu-Daudé 
107f5600924SPhilippe Mathieu-Daudé static void bcm2836_realize(DeviceState *dev, Error **errp)
108f5600924SPhilippe Mathieu-Daudé {
109f5600924SPhilippe Mathieu-Daudé     BCM283XState *s = BCM283X(dev);
110f5600924SPhilippe Mathieu-Daudé     BCM283XClass *bc = BCM283X_GET_CLASS(dev);
111f5600924SPhilippe Mathieu-Daudé     int n;
112f5600924SPhilippe Mathieu-Daudé 
113f5600924SPhilippe Mathieu-Daudé     if (!bcm283x_common_realize(dev, errp)) {
114f5600924SPhilippe Mathieu-Daudé         return;
115f5600924SPhilippe Mathieu-Daudé     }
116bad56236SAndrew Baumann 
117bad56236SAndrew Baumann     /* bcm2836 interrupt controller (and mailboxes, etc.) */
118668f62ecSMarkus Armbruster     if (!sysbus_realize(SYS_BUS_DEVICE(&s->control), errp)) {
119bad56236SAndrew Baumann         return;
120bad56236SAndrew Baumann     }
121bad56236SAndrew Baumann 
12234d1a4f5SPhilippe Mathieu-Daudé     sysbus_mmio_map(SYS_BUS_DEVICE(&s->control), 0, bc->ctrl_base);
123bad56236SAndrew Baumann 
124bad56236SAndrew Baumann     sysbus_connect_irq(SYS_BUS_DEVICE(&s->peripherals), 0,
125bad56236SAndrew Baumann         qdev_get_gpio_in_named(DEVICE(&s->control), "gpu-irq", 0));
126bad56236SAndrew Baumann     sysbus_connect_irq(SYS_BUS_DEVICE(&s->peripherals), 1,
127bad56236SAndrew Baumann         qdev_get_gpio_in_named(DEVICE(&s->control), "gpu-fiq", 0));
128bad56236SAndrew Baumann 
129926dcdf0SPeter Maydell     for (n = 0; n < BCM283X_NCPUS; n++) {
1301bcb4d16SPeter Maydell         /* TODO: this should be converted to a property of ARM_CPU */
13134d1a4f5SPhilippe Mathieu-Daudé         s->cpu[n].core.mp_affinity = (bc->clusterid << 8) | n;
132bad56236SAndrew Baumann 
133bad56236SAndrew Baumann         /* set periphbase/CBAR value for CPU-local registers */
134*ca1d323cSPhilippe Mathieu-Daudé         object_property_set_int(OBJECT(&s->cpu[n].core), "reset-cbar",
135*ca1d323cSPhilippe Mathieu-Daudé                                 bc->peri_base, &error_abort);
136bad56236SAndrew Baumann 
137bad56236SAndrew Baumann         /* start powered off if not enabled */
138778a2dc5SMarkus Armbruster         if (!object_property_set_bool(OBJECT(&s->cpu[n].core),
139778a2dc5SMarkus Armbruster                                       "start-powered-off",
140778a2dc5SMarkus Armbruster                                       n >= s->enabled_cpus,
141668f62ecSMarkus Armbruster                                       errp)) {
142bad56236SAndrew Baumann             return;
143bad56236SAndrew Baumann         }
144bad56236SAndrew Baumann 
145668f62ecSMarkus Armbruster         if (!qdev_realize(DEVICE(&s->cpu[n].core), NULL, errp)) {
146bad56236SAndrew Baumann             return;
147bad56236SAndrew Baumann         }
148bad56236SAndrew Baumann 
149bad56236SAndrew Baumann         /* Connect irq/fiq outputs from the interrupt controller. */
150bad56236SAndrew Baumann         qdev_connect_gpio_out_named(DEVICE(&s->control), "irq", n,
1515e5e9ed6SPhilippe Mathieu-Daudé                 qdev_get_gpio_in(DEVICE(&s->cpu[n].core), ARM_CPU_IRQ));
152bad56236SAndrew Baumann         qdev_connect_gpio_out_named(DEVICE(&s->control), "fiq", n,
1535e5e9ed6SPhilippe Mathieu-Daudé                 qdev_get_gpio_in(DEVICE(&s->cpu[n].core), ARM_CPU_FIQ));
154bad56236SAndrew Baumann 
155bad56236SAndrew Baumann         /* Connect timers from the CPU to the interrupt controller */
1565e5e9ed6SPhilippe Mathieu-Daudé         qdev_connect_gpio_out(DEVICE(&s->cpu[n].core), GTIMER_PHYS,
1570dc19823SPeter Maydell                 qdev_get_gpio_in_named(DEVICE(&s->control), "cntpnsirq", n));
1585e5e9ed6SPhilippe Mathieu-Daudé         qdev_connect_gpio_out(DEVICE(&s->cpu[n].core), GTIMER_VIRT,
159bad56236SAndrew Baumann                 qdev_get_gpio_in_named(DEVICE(&s->control), "cntvirq", n));
1605e5e9ed6SPhilippe Mathieu-Daudé         qdev_connect_gpio_out(DEVICE(&s->cpu[n].core), GTIMER_HYP,
1610dc19823SPeter Maydell                 qdev_get_gpio_in_named(DEVICE(&s->control), "cnthpirq", n));
1625e5e9ed6SPhilippe Mathieu-Daudé         qdev_connect_gpio_out(DEVICE(&s->cpu[n].core), GTIMER_SEC,
1630dc19823SPeter Maydell                 qdev_get_gpio_in_named(DEVICE(&s->control), "cntpsirq", n));
164bad56236SAndrew Baumann     }
165bad56236SAndrew Baumann }
166bad56236SAndrew Baumann 
1670fd74f03SPeter Maydell static void bcm283x_class_init(ObjectClass *oc, void *data)
168bad56236SAndrew Baumann {
169bad56236SAndrew Baumann     DeviceClass *dc = DEVICE_CLASS(oc);
170bad56236SAndrew Baumann 
171cccf96c3SThomas Huth     /* Reason: Must be wired up in code (see raspi_init() function) */
172cccf96c3SThomas Huth     dc->user_creatable = false;
173bad56236SAndrew Baumann }
174bad56236SAndrew Baumann 
175df6cf08dSPhilippe Mathieu-Daudé static void bcm2835_class_init(ObjectClass *oc, void *data)
176df6cf08dSPhilippe Mathieu-Daudé {
177df6cf08dSPhilippe Mathieu-Daudé     DeviceClass *dc = DEVICE_CLASS(oc);
178df6cf08dSPhilippe Mathieu-Daudé     BCM283XClass *bc = BCM283X_CLASS(oc);
179df6cf08dSPhilippe Mathieu-Daudé 
180df6cf08dSPhilippe Mathieu-Daudé     bc->cpu_type = ARM_CPU_TYPE_NAME("arm1176");
181df6cf08dSPhilippe Mathieu-Daudé     bc->core_count = 1;
182df6cf08dSPhilippe Mathieu-Daudé     bc->peri_base = 0x20000000;
183df6cf08dSPhilippe Mathieu-Daudé     dc->realize = bcm2835_realize;
184df6cf08dSPhilippe Mathieu-Daudé };
185df6cf08dSPhilippe Mathieu-Daudé 
18634d1a4f5SPhilippe Mathieu-Daudé static void bcm2836_class_init(ObjectClass *oc, void *data)
18734d1a4f5SPhilippe Mathieu-Daudé {
18834d1a4f5SPhilippe Mathieu-Daudé     DeviceClass *dc = DEVICE_CLASS(oc);
18934d1a4f5SPhilippe Mathieu-Daudé     BCM283XClass *bc = BCM283X_CLASS(oc);
19034d1a4f5SPhilippe Mathieu-Daudé 
19134d1a4f5SPhilippe Mathieu-Daudé     bc->cpu_type = ARM_CPU_TYPE_NAME("cortex-a7");
19225ea2884SPhilippe Mathieu-Daudé     bc->core_count = BCM283X_NCPUS;
19334d1a4f5SPhilippe Mathieu-Daudé     bc->peri_base = 0x3f000000;
19434d1a4f5SPhilippe Mathieu-Daudé     bc->ctrl_base = 0x40000000;
19534d1a4f5SPhilippe Mathieu-Daudé     bc->clusterid = 0xf;
19634d1a4f5SPhilippe Mathieu-Daudé     dc->realize = bcm2836_realize;
19734d1a4f5SPhilippe Mathieu-Daudé };
19834d1a4f5SPhilippe Mathieu-Daudé 
19934d1a4f5SPhilippe Mathieu-Daudé #ifdef TARGET_AARCH64
20034d1a4f5SPhilippe Mathieu-Daudé static void bcm2837_class_init(ObjectClass *oc, void *data)
20134d1a4f5SPhilippe Mathieu-Daudé {
20234d1a4f5SPhilippe Mathieu-Daudé     DeviceClass *dc = DEVICE_CLASS(oc);
20334d1a4f5SPhilippe Mathieu-Daudé     BCM283XClass *bc = BCM283X_CLASS(oc);
20434d1a4f5SPhilippe Mathieu-Daudé 
20534d1a4f5SPhilippe Mathieu-Daudé     bc->cpu_type = ARM_CPU_TYPE_NAME("cortex-a53");
20625ea2884SPhilippe Mathieu-Daudé     bc->core_count = BCM283X_NCPUS;
20734d1a4f5SPhilippe Mathieu-Daudé     bc->peri_base = 0x3f000000;
20834d1a4f5SPhilippe Mathieu-Daudé     bc->ctrl_base = 0x40000000;
20934d1a4f5SPhilippe Mathieu-Daudé     bc->clusterid = 0x0;
21034d1a4f5SPhilippe Mathieu-Daudé     dc->realize = bcm2836_realize;
21134d1a4f5SPhilippe Mathieu-Daudé };
21234d1a4f5SPhilippe Mathieu-Daudé #endif
21334d1a4f5SPhilippe Mathieu-Daudé 
21434d1a4f5SPhilippe Mathieu-Daudé static const TypeInfo bcm283x_types[] = {
21534d1a4f5SPhilippe Mathieu-Daudé     {
216df6cf08dSPhilippe Mathieu-Daudé         .name           = TYPE_BCM2835,
217df6cf08dSPhilippe Mathieu-Daudé         .parent         = TYPE_BCM283X,
218df6cf08dSPhilippe Mathieu-Daudé         .class_init     = bcm2835_class_init,
219df6cf08dSPhilippe Mathieu-Daudé     }, {
22034d1a4f5SPhilippe Mathieu-Daudé         .name           = TYPE_BCM2836,
22134d1a4f5SPhilippe Mathieu-Daudé         .parent         = TYPE_BCM283X,
22234d1a4f5SPhilippe Mathieu-Daudé         .class_init     = bcm2836_class_init,
22334d1a4f5SPhilippe Mathieu-Daudé #ifdef TARGET_AARCH64
22434d1a4f5SPhilippe Mathieu-Daudé     }, {
22534d1a4f5SPhilippe Mathieu-Daudé         .name           = TYPE_BCM2837,
22634d1a4f5SPhilippe Mathieu-Daudé         .parent         = TYPE_BCM283X,
22734d1a4f5SPhilippe Mathieu-Daudé         .class_init     = bcm2837_class_init,
22834d1a4f5SPhilippe Mathieu-Daudé #endif
22934d1a4f5SPhilippe Mathieu-Daudé     }, {
230926dcdf0SPeter Maydell         .name           = TYPE_BCM283X,
2313d260cf3SPeter Maydell         .parent         = TYPE_DEVICE,
232926dcdf0SPeter Maydell         .instance_size  = sizeof(BCM283XState),
233bad56236SAndrew Baumann         .instance_init  = bcm2836_init,
2340fd74f03SPeter Maydell         .class_size     = sizeof(BCM283XClass),
2350fd74f03SPeter Maydell         .class_init     = bcm283x_class_init,
23634d1a4f5SPhilippe Mathieu-Daudé         .abstract       = true,
23734d1a4f5SPhilippe Mathieu-Daudé     }
2380fd74f03SPeter Maydell };
239bad56236SAndrew Baumann 
24034d1a4f5SPhilippe Mathieu-Daudé DEFINE_TYPES(bcm283x_types)
241