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"
14 #include "exec/address-spaces.h"
16 #define TYPE_MCF_INTC "mcf-intc"
17 #define MCF_INTC(obj) OBJECT_CHECK(mcf_intc_state, (obj), TYPE_MCF_INTC)
20 SysBusDevice parent_obj
;
32 static void mcf_intc_update(mcf_intc_state
*s
)
39 active
= (s
->ipr
| s
->ifr
) & s
->enabled
& ~s
->imr
;
43 for (i
= 0; i
< 64; i
++) {
44 if ((active
& 1) != 0 && s
->icr
[i
] >= best_level
) {
45 best_level
= s
->icr
[i
];
51 s
->active_vector
= ((best
== 64) ? 24 : (best
+ 64));
52 m68k_set_irq_level(s
->cpu
, best_level
, s
->active_vector
);
55 static uint64_t mcf_intc_read(void *opaque
, hwaddr addr
,
59 mcf_intc_state
*s
= (mcf_intc_state
*)opaque
;
61 if (offset
>= 0x40 && offset
< 0x80) {
62 return s
->icr
[offset
- 0x40];
66 return (uint32_t)(s
->ipr
>> 32);
68 return (uint32_t)s
->ipr
;
70 return (uint32_t)(s
->imr
>> 32);
72 return (uint32_t)s
->imr
;
74 return (uint32_t)(s
->ifr
>> 32);
76 return (uint32_t)s
->ifr
;
77 case 0xe0: /* SWIACK. */
78 return s
->active_vector
;
79 case 0xe1: case 0xe2: case 0xe3: case 0xe4:
80 case 0xe5: case 0xe6: case 0xe7:
82 hw_error("mcf_intc_read: LnIACK not implemented\n");
88 static void mcf_intc_write(void *opaque
, hwaddr addr
,
89 uint64_t val
, unsigned size
)
92 mcf_intc_state
*s
= (mcf_intc_state
*)opaque
;
94 if (offset
>= 0x40 && offset
< 0x80) {
95 int n
= offset
- 0x40;
98 s
->enabled
&= ~(1ull << n
);
100 s
->enabled
|= (1ull << n
);
105 case 0x00: case 0x04:
106 /* Ignore IPR writes. */
109 s
->imr
= (s
->imr
& 0xffffffff) | ((uint64_t)val
<< 32);
112 s
->imr
= (s
->imr
& 0xffffffff00000000ull
) | (uint32_t)val
;
118 s
->imr
|= (0x1ull
<< (val
& 0x3f));
125 s
->imr
&= ~(0x1ull
<< (val
& 0x3f));
129 hw_error("mcf_intc_write: Bad write offset %d\n", offset
);
135 static void mcf_intc_set_irq(void *opaque
, int irq
, int level
)
137 mcf_intc_state
*s
= (mcf_intc_state
*)opaque
;
141 s
->ipr
|= 1ull << irq
;
143 s
->ipr
&= ~(1ull << irq
);
147 static void mcf_intc_reset(DeviceState
*dev
)
149 mcf_intc_state
*s
= MCF_INTC(dev
);
155 memset(s
->icr
, 0, 64);
156 s
->active_vector
= 24;
159 static const MemoryRegionOps mcf_intc_ops
= {
160 .read
= mcf_intc_read
,
161 .write
= mcf_intc_write
,
162 .endianness
= DEVICE_NATIVE_ENDIAN
,
165 static void mcf_intc_instance_init(Object
*obj
)
167 mcf_intc_state
*s
= MCF_INTC(obj
);
169 memory_region_init_io(&s
->iomem
, obj
, &mcf_intc_ops
, s
, "mcf", 0x100);
172 static void mcf_intc_class_init(ObjectClass
*oc
, void *data
)
174 DeviceClass
*dc
= DEVICE_CLASS(oc
);
176 set_bit(DEVICE_CATEGORY_MISC
, dc
->categories
);
177 dc
->reset
= mcf_intc_reset
;
180 static const TypeInfo mcf_intc_gate_info
= {
181 .name
= TYPE_MCF_INTC
,
182 .parent
= TYPE_SYS_BUS_DEVICE
,
183 .instance_size
= sizeof(mcf_intc_state
),
184 .instance_init
= mcf_intc_instance_init
,
185 .class_init
= mcf_intc_class_init
,
188 static void mcf_intc_register_types(void)
190 type_register_static(&mcf_intc_gate_info
);
193 type_init(mcf_intc_register_types
)
195 qemu_irq
*mcf_intc_init(MemoryRegion
*sysmem
,
202 dev
= qdev_create(NULL
, TYPE_MCF_INTC
);
203 qdev_init_nofail(dev
);
208 memory_region_add_subregion(sysmem
, base
, &s
->iomem
);
210 return qemu_allocate_irqs(mcf_intc_set_irq
, s
, 64);