xref: /openbmc/qemu/hw/misc/macio/mac_dbdma.c (revision 1cde732d88af34849343dc1f0e68072eab0841b9)
149ab747fSPaolo Bonzini /*
249ab747fSPaolo Bonzini  * PowerMac descriptor-based DMA emulation
349ab747fSPaolo Bonzini  *
449ab747fSPaolo Bonzini  * Copyright (c) 2005-2007 Fabrice Bellard
549ab747fSPaolo Bonzini  * Copyright (c) 2007 Jocelyn Mayer
649ab747fSPaolo Bonzini  * Copyright (c) 2009 Laurent Vivier
749ab747fSPaolo Bonzini  *
849ab747fSPaolo Bonzini  * some parts from linux-2.6.28, arch/powerpc/include/asm/dbdma.h
949ab747fSPaolo Bonzini  *
1049ab747fSPaolo Bonzini  *   Definitions for using the Apple Descriptor-Based DMA controller
1149ab747fSPaolo Bonzini  *   in Power Macintosh computers.
1249ab747fSPaolo Bonzini  *
1349ab747fSPaolo Bonzini  *   Copyright (C) 1996 Paul Mackerras.
1449ab747fSPaolo Bonzini  *
1549ab747fSPaolo Bonzini  * some parts from mol 0.9.71
1649ab747fSPaolo Bonzini  *
1749ab747fSPaolo Bonzini  *   Descriptor based DMA emulation
1849ab747fSPaolo Bonzini  *
1949ab747fSPaolo Bonzini  *   Copyright (C) 1998-2004 Samuel Rydh (samuel@ibrium.se)
2049ab747fSPaolo Bonzini  *
2149ab747fSPaolo Bonzini  * Permission is hereby granted, free of charge, to any person obtaining a copy
2249ab747fSPaolo Bonzini  * of this software and associated documentation files (the "Software"), to deal
2349ab747fSPaolo Bonzini  * in the Software without restriction, including without limitation the rights
2449ab747fSPaolo Bonzini  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
2549ab747fSPaolo Bonzini  * copies of the Software, and to permit persons to whom the Software is
2649ab747fSPaolo Bonzini  * furnished to do so, subject to the following conditions:
2749ab747fSPaolo Bonzini  *
2849ab747fSPaolo Bonzini  * The above copyright notice and this permission notice shall be included in
2949ab747fSPaolo Bonzini  * all copies or substantial portions of the Software.
3049ab747fSPaolo Bonzini  *
3149ab747fSPaolo Bonzini  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
3249ab747fSPaolo Bonzini  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
3349ab747fSPaolo Bonzini  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
3449ab747fSPaolo Bonzini  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
3549ab747fSPaolo Bonzini  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
3649ab747fSPaolo Bonzini  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
3749ab747fSPaolo Bonzini  * THE SOFTWARE.
3849ab747fSPaolo Bonzini  */
3949ab747fSPaolo Bonzini #include "hw/hw.h"
4049ab747fSPaolo Bonzini #include "hw/isa/isa.h"
4149ab747fSPaolo Bonzini #include "hw/ppc/mac_dbdma.h"
4249ab747fSPaolo Bonzini #include "qemu/main-loop.h"
4349ab747fSPaolo Bonzini 
4449ab747fSPaolo Bonzini /* debug DBDMA */
4549ab747fSPaolo Bonzini //#define DEBUG_DBDMA
4649ab747fSPaolo Bonzini 
4749ab747fSPaolo Bonzini #ifdef DEBUG_DBDMA
4849ab747fSPaolo Bonzini #define DBDMA_DPRINTF(fmt, ...)                                 \
4949ab747fSPaolo Bonzini     do { printf("DBDMA: " fmt , ## __VA_ARGS__); } while (0)
5049ab747fSPaolo Bonzini #else
5149ab747fSPaolo Bonzini #define DBDMA_DPRINTF(fmt, ...)
5249ab747fSPaolo Bonzini #endif
5349ab747fSPaolo Bonzini 
5449ab747fSPaolo Bonzini /*
5549ab747fSPaolo Bonzini  */
5649ab747fSPaolo Bonzini 
57d2f0ce21SAlexander Graf static DBDMAState *dbdma_from_ch(DBDMA_channel *ch)
58d2f0ce21SAlexander Graf {
59d2f0ce21SAlexander Graf     return container_of(ch, DBDMAState, channels[ch->channel]);
60d2f0ce21SAlexander Graf }
61d2f0ce21SAlexander Graf 
6249ab747fSPaolo Bonzini #ifdef DEBUG_DBDMA
6349ab747fSPaolo Bonzini static void dump_dbdma_cmd(dbdma_cmd *cmd)
6449ab747fSPaolo Bonzini {
6549ab747fSPaolo Bonzini     printf("dbdma_cmd %p\n", cmd);
6649ab747fSPaolo Bonzini     printf("    req_count 0x%04x\n", le16_to_cpu(cmd->req_count));
6749ab747fSPaolo Bonzini     printf("    command 0x%04x\n", le16_to_cpu(cmd->command));
6849ab747fSPaolo Bonzini     printf("    phy_addr 0x%08x\n", le32_to_cpu(cmd->phy_addr));
6949ab747fSPaolo Bonzini     printf("    cmd_dep 0x%08x\n", le32_to_cpu(cmd->cmd_dep));
7049ab747fSPaolo Bonzini     printf("    res_count 0x%04x\n", le16_to_cpu(cmd->res_count));
7149ab747fSPaolo Bonzini     printf("    xfer_status 0x%04x\n", le16_to_cpu(cmd->xfer_status));
7249ab747fSPaolo Bonzini }
7349ab747fSPaolo Bonzini #else
7449ab747fSPaolo Bonzini static void dump_dbdma_cmd(dbdma_cmd *cmd)
7549ab747fSPaolo Bonzini {
7649ab747fSPaolo Bonzini }
7749ab747fSPaolo Bonzini #endif
7849ab747fSPaolo Bonzini static void dbdma_cmdptr_load(DBDMA_channel *ch)
7949ab747fSPaolo Bonzini {
8049ab747fSPaolo Bonzini     DBDMA_DPRINTF("dbdma_cmdptr_load 0x%08x\n",
8149ab747fSPaolo Bonzini                   ch->regs[DBDMA_CMDPTR_LO]);
8249ab747fSPaolo Bonzini     cpu_physical_memory_read(ch->regs[DBDMA_CMDPTR_LO],
83e1fe50dcSStefan Weil                              &ch->current, sizeof(dbdma_cmd));
8449ab747fSPaolo Bonzini }
8549ab747fSPaolo Bonzini 
8649ab747fSPaolo Bonzini static void dbdma_cmdptr_save(DBDMA_channel *ch)
8749ab747fSPaolo Bonzini {
8849ab747fSPaolo Bonzini     DBDMA_DPRINTF("dbdma_cmdptr_save 0x%08x\n",
8949ab747fSPaolo Bonzini                   ch->regs[DBDMA_CMDPTR_LO]);
9049ab747fSPaolo Bonzini     DBDMA_DPRINTF("xfer_status 0x%08x res_count 0x%04x\n",
9149ab747fSPaolo Bonzini                   le16_to_cpu(ch->current.xfer_status),
9249ab747fSPaolo Bonzini                   le16_to_cpu(ch->current.res_count));
9349ab747fSPaolo Bonzini     cpu_physical_memory_write(ch->regs[DBDMA_CMDPTR_LO],
94e1fe50dcSStefan Weil                               &ch->current, sizeof(dbdma_cmd));
9549ab747fSPaolo Bonzini }
9649ab747fSPaolo Bonzini 
9749ab747fSPaolo Bonzini static void kill_channel(DBDMA_channel *ch)
9849ab747fSPaolo Bonzini {
9949ab747fSPaolo Bonzini     DBDMA_DPRINTF("kill_channel\n");
10049ab747fSPaolo Bonzini 
10149ab747fSPaolo Bonzini     ch->regs[DBDMA_STATUS] |= DEAD;
10249ab747fSPaolo Bonzini     ch->regs[DBDMA_STATUS] &= ~ACTIVE;
10349ab747fSPaolo Bonzini 
10449ab747fSPaolo Bonzini     qemu_irq_raise(ch->irq);
10549ab747fSPaolo Bonzini }
10649ab747fSPaolo Bonzini 
10749ab747fSPaolo Bonzini static void conditional_interrupt(DBDMA_channel *ch)
10849ab747fSPaolo Bonzini {
10949ab747fSPaolo Bonzini     dbdma_cmd *current = &ch->current;
11049ab747fSPaolo Bonzini     uint16_t intr;
11149ab747fSPaolo Bonzini     uint16_t sel_mask, sel_value;
11249ab747fSPaolo Bonzini     uint32_t status;
11349ab747fSPaolo Bonzini     int cond;
11449ab747fSPaolo Bonzini 
11533ce36bbSAlexander Graf     DBDMA_DPRINTF("%s\n", __func__);
11649ab747fSPaolo Bonzini 
11749ab747fSPaolo Bonzini     intr = le16_to_cpu(current->command) & INTR_MASK;
11849ab747fSPaolo Bonzini 
11949ab747fSPaolo Bonzini     switch(intr) {
12049ab747fSPaolo Bonzini     case INTR_NEVER:  /* don't interrupt */
12149ab747fSPaolo Bonzini         return;
12249ab747fSPaolo Bonzini     case INTR_ALWAYS: /* always interrupt */
12349ab747fSPaolo Bonzini         qemu_irq_raise(ch->irq);
12433ce36bbSAlexander Graf         DBDMA_DPRINTF("%s: raise\n", __func__);
12549ab747fSPaolo Bonzini         return;
12649ab747fSPaolo Bonzini     }
12749ab747fSPaolo Bonzini 
12849ab747fSPaolo Bonzini     status = ch->regs[DBDMA_STATUS] & DEVSTAT;
12949ab747fSPaolo Bonzini 
13049ab747fSPaolo Bonzini     sel_mask = (ch->regs[DBDMA_INTR_SEL] >> 16) & 0x0f;
13149ab747fSPaolo Bonzini     sel_value = ch->regs[DBDMA_INTR_SEL] & 0x0f;
13249ab747fSPaolo Bonzini 
13349ab747fSPaolo Bonzini     cond = (status & sel_mask) == (sel_value & sel_mask);
13449ab747fSPaolo Bonzini 
13549ab747fSPaolo Bonzini     switch(intr) {
13649ab747fSPaolo Bonzini     case INTR_IFSET:  /* intr if condition bit is 1 */
13733ce36bbSAlexander Graf         if (cond) {
13849ab747fSPaolo Bonzini             qemu_irq_raise(ch->irq);
13933ce36bbSAlexander Graf             DBDMA_DPRINTF("%s: raise\n", __func__);
14033ce36bbSAlexander Graf         }
14149ab747fSPaolo Bonzini         return;
14249ab747fSPaolo Bonzini     case INTR_IFCLR:  /* intr if condition bit is 0 */
14333ce36bbSAlexander Graf         if (!cond) {
14449ab747fSPaolo Bonzini             qemu_irq_raise(ch->irq);
14533ce36bbSAlexander Graf             DBDMA_DPRINTF("%s: raise\n", __func__);
14633ce36bbSAlexander Graf         }
14749ab747fSPaolo Bonzini         return;
14849ab747fSPaolo Bonzini     }
14949ab747fSPaolo Bonzini }
15049ab747fSPaolo Bonzini 
15149ab747fSPaolo Bonzini static int conditional_wait(DBDMA_channel *ch)
15249ab747fSPaolo Bonzini {
15349ab747fSPaolo Bonzini     dbdma_cmd *current = &ch->current;
15449ab747fSPaolo Bonzini     uint16_t wait;
15549ab747fSPaolo Bonzini     uint16_t sel_mask, sel_value;
15649ab747fSPaolo Bonzini     uint32_t status;
15749ab747fSPaolo Bonzini     int cond;
15849ab747fSPaolo Bonzini 
15949ab747fSPaolo Bonzini     DBDMA_DPRINTF("conditional_wait\n");
16049ab747fSPaolo Bonzini 
16149ab747fSPaolo Bonzini     wait = le16_to_cpu(current->command) & WAIT_MASK;
16249ab747fSPaolo Bonzini 
16349ab747fSPaolo Bonzini     switch(wait) {
16449ab747fSPaolo Bonzini     case WAIT_NEVER:  /* don't wait */
16549ab747fSPaolo Bonzini         return 0;
16649ab747fSPaolo Bonzini     case WAIT_ALWAYS: /* always wait */
16749ab747fSPaolo Bonzini         return 1;
16849ab747fSPaolo Bonzini     }
16949ab747fSPaolo Bonzini 
17049ab747fSPaolo Bonzini     status = ch->regs[DBDMA_STATUS] & DEVSTAT;
17149ab747fSPaolo Bonzini 
17249ab747fSPaolo Bonzini     sel_mask = (ch->regs[DBDMA_WAIT_SEL] >> 16) & 0x0f;
17349ab747fSPaolo Bonzini     sel_value = ch->regs[DBDMA_WAIT_SEL] & 0x0f;
17449ab747fSPaolo Bonzini 
17549ab747fSPaolo Bonzini     cond = (status & sel_mask) == (sel_value & sel_mask);
17649ab747fSPaolo Bonzini 
17749ab747fSPaolo Bonzini     switch(wait) {
17849ab747fSPaolo Bonzini     case WAIT_IFSET:  /* wait if condition bit is 1 */
17949ab747fSPaolo Bonzini         if (cond)
18049ab747fSPaolo Bonzini             return 1;
18149ab747fSPaolo Bonzini         return 0;
18249ab747fSPaolo Bonzini     case WAIT_IFCLR:  /* wait if condition bit is 0 */
18349ab747fSPaolo Bonzini         if (!cond)
18449ab747fSPaolo Bonzini             return 1;
18549ab747fSPaolo Bonzini         return 0;
18649ab747fSPaolo Bonzini     }
18749ab747fSPaolo Bonzini     return 0;
18849ab747fSPaolo Bonzini }
18949ab747fSPaolo Bonzini 
19049ab747fSPaolo Bonzini static void next(DBDMA_channel *ch)
19149ab747fSPaolo Bonzini {
19249ab747fSPaolo Bonzini     uint32_t cp;
19349ab747fSPaolo Bonzini 
19449ab747fSPaolo Bonzini     ch->regs[DBDMA_STATUS] &= ~BT;
19549ab747fSPaolo Bonzini 
19649ab747fSPaolo Bonzini     cp = ch->regs[DBDMA_CMDPTR_LO];
19749ab747fSPaolo Bonzini     ch->regs[DBDMA_CMDPTR_LO] = cp + sizeof(dbdma_cmd);
19849ab747fSPaolo Bonzini     dbdma_cmdptr_load(ch);
19949ab747fSPaolo Bonzini }
20049ab747fSPaolo Bonzini 
20149ab747fSPaolo Bonzini static void branch(DBDMA_channel *ch)
20249ab747fSPaolo Bonzini {
20349ab747fSPaolo Bonzini     dbdma_cmd *current = &ch->current;
20449ab747fSPaolo Bonzini 
20549ab747fSPaolo Bonzini     ch->regs[DBDMA_CMDPTR_LO] = current->cmd_dep;
20649ab747fSPaolo Bonzini     ch->regs[DBDMA_STATUS] |= BT;
20749ab747fSPaolo Bonzini     dbdma_cmdptr_load(ch);
20849ab747fSPaolo Bonzini }
20949ab747fSPaolo Bonzini 
21049ab747fSPaolo Bonzini static void conditional_branch(DBDMA_channel *ch)
21149ab747fSPaolo Bonzini {
21249ab747fSPaolo Bonzini     dbdma_cmd *current = &ch->current;
21349ab747fSPaolo Bonzini     uint16_t br;
21449ab747fSPaolo Bonzini     uint16_t sel_mask, sel_value;
21549ab747fSPaolo Bonzini     uint32_t status;
21649ab747fSPaolo Bonzini     int cond;
21749ab747fSPaolo Bonzini 
21849ab747fSPaolo Bonzini     DBDMA_DPRINTF("conditional_branch\n");
21949ab747fSPaolo Bonzini 
22049ab747fSPaolo Bonzini     /* check if we must branch */
22149ab747fSPaolo Bonzini 
22249ab747fSPaolo Bonzini     br = le16_to_cpu(current->command) & BR_MASK;
22349ab747fSPaolo Bonzini 
22449ab747fSPaolo Bonzini     switch(br) {
22549ab747fSPaolo Bonzini     case BR_NEVER:  /* don't branch */
22649ab747fSPaolo Bonzini         next(ch);
22749ab747fSPaolo Bonzini         return;
22849ab747fSPaolo Bonzini     case BR_ALWAYS: /* always branch */
22949ab747fSPaolo Bonzini         branch(ch);
23049ab747fSPaolo Bonzini         return;
23149ab747fSPaolo Bonzini     }
23249ab747fSPaolo Bonzini 
23349ab747fSPaolo Bonzini     status = ch->regs[DBDMA_STATUS] & DEVSTAT;
23449ab747fSPaolo Bonzini 
23549ab747fSPaolo Bonzini     sel_mask = (ch->regs[DBDMA_BRANCH_SEL] >> 16) & 0x0f;
23649ab747fSPaolo Bonzini     sel_value = ch->regs[DBDMA_BRANCH_SEL] & 0x0f;
23749ab747fSPaolo Bonzini 
23849ab747fSPaolo Bonzini     cond = (status & sel_mask) == (sel_value & sel_mask);
23949ab747fSPaolo Bonzini 
24049ab747fSPaolo Bonzini     switch(br) {
24149ab747fSPaolo Bonzini     case BR_IFSET:  /* branch if condition bit is 1 */
24249ab747fSPaolo Bonzini         if (cond)
24349ab747fSPaolo Bonzini             branch(ch);
24449ab747fSPaolo Bonzini         else
24549ab747fSPaolo Bonzini             next(ch);
24649ab747fSPaolo Bonzini         return;
24749ab747fSPaolo Bonzini     case BR_IFCLR:  /* branch if condition bit is 0 */
24849ab747fSPaolo Bonzini         if (!cond)
24949ab747fSPaolo Bonzini             branch(ch);
25049ab747fSPaolo Bonzini         else
25149ab747fSPaolo Bonzini             next(ch);
25249ab747fSPaolo Bonzini         return;
25349ab747fSPaolo Bonzini     }
25449ab747fSPaolo Bonzini }
25549ab747fSPaolo Bonzini 
25649ab747fSPaolo Bonzini static void channel_run(DBDMA_channel *ch);
25749ab747fSPaolo Bonzini 
25849ab747fSPaolo Bonzini static void dbdma_end(DBDMA_io *io)
25949ab747fSPaolo Bonzini {
26049ab747fSPaolo Bonzini     DBDMA_channel *ch = io->channel;
26149ab747fSPaolo Bonzini     dbdma_cmd *current = &ch->current;
26249ab747fSPaolo Bonzini 
26333ce36bbSAlexander Graf     DBDMA_DPRINTF("%s\n", __func__);
26433ce36bbSAlexander Graf 
26549ab747fSPaolo Bonzini     if (conditional_wait(ch))
26649ab747fSPaolo Bonzini         goto wait;
26749ab747fSPaolo Bonzini 
26849ab747fSPaolo Bonzini     current->xfer_status = cpu_to_le16(ch->regs[DBDMA_STATUS]);
26949ab747fSPaolo Bonzini     current->res_count = cpu_to_le16(io->len);
27049ab747fSPaolo Bonzini     dbdma_cmdptr_save(ch);
27149ab747fSPaolo Bonzini     if (io->is_last)
27249ab747fSPaolo Bonzini         ch->regs[DBDMA_STATUS] &= ~FLUSH;
27349ab747fSPaolo Bonzini 
27449ab747fSPaolo Bonzini     conditional_interrupt(ch);
27549ab747fSPaolo Bonzini     conditional_branch(ch);
27649ab747fSPaolo Bonzini 
27749ab747fSPaolo Bonzini wait:
27803ee3b1eSAlexander Graf     /* Indicate that we're ready for a new DMA round */
27903ee3b1eSAlexander Graf     ch->io.processing = false;
28003ee3b1eSAlexander Graf 
28149ab747fSPaolo Bonzini     if ((ch->regs[DBDMA_STATUS] & RUN) &&
28249ab747fSPaolo Bonzini         (ch->regs[DBDMA_STATUS] & ACTIVE))
28349ab747fSPaolo Bonzini         channel_run(ch);
28449ab747fSPaolo Bonzini }
28549ab747fSPaolo Bonzini 
28649ab747fSPaolo Bonzini static void start_output(DBDMA_channel *ch, int key, uint32_t addr,
28749ab747fSPaolo Bonzini                         uint16_t req_count, int is_last)
28849ab747fSPaolo Bonzini {
28949ab747fSPaolo Bonzini     DBDMA_DPRINTF("start_output\n");
29049ab747fSPaolo Bonzini 
29149ab747fSPaolo Bonzini     /* KEY_REGS, KEY_DEVICE and KEY_STREAM
29249ab747fSPaolo Bonzini      * are not implemented in the mac-io chip
29349ab747fSPaolo Bonzini      */
29449ab747fSPaolo Bonzini 
29549ab747fSPaolo Bonzini     DBDMA_DPRINTF("addr 0x%x key 0x%x\n", addr, key);
29649ab747fSPaolo Bonzini     if (!addr || key > KEY_STREAM3) {
29749ab747fSPaolo Bonzini         kill_channel(ch);
29849ab747fSPaolo Bonzini         return;
29949ab747fSPaolo Bonzini     }
30049ab747fSPaolo Bonzini 
30149ab747fSPaolo Bonzini     ch->io.addr = addr;
30249ab747fSPaolo Bonzini     ch->io.len = req_count;
30349ab747fSPaolo Bonzini     ch->io.is_last = is_last;
30449ab747fSPaolo Bonzini     ch->io.dma_end = dbdma_end;
30549ab747fSPaolo Bonzini     ch->io.is_dma_out = 1;
30603ee3b1eSAlexander Graf     ch->io.processing = true;
30749ab747fSPaolo Bonzini     if (ch->rw) {
30849ab747fSPaolo Bonzini         ch->rw(&ch->io);
30949ab747fSPaolo Bonzini     }
31049ab747fSPaolo Bonzini }
31149ab747fSPaolo Bonzini 
31249ab747fSPaolo Bonzini static void start_input(DBDMA_channel *ch, int key, uint32_t addr,
31349ab747fSPaolo Bonzini                        uint16_t req_count, int is_last)
31449ab747fSPaolo Bonzini {
31549ab747fSPaolo Bonzini     DBDMA_DPRINTF("start_input\n");
31649ab747fSPaolo Bonzini 
31749ab747fSPaolo Bonzini     /* KEY_REGS, KEY_DEVICE and KEY_STREAM
31849ab747fSPaolo Bonzini      * are not implemented in the mac-io chip
31949ab747fSPaolo Bonzini      */
32049ab747fSPaolo Bonzini 
32133ce36bbSAlexander Graf     DBDMA_DPRINTF("addr 0x%x key 0x%x\n", addr, key);
32249ab747fSPaolo Bonzini     if (!addr || key > KEY_STREAM3) {
32349ab747fSPaolo Bonzini         kill_channel(ch);
32449ab747fSPaolo Bonzini         return;
32549ab747fSPaolo Bonzini     }
32649ab747fSPaolo Bonzini 
32749ab747fSPaolo Bonzini     ch->io.addr = addr;
32849ab747fSPaolo Bonzini     ch->io.len = req_count;
32949ab747fSPaolo Bonzini     ch->io.is_last = is_last;
33049ab747fSPaolo Bonzini     ch->io.dma_end = dbdma_end;
33149ab747fSPaolo Bonzini     ch->io.is_dma_out = 0;
33203ee3b1eSAlexander Graf     ch->io.processing = true;
33349ab747fSPaolo Bonzini     if (ch->rw) {
33449ab747fSPaolo Bonzini         ch->rw(&ch->io);
33549ab747fSPaolo Bonzini     }
33649ab747fSPaolo Bonzini }
33749ab747fSPaolo Bonzini 
33849ab747fSPaolo Bonzini static void load_word(DBDMA_channel *ch, int key, uint32_t addr,
33949ab747fSPaolo Bonzini                      uint16_t len)
34049ab747fSPaolo Bonzini {
34149ab747fSPaolo Bonzini     dbdma_cmd *current = &ch->current;
34249ab747fSPaolo Bonzini     uint32_t val;
34349ab747fSPaolo Bonzini 
34449ab747fSPaolo Bonzini     DBDMA_DPRINTF("load_word\n");
34549ab747fSPaolo Bonzini 
34649ab747fSPaolo Bonzini     /* only implements KEY_SYSTEM */
34749ab747fSPaolo Bonzini 
34849ab747fSPaolo Bonzini     if (key != KEY_SYSTEM) {
34949ab747fSPaolo Bonzini         printf("DBDMA: LOAD_WORD, unimplemented key %x\n", key);
35049ab747fSPaolo Bonzini         kill_channel(ch);
35149ab747fSPaolo Bonzini         return;
35249ab747fSPaolo Bonzini     }
35349ab747fSPaolo Bonzini 
354e1fe50dcSStefan Weil     cpu_physical_memory_read(addr, &val, len);
35549ab747fSPaolo Bonzini 
35649ab747fSPaolo Bonzini     if (len == 2)
35749ab747fSPaolo Bonzini         val = (val << 16) | (current->cmd_dep & 0x0000ffff);
35849ab747fSPaolo Bonzini     else if (len == 1)
35949ab747fSPaolo Bonzini         val = (val << 24) | (current->cmd_dep & 0x00ffffff);
36049ab747fSPaolo Bonzini 
36149ab747fSPaolo Bonzini     current->cmd_dep = val;
36249ab747fSPaolo Bonzini 
36349ab747fSPaolo Bonzini     if (conditional_wait(ch))
36449ab747fSPaolo Bonzini         goto wait;
36549ab747fSPaolo Bonzini 
36649ab747fSPaolo Bonzini     current->xfer_status = cpu_to_le16(ch->regs[DBDMA_STATUS]);
36749ab747fSPaolo Bonzini     dbdma_cmdptr_save(ch);
36849ab747fSPaolo Bonzini     ch->regs[DBDMA_STATUS] &= ~FLUSH;
36949ab747fSPaolo Bonzini 
37049ab747fSPaolo Bonzini     conditional_interrupt(ch);
37149ab747fSPaolo Bonzini     next(ch);
37249ab747fSPaolo Bonzini 
37349ab747fSPaolo Bonzini wait:
374d2f0ce21SAlexander Graf     DBDMA_kick(dbdma_from_ch(ch));
37549ab747fSPaolo Bonzini }
37649ab747fSPaolo Bonzini 
37749ab747fSPaolo Bonzini static void store_word(DBDMA_channel *ch, int key, uint32_t addr,
37849ab747fSPaolo Bonzini                       uint16_t len)
37949ab747fSPaolo Bonzini {
38049ab747fSPaolo Bonzini     dbdma_cmd *current = &ch->current;
38149ab747fSPaolo Bonzini     uint32_t val;
38249ab747fSPaolo Bonzini 
38349ab747fSPaolo Bonzini     DBDMA_DPRINTF("store_word\n");
38449ab747fSPaolo Bonzini 
38549ab747fSPaolo Bonzini     /* only implements KEY_SYSTEM */
38649ab747fSPaolo Bonzini 
38749ab747fSPaolo Bonzini     if (key != KEY_SYSTEM) {
38849ab747fSPaolo Bonzini         printf("DBDMA: STORE_WORD, unimplemented key %x\n", key);
38949ab747fSPaolo Bonzini         kill_channel(ch);
39049ab747fSPaolo Bonzini         return;
39149ab747fSPaolo Bonzini     }
39249ab747fSPaolo Bonzini 
39349ab747fSPaolo Bonzini     val = current->cmd_dep;
39449ab747fSPaolo Bonzini     if (len == 2)
39549ab747fSPaolo Bonzini         val >>= 16;
39649ab747fSPaolo Bonzini     else if (len == 1)
39749ab747fSPaolo Bonzini         val >>= 24;
39849ab747fSPaolo Bonzini 
399e1fe50dcSStefan Weil     cpu_physical_memory_write(addr, &val, len);
40049ab747fSPaolo Bonzini 
40149ab747fSPaolo Bonzini     if (conditional_wait(ch))
40249ab747fSPaolo Bonzini         goto wait;
40349ab747fSPaolo Bonzini 
40449ab747fSPaolo Bonzini     current->xfer_status = cpu_to_le16(ch->regs[DBDMA_STATUS]);
40549ab747fSPaolo Bonzini     dbdma_cmdptr_save(ch);
40649ab747fSPaolo Bonzini     ch->regs[DBDMA_STATUS] &= ~FLUSH;
40749ab747fSPaolo Bonzini 
40849ab747fSPaolo Bonzini     conditional_interrupt(ch);
40949ab747fSPaolo Bonzini     next(ch);
41049ab747fSPaolo Bonzini 
41149ab747fSPaolo Bonzini wait:
412d2f0ce21SAlexander Graf     DBDMA_kick(dbdma_from_ch(ch));
41349ab747fSPaolo Bonzini }
41449ab747fSPaolo Bonzini 
41549ab747fSPaolo Bonzini static void nop(DBDMA_channel *ch)
41649ab747fSPaolo Bonzini {
41749ab747fSPaolo Bonzini     dbdma_cmd *current = &ch->current;
41849ab747fSPaolo Bonzini 
41949ab747fSPaolo Bonzini     if (conditional_wait(ch))
42049ab747fSPaolo Bonzini         goto wait;
42149ab747fSPaolo Bonzini 
42249ab747fSPaolo Bonzini     current->xfer_status = cpu_to_le16(ch->regs[DBDMA_STATUS]);
42349ab747fSPaolo Bonzini     dbdma_cmdptr_save(ch);
42449ab747fSPaolo Bonzini 
42549ab747fSPaolo Bonzini     conditional_interrupt(ch);
42649ab747fSPaolo Bonzini     conditional_branch(ch);
42749ab747fSPaolo Bonzini 
42849ab747fSPaolo Bonzini wait:
429d2f0ce21SAlexander Graf     DBDMA_kick(dbdma_from_ch(ch));
43049ab747fSPaolo Bonzini }
43149ab747fSPaolo Bonzini 
43249ab747fSPaolo Bonzini static void stop(DBDMA_channel *ch)
43349ab747fSPaolo Bonzini {
43449ab747fSPaolo Bonzini     ch->regs[DBDMA_STATUS] &= ~(ACTIVE|DEAD|FLUSH);
43549ab747fSPaolo Bonzini 
43649ab747fSPaolo Bonzini     /* the stop command does not increment command pointer */
43749ab747fSPaolo Bonzini }
43849ab747fSPaolo Bonzini 
43949ab747fSPaolo Bonzini static void channel_run(DBDMA_channel *ch)
44049ab747fSPaolo Bonzini {
44149ab747fSPaolo Bonzini     dbdma_cmd *current = &ch->current;
44249ab747fSPaolo Bonzini     uint16_t cmd, key;
44349ab747fSPaolo Bonzini     uint16_t req_count;
44449ab747fSPaolo Bonzini     uint32_t phy_addr;
44549ab747fSPaolo Bonzini 
44649ab747fSPaolo Bonzini     DBDMA_DPRINTF("channel_run\n");
44749ab747fSPaolo Bonzini     dump_dbdma_cmd(current);
44849ab747fSPaolo Bonzini 
44949ab747fSPaolo Bonzini     /* clear WAKE flag at command fetch */
45049ab747fSPaolo Bonzini 
45149ab747fSPaolo Bonzini     ch->regs[DBDMA_STATUS] &= ~WAKE;
45249ab747fSPaolo Bonzini 
45349ab747fSPaolo Bonzini     cmd = le16_to_cpu(current->command) & COMMAND_MASK;
45449ab747fSPaolo Bonzini 
45549ab747fSPaolo Bonzini     switch (cmd) {
45649ab747fSPaolo Bonzini     case DBDMA_NOP:
45749ab747fSPaolo Bonzini         nop(ch);
45849ab747fSPaolo Bonzini         return;
45949ab747fSPaolo Bonzini 
46049ab747fSPaolo Bonzini     case DBDMA_STOP:
46149ab747fSPaolo Bonzini         stop(ch);
46249ab747fSPaolo Bonzini         return;
46349ab747fSPaolo Bonzini     }
46449ab747fSPaolo Bonzini 
46549ab747fSPaolo Bonzini     key = le16_to_cpu(current->command) & 0x0700;
46649ab747fSPaolo Bonzini     req_count = le16_to_cpu(current->req_count);
46749ab747fSPaolo Bonzini     phy_addr = le32_to_cpu(current->phy_addr);
46849ab747fSPaolo Bonzini 
46949ab747fSPaolo Bonzini     if (key == KEY_STREAM4) {
47049ab747fSPaolo Bonzini         printf("command %x, invalid key 4\n", cmd);
47149ab747fSPaolo Bonzini         kill_channel(ch);
47249ab747fSPaolo Bonzini         return;
47349ab747fSPaolo Bonzini     }
47449ab747fSPaolo Bonzini 
47549ab747fSPaolo Bonzini     switch (cmd) {
47649ab747fSPaolo Bonzini     case OUTPUT_MORE:
47749ab747fSPaolo Bonzini         start_output(ch, key, phy_addr, req_count, 0);
47849ab747fSPaolo Bonzini         return;
47949ab747fSPaolo Bonzini 
48049ab747fSPaolo Bonzini     case OUTPUT_LAST:
48149ab747fSPaolo Bonzini         start_output(ch, key, phy_addr, req_count, 1);
48249ab747fSPaolo Bonzini         return;
48349ab747fSPaolo Bonzini 
48449ab747fSPaolo Bonzini     case INPUT_MORE:
48549ab747fSPaolo Bonzini         start_input(ch, key, phy_addr, req_count, 0);
48649ab747fSPaolo Bonzini         return;
48749ab747fSPaolo Bonzini 
48849ab747fSPaolo Bonzini     case INPUT_LAST:
48949ab747fSPaolo Bonzini         start_input(ch, key, phy_addr, req_count, 1);
49049ab747fSPaolo Bonzini         return;
49149ab747fSPaolo Bonzini     }
49249ab747fSPaolo Bonzini 
49349ab747fSPaolo Bonzini     if (key < KEY_REGS) {
49449ab747fSPaolo Bonzini         printf("command %x, invalid key %x\n", cmd, key);
49549ab747fSPaolo Bonzini         key = KEY_SYSTEM;
49649ab747fSPaolo Bonzini     }
49749ab747fSPaolo Bonzini 
49849ab747fSPaolo Bonzini     /* for LOAD_WORD and STORE_WORD, req_count is on 3 bits
49949ab747fSPaolo Bonzini      * and BRANCH is invalid
50049ab747fSPaolo Bonzini      */
50149ab747fSPaolo Bonzini 
50249ab747fSPaolo Bonzini     req_count = req_count & 0x0007;
50349ab747fSPaolo Bonzini     if (req_count & 0x4) {
50449ab747fSPaolo Bonzini         req_count = 4;
50549ab747fSPaolo Bonzini         phy_addr &= ~3;
50649ab747fSPaolo Bonzini     } else if (req_count & 0x2) {
50749ab747fSPaolo Bonzini         req_count = 2;
50849ab747fSPaolo Bonzini         phy_addr &= ~1;
50949ab747fSPaolo Bonzini     } else
51049ab747fSPaolo Bonzini         req_count = 1;
51149ab747fSPaolo Bonzini 
51249ab747fSPaolo Bonzini     switch (cmd) {
51349ab747fSPaolo Bonzini     case LOAD_WORD:
51449ab747fSPaolo Bonzini         load_word(ch, key, phy_addr, req_count);
51549ab747fSPaolo Bonzini         return;
51649ab747fSPaolo Bonzini 
51749ab747fSPaolo Bonzini     case STORE_WORD:
51849ab747fSPaolo Bonzini         store_word(ch, key, phy_addr, req_count);
51949ab747fSPaolo Bonzini         return;
52049ab747fSPaolo Bonzini     }
52149ab747fSPaolo Bonzini }
52249ab747fSPaolo Bonzini 
52349ab747fSPaolo Bonzini static void DBDMA_run(DBDMAState *s)
52449ab747fSPaolo Bonzini {
52549ab747fSPaolo Bonzini     int channel;
52649ab747fSPaolo Bonzini 
52749ab747fSPaolo Bonzini     for (channel = 0; channel < DBDMA_CHANNELS; channel++) {
52849ab747fSPaolo Bonzini         DBDMA_channel *ch = &s->channels[channel];
52949ab747fSPaolo Bonzini         uint32_t status = ch->regs[DBDMA_STATUS];
53003ee3b1eSAlexander Graf         if (!ch->io.processing && (status & RUN) && (status & ACTIVE)) {
53149ab747fSPaolo Bonzini             channel_run(ch);
53249ab747fSPaolo Bonzini         }
53349ab747fSPaolo Bonzini     }
53449ab747fSPaolo Bonzini }
53549ab747fSPaolo Bonzini 
53649ab747fSPaolo Bonzini static void DBDMA_run_bh(void *opaque)
53749ab747fSPaolo Bonzini {
53849ab747fSPaolo Bonzini     DBDMAState *s = opaque;
53949ab747fSPaolo Bonzini 
54049ab747fSPaolo Bonzini     DBDMA_DPRINTF("DBDMA_run_bh\n");
54149ab747fSPaolo Bonzini 
54249ab747fSPaolo Bonzini     DBDMA_run(s);
54349ab747fSPaolo Bonzini }
54449ab747fSPaolo Bonzini 
545d1e562deSAlexander Graf void DBDMA_kick(DBDMAState *dbdma)
546d1e562deSAlexander Graf {
547d2f0ce21SAlexander Graf     qemu_bh_schedule(dbdma->bh);
548d1e562deSAlexander Graf }
549d1e562deSAlexander Graf 
55049ab747fSPaolo Bonzini void DBDMA_register_channel(void *dbdma, int nchan, qemu_irq irq,
55149ab747fSPaolo Bonzini                             DBDMA_rw rw, DBDMA_flush flush,
55249ab747fSPaolo Bonzini                             void *opaque)
55349ab747fSPaolo Bonzini {
55449ab747fSPaolo Bonzini     DBDMAState *s = dbdma;
55549ab747fSPaolo Bonzini     DBDMA_channel *ch = &s->channels[nchan];
55649ab747fSPaolo Bonzini 
55749ab747fSPaolo Bonzini     DBDMA_DPRINTF("DBDMA_register_channel 0x%x\n", nchan);
55849ab747fSPaolo Bonzini 
55949ab747fSPaolo Bonzini     ch->irq = irq;
56049ab747fSPaolo Bonzini     ch->channel = nchan;
56149ab747fSPaolo Bonzini     ch->rw = rw;
56249ab747fSPaolo Bonzini     ch->flush = flush;
56349ab747fSPaolo Bonzini     ch->io.opaque = opaque;
56449ab747fSPaolo Bonzini     ch->io.channel = ch;
56549ab747fSPaolo Bonzini }
56649ab747fSPaolo Bonzini 
56749ab747fSPaolo Bonzini static void
56849ab747fSPaolo Bonzini dbdma_control_write(DBDMA_channel *ch)
56949ab747fSPaolo Bonzini {
57049ab747fSPaolo Bonzini     uint16_t mask, value;
57149ab747fSPaolo Bonzini     uint32_t status;
57249ab747fSPaolo Bonzini 
57349ab747fSPaolo Bonzini     mask = (ch->regs[DBDMA_CONTROL] >> 16) & 0xffff;
57449ab747fSPaolo Bonzini     value = ch->regs[DBDMA_CONTROL] & 0xffff;
57549ab747fSPaolo Bonzini 
57649ab747fSPaolo Bonzini     value &= (RUN | PAUSE | FLUSH | WAKE | DEVSTAT);
57749ab747fSPaolo Bonzini 
57849ab747fSPaolo Bonzini     status = ch->regs[DBDMA_STATUS];
57949ab747fSPaolo Bonzini 
58049ab747fSPaolo Bonzini     status = (value & mask) | (status & ~mask);
58149ab747fSPaolo Bonzini 
58249ab747fSPaolo Bonzini     if (status & WAKE)
58349ab747fSPaolo Bonzini         status |= ACTIVE;
58449ab747fSPaolo Bonzini     if (status & RUN) {
58549ab747fSPaolo Bonzini         status |= ACTIVE;
58649ab747fSPaolo Bonzini         status &= ~DEAD;
58749ab747fSPaolo Bonzini     }
58849ab747fSPaolo Bonzini     if (status & PAUSE)
58949ab747fSPaolo Bonzini         status &= ~ACTIVE;
59049ab747fSPaolo Bonzini     if ((ch->regs[DBDMA_STATUS] & RUN) && !(status & RUN)) {
59149ab747fSPaolo Bonzini         /* RUN is cleared */
59249ab747fSPaolo Bonzini         status &= ~(ACTIVE|DEAD);
593*1cde732dSMark Cave-Ayland     }
594*1cde732dSMark Cave-Ayland 
59549ab747fSPaolo Bonzini     if ((status & FLUSH) && ch->flush) {
59649ab747fSPaolo Bonzini         ch->flush(&ch->io);
59749ab747fSPaolo Bonzini         status &= ~FLUSH;
59849ab747fSPaolo Bonzini     }
59949ab747fSPaolo Bonzini 
60049ab747fSPaolo Bonzini     DBDMA_DPRINTF("    status 0x%08x\n", status);
60149ab747fSPaolo Bonzini 
60249ab747fSPaolo Bonzini     ch->regs[DBDMA_STATUS] = status;
60349ab747fSPaolo Bonzini 
604d2f0ce21SAlexander Graf     if (status & ACTIVE) {
605d2f0ce21SAlexander Graf         DBDMA_kick(dbdma_from_ch(ch));
606d2f0ce21SAlexander Graf     }
607d2f0ce21SAlexander Graf }
60849ab747fSPaolo Bonzini 
60949ab747fSPaolo Bonzini static void dbdma_write(void *opaque, hwaddr addr,
61049ab747fSPaolo Bonzini                         uint64_t value, unsigned size)
61149ab747fSPaolo Bonzini {
61249ab747fSPaolo Bonzini     int channel = addr >> DBDMA_CHANNEL_SHIFT;
61349ab747fSPaolo Bonzini     DBDMAState *s = opaque;
61449ab747fSPaolo Bonzini     DBDMA_channel *ch = &s->channels[channel];
61549ab747fSPaolo Bonzini     int reg = (addr - (channel << DBDMA_CHANNEL_SHIFT)) >> 2;
61649ab747fSPaolo Bonzini 
61758c0c311SAlexander Graf     DBDMA_DPRINTF("writel 0x" TARGET_FMT_plx " <= 0x%08"PRIx64"\n",
61858c0c311SAlexander Graf                   addr, value);
61949ab747fSPaolo Bonzini     DBDMA_DPRINTF("channel 0x%x reg 0x%x\n",
62049ab747fSPaolo Bonzini                   (uint32_t)addr >> DBDMA_CHANNEL_SHIFT, reg);
62149ab747fSPaolo Bonzini 
6227eaba824SAlexander Graf     /* cmdptr cannot be modified if channel is ACTIVE */
62349ab747fSPaolo Bonzini 
6247eaba824SAlexander Graf     if (reg == DBDMA_CMDPTR_LO && (ch->regs[DBDMA_STATUS] & ACTIVE)) {
62549ab747fSPaolo Bonzini         return;
6267eaba824SAlexander Graf     }
62749ab747fSPaolo Bonzini 
62849ab747fSPaolo Bonzini     ch->regs[reg] = value;
62949ab747fSPaolo Bonzini 
63049ab747fSPaolo Bonzini     switch(reg) {
63149ab747fSPaolo Bonzini     case DBDMA_CONTROL:
63249ab747fSPaolo Bonzini         dbdma_control_write(ch);
63349ab747fSPaolo Bonzini         break;
63449ab747fSPaolo Bonzini     case DBDMA_CMDPTR_LO:
63549ab747fSPaolo Bonzini         /* 16-byte aligned */
63649ab747fSPaolo Bonzini         ch->regs[DBDMA_CMDPTR_LO] &= ~0xf;
63749ab747fSPaolo Bonzini         dbdma_cmdptr_load(ch);
63849ab747fSPaolo Bonzini         break;
63949ab747fSPaolo Bonzini     case DBDMA_STATUS:
64049ab747fSPaolo Bonzini     case DBDMA_INTR_SEL:
64149ab747fSPaolo Bonzini     case DBDMA_BRANCH_SEL:
64249ab747fSPaolo Bonzini     case DBDMA_WAIT_SEL:
64349ab747fSPaolo Bonzini         /* nothing to do */
64449ab747fSPaolo Bonzini         break;
64549ab747fSPaolo Bonzini     case DBDMA_XFER_MODE:
64649ab747fSPaolo Bonzini     case DBDMA_CMDPTR_HI:
64749ab747fSPaolo Bonzini     case DBDMA_DATA2PTR_HI:
64849ab747fSPaolo Bonzini     case DBDMA_DATA2PTR_LO:
64949ab747fSPaolo Bonzini     case DBDMA_ADDRESS_HI:
65049ab747fSPaolo Bonzini     case DBDMA_BRANCH_ADDR_HI:
65149ab747fSPaolo Bonzini     case DBDMA_RES1:
65249ab747fSPaolo Bonzini     case DBDMA_RES2:
65349ab747fSPaolo Bonzini     case DBDMA_RES3:
65449ab747fSPaolo Bonzini     case DBDMA_RES4:
65549ab747fSPaolo Bonzini         /* unused */
65649ab747fSPaolo Bonzini         break;
65749ab747fSPaolo Bonzini     }
65849ab747fSPaolo Bonzini }
65949ab747fSPaolo Bonzini 
66049ab747fSPaolo Bonzini static uint64_t dbdma_read(void *opaque, hwaddr addr,
66149ab747fSPaolo Bonzini                            unsigned size)
66249ab747fSPaolo Bonzini {
66349ab747fSPaolo Bonzini     uint32_t value;
66449ab747fSPaolo Bonzini     int channel = addr >> DBDMA_CHANNEL_SHIFT;
66549ab747fSPaolo Bonzini     DBDMAState *s = opaque;
66649ab747fSPaolo Bonzini     DBDMA_channel *ch = &s->channels[channel];
66749ab747fSPaolo Bonzini     int reg = (addr - (channel << DBDMA_CHANNEL_SHIFT)) >> 2;
66849ab747fSPaolo Bonzini 
66949ab747fSPaolo Bonzini     value = ch->regs[reg];
67049ab747fSPaolo Bonzini 
67149ab747fSPaolo Bonzini     DBDMA_DPRINTF("readl 0x" TARGET_FMT_plx " => 0x%08x\n", addr, value);
67249ab747fSPaolo Bonzini     DBDMA_DPRINTF("channel 0x%x reg 0x%x\n",
67349ab747fSPaolo Bonzini                   (uint32_t)addr >> DBDMA_CHANNEL_SHIFT, reg);
67449ab747fSPaolo Bonzini 
67549ab747fSPaolo Bonzini     switch(reg) {
67649ab747fSPaolo Bonzini     case DBDMA_CONTROL:
67749ab747fSPaolo Bonzini         value = 0;
67849ab747fSPaolo Bonzini         break;
67949ab747fSPaolo Bonzini     case DBDMA_STATUS:
68049ab747fSPaolo Bonzini     case DBDMA_CMDPTR_LO:
68149ab747fSPaolo Bonzini     case DBDMA_INTR_SEL:
68249ab747fSPaolo Bonzini     case DBDMA_BRANCH_SEL:
68349ab747fSPaolo Bonzini     case DBDMA_WAIT_SEL:
68449ab747fSPaolo Bonzini         /* nothing to do */
68549ab747fSPaolo Bonzini         break;
68649ab747fSPaolo Bonzini     case DBDMA_XFER_MODE:
68749ab747fSPaolo Bonzini     case DBDMA_CMDPTR_HI:
68849ab747fSPaolo Bonzini     case DBDMA_DATA2PTR_HI:
68949ab747fSPaolo Bonzini     case DBDMA_DATA2PTR_LO:
69049ab747fSPaolo Bonzini     case DBDMA_ADDRESS_HI:
69149ab747fSPaolo Bonzini     case DBDMA_BRANCH_ADDR_HI:
69249ab747fSPaolo Bonzini         /* unused */
69349ab747fSPaolo Bonzini         value = 0;
69449ab747fSPaolo Bonzini         break;
69549ab747fSPaolo Bonzini     case DBDMA_RES1:
69649ab747fSPaolo Bonzini     case DBDMA_RES2:
69749ab747fSPaolo Bonzini     case DBDMA_RES3:
69849ab747fSPaolo Bonzini     case DBDMA_RES4:
69949ab747fSPaolo Bonzini         /* reserved */
70049ab747fSPaolo Bonzini         break;
70149ab747fSPaolo Bonzini     }
70249ab747fSPaolo Bonzini 
70349ab747fSPaolo Bonzini     return value;
70449ab747fSPaolo Bonzini }
70549ab747fSPaolo Bonzini 
70649ab747fSPaolo Bonzini static const MemoryRegionOps dbdma_ops = {
70749ab747fSPaolo Bonzini     .read = dbdma_read,
70849ab747fSPaolo Bonzini     .write = dbdma_write,
70949ab747fSPaolo Bonzini     .endianness = DEVICE_LITTLE_ENDIAN,
71049ab747fSPaolo Bonzini     .valid = {
71149ab747fSPaolo Bonzini         .min_access_size = 4,
71249ab747fSPaolo Bonzini         .max_access_size = 4,
71349ab747fSPaolo Bonzini     },
71449ab747fSPaolo Bonzini };
71549ab747fSPaolo Bonzini 
71649ab747fSPaolo Bonzini static const VMStateDescription vmstate_dbdma_channel = {
71749ab747fSPaolo Bonzini     .name = "dbdma_channel",
71849ab747fSPaolo Bonzini     .version_id = 0,
71949ab747fSPaolo Bonzini     .minimum_version_id = 0,
72049ab747fSPaolo Bonzini     .fields = (VMStateField[]) {
72149ab747fSPaolo Bonzini         VMSTATE_UINT32_ARRAY(regs, struct DBDMA_channel, DBDMA_REGS),
72249ab747fSPaolo Bonzini         VMSTATE_END_OF_LIST()
72349ab747fSPaolo Bonzini     }
72449ab747fSPaolo Bonzini };
72549ab747fSPaolo Bonzini 
72649ab747fSPaolo Bonzini static const VMStateDescription vmstate_dbdma = {
72749ab747fSPaolo Bonzini     .name = "dbdma",
72849ab747fSPaolo Bonzini     .version_id = 2,
72949ab747fSPaolo Bonzini     .minimum_version_id = 2,
73049ab747fSPaolo Bonzini     .fields = (VMStateField[]) {
73149ab747fSPaolo Bonzini         VMSTATE_STRUCT_ARRAY(channels, DBDMAState, DBDMA_CHANNELS, 1,
73249ab747fSPaolo Bonzini                              vmstate_dbdma_channel, DBDMA_channel),
73349ab747fSPaolo Bonzini         VMSTATE_END_OF_LIST()
73449ab747fSPaolo Bonzini     }
73549ab747fSPaolo Bonzini };
73649ab747fSPaolo Bonzini 
73749ab747fSPaolo Bonzini static void dbdma_reset(void *opaque)
73849ab747fSPaolo Bonzini {
73949ab747fSPaolo Bonzini     DBDMAState *s = opaque;
74049ab747fSPaolo Bonzini     int i;
74149ab747fSPaolo Bonzini 
74249ab747fSPaolo Bonzini     for (i = 0; i < DBDMA_CHANNELS; i++)
74349ab747fSPaolo Bonzini         memset(s->channels[i].regs, 0, DBDMA_SIZE);
74449ab747fSPaolo Bonzini }
74549ab747fSPaolo Bonzini 
74649ab747fSPaolo Bonzini void* DBDMA_init (MemoryRegion **dbdma_mem)
74749ab747fSPaolo Bonzini {
74849ab747fSPaolo Bonzini     DBDMAState *s;
7493e300fa6SAlexander Graf     int i;
75049ab747fSPaolo Bonzini 
75149ab747fSPaolo Bonzini     s = g_malloc0(sizeof(DBDMAState));
75249ab747fSPaolo Bonzini 
7533e300fa6SAlexander Graf     for (i = 0; i < DBDMA_CHANNELS; i++) {
7543e300fa6SAlexander Graf         DBDMA_io *io = &s->channels[i].io;
7553e300fa6SAlexander Graf         qemu_iovec_init(&io->iov, 1);
7563e300fa6SAlexander Graf     }
7573e300fa6SAlexander Graf 
7582c9b15caSPaolo Bonzini     memory_region_init_io(&s->mem, NULL, &dbdma_ops, s, "dbdma", 0x1000);
75949ab747fSPaolo Bonzini     *dbdma_mem = &s->mem;
76049ab747fSPaolo Bonzini     vmstate_register(NULL, -1, &vmstate_dbdma, s);
76149ab747fSPaolo Bonzini     qemu_register_reset(dbdma_reset, s);
76249ab747fSPaolo Bonzini 
763d2f0ce21SAlexander Graf     s->bh = qemu_bh_new(DBDMA_run_bh, s);
76449ab747fSPaolo Bonzini 
76549ab747fSPaolo Bonzini     return s;
76649ab747fSPaolo Bonzini }
767