2 * Raspberry Pi emulation (c) 2012 Gregory Estrade
3 * This code is licensed under the GNU GPLv2 and later.
7 #include "exec/address-spaces.h"
10 extern AddressSpace
*bcm2835_peripheral_as
;
12 /* DMA CS Control and Status bits */
13 #define BCM2708_DMA_ACTIVE (1 << 0)
14 #define BCM2708_DMA_INT (1 << 2)
15 #define BCM2708_DMA_ISPAUSED (1 << 4) /* Pause requested or not active */
16 #define BCM2708_DMA_ISHELD (1 << 5) /* Is held by DREQ flow control */
17 #define BCM2708_DMA_ERR (1 << 8)
18 #define BCM2708_DMA_ABORT (1 << 30) /* stop current CB, go to next, WO */
19 #define BCM2708_DMA_RESET (1 << 31) /* WO, self clearing */
21 #define BCM2708_DMA_END (1 << 1) /* GE */
23 /* DMA control block "info" field bits */
24 #define BCM2708_DMA_INT_EN (1 << 0)
25 #define BCM2708_DMA_TDMODE (1 << 1)
26 #define BCM2708_DMA_WAIT_RESP (1 << 3)
27 #define BCM2708_DMA_D_INC (1 << 4)
28 #define BCM2708_DMA_D_WIDTH (1 << 5)
29 #define BCM2708_DMA_D_DREQ (1 << 6)
30 #define BCM2708_DMA_S_INC (1 << 8)
31 #define BCM2708_DMA_S_WIDTH (1 << 9)
32 #define BCM2708_DMA_S_DREQ (1 << 10)
34 #define BCM2708_DMA_BURST(x) (((x)&0xf) << 12)
35 #define BCM2708_DMA_PER_MAP(x) ((x) << 16)
36 #define BCM2708_DMA_WAITS(x) (((x)&0x1f) << 21)
38 #define BCM2708_DMA_DREQ_EMMC 11
39 #define BCM2708_DMA_DREQ_SDHOST 13
41 #define BCM2708_DMA_CS 0x00 /* Control and Status */
42 #define BCM2708_DMA_ADDR 0x04
43 /* the current control block appears in the following registers - read only */
44 #define BCM2708_DMA_INFO 0x08
45 #define BCM2708_DMA_NEXTCB 0x1C
46 #define BCM2708_DMA_DEBUG 0x20
48 #define BCM2708_DMA4_CS (BCM2708_DMA_CHAN(4)+BCM2708_DMA_CS)
49 #define BCM2708_DMA4_ADDR (BCM2708_DMA_CHAN(4)+BCM2708_DMA_ADDR)
51 #define BCM2708_DMA_TDMODE_LEN(w, h) ((h) << 16 | (w))
67 #define TYPE_BCM2835_DMA "bcm2835_dma"
68 #define BCM2835_DMA(obj) \
69 OBJECT_CHECK(Bcm2835DmaState, (obj), TYPE_BCM2835_DMA)
73 MemoryRegion iomem0_14
;
83 static void bcm2835_dma_update(Bcm2835DmaState
*s
, int c
)
85 DmaChan
*ch
= &s
->chan
[c
];
88 if (!(s
->enable
& (1 << c
))) {
92 while ((s
->enable
& (1 << c
)) && (ch
->conblk_ad
!= 0)) {
94 ch
->ti
= ldl_phys(bcm2835_peripheral_as
, ch
->conblk_ad
);
95 ch
->source_ad
= ldl_phys(bcm2835_peripheral_as
, ch
->conblk_ad
+ 4);
96 ch
->dest_ad
= ldl_phys(bcm2835_peripheral_as
, ch
->conblk_ad
+ 8);
97 ch
->txfr_len
= ldl_phys(bcm2835_peripheral_as
, ch
->conblk_ad
+ 12);
98 ch
->stride
= ldl_phys(bcm2835_peripheral_as
, ch
->conblk_ad
+ 16);
99 ch
->nextconbk
= ldl_phys(bcm2835_peripheral_as
, ch
->conblk_ad
+ 20);
101 assert(!(ch
->ti
& BCM2708_DMA_TDMODE
));
103 while (ch
->txfr_len
!= 0) {
105 if (ch
->ti
& (1 << 11)) {
108 data
= ldl_phys(bcm2835_peripheral_as
, ch
->source_ad
);
110 if (ch
->ti
& BCM2708_DMA_S_INC
) {
114 if (ch
->ti
& (1 << 7)) {
117 stl_phys(bcm2835_peripheral_as
, ch
->dest_ad
, data
);
119 if (ch
->ti
& BCM2708_DMA_D_INC
) {
124 ch
->cs
|= BCM2708_DMA_END
;
125 if (ch
->ti
& BCM2708_DMA_INT_EN
) {
126 ch
->cs
|= BCM2708_DMA_INT
;
127 s
->int_status
|= (1 << c
);
128 qemu_set_irq(ch
->irq
, 1);
131 /* Process next CB */
132 ch
->conblk_ad
= ch
->nextconbk
;
134 ch
->cs
&= ~BCM2708_DMA_ACTIVE
;
137 static uint64_t bcm2835_dma_read(Bcm2835DmaState
*s
, hwaddr offset
,
138 unsigned size
, int c
)
140 DmaChan
*ch
= &s
->chan
[c
];
174 qemu_log_mask(LOG_GUEST_ERROR
,
175 "bcm2835_dma_read: Bad offset %x\n", (int)offset
);
181 static void bcm2835_dma_write(Bcm2835DmaState
*s
, hwaddr offset
,
182 uint64_t value
, unsigned size
, int c
)
184 DmaChan
*ch
= &s
->chan
[c
];
185 uint32_t oldcs
= ch
->cs
;
191 if (value
& BCM2708_DMA_RESET
) {
192 ch
->cs
|= BCM2708_DMA_RESET
;
194 if (value
& BCM2708_DMA_ABORT
) {
195 ch
->cs
|= BCM2708_DMA_ABORT
;
197 if (value
& BCM2708_DMA_END
) {
198 ch
->cs
&= ~BCM2708_DMA_END
;
200 if (value
& BCM2708_DMA_INT
) {
201 ch
->cs
&= ~BCM2708_DMA_INT
;
202 s
->int_status
&= ~(1 << c
);
203 qemu_set_irq(ch
->irq
, 0);
205 ch
->cs
&= ~0x30ff0001;
206 ch
->cs
|= (value
& 0x30ff0001);
207 if (!(oldcs
& BCM2708_DMA_ACTIVE
) && (ch
->cs
& BCM2708_DMA_ACTIVE
)) {
208 bcm2835_dma_update(s
, c
);
212 ch
->conblk_ad
= value
;
218 ch
->source_ad
= value
;
224 ch
->txfr_len
= value
;
230 ch
->nextconbk
= value
;
236 qemu_log_mask(LOG_GUEST_ERROR
,
237 "bcm2835_dma_write: Bad offset %x\n", (int)offset
);
242 /* ==================================================================== */
244 static uint64_t bcm2835_dma0_14_read(void *opaque
, hwaddr offset
,
247 Bcm2835DmaState
*s
= (Bcm2835DmaState
*)opaque
;
248 if (offset
== 0xfe0) {
249 return s
->int_status
;
251 if (offset
== 0xff0) {
254 return bcm2835_dma_read(s
, (offset
& 0xff),
255 size
, (offset
>> 8) & 0xf);
258 static uint64_t bcm2835_dma15_read(void *opaque
, hwaddr offset
,
261 return bcm2835_dma_read((Bcm2835DmaState
*)opaque
, (offset
& 0xff),
265 static void bcm2835_dma0_14_write(void *opaque
, hwaddr offset
,
266 uint64_t value
, unsigned size
)
268 Bcm2835DmaState
*s
= (Bcm2835DmaState
*)opaque
;
269 if (offset
== 0xfe0) {
272 if (offset
== 0xff0) {
273 s
->enable
= (value
& 0xffff);
276 bcm2835_dma_write(s
, (offset
& 0xff),
277 value
, size
, (offset
>> 8) & 0xf);
280 static void bcm2835_dma15_write(void *opaque
, hwaddr offset
,
281 uint64_t value
, unsigned size
)
283 bcm2835_dma_write((Bcm2835DmaState
*)opaque
, (offset
& 0xff),
288 static const MemoryRegionOps bcm2835_dma0_14_ops
= {
289 .read
= bcm2835_dma0_14_read
,
290 .write
= bcm2835_dma0_14_write
,
291 .endianness
= DEVICE_NATIVE_ENDIAN
,
294 static const MemoryRegionOps bcm2835_dma15_ops
= {
295 .read
= bcm2835_dma15_read
,
296 .write
= bcm2835_dma15_write
,
297 .endianness
= DEVICE_NATIVE_ENDIAN
,
300 static const VMStateDescription vmstate_bcm2835_dma
= {
301 .name
= TYPE_BCM2835_DMA
,
303 .minimum_version_id
= 1,
304 .minimum_version_id_old
= 1,
305 .fields
= (VMStateField
[]) {
306 VMSTATE_END_OF_LIST()
310 static int bcm2835_dma_init(SysBusDevice
*sbd
)
313 DeviceState
*dev
= DEVICE(sbd
);
314 Bcm2835DmaState
*s
= BCM2835_DMA(dev
);
318 for (n
= 0; n
< 16; n
++) {
320 s
->chan
[n
].conblk_ad
= 0;
321 sysbus_init_irq(sbd
, &s
->chan
[n
].irq
);
324 memory_region_init_io(&s
->iomem0_14
, OBJECT(s
), &bcm2835_dma0_14_ops
, s
,
325 "bcm2835_dma0_14", 0xf00);
326 sysbus_init_mmio(sbd
, &s
->iomem0_14
);
327 memory_region_init_io(&s
->iomem15
, OBJECT(s
), &bcm2835_dma15_ops
, s
,
328 "bcm2835_dma15", 0x100);
329 sysbus_init_mmio(sbd
, &s
->iomem15
);
331 vmstate_register(dev
, -1, &vmstate_bcm2835_dma
, s
);
336 static void bcm2835_dma_class_init(ObjectClass
*klass
, void *data
)
338 SysBusDeviceClass
*sdc
= SYS_BUS_DEVICE_CLASS(klass
);
340 sdc
->init
= bcm2835_dma_init
;
343 static TypeInfo bcm2835_dma_info
= {
344 .name
= TYPE_BCM2835_DMA
,
345 .parent
= TYPE_SYS_BUS_DEVICE
,
346 .instance_size
= sizeof(Bcm2835DmaState
),
347 .class_init
= bcm2835_dma_class_init
,
350 static void bcm2835_dma_register_types(void)
352 type_register_static(&bcm2835_dma_info
);
355 type_init(bcm2835_dma_register_types
)