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 "qemu/osdep.h"
11 #include "hw/sysbus.h"
12 #include "exec/address-spaces.h"
15 #define PL080_MAX_CHANNELS 8
16 #define PL080_CONF_E 0x1
17 #define PL080_CONF_M1 0x2
18 #define PL080_CONF_M2 0x4
20 #define PL080_CCONF_H 0x40000
21 #define PL080_CCONF_A 0x20000
22 #define PL080_CCONF_L 0x10000
23 #define PL080_CCONF_ITC 0x08000
24 #define PL080_CCONF_IE 0x04000
25 #define PL080_CCONF_E 0x00001
27 #define PL080_CCTRL_I 0x80000000
28 #define PL080_CCTRL_DI 0x08000000
29 #define PL080_CCTRL_SI 0x04000000
30 #define PL080_CCTRL_D 0x02000000
31 #define PL080_CCTRL_S 0x01000000
41 #define TYPE_PL080 "pl080"
42 #define PL080(obj) OBJECT_CHECK(PL080State, (obj), TYPE_PL080)
44 typedef struct PL080State
{
45 SysBusDevice parent_obj
;
56 pl080_channel chan
[PL080_MAX_CHANNELS
];
58 /* Flag to avoid recursive DMA invocations. */
63 static const VMStateDescription vmstate_pl080_channel
= {
64 .name
= "pl080_channel",
66 .minimum_version_id
= 1,
67 .fields
= (VMStateField
[]) {
68 VMSTATE_UINT32(src
, pl080_channel
),
69 VMSTATE_UINT32(dest
, pl080_channel
),
70 VMSTATE_UINT32(lli
, pl080_channel
),
71 VMSTATE_UINT32(ctrl
, pl080_channel
),
72 VMSTATE_UINT32(conf
, pl080_channel
),
77 static const VMStateDescription vmstate_pl080
= {
80 .minimum_version_id
= 1,
81 .fields
= (VMStateField
[]) {
82 VMSTATE_UINT8(tc_int
, PL080State
),
83 VMSTATE_UINT8(tc_mask
, PL080State
),
84 VMSTATE_UINT8(err_int
, PL080State
),
85 VMSTATE_UINT8(err_mask
, PL080State
),
86 VMSTATE_UINT32(conf
, PL080State
),
87 VMSTATE_UINT32(sync
, PL080State
),
88 VMSTATE_UINT32(req_single
, PL080State
),
89 VMSTATE_UINT32(req_burst
, PL080State
),
90 VMSTATE_UINT8(tc_int
, PL080State
),
91 VMSTATE_UINT8(tc_int
, PL080State
),
92 VMSTATE_UINT8(tc_int
, PL080State
),
93 VMSTATE_STRUCT_ARRAY(chan
, PL080State
, PL080_MAX_CHANNELS
,
94 1, vmstate_pl080_channel
, pl080_channel
),
95 VMSTATE_INT32(running
, PL080State
),
100 static const unsigned char pl080_id
[] =
101 { 0x80, 0x10, 0x04, 0x0a, 0x0d, 0xf0, 0x05, 0xb1 };
103 static const unsigned char pl081_id
[] =
104 { 0x81, 0x10, 0x04, 0x0a, 0x0d, 0xf0, 0x05, 0xb1 };
106 static void pl080_update(PL080State
*s
)
108 if ((s
->tc_int
& s
->tc_mask
)
109 || (s
->err_int
& s
->err_mask
))
110 qemu_irq_raise(s
->irq
);
112 qemu_irq_lower(s
->irq
);
115 static void pl080_run(PL080State
*s
)
131 for (c
= 0; c
< s
->nchannels
; c
++) {
132 if (s
->chan
[c
].conf
& PL080_CCONF_ITC
)
133 s
->tc_mask
|= 1 << c
;
134 if (s
->chan
[c
].conf
& PL080_CCONF_IE
)
135 s
->err_mask
|= 1 << c
;
138 if ((s
->conf
& PL080_CONF_E
) == 0)
141 hw_error("DMA active\n");
142 /* If we are already in the middle of a DMA operation then indicate that
143 there may be new DMA requests and return immediately. */
150 for (c
= 0; c
< s
->nchannels
; c
++) {
153 /* Test if thiws channel has any pending DMA requests. */
154 if ((ch
->conf
& (PL080_CCONF_H
| PL080_CCONF_E
))
157 flow
= (ch
->conf
>> 11) & 7;
160 "pl080_run: Peripheral flow control not implemented\n");
162 src_id
= (ch
->conf
>> 1) & 0x1f;
163 dest_id
= (ch
->conf
>> 6) & 0x1f;
164 size
= ch
->ctrl
& 0xfff;
165 req
= s
->req_single
| s
->req_burst
;
170 if ((req
& (1u << dest_id
)) == 0)
174 if ((req
& (1u << src_id
)) == 0)
178 if ((req
& (1u << src_id
)) == 0
179 || (req
& (1u << dest_id
)) == 0)
186 /* Transfer one element. */
187 /* ??? Should transfer multiple elements for a burst request. */
188 /* ??? Unclear what the proper behavior is when source and
189 destination widths are different. */
190 swidth
= 1 << ((ch
->ctrl
>> 18) & 7);
191 dwidth
= 1 << ((ch
->ctrl
>> 21) & 7);
192 for (n
= 0; n
< dwidth
; n
+= swidth
) {
193 cpu_physical_memory_read(ch
->src
, buff
+ n
, swidth
);
194 if (ch
->ctrl
& PL080_CCTRL_SI
)
197 xsize
= (dwidth
< swidth
) ? swidth
: dwidth
;
198 /* ??? This may pad the value incorrectly for dwidth < 32. */
199 for (n
= 0; n
< xsize
; n
+= dwidth
) {
200 cpu_physical_memory_write(ch
->dest
+ n
, buff
+ n
, dwidth
);
201 if (ch
->ctrl
& PL080_CCTRL_DI
)
206 ch
->ctrl
= (ch
->ctrl
& 0xfffff000) | size
;
208 /* Transfer complete. */
210 ch
->src
= address_space_ldl_le(&address_space_memory
,
212 MEMTXATTRS_UNSPECIFIED
,
214 ch
->dest
= address_space_ldl_le(&address_space_memory
,
216 MEMTXATTRS_UNSPECIFIED
,
218 ch
->ctrl
= address_space_ldl_le(&address_space_memory
,
220 MEMTXATTRS_UNSPECIFIED
,
222 ch
->lli
= address_space_ldl_le(&address_space_memory
,
224 MEMTXATTRS_UNSPECIFIED
,
227 ch
->conf
&= ~PL080_CCONF_E
;
229 if (ch
->ctrl
& PL080_CCTRL_I
) {
240 static uint64_t pl080_read(void *opaque
, hwaddr offset
,
243 PL080State
*s
= (PL080State
*)opaque
;
247 if (offset
>= 0xfe0 && offset
< 0x1000) {
248 if (s
->nchannels
== 8) {
249 return pl080_id
[(offset
- 0xfe0) >> 2];
251 return pl081_id
[(offset
- 0xfe0) >> 2];
254 if (offset
>= 0x100 && offset
< 0x200) {
255 i
= (offset
& 0xe0) >> 5;
256 if (i
>= s
->nchannels
)
258 switch (offset
>> 2) {
259 case 0: /* SrcAddr */
260 return s
->chan
[i
].src
;
261 case 1: /* DestAddr */
262 return s
->chan
[i
].dest
;
264 return s
->chan
[i
].lli
;
265 case 3: /* Control */
266 return s
->chan
[i
].ctrl
;
267 case 4: /* Configuration */
268 return s
->chan
[i
].conf
;
273 switch (offset
>> 2) {
274 case 0: /* IntStatus */
275 return (s
->tc_int
& s
->tc_mask
) | (s
->err_int
& s
->err_mask
);
276 case 1: /* IntTCStatus */
277 return (s
->tc_int
& s
->tc_mask
);
278 case 3: /* IntErrorStatus */
279 return (s
->err_int
& s
->err_mask
);
280 case 5: /* RawIntTCStatus */
282 case 6: /* RawIntErrorStatus */
284 case 7: /* EnbldChns */
286 for (i
= 0; i
< s
->nchannels
; i
++) {
287 if (s
->chan
[i
].conf
& PL080_CCONF_E
)
291 case 8: /* SoftBReq */
292 case 9: /* SoftSReq */
293 case 10: /* SoftLBReq */
294 case 11: /* SoftLSReq */
295 /* ??? Implement these. */
297 case 12: /* Configuration */
303 qemu_log_mask(LOG_GUEST_ERROR
,
304 "pl080_read: Bad offset %x\n", (int)offset
);
309 static void pl080_write(void *opaque
, hwaddr offset
,
310 uint64_t value
, unsigned size
)
312 PL080State
*s
= (PL080State
*)opaque
;
315 if (offset
>= 0x100 && offset
< 0x200) {
316 i
= (offset
& 0xe0) >> 5;
317 if (i
>= s
->nchannels
)
319 switch (offset
>> 2) {
320 case 0: /* SrcAddr */
321 s
->chan
[i
].src
= value
;
323 case 1: /* DestAddr */
324 s
->chan
[i
].dest
= value
;
327 s
->chan
[i
].lli
= value
;
329 case 3: /* Control */
330 s
->chan
[i
].ctrl
= value
;
332 case 4: /* Configuration */
333 s
->chan
[i
].conf
= value
;
338 switch (offset
>> 2) {
339 case 2: /* IntTCClear */
342 case 4: /* IntErrorClear */
343 s
->err_int
&= ~value
;
345 case 8: /* SoftBReq */
346 case 9: /* SoftSReq */
347 case 10: /* SoftLBReq */
348 case 11: /* SoftLSReq */
349 /* ??? Implement these. */
350 qemu_log_mask(LOG_UNIMP
, "pl080_write: Soft DMA not implemented\n");
352 case 12: /* Configuration */
354 if (s
->conf
& (PL080_CONF_M1
| PL080_CONF_M2
)) {
355 qemu_log_mask(LOG_UNIMP
,
356 "pl080_write: Big-endian DMA not implemented\n");
365 qemu_log_mask(LOG_GUEST_ERROR
,
366 "pl080_write: Bad offset %x\n", (int)offset
);
371 static const MemoryRegionOps pl080_ops
= {
373 .write
= pl080_write
,
374 .endianness
= DEVICE_NATIVE_ENDIAN
,
377 static void pl080_init(Object
*obj
)
379 SysBusDevice
*sbd
= SYS_BUS_DEVICE(obj
);
380 PL080State
*s
= PL080(obj
);
382 memory_region_init_io(&s
->iomem
, OBJECT(s
), &pl080_ops
, s
, "pl080", 0x1000);
383 sysbus_init_mmio(sbd
, &s
->iomem
);
384 sysbus_init_irq(sbd
, &s
->irq
);
388 static void pl081_init(Object
*obj
)
390 PL080State
*s
= PL080(obj
);
395 static void pl080_class_init(ObjectClass
*oc
, void *data
)
397 DeviceClass
*dc
= DEVICE_CLASS(oc
);
399 dc
->vmsd
= &vmstate_pl080
;
402 static const TypeInfo pl080_info
= {
404 .parent
= TYPE_SYS_BUS_DEVICE
,
405 .instance_size
= sizeof(PL080State
),
406 .instance_init
= pl080_init
,
407 .class_init
= pl080_class_init
,
410 static const TypeInfo pl081_info
= {
412 .parent
= TYPE_PL080
,
413 .instance_init
= pl081_init
,
416 /* The PL080 and PL081 are the same except for the number of channels
417 they implement (8 and 2 respectively). */
418 static void pl080_register_types(void)
420 type_register_static(&pl080_info
);
421 type_register_static(&pl081_info
);
424 type_init(pl080_register_types
)