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 "qapi/error.h"
11 #include "qemu/module.h"
16 #include "hw/sysbus.h"
17 #include "hw/m68k/mcf.h"
19 #define TYPE_MCF_INTC "mcf-intc"
20 #define MCF_INTC(obj) OBJECT_CHECK(mcf_intc_state, (obj), TYPE_MCF_INTC)
23 SysBusDevice parent_obj
;
35 static void mcf_intc_update(mcf_intc_state
*s
)
42 active
= (s
->ipr
| s
->ifr
) & s
->enabled
& ~s
->imr
;
46 for (i
= 0; i
< 64; i
++) {
47 if ((active
& 1) != 0 && s
->icr
[i
] >= best_level
) {
48 best_level
= s
->icr
[i
];
54 s
->active_vector
= ((best
== 64) ? 24 : (best
+ 64));
55 m68k_set_irq_level(s
->cpu
, best_level
, s
->active_vector
);
58 static uint64_t mcf_intc_read(void *opaque
, hwaddr addr
,
62 mcf_intc_state
*s
= (mcf_intc_state
*)opaque
;
64 if (offset
>= 0x40 && offset
< 0x80) {
65 return s
->icr
[offset
- 0x40];
69 return (uint32_t)(s
->ipr
>> 32);
71 return (uint32_t)s
->ipr
;
73 return (uint32_t)(s
->imr
>> 32);
75 return (uint32_t)s
->imr
;
77 return (uint32_t)(s
->ifr
>> 32);
79 return (uint32_t)s
->ifr
;
80 case 0xe0: /* SWIACK. */
81 return s
->active_vector
;
82 case 0xe1: case 0xe2: case 0xe3: case 0xe4:
83 case 0xe5: case 0xe6: case 0xe7:
85 qemu_log_mask(LOG_UNIMP
, "%s: LnIACK not implemented (offset 0x%02x)\n",
93 static void mcf_intc_write(void *opaque
, hwaddr addr
,
94 uint64_t val
, unsigned size
)
97 mcf_intc_state
*s
= (mcf_intc_state
*)opaque
;
99 if (offset
>= 0x40 && offset
< 0x80) {
100 int n
= offset
- 0x40;
103 s
->enabled
&= ~(1ull << n
);
105 s
->enabled
|= (1ull << n
);
110 case 0x00: case 0x04:
111 /* Ignore IPR writes. */
114 s
->imr
= (s
->imr
& 0xffffffff) | ((uint64_t)val
<< 32);
117 s
->imr
= (s
->imr
& 0xffffffff00000000ull
) | (uint32_t)val
;
123 s
->imr
|= (0x1ull
<< (val
& 0x3f));
130 s
->imr
&= ~(0x1ull
<< (val
& 0x3f));
134 qemu_log_mask(LOG_GUEST_ERROR
, "%s: Bad offset 0x%02x\n",
141 static void mcf_intc_set_irq(void *opaque
, int irq
, int level
)
143 mcf_intc_state
*s
= (mcf_intc_state
*)opaque
;
147 s
->ipr
|= 1ull << irq
;
149 s
->ipr
&= ~(1ull << irq
);
153 static void mcf_intc_reset(DeviceState
*dev
)
155 mcf_intc_state
*s
= MCF_INTC(dev
);
161 memset(s
->icr
, 0, 64);
162 s
->active_vector
= 24;
165 static const MemoryRegionOps mcf_intc_ops
= {
166 .read
= mcf_intc_read
,
167 .write
= mcf_intc_write
,
168 .endianness
= DEVICE_NATIVE_ENDIAN
,
171 static void mcf_intc_instance_init(Object
*obj
)
173 mcf_intc_state
*s
= MCF_INTC(obj
);
175 memory_region_init_io(&s
->iomem
, obj
, &mcf_intc_ops
, s
, "mcf", 0x100);
178 static void mcf_intc_class_init(ObjectClass
*oc
, void *data
)
180 DeviceClass
*dc
= DEVICE_CLASS(oc
);
182 set_bit(DEVICE_CATEGORY_MISC
, dc
->categories
);
183 dc
->reset
= mcf_intc_reset
;
186 static const TypeInfo mcf_intc_gate_info
= {
187 .name
= TYPE_MCF_INTC
,
188 .parent
= TYPE_SYS_BUS_DEVICE
,
189 .instance_size
= sizeof(mcf_intc_state
),
190 .instance_init
= mcf_intc_instance_init
,
191 .class_init
= mcf_intc_class_init
,
194 static void mcf_intc_register_types(void)
196 type_register_static(&mcf_intc_gate_info
);
199 type_init(mcf_intc_register_types
)
201 qemu_irq
*mcf_intc_init(MemoryRegion
*sysmem
,
208 dev
= qdev_new(TYPE_MCF_INTC
);
209 sysbus_realize_and_unref(SYS_BUS_DEVICE(dev
), &error_fatal
);
214 memory_region_add_subregion(sysmem
, base
, &s
->iomem
);
216 return qemu_allocate_irqs(mcf_intc_set_irq
, s
, 64);