xref: /openbmc/qemu/hw/arm/bcm2835_peripherals.c (revision fe1a9cbc)
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 code is licensed under the GNU GPLv2 and later.
9  */
10 
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"
16 
17 /* Peripheral base address on the VC (GPU) system bus */
18 #define BCM2835_VC_PERI_BASE 0x7e000000
19 
20 /* Capabilities for SD controller: no DMA, high-speed, default clocks etc. */
21 #define BCM2835_SDHC_CAPAREG 0x52034b4
22 
23 static void bcm2835_peripherals_init(Object *obj)
24 {
25     BCM2835PeripheralState *s = BCM2835_PERIPHERALS(obj);
26 
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);
31 
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);
35 
36     /* Internal memory region for request/response communication with
37      * mailbox-addressable peripherals (not exported)
38      */
39     memory_region_init(&s->mbox_mr, obj, "bcm2835-mbox",
40                        MBOX_CHAN_COUNT << MBOX_AS_CHAN_SHIFT);
41 
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());
46 
47     /* UART0 */
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());
51 
52     /* AUX / UART1 */
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());
56 
57     /* Mailboxes */
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());
61 
62     object_property_add_const_link(OBJECT(&s->mboxes), "mbox-mr",
63                                    OBJECT(&s->mbox_mr), &error_abort);
64 
65     /* Framebuffer */
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",
69                               &error_abort);
70     qdev_set_parent_bus(DEVICE(&s->fb), sysbus_get_default());
71 
72     object_property_add_const_link(OBJECT(&s->fb), "dma-mr",
73                                    OBJECT(&s->gpu_bus_mr), &error_abort);
74 
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());
81 
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);
86 
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());
91 
92     /* DMA Channels */
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());
96 
97     object_property_add_const_link(OBJECT(&s->dma), "dma-mr",
98                                    OBJECT(&s->gpu_bus_mr), &error_abort);
99 }
100 
101 static void bcm2835_peripherals_realize(DeviceState *dev, Error **errp)
102 {
103     BCM2835PeripheralState *s = BCM2835_PERIPHERALS(dev);
104     Object *obj;
105     MemoryRegion *ram;
106     Error *err = NULL;
107     uint32_t ram_size, vcram_size;
108     CharDriverState *chr;
109     int n;
110 
111     obj = object_property_get_link(OBJECT(dev), "ram", &err);
112     if (obj == NULL) {
113         error_setg(errp, "%s: required ram link not found: %s",
114                    __func__, error_get_pretty(err));
115         return;
116     }
117 
118     ram = MEMORY_REGION(obj);
119     ram_size = memory_region_size(ram);
120 
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));
125 
126     memory_region_add_subregion_overlap(&s->gpu_bus_mr, BCM2835_VC_PERI_BASE,
127                                         &s->peri_mr_alias, 1);
128 
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);
135     }
136 
137     /* Interrupt Controller */
138     object_property_set_bool(OBJECT(&s->ic), true, "realized", &err);
139     if (err) {
140         error_propagate(errp, err);
141         return;
142     }
143 
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));
147 
148     /* UART0 */
149     object_property_set_bool(OBJECT(s->uart0), true, "realized", &err);
150     if (err) {
151         error_propagate(errp, err);
152         return;
153     }
154 
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,
159                                INTERRUPT_UART));
160 
161     /* AUX / UART1 */
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()
165      */
166     chr = qemu_char_get_next_serial();
167     if (chr == NULL) {
168         chr = qemu_chr_new("bcm2835.uart1", "null", NULL);
169     }
170     qdev_prop_set_chr(DEVICE(&s->aux), "chardev", chr);
171 
172     object_property_set_bool(OBJECT(&s->aux), true, "realized", &err);
173     if (err) {
174         error_propagate(errp, err);
175         return;
176     }
177 
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,
182                                INTERRUPT_AUX));
183 
184     /* Mailboxes */
185     object_property_set_bool(OBJECT(&s->mboxes), true, "realized", &err);
186     if (err) {
187         error_propagate(errp, err);
188         return;
189     }
190 
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));
196 
197     /* Framebuffer */
198     vcram_size = (uint32_t)object_property_get_int(OBJECT(s), "vcram-size",
199                                                    &err);
200     if (err) {
201         error_propagate(errp, err);
202         return;
203     }
204 
205     object_property_set_int(OBJECT(&s->fb), ram_size - vcram_size,
206                             "vcram-base", &err);
207     if (err) {
208         error_propagate(errp, err);
209         return;
210     }
211 
212     object_property_set_bool(OBJECT(&s->fb), true, "realized", &err);
213     if (err) {
214         error_propagate(errp, err);
215         return;
216     }
217 
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));
222 
223     /* Property channel */
224     object_property_set_bool(OBJECT(&s->property), true, "realized", &err);
225     if (err) {
226         error_propagate(errp, err);
227         return;
228     }
229 
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));
235 
236     /* Extended Mass Media Controller */
237     object_property_set_int(OBJECT(&s->sdhci), BCM2835_SDHC_CAPAREG, "capareg",
238                             &err);
239     if (err) {
240         error_propagate(errp, err);
241         return;
242     }
243 
244     object_property_set_bool(OBJECT(&s->sdhci), true, "pending-insert-quirk",
245                              &err);
246     if (err) {
247         error_propagate(errp, err);
248         return;
249     }
250 
251     object_property_set_bool(OBJECT(&s->sdhci), true, "realized", &err);
252     if (err) {
253         error_propagate(errp, err);
254         return;
255     }
256 
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",
263                               &err);
264     if (err) {
265         error_propagate(errp, err);
266         return;
267     }
268 
269     /* DMA Channels */
270     object_property_set_bool(OBJECT(&s->dma), true, "realized", &err);
271     if (err) {
272         error_propagate(errp, err);
273         return;
274     }
275 
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));
280 
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),
284                                                   BCM2835_IC_GPU_IRQ,
285                                                   INTERRUPT_DMA0 + n));
286     }
287 }
288 
289 static void bcm2835_peripherals_class_init(ObjectClass *oc, void *data)
290 {
291     DeviceClass *dc = DEVICE_CLASS(oc);
292 
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;
296 }
297 
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,
304 };
305 
306 static void bcm2835_peripherals_register_types(void)
307 {
308     type_register_static(&bcm2835_peripherals_type_info);
309 }
310 
311 type_init(bcm2835_peripherals_register_types)
312