root / hw / sparc32_dma.c @ 2871a3f6
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 | 97bf4851 | Blue Swirl | #include "trace.h" |
33 | 67e999be | bellard | |
34 | 67e999be | bellard | /*
|
35 | 67e999be | bellard | * This is the DMA controller part of chip STP2000 (Master I/O), also
|
36 | 67e999be | bellard | * produced as NCR89C100. See
|
37 | 67e999be | bellard | * http://www.ibiblio.org/pub/historic-linux/early-ports/Sparc/NCR/NCR89C100.txt
|
38 | 67e999be | bellard | * and
|
39 | 67e999be | bellard | * http://www.ibiblio.org/pub/historic-linux/early-ports/Sparc/NCR/DMA2.txt
|
40 | 67e999be | bellard | */
|
41 | 67e999be | bellard | |
42 | 5aca8c3b | blueswir1 | #define DMA_REGS 4 |
43 | 5aca8c3b | blueswir1 | #define DMA_SIZE (4 * sizeof(uint32_t)) |
44 | 09723aa1 | blueswir1 | /* We need the mask, because one instance of the device is not page
|
45 | 09723aa1 | blueswir1 | aligned (ledma, start address 0x0010) */
|
46 | 09723aa1 | blueswir1 | #define DMA_MASK (DMA_SIZE - 1) |
47 | 67e999be | bellard | |
48 | 67e999be | bellard | #define DMA_VER 0xa0000000 |
49 | 67e999be | bellard | #define DMA_INTR 1 |
50 | 67e999be | bellard | #define DMA_INTREN 0x10 |
51 | 67e999be | bellard | #define DMA_WRITE_MEM 0x100 |
52 | 73d74342 | Blue Swirl | #define DMA_EN 0x200 |
53 | 67e999be | bellard | #define DMA_LOADED 0x04000000 |
54 | 5aca8c3b | blueswir1 | #define DMA_DRAIN_FIFO 0x40 |
55 | 67e999be | bellard | #define DMA_RESET 0x80 |
56 | 67e999be | bellard | |
57 | 65899fe3 | Artyom Tarasenko | /* XXX SCSI and ethernet should have different read-only bit masks */
|
58 | 65899fe3 | Artyom Tarasenko | #define DMA_CSR_RO_MASK 0xfe000007 |
59 | 65899fe3 | Artyom Tarasenko | |
60 | 67e999be | bellard | typedef struct DMAState DMAState; |
61 | 67e999be | bellard | |
62 | 67e999be | bellard | struct DMAState {
|
63 | 6f6260c7 | Blue Swirl | SysBusDevice busdev; |
64 | 67e999be | bellard | uint32_t dmaregs[DMA_REGS]; |
65 | 5aca8c3b | blueswir1 | qemu_irq irq; |
66 | 2d069bab | blueswir1 | void *iommu;
|
67 | 73d74342 | Blue Swirl | qemu_irq gpio[2];
|
68 | 73d74342 | Blue Swirl | }; |
69 | 73d74342 | Blue Swirl | |
70 | 73d74342 | Blue Swirl | enum {
|
71 | 73d74342 | Blue Swirl | GPIO_RESET = 0,
|
72 | 73d74342 | Blue Swirl | GPIO_DMA, |
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 | 5aca8c3b | blueswir1 | addr |= s->dmaregs[3];
|
83 | 97bf4851 | Blue Swirl | trace_ledma_memory_read(addr); |
84 | 9b94dc32 | bellard | if (do_bswap) {
|
85 | 9b94dc32 | bellard | sparc_iommu_memory_read(s->iommu, addr, buf, len); |
86 | 9b94dc32 | bellard | } else {
|
87 | 9b94dc32 | bellard | addr &= ~1;
|
88 | 9b94dc32 | bellard | len &= ~1;
|
89 | 9b94dc32 | bellard | sparc_iommu_memory_read(s->iommu, addr, buf, len); |
90 | 9b94dc32 | bellard | for(i = 0; i < len; i += 2) { |
91 | 9b94dc32 | bellard | bswap16s((uint16_t *)(buf + i)); |
92 | 9b94dc32 | bellard | } |
93 | 9b94dc32 | bellard | } |
94 | 67e999be | bellard | } |
95 | 67e999be | bellard | |
96 | c227f099 | Anthony Liguori | void ledma_memory_write(void *opaque, target_phys_addr_t addr, |
97 | 9b94dc32 | bellard | uint8_t *buf, int len, int do_bswap) |
98 | 67e999be | bellard | { |
99 | 67e999be | bellard | DMAState *s = opaque; |
100 | 9b94dc32 | bellard | int l, i;
|
101 | 9b94dc32 | bellard | uint16_t tmp_buf[32];
|
102 | 67e999be | bellard | |
103 | 5aca8c3b | blueswir1 | addr |= s->dmaregs[3];
|
104 | 97bf4851 | Blue Swirl | trace_ledma_memory_write(addr); |
105 | 9b94dc32 | bellard | if (do_bswap) {
|
106 | 9b94dc32 | bellard | sparc_iommu_memory_write(s->iommu, addr, buf, len); |
107 | 9b94dc32 | bellard | } else {
|
108 | 9b94dc32 | bellard | addr &= ~1;
|
109 | 9b94dc32 | bellard | len &= ~1;
|
110 | 9b94dc32 | bellard | while (len > 0) { |
111 | 9b94dc32 | bellard | l = len; |
112 | 9b94dc32 | bellard | if (l > sizeof(tmp_buf)) |
113 | 9b94dc32 | bellard | l = sizeof(tmp_buf);
|
114 | 9b94dc32 | bellard | for(i = 0; i < l; i += 2) { |
115 | 9b94dc32 | bellard | tmp_buf[i >> 1] = bswap16(*(uint16_t *)(buf + i));
|
116 | 9b94dc32 | bellard | } |
117 | 9b94dc32 | bellard | sparc_iommu_memory_write(s->iommu, addr, (uint8_t *)tmp_buf, l); |
118 | 9b94dc32 | bellard | len -= l; |
119 | 9b94dc32 | bellard | buf += l; |
120 | 9b94dc32 | bellard | addr += l; |
121 | 9b94dc32 | bellard | } |
122 | 9b94dc32 | bellard | } |
123 | 67e999be | bellard | } |
124 | 67e999be | bellard | |
125 | 70c0de96 | blueswir1 | static void dma_set_irq(void *opaque, int irq, int level) |
126 | 67e999be | bellard | { |
127 | 67e999be | bellard | DMAState *s = opaque; |
128 | 70c0de96 | blueswir1 | if (level) {
|
129 | 70c0de96 | blueswir1 | s->dmaregs[0] |= DMA_INTR;
|
130 | 6f57bbf4 | Artyom Tarasenko | if (s->dmaregs[0] & DMA_INTREN) { |
131 | 97bf4851 | Blue Swirl | trace_sparc32_dma_set_irq_raise(); |
132 | 6f57bbf4 | Artyom Tarasenko | qemu_irq_raise(s->irq); |
133 | 6f57bbf4 | Artyom Tarasenko | } |
134 | 70c0de96 | blueswir1 | } else {
|
135 | 6f57bbf4 | Artyom Tarasenko | if (s->dmaregs[0] & DMA_INTR) { |
136 | 6f57bbf4 | Artyom Tarasenko | s->dmaregs[0] &= ~DMA_INTR;
|
137 | 6f57bbf4 | Artyom Tarasenko | if (s->dmaregs[0] & DMA_INTREN) { |
138 | 97bf4851 | Blue Swirl | trace_sparc32_dma_set_irq_lower(); |
139 | 6f57bbf4 | Artyom Tarasenko | qemu_irq_lower(s->irq); |
140 | 6f57bbf4 | Artyom Tarasenko | } |
141 | 6f57bbf4 | Artyom Tarasenko | } |
142 | 70c0de96 | blueswir1 | } |
143 | 67e999be | bellard | } |
144 | 67e999be | bellard | |
145 | 67e999be | bellard | void espdma_memory_read(void *opaque, uint8_t *buf, int len) |
146 | 67e999be | bellard | { |
147 | 67e999be | bellard | DMAState *s = opaque; |
148 | 67e999be | bellard | |
149 | 97bf4851 | Blue Swirl | trace_espdma_memory_read(s->dmaregs[1]);
|
150 | 67e999be | bellard | sparc_iommu_memory_read(s->iommu, s->dmaregs[1], buf, len);
|
151 | 67e999be | bellard | s->dmaregs[1] += len;
|
152 | 67e999be | bellard | } |
153 | 67e999be | bellard | |
154 | 67e999be | bellard | void espdma_memory_write(void *opaque, uint8_t *buf, int len) |
155 | 67e999be | bellard | { |
156 | 67e999be | bellard | DMAState *s = opaque; |
157 | 67e999be | bellard | |
158 | 97bf4851 | Blue Swirl | trace_espdma_memory_write(s->dmaregs[1]);
|
159 | 67e999be | bellard | sparc_iommu_memory_write(s->iommu, s->dmaregs[1], buf, len);
|
160 | 67e999be | bellard | s->dmaregs[1] += len;
|
161 | 67e999be | bellard | } |
162 | 67e999be | bellard | |
163 | c227f099 | Anthony Liguori | static uint32_t dma_mem_readl(void *opaque, target_phys_addr_t addr) |
164 | 67e999be | bellard | { |
165 | 67e999be | bellard | DMAState *s = opaque; |
166 | 67e999be | bellard | uint32_t saddr; |
167 | 67e999be | bellard | |
168 | 09723aa1 | blueswir1 | saddr = (addr & DMA_MASK) >> 2;
|
169 | 97bf4851 | Blue Swirl | trace_sparc32_dma_mem_readl(addr, s->dmaregs[saddr]); |
170 | 67e999be | bellard | return s->dmaregs[saddr];
|
171 | 67e999be | bellard | } |
172 | 67e999be | bellard | |
173 | c227f099 | Anthony Liguori | static void dma_mem_writel(void *opaque, target_phys_addr_t addr, uint32_t val) |
174 | 67e999be | bellard | { |
175 | 67e999be | bellard | DMAState *s = opaque; |
176 | 67e999be | bellard | uint32_t saddr; |
177 | 67e999be | bellard | |
178 | 09723aa1 | blueswir1 | saddr = (addr & DMA_MASK) >> 2;
|
179 | 97bf4851 | Blue Swirl | trace_sparc32_dma_mem_writel(addr, s->dmaregs[saddr], val); |
180 | 67e999be | bellard | switch (saddr) {
|
181 | 67e999be | bellard | case 0: |
182 | 6f57bbf4 | Artyom Tarasenko | if (val & DMA_INTREN) {
|
183 | 65899fe3 | Artyom Tarasenko | if (s->dmaregs[0] & DMA_INTR) { |
184 | 97bf4851 | Blue Swirl | trace_sparc32_dma_set_irq_raise(); |
185 | 6f57bbf4 | Artyom Tarasenko | qemu_irq_raise(s->irq); |
186 | 6f57bbf4 | Artyom Tarasenko | } |
187 | 6f57bbf4 | Artyom Tarasenko | } else {
|
188 | 6f57bbf4 | Artyom Tarasenko | if (s->dmaregs[0] & (DMA_INTR | DMA_INTREN)) { |
189 | 97bf4851 | Blue Swirl | trace_sparc32_dma_set_irq_lower(); |
190 | 6f57bbf4 | Artyom Tarasenko | qemu_irq_lower(s->irq); |
191 | 6f57bbf4 | Artyom Tarasenko | } |
192 | d537cf6c | pbrook | } |
193 | 67e999be | bellard | if (val & DMA_RESET) {
|
194 | 73d74342 | Blue Swirl | qemu_irq_raise(s->gpio[GPIO_RESET]); |
195 | 73d74342 | Blue Swirl | qemu_irq_lower(s->gpio[GPIO_RESET]); |
196 | 5aca8c3b | blueswir1 | } else if (val & DMA_DRAIN_FIFO) { |
197 | 5aca8c3b | blueswir1 | val &= ~DMA_DRAIN_FIFO; |
198 | 67e999be | bellard | } else if (val == 0) |
199 | 5aca8c3b | blueswir1 | val = DMA_DRAIN_FIFO; |
200 | 73d74342 | Blue Swirl | |
201 | 73d74342 | Blue Swirl | if (val & DMA_EN && !(s->dmaregs[0] & DMA_EN)) { |
202 | 97bf4851 | Blue Swirl | trace_sparc32_dma_enable_raise(); |
203 | 73d74342 | Blue Swirl | qemu_irq_raise(s->gpio[GPIO_DMA]); |
204 | 73d74342 | Blue Swirl | } else if (!(val & DMA_EN) && !!(s->dmaregs[0] & DMA_EN)) { |
205 | 97bf4851 | Blue Swirl | trace_sparc32_dma_enable_lower(); |
206 | 73d74342 | Blue Swirl | qemu_irq_lower(s->gpio[GPIO_DMA]); |
207 | 73d74342 | Blue Swirl | } |
208 | 73d74342 | Blue Swirl | |
209 | 65899fe3 | Artyom Tarasenko | val &= ~DMA_CSR_RO_MASK; |
210 | 67e999be | bellard | val |= DMA_VER; |
211 | 65899fe3 | Artyom Tarasenko | s->dmaregs[0] = (s->dmaregs[0] & DMA_CSR_RO_MASK) | val; |
212 | 67e999be | bellard | break;
|
213 | 67e999be | bellard | case 1: |
214 | 67e999be | bellard | s->dmaregs[0] |= DMA_LOADED;
|
215 | 65899fe3 | Artyom Tarasenko | /* fall through */
|
216 | 67e999be | bellard | default:
|
217 | 65899fe3 | Artyom Tarasenko | s->dmaregs[saddr] = val; |
218 | 67e999be | bellard | break;
|
219 | 67e999be | bellard | } |
220 | 67e999be | bellard | } |
221 | 67e999be | bellard | |
222 | d60efc6b | Blue Swirl | static CPUReadMemoryFunc * const dma_mem_read[3] = { |
223 | 7c560456 | blueswir1 | NULL,
|
224 | 7c560456 | blueswir1 | NULL,
|
225 | 67e999be | bellard | dma_mem_readl, |
226 | 67e999be | bellard | }; |
227 | 67e999be | bellard | |
228 | d60efc6b | Blue Swirl | static CPUWriteMemoryFunc * const dma_mem_write[3] = { |
229 | 7c560456 | blueswir1 | NULL,
|
230 | 7c560456 | blueswir1 | NULL,
|
231 | 67e999be | bellard | dma_mem_writel, |
232 | 67e999be | bellard | }; |
233 | 67e999be | bellard | |
234 | 49ef6c90 | Blue Swirl | static void dma_reset(DeviceState *d) |
235 | 67e999be | bellard | { |
236 | 49ef6c90 | Blue Swirl | DMAState *s = container_of(d, DMAState, busdev.qdev); |
237 | 67e999be | bellard | |
238 | 5aca8c3b | blueswir1 | memset(s->dmaregs, 0, DMA_SIZE);
|
239 | 67e999be | bellard | s->dmaregs[0] = DMA_VER;
|
240 | 67e999be | bellard | } |
241 | 67e999be | bellard | |
242 | 75c497dc | Blue Swirl | static const VMStateDescription vmstate_dma = { |
243 | 75c497dc | Blue Swirl | .name ="sparc32_dma",
|
244 | 75c497dc | Blue Swirl | .version_id = 2,
|
245 | 75c497dc | Blue Swirl | .minimum_version_id = 2,
|
246 | 75c497dc | Blue Swirl | .minimum_version_id_old = 2,
|
247 | 75c497dc | Blue Swirl | .fields = (VMStateField []) { |
248 | 75c497dc | Blue Swirl | VMSTATE_UINT32_ARRAY(dmaregs, DMAState, DMA_REGS), |
249 | 75c497dc | Blue Swirl | VMSTATE_END_OF_LIST() |
250 | 75c497dc | Blue Swirl | } |
251 | 75c497dc | Blue Swirl | }; |
252 | 67e999be | bellard | |
253 | 81a322d4 | Gerd Hoffmann | static int sparc32_dma_init1(SysBusDevice *dev) |
254 | 6f6260c7 | Blue Swirl | { |
255 | 6f6260c7 | Blue Swirl | DMAState *s = FROM_SYSBUS(DMAState, dev); |
256 | 6f6260c7 | Blue Swirl | int dma_io_memory;
|
257 | 67e999be | bellard | |
258 | 6f6260c7 | Blue Swirl | sysbus_init_irq(dev, &s->irq); |
259 | 67e999be | bellard | |
260 | 1eed09cb | Avi Kivity | dma_io_memory = cpu_register_io_memory(dma_mem_read, dma_mem_write, s); |
261 | 6f6260c7 | Blue Swirl | sysbus_init_mmio(dev, DMA_SIZE, dma_io_memory); |
262 | 67e999be | bellard | |
263 | 6f6260c7 | Blue Swirl | qdev_init_gpio_in(&dev->qdev, dma_set_irq, 1);
|
264 | 73d74342 | Blue Swirl | qdev_init_gpio_out(&dev->qdev, s->gpio, 2);
|
265 | 49ef6c90 | Blue Swirl | |
266 | 81a322d4 | Gerd Hoffmann | return 0; |
267 | 6f6260c7 | Blue Swirl | } |
268 | 67e999be | bellard | |
269 | 6f6260c7 | Blue Swirl | static SysBusDeviceInfo sparc32_dma_info = {
|
270 | 6f6260c7 | Blue Swirl | .init = sparc32_dma_init1, |
271 | 6f6260c7 | Blue Swirl | .qdev.name = "sparc32_dma",
|
272 | 6f6260c7 | Blue Swirl | .qdev.size = sizeof(DMAState),
|
273 | 49ef6c90 | Blue Swirl | .qdev.vmsd = &vmstate_dma, |
274 | 49ef6c90 | Blue Swirl | .qdev.reset = dma_reset, |
275 | ee6847d1 | Gerd Hoffmann | .qdev.props = (Property[]) { |
276 | 3180d772 | Gerd Hoffmann | DEFINE_PROP_PTR("iommu_opaque", DMAState, iommu),
|
277 | 3180d772 | Gerd Hoffmann | DEFINE_PROP_END_OF_LIST(), |
278 | 6f6260c7 | Blue Swirl | } |
279 | 6f6260c7 | Blue Swirl | }; |
280 | 6f6260c7 | Blue Swirl | |
281 | 6f6260c7 | Blue Swirl | static void sparc32_dma_register_devices(void) |
282 | 6f6260c7 | Blue Swirl | { |
283 | 6f6260c7 | Blue Swirl | sysbus_register_withprop(&sparc32_dma_info); |
284 | 67e999be | bellard | } |
285 | 6f6260c7 | Blue Swirl | |
286 | 6f6260c7 | Blue Swirl | device_init(sparc32_dma_register_devices) |