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