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"
15 #include "sysemu/char.h"
17 /* Peripheral base address on the VC (GPU) system bus */
18 #define BCM2835_VC_PERI_BASE 0x7e000000
20 /* Capabilities for SD controller: no DMA, high-speed, default clocks etc. */
21 #define BCM2835_SDHC_CAPAREG 0x52034b4
23 static void bcm2835_peripherals_init(Object
*obj
)
25 BCM2835PeripheralState
*s
= BCM2835_PERIPHERALS(obj
);
27 /* Memory region for peripheral devices, which we export to our parent */
28 memory_region_init(&s
->peri_mr
, obj
,"bcm2835-peripherals", 0x1000000);
29 object_property_add_child(obj
, "peripheral-io", OBJECT(&s
->peri_mr
), NULL
);
30 sysbus_init_mmio(SYS_BUS_DEVICE(s
), &s
->peri_mr
);
32 /* Internal memory region for peripheral bus addresses (not exported) */
33 memory_region_init(&s
->gpu_bus_mr
, obj
, "bcm2835-gpu", (uint64_t)1 << 32);
34 object_property_add_child(obj
, "gpu-bus", OBJECT(&s
->gpu_bus_mr
), NULL
);
36 /* Internal memory region for request/response communication with
37 * mailbox-addressable peripherals (not exported)
39 memory_region_init(&s
->mbox_mr
, obj
, "bcm2835-mbox",
40 MBOX_CHAN_COUNT
<< MBOX_AS_CHAN_SHIFT
);
42 /* Interrupt Controller */
43 object_initialize(&s
->ic
, sizeof(s
->ic
), TYPE_BCM2835_IC
);
44 object_property_add_child(obj
, "ic", OBJECT(&s
->ic
), NULL
);
45 qdev_set_parent_bus(DEVICE(&s
->ic
), sysbus_get_default());
48 s
->uart0
= SYS_BUS_DEVICE(object_new("pl011"));
49 object_property_add_child(obj
, "uart0", OBJECT(s
->uart0
), NULL
);
50 qdev_set_parent_bus(DEVICE(s
->uart0
), sysbus_get_default());
53 object_initialize(&s
->aux
, sizeof(s
->aux
), TYPE_BCM2835_AUX
);
54 object_property_add_child(obj
, "aux", OBJECT(&s
->aux
), NULL
);
55 qdev_set_parent_bus(DEVICE(&s
->aux
), sysbus_get_default());
58 object_initialize(&s
->mboxes
, sizeof(s
->mboxes
), TYPE_BCM2835_MBOX
);
59 object_property_add_child(obj
, "mbox", OBJECT(&s
->mboxes
), NULL
);
60 qdev_set_parent_bus(DEVICE(&s
->mboxes
), sysbus_get_default());
62 object_property_add_const_link(OBJECT(&s
->mboxes
), "mbox-mr",
63 OBJECT(&s
->mbox_mr
), &error_abort
);
66 object_initialize(&s
->fb
, sizeof(s
->fb
), TYPE_BCM2835_FB
);
67 object_property_add_child(obj
, "fb", OBJECT(&s
->fb
), NULL
);
68 object_property_add_alias(obj
, "vcram-size", OBJECT(&s
->fb
), "vcram-size",
70 qdev_set_parent_bus(DEVICE(&s
->fb
), sysbus_get_default());
72 object_property_add_const_link(OBJECT(&s
->fb
), "dma-mr",
73 OBJECT(&s
->gpu_bus_mr
), &error_abort
);
75 /* Property channel */
76 object_initialize(&s
->property
, sizeof(s
->property
), TYPE_BCM2835_PROPERTY
);
77 object_property_add_child(obj
, "property", OBJECT(&s
->property
), NULL
);
78 object_property_add_alias(obj
, "board-rev", OBJECT(&s
->property
),
79 "board-rev", &error_abort
);
80 qdev_set_parent_bus(DEVICE(&s
->property
), sysbus_get_default());
82 object_property_add_const_link(OBJECT(&s
->property
), "fb",
83 OBJECT(&s
->fb
), &error_abort
);
84 object_property_add_const_link(OBJECT(&s
->property
), "dma-mr",
85 OBJECT(&s
->gpu_bus_mr
), &error_abort
);
87 /* Extended Mass Media Controller */
88 object_initialize(&s
->sdhci
, sizeof(s
->sdhci
), TYPE_SYSBUS_SDHCI
);
89 object_property_add_child(obj
, "sdhci", OBJECT(&s
->sdhci
), NULL
);
90 qdev_set_parent_bus(DEVICE(&s
->sdhci
), sysbus_get_default());
93 object_initialize(&s
->dma
, sizeof(s
->dma
), TYPE_BCM2835_DMA
);
94 object_property_add_child(obj
, "dma", OBJECT(&s
->dma
), NULL
);
95 qdev_set_parent_bus(DEVICE(&s
->dma
), sysbus_get_default());
97 object_property_add_const_link(OBJECT(&s
->dma
), "dma-mr",
98 OBJECT(&s
->gpu_bus_mr
), &error_abort
);
101 static void bcm2835_peripherals_realize(DeviceState
*dev
, Error
**errp
)
103 BCM2835PeripheralState
*s
= BCM2835_PERIPHERALS(dev
);
107 uint32_t ram_size
, vcram_size
;
108 CharDriverState
*chr
;
111 obj
= object_property_get_link(OBJECT(dev
), "ram", &err
);
113 error_setg(errp
, "%s: required ram link not found: %s",
114 __func__
, error_get_pretty(err
));
118 ram
= MEMORY_REGION(obj
);
119 ram_size
= memory_region_size(ram
);
121 /* Map peripherals and RAM into the GPU address space. */
122 memory_region_init_alias(&s
->peri_mr_alias
, OBJECT(s
),
123 "bcm2835-peripherals", &s
->peri_mr
, 0,
124 memory_region_size(&s
->peri_mr
));
126 memory_region_add_subregion_overlap(&s
->gpu_bus_mr
, BCM2835_VC_PERI_BASE
,
127 &s
->peri_mr_alias
, 1);
129 /* RAM is aliased four times (different cache configurations) on the GPU */
130 for (n
= 0; n
< 4; n
++) {
131 memory_region_init_alias(&s
->ram_alias
[n
], OBJECT(s
),
132 "bcm2835-gpu-ram-alias[*]", ram
, 0, ram_size
);
133 memory_region_add_subregion_overlap(&s
->gpu_bus_mr
, (hwaddr
)n
<< 30,
134 &s
->ram_alias
[n
], 0);
137 /* Interrupt Controller */
138 object_property_set_bool(OBJECT(&s
->ic
), true, "realized", &err
);
140 error_propagate(errp
, err
);
144 memory_region_add_subregion(&s
->peri_mr
, ARMCTRL_IC_OFFSET
,
145 sysbus_mmio_get_region(SYS_BUS_DEVICE(&s
->ic
), 0));
146 sysbus_pass_irq(SYS_BUS_DEVICE(s
), SYS_BUS_DEVICE(&s
->ic
));
149 object_property_set_bool(OBJECT(s
->uart0
), true, "realized", &err
);
151 error_propagate(errp
, err
);
155 memory_region_add_subregion(&s
->peri_mr
, UART0_OFFSET
,
156 sysbus_mmio_get_region(s
->uart0
, 0));
157 sysbus_connect_irq(s
->uart0
, 0,
158 qdev_get_gpio_in_named(DEVICE(&s
->ic
), BCM2835_IC_GPU_IRQ
,
162 /* TODO: don't call qemu_char_get_next_serial() here, instead set
163 * chardev properties for each uart at the board level, once pl011
164 * (uart0) has been updated to avoid qemu_char_get_next_serial()
166 chr
= qemu_char_get_next_serial();
168 chr
= qemu_chr_new("bcm2835.uart1", "null", NULL
);
170 qdev_prop_set_chr(DEVICE(&s
->aux
), "chardev", chr
);
172 object_property_set_bool(OBJECT(&s
->aux
), true, "realized", &err
);
174 error_propagate(errp
, err
);
178 memory_region_add_subregion(&s
->peri_mr
, UART1_OFFSET
,
179 sysbus_mmio_get_region(SYS_BUS_DEVICE(&s
->aux
), 0));
180 sysbus_connect_irq(SYS_BUS_DEVICE(&s
->aux
), 0,
181 qdev_get_gpio_in_named(DEVICE(&s
->ic
), BCM2835_IC_GPU_IRQ
,
185 object_property_set_bool(OBJECT(&s
->mboxes
), true, "realized", &err
);
187 error_propagate(errp
, err
);
191 memory_region_add_subregion(&s
->peri_mr
, ARMCTRL_0_SBM_OFFSET
,
192 sysbus_mmio_get_region(SYS_BUS_DEVICE(&s
->mboxes
), 0));
193 sysbus_connect_irq(SYS_BUS_DEVICE(&s
->mboxes
), 0,
194 qdev_get_gpio_in_named(DEVICE(&s
->ic
), BCM2835_IC_ARM_IRQ
,
195 INTERRUPT_ARM_MAILBOX
));
198 vcram_size
= (uint32_t)object_property_get_int(OBJECT(s
), "vcram-size",
201 error_propagate(errp
, err
);
205 object_property_set_int(OBJECT(&s
->fb
), ram_size
- vcram_size
,
208 error_propagate(errp
, err
);
212 object_property_set_bool(OBJECT(&s
->fb
), true, "realized", &err
);
214 error_propagate(errp
, err
);
218 memory_region_add_subregion(&s
->mbox_mr
, MBOX_CHAN_FB
<< MBOX_AS_CHAN_SHIFT
,
219 sysbus_mmio_get_region(SYS_BUS_DEVICE(&s
->fb
), 0));
220 sysbus_connect_irq(SYS_BUS_DEVICE(&s
->fb
), 0,
221 qdev_get_gpio_in(DEVICE(&s
->mboxes
), MBOX_CHAN_FB
));
223 /* Property channel */
224 object_property_set_bool(OBJECT(&s
->property
), true, "realized", &err
);
226 error_propagate(errp
, err
);
230 memory_region_add_subregion(&s
->mbox_mr
,
231 MBOX_CHAN_PROPERTY
<< MBOX_AS_CHAN_SHIFT
,
232 sysbus_mmio_get_region(SYS_BUS_DEVICE(&s
->property
), 0));
233 sysbus_connect_irq(SYS_BUS_DEVICE(&s
->property
), 0,
234 qdev_get_gpio_in(DEVICE(&s
->mboxes
), MBOX_CHAN_PROPERTY
));
236 /* Extended Mass Media Controller */
237 object_property_set_int(OBJECT(&s
->sdhci
), BCM2835_SDHC_CAPAREG
, "capareg",
240 error_propagate(errp
, err
);
244 object_property_set_bool(OBJECT(&s
->sdhci
), true, "pending-insert-quirk",
247 error_propagate(errp
, err
);
251 object_property_set_bool(OBJECT(&s
->sdhci
), true, "realized", &err
);
253 error_propagate(errp
, err
);
257 memory_region_add_subregion(&s
->peri_mr
, EMMC_OFFSET
,
258 sysbus_mmio_get_region(SYS_BUS_DEVICE(&s
->sdhci
), 0));
259 sysbus_connect_irq(SYS_BUS_DEVICE(&s
->sdhci
), 0,
260 qdev_get_gpio_in_named(DEVICE(&s
->ic
), BCM2835_IC_GPU_IRQ
,
261 INTERRUPT_ARASANSDIO
));
262 object_property_add_alias(OBJECT(s
), "sd-bus", OBJECT(&s
->sdhci
), "sd-bus",
265 error_propagate(errp
, err
);
270 object_property_set_bool(OBJECT(&s
->dma
), true, "realized", &err
);
272 error_propagate(errp
, err
);
276 memory_region_add_subregion(&s
->peri_mr
, DMA_OFFSET
,
277 sysbus_mmio_get_region(SYS_BUS_DEVICE(&s
->dma
), 0));
278 memory_region_add_subregion(&s
->peri_mr
, DMA15_OFFSET
,
279 sysbus_mmio_get_region(SYS_BUS_DEVICE(&s
->dma
), 1));
281 for (n
= 0; n
<= 12; n
++) {
282 sysbus_connect_irq(SYS_BUS_DEVICE(&s
->dma
), n
,
283 qdev_get_gpio_in_named(DEVICE(&s
->ic
),
285 INTERRUPT_DMA0
+ n
));
289 static void bcm2835_peripherals_class_init(ObjectClass
*oc
, void *data
)
291 DeviceClass
*dc
= DEVICE_CLASS(oc
);
293 dc
->realize
= bcm2835_peripherals_realize
;
294 /* Reason: realize() method uses qemu_char_get_next_serial() */
295 dc
->cannot_instantiate_with_device_add_yet
= true;
298 static const TypeInfo bcm2835_peripherals_type_info
= {
299 .name
= TYPE_BCM2835_PERIPHERALS
,
300 .parent
= TYPE_SYS_BUS_DEVICE
,
301 .instance_size
= sizeof(BCM2835PeripheralState
),
302 .instance_init
= bcm2835_peripherals_init
,
303 .class_init
= bcm2835_peripherals_class_init
,
306 static void bcm2835_peripherals_register_types(void)
308 type_register_static(&bcm2835_peripherals_type_info
);
311 type_init(bcm2835_peripherals_register_types
)