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/qdev-properties.h"
10 #include "migration/vmstate.h"
12 #include "hw/misc/bcm2835_mbox_defs.h"
13 #include "sysemu/dma.h"
15 #include "qemu/module.h"
18 /* https://github.com/raspberrypi/firmware/wiki/Mailbox-property-interface */
20 static void bcm2835_property_mbox_push(BCM2835PropertyState *s, uint32_t value)
28 uint32_t offset, length, color;
31 * Copy the current state of the framebuffer config; we will update
32 * this copy as we process tags and then ask the framebuffer to use
35 BCM2835FBConfig fbconfig = s->fbdev->config;
36 bool fbconfig_updated = false;
42 tot_len = ldl_le_phys(&s->dma_as, value);
44 /* @(addr + 4) : Buffer response code */
46 while (value + 8 <= s->addr + tot_len) {
47 tag = ldl_le_phys(&s->dma_as, value);
48 bufsize = ldl_le_phys(&s->dma_as, value + 4);
49 /* @(value + 8) : Request/response indicator */
52 case 0x00000000: /* End tag */
54 case 0x00000001: /* Get firmware revision */
55 stl_le_phys(&s->dma_as, value + 12, 346337);
58 case 0x00010001: /* Get board model */
59 qemu_log_mask(LOG_UNIMP,
60 "bcm2835_property: 0x%08x get board model NYI\n",
64 case 0x00010002: /* Get board revision */
65 stl_le_phys(&s->dma_as, value + 12, s->board_rev);
68 case 0x00010003: /* Get board MAC address */
69 resplen = sizeof(s->macaddr.a);
70 dma_memory_write(&s->dma_as, value + 12, s->macaddr.a, resplen);
72 case 0x00010004: /* Get board serial */
73 qemu_log_mask(LOG_UNIMP,
74 "bcm2835_property: 0x%08x get board serial NYI\n",
78 case 0x00010005: /* Get ARM memory */
80 stl_le_phys(&s->dma_as, value + 12, 0);
82 stl_le_phys(&s->dma_as, value + 16, s->fbdev->vcram_base);
85 case 0x00010006: /* Get VC memory */
87 stl_le_phys(&s->dma_as, value + 12, s->fbdev->vcram_base);
89 stl_le_phys(&s->dma_as, value + 16, s->fbdev->vcram_size);
92 case 0x00028001: /* Set power state */
93 /* Assume that whatever device they asked for exists,
94 * and we'll just claim we set it to the desired state
96 tmp = ldl_le_phys(&s->dma_as, value + 16);
97 stl_le_phys(&s->dma_as, value + 16, (tmp & 1));
103 case 0x00030001: /* Get clock state */
104 stl_le_phys(&s->dma_as, value + 16, 0x1);
108 case 0x00038001: /* Set clock state */
109 qemu_log_mask(LOG_UNIMP,
110 "bcm2835_property: 0x%08x set clock state NYI\n",
115 case 0x00030002: /* Get clock rate */
116 case 0x00030004: /* Get max clock rate */
117 case 0x00030007: /* Get min clock rate */
118 switch (ldl_le_phys(&s->dma_as, value + 12)) {
120 stl_le_phys(&s->dma_as, value + 16, 50000000);
123 stl_le_phys(&s->dma_as, value + 16, 3000000);
126 stl_le_phys(&s->dma_as, value + 16, 700000000);
132 case 0x00038002: /* Set clock rate */
133 case 0x00038004: /* Set max clock rate */
134 case 0x00038007: /* Set min clock rate */
135 qemu_log_mask(LOG_UNIMP,
136 "bcm2835_property: 0x%08x set clock rate NYI\n",
143 case 0x00030006: /* Get temperature */
144 stl_le_phys(&s->dma_as, value + 16, 25000);
148 case 0x0003000A: /* Get max temperature */
149 stl_le_phys(&s->dma_as, value + 16, 99000);
155 case 0x00040001: /* Allocate buffer */
156 stl_le_phys(&s->dma_as, value + 12, fbconfig.base);
157 stl_le_phys(&s->dma_as, value + 16,
158 bcm2835_fb_get_size(&fbconfig));
161 case 0x00048001: /* Release buffer */
164 case 0x00040002: /* Blank screen */
167 case 0x00044003: /* Test physical display width/height */
168 case 0x00044004: /* Test virtual display width/height */
171 case 0x00048003: /* Set physical display width/height */
172 fbconfig.xres = ldl_le_phys(&s->dma_as, value + 12);
173 fbconfig.yres = ldl_le_phys(&s->dma_as, value + 16);
174 bcm2835_fb_validate_config(&fbconfig);
175 fbconfig_updated = true;
177 case 0x00040003: /* Get physical display width/height */
178 stl_le_phys(&s->dma_as, value + 12, fbconfig.xres);
179 stl_le_phys(&s->dma_as, value + 16, fbconfig.yres);
182 case 0x00048004: /* Set virtual display width/height */
183 fbconfig.xres_virtual = ldl_le_phys(&s->dma_as, value + 12);
184 fbconfig.yres_virtual = ldl_le_phys(&s->dma_as, value + 16);
185 bcm2835_fb_validate_config(&fbconfig);
186 fbconfig_updated = true;
188 case 0x00040004: /* Get virtual display width/height */
189 stl_le_phys(&s->dma_as, value + 12, fbconfig.xres_virtual);
190 stl_le_phys(&s->dma_as, value + 16, fbconfig.yres_virtual);
193 case 0x00044005: /* Test depth */
196 case 0x00048005: /* Set depth */
197 fbconfig.bpp = ldl_le_phys(&s->dma_as, value + 12);
198 bcm2835_fb_validate_config(&fbconfig);
199 fbconfig_updated = true;
201 case 0x00040005: /* Get depth */
202 stl_le_phys(&s->dma_as, value + 12, fbconfig.bpp);
205 case 0x00044006: /* Test pixel order */
208 case 0x00048006: /* Set pixel order */
209 fbconfig.pixo = ldl_le_phys(&s->dma_as, value + 12);
210 bcm2835_fb_validate_config(&fbconfig);
211 fbconfig_updated = true;
213 case 0x00040006: /* Get pixel order */
214 stl_le_phys(&s->dma_as, value + 12, fbconfig.pixo);
217 case 0x00044007: /* Test pixel alpha */
220 case 0x00048007: /* Set alpha */
221 fbconfig.alpha = ldl_le_phys(&s->dma_as, value + 12);
222 bcm2835_fb_validate_config(&fbconfig);
223 fbconfig_updated = true;
225 case 0x00040007: /* Get alpha */
226 stl_le_phys(&s->dma_as, value + 12, fbconfig.alpha);
229 case 0x00040008: /* Get pitch */
230 stl_le_phys(&s->dma_as, value + 12,
231 bcm2835_fb_get_pitch(&fbconfig));
234 case 0x00044009: /* Test virtual offset */
237 case 0x00048009: /* Set virtual offset */
238 fbconfig.xoffset = ldl_le_phys(&s->dma_as, value + 12);
239 fbconfig.yoffset = ldl_le_phys(&s->dma_as, value + 16);
240 bcm2835_fb_validate_config(&fbconfig);
241 fbconfig_updated = true;
243 case 0x00040009: /* Get virtual offset */
244 stl_le_phys(&s->dma_as, value + 12, fbconfig.xoffset);
245 stl_le_phys(&s->dma_as, value + 16, fbconfig.yoffset);
248 case 0x0004000a: /* Get/Test/Set overscan */
251 stl_le_phys(&s->dma_as, value + 12, 0);
252 stl_le_phys(&s->dma_as, value + 16, 0);
253 stl_le_phys(&s->dma_as, value + 20, 0);
254 stl_le_phys(&s->dma_as, value + 24, 0);
257 case 0x0004800b: /* Set palette */
258 offset = ldl_le_phys(&s->dma_as, value + 12);
259 length = ldl_le_phys(&s->dma_as, value + 16);
261 while (n < length - offset) {
262 color = ldl_le_phys(&s->dma_as, value + 20 + (n << 2));
263 stl_le_phys(&s->dma_as,
264 s->fbdev->vcram_base + ((offset + n) << 2), color);
267 stl_le_phys(&s->dma_as, value + 12, 0);
271 case 0x00060001: /* Get DMA channels */
273 stl_le_phys(&s->dma_as, value + 12, 0x003C);
277 case 0x00050001: /* Get command line */
282 qemu_log_mask(LOG_UNIMP,
283 "bcm2835_property: unhandled tag 0x%08x\n", tag);
287 trace_bcm2835_mbox_property(tag, bufsize, resplen);
292 stl_le_phys(&s->dma_as, value + 8, (1 << 31) | resplen);
293 value += bufsize + 12;
296 /* Reconfigure framebuffer if required */
297 if (fbconfig_updated) {
298 bcm2835_fb_reconfigure(s->fbdev, &fbconfig);
301 /* Buffer response code */
302 stl_le_phys(&s->dma_as, s->addr + 4, (1 << 31));
305 static uint64_t bcm2835_property_read(void *opaque, hwaddr offset,
308 BCM2835PropertyState *s = opaque;
313 res = MBOX_CHAN_PROPERTY | s->addr;
315 qemu_set_irq(s->mbox_irq, 0);
318 case MBOX_AS_PENDING:
323 qemu_log_mask(LOG_GUEST_ERROR, "%s: Bad offset %"HWADDR_PRIx"\n",
331 static void bcm2835_property_write(void *opaque, hwaddr offset,
332 uint64_t value, unsigned size)
334 BCM2835PropertyState *s = opaque;
338 /* bcm2835_mbox should check our pending status before pushing */
341 bcm2835_property_mbox_push(s, value);
342 qemu_set_irq(s->mbox_irq, 1);
346 qemu_log_mask(LOG_GUEST_ERROR, "%s: Bad offset %"HWADDR_PRIx"\n",
352 static const MemoryRegionOps bcm2835_property_ops = {
353 .read = bcm2835_property_read,
354 .write = bcm2835_property_write,
355 .endianness = DEVICE_NATIVE_ENDIAN,
356 .valid.min_access_size = 4,
357 .valid.max_access_size = 4,
360 static const VMStateDescription vmstate_bcm2835_property = {
361 .name = TYPE_BCM2835_PROPERTY,
363 .minimum_version_id = 1,
364 .fields = (VMStateField[]) {
365 VMSTATE_MACADDR(macaddr, BCM2835PropertyState),
366 VMSTATE_UINT32(addr, BCM2835PropertyState),
367 VMSTATE_BOOL(pending, BCM2835PropertyState),
368 VMSTATE_END_OF_LIST()
372 static void bcm2835_property_init(Object *obj)
374 BCM2835PropertyState *s = BCM2835_PROPERTY(obj);
376 memory_region_init_io(&s->iomem, OBJECT(s), &bcm2835_property_ops, s,
377 TYPE_BCM2835_PROPERTY, 0x10);
378 sysbus_init_mmio(SYS_BUS_DEVICE(s), &s->iomem);
379 sysbus_init_irq(SYS_BUS_DEVICE(s), &s->mbox_irq);
382 static void bcm2835_property_reset(DeviceState *dev)
384 BCM2835PropertyState *s = BCM2835_PROPERTY(dev);
389 static void bcm2835_property_realize(DeviceState *dev, Error **errp)
391 BCM2835PropertyState *s = BCM2835_PROPERTY(dev);
395 obj = object_property_get_link(OBJECT(dev), "fb", &err);
397 error_setg(errp, "%s: required fb link not found: %s",
398 __func__, error_get_pretty(err));
402 s->fbdev = BCM2835_FB(obj);
404 obj = object_property_get_link(OBJECT(dev), "dma-mr", &err);
406 error_setg(errp, "%s: required dma-mr link not found: %s",
407 __func__, error_get_pretty(err));
411 s->dma_mr = MEMORY_REGION(obj);
412 address_space_init(&s->dma_as, s->dma_mr, TYPE_BCM2835_PROPERTY "-memory");
414 /* TODO: connect to MAC address of USB NIC device, once we emulate it */
415 qemu_macaddr_default_if_unset(&s->macaddr);
417 bcm2835_property_reset(dev);
420 static Property bcm2835_property_props[] = {
421 DEFINE_PROP_UINT32("board-rev", BCM2835PropertyState, board_rev, 0),
422 DEFINE_PROP_END_OF_LIST()
425 static void bcm2835_property_class_init(ObjectClass *klass, void *data)
427 DeviceClass *dc = DEVICE_CLASS(klass);
429 dc->props = bcm2835_property_props;
430 dc->realize = bcm2835_property_realize;
431 dc->vmsd = &vmstate_bcm2835_property;
434 static TypeInfo bcm2835_property_info = {
435 .name = TYPE_BCM2835_PROPERTY,
436 .parent = TYPE_SYS_BUS_DEVICE,
437 .instance_size = sizeof(BCM2835PropertyState),
438 .class_init = bcm2835_property_class_init,
439 .instance_init = bcm2835_property_init,
442 static void bcm2835_property_register_types(void)
444 type_register_static(&bcm2835_property_info);
447 type_init(bcm2835_property_register_types)