2006-09-03 16:09:07 +00:00
|
|
|
/*
|
|
|
|
* QEMU Sparc32 DMA controller emulation
|
|
|
|
*
|
|
|
|
* Copyright (c) 2006 Fabrice Bellard
|
|
|
|
*
|
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
* of this software and associated documentation files (the "Software"), to deal
|
|
|
|
* in the Software without restriction, including without limitation the rights
|
|
|
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
* copies of the Software, and to permit persons to whom the Software is
|
|
|
|
* furnished to do so, subject to the following conditions:
|
|
|
|
*
|
|
|
|
* The above copyright notice and this permission notice shall be included in
|
|
|
|
* all copies or substantial portions of the Software.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
|
|
|
|
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
|
|
* THE SOFTWARE.
|
|
|
|
*/
|
2009-07-15 20:45:19 +00:00
|
|
|
|
2007-11-17 17:14:51 +00:00
|
|
|
#include "hw.h"
|
|
|
|
#include "sparc32_dma.h"
|
|
|
|
#include "sun4m.h"
|
2009-07-15 20:45:19 +00:00
|
|
|
#include "sysbus.h"
|
2006-09-03 16:09:07 +00:00
|
|
|
|
|
|
|
/* debug DMA */
|
|
|
|
//#define DEBUG_DMA
|
|
|
|
|
|
|
|
/*
|
|
|
|
* This is the DMA controller part of chip STP2000 (Master I/O), also
|
|
|
|
* produced as NCR89C100. See
|
|
|
|
* http://www.ibiblio.org/pub/historic-linux/early-ports/Sparc/NCR/NCR89C100.txt
|
|
|
|
* and
|
|
|
|
* http://www.ibiblio.org/pub/historic-linux/early-ports/Sparc/NCR/DMA2.txt
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifdef DEBUG_DMA
|
2009-05-13 17:53:17 +00:00
|
|
|
#define DPRINTF(fmt, ...) \
|
|
|
|
do { printf("DMA: " fmt , ## __VA_ARGS__); } while (0)
|
2006-09-03 16:09:07 +00:00
|
|
|
#else
|
2009-05-13 17:53:17 +00:00
|
|
|
#define DPRINTF(fmt, ...)
|
2006-09-03 16:09:07 +00:00
|
|
|
#endif
|
|
|
|
|
2007-05-26 17:39:43 +00:00
|
|
|
#define DMA_REGS 4
|
|
|
|
#define DMA_SIZE (4 * sizeof(uint32_t))
|
2008-12-15 20:24:25 +00:00
|
|
|
/* We need the mask, because one instance of the device is not page
|
|
|
|
aligned (ledma, start address 0x0010) */
|
|
|
|
#define DMA_MASK (DMA_SIZE - 1)
|
2006-09-03 16:09:07 +00:00
|
|
|
|
|
|
|
#define DMA_VER 0xa0000000
|
|
|
|
#define DMA_INTR 1
|
|
|
|
#define DMA_INTREN 0x10
|
|
|
|
#define DMA_WRITE_MEM 0x100
|
|
|
|
#define DMA_LOADED 0x04000000
|
2007-05-26 17:39:43 +00:00
|
|
|
#define DMA_DRAIN_FIFO 0x40
|
2006-09-03 16:09:07 +00:00
|
|
|
#define DMA_RESET 0x80
|
|
|
|
|
|
|
|
typedef struct DMAState DMAState;
|
|
|
|
|
|
|
|
struct DMAState {
|
2009-07-15 20:45:19 +00:00
|
|
|
SysBusDevice busdev;
|
2006-09-03 16:09:07 +00:00
|
|
|
uint32_t dmaregs[DMA_REGS];
|
2007-05-26 17:39:43 +00:00
|
|
|
qemu_irq irq;
|
2007-08-16 19:56:27 +00:00
|
|
|
void *iommu;
|
|
|
|
qemu_irq dev_reset;
|
2006-09-03 16:09:07 +00:00
|
|
|
};
|
|
|
|
|
2006-09-03 19:48:17 +00:00
|
|
|
/* Note: on sparc, the lance 16 bit bus is swapped */
|
2007-09-16 21:08:06 +00:00
|
|
|
void ledma_memory_read(void *opaque, target_phys_addr_t addr,
|
2006-09-03 19:48:17 +00:00
|
|
|
uint8_t *buf, int len, int do_bswap)
|
2006-09-03 16:09:07 +00:00
|
|
|
{
|
|
|
|
DMAState *s = opaque;
|
2006-09-03 19:48:17 +00:00
|
|
|
int i;
|
2006-09-03 16:09:07 +00:00
|
|
|
|
|
|
|
DPRINTF("DMA write, direction: %c, addr 0x%8.8x\n",
|
|
|
|
s->dmaregs[0] & DMA_WRITE_MEM ? 'w': 'r', s->dmaregs[1]);
|
2007-05-26 17:39:43 +00:00
|
|
|
addr |= s->dmaregs[3];
|
2006-09-03 19:48:17 +00:00
|
|
|
if (do_bswap) {
|
|
|
|
sparc_iommu_memory_read(s->iommu, addr, buf, len);
|
|
|
|
} else {
|
|
|
|
addr &= ~1;
|
|
|
|
len &= ~1;
|
|
|
|
sparc_iommu_memory_read(s->iommu, addr, buf, len);
|
|
|
|
for(i = 0; i < len; i += 2) {
|
|
|
|
bswap16s((uint16_t *)(buf + i));
|
|
|
|
}
|
|
|
|
}
|
2006-09-03 16:09:07 +00:00
|
|
|
}
|
|
|
|
|
2007-09-16 21:08:06 +00:00
|
|
|
void ledma_memory_write(void *opaque, target_phys_addr_t addr,
|
2006-09-03 19:48:17 +00:00
|
|
|
uint8_t *buf, int len, int do_bswap)
|
2006-09-03 16:09:07 +00:00
|
|
|
{
|
|
|
|
DMAState *s = opaque;
|
2006-09-03 19:48:17 +00:00
|
|
|
int l, i;
|
|
|
|
uint16_t tmp_buf[32];
|
2006-09-03 16:09:07 +00:00
|
|
|
|
|
|
|
DPRINTF("DMA read, direction: %c, addr 0x%8.8x\n",
|
|
|
|
s->dmaregs[0] & DMA_WRITE_MEM ? 'w': 'r', s->dmaregs[1]);
|
2007-05-26 17:39:43 +00:00
|
|
|
addr |= s->dmaregs[3];
|
2006-09-03 19:48:17 +00:00
|
|
|
if (do_bswap) {
|
|
|
|
sparc_iommu_memory_write(s->iommu, addr, buf, len);
|
|
|
|
} else {
|
|
|
|
addr &= ~1;
|
|
|
|
len &= ~1;
|
|
|
|
while (len > 0) {
|
|
|
|
l = len;
|
|
|
|
if (l > sizeof(tmp_buf))
|
|
|
|
l = sizeof(tmp_buf);
|
|
|
|
for(i = 0; i < l; i += 2) {
|
|
|
|
tmp_buf[i >> 1] = bswap16(*(uint16_t *)(buf + i));
|
|
|
|
}
|
|
|
|
sparc_iommu_memory_write(s->iommu, addr, (uint8_t *)tmp_buf, l);
|
|
|
|
len -= l;
|
|
|
|
buf += l;
|
|
|
|
addr += l;
|
|
|
|
}
|
|
|
|
}
|
2006-09-03 16:09:07 +00:00
|
|
|
}
|
|
|
|
|
2007-05-27 16:36:10 +00:00
|
|
|
static void dma_set_irq(void *opaque, int irq, int level)
|
2006-09-03 16:09:07 +00:00
|
|
|
{
|
|
|
|
DMAState *s = opaque;
|
2007-05-27 16:36:10 +00:00
|
|
|
if (level) {
|
2007-08-16 19:57:27 +00:00
|
|
|
DPRINTF("Raise IRQ\n");
|
2007-05-27 16:36:10 +00:00
|
|
|
s->dmaregs[0] |= DMA_INTR;
|
|
|
|
qemu_irq_raise(s->irq);
|
|
|
|
} else {
|
|
|
|
s->dmaregs[0] &= ~DMA_INTR;
|
2007-08-16 19:57:27 +00:00
|
|
|
DPRINTF("Lower IRQ\n");
|
2007-05-27 16:36:10 +00:00
|
|
|
qemu_irq_lower(s->irq);
|
|
|
|
}
|
2006-09-03 16:09:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void espdma_memory_read(void *opaque, uint8_t *buf, int len)
|
|
|
|
{
|
|
|
|
DMAState *s = opaque;
|
|
|
|
|
|
|
|
DPRINTF("DMA read, direction: %c, addr 0x%8.8x\n",
|
|
|
|
s->dmaregs[0] & DMA_WRITE_MEM ? 'w': 'r', s->dmaregs[1]);
|
|
|
|
sparc_iommu_memory_read(s->iommu, s->dmaregs[1], buf, len);
|
|
|
|
s->dmaregs[0] |= DMA_INTR;
|
|
|
|
s->dmaregs[1] += len;
|
|
|
|
}
|
|
|
|
|
|
|
|
void espdma_memory_write(void *opaque, uint8_t *buf, int len)
|
|
|
|
{
|
|
|
|
DMAState *s = opaque;
|
|
|
|
|
|
|
|
DPRINTF("DMA write, direction: %c, addr 0x%8.8x\n",
|
|
|
|
s->dmaregs[0] & DMA_WRITE_MEM ? 'w': 'r', s->dmaregs[1]);
|
|
|
|
sparc_iommu_memory_write(s->iommu, s->dmaregs[1], buf, len);
|
|
|
|
s->dmaregs[0] |= DMA_INTR;
|
|
|
|
s->dmaregs[1] += len;
|
|
|
|
}
|
|
|
|
|
|
|
|
static uint32_t dma_mem_readl(void *opaque, target_phys_addr_t addr)
|
|
|
|
{
|
|
|
|
DMAState *s = opaque;
|
|
|
|
uint32_t saddr;
|
|
|
|
|
2008-12-15 20:24:25 +00:00
|
|
|
saddr = (addr & DMA_MASK) >> 2;
|
2007-05-26 17:39:43 +00:00
|
|
|
DPRINTF("read dmareg " TARGET_FMT_plx ": 0x%8.8x\n", addr,
|
|
|
|
s->dmaregs[saddr]);
|
2006-09-03 16:09:07 +00:00
|
|
|
|
|
|
|
return s->dmaregs[saddr];
|
|
|
|
}
|
|
|
|
|
|
|
|
static void dma_mem_writel(void *opaque, target_phys_addr_t addr, uint32_t val)
|
|
|
|
{
|
|
|
|
DMAState *s = opaque;
|
|
|
|
uint32_t saddr;
|
|
|
|
|
2008-12-15 20:24:25 +00:00
|
|
|
saddr = (addr & DMA_MASK) >> 2;
|
2007-05-26 17:39:43 +00:00
|
|
|
DPRINTF("write dmareg " TARGET_FMT_plx ": 0x%8.8x -> 0x%8.8x\n", addr,
|
|
|
|
s->dmaregs[saddr], val);
|
2006-09-03 16:09:07 +00:00
|
|
|
switch (saddr) {
|
|
|
|
case 0:
|
2007-04-07 18:14:41 +00:00
|
|
|
if (!(val & DMA_INTREN)) {
|
2007-05-26 17:39:43 +00:00
|
|
|
DPRINTF("Lower IRQ\n");
|
|
|
|
qemu_irq_lower(s->irq);
|
2007-04-07 18:14:41 +00:00
|
|
|
}
|
2006-09-03 16:09:07 +00:00
|
|
|
if (val & DMA_RESET) {
|
2007-08-16 19:56:27 +00:00
|
|
|
qemu_irq_raise(s->dev_reset);
|
|
|
|
qemu_irq_lower(s->dev_reset);
|
2007-05-26 17:39:43 +00:00
|
|
|
} else if (val & DMA_DRAIN_FIFO) {
|
|
|
|
val &= ~DMA_DRAIN_FIFO;
|
2006-09-03 16:09:07 +00:00
|
|
|
} else if (val == 0)
|
2007-05-26 17:39:43 +00:00
|
|
|
val = DMA_DRAIN_FIFO;
|
2006-09-03 16:09:07 +00:00
|
|
|
val &= 0x0fffffff;
|
|
|
|
val |= DMA_VER;
|
|
|
|
break;
|
|
|
|
case 1:
|
|
|
|
s->dmaregs[0] |= DMA_LOADED;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
s->dmaregs[saddr] = val;
|
|
|
|
}
|
|
|
|
|
|
|
|
static CPUReadMemoryFunc *dma_mem_read[3] = {
|
2008-01-01 17:06:38 +00:00
|
|
|
NULL,
|
|
|
|
NULL,
|
2006-09-03 16:09:07 +00:00
|
|
|
dma_mem_readl,
|
|
|
|
};
|
|
|
|
|
|
|
|
static CPUWriteMemoryFunc *dma_mem_write[3] = {
|
2008-01-01 17:06:38 +00:00
|
|
|
NULL,
|
|
|
|
NULL,
|
2006-09-03 16:09:07 +00:00
|
|
|
dma_mem_writel,
|
|
|
|
};
|
|
|
|
|
|
|
|
static void dma_reset(void *opaque)
|
|
|
|
{
|
|
|
|
DMAState *s = opaque;
|
|
|
|
|
2007-05-26 17:39:43 +00:00
|
|
|
memset(s->dmaregs, 0, DMA_SIZE);
|
2006-09-03 16:09:07 +00:00
|
|
|
s->dmaregs[0] = DMA_VER;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void dma_save(QEMUFile *f, void *opaque)
|
|
|
|
{
|
|
|
|
DMAState *s = opaque;
|
|
|
|
unsigned int i;
|
|
|
|
|
|
|
|
for (i = 0; i < DMA_REGS; i++)
|
|
|
|
qemu_put_be32s(f, &s->dmaregs[i]);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int dma_load(QEMUFile *f, void *opaque, int version_id)
|
|
|
|
{
|
|
|
|
DMAState *s = opaque;
|
|
|
|
unsigned int i;
|
|
|
|
|
2007-05-26 17:39:43 +00:00
|
|
|
if (version_id != 2)
|
2006-09-03 16:09:07 +00:00
|
|
|
return -EINVAL;
|
|
|
|
for (i = 0; i < DMA_REGS; i++)
|
|
|
|
qemu_get_be32s(f, &s->dmaregs[i]);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2007-05-27 16:36:10 +00:00
|
|
|
void *sparc32_dma_init(target_phys_addr_t daddr, qemu_irq parent_irq,
|
2009-07-15 20:45:19 +00:00
|
|
|
void *iommu, qemu_irq *dev_irq, qemu_irq **reset)
|
2006-09-03 16:09:07 +00:00
|
|
|
{
|
2009-07-15 20:45:19 +00:00
|
|
|
DeviceState *dev;
|
|
|
|
SysBusDevice *s;
|
|
|
|
DMAState *d;
|
|
|
|
|
|
|
|
dev = qdev_create(NULL, "sparc32_dma");
|
|
|
|
qdev_set_prop_ptr(dev, "iommu_opaque", iommu);
|
|
|
|
qdev_init(dev);
|
|
|
|
s = sysbus_from_qdev(dev);
|
|
|
|
sysbus_connect_irq(s, 0, parent_irq);
|
|
|
|
*dev_irq = qdev_get_gpio_in(dev, 0);
|
|
|
|
sysbus_mmio_map(s, 0, daddr);
|
|
|
|
|
|
|
|
d = FROM_SYSBUS(DMAState, s);
|
|
|
|
*reset = &d->dev_reset;
|
|
|
|
|
|
|
|
return d;
|
|
|
|
}
|
2006-09-03 16:09:07 +00:00
|
|
|
|
2009-07-15 20:45:19 +00:00
|
|
|
static void sparc32_dma_init1(SysBusDevice *dev)
|
|
|
|
{
|
|
|
|
DMAState *s = FROM_SYSBUS(DMAState, dev);
|
|
|
|
int dma_io_memory;
|
2006-09-03 16:09:07 +00:00
|
|
|
|
2009-07-15 20:45:19 +00:00
|
|
|
sysbus_init_irq(dev, &s->irq);
|
|
|
|
s->iommu = qdev_get_prop_ptr(&dev->qdev, "iommu_opaque");
|
2006-09-03 16:09:07 +00:00
|
|
|
|
2009-06-14 08:38:51 +00:00
|
|
|
dma_io_memory = cpu_register_io_memory(dma_mem_read, dma_mem_write, s);
|
2009-07-15 20:45:19 +00:00
|
|
|
sysbus_init_mmio(dev, DMA_SIZE, dma_io_memory);
|
2006-09-03 16:09:07 +00:00
|
|
|
|
2009-07-15 20:45:19 +00:00
|
|
|
register_savevm("sparc32_dma", -1, 2, dma_save, dma_load, s);
|
2009-06-27 07:25:07 +00:00
|
|
|
qemu_register_reset(dma_reset, s);
|
2006-09-03 16:09:07 +00:00
|
|
|
|
2009-07-15 20:45:19 +00:00
|
|
|
qdev_init_gpio_in(&dev->qdev, dma_set_irq, 1);
|
|
|
|
}
|
2006-09-03 16:09:07 +00:00
|
|
|
|
2009-07-15 20:45:19 +00:00
|
|
|
static SysBusDeviceInfo sparc32_dma_info = {
|
|
|
|
.init = sparc32_dma_init1,
|
|
|
|
.qdev.name = "sparc32_dma",
|
|
|
|
.qdev.size = sizeof(DMAState),
|
|
|
|
.qdev.props = (DevicePropList[]) {
|
|
|
|
{.name = "iommu_opaque", .type = PROP_TYPE_PTR},
|
|
|
|
{.name = NULL}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
static void sparc32_dma_register_devices(void)
|
|
|
|
{
|
|
|
|
sysbus_register_withprop(&sparc32_dma_info);
|
2006-09-03 16:09:07 +00:00
|
|
|
}
|
2009-07-15 20:45:19 +00:00
|
|
|
|
|
|
|
device_init(sparc32_dma_register_devices)
|