virtio-pci: drop unused wmb macro
[qemu/qmp-unstable.git] / hw / misc / macio / mac_dbdma.c
blob2fc7f87bb6fb51cf0404b8031a5429cf57535ce7
1 /*
2 * PowerMac descriptor-based DMA emulation
4 * Copyright (c) 2005-2007 Fabrice Bellard
5 * Copyright (c) 2007 Jocelyn Mayer
6 * Copyright (c) 2009 Laurent Vivier
8 * some parts from linux-2.6.28, arch/powerpc/include/asm/dbdma.h
10 * Definitions for using the Apple Descriptor-Based DMA controller
11 * in Power Macintosh computers.
13 * Copyright (C) 1996 Paul Mackerras.
15 * some parts from mol 0.9.71
17 * Descriptor based DMA emulation
19 * Copyright (C) 1998-2004 Samuel Rydh (samuel@ibrium.se)
21 * Permission is hereby granted, free of charge, to any person obtaining a copy
22 * of this software and associated documentation files (the "Software"), to deal
23 * in the Software without restriction, including without limitation the rights
24 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
25 * copies of the Software, and to permit persons to whom the Software is
26 * furnished to do so, subject to the following conditions:
28 * The above copyright notice and this permission notice shall be included in
29 * all copies or substantial portions of the Software.
31 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
32 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
33 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
34 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
35 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
36 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
37 * THE SOFTWARE.
39 #include "hw/hw.h"
40 #include "hw/isa/isa.h"
41 #include "hw/ppc/mac_dbdma.h"
42 #include "qemu/main-loop.h"
44 /* debug DBDMA */
45 //#define DEBUG_DBDMA
47 #ifdef DEBUG_DBDMA
48 #define DBDMA_DPRINTF(fmt, ...) \
49 do { printf("DBDMA: " fmt , ## __VA_ARGS__); } while (0)
50 #else
51 #define DBDMA_DPRINTF(fmt, ...)
52 #endif
58 * DBDMA control/status registers. All little-endian.
61 #define DBDMA_CONTROL 0x00
62 #define DBDMA_STATUS 0x01
63 #define DBDMA_CMDPTR_HI 0x02
64 #define DBDMA_CMDPTR_LO 0x03
65 #define DBDMA_INTR_SEL 0x04
66 #define DBDMA_BRANCH_SEL 0x05
67 #define DBDMA_WAIT_SEL 0x06
68 #define DBDMA_XFER_MODE 0x07
69 #define DBDMA_DATA2PTR_HI 0x08
70 #define DBDMA_DATA2PTR_LO 0x09
71 #define DBDMA_RES1 0x0A
72 #define DBDMA_ADDRESS_HI 0x0B
73 #define DBDMA_BRANCH_ADDR_HI 0x0C
74 #define DBDMA_RES2 0x0D
75 #define DBDMA_RES3 0x0E
76 #define DBDMA_RES4 0x0F
78 #define DBDMA_REGS 16
79 #define DBDMA_SIZE (DBDMA_REGS * sizeof(uint32_t))
81 #define DBDMA_CHANNEL_SHIFT 7
82 #define DBDMA_CHANNEL_SIZE (1 << DBDMA_CHANNEL_SHIFT)
84 #define DBDMA_CHANNELS (0x1000 >> DBDMA_CHANNEL_SHIFT)
86 /* Bits in control and status registers */
88 #define RUN 0x8000
89 #define PAUSE 0x4000
90 #define FLUSH 0x2000
91 #define WAKE 0x1000
92 #define DEAD 0x0800
93 #define ACTIVE 0x0400
94 #define BT 0x0100
95 #define DEVSTAT 0x00ff
98 * DBDMA command structure. These fields are all little-endian!
101 typedef struct dbdma_cmd {
102 uint16_t req_count; /* requested byte transfer count */
103 uint16_t command; /* command word (has bit-fields) */
104 uint32_t phy_addr; /* physical data address */
105 uint32_t cmd_dep; /* command-dependent field */
106 uint16_t res_count; /* residual count after completion */
107 uint16_t xfer_status; /* transfer status */
108 } dbdma_cmd;
110 /* DBDMA command values in command field */
112 #define COMMAND_MASK 0xf000
113 #define OUTPUT_MORE 0x0000 /* transfer memory data to stream */
114 #define OUTPUT_LAST 0x1000 /* ditto followed by end marker */
115 #define INPUT_MORE 0x2000 /* transfer stream data to memory */
116 #define INPUT_LAST 0x3000 /* ditto, expect end marker */
117 #define STORE_WORD 0x4000 /* write word (4 bytes) to device reg */
118 #define LOAD_WORD 0x5000 /* read word (4 bytes) from device reg */
119 #define DBDMA_NOP 0x6000 /* do nothing */
120 #define DBDMA_STOP 0x7000 /* suspend processing */
122 /* Key values in command field */
124 #define KEY_MASK 0x0700
125 #define KEY_STREAM0 0x0000 /* usual data stream */
126 #define KEY_STREAM1 0x0100 /* control/status stream */
127 #define KEY_STREAM2 0x0200 /* device-dependent stream */
128 #define KEY_STREAM3 0x0300 /* device-dependent stream */
129 #define KEY_STREAM4 0x0400 /* reserved */
130 #define KEY_REGS 0x0500 /* device register space */
131 #define KEY_SYSTEM 0x0600 /* system memory-mapped space */
132 #define KEY_DEVICE 0x0700 /* device memory-mapped space */
134 /* Interrupt control values in command field */
136 #define INTR_MASK 0x0030
137 #define INTR_NEVER 0x0000 /* don't interrupt */
138 #define INTR_IFSET 0x0010 /* intr if condition bit is 1 */
139 #define INTR_IFCLR 0x0020 /* intr if condition bit is 0 */
140 #define INTR_ALWAYS 0x0030 /* always interrupt */
142 /* Branch control values in command field */
144 #define BR_MASK 0x000c
145 #define BR_NEVER 0x0000 /* don't branch */
146 #define BR_IFSET 0x0004 /* branch if condition bit is 1 */
147 #define BR_IFCLR 0x0008 /* branch if condition bit is 0 */
148 #define BR_ALWAYS 0x000c /* always branch */
150 /* Wait control values in command field */
152 #define WAIT_MASK 0x0003
153 #define WAIT_NEVER 0x0000 /* don't wait */
154 #define WAIT_IFSET 0x0001 /* wait if condition bit is 1 */
155 #define WAIT_IFCLR 0x0002 /* wait if condition bit is 0 */
156 #define WAIT_ALWAYS 0x0003 /* always wait */
158 typedef struct DBDMA_channel {
159 int channel;
160 uint32_t regs[DBDMA_REGS];
161 qemu_irq irq;
162 DBDMA_io io;
163 DBDMA_rw rw;
164 DBDMA_flush flush;
165 dbdma_cmd current;
166 int processing;
167 } DBDMA_channel;
169 typedef struct {
170 MemoryRegion mem;
171 DBDMA_channel channels[DBDMA_CHANNELS];
172 } DBDMAState;
174 #ifdef DEBUG_DBDMA
175 static void dump_dbdma_cmd(dbdma_cmd *cmd)
177 printf("dbdma_cmd %p\n", cmd);
178 printf(" req_count 0x%04x\n", le16_to_cpu(cmd->req_count));
179 printf(" command 0x%04x\n", le16_to_cpu(cmd->command));
180 printf(" phy_addr 0x%08x\n", le32_to_cpu(cmd->phy_addr));
181 printf(" cmd_dep 0x%08x\n", le32_to_cpu(cmd->cmd_dep));
182 printf(" res_count 0x%04x\n", le16_to_cpu(cmd->res_count));
183 printf(" xfer_status 0x%04x\n", le16_to_cpu(cmd->xfer_status));
185 #else
186 static void dump_dbdma_cmd(dbdma_cmd *cmd)
189 #endif
190 static void dbdma_cmdptr_load(DBDMA_channel *ch)
192 DBDMA_DPRINTF("dbdma_cmdptr_load 0x%08x\n",
193 ch->regs[DBDMA_CMDPTR_LO]);
194 cpu_physical_memory_read(ch->regs[DBDMA_CMDPTR_LO],
195 &ch->current, sizeof(dbdma_cmd));
198 static void dbdma_cmdptr_save(DBDMA_channel *ch)
200 DBDMA_DPRINTF("dbdma_cmdptr_save 0x%08x\n",
201 ch->regs[DBDMA_CMDPTR_LO]);
202 DBDMA_DPRINTF("xfer_status 0x%08x res_count 0x%04x\n",
203 le16_to_cpu(ch->current.xfer_status),
204 le16_to_cpu(ch->current.res_count));
205 cpu_physical_memory_write(ch->regs[DBDMA_CMDPTR_LO],
206 &ch->current, sizeof(dbdma_cmd));
209 static void kill_channel(DBDMA_channel *ch)
211 DBDMA_DPRINTF("kill_channel\n");
213 ch->regs[DBDMA_STATUS] |= DEAD;
214 ch->regs[DBDMA_STATUS] &= ~ACTIVE;
216 qemu_irq_raise(ch->irq);
219 static void conditional_interrupt(DBDMA_channel *ch)
221 dbdma_cmd *current = &ch->current;
222 uint16_t intr;
223 uint16_t sel_mask, sel_value;
224 uint32_t status;
225 int cond;
227 DBDMA_DPRINTF("conditional_interrupt\n");
229 intr = le16_to_cpu(current->command) & INTR_MASK;
231 switch(intr) {
232 case INTR_NEVER: /* don't interrupt */
233 return;
234 case INTR_ALWAYS: /* always interrupt */
235 qemu_irq_raise(ch->irq);
236 return;
239 status = ch->regs[DBDMA_STATUS] & DEVSTAT;
241 sel_mask = (ch->regs[DBDMA_INTR_SEL] >> 16) & 0x0f;
242 sel_value = ch->regs[DBDMA_INTR_SEL] & 0x0f;
244 cond = (status & sel_mask) == (sel_value & sel_mask);
246 switch(intr) {
247 case INTR_IFSET: /* intr if condition bit is 1 */
248 if (cond)
249 qemu_irq_raise(ch->irq);
250 return;
251 case INTR_IFCLR: /* intr if condition bit is 0 */
252 if (!cond)
253 qemu_irq_raise(ch->irq);
254 return;
258 static int conditional_wait(DBDMA_channel *ch)
260 dbdma_cmd *current = &ch->current;
261 uint16_t wait;
262 uint16_t sel_mask, sel_value;
263 uint32_t status;
264 int cond;
266 DBDMA_DPRINTF("conditional_wait\n");
268 wait = le16_to_cpu(current->command) & WAIT_MASK;
270 switch(wait) {
271 case WAIT_NEVER: /* don't wait */
272 return 0;
273 case WAIT_ALWAYS: /* always wait */
274 return 1;
277 status = ch->regs[DBDMA_STATUS] & DEVSTAT;
279 sel_mask = (ch->regs[DBDMA_WAIT_SEL] >> 16) & 0x0f;
280 sel_value = ch->regs[DBDMA_WAIT_SEL] & 0x0f;
282 cond = (status & sel_mask) == (sel_value & sel_mask);
284 switch(wait) {
285 case WAIT_IFSET: /* wait if condition bit is 1 */
286 if (cond)
287 return 1;
288 return 0;
289 case WAIT_IFCLR: /* wait if condition bit is 0 */
290 if (!cond)
291 return 1;
292 return 0;
294 return 0;
297 static void next(DBDMA_channel *ch)
299 uint32_t cp;
301 ch->regs[DBDMA_STATUS] &= ~BT;
303 cp = ch->regs[DBDMA_CMDPTR_LO];
304 ch->regs[DBDMA_CMDPTR_LO] = cp + sizeof(dbdma_cmd);
305 dbdma_cmdptr_load(ch);
308 static void branch(DBDMA_channel *ch)
310 dbdma_cmd *current = &ch->current;
312 ch->regs[DBDMA_CMDPTR_LO] = current->cmd_dep;
313 ch->regs[DBDMA_STATUS] |= BT;
314 dbdma_cmdptr_load(ch);
317 static void conditional_branch(DBDMA_channel *ch)
319 dbdma_cmd *current = &ch->current;
320 uint16_t br;
321 uint16_t sel_mask, sel_value;
322 uint32_t status;
323 int cond;
325 DBDMA_DPRINTF("conditional_branch\n");
327 /* check if we must branch */
329 br = le16_to_cpu(current->command) & BR_MASK;
331 switch(br) {
332 case BR_NEVER: /* don't branch */
333 next(ch);
334 return;
335 case BR_ALWAYS: /* always branch */
336 branch(ch);
337 return;
340 status = ch->regs[DBDMA_STATUS] & DEVSTAT;
342 sel_mask = (ch->regs[DBDMA_BRANCH_SEL] >> 16) & 0x0f;
343 sel_value = ch->regs[DBDMA_BRANCH_SEL] & 0x0f;
345 cond = (status & sel_mask) == (sel_value & sel_mask);
347 switch(br) {
348 case BR_IFSET: /* branch if condition bit is 1 */
349 if (cond)
350 branch(ch);
351 else
352 next(ch);
353 return;
354 case BR_IFCLR: /* branch if condition bit is 0 */
355 if (!cond)
356 branch(ch);
357 else
358 next(ch);
359 return;
363 static QEMUBH *dbdma_bh;
364 static void channel_run(DBDMA_channel *ch);
366 static void dbdma_end(DBDMA_io *io)
368 DBDMA_channel *ch = io->channel;
369 dbdma_cmd *current = &ch->current;
371 if (conditional_wait(ch))
372 goto wait;
374 current->xfer_status = cpu_to_le16(ch->regs[DBDMA_STATUS]);
375 current->res_count = cpu_to_le16(io->len);
376 dbdma_cmdptr_save(ch);
377 if (io->is_last)
378 ch->regs[DBDMA_STATUS] &= ~FLUSH;
380 conditional_interrupt(ch);
381 conditional_branch(ch);
383 wait:
384 ch->processing = 0;
385 if ((ch->regs[DBDMA_STATUS] & RUN) &&
386 (ch->regs[DBDMA_STATUS] & ACTIVE))
387 channel_run(ch);
390 static void start_output(DBDMA_channel *ch, int key, uint32_t addr,
391 uint16_t req_count, int is_last)
393 DBDMA_DPRINTF("start_output\n");
395 /* KEY_REGS, KEY_DEVICE and KEY_STREAM
396 * are not implemented in the mac-io chip
399 DBDMA_DPRINTF("addr 0x%x key 0x%x\n", addr, key);
400 if (!addr || key > KEY_STREAM3) {
401 kill_channel(ch);
402 return;
405 ch->io.addr = addr;
406 ch->io.len = req_count;
407 ch->io.is_last = is_last;
408 ch->io.dma_end = dbdma_end;
409 ch->io.is_dma_out = 1;
410 ch->processing = 1;
411 if (ch->rw) {
412 ch->rw(&ch->io);
416 static void start_input(DBDMA_channel *ch, int key, uint32_t addr,
417 uint16_t req_count, int is_last)
419 DBDMA_DPRINTF("start_input\n");
421 /* KEY_REGS, KEY_DEVICE and KEY_STREAM
422 * are not implemented in the mac-io chip
425 if (!addr || key > KEY_STREAM3) {
426 kill_channel(ch);
427 return;
430 ch->io.addr = addr;
431 ch->io.len = req_count;
432 ch->io.is_last = is_last;
433 ch->io.dma_end = dbdma_end;
434 ch->io.is_dma_out = 0;
435 ch->processing = 1;
436 if (ch->rw) {
437 ch->rw(&ch->io);
441 static void load_word(DBDMA_channel *ch, int key, uint32_t addr,
442 uint16_t len)
444 dbdma_cmd *current = &ch->current;
445 uint32_t val;
447 DBDMA_DPRINTF("load_word\n");
449 /* only implements KEY_SYSTEM */
451 if (key != KEY_SYSTEM) {
452 printf("DBDMA: LOAD_WORD, unimplemented key %x\n", key);
453 kill_channel(ch);
454 return;
457 cpu_physical_memory_read(addr, &val, len);
459 if (len == 2)
460 val = (val << 16) | (current->cmd_dep & 0x0000ffff);
461 else if (len == 1)
462 val = (val << 24) | (current->cmd_dep & 0x00ffffff);
464 current->cmd_dep = val;
466 if (conditional_wait(ch))
467 goto wait;
469 current->xfer_status = cpu_to_le16(ch->regs[DBDMA_STATUS]);
470 dbdma_cmdptr_save(ch);
471 ch->regs[DBDMA_STATUS] &= ~FLUSH;
473 conditional_interrupt(ch);
474 next(ch);
476 wait:
477 qemu_bh_schedule(dbdma_bh);
480 static void store_word(DBDMA_channel *ch, int key, uint32_t addr,
481 uint16_t len)
483 dbdma_cmd *current = &ch->current;
484 uint32_t val;
486 DBDMA_DPRINTF("store_word\n");
488 /* only implements KEY_SYSTEM */
490 if (key != KEY_SYSTEM) {
491 printf("DBDMA: STORE_WORD, unimplemented key %x\n", key);
492 kill_channel(ch);
493 return;
496 val = current->cmd_dep;
497 if (len == 2)
498 val >>= 16;
499 else if (len == 1)
500 val >>= 24;
502 cpu_physical_memory_write(addr, &val, len);
504 if (conditional_wait(ch))
505 goto wait;
507 current->xfer_status = cpu_to_le16(ch->regs[DBDMA_STATUS]);
508 dbdma_cmdptr_save(ch);
509 ch->regs[DBDMA_STATUS] &= ~FLUSH;
511 conditional_interrupt(ch);
512 next(ch);
514 wait:
515 qemu_bh_schedule(dbdma_bh);
518 static void nop(DBDMA_channel *ch)
520 dbdma_cmd *current = &ch->current;
522 if (conditional_wait(ch))
523 goto wait;
525 current->xfer_status = cpu_to_le16(ch->regs[DBDMA_STATUS]);
526 dbdma_cmdptr_save(ch);
528 conditional_interrupt(ch);
529 conditional_branch(ch);
531 wait:
532 qemu_bh_schedule(dbdma_bh);
535 static void stop(DBDMA_channel *ch)
537 ch->regs[DBDMA_STATUS] &= ~(ACTIVE|DEAD|FLUSH);
539 /* the stop command does not increment command pointer */
542 static void channel_run(DBDMA_channel *ch)
544 dbdma_cmd *current = &ch->current;
545 uint16_t cmd, key;
546 uint16_t req_count;
547 uint32_t phy_addr;
549 DBDMA_DPRINTF("channel_run\n");
550 dump_dbdma_cmd(current);
552 /* clear WAKE flag at command fetch */
554 ch->regs[DBDMA_STATUS] &= ~WAKE;
556 cmd = le16_to_cpu(current->command) & COMMAND_MASK;
558 switch (cmd) {
559 case DBDMA_NOP:
560 nop(ch);
561 return;
563 case DBDMA_STOP:
564 stop(ch);
565 return;
568 key = le16_to_cpu(current->command) & 0x0700;
569 req_count = le16_to_cpu(current->req_count);
570 phy_addr = le32_to_cpu(current->phy_addr);
572 if (key == KEY_STREAM4) {
573 printf("command %x, invalid key 4\n", cmd);
574 kill_channel(ch);
575 return;
578 switch (cmd) {
579 case OUTPUT_MORE:
580 start_output(ch, key, phy_addr, req_count, 0);
581 return;
583 case OUTPUT_LAST:
584 start_output(ch, key, phy_addr, req_count, 1);
585 return;
587 case INPUT_MORE:
588 start_input(ch, key, phy_addr, req_count, 0);
589 return;
591 case INPUT_LAST:
592 start_input(ch, key, phy_addr, req_count, 1);
593 return;
596 if (key < KEY_REGS) {
597 printf("command %x, invalid key %x\n", cmd, key);
598 key = KEY_SYSTEM;
601 /* for LOAD_WORD and STORE_WORD, req_count is on 3 bits
602 * and BRANCH is invalid
605 req_count = req_count & 0x0007;
606 if (req_count & 0x4) {
607 req_count = 4;
608 phy_addr &= ~3;
609 } else if (req_count & 0x2) {
610 req_count = 2;
611 phy_addr &= ~1;
612 } else
613 req_count = 1;
615 switch (cmd) {
616 case LOAD_WORD:
617 load_word(ch, key, phy_addr, req_count);
618 return;
620 case STORE_WORD:
621 store_word(ch, key, phy_addr, req_count);
622 return;
626 static void DBDMA_run(DBDMAState *s)
628 int channel;
630 for (channel = 0; channel < DBDMA_CHANNELS; channel++) {
631 DBDMA_channel *ch = &s->channels[channel];
632 uint32_t status = ch->regs[DBDMA_STATUS];
633 if (!ch->processing && (status & RUN) && (status & ACTIVE)) {
634 channel_run(ch);
639 static void DBDMA_run_bh(void *opaque)
641 DBDMAState *s = opaque;
643 DBDMA_DPRINTF("DBDMA_run_bh\n");
645 DBDMA_run(s);
648 void DBDMA_register_channel(void *dbdma, int nchan, qemu_irq irq,
649 DBDMA_rw rw, DBDMA_flush flush,
650 void *opaque)
652 DBDMAState *s = dbdma;
653 DBDMA_channel *ch = &s->channels[nchan];
655 DBDMA_DPRINTF("DBDMA_register_channel 0x%x\n", nchan);
657 ch->irq = irq;
658 ch->channel = nchan;
659 ch->rw = rw;
660 ch->flush = flush;
661 ch->io.opaque = opaque;
662 ch->io.channel = ch;
665 static void
666 dbdma_control_write(DBDMA_channel *ch)
668 uint16_t mask, value;
669 uint32_t status;
671 mask = (ch->regs[DBDMA_CONTROL] >> 16) & 0xffff;
672 value = ch->regs[DBDMA_CONTROL] & 0xffff;
674 value &= (RUN | PAUSE | FLUSH | WAKE | DEVSTAT);
676 status = ch->regs[DBDMA_STATUS];
678 status = (value & mask) | (status & ~mask);
680 if (status & WAKE)
681 status |= ACTIVE;
682 if (status & RUN) {
683 status |= ACTIVE;
684 status &= ~DEAD;
686 if (status & PAUSE)
687 status &= ~ACTIVE;
688 if ((ch->regs[DBDMA_STATUS] & RUN) && !(status & RUN)) {
689 /* RUN is cleared */
690 status &= ~(ACTIVE|DEAD);
691 if ((status & FLUSH) && ch->flush) {
692 ch->flush(&ch->io);
693 status &= ~FLUSH;
697 DBDMA_DPRINTF(" status 0x%08x\n", status);
699 ch->regs[DBDMA_STATUS] = status;
701 if (status & ACTIVE)
702 qemu_bh_schedule(dbdma_bh);
703 if ((status & FLUSH) && ch->flush)
704 ch->flush(&ch->io);
707 static void dbdma_write(void *opaque, hwaddr addr,
708 uint64_t value, unsigned size)
710 int channel = addr >> DBDMA_CHANNEL_SHIFT;
711 DBDMAState *s = opaque;
712 DBDMA_channel *ch = &s->channels[channel];
713 int reg = (addr - (channel << DBDMA_CHANNEL_SHIFT)) >> 2;
715 DBDMA_DPRINTF("writel 0x" TARGET_FMT_plx " <= 0x%08x\n", addr, value);
716 DBDMA_DPRINTF("channel 0x%x reg 0x%x\n",
717 (uint32_t)addr >> DBDMA_CHANNEL_SHIFT, reg);
719 /* cmdptr cannot be modified if channel is RUN or ACTIVE */
721 if (reg == DBDMA_CMDPTR_LO &&
722 (ch->regs[DBDMA_STATUS] & (RUN | ACTIVE)))
723 return;
725 ch->regs[reg] = value;
727 switch(reg) {
728 case DBDMA_CONTROL:
729 dbdma_control_write(ch);
730 break;
731 case DBDMA_CMDPTR_LO:
732 /* 16-byte aligned */
733 ch->regs[DBDMA_CMDPTR_LO] &= ~0xf;
734 dbdma_cmdptr_load(ch);
735 break;
736 case DBDMA_STATUS:
737 case DBDMA_INTR_SEL:
738 case DBDMA_BRANCH_SEL:
739 case DBDMA_WAIT_SEL:
740 /* nothing to do */
741 break;
742 case DBDMA_XFER_MODE:
743 case DBDMA_CMDPTR_HI:
744 case DBDMA_DATA2PTR_HI:
745 case DBDMA_DATA2PTR_LO:
746 case DBDMA_ADDRESS_HI:
747 case DBDMA_BRANCH_ADDR_HI:
748 case DBDMA_RES1:
749 case DBDMA_RES2:
750 case DBDMA_RES3:
751 case DBDMA_RES4:
752 /* unused */
753 break;
757 static uint64_t dbdma_read(void *opaque, hwaddr addr,
758 unsigned size)
760 uint32_t value;
761 int channel = addr >> DBDMA_CHANNEL_SHIFT;
762 DBDMAState *s = opaque;
763 DBDMA_channel *ch = &s->channels[channel];
764 int reg = (addr - (channel << DBDMA_CHANNEL_SHIFT)) >> 2;
766 value = ch->regs[reg];
768 DBDMA_DPRINTF("readl 0x" TARGET_FMT_plx " => 0x%08x\n", addr, value);
769 DBDMA_DPRINTF("channel 0x%x reg 0x%x\n",
770 (uint32_t)addr >> DBDMA_CHANNEL_SHIFT, reg);
772 switch(reg) {
773 case DBDMA_CONTROL:
774 value = 0;
775 break;
776 case DBDMA_STATUS:
777 case DBDMA_CMDPTR_LO:
778 case DBDMA_INTR_SEL:
779 case DBDMA_BRANCH_SEL:
780 case DBDMA_WAIT_SEL:
781 /* nothing to do */
782 break;
783 case DBDMA_XFER_MODE:
784 case DBDMA_CMDPTR_HI:
785 case DBDMA_DATA2PTR_HI:
786 case DBDMA_DATA2PTR_LO:
787 case DBDMA_ADDRESS_HI:
788 case DBDMA_BRANCH_ADDR_HI:
789 /* unused */
790 value = 0;
791 break;
792 case DBDMA_RES1:
793 case DBDMA_RES2:
794 case DBDMA_RES3:
795 case DBDMA_RES4:
796 /* reserved */
797 break;
800 return value;
803 static const MemoryRegionOps dbdma_ops = {
804 .read = dbdma_read,
805 .write = dbdma_write,
806 .endianness = DEVICE_LITTLE_ENDIAN,
807 .valid = {
808 .min_access_size = 4,
809 .max_access_size = 4,
813 static const VMStateDescription vmstate_dbdma_channel = {
814 .name = "dbdma_channel",
815 .version_id = 0,
816 .minimum_version_id = 0,
817 .minimum_version_id_old = 0,
818 .fields = (VMStateField[]) {
819 VMSTATE_UINT32_ARRAY(regs, struct DBDMA_channel, DBDMA_REGS),
820 VMSTATE_END_OF_LIST()
824 static const VMStateDescription vmstate_dbdma = {
825 .name = "dbdma",
826 .version_id = 2,
827 .minimum_version_id = 2,
828 .minimum_version_id_old = 2,
829 .fields = (VMStateField[]) {
830 VMSTATE_STRUCT_ARRAY(channels, DBDMAState, DBDMA_CHANNELS, 1,
831 vmstate_dbdma_channel, DBDMA_channel),
832 VMSTATE_END_OF_LIST()
836 static void dbdma_reset(void *opaque)
838 DBDMAState *s = opaque;
839 int i;
841 for (i = 0; i < DBDMA_CHANNELS; i++)
842 memset(s->channels[i].regs, 0, DBDMA_SIZE);
845 void* DBDMA_init (MemoryRegion **dbdma_mem)
847 DBDMAState *s;
849 s = g_malloc0(sizeof(DBDMAState));
851 memory_region_init_io(&s->mem, &dbdma_ops, s, "dbdma", 0x1000);
852 *dbdma_mem = &s->mem;
853 vmstate_register(NULL, -1, &vmstate_dbdma, s);
854 qemu_register_reset(dbdma_reset, s);
856 dbdma_bh = qemu_bh_new(DBDMA_run_bh, s);
858 return s;