2 * Arm PrimeCell PL080/PL081 DMA controller
4 * Copyright (c) 2006 CodeSourcery.
5 * Written by Paul Brook
7 * This code is licensed under the GPL.
10 #include "hw/sysbus.h"
11 #include "exec/address-spaces.h"
13 #define PL080_MAX_CHANNELS 8
14 #define PL080_CONF_E 0x1
15 #define PL080_CONF_M1 0x2
16 #define PL080_CONF_M2 0x4
18 #define PL080_CCONF_H 0x40000
19 #define PL080_CCONF_A 0x20000
20 #define PL080_CCONF_L 0x10000
21 #define PL080_CCONF_ITC 0x08000
22 #define PL080_CCONF_IE 0x04000
23 #define PL080_CCONF_E 0x00001
25 #define PL080_CCTRL_I 0x80000000
26 #define PL080_CCTRL_DI 0x08000000
27 #define PL080_CCTRL_SI 0x04000000
28 #define PL080_CCTRL_D 0x02000000
29 #define PL080_CCTRL_S 0x01000000
39 #define TYPE_PL080 "pl080"
40 #define PL080(obj) OBJECT_CHECK(PL080State, (obj), TYPE_PL080)
42 typedef struct PL080State {
43 SysBusDevice parent_obj;
54 pl080_channel chan[PL080_MAX_CHANNELS];
56 /* Flag to avoid recursive DMA invocations. */
61 static const VMStateDescription vmstate_pl080_channel = {
62 .name = "pl080_channel",
64 .minimum_version_id = 1,
65 .fields = (VMStateField[]) {
66 VMSTATE_UINT32(src, pl080_channel),
67 VMSTATE_UINT32(dest, pl080_channel),
68 VMSTATE_UINT32(lli, pl080_channel),
69 VMSTATE_UINT32(ctrl, pl080_channel),
70 VMSTATE_UINT32(conf, pl080_channel),
75 static const VMStateDescription vmstate_pl080 = {
78 .minimum_version_id = 1,
79 .fields = (VMStateField[]) {
80 VMSTATE_UINT8(tc_int, PL080State),
81 VMSTATE_UINT8(tc_mask, PL080State),
82 VMSTATE_UINT8(err_int, PL080State),
83 VMSTATE_UINT8(err_mask, PL080State),
84 VMSTATE_UINT32(conf, PL080State),
85 VMSTATE_UINT32(sync, PL080State),
86 VMSTATE_UINT32(req_single, PL080State),
87 VMSTATE_UINT32(req_burst, PL080State),
88 VMSTATE_UINT8(tc_int, PL080State),
89 VMSTATE_UINT8(tc_int, PL080State),
90 VMSTATE_UINT8(tc_int, PL080State),
91 VMSTATE_STRUCT_ARRAY(chan, PL080State, PL080_MAX_CHANNELS,
92 1, vmstate_pl080_channel, pl080_channel),
93 VMSTATE_INT32(running, PL080State),
98 static const unsigned char pl080_id[] =
99 { 0x80, 0x10, 0x04, 0x0a, 0x0d, 0xf0, 0x05, 0xb1 };
101 static const unsigned char pl081_id[] =
102 { 0x81, 0x10, 0x04, 0x0a, 0x0d, 0xf0, 0x05, 0xb1 };
104 static void pl080_update(PL080State *s)
106 if ((s->tc_int & s->tc_mask)
107 || (s->err_int & s->err_mask))
108 qemu_irq_raise(s->irq);
110 qemu_irq_lower(s->irq);
113 static void pl080_run(PL080State *s)
129 for (c = 0; c < s->nchannels; c++) {
130 if (s->chan[c].conf & PL080_CCONF_ITC)
131 s->tc_mask |= 1 << c;
132 if (s->chan[c].conf & PL080_CCONF_IE)
133 s->err_mask |= 1 << c;
136 if ((s->conf & PL080_CONF_E) == 0)
139 hw_error("DMA active\n");
140 /* If we are already in the middle of a DMA operation then indicate that
141 there may be new DMA requests and return immediately. */
148 for (c = 0; c < s->nchannels; c++) {
151 /* Test if thiws channel has any pending DMA requests. */
152 if ((ch->conf & (PL080_CCONF_H | PL080_CCONF_E))
155 flow = (ch->conf >> 11) & 7;
158 "pl080_run: Peripheral flow control not implemented\n");
160 src_id = (ch->conf >> 1) & 0x1f;
161 dest_id = (ch->conf >> 6) & 0x1f;
162 size = ch->ctrl & 0xfff;
163 req = s->req_single | s->req_burst;
168 if ((req & (1u << dest_id)) == 0)
172 if ((req & (1u << src_id)) == 0)
176 if ((req & (1u << src_id)) == 0
177 || (req & (1u << dest_id)) == 0)
184 /* Transfer one element. */
185 /* ??? Should transfer multiple elements for a burst request. */
186 /* ??? Unclear what the proper behavior is when source and
187 destination widths are different. */
188 swidth = 1 << ((ch->ctrl >> 18) & 7);
189 dwidth = 1 << ((ch->ctrl >> 21) & 7);
190 for (n = 0; n < dwidth; n+= swidth) {
191 cpu_physical_memory_read(ch->src, buff + n, swidth);
192 if (ch->ctrl & PL080_CCTRL_SI)
195 xsize = (dwidth < swidth) ? swidth : dwidth;
196 /* ??? This may pad the value incorrectly for dwidth < 32. */
197 for (n = 0; n < xsize; n += dwidth) {
198 cpu_physical_memory_write(ch->dest + n, buff + n, dwidth);
199 if (ch->ctrl & PL080_CCTRL_DI)
204 ch->ctrl = (ch->ctrl & 0xfffff000) | size;
206 /* Transfer complete. */
208 ch->src = ldl_le_phys(&address_space_memory, ch->lli);
209 ch->dest = ldl_le_phys(&address_space_memory, ch->lli + 4);
210 ch->ctrl = ldl_le_phys(&address_space_memory, ch->lli + 12);
211 ch->lli = ldl_le_phys(&address_space_memory, ch->lli + 8);
213 ch->conf &= ~PL080_CCONF_E;
215 if (ch->ctrl & PL080_CCTRL_I) {
226 static uint64_t pl080_read(void *opaque, hwaddr offset,
229 PL080State *s = (PL080State *)opaque;
233 if (offset >= 0xfe0 && offset < 0x1000) {
234 if (s->nchannels == 8) {
235 return pl080_id[(offset - 0xfe0) >> 2];
237 return pl081_id[(offset - 0xfe0) >> 2];
240 if (offset >= 0x100 && offset < 0x200) {
241 i = (offset & 0xe0) >> 5;
242 if (i >= s->nchannels)
244 switch (offset >> 2) {
245 case 0: /* SrcAddr */
246 return s->chan[i].src;
247 case 1: /* DestAddr */
248 return s->chan[i].dest;
250 return s->chan[i].lli;
251 case 3: /* Control */
252 return s->chan[i].ctrl;
253 case 4: /* Configuration */
254 return s->chan[i].conf;
259 switch (offset >> 2) {
260 case 0: /* IntStatus */
261 return (s->tc_int & s->tc_mask) | (s->err_int & s->err_mask);
262 case 1: /* IntTCStatus */
263 return (s->tc_int & s->tc_mask);
264 case 3: /* IntErrorStatus */
265 return (s->err_int & s->err_mask);
266 case 5: /* RawIntTCStatus */
268 case 6: /* RawIntErrorStatus */
270 case 7: /* EnbldChns */
272 for (i = 0; i < s->nchannels; i++) {
273 if (s->chan[i].conf & PL080_CCONF_E)
277 case 8: /* SoftBReq */
278 case 9: /* SoftSReq */
279 case 10: /* SoftLBReq */
280 case 11: /* SoftLSReq */
281 /* ??? Implement these. */
283 case 12: /* Configuration */
289 qemu_log_mask(LOG_GUEST_ERROR,
290 "pl080_read: Bad offset %x\n", (int)offset);
295 static void pl080_write(void *opaque, hwaddr offset,
296 uint64_t value, unsigned size)
298 PL080State *s = (PL080State *)opaque;
301 if (offset >= 0x100 && offset < 0x200) {
302 i = (offset & 0xe0) >> 5;
303 if (i >= s->nchannels)
305 switch (offset >> 2) {
306 case 0: /* SrcAddr */
307 s->chan[i].src = value;
309 case 1: /* DestAddr */
310 s->chan[i].dest = value;
313 s->chan[i].lli = value;
315 case 3: /* Control */
316 s->chan[i].ctrl = value;
318 case 4: /* Configuration */
319 s->chan[i].conf = value;
324 switch (offset >> 2) {
325 case 2: /* IntTCClear */
328 case 4: /* IntErrorClear */
329 s->err_int &= ~value;
331 case 8: /* SoftBReq */
332 case 9: /* SoftSReq */
333 case 10: /* SoftLBReq */
334 case 11: /* SoftLSReq */
335 /* ??? Implement these. */
336 qemu_log_mask(LOG_UNIMP, "pl080_write: Soft DMA not implemented\n");
338 case 12: /* Configuration */
340 if (s->conf & (PL080_CONF_M1 | PL080_CONF_M1)) {
341 qemu_log_mask(LOG_UNIMP,
342 "pl080_write: Big-endian DMA not implemented\n");
351 qemu_log_mask(LOG_GUEST_ERROR,
352 "pl080_write: Bad offset %x\n", (int)offset);
357 static const MemoryRegionOps pl080_ops = {
359 .write = pl080_write,
360 .endianness = DEVICE_NATIVE_ENDIAN,
363 static void pl080_init(Object *obj)
365 SysBusDevice *sbd = SYS_BUS_DEVICE(obj);
366 PL080State *s = PL080(obj);
368 memory_region_init_io(&s->iomem, OBJECT(s), &pl080_ops, s, "pl080", 0x1000);
369 sysbus_init_mmio(sbd, &s->iomem);
370 sysbus_init_irq(sbd, &s->irq);
374 static void pl081_init(Object *obj)
376 PL080State *s = PL080(obj);
381 static void pl080_class_init(ObjectClass *oc, void *data)
383 DeviceClass *dc = DEVICE_CLASS(oc);
385 dc->vmsd = &vmstate_pl080;
388 static const TypeInfo pl080_info = {
390 .parent = TYPE_SYS_BUS_DEVICE,
391 .instance_size = sizeof(PL080State),
392 .instance_init = pl080_init,
393 .class_init = pl080_class_init,
396 static const TypeInfo pl081_info = {
398 .parent = TYPE_PL080,
399 .instance_init = pl081_init,
402 /* The PL080 and PL081 are the same except for the number of channels
403 they implement (8 and 2 respectively). */
404 static void pl080_register_types(void)
406 type_register_static(&pl080_info);
407 type_register_static(&pl081_info);
410 type_init(pl080_register_types)