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 "qapi/error.h" 13 #include "hw/arm/bcm2835_peripherals.h" 14 #include "hw/misc/bcm2835_mbox_defs.h" 15 #include "hw/arm/raspi_platform.h" 16 #include "sysemu/char.h" 17 #include "sysemu/sysemu.h" 18 19 /* Peripheral base address on the VC (GPU) system bus */ 20 #define BCM2835_VC_PERI_BASE 0x7e000000 21 22 /* Capabilities for SD controller: no DMA, high-speed, default clocks etc. */ 23 #define BCM2835_SDHC_CAPAREG 0x52034b4 24 25 static void bcm2835_peripherals_init(Object *obj) 26 { 27 BCM2835PeripheralState *s = BCM2835_PERIPHERALS(obj); 28 29 /* Memory region for peripheral devices, which we export to our parent */ 30 memory_region_init(&s->peri_mr, obj,"bcm2835-peripherals", 0x1000000); 31 object_property_add_child(obj, "peripheral-io", OBJECT(&s->peri_mr), NULL); 32 sysbus_init_mmio(SYS_BUS_DEVICE(s), &s->peri_mr); 33 34 /* Internal memory region for peripheral bus addresses (not exported) */ 35 memory_region_init(&s->gpu_bus_mr, obj, "bcm2835-gpu", (uint64_t)1 << 32); 36 object_property_add_child(obj, "gpu-bus", OBJECT(&s->gpu_bus_mr), NULL); 37 38 /* Internal memory region for request/response communication with 39 * mailbox-addressable peripherals (not exported) 40 */ 41 memory_region_init(&s->mbox_mr, obj, "bcm2835-mbox", 42 MBOX_CHAN_COUNT << MBOX_AS_CHAN_SHIFT); 43 44 /* Interrupt Controller */ 45 object_initialize(&s->ic, sizeof(s->ic), TYPE_BCM2835_IC); 46 object_property_add_child(obj, "ic", OBJECT(&s->ic), NULL); 47 qdev_set_parent_bus(DEVICE(&s->ic), sysbus_get_default()); 48 49 /* UART0 */ 50 s->uart0 = SYS_BUS_DEVICE(object_new("pl011")); 51 object_property_add_child(obj, "uart0", OBJECT(s->uart0), NULL); 52 qdev_set_parent_bus(DEVICE(s->uart0), sysbus_get_default()); 53 54 /* AUX / UART1 */ 55 object_initialize(&s->aux, sizeof(s->aux), TYPE_BCM2835_AUX); 56 object_property_add_child(obj, "aux", OBJECT(&s->aux), NULL); 57 qdev_set_parent_bus(DEVICE(&s->aux), sysbus_get_default()); 58 59 /* Mailboxes */ 60 object_initialize(&s->mboxes, sizeof(s->mboxes), TYPE_BCM2835_MBOX); 61 object_property_add_child(obj, "mbox", OBJECT(&s->mboxes), NULL); 62 qdev_set_parent_bus(DEVICE(&s->mboxes), sysbus_get_default()); 63 64 object_property_add_const_link(OBJECT(&s->mboxes), "mbox-mr", 65 OBJECT(&s->mbox_mr), &error_abort); 66 67 /* Framebuffer */ 68 object_initialize(&s->fb, sizeof(s->fb), TYPE_BCM2835_FB); 69 object_property_add_child(obj, "fb", OBJECT(&s->fb), NULL); 70 object_property_add_alias(obj, "vcram-size", OBJECT(&s->fb), "vcram-size", 71 &error_abort); 72 qdev_set_parent_bus(DEVICE(&s->fb), sysbus_get_default()); 73 74 object_property_add_const_link(OBJECT(&s->fb), "dma-mr", 75 OBJECT(&s->gpu_bus_mr), &error_abort); 76 77 /* Property channel */ 78 object_initialize(&s->property, sizeof(s->property), TYPE_BCM2835_PROPERTY); 79 object_property_add_child(obj, "property", OBJECT(&s->property), NULL); 80 object_property_add_alias(obj, "board-rev", OBJECT(&s->property), 81 "board-rev", &error_abort); 82 qdev_set_parent_bus(DEVICE(&s->property), sysbus_get_default()); 83 84 object_property_add_const_link(OBJECT(&s->property), "fb", 85 OBJECT(&s->fb), &error_abort); 86 object_property_add_const_link(OBJECT(&s->property), "dma-mr", 87 OBJECT(&s->gpu_bus_mr), &error_abort); 88 89 /* Random Number Generator */ 90 object_initialize(&s->rng, sizeof(s->rng), TYPE_BCM2835_RNG); 91 object_property_add_child(obj, "rng", OBJECT(&s->rng), NULL); 92 qdev_set_parent_bus(DEVICE(&s->rng), sysbus_get_default()); 93 94 /* Extended Mass Media Controller */ 95 object_initialize(&s->sdhci, sizeof(s->sdhci), TYPE_SYSBUS_SDHCI); 96 object_property_add_child(obj, "sdhci", OBJECT(&s->sdhci), NULL); 97 qdev_set_parent_bus(DEVICE(&s->sdhci), sysbus_get_default()); 98 99 /* SDHOST */ 100 object_initialize(&s->sdhost, sizeof(s->sdhost), TYPE_BCM2835_SDHOST); 101 object_property_add_child(obj, "sdhost", OBJECT(&s->sdhost), NULL); 102 qdev_set_parent_bus(DEVICE(&s->sdhost), sysbus_get_default()); 103 104 /* DMA Channels */ 105 object_initialize(&s->dma, sizeof(s->dma), TYPE_BCM2835_DMA); 106 object_property_add_child(obj, "dma", OBJECT(&s->dma), NULL); 107 qdev_set_parent_bus(DEVICE(&s->dma), sysbus_get_default()); 108 109 object_property_add_const_link(OBJECT(&s->dma), "dma-mr", 110 OBJECT(&s->gpu_bus_mr), &error_abort); 111 112 /* GPIO */ 113 object_initialize(&s->gpio, sizeof(s->gpio), TYPE_BCM2835_GPIO); 114 object_property_add_child(obj, "gpio", OBJECT(&s->gpio), NULL); 115 qdev_set_parent_bus(DEVICE(&s->gpio), sysbus_get_default()); 116 117 object_property_add_const_link(OBJECT(&s->gpio), "sdbus-sdhci", 118 OBJECT(&s->sdhci.sdbus), &error_abort); 119 object_property_add_const_link(OBJECT(&s->gpio), "sdbus-sdhost", 120 OBJECT(&s->sdhost.sdbus), &error_abort); 121 } 122 123 static void bcm2835_peripherals_realize(DeviceState *dev, Error **errp) 124 { 125 BCM2835PeripheralState *s = BCM2835_PERIPHERALS(dev); 126 Object *obj; 127 MemoryRegion *ram; 128 Error *err = NULL; 129 uint32_t ram_size, vcram_size; 130 int n; 131 132 obj = object_property_get_link(OBJECT(dev), "ram", &err); 133 if (obj == NULL) { 134 error_setg(errp, "%s: required ram link not found: %s", 135 __func__, error_get_pretty(err)); 136 return; 137 } 138 139 ram = MEMORY_REGION(obj); 140 ram_size = memory_region_size(ram); 141 142 /* Map peripherals and RAM into the GPU address space. */ 143 memory_region_init_alias(&s->peri_mr_alias, OBJECT(s), 144 "bcm2835-peripherals", &s->peri_mr, 0, 145 memory_region_size(&s->peri_mr)); 146 147 memory_region_add_subregion_overlap(&s->gpu_bus_mr, BCM2835_VC_PERI_BASE, 148 &s->peri_mr_alias, 1); 149 150 /* RAM is aliased four times (different cache configurations) on the GPU */ 151 for (n = 0; n < 4; n++) { 152 memory_region_init_alias(&s->ram_alias[n], OBJECT(s), 153 "bcm2835-gpu-ram-alias[*]", ram, 0, ram_size); 154 memory_region_add_subregion_overlap(&s->gpu_bus_mr, (hwaddr)n << 30, 155 &s->ram_alias[n], 0); 156 } 157 158 /* Interrupt Controller */ 159 object_property_set_bool(OBJECT(&s->ic), true, "realized", &err); 160 if (err) { 161 error_propagate(errp, err); 162 return; 163 } 164 165 memory_region_add_subregion(&s->peri_mr, ARMCTRL_IC_OFFSET, 166 sysbus_mmio_get_region(SYS_BUS_DEVICE(&s->ic), 0)); 167 sysbus_pass_irq(SYS_BUS_DEVICE(s), SYS_BUS_DEVICE(&s->ic)); 168 169 /* UART0 */ 170 qdev_prop_set_chr(DEVICE(s->uart0), "chardev", serial_hds[0]); 171 object_property_set_bool(OBJECT(s->uart0), true, "realized", &err); 172 if (err) { 173 error_propagate(errp, err); 174 return; 175 } 176 177 memory_region_add_subregion(&s->peri_mr, UART0_OFFSET, 178 sysbus_mmio_get_region(s->uart0, 0)); 179 sysbus_connect_irq(s->uart0, 0, 180 qdev_get_gpio_in_named(DEVICE(&s->ic), BCM2835_IC_GPU_IRQ, 181 INTERRUPT_UART)); 182 /* AUX / UART1 */ 183 qdev_prop_set_chr(DEVICE(&s->aux), "chardev", serial_hds[1]); 184 185 object_property_set_bool(OBJECT(&s->aux), true, "realized", &err); 186 if (err) { 187 error_propagate(errp, err); 188 return; 189 } 190 191 memory_region_add_subregion(&s->peri_mr, UART1_OFFSET, 192 sysbus_mmio_get_region(SYS_BUS_DEVICE(&s->aux), 0)); 193 sysbus_connect_irq(SYS_BUS_DEVICE(&s->aux), 0, 194 qdev_get_gpio_in_named(DEVICE(&s->ic), BCM2835_IC_GPU_IRQ, 195 INTERRUPT_AUX)); 196 197 /* Mailboxes */ 198 object_property_set_bool(OBJECT(&s->mboxes), true, "realized", &err); 199 if (err) { 200 error_propagate(errp, err); 201 return; 202 } 203 204 memory_region_add_subregion(&s->peri_mr, ARMCTRL_0_SBM_OFFSET, 205 sysbus_mmio_get_region(SYS_BUS_DEVICE(&s->mboxes), 0)); 206 sysbus_connect_irq(SYS_BUS_DEVICE(&s->mboxes), 0, 207 qdev_get_gpio_in_named(DEVICE(&s->ic), BCM2835_IC_ARM_IRQ, 208 INTERRUPT_ARM_MAILBOX)); 209 210 /* Framebuffer */ 211 vcram_size = (uint32_t)object_property_get_int(OBJECT(s), "vcram-size", 212 &err); 213 if (err) { 214 error_propagate(errp, err); 215 return; 216 } 217 218 object_property_set_int(OBJECT(&s->fb), ram_size - vcram_size, 219 "vcram-base", &err); 220 if (err) { 221 error_propagate(errp, err); 222 return; 223 } 224 225 object_property_set_bool(OBJECT(&s->fb), true, "realized", &err); 226 if (err) { 227 error_propagate(errp, err); 228 return; 229 } 230 231 memory_region_add_subregion(&s->mbox_mr, MBOX_CHAN_FB << MBOX_AS_CHAN_SHIFT, 232 sysbus_mmio_get_region(SYS_BUS_DEVICE(&s->fb), 0)); 233 sysbus_connect_irq(SYS_BUS_DEVICE(&s->fb), 0, 234 qdev_get_gpio_in(DEVICE(&s->mboxes), MBOX_CHAN_FB)); 235 236 /* Property channel */ 237 object_property_set_bool(OBJECT(&s->property), true, "realized", &err); 238 if (err) { 239 error_propagate(errp, err); 240 return; 241 } 242 243 memory_region_add_subregion(&s->mbox_mr, 244 MBOX_CHAN_PROPERTY << MBOX_AS_CHAN_SHIFT, 245 sysbus_mmio_get_region(SYS_BUS_DEVICE(&s->property), 0)); 246 sysbus_connect_irq(SYS_BUS_DEVICE(&s->property), 0, 247 qdev_get_gpio_in(DEVICE(&s->mboxes), MBOX_CHAN_PROPERTY)); 248 249 /* Random Number Generator */ 250 object_property_set_bool(OBJECT(&s->rng), true, "realized", &err); 251 if (err) { 252 error_propagate(errp, err); 253 return; 254 } 255 256 memory_region_add_subregion(&s->peri_mr, RNG_OFFSET, 257 sysbus_mmio_get_region(SYS_BUS_DEVICE(&s->rng), 0)); 258 259 /* Extended Mass Media Controller */ 260 object_property_set_int(OBJECT(&s->sdhci), BCM2835_SDHC_CAPAREG, "capareg", 261 &err); 262 if (err) { 263 error_propagate(errp, err); 264 return; 265 } 266 267 object_property_set_bool(OBJECT(&s->sdhci), true, "pending-insert-quirk", 268 &err); 269 if (err) { 270 error_propagate(errp, err); 271 return; 272 } 273 274 object_property_set_bool(OBJECT(&s->sdhci), true, "realized", &err); 275 if (err) { 276 error_propagate(errp, err); 277 return; 278 } 279 280 memory_region_add_subregion(&s->peri_mr, EMMC_OFFSET, 281 sysbus_mmio_get_region(SYS_BUS_DEVICE(&s->sdhci), 0)); 282 sysbus_connect_irq(SYS_BUS_DEVICE(&s->sdhci), 0, 283 qdev_get_gpio_in_named(DEVICE(&s->ic), BCM2835_IC_GPU_IRQ, 284 INTERRUPT_ARASANSDIO)); 285 286 /* SDHOST */ 287 object_property_set_bool(OBJECT(&s->sdhost), true, "realized", &err); 288 if (err) { 289 error_propagate(errp, err); 290 return; 291 } 292 293 memory_region_add_subregion(&s->peri_mr, MMCI0_OFFSET, 294 sysbus_mmio_get_region(SYS_BUS_DEVICE(&s->sdhost), 0)); 295 sysbus_connect_irq(SYS_BUS_DEVICE(&s->sdhost), 0, 296 qdev_get_gpio_in_named(DEVICE(&s->ic), BCM2835_IC_GPU_IRQ, 297 INTERRUPT_SDIO)); 298 299 /* DMA Channels */ 300 object_property_set_bool(OBJECT(&s->dma), true, "realized", &err); 301 if (err) { 302 error_propagate(errp, err); 303 return; 304 } 305 306 memory_region_add_subregion(&s->peri_mr, DMA_OFFSET, 307 sysbus_mmio_get_region(SYS_BUS_DEVICE(&s->dma), 0)); 308 memory_region_add_subregion(&s->peri_mr, DMA15_OFFSET, 309 sysbus_mmio_get_region(SYS_BUS_DEVICE(&s->dma), 1)); 310 311 for (n = 0; n <= 12; n++) { 312 sysbus_connect_irq(SYS_BUS_DEVICE(&s->dma), n, 313 qdev_get_gpio_in_named(DEVICE(&s->ic), 314 BCM2835_IC_GPU_IRQ, 315 INTERRUPT_DMA0 + n)); 316 } 317 318 /* GPIO */ 319 object_property_set_bool(OBJECT(&s->gpio), true, "realized", &err); 320 if (err) { 321 error_propagate(errp, err); 322 return; 323 } 324 325 memory_region_add_subregion(&s->peri_mr, GPIO_OFFSET, 326 sysbus_mmio_get_region(SYS_BUS_DEVICE(&s->gpio), 0)); 327 328 object_property_add_alias(OBJECT(s), "sd-bus", OBJECT(&s->gpio), "sd-bus", 329 &err); 330 if (err) { 331 error_propagate(errp, err); 332 return; 333 } 334 } 335 336 static void bcm2835_peripherals_class_init(ObjectClass *oc, void *data) 337 { 338 DeviceClass *dc = DEVICE_CLASS(oc); 339 340 dc->realize = bcm2835_peripherals_realize; 341 } 342 343 static const TypeInfo bcm2835_peripherals_type_info = { 344 .name = TYPE_BCM2835_PERIPHERALS, 345 .parent = TYPE_SYS_BUS_DEVICE, 346 .instance_size = sizeof(BCM2835PeripheralState), 347 .instance_init = bcm2835_peripherals_init, 348 .class_init = bcm2835_peripherals_class_init, 349 }; 350 351 static void bcm2835_peripherals_register_types(void) 352 { 353 type_register_static(&bcm2835_peripherals_type_info); 354 } 355 356 type_init(bcm2835_peripherals_register_types) 357