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"
17 /* https://github.com/raspberrypi/firmware/wiki/Mailbox-property-interface */
19 static void bcm2835_property_mbox_push(BCM2835PropertyState
*s
, uint32_t value
)
27 uint32_t offset
, length
, color
;
30 * Copy the current state of the framebuffer config; we will update
31 * this copy as we process tags and then ask the framebuffer to use
34 BCM2835FBConfig fbconfig
= s
->fbdev
->config
;
35 bool fbconfig_updated
= false;
41 tot_len
= ldl_le_phys(&s
->dma_as
, value
);
43 /* @(addr + 4) : Buffer response code */
45 while (value
+ 8 <= s
->addr
+ tot_len
) {
46 tag
= ldl_le_phys(&s
->dma_as
, value
);
47 bufsize
= ldl_le_phys(&s
->dma_as
, value
+ 4);
48 /* @(value + 8) : Request/response indicator */
51 case 0x00000000: /* End tag */
53 case 0x00000001: /* Get firmware revision */
54 stl_le_phys(&s
->dma_as
, value
+ 12, 346337);
57 case 0x00010001: /* Get board model */
58 qemu_log_mask(LOG_UNIMP
,
59 "bcm2835_property: 0x%08x get board model NYI\n",
63 case 0x00010002: /* Get board revision */
64 stl_le_phys(&s
->dma_as
, value
+ 12, s
->board_rev
);
67 case 0x00010003: /* Get board MAC address */
68 resplen
= sizeof(s
->macaddr
.a
);
69 dma_memory_write(&s
->dma_as
, value
+ 12, s
->macaddr
.a
, resplen
);
71 case 0x00010004: /* Get board serial */
72 qemu_log_mask(LOG_UNIMP
,
73 "bcm2835_property: 0x%08x get board serial NYI\n",
77 case 0x00010005: /* Get ARM memory */
79 stl_le_phys(&s
->dma_as
, value
+ 12, 0);
81 stl_le_phys(&s
->dma_as
, value
+ 16, s
->fbdev
->vcram_base
);
84 case 0x00010006: /* Get VC memory */
86 stl_le_phys(&s
->dma_as
, value
+ 12, s
->fbdev
->vcram_base
);
88 stl_le_phys(&s
->dma_as
, value
+ 16, s
->fbdev
->vcram_size
);
91 case 0x00028001: /* Set power state */
92 /* Assume that whatever device they asked for exists,
93 * and we'll just claim we set it to the desired state
95 tmp
= ldl_le_phys(&s
->dma_as
, value
+ 16);
96 stl_le_phys(&s
->dma_as
, value
+ 16, (tmp
& 1));
102 case 0x00030001: /* Get clock state */
103 stl_le_phys(&s
->dma_as
, value
+ 16, 0x1);
107 case 0x00038001: /* Set clock state */
108 qemu_log_mask(LOG_UNIMP
,
109 "bcm2835_property: 0x%08x set clock state NYI\n",
114 case 0x00030002: /* Get clock rate */
115 case 0x00030004: /* Get max clock rate */
116 case 0x00030007: /* Get min clock rate */
117 switch (ldl_le_phys(&s
->dma_as
, value
+ 12)) {
119 stl_le_phys(&s
->dma_as
, value
+ 16, 50000000);
122 stl_le_phys(&s
->dma_as
, value
+ 16, 3000000);
125 stl_le_phys(&s
->dma_as
, value
+ 16, 700000000);
131 case 0x00038002: /* Set clock rate */
132 case 0x00038004: /* Set max clock rate */
133 case 0x00038007: /* Set min clock rate */
134 qemu_log_mask(LOG_UNIMP
,
135 "bcm2835_property: 0x%08x set clock rate NYI\n",
142 case 0x00030006: /* Get temperature */
143 stl_le_phys(&s
->dma_as
, value
+ 16, 25000);
147 case 0x0003000A: /* Get max temperature */
148 stl_le_phys(&s
->dma_as
, value
+ 16, 99000);
154 case 0x00040001: /* Allocate buffer */
155 stl_le_phys(&s
->dma_as
, value
+ 12, fbconfig
.base
);
156 stl_le_phys(&s
->dma_as
, value
+ 16,
157 bcm2835_fb_get_size(&fbconfig
));
160 case 0x00048001: /* Release buffer */
163 case 0x00040002: /* Blank screen */
166 case 0x00044003: /* Test physical display width/height */
167 case 0x00044004: /* Test virtual display width/height */
170 case 0x00048003: /* Set physical display width/height */
171 fbconfig
.xres
= ldl_le_phys(&s
->dma_as
, value
+ 12);
172 fbconfig
.yres
= ldl_le_phys(&s
->dma_as
, value
+ 16);
173 bcm2835_fb_validate_config(&fbconfig
);
174 fbconfig_updated
= true;
176 case 0x00040003: /* Get physical display width/height */
177 stl_le_phys(&s
->dma_as
, value
+ 12, fbconfig
.xres
);
178 stl_le_phys(&s
->dma_as
, value
+ 16, fbconfig
.yres
);
181 case 0x00048004: /* Set virtual display width/height */
182 fbconfig
.xres_virtual
= ldl_le_phys(&s
->dma_as
, value
+ 12);
183 fbconfig
.yres_virtual
= ldl_le_phys(&s
->dma_as
, value
+ 16);
184 bcm2835_fb_validate_config(&fbconfig
);
185 fbconfig_updated
= true;
187 case 0x00040004: /* Get virtual display width/height */
188 stl_le_phys(&s
->dma_as
, value
+ 12, fbconfig
.xres_virtual
);
189 stl_le_phys(&s
->dma_as
, value
+ 16, fbconfig
.yres_virtual
);
192 case 0x00044005: /* Test depth */
195 case 0x00048005: /* Set depth */
196 fbconfig
.bpp
= ldl_le_phys(&s
->dma_as
, value
+ 12);
197 bcm2835_fb_validate_config(&fbconfig
);
198 fbconfig_updated
= true;
200 case 0x00040005: /* Get depth */
201 stl_le_phys(&s
->dma_as
, value
+ 12, fbconfig
.bpp
);
204 case 0x00044006: /* Test pixel order */
207 case 0x00048006: /* Set pixel order */
208 fbconfig
.pixo
= ldl_le_phys(&s
->dma_as
, value
+ 12);
209 bcm2835_fb_validate_config(&fbconfig
);
210 fbconfig_updated
= true;
212 case 0x00040006: /* Get pixel order */
213 stl_le_phys(&s
->dma_as
, value
+ 12, fbconfig
.pixo
);
216 case 0x00044007: /* Test pixel alpha */
219 case 0x00048007: /* Set alpha */
220 fbconfig
.alpha
= ldl_le_phys(&s
->dma_as
, value
+ 12);
221 bcm2835_fb_validate_config(&fbconfig
);
222 fbconfig_updated
= true;
224 case 0x00040007: /* Get alpha */
225 stl_le_phys(&s
->dma_as
, value
+ 12, fbconfig
.alpha
);
228 case 0x00040008: /* Get pitch */
229 stl_le_phys(&s
->dma_as
, value
+ 12,
230 bcm2835_fb_get_pitch(&fbconfig
));
233 case 0x00044009: /* Test virtual offset */
236 case 0x00048009: /* Set virtual offset */
237 fbconfig
.xoffset
= ldl_le_phys(&s
->dma_as
, value
+ 12);
238 fbconfig
.yoffset
= ldl_le_phys(&s
->dma_as
, value
+ 16);
239 bcm2835_fb_validate_config(&fbconfig
);
240 fbconfig_updated
= true;
242 case 0x00040009: /* Get virtual offset */
243 stl_le_phys(&s
->dma_as
, value
+ 12, fbconfig
.xoffset
);
244 stl_le_phys(&s
->dma_as
, value
+ 16, fbconfig
.yoffset
);
247 case 0x0004000a: /* Get/Test/Set overscan */
250 stl_le_phys(&s
->dma_as
, value
+ 12, 0);
251 stl_le_phys(&s
->dma_as
, value
+ 16, 0);
252 stl_le_phys(&s
->dma_as
, value
+ 20, 0);
253 stl_le_phys(&s
->dma_as
, value
+ 24, 0);
256 case 0x0004800b: /* Set palette */
257 offset
= ldl_le_phys(&s
->dma_as
, value
+ 12);
258 length
= ldl_le_phys(&s
->dma_as
, value
+ 16);
260 while (n
< length
- offset
) {
261 color
= ldl_le_phys(&s
->dma_as
, value
+ 20 + (n
<< 2));
262 stl_le_phys(&s
->dma_as
,
263 s
->fbdev
->vcram_base
+ ((offset
+ n
) << 2), color
);
266 stl_le_phys(&s
->dma_as
, value
+ 12, 0);
270 case 0x00060001: /* Get DMA channels */
272 stl_le_phys(&s
->dma_as
, value
+ 12, 0x003C);
276 case 0x00050001: /* Get command line */
281 qemu_log_mask(LOG_UNIMP
,
282 "bcm2835_property: unhandled tag 0x%08x\n", tag
);
290 stl_le_phys(&s
->dma_as
, value
+ 8, (1 << 31) | resplen
);
291 value
+= bufsize
+ 12;
294 /* Reconfigure framebuffer if required */
295 if (fbconfig_updated
) {
296 bcm2835_fb_reconfigure(s
->fbdev
, &fbconfig
);
299 /* Buffer response code */
300 stl_le_phys(&s
->dma_as
, s
->addr
+ 4, (1 << 31));
303 static uint64_t bcm2835_property_read(void *opaque
, hwaddr offset
,
306 BCM2835PropertyState
*s
= opaque
;
311 res
= MBOX_CHAN_PROPERTY
| s
->addr
;
313 qemu_set_irq(s
->mbox_irq
, 0);
316 case MBOX_AS_PENDING
:
321 qemu_log_mask(LOG_GUEST_ERROR
, "%s: Bad offset %"HWADDR_PRIx
"\n",
329 static void bcm2835_property_write(void *opaque
, hwaddr offset
,
330 uint64_t value
, unsigned size
)
332 BCM2835PropertyState
*s
= opaque
;
336 /* bcm2835_mbox should check our pending status before pushing */
339 bcm2835_property_mbox_push(s
, value
);
340 qemu_set_irq(s
->mbox_irq
, 1);
344 qemu_log_mask(LOG_GUEST_ERROR
, "%s: Bad offset %"HWADDR_PRIx
"\n",
350 static const MemoryRegionOps bcm2835_property_ops
= {
351 .read
= bcm2835_property_read
,
352 .write
= bcm2835_property_write
,
353 .endianness
= DEVICE_NATIVE_ENDIAN
,
354 .valid
.min_access_size
= 4,
355 .valid
.max_access_size
= 4,
358 static const VMStateDescription vmstate_bcm2835_property
= {
359 .name
= TYPE_BCM2835_PROPERTY
,
361 .minimum_version_id
= 1,
362 .fields
= (VMStateField
[]) {
363 VMSTATE_MACADDR(macaddr
, BCM2835PropertyState
),
364 VMSTATE_UINT32(addr
, BCM2835PropertyState
),
365 VMSTATE_BOOL(pending
, BCM2835PropertyState
),
366 VMSTATE_END_OF_LIST()
370 static void bcm2835_property_init(Object
*obj
)
372 BCM2835PropertyState
*s
= BCM2835_PROPERTY(obj
);
374 memory_region_init_io(&s
->iomem
, OBJECT(s
), &bcm2835_property_ops
, s
,
375 TYPE_BCM2835_PROPERTY
, 0x10);
376 sysbus_init_mmio(SYS_BUS_DEVICE(s
), &s
->iomem
);
377 sysbus_init_irq(SYS_BUS_DEVICE(s
), &s
->mbox_irq
);
380 static void bcm2835_property_reset(DeviceState
*dev
)
382 BCM2835PropertyState
*s
= BCM2835_PROPERTY(dev
);
387 static void bcm2835_property_realize(DeviceState
*dev
, Error
**errp
)
389 BCM2835PropertyState
*s
= BCM2835_PROPERTY(dev
);
393 obj
= object_property_get_link(OBJECT(dev
), "fb", &err
);
395 error_setg(errp
, "%s: required fb link not found: %s",
396 __func__
, error_get_pretty(err
));
400 s
->fbdev
= BCM2835_FB(obj
);
402 obj
= object_property_get_link(OBJECT(dev
), "dma-mr", &err
);
404 error_setg(errp
, "%s: required dma-mr link not found: %s",
405 __func__
, error_get_pretty(err
));
409 s
->dma_mr
= MEMORY_REGION(obj
);
410 address_space_init(&s
->dma_as
, s
->dma_mr
, TYPE_BCM2835_PROPERTY
"-memory");
412 /* TODO: connect to MAC address of USB NIC device, once we emulate it */
413 qemu_macaddr_default_if_unset(&s
->macaddr
);
415 bcm2835_property_reset(dev
);
418 static Property bcm2835_property_props
[] = {
419 DEFINE_PROP_UINT32("board-rev", BCM2835PropertyState
, board_rev
, 0),
420 DEFINE_PROP_END_OF_LIST()
423 static void bcm2835_property_class_init(ObjectClass
*klass
, void *data
)
425 DeviceClass
*dc
= DEVICE_CLASS(klass
);
427 dc
->props
= bcm2835_property_props
;
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
)