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 typedef struct dma_pagetable_entry
{
40 } QEMU_PACKED dma_pagetable_entry
;
42 #define DMA_PAGESIZE 4096
43 #define DMA_REG_ENABLE 1
44 #define DMA_REG_COUNT 2
45 #define DMA_REG_ADDRESS 3
47 #define DMA_FLAG_ENABLE 0x0001
48 #define DMA_FLAG_MEM_TO_DEV 0x0002
49 #define DMA_FLAG_TC_INTR 0x0100
50 #define DMA_FLAG_MEM_INTR 0x0200
51 #define DMA_FLAG_ADDR_INTR 0x0400
53 #define TYPE_RC4030 "rc4030"
55 OBJECT_CHECK(rc4030State, (obj), TYPE_RC4030)
57 #define TYPE_RC4030_IOMMU_MEMORY_REGION "rc4030-iommu-memory-region"
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 /* whole DMA memory region, root of DMA address space */
95 IOMMUMemoryRegion dma_mr
;
98 MemoryRegion iomem_chipset
;
99 MemoryRegion iomem_jazzio
;
102 static void set_next_tick(rc4030State
*s
)
105 qemu_irq_lower(s
->timer_irq
);
107 tm_hz
= 1000 / (s
->itr
+ 1);
109 timer_mod(s
->periodic_timer
, qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL
) +
110 NANOSECONDS_PER_SECOND
/ tm_hz
);
113 /* called for accesses to rc4030 */
114 static uint64_t rc4030_read(void *opaque
, hwaddr addr
, unsigned int size
)
116 rc4030State
*s
= opaque
;
120 switch (addr
& ~0x3) {
121 /* Global config register */
125 /* Revision register */
129 /* Invalid Address register */
131 val
= s
->invalid_address_register
;
133 /* DMA transl. table base */
135 val
= s
->dma_tl_base
;
137 /* DMA transl. table limit */
139 val
= s
->dma_tl_limit
;
141 /* Remote Failed Address */
143 val
= s
->remote_failed_address
;
145 /* Memory Failed Address */
147 val
= s
->memory_failed_address
;
149 /* I/O Cache Byte Mask */
151 val
= s
->cache_bmask
;
153 if (s
->cache_bmask
== (uint32_t)-1)
156 /* Remote Speed Registers */
173 val
= s
->rem_speed
[(addr
- 0x0070) >> 3];
175 /* DMA channel base address */
209 int entry
= (addr
- 0x0100) >> 5;
210 int idx
= (addr
& 0x1f) >> 3;
211 val
= s
->dma_regs
[entry
][idx
];
214 /* Interrupt source */
216 val
= s
->nmi_interrupt
;
222 /* Memory refresh rate */
224 val
= s
->memory_refresh_rate
;
226 /* NV ram protect register */
228 val
= s
->nvram_protect
;
230 /* Interval timer count */
233 qemu_irq_lower(s
->timer_irq
);
237 val
= 7; /* FIXME: should be read from EISA controller */
240 qemu_log_mask(LOG_GUEST_ERROR
,
241 "rc4030: invalid read at 0x%x", (int)addr
);
246 if ((addr
& ~3) != 0x230) {
247 trace_rc4030_read(addr
, val
);
253 static void rc4030_write(void *opaque
, hwaddr addr
, uint64_t data
,
256 rc4030State
*s
= opaque
;
260 trace_rc4030_write(addr
, val
);
262 switch (addr
& ~0x3) {
263 /* Global config register */
267 /* DMA transl. table base */
269 s
->dma_tl_base
= val
;
271 /* DMA transl. table limit */
273 s
->dma_tl_limit
= val
;
275 /* DMA transl. table invalidated */
278 /* Cache Maintenance */
280 s
->cache_maint
= val
;
282 /* I/O Cache Physical Tag */
286 /* I/O Cache Logical Tag */
290 /* I/O Cache Byte Mask */
292 s
->cache_bmask
|= val
; /* HACK */
294 /* I/O Cache Buffer Window */
297 if (s
->cache_ltag
== 0x80000001 && s
->cache_bmask
== 0xf0f0f0f) {
298 hwaddr dest
= s
->cache_ptag
& ~0x1;
299 dest
+= (s
->cache_maint
& 0x3) << 3;
300 cpu_physical_memory_write(dest
, &val
, 4);
303 /* Remote Speed Registers */
320 s
->rem_speed
[(addr
- 0x0070) >> 3] = val
;
322 /* DMA channel base address */
356 int entry
= (addr
- 0x0100) >> 5;
357 int idx
= (addr
& 0x1f) >> 3;
358 s
->dma_regs
[entry
][idx
] = val
;
361 /* Memory refresh rate */
363 s
->memory_refresh_rate
= val
;
365 /* Interval timer reload */
367 s
->itr
= val
& 0x01FF;
368 qemu_irq_lower(s
->timer_irq
);
375 qemu_log_mask(LOG_GUEST_ERROR
,
376 "rc4030: invalid write of 0x%02x at 0x%x",
382 static const MemoryRegionOps rc4030_ops
= {
384 .write
= rc4030_write
,
385 .impl
.min_access_size
= 4,
386 .impl
.max_access_size
= 4,
387 .endianness
= DEVICE_NATIVE_ENDIAN
,
390 static void update_jazz_irq(rc4030State
*s
)
394 pending
= s
->isr_jazz
& s
->imr_jazz
;
397 qemu_irq_raise(s
->jazz_bus_irq
);
399 qemu_irq_lower(s
->jazz_bus_irq
);
402 static void rc4030_irq_jazz_request(void *opaque
, int irq
, int level
)
404 rc4030State
*s
= opaque
;
407 s
->isr_jazz
|= 1 << irq
;
409 s
->isr_jazz
&= ~(1 << irq
);
415 static void rc4030_periodic_timer(void *opaque
)
417 rc4030State
*s
= opaque
;
420 qemu_irq_raise(s
->timer_irq
);
423 static uint64_t jazzio_read(void *opaque
, hwaddr addr
, unsigned int size
)
425 rc4030State
*s
= opaque
;
431 /* Local bus int source */
433 uint32_t pending
= s
->isr_jazz
& s
->imr_jazz
;
438 val
= (irq
+ 1) << 2;
446 /* Local bus int enable mask */
451 qemu_log_mask(LOG_GUEST_ERROR
,
452 "rc4030/jazzio: invalid read at 0x%x", (int)addr
);
457 trace_jazzio_read(addr
, val
);
462 static void jazzio_write(void *opaque
, hwaddr addr
, uint64_t data
,
465 rc4030State
*s
= opaque
;
469 trace_jazzio_write(addr
, val
);
472 /* Local bus int enable mask */
478 qemu_log_mask(LOG_GUEST_ERROR
,
479 "rc4030/jazzio: invalid write of 0x%02x at 0x%x",
485 static const MemoryRegionOps jazzio_ops
= {
487 .write
= jazzio_write
,
488 .impl
.min_access_size
= 2,
489 .impl
.max_access_size
= 2,
490 .endianness
= DEVICE_NATIVE_ENDIAN
,
493 static IOMMUTLBEntry
rc4030_dma_translate(IOMMUMemoryRegion
*iommu
, hwaddr addr
,
494 IOMMUAccessFlags flag
)
496 rc4030State
*s
= container_of(iommu
, rc4030State
, dma_mr
);
497 IOMMUTLBEntry ret
= {
498 .target_as
= &address_space_memory
,
499 .iova
= addr
& ~(DMA_PAGESIZE
- 1),
500 .translated_addr
= 0,
501 .addr_mask
= DMA_PAGESIZE
- 1,
504 uint64_t i
, entry_address
;
505 dma_pagetable_entry entry
;
507 i
= addr
/ DMA_PAGESIZE
;
508 if (i
< s
->dma_tl_limit
/ sizeof(entry
)) {
509 entry_address
= (s
->dma_tl_base
& 0x7fffffff) + i
* sizeof(entry
);
510 if (address_space_read(ret
.target_as
, entry_address
,
511 MEMTXATTRS_UNSPECIFIED
, (unsigned char *)&entry
,
512 sizeof(entry
)) == MEMTX_OK
) {
513 ret
.translated_addr
= entry
.frame
& ~(DMA_PAGESIZE
- 1);
521 static void rc4030_reset(DeviceState
*dev
)
523 rc4030State
*s
= RC4030(dev
);
526 s
->config
= 0x410; /* some boards seem to accept 0x104 too */
528 s
->invalid_address_register
= 0;
530 memset(s
->dma_regs
, 0, sizeof(s
->dma_regs
));
532 s
->remote_failed_address
= s
->memory_failed_address
= 0;
534 s
->cache_ptag
= s
->cache_ltag
= 0;
537 s
->memory_refresh_rate
= 0x18186;
538 s
->nvram_protect
= 7;
539 for (i
= 0; i
< 15; i
++)
541 s
->imr_jazz
= 0x10; /* XXX: required by firmware, but why? */
546 qemu_irq_lower(s
->timer_irq
);
547 qemu_irq_lower(s
->jazz_bus_irq
);
550 static int rc4030_post_load(void *opaque
, int version_id
)
552 rc4030State
* s
= opaque
;
560 static const VMStateDescription vmstate_rc4030
= {
563 .post_load
= rc4030_post_load
,
564 .fields
= (VMStateField
[]) {
565 VMSTATE_UINT32(config
, rc4030State
),
566 VMSTATE_UINT32(invalid_address_register
, rc4030State
),
567 VMSTATE_UINT32_2DARRAY(dma_regs
, rc4030State
, 8, 4),
568 VMSTATE_UINT32(dma_tl_base
, rc4030State
),
569 VMSTATE_UINT32(dma_tl_limit
, rc4030State
),
570 VMSTATE_UINT32(cache_maint
, rc4030State
),
571 VMSTATE_UINT32(remote_failed_address
, rc4030State
),
572 VMSTATE_UINT32(memory_failed_address
, rc4030State
),
573 VMSTATE_UINT32(cache_ptag
, rc4030State
),
574 VMSTATE_UINT32(cache_ltag
, rc4030State
),
575 VMSTATE_UINT32(cache_bmask
, rc4030State
),
576 VMSTATE_UINT32(memory_refresh_rate
, rc4030State
),
577 VMSTATE_UINT32(nvram_protect
, rc4030State
),
578 VMSTATE_UINT32_ARRAY(rem_speed
, rc4030State
, 16),
579 VMSTATE_UINT32(imr_jazz
, rc4030State
),
580 VMSTATE_UINT32(isr_jazz
, rc4030State
),
581 VMSTATE_UINT32(itr
, rc4030State
),
582 VMSTATE_END_OF_LIST()
586 static void rc4030_do_dma(void *opaque
, int n
, uint8_t *buf
, int len
, int is_write
)
588 rc4030State
*s
= opaque
;
592 s
->dma_regs
[n
][DMA_REG_ENABLE
] &= ~(DMA_FLAG_TC_INTR
| DMA_FLAG_MEM_INTR
| DMA_FLAG_ADDR_INTR
);
594 /* Check DMA channel consistency */
595 dev_to_mem
= (s
->dma_regs
[n
][DMA_REG_ENABLE
] & DMA_FLAG_MEM_TO_DEV
) ? 0 : 1;
596 if (!(s
->dma_regs
[n
][DMA_REG_ENABLE
] & DMA_FLAG_ENABLE
) ||
597 (is_write
!= dev_to_mem
)) {
598 s
->dma_regs
[n
][DMA_REG_ENABLE
] |= DMA_FLAG_MEM_INTR
;
599 s
->nmi_interrupt
|= 1 << n
;
603 /* Get start address and len */
604 if (len
> s
->dma_regs
[n
][DMA_REG_COUNT
])
605 len
= s
->dma_regs
[n
][DMA_REG_COUNT
];
606 dma_addr
= s
->dma_regs
[n
][DMA_REG_ADDRESS
];
608 /* Read/write data at right place */
609 address_space_rw(&s
->dma_as
, dma_addr
, MEMTXATTRS_UNSPECIFIED
,
612 s
->dma_regs
[n
][DMA_REG_ENABLE
] |= DMA_FLAG_TC_INTR
;
613 s
->dma_regs
[n
][DMA_REG_COUNT
] -= len
;
616 struct rc4030DMAState
{
621 void rc4030_dma_read(void *dma
, uint8_t *buf
, int len
)
624 rc4030_do_dma(s
->opaque
, s
->n
, buf
, len
, 0);
627 void rc4030_dma_write(void *dma
, uint8_t *buf
, int len
)
630 rc4030_do_dma(s
->opaque
, s
->n
, buf
, len
, 1);
633 static rc4030_dma
*rc4030_allocate_dmas(void *opaque
, int n
)
636 struct rc4030DMAState
*p
;
639 s
= (rc4030_dma
*)g_malloc0(sizeof(rc4030_dma
) * n
);
640 p
= (struct rc4030DMAState
*)g_malloc0(sizeof(struct rc4030DMAState
) * n
);
641 for (i
= 0; i
< n
; i
++) {
650 static void rc4030_initfn(Object
*obj
)
652 DeviceState
*dev
= DEVICE(obj
);
653 rc4030State
*s
= RC4030(obj
);
654 SysBusDevice
*sysbus
= SYS_BUS_DEVICE(obj
);
656 qdev_init_gpio_in(dev
, rc4030_irq_jazz_request
, 16);
658 sysbus_init_irq(sysbus
, &s
->timer_irq
);
659 sysbus_init_irq(sysbus
, &s
->jazz_bus_irq
);
661 sysbus_init_mmio(sysbus
, &s
->iomem_chipset
);
662 sysbus_init_mmio(sysbus
, &s
->iomem_jazzio
);
665 static void rc4030_realize(DeviceState
*dev
, Error
**errp
)
667 rc4030State
*s
= RC4030(dev
);
668 Object
*o
= OBJECT(dev
);
670 s
->periodic_timer
= timer_new_ns(QEMU_CLOCK_VIRTUAL
,
671 rc4030_periodic_timer
, s
);
673 memory_region_init_io(&s
->iomem_chipset
, NULL
, &rc4030_ops
, s
,
674 "rc4030.chipset", 0x300);
675 memory_region_init_io(&s
->iomem_jazzio
, NULL
, &jazzio_ops
, s
,
676 "rc4030.jazzio", 0x00001000);
678 memory_region_init_iommu(&s
->dma_mr
, sizeof(s
->dma_mr
),
679 TYPE_RC4030_IOMMU_MEMORY_REGION
,
680 o
, "rc4030.dma", UINT32_MAX
);
681 address_space_init(&s
->dma_as
, MEMORY_REGION(&s
->dma_mr
), "rc4030-dma");
684 static void rc4030_unrealize(DeviceState
*dev
, Error
**errp
)
686 rc4030State
*s
= RC4030(dev
);
688 timer_free(s
->periodic_timer
);
690 address_space_destroy(&s
->dma_as
);
691 object_unparent(OBJECT(&s
->dma_mr
));
694 static void rc4030_class_init(ObjectClass
*klass
, void *class_data
)
696 DeviceClass
*dc
= DEVICE_CLASS(klass
);
698 dc
->realize
= rc4030_realize
;
699 dc
->unrealize
= rc4030_unrealize
;
700 dc
->reset
= rc4030_reset
;
701 dc
->vmsd
= &vmstate_rc4030
;
704 static const TypeInfo rc4030_info
= {
706 .parent
= TYPE_SYS_BUS_DEVICE
,
707 .instance_size
= sizeof(rc4030State
),
708 .instance_init
= rc4030_initfn
,
709 .class_init
= rc4030_class_init
,
712 static void rc4030_iommu_memory_region_class_init(ObjectClass
*klass
,
715 IOMMUMemoryRegionClass
*imrc
= IOMMU_MEMORY_REGION_CLASS(klass
);
717 imrc
->translate
= rc4030_dma_translate
;
720 static const TypeInfo rc4030_iommu_memory_region_info
= {
721 .parent
= TYPE_IOMMU_MEMORY_REGION
,
722 .name
= TYPE_RC4030_IOMMU_MEMORY_REGION
,
723 .class_init
= rc4030_iommu_memory_region_class_init
,
726 static void rc4030_register_types(void)
728 type_register_static(&rc4030_info
);
729 type_register_static(&rc4030_iommu_memory_region_info
);
732 type_init(rc4030_register_types
)
734 DeviceState
*rc4030_init(rc4030_dma
**dmas
, IOMMUMemoryRegion
**dma_mr
)
738 dev
= qdev_create(NULL
, TYPE_RC4030
);
739 qdev_init_nofail(dev
);
741 *dmas
= rc4030_allocate_dmas(dev
, 4);
742 *dma_mr
= &RC4030(dev
)->dma_mr
;