2 * Raspberry Pi emulation (c) 2012 Gregory Estrade
3 * This code is licensed under the GNU GPLv2 and later.
6 #include "qemu/osdep.h"
7 #include "qapi/error.h"
8 #include "hw/dma/bcm2835_dma.h"
10 #include "migration/vmstate.h"
12 #include "qemu/module.h"
14 /* DMA CS Control and Status bits */
15 #define BCM2708_DMA_ACTIVE (1 << 0)
16 #define BCM2708_DMA_END (1 << 1) /* GE */
17 #define BCM2708_DMA_INT (1 << 2)
18 #define BCM2708_DMA_ISPAUSED (1 << 4) /* Pause requested or not active */
19 #define BCM2708_DMA_ISHELD (1 << 5) /* Is held by DREQ flow control */
20 #define BCM2708_DMA_ERR (1 << 8)
21 #define BCM2708_DMA_ABORT (1 << 30) /* stop current CB, go to next, WO */
22 #define BCM2708_DMA_RESET (1 << 31) /* WO, self clearing */
24 /* DMA control block "info" field bits */
25 #define BCM2708_DMA_INT_EN (1 << 0)
26 #define BCM2708_DMA_TDMODE (1 << 1)
27 #define BCM2708_DMA_WAIT_RESP (1 << 3)
28 #define BCM2708_DMA_D_INC (1 << 4)
29 #define BCM2708_DMA_D_WIDTH (1 << 5)
30 #define BCM2708_DMA_D_DREQ (1 << 6)
31 #define BCM2708_DMA_D_IGNORE (1 << 7)
32 #define BCM2708_DMA_S_INC (1 << 8)
33 #define BCM2708_DMA_S_WIDTH (1 << 9)
34 #define BCM2708_DMA_S_DREQ (1 << 10)
35 #define BCM2708_DMA_S_IGNORE (1 << 11)
37 /* Register offsets */
38 #define BCM2708_DMA_CS 0x00 /* Control and Status */
39 #define BCM2708_DMA_ADDR 0x04 /* Control block address */
40 /* the current control block appears in the following registers - read only */
41 #define BCM2708_DMA_INFO 0x08
42 #define BCM2708_DMA_SOURCE_AD 0x0c
43 #define BCM2708_DMA_DEST_AD 0x10
44 #define BCM2708_DMA_TXFR_LEN 0x14
45 #define BCM2708_DMA_STRIDE 0x18
46 #define BCM2708_DMA_NEXTCB 0x1C
47 #define BCM2708_DMA_DEBUG 0x20
49 #define BCM2708_DMA_INT_STATUS 0xfe0 /* Interrupt status of each channel */
50 #define BCM2708_DMA_ENABLE 0xff0 /* Global enable bits for each channel */
52 #define BCM2708_DMA_CS_RW_MASK 0x30ff0001 /* All RW bits in DMA_CS */
54 static void bcm2835_dma_update(BCM2835DMAState
*s
, unsigned c
)
56 BCM2835DMAChan
*ch
= &s
->chan
[c
];
57 uint32_t data
, xlen
, ylen
;
58 int16_t dst_stride
, src_stride
;
60 if (!(s
->enable
& (1 << c
))) {
64 while ((s
->enable
& (1 << c
)) && (ch
->conblk_ad
!= 0)) {
66 ch
->ti
= ldl_le_phys(&s
->dma_as
, ch
->conblk_ad
);
67 ch
->source_ad
= ldl_le_phys(&s
->dma_as
, ch
->conblk_ad
+ 4);
68 ch
->dest_ad
= ldl_le_phys(&s
->dma_as
, ch
->conblk_ad
+ 8);
69 ch
->txfr_len
= ldl_le_phys(&s
->dma_as
, ch
->conblk_ad
+ 12);
70 ch
->stride
= ldl_le_phys(&s
->dma_as
, ch
->conblk_ad
+ 16);
71 ch
->nextconbk
= ldl_le_phys(&s
->dma_as
, ch
->conblk_ad
+ 20);
73 if (ch
->ti
& BCM2708_DMA_TDMODE
) {
74 /* 2D transfer mode */
75 ylen
= (ch
->txfr_len
>> 16) & 0x3fff;
76 xlen
= ch
->txfr_len
& 0xffff;
77 dst_stride
= ch
->stride
>> 16;
78 src_stride
= ch
->stride
& 0xffff;
87 /* Normal transfer mode */
89 if (ch
->ti
& BCM2708_DMA_S_IGNORE
) {
93 data
= ldl_le_phys(&s
->dma_as
, ch
->source_ad
);
95 if (ch
->ti
& BCM2708_DMA_S_INC
) {
99 if (ch
->ti
& BCM2708_DMA_D_IGNORE
) {
102 stl_le_phys(&s
->dma_as
, ch
->dest_ad
, data
);
104 if (ch
->ti
& BCM2708_DMA_D_INC
) {
108 /* update remaining transfer length */
110 if (ch
->ti
& BCM2708_DMA_TDMODE
) {
111 ch
->txfr_len
= (ylen
<< 16) | xlen
;
118 ch
->source_ad
+= src_stride
;
119 ch
->dest_ad
+= dst_stride
;
122 ch
->cs
|= BCM2708_DMA_END
;
123 if (ch
->ti
& BCM2708_DMA_INT_EN
) {
124 ch
->cs
|= BCM2708_DMA_INT
;
125 s
->int_status
|= (1 << c
);
126 qemu_set_irq(ch
->irq
, 1);
129 /* Process next CB */
130 ch
->conblk_ad
= ch
->nextconbk
;
133 ch
->cs
&= ~BCM2708_DMA_ACTIVE
;
134 ch
->cs
|= BCM2708_DMA_ISPAUSED
;
137 static void bcm2835_dma_chan_reset(BCM2835DMAChan
*ch
)
143 static uint64_t bcm2835_dma_read(BCM2835DMAState
*s
, hwaddr offset
,
144 unsigned size
, unsigned c
)
150 assert(c
< BCM2835_DMA_NCHANS
);
158 case BCM2708_DMA_ADDR
:
161 case BCM2708_DMA_INFO
:
164 case BCM2708_DMA_SOURCE_AD
:
167 case BCM2708_DMA_DEST_AD
:
170 case BCM2708_DMA_TXFR_LEN
:
173 case BCM2708_DMA_STRIDE
:
176 case BCM2708_DMA_NEXTCB
:
179 case BCM2708_DMA_DEBUG
:
183 qemu_log_mask(LOG_GUEST_ERROR
, "%s: Bad offset %"HWADDR_PRIx
"\n",
190 static void bcm2835_dma_write(BCM2835DMAState
*s
, hwaddr offset
,
191 uint64_t value
, unsigned size
, unsigned c
)
197 assert(c
< BCM2835_DMA_NCHANS
);
204 if (value
& BCM2708_DMA_RESET
) {
205 bcm2835_dma_chan_reset(ch
);
207 if (value
& BCM2708_DMA_ABORT
) {
208 /* abort is a no-op, since we always run to completion */
210 if (value
& BCM2708_DMA_END
) {
211 ch
->cs
&= ~BCM2708_DMA_END
;
213 if (value
& BCM2708_DMA_INT
) {
214 ch
->cs
&= ~BCM2708_DMA_INT
;
215 s
->int_status
&= ~(1 << c
);
216 qemu_set_irq(ch
->irq
, 0);
218 ch
->cs
&= ~BCM2708_DMA_CS_RW_MASK
;
219 ch
->cs
|= (value
& BCM2708_DMA_CS_RW_MASK
);
220 if (!(oldcs
& BCM2708_DMA_ACTIVE
) && (ch
->cs
& BCM2708_DMA_ACTIVE
)) {
221 bcm2835_dma_update(s
, c
);
224 case BCM2708_DMA_ADDR
:
225 ch
->conblk_ad
= value
;
227 case BCM2708_DMA_DEBUG
:
231 qemu_log_mask(LOG_GUEST_ERROR
, "%s: Bad offset %"HWADDR_PRIx
"\n",
237 static uint64_t bcm2835_dma0_read(void *opaque
, hwaddr offset
, unsigned size
)
239 BCM2835DMAState
*s
= opaque
;
241 if (offset
< 0xf00) {
242 return bcm2835_dma_read(s
, (offset
& 0xff), size
, (offset
>> 8) & 0xf);
245 case BCM2708_DMA_INT_STATUS
:
246 return s
->int_status
;
247 case BCM2708_DMA_ENABLE
:
250 qemu_log_mask(LOG_GUEST_ERROR
, "%s: Bad offset %"HWADDR_PRIx
"\n",
257 static uint64_t bcm2835_dma15_read(void *opaque
, hwaddr offset
, unsigned size
)
259 return bcm2835_dma_read(opaque
, (offset
& 0xff), size
, 15);
262 static void bcm2835_dma0_write(void *opaque
, hwaddr offset
, uint64_t value
,
265 BCM2835DMAState
*s
= opaque
;
267 if (offset
< 0xf00) {
268 bcm2835_dma_write(s
, (offset
& 0xff), value
, size
, (offset
>> 8) & 0xf);
271 case BCM2708_DMA_INT_STATUS
:
273 case BCM2708_DMA_ENABLE
:
274 s
->enable
= (value
& 0xffff);
277 qemu_log_mask(LOG_GUEST_ERROR
, "%s: Bad offset %"HWADDR_PRIx
"\n",
284 static void bcm2835_dma15_write(void *opaque
, hwaddr offset
, uint64_t value
,
287 bcm2835_dma_write(opaque
, (offset
& 0xff), value
, size
, 15);
290 static const MemoryRegionOps bcm2835_dma0_ops
= {
291 .read
= bcm2835_dma0_read
,
292 .write
= bcm2835_dma0_write
,
293 .endianness
= DEVICE_NATIVE_ENDIAN
,
294 .valid
.min_access_size
= 4,
295 .valid
.max_access_size
= 4,
298 static const MemoryRegionOps bcm2835_dma15_ops
= {
299 .read
= bcm2835_dma15_read
,
300 .write
= bcm2835_dma15_write
,
301 .endianness
= DEVICE_NATIVE_ENDIAN
,
302 .valid
.min_access_size
= 4,
303 .valid
.max_access_size
= 4,
306 static const VMStateDescription vmstate_bcm2835_dma_chan
= {
307 .name
= TYPE_BCM2835_DMA
"-chan",
309 .minimum_version_id
= 1,
310 .fields
= (VMStateField
[]) {
311 VMSTATE_UINT32(cs
, BCM2835DMAChan
),
312 VMSTATE_UINT32(conblk_ad
, BCM2835DMAChan
),
313 VMSTATE_UINT32(ti
, BCM2835DMAChan
),
314 VMSTATE_UINT32(source_ad
, BCM2835DMAChan
),
315 VMSTATE_UINT32(dest_ad
, BCM2835DMAChan
),
316 VMSTATE_UINT32(txfr_len
, BCM2835DMAChan
),
317 VMSTATE_UINT32(stride
, BCM2835DMAChan
),
318 VMSTATE_UINT32(nextconbk
, BCM2835DMAChan
),
319 VMSTATE_UINT32(debug
, BCM2835DMAChan
),
320 VMSTATE_END_OF_LIST()
324 static const VMStateDescription vmstate_bcm2835_dma
= {
325 .name
= TYPE_BCM2835_DMA
,
327 .minimum_version_id
= 1,
328 .fields
= (VMStateField
[]) {
329 VMSTATE_STRUCT_ARRAY(chan
, BCM2835DMAState
, BCM2835_DMA_NCHANS
, 1,
330 vmstate_bcm2835_dma_chan
, BCM2835DMAChan
),
331 VMSTATE_UINT32(int_status
, BCM2835DMAState
),
332 VMSTATE_UINT32(enable
, BCM2835DMAState
),
333 VMSTATE_END_OF_LIST()
337 static void bcm2835_dma_init(Object
*obj
)
339 BCM2835DMAState
*s
= BCM2835_DMA(obj
);
342 /* DMA channels 0-14 occupy a contiguous block of IO memory, along
343 * with the global enable and interrupt status bits. Channel 15
344 * has the same register map, but is mapped at a discontiguous
345 * address in a separate IO block.
347 memory_region_init_io(&s
->iomem0
, OBJECT(s
), &bcm2835_dma0_ops
, s
,
348 TYPE_BCM2835_DMA
, 0x1000);
349 sysbus_init_mmio(SYS_BUS_DEVICE(s
), &s
->iomem0
);
351 memory_region_init_io(&s
->iomem15
, OBJECT(s
), &bcm2835_dma15_ops
, s
,
352 TYPE_BCM2835_DMA
"-chan15", 0x100);
353 sysbus_init_mmio(SYS_BUS_DEVICE(s
), &s
->iomem15
);
355 for (n
= 0; n
< 16; n
++) {
356 sysbus_init_irq(SYS_BUS_DEVICE(s
), &s
->chan
[n
].irq
);
360 static void bcm2835_dma_reset(DeviceState
*dev
)
362 BCM2835DMAState
*s
= BCM2835_DMA(dev
);
367 for (n
= 0; n
< BCM2835_DMA_NCHANS
; n
++) {
368 bcm2835_dma_chan_reset(&s
->chan
[n
]);
372 static void bcm2835_dma_realize(DeviceState
*dev
, Error
**errp
)
374 BCM2835DMAState
*s
= BCM2835_DMA(dev
);
378 obj
= object_property_get_link(OBJECT(dev
), "dma-mr", &err
);
380 error_setg(errp
, "%s: required dma-mr link not found: %s",
381 __func__
, error_get_pretty(err
));
385 s
->dma_mr
= MEMORY_REGION(obj
);
386 address_space_init(&s
->dma_as
, s
->dma_mr
, NULL
);
388 bcm2835_dma_reset(dev
);
391 static void bcm2835_dma_class_init(ObjectClass
*klass
, void *data
)
393 DeviceClass
*dc
= DEVICE_CLASS(klass
);
395 dc
->realize
= bcm2835_dma_realize
;
396 dc
->reset
= bcm2835_dma_reset
;
397 dc
->vmsd
= &vmstate_bcm2835_dma
;
400 static TypeInfo bcm2835_dma_info
= {
401 .name
= TYPE_BCM2835_DMA
,
402 .parent
= TYPE_SYS_BUS_DEVICE
,
403 .instance_size
= sizeof(BCM2835DMAState
),
404 .class_init
= bcm2835_dma_class_init
,
405 .instance_init
= bcm2835_dma_init
,
408 static void bcm2835_dma_register_types(void)
410 type_register_static(&bcm2835_dma_info
);
413 type_init(bcm2835_dma_register_types
)