2 * Raspberry Pi emulation (c) 2012 Gregory Estrade
3 * Upstreaming code cleanup [including bcm2835_*] (c) 2013 Jan Petrous
5 * Rasperry Pi 2 emulation and refactoring Copyright (c) 2015, Microsoft
6 * Written by Andrew Baumann
8 * This work is licensed under the terms of the GNU GPL, version 2 or later.
9 * See the COPYING file in the top-level directory.
12 #include "qemu/osdep.h"
13 #include "qapi/error.h"
14 #include "qemu/module.h"
15 #include "hw/arm/bcm2836.h"
16 #include "hw/arm/raspi_platform.h"
17 #include "hw/sysbus.h"
19 typedef struct BCM283XClass
{
21 DeviceClass parent_class
;
26 hwaddr peri_base
; /* Peripheral base address seen by the CPU */
27 hwaddr ctrl_base
; /* Interrupt controller and mailboxes etc. */
31 #define BCM283X_CLASS(klass) \
32 OBJECT_CLASS_CHECK(BCM283XClass, (klass), TYPE_BCM283X)
33 #define BCM283X_GET_CLASS(obj) \
34 OBJECT_GET_CLASS(BCM283XClass, (obj), TYPE_BCM283X)
36 static Property bcm2836_enabled_cores_property
=
37 DEFINE_PROP_UINT32("enabled-cpus", BCM283XState
, enabled_cpus
, 0);
39 static void bcm2836_init(Object
*obj
)
41 BCM283XState
*s
= BCM283X(obj
);
42 BCM283XClass
*bc
= BCM283X_GET_CLASS(obj
);
45 for (n
= 0; n
< bc
->core_count
; n
++) {
46 object_initialize_child(obj
, "cpu[*]", &s
->cpu
[n
].core
,
49 if (bc
->core_count
> 1) {
50 qdev_property_add_static(DEVICE(obj
), &bcm2836_enabled_cores_property
);
51 qdev_prop_set_uint32(DEVICE(obj
), "enabled-cpus", bc
->core_count
);
55 object_initialize_child(obj
, "control", &s
->control
,
56 TYPE_BCM2836_CONTROL
);
59 object_initialize_child(obj
, "peripherals", &s
->peripherals
,
60 TYPE_BCM2835_PERIPHERALS
);
61 object_property_add_alias(obj
, "board-rev", OBJECT(&s
->peripherals
),
63 object_property_add_alias(obj
, "vcram-size", OBJECT(&s
->peripherals
),
67 static bool bcm283x_common_realize(DeviceState
*dev
, Error
**errp
)
69 BCM283XState
*s
= BCM283X(dev
);
70 BCM283XClass
*bc
= BCM283X_GET_CLASS(dev
);
73 /* common peripherals from bcm2835 */
75 obj
= object_property_get_link(OBJECT(dev
), "ram", &error_abort
);
77 object_property_add_const_link(OBJECT(&s
->peripherals
), "ram", obj
);
79 if (!sysbus_realize(SYS_BUS_DEVICE(&s
->peripherals
), errp
)) {
83 object_property_add_alias(OBJECT(s
), "sd-bus", OBJECT(&s
->peripherals
),
86 sysbus_mmio_map_overlap(SYS_BUS_DEVICE(&s
->peripherals
), 0,
91 static void bcm2835_realize(DeviceState
*dev
, Error
**errp
)
93 BCM283XState
*s
= BCM283X(dev
);
95 if (!bcm283x_common_realize(dev
, errp
)) {
99 if (!qdev_realize(DEVICE(&s
->cpu
[0].core
), NULL
, errp
)) {
103 /* Connect irq/fiq outputs from the interrupt controller. */
104 sysbus_connect_irq(SYS_BUS_DEVICE(&s
->peripherals
), 0,
105 qdev_get_gpio_in(DEVICE(&s
->cpu
[0].core
), ARM_CPU_IRQ
));
106 sysbus_connect_irq(SYS_BUS_DEVICE(&s
->peripherals
), 1,
107 qdev_get_gpio_in(DEVICE(&s
->cpu
[0].core
), ARM_CPU_FIQ
));
110 static void bcm2836_realize(DeviceState
*dev
, Error
**errp
)
112 BCM283XState
*s
= BCM283X(dev
);
113 BCM283XClass
*bc
= BCM283X_GET_CLASS(dev
);
116 if (!bcm283x_common_realize(dev
, errp
)) {
120 /* bcm2836 interrupt controller (and mailboxes, etc.) */
121 if (!sysbus_realize(SYS_BUS_DEVICE(&s
->control
), errp
)) {
125 sysbus_mmio_map(SYS_BUS_DEVICE(&s
->control
), 0, bc
->ctrl_base
);
127 sysbus_connect_irq(SYS_BUS_DEVICE(&s
->peripherals
), 0,
128 qdev_get_gpio_in_named(DEVICE(&s
->control
), "gpu-irq", 0));
129 sysbus_connect_irq(SYS_BUS_DEVICE(&s
->peripherals
), 1,
130 qdev_get_gpio_in_named(DEVICE(&s
->control
), "gpu-fiq", 0));
132 for (n
= 0; n
< BCM283X_NCPUS
; n
++) {
133 /* TODO: this should be converted to a property of ARM_CPU */
134 s
->cpu
[n
].core
.mp_affinity
= (bc
->clusterid
<< 8) | n
;
136 /* set periphbase/CBAR value for CPU-local registers */
137 if (!object_property_set_int(OBJECT(&s
->cpu
[n
].core
), "reset-cbar",
138 bc
->peri_base
, errp
)) {
142 /* start powered off if not enabled */
143 if (!object_property_set_bool(OBJECT(&s
->cpu
[n
].core
),
145 n
>= s
->enabled_cpus
,
150 if (!qdev_realize(DEVICE(&s
->cpu
[n
].core
), NULL
, errp
)) {
154 /* Connect irq/fiq outputs from the interrupt controller. */
155 qdev_connect_gpio_out_named(DEVICE(&s
->control
), "irq", n
,
156 qdev_get_gpio_in(DEVICE(&s
->cpu
[n
].core
), ARM_CPU_IRQ
));
157 qdev_connect_gpio_out_named(DEVICE(&s
->control
), "fiq", n
,
158 qdev_get_gpio_in(DEVICE(&s
->cpu
[n
].core
), ARM_CPU_FIQ
));
160 /* Connect timers from the CPU to the interrupt controller */
161 qdev_connect_gpio_out(DEVICE(&s
->cpu
[n
].core
), GTIMER_PHYS
,
162 qdev_get_gpio_in_named(DEVICE(&s
->control
), "cntpnsirq", n
));
163 qdev_connect_gpio_out(DEVICE(&s
->cpu
[n
].core
), GTIMER_VIRT
,
164 qdev_get_gpio_in_named(DEVICE(&s
->control
), "cntvirq", n
));
165 qdev_connect_gpio_out(DEVICE(&s
->cpu
[n
].core
), GTIMER_HYP
,
166 qdev_get_gpio_in_named(DEVICE(&s
->control
), "cnthpirq", n
));
167 qdev_connect_gpio_out(DEVICE(&s
->cpu
[n
].core
), GTIMER_SEC
,
168 qdev_get_gpio_in_named(DEVICE(&s
->control
), "cntpsirq", n
));
172 static void bcm283x_class_init(ObjectClass
*oc
, void *data
)
174 DeviceClass
*dc
= DEVICE_CLASS(oc
);
176 /* Reason: Must be wired up in code (see raspi_init() function) */
177 dc
->user_creatable
= false;
180 static void bcm2835_class_init(ObjectClass
*oc
, void *data
)
182 DeviceClass
*dc
= DEVICE_CLASS(oc
);
183 BCM283XClass
*bc
= BCM283X_CLASS(oc
);
185 bc
->cpu_type
= ARM_CPU_TYPE_NAME("arm1176");
187 bc
->peri_base
= 0x20000000;
188 dc
->realize
= bcm2835_realize
;
191 static void bcm2836_class_init(ObjectClass
*oc
, void *data
)
193 DeviceClass
*dc
= DEVICE_CLASS(oc
);
194 BCM283XClass
*bc
= BCM283X_CLASS(oc
);
196 bc
->cpu_type
= ARM_CPU_TYPE_NAME("cortex-a7");
197 bc
->core_count
= BCM283X_NCPUS
;
198 bc
->peri_base
= 0x3f000000;
199 bc
->ctrl_base
= 0x40000000;
201 dc
->realize
= bcm2836_realize
;
204 #ifdef TARGET_AARCH64
205 static void bcm2837_class_init(ObjectClass
*oc
, void *data
)
207 DeviceClass
*dc
= DEVICE_CLASS(oc
);
208 BCM283XClass
*bc
= BCM283X_CLASS(oc
);
210 bc
->cpu_type
= ARM_CPU_TYPE_NAME("cortex-a53");
211 bc
->core_count
= BCM283X_NCPUS
;
212 bc
->peri_base
= 0x3f000000;
213 bc
->ctrl_base
= 0x40000000;
215 dc
->realize
= bcm2836_realize
;
219 static const TypeInfo bcm283x_types
[] = {
221 .name
= TYPE_BCM2835
,
222 .parent
= TYPE_BCM283X
,
223 .class_init
= bcm2835_class_init
,
225 .name
= TYPE_BCM2836
,
226 .parent
= TYPE_BCM283X
,
227 .class_init
= bcm2836_class_init
,
228 #ifdef TARGET_AARCH64
230 .name
= TYPE_BCM2837
,
231 .parent
= TYPE_BCM283X
,
232 .class_init
= bcm2837_class_init
,
235 .name
= TYPE_BCM283X
,
236 .parent
= TYPE_DEVICE
,
237 .instance_size
= sizeof(BCM283XState
),
238 .instance_init
= bcm2836_init
,
239 .class_size
= sizeof(BCM283XClass
),
240 .class_init
= bcm283x_class_init
,
245 DEFINE_TYPES(bcm283x_types
)