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;
24 uint32_t xres, yres, xoffset, yoffset, bpp, pixo, alpha;
25 uint32_t tmp_xres, tmp_yres, tmp_xoffset, tmp_yoffset;
26 uint32_t tmp_bpp, tmp_pixo, tmp_alpha;
27 uint32_t *newxres = NULL, *newyres = NULL, *newxoffset = NULL,
28 *newyoffset = NULL, *newbpp = NULL, *newpixo = NULL, *newalpha = NULL;
34 tot_len = ldl_le_phys(&s->dma_as, value);
36 /* @(addr + 4) : Buffer response code */
38 while (value + 8 <= s->addr + tot_len) {
39 tag = ldl_le_phys(&s->dma_as, value);
40 bufsize = ldl_le_phys(&s->dma_as, value + 4);
41 /* @(value + 8) : Request/response indicator */
44 case 0x00000000: /* End tag */
46 case 0x00000001: /* Get firmware revision */
47 stl_le_phys(&s->dma_as, value + 12, 346337);
50 case 0x00010001: /* Get board model */
51 qemu_log_mask(LOG_UNIMP,
52 "bcm2835_property: %x get board model NYI\n", tag);
55 case 0x00010002: /* Get board revision */
56 stl_le_phys(&s->dma_as, value + 12, s->board_rev);
59 case 0x00010003: /* Get board MAC address */
60 resplen = sizeof(s->macaddr.a);
61 dma_memory_write(&s->dma_as, value + 12, s->macaddr.a, resplen);
63 case 0x00010004: /* Get board serial */
64 qemu_log_mask(LOG_UNIMP,
65 "bcm2835_property: %x get board serial NYI\n", tag);
68 case 0x00010005: /* Get ARM memory */
70 stl_le_phys(&s->dma_as, value + 12, 0);
72 stl_le_phys(&s->dma_as, value + 16, s->fbdev->vcram_base);
75 case 0x00010006: /* Get VC memory */
77 stl_le_phys(&s->dma_as, value + 12, s->fbdev->vcram_base);
79 stl_le_phys(&s->dma_as, value + 16, s->fbdev->vcram_size);
82 case 0x00028001: /* Set power state */
83 /* Assume that whatever device they asked for exists,
84 * and we'll just claim we set it to the desired state
86 tmp = ldl_le_phys(&s->dma_as, value + 16);
87 stl_le_phys(&s->dma_as, value + 16, (tmp & 1));
93 case 0x00030001: /* Get clock state */
94 stl_le_phys(&s->dma_as, value + 16, 0x1);
98 case 0x00038001: /* Set clock state */
99 qemu_log_mask(LOG_UNIMP,
100 "bcm2835_property: %x set clock state NYI\n", tag);
104 case 0x00030002: /* Get clock rate */
105 case 0x00030004: /* Get max clock rate */
106 case 0x00030007: /* Get min clock rate */
107 switch (ldl_le_phys(&s->dma_as, value + 12)) {
109 stl_le_phys(&s->dma_as, value + 16, 50000000);
112 stl_le_phys(&s->dma_as, value + 16, 3000000);
115 stl_le_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 */
124 qemu_log_mask(LOG_UNIMP,
125 "bcm2835_property: %x set clock rates NYI\n", tag);
131 case 0x00030006: /* Get temperature */
132 stl_le_phys(&s->dma_as, value + 16, 25000);
136 case 0x0003000A: /* Get max temperature */
137 stl_le_phys(&s->dma_as, value + 16, 99000);
143 case 0x00040001: /* Allocate buffer */
144 stl_le_phys(&s->dma_as, value + 12, s->fbdev->config.base);
145 tmp_xres = newxres != NULL ? *newxres : s->fbdev->config.xres;
146 tmp_yres = newyres != NULL ? *newyres : s->fbdev->config.yres;
147 tmp_bpp = newbpp != NULL ? *newbpp : s->fbdev->config.bpp;
148 stl_le_phys(&s->dma_as, value + 16,
149 tmp_xres * tmp_yres * tmp_bpp / 8);
152 case 0x00048001: /* Release buffer */
155 case 0x00040002: /* Blank screen */
158 case 0x00040003: /* Get display width/height */
160 tmp_xres = newxres != NULL ? *newxres : s->fbdev->config.xres;
161 tmp_yres = newyres != NULL ? *newyres : s->fbdev->config.yres;
162 stl_le_phys(&s->dma_as, value + 12, tmp_xres);
163 stl_le_phys(&s->dma_as, value + 16, tmp_yres);
166 case 0x00044003: /* Test display width/height */
170 case 0x00048003: /* Set display width/height */
172 xres = ldl_le_phys(&s->dma_as, value + 12);
174 yres = ldl_le_phys(&s->dma_as, value + 16);
178 case 0x00040005: /* Get depth */
179 tmp_bpp = newbpp != NULL ? *newbpp : s->fbdev->config.bpp;
180 stl_le_phys(&s->dma_as, value + 12, tmp_bpp);
183 case 0x00044005: /* Test depth */
186 case 0x00048005: /* Set depth */
187 bpp = ldl_le_phys(&s->dma_as, value + 12);
191 case 0x00040006: /* Get pixel order */
192 tmp_pixo = newpixo != NULL ? *newpixo : s->fbdev->config.pixo;
193 stl_le_phys(&s->dma_as, value + 12, tmp_pixo);
196 case 0x00044006: /* Test pixel order */
199 case 0x00048006: /* Set pixel order */
200 pixo = ldl_le_phys(&s->dma_as, value + 12);
204 case 0x00040007: /* Get alpha */
205 tmp_alpha = newalpha != NULL ? *newalpha : s->fbdev->config.alpha;
206 stl_le_phys(&s->dma_as, value + 12, tmp_alpha);
209 case 0x00044007: /* Test pixel alpha */
212 case 0x00048007: /* Set alpha */
213 alpha = ldl_le_phys(&s->dma_as, value + 12);
217 case 0x00040008: /* Get pitch */
218 tmp_xres = newxres != NULL ? *newxres : s->fbdev->config.xres;
219 tmp_bpp = newbpp != NULL ? *newbpp : s->fbdev->config.bpp;
220 stl_le_phys(&s->dma_as, value + 12, tmp_xres * tmp_bpp / 8);
223 case 0x00040009: /* Get virtual offset */
224 tmp_xoffset = newxoffset != NULL ?
225 *newxoffset : s->fbdev->config.xoffset;
226 tmp_yoffset = newyoffset != NULL ?
227 *newyoffset : s->fbdev->config.yoffset;
228 stl_le_phys(&s->dma_as, value + 12, tmp_xoffset);
229 stl_le_phys(&s->dma_as, value + 16, tmp_yoffset);
232 case 0x00044009: /* Test virtual offset */
235 case 0x00048009: /* Set virtual offset */
236 xoffset = ldl_le_phys(&s->dma_as, value + 12);
237 newxoffset = &xoffset;
238 yoffset = ldl_le_phys(&s->dma_as, value + 16);
239 newyoffset = &yoffset;
242 case 0x0004000a: /* Get/Test/Set overscan */
245 stl_le_phys(&s->dma_as, value + 12, 0);
246 stl_le_phys(&s->dma_as, value + 16, 0);
247 stl_le_phys(&s->dma_as, value + 20, 0);
248 stl_le_phys(&s->dma_as, value + 24, 0);
251 case 0x0004800b: /* Set palette */
252 offset = ldl_le_phys(&s->dma_as, value + 12);
253 length = ldl_le_phys(&s->dma_as, value + 16);
255 while (n < length - offset) {
256 color = ldl_le_phys(&s->dma_as, value + 20 + (n << 2));
257 stl_le_phys(&s->dma_as,
258 s->fbdev->vcram_base + ((offset + n) << 2), color);
261 stl_le_phys(&s->dma_as, value + 12, 0);
265 case 0x00060001: /* Get DMA channels */
267 stl_le_phys(&s->dma_as, value + 12, 0x003C);
271 case 0x00050001: /* Get command line */
276 qemu_log_mask(LOG_GUEST_ERROR,
277 "bcm2835_property: unhandled tag %08x\n", tag);
285 stl_le_phys(&s->dma_as, value + 8, (1 << 31) | resplen);
286 value += bufsize + 12;
289 /* Reconfigure framebuffer if required */
290 if (newxres || newyres || newxoffset || newyoffset || newbpp || newpixo
292 bcm2835_fb_reconfigure(s->fbdev, newxres, newyres, newxoffset,
293 newyoffset, newbpp, newpixo, newalpha);
296 /* Buffer response code */
297 stl_le_phys(&s->dma_as, s->addr + 4, (1 << 31));
300 static uint64_t bcm2835_property_read(void *opaque, hwaddr offset,
303 BCM2835PropertyState *s = opaque;
308 res = MBOX_CHAN_PROPERTY | s->addr;
310 qemu_set_irq(s->mbox_irq, 0);
313 case MBOX_AS_PENDING:
318 qemu_log_mask(LOG_GUEST_ERROR, "%s: Bad offset %"HWADDR_PRIx"\n",
326 static void bcm2835_property_write(void *opaque, hwaddr offset,
327 uint64_t value, unsigned size)
329 BCM2835PropertyState *s = opaque;
333 /* bcm2835_mbox should check our pending status before pushing */
336 bcm2835_property_mbox_push(s, value);
337 qemu_set_irq(s->mbox_irq, 1);
341 qemu_log_mask(LOG_GUEST_ERROR, "%s: Bad offset %"HWADDR_PRIx"\n",
347 static const MemoryRegionOps bcm2835_property_ops = {
348 .read = bcm2835_property_read,
349 .write = bcm2835_property_write,
350 .endianness = DEVICE_NATIVE_ENDIAN,
351 .valid.min_access_size = 4,
352 .valid.max_access_size = 4,
355 static const VMStateDescription vmstate_bcm2835_property = {
356 .name = TYPE_BCM2835_PROPERTY,
358 .minimum_version_id = 1,
359 .fields = (VMStateField[]) {
360 VMSTATE_MACADDR(macaddr, BCM2835PropertyState),
361 VMSTATE_UINT32(addr, BCM2835PropertyState),
362 VMSTATE_BOOL(pending, BCM2835PropertyState),
363 VMSTATE_END_OF_LIST()
367 static void bcm2835_property_init(Object *obj)
369 BCM2835PropertyState *s = BCM2835_PROPERTY(obj);
371 memory_region_init_io(&s->iomem, OBJECT(s), &bcm2835_property_ops, s,
372 TYPE_BCM2835_PROPERTY, 0x10);
373 sysbus_init_mmio(SYS_BUS_DEVICE(s), &s->iomem);
374 sysbus_init_irq(SYS_BUS_DEVICE(s), &s->mbox_irq);
377 static void bcm2835_property_reset(DeviceState *dev)
379 BCM2835PropertyState *s = BCM2835_PROPERTY(dev);
384 static void bcm2835_property_realize(DeviceState *dev, Error **errp)
386 BCM2835PropertyState *s = BCM2835_PROPERTY(dev);
390 obj = object_property_get_link(OBJECT(dev), "fb", &err);
392 error_setg(errp, "%s: required fb link not found: %s",
393 __func__, error_get_pretty(err));
397 s->fbdev = BCM2835_FB(obj);
399 obj = object_property_get_link(OBJECT(dev), "dma-mr", &err);
401 error_setg(errp, "%s: required dma-mr link not found: %s",
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 /* TODO: connect to MAC address of USB NIC device, once we emulate it */
410 qemu_macaddr_default_if_unset(&s->macaddr);
412 bcm2835_property_reset(dev);
415 static Property bcm2835_property_props[] = {
416 DEFINE_PROP_UINT32("board-rev", BCM2835PropertyState, board_rev, 0),
417 DEFINE_PROP_END_OF_LIST()
420 static void bcm2835_property_class_init(ObjectClass *klass, void *data)
422 DeviceClass *dc = DEVICE_CLASS(klass);
424 dc->props = bcm2835_property_props;
425 dc->realize = bcm2835_property_realize;
426 dc->vmsd = &vmstate_bcm2835_property;
429 static TypeInfo bcm2835_property_info = {
430 .name = TYPE_BCM2835_PROPERTY,
431 .parent = TYPE_SYS_BUS_DEVICE,
432 .instance_size = sizeof(BCM2835PropertyState),
433 .class_init = bcm2835_property_class_init,
434 .instance_init = bcm2835_property_init,
437 static void bcm2835_property_register_types(void)
439 type_register_static(&bcm2835_property_info);
442 type_init(bcm2835_property_register_types)