]> git.proxmox.com Git - qemu.git/blame - hw/sparc32_dma.c
target-i386: Fix accidental use of SoftFloat uint64 type
[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)
86d1c388
BB
47/* ledma has more than 4 registers, Solaris reads the 5th one */
48#define DMA_ETH_SIZE (8 * sizeof(uint32_t))
49#define DMA_MAX_REG_OFFSET (2 * DMA_SIZE - 1)
67e999be
FB
50
51#define DMA_VER 0xa0000000
52#define DMA_INTR 1
53#define DMA_INTREN 0x10
54#define DMA_WRITE_MEM 0x100
73d74342 55#define DMA_EN 0x200
67e999be 56#define DMA_LOADED 0x04000000
5aca8c3b 57#define DMA_DRAIN_FIFO 0x40
67e999be
FB
58#define DMA_RESET 0x80
59
65899fe3
AT
60/* XXX SCSI and ethernet should have different read-only bit masks */
61#define DMA_CSR_RO_MASK 0xfe000007
62
67e999be
FB
63typedef struct DMAState DMAState;
64
65struct DMAState {
6f6260c7 66 SysBusDevice busdev;
67e999be 67 uint32_t dmaregs[DMA_REGS];
5aca8c3b 68 qemu_irq irq;
2d069bab 69 void *iommu;
73d74342 70 qemu_irq gpio[2];
86d1c388 71 uint32_t is_ledma;
73d74342
BS
72};
73
74enum {
75 GPIO_RESET = 0,
76 GPIO_DMA,
67e999be
FB
77};
78
9b94dc32 79/* Note: on sparc, the lance 16 bit bus is swapped */
c227f099 80void ledma_memory_read(void *opaque, target_phys_addr_t addr,
9b94dc32 81 uint8_t *buf, int len, int do_bswap)
67e999be
FB
82{
83 DMAState *s = opaque;
9b94dc32 84 int i;
67e999be 85
5aca8c3b 86 addr |= s->dmaregs[3];
97bf4851 87 trace_ledma_memory_read(addr);
9b94dc32
FB
88 if (do_bswap) {
89 sparc_iommu_memory_read(s->iommu, addr, buf, len);
90 } else {
91 addr &= ~1;
92 len &= ~1;
93 sparc_iommu_memory_read(s->iommu, addr, buf, len);
94 for(i = 0; i < len; i += 2) {
95 bswap16s((uint16_t *)(buf + i));
96 }
97 }
67e999be
FB
98}
99
c227f099 100void ledma_memory_write(void *opaque, target_phys_addr_t addr,
9b94dc32 101 uint8_t *buf, int len, int do_bswap)
67e999be
FB
102{
103 DMAState *s = opaque;
9b94dc32
FB
104 int l, i;
105 uint16_t tmp_buf[32];
67e999be 106
5aca8c3b 107 addr |= s->dmaregs[3];
97bf4851 108 trace_ledma_memory_write(addr);
9b94dc32
FB
109 if (do_bswap) {
110 sparc_iommu_memory_write(s->iommu, addr, buf, len);
111 } else {
112 addr &= ~1;
113 len &= ~1;
114 while (len > 0) {
115 l = len;
116 if (l > sizeof(tmp_buf))
117 l = sizeof(tmp_buf);
118 for(i = 0; i < l; i += 2) {
119 tmp_buf[i >> 1] = bswap16(*(uint16_t *)(buf + i));
120 }
121 sparc_iommu_memory_write(s->iommu, addr, (uint8_t *)tmp_buf, l);
122 len -= l;
123 buf += l;
124 addr += l;
125 }
126 }
67e999be
FB
127}
128
70c0de96 129static void dma_set_irq(void *opaque, int irq, int level)
67e999be
FB
130{
131 DMAState *s = opaque;
70c0de96 132 if (level) {
70c0de96 133 s->dmaregs[0] |= DMA_INTR;
6f57bbf4 134 if (s->dmaregs[0] & DMA_INTREN) {
97bf4851 135 trace_sparc32_dma_set_irq_raise();
6f57bbf4
AT
136 qemu_irq_raise(s->irq);
137 }
70c0de96 138 } else {
6f57bbf4
AT
139 if (s->dmaregs[0] & DMA_INTR) {
140 s->dmaregs[0] &= ~DMA_INTR;
141 if (s->dmaregs[0] & DMA_INTREN) {
97bf4851 142 trace_sparc32_dma_set_irq_lower();
6f57bbf4
AT
143 qemu_irq_lower(s->irq);
144 }
145 }
70c0de96 146 }
67e999be
FB
147}
148
149void espdma_memory_read(void *opaque, uint8_t *buf, int len)
150{
151 DMAState *s = opaque;
152
97bf4851 153 trace_espdma_memory_read(s->dmaregs[1]);
67e999be 154 sparc_iommu_memory_read(s->iommu, s->dmaregs[1], buf, len);
67e999be
FB
155 s->dmaregs[1] += len;
156}
157
158void espdma_memory_write(void *opaque, uint8_t *buf, int len)
159{
160 DMAState *s = opaque;
161
97bf4851 162 trace_espdma_memory_write(s->dmaregs[1]);
67e999be 163 sparc_iommu_memory_write(s->iommu, s->dmaregs[1], buf, len);
67e999be
FB
164 s->dmaregs[1] += len;
165}
166
c227f099 167static uint32_t dma_mem_readl(void *opaque, target_phys_addr_t addr)
67e999be
FB
168{
169 DMAState *s = opaque;
170 uint32_t saddr;
171
86d1c388
BB
172 if (s->is_ledma && (addr > DMA_MAX_REG_OFFSET)) {
173 return 0; /* extra mystery register(s) */
174 }
09723aa1 175 saddr = (addr & DMA_MASK) >> 2;
97bf4851 176 trace_sparc32_dma_mem_readl(addr, s->dmaregs[saddr]);
67e999be
FB
177 return s->dmaregs[saddr];
178}
179
c227f099 180static void dma_mem_writel(void *opaque, target_phys_addr_t addr, uint32_t val)
67e999be
FB
181{
182 DMAState *s = opaque;
183 uint32_t saddr;
184
86d1c388
BB
185 if (s->is_ledma && (addr > DMA_MAX_REG_OFFSET)) {
186 return; /* extra mystery register(s) */
187 }
09723aa1 188 saddr = (addr & DMA_MASK) >> 2;
97bf4851 189 trace_sparc32_dma_mem_writel(addr, s->dmaregs[saddr], val);
67e999be
FB
190 switch (saddr) {
191 case 0:
6f57bbf4 192 if (val & DMA_INTREN) {
65899fe3 193 if (s->dmaregs[0] & DMA_INTR) {
97bf4851 194 trace_sparc32_dma_set_irq_raise();
6f57bbf4
AT
195 qemu_irq_raise(s->irq);
196 }
197 } else {
198 if (s->dmaregs[0] & (DMA_INTR | DMA_INTREN)) {
97bf4851 199 trace_sparc32_dma_set_irq_lower();
6f57bbf4
AT
200 qemu_irq_lower(s->irq);
201 }
d537cf6c 202 }
67e999be 203 if (val & DMA_RESET) {
73d74342
BS
204 qemu_irq_raise(s->gpio[GPIO_RESET]);
205 qemu_irq_lower(s->gpio[GPIO_RESET]);
5aca8c3b
BS
206 } else if (val & DMA_DRAIN_FIFO) {
207 val &= ~DMA_DRAIN_FIFO;
67e999be 208 } else if (val == 0)
5aca8c3b 209 val = DMA_DRAIN_FIFO;
73d74342
BS
210
211 if (val & DMA_EN && !(s->dmaregs[0] & DMA_EN)) {
97bf4851 212 trace_sparc32_dma_enable_raise();
73d74342
BS
213 qemu_irq_raise(s->gpio[GPIO_DMA]);
214 } else if (!(val & DMA_EN) && !!(s->dmaregs[0] & DMA_EN)) {
97bf4851 215 trace_sparc32_dma_enable_lower();
73d74342
BS
216 qemu_irq_lower(s->gpio[GPIO_DMA]);
217 }
218
65899fe3 219 val &= ~DMA_CSR_RO_MASK;
67e999be 220 val |= DMA_VER;
65899fe3 221 s->dmaregs[0] = (s->dmaregs[0] & DMA_CSR_RO_MASK) | val;
67e999be
FB
222 break;
223 case 1:
224 s->dmaregs[0] |= DMA_LOADED;
65899fe3 225 /* fall through */
67e999be 226 default:
65899fe3 227 s->dmaregs[saddr] = val;
67e999be
FB
228 break;
229 }
67e999be
FB
230}
231
d60efc6b 232static CPUReadMemoryFunc * const dma_mem_read[3] = {
7c560456
BS
233 NULL,
234 NULL,
67e999be
FB
235 dma_mem_readl,
236};
237
d60efc6b 238static CPUWriteMemoryFunc * const dma_mem_write[3] = {
7c560456
BS
239 NULL,
240 NULL,
67e999be
FB
241 dma_mem_writel,
242};
243
49ef6c90 244static void dma_reset(DeviceState *d)
67e999be 245{
49ef6c90 246 DMAState *s = container_of(d, DMAState, busdev.qdev);
67e999be 247
5aca8c3b 248 memset(s->dmaregs, 0, DMA_SIZE);
67e999be 249 s->dmaregs[0] = DMA_VER;
67e999be
FB
250}
251
75c497dc
BS
252static const VMStateDescription vmstate_dma = {
253 .name ="sparc32_dma",
254 .version_id = 2,
255 .minimum_version_id = 2,
256 .minimum_version_id_old = 2,
257 .fields = (VMStateField []) {
258 VMSTATE_UINT32_ARRAY(dmaregs, DMAState, DMA_REGS),
259 VMSTATE_END_OF_LIST()
260 }
261};
67e999be 262
81a322d4 263static int sparc32_dma_init1(SysBusDevice *dev)
6f6260c7
BS
264{
265 DMAState *s = FROM_SYSBUS(DMAState, dev);
266 int dma_io_memory;
86d1c388 267 int reg_size;
67e999be 268
6f6260c7 269 sysbus_init_irq(dev, &s->irq);
67e999be 270
2507c12a
AG
271 dma_io_memory = cpu_register_io_memory(dma_mem_read, dma_mem_write, s,
272 DEVICE_NATIVE_ENDIAN);
86d1c388
BB
273 reg_size = s->is_ledma ? DMA_ETH_SIZE : DMA_SIZE;
274 sysbus_init_mmio(dev, reg_size, dma_io_memory);
67e999be 275
6f6260c7 276 qdev_init_gpio_in(&dev->qdev, dma_set_irq, 1);
73d74342 277 qdev_init_gpio_out(&dev->qdev, s->gpio, 2);
49ef6c90 278
81a322d4 279 return 0;
6f6260c7 280}
67e999be 281
6f6260c7
BS
282static SysBusDeviceInfo sparc32_dma_info = {
283 .init = sparc32_dma_init1,
284 .qdev.name = "sparc32_dma",
285 .qdev.size = sizeof(DMAState),
49ef6c90
BS
286 .qdev.vmsd = &vmstate_dma,
287 .qdev.reset = dma_reset,
ee6847d1 288 .qdev.props = (Property[]) {
3180d772 289 DEFINE_PROP_PTR("iommu_opaque", DMAState, iommu),
86d1c388 290 DEFINE_PROP_UINT32("is_ledma", DMAState, is_ledma, 0),
3180d772 291 DEFINE_PROP_END_OF_LIST(),
6f6260c7
BS
292 }
293};
294
295static void sparc32_dma_register_devices(void)
296{
297 sysbus_register_withprop(&sparc32_dma_info);
67e999be 298}
6f6260c7
BS
299
300device_init(sparc32_dma_register_devices)