Statistics
| Branch: | Revision:

root / hw / rc4030.c @ 0ed8b6f6

History | View | Annotate | Download (21.2 kB)

1 4ce7ff6e aurel32
/*
2 4ce7ff6e aurel32
 * QEMU JAZZ RC4030 chipset
3 4ce7ff6e aurel32
 *
4 9ea0b7a1 aurel32
 * Copyright (c) 2007-2009 Herve Poussineau
5 4ce7ff6e aurel32
 *
6 4ce7ff6e aurel32
 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 4ce7ff6e aurel32
 * of this software and associated documentation files (the "Software"), to deal
8 4ce7ff6e aurel32
 * in the Software without restriction, including without limitation the rights
9 4ce7ff6e aurel32
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 4ce7ff6e aurel32
 * copies of the Software, and to permit persons to whom the Software is
11 4ce7ff6e aurel32
 * furnished to do so, subject to the following conditions:
12 4ce7ff6e aurel32
 *
13 4ce7ff6e aurel32
 * The above copyright notice and this permission notice shall be included in
14 4ce7ff6e aurel32
 * all copies or substantial portions of the Software.
15 4ce7ff6e aurel32
 *
16 4ce7ff6e aurel32
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 4ce7ff6e aurel32
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 4ce7ff6e aurel32
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 4ce7ff6e aurel32
 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 4ce7ff6e aurel32
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 4ce7ff6e aurel32
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 4ce7ff6e aurel32
 * THE SOFTWARE.
23 4ce7ff6e aurel32
 */
24 4ce7ff6e aurel32
25 4ce7ff6e aurel32
#include "hw.h"
26 cd5158ea aurel32
#include "mips.h"
27 4ce7ff6e aurel32
#include "qemu-timer.h"
28 4ce7ff6e aurel32
29 c6945b15 aurel32
/********************************************************/
30 c6945b15 aurel32
/* debug rc4030 */
31 c6945b15 aurel32
32 4ce7ff6e aurel32
//#define DEBUG_RC4030
33 c6945b15 aurel32
//#define DEBUG_RC4030_DMA
34 4ce7ff6e aurel32
35 4ce7ff6e aurel32
#ifdef DEBUG_RC4030
36 001faf32 Blue Swirl
#define DPRINTF(fmt, ...) \
37 001faf32 Blue Swirl
do { printf("rc4030: " fmt , ## __VA_ARGS__); } while (0)
38 4ce7ff6e aurel32
static const char* irq_names[] = { "parallel", "floppy", "sound", "video",
39 4ce7ff6e aurel32
            "network", "scsi", "keyboard", "mouse", "serial0", "serial1" };
40 c6945b15 aurel32
#else
41 001faf32 Blue Swirl
#define DPRINTF(fmt, ...)
42 4ce7ff6e aurel32
#endif
43 4ce7ff6e aurel32
44 001faf32 Blue Swirl
#define RC4030_ERROR(fmt, ...) \
45 001faf32 Blue Swirl
do { fprintf(stderr, "rc4030 ERROR: %s: " fmt, __func__ , ## __VA_ARGS__); } while (0)
46 c6945b15 aurel32
47 c6945b15 aurel32
/********************************************************/
48 c6945b15 aurel32
/* rc4030 emulation                                     */
49 c6945b15 aurel32
50 c6945b15 aurel32
typedef struct dma_pagetable_entry {
51 c6945b15 aurel32
    int32_t frame;
52 c6945b15 aurel32
    int32_t owner;
53 541dc0d4 Stefan Weil
} QEMU_PACKED dma_pagetable_entry;
54 c6945b15 aurel32
55 c6945b15 aurel32
#define DMA_PAGESIZE    4096
56 c6945b15 aurel32
#define DMA_REG_ENABLE  1
57 c6945b15 aurel32
#define DMA_REG_COUNT   2
58 c6945b15 aurel32
#define DMA_REG_ADDRESS 3
59 c6945b15 aurel32
60 c6945b15 aurel32
#define DMA_FLAG_ENABLE     0x0001
61 c6945b15 aurel32
#define DMA_FLAG_MEM_TO_DEV 0x0002
62 c6945b15 aurel32
#define DMA_FLAG_TC_INTR    0x0100
63 c6945b15 aurel32
#define DMA_FLAG_MEM_INTR   0x0200
64 c6945b15 aurel32
#define DMA_FLAG_ADDR_INTR  0x0400
65 c6945b15 aurel32
66 4ce7ff6e aurel32
typedef struct rc4030State
67 4ce7ff6e aurel32
{
68 4ce7ff6e aurel32
    uint32_t config; /* 0x0000: RC4030 config register */
69 9ea0b7a1 aurel32
    uint32_t revision; /* 0x0008: RC4030 Revision register */
70 4ce7ff6e aurel32
    uint32_t invalid_address_register; /* 0x0010: Invalid Address register */
71 4ce7ff6e aurel32
72 4ce7ff6e aurel32
    /* DMA */
73 4ce7ff6e aurel32
    uint32_t dma_regs[8][4];
74 4ce7ff6e aurel32
    uint32_t dma_tl_base; /* 0x0018: DMA transl. table base */
75 4ce7ff6e aurel32
    uint32_t dma_tl_limit; /* 0x0020: DMA transl. table limit */
76 4ce7ff6e aurel32
77 4ce7ff6e aurel32
    /* cache */
78 9ea0b7a1 aurel32
    uint32_t cache_maint; /* 0x0030: Cache Maintenance */
79 4ce7ff6e aurel32
    uint32_t remote_failed_address; /* 0x0038: Remote Failed Address */
80 4ce7ff6e aurel32
    uint32_t memory_failed_address; /* 0x0040: Memory Failed Address */
81 4ce7ff6e aurel32
    uint32_t cache_ptag; /* 0x0048: I/O Cache Physical Tag */
82 4ce7ff6e aurel32
    uint32_t cache_ltag; /* 0x0050: I/O Cache Logical Tag */
83 4ce7ff6e aurel32
    uint32_t cache_bmask; /* 0x0058: I/O Cache Byte Mask */
84 4ce7ff6e aurel32
85 9ea0b7a1 aurel32
    uint32_t nmi_interrupt; /* 0x0200: interrupt source */
86 4ce7ff6e aurel32
    uint32_t offset210;
87 4ce7ff6e aurel32
    uint32_t nvram_protect; /* 0x0220: NV ram protect register */
88 9ea0b7a1 aurel32
    uint32_t rem_speed[16];
89 4ce7ff6e aurel32
    uint32_t imr_jazz; /* Local bus int enable mask */
90 4ce7ff6e aurel32
    uint32_t isr_jazz; /* Local bus int source */
91 4ce7ff6e aurel32
92 4ce7ff6e aurel32
    /* timer */
93 4ce7ff6e aurel32
    QEMUTimer *periodic_timer;
94 4ce7ff6e aurel32
    uint32_t itr; /* Interval timer reload */
95 4ce7ff6e aurel32
96 4ce7ff6e aurel32
    qemu_irq timer_irq;
97 4ce7ff6e aurel32
    qemu_irq jazz_bus_irq;
98 3054434d Avi Kivity
99 3054434d Avi Kivity
    MemoryRegion iomem_chipset;
100 3054434d Avi Kivity
    MemoryRegion iomem_jazzio;
101 4ce7ff6e aurel32
} rc4030State;
102 4ce7ff6e aurel32
103 4ce7ff6e aurel32
static void set_next_tick(rc4030State *s)
104 4ce7ff6e aurel32
{
105 4ce7ff6e aurel32
    qemu_irq_lower(s->timer_irq);
106 b0f74c87 balrog
    uint32_t tm_hz;
107 4ce7ff6e aurel32
108 b0f74c87 balrog
    tm_hz = 1000 / (s->itr + 1);
109 4ce7ff6e aurel32
110 74475455 Paolo Bonzini
    qemu_mod_timer(s->periodic_timer, qemu_get_clock_ns(vm_clock) +
111 6ee093c9 Juan Quintela
                   get_ticks_per_sec() / tm_hz);
112 4ce7ff6e aurel32
}
113 4ce7ff6e aurel32
114 4ce7ff6e aurel32
/* called for accesses to rc4030 */
115 c227f099 Anthony Liguori
static uint32_t rc4030_readl(void *opaque, target_phys_addr_t addr)
116 4ce7ff6e aurel32
{
117 4ce7ff6e aurel32
    rc4030State *s = opaque;
118 4ce7ff6e aurel32
    uint32_t val;
119 4ce7ff6e aurel32
120 4ce7ff6e aurel32
    addr &= 0x3fff;
121 4ce7ff6e aurel32
    switch (addr & ~0x3) {
122 4ce7ff6e aurel32
    /* Global config register */
123 4ce7ff6e aurel32
    case 0x0000:
124 4ce7ff6e aurel32
        val = s->config;
125 4ce7ff6e aurel32
        break;
126 9ea0b7a1 aurel32
    /* Revision register */
127 9ea0b7a1 aurel32
    case 0x0008:
128 9ea0b7a1 aurel32
        val = s->revision;
129 9ea0b7a1 aurel32
        break;
130 4ce7ff6e aurel32
    /* Invalid Address register */
131 4ce7ff6e aurel32
    case 0x0010:
132 4ce7ff6e aurel32
        val = s->invalid_address_register;
133 4ce7ff6e aurel32
        break;
134 4ce7ff6e aurel32
    /* DMA transl. table base */
135 4ce7ff6e aurel32
    case 0x0018:
136 4ce7ff6e aurel32
        val = s->dma_tl_base;
137 4ce7ff6e aurel32
        break;
138 4ce7ff6e aurel32
    /* DMA transl. table limit */
139 4ce7ff6e aurel32
    case 0x0020:
140 4ce7ff6e aurel32
        val = s->dma_tl_limit;
141 4ce7ff6e aurel32
        break;
142 4ce7ff6e aurel32
    /* Remote Failed Address */
143 4ce7ff6e aurel32
    case 0x0038:
144 4ce7ff6e aurel32
        val = s->remote_failed_address;
145 4ce7ff6e aurel32
        break;
146 4ce7ff6e aurel32
    /* Memory Failed Address */
147 4ce7ff6e aurel32
    case 0x0040:
148 4ce7ff6e aurel32
        val = s->memory_failed_address;
149 4ce7ff6e aurel32
        break;
150 4ce7ff6e aurel32
    /* I/O Cache Byte Mask */
151 4ce7ff6e aurel32
    case 0x0058:
152 4ce7ff6e aurel32
        val = s->cache_bmask;
153 4ce7ff6e aurel32
        /* HACK */
154 4ce7ff6e aurel32
        if (s->cache_bmask == (uint32_t)-1)
155 4ce7ff6e aurel32
            s->cache_bmask = 0;
156 4ce7ff6e aurel32
        break;
157 4ce7ff6e aurel32
    /* Remote Speed Registers */
158 4ce7ff6e aurel32
    case 0x0070:
159 4ce7ff6e aurel32
    case 0x0078:
160 4ce7ff6e aurel32
    case 0x0080:
161 4ce7ff6e aurel32
    case 0x0088:
162 4ce7ff6e aurel32
    case 0x0090:
163 4ce7ff6e aurel32
    case 0x0098:
164 4ce7ff6e aurel32
    case 0x00a0:
165 4ce7ff6e aurel32
    case 0x00a8:
166 4ce7ff6e aurel32
    case 0x00b0:
167 4ce7ff6e aurel32
    case 0x00b8:
168 4ce7ff6e aurel32
    case 0x00c0:
169 4ce7ff6e aurel32
    case 0x00c8:
170 4ce7ff6e aurel32
    case 0x00d0:
171 4ce7ff6e aurel32
    case 0x00d8:
172 4ce7ff6e aurel32
    case 0x00e0:
173 9ea0b7a1 aurel32
    case 0x00e8:
174 4ce7ff6e aurel32
        val = s->rem_speed[(addr - 0x0070) >> 3];
175 4ce7ff6e aurel32
        break;
176 4ce7ff6e aurel32
    /* DMA channel base address */
177 4ce7ff6e aurel32
    case 0x0100:
178 4ce7ff6e aurel32
    case 0x0108:
179 4ce7ff6e aurel32
    case 0x0110:
180 4ce7ff6e aurel32
    case 0x0118:
181 4ce7ff6e aurel32
    case 0x0120:
182 4ce7ff6e aurel32
    case 0x0128:
183 4ce7ff6e aurel32
    case 0x0130:
184 4ce7ff6e aurel32
    case 0x0138:
185 4ce7ff6e aurel32
    case 0x0140:
186 4ce7ff6e aurel32
    case 0x0148:
187 4ce7ff6e aurel32
    case 0x0150:
188 4ce7ff6e aurel32
    case 0x0158:
189 4ce7ff6e aurel32
    case 0x0160:
190 4ce7ff6e aurel32
    case 0x0168:
191 4ce7ff6e aurel32
    case 0x0170:
192 4ce7ff6e aurel32
    case 0x0178:
193 4ce7ff6e aurel32
    case 0x0180:
194 4ce7ff6e aurel32
    case 0x0188:
195 4ce7ff6e aurel32
    case 0x0190:
196 4ce7ff6e aurel32
    case 0x0198:
197 4ce7ff6e aurel32
    case 0x01a0:
198 4ce7ff6e aurel32
    case 0x01a8:
199 4ce7ff6e aurel32
    case 0x01b0:
200 4ce7ff6e aurel32
    case 0x01b8:
201 4ce7ff6e aurel32
    case 0x01c0:
202 4ce7ff6e aurel32
    case 0x01c8:
203 4ce7ff6e aurel32
    case 0x01d0:
204 4ce7ff6e aurel32
    case 0x01d8:
205 4ce7ff6e aurel32
    case 0x01e0:
206 c6945b15 aurel32
    case 0x01e8:
207 4ce7ff6e aurel32
    case 0x01f0:
208 4ce7ff6e aurel32
    case 0x01f8:
209 4ce7ff6e aurel32
        {
210 4ce7ff6e aurel32
            int entry = (addr - 0x0100) >> 5;
211 4ce7ff6e aurel32
            int idx = (addr & 0x1f) >> 3;
212 4ce7ff6e aurel32
            val = s->dma_regs[entry][idx];
213 4ce7ff6e aurel32
        }
214 4ce7ff6e aurel32
        break;
215 9ea0b7a1 aurel32
    /* Interrupt source */
216 9ea0b7a1 aurel32
    case 0x0200:
217 9ea0b7a1 aurel32
        val = s->nmi_interrupt;
218 9ea0b7a1 aurel32
        break;
219 9ea0b7a1 aurel32
    /* Error type */
220 4ce7ff6e aurel32
    case 0x0208:
221 c6945b15 aurel32
        val = 0;
222 4ce7ff6e aurel32
        break;
223 4ce7ff6e aurel32
    /* Offset 0x0210 */
224 4ce7ff6e aurel32
    case 0x0210:
225 4ce7ff6e aurel32
        val = s->offset210;
226 4ce7ff6e aurel32
        break;
227 4ce7ff6e aurel32
    /* NV ram protect register */
228 4ce7ff6e aurel32
    case 0x0220:
229 4ce7ff6e aurel32
        val = s->nvram_protect;
230 4ce7ff6e aurel32
        break;
231 4ce7ff6e aurel32
    /* Interval timer count */
232 4ce7ff6e aurel32
    case 0x0230:
233 c6945b15 aurel32
        val = 0;
234 4ce7ff6e aurel32
        qemu_irq_lower(s->timer_irq);
235 4ce7ff6e aurel32
        break;
236 9ea0b7a1 aurel32
    /* EISA interrupt */
237 4ce7ff6e aurel32
    case 0x0238:
238 9ea0b7a1 aurel32
        val = 7; /* FIXME: should be read from EISA controller */
239 4ce7ff6e aurel32
        break;
240 4ce7ff6e aurel32
    default:
241 c6945b15 aurel32
        RC4030_ERROR("invalid read [" TARGET_FMT_plx "]\n", addr);
242 4ce7ff6e aurel32
        val = 0;
243 4ce7ff6e aurel32
        break;
244 4ce7ff6e aurel32
    }
245 4ce7ff6e aurel32
246 4aa720f7 Blue Swirl
    if ((addr & ~3) != 0x230) {
247 c6945b15 aurel32
        DPRINTF("read 0x%02x at " TARGET_FMT_plx "\n", val, addr);
248 4aa720f7 Blue Swirl
    }
249 4ce7ff6e aurel32
250 4ce7ff6e aurel32
    return val;
251 4ce7ff6e aurel32
}
252 4ce7ff6e aurel32
253 c227f099 Anthony Liguori
static uint32_t rc4030_readw(void *opaque, target_phys_addr_t addr)
254 4ce7ff6e aurel32
{
255 4ce7ff6e aurel32
    uint32_t v = rc4030_readl(opaque, addr & ~0x3);
256 4ce7ff6e aurel32
    if (addr & 0x2)
257 4ce7ff6e aurel32
        return v >> 16;
258 4ce7ff6e aurel32
    else
259 4ce7ff6e aurel32
        return v & 0xffff;
260 4ce7ff6e aurel32
}
261 4ce7ff6e aurel32
262 c227f099 Anthony Liguori
static uint32_t rc4030_readb(void *opaque, target_phys_addr_t addr)
263 4ce7ff6e aurel32
{
264 4ce7ff6e aurel32
    uint32_t v = rc4030_readl(opaque, addr & ~0x3);
265 4ce7ff6e aurel32
    return (v >> (8 * (addr & 0x3))) & 0xff;
266 4ce7ff6e aurel32
}
267 4ce7ff6e aurel32
268 c227f099 Anthony Liguori
static void rc4030_writel(void *opaque, target_phys_addr_t addr, uint32_t val)
269 4ce7ff6e aurel32
{
270 4ce7ff6e aurel32
    rc4030State *s = opaque;
271 4ce7ff6e aurel32
    addr &= 0x3fff;
272 4ce7ff6e aurel32
273 c6945b15 aurel32
    DPRINTF("write 0x%02x at " TARGET_FMT_plx "\n", val, addr);
274 4ce7ff6e aurel32
275 4ce7ff6e aurel32
    switch (addr & ~0x3) {
276 4ce7ff6e aurel32
    /* Global config register */
277 4ce7ff6e aurel32
    case 0x0000:
278 4ce7ff6e aurel32
        s->config = val;
279 4ce7ff6e aurel32
        break;
280 4ce7ff6e aurel32
    /* DMA transl. table base */
281 4ce7ff6e aurel32
    case 0x0018:
282 4ce7ff6e aurel32
        s->dma_tl_base = val;
283 4ce7ff6e aurel32
        break;
284 4ce7ff6e aurel32
    /* DMA transl. table limit */
285 4ce7ff6e aurel32
    case 0x0020:
286 4ce7ff6e aurel32
        s->dma_tl_limit = val;
287 4ce7ff6e aurel32
        break;
288 c6945b15 aurel32
    /* DMA transl. table invalidated */
289 c6945b15 aurel32
    case 0x0028:
290 c6945b15 aurel32
        break;
291 c6945b15 aurel32
    /* Cache Maintenance */
292 c6945b15 aurel32
    case 0x0030:
293 9ea0b7a1 aurel32
        s->cache_maint = val;
294 c6945b15 aurel32
        break;
295 4ce7ff6e aurel32
    /* I/O Cache Physical Tag */
296 4ce7ff6e aurel32
    case 0x0048:
297 4ce7ff6e aurel32
        s->cache_ptag = val;
298 4ce7ff6e aurel32
        break;
299 4ce7ff6e aurel32
    /* I/O Cache Logical Tag */
300 4ce7ff6e aurel32
    case 0x0050:
301 4ce7ff6e aurel32
        s->cache_ltag = val;
302 4ce7ff6e aurel32
        break;
303 4ce7ff6e aurel32
    /* I/O Cache Byte Mask */
304 4ce7ff6e aurel32
    case 0x0058:
305 4ce7ff6e aurel32
        s->cache_bmask |= val; /* HACK */
306 4ce7ff6e aurel32
        break;
307 4ce7ff6e aurel32
    /* I/O Cache Buffer Window */
308 4ce7ff6e aurel32
    case 0x0060:
309 4ce7ff6e aurel32
        /* HACK */
310 4ce7ff6e aurel32
        if (s->cache_ltag == 0x80000001 && s->cache_bmask == 0xf0f0f0f) {
311 c227f099 Anthony Liguori
            target_phys_addr_t dest = s->cache_ptag & ~0x1;
312 9ea0b7a1 aurel32
            dest += (s->cache_maint & 0x3) << 3;
313 54f7b4a3 Stefan Weil
            cpu_physical_memory_write(dest, &val, 4);
314 4ce7ff6e aurel32
        }
315 4ce7ff6e aurel32
        break;
316 4ce7ff6e aurel32
    /* Remote Speed Registers */
317 4ce7ff6e aurel32
    case 0x0070:
318 4ce7ff6e aurel32
    case 0x0078:
319 4ce7ff6e aurel32
    case 0x0080:
320 4ce7ff6e aurel32
    case 0x0088:
321 4ce7ff6e aurel32
    case 0x0090:
322 4ce7ff6e aurel32
    case 0x0098:
323 4ce7ff6e aurel32
    case 0x00a0:
324 4ce7ff6e aurel32
    case 0x00a8:
325 4ce7ff6e aurel32
    case 0x00b0:
326 4ce7ff6e aurel32
    case 0x00b8:
327 4ce7ff6e aurel32
    case 0x00c0:
328 4ce7ff6e aurel32
    case 0x00c8:
329 4ce7ff6e aurel32
    case 0x00d0:
330 4ce7ff6e aurel32
    case 0x00d8:
331 4ce7ff6e aurel32
    case 0x00e0:
332 9ea0b7a1 aurel32
    case 0x00e8:
333 4ce7ff6e aurel32
        s->rem_speed[(addr - 0x0070) >> 3] = val;
334 4ce7ff6e aurel32
        break;
335 4ce7ff6e aurel32
    /* DMA channel base address */
336 4ce7ff6e aurel32
    case 0x0100:
337 4ce7ff6e aurel32
    case 0x0108:
338 4ce7ff6e aurel32
    case 0x0110:
339 4ce7ff6e aurel32
    case 0x0118:
340 4ce7ff6e aurel32
    case 0x0120:
341 4ce7ff6e aurel32
    case 0x0128:
342 4ce7ff6e aurel32
    case 0x0130:
343 4ce7ff6e aurel32
    case 0x0138:
344 4ce7ff6e aurel32
    case 0x0140:
345 4ce7ff6e aurel32
    case 0x0148:
346 4ce7ff6e aurel32
    case 0x0150:
347 4ce7ff6e aurel32
    case 0x0158:
348 4ce7ff6e aurel32
    case 0x0160:
349 4ce7ff6e aurel32
    case 0x0168:
350 4ce7ff6e aurel32
    case 0x0170:
351 4ce7ff6e aurel32
    case 0x0178:
352 4ce7ff6e aurel32
    case 0x0180:
353 4ce7ff6e aurel32
    case 0x0188:
354 4ce7ff6e aurel32
    case 0x0190:
355 4ce7ff6e aurel32
    case 0x0198:
356 4ce7ff6e aurel32
    case 0x01a0:
357 4ce7ff6e aurel32
    case 0x01a8:
358 4ce7ff6e aurel32
    case 0x01b0:
359 4ce7ff6e aurel32
    case 0x01b8:
360 4ce7ff6e aurel32
    case 0x01c0:
361 4ce7ff6e aurel32
    case 0x01c8:
362 4ce7ff6e aurel32
    case 0x01d0:
363 4ce7ff6e aurel32
    case 0x01d8:
364 4ce7ff6e aurel32
    case 0x01e0:
365 c6945b15 aurel32
    case 0x01e8:
366 4ce7ff6e aurel32
    case 0x01f0:
367 4ce7ff6e aurel32
    case 0x01f8:
368 4ce7ff6e aurel32
        {
369 4ce7ff6e aurel32
            int entry = (addr - 0x0100) >> 5;
370 4ce7ff6e aurel32
            int idx = (addr & 0x1f) >> 3;
371 4ce7ff6e aurel32
            s->dma_regs[entry][idx] = val;
372 4ce7ff6e aurel32
        }
373 4ce7ff6e aurel32
        break;
374 4ce7ff6e aurel32
    /* Offset 0x0210 */
375 4ce7ff6e aurel32
    case 0x0210:
376 4ce7ff6e aurel32
        s->offset210 = val;
377 4ce7ff6e aurel32
        break;
378 4ce7ff6e aurel32
    /* Interval timer reload */
379 4ce7ff6e aurel32
    case 0x0228:
380 4ce7ff6e aurel32
        s->itr = val;
381 4ce7ff6e aurel32
        qemu_irq_lower(s->timer_irq);
382 4ce7ff6e aurel32
        set_next_tick(s);
383 4ce7ff6e aurel32
        break;
384 9ea0b7a1 aurel32
    /* EISA interrupt */
385 9ea0b7a1 aurel32
    case 0x0238:
386 9ea0b7a1 aurel32
        break;
387 4ce7ff6e aurel32
    default:
388 c6945b15 aurel32
        RC4030_ERROR("invalid write of 0x%02x at [" TARGET_FMT_plx "]\n", val, addr);
389 4ce7ff6e aurel32
        break;
390 4ce7ff6e aurel32
    }
391 4ce7ff6e aurel32
}
392 4ce7ff6e aurel32
393 c227f099 Anthony Liguori
static void rc4030_writew(void *opaque, target_phys_addr_t addr, uint32_t val)
394 4ce7ff6e aurel32
{
395 4ce7ff6e aurel32
    uint32_t old_val = rc4030_readl(opaque, addr & ~0x3);
396 4ce7ff6e aurel32
397 4ce7ff6e aurel32
    if (addr & 0x2)
398 4ce7ff6e aurel32
        val = (val << 16) | (old_val & 0x0000ffff);
399 4ce7ff6e aurel32
    else
400 4ce7ff6e aurel32
        val = val | (old_val & 0xffff0000);
401 4ce7ff6e aurel32
    rc4030_writel(opaque, addr & ~0x3, val);
402 4ce7ff6e aurel32
}
403 4ce7ff6e aurel32
404 c227f099 Anthony Liguori
static void rc4030_writeb(void *opaque, target_phys_addr_t addr, uint32_t val)
405 4ce7ff6e aurel32
{
406 4ce7ff6e aurel32
    uint32_t old_val = rc4030_readl(opaque, addr & ~0x3);
407 4ce7ff6e aurel32
408 4ce7ff6e aurel32
    switch (addr & 3) {
409 4ce7ff6e aurel32
    case 0:
410 4ce7ff6e aurel32
        val = val | (old_val & 0xffffff00);
411 4ce7ff6e aurel32
        break;
412 4ce7ff6e aurel32
    case 1:
413 4ce7ff6e aurel32
        val = (val << 8) | (old_val & 0xffff00ff);
414 4ce7ff6e aurel32
        break;
415 4ce7ff6e aurel32
    case 2:
416 4ce7ff6e aurel32
        val = (val << 16) | (old_val & 0xff00ffff);
417 4ce7ff6e aurel32
        break;
418 4ce7ff6e aurel32
    case 3:
419 4ce7ff6e aurel32
        val = (val << 24) | (old_val & 0x00ffffff);
420 4ce7ff6e aurel32
        break;
421 4ce7ff6e aurel32
    }
422 4ce7ff6e aurel32
    rc4030_writel(opaque, addr & ~0x3, val);
423 4ce7ff6e aurel32
}
424 4ce7ff6e aurel32
425 3054434d Avi Kivity
static const MemoryRegionOps rc4030_ops = {
426 3054434d Avi Kivity
    .old_mmio = {
427 3054434d Avi Kivity
        .read = { rc4030_readb, rc4030_readw, rc4030_readl, },
428 3054434d Avi Kivity
        .write = { rc4030_writeb, rc4030_writew, rc4030_writel, },
429 3054434d Avi Kivity
    },
430 3054434d Avi Kivity
    .endianness = DEVICE_NATIVE_ENDIAN,
431 4ce7ff6e aurel32
};
432 4ce7ff6e aurel32
433 4ce7ff6e aurel32
static void update_jazz_irq(rc4030State *s)
434 4ce7ff6e aurel32
{
435 4ce7ff6e aurel32
    uint16_t pending;
436 4ce7ff6e aurel32
437 4ce7ff6e aurel32
    pending = s->isr_jazz & s->imr_jazz;
438 4ce7ff6e aurel32
439 4ce7ff6e aurel32
#ifdef DEBUG_RC4030
440 4ce7ff6e aurel32
    if (s->isr_jazz != 0) {
441 4ce7ff6e aurel32
        uint32_t irq = 0;
442 c6945b15 aurel32
        DPRINTF("pending irqs:");
443 b1503cda malc
        for (irq = 0; irq < ARRAY_SIZE(irq_names); irq++) {
444 4ce7ff6e aurel32
            if (s->isr_jazz & (1 << irq)) {
445 4ce7ff6e aurel32
                printf(" %s", irq_names[irq]);
446 4ce7ff6e aurel32
                if (!(s->imr_jazz & (1 << irq))) {
447 4ce7ff6e aurel32
                    printf("(ignored)");
448 4ce7ff6e aurel32
                }
449 4ce7ff6e aurel32
            }
450 4ce7ff6e aurel32
        }
451 4ce7ff6e aurel32
        printf("\n");
452 4ce7ff6e aurel32
    }
453 4ce7ff6e aurel32
#endif
454 4ce7ff6e aurel32
455 4ce7ff6e aurel32
    if (pending != 0)
456 4ce7ff6e aurel32
        qemu_irq_raise(s->jazz_bus_irq);
457 4ce7ff6e aurel32
    else
458 4ce7ff6e aurel32
        qemu_irq_lower(s->jazz_bus_irq);
459 4ce7ff6e aurel32
}
460 4ce7ff6e aurel32
461 4ce7ff6e aurel32
static void rc4030_irq_jazz_request(void *opaque, int irq, int level)
462 4ce7ff6e aurel32
{
463 4ce7ff6e aurel32
    rc4030State *s = opaque;
464 4ce7ff6e aurel32
465 4ce7ff6e aurel32
    if (level) {
466 4ce7ff6e aurel32
        s->isr_jazz |= 1 << irq;
467 4ce7ff6e aurel32
    } else {
468 4ce7ff6e aurel32
        s->isr_jazz &= ~(1 << irq);
469 4ce7ff6e aurel32
    }
470 4ce7ff6e aurel32
471 4ce7ff6e aurel32
    update_jazz_irq(s);
472 4ce7ff6e aurel32
}
473 4ce7ff6e aurel32
474 4ce7ff6e aurel32
static void rc4030_periodic_timer(void *opaque)
475 4ce7ff6e aurel32
{
476 4ce7ff6e aurel32
    rc4030State *s = opaque;
477 4ce7ff6e aurel32
478 4ce7ff6e aurel32
    set_next_tick(s);
479 4ce7ff6e aurel32
    qemu_irq_raise(s->timer_irq);
480 4ce7ff6e aurel32
}
481 4ce7ff6e aurel32
482 c227f099 Anthony Liguori
static uint32_t jazzio_readw(void *opaque, target_phys_addr_t addr)
483 4ce7ff6e aurel32
{
484 4ce7ff6e aurel32
    rc4030State *s = opaque;
485 4ce7ff6e aurel32
    uint32_t val;
486 4ce7ff6e aurel32
    uint32_t irq;
487 4ce7ff6e aurel32
    addr &= 0xfff;
488 4ce7ff6e aurel32
489 4ce7ff6e aurel32
    switch (addr) {
490 c6945b15 aurel32
    /* Local bus int source */
491 4ce7ff6e aurel32
    case 0x00: {
492 4ce7ff6e aurel32
        uint32_t pending = s->isr_jazz & s->imr_jazz;
493 4ce7ff6e aurel32
        val = 0;
494 4ce7ff6e aurel32
        irq = 0;
495 4ce7ff6e aurel32
        while (pending) {
496 4ce7ff6e aurel32
            if (pending & 1) {
497 c6945b15 aurel32
                DPRINTF("returning irq %s\n", irq_names[irq]);
498 4ce7ff6e aurel32
                val = (irq + 1) << 2;
499 4ce7ff6e aurel32
                break;
500 4ce7ff6e aurel32
            }
501 4ce7ff6e aurel32
            irq++;
502 4ce7ff6e aurel32
            pending >>= 1;
503 4ce7ff6e aurel32
        }
504 4ce7ff6e aurel32
        break;
505 4ce7ff6e aurel32
    }
506 c6945b15 aurel32
    /* Local bus int enable mask */
507 c6945b15 aurel32
    case 0x02:
508 c6945b15 aurel32
        val = s->imr_jazz;
509 c6945b15 aurel32
        break;
510 4ce7ff6e aurel32
    default:
511 c6945b15 aurel32
        RC4030_ERROR("(jazz io controller) invalid read [" TARGET_FMT_plx "]\n", addr);
512 c6945b15 aurel32
        val = 0;
513 4ce7ff6e aurel32
    }
514 4ce7ff6e aurel32
515 c6945b15 aurel32
    DPRINTF("(jazz io controller) read 0x%04x at " TARGET_FMT_plx "\n", val, addr);
516 4ce7ff6e aurel32
517 4ce7ff6e aurel32
    return val;
518 4ce7ff6e aurel32
}
519 4ce7ff6e aurel32
520 c227f099 Anthony Liguori
static uint32_t jazzio_readb(void *opaque, target_phys_addr_t addr)
521 4ce7ff6e aurel32
{
522 4ce7ff6e aurel32
    uint32_t v;
523 c6945b15 aurel32
    v = jazzio_readw(opaque, addr & ~0x1);
524 c6945b15 aurel32
    return (v >> (8 * (addr & 0x1))) & 0xff;
525 4ce7ff6e aurel32
}
526 4ce7ff6e aurel32
527 c227f099 Anthony Liguori
static uint32_t jazzio_readl(void *opaque, target_phys_addr_t addr)
528 4ce7ff6e aurel32
{
529 4ce7ff6e aurel32
    uint32_t v;
530 c6945b15 aurel32
    v = jazzio_readw(opaque, addr);
531 c6945b15 aurel32
    v |= jazzio_readw(opaque, addr + 2) << 16;
532 4ce7ff6e aurel32
    return v;
533 4ce7ff6e aurel32
}
534 4ce7ff6e aurel32
535 c227f099 Anthony Liguori
static void jazzio_writew(void *opaque, target_phys_addr_t addr, uint32_t val)
536 4ce7ff6e aurel32
{
537 4ce7ff6e aurel32
    rc4030State *s = opaque;
538 4ce7ff6e aurel32
    addr &= 0xfff;
539 4ce7ff6e aurel32
540 c6945b15 aurel32
    DPRINTF("(jazz io controller) write 0x%04x at " TARGET_FMT_plx "\n", val, addr);
541 4ce7ff6e aurel32
542 4ce7ff6e aurel32
    switch (addr) {
543 4ce7ff6e aurel32
    /* Local bus int enable mask */
544 4ce7ff6e aurel32
    case 0x02:
545 c6945b15 aurel32
        s->imr_jazz = val;
546 c6945b15 aurel32
        update_jazz_irq(s);
547 4ce7ff6e aurel32
        break;
548 4ce7ff6e aurel32
    default:
549 c6945b15 aurel32
        RC4030_ERROR("(jazz io controller) invalid write of 0x%04x at [" TARGET_FMT_plx "]\n", val, addr);
550 4ce7ff6e aurel32
        break;
551 4ce7ff6e aurel32
    }
552 4ce7ff6e aurel32
}
553 4ce7ff6e aurel32
554 c227f099 Anthony Liguori
static void jazzio_writeb(void *opaque, target_phys_addr_t addr, uint32_t val)
555 4ce7ff6e aurel32
{
556 c6945b15 aurel32
    uint32_t old_val = jazzio_readw(opaque, addr & ~0x1);
557 c6945b15 aurel32
558 c6945b15 aurel32
    switch (addr & 1) {
559 c6945b15 aurel32
    case 0:
560 c6945b15 aurel32
        val = val | (old_val & 0xff00);
561 c6945b15 aurel32
        break;
562 c6945b15 aurel32
    case 1:
563 c6945b15 aurel32
        val = (val << 8) | (old_val & 0x00ff);
564 c6945b15 aurel32
        break;
565 c6945b15 aurel32
    }
566 c6945b15 aurel32
    jazzio_writew(opaque, addr & ~0x1, val);
567 4ce7ff6e aurel32
}
568 4ce7ff6e aurel32
569 c227f099 Anthony Liguori
static void jazzio_writel(void *opaque, target_phys_addr_t addr, uint32_t val)
570 4ce7ff6e aurel32
{
571 c6945b15 aurel32
    jazzio_writew(opaque, addr, val & 0xffff);
572 c6945b15 aurel32
    jazzio_writew(opaque, addr + 2, (val >> 16) & 0xffff);
573 4ce7ff6e aurel32
}
574 4ce7ff6e aurel32
575 3054434d Avi Kivity
static const MemoryRegionOps jazzio_ops = {
576 3054434d Avi Kivity
    .old_mmio = {
577 3054434d Avi Kivity
        .read = { jazzio_readb, jazzio_readw, jazzio_readl, },
578 3054434d Avi Kivity
        .write = { jazzio_writeb, jazzio_writew, jazzio_writel, },
579 3054434d Avi Kivity
    },
580 3054434d Avi Kivity
    .endianness = DEVICE_NATIVE_ENDIAN,
581 4ce7ff6e aurel32
};
582 4ce7ff6e aurel32
583 4ce7ff6e aurel32
static void rc4030_reset(void *opaque)
584 4ce7ff6e aurel32
{
585 4ce7ff6e aurel32
    rc4030State *s = opaque;
586 4ce7ff6e aurel32
    int i;
587 4ce7ff6e aurel32
588 c6945b15 aurel32
    s->config = 0x410; /* some boards seem to accept 0x104 too */
589 9ea0b7a1 aurel32
    s->revision = 1;
590 4ce7ff6e aurel32
    s->invalid_address_register = 0;
591 4ce7ff6e aurel32
592 4ce7ff6e aurel32
    memset(s->dma_regs, 0, sizeof(s->dma_regs));
593 4ce7ff6e aurel32
    s->dma_tl_base = s->dma_tl_limit = 0;
594 4ce7ff6e aurel32
595 4ce7ff6e aurel32
    s->remote_failed_address = s->memory_failed_address = 0;
596 9ea0b7a1 aurel32
    s->cache_maint = 0;
597 4ce7ff6e aurel32
    s->cache_ptag = s->cache_ltag = 0;
598 9ea0b7a1 aurel32
    s->cache_bmask = 0;
599 4ce7ff6e aurel32
600 4ce7ff6e aurel32
    s->offset210 = 0x18186;
601 4ce7ff6e aurel32
    s->nvram_protect = 7;
602 4ce7ff6e aurel32
    for (i = 0; i < 15; i++)
603 4ce7ff6e aurel32
        s->rem_speed[i] = 7;
604 9ea0b7a1 aurel32
    s->imr_jazz = 0x10; /* XXX: required by firmware, but why? */
605 9ea0b7a1 aurel32
    s->isr_jazz = 0;
606 4ce7ff6e aurel32
607 4ce7ff6e aurel32
    s->itr = 0;
608 4ce7ff6e aurel32
609 4ce7ff6e aurel32
    qemu_irq_lower(s->timer_irq);
610 4ce7ff6e aurel32
    qemu_irq_lower(s->jazz_bus_irq);
611 4ce7ff6e aurel32
}
612 4ce7ff6e aurel32
613 d5853c20 aurel32
static int rc4030_load(QEMUFile *f, void *opaque, int version_id)
614 d5853c20 aurel32
{
615 d5853c20 aurel32
    rc4030State* s = opaque;
616 d5853c20 aurel32
    int i, j;
617 d5853c20 aurel32
618 9ea0b7a1 aurel32
    if (version_id != 2)
619 d5853c20 aurel32
        return -EINVAL;
620 d5853c20 aurel32
621 d5853c20 aurel32
    s->config = qemu_get_be32(f);
622 d5853c20 aurel32
    s->invalid_address_register = qemu_get_be32(f);
623 d5853c20 aurel32
    for (i = 0; i < 8; i++)
624 d5853c20 aurel32
        for (j = 0; j < 4; j++)
625 d5853c20 aurel32
            s->dma_regs[i][j] = qemu_get_be32(f);
626 d5853c20 aurel32
    s->dma_tl_base = qemu_get_be32(f);
627 d5853c20 aurel32
    s->dma_tl_limit = qemu_get_be32(f);
628 9ea0b7a1 aurel32
    s->cache_maint = qemu_get_be32(f);
629 d5853c20 aurel32
    s->remote_failed_address = qemu_get_be32(f);
630 d5853c20 aurel32
    s->memory_failed_address = qemu_get_be32(f);
631 d5853c20 aurel32
    s->cache_ptag = qemu_get_be32(f);
632 d5853c20 aurel32
    s->cache_ltag = qemu_get_be32(f);
633 d5853c20 aurel32
    s->cache_bmask = qemu_get_be32(f);
634 d5853c20 aurel32
    s->offset210 = qemu_get_be32(f);
635 d5853c20 aurel32
    s->nvram_protect = qemu_get_be32(f);
636 d5853c20 aurel32
    for (i = 0; i < 15; i++)
637 d5853c20 aurel32
        s->rem_speed[i] = qemu_get_be32(f);
638 d5853c20 aurel32
    s->imr_jazz = qemu_get_be32(f);
639 d5853c20 aurel32
    s->isr_jazz = qemu_get_be32(f);
640 d5853c20 aurel32
    s->itr = qemu_get_be32(f);
641 d5853c20 aurel32
642 d5853c20 aurel32
    set_next_tick(s);
643 d5853c20 aurel32
    update_jazz_irq(s);
644 d5853c20 aurel32
645 d5853c20 aurel32
    return 0;
646 d5853c20 aurel32
}
647 d5853c20 aurel32
648 d5853c20 aurel32
static void rc4030_save(QEMUFile *f, void *opaque)
649 d5853c20 aurel32
{
650 d5853c20 aurel32
    rc4030State* s = opaque;
651 d5853c20 aurel32
    int i, j;
652 d5853c20 aurel32
653 d5853c20 aurel32
    qemu_put_be32(f, s->config);
654 d5853c20 aurel32
    qemu_put_be32(f, s->invalid_address_register);
655 d5853c20 aurel32
    for (i = 0; i < 8; i++)
656 d5853c20 aurel32
        for (j = 0; j < 4; j++)
657 d5853c20 aurel32
            qemu_put_be32(f, s->dma_regs[i][j]);
658 d5853c20 aurel32
    qemu_put_be32(f, s->dma_tl_base);
659 d5853c20 aurel32
    qemu_put_be32(f, s->dma_tl_limit);
660 9ea0b7a1 aurel32
    qemu_put_be32(f, s->cache_maint);
661 d5853c20 aurel32
    qemu_put_be32(f, s->remote_failed_address);
662 d5853c20 aurel32
    qemu_put_be32(f, s->memory_failed_address);
663 d5853c20 aurel32
    qemu_put_be32(f, s->cache_ptag);
664 d5853c20 aurel32
    qemu_put_be32(f, s->cache_ltag);
665 d5853c20 aurel32
    qemu_put_be32(f, s->cache_bmask);
666 d5853c20 aurel32
    qemu_put_be32(f, s->offset210);
667 d5853c20 aurel32
    qemu_put_be32(f, s->nvram_protect);
668 d5853c20 aurel32
    for (i = 0; i < 15; i++)
669 d5853c20 aurel32
        qemu_put_be32(f, s->rem_speed[i]);
670 d5853c20 aurel32
    qemu_put_be32(f, s->imr_jazz);
671 d5853c20 aurel32
    qemu_put_be32(f, s->isr_jazz);
672 d5853c20 aurel32
    qemu_put_be32(f, s->itr);
673 d5853c20 aurel32
}
674 d5853c20 aurel32
675 c227f099 Anthony Liguori
void rc4030_dma_memory_rw(void *opaque, target_phys_addr_t addr, uint8_t *buf, int len, int is_write)
676 c6945b15 aurel32
{
677 c6945b15 aurel32
    rc4030State *s = opaque;
678 c227f099 Anthony Liguori
    target_phys_addr_t entry_addr;
679 c227f099 Anthony Liguori
    target_phys_addr_t phys_addr;
680 c6945b15 aurel32
    dma_pagetable_entry entry;
681 9ea0b7a1 aurel32
    int index;
682 c6945b15 aurel32
    int ncpy, i;
683 c6945b15 aurel32
684 c6945b15 aurel32
    i = 0;
685 c6945b15 aurel32
    for (;;) {
686 c6945b15 aurel32
        if (i == len) {
687 c6945b15 aurel32
            break;
688 c6945b15 aurel32
        }
689 c6945b15 aurel32
690 9ea0b7a1 aurel32
        ncpy = DMA_PAGESIZE - (addr & (DMA_PAGESIZE - 1));
691 c6945b15 aurel32
        if (ncpy > len - i)
692 c6945b15 aurel32
            ncpy = len - i;
693 c6945b15 aurel32
694 c6945b15 aurel32
        /* Get DMA translation table entry */
695 9ea0b7a1 aurel32
        index = addr / DMA_PAGESIZE;
696 c6945b15 aurel32
        if (index >= s->dma_tl_limit / sizeof(dma_pagetable_entry)) {
697 c6945b15 aurel32
            break;
698 c6945b15 aurel32
        }
699 c6945b15 aurel32
        entry_addr = s->dma_tl_base + index * sizeof(dma_pagetable_entry);
700 c6945b15 aurel32
        /* XXX: not sure. should we really use only lowest bits? */
701 c6945b15 aurel32
        entry_addr &= 0x7fffffff;
702 54f7b4a3 Stefan Weil
        cpu_physical_memory_read(entry_addr, &entry, sizeof(entry));
703 c6945b15 aurel32
704 c6945b15 aurel32
        /* Read/write data at right place */
705 9ea0b7a1 aurel32
        phys_addr = entry.frame + (addr & (DMA_PAGESIZE - 1));
706 c6945b15 aurel32
        cpu_physical_memory_rw(phys_addr, &buf[i], ncpy, is_write);
707 c6945b15 aurel32
708 c6945b15 aurel32
        i += ncpy;
709 9ea0b7a1 aurel32
        addr += ncpy;
710 9ea0b7a1 aurel32
    }
711 9ea0b7a1 aurel32
}
712 9ea0b7a1 aurel32
713 9ea0b7a1 aurel32
static void rc4030_do_dma(void *opaque, int n, uint8_t *buf, int len, int is_write)
714 9ea0b7a1 aurel32
{
715 9ea0b7a1 aurel32
    rc4030State *s = opaque;
716 c227f099 Anthony Liguori
    target_phys_addr_t dma_addr;
717 9ea0b7a1 aurel32
    int dev_to_mem;
718 9ea0b7a1 aurel32
719 9ea0b7a1 aurel32
    s->dma_regs[n][DMA_REG_ENABLE] &= ~(DMA_FLAG_TC_INTR | DMA_FLAG_MEM_INTR | DMA_FLAG_ADDR_INTR);
720 9ea0b7a1 aurel32
721 9ea0b7a1 aurel32
    /* Check DMA channel consistency */
722 9ea0b7a1 aurel32
    dev_to_mem = (s->dma_regs[n][DMA_REG_ENABLE] & DMA_FLAG_MEM_TO_DEV) ? 0 : 1;
723 9ea0b7a1 aurel32
    if (!(s->dma_regs[n][DMA_REG_ENABLE] & DMA_FLAG_ENABLE) ||
724 9ea0b7a1 aurel32
        (is_write != dev_to_mem)) {
725 9ea0b7a1 aurel32
        s->dma_regs[n][DMA_REG_ENABLE] |= DMA_FLAG_MEM_INTR;
726 9ea0b7a1 aurel32
        s->nmi_interrupt |= 1 << n;
727 9ea0b7a1 aurel32
        return;
728 c6945b15 aurel32
    }
729 c6945b15 aurel32
730 9ea0b7a1 aurel32
    /* Get start address and len */
731 9ea0b7a1 aurel32
    if (len > s->dma_regs[n][DMA_REG_COUNT])
732 9ea0b7a1 aurel32
        len = s->dma_regs[n][DMA_REG_COUNT];
733 9ea0b7a1 aurel32
    dma_addr = s->dma_regs[n][DMA_REG_ADDRESS];
734 9ea0b7a1 aurel32
735 9ea0b7a1 aurel32
    /* Read/write data at right place */
736 9ea0b7a1 aurel32
    rc4030_dma_memory_rw(opaque, dma_addr, buf, len, is_write);
737 9ea0b7a1 aurel32
738 9ea0b7a1 aurel32
    s->dma_regs[n][DMA_REG_ENABLE] |= DMA_FLAG_TC_INTR;
739 9ea0b7a1 aurel32
    s->dma_regs[n][DMA_REG_COUNT] -= len;
740 9ea0b7a1 aurel32
741 c6945b15 aurel32
#ifdef DEBUG_RC4030_DMA
742 c6945b15 aurel32
    {
743 c6945b15 aurel32
        int i, j;
744 c6945b15 aurel32
        printf("rc4030 dma: Copying %d bytes %s host %p\n",
745 c6945b15 aurel32
            len, is_write ? "from" : "to", buf);
746 c6945b15 aurel32
        for (i = 0; i < len; i += 16) {
747 b832134d Stefan Weil
            int n = 16;
748 b832134d Stefan Weil
            if (n > len - i) {
749 b832134d Stefan Weil
                n = len - i;
750 b832134d Stefan Weil
            }
751 c6945b15 aurel32
            for (j = 0; j < n; j++)
752 c6945b15 aurel32
                printf("%02x ", buf[i + j]);
753 c6945b15 aurel32
            while (j++ < 16)
754 c6945b15 aurel32
                printf("   ");
755 c6945b15 aurel32
            printf("| ");
756 c6945b15 aurel32
            for (j = 0; j < n; j++)
757 c6945b15 aurel32
                printf("%c", isprint(buf[i + j]) ? buf[i + j] : '.');
758 c6945b15 aurel32
            printf("\n");
759 c6945b15 aurel32
        }
760 c6945b15 aurel32
    }
761 c6945b15 aurel32
#endif
762 c6945b15 aurel32
}
763 c6945b15 aurel32
764 c6945b15 aurel32
struct rc4030DMAState {
765 c6945b15 aurel32
    void *opaque;
766 c6945b15 aurel32
    int n;
767 c6945b15 aurel32
};
768 c6945b15 aurel32
769 68238a9e aurel32
void rc4030_dma_read(void *dma, uint8_t *buf, int len)
770 c6945b15 aurel32
{
771 c6945b15 aurel32
    rc4030_dma s = dma;
772 c6945b15 aurel32
    rc4030_do_dma(s->opaque, s->n, buf, len, 0);
773 c6945b15 aurel32
}
774 c6945b15 aurel32
775 68238a9e aurel32
void rc4030_dma_write(void *dma, uint8_t *buf, int len)
776 c6945b15 aurel32
{
777 c6945b15 aurel32
    rc4030_dma s = dma;
778 c6945b15 aurel32
    rc4030_do_dma(s->opaque, s->n, buf, len, 1);
779 c6945b15 aurel32
}
780 c6945b15 aurel32
781 c6945b15 aurel32
static rc4030_dma *rc4030_allocate_dmas(void *opaque, int n)
782 c6945b15 aurel32
{
783 c6945b15 aurel32
    rc4030_dma *s;
784 c6945b15 aurel32
    struct rc4030DMAState *p;
785 c6945b15 aurel32
    int i;
786 c6945b15 aurel32
787 7267c094 Anthony Liguori
    s = (rc4030_dma *)g_malloc0(sizeof(rc4030_dma) * n);
788 7267c094 Anthony Liguori
    p = (struct rc4030DMAState *)g_malloc0(sizeof(struct rc4030DMAState) * n);
789 c6945b15 aurel32
    for (i = 0; i < n; i++) {
790 c6945b15 aurel32
        p->opaque = opaque;
791 c6945b15 aurel32
        p->n = i;
792 c6945b15 aurel32
        s[i] = p;
793 c6945b15 aurel32
        p++;
794 c6945b15 aurel32
    }
795 c6945b15 aurel32
    return s;
796 c6945b15 aurel32
}
797 c6945b15 aurel32
798 68238a9e aurel32
void *rc4030_init(qemu_irq timer, qemu_irq jazz_bus,
799 3054434d Avi Kivity
                  qemu_irq **irqs, rc4030_dma **dmas,
800 3054434d Avi Kivity
                  MemoryRegion *sysmem)
801 4ce7ff6e aurel32
{
802 4ce7ff6e aurel32
    rc4030State *s;
803 4ce7ff6e aurel32
804 7267c094 Anthony Liguori
    s = g_malloc0(sizeof(rc4030State));
805 4ce7ff6e aurel32
806 68238a9e aurel32
    *irqs = qemu_allocate_irqs(rc4030_irq_jazz_request, s, 16);
807 c6945b15 aurel32
    *dmas = rc4030_allocate_dmas(s, 4);
808 c6945b15 aurel32
809 74475455 Paolo Bonzini
    s->periodic_timer = qemu_new_timer_ns(vm_clock, rc4030_periodic_timer, s);
810 4ce7ff6e aurel32
    s->timer_irq = timer;
811 4ce7ff6e aurel32
    s->jazz_bus_irq = jazz_bus;
812 4ce7ff6e aurel32
813 a08d4367 Jan Kiszka
    qemu_register_reset(rc4030_reset, s);
814 0be71e32 Alex Williamson
    register_savevm(NULL, "rc4030", 0, 2, rc4030_save, rc4030_load, s);
815 4ce7ff6e aurel32
    rc4030_reset(s);
816 4ce7ff6e aurel32
817 3054434d Avi Kivity
    memory_region_init_io(&s->iomem_chipset, &rc4030_ops, s,
818 3054434d Avi Kivity
                          "rc4030.chipset", 0x300);
819 3054434d Avi Kivity
    memory_region_add_subregion(sysmem, 0x80000000, &s->iomem_chipset);
820 3054434d Avi Kivity
    memory_region_init_io(&s->iomem_jazzio, &jazzio_ops, s,
821 3054434d Avi Kivity
                          "rc4030.jazzio", 0x00001000);
822 3054434d Avi Kivity
    memory_region_add_subregion(sysmem, 0xf0000000, &s->iomem_jazzio);
823 4ce7ff6e aurel32
824 68238a9e aurel32
    return s;
825 4ce7ff6e aurel32
}