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