target/mips: msa: Split helpers for DPADD_U.<H|W|D>
[qemu/ar7.git] / hw / arm / bcm2835_peripherals.c
blobcca5b5ad04f6cd83aa38fccf68426948429239ab
1 /*
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/bcm2835_peripherals.h"
16 #include "hw/misc/bcm2835_mbox_defs.h"
17 #include "hw/arm/raspi_platform.h"
18 #include "sysemu/sysemu.h"
20 /* Peripheral base address on the VC (GPU) system bus */
21 #define BCM2835_VC_PERI_BASE 0x7e000000
23 /* Capabilities for SD controller: no DMA, high-speed, default clocks etc. */
24 #define BCM2835_SDHC_CAPAREG 0x52134b4
26 static void create_unimp(BCM2835PeripheralState *ps,
27 UnimplementedDeviceState *uds,
28 const char *name, hwaddr ofs, hwaddr size)
30 sysbus_init_child_obj(OBJECT(ps), name, uds,
31 sizeof(UnimplementedDeviceState),
32 TYPE_UNIMPLEMENTED_DEVICE);
33 qdev_prop_set_string(DEVICE(uds), "name", name);
34 qdev_prop_set_uint64(DEVICE(uds), "size", size);
35 object_property_set_bool(OBJECT(uds), true, "realized", &error_fatal);
36 memory_region_add_subregion_overlap(&ps->peri_mr, ofs,
37 sysbus_mmio_get_region(SYS_BUS_DEVICE(uds), 0), -1000);
40 static void bcm2835_peripherals_init(Object *obj)
42 BCM2835PeripheralState *s = BCM2835_PERIPHERALS(obj);
44 /* Memory region for peripheral devices, which we export to our parent */
45 memory_region_init(&s->peri_mr, obj,"bcm2835-peripherals", 0x1000000);
46 sysbus_init_mmio(SYS_BUS_DEVICE(s), &s->peri_mr);
48 /* Internal memory region for peripheral bus addresses (not exported) */
49 memory_region_init(&s->gpu_bus_mr, obj, "bcm2835-gpu", (uint64_t)1 << 32);
51 /* Internal memory region for request/response communication with
52 * mailbox-addressable peripherals (not exported)
54 memory_region_init(&s->mbox_mr, obj, "bcm2835-mbox",
55 MBOX_CHAN_COUNT << MBOX_AS_CHAN_SHIFT);
57 /* Interrupt Controller */
58 sysbus_init_child_obj(obj, "ic", &s->ic, sizeof(s->ic), TYPE_BCM2835_IC);
60 /* SYS Timer */
61 sysbus_init_child_obj(obj, "systimer", &s->systmr, sizeof(s->systmr),
62 TYPE_BCM2835_SYSTIMER);
64 /* UART0 */
65 sysbus_init_child_obj(obj, "uart0", &s->uart0, sizeof(s->uart0),
66 TYPE_PL011);
68 /* AUX / UART1 */
69 sysbus_init_child_obj(obj, "aux", &s->aux, sizeof(s->aux),
70 TYPE_BCM2835_AUX);
72 /* Mailboxes */
73 sysbus_init_child_obj(obj, "mbox", &s->mboxes, sizeof(s->mboxes),
74 TYPE_BCM2835_MBOX);
76 object_property_add_const_link(OBJECT(&s->mboxes), "mbox-mr",
77 OBJECT(&s->mbox_mr));
79 /* Framebuffer */
80 sysbus_init_child_obj(obj, "fb", &s->fb, sizeof(s->fb), TYPE_BCM2835_FB);
81 object_property_add_alias(obj, "vcram-size", OBJECT(&s->fb), "vcram-size");
83 object_property_add_const_link(OBJECT(&s->fb), "dma-mr",
84 OBJECT(&s->gpu_bus_mr));
86 /* Property channel */
87 sysbus_init_child_obj(obj, "property", &s->property, sizeof(s->property),
88 TYPE_BCM2835_PROPERTY);
89 object_property_add_alias(obj, "board-rev", OBJECT(&s->property),
90 "board-rev");
92 object_property_add_const_link(OBJECT(&s->property), "fb",
93 OBJECT(&s->fb));
94 object_property_add_const_link(OBJECT(&s->property), "dma-mr",
95 OBJECT(&s->gpu_bus_mr));
97 /* Random Number Generator */
98 sysbus_init_child_obj(obj, "rng", &s->rng, sizeof(s->rng),
99 TYPE_BCM2835_RNG);
101 /* Extended Mass Media Controller */
102 sysbus_init_child_obj(obj, "sdhci", &s->sdhci, sizeof(s->sdhci),
103 TYPE_SYSBUS_SDHCI);
105 /* SDHOST */
106 sysbus_init_child_obj(obj, "sdhost", &s->sdhost, sizeof(s->sdhost),
107 TYPE_BCM2835_SDHOST);
109 /* DMA Channels */
110 sysbus_init_child_obj(obj, "dma", &s->dma, sizeof(s->dma),
111 TYPE_BCM2835_DMA);
113 object_property_add_const_link(OBJECT(&s->dma), "dma-mr",
114 OBJECT(&s->gpu_bus_mr));
116 /* Thermal */
117 sysbus_init_child_obj(obj, "thermal", &s->thermal, sizeof(s->thermal),
118 TYPE_BCM2835_THERMAL);
120 /* GPIO */
121 sysbus_init_child_obj(obj, "gpio", &s->gpio, sizeof(s->gpio),
122 TYPE_BCM2835_GPIO);
124 object_property_add_const_link(OBJECT(&s->gpio), "sdbus-sdhci",
125 OBJECT(&s->sdhci.sdbus));
126 object_property_add_const_link(OBJECT(&s->gpio), "sdbus-sdhost",
127 OBJECT(&s->sdhost.sdbus));
129 /* Mphi */
130 sysbus_init_child_obj(obj, "mphi", &s->mphi, sizeof(s->mphi),
131 TYPE_BCM2835_MPHI);
133 /* DWC2 */
134 sysbus_init_child_obj(obj, "dwc2", &s->dwc2, sizeof(s->dwc2),
135 TYPE_DWC2_USB);
137 object_property_add_const_link(OBJECT(&s->dwc2), "dma-mr",
138 OBJECT(&s->gpu_bus_mr));
141 static void bcm2835_peripherals_realize(DeviceState *dev, Error **errp)
143 BCM2835PeripheralState *s = BCM2835_PERIPHERALS(dev);
144 Object *obj;
145 MemoryRegion *ram;
146 Error *err = NULL;
147 uint64_t ram_size, vcram_size;
148 int n;
150 obj = object_property_get_link(OBJECT(dev), "ram", &err);
151 if (obj == NULL) {
152 error_setg(errp, "%s: required ram link not found: %s",
153 __func__, error_get_pretty(err));
154 return;
157 ram = MEMORY_REGION(obj);
158 ram_size = memory_region_size(ram);
160 /* Map peripherals and RAM into the GPU address space. */
161 memory_region_init_alias(&s->peri_mr_alias, OBJECT(s),
162 "bcm2835-peripherals", &s->peri_mr, 0,
163 memory_region_size(&s->peri_mr));
165 memory_region_add_subregion_overlap(&s->gpu_bus_mr, BCM2835_VC_PERI_BASE,
166 &s->peri_mr_alias, 1);
168 /* RAM is aliased four times (different cache configurations) on the GPU */
169 for (n = 0; n < 4; n++) {
170 memory_region_init_alias(&s->ram_alias[n], OBJECT(s),
171 "bcm2835-gpu-ram-alias[*]", ram, 0, ram_size);
172 memory_region_add_subregion_overlap(&s->gpu_bus_mr, (hwaddr)n << 30,
173 &s->ram_alias[n], 0);
176 /* Interrupt Controller */
177 object_property_set_bool(OBJECT(&s->ic), true, "realized", &err);
178 if (err) {
179 error_propagate(errp, err);
180 return;
183 memory_region_add_subregion(&s->peri_mr, ARMCTRL_IC_OFFSET,
184 sysbus_mmio_get_region(SYS_BUS_DEVICE(&s->ic), 0));
185 sysbus_pass_irq(SYS_BUS_DEVICE(s), SYS_BUS_DEVICE(&s->ic));
187 /* Sys Timer */
188 object_property_set_bool(OBJECT(&s->systmr), true, "realized", &err);
189 if (err) {
190 error_propagate(errp, err);
191 return;
193 memory_region_add_subregion(&s->peri_mr, ST_OFFSET,
194 sysbus_mmio_get_region(SYS_BUS_DEVICE(&s->systmr), 0));
195 sysbus_connect_irq(SYS_BUS_DEVICE(&s->systmr), 0,
196 qdev_get_gpio_in_named(DEVICE(&s->ic), BCM2835_IC_ARM_IRQ,
197 INTERRUPT_ARM_TIMER));
199 /* UART0 */
200 qdev_prop_set_chr(DEVICE(&s->uart0), "chardev", serial_hd(0));
201 object_property_set_bool(OBJECT(&s->uart0), true, "realized", &err);
202 if (err) {
203 error_propagate(errp, err);
204 return;
207 memory_region_add_subregion(&s->peri_mr, UART0_OFFSET,
208 sysbus_mmio_get_region(SYS_BUS_DEVICE(&s->uart0), 0));
209 sysbus_connect_irq(SYS_BUS_DEVICE(&s->uart0), 0,
210 qdev_get_gpio_in_named(DEVICE(&s->ic), BCM2835_IC_GPU_IRQ,
211 INTERRUPT_UART0));
213 /* AUX / UART1 */
214 qdev_prop_set_chr(DEVICE(&s->aux), "chardev", serial_hd(1));
216 object_property_set_bool(OBJECT(&s->aux), true, "realized", &err);
217 if (err) {
218 error_propagate(errp, err);
219 return;
222 memory_region_add_subregion(&s->peri_mr, AUX_OFFSET,
223 sysbus_mmio_get_region(SYS_BUS_DEVICE(&s->aux), 0));
224 sysbus_connect_irq(SYS_BUS_DEVICE(&s->aux), 0,
225 qdev_get_gpio_in_named(DEVICE(&s->ic), BCM2835_IC_GPU_IRQ,
226 INTERRUPT_AUX));
228 /* Mailboxes */
229 object_property_set_bool(OBJECT(&s->mboxes), true, "realized", &err);
230 if (err) {
231 error_propagate(errp, err);
232 return;
235 memory_region_add_subregion(&s->peri_mr, ARMCTRL_0_SBM_OFFSET,
236 sysbus_mmio_get_region(SYS_BUS_DEVICE(&s->mboxes), 0));
237 sysbus_connect_irq(SYS_BUS_DEVICE(&s->mboxes), 0,
238 qdev_get_gpio_in_named(DEVICE(&s->ic), BCM2835_IC_ARM_IRQ,
239 INTERRUPT_ARM_MAILBOX));
241 /* Framebuffer */
242 vcram_size = object_property_get_uint(OBJECT(s), "vcram-size", &err);
243 if (err) {
244 error_propagate(errp, err);
245 return;
248 object_property_set_uint(OBJECT(&s->fb), ram_size - vcram_size,
249 "vcram-base", &err);
250 if (err) {
251 error_propagate(errp, err);
252 return;
255 object_property_set_bool(OBJECT(&s->fb), true, "realized", &err);
256 if (err) {
257 error_propagate(errp, err);
258 return;
261 memory_region_add_subregion(&s->mbox_mr, MBOX_CHAN_FB << MBOX_AS_CHAN_SHIFT,
262 sysbus_mmio_get_region(SYS_BUS_DEVICE(&s->fb), 0));
263 sysbus_connect_irq(SYS_BUS_DEVICE(&s->fb), 0,
264 qdev_get_gpio_in(DEVICE(&s->mboxes), MBOX_CHAN_FB));
266 /* Property channel */
267 object_property_set_bool(OBJECT(&s->property), true, "realized", &err);
268 if (err) {
269 error_propagate(errp, err);
270 return;
273 memory_region_add_subregion(&s->mbox_mr,
274 MBOX_CHAN_PROPERTY << MBOX_AS_CHAN_SHIFT,
275 sysbus_mmio_get_region(SYS_BUS_DEVICE(&s->property), 0));
276 sysbus_connect_irq(SYS_BUS_DEVICE(&s->property), 0,
277 qdev_get_gpio_in(DEVICE(&s->mboxes), MBOX_CHAN_PROPERTY));
279 /* Random Number Generator */
280 object_property_set_bool(OBJECT(&s->rng), true, "realized", &err);
281 if (err) {
282 error_propagate(errp, err);
283 return;
286 memory_region_add_subregion(&s->peri_mr, RNG_OFFSET,
287 sysbus_mmio_get_region(SYS_BUS_DEVICE(&s->rng), 0));
289 /* Extended Mass Media Controller
291 * Compatible with:
292 * - SD Host Controller Specification Version 3.0 Draft 1.0
293 * - SDIO Specification Version 3.0
294 * - MMC Specification Version 4.4
296 * For the exact details please refer to the Arasan documentation:
297 * SD3.0_Host_AHB_eMMC4.4_Usersguide_ver5.9_jan11_10.pdf
299 object_property_set_uint(OBJECT(&s->sdhci), 3, "sd-spec-version", &err);
300 object_property_set_uint(OBJECT(&s->sdhci), BCM2835_SDHC_CAPAREG, "capareg",
301 &err);
302 object_property_set_bool(OBJECT(&s->sdhci), true, "pending-insert-quirk",
303 &err);
304 if (err) {
305 error_propagate(errp, err);
306 return;
309 object_property_set_bool(OBJECT(&s->sdhci), true, "realized", &err);
310 if (err) {
311 error_propagate(errp, err);
312 return;
315 memory_region_add_subregion(&s->peri_mr, EMMC1_OFFSET,
316 sysbus_mmio_get_region(SYS_BUS_DEVICE(&s->sdhci), 0));
317 sysbus_connect_irq(SYS_BUS_DEVICE(&s->sdhci), 0,
318 qdev_get_gpio_in_named(DEVICE(&s->ic), BCM2835_IC_GPU_IRQ,
319 INTERRUPT_ARASANSDIO));
321 /* SDHOST */
322 object_property_set_bool(OBJECT(&s->sdhost), true, "realized", &err);
323 if (err) {
324 error_propagate(errp, err);
325 return;
328 memory_region_add_subregion(&s->peri_mr, MMCI0_OFFSET,
329 sysbus_mmio_get_region(SYS_BUS_DEVICE(&s->sdhost), 0));
330 sysbus_connect_irq(SYS_BUS_DEVICE(&s->sdhost), 0,
331 qdev_get_gpio_in_named(DEVICE(&s->ic), BCM2835_IC_GPU_IRQ,
332 INTERRUPT_SDIO));
334 /* DMA Channels */
335 object_property_set_bool(OBJECT(&s->dma), true, "realized", &err);
336 if (err) {
337 error_propagate(errp, err);
338 return;
341 memory_region_add_subregion(&s->peri_mr, DMA_OFFSET,
342 sysbus_mmio_get_region(SYS_BUS_DEVICE(&s->dma), 0));
343 memory_region_add_subregion(&s->peri_mr, DMA15_OFFSET,
344 sysbus_mmio_get_region(SYS_BUS_DEVICE(&s->dma), 1));
346 for (n = 0; n <= 12; n++) {
347 sysbus_connect_irq(SYS_BUS_DEVICE(&s->dma), n,
348 qdev_get_gpio_in_named(DEVICE(&s->ic),
349 BCM2835_IC_GPU_IRQ,
350 INTERRUPT_DMA0 + n));
353 /* THERMAL */
354 object_property_set_bool(OBJECT(&s->thermal), true, "realized", &err);
355 if (err) {
356 error_propagate(errp, err);
357 return;
359 memory_region_add_subregion(&s->peri_mr, THERMAL_OFFSET,
360 sysbus_mmio_get_region(SYS_BUS_DEVICE(&s->thermal), 0));
362 /* GPIO */
363 object_property_set_bool(OBJECT(&s->gpio), true, "realized", &err);
364 if (err) {
365 error_propagate(errp, err);
366 return;
369 memory_region_add_subregion(&s->peri_mr, GPIO_OFFSET,
370 sysbus_mmio_get_region(SYS_BUS_DEVICE(&s->gpio), 0));
372 object_property_add_alias(OBJECT(s), "sd-bus", OBJECT(&s->gpio), "sd-bus");
374 /* Mphi */
375 object_property_set_bool(OBJECT(&s->mphi), true, "realized", &err);
376 if (err) {
377 error_propagate(errp, err);
378 return;
381 memory_region_add_subregion(&s->peri_mr, MPHI_OFFSET,
382 sysbus_mmio_get_region(SYS_BUS_DEVICE(&s->mphi), 0));
383 sysbus_connect_irq(SYS_BUS_DEVICE(&s->mphi), 0,
384 qdev_get_gpio_in_named(DEVICE(&s->ic), BCM2835_IC_GPU_IRQ,
385 INTERRUPT_HOSTPORT));
387 /* DWC2 */
388 object_property_set_bool(OBJECT(&s->dwc2), true, "realized", &err);
389 if (err) {
390 error_propagate(errp, err);
391 return;
394 memory_region_add_subregion(&s->peri_mr, USB_OTG_OFFSET,
395 sysbus_mmio_get_region(SYS_BUS_DEVICE(&s->dwc2), 0));
396 sysbus_connect_irq(SYS_BUS_DEVICE(&s->dwc2), 0,
397 qdev_get_gpio_in_named(DEVICE(&s->ic), BCM2835_IC_GPU_IRQ,
398 INTERRUPT_USB));
400 create_unimp(s, &s->armtmr, "bcm2835-sp804", ARMCTRL_TIMER0_1_OFFSET, 0x40);
401 create_unimp(s, &s->cprman, "bcm2835-cprman", CPRMAN_OFFSET, 0x1000);
402 create_unimp(s, &s->a2w, "bcm2835-a2w", A2W_OFFSET, 0x1000);
403 create_unimp(s, &s->i2s, "bcm2835-i2s", I2S_OFFSET, 0x100);
404 create_unimp(s, &s->smi, "bcm2835-smi", SMI_OFFSET, 0x100);
405 create_unimp(s, &s->spi[0], "bcm2835-spi0", SPI0_OFFSET, 0x20);
406 create_unimp(s, &s->bscsl, "bcm2835-spis", BSC_SL_OFFSET, 0x100);
407 create_unimp(s, &s->i2c[0], "bcm2835-i2c0", BSC0_OFFSET, 0x20);
408 create_unimp(s, &s->i2c[1], "bcm2835-i2c1", BSC1_OFFSET, 0x20);
409 create_unimp(s, &s->i2c[2], "bcm2835-i2c2", BSC2_OFFSET, 0x20);
410 create_unimp(s, &s->otp, "bcm2835-otp", OTP_OFFSET, 0x80);
411 create_unimp(s, &s->dbus, "bcm2835-dbus", DBUS_OFFSET, 0x8000);
412 create_unimp(s, &s->ave0, "bcm2835-ave0", AVE0_OFFSET, 0x8000);
413 create_unimp(s, &s->sdramc, "bcm2835-sdramc", SDRAMC_OFFSET, 0x100);
416 static void bcm2835_peripherals_class_init(ObjectClass *oc, void *data)
418 DeviceClass *dc = DEVICE_CLASS(oc);
420 dc->realize = bcm2835_peripherals_realize;
423 static const TypeInfo bcm2835_peripherals_type_info = {
424 .name = TYPE_BCM2835_PERIPHERALS,
425 .parent = TYPE_SYS_BUS_DEVICE,
426 .instance_size = sizeof(BCM2835PeripheralState),
427 .instance_init = bcm2835_peripherals_init,
428 .class_init = bcm2835_peripherals_class_init,
431 static void bcm2835_peripherals_register_types(void)
433 type_register_static(&bcm2835_peripherals_type_info);
436 type_init(bcm2835_peripherals_register_types)