2 * Raspberry Pi emulation (c) 2012 Gregory Estrade
3 * This code is licensed under the GNU GPLv2 and later.
6 #include "hw/display/framebuffer.h"
8 #include "ui/console.h"
9 #include "ui/pixel_ops.h"
10 #include "exec/address-spaces.h"
12 #include "hw/arm/bcm2835_common.h"
15 extern AddressSpace
*bcm2835_peripheral_as
;
17 #define TYPE_BCM2835_PROPERTY "bcm2835_property"
18 #define BCM2835_PROPERTY(obj) \
19 OBJECT_CHECK(Bcm2835PropertyState, (obj), TYPE_BCM2835_PROPERTY)
28 } Bcm2835PropertyState
;
30 static void update_fb(void)
34 bcm2835_fb
.base
= bcm2835_vcram_base
;
35 bcm2835_fb
.base
+= BCM2835_FB_OFFSET
;
37 /* TODO - Manage properly virtual resolution */
39 bcm2835_fb
.pitch
= bcm2835_fb
.xres
* (bcm2835_fb
.bpp
>> 3);
40 bcm2835_fb
.size
= bcm2835_fb
.yres
* bcm2835_fb
.pitch
;
43 /* https://github.com/raspberrypi/firmware/wiki/Mailbox-property-interface */
45 static void bcm2835_property_mbox_push(Bcm2835PropertyState
*s
,
53 uint32_t offset
, length
, color
;
60 tot_len
= ldl_phys(bcm2835_peripheral_as
, value
);
62 /* @(s->addr + 4) : Buffer response code */
64 while (value
+ 8 <= s
->addr
+ tot_len
) {
65 tag
= ldl_phys(bcm2835_peripheral_as
, value
);
66 bufsize
= ldl_phys(bcm2835_peripheral_as
, value
+ 4);
67 /* @(value + 8) : Request/response indicator */
70 case 0x00000000: /* End tag */
72 case 0x00000001: /* Get firmware revision */
73 stl_phys(bcm2835_peripheral_as
, value
+ 12, 346337);
77 case 0x00010001: /* Get board model */
80 case 0x00010002: /* Get board revision */
83 case 0x00010003: /* Get board MAC address */
84 /* write the first four bytes of the 6-byte MAC */
85 stl_phys(bcm2835_peripheral_as
, value
+ 12, 0xB827EBD0);
86 /* write the last two bytes, avoid any write past the buffer end */
87 stb_phys(bcm2835_peripheral_as
, value
+ 16, 0xEE);
88 stb_phys(bcm2835_peripheral_as
, value
+ 17, 0xDF);
91 case 0x00010004: /* Get board serial */
94 case 0x00010005: /* Get ARM memory */
96 stl_phys(bcm2835_peripheral_as
, value
+ 12, 0);
98 stl_phys(bcm2835_peripheral_as
, value
+ 16, bcm2835_vcram_base
);
101 case 0x00010006: /* Get VC memory */
103 stl_phys(bcm2835_peripheral_as
, value
+ 12, bcm2835_vcram_base
);
105 stl_phys(bcm2835_peripheral_as
, value
+ 16, VCRAM_SIZE
);
108 case 0x00028001: /* Set power state */
109 /* Assume that whatever device they asked for exists,
110 * and we'll just claim we set it to the desired state */
111 tmp
= ldl_phys(bcm2835_peripheral_as
, value
+ 16);
112 stl_phys(bcm2835_peripheral_as
, value
+ 16, (tmp
& 1));
118 case 0x00030001: /* Get clock state */
119 stl_phys(bcm2835_peripheral_as
, value
+ 16, 0x1);
123 case 0x00038001: /* Set clock state */
127 case 0x00030002: /* Get clock rate */
128 case 0x00030004: /* Get max clock rate */
129 case 0x00030007: /* Get min clock rate */
130 switch (ldl_phys(bcm2835_peripheral_as
, value
+ 12)) {
132 stl_phys(bcm2835_peripheral_as
, value
+ 16, 50000000);
135 stl_phys(bcm2835_peripheral_as
, value
+ 16, 3000000);
138 stl_phys(bcm2835_peripheral_as
, value
+ 16, 700000000);
144 case 0x00038002: /* Set clock rate */
145 case 0x00038004: /* Set max clock rate */
146 case 0x00038007: /* Set min clock rate */
152 case 0x00030006: /* Get temperature */
153 stl_phys(bcm2835_peripheral_as
, value
+ 16, 25000);
157 case 0x0003000A: /* Get max temperature */
158 stl_phys(bcm2835_peripheral_as
, value
+ 16, 99000);
165 case 0x00040001: /* Allocate buffer */
166 stl_phys(bcm2835_peripheral_as
, value
+ 12, bcm2835_fb
.base
);
167 stl_phys(bcm2835_peripheral_as
, value
+ 16, bcm2835_fb
.size
);
170 case 0x00048001: /* Release buffer */
173 case 0x00040002: /* Blank screen */
176 case 0x00040003: /* Get display width/height */
178 stl_phys(bcm2835_peripheral_as
, value
+ 12, bcm2835_fb
.xres
);
179 stl_phys(bcm2835_peripheral_as
, value
+ 16, bcm2835_fb
.yres
);
182 case 0x00044003: /* Test display width/height */
186 case 0x00048003: /* Set display width/height */
188 bcm2835_fb
.xres
= ldl_phys(bcm2835_peripheral_as
, value
+ 12);
189 bcm2835_fb
.yres
= ldl_phys(bcm2835_peripheral_as
, value
+ 16);
193 case 0x00040005: /* Get depth */
194 stl_phys(bcm2835_peripheral_as
, value
+ 12, bcm2835_fb
.bpp
);
197 case 0x00044005: /* Test depth */
200 case 0x00048005: /* Set depth */
201 bcm2835_fb
.bpp
= ldl_phys(bcm2835_peripheral_as
, value
+ 12);
205 case 0x00040006: /* Get pixel order */
206 stl_phys(bcm2835_peripheral_as
, value
+ 12, bcm2835_fb
.pixo
);
209 case 0x00044006: /* Test pixel order */
212 case 0x00048006: /* Set pixel order */
213 bcm2835_fb
.pixo
= ldl_phys(bcm2835_peripheral_as
, value
+ 12);
217 case 0x00040007: /* Get alpha */
218 stl_phys(bcm2835_peripheral_as
, value
+ 12, bcm2835_fb
.alpha
);
221 case 0x00044007: /* Test pixel alpha */
224 case 0x00048007: /* Set alpha */
225 bcm2835_fb
.alpha
= ldl_phys(bcm2835_peripheral_as
, value
+ 12);
229 case 0x00040008: /* Get pitch */
230 stl_phys(bcm2835_peripheral_as
, value
+ 12, bcm2835_fb
.pitch
);
233 case 0x00040009: /* Get virtual offset */
234 stl_phys(bcm2835_peripheral_as
, value
+ 12, bcm2835_fb
.xoffset
);
235 stl_phys(bcm2835_peripheral_as
, value
+ 16, bcm2835_fb
.yoffset
);
238 case 0x00044009: /* Test virtual offset */
241 case 0x00048009: /* Set virtual offset */
242 bcm2835_fb
.xoffset
= ldl_phys(bcm2835_peripheral_as
, value
+ 12);
243 bcm2835_fb
.yoffset
= ldl_phys(bcm2835_peripheral_as
, value
+ 16);
245 stl_phys(bcm2835_peripheral_as
, value
+ 12, bcm2835_fb
.xres
);
246 stl_phys(bcm2835_peripheral_as
, value
+ 16, bcm2835_fb
.yres
);
249 case 0x0004000a: /* Get/Test/Set overscan */
252 stl_phys(bcm2835_peripheral_as
, value
+ 12, 0);
253 stl_phys(bcm2835_peripheral_as
, value
+ 16, 0);
254 stl_phys(bcm2835_peripheral_as
, value
+ 20, 0);
255 stl_phys(bcm2835_peripheral_as
, value
+ 24, 0);
259 case 0x0004800b: /* Set palette */
260 offset
= ldl_phys(bcm2835_peripheral_as
, value
+ 12);
261 length
= ldl_phys(bcm2835_peripheral_as
, value
+ 16);
263 while (n
< length
- offset
) {
264 color
= ldl_phys(bcm2835_peripheral_as
, value
+ 20 + (n
<< 2));
265 stl_phys(bcm2835_peripheral_as
,
266 bcm2835_vcram_base
+ ((offset
+ n
) << 2), color
);
269 stl_phys(bcm2835_peripheral_as
, value
+ 12, 0);
273 case 0x00060001: /* Get DMA channels */
275 stl_phys(bcm2835_peripheral_as
, value
+ 12, 0x003C);
279 case 0x00050001: /* Get command line */
284 qemu_log_mask(LOG_GUEST_ERROR
,
285 "bcm2835_property: unhandled tag %08x\n", tag
);
293 stl_phys(bcm2835_peripheral_as
, value
+ 8, (1 << 31) | resplen
);
294 value
+= bufsize
+ 12;
297 /* Buffer response code */
298 stl_phys(bcm2835_peripheral_as
, s
->addr
+ 4, (1 << 31));
300 if (bcm2835_fb
.lock
) {
301 bcm2835_fb
.invalidate
= 1;
302 qemu_console_resize(bcm2835_fb
.con
, bcm2835_fb
.xres
, bcm2835_fb
.yres
);
307 static uint64_t bcm2835_property_read(void *opaque
, hwaddr offset
,
310 Bcm2835PropertyState
*s
= (Bcm2835PropertyState
*)opaque
;
315 res
= MBOX_CHAN_PROPERTY
| s
->addr
;
317 qemu_set_irq(s
->mbox_irq
, 0);
323 qemu_log_mask(LOG_GUEST_ERROR
,
324 "bcm2835_property_read: Bad offset %x\n", (int)offset
);
329 static void bcm2835_property_write(void *opaque
, hwaddr offset
,
330 uint64_t value
, unsigned size
)
332 Bcm2835PropertyState
*s
= (Bcm2835PropertyState
*)opaque
;
337 bcm2835_property_mbox_push(s
, value
);
338 qemu_set_irq(s
->mbox_irq
, 1);
342 qemu_log_mask(LOG_GUEST_ERROR
,
343 "bcm2835_property_write: Bad offset %x\n", (int)offset
);
350 static const MemoryRegionOps bcm2835_property_ops
= {
351 .read
= bcm2835_property_read
,
352 .write
= bcm2835_property_write
,
353 .endianness
= DEVICE_NATIVE_ENDIAN
,
357 static const VMStateDescription vmstate_bcm2835_property
= {
358 .name
= TYPE_BCM2835_PROPERTY
,
360 .minimum_version_id
= 1,
361 .minimum_version_id_old
= 1,
362 .fields
= (VMStateField
[]) {
363 VMSTATE_END_OF_LIST()
367 static int bcm2835_property_init(SysBusDevice
*sbd
)
369 DeviceState
*dev
= DEVICE(sbd
);
370 Bcm2835PropertyState
*s
= BCM2835_PROPERTY(dev
);
375 sysbus_init_irq(sbd
, &s
->mbox_irq
);
376 memory_region_init_io(&s
->iomem
, OBJECT(s
), &bcm2835_property_ops
, s
,
377 TYPE_BCM2835_PROPERTY
, 0x10);
378 sysbus_init_mmio(sbd
, &s
->iomem
);
379 vmstate_register(dev
, -1, &vmstate_bcm2835_property
, s
);
384 static void bcm2835_property_class_init(ObjectClass
*klass
, void *data
)
386 SysBusDeviceClass
*sdc
= SYS_BUS_DEVICE_CLASS(klass
);
388 sdc
->init
= bcm2835_property_init
;
391 static TypeInfo bcm2835_property_info
= {
392 .name
= TYPE_BCM2835_PROPERTY
,
393 .parent
= TYPE_SYS_BUS_DEVICE
,
394 .instance_size
= sizeof(Bcm2835PropertyState
),
395 .class_init
= bcm2835_property_class_init
,
398 static void bcm2835_property_register_types(void)
400 type_register_static(&bcm2835_property_info
);
403 type_init(bcm2835_property_register_types
)