xref: /openbmc/qemu/hw/arm/bcm2835_peripherals.c (revision 0c4e9931)
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/bcm2835_peripherals.h"
16 #include "hw/misc/bcm2835_mbox_defs.h"
17 #include "hw/arm/raspi_platform.h"
18 #include "sysemu/sysemu.h"
19 
20 /* Peripheral base address on the VC (GPU) system bus */
21 #define BCM2835_VC_PERI_BASE 0x7e000000
22 
23 /* Capabilities for SD controller: no DMA, high-speed, default clocks etc. */
24 #define BCM2835_SDHC_CAPAREG 0x52134b4
25 
26 static void create_unimp(BCM2835PeripheralState *ps,
27                          UnimplementedDeviceState *uds,
28                          const char *name, hwaddr ofs, hwaddr size)
29 {
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);
38 }
39 
40 static void bcm2835_peripherals_init(Object *obj)
41 {
42     BCM2835PeripheralState *s = BCM2835_PERIPHERALS(obj);
43 
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);
47 
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);
50 
51     /* Internal memory region for request/response communication with
52      * mailbox-addressable peripherals (not exported)
53      */
54     memory_region_init(&s->mbox_mr, obj, "bcm2835-mbox",
55                        MBOX_CHAN_COUNT << MBOX_AS_CHAN_SHIFT);
56 
57     /* Interrupt Controller */
58     sysbus_init_child_obj(obj, "ic", &s->ic, sizeof(s->ic), TYPE_BCM2835_IC);
59 
60     /* SYS Timer */
61     sysbus_init_child_obj(obj, "systimer", &s->systmr, sizeof(s->systmr),
62                           TYPE_BCM2835_SYSTIMER);
63 
64     /* UART0 */
65     sysbus_init_child_obj(obj, "uart0", &s->uart0, sizeof(s->uart0),
66                           TYPE_PL011);
67 
68     /* AUX / UART1 */
69     sysbus_init_child_obj(obj, "aux", &s->aux, sizeof(s->aux),
70                           TYPE_BCM2835_AUX);
71 
72     /* Mailboxes */
73     sysbus_init_child_obj(obj, "mbox", &s->mboxes, sizeof(s->mboxes),
74                           TYPE_BCM2835_MBOX);
75 
76     object_property_add_const_link(OBJECT(&s->mboxes), "mbox-mr",
77                                    OBJECT(&s->mbox_mr));
78 
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");
82 
83     object_property_add_const_link(OBJECT(&s->fb), "dma-mr",
84                                    OBJECT(&s->gpu_bus_mr));
85 
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");
91 
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));
96 
97     /* Random Number Generator */
98     sysbus_init_child_obj(obj, "rng", &s->rng, sizeof(s->rng),
99                           TYPE_BCM2835_RNG);
100 
101     /* Extended Mass Media Controller */
102     sysbus_init_child_obj(obj, "sdhci", &s->sdhci, sizeof(s->sdhci),
103                           TYPE_SYSBUS_SDHCI);
104 
105     /* SDHOST */
106     sysbus_init_child_obj(obj, "sdhost", &s->sdhost, sizeof(s->sdhost),
107                           TYPE_BCM2835_SDHOST);
108 
109     /* DMA Channels */
110     sysbus_init_child_obj(obj, "dma", &s->dma, sizeof(s->dma),
111                           TYPE_BCM2835_DMA);
112 
113     object_property_add_const_link(OBJECT(&s->dma), "dma-mr",
114                                    OBJECT(&s->gpu_bus_mr));
115 
116     /* Thermal */
117     sysbus_init_child_obj(obj, "thermal", &s->thermal, sizeof(s->thermal),
118                           TYPE_BCM2835_THERMAL);
119 
120     /* GPIO */
121     sysbus_init_child_obj(obj, "gpio", &s->gpio, sizeof(s->gpio),
122                           TYPE_BCM2835_GPIO);
123 
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));
128 }
129 
130 static void bcm2835_peripherals_realize(DeviceState *dev, Error **errp)
131 {
132     BCM2835PeripheralState *s = BCM2835_PERIPHERALS(dev);
133     Object *obj;
134     MemoryRegion *ram;
135     Error *err = NULL;
136     uint64_t ram_size, vcram_size;
137     int n;
138 
139     obj = object_property_get_link(OBJECT(dev), "ram", &err);
140     if (obj == NULL) {
141         error_setg(errp, "%s: required ram link not found: %s",
142                    __func__, error_get_pretty(err));
143         return;
144     }
145 
146     ram = MEMORY_REGION(obj);
147     ram_size = memory_region_size(ram);
148 
149     /* Map peripherals and RAM into the GPU address space. */
150     memory_region_init_alias(&s->peri_mr_alias, OBJECT(s),
151                              "bcm2835-peripherals", &s->peri_mr, 0,
152                              memory_region_size(&s->peri_mr));
153 
154     memory_region_add_subregion_overlap(&s->gpu_bus_mr, BCM2835_VC_PERI_BASE,
155                                         &s->peri_mr_alias, 1);
156 
157     /* RAM is aliased four times (different cache configurations) on the GPU */
158     for (n = 0; n < 4; n++) {
159         memory_region_init_alias(&s->ram_alias[n], OBJECT(s),
160                                  "bcm2835-gpu-ram-alias[*]", ram, 0, ram_size);
161         memory_region_add_subregion_overlap(&s->gpu_bus_mr, (hwaddr)n << 30,
162                                             &s->ram_alias[n], 0);
163     }
164 
165     /* Interrupt Controller */
166     object_property_set_bool(OBJECT(&s->ic), true, "realized", &err);
167     if (err) {
168         error_propagate(errp, err);
169         return;
170     }
171 
172     memory_region_add_subregion(&s->peri_mr, ARMCTRL_IC_OFFSET,
173                 sysbus_mmio_get_region(SYS_BUS_DEVICE(&s->ic), 0));
174     sysbus_pass_irq(SYS_BUS_DEVICE(s), SYS_BUS_DEVICE(&s->ic));
175 
176     /* Sys Timer */
177     object_property_set_bool(OBJECT(&s->systmr), true, "realized", &err);
178     if (err) {
179         error_propagate(errp, err);
180         return;
181     }
182     memory_region_add_subregion(&s->peri_mr, ST_OFFSET,
183                 sysbus_mmio_get_region(SYS_BUS_DEVICE(&s->systmr), 0));
184     sysbus_connect_irq(SYS_BUS_DEVICE(&s->systmr), 0,
185         qdev_get_gpio_in_named(DEVICE(&s->ic), BCM2835_IC_ARM_IRQ,
186                                INTERRUPT_ARM_TIMER));
187 
188     /* UART0 */
189     qdev_prop_set_chr(DEVICE(&s->uart0), "chardev", serial_hd(0));
190     object_property_set_bool(OBJECT(&s->uart0), true, "realized", &err);
191     if (err) {
192         error_propagate(errp, err);
193         return;
194     }
195 
196     memory_region_add_subregion(&s->peri_mr, UART0_OFFSET,
197                 sysbus_mmio_get_region(SYS_BUS_DEVICE(&s->uart0), 0));
198     sysbus_connect_irq(SYS_BUS_DEVICE(&s->uart0), 0,
199         qdev_get_gpio_in_named(DEVICE(&s->ic), BCM2835_IC_GPU_IRQ,
200                                INTERRUPT_UART0));
201 
202     /* AUX / UART1 */
203     qdev_prop_set_chr(DEVICE(&s->aux), "chardev", serial_hd(1));
204 
205     object_property_set_bool(OBJECT(&s->aux), true, "realized", &err);
206     if (err) {
207         error_propagate(errp, err);
208         return;
209     }
210 
211     memory_region_add_subregion(&s->peri_mr, AUX_OFFSET,
212                 sysbus_mmio_get_region(SYS_BUS_DEVICE(&s->aux), 0));
213     sysbus_connect_irq(SYS_BUS_DEVICE(&s->aux), 0,
214         qdev_get_gpio_in_named(DEVICE(&s->ic), BCM2835_IC_GPU_IRQ,
215                                INTERRUPT_AUX));
216 
217     /* Mailboxes */
218     object_property_set_bool(OBJECT(&s->mboxes), true, "realized", &err);
219     if (err) {
220         error_propagate(errp, err);
221         return;
222     }
223 
224     memory_region_add_subregion(&s->peri_mr, ARMCTRL_0_SBM_OFFSET,
225                 sysbus_mmio_get_region(SYS_BUS_DEVICE(&s->mboxes), 0));
226     sysbus_connect_irq(SYS_BUS_DEVICE(&s->mboxes), 0,
227         qdev_get_gpio_in_named(DEVICE(&s->ic), BCM2835_IC_ARM_IRQ,
228                                INTERRUPT_ARM_MAILBOX));
229 
230     /* Framebuffer */
231     vcram_size = object_property_get_uint(OBJECT(s), "vcram-size", &err);
232     if (err) {
233         error_propagate(errp, err);
234         return;
235     }
236 
237     object_property_set_uint(OBJECT(&s->fb), ram_size - vcram_size,
238                              "vcram-base", &err);
239     if (err) {
240         error_propagate(errp, err);
241         return;
242     }
243 
244     object_property_set_bool(OBJECT(&s->fb), true, "realized", &err);
245     if (err) {
246         error_propagate(errp, err);
247         return;
248     }
249 
250     memory_region_add_subregion(&s->mbox_mr, MBOX_CHAN_FB << MBOX_AS_CHAN_SHIFT,
251                 sysbus_mmio_get_region(SYS_BUS_DEVICE(&s->fb), 0));
252     sysbus_connect_irq(SYS_BUS_DEVICE(&s->fb), 0,
253                        qdev_get_gpio_in(DEVICE(&s->mboxes), MBOX_CHAN_FB));
254 
255     /* Property channel */
256     object_property_set_bool(OBJECT(&s->property), true, "realized", &err);
257     if (err) {
258         error_propagate(errp, err);
259         return;
260     }
261 
262     memory_region_add_subregion(&s->mbox_mr,
263                 MBOX_CHAN_PROPERTY << MBOX_AS_CHAN_SHIFT,
264                 sysbus_mmio_get_region(SYS_BUS_DEVICE(&s->property), 0));
265     sysbus_connect_irq(SYS_BUS_DEVICE(&s->property), 0,
266                       qdev_get_gpio_in(DEVICE(&s->mboxes), MBOX_CHAN_PROPERTY));
267 
268     /* Random Number Generator */
269     object_property_set_bool(OBJECT(&s->rng), true, "realized", &err);
270     if (err) {
271         error_propagate(errp, err);
272         return;
273     }
274 
275     memory_region_add_subregion(&s->peri_mr, RNG_OFFSET,
276                 sysbus_mmio_get_region(SYS_BUS_DEVICE(&s->rng), 0));
277 
278     /* Extended Mass Media Controller
279      *
280      * Compatible with:
281      * - SD Host Controller Specification Version 3.0 Draft 1.0
282      * - SDIO Specification Version 3.0
283      * - MMC Specification Version 4.4
284      *
285      * For the exact details please refer to the Arasan documentation:
286      *   SD3.0_Host_AHB_eMMC4.4_Usersguide_ver5.9_jan11_10.pdf
287      */
288     object_property_set_uint(OBJECT(&s->sdhci), 3, "sd-spec-version", &err);
289     object_property_set_uint(OBJECT(&s->sdhci), BCM2835_SDHC_CAPAREG, "capareg",
290                              &err);
291     object_property_set_bool(OBJECT(&s->sdhci), true, "pending-insert-quirk",
292                              &err);
293     if (err) {
294         error_propagate(errp, err);
295         return;
296     }
297 
298     object_property_set_bool(OBJECT(&s->sdhci), true, "realized", &err);
299     if (err) {
300         error_propagate(errp, err);
301         return;
302     }
303 
304     memory_region_add_subregion(&s->peri_mr, EMMC1_OFFSET,
305                 sysbus_mmio_get_region(SYS_BUS_DEVICE(&s->sdhci), 0));
306     sysbus_connect_irq(SYS_BUS_DEVICE(&s->sdhci), 0,
307         qdev_get_gpio_in_named(DEVICE(&s->ic), BCM2835_IC_GPU_IRQ,
308                                INTERRUPT_ARASANSDIO));
309 
310     /* SDHOST */
311     object_property_set_bool(OBJECT(&s->sdhost), true, "realized", &err);
312     if (err) {
313         error_propagate(errp, err);
314         return;
315     }
316 
317     memory_region_add_subregion(&s->peri_mr, MMCI0_OFFSET,
318                 sysbus_mmio_get_region(SYS_BUS_DEVICE(&s->sdhost), 0));
319     sysbus_connect_irq(SYS_BUS_DEVICE(&s->sdhost), 0,
320         qdev_get_gpio_in_named(DEVICE(&s->ic), BCM2835_IC_GPU_IRQ,
321                                INTERRUPT_SDIO));
322 
323     /* DMA Channels */
324     object_property_set_bool(OBJECT(&s->dma), true, "realized", &err);
325     if (err) {
326         error_propagate(errp, err);
327         return;
328     }
329 
330     memory_region_add_subregion(&s->peri_mr, DMA_OFFSET,
331                 sysbus_mmio_get_region(SYS_BUS_DEVICE(&s->dma), 0));
332     memory_region_add_subregion(&s->peri_mr, DMA15_OFFSET,
333                 sysbus_mmio_get_region(SYS_BUS_DEVICE(&s->dma), 1));
334 
335     for (n = 0; n <= 12; n++) {
336         sysbus_connect_irq(SYS_BUS_DEVICE(&s->dma), n,
337                            qdev_get_gpio_in_named(DEVICE(&s->ic),
338                                                   BCM2835_IC_GPU_IRQ,
339                                                   INTERRUPT_DMA0 + n));
340     }
341 
342     /* THERMAL */
343     object_property_set_bool(OBJECT(&s->thermal), true, "realized", &err);
344     if (err) {
345         error_propagate(errp, err);
346         return;
347     }
348     memory_region_add_subregion(&s->peri_mr, THERMAL_OFFSET,
349                 sysbus_mmio_get_region(SYS_BUS_DEVICE(&s->thermal), 0));
350 
351     /* GPIO */
352     object_property_set_bool(OBJECT(&s->gpio), true, "realized", &err);
353     if (err) {
354         error_propagate(errp, err);
355         return;
356     }
357 
358     memory_region_add_subregion(&s->peri_mr, GPIO_OFFSET,
359                 sysbus_mmio_get_region(SYS_BUS_DEVICE(&s->gpio), 0));
360 
361     object_property_add_alias(OBJECT(s), "sd-bus", OBJECT(&s->gpio), "sd-bus");
362 
363     create_unimp(s, &s->armtmr, "bcm2835-sp804", ARMCTRL_TIMER0_1_OFFSET, 0x40);
364     create_unimp(s, &s->cprman, "bcm2835-cprman", CPRMAN_OFFSET, 0x1000);
365     create_unimp(s, &s->a2w, "bcm2835-a2w", A2W_OFFSET, 0x1000);
366     create_unimp(s, &s->i2s, "bcm2835-i2s", I2S_OFFSET, 0x100);
367     create_unimp(s, &s->smi, "bcm2835-smi", SMI_OFFSET, 0x100);
368     create_unimp(s, &s->spi[0], "bcm2835-spi0", SPI0_OFFSET, 0x20);
369     create_unimp(s, &s->bscsl, "bcm2835-spis", BSC_SL_OFFSET, 0x100);
370     create_unimp(s, &s->i2c[0], "bcm2835-i2c0", BSC0_OFFSET, 0x20);
371     create_unimp(s, &s->i2c[1], "bcm2835-i2c1", BSC1_OFFSET, 0x20);
372     create_unimp(s, &s->i2c[2], "bcm2835-i2c2", BSC2_OFFSET, 0x20);
373     create_unimp(s, &s->otp, "bcm2835-otp", OTP_OFFSET, 0x80);
374     create_unimp(s, &s->dbus, "bcm2835-dbus", DBUS_OFFSET, 0x8000);
375     create_unimp(s, &s->ave0, "bcm2835-ave0", AVE0_OFFSET, 0x8000);
376     create_unimp(s, &s->dwc2, "dwc-usb2", USB_OTG_OFFSET, 0x1000);
377     create_unimp(s, &s->sdramc, "bcm2835-sdramc", SDRAMC_OFFSET, 0x100);
378 }
379 
380 static void bcm2835_peripherals_class_init(ObjectClass *oc, void *data)
381 {
382     DeviceClass *dc = DEVICE_CLASS(oc);
383 
384     dc->realize = bcm2835_peripherals_realize;
385 }
386 
387 static const TypeInfo bcm2835_peripherals_type_info = {
388     .name = TYPE_BCM2835_PERIPHERALS,
389     .parent = TYPE_SYS_BUS_DEVICE,
390     .instance_size = sizeof(BCM2835PeripheralState),
391     .instance_init = bcm2835_peripherals_init,
392     .class_init = bcm2835_peripherals_class_init,
393 };
394 
395 static void bcm2835_peripherals_register_types(void)
396 {
397     type_register_static(&bcm2835_peripherals_type_info);
398 }
399 
400 type_init(bcm2835_peripherals_register_types)
401