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: %x get board model NYI\n", tag
);
62 case 0x00010002: /* Get board revision */
63 stl_le_phys(&s
->dma_as
, value
+ 12, s
->board_rev
);
66 case 0x00010003: /* Get board MAC address */
67 resplen
= sizeof(s
->macaddr
.a
);
68 dma_memory_write(&s
->dma_as
, value
+ 12, s
->macaddr
.a
, resplen
);
70 case 0x00010004: /* Get board serial */
71 qemu_log_mask(LOG_UNIMP
,
72 "bcm2835_property: %x get board serial NYI\n", tag
);
75 case 0x00010005: /* Get ARM memory */
77 stl_le_phys(&s
->dma_as
, value
+ 12, 0);
79 stl_le_phys(&s
->dma_as
, value
+ 16, s
->fbdev
->vcram_base
);
82 case 0x00010006: /* Get VC memory */
84 stl_le_phys(&s
->dma_as
, value
+ 12, s
->fbdev
->vcram_base
);
86 stl_le_phys(&s
->dma_as
, value
+ 16, s
->fbdev
->vcram_size
);
89 case 0x00028001: /* Set power state */
90 /* Assume that whatever device they asked for exists,
91 * and we'll just claim we set it to the desired state
93 tmp
= ldl_le_phys(&s
->dma_as
, value
+ 16);
94 stl_le_phys(&s
->dma_as
, value
+ 16, (tmp
& 1));
100 case 0x00030001: /* Get clock state */
101 stl_le_phys(&s
->dma_as
, value
+ 16, 0x1);
105 case 0x00038001: /* Set clock state */
106 qemu_log_mask(LOG_UNIMP
,
107 "bcm2835_property: %x set clock state NYI\n", tag
);
111 case 0x00030002: /* Get clock rate */
112 case 0x00030004: /* Get max clock rate */
113 case 0x00030007: /* Get min clock rate */
114 switch (ldl_le_phys(&s
->dma_as
, value
+ 12)) {
116 stl_le_phys(&s
->dma_as
, value
+ 16, 50000000);
119 stl_le_phys(&s
->dma_as
, value
+ 16, 3000000);
122 stl_le_phys(&s
->dma_as
, value
+ 16, 700000000);
128 case 0x00038002: /* Set clock rate */
129 case 0x00038004: /* Set max clock rate */
130 case 0x00038007: /* Set min clock rate */
131 qemu_log_mask(LOG_UNIMP
,
132 "bcm2835_property: %x set clock rates NYI\n", tag
);
138 case 0x00030006: /* Get temperature */
139 stl_le_phys(&s
->dma_as
, value
+ 16, 25000);
143 case 0x0003000A: /* Get max temperature */
144 stl_le_phys(&s
->dma_as
, value
+ 16, 99000);
150 case 0x00040001: /* Allocate buffer */
151 stl_le_phys(&s
->dma_as
, value
+ 12, fbconfig
.base
);
152 stl_le_phys(&s
->dma_as
, value
+ 16,
153 bcm2835_fb_get_size(&fbconfig
));
156 case 0x00048001: /* Release buffer */
159 case 0x00040002: /* Blank screen */
162 case 0x00044003: /* Test physical display width/height */
163 case 0x00044004: /* Test virtual display width/height */
166 case 0x00048003: /* Set physical display width/height */
167 fbconfig
.xres
= ldl_le_phys(&s
->dma_as
, value
+ 12);
168 fbconfig
.yres
= ldl_le_phys(&s
->dma_as
, value
+ 16);
169 bcm2835_fb_validate_config(&fbconfig
);
170 fbconfig_updated
= true;
172 case 0x00040003: /* Get physical display width/height */
173 stl_le_phys(&s
->dma_as
, value
+ 12, fbconfig
.xres
);
174 stl_le_phys(&s
->dma_as
, value
+ 16, fbconfig
.yres
);
177 case 0x00048004: /* Set virtual display width/height */
178 fbconfig
.xres_virtual
= ldl_le_phys(&s
->dma_as
, value
+ 12);
179 fbconfig
.yres_virtual
= ldl_le_phys(&s
->dma_as
, value
+ 16);
180 bcm2835_fb_validate_config(&fbconfig
);
181 fbconfig_updated
= true;
183 case 0x00040004: /* Get virtual display width/height */
184 stl_le_phys(&s
->dma_as
, value
+ 12, fbconfig
.xres_virtual
);
185 stl_le_phys(&s
->dma_as
, value
+ 16, fbconfig
.yres_virtual
);
188 case 0x00044005: /* Test depth */
191 case 0x00048005: /* Set depth */
192 fbconfig
.bpp
= ldl_le_phys(&s
->dma_as
, value
+ 12);
193 bcm2835_fb_validate_config(&fbconfig
);
194 fbconfig_updated
= true;
196 case 0x00040005: /* Get depth */
197 stl_le_phys(&s
->dma_as
, value
+ 12, fbconfig
.bpp
);
200 case 0x00044006: /* Test pixel order */
203 case 0x00048006: /* Set pixel order */
204 fbconfig
.pixo
= ldl_le_phys(&s
->dma_as
, value
+ 12);
205 bcm2835_fb_validate_config(&fbconfig
);
206 fbconfig_updated
= true;
208 case 0x00040006: /* Get pixel order */
209 stl_le_phys(&s
->dma_as
, value
+ 12, fbconfig
.pixo
);
212 case 0x00044007: /* Test pixel alpha */
215 case 0x00048007: /* Set alpha */
216 fbconfig
.alpha
= ldl_le_phys(&s
->dma_as
, value
+ 12);
217 bcm2835_fb_validate_config(&fbconfig
);
218 fbconfig_updated
= true;
220 case 0x00040007: /* Get alpha */
221 stl_le_phys(&s
->dma_as
, value
+ 12, fbconfig
.alpha
);
224 case 0x00040008: /* Get pitch */
225 stl_le_phys(&s
->dma_as
, value
+ 12,
226 bcm2835_fb_get_pitch(&fbconfig
));
229 case 0x00044009: /* Test virtual offset */
232 case 0x00048009: /* Set virtual offset */
233 fbconfig
.xoffset
= ldl_le_phys(&s
->dma_as
, value
+ 12);
234 fbconfig
.yoffset
= ldl_le_phys(&s
->dma_as
, value
+ 16);
235 bcm2835_fb_validate_config(&fbconfig
);
236 fbconfig_updated
= true;
238 case 0x00040009: /* Get virtual offset */
239 stl_le_phys(&s
->dma_as
, value
+ 12, fbconfig
.xoffset
);
240 stl_le_phys(&s
->dma_as
, value
+ 16, fbconfig
.yoffset
);
243 case 0x0004000a: /* Get/Test/Set overscan */
246 stl_le_phys(&s
->dma_as
, value
+ 12, 0);
247 stl_le_phys(&s
->dma_as
, value
+ 16, 0);
248 stl_le_phys(&s
->dma_as
, value
+ 20, 0);
249 stl_le_phys(&s
->dma_as
, value
+ 24, 0);
252 case 0x0004800b: /* Set palette */
253 offset
= ldl_le_phys(&s
->dma_as
, value
+ 12);
254 length
= ldl_le_phys(&s
->dma_as
, value
+ 16);
256 while (n
< length
- offset
) {
257 color
= ldl_le_phys(&s
->dma_as
, value
+ 20 + (n
<< 2));
258 stl_le_phys(&s
->dma_as
,
259 s
->fbdev
->vcram_base
+ ((offset
+ n
) << 2), color
);
262 stl_le_phys(&s
->dma_as
, value
+ 12, 0);
266 case 0x00060001: /* Get DMA channels */
268 stl_le_phys(&s
->dma_as
, value
+ 12, 0x003C);
272 case 0x00050001: /* Get command line */
277 qemu_log_mask(LOG_GUEST_ERROR
,
278 "bcm2835_property: unhandled tag %08x\n", tag
);
286 stl_le_phys(&s
->dma_as
, value
+ 8, (1 << 31) | resplen
);
287 value
+= bufsize
+ 12;
290 /* Reconfigure framebuffer if required */
291 if (fbconfig_updated
) {
292 bcm2835_fb_reconfigure(s
->fbdev
, &fbconfig
);
295 /* Buffer response code */
296 stl_le_phys(&s
->dma_as
, s
->addr
+ 4, (1 << 31));
299 static uint64_t bcm2835_property_read(void *opaque
, hwaddr offset
,
302 BCM2835PropertyState
*s
= opaque
;
307 res
= MBOX_CHAN_PROPERTY
| s
->addr
;
309 qemu_set_irq(s
->mbox_irq
, 0);
312 case MBOX_AS_PENDING
:
317 qemu_log_mask(LOG_GUEST_ERROR
, "%s: Bad offset %"HWADDR_PRIx
"\n",
325 static void bcm2835_property_write(void *opaque
, hwaddr offset
,
326 uint64_t value
, unsigned size
)
328 BCM2835PropertyState
*s
= opaque
;
332 /* bcm2835_mbox should check our pending status before pushing */
335 bcm2835_property_mbox_push(s
, value
);
336 qemu_set_irq(s
->mbox_irq
, 1);
340 qemu_log_mask(LOG_GUEST_ERROR
, "%s: Bad offset %"HWADDR_PRIx
"\n",
346 static const MemoryRegionOps bcm2835_property_ops
= {
347 .read
= bcm2835_property_read
,
348 .write
= bcm2835_property_write
,
349 .endianness
= DEVICE_NATIVE_ENDIAN
,
350 .valid
.min_access_size
= 4,
351 .valid
.max_access_size
= 4,
354 static const VMStateDescription vmstate_bcm2835_property
= {
355 .name
= TYPE_BCM2835_PROPERTY
,
357 .minimum_version_id
= 1,
358 .fields
= (VMStateField
[]) {
359 VMSTATE_MACADDR(macaddr
, BCM2835PropertyState
),
360 VMSTATE_UINT32(addr
, BCM2835PropertyState
),
361 VMSTATE_BOOL(pending
, BCM2835PropertyState
),
362 VMSTATE_END_OF_LIST()
366 static void bcm2835_property_init(Object
*obj
)
368 BCM2835PropertyState
*s
= BCM2835_PROPERTY(obj
);
370 memory_region_init_io(&s
->iomem
, OBJECT(s
), &bcm2835_property_ops
, s
,
371 TYPE_BCM2835_PROPERTY
, 0x10);
372 sysbus_init_mmio(SYS_BUS_DEVICE(s
), &s
->iomem
);
373 sysbus_init_irq(SYS_BUS_DEVICE(s
), &s
->mbox_irq
);
376 static void bcm2835_property_reset(DeviceState
*dev
)
378 BCM2835PropertyState
*s
= BCM2835_PROPERTY(dev
);
383 static void bcm2835_property_realize(DeviceState
*dev
, Error
**errp
)
385 BCM2835PropertyState
*s
= BCM2835_PROPERTY(dev
);
389 obj
= object_property_get_link(OBJECT(dev
), "fb", &err
);
391 error_setg(errp
, "%s: required fb link not found: %s",
392 __func__
, error_get_pretty(err
));
396 s
->fbdev
= BCM2835_FB(obj
);
398 obj
= object_property_get_link(OBJECT(dev
), "dma-mr", &err
);
400 error_setg(errp
, "%s: required dma-mr link not found: %s",
401 __func__
, error_get_pretty(err
));
405 s
->dma_mr
= MEMORY_REGION(obj
);
406 address_space_init(&s
->dma_as
, s
->dma_mr
, NULL
);
408 /* TODO: connect to MAC address of USB NIC device, once we emulate it */
409 qemu_macaddr_default_if_unset(&s
->macaddr
);
411 bcm2835_property_reset(dev
);
414 static Property bcm2835_property_props
[] = {
415 DEFINE_PROP_UINT32("board-rev", BCM2835PropertyState
, board_rev
, 0),
416 DEFINE_PROP_END_OF_LIST()
419 static void bcm2835_property_class_init(ObjectClass
*klass
, void *data
)
421 DeviceClass
*dc
= DEVICE_CLASS(klass
);
423 dc
->props
= bcm2835_property_props
;
424 dc
->realize
= bcm2835_property_realize
;
425 dc
->vmsd
= &vmstate_bcm2835_property
;
428 static TypeInfo bcm2835_property_info
= {
429 .name
= TYPE_BCM2835_PROPERTY
,
430 .parent
= TYPE_SYS_BUS_DEVICE
,
431 .instance_size
= sizeof(BCM2835PropertyState
),
432 .class_init
= bcm2835_property_class_init
,
433 .instance_init
= bcm2835_property_init
,
436 static void bcm2835_property_register_types(void)
438 type_register_static(&bcm2835_property_info
);
441 type_init(bcm2835_property_register_types
)