2 * ColdFire Interrupt Controller emulation.
4 * Copyright (c) 2007 CodeSourcery.
6 * This code is licensed under the GPL
9 #include "qemu/osdep.h"
10 #include "qemu/module.h"
15 #include "hw/sysbus.h"
16 #include "hw/m68k/mcf.h"
18 #define TYPE_MCF_INTC "mcf-intc"
19 #define MCF_INTC(obj) OBJECT_CHECK(mcf_intc_state, (obj), TYPE_MCF_INTC)
22 SysBusDevice parent_obj;
34 static void mcf_intc_update(mcf_intc_state *s)
41 active = (s->ipr | s->ifr) & s->enabled & ~s->imr;
45 for (i = 0; i < 64; i++) {
46 if ((active & 1) != 0 && s->icr[i] >= best_level) {
47 best_level = s->icr[i];
53 s->active_vector = ((best == 64) ? 24 : (best + 64));
54 m68k_set_irq_level(s->cpu, best_level, s->active_vector);
57 static uint64_t mcf_intc_read(void *opaque, hwaddr addr,
61 mcf_intc_state *s = (mcf_intc_state *)opaque;
63 if (offset >= 0x40 && offset < 0x80) {
64 return s->icr[offset - 0x40];
68 return (uint32_t)(s->ipr >> 32);
70 return (uint32_t)s->ipr;
72 return (uint32_t)(s->imr >> 32);
74 return (uint32_t)s->imr;
76 return (uint32_t)(s->ifr >> 32);
78 return (uint32_t)s->ifr;
79 case 0xe0: /* SWIACK. */
80 return s->active_vector;
81 case 0xe1: case 0xe2: case 0xe3: case 0xe4:
82 case 0xe5: case 0xe6: case 0xe7:
84 qemu_log_mask(LOG_UNIMP, "%s: LnIACK not implemented (offset 0x%02x)\n",
92 static void mcf_intc_write(void *opaque, hwaddr addr,
93 uint64_t val, unsigned size)
96 mcf_intc_state *s = (mcf_intc_state *)opaque;
98 if (offset >= 0x40 && offset < 0x80) {
99 int n = offset - 0x40;
102 s->enabled &= ~(1ull << n);
104 s->enabled |= (1ull << n);
109 case 0x00: case 0x04:
110 /* Ignore IPR writes. */
113 s->imr = (s->imr & 0xffffffff) | ((uint64_t)val << 32);
116 s->imr = (s->imr & 0xffffffff00000000ull) | (uint32_t)val;
122 s->imr |= (0x1ull << (val & 0x3f));
129 s->imr &= ~(0x1ull << (val & 0x3f));
133 qemu_log_mask(LOG_GUEST_ERROR, "%s: Bad offset 0x%02x\n",
140 static void mcf_intc_set_irq(void *opaque, int irq, int level)
142 mcf_intc_state *s = (mcf_intc_state *)opaque;
146 s->ipr |= 1ull << irq;
148 s->ipr &= ~(1ull << irq);
152 static void mcf_intc_reset(DeviceState *dev)
154 mcf_intc_state *s = MCF_INTC(dev);
160 memset(s->icr, 0, 64);
161 s->active_vector = 24;
164 static const MemoryRegionOps mcf_intc_ops = {
165 .read = mcf_intc_read,
166 .write = mcf_intc_write,
167 .endianness = DEVICE_NATIVE_ENDIAN,
170 static void mcf_intc_instance_init(Object *obj)
172 mcf_intc_state *s = MCF_INTC(obj);
174 memory_region_init_io(&s->iomem, obj, &mcf_intc_ops, s, "mcf", 0x100);
177 static void mcf_intc_class_init(ObjectClass *oc, void *data)
179 DeviceClass *dc = DEVICE_CLASS(oc);
181 set_bit(DEVICE_CATEGORY_MISC, dc->categories);
182 dc->reset = mcf_intc_reset;
185 static const TypeInfo mcf_intc_gate_info = {
186 .name = TYPE_MCF_INTC,
187 .parent = TYPE_SYS_BUS_DEVICE,
188 .instance_size = sizeof(mcf_intc_state),
189 .instance_init = mcf_intc_instance_init,
190 .class_init = mcf_intc_class_init,
193 static void mcf_intc_register_types(void)
195 type_register_static(&mcf_intc_gate_info);
198 type_init(mcf_intc_register_types)
200 qemu_irq *mcf_intc_init(MemoryRegion *sysmem,
207 dev = qdev_create(NULL, TYPE_MCF_INTC);
208 qdev_init_nofail(dev);
213 memory_region_add_subregion(sysmem, base, &s->iomem);
215 return qemu_allocate_irqs(mcf_intc_set_irq, s, 64);