2 * Raspberry Pi emulation (c) 2012 Gregory Estrade
3 * This code is licensed under the GNU GPLv2 and later.
6 #include "hw/misc/bcm2835_property.h"
7 #include "hw/misc/bcm2835_mbox_defs.h"
9 /* https://github.com/raspberrypi/firmware/wiki/Mailbox-property-interface */
11 static void bcm2835_property_mbox_push(BCM2835PropertyState *s, uint32_t value)
19 uint32_t offset, length, color;
21 uint32_t xres, yres, xoffset, yoffset, bpp, pixo, alpha;
22 uint32_t *newxres = NULL, *newyres = NULL, *newxoffset = NULL,
23 *newyoffset = NULL, *newbpp = NULL, *newpixo = NULL, *newalpha = NULL;
27 >>>>>>> upstreaming-raspi
33 tot_len = ldl_phys(&s->dma_as, value);
35 /* @(addr + 4) : Buffer response code */
37 while (value + 8 <= s->addr + tot_len) {
38 tag = ldl_phys(&s->dma_as, value);
39 bufsize = ldl_phys(&s->dma_as, value + 4);
40 /* @(value + 8) : Request/response indicator */
43 case 0x00000000: /* End tag */
45 case 0x00000001: /* Get firmware revision */
46 stl_phys(&s->dma_as, value + 12, 346337);
50 case 0x00010001: /* Get board model */
53 case 0x00010002: /* Get board revision */
56 case 0x00010003: /* Get board MAC address */
57 /* write the first four bytes of the 6-byte MAC */
58 stl_phys(&s->dma_as, value + 12, 0xB827EBD0);
59 /* write the last two bytes, avoid any write past the buffer end */
60 stb_phys(&s->dma_as, value + 16, 0xEE);
61 stb_phys(&s->dma_as, value + 17, 0xDF);
64 case 0x00010004: /* Get board serial */
67 case 0x00010005: /* Get ARM memory */
69 stl_phys(&s->dma_as, value + 12, 0);
72 stl_phys(&s->dma_as, value + 16, s->fbdev->vcram_base);
75 case 0x00010006: /* Get VC memory */
77 stl_phys(&s->dma_as, value + 12, s->fbdev->vcram_base);
79 stl_phys(&s->dma_as, value + 16, s->fbdev->vcram_size);
81 stl_phys(&s->dma_as, value + 16, s->ram_size);
82 >>>>>>> upstreaming-raspi
85 case 0x00028001: /* Set power state */
86 /* Assume that whatever device they asked for exists,
87 * and we'll just claim we set it to the desired state */
88 tmp = ldl_phys(&s->dma_as, value + 16);
89 stl_phys(&s->dma_as, value + 16, (tmp & 1));
95 case 0x00030001: /* Get clock state */
96 stl_phys(&s->dma_as, value + 16, 0x1);
100 case 0x00038001: /* Set clock state */
104 case 0x00030002: /* Get clock rate */
105 case 0x00030004: /* Get max clock rate */
106 case 0x00030007: /* Get min clock rate */
107 switch (ldl_phys(&s->dma_as, value + 12)) {
109 stl_phys(&s->dma_as, value + 16, 50000000);
112 stl_phys(&s->dma_as, value + 16, 3000000);
115 stl_phys(&s->dma_as, value + 16, 700000000);
121 case 0x00038002: /* Set clock rate */
122 case 0x00038004: /* Set max clock rate */
123 case 0x00038007: /* Set min clock rate */
129 case 0x00030006: /* Get temperature */
130 stl_phys(&s->dma_as, value + 16, 25000);
134 case 0x0003000A: /* Get max temperature */
135 stl_phys(&s->dma_as, value + 16, 99000);
143 case 0x00040001: /* Allocate buffer */
144 stl_phys(&s->dma_as, value + 12, s->fbdev->base);
145 stl_phys(&s->dma_as, value + 16, s->fbdev->size);
148 case 0x00048001: /* Release buffer */
151 case 0x00040002: /* Blank screen */
154 case 0x00040003: /* Get display width/height */
156 stl_phys(&s->dma_as, value + 12, s->fbdev->xres);
157 stl_phys(&s->dma_as, value + 16, s->fbdev->yres);
160 case 0x00044003: /* Test display width/height */
164 case 0x00048003: /* Set display width/height */
166 xres = ldl_phys(&s->dma_as, value + 12);
168 yres = ldl_phys(&s->dma_as, value + 16);
172 case 0x00040005: /* Get depth */
173 stl_phys(&s->dma_as, value + 12, s->fbdev->bpp);
176 case 0x00044005: /* Test depth */
179 case 0x00048005: /* Set depth */
180 bpp = ldl_phys(&s->dma_as, value + 12);
184 case 0x00040006: /* Get pixel order */
185 stl_phys(&s->dma_as, value + 12, s->fbdev->pixo);
188 case 0x00044006: /* Test pixel order */
191 case 0x00048006: /* Set pixel order */
192 pixo = ldl_phys(&s->dma_as, value + 12);
196 case 0x00040007: /* Get alpha */
197 stl_phys(&s->dma_as, value + 12, s->fbdev->alpha);
200 case 0x00044007: /* Test pixel alpha */
203 case 0x00048007: /* Set alpha */
204 alpha = ldl_phys(&s->dma_as, value + 12);
208 case 0x00040008: /* Get pitch */
209 stl_phys(&s->dma_as, value + 12, s->fbdev->pitch);
212 case 0x00040009: /* Get virtual offset */
213 stl_phys(&s->dma_as, value + 12, s->fbdev->xoffset);
214 stl_phys(&s->dma_as, value + 16, s->fbdev->yoffset);
217 case 0x00044009: /* Test virtual offset */
220 case 0x00048009: /* Set virtual offset */
221 xoffset = ldl_phys(&s->dma_as, value + 12);
222 newxoffset = &xoffset;
223 yoffset = ldl_phys(&s->dma_as, value + 16);
224 newyoffset = &yoffset;
226 stl_phys(&s->dma_as, value + 12, bcm2835_fb.xres);
227 stl_phys(&s->dma_as, value + 16, bcm2835_fb.yres);
231 case 0x0004000a: /* Get/Test/Set overscan */
234 stl_phys(&s->dma_as, value + 12, 0);
235 stl_phys(&s->dma_as, value + 16, 0);
236 stl_phys(&s->dma_as, value + 20, 0);
237 stl_phys(&s->dma_as, value + 24, 0);
241 case 0x0004800b: /* Set palette */
242 offset = ldl_phys(&s->dma_as, value + 12);
243 length = ldl_phys(&s->dma_as, value + 16);
245 while (n < length - offset) {
246 color = ldl_phys(&s->dma_as, value + 20 + (n << 2));
248 s->fbdev->vcram_base + ((offset + n) << 2), color);
251 stl_phys(&s->dma_as, value + 12, 0);
256 >>>>>>> upstreaming-raspi
257 case 0x00060001: /* Get DMA channels */
259 stl_phys(&s->dma_as, value + 12, 0x003C);
263 case 0x00050001: /* Get command line */
268 qemu_log_mask(LOG_GUEST_ERROR,
269 "bcm2835_property: unhandled tag %08x\n", tag);
277 stl_phys(&s->dma_as, value + 8, (1 << 31) | resplen);
278 value += bufsize + 12;
282 if (newxres || newyres || newxoffset || newyoffset || newbpp || newpixo
284 bcm2835_fb_reconfigure(s->fbdev, newxres, newyres, newxoffset,
285 newyoffset, newbpp, newpixo, newalpha);
289 >>>>>>> upstreaming-raspi
290 /* Buffer response code */
291 stl_phys(&s->dma_as, s->addr + 4, (1 << 31));
294 static uint64_t bcm2835_property_read(void *opaque, hwaddr offset,
297 BCM2835PropertyState *s = opaque;
302 res = MBOX_CHAN_PROPERTY | s->addr;
304 qemu_set_irq(s->mbox_irq, 0);
307 case MBOX_AS_PENDING:
312 qemu_log_mask(LOG_GUEST_ERROR, "%s: Bad offset %"HWADDR_PRIx"\n",
320 static void bcm2835_property_write(void *opaque, hwaddr offset,
321 uint64_t value, unsigned size)
323 BCM2835PropertyState *s = opaque;
329 bcm2835_property_mbox_push(s, value);
330 qemu_set_irq(s->mbox_irq, 1);
335 qemu_log_mask(LOG_GUEST_ERROR, "%s: Bad offset %"HWADDR_PRIx"\n",
342 static const MemoryRegionOps bcm2835_property_ops = {
343 .read = bcm2835_property_read,
344 .write = bcm2835_property_write,
345 .endianness = DEVICE_NATIVE_ENDIAN,
346 .valid.min_access_size = 4,
347 .valid.max_access_size = 4,
350 static const VMStateDescription vmstate_bcm2835_property = {
351 .name = TYPE_BCM2835_PROPERTY,
353 .minimum_version_id = 1,
354 .minimum_version_id_old = 1,
355 .fields = (VMStateField[]) {
356 VMSTATE_UINT32(addr, BCM2835PropertyState),
357 VMSTATE_BOOL(pending, BCM2835PropertyState),
358 VMSTATE_END_OF_LIST()
362 static void bcm2835_property_init(Object *obj)
364 BCM2835PropertyState *s = BCM2835_PROPERTY(obj);
365 memory_region_init_io(&s->iomem, OBJECT(s), &bcm2835_property_ops, s,
366 TYPE_BCM2835_PROPERTY, 0x10);
367 sysbus_init_mmio(SYS_BUS_DEVICE(s), &s->iomem);
368 sysbus_init_irq(SYS_BUS_DEVICE(s), &s->mbox_irq);
371 static void bcm2835_property_reset(DeviceState *dev)
373 BCM2835PropertyState *s = BCM2835_PROPERTY(dev);
378 static void bcm2835_property_realize(DeviceState *dev, Error **errp)
380 BCM2835PropertyState *s = BCM2835_PROPERTY(dev);
385 obj = object_property_get_link(OBJECT(dev), "bcm2835_fb", &err);
387 error_setg(errp, "%s: required bcm2835_fb link not found: %s",
388 __func__, error_get_pretty(err));
392 s->fbdev = BCM2835_FB(obj);
394 obj = object_property_get_link(OBJECT(dev), "dma_mr", &err);
396 error_setg(errp, "%s: required dma_mr link not found: %s",
398 obj = object_property_get_link(OBJECT(dev), "dma-mr", &err);
400 error_setg(errp, "%s: required dma-mr link not found: %s",
401 >>>>>>> upstreaming-raspi
402 __func__, error_get_pretty(err));
406 s->dma_mr = MEMORY_REGION(obj);
407 address_space_init(&s->dma_as, s->dma_mr, NULL);
409 bcm2835_property_reset(dev);
414 static Property bcm2835_property_props[] = {
415 DEFINE_PROP_UINT32("ram-size", BCM2835PropertyState, ram_size, 0),
416 DEFINE_PROP_END_OF_LIST()
419 >>>>>>> upstreaming-raspi
420 static void bcm2835_property_class_init(ObjectClass *klass, void *data)
422 DeviceClass *dc = DEVICE_CLASS(klass);
426 dc->props = bcm2835_property_props;
427 >>>>>>> upstreaming-raspi
428 dc->realize = bcm2835_property_realize;
429 dc->vmsd = &vmstate_bcm2835_property;
432 static TypeInfo bcm2835_property_info = {
433 .name = TYPE_BCM2835_PROPERTY,
434 .parent = TYPE_SYS_BUS_DEVICE,
435 .instance_size = sizeof(BCM2835PropertyState),
436 .class_init = bcm2835_property_class_init,
437 .instance_init = bcm2835_property_init,
440 static void bcm2835_property_register_types(void)
442 type_register_static(&bcm2835_property_info);
445 type_init(bcm2835_property_register_types)