2 * ColdFire Interrupt Controller emulation.
4 * Copyright (c) 2007 CodeSourcery.
6 * This code is licensed under the GPL
8 #include "qemu/osdep.h"
9 #include "qemu-common.h"
12 #include "hw/sysbus.h"
13 #include "hw/m68k/mcf.h"
15 #define TYPE_MCF_INTC "mcf-intc"
16 #define MCF_INTC(obj) OBJECT_CHECK(mcf_intc_state, (obj), TYPE_MCF_INTC)
19 SysBusDevice parent_obj
;
31 static void mcf_intc_update(mcf_intc_state
*s
)
38 active
= (s
->ipr
| s
->ifr
) & s
->enabled
& ~s
->imr
;
42 for (i
= 0; i
< 64; i
++) {
43 if ((active
& 1) != 0 && s
->icr
[i
] >= best_level
) {
44 best_level
= s
->icr
[i
];
50 s
->active_vector
= ((best
== 64) ? 24 : (best
+ 64));
51 m68k_set_irq_level(s
->cpu
, best_level
, s
->active_vector
);
54 static uint64_t mcf_intc_read(void *opaque
, hwaddr addr
,
58 mcf_intc_state
*s
= (mcf_intc_state
*)opaque
;
60 if (offset
>= 0x40 && offset
< 0x80) {
61 return s
->icr
[offset
- 0x40];
65 return (uint32_t)(s
->ipr
>> 32);
67 return (uint32_t)s
->ipr
;
69 return (uint32_t)(s
->imr
>> 32);
71 return (uint32_t)s
->imr
;
73 return (uint32_t)(s
->ifr
>> 32);
75 return (uint32_t)s
->ifr
;
76 case 0xe0: /* SWIACK. */
77 return s
->active_vector
;
78 case 0xe1: case 0xe2: case 0xe3: case 0xe4:
79 case 0xe5: case 0xe6: case 0xe7:
81 hw_error("mcf_intc_read: LnIACK not implemented\n");
87 static void mcf_intc_write(void *opaque
, hwaddr addr
,
88 uint64_t val
, unsigned size
)
91 mcf_intc_state
*s
= (mcf_intc_state
*)opaque
;
93 if (offset
>= 0x40 && offset
< 0x80) {
94 int n
= offset
- 0x40;
97 s
->enabled
&= ~(1ull << n
);
99 s
->enabled
|= (1ull << n
);
104 case 0x00: case 0x04:
105 /* Ignore IPR writes. */
108 s
->imr
= (s
->imr
& 0xffffffff) | ((uint64_t)val
<< 32);
111 s
->imr
= (s
->imr
& 0xffffffff00000000ull
) | (uint32_t)val
;
117 s
->imr
|= (0x1ull
<< (val
& 0x3f));
124 s
->imr
&= ~(0x1ull
<< (val
& 0x3f));
128 hw_error("mcf_intc_write: Bad write offset %d\n", offset
);
134 static void mcf_intc_set_irq(void *opaque
, int irq
, int level
)
136 mcf_intc_state
*s
= (mcf_intc_state
*)opaque
;
140 s
->ipr
|= 1ull << irq
;
142 s
->ipr
&= ~(1ull << irq
);
146 static void mcf_intc_reset(DeviceState
*dev
)
148 mcf_intc_state
*s
= MCF_INTC(dev
);
154 memset(s
->icr
, 0, 64);
155 s
->active_vector
= 24;
158 static const MemoryRegionOps mcf_intc_ops
= {
159 .read
= mcf_intc_read
,
160 .write
= mcf_intc_write
,
161 .endianness
= DEVICE_NATIVE_ENDIAN
,
164 static void mcf_intc_instance_init(Object
*obj
)
166 mcf_intc_state
*s
= MCF_INTC(obj
);
168 memory_region_init_io(&s
->iomem
, obj
, &mcf_intc_ops
, s
, "mcf", 0x100);
171 static void mcf_intc_class_init(ObjectClass
*oc
, void *data
)
173 DeviceClass
*dc
= DEVICE_CLASS(oc
);
175 set_bit(DEVICE_CATEGORY_MISC
, dc
->categories
);
176 dc
->reset
= mcf_intc_reset
;
179 static const TypeInfo mcf_intc_gate_info
= {
180 .name
= TYPE_MCF_INTC
,
181 .parent
= TYPE_SYS_BUS_DEVICE
,
182 .instance_size
= sizeof(mcf_intc_state
),
183 .instance_init
= mcf_intc_instance_init
,
184 .class_init
= mcf_intc_class_init
,
187 static void mcf_intc_register_types(void)
189 type_register_static(&mcf_intc_gate_info
);
192 type_init(mcf_intc_register_types
)
194 qemu_irq
*mcf_intc_init(MemoryRegion
*sysmem
,
201 dev
= qdev_create(NULL
, TYPE_MCF_INTC
);
202 qdev_init_nofail(dev
);
207 memory_region_add_subregion(sysmem
, base
, &s
->iomem
);
209 return qemu_allocate_irqs(mcf_intc_set_irq
, s
, 64);