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/m68k/mcf.h"
13 #include "exec/address-spaces.h"
26 static void mcf_intc_update(mcf_intc_state
*s
)
33 active
= (s
->ipr
| s
->ifr
) & s
->enabled
& ~s
->imr
;
37 for (i
= 0; i
< 64; i
++) {
38 if ((active
& 1) != 0 && s
->icr
[i
] >= best_level
) {
39 best_level
= s
->icr
[i
];
45 s
->active_vector
= ((best
== 64) ? 24 : (best
+ 64));
46 m68k_set_irq_level(s
->cpu
, best_level
, s
->active_vector
);
49 static uint64_t mcf_intc_read(void *opaque
, hwaddr addr
,
53 mcf_intc_state
*s
= (mcf_intc_state
*)opaque
;
55 if (offset
>= 0x40 && offset
< 0x80) {
56 return s
->icr
[offset
- 0x40];
60 return (uint32_t)(s
->ipr
>> 32);
62 return (uint32_t)s
->ipr
;
64 return (uint32_t)(s
->imr
>> 32);
66 return (uint32_t)s
->imr
;
68 return (uint32_t)(s
->ifr
>> 32);
70 return (uint32_t)s
->ifr
;
71 case 0xe0: /* SWIACK. */
72 return s
->active_vector
;
73 case 0xe1: case 0xe2: case 0xe3: case 0xe4:
74 case 0xe5: case 0xe6: case 0xe7:
76 hw_error("mcf_intc_read: LnIACK not implemented\n");
82 static void mcf_intc_write(void *opaque
, hwaddr addr
,
83 uint64_t val
, unsigned size
)
86 mcf_intc_state
*s
= (mcf_intc_state
*)opaque
;
88 if (offset
>= 0x40 && offset
< 0x80) {
89 int n
= offset
- 0x40;
92 s
->enabled
&= ~(1ull << n
);
94 s
->enabled
|= (1ull << n
);
100 /* Ignore IPR writes. */
103 s
->imr
= (s
->imr
& 0xffffffff) | ((uint64_t)val
<< 32);
106 s
->imr
= (s
->imr
& 0xffffffff00000000ull
) | (uint32_t)val
;
112 s
->imr
|= (0x1ull
<< (val
& 0x3f));
119 s
->imr
&= ~(0x1ull
<< (val
& 0x3f));
123 hw_error("mcf_intc_write: Bad write offset %d\n", offset
);
129 static void mcf_intc_set_irq(void *opaque
, int irq
, int level
)
131 mcf_intc_state
*s
= (mcf_intc_state
*)opaque
;
135 s
->ipr
|= 1ull << irq
;
137 s
->ipr
&= ~(1ull << irq
);
141 static void mcf_intc_reset(mcf_intc_state
*s
)
147 memset(s
->icr
, 0, 64);
148 s
->active_vector
= 24;
151 static const MemoryRegionOps mcf_intc_ops
= {
152 .read
= mcf_intc_read
,
153 .write
= mcf_intc_write
,
154 .endianness
= DEVICE_NATIVE_ENDIAN
,
157 qemu_irq
*mcf_intc_init(MemoryRegion
*sysmem
,
163 s
= g_malloc0(sizeof(mcf_intc_state
));
167 memory_region_init_io(&s
->iomem
, NULL
, &mcf_intc_ops
, s
, "mcf", 0x100);
168 memory_region_add_subregion(sysmem
, base
, &s
->iomem
);
170 return qemu_allocate_irqs(mcf_intc_set_irq
, s
, 64);