2 * ColdFire Interrupt Controller emulation.
4 * Copyright (c) 2007 CodeSourcery.
6 * This code is licensed under the GPL
9 #include "hw/m68k/mcf.h"
10 #include "exec/address-spaces.h"
23 static void mcf_intc_update(mcf_intc_state
*s
)
30 active
= (s
->ipr
| s
->ifr
) & s
->enabled
& ~s
->imr
;
34 for (i
= 0; i
< 64; i
++) {
35 if ((active
& 1) != 0 && s
->icr
[i
] >= best_level
) {
36 best_level
= s
->icr
[i
];
42 s
->active_vector
= ((best
== 64) ? 24 : (best
+ 64));
43 m68k_set_irq_level(s
->cpu
, best_level
, s
->active_vector
);
46 static uint64_t mcf_intc_read(void *opaque
, hwaddr addr
,
50 mcf_intc_state
*s
= (mcf_intc_state
*)opaque
;
52 if (offset
>= 0x40 && offset
< 0x80) {
53 return s
->icr
[offset
- 0x40];
57 return (uint32_t)(s
->ipr
>> 32);
59 return (uint32_t)s
->ipr
;
61 return (uint32_t)(s
->imr
>> 32);
63 return (uint32_t)s
->imr
;
65 return (uint32_t)(s
->ifr
>> 32);
67 return (uint32_t)s
->ifr
;
68 case 0xe0: /* SWIACK. */
69 return s
->active_vector
;
70 case 0xe1: case 0xe2: case 0xe3: case 0xe4:
71 case 0xe5: case 0xe6: case 0xe7:
73 hw_error("mcf_intc_read: LnIACK not implemented\n");
79 static void mcf_intc_write(void *opaque
, hwaddr addr
,
80 uint64_t val
, unsigned size
)
83 mcf_intc_state
*s
= (mcf_intc_state
*)opaque
;
85 if (offset
>= 0x40 && offset
< 0x80) {
86 int n
= offset
- 0x40;
89 s
->enabled
&= ~(1ull << n
);
91 s
->enabled
|= (1ull << n
);
97 /* Ignore IPR writes. */
100 s
->imr
= (s
->imr
& 0xffffffff) | ((uint64_t)val
<< 32);
103 s
->imr
= (s
->imr
& 0xffffffff00000000ull
) | (uint32_t)val
;
109 s
->imr
|= (0x1ull
<< (val
& 0x3f));
116 s
->imr
&= ~(0x1ull
<< (val
& 0x3f));
120 hw_error("mcf_intc_write: Bad write offset %d\n", offset
);
126 static void mcf_intc_set_irq(void *opaque
, int irq
, int level
)
128 mcf_intc_state
*s
= (mcf_intc_state
*)opaque
;
132 s
->ipr
|= 1ull << irq
;
134 s
->ipr
&= ~(1ull << irq
);
138 static void mcf_intc_reset(mcf_intc_state
*s
)
144 memset(s
->icr
, 0, 64);
145 s
->active_vector
= 24;
148 static const MemoryRegionOps mcf_intc_ops
= {
149 .read
= mcf_intc_read
,
150 .write
= mcf_intc_write
,
151 .endianness
= DEVICE_NATIVE_ENDIAN
,
154 qemu_irq
*mcf_intc_init(MemoryRegion
*sysmem
,
160 s
= g_malloc0(sizeof(mcf_intc_state
));
164 memory_region_init_io(&s
->iomem
, NULL
, &mcf_intc_ops
, s
, "mcf", 0x100);
165 memory_region_add_subregion(sysmem
, base
, &s
->iomem
);
167 return qemu_allocate_irqs(mcf_intc_set_irq
, s
, 64);