xref: /openbmc/qemu/hw/arm/bcm2835_peripherals.c (revision da278d58)
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     /* Mphi */
130     sysbus_init_child_obj(obj, "mphi", &s->mphi, sizeof(s->mphi),
131                           TYPE_BCM2835_MPHI);
132 
133     /* DWC2 */
134     sysbus_init_child_obj(obj, "dwc2", &s->dwc2, sizeof(s->dwc2),
135                           TYPE_DWC2_USB);
136 
137     object_property_add_const_link(OBJECT(&s->dwc2), "dma-mr",
138                                    OBJECT(&s->gpu_bus_mr));
139 }
140 
141 static void bcm2835_peripherals_realize(DeviceState *dev, Error **errp)
142 {
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;
149 
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;
155     }
156 
157     ram = MEMORY_REGION(obj);
158     ram_size = memory_region_size(ram);
159 
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));
164 
165     memory_region_add_subregion_overlap(&s->gpu_bus_mr, BCM2835_VC_PERI_BASE,
166                                         &s->peri_mr_alias, 1);
167 
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);
174     }
175 
176     /* Interrupt Controller */
177     object_property_set_bool(OBJECT(&s->ic), true, "realized", &err);
178     if (err) {
179         error_propagate(errp, err);
180         return;
181     }
182 
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));
186 
187     /* Sys Timer */
188     object_property_set_bool(OBJECT(&s->systmr), true, "realized", &err);
189     if (err) {
190         error_propagate(errp, err);
191         return;
192     }
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));
198 
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;
205     }
206 
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));
212 
213     /* AUX / UART1 */
214     qdev_prop_set_chr(DEVICE(&s->aux), "chardev", serial_hd(1));
215 
216     object_property_set_bool(OBJECT(&s->aux), true, "realized", &err);
217     if (err) {
218         error_propagate(errp, err);
219         return;
220     }
221 
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));
227 
228     /* Mailboxes */
229     object_property_set_bool(OBJECT(&s->mboxes), true, "realized", &err);
230     if (err) {
231         error_propagate(errp, err);
232         return;
233     }
234 
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));
240 
241     /* Framebuffer */
242     vcram_size = object_property_get_uint(OBJECT(s), "vcram-size", &err);
243     if (err) {
244         error_propagate(errp, err);
245         return;
246     }
247 
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;
253     }
254 
255     object_property_set_bool(OBJECT(&s->fb), true, "realized", &err);
256     if (err) {
257         error_propagate(errp, err);
258         return;
259     }
260 
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));
265 
266     /* Property channel */
267     object_property_set_bool(OBJECT(&s->property), true, "realized", &err);
268     if (err) {
269         error_propagate(errp, err);
270         return;
271     }
272 
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));
278 
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;
284     }
285 
286     memory_region_add_subregion(&s->peri_mr, RNG_OFFSET,
287                 sysbus_mmio_get_region(SYS_BUS_DEVICE(&s->rng), 0));
288 
289     /* Extended Mass Media Controller
290      *
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
295      *
296      * For the exact details please refer to the Arasan documentation:
297      *   SD3.0_Host_AHB_eMMC4.4_Usersguide_ver5.9_jan11_10.pdf
298      */
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;
307     }
308 
309     object_property_set_bool(OBJECT(&s->sdhci), true, "realized", &err);
310     if (err) {
311         error_propagate(errp, err);
312         return;
313     }
314 
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));
320 
321     /* SDHOST */
322     object_property_set_bool(OBJECT(&s->sdhost), true, "realized", &err);
323     if (err) {
324         error_propagate(errp, err);
325         return;
326     }
327 
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));
333 
334     /* DMA Channels */
335     object_property_set_bool(OBJECT(&s->dma), true, "realized", &err);
336     if (err) {
337         error_propagate(errp, err);
338         return;
339     }
340 
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));
345 
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));
351     }
352 
353     /* THERMAL */
354     object_property_set_bool(OBJECT(&s->thermal), true, "realized", &err);
355     if (err) {
356         error_propagate(errp, err);
357         return;
358     }
359     memory_region_add_subregion(&s->peri_mr, THERMAL_OFFSET,
360                 sysbus_mmio_get_region(SYS_BUS_DEVICE(&s->thermal), 0));
361 
362     /* GPIO */
363     object_property_set_bool(OBJECT(&s->gpio), true, "realized", &err);
364     if (err) {
365         error_propagate(errp, err);
366         return;
367     }
368 
369     memory_region_add_subregion(&s->peri_mr, GPIO_OFFSET,
370                 sysbus_mmio_get_region(SYS_BUS_DEVICE(&s->gpio), 0));
371 
372     object_property_add_alias(OBJECT(s), "sd-bus", OBJECT(&s->gpio), "sd-bus");
373 
374     /* Mphi */
375     object_property_set_bool(OBJECT(&s->mphi), true, "realized", &err);
376     if (err) {
377         error_propagate(errp, err);
378         return;
379     }
380 
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));
386 
387     /* DWC2 */
388     object_property_set_bool(OBJECT(&s->dwc2), true, "realized", &err);
389     if (err) {
390         error_propagate(errp, err);
391         return;
392     }
393 
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));
399 
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);
414 }
415 
416 static void bcm2835_peripherals_class_init(ObjectClass *oc, void *data)
417 {
418     DeviceClass *dc = DEVICE_CLASS(oc);
419 
420     dc->realize = bcm2835_peripherals_realize;
421 }
422 
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,
429 };
430 
431 static void bcm2835_peripherals_register_types(void)
432 {
433     type_register_static(&bcm2835_peripherals_type_info);
434 }
435 
436 type_init(bcm2835_peripherals_register_types)
437