]> git.proxmox.com Git - qemu.git/blame - hw/mac_dbdma.c
Use glib memory allocation and free functions
[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"
3cbee15b 42
ea026b2f
BS
43/* debug DBDMA */
44//#define DEBUG_DBDMA
45
46#ifdef DEBUG_DBDMA
001faf32
BS
47#define DBDMA_DPRINTF(fmt, ...) \
48 do { printf("DBDMA: " fmt , ## __VA_ARGS__); } while (0)
ea026b2f 49#else
001faf32 50#define DBDMA_DPRINTF(fmt, ...)
ea026b2f
BS
51#endif
52
28ce5ce6
AJ
53/*
54 */
55
56/*
57 * DBDMA control/status registers. All little-endian.
58 */
3cbee15b 59
28ce5ce6
AJ
60#define DBDMA_CONTROL 0x00
61#define DBDMA_STATUS 0x01
62#define DBDMA_CMDPTR_HI 0x02
63#define DBDMA_CMDPTR_LO 0x03
64#define DBDMA_INTR_SEL 0x04
65#define DBDMA_BRANCH_SEL 0x05
66#define DBDMA_WAIT_SEL 0x06
67#define DBDMA_XFER_MODE 0x07
68#define DBDMA_DATA2PTR_HI 0x08
69#define DBDMA_DATA2PTR_LO 0x09
70#define DBDMA_RES1 0x0A
71#define DBDMA_ADDRESS_HI 0x0B
72#define DBDMA_BRANCH_ADDR_HI 0x0C
73#define DBDMA_RES2 0x0D
74#define DBDMA_RES3 0x0E
75#define DBDMA_RES4 0x0F
76
77#define DBDMA_REGS 16
78#define DBDMA_SIZE (DBDMA_REGS * sizeof(uint32_t))
79
80#define DBDMA_CHANNEL_SHIFT 7
81#define DBDMA_CHANNEL_SIZE (1 << DBDMA_CHANNEL_SHIFT)
82
83#define DBDMA_CHANNELS (0x1000 >> DBDMA_CHANNEL_SHIFT)
84
85/* Bits in control and status registers */
86
87#define RUN 0x8000
88#define PAUSE 0x4000
89#define FLUSH 0x2000
90#define WAKE 0x1000
91#define DEAD 0x0800
92#define ACTIVE 0x0400
93#define BT 0x0100
94#define DEVSTAT 0x00ff
95
96/*
97 * DBDMA command structure. These fields are all little-endian!
98 */
99
100typedef struct dbdma_cmd {
101 uint16_t req_count; /* requested byte transfer count */
102 uint16_t command; /* command word (has bit-fields) */
103 uint32_t phy_addr; /* physical data address */
104 uint32_t cmd_dep; /* command-dependent field */
105 uint16_t res_count; /* residual count after completion */
106 uint16_t xfer_status; /* transfer status */
107} dbdma_cmd;
108
109/* DBDMA command values in command field */
110
111#define COMMAND_MASK 0xf000
112#define OUTPUT_MORE 0x0000 /* transfer memory data to stream */
113#define OUTPUT_LAST 0x1000 /* ditto followed by end marker */
114#define INPUT_MORE 0x2000 /* transfer stream data to memory */
115#define INPUT_LAST 0x3000 /* ditto, expect end marker */
116#define STORE_WORD 0x4000 /* write word (4 bytes) to device reg */
117#define LOAD_WORD 0x5000 /* read word (4 bytes) from device reg */
118#define DBDMA_NOP 0x6000 /* do nothing */
119#define DBDMA_STOP 0x7000 /* suspend processing */
120
121/* Key values in command field */
122
123#define KEY_MASK 0x0700
124#define KEY_STREAM0 0x0000 /* usual data stream */
125#define KEY_STREAM1 0x0100 /* control/status stream */
126#define KEY_STREAM2 0x0200 /* device-dependent stream */
127#define KEY_STREAM3 0x0300 /* device-dependent stream */
128#define KEY_STREAM4 0x0400 /* reserved */
129#define KEY_REGS 0x0500 /* device register space */
130#define KEY_SYSTEM 0x0600 /* system memory-mapped space */
131#define KEY_DEVICE 0x0700 /* device memory-mapped space */
132
133/* Interrupt control values in command field */
134
135#define INTR_MASK 0x0030
136#define INTR_NEVER 0x0000 /* don't interrupt */
137#define INTR_IFSET 0x0010 /* intr if condition bit is 1 */
138#define INTR_IFCLR 0x0020 /* intr if condition bit is 0 */
139#define INTR_ALWAYS 0x0030 /* always interrupt */
140
141/* Branch control values in command field */
142
143#define BR_MASK 0x000c
144#define BR_NEVER 0x0000 /* don't branch */
145#define BR_IFSET 0x0004 /* branch if condition bit is 1 */
146#define BR_IFCLR 0x0008 /* branch if condition bit is 0 */
147#define BR_ALWAYS 0x000c /* always branch */
148
149/* Wait control values in command field */
150
151#define WAIT_MASK 0x0003
152#define WAIT_NEVER 0x0000 /* don't wait */
153#define WAIT_IFSET 0x0001 /* wait if condition bit is 1 */
154#define WAIT_IFCLR 0x0002 /* wait if condition bit is 0 */
155#define WAIT_ALWAYS 0x0003 /* always wait */
156
157typedef struct DBDMA_channel {
158 int channel;
159 uint32_t regs[DBDMA_REGS];
160 qemu_irq irq;
b42ec42d
AJ
161 DBDMA_io io;
162 DBDMA_rw rw;
862c9280 163 DBDMA_flush flush;
28ce5ce6 164 dbdma_cmd current;
b42ec42d 165 int processing;
28ce5ce6
AJ
166} DBDMA_channel;
167
c20df14b 168typedef struct {
23c5e4ca 169 MemoryRegion mem;
c20df14b
JQ
170 DBDMA_channel channels[DBDMA_CHANNELS];
171} DBDMAState;
172
28ce5ce6
AJ
173#ifdef DEBUG_DBDMA
174static void dump_dbdma_cmd(dbdma_cmd *cmd)
175{
176 printf("dbdma_cmd %p\n", cmd);
177 printf(" req_count 0x%04x\n", le16_to_cpu(cmd->req_count));
178 printf(" command 0x%04x\n", le16_to_cpu(cmd->command));
179 printf(" phy_addr 0x%08x\n", le32_to_cpu(cmd->phy_addr));
180 printf(" cmd_dep 0x%08x\n", le32_to_cpu(cmd->cmd_dep));
181 printf(" res_count 0x%04x\n", le16_to_cpu(cmd->res_count));
182 printf(" xfer_status 0x%04x\n", le16_to_cpu(cmd->xfer_status));
183}
184#else
185static void dump_dbdma_cmd(dbdma_cmd *cmd)
3cbee15b 186{
28ce5ce6
AJ
187}
188#endif
189static void dbdma_cmdptr_load(DBDMA_channel *ch)
190{
191 DBDMA_DPRINTF("dbdma_cmdptr_load 0x%08x\n",
ad674e53
AJ
192 ch->regs[DBDMA_CMDPTR_LO]);
193 cpu_physical_memory_read(ch->regs[DBDMA_CMDPTR_LO],
28ce5ce6 194 (uint8_t*)&ch->current, sizeof(dbdma_cmd));
3cbee15b
JM
195}
196
28ce5ce6 197static void dbdma_cmdptr_save(DBDMA_channel *ch)
3cbee15b 198{
28ce5ce6 199 DBDMA_DPRINTF("dbdma_cmdptr_save 0x%08x\n",
ad674e53 200 ch->regs[DBDMA_CMDPTR_LO]);
28ce5ce6
AJ
201 DBDMA_DPRINTF("xfer_status 0x%08x res_count 0x%04x\n",
202 le16_to_cpu(ch->current.xfer_status),
203 le16_to_cpu(ch->current.res_count));
ad674e53 204 cpu_physical_memory_write(ch->regs[DBDMA_CMDPTR_LO],
28ce5ce6 205 (uint8_t*)&ch->current, sizeof(dbdma_cmd));
3cbee15b
JM
206}
207
28ce5ce6 208static void kill_channel(DBDMA_channel *ch)
3cbee15b 209{
28ce5ce6
AJ
210 DBDMA_DPRINTF("kill_channel\n");
211
ad674e53
AJ
212 ch->regs[DBDMA_STATUS] |= DEAD;
213 ch->regs[DBDMA_STATUS] &= ~ACTIVE;
28ce5ce6
AJ
214
215 qemu_irq_raise(ch->irq);
216}
217
218static void conditional_interrupt(DBDMA_channel *ch)
219{
220 dbdma_cmd *current = &ch->current;
221 uint16_t intr;
222 uint16_t sel_mask, sel_value;
223 uint32_t status;
224 int cond;
225
226 DBDMA_DPRINTF("conditional_interrupt\n");
227
b42ec42d 228 intr = le16_to_cpu(current->command) & INTR_MASK;
28ce5ce6
AJ
229
230 switch(intr) {
231 case INTR_NEVER: /* don't interrupt */
232 return;
233 case INTR_ALWAYS: /* always interrupt */
234 qemu_irq_raise(ch->irq);
235 return;
236 }
237
ad674e53 238 status = ch->regs[DBDMA_STATUS] & DEVSTAT;
28ce5ce6 239
ad674e53
AJ
240 sel_mask = (ch->regs[DBDMA_INTR_SEL] >> 16) & 0x0f;
241 sel_value = ch->regs[DBDMA_INTR_SEL] & 0x0f;
28ce5ce6
AJ
242
243 cond = (status & sel_mask) == (sel_value & sel_mask);
244
245 switch(intr) {
246 case INTR_IFSET: /* intr if condition bit is 1 */
247 if (cond)
248 qemu_irq_raise(ch->irq);
249 return;
250 case INTR_IFCLR: /* intr if condition bit is 0 */
251 if (!cond)
252 qemu_irq_raise(ch->irq);
253 return;
254 }
255}
256
257static int conditional_wait(DBDMA_channel *ch)
258{
259 dbdma_cmd *current = &ch->current;
260 uint16_t wait;
261 uint16_t sel_mask, sel_value;
262 uint32_t status;
263 int cond;
264
265 DBDMA_DPRINTF("conditional_wait\n");
266
b42ec42d 267 wait = le16_to_cpu(current->command) & WAIT_MASK;
28ce5ce6
AJ
268
269 switch(wait) {
270 case WAIT_NEVER: /* don't wait */
271 return 0;
272 case WAIT_ALWAYS: /* always wait */
273 return 1;
274 }
275
ad674e53 276 status = ch->regs[DBDMA_STATUS] & DEVSTAT;
28ce5ce6 277
ad674e53
AJ
278 sel_mask = (ch->regs[DBDMA_WAIT_SEL] >> 16) & 0x0f;
279 sel_value = ch->regs[DBDMA_WAIT_SEL] & 0x0f;
28ce5ce6
AJ
280
281 cond = (status & sel_mask) == (sel_value & sel_mask);
282
283 switch(wait) {
284 case WAIT_IFSET: /* wait if condition bit is 1 */
285 if (cond)
286 return 1;
287 return 0;
288 case WAIT_IFCLR: /* wait if condition bit is 0 */
289 if (!cond)
290 return 1;
291 return 0;
292 }
293 return 0;
294}
295
296static void next(DBDMA_channel *ch)
297{
298 uint32_t cp;
299
ad674e53 300 ch->regs[DBDMA_STATUS] &= ~BT;
28ce5ce6 301
ad674e53
AJ
302 cp = ch->regs[DBDMA_CMDPTR_LO];
303 ch->regs[DBDMA_CMDPTR_LO] = cp + sizeof(dbdma_cmd);
28ce5ce6
AJ
304 dbdma_cmdptr_load(ch);
305}
306
307static void branch(DBDMA_channel *ch)
308{
309 dbdma_cmd *current = &ch->current;
310
311 ch->regs[DBDMA_CMDPTR_LO] = current->cmd_dep;
ad674e53 312 ch->regs[DBDMA_STATUS] |= BT;
28ce5ce6
AJ
313 dbdma_cmdptr_load(ch);
314}
315
316static void conditional_branch(DBDMA_channel *ch)
317{
318 dbdma_cmd *current = &ch->current;
319 uint16_t br;
320 uint16_t sel_mask, sel_value;
321 uint32_t status;
322 int cond;
323
324 DBDMA_DPRINTF("conditional_branch\n");
325
326 /* check if we must branch */
327
b42ec42d 328 br = le16_to_cpu(current->command) & BR_MASK;
28ce5ce6
AJ
329
330 switch(br) {
331 case BR_NEVER: /* don't branch */
332 next(ch);
333 return;
334 case BR_ALWAYS: /* always branch */
335 branch(ch);
336 return;
337 }
338
ad674e53 339 status = ch->regs[DBDMA_STATUS] & DEVSTAT;
28ce5ce6 340
ad674e53
AJ
341 sel_mask = (ch->regs[DBDMA_BRANCH_SEL] >> 16) & 0x0f;
342 sel_value = ch->regs[DBDMA_BRANCH_SEL] & 0x0f;
28ce5ce6
AJ
343
344 cond = (status & sel_mask) == (sel_value & sel_mask);
345
346 switch(br) {
347 case BR_IFSET: /* branch if condition bit is 1 */
348 if (cond)
349 branch(ch);
350 else
351 next(ch);
352 return;
353 case BR_IFCLR: /* branch if condition bit is 0 */
354 if (!cond)
355 branch(ch);
356 else
357 next(ch);
358 return;
359 }
360}
361
b42ec42d
AJ
362static QEMUBH *dbdma_bh;
363static void channel_run(DBDMA_channel *ch);
28ce5ce6 364
b42ec42d 365static void dbdma_end(DBDMA_io *io)
28ce5ce6
AJ
366{
367 DBDMA_channel *ch = io->channel;
368 dbdma_cmd *current = &ch->current;
369
b42ec42d
AJ
370 if (conditional_wait(ch))
371 goto wait;
28ce5ce6 372
ad674e53
AJ
373 current->xfer_status = cpu_to_le16(ch->regs[DBDMA_STATUS]);
374 current->res_count = cpu_to_le16(io->len);
b42ec42d 375 dbdma_cmdptr_save(ch);
862c9280 376 if (io->is_last)
ad674e53 377 ch->regs[DBDMA_STATUS] &= ~FLUSH;
b42ec42d
AJ
378
379 conditional_interrupt(ch);
380 conditional_branch(ch);
28ce5ce6 381
b42ec42d
AJ
382wait:
383 ch->processing = 0;
ad674e53
AJ
384 if ((ch->regs[DBDMA_STATUS] & RUN) &&
385 (ch->regs[DBDMA_STATUS] & ACTIVE))
b42ec42d 386 channel_run(ch);
28ce5ce6
AJ
387}
388
b42ec42d 389static void start_output(DBDMA_channel *ch, int key, uint32_t addr,
28ce5ce6
AJ
390 uint16_t req_count, int is_last)
391{
28ce5ce6
AJ
392 DBDMA_DPRINTF("start_output\n");
393
394 /* KEY_REGS, KEY_DEVICE and KEY_STREAM
395 * are not implemented in the mac-io chip
396 */
397
398 DBDMA_DPRINTF("addr 0x%x key 0x%x\n", addr, key);
399 if (!addr || key > KEY_STREAM3) {
400 kill_channel(ch);
b42ec42d 401 return;
28ce5ce6
AJ
402 }
403
b42ec42d 404 ch->io.addr = addr;
28ce5ce6
AJ
405 ch->io.len = req_count;
406 ch->io.is_last = is_last;
b42ec42d
AJ
407 ch->io.dma_end = dbdma_end;
408 ch->io.is_dma_out = 1;
409 ch->processing = 1;
a9ceb76d
AG
410 if (ch->rw) {
411 ch->rw(&ch->io);
412 }
28ce5ce6
AJ
413}
414
b42ec42d 415static void start_input(DBDMA_channel *ch, int key, uint32_t addr,
28ce5ce6
AJ
416 uint16_t req_count, int is_last)
417{
28ce5ce6
AJ
418 DBDMA_DPRINTF("start_input\n");
419
420 /* KEY_REGS, KEY_DEVICE and KEY_STREAM
421 * are not implemented in the mac-io chip
422 */
423
424 if (!addr || key > KEY_STREAM3) {
425 kill_channel(ch);
b42ec42d 426 return;
28ce5ce6
AJ
427 }
428
b42ec42d 429 ch->io.addr = addr;
28ce5ce6
AJ
430 ch->io.len = req_count;
431 ch->io.is_last = is_last;
b42ec42d
AJ
432 ch->io.dma_end = dbdma_end;
433 ch->io.is_dma_out = 0;
434 ch->processing = 1;
a9ceb76d
AG
435 if (ch->rw) {
436 ch->rw(&ch->io);
437 }
28ce5ce6
AJ
438}
439
b42ec42d 440static void load_word(DBDMA_channel *ch, int key, uint32_t addr,
28ce5ce6
AJ
441 uint16_t len)
442{
443 dbdma_cmd *current = &ch->current;
444 uint32_t val;
445
446 DBDMA_DPRINTF("load_word\n");
447
448 /* only implements KEY_SYSTEM */
449
450 if (key != KEY_SYSTEM) {
451 printf("DBDMA: LOAD_WORD, unimplemented key %x\n", key);
452 kill_channel(ch);
b42ec42d 453 return;
28ce5ce6
AJ
454 }
455
456 cpu_physical_memory_read(addr, (uint8_t*)&val, len);
457
458 if (len == 2)
459 val = (val << 16) | (current->cmd_dep & 0x0000ffff);
460 else if (len == 1)
461 val = (val << 24) | (current->cmd_dep & 0x00ffffff);
462
463 current->cmd_dep = val;
464
465 if (conditional_wait(ch))
b42ec42d 466 goto wait;
28ce5ce6 467
ad674e53 468 current->xfer_status = cpu_to_le16(ch->regs[DBDMA_STATUS]);
28ce5ce6 469 dbdma_cmdptr_save(ch);
ad674e53 470 ch->regs[DBDMA_STATUS] &= ~FLUSH;
28ce5ce6
AJ
471
472 conditional_interrupt(ch);
473 next(ch);
474
b42ec42d
AJ
475wait:
476 qemu_bh_schedule(dbdma_bh);
28ce5ce6
AJ
477}
478
b42ec42d 479static void store_word(DBDMA_channel *ch, int key, uint32_t addr,
28ce5ce6
AJ
480 uint16_t len)
481{
482 dbdma_cmd *current = &ch->current;
483 uint32_t val;
484
485 DBDMA_DPRINTF("store_word\n");
486
487 /* only implements KEY_SYSTEM */
488
489 if (key != KEY_SYSTEM) {
490 printf("DBDMA: STORE_WORD, unimplemented key %x\n", key);
491 kill_channel(ch);
b42ec42d 492 return;
28ce5ce6
AJ
493 }
494
495 val = current->cmd_dep;
496 if (len == 2)
497 val >>= 16;
498 else if (len == 1)
499 val >>= 24;
500
501 cpu_physical_memory_write(addr, (uint8_t*)&val, len);
502
503 if (conditional_wait(ch))
b42ec42d 504 goto wait;
28ce5ce6 505
ad674e53 506 current->xfer_status = cpu_to_le16(ch->regs[DBDMA_STATUS]);
28ce5ce6 507 dbdma_cmdptr_save(ch);
ad674e53 508 ch->regs[DBDMA_STATUS] &= ~FLUSH;
28ce5ce6
AJ
509
510 conditional_interrupt(ch);
511 next(ch);
512
b42ec42d
AJ
513wait:
514 qemu_bh_schedule(dbdma_bh);
28ce5ce6
AJ
515}
516
b42ec42d 517static void nop(DBDMA_channel *ch)
28ce5ce6
AJ
518{
519 dbdma_cmd *current = &ch->current;
520
521 if (conditional_wait(ch))
b42ec42d 522 goto wait;
28ce5ce6 523
ad674e53 524 current->xfer_status = cpu_to_le16(ch->regs[DBDMA_STATUS]);
28ce5ce6
AJ
525 dbdma_cmdptr_save(ch);
526
527 conditional_interrupt(ch);
528 conditional_branch(ch);
529
b42ec42d
AJ
530wait:
531 qemu_bh_schedule(dbdma_bh);
3cbee15b
JM
532}
533
b42ec42d 534static void stop(DBDMA_channel *ch)
3cbee15b 535{
ad674e53 536 ch->regs[DBDMA_STATUS] &= ~(ACTIVE|DEAD|FLUSH);
28ce5ce6
AJ
537
538 /* the stop command does not increment command pointer */
3cbee15b
JM
539}
540
b42ec42d 541static void channel_run(DBDMA_channel *ch)
3cbee15b 542{
28ce5ce6
AJ
543 dbdma_cmd *current = &ch->current;
544 uint16_t cmd, key;
545 uint16_t req_count;
546 uint32_t phy_addr;
547
548 DBDMA_DPRINTF("channel_run\n");
549 dump_dbdma_cmd(current);
550
551 /* clear WAKE flag at command fetch */
552
ad674e53 553 ch->regs[DBDMA_STATUS] &= ~WAKE;
28ce5ce6
AJ
554
555 cmd = le16_to_cpu(current->command) & COMMAND_MASK;
556
557 switch (cmd) {
558 case DBDMA_NOP:
b42ec42d
AJ
559 nop(ch);
560 return;
28ce5ce6
AJ
561
562 case DBDMA_STOP:
b42ec42d
AJ
563 stop(ch);
564 return;
28ce5ce6
AJ
565 }
566
567 key = le16_to_cpu(current->command) & 0x0700;
568 req_count = le16_to_cpu(current->req_count);
569 phy_addr = le32_to_cpu(current->phy_addr);
570
571 if (key == KEY_STREAM4) {
572 printf("command %x, invalid key 4\n", cmd);
573 kill_channel(ch);
b42ec42d 574 return;
28ce5ce6
AJ
575 }
576
577 switch (cmd) {
578 case OUTPUT_MORE:
b42ec42d
AJ
579 start_output(ch, key, phy_addr, req_count, 0);
580 return;
28ce5ce6
AJ
581
582 case OUTPUT_LAST:
b42ec42d
AJ
583 start_output(ch, key, phy_addr, req_count, 1);
584 return;
28ce5ce6
AJ
585
586 case INPUT_MORE:
b42ec42d
AJ
587 start_input(ch, key, phy_addr, req_count, 0);
588 return;
28ce5ce6
AJ
589
590 case INPUT_LAST:
b42ec42d
AJ
591 start_input(ch, key, phy_addr, req_count, 1);
592 return;
28ce5ce6
AJ
593 }
594
595 if (key < KEY_REGS) {
596 printf("command %x, invalid key %x\n", cmd, key);
597 key = KEY_SYSTEM;
598 }
599
600 /* for LOAD_WORD and STORE_WORD, req_count is on 3 bits
601 * and BRANCH is invalid
602 */
603
604 req_count = req_count & 0x0007;
605 if (req_count & 0x4) {
606 req_count = 4;
607 phy_addr &= ~3;
608 } else if (req_count & 0x2) {
609 req_count = 2;
610 phy_addr &= ~1;
611 } else
612 req_count = 1;
613
614 switch (cmd) {
615 case LOAD_WORD:
b42ec42d
AJ
616 load_word(ch, key, phy_addr, req_count);
617 return;
28ce5ce6
AJ
618
619 case STORE_WORD:
b42ec42d
AJ
620 store_word(ch, key, phy_addr, req_count);
621 return;
28ce5ce6 622 }
3cbee15b
JM
623}
624
c20df14b 625static void DBDMA_run(DBDMAState *s)
28ce5ce6
AJ
626{
627 int channel;
28ce5ce6 628
c20df14b
JQ
629 for (channel = 0; channel < DBDMA_CHANNELS; channel++) {
630 DBDMA_channel *ch = &s->channels[channel];
631 uint32_t status = ch->regs[DBDMA_STATUS];
632 if (!ch->processing && (status & RUN) && (status & ACTIVE)) {
633 channel_run(ch);
634 }
28ce5ce6 635 }
28ce5ce6
AJ
636}
637
638static void DBDMA_run_bh(void *opaque)
639{
c20df14b 640 DBDMAState *s = opaque;
28ce5ce6
AJ
641
642 DBDMA_DPRINTF("DBDMA_run_bh\n");
643
c20df14b 644 DBDMA_run(s);
28ce5ce6
AJ
645}
646
647void DBDMA_register_channel(void *dbdma, int nchan, qemu_irq irq,
862c9280 648 DBDMA_rw rw, DBDMA_flush flush,
28ce5ce6
AJ
649 void *opaque)
650{
c20df14b
JQ
651 DBDMAState *s = dbdma;
652 DBDMA_channel *ch = &s->channels[nchan];
28ce5ce6
AJ
653
654 DBDMA_DPRINTF("DBDMA_register_channel 0x%x\n", nchan);
655
656 ch->irq = irq;
657 ch->channel = nchan;
b42ec42d 658 ch->rw = rw;
862c9280 659 ch->flush = flush;
28ce5ce6
AJ
660 ch->io.opaque = opaque;
661 ch->io.channel = ch;
662}
663
664void DBDMA_schedule(void)
665{
d9f75a4e 666 qemu_notify_event();
28ce5ce6
AJ
667}
668
669static void
670dbdma_control_write(DBDMA_channel *ch)
671{
672 uint16_t mask, value;
673 uint32_t status;
674
ad674e53
AJ
675 mask = (ch->regs[DBDMA_CONTROL] >> 16) & 0xffff;
676 value = ch->regs[DBDMA_CONTROL] & 0xffff;
28ce5ce6
AJ
677
678 value &= (RUN | PAUSE | FLUSH | WAKE | DEVSTAT);
679
ad674e53 680 status = ch->regs[DBDMA_STATUS];
28ce5ce6
AJ
681
682 status = (value & mask) | (status & ~mask);
683
684 if (status & WAKE)
685 status |= ACTIVE;
686 if (status & RUN) {
687 status |= ACTIVE;
688 status &= ~DEAD;
689 }
690 if (status & PAUSE)
691 status &= ~ACTIVE;
ad674e53 692 if ((ch->regs[DBDMA_STATUS] & RUN) && !(status & RUN)) {
28ce5ce6
AJ
693 /* RUN is cleared */
694 status &= ~(ACTIVE|DEAD);
695 }
696
697 DBDMA_DPRINTF(" status 0x%08x\n", status);
698
ad674e53 699 ch->regs[DBDMA_STATUS] = status;
28ce5ce6 700
b42ec42d
AJ
701 if (status & ACTIVE)
702 qemu_bh_schedule(dbdma_bh);
a9ceb76d 703 if ((status & FLUSH) && ch->flush)
862c9280 704 ch->flush(&ch->io);
28ce5ce6
AJ
705}
706
23c5e4ca
AK
707static void dbdma_write(void *opaque, target_phys_addr_t addr,
708 uint64_t value, unsigned size)
28ce5ce6
AJ
709{
710 int channel = addr >> DBDMA_CHANNEL_SHIFT;
c20df14b
JQ
711 DBDMAState *s = opaque;
712 DBDMA_channel *ch = &s->channels[channel];
28ce5ce6
AJ
713 int reg = (addr - (channel << DBDMA_CHANNEL_SHIFT)) >> 2;
714
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);
718
719 /* cmdptr cannot be modified if channel is RUN or ACTIVE */
720
721 if (reg == DBDMA_CMDPTR_LO &&
ad674e53 722 (ch->regs[DBDMA_STATUS] & (RUN | ACTIVE)))
28ce5ce6
AJ
723 return;
724
725 ch->regs[reg] = value;
726
727 switch(reg) {
728 case DBDMA_CONTROL:
729 dbdma_control_write(ch);
730 break;
731 case DBDMA_CMDPTR_LO:
732 /* 16-byte aligned */
ad674e53 733 ch->regs[DBDMA_CMDPTR_LO] &= ~0xf;
28ce5ce6
AJ
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;
754 }
755}
756
23c5e4ca
AK
757static uint64_t dbdma_read(void *opaque, target_phys_addr_t addr,
758 unsigned size)
3cbee15b 759{
28ce5ce6
AJ
760 uint32_t value;
761 int channel = addr >> DBDMA_CHANNEL_SHIFT;
c20df14b
JQ
762 DBDMAState *s = opaque;
763 DBDMA_channel *ch = &s->channels[channel];
28ce5ce6 764 int reg = (addr - (channel << DBDMA_CHANNEL_SHIFT)) >> 2;
ea026b2f 765
28ce5ce6
AJ
766 value = ch->regs[reg];
767
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);
771
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;
798 }
799
800 return value;
3cbee15b
JM
801}
802
23c5e4ca
AK
803static 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,
810 },
3cbee15b
JM
811};
812
da26fdc3
JQ
813static 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()
821 }
822};
28ce5ce6 823
da26fdc3
JQ
824static 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()
833 }
834};
9b64997f 835
6e6b7363
BS
836static void dbdma_reset(void *opaque)
837{
c20df14b 838 DBDMAState *s = opaque;
28ce5ce6
AJ
839 int i;
840
841 for (i = 0; i < DBDMA_CHANNELS; i++)
c20df14b 842 memset(s->channels[i].regs, 0, DBDMA_SIZE);
6e6b7363
BS
843}
844
23c5e4ca 845void* DBDMA_init (MemoryRegion **dbdma_mem)
3cbee15b 846{
c20df14b 847 DBDMAState *s;
28ce5ce6 848
7267c094 849 s = g_malloc0(sizeof(DBDMAState));
28ce5ce6 850
23c5e4ca
AK
851 memory_region_init_io(&s->mem, &dbdma_ops, s, "dbdma", 0x1000);
852 *dbdma_mem = &s->mem;
da26fdc3 853 vmstate_register(NULL, -1, &vmstate_dbdma, s);
a08d4367 854 qemu_register_reset(dbdma_reset, s);
28ce5ce6
AJ
855
856 dbdma_bh = qemu_bh_new(DBDMA_run_bh, s);
857
858 return s;
3cbee15b 859}