root / hw / sparc32_dma.c @ 2be24aaa
History | View | Annotate | Download (7.8 kB)
1 | 67e999be | bellard | /*
|
---|---|---|---|
2 | 67e999be | bellard | * QEMU Sparc32 DMA controller emulation
|
3 | 67e999be | bellard | *
|
4 | 67e999be | bellard | * Copyright (c) 2006 Fabrice Bellard
|
5 | 67e999be | bellard | *
|
6 | 6f57bbf4 | Artyom Tarasenko | * Modifications:
|
7 | 6f57bbf4 | Artyom Tarasenko | * 2010-Feb-14 Artyom Tarasenko : reworked irq generation
|
8 | 6f57bbf4 | Artyom Tarasenko | *
|
9 | 67e999be | bellard | * Permission is hereby granted, free of charge, to any person obtaining a copy
|
10 | 67e999be | bellard | * of this software and associated documentation files (the "Software"), to deal
|
11 | 67e999be | bellard | * in the Software without restriction, including without limitation the rights
|
12 | 67e999be | bellard | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
13 | 67e999be | bellard | * copies of the Software, and to permit persons to whom the Software is
|
14 | 67e999be | bellard | * furnished to do so, subject to the following conditions:
|
15 | 67e999be | bellard | *
|
16 | 67e999be | bellard | * The above copyright notice and this permission notice shall be included in
|
17 | 67e999be | bellard | * all copies or substantial portions of the Software.
|
18 | 67e999be | bellard | *
|
19 | 67e999be | bellard | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
20 | 67e999be | bellard | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
21 | 67e999be | bellard | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
|
22 | 67e999be | bellard | * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
23 | 67e999be | bellard | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
24 | 67e999be | bellard | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
25 | 67e999be | bellard | * THE SOFTWARE.
|
26 | 67e999be | bellard | */
|
27 | 6f6260c7 | Blue Swirl | |
28 | 87ecb68b | pbrook | #include "hw.h" |
29 | 87ecb68b | pbrook | #include "sparc32_dma.h" |
30 | 87ecb68b | pbrook | #include "sun4m.h" |
31 | 6f6260c7 | Blue Swirl | #include "sysbus.h" |
32 | 67e999be | bellard | |
33 | 67e999be | bellard | /* debug DMA */
|
34 | 67e999be | bellard | //#define DEBUG_DMA
|
35 | 67e999be | bellard | |
36 | 67e999be | bellard | /*
|
37 | 67e999be | bellard | * This is the DMA controller part of chip STP2000 (Master I/O), also
|
38 | 67e999be | bellard | * produced as NCR89C100. See
|
39 | 67e999be | bellard | * http://www.ibiblio.org/pub/historic-linux/early-ports/Sparc/NCR/NCR89C100.txt
|
40 | 67e999be | bellard | * and
|
41 | 67e999be | bellard | * http://www.ibiblio.org/pub/historic-linux/early-ports/Sparc/NCR/DMA2.txt
|
42 | 67e999be | bellard | */
|
43 | 67e999be | bellard | |
44 | 67e999be | bellard | #ifdef DEBUG_DMA
|
45 | 001faf32 | Blue Swirl | #define DPRINTF(fmt, ...) \
|
46 | 001faf32 | Blue Swirl | do { printf("DMA: " fmt , ## __VA_ARGS__); } while (0) |
47 | 67e999be | bellard | #else
|
48 | 001faf32 | Blue Swirl | #define DPRINTF(fmt, ...)
|
49 | 67e999be | bellard | #endif
|
50 | 67e999be | bellard | |
51 | 5aca8c3b | blueswir1 | #define DMA_REGS 4 |
52 | 5aca8c3b | blueswir1 | #define DMA_SIZE (4 * sizeof(uint32_t)) |
53 | 09723aa1 | blueswir1 | /* We need the mask, because one instance of the device is not page
|
54 | 09723aa1 | blueswir1 | aligned (ledma, start address 0x0010) */
|
55 | 09723aa1 | blueswir1 | #define DMA_MASK (DMA_SIZE - 1) |
56 | 67e999be | bellard | |
57 | 67e999be | bellard | #define DMA_VER 0xa0000000 |
58 | 67e999be | bellard | #define DMA_INTR 1 |
59 | 67e999be | bellard | #define DMA_INTREN 0x10 |
60 | 67e999be | bellard | #define DMA_WRITE_MEM 0x100 |
61 | 67e999be | bellard | #define DMA_LOADED 0x04000000 |
62 | 5aca8c3b | blueswir1 | #define DMA_DRAIN_FIFO 0x40 |
63 | 67e999be | bellard | #define DMA_RESET 0x80 |
64 | 67e999be | bellard | |
65 | 67e999be | bellard | typedef struct DMAState DMAState; |
66 | 67e999be | bellard | |
67 | 67e999be | bellard | struct DMAState {
|
68 | 6f6260c7 | Blue Swirl | SysBusDevice busdev; |
69 | 67e999be | bellard | uint32_t dmaregs[DMA_REGS]; |
70 | 5aca8c3b | blueswir1 | qemu_irq irq; |
71 | 2d069bab | blueswir1 | void *iommu;
|
72 | 2d069bab | blueswir1 | qemu_irq dev_reset; |
73 | 67e999be | bellard | }; |
74 | 67e999be | bellard | |
75 | 9b94dc32 | bellard | /* Note: on sparc, the lance 16 bit bus is swapped */
|
76 | c227f099 | Anthony Liguori | void ledma_memory_read(void *opaque, target_phys_addr_t addr, |
77 | 9b94dc32 | bellard | uint8_t *buf, int len, int do_bswap) |
78 | 67e999be | bellard | { |
79 | 67e999be | bellard | DMAState *s = opaque; |
80 | 9b94dc32 | bellard | int i;
|
81 | 67e999be | bellard | |
82 | 67e999be | bellard | DPRINTF("DMA write, direction: %c, addr 0x%8.8x\n",
|
83 | 67e999be | bellard | s->dmaregs[0] & DMA_WRITE_MEM ? 'w': 'r', s->dmaregs[1]); |
84 | 5aca8c3b | blueswir1 | addr |= s->dmaregs[3];
|
85 | 9b94dc32 | bellard | if (do_bswap) {
|
86 | 9b94dc32 | bellard | sparc_iommu_memory_read(s->iommu, addr, buf, len); |
87 | 9b94dc32 | bellard | } else {
|
88 | 9b94dc32 | bellard | addr &= ~1;
|
89 | 9b94dc32 | bellard | len &= ~1;
|
90 | 9b94dc32 | bellard | sparc_iommu_memory_read(s->iommu, addr, buf, len); |
91 | 9b94dc32 | bellard | for(i = 0; i < len; i += 2) { |
92 | 9b94dc32 | bellard | bswap16s((uint16_t *)(buf + i)); |
93 | 9b94dc32 | bellard | } |
94 | 9b94dc32 | bellard | } |
95 | 67e999be | bellard | } |
96 | 67e999be | bellard | |
97 | c227f099 | Anthony Liguori | void ledma_memory_write(void *opaque, target_phys_addr_t addr, |
98 | 9b94dc32 | bellard | uint8_t *buf, int len, int do_bswap) |
99 | 67e999be | bellard | { |
100 | 67e999be | bellard | DMAState *s = opaque; |
101 | 9b94dc32 | bellard | int l, i;
|
102 | 9b94dc32 | bellard | uint16_t tmp_buf[32];
|
103 | 67e999be | bellard | |
104 | 67e999be | bellard | DPRINTF("DMA read, direction: %c, addr 0x%8.8x\n",
|
105 | 67e999be | bellard | s->dmaregs[0] & DMA_WRITE_MEM ? 'w': 'r', s->dmaregs[1]); |
106 | 5aca8c3b | blueswir1 | addr |= s->dmaregs[3];
|
107 | 9b94dc32 | bellard | if (do_bswap) {
|
108 | 9b94dc32 | bellard | sparc_iommu_memory_write(s->iommu, addr, buf, len); |
109 | 9b94dc32 | bellard | } else {
|
110 | 9b94dc32 | bellard | addr &= ~1;
|
111 | 9b94dc32 | bellard | len &= ~1;
|
112 | 9b94dc32 | bellard | while (len > 0) { |
113 | 9b94dc32 | bellard | l = len; |
114 | 9b94dc32 | bellard | if (l > sizeof(tmp_buf)) |
115 | 9b94dc32 | bellard | l = sizeof(tmp_buf);
|
116 | 9b94dc32 | bellard | for(i = 0; i < l; i += 2) { |
117 | 9b94dc32 | bellard | tmp_buf[i >> 1] = bswap16(*(uint16_t *)(buf + i));
|
118 | 9b94dc32 | bellard | } |
119 | 9b94dc32 | bellard | sparc_iommu_memory_write(s->iommu, addr, (uint8_t *)tmp_buf, l); |
120 | 9b94dc32 | bellard | len -= l; |
121 | 9b94dc32 | bellard | buf += l; |
122 | 9b94dc32 | bellard | addr += l; |
123 | 9b94dc32 | bellard | } |
124 | 9b94dc32 | bellard | } |
125 | 67e999be | bellard | } |
126 | 67e999be | bellard | |
127 | 70c0de96 | blueswir1 | static void dma_set_irq(void *opaque, int irq, int level) |
128 | 67e999be | bellard | { |
129 | 67e999be | bellard | DMAState *s = opaque; |
130 | 70c0de96 | blueswir1 | if (level) {
|
131 | 70c0de96 | blueswir1 | s->dmaregs[0] |= DMA_INTR;
|
132 | 6f57bbf4 | Artyom Tarasenko | if (s->dmaregs[0] & DMA_INTREN) { |
133 | 6f57bbf4 | Artyom Tarasenko | DPRINTF("Raise IRQ\n");
|
134 | 6f57bbf4 | Artyom Tarasenko | qemu_irq_raise(s->irq); |
135 | 6f57bbf4 | Artyom Tarasenko | } |
136 | 70c0de96 | blueswir1 | } else {
|
137 | 6f57bbf4 | Artyom Tarasenko | if (s->dmaregs[0] & DMA_INTR) { |
138 | 6f57bbf4 | Artyom Tarasenko | s->dmaregs[0] &= ~DMA_INTR;
|
139 | 6f57bbf4 | Artyom Tarasenko | if (s->dmaregs[0] & DMA_INTREN) { |
140 | 6f57bbf4 | Artyom Tarasenko | DPRINTF("Lower IRQ\n");
|
141 | 6f57bbf4 | Artyom Tarasenko | qemu_irq_lower(s->irq); |
142 | 6f57bbf4 | Artyom Tarasenko | } |
143 | 6f57bbf4 | Artyom Tarasenko | } |
144 | 70c0de96 | blueswir1 | } |
145 | 67e999be | bellard | } |
146 | 67e999be | bellard | |
147 | 67e999be | bellard | void espdma_memory_read(void *opaque, uint8_t *buf, int len) |
148 | 67e999be | bellard | { |
149 | 67e999be | bellard | DMAState *s = opaque; |
150 | 67e999be | bellard | |
151 | 67e999be | bellard | DPRINTF("DMA read, direction: %c, addr 0x%8.8x\n",
|
152 | 67e999be | bellard | s->dmaregs[0] & DMA_WRITE_MEM ? 'w': 'r', s->dmaregs[1]); |
153 | 67e999be | bellard | sparc_iommu_memory_read(s->iommu, s->dmaregs[1], buf, len);
|
154 | 67e999be | bellard | s->dmaregs[1] += len;
|
155 | 67e999be | bellard | } |
156 | 67e999be | bellard | |
157 | 67e999be | bellard | void espdma_memory_write(void *opaque, uint8_t *buf, int len) |
158 | 67e999be | bellard | { |
159 | 67e999be | bellard | DMAState *s = opaque; |
160 | 67e999be | bellard | |
161 | 67e999be | bellard | DPRINTF("DMA write, direction: %c, addr 0x%8.8x\n",
|
162 | 67e999be | bellard | s->dmaregs[0] & DMA_WRITE_MEM ? 'w': 'r', s->dmaregs[1]); |
163 | 67e999be | bellard | sparc_iommu_memory_write(s->iommu, s->dmaregs[1], buf, len);
|
164 | 67e999be | bellard | s->dmaregs[1] += len;
|
165 | 67e999be | bellard | } |
166 | 67e999be | bellard | |
167 | c227f099 | Anthony Liguori | static uint32_t dma_mem_readl(void *opaque, target_phys_addr_t addr) |
168 | 67e999be | bellard | { |
169 | 67e999be | bellard | DMAState *s = opaque; |
170 | 67e999be | bellard | uint32_t saddr; |
171 | 67e999be | bellard | |
172 | 09723aa1 | blueswir1 | saddr = (addr & DMA_MASK) >> 2;
|
173 | 5aca8c3b | blueswir1 | DPRINTF("read dmareg " TARGET_FMT_plx ": 0x%8.8x\n", addr, |
174 | 5aca8c3b | blueswir1 | s->dmaregs[saddr]); |
175 | 67e999be | bellard | |
176 | 67e999be | bellard | return s->dmaregs[saddr];
|
177 | 67e999be | bellard | } |
178 | 67e999be | bellard | |
179 | c227f099 | Anthony Liguori | static void dma_mem_writel(void *opaque, target_phys_addr_t addr, uint32_t val) |
180 | 67e999be | bellard | { |
181 | 67e999be | bellard | DMAState *s = opaque; |
182 | 67e999be | bellard | uint32_t saddr; |
183 | 67e999be | bellard | |
184 | 09723aa1 | blueswir1 | saddr = (addr & DMA_MASK) >> 2;
|
185 | 5aca8c3b | blueswir1 | DPRINTF("write dmareg " TARGET_FMT_plx ": 0x%8.8x -> 0x%8.8x\n", addr, |
186 | 5aca8c3b | blueswir1 | s->dmaregs[saddr], val); |
187 | 67e999be | bellard | switch (saddr) {
|
188 | 67e999be | bellard | case 0: |
189 | 6f57bbf4 | Artyom Tarasenko | if (val & DMA_INTREN) {
|
190 | 6f57bbf4 | Artyom Tarasenko | if (val & DMA_INTR) {
|
191 | 6f57bbf4 | Artyom Tarasenko | DPRINTF("Raise IRQ\n");
|
192 | 6f57bbf4 | Artyom Tarasenko | qemu_irq_raise(s->irq); |
193 | 6f57bbf4 | Artyom Tarasenko | } |
194 | 6f57bbf4 | Artyom Tarasenko | } else {
|
195 | 6f57bbf4 | Artyom Tarasenko | if (s->dmaregs[0] & (DMA_INTR | DMA_INTREN)) { |
196 | 6f57bbf4 | Artyom Tarasenko | DPRINTF("Lower IRQ\n");
|
197 | 6f57bbf4 | Artyom Tarasenko | qemu_irq_lower(s->irq); |
198 | 6f57bbf4 | Artyom Tarasenko | } |
199 | d537cf6c | pbrook | } |
200 | 67e999be | bellard | if (val & DMA_RESET) {
|
201 | 2d069bab | blueswir1 | qemu_irq_raise(s->dev_reset); |
202 | 2d069bab | blueswir1 | qemu_irq_lower(s->dev_reset); |
203 | 5aca8c3b | blueswir1 | } else if (val & DMA_DRAIN_FIFO) { |
204 | 5aca8c3b | blueswir1 | val &= ~DMA_DRAIN_FIFO; |
205 | 67e999be | bellard | } else if (val == 0) |
206 | 5aca8c3b | blueswir1 | val = DMA_DRAIN_FIFO; |
207 | 67e999be | bellard | val &= 0x0fffffff;
|
208 | 67e999be | bellard | val |= DMA_VER; |
209 | 67e999be | bellard | break;
|
210 | 67e999be | bellard | case 1: |
211 | 67e999be | bellard | s->dmaregs[0] |= DMA_LOADED;
|
212 | 67e999be | bellard | break;
|
213 | 67e999be | bellard | default:
|
214 | 67e999be | bellard | break;
|
215 | 67e999be | bellard | } |
216 | 67e999be | bellard | s->dmaregs[saddr] = val; |
217 | 67e999be | bellard | } |
218 | 67e999be | bellard | |
219 | d60efc6b | Blue Swirl | static CPUReadMemoryFunc * const dma_mem_read[3] = { |
220 | 7c560456 | blueswir1 | NULL,
|
221 | 7c560456 | blueswir1 | NULL,
|
222 | 67e999be | bellard | dma_mem_readl, |
223 | 67e999be | bellard | }; |
224 | 67e999be | bellard | |
225 | d60efc6b | Blue Swirl | static CPUWriteMemoryFunc * const dma_mem_write[3] = { |
226 | 7c560456 | blueswir1 | NULL,
|
227 | 7c560456 | blueswir1 | NULL,
|
228 | 67e999be | bellard | dma_mem_writel, |
229 | 67e999be | bellard | }; |
230 | 67e999be | bellard | |
231 | 49ef6c90 | Blue Swirl | static void dma_reset(DeviceState *d) |
232 | 67e999be | bellard | { |
233 | 49ef6c90 | Blue Swirl | DMAState *s = container_of(d, DMAState, busdev.qdev); |
234 | 67e999be | bellard | |
235 | 5aca8c3b | blueswir1 | memset(s->dmaregs, 0, DMA_SIZE);
|
236 | 67e999be | bellard | s->dmaregs[0] = DMA_VER;
|
237 | 67e999be | bellard | } |
238 | 67e999be | bellard | |
239 | 75c497dc | Blue Swirl | static const VMStateDescription vmstate_dma = { |
240 | 75c497dc | Blue Swirl | .name ="sparc32_dma",
|
241 | 75c497dc | Blue Swirl | .version_id = 2,
|
242 | 75c497dc | Blue Swirl | .minimum_version_id = 2,
|
243 | 75c497dc | Blue Swirl | .minimum_version_id_old = 2,
|
244 | 75c497dc | Blue Swirl | .fields = (VMStateField []) { |
245 | 75c497dc | Blue Swirl | VMSTATE_UINT32_ARRAY(dmaregs, DMAState, DMA_REGS), |
246 | 75c497dc | Blue Swirl | VMSTATE_END_OF_LIST() |
247 | 75c497dc | Blue Swirl | } |
248 | 75c497dc | Blue Swirl | }; |
249 | 67e999be | bellard | |
250 | 81a322d4 | Gerd Hoffmann | static int sparc32_dma_init1(SysBusDevice *dev) |
251 | 6f6260c7 | Blue Swirl | { |
252 | 6f6260c7 | Blue Swirl | DMAState *s = FROM_SYSBUS(DMAState, dev); |
253 | 6f6260c7 | Blue Swirl | int dma_io_memory;
|
254 | 67e999be | bellard | |
255 | 6f6260c7 | Blue Swirl | sysbus_init_irq(dev, &s->irq); |
256 | 67e999be | bellard | |
257 | 1eed09cb | Avi Kivity | dma_io_memory = cpu_register_io_memory(dma_mem_read, dma_mem_write, s); |
258 | 6f6260c7 | Blue Swirl | sysbus_init_mmio(dev, DMA_SIZE, dma_io_memory); |
259 | 67e999be | bellard | |
260 | 6f6260c7 | Blue Swirl | qdev_init_gpio_in(&dev->qdev, dma_set_irq, 1);
|
261 | 74ff8d90 | Blue Swirl | qdev_init_gpio_out(&dev->qdev, &s->dev_reset, 1);
|
262 | 49ef6c90 | Blue Swirl | |
263 | 81a322d4 | Gerd Hoffmann | return 0; |
264 | 6f6260c7 | Blue Swirl | } |
265 | 67e999be | bellard | |
266 | 6f6260c7 | Blue Swirl | static SysBusDeviceInfo sparc32_dma_info = {
|
267 | 6f6260c7 | Blue Swirl | .init = sparc32_dma_init1, |
268 | 6f6260c7 | Blue Swirl | .qdev.name = "sparc32_dma",
|
269 | 6f6260c7 | Blue Swirl | .qdev.size = sizeof(DMAState),
|
270 | 49ef6c90 | Blue Swirl | .qdev.vmsd = &vmstate_dma, |
271 | 49ef6c90 | Blue Swirl | .qdev.reset = dma_reset, |
272 | ee6847d1 | Gerd Hoffmann | .qdev.props = (Property[]) { |
273 | 3180d772 | Gerd Hoffmann | DEFINE_PROP_PTR("iommu_opaque", DMAState, iommu),
|
274 | 3180d772 | Gerd Hoffmann | DEFINE_PROP_END_OF_LIST(), |
275 | 6f6260c7 | Blue Swirl | } |
276 | 6f6260c7 | Blue Swirl | }; |
277 | 6f6260c7 | Blue Swirl | |
278 | 6f6260c7 | Blue Swirl | static void sparc32_dma_register_devices(void) |
279 | 6f6260c7 | Blue Swirl | { |
280 | 6f6260c7 | Blue Swirl | sysbus_register_withprop(&sparc32_dma_info); |
281 | 67e999be | bellard | } |
282 | 6f6260c7 | Blue Swirl | |
283 | 6f6260c7 | Blue Swirl | device_init(sparc32_dma_register_devices) |