2 * i.MX31 Vectored Interrupt Controller
4 * Note this is NOT the PL192 provided by ARM, but
5 * a custom implementation by Freescale.
7 * Copyright (c) 2008 OKL
8 * Copyright (c) 2011 NICTA Pty Ltd
9 * Originally written by Hans Jiang
11 * This code is licensed under the GPL version 2 or later. See
12 * the COPYING file in the top-level directory.
14 * TODO: implement vectors.
18 #include "hw/sysbus.h"
19 #include "qemu/host-utils.h"
22 #undef DEBUG_INT /* comment out for debugging */
25 #define DPRINTF(fmt, args...) \
26 do { printf("imx_avic: " fmt , ##args); } while (0)
28 #define DPRINTF(fmt, args...) do {} while (0)
32 * Define to 1 for messages about attempts to
33 * access unimplemented registers or similar.
35 #define DEBUG_IMPLEMENTATION 1
36 #if DEBUG_IMPLEMENTATION
37 # define IPRINTF(fmt, args...) \
38 do { fprintf(stderr, "imx_avic: " fmt, ##args); } while (0)
40 # define IPRINTF(fmt, args...) do {} while (0)
43 #define IMX_AVIC_NUM_IRQS 64
45 /* Interrupt Control Bits */
46 #define ABFLAG (1<<25)
48 #define NIDIS (1<<22) /* Normal Interrupt disable */
49 #define FIDIS (1<<21) /* Fast interrupt disable */
50 #define NIAD (1<<20) /* Normal Interrupt Arbiter Rise ARM level */
51 #define FIAD (1<<19) /* Fast Interrupt Arbiter Rise ARM level */
52 #define NM (1<<18) /* Normal interrupt mode */
55 #define PRIO_PER_WORD (sizeof(uint32_t) * 8 / 4)
56 #define PRIO_WORDS (IMX_AVIC_NUM_IRQS/PRIO_PER_WORD)
58 #define TYPE_IMX_AVIC "imx_avic"
59 #define IMX_AVIC(obj) \
60 OBJECT_CHECK(IMXAVICState, (obj), TYPE_IMX_AVIC)
62 typedef struct IMXAVICState
{
63 SysBusDevice parent_obj
;
73 uint32_t prio
[PRIO_WORDS
]; /* Priorities are 4-bits each */
76 static const VMStateDescription vmstate_imx_avic
= {
79 .minimum_version_id
= 1,
80 .fields
= (VMStateField
[]) {
81 VMSTATE_UINT64(pending
, IMXAVICState
),
82 VMSTATE_UINT64(enabled
, IMXAVICState
),
83 VMSTATE_UINT64(is_fiq
, IMXAVICState
),
84 VMSTATE_UINT32(intcntl
, IMXAVICState
),
85 VMSTATE_UINT32(intmask
, IMXAVICState
),
86 VMSTATE_UINT32_ARRAY(prio
, IMXAVICState
, PRIO_WORDS
),
93 static inline int imx_avic_prio(IMXAVICState
*s
, int irq
)
95 uint32_t word
= irq
/ PRIO_PER_WORD
;
96 uint32_t part
= 4 * (irq
% PRIO_PER_WORD
);
97 return 0xf & (s
->prio
[word
] >> part
);
100 static inline void imx_avic_set_prio(IMXAVICState
*s
, int irq
, int prio
)
102 uint32_t word
= irq
/ PRIO_PER_WORD
;
103 uint32_t part
= 4 * (irq
% PRIO_PER_WORD
);
104 uint32_t mask
= ~(0xf << part
);
105 s
->prio
[word
] &= mask
;
106 s
->prio
[word
] |= prio
<< part
;
109 /* Update interrupts. */
110 static void imx_avic_update(IMXAVICState
*s
)
113 uint64_t new = s
->pending
& s
->enabled
;
116 flags
= new & s
->is_fiq
;
117 qemu_set_irq(s
->fiq
, !!flags
);
119 flags
= new & ~s
->is_fiq
;
120 if (!flags
|| (s
->intmask
== 0x1f)) {
121 qemu_set_irq(s
->irq
, !!flags
);
126 * Take interrupt if there's a pending interrupt with
127 * priority higher than the value of intmask
129 for (i
= 0; i
< IMX_AVIC_NUM_IRQS
; i
++) {
130 if (flags
& (1UL << i
)) {
131 if (imx_avic_prio(s
, i
) > s
->intmask
) {
132 qemu_set_irq(s
->irq
, 1);
137 qemu_set_irq(s
->irq
, 0);
140 static void imx_avic_set_irq(void *opaque
, int irq
, int level
)
142 IMXAVICState
*s
= (IMXAVICState
*)opaque
;
145 DPRINTF("Raising IRQ %d, prio %d\n",
146 irq
, imx_avic_prio(s
, irq
));
147 s
->pending
|= (1ULL << irq
);
149 DPRINTF("Clearing IRQ %d, prio %d\n",
150 irq
, imx_avic_prio(s
, irq
));
151 s
->pending
&= ~(1ULL << irq
);
158 static uint64_t imx_avic_read(void *opaque
,
159 hwaddr offset
, unsigned size
)
161 IMXAVICState
*s
= (IMXAVICState
*)opaque
;
164 DPRINTF("read(offset = 0x%x)\n", offset
>> 2);
165 switch (offset
>> 2) {
166 case 0: /* INTCNTL */
169 case 1: /* Normal Interrupt Mask Register, NIMASK */
172 case 2: /* Interrupt Enable Number Register, INTENNUM */
173 case 3: /* Interrupt Disable Number Register, INTDISNUM */
176 case 4: /* Interrupt Enabled Number Register High */
177 return s
->enabled
>> 32;
179 case 5: /* Interrupt Enabled Number Register Low */
180 return s
->enabled
& 0xffffffffULL
;
182 case 6: /* Interrupt Type Register High */
183 return s
->is_fiq
>> 32;
185 case 7: /* Interrupt Type Register Low */
186 return s
->is_fiq
& 0xffffffffULL
;
188 case 8: /* Normal Interrupt Priority Register 7 */
189 case 9: /* Normal Interrupt Priority Register 6 */
190 case 10:/* Normal Interrupt Priority Register 5 */
191 case 11:/* Normal Interrupt Priority Register 4 */
192 case 12:/* Normal Interrupt Priority Register 3 */
193 case 13:/* Normal Interrupt Priority Register 2 */
194 case 14:/* Normal Interrupt Priority Register 1 */
195 case 15:/* Normal Interrupt Priority Register 0 */
196 return s
->prio
[15-(offset
>>2)];
198 case 16: /* Normal interrupt vector and status register */
201 * This returns the highest priority
202 * outstanding interrupt. Where there is more than
203 * one pending IRQ with the same priority,
204 * take the highest numbered one.
206 uint64_t flags
= s
->pending
& s
->enabled
& ~s
->is_fiq
;
210 for (i
= 63; i
>= 0; --i
) {
211 if (flags
& (1ULL<<i
)) {
212 int irq_prio
= imx_avic_prio(s
, i
);
213 if (irq_prio
> prio
) {
220 imx_avic_set_irq(s
, irq
, 0);
221 return irq
<< 16 | prio
;
223 return 0xffffffffULL
;
225 case 17:/* Fast Interrupt vector and status register */
227 uint64_t flags
= s
->pending
& s
->enabled
& s
->is_fiq
;
228 int i
= ctz64(flags
);
230 imx_avic_set_irq(opaque
, i
, 0);
233 return 0xffffffffULL
;
235 case 18:/* Interrupt source register high */
236 return s
->pending
>> 32;
238 case 19:/* Interrupt source register low */
239 return s
->pending
& 0xffffffffULL
;
241 case 20:/* Interrupt Force Register high */
242 case 21:/* Interrupt Force Register low */
245 case 22:/* Normal Interrupt Pending Register High */
246 return (s
->pending
& s
->enabled
& ~s
->is_fiq
) >> 32;
248 case 23:/* Normal Interrupt Pending Register Low */
249 return (s
->pending
& s
->enabled
& ~s
->is_fiq
) & 0xffffffffULL
;
251 case 24: /* Fast Interrupt Pending Register High */
252 return (s
->pending
& s
->enabled
& s
->is_fiq
) >> 32;
254 case 25: /* Fast Interrupt Pending Register Low */
255 return (s
->pending
& s
->enabled
& s
->is_fiq
) & 0xffffffffULL
;
257 case 0x40: /* AVIC vector 0, use for WFI WAR */
261 IPRINTF("imx_avic_read: Bad offset 0x%x\n", (int)offset
);
266 static void imx_avic_write(void *opaque
, hwaddr offset
,
267 uint64_t val
, unsigned size
)
269 IMXAVICState
*s
= (IMXAVICState
*)opaque
;
271 /* Vector Registers not yet supported */
272 if (offset
>= 0x100 && offset
<= 0x2fc) {
273 IPRINTF("imx_avic_write to vector register %d ignored\n",
274 (unsigned int)((offset
- 0x100) >> 2));
278 DPRINTF("imx_avic_write(0x%x) = %x\n",
279 (unsigned int)offset
>>2, (unsigned int)val
);
280 switch (offset
>> 2) {
281 case 0: /* Interrupt Control Register, INTCNTL */
282 s
->intcntl
= val
& (ABFEN
| NIDIS
| FIDIS
| NIAD
| FIAD
| NM
);
283 if (s
->intcntl
& ABFEN
) {
284 s
->intcntl
&= ~(val
& ABFLAG
);
288 case 1: /* Normal Interrupt Mask Register, NIMASK */
289 s
->intmask
= val
& 0x1f;
292 case 2: /* Interrupt Enable Number Register, INTENNUM */
293 DPRINTF("enable(%d)\n", (int)val
);
295 s
->enabled
|= (1ULL << val
);
298 case 3: /* Interrupt Disable Number Register, INTDISNUM */
299 DPRINTF("disable(%d)\n", (int)val
);
301 s
->enabled
&= ~(1ULL << val
);
304 case 4: /* Interrupt Enable Number Register High */
305 s
->enabled
= (s
->enabled
& 0xffffffffULL
) | (val
<< 32);
308 case 5: /* Interrupt Enable Number Register Low */
309 s
->enabled
= (s
->enabled
& 0xffffffff00000000ULL
) | val
;
312 case 6: /* Interrupt Type Register High */
313 s
->is_fiq
= (s
->is_fiq
& 0xffffffffULL
) | (val
<< 32);
316 case 7: /* Interrupt Type Register Low */
317 s
->is_fiq
= (s
->is_fiq
& 0xffffffff00000000ULL
) | val
;
320 case 8: /* Normal Interrupt Priority Register 7 */
321 case 9: /* Normal Interrupt Priority Register 6 */
322 case 10:/* Normal Interrupt Priority Register 5 */
323 case 11:/* Normal Interrupt Priority Register 4 */
324 case 12:/* Normal Interrupt Priority Register 3 */
325 case 13:/* Normal Interrupt Priority Register 2 */
326 case 14:/* Normal Interrupt Priority Register 1 */
327 case 15:/* Normal Interrupt Priority Register 0 */
328 s
->prio
[15-(offset
>>2)] = val
;
331 /* Read-only registers, writes ignored */
332 case 16:/* Normal Interrupt Vector and Status register */
333 case 17:/* Fast Interrupt vector and status register */
334 case 18:/* Interrupt source register high */
335 case 19:/* Interrupt source register low */
338 case 20:/* Interrupt Force Register high */
339 s
->pending
= (s
->pending
& 0xffffffffULL
) | (val
<< 32);
342 case 21:/* Interrupt Force Register low */
343 s
->pending
= (s
->pending
& 0xffffffff00000000ULL
) | val
;
346 case 22:/* Normal Interrupt Pending Register High */
347 case 23:/* Normal Interrupt Pending Register Low */
348 case 24: /* Fast Interrupt Pending Register High */
349 case 25: /* Fast Interrupt Pending Register Low */
353 IPRINTF("imx_avic_write: Bad offset %x\n", (int)offset
);
358 static const MemoryRegionOps imx_avic_ops
= {
359 .read
= imx_avic_read
,
360 .write
= imx_avic_write
,
361 .endianness
= DEVICE_NATIVE_ENDIAN
,
364 static void imx_avic_reset(DeviceState
*dev
)
366 IMXAVICState
*s
= IMX_AVIC(dev
);
373 memset(s
->prio
, 0, sizeof s
->prio
);
376 static int imx_avic_init(SysBusDevice
*sbd
)
378 DeviceState
*dev
= DEVICE(sbd
);
379 IMXAVICState
*s
= IMX_AVIC(dev
);
381 memory_region_init_io(&s
->iomem
, OBJECT(s
), &imx_avic_ops
, s
,
383 sysbus_init_mmio(sbd
, &s
->iomem
);
385 qdev_init_gpio_in(dev
, imx_avic_set_irq
, IMX_AVIC_NUM_IRQS
);
386 sysbus_init_irq(sbd
, &s
->irq
);
387 sysbus_init_irq(sbd
, &s
->fiq
);
393 static void imx_avic_class_init(ObjectClass
*klass
, void *data
)
395 DeviceClass
*dc
= DEVICE_CLASS(klass
);
396 SysBusDeviceClass
*k
= SYS_BUS_DEVICE_CLASS(klass
);
397 k
->init
= imx_avic_init
;
398 dc
->vmsd
= &vmstate_imx_avic
;
399 dc
->reset
= imx_avic_reset
;
400 dc
->desc
= "i.MX Advanced Vector Interrupt Controller";
403 static const TypeInfo imx_avic_info
= {
404 .name
= TYPE_IMX_AVIC
,
405 .parent
= TYPE_SYS_BUS_DEVICE
,
406 .instance_size
= sizeof(IMXAVICState
),
407 .class_init
= imx_avic_class_init
,
410 static void imx_avic_register_types(void)
412 type_register_static(&imx_avic_info
);
415 type_init(imx_avic_register_types
)