2 * Raspberry Pi emulation (c) 2012 Gregory Estrade
3 * This code is licensed under the GNU GPLv2 and later.
6 #include "qemu/osdep.h"
7 #include "qapi/error.h"
8 #include "hw/misc/bcm2835_property.h"
9 #include "hw/misc/bcm2835_mbox_defs.h"
10 #include "sysemu/dma.h"
13 /* https://github.com/raspberrypi/firmware/wiki/Mailbox-property-interface */
15 static void bcm2835_property_mbox_push(BCM2835PropertyState *s, uint32_t value)
23 uint32_t offset, length, color;
26 * Copy the current state of the framebuffer config; we will update
27 * this copy as we process tags and then ask the framebuffer to use
30 BCM2835FBConfig fbconfig = s->fbdev->config;
31 bool fbconfig_updated = false;
37 tot_len = ldl_le_phys(&s->dma_as, value);
39 /* @(addr + 4) : Buffer response code */
41 while (value + 8 <= s->addr + tot_len) {
42 tag = ldl_le_phys(&s->dma_as, value);
43 bufsize = ldl_le_phys(&s->dma_as, value + 4);
44 /* @(value + 8) : Request/response indicator */
47 case 0x00000000: /* End tag */
49 case 0x00000001: /* Get firmware revision */
50 stl_le_phys(&s->dma_as, value + 12, 346337);
53 case 0x00010001: /* Get board model */
54 qemu_log_mask(LOG_UNIMP,
55 "bcm2835_property: %x get board model NYI\n", tag);
58 case 0x00010002: /* Get board revision */
59 stl_le_phys(&s->dma_as, value + 12, s->board_rev);
62 case 0x00010003: /* Get board MAC address */
63 resplen = sizeof(s->macaddr.a);
64 dma_memory_write(&s->dma_as, value + 12, s->macaddr.a, resplen);
66 case 0x00010004: /* Get board serial */
67 qemu_log_mask(LOG_UNIMP,
68 "bcm2835_property: %x get board serial NYI\n", tag);
71 case 0x00010005: /* Get ARM memory */
73 stl_le_phys(&s->dma_as, value + 12, 0);
75 stl_le_phys(&s->dma_as, value + 16, s->fbdev->vcram_base);
78 case 0x00010006: /* Get VC memory */
80 stl_le_phys(&s->dma_as, value + 12, s->fbdev->vcram_base);
82 stl_le_phys(&s->dma_as, value + 16, s->fbdev->vcram_size);
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
89 tmp = ldl_le_phys(&s->dma_as, value + 16);
90 stl_le_phys(&s->dma_as, value + 16, (tmp & 1));
96 case 0x00030001: /* Get clock state */
97 stl_le_phys(&s->dma_as, value + 16, 0x1);
101 case 0x00038001: /* Set clock state */
102 qemu_log_mask(LOG_UNIMP,
103 "bcm2835_property: %x set clock state NYI\n", tag);
107 case 0x00030002: /* Get clock rate */
108 case 0x00030004: /* Get max clock rate */
109 case 0x00030007: /* Get min clock rate */
110 switch (ldl_le_phys(&s->dma_as, value + 12)) {
112 stl_le_phys(&s->dma_as, value + 16, 50000000);
115 stl_le_phys(&s->dma_as, value + 16, 3000000);
118 stl_le_phys(&s->dma_as, value + 16, 700000000);
124 case 0x00038002: /* Set clock rate */
125 case 0x00038004: /* Set max clock rate */
126 case 0x00038007: /* Set min clock rate */
127 qemu_log_mask(LOG_UNIMP,
128 "bcm2835_property: %x set clock rates NYI\n", tag);
134 case 0x00030006: /* Get temperature */
135 stl_le_phys(&s->dma_as, value + 16, 25000);
139 case 0x0003000A: /* Get max temperature */
140 stl_le_phys(&s->dma_as, value + 16, 99000);
146 case 0x00040001: /* Allocate buffer */
147 stl_le_phys(&s->dma_as, value + 12, fbconfig.base);
148 stl_le_phys(&s->dma_as, value + 16,
149 fbconfig.xres * fbconfig.yres * fbconfig.bpp / 8);
152 case 0x00048001: /* Release buffer */
155 case 0x00040002: /* Blank screen */
158 case 0x00040003: /* Get display width/height */
160 stl_le_phys(&s->dma_as, value + 12, fbconfig.xres);
161 stl_le_phys(&s->dma_as, value + 16, fbconfig.yres);
164 case 0x00044003: /* Test display width/height */
168 case 0x00048003: /* Set display width/height */
170 fbconfig.xres = ldl_le_phys(&s->dma_as, value + 12);
171 fbconfig.yres = ldl_le_phys(&s->dma_as, value + 16);
172 fbconfig_updated = true;
175 case 0x00040005: /* Get depth */
176 stl_le_phys(&s->dma_as, value + 12, fbconfig.bpp);
179 case 0x00044005: /* Test depth */
182 case 0x00048005: /* Set depth */
183 fbconfig.bpp = ldl_le_phys(&s->dma_as, value + 12);
184 fbconfig_updated = true;
187 case 0x00040006: /* Get pixel order */
188 stl_le_phys(&s->dma_as, value + 12, fbconfig.pixo);
191 case 0x00044006: /* Test pixel order */
194 case 0x00048006: /* Set pixel order */
195 fbconfig.pixo = ldl_le_phys(&s->dma_as, value + 12);
196 fbconfig_updated = true;
199 case 0x00040007: /* Get alpha */
200 stl_le_phys(&s->dma_as, value + 12, fbconfig.alpha);
203 case 0x00044007: /* Test pixel alpha */
206 case 0x00048007: /* Set alpha */
207 fbconfig.alpha = ldl_le_phys(&s->dma_as, value + 12);
208 fbconfig_updated = true;
211 case 0x00040008: /* Get pitch */
212 stl_le_phys(&s->dma_as, value + 12,
213 fbconfig.xres * fbconfig.bpp / 8);
216 case 0x00040009: /* Get virtual offset */
217 stl_le_phys(&s->dma_as, value + 12, fbconfig.xoffset);
218 stl_le_phys(&s->dma_as, value + 16, fbconfig.yoffset);
221 case 0x00044009: /* Test virtual offset */
224 case 0x00048009: /* Set virtual offset */
225 fbconfig.xoffset = ldl_le_phys(&s->dma_as, value + 12);
226 fbconfig.yoffset = ldl_le_phys(&s->dma_as, value + 16);
227 fbconfig_updated = true;
230 case 0x0004000a: /* Get/Test/Set overscan */
233 stl_le_phys(&s->dma_as, value + 12, 0);
234 stl_le_phys(&s->dma_as, value + 16, 0);
235 stl_le_phys(&s->dma_as, value + 20, 0);
236 stl_le_phys(&s->dma_as, value + 24, 0);
239 case 0x0004800b: /* Set palette */
240 offset = ldl_le_phys(&s->dma_as, value + 12);
241 length = ldl_le_phys(&s->dma_as, value + 16);
243 while (n < length - offset) {
244 color = ldl_le_phys(&s->dma_as, value + 20 + (n << 2));
245 stl_le_phys(&s->dma_as,
246 s->fbdev->vcram_base + ((offset + n) << 2), color);
249 stl_le_phys(&s->dma_as, value + 12, 0);
253 case 0x00060001: /* Get DMA channels */
255 stl_le_phys(&s->dma_as, value + 12, 0x003C);
259 case 0x00050001: /* Get command line */
264 qemu_log_mask(LOG_GUEST_ERROR,
265 "bcm2835_property: unhandled tag %08x\n", tag);
273 stl_le_phys(&s->dma_as, value + 8, (1 << 31) | resplen);
274 value += bufsize + 12;
277 /* Reconfigure framebuffer if required */
278 if (fbconfig_updated) {
279 bcm2835_fb_reconfigure(s->fbdev, &fbconfig);
282 /* Buffer response code */
283 stl_le_phys(&s->dma_as, s->addr + 4, (1 << 31));
286 static uint64_t bcm2835_property_read(void *opaque, hwaddr offset,
289 BCM2835PropertyState *s = opaque;
294 res = MBOX_CHAN_PROPERTY | s->addr;
296 qemu_set_irq(s->mbox_irq, 0);
299 case MBOX_AS_PENDING:
304 qemu_log_mask(LOG_GUEST_ERROR, "%s: Bad offset %"HWADDR_PRIx"\n",
312 static void bcm2835_property_write(void *opaque, hwaddr offset,
313 uint64_t value, unsigned size)
315 BCM2835PropertyState *s = opaque;
319 /* bcm2835_mbox should check our pending status before pushing */
322 bcm2835_property_mbox_push(s, value);
323 qemu_set_irq(s->mbox_irq, 1);
327 qemu_log_mask(LOG_GUEST_ERROR, "%s: Bad offset %"HWADDR_PRIx"\n",
333 static const MemoryRegionOps bcm2835_property_ops = {
334 .read = bcm2835_property_read,
335 .write = bcm2835_property_write,
336 .endianness = DEVICE_NATIVE_ENDIAN,
337 .valid.min_access_size = 4,
338 .valid.max_access_size = 4,
341 static const VMStateDescription vmstate_bcm2835_property = {
342 .name = TYPE_BCM2835_PROPERTY,
344 .minimum_version_id = 1,
345 .fields = (VMStateField[]) {
346 VMSTATE_MACADDR(macaddr, BCM2835PropertyState),
347 VMSTATE_UINT32(addr, BCM2835PropertyState),
348 VMSTATE_BOOL(pending, BCM2835PropertyState),
349 VMSTATE_END_OF_LIST()
353 static void bcm2835_property_init(Object *obj)
355 BCM2835PropertyState *s = BCM2835_PROPERTY(obj);
357 memory_region_init_io(&s->iomem, OBJECT(s), &bcm2835_property_ops, s,
358 TYPE_BCM2835_PROPERTY, 0x10);
359 sysbus_init_mmio(SYS_BUS_DEVICE(s), &s->iomem);
360 sysbus_init_irq(SYS_BUS_DEVICE(s), &s->mbox_irq);
363 static void bcm2835_property_reset(DeviceState *dev)
365 BCM2835PropertyState *s = BCM2835_PROPERTY(dev);
370 static void bcm2835_property_realize(DeviceState *dev, Error **errp)
372 BCM2835PropertyState *s = BCM2835_PROPERTY(dev);
376 obj = object_property_get_link(OBJECT(dev), "fb", &err);
378 error_setg(errp, "%s: required fb link not found: %s",
379 __func__, error_get_pretty(err));
383 s->fbdev = BCM2835_FB(obj);
385 obj = object_property_get_link(OBJECT(dev), "dma-mr", &err);
387 error_setg(errp, "%s: required dma-mr link not found: %s",
388 __func__, error_get_pretty(err));
392 s->dma_mr = MEMORY_REGION(obj);
393 address_space_init(&s->dma_as, s->dma_mr, NULL);
395 /* TODO: connect to MAC address of USB NIC device, once we emulate it */
396 qemu_macaddr_default_if_unset(&s->macaddr);
398 bcm2835_property_reset(dev);
401 static Property bcm2835_property_props[] = {
402 DEFINE_PROP_UINT32("board-rev", BCM2835PropertyState, board_rev, 0),
403 DEFINE_PROP_END_OF_LIST()
406 static void bcm2835_property_class_init(ObjectClass *klass, void *data)
408 DeviceClass *dc = DEVICE_CLASS(klass);
410 dc->props = bcm2835_property_props;
411 dc->realize = bcm2835_property_realize;
412 dc->vmsd = &vmstate_bcm2835_property;
415 static TypeInfo bcm2835_property_info = {
416 .name = TYPE_BCM2835_PROPERTY,
417 .parent = TYPE_SYS_BUS_DEVICE,
418 .instance_size = sizeof(BCM2835PropertyState),
419 .class_init = bcm2835_property_class_init,
420 .instance_init = bcm2835_property_init,
423 static void bcm2835_property_register_types(void)
425 type_register_static(&bcm2835_property_info);
428 type_init(bcm2835_property_register_types)