2 * Intel XScale PXA255/270 DMA controller.
4 * Copyright (c) 2006 Openedhand Ltd.
5 * Copyright (c) 2006 Thorsten Zitterell
6 * Written by Andrzej Zaborowski <balrog@zabor.org>
8 * This code is licensed under the GPL.
12 #include "hw/arm/pxa.h"
13 #include "hw/sysbus.h"
15 #define PXA255_DMA_NUM_CHANNELS 16
16 #define PXA27X_DMA_NUM_CHANNELS 32
18 #define PXA2XX_DMA_NUM_REQUESTS 75
29 #define TYPE_PXA2XX_DMA "pxa2xx-dma"
30 #define PXA2XX_DMA(obj) OBJECT_CHECK(PXA2xxDMAState, (obj), TYPE_PXA2XX_DMA)
32 typedef struct PXA2xxDMAState
{
33 SysBusDevice parent_obj
;
48 PXA2xxDMAChannel
*chan
;
50 uint8_t req
[PXA2XX_DMA_NUM_REQUESTS
];
52 /* Flag to avoid recursive DMA invocations. */
56 #define DCSR0 0x0000 /* DMA Control / Status register for Channel 0 */
57 #define DCSR31 0x007c /* DMA Control / Status register for Channel 31 */
58 #define DALGN 0x00a0 /* DMA Alignment register */
59 #define DPCSR 0x00a4 /* DMA Programmed I/O Control Status register */
60 #define DRQSR0 0x00e0 /* DMA DREQ<0> Status register */
61 #define DRQSR1 0x00e4 /* DMA DREQ<1> Status register */
62 #define DRQSR2 0x00e8 /* DMA DREQ<2> Status register */
63 #define DINT 0x00f0 /* DMA Interrupt register */
64 #define DRCMR0 0x0100 /* Request to Channel Map register 0 */
65 #define DRCMR63 0x01fc /* Request to Channel Map register 63 */
66 #define D_CH0 0x0200 /* Channel 0 Descriptor start */
67 #define DRCMR64 0x1100 /* Request to Channel Map register 64 */
68 #define DRCMR74 0x1128 /* Request to Channel Map register 74 */
70 /* Per-channel register */
77 #define DRCMR_CHLNUM 0x1f
78 #define DRCMR_MAPVLD (1 << 7)
79 #define DDADR_STOP (1 << 0)
80 #define DDADR_BREN (1 << 1)
81 #define DCMD_LEN 0x1fff
82 #define DCMD_WIDTH(x) (1 << ((((x) >> 14) & 3) - 1))
83 #define DCMD_SIZE(x) (4 << (((x) >> 16) & 3))
84 #define DCMD_FLYBYT (1 << 19)
85 #define DCMD_FLYBYS (1 << 20)
86 #define DCMD_ENDIRQEN (1 << 21)
87 #define DCMD_STARTIRQEN (1 << 22)
88 #define DCMD_CMPEN (1 << 25)
89 #define DCMD_FLOWTRG (1 << 28)
90 #define DCMD_FLOWSRC (1 << 29)
91 #define DCMD_INCTRGADDR (1 << 30)
92 #define DCMD_INCSRCADDR (1 << 31)
93 #define DCSR_BUSERRINTR (1 << 0)
94 #define DCSR_STARTINTR (1 << 1)
95 #define DCSR_ENDINTR (1 << 2)
96 #define DCSR_STOPINTR (1 << 3)
97 #define DCSR_RASINTR (1 << 4)
98 #define DCSR_REQPEND (1 << 8)
99 #define DCSR_EORINT (1 << 9)
100 #define DCSR_CMPST (1 << 10)
101 #define DCSR_MASKRUN (1 << 22)
102 #define DCSR_RASIRQEN (1 << 23)
103 #define DCSR_CLRCMPST (1 << 24)
104 #define DCSR_SETCMPST (1 << 25)
105 #define DCSR_EORSTOPEN (1 << 26)
106 #define DCSR_EORJMPEN (1 << 27)
107 #define DCSR_EORIRQEN (1 << 28)
108 #define DCSR_STOPIRQEN (1 << 29)
109 #define DCSR_NODESCFETCH (1 << 30)
110 #define DCSR_RUN (1 << 31)
112 static inline void pxa2xx_dma_update(PXA2xxDMAState
*s
, int ch
)
115 if ((s
->chan
[ch
].state
& DCSR_STOPIRQEN
) &&
116 (s
->chan
[ch
].state
& DCSR_STOPINTR
))
117 s
->stopintr
|= 1 << ch
;
119 s
->stopintr
&= ~(1 << ch
);
121 if ((s
->chan
[ch
].state
& DCSR_EORIRQEN
) &&
122 (s
->chan
[ch
].state
& DCSR_EORINT
))
123 s
->eorintr
|= 1 << ch
;
125 s
->eorintr
&= ~(1 << ch
);
127 if ((s
->chan
[ch
].state
& DCSR_RASIRQEN
) &&
128 (s
->chan
[ch
].state
& DCSR_RASINTR
))
129 s
->rasintr
|= 1 << ch
;
131 s
->rasintr
&= ~(1 << ch
);
133 if (s
->chan
[ch
].state
& DCSR_STARTINTR
)
134 s
->startintr
|= 1 << ch
;
136 s
->startintr
&= ~(1 << ch
);
138 if (s
->chan
[ch
].state
& DCSR_ENDINTR
)
139 s
->endintr
|= 1 << ch
;
141 s
->endintr
&= ~(1 << ch
);
144 if (s
->stopintr
| s
->eorintr
| s
->rasintr
| s
->startintr
| s
->endintr
)
145 qemu_irq_raise(s
->irq
);
147 qemu_irq_lower(s
->irq
);
150 static inline void pxa2xx_dma_descriptor_fetch(
151 PXA2xxDMAState
*s
, int ch
)
154 hwaddr daddr
= s
->chan
[ch
].descr
& ~0xf;
155 if ((s
->chan
[ch
].descr
& DDADR_BREN
) && (s
->chan
[ch
].state
& DCSR_CMPST
))
158 cpu_physical_memory_read(daddr
, desc
, 16);
159 s
->chan
[ch
].descr
= desc
[DDADR
];
160 s
->chan
[ch
].src
= desc
[DSADR
];
161 s
->chan
[ch
].dest
= desc
[DTADR
];
162 s
->chan
[ch
].cmd
= desc
[DCMD
];
164 if (s
->chan
[ch
].cmd
& DCMD_FLOWSRC
)
165 s
->chan
[ch
].src
&= ~3;
166 if (s
->chan
[ch
].cmd
& DCMD_FLOWTRG
)
167 s
->chan
[ch
].dest
&= ~3;
169 if (s
->chan
[ch
].cmd
& (DCMD_CMPEN
| DCMD_FLYBYS
| DCMD_FLYBYT
))
170 printf("%s: unsupported mode in channel %i\n", __FUNCTION__
, ch
);
172 if (s
->chan
[ch
].cmd
& DCMD_STARTIRQEN
)
173 s
->chan
[ch
].state
|= DCSR_STARTINTR
;
176 static void pxa2xx_dma_run(PXA2xxDMAState
*s
)
178 int c
, srcinc
, destinc
;
183 PXA2xxDMAChannel
*ch
;
190 for (c
= 0; c
< s
->channels
; c
++) {
193 while ((ch
->state
& DCSR_RUN
) && !(ch
->state
& DCSR_STOPINTR
)) {
194 /* Test for pending requests */
195 if ((ch
->cmd
& (DCMD_FLOWSRC
| DCMD_FLOWTRG
)) && !ch
->request
)
198 length
= ch
->cmd
& DCMD_LEN
;
199 size
= DCMD_SIZE(ch
->cmd
);
200 width
= DCMD_WIDTH(ch
->cmd
);
202 srcinc
= (ch
->cmd
& DCMD_INCSRCADDR
) ? width
: 0;
203 destinc
= (ch
->cmd
& DCMD_INCTRGADDR
) ? width
: 0;
206 size
= MIN(length
, size
);
208 for (n
= 0; n
< size
; n
+= width
) {
209 cpu_physical_memory_read(ch
->src
, buffer
+ n
, width
);
213 for (n
= 0; n
< size
; n
+= width
) {
214 cpu_physical_memory_write(ch
->dest
, buffer
+ n
, width
);
220 if ((ch
->cmd
& (DCMD_FLOWSRC
| DCMD_FLOWTRG
)) &&
222 ch
->state
|= DCSR_EORINT
;
223 if (ch
->state
& DCSR_EORSTOPEN
)
224 ch
->state
|= DCSR_STOPINTR
;
225 if ((ch
->state
& DCSR_EORJMPEN
) &&
226 !(ch
->state
& DCSR_NODESCFETCH
))
227 pxa2xx_dma_descriptor_fetch(s
, c
);
232 ch
->cmd
= (ch
->cmd
& ~DCMD_LEN
) | length
;
234 /* Is the transfer complete now? */
236 if (ch
->cmd
& DCMD_ENDIRQEN
)
237 ch
->state
|= DCSR_ENDINTR
;
239 if ((ch
->state
& DCSR_NODESCFETCH
) ||
240 (ch
->descr
& DDADR_STOP
) ||
241 (ch
->state
& DCSR_EORSTOPEN
)) {
242 ch
->state
|= DCSR_STOPINTR
;
243 ch
->state
&= ~DCSR_RUN
;
248 ch
->state
|= DCSR_STOPINTR
;
258 static uint64_t pxa2xx_dma_read(void *opaque
, hwaddr offset
,
261 PXA2xxDMAState
*s
= (PXA2xxDMAState
*) opaque
;
262 unsigned int channel
;
265 hw_error("%s: Bad access width\n", __FUNCTION__
);
270 case DRCMR64
... DRCMR74
:
271 offset
-= DRCMR64
- DRCMR0
- (64 << 2);
273 case DRCMR0
... DRCMR63
:
274 channel
= (offset
- DRCMR0
) >> 2;
275 return s
->req
[channel
];
282 case DCSR0
... DCSR31
:
283 channel
= offset
>> 2;
284 if (s
->chan
[channel
].request
)
285 return s
->chan
[channel
].state
| DCSR_REQPEND
;
286 return s
->chan
[channel
].state
;
289 return s
->stopintr
| s
->eorintr
| s
->rasintr
|
290 s
->startintr
| s
->endintr
;
299 if (offset
>= D_CH0
&& offset
< D_CH0
+ (s
->channels
<< 4)) {
300 channel
= (offset
- D_CH0
) >> 4;
301 switch ((offset
& 0x0f) >> 2) {
303 return s
->chan
[channel
].descr
;
305 return s
->chan
[channel
].src
;
307 return s
->chan
[channel
].dest
;
309 return s
->chan
[channel
].cmd
;
313 hw_error("%s: Bad offset 0x" TARGET_FMT_plx
"\n", __FUNCTION__
, offset
);
317 static void pxa2xx_dma_write(void *opaque
, hwaddr offset
,
318 uint64_t value
, unsigned size
)
320 PXA2xxDMAState
*s
= (PXA2xxDMAState
*) opaque
;
321 unsigned int channel
;
324 hw_error("%s: Bad access width\n", __FUNCTION__
);
329 case DRCMR64
... DRCMR74
:
330 offset
-= DRCMR64
- DRCMR0
- (64 << 2);
332 case DRCMR0
... DRCMR63
:
333 channel
= (offset
- DRCMR0
) >> 2;
335 if (value
& DRCMR_MAPVLD
)
336 if ((value
& DRCMR_CHLNUM
) > s
->channels
)
337 hw_error("%s: Bad DMA channel %i\n",
338 __FUNCTION__
, (unsigned)value
& DRCMR_CHLNUM
);
340 s
->req
[channel
] = value
;
349 case DCSR0
... DCSR31
:
350 channel
= offset
>> 2;
351 s
->chan
[channel
].state
&= 0x0000071f & ~(value
&
352 (DCSR_EORINT
| DCSR_ENDINTR
|
353 DCSR_STARTINTR
| DCSR_BUSERRINTR
));
354 s
->chan
[channel
].state
|= value
& 0xfc800000;
356 if (s
->chan
[channel
].state
& DCSR_STOPIRQEN
)
357 s
->chan
[channel
].state
&= ~DCSR_STOPINTR
;
359 if (value
& DCSR_NODESCFETCH
) {
360 /* No-descriptor-fetch mode */
361 if (value
& DCSR_RUN
) {
362 s
->chan
[channel
].state
&= ~DCSR_STOPINTR
;
366 /* Descriptor-fetch mode */
367 if (value
& DCSR_RUN
) {
368 s
->chan
[channel
].state
&= ~DCSR_STOPINTR
;
369 pxa2xx_dma_descriptor_fetch(s
, channel
);
374 /* Shouldn't matter as our DMA is synchronous. */
375 if (!(value
& (DCSR_RUN
| DCSR_MASKRUN
)))
376 s
->chan
[channel
].state
|= DCSR_STOPINTR
;
378 if (value
& DCSR_CLRCMPST
)
379 s
->chan
[channel
].state
&= ~DCSR_CMPST
;
380 if (value
& DCSR_SETCMPST
)
381 s
->chan
[channel
].state
|= DCSR_CMPST
;
383 pxa2xx_dma_update(s
, channel
);
391 s
->pio
= value
& 0x80000001;
395 if (offset
>= D_CH0
&& offset
< D_CH0
+ (s
->channels
<< 4)) {
396 channel
= (offset
- D_CH0
) >> 4;
397 switch ((offset
& 0x0f) >> 2) {
399 s
->chan
[channel
].descr
= value
;
402 s
->chan
[channel
].src
= value
;
405 s
->chan
[channel
].dest
= value
;
408 s
->chan
[channel
].cmd
= value
;
417 hw_error("%s: Bad offset " TARGET_FMT_plx
"\n", __FUNCTION__
, offset
);
421 static const MemoryRegionOps pxa2xx_dma_ops
= {
422 .read
= pxa2xx_dma_read
,
423 .write
= pxa2xx_dma_write
,
424 .endianness
= DEVICE_NATIVE_ENDIAN
,
427 static void pxa2xx_dma_request(void *opaque
, int req_num
, int on
)
429 PXA2xxDMAState
*s
= opaque
;
431 if (req_num
< 0 || req_num
>= PXA2XX_DMA_NUM_REQUESTS
)
432 hw_error("%s: Bad DMA request %i\n", __FUNCTION__
, req_num
);
434 if (!(s
->req
[req_num
] & DRCMR_MAPVLD
))
436 ch
= s
->req
[req_num
] & DRCMR_CHLNUM
;
438 if (!s
->chan
[ch
].request
&& on
)
439 s
->chan
[ch
].state
|= DCSR_RASINTR
;
441 s
->chan
[ch
].state
&= ~DCSR_RASINTR
;
442 if (s
->chan
[ch
].request
&& !on
)
443 s
->chan
[ch
].state
|= DCSR_EORINT
;
445 s
->chan
[ch
].request
= on
;
448 pxa2xx_dma_update(s
, ch
);
452 static int pxa2xx_dma_init(SysBusDevice
*sbd
)
454 DeviceState
*dev
= DEVICE(sbd
);
455 PXA2xxDMAState
*s
= PXA2XX_DMA(dev
);
458 if (s
->channels
<= 0) {
462 s
->chan
= g_malloc0(sizeof(PXA2xxDMAChannel
) * s
->channels
);
464 memset(s
->chan
, 0, sizeof(PXA2xxDMAChannel
) * s
->channels
);
465 for (i
= 0; i
< s
->channels
; i
++)
466 s
->chan
[i
].state
= DCSR_STOPINTR
;
468 memset(s
->req
, 0, sizeof(uint8_t) * PXA2XX_DMA_NUM_REQUESTS
);
470 qdev_init_gpio_in(dev
, pxa2xx_dma_request
, PXA2XX_DMA_NUM_REQUESTS
);
472 memory_region_init_io(&s
->iomem
, OBJECT(s
), &pxa2xx_dma_ops
, s
,
473 "pxa2xx.dma", 0x00010000);
474 sysbus_init_mmio(sbd
, &s
->iomem
);
475 sysbus_init_irq(sbd
, &s
->irq
);
480 DeviceState
*pxa27x_dma_init(hwaddr base
, qemu_irq irq
)
484 dev
= qdev_create(NULL
, "pxa2xx-dma");
485 qdev_prop_set_int32(dev
, "channels", PXA27X_DMA_NUM_CHANNELS
);
486 qdev_init_nofail(dev
);
488 sysbus_mmio_map(SYS_BUS_DEVICE(dev
), 0, base
);
489 sysbus_connect_irq(SYS_BUS_DEVICE(dev
), 0, irq
);
494 DeviceState
*pxa255_dma_init(hwaddr base
, qemu_irq irq
)
498 dev
= qdev_create(NULL
, "pxa2xx-dma");
499 qdev_prop_set_int32(dev
, "channels", PXA27X_DMA_NUM_CHANNELS
);
500 qdev_init_nofail(dev
);
502 sysbus_mmio_map(SYS_BUS_DEVICE(dev
), 0, base
);
503 sysbus_connect_irq(SYS_BUS_DEVICE(dev
), 0, irq
);
508 static bool is_version_0(void *opaque
, int version_id
)
510 return version_id
== 0;
513 static VMStateDescription vmstate_pxa2xx_dma_chan
= {
514 .name
= "pxa2xx_dma_chan",
516 .minimum_version_id
= 1,
517 .fields
= (VMStateField
[]) {
518 VMSTATE_UINT32(descr
, PXA2xxDMAChannel
),
519 VMSTATE_UINT32(src
, PXA2xxDMAChannel
),
520 VMSTATE_UINT32(dest
, PXA2xxDMAChannel
),
521 VMSTATE_UINT32(cmd
, PXA2xxDMAChannel
),
522 VMSTATE_UINT32(state
, PXA2xxDMAChannel
),
523 VMSTATE_INT32(request
, PXA2xxDMAChannel
),
524 VMSTATE_END_OF_LIST(),
528 static VMStateDescription vmstate_pxa2xx_dma
= {
529 .name
= "pxa2xx_dma",
531 .minimum_version_id
= 0,
532 .fields
= (VMStateField
[]) {
533 VMSTATE_UNUSED_TEST(is_version_0
, 4),
534 VMSTATE_UINT32(stopintr
, PXA2xxDMAState
),
535 VMSTATE_UINT32(eorintr
, PXA2xxDMAState
),
536 VMSTATE_UINT32(rasintr
, PXA2xxDMAState
),
537 VMSTATE_UINT32(startintr
, PXA2xxDMAState
),
538 VMSTATE_UINT32(endintr
, PXA2xxDMAState
),
539 VMSTATE_UINT32(align
, PXA2xxDMAState
),
540 VMSTATE_UINT32(pio
, PXA2xxDMAState
),
541 VMSTATE_BUFFER(req
, PXA2xxDMAState
),
542 VMSTATE_STRUCT_VARRAY_POINTER_INT32(chan
, PXA2xxDMAState
, channels
,
543 vmstate_pxa2xx_dma_chan
, PXA2xxDMAChannel
),
544 VMSTATE_END_OF_LIST(),
548 static Property pxa2xx_dma_properties
[] = {
549 DEFINE_PROP_INT32("channels", PXA2xxDMAState
, channels
, -1),
550 DEFINE_PROP_END_OF_LIST(),
553 static void pxa2xx_dma_class_init(ObjectClass
*klass
, void *data
)
555 DeviceClass
*dc
= DEVICE_CLASS(klass
);
556 SysBusDeviceClass
*k
= SYS_BUS_DEVICE_CLASS(klass
);
558 k
->init
= pxa2xx_dma_init
;
559 dc
->desc
= "PXA2xx DMA controller";
560 dc
->vmsd
= &vmstate_pxa2xx_dma
;
561 dc
->props
= pxa2xx_dma_properties
;
564 static const TypeInfo pxa2xx_dma_info
= {
565 .name
= TYPE_PXA2XX_DMA
,
566 .parent
= TYPE_SYS_BUS_DEVICE
,
567 .instance_size
= sizeof(PXA2xxDMAState
),
568 .class_init
= pxa2xx_dma_class_init
,
571 static void pxa2xx_dma_register_types(void)
573 type_register_static(&pxa2xx_dma_info
);
576 type_init(pxa2xx_dma_register_types
)