]> git.ipfire.org Git - thirdparty/qemu.git/blame - hw/mac_dbdma.c
misc: move include files to include/qemu/
[thirdparty/qemu.git] / hw / mac_dbdma.c
CommitLineData
3cbee15b
JM
1/*
2 * PowerMac descriptor-based DMA emulation
3 *
4 * Copyright (c) 2005-2007 Fabrice Bellard
5 * Copyright (c) 2007 Jocelyn Mayer
28ce5ce6
AJ
6 * Copyright (c) 2009 Laurent Vivier
7 *
8 * some parts from linux-2.6.28, arch/powerpc/include/asm/dbdma.h
9 *
10 * Definitions for using the Apple Descriptor-Based DMA controller
11 * in Power Macintosh computers.
12 *
13 * Copyright (C) 1996 Paul Mackerras.
14 *
15 * some parts from mol 0.9.71
16 *
17 * Descriptor based DMA emulation
18 *
19 * Copyright (C) 1998-2004 Samuel Rydh (samuel@ibrium.se)
3cbee15b
JM
20 *
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:
27 *
28 * The above copyright notice and this permission notice shall be included in
29 * all copies or substantial portions of the Software.
30 *
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.
38 */
87ecb68b 39#include "hw.h"
28ce5ce6
AJ
40#include "isa.h"
41#include "mac_dbdma.h"
1de7afc9 42#include "qemu/main-loop.h"
3cbee15b 43
ea026b2f
BS
44/* debug DBDMA */
45//#define DEBUG_DBDMA
46
47#ifdef DEBUG_DBDMA
001faf32
BS
48#define DBDMA_DPRINTF(fmt, ...) \
49 do { printf("DBDMA: " fmt , ## __VA_ARGS__); } while (0)
ea026b2f 50#else
001faf32 51#define DBDMA_DPRINTF(fmt, ...)
ea026b2f
BS
52#endif
53
28ce5ce6
AJ
54/*
55 */
56
57/*
58 * DBDMA control/status registers. All little-endian.
59 */
3cbee15b 60
28ce5ce6
AJ
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
77
78#define DBDMA_REGS 16
79#define DBDMA_SIZE (DBDMA_REGS * sizeof(uint32_t))
80
81#define DBDMA_CHANNEL_SHIFT 7
82#define DBDMA_CHANNEL_SIZE (1 << DBDMA_CHANNEL_SHIFT)
83
84#define DBDMA_CHANNELS (0x1000 >> DBDMA_CHANNEL_SHIFT)
85
86/* Bits in control and status registers */
87
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
96
97/*
98 * DBDMA command structure. These fields are all little-endian!
99 */
100
101typedef 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;
109
110/* DBDMA command values in command field */
111
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 */
121
122/* Key values in command field */
123
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 */
133
134/* Interrupt control values in command field */
135
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 */
141
142/* Branch control values in command field */
143
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 */
149
150/* Wait control values in command field */
151
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 */
157
158typedef struct DBDMA_channel {
159 int channel;
160 uint32_t regs[DBDMA_REGS];
161 qemu_irq irq;
b42ec42d
AJ
162 DBDMA_io io;
163 DBDMA_rw rw;
862c9280 164 DBDMA_flush flush;
28ce5ce6 165 dbdma_cmd current;
b42ec42d 166 int processing;
28ce5ce6
AJ
167} DBDMA_channel;
168
c20df14b 169typedef struct {
23c5e4ca 170 MemoryRegion mem;
c20df14b
JQ
171 DBDMA_channel channels[DBDMA_CHANNELS];
172} DBDMAState;
173
28ce5ce6
AJ
174#ifdef DEBUG_DBDMA
175static void dump_dbdma_cmd(dbdma_cmd *cmd)
176{
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));
184}
185#else
186static void dump_dbdma_cmd(dbdma_cmd *cmd)
3cbee15b 187{
28ce5ce6
AJ
188}
189#endif
190static void dbdma_cmdptr_load(DBDMA_channel *ch)
191{
192 DBDMA_DPRINTF("dbdma_cmdptr_load 0x%08x\n",
ad674e53
AJ
193 ch->regs[DBDMA_CMDPTR_LO]);
194 cpu_physical_memory_read(ch->regs[DBDMA_CMDPTR_LO],
28ce5ce6 195 (uint8_t*)&ch->current, sizeof(dbdma_cmd));
3cbee15b
JM
196}
197
28ce5ce6 198static void dbdma_cmdptr_save(DBDMA_channel *ch)
3cbee15b 199{
28ce5ce6 200 DBDMA_DPRINTF("dbdma_cmdptr_save 0x%08x\n",
ad674e53 201 ch->regs[DBDMA_CMDPTR_LO]);
28ce5ce6
AJ
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));
ad674e53 205 cpu_physical_memory_write(ch->regs[DBDMA_CMDPTR_LO],
28ce5ce6 206 (uint8_t*)&ch->current, sizeof(dbdma_cmd));
3cbee15b
JM
207}
208
28ce5ce6 209static void kill_channel(DBDMA_channel *ch)
3cbee15b 210{
28ce5ce6
AJ
211 DBDMA_DPRINTF("kill_channel\n");
212
ad674e53
AJ
213 ch->regs[DBDMA_STATUS] |= DEAD;
214 ch->regs[DBDMA_STATUS] &= ~ACTIVE;
28ce5ce6
AJ
215
216 qemu_irq_raise(ch->irq);
217}
218
219static void conditional_interrupt(DBDMA_channel *ch)
220{
221 dbdma_cmd *current = &ch->current;
222 uint16_t intr;
223 uint16_t sel_mask, sel_value;
224 uint32_t status;
225 int cond;
226
227 DBDMA_DPRINTF("conditional_interrupt\n");
228
b42ec42d 229 intr = le16_to_cpu(current->command) & INTR_MASK;
28ce5ce6
AJ
230
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;
237 }
238
ad674e53 239 status = ch->regs[DBDMA_STATUS] & DEVSTAT;
28ce5ce6 240
ad674e53
AJ
241 sel_mask = (ch->regs[DBDMA_INTR_SEL] >> 16) & 0x0f;
242 sel_value = ch->regs[DBDMA_INTR_SEL] & 0x0f;
28ce5ce6
AJ
243
244 cond = (status & sel_mask) == (sel_value & sel_mask);
245
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;
255 }
256}
257
258static int conditional_wait(DBDMA_channel *ch)
259{
260 dbdma_cmd *current = &ch->current;
261 uint16_t wait;
262 uint16_t sel_mask, sel_value;
263 uint32_t status;
264 int cond;
265
266 DBDMA_DPRINTF("conditional_wait\n");
267
b42ec42d 268 wait = le16_to_cpu(current->command) & WAIT_MASK;
28ce5ce6
AJ
269
270 switch(wait) {
271 case WAIT_NEVER: /* don't wait */
272 return 0;
273 case WAIT_ALWAYS: /* always wait */
274 return 1;
275 }
276
ad674e53 277 status = ch->regs[DBDMA_STATUS] & DEVSTAT;
28ce5ce6 278
ad674e53
AJ
279 sel_mask = (ch->regs[DBDMA_WAIT_SEL] >> 16) & 0x0f;
280 sel_value = ch->regs[DBDMA_WAIT_SEL] & 0x0f;
28ce5ce6
AJ
281
282 cond = (status & sel_mask) == (sel_value & sel_mask);
283
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;
293 }
294 return 0;
295}
296
297static void next(DBDMA_channel *ch)
298{
299 uint32_t cp;
300
ad674e53 301 ch->regs[DBDMA_STATUS] &= ~BT;
28ce5ce6 302
ad674e53
AJ
303 cp = ch->regs[DBDMA_CMDPTR_LO];
304 ch->regs[DBDMA_CMDPTR_LO] = cp + sizeof(dbdma_cmd);
28ce5ce6
AJ
305 dbdma_cmdptr_load(ch);
306}
307
308static void branch(DBDMA_channel *ch)
309{
310 dbdma_cmd *current = &ch->current;
311
312 ch->regs[DBDMA_CMDPTR_LO] = current->cmd_dep;
ad674e53 313 ch->regs[DBDMA_STATUS] |= BT;
28ce5ce6
AJ
314 dbdma_cmdptr_load(ch);
315}
316
317static void conditional_branch(DBDMA_channel *ch)
318{
319 dbdma_cmd *current = &ch->current;
320 uint16_t br;
321 uint16_t sel_mask, sel_value;
322 uint32_t status;
323 int cond;
324
325 DBDMA_DPRINTF("conditional_branch\n");
326
327 /* check if we must branch */
328
b42ec42d 329 br = le16_to_cpu(current->command) & BR_MASK;
28ce5ce6
AJ
330
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;
338 }
339
ad674e53 340 status = ch->regs[DBDMA_STATUS] & DEVSTAT;
28ce5ce6 341
ad674e53
AJ
342 sel_mask = (ch->regs[DBDMA_BRANCH_SEL] >> 16) & 0x0f;
343 sel_value = ch->regs[DBDMA_BRANCH_SEL] & 0x0f;
28ce5ce6
AJ
344
345 cond = (status & sel_mask) == (sel_value & sel_mask);
346
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;
360 }
361}
362
b42ec42d
AJ
363static QEMUBH *dbdma_bh;
364static void channel_run(DBDMA_channel *ch);
28ce5ce6 365
b42ec42d 366static void dbdma_end(DBDMA_io *io)
28ce5ce6
AJ
367{
368 DBDMA_channel *ch = io->channel;
369 dbdma_cmd *current = &ch->current;
370
b42ec42d
AJ
371 if (conditional_wait(ch))
372 goto wait;
28ce5ce6 373
ad674e53
AJ
374 current->xfer_status = cpu_to_le16(ch->regs[DBDMA_STATUS]);
375 current->res_count = cpu_to_le16(io->len);
b42ec42d 376 dbdma_cmdptr_save(ch);
862c9280 377 if (io->is_last)
ad674e53 378 ch->regs[DBDMA_STATUS] &= ~FLUSH;
b42ec42d
AJ
379
380 conditional_interrupt(ch);
381 conditional_branch(ch);
28ce5ce6 382
b42ec42d
AJ
383wait:
384 ch->processing = 0;
ad674e53
AJ
385 if ((ch->regs[DBDMA_STATUS] & RUN) &&
386 (ch->regs[DBDMA_STATUS] & ACTIVE))
b42ec42d 387 channel_run(ch);
28ce5ce6
AJ
388}
389
b42ec42d 390static void start_output(DBDMA_channel *ch, int key, uint32_t addr,
28ce5ce6
AJ
391 uint16_t req_count, int is_last)
392{
28ce5ce6
AJ
393 DBDMA_DPRINTF("start_output\n");
394
395 /* KEY_REGS, KEY_DEVICE and KEY_STREAM
396 * are not implemented in the mac-io chip
397 */
398
399 DBDMA_DPRINTF("addr 0x%x key 0x%x\n", addr, key);
400 if (!addr || key > KEY_STREAM3) {
401 kill_channel(ch);
b42ec42d 402 return;
28ce5ce6
AJ
403 }
404
b42ec42d 405 ch->io.addr = addr;
28ce5ce6
AJ
406 ch->io.len = req_count;
407 ch->io.is_last = is_last;
b42ec42d
AJ
408 ch->io.dma_end = dbdma_end;
409 ch->io.is_dma_out = 1;
410 ch->processing = 1;
a9ceb76d
AG
411 if (ch->rw) {
412 ch->rw(&ch->io);
413 }
28ce5ce6
AJ
414}
415
b42ec42d 416static void start_input(DBDMA_channel *ch, int key, uint32_t addr,
28ce5ce6
AJ
417 uint16_t req_count, int is_last)
418{
28ce5ce6
AJ
419 DBDMA_DPRINTF("start_input\n");
420
421 /* KEY_REGS, KEY_DEVICE and KEY_STREAM
422 * are not implemented in the mac-io chip
423 */
424
425 if (!addr || key > KEY_STREAM3) {
426 kill_channel(ch);
b42ec42d 427 return;
28ce5ce6
AJ
428 }
429
b42ec42d 430 ch->io.addr = addr;
28ce5ce6
AJ
431 ch->io.len = req_count;
432 ch->io.is_last = is_last;
b42ec42d
AJ
433 ch->io.dma_end = dbdma_end;
434 ch->io.is_dma_out = 0;
435 ch->processing = 1;
a9ceb76d
AG
436 if (ch->rw) {
437 ch->rw(&ch->io);
438 }
28ce5ce6
AJ
439}
440
b42ec42d 441static void load_word(DBDMA_channel *ch, int key, uint32_t addr,
28ce5ce6
AJ
442 uint16_t len)
443{
444 dbdma_cmd *current = &ch->current;
445 uint32_t val;
446
447 DBDMA_DPRINTF("load_word\n");
448
449 /* only implements KEY_SYSTEM */
450
451 if (key != KEY_SYSTEM) {
452 printf("DBDMA: LOAD_WORD, unimplemented key %x\n", key);
453 kill_channel(ch);
b42ec42d 454 return;
28ce5ce6
AJ
455 }
456
457 cpu_physical_memory_read(addr, (uint8_t*)&val, len);
458
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);
463
464 current->cmd_dep = val;
465
466 if (conditional_wait(ch))
b42ec42d 467 goto wait;
28ce5ce6 468
ad674e53 469 current->xfer_status = cpu_to_le16(ch->regs[DBDMA_STATUS]);
28ce5ce6 470 dbdma_cmdptr_save(ch);
ad674e53 471 ch->regs[DBDMA_STATUS] &= ~FLUSH;
28ce5ce6
AJ
472
473 conditional_interrupt(ch);
474 next(ch);
475
b42ec42d
AJ
476wait:
477 qemu_bh_schedule(dbdma_bh);
28ce5ce6
AJ
478}
479
b42ec42d 480static void store_word(DBDMA_channel *ch, int key, uint32_t addr,
28ce5ce6
AJ
481 uint16_t len)
482{
483 dbdma_cmd *current = &ch->current;
484 uint32_t val;
485
486 DBDMA_DPRINTF("store_word\n");
487
488 /* only implements KEY_SYSTEM */
489
490 if (key != KEY_SYSTEM) {
491 printf("DBDMA: STORE_WORD, unimplemented key %x\n", key);
492 kill_channel(ch);
b42ec42d 493 return;
28ce5ce6
AJ
494 }
495
496 val = current->cmd_dep;
497 if (len == 2)
498 val >>= 16;
499 else if (len == 1)
500 val >>= 24;
501
502 cpu_physical_memory_write(addr, (uint8_t*)&val, len);
503
504 if (conditional_wait(ch))
b42ec42d 505 goto wait;
28ce5ce6 506
ad674e53 507 current->xfer_status = cpu_to_le16(ch->regs[DBDMA_STATUS]);
28ce5ce6 508 dbdma_cmdptr_save(ch);
ad674e53 509 ch->regs[DBDMA_STATUS] &= ~FLUSH;
28ce5ce6
AJ
510
511 conditional_interrupt(ch);
512 next(ch);
513
b42ec42d
AJ
514wait:
515 qemu_bh_schedule(dbdma_bh);
28ce5ce6
AJ
516}
517
b42ec42d 518static void nop(DBDMA_channel *ch)
28ce5ce6
AJ
519{
520 dbdma_cmd *current = &ch->current;
521
522 if (conditional_wait(ch))
b42ec42d 523 goto wait;
28ce5ce6 524
ad674e53 525 current->xfer_status = cpu_to_le16(ch->regs[DBDMA_STATUS]);
28ce5ce6
AJ
526 dbdma_cmdptr_save(ch);
527
528 conditional_interrupt(ch);
529 conditional_branch(ch);
530
b42ec42d
AJ
531wait:
532 qemu_bh_schedule(dbdma_bh);
3cbee15b
JM
533}
534
b42ec42d 535static void stop(DBDMA_channel *ch)
3cbee15b 536{
ad674e53 537 ch->regs[DBDMA_STATUS] &= ~(ACTIVE|DEAD|FLUSH);
28ce5ce6
AJ
538
539 /* the stop command does not increment command pointer */
3cbee15b
JM
540}
541
b42ec42d 542static void channel_run(DBDMA_channel *ch)
3cbee15b 543{
28ce5ce6
AJ
544 dbdma_cmd *current = &ch->current;
545 uint16_t cmd, key;
546 uint16_t req_count;
547 uint32_t phy_addr;
548
549 DBDMA_DPRINTF("channel_run\n");
550 dump_dbdma_cmd(current);
551
552 /* clear WAKE flag at command fetch */
553
ad674e53 554 ch->regs[DBDMA_STATUS] &= ~WAKE;
28ce5ce6
AJ
555
556 cmd = le16_to_cpu(current->command) & COMMAND_MASK;
557
558 switch (cmd) {
559 case DBDMA_NOP:
b42ec42d
AJ
560 nop(ch);
561 return;
28ce5ce6
AJ
562
563 case DBDMA_STOP:
b42ec42d
AJ
564 stop(ch);
565 return;
28ce5ce6
AJ
566 }
567
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);
571
572 if (key == KEY_STREAM4) {
573 printf("command %x, invalid key 4\n", cmd);
574 kill_channel(ch);
b42ec42d 575 return;
28ce5ce6
AJ
576 }
577
578 switch (cmd) {
579 case OUTPUT_MORE:
b42ec42d
AJ
580 start_output(ch, key, phy_addr, req_count, 0);
581 return;
28ce5ce6
AJ
582
583 case OUTPUT_LAST:
b42ec42d
AJ
584 start_output(ch, key, phy_addr, req_count, 1);
585 return;
28ce5ce6
AJ
586
587 case INPUT_MORE:
b42ec42d
AJ
588 start_input(ch, key, phy_addr, req_count, 0);
589 return;
28ce5ce6
AJ
590
591 case INPUT_LAST:
b42ec42d
AJ
592 start_input(ch, key, phy_addr, req_count, 1);
593 return;
28ce5ce6
AJ
594 }
595
596 if (key < KEY_REGS) {
597 printf("command %x, invalid key %x\n", cmd, key);
598 key = KEY_SYSTEM;
599 }
600
601 /* for LOAD_WORD and STORE_WORD, req_count is on 3 bits
602 * and BRANCH is invalid
603 */
604
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;
614
615 switch (cmd) {
616 case LOAD_WORD:
b42ec42d
AJ
617 load_word(ch, key, phy_addr, req_count);
618 return;
28ce5ce6
AJ
619
620 case STORE_WORD:
b42ec42d
AJ
621 store_word(ch, key, phy_addr, req_count);
622 return;
28ce5ce6 623 }
3cbee15b
JM
624}
625
c20df14b 626static void DBDMA_run(DBDMAState *s)
28ce5ce6
AJ
627{
628 int channel;
28ce5ce6 629
c20df14b
JQ
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);
635 }
28ce5ce6 636 }
28ce5ce6
AJ
637}
638
639static void DBDMA_run_bh(void *opaque)
640{
c20df14b 641 DBDMAState *s = opaque;
28ce5ce6
AJ
642
643 DBDMA_DPRINTF("DBDMA_run_bh\n");
644
c20df14b 645 DBDMA_run(s);
28ce5ce6
AJ
646}
647
648void DBDMA_register_channel(void *dbdma, int nchan, qemu_irq irq,
862c9280 649 DBDMA_rw rw, DBDMA_flush flush,
28ce5ce6
AJ
650 void *opaque)
651{
c20df14b
JQ
652 DBDMAState *s = dbdma;
653 DBDMA_channel *ch = &s->channels[nchan];
28ce5ce6
AJ
654
655 DBDMA_DPRINTF("DBDMA_register_channel 0x%x\n", nchan);
656
657 ch->irq = irq;
658 ch->channel = nchan;
b42ec42d 659 ch->rw = rw;
862c9280 660 ch->flush = flush;
28ce5ce6
AJ
661 ch->io.opaque = opaque;
662 ch->io.channel = ch;
663}
664
28ce5ce6
AJ
665static void
666dbdma_control_write(DBDMA_channel *ch)
667{
668 uint16_t mask, value;
669 uint32_t status;
670
ad674e53
AJ
671 mask = (ch->regs[DBDMA_CONTROL] >> 16) & 0xffff;
672 value = ch->regs[DBDMA_CONTROL] & 0xffff;
28ce5ce6
AJ
673
674 value &= (RUN | PAUSE | FLUSH | WAKE | DEVSTAT);
675
ad674e53 676 status = ch->regs[DBDMA_STATUS];
28ce5ce6
AJ
677
678 status = (value & mask) | (status & ~mask);
679
680 if (status & WAKE)
681 status |= ACTIVE;
682 if (status & RUN) {
683 status |= ACTIVE;
684 status &= ~DEAD;
685 }
686 if (status & PAUSE)
687 status &= ~ACTIVE;
ad674e53 688 if ((ch->regs[DBDMA_STATUS] & RUN) && !(status & RUN)) {
28ce5ce6
AJ
689 /* RUN is cleared */
690 status &= ~(ACTIVE|DEAD);
691 }
692
693 DBDMA_DPRINTF(" status 0x%08x\n", status);
694
ad674e53 695 ch->regs[DBDMA_STATUS] = status;
28ce5ce6 696
b42ec42d
AJ
697 if (status & ACTIVE)
698 qemu_bh_schedule(dbdma_bh);
a9ceb76d 699 if ((status & FLUSH) && ch->flush)
862c9280 700 ch->flush(&ch->io);
28ce5ce6
AJ
701}
702
a8170e5e 703static void dbdma_write(void *opaque, hwaddr addr,
23c5e4ca 704 uint64_t value, unsigned size)
28ce5ce6
AJ
705{
706 int channel = addr >> DBDMA_CHANNEL_SHIFT;
c20df14b
JQ
707 DBDMAState *s = opaque;
708 DBDMA_channel *ch = &s->channels[channel];
28ce5ce6
AJ
709 int reg = (addr - (channel << DBDMA_CHANNEL_SHIFT)) >> 2;
710
711 DBDMA_DPRINTF("writel 0x" TARGET_FMT_plx " <= 0x%08x\n", addr, value);
712 DBDMA_DPRINTF("channel 0x%x reg 0x%x\n",
713 (uint32_t)addr >> DBDMA_CHANNEL_SHIFT, reg);
714
715 /* cmdptr cannot be modified if channel is RUN or ACTIVE */
716
717 if (reg == DBDMA_CMDPTR_LO &&
ad674e53 718 (ch->regs[DBDMA_STATUS] & (RUN | ACTIVE)))
28ce5ce6
AJ
719 return;
720
721 ch->regs[reg] = value;
722
723 switch(reg) {
724 case DBDMA_CONTROL:
725 dbdma_control_write(ch);
726 break;
727 case DBDMA_CMDPTR_LO:
728 /* 16-byte aligned */
ad674e53 729 ch->regs[DBDMA_CMDPTR_LO] &= ~0xf;
28ce5ce6
AJ
730 dbdma_cmdptr_load(ch);
731 break;
732 case DBDMA_STATUS:
733 case DBDMA_INTR_SEL:
734 case DBDMA_BRANCH_SEL:
735 case DBDMA_WAIT_SEL:
736 /* nothing to do */
737 break;
738 case DBDMA_XFER_MODE:
739 case DBDMA_CMDPTR_HI:
740 case DBDMA_DATA2PTR_HI:
741 case DBDMA_DATA2PTR_LO:
742 case DBDMA_ADDRESS_HI:
743 case DBDMA_BRANCH_ADDR_HI:
744 case DBDMA_RES1:
745 case DBDMA_RES2:
746 case DBDMA_RES3:
747 case DBDMA_RES4:
748 /* unused */
749 break;
750 }
751}
752
a8170e5e 753static uint64_t dbdma_read(void *opaque, hwaddr addr,
23c5e4ca 754 unsigned size)
3cbee15b 755{
28ce5ce6
AJ
756 uint32_t value;
757 int channel = addr >> DBDMA_CHANNEL_SHIFT;
c20df14b
JQ
758 DBDMAState *s = opaque;
759 DBDMA_channel *ch = &s->channels[channel];
28ce5ce6 760 int reg = (addr - (channel << DBDMA_CHANNEL_SHIFT)) >> 2;
ea026b2f 761
28ce5ce6
AJ
762 value = ch->regs[reg];
763
764 DBDMA_DPRINTF("readl 0x" TARGET_FMT_plx " => 0x%08x\n", addr, value);
765 DBDMA_DPRINTF("channel 0x%x reg 0x%x\n",
766 (uint32_t)addr >> DBDMA_CHANNEL_SHIFT, reg);
767
768 switch(reg) {
769 case DBDMA_CONTROL:
770 value = 0;
771 break;
772 case DBDMA_STATUS:
773 case DBDMA_CMDPTR_LO:
774 case DBDMA_INTR_SEL:
775 case DBDMA_BRANCH_SEL:
776 case DBDMA_WAIT_SEL:
777 /* nothing to do */
778 break;
779 case DBDMA_XFER_MODE:
780 case DBDMA_CMDPTR_HI:
781 case DBDMA_DATA2PTR_HI:
782 case DBDMA_DATA2PTR_LO:
783 case DBDMA_ADDRESS_HI:
784 case DBDMA_BRANCH_ADDR_HI:
785 /* unused */
786 value = 0;
787 break;
788 case DBDMA_RES1:
789 case DBDMA_RES2:
790 case DBDMA_RES3:
791 case DBDMA_RES4:
792 /* reserved */
793 break;
794 }
795
796 return value;
3cbee15b
JM
797}
798
23c5e4ca
AK
799static const MemoryRegionOps dbdma_ops = {
800 .read = dbdma_read,
801 .write = dbdma_write,
802 .endianness = DEVICE_LITTLE_ENDIAN,
803 .valid = {
804 .min_access_size = 4,
805 .max_access_size = 4,
806 },
3cbee15b
JM
807};
808
da26fdc3
JQ
809static const VMStateDescription vmstate_dbdma_channel = {
810 .name = "dbdma_channel",
811 .version_id = 0,
812 .minimum_version_id = 0,
813 .minimum_version_id_old = 0,
814 .fields = (VMStateField[]) {
815 VMSTATE_UINT32_ARRAY(regs, struct DBDMA_channel, DBDMA_REGS),
816 VMSTATE_END_OF_LIST()
817 }
818};
28ce5ce6 819
da26fdc3
JQ
820static const VMStateDescription vmstate_dbdma = {
821 .name = "dbdma",
822 .version_id = 2,
823 .minimum_version_id = 2,
824 .minimum_version_id_old = 2,
825 .fields = (VMStateField[]) {
826 VMSTATE_STRUCT_ARRAY(channels, DBDMAState, DBDMA_CHANNELS, 1,
827 vmstate_dbdma_channel, DBDMA_channel),
828 VMSTATE_END_OF_LIST()
829 }
830};
9b64997f 831
6e6b7363
BS
832static void dbdma_reset(void *opaque)
833{
c20df14b 834 DBDMAState *s = opaque;
28ce5ce6
AJ
835 int i;
836
837 for (i = 0; i < DBDMA_CHANNELS; i++)
c20df14b 838 memset(s->channels[i].regs, 0, DBDMA_SIZE);
6e6b7363
BS
839}
840
23c5e4ca 841void* DBDMA_init (MemoryRegion **dbdma_mem)
3cbee15b 842{
c20df14b 843 DBDMAState *s;
28ce5ce6 844
7267c094 845 s = g_malloc0(sizeof(DBDMAState));
28ce5ce6 846
23c5e4ca
AK
847 memory_region_init_io(&s->mem, &dbdma_ops, s, "dbdma", 0x1000);
848 *dbdma_mem = &s->mem;
da26fdc3 849 vmstate_register(NULL, -1, &vmstate_dbdma, s);
a08d4367 850 qemu_register_reset(dbdma_reset, s);
28ce5ce6
AJ
851
852 dbdma_bh = qemu_bh_new(DBDMA_run_bh, s);
853
854 return s;
3cbee15b 855}