Separate the DMA controllers - Convert ESP to new DMA methods (Blue Swirl)
[qemu] / hw / sparc32_dma.c
1 /*
2  * QEMU Sparc32 DMA controller emulation
3  *
4  * Copyright (c) 2006 Fabrice Bellard
5  *
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:
12  *
13  * The above copyright notice and this permission notice shall be included in
14  * all copies or substantial portions of the Software.
15  *
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
22  * THE SOFTWARE.
23  */
24 #include "vl.h"
25
26 /* debug DMA */
27 //#define DEBUG_DMA
28
29 /*
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
33  * and
34  * http://www.ibiblio.org/pub/historic-linux/early-ports/Sparc/NCR/DMA2.txt
35  */
36
37 #ifdef DEBUG_DMA
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)
43 #else
44 #define DPRINTF(fmt, args...)
45 #endif
46
47 #define DMA_REGS 8
48 #define DMA_MAXADDR (DMA_REGS * 4 - 1)
49
50 #define DMA_VER 0xa0000000
51 #define DMA_INTR 1
52 #define DMA_INTREN 0x10
53 #define DMA_WRITE_MEM 0x100
54 #define DMA_LOADED 0x04000000
55 #define DMA_RESET 0x80
56
57 typedef struct DMAState DMAState;
58
59 struct DMAState {
60     uint32_t dmaregs[DMA_REGS];
61     int espirq, leirq;
62     void *iommu, *esp_opaque, *lance_opaque, *intctl;
63 };
64
65 void ledma_set_irq(void *opaque, int isr)
66 {
67     DMAState *s = opaque;
68
69     pic_set_irq_new(s->intctl, s->leirq, isr);
70 }
71
72 void ledma_memory_read(void *opaque, target_phys_addr_t addr, uint8_t *buf, int len)
73 {
74     DMAState *s = opaque;
75
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);
79 }
80
81 void ledma_memory_write(void *opaque, target_phys_addr_t addr, uint8_t *buf, int len)
82 {
83     DMAState *s = opaque;
84
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);
88 }
89
90 void espdma_raise_irq(void *opaque)
91 {
92     DMAState *s = opaque;
93
94     s->dmaregs[0] |= DMA_INTR;
95     pic_set_irq_new(s->intctl, s->espirq, 1);
96 }
97
98 void espdma_clear_irq(void *opaque)
99 {
100     DMAState *s = opaque;
101
102     s->dmaregs[0] &= ~DMA_INTR;
103     pic_set_irq_new(s->intctl, s->espirq, 0);
104 }
105
106 void espdma_memory_read(void *opaque, uint8_t *buf, int len)
107 {
108     DMAState *s = opaque;
109
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;
115 }
116
117 void espdma_memory_write(void *opaque, uint8_t *buf, int len)
118 {
119     DMAState *s = opaque;
120
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;
126 }
127
128 static uint32_t dma_mem_readl(void *opaque, target_phys_addr_t addr)
129 {
130     DMAState *s = opaque;
131     uint32_t saddr;
132
133     saddr = (addr & DMA_MAXADDR) >> 2;
134     DPRINTF("read dmareg[%d]: 0x%8.8x\n", saddr, s->dmaregs[saddr]);
135
136     return s->dmaregs[saddr];
137 }
138
139 static void dma_mem_writel(void *opaque, target_phys_addr_t addr, uint32_t val)
140 {
141     DMAState *s = opaque;
142     uint32_t saddr;
143
144     saddr = (addr & DMA_MAXADDR) >> 2;
145     DPRINTF("write dmareg[%d]: 0x%8.8x -> 0x%8.8x\n", saddr, s->dmaregs[saddr], val);
146     switch (saddr) {
147     case 0:
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) {
153             val &= ~0x40;
154         } else if (val == 0)
155             val = 0x40;
156         val &= 0x0fffffff;
157         val |= DMA_VER;
158         break;
159     case 1:
160         s->dmaregs[0] |= DMA_LOADED;
161         break;
162     case 4:
163         if (!(val & DMA_INTREN))
164             pic_set_irq_new(s->intctl, s->leirq, 0);
165         if (val & DMA_RESET)
166             pcnet_h_reset(s->lance_opaque);
167         val &= 0x0fffffff;
168         val |= DMA_VER;
169         break;
170     default:
171         break;
172     }
173     s->dmaregs[saddr] = val;
174 }
175
176 static CPUReadMemoryFunc *dma_mem_read[3] = {
177     dma_mem_readl,
178     dma_mem_readl,
179     dma_mem_readl,
180 };
181
182 static CPUWriteMemoryFunc *dma_mem_write[3] = {
183     dma_mem_writel,
184     dma_mem_writel,
185     dma_mem_writel,
186 };
187
188 static void dma_reset(void *opaque)
189 {
190     DMAState *s = opaque;
191
192     memset(s->dmaregs, 0, DMA_REGS * 4);
193     s->dmaregs[0] = DMA_VER;
194     s->dmaregs[4] = DMA_VER;
195 }
196
197 static void dma_save(QEMUFile *f, void *opaque)
198 {
199     DMAState *s = opaque;
200     unsigned int i;
201
202     for (i = 0; i < DMA_REGS; i++)
203         qemu_put_be32s(f, &s->dmaregs[i]);
204 }
205
206 static int dma_load(QEMUFile *f, void *opaque, int version_id)
207 {
208     DMAState *s = opaque;
209     unsigned int i;
210
211     if (version_id != 1)
212         return -EINVAL;
213     for (i = 0; i < DMA_REGS; i++)
214         qemu_get_be32s(f, &s->dmaregs[i]);
215
216     return 0;
217 }
218
219 void *sparc32_dma_init(uint32_t daddr, int espirq, int leirq, void *iommu, void *intctl)
220 {
221     DMAState *s;
222     int dma_io_memory;
223
224     s = qemu_mallocz(sizeof(DMAState));
225     if (!s)
226         return NULL;
227
228     s->espirq = espirq;
229     s->leirq = leirq;
230     s->iommu = iommu;
231     s->intctl = intctl;
232
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);
235
236     register_savevm("sparc32_dma", daddr, 1, dma_save, dma_load, s);
237     qemu_register_reset(dma_reset, s);
238
239     return s;
240 }
241
242 void sparc32_dma_set_reset_data(void *opaque, void *esp_opaque,
243                                 void *lance_opaque)
244 {
245     DMAState *s = opaque;
246
247     s->esp_opaque = esp_opaque;
248     s->lance_opaque = lance_opaque;
249 }