xref: /openbmc/qemu/hw/arm/bcm2836.c (revision effd60c8)
1 /*
2  * Raspberry Pi emulation (c) 2012 Gregory Estrade
3  * Upstreaming code cleanup [including bcm2835_*] (c) 2013 Jan Petrous
4  *
5  * Rasperry Pi 2 emulation and refactoring Copyright (c) 2015, Microsoft
6  * Written by Andrew Baumann
7  *
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.
10  */
11 
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"
18 
19 struct BCM283XClass {
20     /*< private >*/
21     DeviceClass parent_class;
22     /*< public >*/
23     const char *name;
24     const char *cpu_type;
25     unsigned core_count;
26     hwaddr peri_base; /* Peripheral base address seen by the CPU */
27     hwaddr ctrl_base; /* Interrupt controller and mailboxes etc. */
28     int clusterid;
29 };
30 
31 static Property bcm2836_enabled_cores_property =
32     DEFINE_PROP_UINT32("enabled-cpus", BCM283XState, enabled_cpus, 0);
33 
34 static void bcm2836_init(Object *obj)
35 {
36     BCM283XState *s = BCM283X(obj);
37     BCM283XClass *bc = BCM283X_GET_CLASS(obj);
38     int n;
39 
40     for (n = 0; n < bc->core_count; n++) {
41         object_initialize_child(obj, "cpu[*]", &s->cpu[n].core,
42                                 bc->cpu_type);
43     }
44     if (bc->core_count > 1) {
45         qdev_property_add_static(DEVICE(obj), &bcm2836_enabled_cores_property);
46         qdev_prop_set_uint32(DEVICE(obj), "enabled-cpus", bc->core_count);
47     }
48 
49     if (bc->ctrl_base) {
50         object_initialize_child(obj, "control", &s->control,
51                                 TYPE_BCM2836_CONTROL);
52     }
53 
54     object_initialize_child(obj, "peripherals", &s->peripherals,
55                             TYPE_BCM2835_PERIPHERALS);
56     object_property_add_alias(obj, "board-rev", OBJECT(&s->peripherals),
57                               "board-rev");
58     object_property_add_alias(obj, "command-line", OBJECT(&s->peripherals),
59                               "command-line");
60     object_property_add_alias(obj, "vcram-size", OBJECT(&s->peripherals),
61                               "vcram-size");
62 }
63 
64 static bool bcm283x_common_realize(DeviceState *dev, Error **errp)
65 {
66     BCM283XState *s = BCM283X(dev);
67     BCM283XClass *bc = BCM283X_GET_CLASS(dev);
68     Object *obj;
69 
70     /* common peripherals from bcm2835 */
71 
72     obj = object_property_get_link(OBJECT(dev), "ram", &error_abort);
73 
74     object_property_add_const_link(OBJECT(&s->peripherals), "ram", obj);
75 
76     if (!sysbus_realize(SYS_BUS_DEVICE(&s->peripherals), errp)) {
77         return false;
78     }
79 
80     object_property_add_alias(OBJECT(s), "sd-bus", OBJECT(&s->peripherals),
81                               "sd-bus");
82 
83     sysbus_mmio_map_overlap(SYS_BUS_DEVICE(&s->peripherals), 0,
84                             bc->peri_base, 1);
85     return true;
86 }
87 
88 static void bcm2835_realize(DeviceState *dev, Error **errp)
89 {
90     BCM283XState *s = BCM283X(dev);
91 
92     if (!bcm283x_common_realize(dev, errp)) {
93         return;
94     }
95 
96     if (!qdev_realize(DEVICE(&s->cpu[0].core), NULL, errp)) {
97         return;
98     }
99 
100     /* Connect irq/fiq outputs from the interrupt controller. */
101     sysbus_connect_irq(SYS_BUS_DEVICE(&s->peripherals), 0,
102             qdev_get_gpio_in(DEVICE(&s->cpu[0].core), ARM_CPU_IRQ));
103     sysbus_connect_irq(SYS_BUS_DEVICE(&s->peripherals), 1,
104             qdev_get_gpio_in(DEVICE(&s->cpu[0].core), ARM_CPU_FIQ));
105 }
106 
107 static void bcm2836_realize(DeviceState *dev, Error **errp)
108 {
109     BCM283XState *s = BCM283X(dev);
110     BCM283XClass *bc = BCM283X_GET_CLASS(dev);
111     int n;
112 
113     if (!bcm283x_common_realize(dev, errp)) {
114         return;
115     }
116 
117     /* bcm2836 interrupt controller (and mailboxes, etc.) */
118     if (!sysbus_realize(SYS_BUS_DEVICE(&s->control), errp)) {
119         return;
120     }
121 
122     sysbus_mmio_map(SYS_BUS_DEVICE(&s->control), 0, bc->ctrl_base);
123 
124     sysbus_connect_irq(SYS_BUS_DEVICE(&s->peripherals), 0,
125         qdev_get_gpio_in_named(DEVICE(&s->control), "gpu-irq", 0));
126     sysbus_connect_irq(SYS_BUS_DEVICE(&s->peripherals), 1,
127         qdev_get_gpio_in_named(DEVICE(&s->control), "gpu-fiq", 0));
128 
129     for (n = 0; n < BCM283X_NCPUS; n++) {
130         object_property_set_int(OBJECT(&s->cpu[n].core), "mp-affinity",
131                                 (bc->clusterid << 8) | n, &error_abort);
132 
133         /* set periphbase/CBAR value for CPU-local registers */
134         object_property_set_int(OBJECT(&s->cpu[n].core), "reset-cbar",
135                                 bc->peri_base, &error_abort);
136 
137         /* start powered off if not enabled */
138         object_property_set_bool(OBJECT(&s->cpu[n].core), "start-powered-off",
139                                  n >= s->enabled_cpus, &error_abort);
140 
141         if (!qdev_realize(DEVICE(&s->cpu[n].core), NULL, errp)) {
142             return;
143         }
144 
145         /* Connect irq/fiq outputs from the interrupt controller. */
146         qdev_connect_gpio_out_named(DEVICE(&s->control), "irq", n,
147                 qdev_get_gpio_in(DEVICE(&s->cpu[n].core), ARM_CPU_IRQ));
148         qdev_connect_gpio_out_named(DEVICE(&s->control), "fiq", n,
149                 qdev_get_gpio_in(DEVICE(&s->cpu[n].core), ARM_CPU_FIQ));
150 
151         /* Connect timers from the CPU to the interrupt controller */
152         qdev_connect_gpio_out(DEVICE(&s->cpu[n].core), GTIMER_PHYS,
153                 qdev_get_gpio_in_named(DEVICE(&s->control), "cntpnsirq", n));
154         qdev_connect_gpio_out(DEVICE(&s->cpu[n].core), GTIMER_VIRT,
155                 qdev_get_gpio_in_named(DEVICE(&s->control), "cntvirq", n));
156         qdev_connect_gpio_out(DEVICE(&s->cpu[n].core), GTIMER_HYP,
157                 qdev_get_gpio_in_named(DEVICE(&s->control), "cnthpirq", n));
158         qdev_connect_gpio_out(DEVICE(&s->cpu[n].core), GTIMER_SEC,
159                 qdev_get_gpio_in_named(DEVICE(&s->control), "cntpsirq", n));
160     }
161 }
162 
163 static void bcm283x_class_init(ObjectClass *oc, void *data)
164 {
165     DeviceClass *dc = DEVICE_CLASS(oc);
166 
167     /* Reason: Must be wired up in code (see raspi_init() function) */
168     dc->user_creatable = false;
169 }
170 
171 static void bcm2835_class_init(ObjectClass *oc, void *data)
172 {
173     DeviceClass *dc = DEVICE_CLASS(oc);
174     BCM283XClass *bc = BCM283X_CLASS(oc);
175 
176     bc->cpu_type = ARM_CPU_TYPE_NAME("arm1176");
177     bc->core_count = 1;
178     bc->peri_base = 0x20000000;
179     dc->realize = bcm2835_realize;
180 };
181 
182 static void bcm2836_class_init(ObjectClass *oc, void *data)
183 {
184     DeviceClass *dc = DEVICE_CLASS(oc);
185     BCM283XClass *bc = BCM283X_CLASS(oc);
186 
187     bc->cpu_type = ARM_CPU_TYPE_NAME("cortex-a7");
188     bc->core_count = BCM283X_NCPUS;
189     bc->peri_base = 0x3f000000;
190     bc->ctrl_base = 0x40000000;
191     bc->clusterid = 0xf;
192     dc->realize = bcm2836_realize;
193 };
194 
195 #ifdef TARGET_AARCH64
196 static void bcm2837_class_init(ObjectClass *oc, void *data)
197 {
198     DeviceClass *dc = DEVICE_CLASS(oc);
199     BCM283XClass *bc = BCM283X_CLASS(oc);
200 
201     bc->cpu_type = ARM_CPU_TYPE_NAME("cortex-a53");
202     bc->core_count = BCM283X_NCPUS;
203     bc->peri_base = 0x3f000000;
204     bc->ctrl_base = 0x40000000;
205     bc->clusterid = 0x0;
206     dc->realize = bcm2836_realize;
207 };
208 #endif
209 
210 static const TypeInfo bcm283x_types[] = {
211     {
212         .name           = TYPE_BCM2835,
213         .parent         = TYPE_BCM283X,
214         .class_init     = bcm2835_class_init,
215     }, {
216         .name           = TYPE_BCM2836,
217         .parent         = TYPE_BCM283X,
218         .class_init     = bcm2836_class_init,
219 #ifdef TARGET_AARCH64
220     }, {
221         .name           = TYPE_BCM2837,
222         .parent         = TYPE_BCM283X,
223         .class_init     = bcm2837_class_init,
224 #endif
225     }, {
226         .name           = TYPE_BCM283X,
227         .parent         = TYPE_DEVICE,
228         .instance_size  = sizeof(BCM283XState),
229         .instance_init  = bcm2836_init,
230         .class_size     = sizeof(BCM283XClass),
231         .class_init     = bcm283x_class_init,
232         .abstract       = true,
233     }
234 };
235 
236 DEFINE_TYPES(bcm283x_types)
237