2 * ColdFire Interrupt Controller emulation.
4 * Copyright (c) 2007 CodeSourcery.
6 * This code is licensed under the GPL
8 #include "qemu/osdep.h"
10 #include "hw/m68k/mcf.h"
11 #include "exec/address-spaces.h"
24 static void mcf_intc_update(mcf_intc_state
*s
)
31 active
= (s
->ipr
| s
->ifr
) & s
->enabled
& ~s
->imr
;
35 for (i
= 0; i
< 64; i
++) {
36 if ((active
& 1) != 0 && s
->icr
[i
] >= best_level
) {
37 best_level
= s
->icr
[i
];
43 s
->active_vector
= ((best
== 64) ? 24 : (best
+ 64));
44 m68k_set_irq_level(s
->cpu
, best_level
, s
->active_vector
);
47 static uint64_t mcf_intc_read(void *opaque
, hwaddr addr
,
51 mcf_intc_state
*s
= (mcf_intc_state
*)opaque
;
53 if (offset
>= 0x40 && offset
< 0x80) {
54 return s
->icr
[offset
- 0x40];
58 return (uint32_t)(s
->ipr
>> 32);
60 return (uint32_t)s
->ipr
;
62 return (uint32_t)(s
->imr
>> 32);
64 return (uint32_t)s
->imr
;
66 return (uint32_t)(s
->ifr
>> 32);
68 return (uint32_t)s
->ifr
;
69 case 0xe0: /* SWIACK. */
70 return s
->active_vector
;
71 case 0xe1: case 0xe2: case 0xe3: case 0xe4:
72 case 0xe5: case 0xe6: case 0xe7:
74 hw_error("mcf_intc_read: LnIACK not implemented\n");
80 static void mcf_intc_write(void *opaque
, hwaddr addr
,
81 uint64_t val
, unsigned size
)
84 mcf_intc_state
*s
= (mcf_intc_state
*)opaque
;
86 if (offset
>= 0x40 && offset
< 0x80) {
87 int n
= offset
- 0x40;
90 s
->enabled
&= ~(1ull << n
);
92 s
->enabled
|= (1ull << n
);
98 /* Ignore IPR writes. */
101 s
->imr
= (s
->imr
& 0xffffffff) | ((uint64_t)val
<< 32);
104 s
->imr
= (s
->imr
& 0xffffffff00000000ull
) | (uint32_t)val
;
110 s
->imr
|= (0x1ull
<< (val
& 0x3f));
117 s
->imr
&= ~(0x1ull
<< (val
& 0x3f));
121 hw_error("mcf_intc_write: Bad write offset %d\n", offset
);
127 static void mcf_intc_set_irq(void *opaque
, int irq
, int level
)
129 mcf_intc_state
*s
= (mcf_intc_state
*)opaque
;
133 s
->ipr
|= 1ull << irq
;
135 s
->ipr
&= ~(1ull << irq
);
139 static void mcf_intc_reset(mcf_intc_state
*s
)
145 memset(s
->icr
, 0, 64);
146 s
->active_vector
= 24;
149 static const MemoryRegionOps mcf_intc_ops
= {
150 .read
= mcf_intc_read
,
151 .write
= mcf_intc_write
,
152 .endianness
= DEVICE_NATIVE_ENDIAN
,
155 qemu_irq
*mcf_intc_init(MemoryRegion
*sysmem
,
161 s
= g_malloc0(sizeof(mcf_intc_state
));
165 memory_region_init_io(&s
->iomem
, NULL
, &mcf_intc_ops
, s
, "mcf", 0x100);
166 memory_region_add_subregion(sysmem
, base
, &s
->iomem
);
168 return qemu_allocate_irqs(mcf_intc_set_irq
, s
, 64);