2 * QEMU JAZZ RC4030 chipset
4 * Copyright (c) 2007-2009 Herve Poussineau
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
26 #include "hw/mips/mips.h"
27 #include "qemu/timer.h"
29 /********************************************************/
32 //#define DEBUG_RC4030
33 //#define DEBUG_RC4030_DMA
36 #define DPRINTF(fmt, ...) \
37 do { printf("rc4030: " fmt , ## __VA_ARGS__); } while (0)
38 static const char* irq_names
[] = { "parallel", "floppy", "sound", "video",
39 "network", "scsi", "keyboard", "mouse", "serial0", "serial1" };
41 #define DPRINTF(fmt, ...)
44 #define RC4030_ERROR(fmt, ...) \
45 do { fprintf(stderr, "rc4030 ERROR: %s: " fmt, __func__ , ## __VA_ARGS__); } while (0)
47 /********************************************************/
48 /* rc4030 emulation */
50 typedef struct dma_pagetable_entry
{
53 } QEMU_PACKED dma_pagetable_entry
;
55 #define DMA_PAGESIZE 4096
56 #define DMA_REG_ENABLE 1
57 #define DMA_REG_COUNT 2
58 #define DMA_REG_ADDRESS 3
60 #define DMA_FLAG_ENABLE 0x0001
61 #define DMA_FLAG_MEM_TO_DEV 0x0002
62 #define DMA_FLAG_TC_INTR 0x0100
63 #define DMA_FLAG_MEM_INTR 0x0200
64 #define DMA_FLAG_ADDR_INTR 0x0400
66 typedef struct rc4030State
68 uint32_t config
; /* 0x0000: RC4030 config register */
69 uint32_t revision
; /* 0x0008: RC4030 Revision register */
70 uint32_t invalid_address_register
; /* 0x0010: Invalid Address register */
73 uint32_t dma_regs
[8][4];
74 uint32_t dma_tl_base
; /* 0x0018: DMA transl. table base */
75 uint32_t dma_tl_limit
; /* 0x0020: DMA transl. table limit */
78 uint32_t cache_maint
; /* 0x0030: Cache Maintenance */
79 uint32_t remote_failed_address
; /* 0x0038: Remote Failed Address */
80 uint32_t memory_failed_address
; /* 0x0040: Memory Failed Address */
81 uint32_t cache_ptag
; /* 0x0048: I/O Cache Physical Tag */
82 uint32_t cache_ltag
; /* 0x0050: I/O Cache Logical Tag */
83 uint32_t cache_bmask
; /* 0x0058: I/O Cache Byte Mask */
85 uint32_t nmi_interrupt
; /* 0x0200: interrupt source */
87 uint32_t nvram_protect
; /* 0x0220: NV ram protect register */
88 uint32_t rem_speed
[16];
89 uint32_t imr_jazz
; /* Local bus int enable mask */
90 uint32_t isr_jazz
; /* Local bus int source */
93 QEMUTimer
*periodic_timer
;
94 uint32_t itr
; /* Interval timer reload */
97 qemu_irq jazz_bus_irq
;
99 MemoryRegion iomem_chipset
;
100 MemoryRegion iomem_jazzio
;
103 static void set_next_tick(rc4030State
*s
)
105 qemu_irq_lower(s
->timer_irq
);
108 tm_hz
= 1000 / (s
->itr
+ 1);
110 timer_mod(s
->periodic_timer
, qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL
) +
111 get_ticks_per_sec() / tm_hz
);
114 /* called for accesses to rc4030 */
115 static uint32_t rc4030_readl(void *opaque
, hwaddr addr
)
117 rc4030State
*s
= opaque
;
121 switch (addr
& ~0x3) {
122 /* Global config register */
126 /* Revision register */
130 /* Invalid Address register */
132 val
= s
->invalid_address_register
;
134 /* DMA transl. table base */
136 val
= s
->dma_tl_base
;
138 /* DMA transl. table limit */
140 val
= s
->dma_tl_limit
;
142 /* Remote Failed Address */
144 val
= s
->remote_failed_address
;
146 /* Memory Failed Address */
148 val
= s
->memory_failed_address
;
150 /* I/O Cache Byte Mask */
152 val
= s
->cache_bmask
;
154 if (s
->cache_bmask
== (uint32_t)-1)
157 /* Remote Speed Registers */
174 val
= s
->rem_speed
[(addr
- 0x0070) >> 3];
176 /* DMA channel base address */
210 int entry
= (addr
- 0x0100) >> 5;
211 int idx
= (addr
& 0x1f) >> 3;
212 val
= s
->dma_regs
[entry
][idx
];
215 /* Interrupt source */
217 val
= s
->nmi_interrupt
;
227 /* NV ram protect register */
229 val
= s
->nvram_protect
;
231 /* Interval timer count */
234 qemu_irq_lower(s
->timer_irq
);
238 val
= 7; /* FIXME: should be read from EISA controller */
241 RC4030_ERROR("invalid read [" TARGET_FMT_plx
"]\n", addr
);
246 if ((addr
& ~3) != 0x230) {
247 DPRINTF("read 0x%02x at " TARGET_FMT_plx
"\n", val
, addr
);
253 static uint32_t rc4030_readw(void *opaque
, hwaddr addr
)
255 uint32_t v
= rc4030_readl(opaque
, addr
& ~0x3);
262 static uint32_t rc4030_readb(void *opaque
, hwaddr addr
)
264 uint32_t v
= rc4030_readl(opaque
, addr
& ~0x3);
265 return (v
>> (8 * (addr
& 0x3))) & 0xff;
268 static void rc4030_writel(void *opaque
, hwaddr addr
, uint32_t val
)
270 rc4030State
*s
= opaque
;
273 DPRINTF("write 0x%02x at " TARGET_FMT_plx
"\n", val
, addr
);
275 switch (addr
& ~0x3) {
276 /* Global config register */
280 /* DMA transl. table base */
282 s
->dma_tl_base
= val
;
284 /* DMA transl. table limit */
286 s
->dma_tl_limit
= val
;
288 /* DMA transl. table invalidated */
291 /* Cache Maintenance */
293 s
->cache_maint
= val
;
295 /* I/O Cache Physical Tag */
299 /* I/O Cache Logical Tag */
303 /* I/O Cache Byte Mask */
305 s
->cache_bmask
|= val
; /* HACK */
307 /* I/O Cache Buffer Window */
310 if (s
->cache_ltag
== 0x80000001 && s
->cache_bmask
== 0xf0f0f0f) {
311 hwaddr dest
= s
->cache_ptag
& ~0x1;
312 dest
+= (s
->cache_maint
& 0x3) << 3;
313 cpu_physical_memory_write(dest
, &val
, 4);
316 /* Remote Speed Registers */
333 s
->rem_speed
[(addr
- 0x0070) >> 3] = val
;
335 /* DMA channel base address */
369 int entry
= (addr
- 0x0100) >> 5;
370 int idx
= (addr
& 0x1f) >> 3;
371 s
->dma_regs
[entry
][idx
] = val
;
378 /* Interval timer reload */
381 qemu_irq_lower(s
->timer_irq
);
388 RC4030_ERROR("invalid write of 0x%02x at [" TARGET_FMT_plx
"]\n", val
, addr
);
393 static void rc4030_writew(void *opaque
, hwaddr addr
, uint32_t val
)
395 uint32_t old_val
= rc4030_readl(opaque
, addr
& ~0x3);
398 val
= (val
<< 16) | (old_val
& 0x0000ffff);
400 val
= val
| (old_val
& 0xffff0000);
401 rc4030_writel(opaque
, addr
& ~0x3, val
);
404 static void rc4030_writeb(void *opaque
, hwaddr addr
, uint32_t val
)
406 uint32_t old_val
= rc4030_readl(opaque
, addr
& ~0x3);
410 val
= val
| (old_val
& 0xffffff00);
413 val
= (val
<< 8) | (old_val
& 0xffff00ff);
416 val
= (val
<< 16) | (old_val
& 0xff00ffff);
419 val
= (val
<< 24) | (old_val
& 0x00ffffff);
422 rc4030_writel(opaque
, addr
& ~0x3, val
);
425 static const MemoryRegionOps rc4030_ops
= {
427 .read
= { rc4030_readb
, rc4030_readw
, rc4030_readl
, },
428 .write
= { rc4030_writeb
, rc4030_writew
, rc4030_writel
, },
430 .endianness
= DEVICE_NATIVE_ENDIAN
,
433 static void update_jazz_irq(rc4030State
*s
)
437 pending
= s
->isr_jazz
& s
->imr_jazz
;
440 if (s
->isr_jazz
!= 0) {
442 DPRINTF("pending irqs:");
443 for (irq
= 0; irq
< ARRAY_SIZE(irq_names
); irq
++) {
444 if (s
->isr_jazz
& (1 << irq
)) {
445 printf(" %s", irq_names
[irq
]);
446 if (!(s
->imr_jazz
& (1 << irq
))) {
456 qemu_irq_raise(s
->jazz_bus_irq
);
458 qemu_irq_lower(s
->jazz_bus_irq
);
461 static void rc4030_irq_jazz_request(void *opaque
, int irq
, int level
)
463 rc4030State
*s
= opaque
;
466 s
->isr_jazz
|= 1 << irq
;
468 s
->isr_jazz
&= ~(1 << irq
);
474 static void rc4030_periodic_timer(void *opaque
)
476 rc4030State
*s
= opaque
;
479 qemu_irq_raise(s
->timer_irq
);
482 static uint32_t jazzio_readw(void *opaque
, hwaddr addr
)
484 rc4030State
*s
= opaque
;
490 /* Local bus int source */
492 uint32_t pending
= s
->isr_jazz
& s
->imr_jazz
;
497 DPRINTF("returning irq %s\n", irq_names
[irq
]);
498 val
= (irq
+ 1) << 2;
506 /* Local bus int enable mask */
511 RC4030_ERROR("(jazz io controller) invalid read [" TARGET_FMT_plx
"]\n", addr
);
515 DPRINTF("(jazz io controller) read 0x%04x at " TARGET_FMT_plx
"\n", val
, addr
);
520 static uint32_t jazzio_readb(void *opaque
, hwaddr addr
)
523 v
= jazzio_readw(opaque
, addr
& ~0x1);
524 return (v
>> (8 * (addr
& 0x1))) & 0xff;
527 static uint32_t jazzio_readl(void *opaque
, hwaddr addr
)
530 v
= jazzio_readw(opaque
, addr
);
531 v
|= jazzio_readw(opaque
, addr
+ 2) << 16;
535 static void jazzio_writew(void *opaque
, hwaddr addr
, uint32_t val
)
537 rc4030State
*s
= opaque
;
540 DPRINTF("(jazz io controller) write 0x%04x at " TARGET_FMT_plx
"\n", val
, addr
);
543 /* Local bus int enable mask */
549 RC4030_ERROR("(jazz io controller) invalid write of 0x%04x at [" TARGET_FMT_plx
"]\n", val
, addr
);
554 static void jazzio_writeb(void *opaque
, hwaddr addr
, uint32_t val
)
556 uint32_t old_val
= jazzio_readw(opaque
, addr
& ~0x1);
560 val
= val
| (old_val
& 0xff00);
563 val
= (val
<< 8) | (old_val
& 0x00ff);
566 jazzio_writew(opaque
, addr
& ~0x1, val
);
569 static void jazzio_writel(void *opaque
, hwaddr addr
, uint32_t val
)
571 jazzio_writew(opaque
, addr
, val
& 0xffff);
572 jazzio_writew(opaque
, addr
+ 2, (val
>> 16) & 0xffff);
575 static const MemoryRegionOps jazzio_ops
= {
577 .read
= { jazzio_readb
, jazzio_readw
, jazzio_readl
, },
578 .write
= { jazzio_writeb
, jazzio_writew
, jazzio_writel
, },
580 .endianness
= DEVICE_NATIVE_ENDIAN
,
583 static void rc4030_reset(void *opaque
)
585 rc4030State
*s
= opaque
;
588 s
->config
= 0x410; /* some boards seem to accept 0x104 too */
590 s
->invalid_address_register
= 0;
592 memset(s
->dma_regs
, 0, sizeof(s
->dma_regs
));
593 s
->dma_tl_base
= s
->dma_tl_limit
= 0;
595 s
->remote_failed_address
= s
->memory_failed_address
= 0;
597 s
->cache_ptag
= s
->cache_ltag
= 0;
600 s
->offset210
= 0x18186;
601 s
->nvram_protect
= 7;
602 for (i
= 0; i
< 15; i
++)
604 s
->imr_jazz
= 0x10; /* XXX: required by firmware, but why? */
609 qemu_irq_lower(s
->timer_irq
);
610 qemu_irq_lower(s
->jazz_bus_irq
);
613 static int rc4030_load(QEMUFile
*f
, void *opaque
, int version_id
)
615 rc4030State
* s
= opaque
;
621 s
->config
= qemu_get_be32(f
);
622 s
->invalid_address_register
= qemu_get_be32(f
);
623 for (i
= 0; i
< 8; i
++)
624 for (j
= 0; j
< 4; j
++)
625 s
->dma_regs
[i
][j
] = qemu_get_be32(f
);
626 s
->dma_tl_base
= qemu_get_be32(f
);
627 s
->dma_tl_limit
= qemu_get_be32(f
);
628 s
->cache_maint
= qemu_get_be32(f
);
629 s
->remote_failed_address
= qemu_get_be32(f
);
630 s
->memory_failed_address
= qemu_get_be32(f
);
631 s
->cache_ptag
= qemu_get_be32(f
);
632 s
->cache_ltag
= qemu_get_be32(f
);
633 s
->cache_bmask
= qemu_get_be32(f
);
634 s
->offset210
= qemu_get_be32(f
);
635 s
->nvram_protect
= qemu_get_be32(f
);
636 for (i
= 0; i
< 15; i
++)
637 s
->rem_speed
[i
] = qemu_get_be32(f
);
638 s
->imr_jazz
= qemu_get_be32(f
);
639 s
->isr_jazz
= qemu_get_be32(f
);
640 s
->itr
= qemu_get_be32(f
);
648 static void rc4030_save(QEMUFile
*f
, void *opaque
)
650 rc4030State
* s
= opaque
;
653 qemu_put_be32(f
, s
->config
);
654 qemu_put_be32(f
, s
->invalid_address_register
);
655 for (i
= 0; i
< 8; i
++)
656 for (j
= 0; j
< 4; j
++)
657 qemu_put_be32(f
, s
->dma_regs
[i
][j
]);
658 qemu_put_be32(f
, s
->dma_tl_base
);
659 qemu_put_be32(f
, s
->dma_tl_limit
);
660 qemu_put_be32(f
, s
->cache_maint
);
661 qemu_put_be32(f
, s
->remote_failed_address
);
662 qemu_put_be32(f
, s
->memory_failed_address
);
663 qemu_put_be32(f
, s
->cache_ptag
);
664 qemu_put_be32(f
, s
->cache_ltag
);
665 qemu_put_be32(f
, s
->cache_bmask
);
666 qemu_put_be32(f
, s
->offset210
);
667 qemu_put_be32(f
, s
->nvram_protect
);
668 for (i
= 0; i
< 15; i
++)
669 qemu_put_be32(f
, s
->rem_speed
[i
]);
670 qemu_put_be32(f
, s
->imr_jazz
);
671 qemu_put_be32(f
, s
->isr_jazz
);
672 qemu_put_be32(f
, s
->itr
);
675 void rc4030_dma_memory_rw(void *opaque
, hwaddr addr
, uint8_t *buf
, int len
, int is_write
)
677 rc4030State
*s
= opaque
;
680 dma_pagetable_entry entry
;
690 ncpy
= DMA_PAGESIZE
- (addr
& (DMA_PAGESIZE
- 1));
694 /* Get DMA translation table entry */
695 index
= addr
/ DMA_PAGESIZE
;
696 if (index
>= s
->dma_tl_limit
/ sizeof(dma_pagetable_entry
)) {
699 entry_addr
= s
->dma_tl_base
+ index
* sizeof(dma_pagetable_entry
);
700 /* XXX: not sure. should we really use only lowest bits? */
701 entry_addr
&= 0x7fffffff;
702 cpu_physical_memory_read(entry_addr
, &entry
, sizeof(entry
));
704 /* Read/write data at right place */
705 phys_addr
= entry
.frame
+ (addr
& (DMA_PAGESIZE
- 1));
706 cpu_physical_memory_rw(phys_addr
, &buf
[i
], ncpy
, is_write
);
713 static void rc4030_do_dma(void *opaque
, int n
, uint8_t *buf
, int len
, int is_write
)
715 rc4030State
*s
= opaque
;
719 s
->dma_regs
[n
][DMA_REG_ENABLE
] &= ~(DMA_FLAG_TC_INTR
| DMA_FLAG_MEM_INTR
| DMA_FLAG_ADDR_INTR
);
721 /* Check DMA channel consistency */
722 dev_to_mem
= (s
->dma_regs
[n
][DMA_REG_ENABLE
] & DMA_FLAG_MEM_TO_DEV
) ? 0 : 1;
723 if (!(s
->dma_regs
[n
][DMA_REG_ENABLE
] & DMA_FLAG_ENABLE
) ||
724 (is_write
!= dev_to_mem
)) {
725 s
->dma_regs
[n
][DMA_REG_ENABLE
] |= DMA_FLAG_MEM_INTR
;
726 s
->nmi_interrupt
|= 1 << n
;
730 /* Get start address and len */
731 if (len
> s
->dma_regs
[n
][DMA_REG_COUNT
])
732 len
= s
->dma_regs
[n
][DMA_REG_COUNT
];
733 dma_addr
= s
->dma_regs
[n
][DMA_REG_ADDRESS
];
735 /* Read/write data at right place */
736 rc4030_dma_memory_rw(opaque
, dma_addr
, buf
, len
, is_write
);
738 s
->dma_regs
[n
][DMA_REG_ENABLE
] |= DMA_FLAG_TC_INTR
;
739 s
->dma_regs
[n
][DMA_REG_COUNT
] -= len
;
741 #ifdef DEBUG_RC4030_DMA
744 printf("rc4030 dma: Copying %d bytes %s host %p\n",
745 len
, is_write
? "from" : "to", buf
);
746 for (i
= 0; i
< len
; i
+= 16) {
751 for (j
= 0; j
< n
; j
++)
752 printf("%02x ", buf
[i
+ j
]);
756 for (j
= 0; j
< n
; j
++)
757 printf("%c", isprint(buf
[i
+ j
]) ? buf
[i
+ j
] : '.');
764 struct rc4030DMAState
{
769 void rc4030_dma_read(void *dma
, uint8_t *buf
, int len
)
772 rc4030_do_dma(s
->opaque
, s
->n
, buf
, len
, 0);
775 void rc4030_dma_write(void *dma
, uint8_t *buf
, int len
)
778 rc4030_do_dma(s
->opaque
, s
->n
, buf
, len
, 1);
781 static rc4030_dma
*rc4030_allocate_dmas(void *opaque
, int n
)
784 struct rc4030DMAState
*p
;
787 s
= (rc4030_dma
*)g_malloc0(sizeof(rc4030_dma
) * n
);
788 p
= (struct rc4030DMAState
*)g_malloc0(sizeof(struct rc4030DMAState
) * n
);
789 for (i
= 0; i
< n
; i
++) {
798 void *rc4030_init(qemu_irq timer
, qemu_irq jazz_bus
,
799 qemu_irq
**irqs
, rc4030_dma
**dmas
,
800 MemoryRegion
*sysmem
)
804 s
= g_malloc0(sizeof(rc4030State
));
806 *irqs
= qemu_allocate_irqs(rc4030_irq_jazz_request
, s
, 16);
807 *dmas
= rc4030_allocate_dmas(s
, 4);
809 s
->periodic_timer
= timer_new_ns(QEMU_CLOCK_VIRTUAL
, rc4030_periodic_timer
, s
);
810 s
->timer_irq
= timer
;
811 s
->jazz_bus_irq
= jazz_bus
;
813 qemu_register_reset(rc4030_reset
, s
);
814 register_savevm(NULL
, "rc4030", 0, 2, rc4030_save
, rc4030_load
, s
);
817 memory_region_init_io(&s
->iomem_chipset
, NULL
, &rc4030_ops
, s
,
818 "rc4030.chipset", 0x300);
819 memory_region_add_subregion(sysmem
, 0x80000000, &s
->iomem_chipset
);
820 memory_region_init_io(&s
->iomem_jazzio
, NULL
, &jazzio_ops
, s
,
821 "rc4030.jazzio", 0x00001000);
822 memory_region_add_subregion(sysmem
, 0xf0000000, &s
->iomem_jazzio
);