bcm2835_property: add bcm2835 property channel
[qemu/ar7.git] / hw / misc / bcm2835_property.c
blob2385be459e5e4ac5eb698b873801897c59b893fb
1 /*
2 * Raspberry Pi emulation (c) 2012 Gregory Estrade
3 * This code is licensed under the GNU GPLv2 and later.
4 */
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)
13 uint32_t tag;
14 uint32_t bufsize;
15 uint32_t tot_len;
16 size_t resplen;
17 uint32_t tmp;
19 value &= ~0xf;
21 s->addr = value;
23 tot_len = ldl_phys(&s->dma_as, value);
25 /* @(addr + 4) : Buffer response code */
26 value = s->addr + 8;
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 */
31 resplen = 0;
32 switch (tag) {
33 case 0x00000000: /* End tag */
34 break;
35 case 0x00000001: /* Get firmware revision */
36 stl_phys(&s->dma_as, value + 12, 346337);
37 resplen = 4;
38 break;
40 case 0x00010001: /* Get board model */
41 resplen = 4;
42 break;
43 case 0x00010002: /* Get board revision */
44 resplen = 4;
45 break;
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);
52 resplen = 6;
53 break;
54 case 0x00010004: /* Get board serial */
55 resplen = 8;
56 break;
57 case 0x00010005: /* Get ARM memory */
58 /* base */
59 stl_phys(&s->dma_as, value + 12, 0);
60 /* size */
61 stl_phys(&s->dma_as, value + 16, s->ram_size);
62 resplen = 8;
63 break;
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));
69 resplen = 8;
70 break;
72 /* Clocks */
74 case 0x00030001: /* Get clock state */
75 stl_phys(&s->dma_as, value + 16, 0x1);
76 resplen = 8;
77 break;
79 case 0x00038001: /* Set clock state */
80 resplen = 8;
81 break;
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)) {
87 case 1: /* EMMC */
88 stl_phys(&s->dma_as, value + 16, 50000000);
89 break;
90 case 2: /* UART */
91 stl_phys(&s->dma_as, value + 16, 3000000);
92 break;
93 default:
94 stl_phys(&s->dma_as, value + 16, 700000000);
95 break;
97 resplen = 8;
98 break;
100 case 0x00038002: /* Set clock rate */
101 case 0x00038004: /* Set max clock rate */
102 case 0x00038007: /* Set min clock rate */
103 resplen = 8;
104 break;
106 /* Temperature */
108 case 0x00030006: /* Get temperature */
109 stl_phys(&s->dma_as, value + 16, 25000);
110 resplen = 8;
111 break;
113 case 0x0003000A: /* Get max temperature */
114 stl_phys(&s->dma_as, value + 16, 99000);
115 resplen = 8;
116 break;
119 case 0x00060001: /* Get DMA channels */
120 /* channels 2-5 */
121 stl_phys(&s->dma_as, value + 12, 0x003C);
122 resplen = 4;
123 break;
125 case 0x00050001: /* Get command line */
126 resplen = 0;
127 break;
129 default:
130 qemu_log_mask(LOG_GUEST_ERROR,
131 "bcm2835_property: unhandled tag %08x\n", tag);
132 break;
135 if (tag == 0) {
136 break;
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,
148 unsigned size)
150 BCM2835PropertyState *s = opaque;
151 uint32_t res = 0;
153 switch (offset) {
154 case MBOX_AS_DATA:
155 res = MBOX_CHAN_PROPERTY | s->addr;
156 s->pending = false;
157 qemu_set_irq(s->mbox_irq, 0);
158 break;
160 case MBOX_AS_PENDING:
161 res = s->pending;
162 break;
164 default:
165 qemu_log_mask(LOG_GUEST_ERROR, "%s: Bad offset %"HWADDR_PRIx"\n",
166 __func__, offset);
167 return 0;
170 return res;
173 static void bcm2835_property_write(void *opaque, hwaddr offset,
174 uint64_t value, unsigned size)
176 BCM2835PropertyState *s = opaque;
178 switch (offset) {
179 case MBOX_AS_DATA:
180 if (!s->pending) {
181 s->pending = true;
182 bcm2835_property_mbox_push(s, value);
183 qemu_set_irq(s->mbox_irq, 1);
185 break;
187 default:
188 qemu_log_mask(LOG_GUEST_ERROR, "%s: Bad offset %"HWADDR_PRIx"\n",
189 __func__, offset);
190 return;
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,
205 .version_id = 1,
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);
228 s->pending = false;
231 static void bcm2835_property_realize(DeviceState *dev, Error **errp)
233 BCM2835PropertyState *s = BCM2835_PROPERTY(dev);
234 Object *obj;
235 Error *err = NULL;
237 obj = object_property_get_link(OBJECT(dev), "dma-mr", &err);
238 if (obj == NULL) {
239 error_setg(errp, "%s: required dma-mr link not found: %s",
240 __func__, error_get_pretty(err));
241 return;
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)