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