2006-09-03 16:09:07 +00:00
|
|
|
/*
|
|
|
|
* QEMU Sparc32 DMA controller emulation
|
|
|
|
*
|
|
|
|
* Copyright (c) 2006 Fabrice Bellard
|
|
|
|
*
|
2010-02-15 17:39:50 +00:00
|
|
|
* Modifications:
|
|
|
|
* 2010-Feb-14 Artyom Tarasenko : reworked irq generation
|
|
|
|
*
|
2006-09-03 16:09:07 +00:00
|
|
|
* 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
|
|
|
|
2013-02-04 14:40:22 +00:00
|
|
|
#include "hw/hw.h"
|
2013-02-05 16:06:20 +00:00
|
|
|
#include "hw/sparc/sparc32_dma.h"
|
|
|
|
#include "hw/sparc/sun4m.h"
|
2013-02-04 14:40:22 +00:00
|
|
|
#include "hw/sysbus.h"
|
2010-10-31 09:24:14 +00:00
|
|
|
#include "trace.h"
|
2006-09-03 16:09:07 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* 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
|
|
|
|
*/
|
|
|
|
|
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)
|
2010-12-20 17:55:33 +00:00
|
|
|
/* OBP says 0x20 bytes for ledma, the extras are aliased to espdma */
|
2010-12-18 17:09:04 +00:00
|
|
|
#define DMA_ETH_SIZE (8 * sizeof(uint32_t))
|
|
|
|
#define DMA_MAX_REG_OFFSET (2 * 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
|
2010-09-11 16:38:33 +00:00
|
|
|
#define DMA_EN 0x200
|
2006-09-03 16:09:07 +00:00
|
|
|
#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
|
|
|
|
|
2010-05-22 08:38:56 +00:00
|
|
|
/* XXX SCSI and ethernet should have different read-only bit masks */
|
|
|
|
#define DMA_CSR_RO_MASK 0xfe000007
|
|
|
|
|
2013-07-26 14:55:59 +00:00
|
|
|
#define TYPE_SPARC32_DMA "sparc32_dma"
|
|
|
|
#define SPARC32_DMA(obj) OBJECT_CHECK(DMAState, (obj), TYPE_SPARC32_DMA)
|
|
|
|
|
2006-09-03 16:09:07 +00:00
|
|
|
typedef struct DMAState DMAState;
|
|
|
|
|
|
|
|
struct DMAState {
|
2013-07-26 14:55:59 +00:00
|
|
|
SysBusDevice parent_obj;
|
|
|
|
|
2011-11-14 09:55:27 +00:00
|
|
|
MemoryRegion iomem;
|
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;
|
2010-09-11 16:38:33 +00:00
|
|
|
qemu_irq gpio[2];
|
2010-12-18 17:09:04 +00:00
|
|
|
uint32_t is_ledma;
|
2010-09-11 16:38:33 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
enum {
|
|
|
|
GPIO_RESET = 0,
|
|
|
|
GPIO_DMA,
|
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 */
|
2012-10-23 10:30:10 +00:00
|
|
|
void ledma_memory_read(void *opaque, hwaddr 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
|
|
|
|
2007-05-26 17:39:43 +00:00
|
|
|
addr |= s->dmaregs[3];
|
2010-10-31 09:24:14 +00:00
|
|
|
trace_ledma_memory_read(addr);
|
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
|
|
|
}
|
|
|
|
|
2012-10-23 10:30:10 +00:00
|
|
|
void ledma_memory_write(void *opaque, hwaddr 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
|
|
|
|
2007-05-26 17:39:43 +00:00
|
|
|
addr |= s->dmaregs[3];
|
2010-10-31 09:24:14 +00:00
|
|
|
trace_ledma_memory_write(addr);
|
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) {
|
|
|
|
s->dmaregs[0] |= DMA_INTR;
|
2010-02-15 17:39:50 +00:00
|
|
|
if (s->dmaregs[0] & DMA_INTREN) {
|
2010-10-31 09:24:14 +00:00
|
|
|
trace_sparc32_dma_set_irq_raise();
|
2010-02-15 17:39:50 +00:00
|
|
|
qemu_irq_raise(s->irq);
|
|
|
|
}
|
2007-05-27 16:36:10 +00:00
|
|
|
} else {
|
2010-02-15 17:39:50 +00:00
|
|
|
if (s->dmaregs[0] & DMA_INTR) {
|
|
|
|
s->dmaregs[0] &= ~DMA_INTR;
|
|
|
|
if (s->dmaregs[0] & DMA_INTREN) {
|
2010-10-31 09:24:14 +00:00
|
|
|
trace_sparc32_dma_set_irq_lower();
|
2010-02-15 17:39:50 +00:00
|
|
|
qemu_irq_lower(s->irq);
|
|
|
|
}
|
|
|
|
}
|
2007-05-27 16:36:10 +00:00
|
|
|
}
|
2006-09-03 16:09:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void espdma_memory_read(void *opaque, uint8_t *buf, int len)
|
|
|
|
{
|
|
|
|
DMAState *s = opaque;
|
|
|
|
|
2010-10-31 09:24:14 +00:00
|
|
|
trace_espdma_memory_read(s->dmaregs[1]);
|
2006-09-03 16:09:07 +00:00
|
|
|
sparc_iommu_memory_read(s->iommu, s->dmaregs[1], buf, len);
|
|
|
|
s->dmaregs[1] += len;
|
|
|
|
}
|
|
|
|
|
|
|
|
void espdma_memory_write(void *opaque, uint8_t *buf, int len)
|
|
|
|
{
|
|
|
|
DMAState *s = opaque;
|
|
|
|
|
2010-10-31 09:24:14 +00:00
|
|
|
trace_espdma_memory_write(s->dmaregs[1]);
|
2006-09-03 16:09:07 +00:00
|
|
|
sparc_iommu_memory_write(s->iommu, s->dmaregs[1], buf, len);
|
|
|
|
s->dmaregs[1] += len;
|
|
|
|
}
|
|
|
|
|
2012-10-23 10:30:10 +00:00
|
|
|
static uint64_t dma_mem_read(void *opaque, hwaddr addr,
|
2011-11-14 09:55:27 +00:00
|
|
|
unsigned size)
|
2006-09-03 16:09:07 +00:00
|
|
|
{
|
|
|
|
DMAState *s = opaque;
|
|
|
|
uint32_t saddr;
|
|
|
|
|
2010-12-18 17:09:04 +00:00
|
|
|
if (s->is_ledma && (addr > DMA_MAX_REG_OFFSET)) {
|
2010-12-20 17:55:33 +00:00
|
|
|
/* aliased to espdma, but we can't get there from here */
|
|
|
|
/* buggy driver if using undocumented behavior, just return 0 */
|
|
|
|
trace_sparc32_dma_mem_readl(addr, 0);
|
|
|
|
return 0;
|
2010-12-18 17:09:04 +00:00
|
|
|
}
|
2008-12-15 20:24:25 +00:00
|
|
|
saddr = (addr & DMA_MASK) >> 2;
|
2010-10-31 09:24:14 +00:00
|
|
|
trace_sparc32_dma_mem_readl(addr, s->dmaregs[saddr]);
|
2006-09-03 16:09:07 +00:00
|
|
|
return s->dmaregs[saddr];
|
|
|
|
}
|
|
|
|
|
2012-10-23 10:30:10 +00:00
|
|
|
static void dma_mem_write(void *opaque, hwaddr addr,
|
2011-11-14 09:55:27 +00:00
|
|
|
uint64_t val, unsigned size)
|
2006-09-03 16:09:07 +00:00
|
|
|
{
|
|
|
|
DMAState *s = opaque;
|
|
|
|
uint32_t saddr;
|
|
|
|
|
2010-12-18 17:09:04 +00:00
|
|
|
if (s->is_ledma && (addr > DMA_MAX_REG_OFFSET)) {
|
2010-12-20 17:55:33 +00:00
|
|
|
/* aliased to espdma, but we can't get there from here */
|
|
|
|
trace_sparc32_dma_mem_writel(addr, 0, val);
|
|
|
|
return;
|
2010-12-18 17:09:04 +00:00
|
|
|
}
|
2008-12-15 20:24:25 +00:00
|
|
|
saddr = (addr & DMA_MASK) >> 2;
|
2010-10-31 09:24:14 +00:00
|
|
|
trace_sparc32_dma_mem_writel(addr, s->dmaregs[saddr], val);
|
2006-09-03 16:09:07 +00:00
|
|
|
switch (saddr) {
|
|
|
|
case 0:
|
2010-02-15 17:39:50 +00:00
|
|
|
if (val & DMA_INTREN) {
|
2010-05-22 08:38:56 +00:00
|
|
|
if (s->dmaregs[0] & DMA_INTR) {
|
2010-10-31 09:24:14 +00:00
|
|
|
trace_sparc32_dma_set_irq_raise();
|
2010-02-15 17:39:50 +00:00
|
|
|
qemu_irq_raise(s->irq);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (s->dmaregs[0] & (DMA_INTR | DMA_INTREN)) {
|
2010-10-31 09:24:14 +00:00
|
|
|
trace_sparc32_dma_set_irq_lower();
|
2010-02-15 17:39:50 +00:00
|
|
|
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) {
|
2010-09-11 16:38:33 +00:00
|
|
|
qemu_irq_raise(s->gpio[GPIO_RESET]);
|
|
|
|
qemu_irq_lower(s->gpio[GPIO_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;
|
2010-09-11 16:38:33 +00:00
|
|
|
|
|
|
|
if (val & DMA_EN && !(s->dmaregs[0] & DMA_EN)) {
|
2010-10-31 09:24:14 +00:00
|
|
|
trace_sparc32_dma_enable_raise();
|
2010-09-11 16:38:33 +00:00
|
|
|
qemu_irq_raise(s->gpio[GPIO_DMA]);
|
|
|
|
} else if (!(val & DMA_EN) && !!(s->dmaregs[0] & DMA_EN)) {
|
2010-10-31 09:24:14 +00:00
|
|
|
trace_sparc32_dma_enable_lower();
|
2010-09-11 16:38:33 +00:00
|
|
|
qemu_irq_lower(s->gpio[GPIO_DMA]);
|
|
|
|
}
|
|
|
|
|
2010-05-22 08:38:56 +00:00
|
|
|
val &= ~DMA_CSR_RO_MASK;
|
2006-09-03 16:09:07 +00:00
|
|
|
val |= DMA_VER;
|
2010-05-22 08:38:56 +00:00
|
|
|
s->dmaregs[0] = (s->dmaregs[0] & DMA_CSR_RO_MASK) | val;
|
2006-09-03 16:09:07 +00:00
|
|
|
break;
|
|
|
|
case 1:
|
|
|
|
s->dmaregs[0] |= DMA_LOADED;
|
2010-05-22 08:38:56 +00:00
|
|
|
/* fall through */
|
2006-09-03 16:09:07 +00:00
|
|
|
default:
|
2010-05-22 08:38:56 +00:00
|
|
|
s->dmaregs[saddr] = val;
|
2006-09-03 16:09:07 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-11-14 09:55:27 +00:00
|
|
|
static const MemoryRegionOps dma_mem_ops = {
|
|
|
|
.read = dma_mem_read,
|
|
|
|
.write = dma_mem_write,
|
|
|
|
.endianness = DEVICE_NATIVE_ENDIAN,
|
|
|
|
.valid = {
|
|
|
|
.min_access_size = 4,
|
|
|
|
.max_access_size = 4,
|
|
|
|
},
|
2006-09-03 16:09:07 +00:00
|
|
|
};
|
|
|
|
|
2009-10-24 19:35:32 +00:00
|
|
|
static void dma_reset(DeviceState *d)
|
2006-09-03 16:09:07 +00:00
|
|
|
{
|
2013-07-26 14:55:59 +00:00
|
|
|
DMAState *s = SPARC32_DMA(d);
|
2006-09-03 16:09:07 +00:00
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2009-08-28 20:46:15 +00:00
|
|
|
static const VMStateDescription vmstate_dma = {
|
|
|
|
.name ="sparc32_dma",
|
|
|
|
.version_id = 2,
|
|
|
|
.minimum_version_id = 2,
|
2014-04-16 14:01:33 +00:00
|
|
|
.fields = (VMStateField[]) {
|
2009-08-28 20:46:15 +00:00
|
|
|
VMSTATE_UINT32_ARRAY(dmaregs, DMAState, DMA_REGS),
|
|
|
|
VMSTATE_END_OF_LIST()
|
|
|
|
}
|
|
|
|
};
|
2006-09-03 16:09:07 +00:00
|
|
|
|
2013-07-26 14:55:59 +00:00
|
|
|
static int sparc32_dma_init1(SysBusDevice *sbd)
|
2009-07-15 20:45:19 +00:00
|
|
|
{
|
2013-07-26 14:55:59 +00:00
|
|
|
DeviceState *dev = DEVICE(sbd);
|
|
|
|
DMAState *s = SPARC32_DMA(dev);
|
2010-12-18 17:09:04 +00:00
|
|
|
int reg_size;
|
2006-09-03 16:09:07 +00:00
|
|
|
|
2013-07-26 14:55:59 +00:00
|
|
|
sysbus_init_irq(sbd, &s->irq);
|
2006-09-03 16:09:07 +00:00
|
|
|
|
2010-12-18 17:09:04 +00:00
|
|
|
reg_size = s->is_ledma ? DMA_ETH_SIZE : DMA_SIZE;
|
2013-06-07 01:25:08 +00:00
|
|
|
memory_region_init_io(&s->iomem, OBJECT(s), &dma_mem_ops, s,
|
|
|
|
"dma", reg_size);
|
2013-07-26 14:55:59 +00:00
|
|
|
sysbus_init_mmio(sbd, &s->iomem);
|
2006-09-03 16:09:07 +00:00
|
|
|
|
2013-07-26 14:55:59 +00:00
|
|
|
qdev_init_gpio_in(dev, dma_set_irq, 1);
|
|
|
|
qdev_init_gpio_out(dev, s->gpio, 2);
|
2009-10-24 19:35:32 +00:00
|
|
|
|
2009-08-14 08:36:05 +00:00
|
|
|
return 0;
|
2009-07-15 20:45:19 +00:00
|
|
|
}
|
2006-09-03 16:09:07 +00:00
|
|
|
|
2012-01-24 19:12:29 +00:00
|
|
|
static Property sparc32_dma_properties[] = {
|
|
|
|
DEFINE_PROP_PTR("iommu_opaque", DMAState, iommu),
|
|
|
|
DEFINE_PROP_UINT32("is_ledma", DMAState, is_ledma, 0),
|
|
|
|
DEFINE_PROP_END_OF_LIST(),
|
|
|
|
};
|
|
|
|
|
|
|
|
static void sparc32_dma_class_init(ObjectClass *klass, void *data)
|
|
|
|
{
|
2011-12-08 03:34:16 +00:00
|
|
|
DeviceClass *dc = DEVICE_CLASS(klass);
|
2012-01-24 19:12:29 +00:00
|
|
|
SysBusDeviceClass *k = SYS_BUS_DEVICE_CLASS(klass);
|
|
|
|
|
|
|
|
k->init = sparc32_dma_init1;
|
2011-12-08 03:34:16 +00:00
|
|
|
dc->reset = dma_reset;
|
|
|
|
dc->vmsd = &vmstate_dma;
|
|
|
|
dc->props = sparc32_dma_properties;
|
2013-11-29 09:43:44 +00:00
|
|
|
/* Reason: pointer property "iommu_opaque" */
|
|
|
|
dc->cannot_instantiate_with_device_add_yet = true;
|
2012-01-24 19:12:29 +00:00
|
|
|
}
|
|
|
|
|
2013-01-10 15:19:07 +00:00
|
|
|
static const TypeInfo sparc32_dma_info = {
|
2013-07-26 14:55:59 +00:00
|
|
|
.name = TYPE_SPARC32_DMA,
|
2011-12-08 03:34:16 +00:00
|
|
|
.parent = TYPE_SYS_BUS_DEVICE,
|
|
|
|
.instance_size = sizeof(DMAState),
|
|
|
|
.class_init = sparc32_dma_class_init,
|
2009-07-15 20:45:19 +00:00
|
|
|
};
|
|
|
|
|
2012-02-09 14:20:55 +00:00
|
|
|
static void sparc32_dma_register_types(void)
|
2009-07-15 20:45:19 +00:00
|
|
|
{
|
2011-12-08 03:34:16 +00:00
|
|
|
type_register_static(&sparc32_dma_info);
|
2006-09-03 16:09:07 +00:00
|
|
|
}
|
2009-07-15 20:45:19 +00:00
|
|
|
|
2012-02-09 14:20:55 +00:00
|
|
|
type_init(sparc32_dma_register_types)
|