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.
12 #define PL080_MAX_CHANNELS 8
13 #define PL080_CONF_E 0x1
14 #define PL080_CONF_M1 0x2
15 #define PL080_CONF_M2 0x4
17 #define PL080_CCONF_H 0x40000
18 #define PL080_CCONF_A 0x20000
19 #define PL080_CCONF_L 0x10000
20 #define PL080_CCONF_ITC 0x08000
21 #define PL080_CCONF_IE 0x04000
22 #define PL080_CCONF_E 0x00001
24 #define PL080_CCTRL_I 0x80000000
25 #define PL080_CCTRL_DI 0x08000000
26 #define PL080_CCTRL_SI 0x04000000
27 #define PL080_CCTRL_D 0x02000000
28 #define PL080_CCTRL_S 0x01000000
49 pl080_channel chan[PL080_MAX_CHANNELS];
51 /* Flag to avoid recursive DMA invocations. */
56 static const VMStateDescription vmstate_pl080_channel = {
57 .name = "pl080_channel",
59 .minimum_version_id = 1,
60 .fields = (VMStateField[]) {
61 VMSTATE_UINT32(src, pl080_channel),
62 VMSTATE_UINT32(dest, pl080_channel),
63 VMSTATE_UINT32(lli, pl080_channel),
64 VMSTATE_UINT32(ctrl, pl080_channel),
65 VMSTATE_UINT32(conf, pl080_channel),
70 static const VMStateDescription vmstate_pl080 = {
73 .minimum_version_id = 1,
74 .fields = (VMStateField[]) {
75 VMSTATE_UINT8(tc_int, pl080_state),
76 VMSTATE_UINT8(tc_mask, pl080_state),
77 VMSTATE_UINT8(err_int, pl080_state),
78 VMSTATE_UINT8(err_mask, pl080_state),
79 VMSTATE_UINT32(conf, pl080_state),
80 VMSTATE_UINT32(sync, pl080_state),
81 VMSTATE_UINT32(req_single, pl080_state),
82 VMSTATE_UINT32(req_burst, pl080_state),
83 VMSTATE_UINT8(tc_int, pl080_state),
84 VMSTATE_UINT8(tc_int, pl080_state),
85 VMSTATE_UINT8(tc_int, pl080_state),
86 VMSTATE_STRUCT_ARRAY(chan, pl080_state, PL080_MAX_CHANNELS,
87 1, vmstate_pl080_channel, pl080_channel),
88 VMSTATE_INT32(running, pl080_state),
93 static const unsigned char pl080_id[] =
94 { 0x80, 0x10, 0x04, 0x0a, 0x0d, 0xf0, 0x05, 0xb1 };
96 static const unsigned char pl081_id[] =
97 { 0x81, 0x10, 0x04, 0x0a, 0x0d, 0xf0, 0x05, 0xb1 };
99 static void pl080_update(pl080_state *s)
101 if ((s->tc_int & s->tc_mask)
102 || (s->err_int & s->err_mask))
103 qemu_irq_raise(s->irq);
105 qemu_irq_lower(s->irq);
108 static void pl080_run(pl080_state *s)
124 for (c = 0; c < s->nchannels; c++) {
125 if (s->chan[c].conf & PL080_CCONF_ITC)
126 s->tc_mask |= 1 << c;
127 if (s->chan[c].conf & PL080_CCONF_IE)
128 s->err_mask |= 1 << c;
131 if ((s->conf & PL080_CONF_E) == 0)
134 hw_error("DMA active\n");
135 /* If we are already in the middle of a DMA operation then indicate that
136 there may be new DMA requests and return immediately. */
143 for (c = 0; c < s->nchannels; c++) {
146 /* Test if thiws channel has any pending DMA requests. */
147 if ((ch->conf & (PL080_CCONF_H | PL080_CCONF_E))
150 flow = (ch->conf >> 11) & 7;
153 "pl080_run: Peripheral flow control not implemented\n");
155 src_id = (ch->conf >> 1) & 0x1f;
156 dest_id = (ch->conf >> 6) & 0x1f;
157 size = ch->ctrl & 0xfff;
158 req = s->req_single | s->req_burst;
163 if ((req & (1u << dest_id)) == 0)
167 if ((req & (1u << src_id)) == 0)
171 if ((req & (1u << src_id)) == 0
172 || (req & (1u << dest_id)) == 0)
179 /* Transfer one element. */
180 /* ??? Should transfer multiple elements for a burst request. */
181 /* ??? Unclear what the proper behavior is when source and
182 destination widths are different. */
183 swidth = 1 << ((ch->ctrl >> 18) & 7);
184 dwidth = 1 << ((ch->ctrl >> 21) & 7);
185 for (n = 0; n < dwidth; n+= swidth) {
186 cpu_physical_memory_read(ch->src, buff + n, swidth);
187 if (ch->ctrl & PL080_CCTRL_SI)
190 xsize = (dwidth < swidth) ? swidth : dwidth;
191 /* ??? This may pad the value incorrectly for dwidth < 32. */
192 for (n = 0; n < xsize; n += dwidth) {
193 cpu_physical_memory_write(ch->dest + n, buff + n, dwidth);
194 if (ch->ctrl & PL080_CCTRL_DI)
199 ch->ctrl = (ch->ctrl & 0xfffff000) | size;
201 /* Transfer complete. */
203 ch->src = ldl_le_phys(ch->lli);
204 ch->dest = ldl_le_phys(ch->lli + 4);
205 ch->ctrl = ldl_le_phys(ch->lli + 12);
206 ch->lli = ldl_le_phys(ch->lli + 8);
208 ch->conf &= ~PL080_CCONF_E;
210 if (ch->ctrl & PL080_CCTRL_I) {
221 static uint64_t pl080_read(void *opaque, target_phys_addr_t offset,
224 pl080_state *s = (pl080_state *)opaque;
228 if (offset >= 0xfe0 && offset < 0x1000) {
229 if (s->nchannels == 8) {
230 return pl080_id[(offset - 0xfe0) >> 2];
232 return pl081_id[(offset - 0xfe0) >> 2];
235 if (offset >= 0x100 && offset < 0x200) {
236 i = (offset & 0xe0) >> 5;
237 if (i >= s->nchannels)
239 switch (offset >> 2) {
240 case 0: /* SrcAddr */
241 return s->chan[i].src;
242 case 1: /* DestAddr */
243 return s->chan[i].dest;
245 return s->chan[i].lli;
246 case 3: /* Control */
247 return s->chan[i].ctrl;
248 case 4: /* Configuration */
249 return s->chan[i].conf;
254 switch (offset >> 2) {
255 case 0: /* IntStatus */
256 return (s->tc_int & s->tc_mask) | (s->err_int & s->err_mask);
257 case 1: /* IntTCStatus */
258 return (s->tc_int & s->tc_mask);
259 case 3: /* IntErrorStatus */
260 return (s->err_int & s->err_mask);
261 case 5: /* RawIntTCStatus */
263 case 6: /* RawIntErrorStatus */
265 case 7: /* EnbldChns */
267 for (i = 0; i < s->nchannels; i++) {
268 if (s->chan[i].conf & PL080_CCONF_E)
272 case 8: /* SoftBReq */
273 case 9: /* SoftSReq */
274 case 10: /* SoftLBReq */
275 case 11: /* SoftLSReq */
276 /* ??? Implement these. */
278 case 12: /* Configuration */
284 hw_error("pl080_read: Bad offset %x\n", (int)offset);
289 static void pl080_write(void *opaque, target_phys_addr_t offset,
290 uint64_t value, unsigned size)
292 pl080_state *s = (pl080_state *)opaque;
295 if (offset >= 0x100 && offset < 0x200) {
296 i = (offset & 0xe0) >> 5;
297 if (i >= s->nchannels)
299 switch (offset >> 2) {
300 case 0: /* SrcAddr */
301 s->chan[i].src = value;
303 case 1: /* DestAddr */
304 s->chan[i].dest = value;
307 s->chan[i].lli = value;
309 case 3: /* Control */
310 s->chan[i].ctrl = value;
312 case 4: /* Configuration */
313 s->chan[i].conf = value;
318 switch (offset >> 2) {
319 case 2: /* IntTCClear */
322 case 4: /* IntErrorClear */
323 s->err_int &= ~value;
325 case 8: /* SoftBReq */
326 case 9: /* SoftSReq */
327 case 10: /* SoftLBReq */
328 case 11: /* SoftLSReq */
329 /* ??? Implement these. */
330 hw_error("pl080_write: Soft DMA not implemented\n");
332 case 12: /* Configuration */
334 if (s->conf & (PL080_CONF_M1 | PL080_CONF_M1)) {
335 hw_error("pl080_write: Big-endian DMA not implemented\n");
344 hw_error("pl080_write: Bad offset %x\n", (int)offset);
349 static const MemoryRegionOps pl080_ops = {
351 .write = pl080_write,
352 .endianness = DEVICE_NATIVE_ENDIAN,
355 static int pl08x_init(SysBusDevice *dev, int nchannels)
357 pl080_state *s = FROM_SYSBUS(pl080_state, dev);
359 memory_region_init_io(&s->iomem, &pl080_ops, s, "pl080", 0x1000);
360 sysbus_init_mmio(dev, &s->iomem);
361 sysbus_init_irq(dev, &s->irq);
362 s->nchannels = nchannels;
366 static int pl080_init(SysBusDevice *dev)
368 return pl08x_init(dev, 8);
371 static int pl081_init(SysBusDevice *dev)
373 return pl08x_init(dev, 2);
376 static void pl080_class_init(ObjectClass *klass, void *data)
378 DeviceClass *dc = DEVICE_CLASS(klass);
379 SysBusDeviceClass *k = SYS_BUS_DEVICE_CLASS(klass);
381 k->init = pl080_init;
383 dc->vmsd = &vmstate_pl080;
386 static TypeInfo pl080_info = {
388 .parent = TYPE_SYS_BUS_DEVICE,
389 .instance_size = sizeof(pl080_state),
390 .class_init = pl080_class_init,
393 static void pl081_class_init(ObjectClass *klass, void *data)
395 DeviceClass *dc = DEVICE_CLASS(klass);
396 SysBusDeviceClass *k = SYS_BUS_DEVICE_CLASS(klass);
398 k->init = pl081_init;
400 dc->vmsd = &vmstate_pl080;
403 static TypeInfo pl081_info = {
405 .parent = TYPE_SYS_BUS_DEVICE,
406 .instance_size = sizeof(pl080_state),
407 .class_init = pl081_class_init,
410 /* The PL080 and PL081 are the same except for the number of channels
411 they implement (8 and 2 respectively). */
412 static void pl080_register_types(void)
414 type_register_static(&pl080_info);
415 type_register_static(&pl081_info);
418 type_init(pl080_register_types)