sparc32_dma.c 7.5 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
/*
 * 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.
 */
24

P
pbrook 已提交
25 26 27
#include "hw.h"
#include "sparc32_dma.h"
#include "sun4m.h"
28
#include "sysbus.h"
29 30 31 32 33 34 35 36 37 38 39 40 41

/* 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
42 43
#define DPRINTF(fmt, ...)                               \
    do { printf("DMA: " fmt , ## __VA_ARGS__); } while (0)
44
#else
45
#define DPRINTF(fmt, ...)
46 47
#endif

B
blueswir1 已提交
48 49
#define DMA_REGS 4
#define DMA_SIZE (4 * sizeof(uint32_t))
B
blueswir1 已提交
50 51 52
/* We need the mask, because one instance of the device is not page
   aligned (ledma, start address 0x0010) */
#define DMA_MASK (DMA_SIZE - 1)
53 54 55 56 57 58

#define DMA_VER 0xa0000000
#define DMA_INTR 1
#define DMA_INTREN 0x10
#define DMA_WRITE_MEM 0x100
#define DMA_LOADED 0x04000000
B
blueswir1 已提交
59
#define DMA_DRAIN_FIFO 0x40
60 61 62 63 64
#define DMA_RESET 0x80

typedef struct DMAState DMAState;

struct DMAState {
65
    SysBusDevice busdev;
66
    uint32_t dmaregs[DMA_REGS];
B
blueswir1 已提交
67
    qemu_irq irq;
68 69
    void *iommu;
    qemu_irq dev_reset;
70 71
};

B
bellard 已提交
72
/* Note: on sparc, the lance 16 bit bus is swapped */
73
void ledma_memory_read(void *opaque, target_phys_addr_t addr,
B
bellard 已提交
74
                       uint8_t *buf, int len, int do_bswap)
75 76
{
    DMAState *s = opaque;
B
bellard 已提交
77
    int i;
78 79 80

    DPRINTF("DMA write, direction: %c, addr 0x%8.8x\n",
            s->dmaregs[0] & DMA_WRITE_MEM ? 'w': 'r', s->dmaregs[1]);
B
blueswir1 已提交
81
    addr |= s->dmaregs[3];
B
bellard 已提交
82 83 84 85 86 87 88 89 90 91
    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));
        }
    }
92 93
}

94
void ledma_memory_write(void *opaque, target_phys_addr_t addr,
B
bellard 已提交
95
                        uint8_t *buf, int len, int do_bswap)
96 97
{
    DMAState *s = opaque;
B
bellard 已提交
98 99
    int l, i;
    uint16_t tmp_buf[32];
100 101 102

    DPRINTF("DMA read, direction: %c, addr 0x%8.8x\n",
            s->dmaregs[0] & DMA_WRITE_MEM ? 'w': 'r', s->dmaregs[1]);
B
blueswir1 已提交
103
    addr |= s->dmaregs[3];
B
bellard 已提交
104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121
    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;
        }
    }
122 123
}

124
static void dma_set_irq(void *opaque, int irq, int level)
125 126
{
    DMAState *s = opaque;
127
    if (level) {
B
blueswir1 已提交
128
        DPRINTF("Raise IRQ\n");
129 130 131 132
        s->dmaregs[0] |= DMA_INTR;
        qemu_irq_raise(s->irq);
    } else {
        s->dmaregs[0] &= ~DMA_INTR;
B
blueswir1 已提交
133
        DPRINTF("Lower IRQ\n");
134 135
        qemu_irq_lower(s->irq);
    }
136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164
}

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;

B
blueswir1 已提交
165
    saddr = (addr & DMA_MASK) >> 2;
B
blueswir1 已提交
166 167
    DPRINTF("read dmareg " TARGET_FMT_plx ": 0x%8.8x\n", addr,
            s->dmaregs[saddr]);
168 169 170 171 172 173 174 175 176

    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;

B
blueswir1 已提交
177
    saddr = (addr & DMA_MASK) >> 2;
B
blueswir1 已提交
178 179
    DPRINTF("write dmareg " TARGET_FMT_plx ": 0x%8.8x -> 0x%8.8x\n", addr,
            s->dmaregs[saddr], val);
180 181
    switch (saddr) {
    case 0:
P
pbrook 已提交
182
        if (!(val & DMA_INTREN)) {
B
blueswir1 已提交
183 184
            DPRINTF("Lower IRQ\n");
            qemu_irq_lower(s->irq);
P
pbrook 已提交
185
        }
186
        if (val & DMA_RESET) {
187 188
            qemu_irq_raise(s->dev_reset);
            qemu_irq_lower(s->dev_reset);
B
blueswir1 已提交
189 190
        } else if (val & DMA_DRAIN_FIFO) {
            val &= ~DMA_DRAIN_FIFO;
191
        } else if (val == 0)
B
blueswir1 已提交
192
            val = DMA_DRAIN_FIFO;
193 194 195 196 197 198 199 200 201 202 203 204 205
        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] = {
206 207
    NULL,
    NULL,
208 209 210 211
    dma_mem_readl,
};

static CPUWriteMemoryFunc *dma_mem_write[3] = {
212 213
    NULL,
    NULL,
214 215 216 217 218 219 220
    dma_mem_writel,
};

static void dma_reset(void *opaque)
{
    DMAState *s = opaque;

B
blueswir1 已提交
221
    memset(s->dmaregs, 0, DMA_SIZE);
222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238
    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;

B
blueswir1 已提交
239
    if (version_id != 2)
240 241 242 243 244 245 246
        return -EINVAL;
    for (i = 0; i < DMA_REGS; i++)
        qemu_get_be32s(f, &s->dmaregs[i]);

    return 0;
}

247 248 249 250
static void sparc32_dma_init1(SysBusDevice *dev)
{
    DMAState *s = FROM_SYSBUS(DMAState, dev);
    int dma_io_memory;
251

252
    sysbus_init_irq(dev, &s->irq);
253

254
    dma_io_memory = cpu_register_io_memory(dma_mem_read, dma_mem_write, s);
255
    sysbus_init_mmio(dev, DMA_SIZE, dma_io_memory);
256

257
    register_savevm("sparc32_dma", -1, 2, dma_save, dma_load, s);
258
    qemu_register_reset(dma_reset, s);
259

260
    qdev_init_gpio_in(&dev->qdev, dma_set_irq, 1);
261
    qdev_init_gpio_out(&dev->qdev, &s->dev_reset, 1);
262
}
263

264 265 266 267
static SysBusDeviceInfo sparc32_dma_info = {
    .init = sparc32_dma_init1,
    .qdev.name  = "sparc32_dma",
    .qdev.size  = sizeof(DMAState),
G
Gerd Hoffmann 已提交
268
    .qdev.props = (Property[]) {
269 270
        DEFINE_PROP_PTR("iommu_opaque", DMAState, iommu),
        DEFINE_PROP_END_OF_LIST(),
271 272 273 274 275 276
    }
};

static void sparc32_dma_register_devices(void)
{
    sysbus_register_withprop(&sparc32_dma_info);
277
}
278 279

device_init(sparc32_dma_register_devices)