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
= address_space_ldl_le(&address_space_memory
,
210 MEMTXATTRS_UNSPECIFIED
,
212 ch
->dest
= address_space_ldl_le(&address_space_memory
,
214 MEMTXATTRS_UNSPECIFIED
,
216 ch
->ctrl
= address_space_ldl_le(&address_space_memory
,
218 MEMTXATTRS_UNSPECIFIED
,
220 ch
->lli
= address_space_ldl_le(&address_space_memory
,
222 MEMTXATTRS_UNSPECIFIED
,
225 ch
->conf
&= ~PL080_CCONF_E
;
227 if (ch
->ctrl
& PL080_CCTRL_I
) {
238 static uint64_t pl080_read(void *opaque
, hwaddr offset
,
241 PL080State
*s
= (PL080State
*)opaque
;
245 if (offset
>= 0xfe0 && offset
< 0x1000) {
246 if (s
->nchannels
== 8) {
247 return pl080_id
[(offset
- 0xfe0) >> 2];
249 return pl081_id
[(offset
- 0xfe0) >> 2];
252 if (offset
>= 0x100 && offset
< 0x200) {
253 i
= (offset
& 0xe0) >> 5;
254 if (i
>= s
->nchannels
)
256 switch (offset
>> 2) {
257 case 0: /* SrcAddr */
258 return s
->chan
[i
].src
;
259 case 1: /* DestAddr */
260 return s
->chan
[i
].dest
;
262 return s
->chan
[i
].lli
;
263 case 3: /* Control */
264 return s
->chan
[i
].ctrl
;
265 case 4: /* Configuration */
266 return s
->chan
[i
].conf
;
271 switch (offset
>> 2) {
272 case 0: /* IntStatus */
273 return (s
->tc_int
& s
->tc_mask
) | (s
->err_int
& s
->err_mask
);
274 case 1: /* IntTCStatus */
275 return (s
->tc_int
& s
->tc_mask
);
276 case 3: /* IntErrorStatus */
277 return (s
->err_int
& s
->err_mask
);
278 case 5: /* RawIntTCStatus */
280 case 6: /* RawIntErrorStatus */
282 case 7: /* EnbldChns */
284 for (i
= 0; i
< s
->nchannels
; i
++) {
285 if (s
->chan
[i
].conf
& PL080_CCONF_E
)
289 case 8: /* SoftBReq */
290 case 9: /* SoftSReq */
291 case 10: /* SoftLBReq */
292 case 11: /* SoftLSReq */
293 /* ??? Implement these. */
295 case 12: /* Configuration */
301 qemu_log_mask(LOG_GUEST_ERROR
,
302 "pl080_read: Bad offset %x\n", (int)offset
);
307 static void pl080_write(void *opaque
, hwaddr offset
,
308 uint64_t value
, unsigned size
)
310 PL080State
*s
= (PL080State
*)opaque
;
313 if (offset
>= 0x100 && offset
< 0x200) {
314 i
= (offset
& 0xe0) >> 5;
315 if (i
>= s
->nchannels
)
317 switch (offset
>> 2) {
318 case 0: /* SrcAddr */
319 s
->chan
[i
].src
= value
;
321 case 1: /* DestAddr */
322 s
->chan
[i
].dest
= value
;
325 s
->chan
[i
].lli
= value
;
327 case 3: /* Control */
328 s
->chan
[i
].ctrl
= value
;
330 case 4: /* Configuration */
331 s
->chan
[i
].conf
= value
;
336 switch (offset
>> 2) {
337 case 2: /* IntTCClear */
340 case 4: /* IntErrorClear */
341 s
->err_int
&= ~value
;
343 case 8: /* SoftBReq */
344 case 9: /* SoftSReq */
345 case 10: /* SoftLBReq */
346 case 11: /* SoftLSReq */
347 /* ??? Implement these. */
348 qemu_log_mask(LOG_UNIMP
, "pl080_write: Soft DMA not implemented\n");
350 case 12: /* Configuration */
352 if (s
->conf
& (PL080_CONF_M1
| PL080_CONF_M1
)) {
353 qemu_log_mask(LOG_UNIMP
,
354 "pl080_write: Big-endian DMA not implemented\n");
363 qemu_log_mask(LOG_GUEST_ERROR
,
364 "pl080_write: Bad offset %x\n", (int)offset
);
369 static const MemoryRegionOps pl080_ops
= {
371 .write
= pl080_write
,
372 .endianness
= DEVICE_NATIVE_ENDIAN
,
375 static void pl080_init(Object
*obj
)
377 SysBusDevice
*sbd
= SYS_BUS_DEVICE(obj
);
378 PL080State
*s
= PL080(obj
);
380 memory_region_init_io(&s
->iomem
, OBJECT(s
), &pl080_ops
, s
, "pl080", 0x1000);
381 sysbus_init_mmio(sbd
, &s
->iomem
);
382 sysbus_init_irq(sbd
, &s
->irq
);
386 static void pl081_init(Object
*obj
)
388 PL080State
*s
= PL080(obj
);
393 static void pl080_class_init(ObjectClass
*oc
, void *data
)
395 DeviceClass
*dc
= DEVICE_CLASS(oc
);
397 dc
->vmsd
= &vmstate_pl080
;
400 static const TypeInfo pl080_info
= {
402 .parent
= TYPE_SYS_BUS_DEVICE
,
403 .instance_size
= sizeof(PL080State
),
404 .instance_init
= pl080_init
,
405 .class_init
= pl080_class_init
,
408 static const TypeInfo pl081_info
= {
410 .parent
= TYPE_PL080
,
411 .instance_init
= pl081_init
,
414 /* The PL080 and PL081 are the same except for the number of channels
415 they implement (8 and 2 respectively). */
416 static void pl080_register_types(void)
418 type_register_static(&pl080_info
);
419 type_register_static(&pl081_info
);
422 type_init(pl080_register_types
)