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"
14 #define PL080_MAX_CHANNELS 8
15 #define PL080_CONF_E 0x1
16 #define PL080_CONF_M1 0x2
17 #define PL080_CONF_M2 0x4
19 #define PL080_CCONF_H 0x40000
20 #define PL080_CCONF_A 0x20000
21 #define PL080_CCONF_L 0x10000
22 #define PL080_CCONF_ITC 0x08000
23 #define PL080_CCONF_IE 0x04000
24 #define PL080_CCONF_E 0x00001
26 #define PL080_CCTRL_I 0x80000000
27 #define PL080_CCTRL_DI 0x08000000
28 #define PL080_CCTRL_SI 0x04000000
29 #define PL080_CCTRL_D 0x02000000
30 #define PL080_CCTRL_S 0x01000000
40 #define TYPE_PL080 "pl080"
41 #define PL080(obj) OBJECT_CHECK(PL080State, (obj), TYPE_PL080)
43 typedef struct PL080State
{
44 SysBusDevice parent_obj
;
55 pl080_channel chan
[PL080_MAX_CHANNELS
];
57 /* Flag to avoid recursive DMA invocations. */
62 static const VMStateDescription vmstate_pl080_channel
= {
63 .name
= "pl080_channel",
65 .minimum_version_id
= 1,
66 .fields
= (VMStateField
[]) {
67 VMSTATE_UINT32(src
, pl080_channel
),
68 VMSTATE_UINT32(dest
, pl080_channel
),
69 VMSTATE_UINT32(lli
, pl080_channel
),
70 VMSTATE_UINT32(ctrl
, pl080_channel
),
71 VMSTATE_UINT32(conf
, pl080_channel
),
76 static const VMStateDescription vmstate_pl080
= {
79 .minimum_version_id
= 1,
80 .fields
= (VMStateField
[]) {
81 VMSTATE_UINT8(tc_int
, PL080State
),
82 VMSTATE_UINT8(tc_mask
, PL080State
),
83 VMSTATE_UINT8(err_int
, PL080State
),
84 VMSTATE_UINT8(err_mask
, PL080State
),
85 VMSTATE_UINT32(conf
, PL080State
),
86 VMSTATE_UINT32(sync
, PL080State
),
87 VMSTATE_UINT32(req_single
, PL080State
),
88 VMSTATE_UINT32(req_burst
, PL080State
),
89 VMSTATE_UINT8(tc_int
, PL080State
),
90 VMSTATE_UINT8(tc_int
, PL080State
),
91 VMSTATE_UINT8(tc_int
, PL080State
),
92 VMSTATE_STRUCT_ARRAY(chan
, PL080State
, PL080_MAX_CHANNELS
,
93 1, vmstate_pl080_channel
, pl080_channel
),
94 VMSTATE_INT32(running
, PL080State
),
99 static const unsigned char pl080_id
[] =
100 { 0x80, 0x10, 0x04, 0x0a, 0x0d, 0xf0, 0x05, 0xb1 };
102 static const unsigned char pl081_id
[] =
103 { 0x81, 0x10, 0x04, 0x0a, 0x0d, 0xf0, 0x05, 0xb1 };
105 static void pl080_update(PL080State
*s
)
107 if ((s
->tc_int
& s
->tc_mask
)
108 || (s
->err_int
& s
->err_mask
))
109 qemu_irq_raise(s
->irq
);
111 qemu_irq_lower(s
->irq
);
114 static void pl080_run(PL080State
*s
)
130 for (c
= 0; c
< s
->nchannels
; c
++) {
131 if (s
->chan
[c
].conf
& PL080_CCONF_ITC
)
132 s
->tc_mask
|= 1 << c
;
133 if (s
->chan
[c
].conf
& PL080_CCONF_IE
)
134 s
->err_mask
|= 1 << c
;
137 if ((s
->conf
& PL080_CONF_E
) == 0)
140 hw_error("DMA active\n");
141 /* If we are already in the middle of a DMA operation then indicate that
142 there may be new DMA requests and return immediately. */
149 for (c
= 0; c
< s
->nchannels
; c
++) {
152 /* Test if thiws channel has any pending DMA requests. */
153 if ((ch
->conf
& (PL080_CCONF_H
| PL080_CCONF_E
))
156 flow
= (ch
->conf
>> 11) & 7;
159 "pl080_run: Peripheral flow control not implemented\n");
161 src_id
= (ch
->conf
>> 1) & 0x1f;
162 dest_id
= (ch
->conf
>> 6) & 0x1f;
163 size
= ch
->ctrl
& 0xfff;
164 req
= s
->req_single
| s
->req_burst
;
169 if ((req
& (1u << dest_id
)) == 0)
173 if ((req
& (1u << src_id
)) == 0)
177 if ((req
& (1u << src_id
)) == 0
178 || (req
& (1u << dest_id
)) == 0)
185 /* Transfer one element. */
186 /* ??? Should transfer multiple elements for a burst request. */
187 /* ??? Unclear what the proper behavior is when source and
188 destination widths are different. */
189 swidth
= 1 << ((ch
->ctrl
>> 18) & 7);
190 dwidth
= 1 << ((ch
->ctrl
>> 21) & 7);
191 for (n
= 0; n
< dwidth
; n
+= swidth
) {
192 cpu_physical_memory_read(ch
->src
, buff
+ n
, swidth
);
193 if (ch
->ctrl
& PL080_CCTRL_SI
)
196 xsize
= (dwidth
< swidth
) ? swidth
: dwidth
;
197 /* ??? This may pad the value incorrectly for dwidth < 32. */
198 for (n
= 0; n
< xsize
; n
+= dwidth
) {
199 cpu_physical_memory_write(ch
->dest
+ n
, buff
+ n
, dwidth
);
200 if (ch
->ctrl
& PL080_CCTRL_DI
)
205 ch
->ctrl
= (ch
->ctrl
& 0xfffff000) | size
;
207 /* Transfer complete. */
209 ch
->src
= address_space_ldl_le(&address_space_memory
,
211 MEMTXATTRS_UNSPECIFIED
,
213 ch
->dest
= address_space_ldl_le(&address_space_memory
,
215 MEMTXATTRS_UNSPECIFIED
,
217 ch
->ctrl
= address_space_ldl_le(&address_space_memory
,
219 MEMTXATTRS_UNSPECIFIED
,
221 ch
->lli
= address_space_ldl_le(&address_space_memory
,
223 MEMTXATTRS_UNSPECIFIED
,
226 ch
->conf
&= ~PL080_CCONF_E
;
228 if (ch
->ctrl
& PL080_CCTRL_I
) {
239 static uint64_t pl080_read(void *opaque
, hwaddr offset
,
242 PL080State
*s
= (PL080State
*)opaque
;
246 if (offset
>= 0xfe0 && offset
< 0x1000) {
247 if (s
->nchannels
== 8) {
248 return pl080_id
[(offset
- 0xfe0) >> 2];
250 return pl081_id
[(offset
- 0xfe0) >> 2];
253 if (offset
>= 0x100 && offset
< 0x200) {
254 i
= (offset
& 0xe0) >> 5;
255 if (i
>= s
->nchannels
)
257 switch (offset
>> 2) {
258 case 0: /* SrcAddr */
259 return s
->chan
[i
].src
;
260 case 1: /* DestAddr */
261 return s
->chan
[i
].dest
;
263 return s
->chan
[i
].lli
;
264 case 3: /* Control */
265 return s
->chan
[i
].ctrl
;
266 case 4: /* Configuration */
267 return s
->chan
[i
].conf
;
272 switch (offset
>> 2) {
273 case 0: /* IntStatus */
274 return (s
->tc_int
& s
->tc_mask
) | (s
->err_int
& s
->err_mask
);
275 case 1: /* IntTCStatus */
276 return (s
->tc_int
& s
->tc_mask
);
277 case 3: /* IntErrorStatus */
278 return (s
->err_int
& s
->err_mask
);
279 case 5: /* RawIntTCStatus */
281 case 6: /* RawIntErrorStatus */
283 case 7: /* EnbldChns */
285 for (i
= 0; i
< s
->nchannels
; i
++) {
286 if (s
->chan
[i
].conf
& PL080_CCONF_E
)
290 case 8: /* SoftBReq */
291 case 9: /* SoftSReq */
292 case 10: /* SoftLBReq */
293 case 11: /* SoftLSReq */
294 /* ??? Implement these. */
296 case 12: /* Configuration */
302 qemu_log_mask(LOG_GUEST_ERROR
,
303 "pl080_read: Bad offset %x\n", (int)offset
);
308 static void pl080_write(void *opaque
, hwaddr offset
,
309 uint64_t value
, unsigned size
)
311 PL080State
*s
= (PL080State
*)opaque
;
314 if (offset
>= 0x100 && offset
< 0x200) {
315 i
= (offset
& 0xe0) >> 5;
316 if (i
>= s
->nchannels
)
318 switch (offset
>> 2) {
319 case 0: /* SrcAddr */
320 s
->chan
[i
].src
= value
;
322 case 1: /* DestAddr */
323 s
->chan
[i
].dest
= value
;
326 s
->chan
[i
].lli
= value
;
328 case 3: /* Control */
329 s
->chan
[i
].ctrl
= value
;
331 case 4: /* Configuration */
332 s
->chan
[i
].conf
= value
;
337 switch (offset
>> 2) {
338 case 2: /* IntTCClear */
341 case 4: /* IntErrorClear */
342 s
->err_int
&= ~value
;
344 case 8: /* SoftBReq */
345 case 9: /* SoftSReq */
346 case 10: /* SoftLBReq */
347 case 11: /* SoftLSReq */
348 /* ??? Implement these. */
349 qemu_log_mask(LOG_UNIMP
, "pl080_write: Soft DMA not implemented\n");
351 case 12: /* Configuration */
353 if (s
->conf
& (PL080_CONF_M1
| PL080_CONF_M1
)) {
354 qemu_log_mask(LOG_UNIMP
,
355 "pl080_write: Big-endian DMA not implemented\n");
364 qemu_log_mask(LOG_GUEST_ERROR
,
365 "pl080_write: Bad offset %x\n", (int)offset
);
370 static const MemoryRegionOps pl080_ops
= {
372 .write
= pl080_write
,
373 .endianness
= DEVICE_NATIVE_ENDIAN
,
376 static void pl080_init(Object
*obj
)
378 SysBusDevice
*sbd
= SYS_BUS_DEVICE(obj
);
379 PL080State
*s
= PL080(obj
);
381 memory_region_init_io(&s
->iomem
, OBJECT(s
), &pl080_ops
, s
, "pl080", 0x1000);
382 sysbus_init_mmio(sbd
, &s
->iomem
);
383 sysbus_init_irq(sbd
, &s
->irq
);
387 static void pl081_init(Object
*obj
)
389 PL080State
*s
= PL080(obj
);
394 static void pl080_class_init(ObjectClass
*oc
, void *data
)
396 DeviceClass
*dc
= DEVICE_CLASS(oc
);
398 dc
->vmsd
= &vmstate_pl080
;
401 static const TypeInfo pl080_info
= {
403 .parent
= TYPE_SYS_BUS_DEVICE
,
404 .instance_size
= sizeof(PL080State
),
405 .instance_init
= pl080_init
,
406 .class_init
= pl080_class_init
,
409 static const TypeInfo pl081_info
= {
411 .parent
= TYPE_PL080
,
412 .instance_init
= pl081_init
,
415 /* The PL080 and PL081 are the same except for the number of channels
416 they implement (8 and 2 respectively). */
417 static void pl080_register_types(void)
419 type_register_static(&pl080_info
);
420 type_register_static(&pl081_info
);
423 type_init(pl080_register_types
)