2 * TI OMAP interrupt controller emulation.
4 * Copyright (C) 2006-2008 Andrzej Zaborowski <balrog@zabor.org>
5 * Copyright (C) 2007-2008 Nokia Corporation
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License as
9 * published by the Free Software Foundation; either version 2 or
10 * (at your option) version 3 of the License.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License along
18 * with this program; if not, see <http://www.gnu.org/licenses/>.
23 /* Interrupt Handlers */
24 struct omap_intr_handler_bank_s
{
31 unsigned char priority
[32];
34 struct omap_intr_handler_s
{
36 qemu_irq parent_intr
[2];
45 struct omap_intr_handler_bank_s bank
[];
48 inline qemu_irq
omap_inth_get_pin(struct omap_intr_handler_s
*s
, int n
)
53 static void omap_inth_sir_update(struct omap_intr_handler_s
*s
, int is_fiq
)
55 int i
, j
, sir_intr
, p_intr
, p
, f
;
60 /* Find the interrupt line with the highest dynamic priority.
61 * Note: 0 denotes the hightest priority.
62 * If all interrupts have the same priority, the default order is IRQ_N,
63 * IRQ_N-1,...,IRQ_0. */
64 for (j
= 0; j
< s
->nbanks
; ++j
) {
65 level
= s
->bank
[j
].irqs
& ~s
->bank
[j
].mask
&
66 (is_fiq
? s
->bank
[j
].fiq
: ~s
->bank
[j
].fiq
);
67 for (f
= ffs(level
), i
= f
- 1, level
>>= f
- 1; f
; i
+= f
,
69 p
= s
->bank
[j
].priority
[i
];
72 sir_intr
= 32 * j
+ i
;
77 s
->sir_intr
[is_fiq
] = sir_intr
;
80 static inline void omap_inth_update(struct omap_intr_handler_s
*s
, int is_fiq
)
83 uint32_t has_intr
= 0;
85 for (i
= 0; i
< s
->nbanks
; ++i
)
86 has_intr
|= s
->bank
[i
].irqs
& ~s
->bank
[i
].mask
&
87 (is_fiq
? s
->bank
[i
].fiq
: ~s
->bank
[i
].fiq
);
89 if (s
->new_agr
[is_fiq
] & has_intr
& s
->mask
) {
90 s
->new_agr
[is_fiq
] = 0;
91 omap_inth_sir_update(s
, is_fiq
);
92 qemu_set_irq(s
->parent_intr
[is_fiq
], 1);
96 #define INT_FALLING_EDGE 0
97 #define INT_LOW_LEVEL 1
99 static void omap_set_intr(void *opaque
, int irq
, int req
)
101 struct omap_intr_handler_s
*ih
= (struct omap_intr_handler_s
*) opaque
;
104 struct omap_intr_handler_bank_s
*bank
= &ih
->bank
[irq
>> 5];
108 rise
= ~bank
->irqs
& (1 << n
);
109 if (~bank
->sens_edge
& (1 << n
))
110 rise
&= ~bank
->inputs
;
112 bank
->inputs
|= (1 << n
);
115 omap_inth_update(ih
, 0);
116 omap_inth_update(ih
, 1);
119 rise
= bank
->sens_edge
& bank
->irqs
& (1 << n
);
121 bank
->inputs
&= ~(1 << n
);
125 /* Simplified version with no edge detection */
126 static void omap_set_intr_noedge(void *opaque
, int irq
, int req
)
128 struct omap_intr_handler_s
*ih
= (struct omap_intr_handler_s
*) opaque
;
131 struct omap_intr_handler_bank_s
*bank
= &ih
->bank
[irq
>> 5];
135 rise
= ~bank
->inputs
& (1 << n
);
137 bank
->irqs
|= bank
->inputs
|= rise
;
138 omap_inth_update(ih
, 0);
139 omap_inth_update(ih
, 1);
142 bank
->irqs
= (bank
->inputs
&= ~(1 << n
)) | bank
->swi
;
145 static uint32_t omap_inth_read(void *opaque
, target_phys_addr_t addr
)
147 struct omap_intr_handler_s
*s
= (struct omap_intr_handler_s
*) opaque
;
148 int i
, offset
= addr
;
149 int bank_no
= offset
>> 8;
151 struct omap_intr_handler_bank_s
*bank
= &s
->bank
[bank_no
];
161 case 0x10: /* SIR_IRQ_CODE */
162 case 0x14: /* SIR_FIQ_CODE */
165 line_no
= s
->sir_intr
[(offset
- 0x10) >> 2];
166 bank
= &s
->bank
[line_no
>> 5];
168 if (((bank
->sens_edge
>> i
) & 1) == INT_FALLING_EDGE
)
169 bank
->irqs
&= ~(1 << i
);
172 case 0x18: /* CONTROL_REG */
177 case 0x1c: /* ILR0 */
178 case 0x20: /* ILR1 */
179 case 0x24: /* ILR2 */
180 case 0x28: /* ILR3 */
181 case 0x2c: /* ILR4 */
182 case 0x30: /* ILR5 */
183 case 0x34: /* ILR6 */
184 case 0x38: /* ILR7 */
185 case 0x3c: /* ILR8 */
186 case 0x40: /* ILR9 */
187 case 0x44: /* ILR10 */
188 case 0x48: /* ILR11 */
189 case 0x4c: /* ILR12 */
190 case 0x50: /* ILR13 */
191 case 0x54: /* ILR14 */
192 case 0x58: /* ILR15 */
193 case 0x5c: /* ILR16 */
194 case 0x60: /* ILR17 */
195 case 0x64: /* ILR18 */
196 case 0x68: /* ILR19 */
197 case 0x6c: /* ILR20 */
198 case 0x70: /* ILR21 */
199 case 0x74: /* ILR22 */
200 case 0x78: /* ILR23 */
201 case 0x7c: /* ILR24 */
202 case 0x80: /* ILR25 */
203 case 0x84: /* ILR26 */
204 case 0x88: /* ILR27 */
205 case 0x8c: /* ILR28 */
206 case 0x90: /* ILR29 */
207 case 0x94: /* ILR30 */
208 case 0x98: /* ILR31 */
209 i
= (offset
- 0x1c) >> 2;
210 return (bank
->priority
[i
] << 2) |
211 (((bank
->sens_edge
>> i
) & 1) << 1) |
212 ((bank
->fiq
>> i
) & 1);
222 static void omap_inth_write(void *opaque
, target_phys_addr_t addr
,
225 struct omap_intr_handler_s
*s
= (struct omap_intr_handler_s
*) opaque
;
226 int i
, offset
= addr
;
227 int bank_no
= offset
>> 8;
228 struct omap_intr_handler_bank_s
*bank
= &s
->bank
[bank_no
];
233 /* Important: ignore the clearing if the IRQ is level-triggered and
234 the input bit is 1 */
235 bank
->irqs
&= value
| (bank
->inputs
& bank
->sens_edge
);
240 omap_inth_update(s
, 0);
241 omap_inth_update(s
, 1);
244 case 0x10: /* SIR_IRQ_CODE */
245 case 0x14: /* SIR_FIQ_CODE */
249 case 0x18: /* CONTROL_REG */
253 qemu_set_irq(s
->parent_intr
[1], 0);
255 omap_inth_update(s
, 1);
258 qemu_set_irq(s
->parent_intr
[0], 0);
260 omap_inth_update(s
, 0);
264 case 0x1c: /* ILR0 */
265 case 0x20: /* ILR1 */
266 case 0x24: /* ILR2 */
267 case 0x28: /* ILR3 */
268 case 0x2c: /* ILR4 */
269 case 0x30: /* ILR5 */
270 case 0x34: /* ILR6 */
271 case 0x38: /* ILR7 */
272 case 0x3c: /* ILR8 */
273 case 0x40: /* ILR9 */
274 case 0x44: /* ILR10 */
275 case 0x48: /* ILR11 */
276 case 0x4c: /* ILR12 */
277 case 0x50: /* ILR13 */
278 case 0x54: /* ILR14 */
279 case 0x58: /* ILR15 */
280 case 0x5c: /* ILR16 */
281 case 0x60: /* ILR17 */
282 case 0x64: /* ILR18 */
283 case 0x68: /* ILR19 */
284 case 0x6c: /* ILR20 */
285 case 0x70: /* ILR21 */
286 case 0x74: /* ILR22 */
287 case 0x78: /* ILR23 */
288 case 0x7c: /* ILR24 */
289 case 0x80: /* ILR25 */
290 case 0x84: /* ILR26 */
291 case 0x88: /* ILR27 */
292 case 0x8c: /* ILR28 */
293 case 0x90: /* ILR29 */
294 case 0x94: /* ILR30 */
295 case 0x98: /* ILR31 */
296 i
= (offset
- 0x1c) >> 2;
297 bank
->priority
[i
] = (value
>> 2) & 0x1f;
298 bank
->sens_edge
&= ~(1 << i
);
299 bank
->sens_edge
|= ((value
>> 1) & 1) << i
;
300 bank
->fiq
&= ~(1 << i
);
301 bank
->fiq
|= (value
& 1) << i
;
305 for (i
= 0; i
< 32; i
++)
306 if (value
& (1 << i
)) {
307 omap_set_intr(s
, 32 * bank_no
+ i
, 1);
315 static CPUReadMemoryFunc
* const omap_inth_readfn
[] = {
316 omap_badwidth_read32
,
317 omap_badwidth_read32
,
321 static CPUWriteMemoryFunc
* const omap_inth_writefn
[] = {
327 void omap_inth_reset(struct omap_intr_handler_s
*s
)
331 for (i
= 0; i
< s
->nbanks
; ++i
){
332 s
->bank
[i
].irqs
= 0x00000000;
333 s
->bank
[i
].mask
= 0xffffffff;
334 s
->bank
[i
].sens_edge
= 0x00000000;
335 s
->bank
[i
].fiq
= 0x00000000;
336 s
->bank
[i
].inputs
= 0x00000000;
337 s
->bank
[i
].swi
= 0x00000000;
338 memset(s
->bank
[i
].priority
, 0, sizeof(s
->bank
[i
].priority
));
341 s
->bank
[i
].sens_edge
= 0xffffffff;
351 qemu_set_irq(s
->parent_intr
[0], 0);
352 qemu_set_irq(s
->parent_intr
[1], 0);
355 struct omap_intr_handler_s
*omap_inth_init(target_phys_addr_t base
,
356 unsigned long size
, unsigned char nbanks
, qemu_irq
**pins
,
357 qemu_irq parent_irq
, qemu_irq parent_fiq
, omap_clk clk
)
360 struct omap_intr_handler_s
*s
= (struct omap_intr_handler_s
*)
361 g_malloc0(sizeof(struct omap_intr_handler_s
) +
362 sizeof(struct omap_intr_handler_bank_s
) * nbanks
);
364 s
->parent_intr
[0] = parent_irq
;
365 s
->parent_intr
[1] = parent_fiq
;
367 s
->pins
= qemu_allocate_irqs(omap_set_intr
, s
, nbanks
* 32);
373 iomemtype
= cpu_register_io_memory(omap_inth_readfn
,
374 omap_inth_writefn
, s
, DEVICE_NATIVE_ENDIAN
);
375 cpu_register_physical_memory(base
, size
, iomemtype
);
380 static uint32_t omap2_inth_read(void *opaque
, target_phys_addr_t addr
)
382 struct omap_intr_handler_s
*s
= (struct omap_intr_handler_s
*) opaque
;
384 int bank_no
, line_no
;
385 struct omap_intr_handler_bank_s
*bank
= NULL
;
387 if ((offset
& 0xf80) == 0x80) {
388 bank_no
= (offset
& 0x60) >> 5;
389 if (bank_no
< s
->nbanks
) {
391 bank
= &s
->bank
[bank_no
];
396 case 0x00: /* INTC_REVISION */
399 case 0x10: /* INTC_SYSCONFIG */
400 return (s
->autoidle
>> 2) & 1;
402 case 0x14: /* INTC_SYSSTATUS */
403 return 1; /* RESETDONE */
405 case 0x40: /* INTC_SIR_IRQ */
406 return s
->sir_intr
[0];
408 case 0x44: /* INTC_SIR_FIQ */
409 return s
->sir_intr
[1];
411 case 0x48: /* INTC_CONTROL */
412 return (!s
->mask
) << 2; /* GLOBALMASK */
414 case 0x4c: /* INTC_PROTECTION */
417 case 0x50: /* INTC_IDLE */
418 return s
->autoidle
& 3;
420 /* Per-bank registers */
421 case 0x80: /* INTC_ITR */
424 case 0x84: /* INTC_MIR */
427 case 0x88: /* INTC_MIR_CLEAR */
428 case 0x8c: /* INTC_MIR_SET */
431 case 0x90: /* INTC_ISR_SET */
434 case 0x94: /* INTC_ISR_CLEAR */
437 case 0x98: /* INTC_PENDING_IRQ */
438 return bank
->irqs
& ~bank
->mask
& ~bank
->fiq
;
440 case 0x9c: /* INTC_PENDING_FIQ */
441 return bank
->irqs
& ~bank
->mask
& bank
->fiq
;
443 /* Per-line registers */
444 case 0x100 ... 0x300: /* INTC_ILR */
445 bank_no
= (offset
- 0x100) >> 7;
446 if (bank_no
> s
->nbanks
)
448 bank
= &s
->bank
[bank_no
];
449 line_no
= (offset
& 0x7f) >> 2;
450 return (bank
->priority
[line_no
] << 2) |
451 ((bank
->fiq
>> line_no
) & 1);
457 static void omap2_inth_write(void *opaque
, target_phys_addr_t addr
,
460 struct omap_intr_handler_s
*s
= (struct omap_intr_handler_s
*) opaque
;
462 int bank_no
, line_no
;
463 struct omap_intr_handler_bank_s
*bank
= NULL
;
465 if ((offset
& 0xf80) == 0x80) {
466 bank_no
= (offset
& 0x60) >> 5;
467 if (bank_no
< s
->nbanks
) {
469 bank
= &s
->bank
[bank_no
];
474 case 0x10: /* INTC_SYSCONFIG */
476 s
->autoidle
|= (value
& 1) << 2;
477 if (value
& 2) /* SOFTRESET */
481 case 0x48: /* INTC_CONTROL */
482 s
->mask
= (value
& 4) ? 0 : ~0; /* GLOBALMASK */
483 if (value
& 2) { /* NEWFIQAGR */
484 qemu_set_irq(s
->parent_intr
[1], 0);
486 omap_inth_update(s
, 1);
488 if (value
& 1) { /* NEWIRQAGR */
489 qemu_set_irq(s
->parent_intr
[0], 0);
491 omap_inth_update(s
, 0);
495 case 0x4c: /* INTC_PROTECTION */
496 /* TODO: Make a bitmap (or sizeof(char)map) of access privileges
497 * for every register, see Chapter 3 and 4 for privileged mode. */
499 fprintf(stderr
, "%s: protection mode enable attempt\n",
503 case 0x50: /* INTC_IDLE */
505 s
->autoidle
|= value
& 3;
508 /* Per-bank registers */
509 case 0x84: /* INTC_MIR */
511 omap_inth_update(s
, 0);
512 omap_inth_update(s
, 1);
515 case 0x88: /* INTC_MIR_CLEAR */
516 bank
->mask
&= ~value
;
517 omap_inth_update(s
, 0);
518 omap_inth_update(s
, 1);
521 case 0x8c: /* INTC_MIR_SET */
525 case 0x90: /* INTC_ISR_SET */
526 bank
->irqs
|= bank
->swi
|= value
;
527 omap_inth_update(s
, 0);
528 omap_inth_update(s
, 1);
531 case 0x94: /* INTC_ISR_CLEAR */
533 bank
->irqs
= bank
->swi
& bank
->inputs
;
536 /* Per-line registers */
537 case 0x100 ... 0x300: /* INTC_ILR */
538 bank_no
= (offset
- 0x100) >> 7;
539 if (bank_no
> s
->nbanks
)
541 bank
= &s
->bank
[bank_no
];
542 line_no
= (offset
& 0x7f) >> 2;
543 bank
->priority
[line_no
] = (value
>> 2) & 0x3f;
544 bank
->fiq
&= ~(1 << line_no
);
545 bank
->fiq
|= (value
& 1) << line_no
;
548 case 0x00: /* INTC_REVISION */
549 case 0x14: /* INTC_SYSSTATUS */
550 case 0x40: /* INTC_SIR_IRQ */
551 case 0x44: /* INTC_SIR_FIQ */
552 case 0x80: /* INTC_ITR */
553 case 0x98: /* INTC_PENDING_IRQ */
554 case 0x9c: /* INTC_PENDING_FIQ */
561 static CPUReadMemoryFunc
* const omap2_inth_readfn
[] = {
562 omap_badwidth_read32
,
563 omap_badwidth_read32
,
567 static CPUWriteMemoryFunc
* const omap2_inth_writefn
[] = {
573 struct omap_intr_handler_s
*omap2_inth_init(target_phys_addr_t base
,
574 int size
, int nbanks
, qemu_irq
**pins
,
575 qemu_irq parent_irq
, qemu_irq parent_fiq
,
576 omap_clk fclk
, omap_clk iclk
)
579 struct omap_intr_handler_s
*s
= (struct omap_intr_handler_s
*)
580 g_malloc0(sizeof(struct omap_intr_handler_s
) +
581 sizeof(struct omap_intr_handler_bank_s
) * nbanks
);
583 s
->parent_intr
[0] = parent_irq
;
584 s
->parent_intr
[1] = parent_fiq
;
587 s
->pins
= qemu_allocate_irqs(omap_set_intr_noedge
, s
, nbanks
* 32);
593 iomemtype
= cpu_register_io_memory(omap2_inth_readfn
,
594 omap2_inth_writefn
, s
, DEVICE_NATIVE_ENDIAN
);
595 cpu_register_physical_memory(base
, size
, iomemtype
);