]> git.proxmox.com Git - qemu.git/blame - hw/sparc32_dma.c
user: Restore debug usage message for '-d ?' in user mode emulation
[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)
e0087e61 47/* OBP says 0x20 bytes for ledma, the extras are aliased to espdma */
86d1c388
BB
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 172 if (s->is_ledma && (addr > DMA_MAX_REG_OFFSET)) {
e0087e61
BB
173 /* aliased to espdma, but we can't get there from here */
174 /* buggy driver if using undocumented behavior, just return 0 */
175 trace_sparc32_dma_mem_readl(addr, 0);
176 return 0;
86d1c388 177 }
09723aa1 178 saddr = (addr & DMA_MASK) >> 2;
97bf4851 179 trace_sparc32_dma_mem_readl(addr, s->dmaregs[saddr]);
67e999be
FB
180 return s->dmaregs[saddr];
181}
182
c227f099 183static void dma_mem_writel(void *opaque, target_phys_addr_t addr, uint32_t val)
67e999be
FB
184{
185 DMAState *s = opaque;
186 uint32_t saddr;
187
86d1c388 188 if (s->is_ledma && (addr > DMA_MAX_REG_OFFSET)) {
e0087e61
BB
189 /* aliased to espdma, but we can't get there from here */
190 trace_sparc32_dma_mem_writel(addr, 0, val);
191 return;
86d1c388 192 }
09723aa1 193 saddr = (addr & DMA_MASK) >> 2;
97bf4851 194 trace_sparc32_dma_mem_writel(addr, s->dmaregs[saddr], val);
67e999be
FB
195 switch (saddr) {
196 case 0:
6f57bbf4 197 if (val & DMA_INTREN) {
65899fe3 198 if (s->dmaregs[0] & DMA_INTR) {
97bf4851 199 trace_sparc32_dma_set_irq_raise();
6f57bbf4
AT
200 qemu_irq_raise(s->irq);
201 }
202 } else {
203 if (s->dmaregs[0] & (DMA_INTR | DMA_INTREN)) {
97bf4851 204 trace_sparc32_dma_set_irq_lower();
6f57bbf4
AT
205 qemu_irq_lower(s->irq);
206 }
d537cf6c 207 }
67e999be 208 if (val & DMA_RESET) {
73d74342
BS
209 qemu_irq_raise(s->gpio[GPIO_RESET]);
210 qemu_irq_lower(s->gpio[GPIO_RESET]);
5aca8c3b
BS
211 } else if (val & DMA_DRAIN_FIFO) {
212 val &= ~DMA_DRAIN_FIFO;
67e999be 213 } else if (val == 0)
5aca8c3b 214 val = DMA_DRAIN_FIFO;
73d74342
BS
215
216 if (val & DMA_EN && !(s->dmaregs[0] & DMA_EN)) {
97bf4851 217 trace_sparc32_dma_enable_raise();
73d74342
BS
218 qemu_irq_raise(s->gpio[GPIO_DMA]);
219 } else if (!(val & DMA_EN) && !!(s->dmaregs[0] & DMA_EN)) {
97bf4851 220 trace_sparc32_dma_enable_lower();
73d74342
BS
221 qemu_irq_lower(s->gpio[GPIO_DMA]);
222 }
223
65899fe3 224 val &= ~DMA_CSR_RO_MASK;
67e999be 225 val |= DMA_VER;
65899fe3 226 s->dmaregs[0] = (s->dmaregs[0] & DMA_CSR_RO_MASK) | val;
67e999be
FB
227 break;
228 case 1:
229 s->dmaregs[0] |= DMA_LOADED;
65899fe3 230 /* fall through */
67e999be 231 default:
65899fe3 232 s->dmaregs[saddr] = val;
67e999be
FB
233 break;
234 }
67e999be
FB
235}
236
d60efc6b 237static CPUReadMemoryFunc * const dma_mem_read[3] = {
7c560456
BS
238 NULL,
239 NULL,
67e999be
FB
240 dma_mem_readl,
241};
242
d60efc6b 243static CPUWriteMemoryFunc * const dma_mem_write[3] = {
7c560456
BS
244 NULL,
245 NULL,
67e999be
FB
246 dma_mem_writel,
247};
248
49ef6c90 249static void dma_reset(DeviceState *d)
67e999be 250{
49ef6c90 251 DMAState *s = container_of(d, DMAState, busdev.qdev);
67e999be 252
5aca8c3b 253 memset(s->dmaregs, 0, DMA_SIZE);
67e999be 254 s->dmaregs[0] = DMA_VER;
67e999be
FB
255}
256
75c497dc
BS
257static const VMStateDescription vmstate_dma = {
258 .name ="sparc32_dma",
259 .version_id = 2,
260 .minimum_version_id = 2,
261 .minimum_version_id_old = 2,
262 .fields = (VMStateField []) {
263 VMSTATE_UINT32_ARRAY(dmaregs, DMAState, DMA_REGS),
264 VMSTATE_END_OF_LIST()
265 }
266};
67e999be 267
81a322d4 268static int sparc32_dma_init1(SysBusDevice *dev)
6f6260c7
BS
269{
270 DMAState *s = FROM_SYSBUS(DMAState, dev);
271 int dma_io_memory;
86d1c388 272 int reg_size;
67e999be 273
6f6260c7 274 sysbus_init_irq(dev, &s->irq);
67e999be 275
2507c12a
AG
276 dma_io_memory = cpu_register_io_memory(dma_mem_read, dma_mem_write, s,
277 DEVICE_NATIVE_ENDIAN);
86d1c388
BB
278 reg_size = s->is_ledma ? DMA_ETH_SIZE : DMA_SIZE;
279 sysbus_init_mmio(dev, reg_size, dma_io_memory);
67e999be 280
6f6260c7 281 qdev_init_gpio_in(&dev->qdev, dma_set_irq, 1);
73d74342 282 qdev_init_gpio_out(&dev->qdev, s->gpio, 2);
49ef6c90 283
81a322d4 284 return 0;
6f6260c7 285}
67e999be 286
6f6260c7
BS
287static SysBusDeviceInfo sparc32_dma_info = {
288 .init = sparc32_dma_init1,
289 .qdev.name = "sparc32_dma",
290 .qdev.size = sizeof(DMAState),
49ef6c90
BS
291 .qdev.vmsd = &vmstate_dma,
292 .qdev.reset = dma_reset,
ee6847d1 293 .qdev.props = (Property[]) {
3180d772 294 DEFINE_PROP_PTR("iommu_opaque", DMAState, iommu),
86d1c388 295 DEFINE_PROP_UINT32("is_ledma", DMAState, is_ledma, 0),
3180d772 296 DEFINE_PROP_END_OF_LIST(),
6f6260c7
BS
297 }
298};
299
300static void sparc32_dma_register_devices(void)
301{
302 sysbus_register_withprop(&sparc32_dma_info);
67e999be 303}
6f6260c7
BS
304
305device_init(sparc32_dma_register_devices)