Statistics
| Branch: | Revision:

root / hw / mac_dbdma.c @ e824012b

History | View | Annotate | Download (21.2 kB)

1 3cbee15b j_mayer
/*
2 3cbee15b j_mayer
 * PowerMac descriptor-based DMA emulation
3 3cbee15b j_mayer
 *
4 3cbee15b j_mayer
 * Copyright (c) 2005-2007 Fabrice Bellard
5 3cbee15b j_mayer
 * Copyright (c) 2007 Jocelyn Mayer
6 28ce5ce6 aurel32
 * Copyright (c) 2009 Laurent Vivier
7 28ce5ce6 aurel32
 *
8 28ce5ce6 aurel32
 * some parts from linux-2.6.28, arch/powerpc/include/asm/dbdma.h
9 28ce5ce6 aurel32
 *
10 28ce5ce6 aurel32
 *   Definitions for using the Apple Descriptor-Based DMA controller
11 28ce5ce6 aurel32
 *   in Power Macintosh computers.
12 28ce5ce6 aurel32
 *
13 28ce5ce6 aurel32
 *   Copyright (C) 1996 Paul Mackerras.
14 28ce5ce6 aurel32
 *
15 28ce5ce6 aurel32
 * some parts from mol 0.9.71
16 28ce5ce6 aurel32
 *
17 28ce5ce6 aurel32
 *   Descriptor based DMA emulation
18 28ce5ce6 aurel32
 *
19 28ce5ce6 aurel32
 *   Copyright (C) 1998-2004 Samuel Rydh (samuel@ibrium.se)
20 3cbee15b j_mayer
 *
21 3cbee15b j_mayer
 * Permission is hereby granted, free of charge, to any person obtaining a copy
22 3cbee15b j_mayer
 * of this software and associated documentation files (the "Software"), to deal
23 3cbee15b j_mayer
 * in the Software without restriction, including without limitation the rights
24 3cbee15b j_mayer
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
25 3cbee15b j_mayer
 * copies of the Software, and to permit persons to whom the Software is
26 3cbee15b j_mayer
 * furnished to do so, subject to the following conditions:
27 3cbee15b j_mayer
 *
28 3cbee15b j_mayer
 * The above copyright notice and this permission notice shall be included in
29 3cbee15b j_mayer
 * all copies or substantial portions of the Software.
30 3cbee15b j_mayer
 *
31 3cbee15b j_mayer
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
32 3cbee15b j_mayer
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
33 3cbee15b j_mayer
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
34 3cbee15b j_mayer
 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
35 3cbee15b j_mayer
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
36 3cbee15b j_mayer
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
37 3cbee15b j_mayer
 * THE SOFTWARE.
38 3cbee15b j_mayer
 */
39 87ecb68b pbrook
#include "hw.h"
40 28ce5ce6 aurel32
#include "isa.h"
41 28ce5ce6 aurel32
#include "mac_dbdma.h"
42 3cbee15b j_mayer
43 ea026b2f blueswir1
/* debug DBDMA */
44 ea026b2f blueswir1
//#define DEBUG_DBDMA
45 ea026b2f blueswir1
46 ea026b2f blueswir1
#ifdef DEBUG_DBDMA
47 001faf32 Blue Swirl
#define DBDMA_DPRINTF(fmt, ...)                                 \
48 001faf32 Blue Swirl
    do { printf("DBDMA: " fmt , ## __VA_ARGS__); } while (0)
49 ea026b2f blueswir1
#else
50 001faf32 Blue Swirl
#define DBDMA_DPRINTF(fmt, ...)
51 ea026b2f blueswir1
#endif
52 ea026b2f blueswir1
53 28ce5ce6 aurel32
/*
54 28ce5ce6 aurel32
 */
55 28ce5ce6 aurel32
56 28ce5ce6 aurel32
/*
57 28ce5ce6 aurel32
 * DBDMA control/status registers.  All little-endian.
58 28ce5ce6 aurel32
 */
59 3cbee15b j_mayer
60 28ce5ce6 aurel32
#define DBDMA_CONTROL         0x00
61 28ce5ce6 aurel32
#define DBDMA_STATUS          0x01
62 28ce5ce6 aurel32
#define DBDMA_CMDPTR_HI       0x02
63 28ce5ce6 aurel32
#define DBDMA_CMDPTR_LO       0x03
64 28ce5ce6 aurel32
#define DBDMA_INTR_SEL        0x04
65 28ce5ce6 aurel32
#define DBDMA_BRANCH_SEL      0x05
66 28ce5ce6 aurel32
#define DBDMA_WAIT_SEL        0x06
67 28ce5ce6 aurel32
#define DBDMA_XFER_MODE       0x07
68 28ce5ce6 aurel32
#define DBDMA_DATA2PTR_HI     0x08
69 28ce5ce6 aurel32
#define DBDMA_DATA2PTR_LO     0x09
70 28ce5ce6 aurel32
#define DBDMA_RES1            0x0A
71 28ce5ce6 aurel32
#define DBDMA_ADDRESS_HI      0x0B
72 28ce5ce6 aurel32
#define DBDMA_BRANCH_ADDR_HI  0x0C
73 28ce5ce6 aurel32
#define DBDMA_RES2            0x0D
74 28ce5ce6 aurel32
#define DBDMA_RES3            0x0E
75 28ce5ce6 aurel32
#define DBDMA_RES4            0x0F
76 28ce5ce6 aurel32
77 28ce5ce6 aurel32
#define DBDMA_REGS            16
78 28ce5ce6 aurel32
#define DBDMA_SIZE            (DBDMA_REGS * sizeof(uint32_t))
79 28ce5ce6 aurel32
80 28ce5ce6 aurel32
#define DBDMA_CHANNEL_SHIFT   7
81 28ce5ce6 aurel32
#define DBDMA_CHANNEL_SIZE    (1 << DBDMA_CHANNEL_SHIFT)
82 28ce5ce6 aurel32
83 28ce5ce6 aurel32
#define DBDMA_CHANNELS        (0x1000 >> DBDMA_CHANNEL_SHIFT)
84 28ce5ce6 aurel32
85 28ce5ce6 aurel32
/* Bits in control and status registers */
86 28ce5ce6 aurel32
87 28ce5ce6 aurel32
#define RUN        0x8000
88 28ce5ce6 aurel32
#define PAUSE        0x4000
89 28ce5ce6 aurel32
#define FLUSH        0x2000
90 28ce5ce6 aurel32
#define WAKE        0x1000
91 28ce5ce6 aurel32
#define DEAD        0x0800
92 28ce5ce6 aurel32
#define ACTIVE        0x0400
93 28ce5ce6 aurel32
#define BT        0x0100
94 28ce5ce6 aurel32
#define DEVSTAT        0x00ff
95 28ce5ce6 aurel32
96 28ce5ce6 aurel32
/*
97 28ce5ce6 aurel32
 * DBDMA command structure.  These fields are all little-endian!
98 28ce5ce6 aurel32
 */
99 28ce5ce6 aurel32
100 28ce5ce6 aurel32
typedef struct dbdma_cmd {
101 28ce5ce6 aurel32
    uint16_t req_count;          /* requested byte transfer count */
102 28ce5ce6 aurel32
    uint16_t command;          /* command word (has bit-fields) */
103 28ce5ce6 aurel32
    uint32_t phy_addr;          /* physical data address */
104 28ce5ce6 aurel32
    uint32_t cmd_dep;          /* command-dependent field */
105 28ce5ce6 aurel32
    uint16_t res_count;          /* residual count after completion */
106 28ce5ce6 aurel32
    uint16_t xfer_status; /* transfer status */
107 28ce5ce6 aurel32
} dbdma_cmd;
108 28ce5ce6 aurel32
109 28ce5ce6 aurel32
/* DBDMA command values in command field */
110 28ce5ce6 aurel32
111 28ce5ce6 aurel32
#define COMMAND_MASK    0xf000
112 28ce5ce6 aurel32
#define OUTPUT_MORE        0x0000        /* transfer memory data to stream */
113 28ce5ce6 aurel32
#define OUTPUT_LAST        0x1000        /* ditto followed by end marker */
114 28ce5ce6 aurel32
#define INPUT_MORE        0x2000        /* transfer stream data to memory */
115 28ce5ce6 aurel32
#define INPUT_LAST        0x3000        /* ditto, expect end marker */
116 28ce5ce6 aurel32
#define STORE_WORD        0x4000        /* write word (4 bytes) to device reg */
117 28ce5ce6 aurel32
#define LOAD_WORD        0x5000        /* read word (4 bytes) from device reg */
118 28ce5ce6 aurel32
#define DBDMA_NOP        0x6000        /* do nothing */
119 28ce5ce6 aurel32
#define DBDMA_STOP        0x7000        /* suspend processing */
120 28ce5ce6 aurel32
121 28ce5ce6 aurel32
/* Key values in command field */
122 28ce5ce6 aurel32
123 28ce5ce6 aurel32
#define KEY_MASK        0x0700
124 28ce5ce6 aurel32
#define KEY_STREAM0        0x0000        /* usual data stream */
125 28ce5ce6 aurel32
#define KEY_STREAM1        0x0100        /* control/status stream */
126 28ce5ce6 aurel32
#define KEY_STREAM2        0x0200        /* device-dependent stream */
127 28ce5ce6 aurel32
#define KEY_STREAM3        0x0300        /* device-dependent stream */
128 28ce5ce6 aurel32
#define KEY_STREAM4        0x0400        /* reserved */
129 28ce5ce6 aurel32
#define KEY_REGS        0x0500        /* device register space */
130 28ce5ce6 aurel32
#define KEY_SYSTEM        0x0600        /* system memory-mapped space */
131 28ce5ce6 aurel32
#define KEY_DEVICE        0x0700        /* device memory-mapped space */
132 28ce5ce6 aurel32
133 28ce5ce6 aurel32
/* Interrupt control values in command field */
134 28ce5ce6 aurel32
135 28ce5ce6 aurel32
#define INTR_MASK       0x0030
136 28ce5ce6 aurel32
#define INTR_NEVER        0x0000        /* don't interrupt */
137 28ce5ce6 aurel32
#define INTR_IFSET        0x0010        /* intr if condition bit is 1 */
138 28ce5ce6 aurel32
#define INTR_IFCLR        0x0020        /* intr if condition bit is 0 */
139 28ce5ce6 aurel32
#define INTR_ALWAYS        0x0030        /* always interrupt */
140 28ce5ce6 aurel32
141 28ce5ce6 aurel32
/* Branch control values in command field */
142 28ce5ce6 aurel32
143 28ce5ce6 aurel32
#define BR_MASK         0x000c
144 28ce5ce6 aurel32
#define BR_NEVER        0x0000        /* don't branch */
145 28ce5ce6 aurel32
#define BR_IFSET        0x0004        /* branch if condition bit is 1 */
146 28ce5ce6 aurel32
#define BR_IFCLR        0x0008        /* branch if condition bit is 0 */
147 28ce5ce6 aurel32
#define BR_ALWAYS        0x000c        /* always branch */
148 28ce5ce6 aurel32
149 28ce5ce6 aurel32
/* Wait control values in command field */
150 28ce5ce6 aurel32
151 28ce5ce6 aurel32
#define WAIT_MASK       0x0003
152 28ce5ce6 aurel32
#define WAIT_NEVER        0x0000        /* don't wait */
153 28ce5ce6 aurel32
#define WAIT_IFSET        0x0001        /* wait if condition bit is 1 */
154 28ce5ce6 aurel32
#define WAIT_IFCLR        0x0002        /* wait if condition bit is 0 */
155 28ce5ce6 aurel32
#define WAIT_ALWAYS        0x0003        /* always wait */
156 28ce5ce6 aurel32
157 28ce5ce6 aurel32
typedef struct DBDMA_channel {
158 28ce5ce6 aurel32
    int channel;
159 28ce5ce6 aurel32
    uint32_t regs[DBDMA_REGS];
160 28ce5ce6 aurel32
    qemu_irq irq;
161 b42ec42d aurel32
    DBDMA_io io;
162 b42ec42d aurel32
    DBDMA_rw rw;
163 862c9280 aurel32
    DBDMA_flush flush;
164 28ce5ce6 aurel32
    dbdma_cmd current;
165 b42ec42d aurel32
    int processing;
166 28ce5ce6 aurel32
} DBDMA_channel;
167 28ce5ce6 aurel32
168 28ce5ce6 aurel32
#ifdef DEBUG_DBDMA
169 28ce5ce6 aurel32
static void dump_dbdma_cmd(dbdma_cmd *cmd)
170 28ce5ce6 aurel32
{
171 28ce5ce6 aurel32
    printf("dbdma_cmd %p\n", cmd);
172 28ce5ce6 aurel32
    printf("    req_count 0x%04x\n", le16_to_cpu(cmd->req_count));
173 28ce5ce6 aurel32
    printf("    command 0x%04x\n", le16_to_cpu(cmd->command));
174 28ce5ce6 aurel32
    printf("    phy_addr 0x%08x\n", le32_to_cpu(cmd->phy_addr));
175 28ce5ce6 aurel32
    printf("    cmd_dep 0x%08x\n", le32_to_cpu(cmd->cmd_dep));
176 28ce5ce6 aurel32
    printf("    res_count 0x%04x\n", le16_to_cpu(cmd->res_count));
177 28ce5ce6 aurel32
    printf("    xfer_status 0x%04x\n", le16_to_cpu(cmd->xfer_status));
178 28ce5ce6 aurel32
}
179 28ce5ce6 aurel32
#else
180 28ce5ce6 aurel32
static void dump_dbdma_cmd(dbdma_cmd *cmd)
181 3cbee15b j_mayer
{
182 28ce5ce6 aurel32
}
183 28ce5ce6 aurel32
#endif
184 28ce5ce6 aurel32
static void dbdma_cmdptr_load(DBDMA_channel *ch)
185 28ce5ce6 aurel32
{
186 28ce5ce6 aurel32
    DBDMA_DPRINTF("dbdma_cmdptr_load 0x%08x\n",
187 ad674e53 Aurelien Jarno
                  ch->regs[DBDMA_CMDPTR_LO]);
188 ad674e53 Aurelien Jarno
    cpu_physical_memory_read(ch->regs[DBDMA_CMDPTR_LO],
189 28ce5ce6 aurel32
                             (uint8_t*)&ch->current, sizeof(dbdma_cmd));
190 3cbee15b j_mayer
}
191 3cbee15b j_mayer
192 28ce5ce6 aurel32
static void dbdma_cmdptr_save(DBDMA_channel *ch)
193 3cbee15b j_mayer
{
194 28ce5ce6 aurel32
    DBDMA_DPRINTF("dbdma_cmdptr_save 0x%08x\n",
195 ad674e53 Aurelien Jarno
                  ch->regs[DBDMA_CMDPTR_LO]);
196 28ce5ce6 aurel32
    DBDMA_DPRINTF("xfer_status 0x%08x res_count 0x%04x\n",
197 28ce5ce6 aurel32
                  le16_to_cpu(ch->current.xfer_status),
198 28ce5ce6 aurel32
                  le16_to_cpu(ch->current.res_count));
199 ad674e53 Aurelien Jarno
    cpu_physical_memory_write(ch->regs[DBDMA_CMDPTR_LO],
200 28ce5ce6 aurel32
                              (uint8_t*)&ch->current, sizeof(dbdma_cmd));
201 3cbee15b j_mayer
}
202 3cbee15b j_mayer
203 28ce5ce6 aurel32
static void kill_channel(DBDMA_channel *ch)
204 3cbee15b j_mayer
{
205 28ce5ce6 aurel32
    DBDMA_DPRINTF("kill_channel\n");
206 28ce5ce6 aurel32
207 ad674e53 Aurelien Jarno
    ch->regs[DBDMA_STATUS] |= DEAD;
208 ad674e53 Aurelien Jarno
    ch->regs[DBDMA_STATUS] &= ~ACTIVE;
209 28ce5ce6 aurel32
210 28ce5ce6 aurel32
    qemu_irq_raise(ch->irq);
211 28ce5ce6 aurel32
}
212 28ce5ce6 aurel32
213 28ce5ce6 aurel32
static void conditional_interrupt(DBDMA_channel *ch)
214 28ce5ce6 aurel32
{
215 28ce5ce6 aurel32
    dbdma_cmd *current = &ch->current;
216 28ce5ce6 aurel32
    uint16_t intr;
217 28ce5ce6 aurel32
    uint16_t sel_mask, sel_value;
218 28ce5ce6 aurel32
    uint32_t status;
219 28ce5ce6 aurel32
    int cond;
220 28ce5ce6 aurel32
221 28ce5ce6 aurel32
    DBDMA_DPRINTF("conditional_interrupt\n");
222 28ce5ce6 aurel32
223 b42ec42d aurel32
    intr = le16_to_cpu(current->command) & INTR_MASK;
224 28ce5ce6 aurel32
225 28ce5ce6 aurel32
    switch(intr) {
226 28ce5ce6 aurel32
    case INTR_NEVER:  /* don't interrupt */
227 28ce5ce6 aurel32
        return;
228 28ce5ce6 aurel32
    case INTR_ALWAYS: /* always interrupt */
229 28ce5ce6 aurel32
        qemu_irq_raise(ch->irq);
230 28ce5ce6 aurel32
        return;
231 28ce5ce6 aurel32
    }
232 28ce5ce6 aurel32
233 ad674e53 Aurelien Jarno
    status = ch->regs[DBDMA_STATUS] & DEVSTAT;
234 28ce5ce6 aurel32
235 ad674e53 Aurelien Jarno
    sel_mask = (ch->regs[DBDMA_INTR_SEL] >> 16) & 0x0f;
236 ad674e53 Aurelien Jarno
    sel_value = ch->regs[DBDMA_INTR_SEL] & 0x0f;
237 28ce5ce6 aurel32
238 28ce5ce6 aurel32
    cond = (status & sel_mask) == (sel_value & sel_mask);
239 28ce5ce6 aurel32
240 28ce5ce6 aurel32
    switch(intr) {
241 28ce5ce6 aurel32
    case INTR_IFSET:  /* intr if condition bit is 1 */
242 28ce5ce6 aurel32
        if (cond)
243 28ce5ce6 aurel32
            qemu_irq_raise(ch->irq);
244 28ce5ce6 aurel32
        return;
245 28ce5ce6 aurel32
    case INTR_IFCLR:  /* intr if condition bit is 0 */
246 28ce5ce6 aurel32
        if (!cond)
247 28ce5ce6 aurel32
            qemu_irq_raise(ch->irq);
248 28ce5ce6 aurel32
        return;
249 28ce5ce6 aurel32
    }
250 28ce5ce6 aurel32
}
251 28ce5ce6 aurel32
252 28ce5ce6 aurel32
static int conditional_wait(DBDMA_channel *ch)
253 28ce5ce6 aurel32
{
254 28ce5ce6 aurel32
    dbdma_cmd *current = &ch->current;
255 28ce5ce6 aurel32
    uint16_t wait;
256 28ce5ce6 aurel32
    uint16_t sel_mask, sel_value;
257 28ce5ce6 aurel32
    uint32_t status;
258 28ce5ce6 aurel32
    int cond;
259 28ce5ce6 aurel32
260 28ce5ce6 aurel32
    DBDMA_DPRINTF("conditional_wait\n");
261 28ce5ce6 aurel32
262 b42ec42d aurel32
    wait = le16_to_cpu(current->command) & WAIT_MASK;
263 28ce5ce6 aurel32
264 28ce5ce6 aurel32
    switch(wait) {
265 28ce5ce6 aurel32
    case WAIT_NEVER:  /* don't wait */
266 28ce5ce6 aurel32
        return 0;
267 28ce5ce6 aurel32
    case WAIT_ALWAYS: /* always wait */
268 28ce5ce6 aurel32
        return 1;
269 28ce5ce6 aurel32
    }
270 28ce5ce6 aurel32
271 ad674e53 Aurelien Jarno
    status = ch->regs[DBDMA_STATUS] & DEVSTAT;
272 28ce5ce6 aurel32
273 ad674e53 Aurelien Jarno
    sel_mask = (ch->regs[DBDMA_WAIT_SEL] >> 16) & 0x0f;
274 ad674e53 Aurelien Jarno
    sel_value = ch->regs[DBDMA_WAIT_SEL] & 0x0f;
275 28ce5ce6 aurel32
276 28ce5ce6 aurel32
    cond = (status & sel_mask) == (sel_value & sel_mask);
277 28ce5ce6 aurel32
278 28ce5ce6 aurel32
    switch(wait) {
279 28ce5ce6 aurel32
    case WAIT_IFSET:  /* wait if condition bit is 1 */
280 28ce5ce6 aurel32
        if (cond)
281 28ce5ce6 aurel32
            return 1;
282 28ce5ce6 aurel32
        return 0;
283 28ce5ce6 aurel32
    case WAIT_IFCLR:  /* wait if condition bit is 0 */
284 28ce5ce6 aurel32
        if (!cond)
285 28ce5ce6 aurel32
            return 1;
286 28ce5ce6 aurel32
        return 0;
287 28ce5ce6 aurel32
    }
288 28ce5ce6 aurel32
    return 0;
289 28ce5ce6 aurel32
}
290 28ce5ce6 aurel32
291 28ce5ce6 aurel32
static void next(DBDMA_channel *ch)
292 28ce5ce6 aurel32
{
293 28ce5ce6 aurel32
    uint32_t cp;
294 28ce5ce6 aurel32
295 ad674e53 Aurelien Jarno
    ch->regs[DBDMA_STATUS] &= ~BT;
296 28ce5ce6 aurel32
297 ad674e53 Aurelien Jarno
    cp = ch->regs[DBDMA_CMDPTR_LO];
298 ad674e53 Aurelien Jarno
    ch->regs[DBDMA_CMDPTR_LO] = cp + sizeof(dbdma_cmd);
299 28ce5ce6 aurel32
    dbdma_cmdptr_load(ch);
300 28ce5ce6 aurel32
}
301 28ce5ce6 aurel32
302 28ce5ce6 aurel32
static void branch(DBDMA_channel *ch)
303 28ce5ce6 aurel32
{
304 28ce5ce6 aurel32
    dbdma_cmd *current = &ch->current;
305 28ce5ce6 aurel32
306 28ce5ce6 aurel32
    ch->regs[DBDMA_CMDPTR_LO] = current->cmd_dep;
307 ad674e53 Aurelien Jarno
    ch->regs[DBDMA_STATUS] |= BT;
308 28ce5ce6 aurel32
    dbdma_cmdptr_load(ch);
309 28ce5ce6 aurel32
}
310 28ce5ce6 aurel32
311 28ce5ce6 aurel32
static void conditional_branch(DBDMA_channel *ch)
312 28ce5ce6 aurel32
{
313 28ce5ce6 aurel32
    dbdma_cmd *current = &ch->current;
314 28ce5ce6 aurel32
    uint16_t br;
315 28ce5ce6 aurel32
    uint16_t sel_mask, sel_value;
316 28ce5ce6 aurel32
    uint32_t status;
317 28ce5ce6 aurel32
    int cond;
318 28ce5ce6 aurel32
319 28ce5ce6 aurel32
    DBDMA_DPRINTF("conditional_branch\n");
320 28ce5ce6 aurel32
321 28ce5ce6 aurel32
    /* check if we must branch */
322 28ce5ce6 aurel32
323 b42ec42d aurel32
    br = le16_to_cpu(current->command) & BR_MASK;
324 28ce5ce6 aurel32
325 28ce5ce6 aurel32
    switch(br) {
326 28ce5ce6 aurel32
    case BR_NEVER:  /* don't branch */
327 28ce5ce6 aurel32
        next(ch);
328 28ce5ce6 aurel32
        return;
329 28ce5ce6 aurel32
    case BR_ALWAYS: /* always branch */
330 28ce5ce6 aurel32
        branch(ch);
331 28ce5ce6 aurel32
        return;
332 28ce5ce6 aurel32
    }
333 28ce5ce6 aurel32
334 ad674e53 Aurelien Jarno
    status = ch->regs[DBDMA_STATUS] & DEVSTAT;
335 28ce5ce6 aurel32
336 ad674e53 Aurelien Jarno
    sel_mask = (ch->regs[DBDMA_BRANCH_SEL] >> 16) & 0x0f;
337 ad674e53 Aurelien Jarno
    sel_value = ch->regs[DBDMA_BRANCH_SEL] & 0x0f;
338 28ce5ce6 aurel32
339 28ce5ce6 aurel32
    cond = (status & sel_mask) == (sel_value & sel_mask);
340 28ce5ce6 aurel32
341 28ce5ce6 aurel32
    switch(br) {
342 28ce5ce6 aurel32
    case BR_IFSET:  /* branch if condition bit is 1 */
343 28ce5ce6 aurel32
        if (cond)
344 28ce5ce6 aurel32
            branch(ch);
345 28ce5ce6 aurel32
        else
346 28ce5ce6 aurel32
            next(ch);
347 28ce5ce6 aurel32
        return;
348 28ce5ce6 aurel32
    case BR_IFCLR:  /* branch if condition bit is 0 */
349 28ce5ce6 aurel32
        if (!cond)
350 28ce5ce6 aurel32
            branch(ch);
351 28ce5ce6 aurel32
        else
352 28ce5ce6 aurel32
            next(ch);
353 28ce5ce6 aurel32
        return;
354 28ce5ce6 aurel32
    }
355 28ce5ce6 aurel32
}
356 28ce5ce6 aurel32
357 b42ec42d aurel32
static QEMUBH *dbdma_bh;
358 b42ec42d aurel32
static void channel_run(DBDMA_channel *ch);
359 28ce5ce6 aurel32
360 b42ec42d aurel32
static void dbdma_end(DBDMA_io *io)
361 28ce5ce6 aurel32
{
362 28ce5ce6 aurel32
    DBDMA_channel *ch = io->channel;
363 28ce5ce6 aurel32
    dbdma_cmd *current = &ch->current;
364 28ce5ce6 aurel32
365 b42ec42d aurel32
    if (conditional_wait(ch))
366 b42ec42d aurel32
        goto wait;
367 28ce5ce6 aurel32
368 ad674e53 Aurelien Jarno
    current->xfer_status = cpu_to_le16(ch->regs[DBDMA_STATUS]);
369 ad674e53 Aurelien Jarno
    current->res_count = cpu_to_le16(io->len);
370 b42ec42d aurel32
    dbdma_cmdptr_save(ch);
371 862c9280 aurel32
    if (io->is_last)
372 ad674e53 Aurelien Jarno
        ch->regs[DBDMA_STATUS] &= ~FLUSH;
373 b42ec42d aurel32
374 b42ec42d aurel32
    conditional_interrupt(ch);
375 b42ec42d aurel32
    conditional_branch(ch);
376 28ce5ce6 aurel32
377 b42ec42d aurel32
wait:
378 b42ec42d aurel32
    ch->processing = 0;
379 ad674e53 Aurelien Jarno
    if ((ch->regs[DBDMA_STATUS] & RUN) &&
380 ad674e53 Aurelien Jarno
        (ch->regs[DBDMA_STATUS] & ACTIVE))
381 b42ec42d aurel32
        channel_run(ch);
382 28ce5ce6 aurel32
}
383 28ce5ce6 aurel32
384 b42ec42d aurel32
static void start_output(DBDMA_channel *ch, int key, uint32_t addr,
385 28ce5ce6 aurel32
                        uint16_t req_count, int is_last)
386 28ce5ce6 aurel32
{
387 28ce5ce6 aurel32
    DBDMA_DPRINTF("start_output\n");
388 28ce5ce6 aurel32
389 28ce5ce6 aurel32
    /* KEY_REGS, KEY_DEVICE and KEY_STREAM
390 28ce5ce6 aurel32
     * are not implemented in the mac-io chip
391 28ce5ce6 aurel32
     */
392 28ce5ce6 aurel32
393 28ce5ce6 aurel32
    DBDMA_DPRINTF("addr 0x%x key 0x%x\n", addr, key);
394 28ce5ce6 aurel32
    if (!addr || key > KEY_STREAM3) {
395 28ce5ce6 aurel32
        kill_channel(ch);
396 b42ec42d aurel32
        return;
397 28ce5ce6 aurel32
    }
398 28ce5ce6 aurel32
399 b42ec42d aurel32
    ch->io.addr = addr;
400 28ce5ce6 aurel32
    ch->io.len = req_count;
401 28ce5ce6 aurel32
    ch->io.is_last = is_last;
402 b42ec42d aurel32
    ch->io.dma_end = dbdma_end;
403 b42ec42d aurel32
    ch->io.is_dma_out = 1;
404 b42ec42d aurel32
    ch->processing = 1;
405 a9ceb76d Alexander Graf
    if (ch->rw) {
406 a9ceb76d Alexander Graf
        ch->rw(&ch->io);
407 a9ceb76d Alexander Graf
    }
408 28ce5ce6 aurel32
}
409 28ce5ce6 aurel32
410 b42ec42d aurel32
static void start_input(DBDMA_channel *ch, int key, uint32_t addr,
411 28ce5ce6 aurel32
                       uint16_t req_count, int is_last)
412 28ce5ce6 aurel32
{
413 28ce5ce6 aurel32
    DBDMA_DPRINTF("start_input\n");
414 28ce5ce6 aurel32
415 28ce5ce6 aurel32
    /* KEY_REGS, KEY_DEVICE and KEY_STREAM
416 28ce5ce6 aurel32
     * are not implemented in the mac-io chip
417 28ce5ce6 aurel32
     */
418 28ce5ce6 aurel32
419 28ce5ce6 aurel32
    if (!addr || key > KEY_STREAM3) {
420 28ce5ce6 aurel32
        kill_channel(ch);
421 b42ec42d aurel32
        return;
422 28ce5ce6 aurel32
    }
423 28ce5ce6 aurel32
424 b42ec42d aurel32
    ch->io.addr = addr;
425 28ce5ce6 aurel32
    ch->io.len = req_count;
426 28ce5ce6 aurel32
    ch->io.is_last = is_last;
427 b42ec42d aurel32
    ch->io.dma_end = dbdma_end;
428 b42ec42d aurel32
    ch->io.is_dma_out = 0;
429 b42ec42d aurel32
    ch->processing = 1;
430 a9ceb76d Alexander Graf
    if (ch->rw) {
431 a9ceb76d Alexander Graf
        ch->rw(&ch->io);
432 a9ceb76d Alexander Graf
    }
433 28ce5ce6 aurel32
}
434 28ce5ce6 aurel32
435 b42ec42d aurel32
static void load_word(DBDMA_channel *ch, int key, uint32_t addr,
436 28ce5ce6 aurel32
                     uint16_t len)
437 28ce5ce6 aurel32
{
438 28ce5ce6 aurel32
    dbdma_cmd *current = &ch->current;
439 28ce5ce6 aurel32
    uint32_t val;
440 28ce5ce6 aurel32
441 28ce5ce6 aurel32
    DBDMA_DPRINTF("load_word\n");
442 28ce5ce6 aurel32
443 28ce5ce6 aurel32
    /* only implements KEY_SYSTEM */
444 28ce5ce6 aurel32
445 28ce5ce6 aurel32
    if (key != KEY_SYSTEM) {
446 28ce5ce6 aurel32
        printf("DBDMA: LOAD_WORD, unimplemented key %x\n", key);
447 28ce5ce6 aurel32
        kill_channel(ch);
448 b42ec42d aurel32
        return;
449 28ce5ce6 aurel32
    }
450 28ce5ce6 aurel32
451 28ce5ce6 aurel32
    cpu_physical_memory_read(addr, (uint8_t*)&val, len);
452 28ce5ce6 aurel32
453 28ce5ce6 aurel32
    if (len == 2)
454 28ce5ce6 aurel32
        val = (val << 16) | (current->cmd_dep & 0x0000ffff);
455 28ce5ce6 aurel32
    else if (len == 1)
456 28ce5ce6 aurel32
        val = (val << 24) | (current->cmd_dep & 0x00ffffff);
457 28ce5ce6 aurel32
458 28ce5ce6 aurel32
    current->cmd_dep = val;
459 28ce5ce6 aurel32
460 28ce5ce6 aurel32
    if (conditional_wait(ch))
461 b42ec42d aurel32
        goto wait;
462 28ce5ce6 aurel32
463 ad674e53 Aurelien Jarno
    current->xfer_status = cpu_to_le16(ch->regs[DBDMA_STATUS]);
464 28ce5ce6 aurel32
    dbdma_cmdptr_save(ch);
465 ad674e53 Aurelien Jarno
    ch->regs[DBDMA_STATUS] &= ~FLUSH;
466 28ce5ce6 aurel32
467 28ce5ce6 aurel32
    conditional_interrupt(ch);
468 28ce5ce6 aurel32
    next(ch);
469 28ce5ce6 aurel32
470 b42ec42d aurel32
wait:
471 b42ec42d aurel32
    qemu_bh_schedule(dbdma_bh);
472 28ce5ce6 aurel32
}
473 28ce5ce6 aurel32
474 b42ec42d aurel32
static void store_word(DBDMA_channel *ch, int key, uint32_t addr,
475 28ce5ce6 aurel32
                      uint16_t len)
476 28ce5ce6 aurel32
{
477 28ce5ce6 aurel32
    dbdma_cmd *current = &ch->current;
478 28ce5ce6 aurel32
    uint32_t val;
479 28ce5ce6 aurel32
480 28ce5ce6 aurel32
    DBDMA_DPRINTF("store_word\n");
481 28ce5ce6 aurel32
482 28ce5ce6 aurel32
    /* only implements KEY_SYSTEM */
483 28ce5ce6 aurel32
484 28ce5ce6 aurel32
    if (key != KEY_SYSTEM) {
485 28ce5ce6 aurel32
        printf("DBDMA: STORE_WORD, unimplemented key %x\n", key);
486 28ce5ce6 aurel32
        kill_channel(ch);
487 b42ec42d aurel32
        return;
488 28ce5ce6 aurel32
    }
489 28ce5ce6 aurel32
490 28ce5ce6 aurel32
    val = current->cmd_dep;
491 28ce5ce6 aurel32
    if (len == 2)
492 28ce5ce6 aurel32
        val >>= 16;
493 28ce5ce6 aurel32
    else if (len == 1)
494 28ce5ce6 aurel32
        val >>= 24;
495 28ce5ce6 aurel32
496 28ce5ce6 aurel32
    cpu_physical_memory_write(addr, (uint8_t*)&val, len);
497 28ce5ce6 aurel32
498 28ce5ce6 aurel32
    if (conditional_wait(ch))
499 b42ec42d aurel32
        goto wait;
500 28ce5ce6 aurel32
501 ad674e53 Aurelien Jarno
    current->xfer_status = cpu_to_le16(ch->regs[DBDMA_STATUS]);
502 28ce5ce6 aurel32
    dbdma_cmdptr_save(ch);
503 ad674e53 Aurelien Jarno
    ch->regs[DBDMA_STATUS] &= ~FLUSH;
504 28ce5ce6 aurel32
505 28ce5ce6 aurel32
    conditional_interrupt(ch);
506 28ce5ce6 aurel32
    next(ch);
507 28ce5ce6 aurel32
508 b42ec42d aurel32
wait:
509 b42ec42d aurel32
    qemu_bh_schedule(dbdma_bh);
510 28ce5ce6 aurel32
}
511 28ce5ce6 aurel32
512 b42ec42d aurel32
static void nop(DBDMA_channel *ch)
513 28ce5ce6 aurel32
{
514 28ce5ce6 aurel32
    dbdma_cmd *current = &ch->current;
515 28ce5ce6 aurel32
516 28ce5ce6 aurel32
    if (conditional_wait(ch))
517 b42ec42d aurel32
        goto wait;
518 28ce5ce6 aurel32
519 ad674e53 Aurelien Jarno
    current->xfer_status = cpu_to_le16(ch->regs[DBDMA_STATUS]);
520 28ce5ce6 aurel32
    dbdma_cmdptr_save(ch);
521 28ce5ce6 aurel32
522 28ce5ce6 aurel32
    conditional_interrupt(ch);
523 28ce5ce6 aurel32
    conditional_branch(ch);
524 28ce5ce6 aurel32
525 b42ec42d aurel32
wait:
526 b42ec42d aurel32
    qemu_bh_schedule(dbdma_bh);
527 3cbee15b j_mayer
}
528 3cbee15b j_mayer
529 b42ec42d aurel32
static void stop(DBDMA_channel *ch)
530 3cbee15b j_mayer
{
531 ad674e53 Aurelien Jarno
    ch->regs[DBDMA_STATUS] &= ~(ACTIVE|DEAD|FLUSH);
532 28ce5ce6 aurel32
533 28ce5ce6 aurel32
    /* the stop command does not increment command pointer */
534 3cbee15b j_mayer
}
535 3cbee15b j_mayer
536 b42ec42d aurel32
static void channel_run(DBDMA_channel *ch)
537 3cbee15b j_mayer
{
538 28ce5ce6 aurel32
    dbdma_cmd *current = &ch->current;
539 28ce5ce6 aurel32
    uint16_t cmd, key;
540 28ce5ce6 aurel32
    uint16_t req_count;
541 28ce5ce6 aurel32
    uint32_t phy_addr;
542 28ce5ce6 aurel32
543 28ce5ce6 aurel32
    DBDMA_DPRINTF("channel_run\n");
544 28ce5ce6 aurel32
    dump_dbdma_cmd(current);
545 28ce5ce6 aurel32
546 28ce5ce6 aurel32
    /* clear WAKE flag at command fetch */
547 28ce5ce6 aurel32
548 ad674e53 Aurelien Jarno
    ch->regs[DBDMA_STATUS] &= ~WAKE;
549 28ce5ce6 aurel32
550 28ce5ce6 aurel32
    cmd = le16_to_cpu(current->command) & COMMAND_MASK;
551 28ce5ce6 aurel32
552 28ce5ce6 aurel32
    switch (cmd) {
553 28ce5ce6 aurel32
    case DBDMA_NOP:
554 b42ec42d aurel32
        nop(ch);
555 b42ec42d aurel32
        return;
556 28ce5ce6 aurel32
557 28ce5ce6 aurel32
    case DBDMA_STOP:
558 b42ec42d aurel32
        stop(ch);
559 b42ec42d aurel32
        return;
560 28ce5ce6 aurel32
    }
561 28ce5ce6 aurel32
562 28ce5ce6 aurel32
    key = le16_to_cpu(current->command) & 0x0700;
563 28ce5ce6 aurel32
    req_count = le16_to_cpu(current->req_count);
564 28ce5ce6 aurel32
    phy_addr = le32_to_cpu(current->phy_addr);
565 28ce5ce6 aurel32
566 28ce5ce6 aurel32
    if (key == KEY_STREAM4) {
567 28ce5ce6 aurel32
        printf("command %x, invalid key 4\n", cmd);
568 28ce5ce6 aurel32
        kill_channel(ch);
569 b42ec42d aurel32
        return;
570 28ce5ce6 aurel32
    }
571 28ce5ce6 aurel32
572 28ce5ce6 aurel32
    switch (cmd) {
573 28ce5ce6 aurel32
    case OUTPUT_MORE:
574 b42ec42d aurel32
        start_output(ch, key, phy_addr, req_count, 0);
575 b42ec42d aurel32
        return;
576 28ce5ce6 aurel32
577 28ce5ce6 aurel32
    case OUTPUT_LAST:
578 b42ec42d aurel32
        start_output(ch, key, phy_addr, req_count, 1);
579 b42ec42d aurel32
        return;
580 28ce5ce6 aurel32
581 28ce5ce6 aurel32
    case INPUT_MORE:
582 b42ec42d aurel32
        start_input(ch, key, phy_addr, req_count, 0);
583 b42ec42d aurel32
        return;
584 28ce5ce6 aurel32
585 28ce5ce6 aurel32
    case INPUT_LAST:
586 b42ec42d aurel32
        start_input(ch, key, phy_addr, req_count, 1);
587 b42ec42d aurel32
        return;
588 28ce5ce6 aurel32
    }
589 28ce5ce6 aurel32
590 28ce5ce6 aurel32
    if (key < KEY_REGS) {
591 28ce5ce6 aurel32
        printf("command %x, invalid key %x\n", cmd, key);
592 28ce5ce6 aurel32
        key = KEY_SYSTEM;
593 28ce5ce6 aurel32
    }
594 28ce5ce6 aurel32
595 28ce5ce6 aurel32
    /* for LOAD_WORD and STORE_WORD, req_count is on 3 bits
596 28ce5ce6 aurel32
     * and BRANCH is invalid
597 28ce5ce6 aurel32
     */
598 28ce5ce6 aurel32
599 28ce5ce6 aurel32
    req_count = req_count & 0x0007;
600 28ce5ce6 aurel32
    if (req_count & 0x4) {
601 28ce5ce6 aurel32
        req_count = 4;
602 28ce5ce6 aurel32
        phy_addr &= ~3;
603 28ce5ce6 aurel32
    } else if (req_count & 0x2) {
604 28ce5ce6 aurel32
        req_count = 2;
605 28ce5ce6 aurel32
        phy_addr &= ~1;
606 28ce5ce6 aurel32
    } else
607 28ce5ce6 aurel32
        req_count = 1;
608 28ce5ce6 aurel32
609 28ce5ce6 aurel32
    switch (cmd) {
610 28ce5ce6 aurel32
    case LOAD_WORD:
611 b42ec42d aurel32
        load_word(ch, key, phy_addr, req_count);
612 b42ec42d aurel32
        return;
613 28ce5ce6 aurel32
614 28ce5ce6 aurel32
    case STORE_WORD:
615 b42ec42d aurel32
        store_word(ch, key, phy_addr, req_count);
616 b42ec42d aurel32
        return;
617 28ce5ce6 aurel32
    }
618 3cbee15b j_mayer
}
619 3cbee15b j_mayer
620 28ce5ce6 aurel32
static void DBDMA_run (DBDMA_channel *ch)
621 28ce5ce6 aurel32
{
622 28ce5ce6 aurel32
    int channel;
623 28ce5ce6 aurel32
624 28ce5ce6 aurel32
    for (channel = 0; channel < DBDMA_CHANNELS; channel++, ch++) {
625 ad674e53 Aurelien Jarno
            uint32_t status = ch->regs[DBDMA_STATUS];
626 b42ec42d aurel32
            if (!ch->processing && (status & RUN) && (status & ACTIVE))
627 b42ec42d aurel32
                channel_run(ch);
628 28ce5ce6 aurel32
    }
629 28ce5ce6 aurel32
}
630 28ce5ce6 aurel32
631 28ce5ce6 aurel32
static void DBDMA_run_bh(void *opaque)
632 28ce5ce6 aurel32
{
633 28ce5ce6 aurel32
    DBDMA_channel *ch = opaque;
634 28ce5ce6 aurel32
635 28ce5ce6 aurel32
    DBDMA_DPRINTF("DBDMA_run_bh\n");
636 28ce5ce6 aurel32
637 28ce5ce6 aurel32
    DBDMA_run(ch);
638 28ce5ce6 aurel32
}
639 28ce5ce6 aurel32
640 28ce5ce6 aurel32
void DBDMA_register_channel(void *dbdma, int nchan, qemu_irq irq,
641 862c9280 aurel32
                            DBDMA_rw rw, DBDMA_flush flush,
642 28ce5ce6 aurel32
                            void *opaque)
643 28ce5ce6 aurel32
{
644 28ce5ce6 aurel32
    DBDMA_channel *ch = ( DBDMA_channel *)dbdma + nchan;
645 28ce5ce6 aurel32
646 28ce5ce6 aurel32
    DBDMA_DPRINTF("DBDMA_register_channel 0x%x\n", nchan);
647 28ce5ce6 aurel32
648 28ce5ce6 aurel32
    ch->irq = irq;
649 28ce5ce6 aurel32
    ch->channel = nchan;
650 b42ec42d aurel32
    ch->rw = rw;
651 862c9280 aurel32
    ch->flush = flush;
652 28ce5ce6 aurel32
    ch->io.opaque = opaque;
653 28ce5ce6 aurel32
    ch->io.channel = ch;
654 28ce5ce6 aurel32
}
655 28ce5ce6 aurel32
656 28ce5ce6 aurel32
void DBDMA_schedule(void)
657 28ce5ce6 aurel32
{
658 d9f75a4e aliguori
    qemu_notify_event();
659 28ce5ce6 aurel32
}
660 28ce5ce6 aurel32
661 28ce5ce6 aurel32
static void
662 28ce5ce6 aurel32
dbdma_control_write(DBDMA_channel *ch)
663 28ce5ce6 aurel32
{
664 28ce5ce6 aurel32
    uint16_t mask, value;
665 28ce5ce6 aurel32
    uint32_t status;
666 28ce5ce6 aurel32
667 ad674e53 Aurelien Jarno
    mask = (ch->regs[DBDMA_CONTROL] >> 16) & 0xffff;
668 ad674e53 Aurelien Jarno
    value = ch->regs[DBDMA_CONTROL] & 0xffff;
669 28ce5ce6 aurel32
670 28ce5ce6 aurel32
    value &= (RUN | PAUSE | FLUSH | WAKE | DEVSTAT);
671 28ce5ce6 aurel32
672 ad674e53 Aurelien Jarno
    status = ch->regs[DBDMA_STATUS];
673 28ce5ce6 aurel32
674 28ce5ce6 aurel32
    status = (value & mask) | (status & ~mask);
675 28ce5ce6 aurel32
676 28ce5ce6 aurel32
    if (status & WAKE)
677 28ce5ce6 aurel32
        status |= ACTIVE;
678 28ce5ce6 aurel32
    if (status & RUN) {
679 28ce5ce6 aurel32
        status |= ACTIVE;
680 28ce5ce6 aurel32
        status &= ~DEAD;
681 28ce5ce6 aurel32
    }
682 28ce5ce6 aurel32
    if (status & PAUSE)
683 28ce5ce6 aurel32
        status &= ~ACTIVE;
684 ad674e53 Aurelien Jarno
    if ((ch->regs[DBDMA_STATUS] & RUN) && !(status & RUN)) {
685 28ce5ce6 aurel32
        /* RUN is cleared */
686 28ce5ce6 aurel32
        status &= ~(ACTIVE|DEAD);
687 28ce5ce6 aurel32
    }
688 28ce5ce6 aurel32
689 28ce5ce6 aurel32
    DBDMA_DPRINTF("    status 0x%08x\n", status);
690 28ce5ce6 aurel32
691 ad674e53 Aurelien Jarno
    ch->regs[DBDMA_STATUS] = status;
692 28ce5ce6 aurel32
693 b42ec42d aurel32
    if (status & ACTIVE)
694 b42ec42d aurel32
        qemu_bh_schedule(dbdma_bh);
695 a9ceb76d Alexander Graf
    if ((status & FLUSH) && ch->flush)
696 862c9280 aurel32
        ch->flush(&ch->io);
697 28ce5ce6 aurel32
}
698 28ce5ce6 aurel32
699 28ce5ce6 aurel32
static void dbdma_writel (void *opaque,
700 c227f099 Anthony Liguori
                          target_phys_addr_t addr, uint32_t value)
701 28ce5ce6 aurel32
{
702 28ce5ce6 aurel32
    int channel = addr >> DBDMA_CHANNEL_SHIFT;
703 28ce5ce6 aurel32
    DBDMA_channel *ch = (DBDMA_channel *)opaque + channel;
704 28ce5ce6 aurel32
    int reg = (addr - (channel << DBDMA_CHANNEL_SHIFT)) >> 2;
705 28ce5ce6 aurel32
706 28ce5ce6 aurel32
    DBDMA_DPRINTF("writel 0x" TARGET_FMT_plx " <= 0x%08x\n", addr, value);
707 28ce5ce6 aurel32
    DBDMA_DPRINTF("channel 0x%x reg 0x%x\n",
708 28ce5ce6 aurel32
                  (uint32_t)addr >> DBDMA_CHANNEL_SHIFT, reg);
709 28ce5ce6 aurel32
710 ad674e53 Aurelien Jarno
#ifdef TARGET_WORDS_BIGENDIAN
711 ad674e53 Aurelien Jarno
    value = bswap32(value);
712 ad674e53 Aurelien Jarno
#endif
713 ad674e53 Aurelien Jarno
714 28ce5ce6 aurel32
    /* cmdptr cannot be modified if channel is RUN or ACTIVE */
715 28ce5ce6 aurel32
716 28ce5ce6 aurel32
    if (reg == DBDMA_CMDPTR_LO &&
717 ad674e53 Aurelien Jarno
        (ch->regs[DBDMA_STATUS] & (RUN | ACTIVE)))
718 28ce5ce6 aurel32
        return;
719 28ce5ce6 aurel32
720 28ce5ce6 aurel32
    ch->regs[reg] = value;
721 28ce5ce6 aurel32
722 28ce5ce6 aurel32
    switch(reg) {
723 28ce5ce6 aurel32
    case DBDMA_CONTROL:
724 28ce5ce6 aurel32
        dbdma_control_write(ch);
725 28ce5ce6 aurel32
        break;
726 28ce5ce6 aurel32
    case DBDMA_CMDPTR_LO:
727 28ce5ce6 aurel32
        /* 16-byte aligned */
728 ad674e53 Aurelien Jarno
        ch->regs[DBDMA_CMDPTR_LO] &= ~0xf;
729 28ce5ce6 aurel32
        dbdma_cmdptr_load(ch);
730 28ce5ce6 aurel32
        break;
731 28ce5ce6 aurel32
    case DBDMA_STATUS:
732 28ce5ce6 aurel32
    case DBDMA_INTR_SEL:
733 28ce5ce6 aurel32
    case DBDMA_BRANCH_SEL:
734 28ce5ce6 aurel32
    case DBDMA_WAIT_SEL:
735 28ce5ce6 aurel32
        /* nothing to do */
736 28ce5ce6 aurel32
        break;
737 28ce5ce6 aurel32
    case DBDMA_XFER_MODE:
738 28ce5ce6 aurel32
    case DBDMA_CMDPTR_HI:
739 28ce5ce6 aurel32
    case DBDMA_DATA2PTR_HI:
740 28ce5ce6 aurel32
    case DBDMA_DATA2PTR_LO:
741 28ce5ce6 aurel32
    case DBDMA_ADDRESS_HI:
742 28ce5ce6 aurel32
    case DBDMA_BRANCH_ADDR_HI:
743 28ce5ce6 aurel32
    case DBDMA_RES1:
744 28ce5ce6 aurel32
    case DBDMA_RES2:
745 28ce5ce6 aurel32
    case DBDMA_RES3:
746 28ce5ce6 aurel32
    case DBDMA_RES4:
747 28ce5ce6 aurel32
        /* unused */
748 28ce5ce6 aurel32
        break;
749 28ce5ce6 aurel32
    }
750 28ce5ce6 aurel32
}
751 28ce5ce6 aurel32
752 c227f099 Anthony Liguori
static uint32_t dbdma_readl (void *opaque, target_phys_addr_t addr)
753 3cbee15b j_mayer
{
754 28ce5ce6 aurel32
    uint32_t value;
755 28ce5ce6 aurel32
    int channel = addr >> DBDMA_CHANNEL_SHIFT;
756 28ce5ce6 aurel32
    DBDMA_channel *ch = (DBDMA_channel *)opaque + channel;
757 28ce5ce6 aurel32
    int reg = (addr - (channel << DBDMA_CHANNEL_SHIFT)) >> 2;
758 ea026b2f blueswir1
759 28ce5ce6 aurel32
    value = ch->regs[reg];
760 28ce5ce6 aurel32
761 28ce5ce6 aurel32
    DBDMA_DPRINTF("readl 0x" TARGET_FMT_plx " => 0x%08x\n", addr, value);
762 28ce5ce6 aurel32
    DBDMA_DPRINTF("channel 0x%x reg 0x%x\n",
763 28ce5ce6 aurel32
                  (uint32_t)addr >> DBDMA_CHANNEL_SHIFT, reg);
764 28ce5ce6 aurel32
765 28ce5ce6 aurel32
    switch(reg) {
766 28ce5ce6 aurel32
    case DBDMA_CONTROL:
767 28ce5ce6 aurel32
        value = 0;
768 28ce5ce6 aurel32
        break;
769 28ce5ce6 aurel32
    case DBDMA_STATUS:
770 28ce5ce6 aurel32
    case DBDMA_CMDPTR_LO:
771 28ce5ce6 aurel32
    case DBDMA_INTR_SEL:
772 28ce5ce6 aurel32
    case DBDMA_BRANCH_SEL:
773 28ce5ce6 aurel32
    case DBDMA_WAIT_SEL:
774 28ce5ce6 aurel32
        /* nothing to do */
775 28ce5ce6 aurel32
        break;
776 28ce5ce6 aurel32
    case DBDMA_XFER_MODE:
777 28ce5ce6 aurel32
    case DBDMA_CMDPTR_HI:
778 28ce5ce6 aurel32
    case DBDMA_DATA2PTR_HI:
779 28ce5ce6 aurel32
    case DBDMA_DATA2PTR_LO:
780 28ce5ce6 aurel32
    case DBDMA_ADDRESS_HI:
781 28ce5ce6 aurel32
    case DBDMA_BRANCH_ADDR_HI:
782 28ce5ce6 aurel32
        /* unused */
783 28ce5ce6 aurel32
        value = 0;
784 28ce5ce6 aurel32
        break;
785 28ce5ce6 aurel32
    case DBDMA_RES1:
786 28ce5ce6 aurel32
    case DBDMA_RES2:
787 28ce5ce6 aurel32
    case DBDMA_RES3:
788 28ce5ce6 aurel32
    case DBDMA_RES4:
789 28ce5ce6 aurel32
        /* reserved */
790 28ce5ce6 aurel32
        break;
791 28ce5ce6 aurel32
    }
792 28ce5ce6 aurel32
793 ad674e53 Aurelien Jarno
#ifdef TARGET_WORDS_BIGENDIAN
794 ad674e53 Aurelien Jarno
    value = bswap32(value);
795 ad674e53 Aurelien Jarno
#endif
796 28ce5ce6 aurel32
    return value;
797 3cbee15b j_mayer
}
798 3cbee15b j_mayer
799 d60efc6b Blue Swirl
static CPUWriteMemoryFunc * const dbdma_write[] = {
800 28ce5ce6 aurel32
    NULL,
801 28ce5ce6 aurel32
    NULL,
802 28ce5ce6 aurel32
    dbdma_writel,
803 3cbee15b j_mayer
};
804 3cbee15b j_mayer
805 d60efc6b Blue Swirl
static CPUReadMemoryFunc * const dbdma_read[] = {
806 28ce5ce6 aurel32
    NULL,
807 28ce5ce6 aurel32
    NULL,
808 28ce5ce6 aurel32
    dbdma_readl,
809 3cbee15b j_mayer
};
810 3cbee15b j_mayer
811 9b64997f blueswir1
static void dbdma_save(QEMUFile *f, void *opaque)
812 9b64997f blueswir1
{
813 28ce5ce6 aurel32
    DBDMA_channel *s = opaque;
814 28ce5ce6 aurel32
    unsigned int i, j;
815 28ce5ce6 aurel32
816 28ce5ce6 aurel32
    for (i = 0; i < DBDMA_CHANNELS; i++)
817 28ce5ce6 aurel32
        for (j = 0; j < DBDMA_REGS; j++)
818 28ce5ce6 aurel32
            qemu_put_be32s(f, &s[i].regs[j]);
819 9b64997f blueswir1
}
820 9b64997f blueswir1
821 9b64997f blueswir1
static int dbdma_load(QEMUFile *f, void *opaque, int version_id)
822 9b64997f blueswir1
{
823 28ce5ce6 aurel32
    DBDMA_channel *s = opaque;
824 28ce5ce6 aurel32
    unsigned int i, j;
825 28ce5ce6 aurel32
826 28ce5ce6 aurel32
    if (version_id != 2)
827 9b64997f blueswir1
        return -EINVAL;
828 9b64997f blueswir1
829 28ce5ce6 aurel32
    for (i = 0; i < DBDMA_CHANNELS; i++)
830 28ce5ce6 aurel32
        for (j = 0; j < DBDMA_REGS; j++)
831 28ce5ce6 aurel32
            qemu_get_be32s(f, &s[i].regs[j]);
832 28ce5ce6 aurel32
833 9b64997f blueswir1
    return 0;
834 9b64997f blueswir1
}
835 9b64997f blueswir1
836 6e6b7363 blueswir1
static void dbdma_reset(void *opaque)
837 6e6b7363 blueswir1
{
838 28ce5ce6 aurel32
    DBDMA_channel *s = opaque;
839 28ce5ce6 aurel32
    int i;
840 28ce5ce6 aurel32
841 28ce5ce6 aurel32
    for (i = 0; i < DBDMA_CHANNELS; i++)
842 28ce5ce6 aurel32
        memset(s[i].regs, 0, DBDMA_SIZE);
843 6e6b7363 blueswir1
}
844 6e6b7363 blueswir1
845 28ce5ce6 aurel32
void* DBDMA_init (int *dbdma_mem_index)
846 3cbee15b j_mayer
{
847 28ce5ce6 aurel32
    DBDMA_channel *s;
848 28ce5ce6 aurel32
849 28ce5ce6 aurel32
    s = qemu_mallocz(sizeof(DBDMA_channel) * DBDMA_CHANNELS);
850 28ce5ce6 aurel32
851 1eed09cb Avi Kivity
    *dbdma_mem_index = cpu_register_io_memory(dbdma_read, dbdma_write, s);
852 28ce5ce6 aurel32
    register_savevm("dbdma", -1, 1, dbdma_save, dbdma_load, s);
853 a08d4367 Jan Kiszka
    qemu_register_reset(dbdma_reset, s);
854 28ce5ce6 aurel32
855 28ce5ce6 aurel32
    dbdma_bh = qemu_bh_new(DBDMA_run_bh, s);
856 28ce5ce6 aurel32
857 28ce5ce6 aurel32
    return s;
858 3cbee15b j_mayer
}