2 * QEMU JAZZ RC4030 chipset
4 * Copyright (c) 2007-2013 Hervé 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
25 #include "qemu/osdep.h"
27 #include "hw/mips/mips.h"
28 #include "hw/sysbus.h"
29 #include "qemu/timer.h"
31 #include "exec/address-spaces.h"
34 /********************************************************/
35 /* rc4030 emulation */
37 #define MAX_TL_ENTRIES 512
39 typedef struct dma_pagetable_entry
{
42 } QEMU_PACKED dma_pagetable_entry
;
44 #define DMA_PAGESIZE 4096
45 #define DMA_REG_ENABLE 1
46 #define DMA_REG_COUNT 2
47 #define DMA_REG_ADDRESS 3
49 #define DMA_FLAG_ENABLE 0x0001
50 #define DMA_FLAG_MEM_TO_DEV 0x0002
51 #define DMA_FLAG_TC_INTR 0x0100
52 #define DMA_FLAG_MEM_INTR 0x0200
53 #define DMA_FLAG_ADDR_INTR 0x0400
55 #define TYPE_RC4030 "rc4030"
57 OBJECT_CHECK(rc4030State, (obj), TYPE_RC4030)
59 typedef struct rc4030State
63 uint32_t config
; /* 0x0000: RC4030 config register */
64 uint32_t revision
; /* 0x0008: RC4030 Revision register */
65 uint32_t invalid_address_register
; /* 0x0010: Invalid Address register */
68 uint32_t dma_regs
[8][4];
69 uint32_t dma_tl_base
; /* 0x0018: DMA transl. table base */
70 uint32_t dma_tl_limit
; /* 0x0020: DMA transl. table limit */
73 uint32_t cache_maint
; /* 0x0030: Cache Maintenance */
74 uint32_t remote_failed_address
; /* 0x0038: Remote Failed Address */
75 uint32_t memory_failed_address
; /* 0x0040: Memory Failed Address */
76 uint32_t cache_ptag
; /* 0x0048: I/O Cache Physical Tag */
77 uint32_t cache_ltag
; /* 0x0050: I/O Cache Logical Tag */
78 uint32_t cache_bmask
; /* 0x0058: I/O Cache Byte Mask */
80 uint32_t nmi_interrupt
; /* 0x0200: interrupt source */
81 uint32_t memory_refresh_rate
; /* 0x0210: memory refresh rate */
82 uint32_t nvram_protect
; /* 0x0220: NV ram protect register */
83 uint32_t rem_speed
[16];
84 uint32_t imr_jazz
; /* Local bus int enable mask */
85 uint32_t isr_jazz
; /* Local bus int source */
88 QEMUTimer
*periodic_timer
;
89 uint32_t itr
; /* Interval timer reload */
92 qemu_irq jazz_bus_irq
;
94 /* biggest translation table */
96 /* translation table memory region alias, added to system RAM */
97 MemoryRegion dma_tt_alias
;
98 /* whole DMA memory region, root of DMA address space */
100 /* translation table entry aliases, added to DMA memory region */
101 MemoryRegion dma_mrs
[MAX_TL_ENTRIES
];
104 MemoryRegion iomem_chipset
;
105 MemoryRegion iomem_jazzio
;
108 static void set_next_tick(rc4030State
*s
)
110 qemu_irq_lower(s
->timer_irq
);
113 tm_hz
= 1000 / (s
->itr
+ 1);
115 timer_mod(s
->periodic_timer
, qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL
) +
116 NANOSECONDS_PER_SECOND
/ tm_hz
);
119 /* called for accesses to rc4030 */
120 static uint64_t rc4030_read(void *opaque
, hwaddr addr
, unsigned int size
)
122 rc4030State
*s
= opaque
;
126 switch (addr
& ~0x3) {
127 /* Global config register */
131 /* Revision register */
135 /* Invalid Address register */
137 val
= s
->invalid_address_register
;
139 /* DMA transl. table base */
141 val
= s
->dma_tl_base
;
143 /* DMA transl. table limit */
145 val
= s
->dma_tl_limit
;
147 /* Remote Failed Address */
149 val
= s
->remote_failed_address
;
151 /* Memory Failed Address */
153 val
= s
->memory_failed_address
;
155 /* I/O Cache Byte Mask */
157 val
= s
->cache_bmask
;
159 if (s
->cache_bmask
== (uint32_t)-1)
162 /* Remote Speed Registers */
179 val
= s
->rem_speed
[(addr
- 0x0070) >> 3];
181 /* DMA channel base address */
215 int entry
= (addr
- 0x0100) >> 5;
216 int idx
= (addr
& 0x1f) >> 3;
217 val
= s
->dma_regs
[entry
][idx
];
220 /* Interrupt source */
222 val
= s
->nmi_interrupt
;
228 /* Memory refresh rate */
230 val
= s
->memory_refresh_rate
;
232 /* NV ram protect register */
234 val
= s
->nvram_protect
;
236 /* Interval timer count */
239 qemu_irq_lower(s
->timer_irq
);
243 val
= 7; /* FIXME: should be read from EISA controller */
246 qemu_log_mask(LOG_GUEST_ERROR
,
247 "rc4030: invalid read at 0x%x", (int)addr
);
252 if ((addr
& ~3) != 0x230) {
253 trace_rc4030_read(addr
, val
);
259 static void rc4030_dma_as_update_one(rc4030State
*s
, int index
, uint32_t frame
)
261 if (index
< MAX_TL_ENTRIES
) {
262 memory_region_set_enabled(&s
->dma_mrs
[index
], false);
269 if (index
>= MAX_TL_ENTRIES
) {
270 qemu_log_mask(LOG_UNIMP
,
271 "rc4030: trying to use too high "
272 "translation table entry %d (max allowed=%d)",
273 index
, MAX_TL_ENTRIES
);
276 memory_region_set_alias_offset(&s
->dma_mrs
[index
], frame
);
277 memory_region_set_enabled(&s
->dma_mrs
[index
], true);
280 static void rc4030_dma_tt_write(void *opaque
, hwaddr addr
, uint64_t data
,
283 rc4030State
*s
= opaque
;
286 memcpy(memory_region_get_ram_ptr(&s
->dma_tt
) + addr
, &data
, size
);
288 /* update dma address space (only if frame field has been written) */
289 if (addr
% sizeof(dma_pagetable_entry
) == 0) {
290 int index
= addr
/ sizeof(dma_pagetable_entry
);
291 memory_region_transaction_begin();
292 rc4030_dma_as_update_one(s
, index
, (uint32_t)data
);
293 memory_region_transaction_commit();
297 static const MemoryRegionOps rc4030_dma_tt_ops
= {
298 .write
= rc4030_dma_tt_write
,
299 .impl
.min_access_size
= 4,
300 .impl
.max_access_size
= 4,
303 static void rc4030_dma_tt_update(rc4030State
*s
, uint32_t new_tl_base
,
304 uint32_t new_tl_limit
)
307 dma_pagetable_entry
*dma_tl_contents
;
309 if (s
->dma_tl_limit
) {
310 /* write old dma tl table to physical memory */
311 memory_region_del_subregion(get_system_memory(), &s
->dma_tt_alias
);
312 cpu_physical_memory_write(s
->dma_tl_limit
& 0x7fffffff,
313 memory_region_get_ram_ptr(&s
->dma_tt
),
314 memory_region_size(&s
->dma_tt_alias
));
316 object_unparent(OBJECT(&s
->dma_tt_alias
));
318 s
->dma_tl_base
= new_tl_base
;
319 s
->dma_tl_limit
= new_tl_limit
;
320 new_tl_base
&= 0x7fffffff;
322 if (s
->dma_tl_limit
) {
323 uint64_t dma_tt_size
;
324 if (s
->dma_tl_limit
<= memory_region_size(&s
->dma_tt
)) {
325 dma_tt_size
= s
->dma_tl_limit
;
327 dma_tt_size
= memory_region_size(&s
->dma_tt
);
329 memory_region_init_alias(&s
->dma_tt_alias
, OBJECT(s
),
331 &s
->dma_tt
, 0, dma_tt_size
);
332 dma_tl_contents
= memory_region_get_ram_ptr(&s
->dma_tt
);
333 cpu_physical_memory_read(new_tl_base
, dma_tl_contents
, dma_tt_size
);
335 memory_region_transaction_begin();
336 entries
= dma_tt_size
/ sizeof(dma_pagetable_entry
);
337 for (i
= 0; i
< entries
; i
++) {
338 rc4030_dma_as_update_one(s
, i
, dma_tl_contents
[i
].frame
);
340 memory_region_add_subregion(get_system_memory(), new_tl_base
,
342 memory_region_transaction_commit();
344 memory_region_init(&s
->dma_tt_alias
, OBJECT(s
),
345 "dma-table-alias", 0);
349 static void rc4030_write(void *opaque
, hwaddr addr
, uint64_t data
,
352 rc4030State
*s
= opaque
;
356 trace_rc4030_write(addr
, val
);
358 switch (addr
& ~0x3) {
359 /* Global config register */
363 /* DMA transl. table base */
365 rc4030_dma_tt_update(s
, val
, s
->dma_tl_limit
);
367 /* DMA transl. table limit */
369 rc4030_dma_tt_update(s
, s
->dma_tl_base
, val
);
371 /* DMA transl. table invalidated */
374 /* Cache Maintenance */
376 s
->cache_maint
= val
;
378 /* I/O Cache Physical Tag */
382 /* I/O Cache Logical Tag */
386 /* I/O Cache Byte Mask */
388 s
->cache_bmask
|= val
; /* HACK */
390 /* I/O Cache Buffer Window */
393 if (s
->cache_ltag
== 0x80000001 && s
->cache_bmask
== 0xf0f0f0f) {
394 hwaddr dest
= s
->cache_ptag
& ~0x1;
395 dest
+= (s
->cache_maint
& 0x3) << 3;
396 cpu_physical_memory_write(dest
, &val
, 4);
399 /* Remote Speed Registers */
416 s
->rem_speed
[(addr
- 0x0070) >> 3] = val
;
418 /* DMA channel base address */
452 int entry
= (addr
- 0x0100) >> 5;
453 int idx
= (addr
& 0x1f) >> 3;
454 s
->dma_regs
[entry
][idx
] = val
;
457 /* Memory refresh rate */
459 s
->memory_refresh_rate
= val
;
461 /* Interval timer reload */
464 qemu_irq_lower(s
->timer_irq
);
471 qemu_log_mask(LOG_GUEST_ERROR
,
472 "rc4030: invalid write of 0x%02x at 0x%x",
478 static const MemoryRegionOps rc4030_ops
= {
480 .write
= rc4030_write
,
481 .impl
.min_access_size
= 4,
482 .impl
.max_access_size
= 4,
483 .endianness
= DEVICE_NATIVE_ENDIAN
,
486 static void update_jazz_irq(rc4030State
*s
)
490 pending
= s
->isr_jazz
& s
->imr_jazz
;
493 qemu_irq_raise(s
->jazz_bus_irq
);
495 qemu_irq_lower(s
->jazz_bus_irq
);
498 static void rc4030_irq_jazz_request(void *opaque
, int irq
, int level
)
500 rc4030State
*s
= opaque
;
503 s
->isr_jazz
|= 1 << irq
;
505 s
->isr_jazz
&= ~(1 << irq
);
511 static void rc4030_periodic_timer(void *opaque
)
513 rc4030State
*s
= opaque
;
516 qemu_irq_raise(s
->timer_irq
);
519 static uint64_t jazzio_read(void *opaque
, hwaddr addr
, unsigned int size
)
521 rc4030State
*s
= opaque
;
527 /* Local bus int source */
529 uint32_t pending
= s
->isr_jazz
& s
->imr_jazz
;
534 val
= (irq
+ 1) << 2;
542 /* Local bus int enable mask */
547 qemu_log_mask(LOG_GUEST_ERROR
,
548 "rc4030/jazzio: invalid read at 0x%x", (int)addr
);
553 trace_jazzio_read(addr
, val
);
558 static void jazzio_write(void *opaque
, hwaddr addr
, uint64_t data
,
561 rc4030State
*s
= opaque
;
565 trace_jazzio_write(addr
, val
);
568 /* Local bus int enable mask */
574 qemu_log_mask(LOG_GUEST_ERROR
,
575 "rc4030/jazzio: invalid write of 0x%02x at 0x%x",
581 static const MemoryRegionOps jazzio_ops
= {
583 .write
= jazzio_write
,
584 .impl
.min_access_size
= 2,
585 .impl
.max_access_size
= 2,
586 .endianness
= DEVICE_NATIVE_ENDIAN
,
589 static void rc4030_reset(DeviceState
*dev
)
591 rc4030State
*s
= RC4030(dev
);
594 s
->config
= 0x410; /* some boards seem to accept 0x104 too */
596 s
->invalid_address_register
= 0;
598 memset(s
->dma_regs
, 0, sizeof(s
->dma_regs
));
599 rc4030_dma_tt_update(s
, 0, 0);
601 s
->remote_failed_address
= s
->memory_failed_address
= 0;
603 s
->cache_ptag
= s
->cache_ltag
= 0;
606 s
->memory_refresh_rate
= 0x18186;
607 s
->nvram_protect
= 7;
608 for (i
= 0; i
< 15; i
++)
610 s
->imr_jazz
= 0x10; /* XXX: required by firmware, but why? */
615 qemu_irq_lower(s
->timer_irq
);
616 qemu_irq_lower(s
->jazz_bus_irq
);
619 static int rc4030_load(QEMUFile
*f
, void *opaque
, int version_id
)
621 rc4030State
* s
= opaque
;
627 s
->config
= qemu_get_be32(f
);
628 s
->invalid_address_register
= qemu_get_be32(f
);
629 for (i
= 0; i
< 8; i
++)
630 for (j
= 0; j
< 4; j
++)
631 s
->dma_regs
[i
][j
] = qemu_get_be32(f
);
632 s
->dma_tl_base
= qemu_get_be32(f
);
633 s
->dma_tl_limit
= qemu_get_be32(f
);
634 s
->cache_maint
= qemu_get_be32(f
);
635 s
->remote_failed_address
= qemu_get_be32(f
);
636 s
->memory_failed_address
= qemu_get_be32(f
);
637 s
->cache_ptag
= qemu_get_be32(f
);
638 s
->cache_ltag
= qemu_get_be32(f
);
639 s
->cache_bmask
= qemu_get_be32(f
);
640 s
->memory_refresh_rate
= qemu_get_be32(f
);
641 s
->nvram_protect
= qemu_get_be32(f
);
642 for (i
= 0; i
< 15; i
++)
643 s
->rem_speed
[i
] = qemu_get_be32(f
);
644 s
->imr_jazz
= qemu_get_be32(f
);
645 s
->isr_jazz
= qemu_get_be32(f
);
646 s
->itr
= qemu_get_be32(f
);
654 static void rc4030_save(QEMUFile
*f
, void *opaque
)
656 rc4030State
* s
= opaque
;
659 qemu_put_be32(f
, s
->config
);
660 qemu_put_be32(f
, s
->invalid_address_register
);
661 for (i
= 0; i
< 8; i
++)
662 for (j
= 0; j
< 4; j
++)
663 qemu_put_be32(f
, s
->dma_regs
[i
][j
]);
664 qemu_put_be32(f
, s
->dma_tl_base
);
665 qemu_put_be32(f
, s
->dma_tl_limit
);
666 qemu_put_be32(f
, s
->cache_maint
);
667 qemu_put_be32(f
, s
->remote_failed_address
);
668 qemu_put_be32(f
, s
->memory_failed_address
);
669 qemu_put_be32(f
, s
->cache_ptag
);
670 qemu_put_be32(f
, s
->cache_ltag
);
671 qemu_put_be32(f
, s
->cache_bmask
);
672 qemu_put_be32(f
, s
->memory_refresh_rate
);
673 qemu_put_be32(f
, s
->nvram_protect
);
674 for (i
= 0; i
< 15; i
++)
675 qemu_put_be32(f
, s
->rem_speed
[i
]);
676 qemu_put_be32(f
, s
->imr_jazz
);
677 qemu_put_be32(f
, s
->isr_jazz
);
678 qemu_put_be32(f
, s
->itr
);
681 static void rc4030_do_dma(void *opaque
, int n
, uint8_t *buf
, int len
, int is_write
)
683 rc4030State
*s
= opaque
;
687 s
->dma_regs
[n
][DMA_REG_ENABLE
] &= ~(DMA_FLAG_TC_INTR
| DMA_FLAG_MEM_INTR
| DMA_FLAG_ADDR_INTR
);
689 /* Check DMA channel consistency */
690 dev_to_mem
= (s
->dma_regs
[n
][DMA_REG_ENABLE
] & DMA_FLAG_MEM_TO_DEV
) ? 0 : 1;
691 if (!(s
->dma_regs
[n
][DMA_REG_ENABLE
] & DMA_FLAG_ENABLE
) ||
692 (is_write
!= dev_to_mem
)) {
693 s
->dma_regs
[n
][DMA_REG_ENABLE
] |= DMA_FLAG_MEM_INTR
;
694 s
->nmi_interrupt
|= 1 << n
;
698 /* Get start address and len */
699 if (len
> s
->dma_regs
[n
][DMA_REG_COUNT
])
700 len
= s
->dma_regs
[n
][DMA_REG_COUNT
];
701 dma_addr
= s
->dma_regs
[n
][DMA_REG_ADDRESS
];
703 /* Read/write data at right place */
704 address_space_rw(&s
->dma_as
, dma_addr
, MEMTXATTRS_UNSPECIFIED
,
707 s
->dma_regs
[n
][DMA_REG_ENABLE
] |= DMA_FLAG_TC_INTR
;
708 s
->dma_regs
[n
][DMA_REG_COUNT
] -= len
;
711 struct rc4030DMAState
{
716 void rc4030_dma_read(void *dma
, uint8_t *buf
, int len
)
719 rc4030_do_dma(s
->opaque
, s
->n
, buf
, len
, 0);
722 void rc4030_dma_write(void *dma
, uint8_t *buf
, int len
)
725 rc4030_do_dma(s
->opaque
, s
->n
, buf
, len
, 1);
728 static rc4030_dma
*rc4030_allocate_dmas(void *opaque
, int n
)
731 struct rc4030DMAState
*p
;
734 s
= (rc4030_dma
*)g_malloc0(sizeof(rc4030_dma
) * n
);
735 p
= (struct rc4030DMAState
*)g_malloc0(sizeof(struct rc4030DMAState
) * n
);
736 for (i
= 0; i
< n
; i
++) {
745 static void rc4030_initfn(Object
*obj
)
747 DeviceState
*dev
= DEVICE(obj
);
748 rc4030State
*s
= RC4030(obj
);
749 SysBusDevice
*sysbus
= SYS_BUS_DEVICE(obj
);
751 qdev_init_gpio_in(dev
, rc4030_irq_jazz_request
, 16);
753 sysbus_init_irq(sysbus
, &s
->timer_irq
);
754 sysbus_init_irq(sysbus
, &s
->jazz_bus_irq
);
756 register_savevm(NULL
, "rc4030", 0, 2, rc4030_save
, rc4030_load
, s
);
758 sysbus_init_mmio(sysbus
, &s
->iomem_chipset
);
759 sysbus_init_mmio(sysbus
, &s
->iomem_jazzio
);
762 static void rc4030_realize(DeviceState
*dev
, Error
**errp
)
764 rc4030State
*s
= RC4030(dev
);
765 Object
*o
= OBJECT(dev
);
768 s
->periodic_timer
= timer_new_ns(QEMU_CLOCK_VIRTUAL
,
769 rc4030_periodic_timer
, s
);
771 memory_region_init_io(&s
->iomem_chipset
, NULL
, &rc4030_ops
, s
,
772 "rc4030.chipset", 0x300);
773 memory_region_init_io(&s
->iomem_jazzio
, NULL
, &jazzio_ops
, s
,
774 "rc4030.jazzio", 0x00001000);
776 memory_region_init_rom_device(&s
->dma_tt
, o
,
777 &rc4030_dma_tt_ops
, s
, "dma-table",
778 MAX_TL_ENTRIES
* sizeof(dma_pagetable_entry
),
780 memory_region_init(&s
->dma_tt_alias
, o
, "dma-table-alias", 0);
781 memory_region_init(&s
->dma_mr
, o
, "dma", INT32_MAX
);
782 for (i
= 0; i
< MAX_TL_ENTRIES
; ++i
) {
783 memory_region_init_alias(&s
->dma_mrs
[i
], o
, "dma-alias",
784 get_system_memory(), 0, DMA_PAGESIZE
);
785 memory_region_set_enabled(&s
->dma_mrs
[i
], false);
786 memory_region_add_subregion(&s
->dma_mr
, i
* DMA_PAGESIZE
,
789 address_space_init(&s
->dma_as
, &s
->dma_mr
, "rc4030-dma");
792 static void rc4030_unrealize(DeviceState
*dev
, Error
**errp
)
794 rc4030State
*s
= RC4030(dev
);
797 timer_free(s
->periodic_timer
);
799 address_space_destroy(&s
->dma_as
);
800 object_unparent(OBJECT(&s
->dma_tt
));
801 object_unparent(OBJECT(&s
->dma_tt_alias
));
802 object_unparent(OBJECT(&s
->dma_mr
));
803 for (i
= 0; i
< MAX_TL_ENTRIES
; ++i
) {
804 memory_region_del_subregion(&s
->dma_mr
, &s
->dma_mrs
[i
]);
805 object_unparent(OBJECT(&s
->dma_mrs
[i
]));
809 static void rc4030_class_init(ObjectClass
*klass
, void *class_data
)
811 DeviceClass
*dc
= DEVICE_CLASS(klass
);
813 dc
->realize
= rc4030_realize
;
814 dc
->unrealize
= rc4030_unrealize
;
815 dc
->reset
= rc4030_reset
;
818 static const TypeInfo rc4030_info
= {
820 .parent
= TYPE_SYS_BUS_DEVICE
,
821 .instance_size
= sizeof(rc4030State
),
822 .instance_init
= rc4030_initfn
,
823 .class_init
= rc4030_class_init
,
826 static void rc4030_register_types(void)
828 type_register_static(&rc4030_info
);
831 type_init(rc4030_register_types
)
833 DeviceState
*rc4030_init(rc4030_dma
**dmas
, MemoryRegion
**dma_mr
)
837 dev
= qdev_create(NULL
, TYPE_RC4030
);
838 qdev_init_nofail(dev
);
840 *dmas
= rc4030_allocate_dmas(dev
, 4);
841 *dma_mr
= &RC4030(dev
)->dma_mr
;