2 * Raspberry Pi emulation (c) 2012 Gregory Estrade
3 * This code is licensed under the GNU GPLv2 and later.
6 #include "hw/misc/bcm2835_property.h"
7 #include "hw/misc/bcm2835_mbox_defs.h"
9 /* https://github.com/raspberrypi/firmware/wiki/Mailbox-property-interface */
11 static void bcm2835_property_mbox_push(BCM2835PropertyState
*s
, uint32_t value
)
23 tot_len
= ldl_phys(&s
->dma_as
, value
);
25 /* @(addr + 4) : Buffer response code */
27 while (value
+ 8 <= s
->addr
+ tot_len
) {
28 tag
= ldl_phys(&s
->dma_as
, value
);
29 bufsize
= ldl_phys(&s
->dma_as
, value
+ 4);
30 /* @(value + 8) : Request/response indicator */
33 case 0x00000000: /* End tag */
35 case 0x00000001: /* Get firmware revision */
36 stl_phys(&s
->dma_as
, value
+ 12, 346337);
40 case 0x00010001: /* Get board model */
43 case 0x00010002: /* Get board revision */
46 case 0x00010003: /* Get board MAC address */
47 /* write the first four bytes of the 6-byte MAC */
48 stl_phys(&s
->dma_as
, value
+ 12, 0xB827EBD0);
49 /* write the last two bytes, avoid any write past the buffer end */
50 stb_phys(&s
->dma_as
, value
+ 16, 0xEE);
51 stb_phys(&s
->dma_as
, value
+ 17, 0xDF);
54 case 0x00010004: /* Get board serial */
57 case 0x00010005: /* Get ARM memory */
59 stl_phys(&s
->dma_as
, value
+ 12, 0);
61 stl_phys(&s
->dma_as
, value
+ 16, s
->ram_size
);
64 case 0x00028001: /* Set power state */
65 /* Assume that whatever device they asked for exists,
66 * and we'll just claim we set it to the desired state */
67 tmp
= ldl_phys(&s
->dma_as
, value
+ 16);
68 stl_phys(&s
->dma_as
, value
+ 16, (tmp
& 1));
74 case 0x00030001: /* Get clock state */
75 stl_phys(&s
->dma_as
, value
+ 16, 0x1);
79 case 0x00038001: /* Set clock state */
83 case 0x00030002: /* Get clock rate */
84 case 0x00030004: /* Get max clock rate */
85 case 0x00030007: /* Get min clock rate */
86 switch (ldl_phys(&s
->dma_as
, value
+ 12)) {
88 stl_phys(&s
->dma_as
, value
+ 16, 50000000);
91 stl_phys(&s
->dma_as
, value
+ 16, 3000000);
94 stl_phys(&s
->dma_as
, value
+ 16, 700000000);
100 case 0x00038002: /* Set clock rate */
101 case 0x00038004: /* Set max clock rate */
102 case 0x00038007: /* Set min clock rate */
108 case 0x00030006: /* Get temperature */
109 stl_phys(&s
->dma_as
, value
+ 16, 25000);
113 case 0x0003000A: /* Get max temperature */
114 stl_phys(&s
->dma_as
, value
+ 16, 99000);
119 case 0x00060001: /* Get DMA channels */
121 stl_phys(&s
->dma_as
, value
+ 12, 0x003C);
125 case 0x00050001: /* Get command line */
130 qemu_log_mask(LOG_GUEST_ERROR
,
131 "bcm2835_property: unhandled tag %08x\n", tag
);
139 stl_phys(&s
->dma_as
, value
+ 8, (1 << 31) | resplen
);
140 value
+= bufsize
+ 12;
143 /* Buffer response code */
144 stl_phys(&s
->dma_as
, s
->addr
+ 4, (1 << 31));
147 static uint64_t bcm2835_property_read(void *opaque
, hwaddr offset
,
150 BCM2835PropertyState
*s
= opaque
;
155 res
= MBOX_CHAN_PROPERTY
| s
->addr
;
157 qemu_set_irq(s
->mbox_irq
, 0);
160 case MBOX_AS_PENDING
:
165 qemu_log_mask(LOG_GUEST_ERROR
, "%s: Bad offset %"HWADDR_PRIx
"\n",
173 static void bcm2835_property_write(void *opaque
, hwaddr offset
,
174 uint64_t value
, unsigned size
)
176 BCM2835PropertyState
*s
= opaque
;
182 bcm2835_property_mbox_push(s
, value
);
183 qemu_set_irq(s
->mbox_irq
, 1);
188 qemu_log_mask(LOG_GUEST_ERROR
, "%s: Bad offset %"HWADDR_PRIx
"\n",
195 static const MemoryRegionOps bcm2835_property_ops
= {
196 .read
= bcm2835_property_read
,
197 .write
= bcm2835_property_write
,
198 .endianness
= DEVICE_NATIVE_ENDIAN
,
199 .valid
.min_access_size
= 4,
200 .valid
.max_access_size
= 4,
203 static const VMStateDescription vmstate_bcm2835_property
= {
204 .name
= TYPE_BCM2835_PROPERTY
,
206 .minimum_version_id
= 1,
207 .minimum_version_id_old
= 1,
208 .fields
= (VMStateField
[]) {
209 VMSTATE_UINT32(addr
, BCM2835PropertyState
),
210 VMSTATE_BOOL(pending
, BCM2835PropertyState
),
211 VMSTATE_END_OF_LIST()
215 static void bcm2835_property_init(Object
*obj
)
217 BCM2835PropertyState
*s
= BCM2835_PROPERTY(obj
);
218 memory_region_init_io(&s
->iomem
, OBJECT(s
), &bcm2835_property_ops
, s
,
219 TYPE_BCM2835_PROPERTY
, 0x10);
220 sysbus_init_mmio(SYS_BUS_DEVICE(s
), &s
->iomem
);
221 sysbus_init_irq(SYS_BUS_DEVICE(s
), &s
->mbox_irq
);
224 static void bcm2835_property_reset(DeviceState
*dev
)
226 BCM2835PropertyState
*s
= BCM2835_PROPERTY(dev
);
231 static void bcm2835_property_realize(DeviceState
*dev
, Error
**errp
)
233 BCM2835PropertyState
*s
= BCM2835_PROPERTY(dev
);
237 obj
= object_property_get_link(OBJECT(dev
), "dma-mr", &err
);
239 error_setg(errp
, "%s: required dma-mr link not found: %s",
240 __func__
, error_get_pretty(err
));
244 s
->dma_mr
= MEMORY_REGION(obj
);
245 address_space_init(&s
->dma_as
, s
->dma_mr
, NULL
);
247 bcm2835_property_reset(dev
);
250 static Property bcm2835_property_props
[] = {
251 DEFINE_PROP_UINT32("ram-size", BCM2835PropertyState
, ram_size
, 0),
252 DEFINE_PROP_END_OF_LIST()
255 static void bcm2835_property_class_init(ObjectClass
*klass
, void *data
)
257 DeviceClass
*dc
= DEVICE_CLASS(klass
);
259 dc
->props
= bcm2835_property_props
;
260 dc
->realize
= bcm2835_property_realize
;
261 dc
->vmsd
= &vmstate_bcm2835_property
;
264 static TypeInfo bcm2835_property_info
= {
265 .name
= TYPE_BCM2835_PROPERTY
,
266 .parent
= TYPE_SYS_BUS_DEVICE
,
267 .instance_size
= sizeof(BCM2835PropertyState
),
268 .class_init
= bcm2835_property_class_init
,
269 .instance_init
= bcm2835_property_init
,
272 static void bcm2835_property_register_types(void)
274 type_register_static(&bcm2835_property_info
);
277 type_init(bcm2835_property_register_types
)