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 code is licensed under the GNU GPLv2 and later.
11 #include "qemu/osdep.h"
12 #include "hw/arm/bcm2835_peripherals.h"
13 #include "hw/misc/bcm2835_mbox_defs.h"
14 #include "hw/arm/raspi_platform.h"
16 /* Peripheral base address on the VC (GPU) system bus */
17 #define BCM2835_VC_PERI_BASE 0x7e000000
19 /* Capabilities for SD controller: no DMA, high-speed, default clocks etc. */
20 #define BCM2835_SDHC_CAPAREG 0x52034b4
22 static void bcm2835_peripherals_init(Object
*obj
)
24 BCM2835PeripheralState
*s
= BCM2835_PERIPHERALS(obj
);
26 /* Memory region for peripheral devices, which we export to our parent */
27 memory_region_init(&s
->peri_mr
, obj
,"bcm2835-peripherals", 0x1000000);
28 object_property_add_child(obj
, "peripheral-io", OBJECT(&s
->peri_mr
), NULL
);
29 sysbus_init_mmio(SYS_BUS_DEVICE(s
), &s
->peri_mr
);
31 /* Internal memory region for peripheral bus addresses (not exported) */
32 memory_region_init(&s
->gpu_bus_mr
, obj
, "bcm2835-gpu", (uint64_t)1 << 32);
33 object_property_add_child(obj
, "gpu-bus", OBJECT(&s
->gpu_bus_mr
), NULL
);
35 /* Internal memory region for request/response communication with
36 * mailbox-addressable peripherals (not exported)
38 memory_region_init(&s
->mbox_mr
, obj
, "bcm2835-mbox",
39 MBOX_CHAN_COUNT
<< MBOX_AS_CHAN_SHIFT
);
41 /* Interrupt Controller */
42 object_initialize(&s
->ic
, sizeof(s
->ic
), TYPE_BCM2835_IC
);
43 object_property_add_child(obj
, "ic", OBJECT(&s
->ic
), NULL
);
44 qdev_set_parent_bus(DEVICE(&s
->ic
), sysbus_get_default());
47 s
->uart0
= SYS_BUS_DEVICE(object_new("pl011"));
48 object_property_add_child(obj
, "uart0", OBJECT(s
->uart0
), NULL
);
49 qdev_set_parent_bus(DEVICE(s
->uart0
), sysbus_get_default());
52 object_initialize(&s
->aux
, sizeof(s
->aux
), TYPE_BCM2835_AUX
);
53 object_property_add_child(obj
, "aux", OBJECT(&s
->aux
), NULL
);
54 qdev_set_parent_bus(DEVICE(&s
->aux
), sysbus_get_default());
57 object_initialize(&s
->st
, sizeof(s
->st
), TYPE_BCM2835_ST
);
58 object_property_add_child(obj
, "systimer", OBJECT(&s
->st
), NULL
);
59 qdev_set_parent_bus(DEVICE(&s
->st
), sysbus_get_default());
62 object_initialize(&s
->timer
, sizeof(s
->timer
), TYPE_BCM2835_TIMER
);
63 object_property_add_child(obj
, "armtimer", OBJECT(&s
->timer
), NULL
);
64 qdev_set_parent_bus(DEVICE(&s
->timer
), sysbus_get_default());
67 object_initialize(&s
->usb
, sizeof(s
->usb
), TYPE_BCM2835_USB
);
68 object_property_add_child(obj
, "usb", OBJECT(&s
->usb
), NULL
);
69 qdev_set_parent_bus(DEVICE(&s
->usb
), sysbus_get_default());
71 object_property_add_const_link(OBJECT(&s
->usb
), "dma_mr",
72 OBJECT(&s
->gpu_bus_mr
), &error_abort
);
74 /* MPHI - Message-based Parallel Host Interface */
75 object_initialize(&s
->mphi
, sizeof(s
->mphi
), TYPE_BCM2835_MPHI
);
76 object_property_add_child(obj
, "mphi", OBJECT(&s
->mphi
), NULL
);
77 qdev_set_parent_bus(DEVICE(&s
->mphi
), sysbus_get_default());
80 object_initialize(&s
->mboxes
, sizeof(s
->mboxes
), TYPE_BCM2835_MBOX
);
81 object_property_add_child(obj
, "mbox", OBJECT(&s
->mboxes
), NULL
);
82 qdev_set_parent_bus(DEVICE(&s
->mboxes
), sysbus_get_default());
84 object_property_add_const_link(OBJECT(&s
->mboxes
), "mbox-mr",
85 OBJECT(&s
->mbox_mr
), &error_abort
);
87 /* Power management */
88 object_initialize(&s
->power
, sizeof(s
->power
), TYPE_BCM2835_POWER
);
89 object_property_add_child(obj
, "power", OBJECT(&s
->power
), NULL
);
90 qdev_set_parent_bus(DEVICE(&s
->power
), sysbus_get_default());
93 object_initialize(&s
->fb
, sizeof(s
->fb
), TYPE_BCM2835_FB
);
94 object_property_add_child(obj
, "fb", OBJECT(&s
->fb
), NULL
);
95 object_property_add_alias(obj
, "vcram-size", OBJECT(&s
->fb
), "vcram-size",
97 qdev_set_parent_bus(DEVICE(&s
->fb
), sysbus_get_default());
99 object_property_add_const_link(OBJECT(&s
->fb
), "dma-mr",
100 OBJECT(&s
->gpu_bus_mr
), &error_abort
);
102 /* Property channel */
103 object_initialize(&s
->property
, sizeof(s
->property
), TYPE_BCM2835_PROPERTY
);
104 object_property_add_child(obj
, "property", OBJECT(&s
->property
), NULL
);
105 object_property_add_alias(obj
, "board-rev", OBJECT(&s
->property
),
106 "board-rev", &error_abort
);
107 qdev_set_parent_bus(DEVICE(&s
->property
), sysbus_get_default());
109 object_property_add_const_link(OBJECT(&s
->property
), "fb",
110 OBJECT(&s
->fb
), &error_abort
);
111 object_property_add_const_link(OBJECT(&s
->property
), "dma-mr",
112 OBJECT(&s
->gpu_bus_mr
), &error_abort
);
114 /* Extended Mass Media Controller */
115 object_initialize(&s
->sdhci
, sizeof(s
->sdhci
), TYPE_SYSBUS_SDHCI
);
116 object_property_add_child(obj
, "sdhci", OBJECT(&s
->sdhci
), NULL
);
117 qdev_set_parent_bus(DEVICE(&s
->sdhci
), sysbus_get_default());
120 object_initialize(&s
->dma
, sizeof(s
->dma
), TYPE_BCM2835_DMA
);
121 object_property_add_child(obj
, "dma", OBJECT(&s
->dma
), NULL
);
122 qdev_set_parent_bus(DEVICE(&s
->dma
), sysbus_get_default());
124 object_property_add_const_link(OBJECT(&s
->dma
), "dma-mr",
125 OBJECT(&s
->gpu_bus_mr
), &error_abort
);
129 static void bcm2835_peripherals_realize(DeviceState
*dev
, Error
**errp
)
131 BCM2835PeripheralState
*s
= BCM2835_PERIPHERALS(dev
);
135 uint32_t ram_size
, vcram_size
;
138 obj
= object_property_get_link(OBJECT(dev
), "ram", &err
);
140 error_setg(errp
, "%s: required ram link not found: %s",
141 __func__
, error_get_pretty(err
));
145 ram
= MEMORY_REGION(obj
);
146 ram_size
= memory_region_size(ram
);
148 /* Map peripherals and RAM into the GPU address space. */
149 memory_region_init_alias(&s
->peri_mr_alias
, OBJECT(s
),
150 "bcm2835-peripherals", &s
->peri_mr
, 0,
151 memory_region_size(&s
->peri_mr
));
153 memory_region_add_subregion_overlap(&s
->gpu_bus_mr
, BCM2835_VC_PERI_BASE
,
154 &s
->peri_mr_alias
, 1);
156 /* RAM is aliased four times (different cache configurations) on the GPU */
157 for (n
= 0; n
< 4; n
++) {
158 memory_region_init_alias(&s
->ram_alias
[n
], OBJECT(s
),
159 "bcm2835-gpu-ram-alias[*]", ram
, 0, ram_size
);
160 memory_region_add_subregion_overlap(&s
->gpu_bus_mr
, (hwaddr
)n
<< 30,
161 &s
->ram_alias
[n
], 0);
164 /* Interrupt Controller */
165 object_property_set_bool(OBJECT(&s
->ic
), true, "realized", &err
);
167 error_propagate(errp
, err
);
171 memory_region_add_subregion(&s
->peri_mr
, ARMCTRL_IC_OFFSET
,
172 sysbus_mmio_get_region(SYS_BUS_DEVICE(&s
->ic
), 0));
173 sysbus_pass_irq(SYS_BUS_DEVICE(s
), SYS_BUS_DEVICE(&s
->ic
));
176 object_property_set_bool(OBJECT(s
->uart0
), true, "realized", &err
);
178 error_propagate(errp
, err
);
182 memory_region_add_subregion(&s
->peri_mr
, UART0_OFFSET
,
183 sysbus_mmio_get_region(s
->uart0
, 0));
184 sysbus_connect_irq(s
->uart0
, 0,
185 qdev_get_gpio_in_named(DEVICE(&s
->ic
), BCM2835_IC_GPU_IRQ
,
189 object_property_set_bool(OBJECT(&s
->aux
), true, "realized", &err
);
191 error_propagate(errp
, err
);
195 memory_region_add_subregion(&s
->peri_mr
, UART1_OFFSET
,
196 sysbus_mmio_get_region(SYS_BUS_DEVICE(&s
->aux
), 0));
197 sysbus_connect_irq(SYS_BUS_DEVICE(&s
->aux
), 0,
198 qdev_get_gpio_in_named(DEVICE(&s
->ic
), BCM2835_IC_GPU_IRQ
,
202 object_property_set_bool(OBJECT(&s
->st
), true, "realized", &err
);
204 error_propagate(errp
, err
);
208 memory_region_add_subregion(&s
->peri_mr
, ST_OFFSET
,
209 sysbus_mmio_get_region(SYS_BUS_DEVICE(&s
->st
), 0));
210 sysbus_connect_irq(SYS_BUS_DEVICE(&s
->st
), 0,
211 qdev_get_gpio_in_named(DEVICE(&s
->ic
), BCM2835_IC_GPU_IRQ
,
213 sysbus_connect_irq(SYS_BUS_DEVICE(&s
->st
), 1,
214 qdev_get_gpio_in_named(DEVICE(&s
->ic
), BCM2835_IC_GPU_IRQ
,
216 sysbus_connect_irq(SYS_BUS_DEVICE(&s
->st
), 2,
217 qdev_get_gpio_in_named(DEVICE(&s
->ic
), BCM2835_IC_GPU_IRQ
,
219 sysbus_connect_irq(SYS_BUS_DEVICE(&s
->st
), 3,
220 qdev_get_gpio_in_named(DEVICE(&s
->ic
), BCM2835_IC_GPU_IRQ
,
224 object_property_set_bool(OBJECT(&s
->timer
), true, "realized", &err
);
226 error_propagate(errp
, err
);
230 memory_region_add_subregion(&s
->peri_mr
, ARMCTRL_TIMER0_1_OFFSET
,
231 sysbus_mmio_get_region(SYS_BUS_DEVICE(&s
->timer
), 0));
232 sysbus_connect_irq(SYS_BUS_DEVICE(&s
->timer
), 0,
233 qdev_get_gpio_in_named(DEVICE(&s
->ic
), BCM2835_IC_ARM_IRQ
,
234 INTERRUPT_ARM_TIMER
));
237 object_property_set_bool(OBJECT(&s
->usb
), true, "realized", &err
);
239 error_propagate(errp
, err
);
243 memory_region_add_subregion(&s
->peri_mr
, USB_OFFSET
,
244 sysbus_mmio_get_region(SYS_BUS_DEVICE(&s
->usb
), 0));
245 sysbus_connect_irq(SYS_BUS_DEVICE(&s
->usb
), 0,
246 qdev_get_gpio_in_named(DEVICE(&s
->ic
), BCM2835_IC_GPU_IRQ
,
249 /* MPHI - Message-based Parallel Host Interface */
250 object_property_set_bool(OBJECT(&s
->mphi
), true, "realized", &err
);
252 error_propagate(errp
, err
);
256 memory_region_add_subregion(&s
->peri_mr
, MPHI_OFFSET
,
257 sysbus_mmio_get_region(SYS_BUS_DEVICE(&s
->mphi
), 0));
258 sysbus_connect_irq(SYS_BUS_DEVICE(&s
->mphi
), 0,
259 qdev_get_gpio_in_named(DEVICE(&s
->ic
), BCM2835_IC_GPU_IRQ
,
260 INTERRUPT_HOSTPORT
));
263 object_property_set_bool(OBJECT(&s
->mboxes
), true, "realized", &err
);
265 error_propagate(errp
, err
);
269 memory_region_add_subregion(&s
->peri_mr
, ARMCTRL_0_SBM_OFFSET
,
270 sysbus_mmio_get_region(SYS_BUS_DEVICE(&s
->mboxes
), 0));
271 sysbus_connect_irq(SYS_BUS_DEVICE(&s
->mboxes
), 0,
272 qdev_get_gpio_in_named(DEVICE(&s
->ic
), BCM2835_IC_ARM_IRQ
,
273 INTERRUPT_ARM_MAILBOX
));
275 /* Power management */
276 object_property_set_bool(OBJECT(&s
->power
), true, "realized", &err
);
278 error_propagate(errp
, err
);
282 memory_region_add_subregion(&s
->mbox_mr
, MBOX_CHAN_POWER
<< MBOX_AS_CHAN_SHIFT
,
283 sysbus_mmio_get_region(SYS_BUS_DEVICE(&s
->power
), 0));
284 sysbus_connect_irq(SYS_BUS_DEVICE(&s
->power
), 0,
285 qdev_get_gpio_in(DEVICE(&s
->mboxes
), MBOX_CHAN_POWER
));
288 vcram_size
= (uint32_t)object_property_get_int(OBJECT(s
), "vcram-size",
291 error_propagate(errp
, err
);
295 object_property_set_int(OBJECT(&s
->fb
), ram_size
- vcram_size
,
298 error_propagate(errp
, err
);
302 object_property_set_bool(OBJECT(&s
->fb
), true, "realized", &err
);
304 error_propagate(errp
, err
);
308 memory_region_add_subregion(&s
->mbox_mr
, MBOX_CHAN_FB
<< MBOX_AS_CHAN_SHIFT
,
309 sysbus_mmio_get_region(SYS_BUS_DEVICE(&s
->fb
), 0));
310 sysbus_connect_irq(SYS_BUS_DEVICE(&s
->fb
), 0,
311 qdev_get_gpio_in(DEVICE(&s
->mboxes
), MBOX_CHAN_FB
));
313 /* Property channel */
314 object_property_set_bool(OBJECT(&s
->property
), true, "realized", &err
);
316 error_propagate(errp
, err
);
320 memory_region_add_subregion(&s
->mbox_mr
,
321 MBOX_CHAN_PROPERTY
<< MBOX_AS_CHAN_SHIFT
,
322 sysbus_mmio_get_region(SYS_BUS_DEVICE(&s
->property
), 0));
323 sysbus_connect_irq(SYS_BUS_DEVICE(&s
->property
), 0,
324 qdev_get_gpio_in(DEVICE(&s
->mboxes
), MBOX_CHAN_PROPERTY
));
326 /* Extended Mass Media Controller */
327 object_property_set_int(OBJECT(&s
->sdhci
), BCM2835_SDHC_CAPAREG
, "capareg",
330 error_propagate(errp
, err
);
334 object_property_set_bool(OBJECT(&s
->sdhci
), true, "pending-insert-quirk", &err
);
336 error_propagate(errp
, err
);
340 object_property_set_bool(OBJECT(&s
->sdhci
), true, "realized", &err
);
342 error_propagate(errp
, err
);
346 memory_region_add_subregion(&s
->peri_mr
, EMMC_OFFSET
,
347 sysbus_mmio_get_region(SYS_BUS_DEVICE(&s
->sdhci
), 0));
348 sysbus_connect_irq(SYS_BUS_DEVICE(&s
->sdhci
), 0,
349 qdev_get_gpio_in_named(DEVICE(&s
->ic
), BCM2835_IC_GPU_IRQ
,
350 INTERRUPT_ARASANSDIO
));
352 object_property_add_alias(OBJECT(s
), "sd-bus", OBJECT(&s
->sdhci
), "sd-bus",
355 error_propagate(errp
, err
);
360 object_property_set_bool(OBJECT(&s
->dma
), true, "realized", &err
);
362 error_propagate(errp
, err
);
366 memory_region_add_subregion(&s
->peri_mr
, DMA_OFFSET
,
367 sysbus_mmio_get_region(SYS_BUS_DEVICE(&s
->dma
), 0));
368 memory_region_add_subregion(&s
->peri_mr
, DMA15_OFFSET
,
369 sysbus_mmio_get_region(SYS_BUS_DEVICE(&s
->dma
), 1));
371 sysbus_connect_irq(SYS_BUS_DEVICE(&s
->dma
), 0,
372 qdev_get_gpio_in_named(DEVICE(&s
->ic
), BCM2835_IC_GPU_IRQ
, INTERRUPT_DMA0
));
373 sysbus_connect_irq(SYS_BUS_DEVICE(&s
->dma
), 1,
374 qdev_get_gpio_in_named(DEVICE(&s
->ic
), BCM2835_IC_GPU_IRQ
, INTERRUPT_DMA1
));
375 sysbus_connect_irq(SYS_BUS_DEVICE(&s
->dma
), 2,
376 qdev_get_gpio_in_named(DEVICE(&s
->ic
), BCM2835_IC_GPU_IRQ
, INTERRUPT_DMA2
));
377 sysbus_connect_irq(SYS_BUS_DEVICE(&s
->dma
), 3,
378 qdev_get_gpio_in_named(DEVICE(&s
->ic
), BCM2835_IC_GPU_IRQ
, INTERRUPT_DMA3
));
379 sysbus_connect_irq(SYS_BUS_DEVICE(&s
->dma
), 4,
380 qdev_get_gpio_in_named(DEVICE(&s
->ic
), BCM2835_IC_GPU_IRQ
, INTERRUPT_DMA4
));
381 sysbus_connect_irq(SYS_BUS_DEVICE(&s
->dma
), 5,
382 qdev_get_gpio_in_named(DEVICE(&s
->ic
), BCM2835_IC_GPU_IRQ
, INTERRUPT_DMA5
));
383 sysbus_connect_irq(SYS_BUS_DEVICE(&s
->dma
), 6,
384 qdev_get_gpio_in_named(DEVICE(&s
->ic
), BCM2835_IC_GPU_IRQ
, INTERRUPT_DMA6
));
385 sysbus_connect_irq(SYS_BUS_DEVICE(&s
->dma
), 7,
386 qdev_get_gpio_in_named(DEVICE(&s
->ic
), BCM2835_IC_GPU_IRQ
, INTERRUPT_DMA7
));
387 sysbus_connect_irq(SYS_BUS_DEVICE(&s
->dma
), 8,
388 qdev_get_gpio_in_named(DEVICE(&s
->ic
), BCM2835_IC_GPU_IRQ
, INTERRUPT_DMA8
));
389 sysbus_connect_irq(SYS_BUS_DEVICE(&s
->dma
), 9,
390 qdev_get_gpio_in_named(DEVICE(&s
->ic
), BCM2835_IC_GPU_IRQ
, INTERRUPT_DMA9
));
391 sysbus_connect_irq(SYS_BUS_DEVICE(&s
->dma
), 10,
392 qdev_get_gpio_in_named(DEVICE(&s
->ic
), BCM2835_IC_GPU_IRQ
, INTERRUPT_DMA10
));
393 sysbus_connect_irq(SYS_BUS_DEVICE(&s
->dma
), 11,
394 qdev_get_gpio_in_named(DEVICE(&s
->ic
), BCM2835_IC_GPU_IRQ
, INTERRUPT_DMA11
));
395 sysbus_connect_irq(SYS_BUS_DEVICE(&s
->dma
), 12,
396 qdev_get_gpio_in_named(DEVICE(&s
->ic
), BCM2835_IC_GPU_IRQ
, INTERRUPT_DMA12
));
399 static void bcm2835_peripherals_class_init(ObjectClass
*oc
, void *data
)
401 DeviceClass
*dc
= DEVICE_CLASS(oc
);
403 dc
->realize
= bcm2835_peripherals_realize
;
406 static const TypeInfo bcm2835_peripherals_type_info
= {
407 .name
= TYPE_BCM2835_PERIPHERALS
,
408 .parent
= TYPE_SYS_BUS_DEVICE
,
409 .instance_size
= sizeof(BCM2835PeripheralState
),
410 .instance_init
= bcm2835_peripherals_init
,
411 .class_init
= bcm2835_peripherals_class_init
,
414 static void bcm2835_peripherals_register_types(void)
416 type_register_static(&bcm2835_peripherals_type_info
);
419 type_init(bcm2835_peripherals_register_types
)