Statistics
| Branch: | Revision:

root / hw / mac_dbdma.c @ b7169916

History | View | Annotate | Download (22.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 ea026b2f blueswir1
#define DBDMA_DPRINTF(fmt, args...) \
48 ea026b2f blueswir1
do { printf("DBDMA: " fmt , ##args); } while (0)
49 ea026b2f blueswir1
#else
50 ea026b2f blueswir1
#define DBDMA_DPRINTF(fmt, args...)
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 28ce5ce6 aurel32
    DBDMA_transfer io;
162 28ce5ce6 aurel32
    DBDMA_transfer_handler transfer_handler;
163 28ce5ce6 aurel32
    dbdma_cmd current;
164 28ce5ce6 aurel32
} DBDMA_channel;
165 28ce5ce6 aurel32
166 28ce5ce6 aurel32
#ifdef DEBUG_DBDMA
167 28ce5ce6 aurel32
static void dump_dbdma_cmd(dbdma_cmd *cmd)
168 28ce5ce6 aurel32
{
169 28ce5ce6 aurel32
    printf("dbdma_cmd %p\n", cmd);
170 28ce5ce6 aurel32
    printf("    req_count 0x%04x\n", le16_to_cpu(cmd->req_count));
171 28ce5ce6 aurel32
    printf("    command 0x%04x\n", le16_to_cpu(cmd->command));
172 28ce5ce6 aurel32
    printf("    phy_addr 0x%08x\n", le32_to_cpu(cmd->phy_addr));
173 28ce5ce6 aurel32
    printf("    cmd_dep 0x%08x\n", le32_to_cpu(cmd->cmd_dep));
174 28ce5ce6 aurel32
    printf("    res_count 0x%04x\n", le16_to_cpu(cmd->res_count));
175 28ce5ce6 aurel32
    printf("    xfer_status 0x%04x\n", le16_to_cpu(cmd->xfer_status));
176 28ce5ce6 aurel32
}
177 28ce5ce6 aurel32
#else
178 28ce5ce6 aurel32
static void dump_dbdma_cmd(dbdma_cmd *cmd)
179 3cbee15b j_mayer
{
180 28ce5ce6 aurel32
}
181 28ce5ce6 aurel32
#endif
182 28ce5ce6 aurel32
static void dbdma_cmdptr_load(DBDMA_channel *ch)
183 28ce5ce6 aurel32
{
184 28ce5ce6 aurel32
    DBDMA_DPRINTF("dbdma_cmdptr_load 0x%08x\n",
185 28ce5ce6 aurel32
                  be32_to_cpu(ch->regs[DBDMA_CMDPTR_LO]));
186 28ce5ce6 aurel32
    cpu_physical_memory_read(be32_to_cpu(ch->regs[DBDMA_CMDPTR_LO]),
187 28ce5ce6 aurel32
                             (uint8_t*)&ch->current, sizeof(dbdma_cmd));
188 3cbee15b j_mayer
}
189 3cbee15b j_mayer
190 28ce5ce6 aurel32
static void dbdma_cmdptr_save(DBDMA_channel *ch)
191 3cbee15b j_mayer
{
192 28ce5ce6 aurel32
    DBDMA_DPRINTF("dbdma_cmdptr_save 0x%08x\n",
193 28ce5ce6 aurel32
                  be32_to_cpu(ch->regs[DBDMA_CMDPTR_LO]));
194 28ce5ce6 aurel32
    DBDMA_DPRINTF("xfer_status 0x%08x res_count 0x%04x\n",
195 28ce5ce6 aurel32
                  le16_to_cpu(ch->current.xfer_status),
196 28ce5ce6 aurel32
                  le16_to_cpu(ch->current.res_count));
197 28ce5ce6 aurel32
    cpu_physical_memory_write(be32_to_cpu(ch->regs[DBDMA_CMDPTR_LO]),
198 28ce5ce6 aurel32
                              (uint8_t*)&ch->current, sizeof(dbdma_cmd));
199 3cbee15b j_mayer
}
200 3cbee15b j_mayer
201 28ce5ce6 aurel32
static void kill_channel(DBDMA_channel *ch)
202 3cbee15b j_mayer
{
203 28ce5ce6 aurel32
    DBDMA_DPRINTF("kill_channel\n");
204 28ce5ce6 aurel32
205 28ce5ce6 aurel32
    ch->regs[DBDMA_STATUS] |= cpu_to_be32(DEAD);
206 28ce5ce6 aurel32
    ch->regs[DBDMA_STATUS] &= cpu_to_be32(~ACTIVE);
207 28ce5ce6 aurel32
208 28ce5ce6 aurel32
    qemu_irq_raise(ch->irq);
209 28ce5ce6 aurel32
}
210 28ce5ce6 aurel32
211 28ce5ce6 aurel32
static void conditional_interrupt(DBDMA_channel *ch)
212 28ce5ce6 aurel32
{
213 28ce5ce6 aurel32
    dbdma_cmd *current = &ch->current;
214 28ce5ce6 aurel32
    uint16_t intr;
215 28ce5ce6 aurel32
    uint16_t sel_mask, sel_value;
216 28ce5ce6 aurel32
    uint32_t status;
217 28ce5ce6 aurel32
    int cond;
218 28ce5ce6 aurel32
219 28ce5ce6 aurel32
    DBDMA_DPRINTF("conditional_interrupt\n");
220 28ce5ce6 aurel32
221 28ce5ce6 aurel32
    intr = be16_to_cpu(current->command) & INTR_MASK;
222 28ce5ce6 aurel32
223 28ce5ce6 aurel32
    switch(intr) {
224 28ce5ce6 aurel32
    case INTR_NEVER:  /* don't interrupt */
225 28ce5ce6 aurel32
        return;
226 28ce5ce6 aurel32
    case INTR_ALWAYS: /* always interrupt */
227 28ce5ce6 aurel32
        qemu_irq_raise(ch->irq);
228 28ce5ce6 aurel32
        return;
229 28ce5ce6 aurel32
    }
230 28ce5ce6 aurel32
231 28ce5ce6 aurel32
    status = be32_to_cpu(ch->regs[DBDMA_STATUS]) & DEVSTAT;
232 28ce5ce6 aurel32
233 28ce5ce6 aurel32
    sel_mask = (be32_to_cpu(ch->regs[DBDMA_INTR_SEL]) >> 16) & 0x0f;
234 28ce5ce6 aurel32
    sel_value = be32_to_cpu(ch->regs[DBDMA_INTR_SEL]) & 0x0f;
235 28ce5ce6 aurel32
236 28ce5ce6 aurel32
    cond = (status & sel_mask) == (sel_value & sel_mask);
237 28ce5ce6 aurel32
238 28ce5ce6 aurel32
    switch(intr) {
239 28ce5ce6 aurel32
    case INTR_IFSET:  /* intr if condition bit is 1 */
240 28ce5ce6 aurel32
        if (cond)
241 28ce5ce6 aurel32
            qemu_irq_raise(ch->irq);
242 28ce5ce6 aurel32
        return;
243 28ce5ce6 aurel32
    case INTR_IFCLR:  /* intr if condition bit is 0 */
244 28ce5ce6 aurel32
        if (!cond)
245 28ce5ce6 aurel32
            qemu_irq_raise(ch->irq);
246 28ce5ce6 aurel32
        return;
247 28ce5ce6 aurel32
    }
248 28ce5ce6 aurel32
}
249 28ce5ce6 aurel32
250 28ce5ce6 aurel32
static int conditional_wait(DBDMA_channel *ch)
251 28ce5ce6 aurel32
{
252 28ce5ce6 aurel32
    dbdma_cmd *current = &ch->current;
253 28ce5ce6 aurel32
    uint16_t wait;
254 28ce5ce6 aurel32
    uint16_t sel_mask, sel_value;
255 28ce5ce6 aurel32
    uint32_t status;
256 28ce5ce6 aurel32
    int cond;
257 28ce5ce6 aurel32
258 28ce5ce6 aurel32
    DBDMA_DPRINTF("conditional_wait\n");
259 28ce5ce6 aurel32
260 28ce5ce6 aurel32
    wait = be16_to_cpu(current->command) & WAIT_MASK;
261 28ce5ce6 aurel32
262 28ce5ce6 aurel32
    switch(wait) {
263 28ce5ce6 aurel32
    case WAIT_NEVER:  /* don't wait */
264 28ce5ce6 aurel32
        return 0;
265 28ce5ce6 aurel32
    case WAIT_ALWAYS: /* always wait */
266 28ce5ce6 aurel32
        return 1;
267 28ce5ce6 aurel32
    }
268 28ce5ce6 aurel32
269 28ce5ce6 aurel32
    status = be32_to_cpu(ch->regs[DBDMA_STATUS]) & DEVSTAT;
270 28ce5ce6 aurel32
271 28ce5ce6 aurel32
    sel_mask = (be32_to_cpu(ch->regs[DBDMA_WAIT_SEL]) >> 16) & 0x0f;
272 28ce5ce6 aurel32
    sel_value = be32_to_cpu(ch->regs[DBDMA_WAIT_SEL]) & 0x0f;
273 28ce5ce6 aurel32
274 28ce5ce6 aurel32
    cond = (status & sel_mask) == (sel_value & sel_mask);
275 28ce5ce6 aurel32
276 28ce5ce6 aurel32
    switch(wait) {
277 28ce5ce6 aurel32
    case WAIT_IFSET:  /* wait if condition bit is 1 */
278 28ce5ce6 aurel32
        if (cond)
279 28ce5ce6 aurel32
            return 1;
280 28ce5ce6 aurel32
        return 0;
281 28ce5ce6 aurel32
    case WAIT_IFCLR:  /* wait if condition bit is 0 */
282 28ce5ce6 aurel32
        if (!cond)
283 28ce5ce6 aurel32
            return 1;
284 28ce5ce6 aurel32
        return 0;
285 28ce5ce6 aurel32
    }
286 28ce5ce6 aurel32
    return 0;
287 28ce5ce6 aurel32
}
288 28ce5ce6 aurel32
289 28ce5ce6 aurel32
static void next(DBDMA_channel *ch)
290 28ce5ce6 aurel32
{
291 28ce5ce6 aurel32
    uint32_t cp;
292 28ce5ce6 aurel32
293 28ce5ce6 aurel32
    ch->regs[DBDMA_STATUS] &= cpu_to_be32(~BT);
294 28ce5ce6 aurel32
295 28ce5ce6 aurel32
    cp = be32_to_cpu(ch->regs[DBDMA_CMDPTR_LO]);
296 28ce5ce6 aurel32
    ch->regs[DBDMA_CMDPTR_LO] = cpu_to_be32(cp + sizeof(dbdma_cmd));
297 28ce5ce6 aurel32
    dbdma_cmdptr_load(ch);
298 28ce5ce6 aurel32
}
299 28ce5ce6 aurel32
300 28ce5ce6 aurel32
static void branch(DBDMA_channel *ch)
301 28ce5ce6 aurel32
{
302 28ce5ce6 aurel32
    dbdma_cmd *current = &ch->current;
303 28ce5ce6 aurel32
304 28ce5ce6 aurel32
    ch->regs[DBDMA_CMDPTR_LO] = current->cmd_dep;
305 28ce5ce6 aurel32
    ch->regs[DBDMA_STATUS] |= cpu_to_be32(BT);
306 28ce5ce6 aurel32
    dbdma_cmdptr_load(ch);
307 28ce5ce6 aurel32
}
308 28ce5ce6 aurel32
309 28ce5ce6 aurel32
static void conditional_branch(DBDMA_channel *ch)
310 28ce5ce6 aurel32
{
311 28ce5ce6 aurel32
    dbdma_cmd *current = &ch->current;
312 28ce5ce6 aurel32
    uint16_t br;
313 28ce5ce6 aurel32
    uint16_t sel_mask, sel_value;
314 28ce5ce6 aurel32
    uint32_t status;
315 28ce5ce6 aurel32
    int cond;
316 28ce5ce6 aurel32
317 28ce5ce6 aurel32
    DBDMA_DPRINTF("conditional_branch\n");
318 28ce5ce6 aurel32
319 28ce5ce6 aurel32
    /* check if we must branch */
320 28ce5ce6 aurel32
321 28ce5ce6 aurel32
    br = be16_to_cpu(current->command) & BR_MASK;
322 28ce5ce6 aurel32
323 28ce5ce6 aurel32
    switch(br) {
324 28ce5ce6 aurel32
    case BR_NEVER:  /* don't branch */
325 28ce5ce6 aurel32
        next(ch);
326 28ce5ce6 aurel32
        return;
327 28ce5ce6 aurel32
    case BR_ALWAYS: /* always branch */
328 28ce5ce6 aurel32
        branch(ch);
329 28ce5ce6 aurel32
        return;
330 28ce5ce6 aurel32
    }
331 28ce5ce6 aurel32
332 28ce5ce6 aurel32
    status = be32_to_cpu(ch->regs[DBDMA_STATUS]) & DEVSTAT;
333 28ce5ce6 aurel32
334 28ce5ce6 aurel32
    sel_mask = (be32_to_cpu(ch->regs[DBDMA_BRANCH_SEL]) >> 16) & 0x0f;
335 28ce5ce6 aurel32
    sel_value = be32_to_cpu(ch->regs[DBDMA_BRANCH_SEL]) & 0x0f;
336 28ce5ce6 aurel32
337 28ce5ce6 aurel32
    cond = (status & sel_mask) == (sel_value & sel_mask);
338 28ce5ce6 aurel32
339 28ce5ce6 aurel32
    switch(br) {
340 28ce5ce6 aurel32
    case BR_IFSET:  /* branch if condition bit is 1 */
341 28ce5ce6 aurel32
        if (cond)
342 28ce5ce6 aurel32
            branch(ch);
343 28ce5ce6 aurel32
        else
344 28ce5ce6 aurel32
            next(ch);
345 28ce5ce6 aurel32
        return;
346 28ce5ce6 aurel32
    case BR_IFCLR:  /* branch if condition bit is 0 */
347 28ce5ce6 aurel32
        if (!cond)
348 28ce5ce6 aurel32
            branch(ch);
349 28ce5ce6 aurel32
        else
350 28ce5ce6 aurel32
            next(ch);
351 28ce5ce6 aurel32
        return;
352 28ce5ce6 aurel32
    }
353 28ce5ce6 aurel32
}
354 28ce5ce6 aurel32
355 28ce5ce6 aurel32
static int dbdma_read_memory(DBDMA_transfer *io)
356 28ce5ce6 aurel32
{
357 28ce5ce6 aurel32
    DBDMA_channel *ch = io->channel;
358 28ce5ce6 aurel32
    dbdma_cmd *current = &ch->current;
359 28ce5ce6 aurel32
360 28ce5ce6 aurel32
    DBDMA_DPRINTF("DBDMA_read_memory\n");
361 28ce5ce6 aurel32
362 28ce5ce6 aurel32
    cpu_physical_memory_read(le32_to_cpu(current->phy_addr) + io->buf_pos,
363 28ce5ce6 aurel32
                             io->buf, io->buf_len);
364 28ce5ce6 aurel32
365 28ce5ce6 aurel32
    return io->buf_len;
366 28ce5ce6 aurel32
}
367 28ce5ce6 aurel32
368 28ce5ce6 aurel32
static int dbdma_write_memory(DBDMA_transfer *io)
369 28ce5ce6 aurel32
{
370 28ce5ce6 aurel32
    DBDMA_channel *ch = io->channel;
371 28ce5ce6 aurel32
    dbdma_cmd *current = &ch->current;
372 28ce5ce6 aurel32
373 28ce5ce6 aurel32
    DBDMA_DPRINTF("DBDMA_write_memory\n");
374 28ce5ce6 aurel32
375 28ce5ce6 aurel32
    cpu_physical_memory_write(le32_to_cpu(current->phy_addr) + io->buf_pos,
376 28ce5ce6 aurel32
                              io->buf, io->buf_len);
377 28ce5ce6 aurel32
378 28ce5ce6 aurel32
    return io->buf_len;
379 28ce5ce6 aurel32
}
380 28ce5ce6 aurel32
381 28ce5ce6 aurel32
static int start_output(DBDMA_channel *ch, int key, uint32_t addr,
382 28ce5ce6 aurel32
                        uint16_t req_count, int is_last)
383 28ce5ce6 aurel32
{
384 28ce5ce6 aurel32
    dbdma_cmd *current = &ch->current;
385 28ce5ce6 aurel32
    uint32_t n;
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 28ce5ce6 aurel32
        return 0;
397 28ce5ce6 aurel32
    }
398 28ce5ce6 aurel32
399 28ce5ce6 aurel32
    ch->io.buf = NULL;
400 28ce5ce6 aurel32
    ch->io.buf_pos = 0;
401 28ce5ce6 aurel32
    ch->io.buf_len = 0;
402 28ce5ce6 aurel32
    ch->io.len = req_count;
403 28ce5ce6 aurel32
    ch->io.is_last = is_last;
404 28ce5ce6 aurel32
    n = ch->transfer_handler(&ch->io, dbdma_read_memory);
405 28ce5ce6 aurel32
406 28ce5ce6 aurel32
    if (conditional_wait(ch))
407 28ce5ce6 aurel32
        return 1;
408 28ce5ce6 aurel32
409 28ce5ce6 aurel32
    current->xfer_status = cpu_to_le16(be32_to_cpu(ch->regs[DBDMA_STATUS]));
410 28ce5ce6 aurel32
    current->res_count = cpu_to_le16(0);
411 28ce5ce6 aurel32
    dbdma_cmdptr_save(ch);
412 28ce5ce6 aurel32
413 28ce5ce6 aurel32
    conditional_interrupt(ch);
414 28ce5ce6 aurel32
    conditional_branch(ch);
415 28ce5ce6 aurel32
416 28ce5ce6 aurel32
    return 1;
417 28ce5ce6 aurel32
}
418 28ce5ce6 aurel32
419 28ce5ce6 aurel32
static int start_input(DBDMA_channel *ch, int key, uint32_t addr,
420 28ce5ce6 aurel32
                       uint16_t req_count, int is_last)
421 28ce5ce6 aurel32
{
422 28ce5ce6 aurel32
    dbdma_cmd *current = &ch->current;
423 28ce5ce6 aurel32
    uint32_t n;
424 28ce5ce6 aurel32
425 28ce5ce6 aurel32
    DBDMA_DPRINTF("start_input\n");
426 28ce5ce6 aurel32
427 28ce5ce6 aurel32
    /* KEY_REGS, KEY_DEVICE and KEY_STREAM
428 28ce5ce6 aurel32
     * are not implemented in the mac-io chip
429 28ce5ce6 aurel32
     */
430 28ce5ce6 aurel32
431 28ce5ce6 aurel32
    if (!addr || key > KEY_STREAM3) {
432 28ce5ce6 aurel32
        kill_channel(ch);
433 28ce5ce6 aurel32
        return 0;
434 28ce5ce6 aurel32
    }
435 28ce5ce6 aurel32
436 28ce5ce6 aurel32
    ch->io.buf = NULL;
437 28ce5ce6 aurel32
    ch->io.buf_pos = 0;
438 28ce5ce6 aurel32
    ch->io.buf_len = 0;
439 28ce5ce6 aurel32
    ch->io.len = req_count;
440 28ce5ce6 aurel32
    ch->io.is_last = is_last;
441 28ce5ce6 aurel32
    n = ch->transfer_handler(&ch->io, dbdma_write_memory);
442 28ce5ce6 aurel32
443 28ce5ce6 aurel32
    if (conditional_wait(ch))
444 28ce5ce6 aurel32
        return 1;
445 28ce5ce6 aurel32
446 28ce5ce6 aurel32
    current->xfer_status = cpu_to_le16(be32_to_cpu(ch->regs[DBDMA_STATUS]));
447 28ce5ce6 aurel32
    current->res_count = cpu_to_le16(0);
448 28ce5ce6 aurel32
    dbdma_cmdptr_save(ch);
449 28ce5ce6 aurel32
450 28ce5ce6 aurel32
    conditional_interrupt(ch);
451 28ce5ce6 aurel32
    conditional_branch(ch);
452 28ce5ce6 aurel32
453 28ce5ce6 aurel32
    return 1;
454 28ce5ce6 aurel32
}
455 28ce5ce6 aurel32
456 28ce5ce6 aurel32
static int load_word(DBDMA_channel *ch, int key, uint32_t addr,
457 28ce5ce6 aurel32
                     uint16_t len)
458 28ce5ce6 aurel32
{
459 28ce5ce6 aurel32
    dbdma_cmd *current = &ch->current;
460 28ce5ce6 aurel32
    uint32_t val;
461 28ce5ce6 aurel32
462 28ce5ce6 aurel32
    DBDMA_DPRINTF("load_word\n");
463 28ce5ce6 aurel32
464 28ce5ce6 aurel32
    /* only implements KEY_SYSTEM */
465 28ce5ce6 aurel32
466 28ce5ce6 aurel32
    if (key != KEY_SYSTEM) {
467 28ce5ce6 aurel32
        printf("DBDMA: LOAD_WORD, unimplemented key %x\n", key);
468 28ce5ce6 aurel32
        kill_channel(ch);
469 28ce5ce6 aurel32
        return 0;
470 28ce5ce6 aurel32
    }
471 28ce5ce6 aurel32
472 28ce5ce6 aurel32
    cpu_physical_memory_read(addr, (uint8_t*)&val, len);
473 28ce5ce6 aurel32
474 28ce5ce6 aurel32
    if (len == 2)
475 28ce5ce6 aurel32
        val = (val << 16) | (current->cmd_dep & 0x0000ffff);
476 28ce5ce6 aurel32
    else if (len == 1)
477 28ce5ce6 aurel32
        val = (val << 24) | (current->cmd_dep & 0x00ffffff);
478 28ce5ce6 aurel32
479 28ce5ce6 aurel32
    current->cmd_dep = val;
480 28ce5ce6 aurel32
481 28ce5ce6 aurel32
    if (conditional_wait(ch))
482 28ce5ce6 aurel32
        return 1;
483 28ce5ce6 aurel32
484 28ce5ce6 aurel32
    current->xfer_status = cpu_to_le16(be32_to_cpu(ch->regs[DBDMA_STATUS]));
485 28ce5ce6 aurel32
    dbdma_cmdptr_save(ch);
486 28ce5ce6 aurel32
487 28ce5ce6 aurel32
    conditional_interrupt(ch);
488 28ce5ce6 aurel32
    next(ch);
489 28ce5ce6 aurel32
490 28ce5ce6 aurel32
    return 1;
491 28ce5ce6 aurel32
}
492 28ce5ce6 aurel32
493 28ce5ce6 aurel32
static int store_word(DBDMA_channel *ch, int key, uint32_t addr,
494 28ce5ce6 aurel32
                      uint16_t len)
495 28ce5ce6 aurel32
{
496 28ce5ce6 aurel32
    dbdma_cmd *current = &ch->current;
497 28ce5ce6 aurel32
    uint32_t val;
498 28ce5ce6 aurel32
499 28ce5ce6 aurel32
    DBDMA_DPRINTF("store_word\n");
500 28ce5ce6 aurel32
501 28ce5ce6 aurel32
    /* only implements KEY_SYSTEM */
502 28ce5ce6 aurel32
503 28ce5ce6 aurel32
    if (key != KEY_SYSTEM) {
504 28ce5ce6 aurel32
        printf("DBDMA: STORE_WORD, unimplemented key %x\n", key);
505 28ce5ce6 aurel32
        kill_channel(ch);
506 28ce5ce6 aurel32
        return 0;
507 28ce5ce6 aurel32
    }
508 28ce5ce6 aurel32
509 28ce5ce6 aurel32
    val = current->cmd_dep;
510 28ce5ce6 aurel32
    if (len == 2)
511 28ce5ce6 aurel32
        val >>= 16;
512 28ce5ce6 aurel32
    else if (len == 1)
513 28ce5ce6 aurel32
        val >>= 24;
514 28ce5ce6 aurel32
515 28ce5ce6 aurel32
    cpu_physical_memory_write(addr, (uint8_t*)&val, len);
516 28ce5ce6 aurel32
517 28ce5ce6 aurel32
    if (conditional_wait(ch))
518 28ce5ce6 aurel32
        return 1;
519 28ce5ce6 aurel32
520 28ce5ce6 aurel32
    current->xfer_status = cpu_to_le16(be32_to_cpu(ch->regs[DBDMA_STATUS]));
521 28ce5ce6 aurel32
    dbdma_cmdptr_save(ch);
522 28ce5ce6 aurel32
523 28ce5ce6 aurel32
    conditional_interrupt(ch);
524 28ce5ce6 aurel32
    next(ch);
525 28ce5ce6 aurel32
526 28ce5ce6 aurel32
    return 1;
527 28ce5ce6 aurel32
}
528 28ce5ce6 aurel32
529 28ce5ce6 aurel32
static int nop(DBDMA_channel *ch)
530 28ce5ce6 aurel32
{
531 28ce5ce6 aurel32
    dbdma_cmd *current = &ch->current;
532 28ce5ce6 aurel32
533 28ce5ce6 aurel32
    if (conditional_wait(ch))
534 28ce5ce6 aurel32
        return 1;
535 28ce5ce6 aurel32
536 28ce5ce6 aurel32
    current->xfer_status = cpu_to_le16(be32_to_cpu(ch->regs[DBDMA_STATUS]));
537 28ce5ce6 aurel32
    dbdma_cmdptr_save(ch);
538 28ce5ce6 aurel32
539 28ce5ce6 aurel32
    conditional_interrupt(ch);
540 28ce5ce6 aurel32
    conditional_branch(ch);
541 28ce5ce6 aurel32
542 28ce5ce6 aurel32
    return 1;
543 3cbee15b j_mayer
}
544 3cbee15b j_mayer
545 28ce5ce6 aurel32
static int stop(DBDMA_channel *ch)
546 3cbee15b j_mayer
{
547 28ce5ce6 aurel32
    ch->regs[DBDMA_STATUS] &= cpu_to_be32(~(ACTIVE|DEAD));
548 28ce5ce6 aurel32
549 28ce5ce6 aurel32
    /* the stop command does not increment command pointer */
550 3cbee15b j_mayer
551 3cbee15b j_mayer
    return 0;
552 3cbee15b j_mayer
}
553 3cbee15b j_mayer
554 28ce5ce6 aurel32
static int channel_run(DBDMA_channel *ch)
555 3cbee15b j_mayer
{
556 28ce5ce6 aurel32
    dbdma_cmd *current = &ch->current;
557 28ce5ce6 aurel32
    uint16_t cmd, key;
558 28ce5ce6 aurel32
    uint16_t req_count;
559 28ce5ce6 aurel32
    uint32_t phy_addr;
560 28ce5ce6 aurel32
561 28ce5ce6 aurel32
    DBDMA_DPRINTF("channel_run\n");
562 28ce5ce6 aurel32
    dump_dbdma_cmd(current);
563 28ce5ce6 aurel32
564 28ce5ce6 aurel32
    /* clear WAKE flag at command fetch */
565 28ce5ce6 aurel32
566 28ce5ce6 aurel32
    ch->regs[DBDMA_STATUS] &= cpu_to_be32(~WAKE);
567 28ce5ce6 aurel32
568 28ce5ce6 aurel32
    cmd = le16_to_cpu(current->command) & COMMAND_MASK;
569 28ce5ce6 aurel32
570 28ce5ce6 aurel32
    switch (cmd) {
571 28ce5ce6 aurel32
    case DBDMA_NOP:
572 28ce5ce6 aurel32
        return nop(ch);
573 28ce5ce6 aurel32
574 28ce5ce6 aurel32
    case DBDMA_STOP:
575 28ce5ce6 aurel32
        return stop(ch);
576 28ce5ce6 aurel32
    }
577 28ce5ce6 aurel32
578 28ce5ce6 aurel32
    key = le16_to_cpu(current->command) & 0x0700;
579 28ce5ce6 aurel32
    req_count = le16_to_cpu(current->req_count);
580 28ce5ce6 aurel32
    phy_addr = le32_to_cpu(current->phy_addr);
581 28ce5ce6 aurel32
582 28ce5ce6 aurel32
    if (key == KEY_STREAM4) {
583 28ce5ce6 aurel32
        printf("command %x, invalid key 4\n", cmd);
584 28ce5ce6 aurel32
        kill_channel(ch);
585 28ce5ce6 aurel32
        return 0;
586 28ce5ce6 aurel32
    }
587 28ce5ce6 aurel32
588 28ce5ce6 aurel32
    switch (cmd) {
589 28ce5ce6 aurel32
    case OUTPUT_MORE:
590 28ce5ce6 aurel32
        return start_output(ch, key, phy_addr, req_count, 0);
591 28ce5ce6 aurel32
592 28ce5ce6 aurel32
    case OUTPUT_LAST:
593 28ce5ce6 aurel32
        return start_output(ch, key, phy_addr, req_count, 1);
594 28ce5ce6 aurel32
595 28ce5ce6 aurel32
    case INPUT_MORE:
596 28ce5ce6 aurel32
        return start_input(ch, key, phy_addr, req_count, 0);
597 28ce5ce6 aurel32
598 28ce5ce6 aurel32
    case INPUT_LAST:
599 28ce5ce6 aurel32
        return start_input(ch, key, phy_addr, req_count, 1);
600 28ce5ce6 aurel32
    }
601 28ce5ce6 aurel32
602 28ce5ce6 aurel32
    if (key < KEY_REGS) {
603 28ce5ce6 aurel32
        printf("command %x, invalid key %x\n", cmd, key);
604 28ce5ce6 aurel32
        key = KEY_SYSTEM;
605 28ce5ce6 aurel32
    }
606 28ce5ce6 aurel32
607 28ce5ce6 aurel32
    /* for LOAD_WORD and STORE_WORD, req_count is on 3 bits
608 28ce5ce6 aurel32
     * and BRANCH is invalid
609 28ce5ce6 aurel32
     */
610 28ce5ce6 aurel32
611 28ce5ce6 aurel32
    req_count = req_count & 0x0007;
612 28ce5ce6 aurel32
    if (req_count & 0x4) {
613 28ce5ce6 aurel32
        req_count = 4;
614 28ce5ce6 aurel32
        phy_addr &= ~3;
615 28ce5ce6 aurel32
    } else if (req_count & 0x2) {
616 28ce5ce6 aurel32
        req_count = 2;
617 28ce5ce6 aurel32
        phy_addr &= ~1;
618 28ce5ce6 aurel32
    } else
619 28ce5ce6 aurel32
        req_count = 1;
620 28ce5ce6 aurel32
621 28ce5ce6 aurel32
    switch (cmd) {
622 28ce5ce6 aurel32
    case LOAD_WORD:
623 28ce5ce6 aurel32
        return load_word(ch, key, phy_addr, req_count);
624 28ce5ce6 aurel32
625 28ce5ce6 aurel32
    case STORE_WORD:
626 28ce5ce6 aurel32
        return store_word(ch, key, phy_addr, req_count);
627 28ce5ce6 aurel32
    }
628 ea026b2f blueswir1
629 3cbee15b j_mayer
    return 0;
630 3cbee15b j_mayer
}
631 3cbee15b j_mayer
632 28ce5ce6 aurel32
static QEMUBH *dbdma_bh;
633 28ce5ce6 aurel32
634 28ce5ce6 aurel32
static void DBDMA_run (DBDMA_channel *ch)
635 28ce5ce6 aurel32
{
636 28ce5ce6 aurel32
    int channel;
637 28ce5ce6 aurel32
    int rearm = 0;
638 28ce5ce6 aurel32
639 28ce5ce6 aurel32
    for (channel = 0; channel < DBDMA_CHANNELS; channel++, ch++) {
640 28ce5ce6 aurel32
            uint32_t status = be32_to_cpu(ch->regs[DBDMA_STATUS]);
641 28ce5ce6 aurel32
            if ((status & RUN) && (status & ACTIVE)) {
642 28ce5ce6 aurel32
                if (status & FLUSH)
643 28ce5ce6 aurel32
                    while (channel_run(ch));
644 28ce5ce6 aurel32
                else if (channel_run(ch))
645 28ce5ce6 aurel32
                    rearm = 1;
646 28ce5ce6 aurel32
            }
647 28ce5ce6 aurel32
            ch->regs[DBDMA_STATUS] &= cpu_to_be32(~FLUSH);
648 28ce5ce6 aurel32
    }
649 28ce5ce6 aurel32
650 28ce5ce6 aurel32
    if (rearm)
651 28ce5ce6 aurel32
        qemu_bh_schedule_idle(dbdma_bh);
652 28ce5ce6 aurel32
}
653 28ce5ce6 aurel32
654 28ce5ce6 aurel32
static void DBDMA_run_bh(void *opaque)
655 28ce5ce6 aurel32
{
656 28ce5ce6 aurel32
    DBDMA_channel *ch = opaque;
657 28ce5ce6 aurel32
658 28ce5ce6 aurel32
    DBDMA_DPRINTF("DBDMA_run_bh\n");
659 28ce5ce6 aurel32
660 28ce5ce6 aurel32
    DBDMA_run(ch);
661 28ce5ce6 aurel32
}
662 28ce5ce6 aurel32
663 28ce5ce6 aurel32
void DBDMA_register_channel(void *dbdma, int nchan, qemu_irq irq,
664 28ce5ce6 aurel32
                            DBDMA_transfer_handler transfer_handler,
665 28ce5ce6 aurel32
                            void *opaque)
666 28ce5ce6 aurel32
{
667 28ce5ce6 aurel32
    DBDMA_channel *ch = ( DBDMA_channel *)dbdma + nchan;
668 28ce5ce6 aurel32
669 28ce5ce6 aurel32
    DBDMA_DPRINTF("DBDMA_register_channel 0x%x\n", nchan);
670 28ce5ce6 aurel32
671 28ce5ce6 aurel32
    ch->irq = irq;
672 28ce5ce6 aurel32
    ch->channel = nchan;
673 28ce5ce6 aurel32
    ch->transfer_handler = transfer_handler;
674 28ce5ce6 aurel32
    ch->io.opaque = opaque;
675 28ce5ce6 aurel32
    ch->io.channel = ch;
676 28ce5ce6 aurel32
}
677 28ce5ce6 aurel32
678 28ce5ce6 aurel32
void DBDMA_schedule(void)
679 28ce5ce6 aurel32
{
680 28ce5ce6 aurel32
    CPUState *env = cpu_single_env;
681 28ce5ce6 aurel32
    if (env)
682 28ce5ce6 aurel32
        cpu_interrupt(env, CPU_INTERRUPT_EXIT);
683 28ce5ce6 aurel32
}
684 28ce5ce6 aurel32
685 28ce5ce6 aurel32
static void
686 28ce5ce6 aurel32
dbdma_control_write(DBDMA_channel *ch)
687 28ce5ce6 aurel32
{
688 28ce5ce6 aurel32
    uint16_t mask, value;
689 28ce5ce6 aurel32
    uint32_t status;
690 28ce5ce6 aurel32
691 28ce5ce6 aurel32
    mask = (be32_to_cpu(ch->regs[DBDMA_CONTROL]) >> 16) & 0xffff;
692 28ce5ce6 aurel32
    value = be32_to_cpu(ch->regs[DBDMA_CONTROL]) & 0xffff;
693 28ce5ce6 aurel32
694 28ce5ce6 aurel32
    value &= (RUN | PAUSE | FLUSH | WAKE | DEVSTAT);
695 28ce5ce6 aurel32
696 28ce5ce6 aurel32
    status = be32_to_cpu(ch->regs[DBDMA_STATUS]);
697 28ce5ce6 aurel32
698 28ce5ce6 aurel32
    status = (value & mask) | (status & ~mask);
699 28ce5ce6 aurel32
700 28ce5ce6 aurel32
    if (status & WAKE)
701 28ce5ce6 aurel32
        status |= ACTIVE;
702 28ce5ce6 aurel32
    if (status & RUN) {
703 28ce5ce6 aurel32
        status |= ACTIVE;
704 28ce5ce6 aurel32
        status &= ~DEAD;
705 28ce5ce6 aurel32
    }
706 28ce5ce6 aurel32
    if (status & PAUSE)
707 28ce5ce6 aurel32
        status &= ~ACTIVE;
708 28ce5ce6 aurel32
    if ((be32_to_cpu(ch->regs[DBDMA_STATUS]) & RUN) && !(status & RUN)) {
709 28ce5ce6 aurel32
        /* RUN is cleared */
710 28ce5ce6 aurel32
        status &= ~(ACTIVE|DEAD);
711 28ce5ce6 aurel32
    }
712 28ce5ce6 aurel32
713 28ce5ce6 aurel32
    DBDMA_DPRINTF("    status 0x%08x\n", status);
714 28ce5ce6 aurel32
715 28ce5ce6 aurel32
    ch->regs[DBDMA_STATUS] = cpu_to_be32(status);
716 28ce5ce6 aurel32
717 28ce5ce6 aurel32
    if (status & ACTIVE) {
718 28ce5ce6 aurel32
        qemu_bh_schedule_idle(dbdma_bh);
719 28ce5ce6 aurel32
        if (status & FLUSH)
720 28ce5ce6 aurel32
            DBDMA_schedule();
721 28ce5ce6 aurel32
    }
722 28ce5ce6 aurel32
}
723 28ce5ce6 aurel32
724 28ce5ce6 aurel32
static void dbdma_writel (void *opaque,
725 28ce5ce6 aurel32
                          target_phys_addr_t addr, uint32_t value)
726 28ce5ce6 aurel32
{
727 28ce5ce6 aurel32
    int channel = addr >> DBDMA_CHANNEL_SHIFT;
728 28ce5ce6 aurel32
    DBDMA_channel *ch = (DBDMA_channel *)opaque + channel;
729 28ce5ce6 aurel32
    int reg = (addr - (channel << DBDMA_CHANNEL_SHIFT)) >> 2;
730 28ce5ce6 aurel32
731 28ce5ce6 aurel32
    DBDMA_DPRINTF("writel 0x" TARGET_FMT_plx " <= 0x%08x\n", addr, value);
732 28ce5ce6 aurel32
    DBDMA_DPRINTF("channel 0x%x reg 0x%x\n",
733 28ce5ce6 aurel32
                  (uint32_t)addr >> DBDMA_CHANNEL_SHIFT, reg);
734 28ce5ce6 aurel32
735 28ce5ce6 aurel32
    /* cmdptr cannot be modified if channel is RUN or ACTIVE */
736 28ce5ce6 aurel32
737 28ce5ce6 aurel32
    if (reg == DBDMA_CMDPTR_LO &&
738 28ce5ce6 aurel32
        (ch->regs[DBDMA_STATUS] & cpu_to_be32(RUN | ACTIVE)))
739 28ce5ce6 aurel32
        return;
740 28ce5ce6 aurel32
741 28ce5ce6 aurel32
    ch->regs[reg] = value;
742 28ce5ce6 aurel32
743 28ce5ce6 aurel32
    switch(reg) {
744 28ce5ce6 aurel32
    case DBDMA_CONTROL:
745 28ce5ce6 aurel32
        dbdma_control_write(ch);
746 28ce5ce6 aurel32
        break;
747 28ce5ce6 aurel32
    case DBDMA_CMDPTR_LO:
748 28ce5ce6 aurel32
        /* 16-byte aligned */
749 28ce5ce6 aurel32
        ch->regs[DBDMA_CMDPTR_LO] &= cpu_to_be32(~0xf);
750 28ce5ce6 aurel32
        dbdma_cmdptr_load(ch);
751 28ce5ce6 aurel32
        break;
752 28ce5ce6 aurel32
    case DBDMA_STATUS:
753 28ce5ce6 aurel32
    case DBDMA_INTR_SEL:
754 28ce5ce6 aurel32
    case DBDMA_BRANCH_SEL:
755 28ce5ce6 aurel32
    case DBDMA_WAIT_SEL:
756 28ce5ce6 aurel32
        /* nothing to do */
757 28ce5ce6 aurel32
        break;
758 28ce5ce6 aurel32
    case DBDMA_XFER_MODE:
759 28ce5ce6 aurel32
    case DBDMA_CMDPTR_HI:
760 28ce5ce6 aurel32
    case DBDMA_DATA2PTR_HI:
761 28ce5ce6 aurel32
    case DBDMA_DATA2PTR_LO:
762 28ce5ce6 aurel32
    case DBDMA_ADDRESS_HI:
763 28ce5ce6 aurel32
    case DBDMA_BRANCH_ADDR_HI:
764 28ce5ce6 aurel32
    case DBDMA_RES1:
765 28ce5ce6 aurel32
    case DBDMA_RES2:
766 28ce5ce6 aurel32
    case DBDMA_RES3:
767 28ce5ce6 aurel32
    case DBDMA_RES4:
768 28ce5ce6 aurel32
        /* unused */
769 28ce5ce6 aurel32
        break;
770 28ce5ce6 aurel32
    }
771 28ce5ce6 aurel32
}
772 28ce5ce6 aurel32
773 3cbee15b j_mayer
static uint32_t dbdma_readl (void *opaque, target_phys_addr_t addr)
774 3cbee15b j_mayer
{
775 28ce5ce6 aurel32
    uint32_t value;
776 28ce5ce6 aurel32
    int channel = addr >> DBDMA_CHANNEL_SHIFT;
777 28ce5ce6 aurel32
    DBDMA_channel *ch = (DBDMA_channel *)opaque + channel;
778 28ce5ce6 aurel32
    int reg = (addr - (channel << DBDMA_CHANNEL_SHIFT)) >> 2;
779 ea026b2f blueswir1
780 28ce5ce6 aurel32
    value = ch->regs[reg];
781 28ce5ce6 aurel32
782 28ce5ce6 aurel32
    DBDMA_DPRINTF("readl 0x" TARGET_FMT_plx " => 0x%08x\n", addr, value);
783 28ce5ce6 aurel32
    DBDMA_DPRINTF("channel 0x%x reg 0x%x\n",
784 28ce5ce6 aurel32
                  (uint32_t)addr >> DBDMA_CHANNEL_SHIFT, reg);
785 28ce5ce6 aurel32
786 28ce5ce6 aurel32
    switch(reg) {
787 28ce5ce6 aurel32
    case DBDMA_CONTROL:
788 28ce5ce6 aurel32
        value = 0;
789 28ce5ce6 aurel32
        break;
790 28ce5ce6 aurel32
    case DBDMA_STATUS:
791 28ce5ce6 aurel32
    case DBDMA_CMDPTR_LO:
792 28ce5ce6 aurel32
    case DBDMA_INTR_SEL:
793 28ce5ce6 aurel32
    case DBDMA_BRANCH_SEL:
794 28ce5ce6 aurel32
    case DBDMA_WAIT_SEL:
795 28ce5ce6 aurel32
        /* nothing to do */
796 28ce5ce6 aurel32
        break;
797 28ce5ce6 aurel32
    case DBDMA_XFER_MODE:
798 28ce5ce6 aurel32
    case DBDMA_CMDPTR_HI:
799 28ce5ce6 aurel32
    case DBDMA_DATA2PTR_HI:
800 28ce5ce6 aurel32
    case DBDMA_DATA2PTR_LO:
801 28ce5ce6 aurel32
    case DBDMA_ADDRESS_HI:
802 28ce5ce6 aurel32
    case DBDMA_BRANCH_ADDR_HI:
803 28ce5ce6 aurel32
        /* unused */
804 28ce5ce6 aurel32
        value = 0;
805 28ce5ce6 aurel32
        break;
806 28ce5ce6 aurel32
    case DBDMA_RES1:
807 28ce5ce6 aurel32
    case DBDMA_RES2:
808 28ce5ce6 aurel32
    case DBDMA_RES3:
809 28ce5ce6 aurel32
    case DBDMA_RES4:
810 28ce5ce6 aurel32
        /* reserved */
811 28ce5ce6 aurel32
        break;
812 28ce5ce6 aurel32
    }
813 28ce5ce6 aurel32
814 28ce5ce6 aurel32
    return value;
815 3cbee15b j_mayer
}
816 3cbee15b j_mayer
817 3cbee15b j_mayer
static CPUWriteMemoryFunc *dbdma_write[] = {
818 28ce5ce6 aurel32
    NULL,
819 28ce5ce6 aurel32
    NULL,
820 28ce5ce6 aurel32
    dbdma_writel,
821 3cbee15b j_mayer
};
822 3cbee15b j_mayer
823 3cbee15b j_mayer
static CPUReadMemoryFunc *dbdma_read[] = {
824 28ce5ce6 aurel32
    NULL,
825 28ce5ce6 aurel32
    NULL,
826 28ce5ce6 aurel32
    dbdma_readl,
827 3cbee15b j_mayer
};
828 3cbee15b j_mayer
829 9b64997f blueswir1
static void dbdma_save(QEMUFile *f, void *opaque)
830 9b64997f blueswir1
{
831 28ce5ce6 aurel32
    DBDMA_channel *s = opaque;
832 28ce5ce6 aurel32
    unsigned int i, j;
833 28ce5ce6 aurel32
834 28ce5ce6 aurel32
    for (i = 0; i < DBDMA_CHANNELS; i++)
835 28ce5ce6 aurel32
        for (j = 0; j < DBDMA_REGS; j++)
836 28ce5ce6 aurel32
            qemu_put_be32s(f, &s[i].regs[j]);
837 9b64997f blueswir1
}
838 9b64997f blueswir1
839 9b64997f blueswir1
static int dbdma_load(QEMUFile *f, void *opaque, int version_id)
840 9b64997f blueswir1
{
841 28ce5ce6 aurel32
    DBDMA_channel *s = opaque;
842 28ce5ce6 aurel32
    unsigned int i, j;
843 28ce5ce6 aurel32
844 28ce5ce6 aurel32
    if (version_id != 2)
845 9b64997f blueswir1
        return -EINVAL;
846 9b64997f blueswir1
847 28ce5ce6 aurel32
    for (i = 0; i < DBDMA_CHANNELS; i++)
848 28ce5ce6 aurel32
        for (j = 0; j < DBDMA_REGS; j++)
849 28ce5ce6 aurel32
            qemu_get_be32s(f, &s[i].regs[j]);
850 28ce5ce6 aurel32
851 9b64997f blueswir1
    return 0;
852 9b64997f blueswir1
}
853 9b64997f blueswir1
854 6e6b7363 blueswir1
static void dbdma_reset(void *opaque)
855 6e6b7363 blueswir1
{
856 28ce5ce6 aurel32
    DBDMA_channel *s = opaque;
857 28ce5ce6 aurel32
    int i;
858 28ce5ce6 aurel32
859 28ce5ce6 aurel32
    for (i = 0; i < DBDMA_CHANNELS; i++)
860 28ce5ce6 aurel32
        memset(s[i].regs, 0, DBDMA_SIZE);
861 6e6b7363 blueswir1
}
862 6e6b7363 blueswir1
863 28ce5ce6 aurel32
void* DBDMA_init (int *dbdma_mem_index)
864 3cbee15b j_mayer
{
865 28ce5ce6 aurel32
    DBDMA_channel *s;
866 28ce5ce6 aurel32
867 28ce5ce6 aurel32
    s = qemu_mallocz(sizeof(DBDMA_channel) * DBDMA_CHANNELS);
868 28ce5ce6 aurel32
869 28ce5ce6 aurel32
    *dbdma_mem_index = cpu_register_io_memory(0, dbdma_read, dbdma_write, s);
870 28ce5ce6 aurel32
    register_savevm("dbdma", -1, 1, dbdma_save, dbdma_load, s);
871 28ce5ce6 aurel32
    qemu_register_reset(dbdma_reset, s);
872 28ce5ce6 aurel32
    dbdma_reset(s);
873 28ce5ce6 aurel32
874 28ce5ce6 aurel32
    dbdma_bh = qemu_bh_new(DBDMA_run_bh, s);
875 28ce5ce6 aurel32
876 28ce5ce6 aurel32
    return s;
877 3cbee15b j_mayer
}