Statistics
| Branch: | Revision:

root / hw / sparc32_dma.c @ 9f6f0423

History | View | Annotate | Download (7.4 kB)

1 67e999be bellard
/*
2 67e999be bellard
 * QEMU Sparc32 DMA controller emulation
3 67e999be bellard
 *
4 67e999be bellard
 * Copyright (c) 2006 Fabrice Bellard
5 67e999be bellard
 *
6 67e999be bellard
 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 67e999be bellard
 * of this software and associated documentation files (the "Software"), to deal
8 67e999be bellard
 * in the Software without restriction, including without limitation the rights
9 67e999be bellard
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 67e999be bellard
 * copies of the Software, and to permit persons to whom the Software is
11 67e999be bellard
 * furnished to do so, subject to the following conditions:
12 67e999be bellard
 *
13 67e999be bellard
 * The above copyright notice and this permission notice shall be included in
14 67e999be bellard
 * all copies or substantial portions of the Software.
15 67e999be bellard
 *
16 67e999be bellard
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 67e999be bellard
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 67e999be bellard
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 67e999be bellard
 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 67e999be bellard
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 67e999be bellard
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 67e999be bellard
 * THE SOFTWARE.
23 67e999be bellard
 */
24 6f6260c7 Blue Swirl
25 87ecb68b pbrook
#include "hw.h"
26 87ecb68b pbrook
#include "sparc32_dma.h"
27 87ecb68b pbrook
#include "sun4m.h"
28 6f6260c7 Blue Swirl
#include "sysbus.h"
29 67e999be bellard
30 67e999be bellard
/* debug DMA */
31 67e999be bellard
//#define DEBUG_DMA
32 67e999be bellard
33 67e999be bellard
/*
34 67e999be bellard
 * This is the DMA controller part of chip STP2000 (Master I/O), also
35 67e999be bellard
 * produced as NCR89C100. See
36 67e999be bellard
 * http://www.ibiblio.org/pub/historic-linux/early-ports/Sparc/NCR/NCR89C100.txt
37 67e999be bellard
 * and
38 67e999be bellard
 * http://www.ibiblio.org/pub/historic-linux/early-ports/Sparc/NCR/DMA2.txt
39 67e999be bellard
 */
40 67e999be bellard
41 67e999be bellard
#ifdef DEBUG_DMA
42 001faf32 Blue Swirl
#define DPRINTF(fmt, ...)                               \
43 001faf32 Blue Swirl
    do { printf("DMA: " fmt , ## __VA_ARGS__); } while (0)
44 67e999be bellard
#else
45 001faf32 Blue Swirl
#define DPRINTF(fmt, ...)
46 67e999be bellard
#endif
47 67e999be bellard
48 5aca8c3b blueswir1
#define DMA_REGS 4
49 5aca8c3b blueswir1
#define DMA_SIZE (4 * sizeof(uint32_t))
50 09723aa1 blueswir1
/* We need the mask, because one instance of the device is not page
51 09723aa1 blueswir1
   aligned (ledma, start address 0x0010) */
52 09723aa1 blueswir1
#define DMA_MASK (DMA_SIZE - 1)
53 67e999be bellard
54 67e999be bellard
#define DMA_VER 0xa0000000
55 67e999be bellard
#define DMA_INTR 1
56 67e999be bellard
#define DMA_INTREN 0x10
57 67e999be bellard
#define DMA_WRITE_MEM 0x100
58 67e999be bellard
#define DMA_LOADED 0x04000000
59 5aca8c3b blueswir1
#define DMA_DRAIN_FIFO 0x40
60 67e999be bellard
#define DMA_RESET 0x80
61 67e999be bellard
62 67e999be bellard
typedef struct DMAState DMAState;
63 67e999be bellard
64 67e999be bellard
struct DMAState {
65 6f6260c7 Blue Swirl
    SysBusDevice busdev;
66 67e999be bellard
    uint32_t dmaregs[DMA_REGS];
67 5aca8c3b blueswir1
    qemu_irq irq;
68 2d069bab blueswir1
    void *iommu;
69 2d069bab blueswir1
    qemu_irq dev_reset;
70 67e999be bellard
};
71 67e999be bellard
72 9b94dc32 bellard
/* Note: on sparc, the lance 16 bit bus is swapped */
73 c227f099 Anthony Liguori
void ledma_memory_read(void *opaque, target_phys_addr_t addr,
74 9b94dc32 bellard
                       uint8_t *buf, int len, int do_bswap)
75 67e999be bellard
{
76 67e999be bellard
    DMAState *s = opaque;
77 9b94dc32 bellard
    int i;
78 67e999be bellard
79 67e999be bellard
    DPRINTF("DMA write, direction: %c, addr 0x%8.8x\n",
80 67e999be bellard
            s->dmaregs[0] & DMA_WRITE_MEM ? 'w': 'r', s->dmaregs[1]);
81 5aca8c3b blueswir1
    addr |= s->dmaregs[3];
82 9b94dc32 bellard
    if (do_bswap) {
83 9b94dc32 bellard
        sparc_iommu_memory_read(s->iommu, addr, buf, len);
84 9b94dc32 bellard
    } else {
85 9b94dc32 bellard
        addr &= ~1;
86 9b94dc32 bellard
        len &= ~1;
87 9b94dc32 bellard
        sparc_iommu_memory_read(s->iommu, addr, buf, len);
88 9b94dc32 bellard
        for(i = 0; i < len; i += 2) {
89 9b94dc32 bellard
            bswap16s((uint16_t *)(buf + i));
90 9b94dc32 bellard
        }
91 9b94dc32 bellard
    }
92 67e999be bellard
}
93 67e999be bellard
94 c227f099 Anthony Liguori
void ledma_memory_write(void *opaque, target_phys_addr_t addr,
95 9b94dc32 bellard
                        uint8_t *buf, int len, int do_bswap)
96 67e999be bellard
{
97 67e999be bellard
    DMAState *s = opaque;
98 9b94dc32 bellard
    int l, i;
99 9b94dc32 bellard
    uint16_t tmp_buf[32];
100 67e999be bellard
101 67e999be bellard
    DPRINTF("DMA read, direction: %c, addr 0x%8.8x\n",
102 67e999be bellard
            s->dmaregs[0] & DMA_WRITE_MEM ? 'w': 'r', s->dmaregs[1]);
103 5aca8c3b blueswir1
    addr |= s->dmaregs[3];
104 9b94dc32 bellard
    if (do_bswap) {
105 9b94dc32 bellard
        sparc_iommu_memory_write(s->iommu, addr, buf, len);
106 9b94dc32 bellard
    } else {
107 9b94dc32 bellard
        addr &= ~1;
108 9b94dc32 bellard
        len &= ~1;
109 9b94dc32 bellard
        while (len > 0) {
110 9b94dc32 bellard
            l = len;
111 9b94dc32 bellard
            if (l > sizeof(tmp_buf))
112 9b94dc32 bellard
                l = sizeof(tmp_buf);
113 9b94dc32 bellard
            for(i = 0; i < l; i += 2) {
114 9b94dc32 bellard
                tmp_buf[i >> 1] = bswap16(*(uint16_t *)(buf + i));
115 9b94dc32 bellard
            }
116 9b94dc32 bellard
            sparc_iommu_memory_write(s->iommu, addr, (uint8_t *)tmp_buf, l);
117 9b94dc32 bellard
            len -= l;
118 9b94dc32 bellard
            buf += l;
119 9b94dc32 bellard
            addr += l;
120 9b94dc32 bellard
        }
121 9b94dc32 bellard
    }
122 67e999be bellard
}
123 67e999be bellard
124 70c0de96 blueswir1
static void dma_set_irq(void *opaque, int irq, int level)
125 67e999be bellard
{
126 67e999be bellard
    DMAState *s = opaque;
127 70c0de96 blueswir1
    if (level) {
128 9b5207aa blueswir1
        DPRINTF("Raise IRQ\n");
129 70c0de96 blueswir1
        s->dmaregs[0] |= DMA_INTR;
130 70c0de96 blueswir1
        qemu_irq_raise(s->irq);
131 70c0de96 blueswir1
    } else {
132 70c0de96 blueswir1
        s->dmaregs[0] &= ~DMA_INTR;
133 9b5207aa blueswir1
        DPRINTF("Lower IRQ\n");
134 70c0de96 blueswir1
        qemu_irq_lower(s->irq);
135 70c0de96 blueswir1
    }
136 67e999be bellard
}
137 67e999be bellard
138 67e999be bellard
void espdma_memory_read(void *opaque, uint8_t *buf, int len)
139 67e999be bellard
{
140 67e999be bellard
    DMAState *s = opaque;
141 67e999be bellard
142 67e999be bellard
    DPRINTF("DMA read, direction: %c, addr 0x%8.8x\n",
143 67e999be bellard
            s->dmaregs[0] & DMA_WRITE_MEM ? 'w': 'r', s->dmaregs[1]);
144 67e999be bellard
    sparc_iommu_memory_read(s->iommu, s->dmaregs[1], buf, len);
145 787cfbc4 Blue Swirl
    DPRINTF("Raise IRQ\n");
146 67e999be bellard
    s->dmaregs[0] |= DMA_INTR;
147 67e999be bellard
    s->dmaregs[1] += len;
148 67e999be bellard
}
149 67e999be bellard
150 67e999be bellard
void espdma_memory_write(void *opaque, uint8_t *buf, int len)
151 67e999be bellard
{
152 67e999be bellard
    DMAState *s = opaque;
153 67e999be bellard
154 67e999be bellard
    DPRINTF("DMA write, direction: %c, addr 0x%8.8x\n",
155 67e999be bellard
            s->dmaregs[0] & DMA_WRITE_MEM ? 'w': 'r', s->dmaregs[1]);
156 67e999be bellard
    sparc_iommu_memory_write(s->iommu, s->dmaregs[1], buf, len);
157 787cfbc4 Blue Swirl
    DPRINTF("Raise IRQ\n");
158 67e999be bellard
    s->dmaregs[0] |= DMA_INTR;
159 67e999be bellard
    s->dmaregs[1] += len;
160 67e999be bellard
}
161 67e999be bellard
162 c227f099 Anthony Liguori
static uint32_t dma_mem_readl(void *opaque, target_phys_addr_t addr)
163 67e999be bellard
{
164 67e999be bellard
    DMAState *s = opaque;
165 67e999be bellard
    uint32_t saddr;
166 67e999be bellard
167 09723aa1 blueswir1
    saddr = (addr & DMA_MASK) >> 2;
168 5aca8c3b blueswir1
    DPRINTF("read dmareg " TARGET_FMT_plx ": 0x%8.8x\n", addr,
169 5aca8c3b blueswir1
            s->dmaregs[saddr]);
170 67e999be bellard
171 67e999be bellard
    return s->dmaregs[saddr];
172 67e999be bellard
}
173 67e999be bellard
174 c227f099 Anthony Liguori
static void dma_mem_writel(void *opaque, target_phys_addr_t addr, uint32_t val)
175 67e999be bellard
{
176 67e999be bellard
    DMAState *s = opaque;
177 67e999be bellard
    uint32_t saddr;
178 67e999be bellard
179 09723aa1 blueswir1
    saddr = (addr & DMA_MASK) >> 2;
180 5aca8c3b blueswir1
    DPRINTF("write dmareg " TARGET_FMT_plx ": 0x%8.8x -> 0x%8.8x\n", addr,
181 5aca8c3b blueswir1
            s->dmaregs[saddr], val);
182 67e999be bellard
    switch (saddr) {
183 67e999be bellard
    case 0:
184 d537cf6c pbrook
        if (!(val & DMA_INTREN)) {
185 5aca8c3b blueswir1
            DPRINTF("Lower IRQ\n");
186 5aca8c3b blueswir1
            qemu_irq_lower(s->irq);
187 d537cf6c pbrook
        }
188 67e999be bellard
        if (val & DMA_RESET) {
189 2d069bab blueswir1
            qemu_irq_raise(s->dev_reset);
190 2d069bab blueswir1
            qemu_irq_lower(s->dev_reset);
191 5aca8c3b blueswir1
        } else if (val & DMA_DRAIN_FIFO) {
192 5aca8c3b blueswir1
            val &= ~DMA_DRAIN_FIFO;
193 67e999be bellard
        } else if (val == 0)
194 5aca8c3b blueswir1
            val = DMA_DRAIN_FIFO;
195 67e999be bellard
        val &= 0x0fffffff;
196 67e999be bellard
        val |= DMA_VER;
197 67e999be bellard
        break;
198 67e999be bellard
    case 1:
199 67e999be bellard
        s->dmaregs[0] |= DMA_LOADED;
200 67e999be bellard
        break;
201 67e999be bellard
    default:
202 67e999be bellard
        break;
203 67e999be bellard
    }
204 67e999be bellard
    s->dmaregs[saddr] = val;
205 67e999be bellard
}
206 67e999be bellard
207 d60efc6b Blue Swirl
static CPUReadMemoryFunc * const dma_mem_read[3] = {
208 7c560456 blueswir1
    NULL,
209 7c560456 blueswir1
    NULL,
210 67e999be bellard
    dma_mem_readl,
211 67e999be bellard
};
212 67e999be bellard
213 d60efc6b Blue Swirl
static CPUWriteMemoryFunc * const dma_mem_write[3] = {
214 7c560456 blueswir1
    NULL,
215 7c560456 blueswir1
    NULL,
216 67e999be bellard
    dma_mem_writel,
217 67e999be bellard
};
218 67e999be bellard
219 49ef6c90 Blue Swirl
static void dma_reset(DeviceState *d)
220 67e999be bellard
{
221 49ef6c90 Blue Swirl
    DMAState *s = container_of(d, DMAState, busdev.qdev);
222 67e999be bellard
223 5aca8c3b blueswir1
    memset(s->dmaregs, 0, DMA_SIZE);
224 67e999be bellard
    s->dmaregs[0] = DMA_VER;
225 67e999be bellard
}
226 67e999be bellard
227 75c497dc Blue Swirl
static const VMStateDescription vmstate_dma = {
228 75c497dc Blue Swirl
    .name ="sparc32_dma",
229 75c497dc Blue Swirl
    .version_id = 2,
230 75c497dc Blue Swirl
    .minimum_version_id = 2,
231 75c497dc Blue Swirl
    .minimum_version_id_old = 2,
232 75c497dc Blue Swirl
    .fields      = (VMStateField []) {
233 75c497dc Blue Swirl
        VMSTATE_UINT32_ARRAY(dmaregs, DMAState, DMA_REGS),
234 75c497dc Blue Swirl
        VMSTATE_END_OF_LIST()
235 75c497dc Blue Swirl
    }
236 75c497dc Blue Swirl
};
237 67e999be bellard
238 81a322d4 Gerd Hoffmann
static int sparc32_dma_init1(SysBusDevice *dev)
239 6f6260c7 Blue Swirl
{
240 6f6260c7 Blue Swirl
    DMAState *s = FROM_SYSBUS(DMAState, dev);
241 6f6260c7 Blue Swirl
    int dma_io_memory;
242 67e999be bellard
243 6f6260c7 Blue Swirl
    sysbus_init_irq(dev, &s->irq);
244 67e999be bellard
245 1eed09cb Avi Kivity
    dma_io_memory = cpu_register_io_memory(dma_mem_read, dma_mem_write, s);
246 6f6260c7 Blue Swirl
    sysbus_init_mmio(dev, DMA_SIZE, dma_io_memory);
247 67e999be bellard
248 6f6260c7 Blue Swirl
    qdev_init_gpio_in(&dev->qdev, dma_set_irq, 1);
249 74ff8d90 Blue Swirl
    qdev_init_gpio_out(&dev->qdev, &s->dev_reset, 1);
250 49ef6c90 Blue Swirl
251 81a322d4 Gerd Hoffmann
    return 0;
252 6f6260c7 Blue Swirl
}
253 67e999be bellard
254 6f6260c7 Blue Swirl
static SysBusDeviceInfo sparc32_dma_info = {
255 6f6260c7 Blue Swirl
    .init = sparc32_dma_init1,
256 6f6260c7 Blue Swirl
    .qdev.name  = "sparc32_dma",
257 6f6260c7 Blue Swirl
    .qdev.size  = sizeof(DMAState),
258 49ef6c90 Blue Swirl
    .qdev.vmsd  = &vmstate_dma,
259 49ef6c90 Blue Swirl
    .qdev.reset = dma_reset,
260 ee6847d1 Gerd Hoffmann
    .qdev.props = (Property[]) {
261 3180d772 Gerd Hoffmann
        DEFINE_PROP_PTR("iommu_opaque", DMAState, iommu),
262 3180d772 Gerd Hoffmann
        DEFINE_PROP_END_OF_LIST(),
263 6f6260c7 Blue Swirl
    }
264 6f6260c7 Blue Swirl
};
265 6f6260c7 Blue Swirl
266 6f6260c7 Blue Swirl
static void sparc32_dma_register_devices(void)
267 6f6260c7 Blue Swirl
{
268 6f6260c7 Blue Swirl
    sysbus_register_withprop(&sparc32_dma_info);
269 67e999be bellard
}
270 6f6260c7 Blue Swirl
271 6f6260c7 Blue Swirl
device_init(sparc32_dma_register_devices)