4 * Copyright (c) 2003 Vassili Karpov (malc)
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
28 #define log(...) fprintf (stderr, "dma: " __VA_ARGS__)
30 #define lwarn(...) fprintf (stderr, "dma: " __VA_ARGS__)
31 #define linfo(...) fprintf (stderr, "dma: " __VA_ARGS__)
32 #define ldebug(...) fprintf (stderr, "dma: " __VA_ARGS__)
39 #define LENOFA(a) ((int) (sizeof(a)/sizeof(a[0])))
49 DMA_transfer_handler transfer_handler;
56 static struct dma_cont {
62 struct dma_regs regs[4];
66 CMD_MEMORY_TO_MEMORY = 0x01,
67 CMD_FIXED_ADDRESS = 0x02,
68 CMD_BLOCK_CONTROLLER = 0x04,
69 CMD_COMPRESSED_TIME = 0x08,
70 CMD_CYCLIC_PRIORITY = 0x10,
71 CMD_EXTENDED_WRITE = 0x20,
74 CMD_NOT_SUPPORTED = CMD_MEMORY_TO_MEMORY | CMD_FIXED_ADDRESS
75 | CMD_COMPRESSED_TIME | CMD_CYCLIC_PRIORITY | CMD_EXTENDED_WRITE
76 | CMD_LOW_DREQ | CMD_LOW_DACK
80 static int channels[8] = {-1, 2, 3, 1, -1, -1, -1, 0};
82 static void write_page (void *opaque, uint32_t nport, uint32_t data)
84 struct dma_cont *d = opaque;
87 ichan = channels[nport & 7];
89 log ("invalid channel %#x %#x\n", nport, data);
92 d->regs[ichan].page = data;
95 static void write_pageh (void *opaque, uint32_t nport, uint32_t data)
97 struct dma_cont *d = opaque;
100 ichan = channels[nport & 7];
102 log ("invalid channel %#x %#x\n", nport, data);
105 d->regs[ichan].pageh = data;
108 static uint32_t read_page (void *opaque, uint32_t nport)
110 struct dma_cont *d = opaque;
113 ichan = channels[nport & 7];
115 log ("invalid channel read %#x\n", nport);
118 return d->regs[ichan].page;
121 static uint32_t read_pageh (void *opaque, uint32_t nport)
123 struct dma_cont *d = opaque;
126 ichan = channels[nport & 7];
128 log ("invalid channel read %#x\n", nport);
131 return d->regs[ichan].pageh;
134 static inline void init_chan (struct dma_cont *d, int ichan)
139 r->now[ADDR] = r->base[0] << d->dshift;
143 static inline int getff (struct dma_cont *d)
152 static uint32_t read_chan (void *opaque, uint32_t nport)
154 struct dma_cont *d = opaque;
155 int ichan, nreg, iport, ff, val;
158 iport = (nport >> d->dshift) & 0x0f;
165 val = (r->base[COUNT] << d->dshift) - r->now[COUNT];
167 val = r->now[ADDR] + r->now[COUNT];
169 return (val >> (d->dshift + (ff << 3))) & 0xff;
172 static void write_chan (void *opaque, uint32_t nport, uint32_t data)
174 struct dma_cont *d = opaque;
175 int iport, ichan, nreg;
178 iport = (nport >> d->dshift) & 0x0f;
183 r->base[nreg] = (r->base[nreg] & 0xff) | ((data << 8) & 0xff00);
184 init_chan (d, ichan);
186 r->base[nreg] = (r->base[nreg] & 0xff00) | (data & 0xff);
190 static void write_cont (void *opaque, uint32_t nport, uint32_t data)
192 struct dma_cont *d = opaque;
195 iport = (nport >> d->dshift) & 0x0f;
197 case 8: /* command */
198 if ((data != 0) && (data & CMD_NOT_SUPPORTED)) {
199 log ("command %#x not supported\n", data);
208 d->status |= 1 << (ichan + 4);
211 d->status &= ~(1 << (ichan + 4));
213 d->status &= ~(1 << ichan);
216 case 0xa: /* single mask */
218 d->mask |= 1 << (data & 3);
220 d->mask &= ~(1 << (data & 3));
232 op = (data >> 2) & 3;
233 ai = (data >> 4) & 1;
234 dir = (data >> 5) & 1;
235 opmode = (data >> 6) & 3;
237 linfo ("ichan %d, op %d, ai %d, dir %d, opmode %d\n",
238 ichan, op, ai, dir, opmode);
241 d->regs[ichan].mode = data;
245 case 0xc: /* clear flip flop */
249 case 0xd: /* reset */
256 case 0xe: /* clear mask for all channels */
260 case 0xf: /* write mask for all channels */
265 log ("dma: unknown iport %#x\n", iport);
271 linfo ("nport %#06x, ichan % 2d, val %#06x\n",
277 static uint32_t read_cont (void *opaque, uint32_t nport)
279 struct dma_cont *d = opaque;
282 iport = (nport >> d->dshift) & 0x0f;
284 case 0x08: /* status */
288 case 0x0f: /* mask */
298 int DMA_get_channel_mode (int nchan)
300 return dma_controllers[nchan > 3].regs[nchan & 3].mode;
303 void DMA_hold_DREQ (int nchan)
309 linfo ("held cont=%d chan=%d\n", ncont, ichan);
310 dma_controllers[ncont].status |= 1 << (ichan + 4);
313 void DMA_release_DREQ (int nchan)
319 linfo ("released cont=%d chan=%d\n", ncont, ichan);
320 dma_controllers[ncont].status &= ~(1 << (ichan + 4));
323 static void channel_run (int ncont, int ichan)
330 r = dma_controllers[ncont].regs + ichan;
331 /* ai = r->mode & 16; */
332 /* dir = r->mode & 32 ? -1 : 1; */
334 /* NOTE: pageh is only used by PPC PREP */
335 addr = ((r->pageh & 0x7f) << 24) | (r->page << 16) | r->now[ADDR];
336 n = r->transfer_handler (r->opaque, addr,
337 (r->base[COUNT] << ncont) + (1 << ncont));
340 ldebug ("dma_pos %d size %d\n",
341 n, (r->base[1] << ncont) + (1 << ncont));
351 for (icont = 0; icont < 2; icont++, d++) {
352 for (ichan = 0; ichan < 4; ichan++) {
357 if ((0 == (d->mask & mask)) && (0 != (d->status & (mask << 4))))
358 channel_run (icont, ichan);
363 void DMA_register_channel (int nchan,
364 DMA_transfer_handler transfer_handler,
373 r = dma_controllers[ncont].regs + ichan;
374 r->transfer_handler = transfer_handler;
378 /* request the emulator to transfer a new DMA memory block ASAP */
379 void DMA_schedule(int nchan)
381 cpu_interrupt(cpu_single_env, CPU_INTERRUPT_EXIT);
384 static void dma_reset(void *opaque)
386 struct dma_cont *d = opaque;
387 write_cont (d, (0x0d << d->dshift), 0);
390 /* dshift = 0: 8 bit DMA, 1 = 16 bit DMA */
391 static void dma_init2(struct dma_cont *d, int base, int dshift,
392 int page_base, int pageh_base)
394 const static int page_port_list[] = { 0x1, 0x2, 0x3, 0x7 };
398 for (i = 0; i < 8; i++) {
399 register_ioport_write (base + (i << dshift), 1, 1, write_chan, d);
400 register_ioport_read (base + (i << dshift), 1, 1, read_chan, d);
402 for (i = 0; i < LENOFA (page_port_list); i++) {
403 register_ioport_write (page_base + page_port_list[i], 1, 1,
405 register_ioport_read (page_base + page_port_list[i], 1, 1,
407 if (pageh_base >= 0) {
408 register_ioport_write (pageh_base + page_port_list[i], 1, 1,
410 register_ioport_read (pageh_base + page_port_list[i], 1, 1,
414 for (i = 0; i < 8; i++) {
415 register_ioport_write (base + ((i + 8) << dshift), 1, 1,
417 register_ioport_read (base + ((i + 8) << dshift), 1, 1,
420 qemu_register_reset(dma_reset, d);
424 void DMA_init (int high_page_enable)
426 dma_init2(&dma_controllers[0], 0x00, 0, 0x80,
427 high_page_enable ? 0x480 : -1);
428 dma_init2(&dma_controllers[1], 0xc0, 1, 0x88,
429 high_page_enable ? 0x488 : -1);