2 * QEMU Sparc32 DMA controller emulation
4 * Copyright (c) 2006 Fabrice Bellard
7 * 2010-Feb-14 Artyom Tarasenko : reworked irq generation
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:
16 * The above copyright notice and this permission notice shall be included in
17 * all copies or substantial portions of the Software.
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
28 #include "qemu/osdep.h"
30 #include "hw/sparc/sparc32_dma.h"
31 #include "hw/sparc/sun4m_iommu.h"
32 #include "hw/sysbus.h"
33 #include "sysemu/dma.h"
34 #include "qapi/error.h"
35 #include "qemu/module.h"
39 * This is the DMA controller part of chip STP2000 (Master I/O), also
40 * produced as NCR89C100. See
41 * http://www.ibiblio.org/pub/historic-linux/early-ports/Sparc/NCR/NCR89C100.txt
43 * http://www.ibiblio.org/pub/historic-linux/early-ports/Sparc/NCR/DMA2.txt
46 #define DMA_SIZE (4 * sizeof(uint32_t))
47 /* We need the mask, because one instance of the device is not page
48 aligned (ledma, start address 0x0010) */
49 #define DMA_MASK (DMA_SIZE - 1)
50 /* OBP says 0x20 bytes for ledma, the extras are aliased to espdma */
51 #define DMA_ETH_SIZE (8 * sizeof(uint32_t))
52 #define DMA_MAX_REG_OFFSET (2 * DMA_SIZE - 1)
54 #define DMA_VER 0xa0000000
56 #define DMA_INTREN 0x10
57 #define DMA_WRITE_MEM 0x100
59 #define DMA_LOADED 0x04000000
60 #define DMA_DRAIN_FIFO 0x40
61 #define DMA_RESET 0x80
63 /* XXX SCSI and ethernet should have different read-only bit masks */
64 #define DMA_CSR_RO_MASK 0xfe000007
71 /* Note: on sparc, the lance 16 bit bus is swapped */
72 void ledma_memory_read(void *opaque
, hwaddr addr
,
73 uint8_t *buf
, int len
, int do_bswap
)
75 DMADeviceState
*s
= opaque
;
76 IOMMUState
*is
= (IOMMUState
*)s
->iommu
;
79 addr
|= s
->dmaregs
[3];
80 trace_ledma_memory_read(addr
, len
);
82 dma_memory_read(&is
->iommu_as
, addr
, buf
, len
);
86 dma_memory_read(&is
->iommu_as
, addr
, buf
, len
);
87 for(i
= 0; i
< len
; i
+= 2) {
88 bswap16s((uint16_t *)(buf
+ i
));
93 void ledma_memory_write(void *opaque
, hwaddr addr
,
94 uint8_t *buf
, int len
, int do_bswap
)
96 DMADeviceState
*s
= opaque
;
97 IOMMUState
*is
= (IOMMUState
*)s
->iommu
;
101 addr
|= s
->dmaregs
[3];
102 trace_ledma_memory_write(addr
, len
);
104 dma_memory_write(&is
->iommu_as
, addr
, buf
, len
);
110 if (l
> sizeof(tmp_buf
))
112 for(i
= 0; i
< l
; i
+= 2) {
113 tmp_buf
[i
>> 1] = bswap16(*(uint16_t *)(buf
+ i
));
115 dma_memory_write(&is
->iommu_as
, addr
, tmp_buf
, l
);
123 static void dma_set_irq(void *opaque
, int irq
, int level
)
125 DMADeviceState
*s
= opaque
;
127 s
->dmaregs
[0] |= DMA_INTR
;
128 if (s
->dmaregs
[0] & DMA_INTREN
) {
129 trace_sparc32_dma_set_irq_raise();
130 qemu_irq_raise(s
->irq
);
133 if (s
->dmaregs
[0] & DMA_INTR
) {
134 s
->dmaregs
[0] &= ~DMA_INTR
;
135 if (s
->dmaregs
[0] & DMA_INTREN
) {
136 trace_sparc32_dma_set_irq_lower();
137 qemu_irq_lower(s
->irq
);
143 void espdma_memory_read(void *opaque
, uint8_t *buf
, int len
)
145 DMADeviceState
*s
= opaque
;
146 IOMMUState
*is
= (IOMMUState
*)s
->iommu
;
148 trace_espdma_memory_read(s
->dmaregs
[1], len
);
149 dma_memory_read(&is
->iommu_as
, s
->dmaregs
[1], buf
, len
);
150 s
->dmaregs
[1] += len
;
153 void espdma_memory_write(void *opaque
, uint8_t *buf
, int len
)
155 DMADeviceState
*s
= opaque
;
156 IOMMUState
*is
= (IOMMUState
*)s
->iommu
;
158 trace_espdma_memory_write(s
->dmaregs
[1], len
);
159 dma_memory_write(&is
->iommu_as
, s
->dmaregs
[1], buf
, len
);
160 s
->dmaregs
[1] += len
;
163 static uint64_t dma_mem_read(void *opaque
, hwaddr addr
,
166 DMADeviceState
*s
= opaque
;
169 saddr
= (addr
& DMA_MASK
) >> 2;
170 trace_sparc32_dma_mem_readl(addr
, s
->dmaregs
[saddr
]);
171 return s
->dmaregs
[saddr
];
174 static void dma_mem_write(void *opaque
, hwaddr addr
,
175 uint64_t val
, unsigned size
)
177 DMADeviceState
*s
= opaque
;
180 saddr
= (addr
& DMA_MASK
) >> 2;
181 trace_sparc32_dma_mem_writel(addr
, s
->dmaregs
[saddr
], val
);
184 if (val
& DMA_INTREN
) {
185 if (s
->dmaregs
[0] & DMA_INTR
) {
186 trace_sparc32_dma_set_irq_raise();
187 qemu_irq_raise(s
->irq
);
190 if (s
->dmaregs
[0] & (DMA_INTR
| DMA_INTREN
)) {
191 trace_sparc32_dma_set_irq_lower();
192 qemu_irq_lower(s
->irq
);
195 if (val
& DMA_RESET
) {
196 qemu_irq_raise(s
->gpio
[GPIO_RESET
]);
197 qemu_irq_lower(s
->gpio
[GPIO_RESET
]);
198 } else if (val
& DMA_DRAIN_FIFO
) {
199 val
&= ~DMA_DRAIN_FIFO
;
201 val
= DMA_DRAIN_FIFO
;
203 if (val
& DMA_EN
&& !(s
->dmaregs
[0] & DMA_EN
)) {
204 trace_sparc32_dma_enable_raise();
205 qemu_irq_raise(s
->gpio
[GPIO_DMA
]);
206 } else if (!(val
& DMA_EN
) && !!(s
->dmaregs
[0] & DMA_EN
)) {
207 trace_sparc32_dma_enable_lower();
208 qemu_irq_lower(s
->gpio
[GPIO_DMA
]);
211 val
&= ~DMA_CSR_RO_MASK
;
213 s
->dmaregs
[0] = (s
->dmaregs
[0] & DMA_CSR_RO_MASK
) | val
;
216 s
->dmaregs
[0] |= DMA_LOADED
;
219 s
->dmaregs
[saddr
] = val
;
224 static const MemoryRegionOps dma_mem_ops
= {
225 .read
= dma_mem_read
,
226 .write
= dma_mem_write
,
227 .endianness
= DEVICE_NATIVE_ENDIAN
,
229 .min_access_size
= 4,
230 .max_access_size
= 4,
234 static void sparc32_dma_device_reset(DeviceState
*d
)
236 DMADeviceState
*s
= SPARC32_DMA_DEVICE(d
);
238 memset(s
->dmaregs
, 0, DMA_SIZE
);
239 s
->dmaregs
[0] = DMA_VER
;
242 static const VMStateDescription vmstate_sparc32_dma_device
= {
243 .name
="sparc32_dma",
245 .minimum_version_id
= 2,
246 .fields
= (VMStateField
[]) {
247 VMSTATE_UINT32_ARRAY(dmaregs
, DMADeviceState
, DMA_REGS
),
248 VMSTATE_END_OF_LIST()
252 static void sparc32_dma_device_init(Object
*obj
)
254 DeviceState
*dev
= DEVICE(obj
);
255 DMADeviceState
*s
= SPARC32_DMA_DEVICE(obj
);
256 SysBusDevice
*sbd
= SYS_BUS_DEVICE(obj
);
258 sysbus_init_irq(sbd
, &s
->irq
);
260 sysbus_init_mmio(sbd
, &s
->iomem
);
262 object_property_add_link(OBJECT(dev
), "iommu", TYPE_SUN4M_IOMMU
,
263 (Object
**) &s
->iommu
,
264 qdev_prop_allow_set_link_before_realize
,
267 qdev_init_gpio_in(dev
, dma_set_irq
, 1);
268 qdev_init_gpio_out(dev
, s
->gpio
, 2);
271 static void sparc32_dma_device_class_init(ObjectClass
*klass
, void *data
)
273 DeviceClass
*dc
= DEVICE_CLASS(klass
);
275 dc
->reset
= sparc32_dma_device_reset
;
276 dc
->vmsd
= &vmstate_sparc32_dma_device
;
279 static const TypeInfo sparc32_dma_device_info
= {
280 .name
= TYPE_SPARC32_DMA_DEVICE
,
281 .parent
= TYPE_SYS_BUS_DEVICE
,
283 .instance_size
= sizeof(DMADeviceState
),
284 .instance_init
= sparc32_dma_device_init
,
285 .class_init
= sparc32_dma_device_class_init
,
288 static void sparc32_espdma_device_init(Object
*obj
)
290 DMADeviceState
*s
= SPARC32_DMA_DEVICE(obj
);
292 memory_region_init_io(&s
->iomem
, OBJECT(s
), &dma_mem_ops
, s
,
293 "espdma-mmio", DMA_SIZE
);
296 static void sparc32_espdma_device_realize(DeviceState
*dev
, Error
**errp
)
299 SysBusESPState
*sysbus
;
302 d
= qdev_create(NULL
, TYPE_ESP
);
303 object_property_add_child(OBJECT(dev
), "esp", OBJECT(d
), errp
);
304 sysbus
= ESP_STATE(d
);
306 esp
->dma_memory_read
= espdma_memory_read
;
307 esp
->dma_memory_write
= espdma_memory_write
;
308 esp
->dma_opaque
= SPARC32_DMA_DEVICE(dev
);
309 sysbus
->it_shift
= 2;
310 esp
->dma_enabled
= 1;
314 static void sparc32_espdma_device_class_init(ObjectClass
*klass
, void *data
)
316 DeviceClass
*dc
= DEVICE_CLASS(klass
);
318 dc
->realize
= sparc32_espdma_device_realize
;
321 static const TypeInfo sparc32_espdma_device_info
= {
322 .name
= TYPE_SPARC32_ESPDMA_DEVICE
,
323 .parent
= TYPE_SPARC32_DMA_DEVICE
,
324 .instance_size
= sizeof(ESPDMADeviceState
),
325 .instance_init
= sparc32_espdma_device_init
,
326 .class_init
= sparc32_espdma_device_class_init
,
329 static void sparc32_ledma_device_init(Object
*obj
)
331 DMADeviceState
*s
= SPARC32_DMA_DEVICE(obj
);
333 memory_region_init_io(&s
->iomem
, OBJECT(s
), &dma_mem_ops
, s
,
334 "ledma-mmio", DMA_SIZE
);
337 static void sparc32_ledma_device_realize(DeviceState
*dev
, Error
**errp
)
340 NICInfo
*nd
= &nd_table
[0];
342 qemu_check_nic_model(nd
, TYPE_LANCE
);
344 d
= qdev_create(NULL
, TYPE_LANCE
);
345 object_property_add_child(OBJECT(dev
), "lance", OBJECT(d
), errp
);
346 qdev_set_nic_properties(d
, nd
);
347 qdev_prop_set_ptr(d
, "dma", dev
);
351 static void sparc32_ledma_device_class_init(ObjectClass
*klass
, void *data
)
353 DeviceClass
*dc
= DEVICE_CLASS(klass
);
355 dc
->realize
= sparc32_ledma_device_realize
;
358 static const TypeInfo sparc32_ledma_device_info
= {
359 .name
= TYPE_SPARC32_LEDMA_DEVICE
,
360 .parent
= TYPE_SPARC32_DMA_DEVICE
,
361 .instance_size
= sizeof(LEDMADeviceState
),
362 .instance_init
= sparc32_ledma_device_init
,
363 .class_init
= sparc32_ledma_device_class_init
,
366 static void sparc32_dma_realize(DeviceState
*dev
, Error
**errp
)
368 SPARC32DMAState
*s
= SPARC32_DMA(dev
);
369 DeviceState
*espdma
, *esp
, *ledma
, *lance
;
373 iommu
= object_resolve_path_type("", TYPE_SUN4M_IOMMU
, NULL
);
375 error_setg(errp
, "unable to locate sun4m IOMMU device");
379 espdma
= qdev_create(NULL
, TYPE_SPARC32_ESPDMA_DEVICE
);
380 object_property_set_link(OBJECT(espdma
), iommu
, "iommu", errp
);
381 object_property_add_child(OBJECT(s
), "espdma", OBJECT(espdma
), errp
);
382 qdev_init_nofail(espdma
);
384 esp
= DEVICE(object_resolve_path_component(OBJECT(espdma
), "esp"));
385 sbd
= SYS_BUS_DEVICE(esp
);
386 sysbus_connect_irq(sbd
, 0, qdev_get_gpio_in(espdma
, 0));
387 qdev_connect_gpio_out(espdma
, 0, qdev_get_gpio_in(esp
, 0));
388 qdev_connect_gpio_out(espdma
, 1, qdev_get_gpio_in(esp
, 1));
390 sbd
= SYS_BUS_DEVICE(espdma
);
391 memory_region_add_subregion(&s
->dmamem
, 0x0,
392 sysbus_mmio_get_region(sbd
, 0));
394 ledma
= qdev_create(NULL
, TYPE_SPARC32_LEDMA_DEVICE
);
395 object_property_set_link(OBJECT(ledma
), iommu
, "iommu", errp
);
396 object_property_add_child(OBJECT(s
), "ledma", OBJECT(ledma
), errp
);
397 qdev_init_nofail(ledma
);
399 lance
= DEVICE(object_resolve_path_component(OBJECT(ledma
), "lance"));
400 sbd
= SYS_BUS_DEVICE(lance
);
401 sysbus_connect_irq(sbd
, 0, qdev_get_gpio_in(ledma
, 0));
402 qdev_connect_gpio_out(ledma
, 0, qdev_get_gpio_in(lance
, 0));
404 sbd
= SYS_BUS_DEVICE(ledma
);
405 memory_region_add_subregion(&s
->dmamem
, 0x10,
406 sysbus_mmio_get_region(sbd
, 0));
408 /* Add ledma alias to handle SunOS 5.7 - Solaris 9 invalid access bug */
409 memory_region_init_alias(&s
->ledma_alias
, OBJECT(dev
), "ledma-alias",
410 sysbus_mmio_get_region(sbd
, 0), 0x4, 0x4);
411 memory_region_add_subregion(&s
->dmamem
, 0x20, &s
->ledma_alias
);
414 static void sparc32_dma_init(Object
*obj
)
416 SPARC32DMAState
*s
= SPARC32_DMA(obj
);
417 SysBusDevice
*sbd
= SYS_BUS_DEVICE(obj
);
419 memory_region_init(&s
->dmamem
, OBJECT(s
), "dma", DMA_SIZE
+ DMA_ETH_SIZE
);
420 sysbus_init_mmio(sbd
, &s
->dmamem
);
423 static void sparc32_dma_class_init(ObjectClass
*klass
, void *data
)
425 DeviceClass
*dc
= DEVICE_CLASS(klass
);
427 dc
->realize
= sparc32_dma_realize
;
430 static const TypeInfo sparc32_dma_info
= {
431 .name
= TYPE_SPARC32_DMA
,
432 .parent
= TYPE_SYS_BUS_DEVICE
,
433 .instance_size
= sizeof(SPARC32DMAState
),
434 .instance_init
= sparc32_dma_init
,
435 .class_init
= sparc32_dma_class_init
,
439 static void sparc32_dma_register_types(void)
441 type_register_static(&sparc32_dma_device_info
);
442 type_register_static(&sparc32_espdma_device_info
);
443 type_register_static(&sparc32_ledma_device_info
);
444 type_register_static(&sparc32_dma_info
);
447 type_init(sparc32_dma_register_types
)