2 * QEMU Sparc32 DMA controller emulation
4 * Copyright (c) 2006 Fabrice Bellard
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
30 * This is the DMA controller part of chip STP2000 (Master I/O), also
31 * produced as NCR89C100. See
32 * http://www.ibiblio.org/pub/historic-linux/early-ports/Sparc/NCR/NCR89C100.txt
34 * http://www.ibiblio.org/pub/historic-linux/early-ports/Sparc/NCR/DMA2.txt
38 #define DPRINTF(fmt, args...) \
39 do { printf("DMA: " fmt , ##args); } while (0)
40 #define pic_set_irq_new(ctl, irq, level) \
41 do { printf("DMA: set_irq(%d): %d\n", (irq), (level)); \
42 pic_set_irq_new((ctl), (irq),(level));} while (0)
44 #define DPRINTF(fmt, args...)
48 #define DMA_MAXADDR (DMA_REGS * 4 - 1)
50 #define DMA_VER 0xa0000000
52 #define DMA_INTREN 0x10
53 #define DMA_WRITE_MEM 0x100
54 #define DMA_LOADED 0x04000000
55 #define DMA_RESET 0x80
57 typedef struct DMAState DMAState;
60 uint32_t dmaregs[DMA_REGS];
62 void *iommu, *esp_opaque, *lance_opaque, *intctl;
65 void ledma_set_irq(void *opaque, int isr)
69 pic_set_irq_new(s->intctl, s->leirq, isr);
72 void ledma_memory_read(void *opaque, target_phys_addr_t addr, uint8_t *buf, int len)
76 DPRINTF("DMA write, direction: %c, addr 0x%8.8x\n",
77 s->dmaregs[0] & DMA_WRITE_MEM ? 'w': 'r', s->dmaregs[1]);
78 sparc_iommu_memory_read(s->iommu, addr | s->dmaregs[7], buf, len);
81 void ledma_memory_write(void *opaque, target_phys_addr_t addr, uint8_t *buf, int len)
85 DPRINTF("DMA read, direction: %c, addr 0x%8.8x\n",
86 s->dmaregs[0] & DMA_WRITE_MEM ? 'w': 'r', s->dmaregs[1]);
87 sparc_iommu_memory_write(s->iommu, addr | s->dmaregs[7], buf, len);
90 void espdma_raise_irq(void *opaque)
94 s->dmaregs[0] |= DMA_INTR;
95 pic_set_irq_new(s->intctl, s->espirq, 1);
98 void espdma_clear_irq(void *opaque)
100 DMAState *s = opaque;
102 s->dmaregs[0] &= ~DMA_INTR;
103 pic_set_irq_new(s->intctl, s->espirq, 0);
106 void espdma_memory_read(void *opaque, uint8_t *buf, int len)
108 DMAState *s = opaque;
110 DPRINTF("DMA read, direction: %c, addr 0x%8.8x\n",
111 s->dmaregs[0] & DMA_WRITE_MEM ? 'w': 'r', s->dmaregs[1]);
112 sparc_iommu_memory_read(s->iommu, s->dmaregs[1], buf, len);
113 s->dmaregs[0] |= DMA_INTR;
114 s->dmaregs[1] += len;
117 void espdma_memory_write(void *opaque, uint8_t *buf, int len)
119 DMAState *s = opaque;
121 DPRINTF("DMA write, direction: %c, addr 0x%8.8x\n",
122 s->dmaregs[0] & DMA_WRITE_MEM ? 'w': 'r', s->dmaregs[1]);
123 sparc_iommu_memory_write(s->iommu, s->dmaregs[1], buf, len);
124 s->dmaregs[0] |= DMA_INTR;
125 s->dmaregs[1] += len;
128 static uint32_t dma_mem_readl(void *opaque, target_phys_addr_t addr)
130 DMAState *s = opaque;
133 saddr = (addr & DMA_MAXADDR) >> 2;
134 DPRINTF("read dmareg[%d]: 0x%8.8x\n", saddr, s->dmaregs[saddr]);
136 return s->dmaregs[saddr];
139 static void dma_mem_writel(void *opaque, target_phys_addr_t addr, uint32_t val)
141 DMAState *s = opaque;
144 saddr = (addr & DMA_MAXADDR) >> 2;
145 DPRINTF("write dmareg[%d]: 0x%8.8x -> 0x%8.8x\n", saddr, s->dmaregs[saddr], val);
148 if (!(val & DMA_INTREN))
149 pic_set_irq_new(s->intctl, s->espirq, 0);
150 if (val & DMA_RESET) {
151 esp_reset(s->esp_opaque);
152 } else if (val & 0x40) {
160 s->dmaregs[0] |= DMA_LOADED;
163 if (!(val & DMA_INTREN))
164 pic_set_irq_new(s->intctl, s->leirq, 0);
166 pcnet_h_reset(s->lance_opaque);
173 s->dmaregs[saddr] = val;
176 static CPUReadMemoryFunc *dma_mem_read[3] = {
182 static CPUWriteMemoryFunc *dma_mem_write[3] = {
188 static void dma_reset(void *opaque)
190 DMAState *s = opaque;
192 memset(s->dmaregs, 0, DMA_REGS * 4);
193 s->dmaregs[0] = DMA_VER;
194 s->dmaregs[4] = DMA_VER;
197 static void dma_save(QEMUFile *f, void *opaque)
199 DMAState *s = opaque;
202 for (i = 0; i < DMA_REGS; i++)
203 qemu_put_be32s(f, &s->dmaregs[i]);
206 static int dma_load(QEMUFile *f, void *opaque, int version_id)
208 DMAState *s = opaque;
213 for (i = 0; i < DMA_REGS; i++)
214 qemu_get_be32s(f, &s->dmaregs[i]);
219 void *sparc32_dma_init(uint32_t daddr, int espirq, int leirq, void *iommu, void *intctl)
224 s = qemu_mallocz(sizeof(DMAState));
233 dma_io_memory = cpu_register_io_memory(0, dma_mem_read, dma_mem_write, s);
234 cpu_register_physical_memory(daddr, 16 * 2, dma_io_memory);
236 register_savevm("sparc32_dma", daddr, 1, dma_save, dma_load, s);
237 qemu_register_reset(dma_reset, s);
242 void sparc32_dma_set_reset_data(void *opaque, void *esp_opaque,
245 DMAState *s = opaque;
247 s->esp_opaque = esp_opaque;
248 s->lance_opaque = lance_opaque;