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"
8 #include "sysemu/dma.h"
10 /* https://github.com/raspberrypi/firmware/wiki/Mailbox-property-interface */
12 static void bcm2835_property_mbox_push(BCM2835PropertyState
*s
, uint32_t value
)
24 tot_len
= ldl_phys(&s
->dma_as
, value
);
26 /* @(addr + 4) : Buffer response code */
28 while (value
+ 8 <= s
->addr
+ tot_len
) {
29 tag
= ldl_phys(&s
->dma_as
, value
);
30 bufsize
= ldl_phys(&s
->dma_as
, value
+ 4);
31 /* @(value + 8) : Request/response indicator */
34 case 0x00000000: /* End tag */
36 case 0x00000001: /* Get firmware revision */
37 stl_phys(&s
->dma_as
, value
+ 12, 346337);
40 case 0x00010001: /* Get board model */
41 qemu_log_mask(LOG_UNIMP
,
42 "bcm2835_property: %x get board model NYI\n", tag
);
45 case 0x00010002: /* Get board revision */
46 stl_phys(&s
->dma_as
, value
+ 12, s
->board_rev
);
49 case 0x00010003: /* Get board MAC address */
50 resplen
= sizeof(s
->macaddr
.a
);
51 dma_memory_write(&s
->dma_as
, value
+ 12, s
->macaddr
.a
, resplen
);
53 case 0x00010004: /* Get board serial */
54 qemu_log_mask(LOG_UNIMP
,
55 "bcm2835_property: %x get board serial NYI\n", tag
);
58 case 0x00010005: /* Get ARM memory */
60 stl_phys(&s
->dma_as
, value
+ 12, 0);
62 stl_phys(&s
->dma_as
, value
+ 16, s
->ram_size
);
65 case 0x00028001: /* Set power state */
66 /* Assume that whatever device they asked for exists,
67 * and we'll just claim we set it to the desired state
69 tmp
= ldl_phys(&s
->dma_as
, value
+ 16);
70 stl_phys(&s
->dma_as
, value
+ 16, (tmp
& 1));
76 case 0x00030001: /* Get clock state */
77 stl_phys(&s
->dma_as
, value
+ 16, 0x1);
81 case 0x00038001: /* Set clock state */
82 qemu_log_mask(LOG_UNIMP
,
83 "bcm2835_property: %x set clock state NYI\n", tag
);
87 case 0x00030002: /* Get clock rate */
88 case 0x00030004: /* Get max clock rate */
89 case 0x00030007: /* Get min clock rate */
90 switch (ldl_phys(&s
->dma_as
, value
+ 12)) {
92 stl_phys(&s
->dma_as
, value
+ 16, 50000000);
95 stl_phys(&s
->dma_as
, value
+ 16, 3000000);
98 stl_phys(&s
->dma_as
, value
+ 16, 700000000);
104 case 0x00038002: /* Set clock rate */
105 case 0x00038004: /* Set max clock rate */
106 case 0x00038007: /* Set min clock rate */
107 qemu_log_mask(LOG_UNIMP
,
108 "bcm2835_property: %x set clock rates NYI\n", tag
);
114 case 0x00030006: /* Get temperature */
115 stl_phys(&s
->dma_as
, value
+ 16, 25000);
119 case 0x0003000A: /* Get max temperature */
120 stl_phys(&s
->dma_as
, value
+ 16, 99000);
125 case 0x00060001: /* Get DMA channels */
127 stl_phys(&s
->dma_as
, value
+ 12, 0x003C);
131 case 0x00050001: /* Get command line */
136 qemu_log_mask(LOG_GUEST_ERROR
,
137 "bcm2835_property: unhandled tag %08x\n", tag
);
145 stl_phys(&s
->dma_as
, value
+ 8, (1 << 31) | resplen
);
146 value
+= bufsize
+ 12;
149 /* Buffer response code */
150 stl_phys(&s
->dma_as
, s
->addr
+ 4, (1 << 31));
153 static uint64_t bcm2835_property_read(void *opaque
, hwaddr offset
,
156 BCM2835PropertyState
*s
= opaque
;
161 res
= MBOX_CHAN_PROPERTY
| s
->addr
;
163 qemu_set_irq(s
->mbox_irq
, 0);
166 case MBOX_AS_PENDING
:
171 qemu_log_mask(LOG_GUEST_ERROR
, "%s: Bad offset %"HWADDR_PRIx
"\n",
179 static void bcm2835_property_write(void *opaque
, hwaddr offset
,
180 uint64_t value
, unsigned size
)
182 BCM2835PropertyState
*s
= opaque
;
186 /* bcm2835_mbox should check our pending status before pushing */
189 bcm2835_property_mbox_push(s
, value
);
190 qemu_set_irq(s
->mbox_irq
, 1);
194 qemu_log_mask(LOG_GUEST_ERROR
, "%s: Bad offset %"HWADDR_PRIx
"\n",
200 static const MemoryRegionOps bcm2835_property_ops
= {
201 .read
= bcm2835_property_read
,
202 .write
= bcm2835_property_write
,
203 .endianness
= DEVICE_NATIVE_ENDIAN
,
204 .valid
.min_access_size
= 4,
205 .valid
.max_access_size
= 4,
208 static const VMStateDescription vmstate_bcm2835_property
= {
209 .name
= TYPE_BCM2835_PROPERTY
,
211 .minimum_version_id
= 1,
212 .fields
= (VMStateField
[]) {
213 VMSTATE_MACADDR(macaddr
, BCM2835PropertyState
),
214 VMSTATE_UINT32(addr
, BCM2835PropertyState
),
215 VMSTATE_BOOL(pending
, BCM2835PropertyState
),
216 VMSTATE_END_OF_LIST()
220 static void bcm2835_property_init(Object
*obj
)
222 BCM2835PropertyState
*s
= BCM2835_PROPERTY(obj
);
224 memory_region_init_io(&s
->iomem
, OBJECT(s
), &bcm2835_property_ops
, s
,
225 TYPE_BCM2835_PROPERTY
, 0x10);
226 sysbus_init_mmio(SYS_BUS_DEVICE(s
), &s
->iomem
);
227 sysbus_init_irq(SYS_BUS_DEVICE(s
), &s
->mbox_irq
);
230 static void bcm2835_property_reset(DeviceState
*dev
)
232 BCM2835PropertyState
*s
= BCM2835_PROPERTY(dev
);
237 static void bcm2835_property_realize(DeviceState
*dev
, Error
**errp
)
239 BCM2835PropertyState
*s
= BCM2835_PROPERTY(dev
);
243 obj
= object_property_get_link(OBJECT(dev
), "dma-mr", &err
);
245 error_setg(errp
, "%s: required dma-mr link not found: %s",
246 __func__
, error_get_pretty(err
));
250 s
->dma_mr
= MEMORY_REGION(obj
);
251 address_space_init(&s
->dma_as
, s
->dma_mr
, NULL
);
253 /* TODO: connect to MAC address of USB NIC device, once we emulate it */
254 qemu_macaddr_default_if_unset(&s
->macaddr
);
256 bcm2835_property_reset(dev
);
259 static Property bcm2835_property_props
[] = {
260 DEFINE_PROP_UINT32("board-rev", BCM2835PropertyState
, board_rev
, 0),
261 DEFINE_PROP_UINT32("ram-size", BCM2835PropertyState
, ram_size
, 0),
262 DEFINE_PROP_END_OF_LIST()
265 static void bcm2835_property_class_init(ObjectClass
*klass
, void *data
)
267 DeviceClass
*dc
= DEVICE_CLASS(klass
);
269 dc
->props
= bcm2835_property_props
;
270 dc
->realize
= bcm2835_property_realize
;
271 dc
->vmsd
= &vmstate_bcm2835_property
;
274 static TypeInfo bcm2835_property_info
= {
275 .name
= TYPE_BCM2835_PROPERTY
,
276 .parent
= TYPE_SYS_BUS_DEVICE
,
277 .instance_size
= sizeof(BCM2835PropertyState
),
278 .class_init
= bcm2835_property_class_init
,
279 .instance_init
= bcm2835_property_init
,
282 static void bcm2835_property_register_types(void)
284 type_register_static(&bcm2835_property_info
);
287 type_init(bcm2835_property_register_types
)