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"
14 #include "hw/sysbus.h"
15 #include "hw/m68k/mcf.h"
17 #define TYPE_MCF_INTC "mcf-intc"
18 #define MCF_INTC(obj) OBJECT_CHECK(mcf_intc_state, (obj), TYPE_MCF_INTC)
21 SysBusDevice parent_obj
;
33 static void mcf_intc_update(mcf_intc_state
*s
)
40 active
= (s
->ipr
| s
->ifr
) & s
->enabled
& ~s
->imr
;
44 for (i
= 0; i
< 64; i
++) {
45 if ((active
& 1) != 0 && s
->icr
[i
] >= best_level
) {
46 best_level
= s
->icr
[i
];
52 s
->active_vector
= ((best
== 64) ? 24 : (best
+ 64));
53 m68k_set_irq_level(s
->cpu
, best_level
, s
->active_vector
);
56 static uint64_t mcf_intc_read(void *opaque
, hwaddr addr
,
60 mcf_intc_state
*s
= (mcf_intc_state
*)opaque
;
62 if (offset
>= 0x40 && offset
< 0x80) {
63 return s
->icr
[offset
- 0x40];
67 return (uint32_t)(s
->ipr
>> 32);
69 return (uint32_t)s
->ipr
;
71 return (uint32_t)(s
->imr
>> 32);
73 return (uint32_t)s
->imr
;
75 return (uint32_t)(s
->ifr
>> 32);
77 return (uint32_t)s
->ifr
;
78 case 0xe0: /* SWIACK. */
79 return s
->active_vector
;
80 case 0xe1: case 0xe2: case 0xe3: case 0xe4:
81 case 0xe5: case 0xe6: case 0xe7:
83 hw_error("mcf_intc_read: LnIACK not implemented\n");
89 static void mcf_intc_write(void *opaque
, hwaddr addr
,
90 uint64_t val
, unsigned size
)
93 mcf_intc_state
*s
= (mcf_intc_state
*)opaque
;
95 if (offset
>= 0x40 && offset
< 0x80) {
96 int n
= offset
- 0x40;
99 s
->enabled
&= ~(1ull << n
);
101 s
->enabled
|= (1ull << n
);
106 case 0x00: case 0x04:
107 /* Ignore IPR writes. */
110 s
->imr
= (s
->imr
& 0xffffffff) | ((uint64_t)val
<< 32);
113 s
->imr
= (s
->imr
& 0xffffffff00000000ull
) | (uint32_t)val
;
119 s
->imr
|= (0x1ull
<< (val
& 0x3f));
126 s
->imr
&= ~(0x1ull
<< (val
& 0x3f));
130 hw_error("mcf_intc_write: Bad write offset %d\n", offset
);
136 static void mcf_intc_set_irq(void *opaque
, int irq
, int level
)
138 mcf_intc_state
*s
= (mcf_intc_state
*)opaque
;
142 s
->ipr
|= 1ull << irq
;
144 s
->ipr
&= ~(1ull << irq
);
148 static void mcf_intc_reset(DeviceState
*dev
)
150 mcf_intc_state
*s
= MCF_INTC(dev
);
156 memset(s
->icr
, 0, 64);
157 s
->active_vector
= 24;
160 static const MemoryRegionOps mcf_intc_ops
= {
161 .read
= mcf_intc_read
,
162 .write
= mcf_intc_write
,
163 .endianness
= DEVICE_NATIVE_ENDIAN
,
166 static void mcf_intc_instance_init(Object
*obj
)
168 mcf_intc_state
*s
= MCF_INTC(obj
);
170 memory_region_init_io(&s
->iomem
, obj
, &mcf_intc_ops
, s
, "mcf", 0x100);
173 static void mcf_intc_class_init(ObjectClass
*oc
, void *data
)
175 DeviceClass
*dc
= DEVICE_CLASS(oc
);
177 set_bit(DEVICE_CATEGORY_MISC
, dc
->categories
);
178 dc
->reset
= mcf_intc_reset
;
181 static const TypeInfo mcf_intc_gate_info
= {
182 .name
= TYPE_MCF_INTC
,
183 .parent
= TYPE_SYS_BUS_DEVICE
,
184 .instance_size
= sizeof(mcf_intc_state
),
185 .instance_init
= mcf_intc_instance_init
,
186 .class_init
= mcf_intc_class_init
,
189 static void mcf_intc_register_types(void)
191 type_register_static(&mcf_intc_gate_info
);
194 type_init(mcf_intc_register_types
)
196 qemu_irq
*mcf_intc_init(MemoryRegion
*sysmem
,
203 dev
= qdev_create(NULL
, TYPE_MCF_INTC
);
204 qdev_init_nofail(dev
);
209 memory_region_add_subregion(sysmem
, base
, &s
->iomem
);
211 return qemu_allocate_irqs(mcf_intc_set_irq
, s
, 64);