2 * SuperH interrupt controller module
4 * Copyright (c) 2007 Magnus Damm
5 * Based on sh_timer.c and arm_timer.c by Paul Brook
6 * Copyright (c) 2005-2006 CodeSourcery.
8 * This code is licenced under the GPL.
17 //#define DEBUG_INTC_SOURCES
19 #define INTC_A7(x) ((x) & 0x1fffffff)
21 void sh_intc_toggle_source(struct intc_source
*source
,
22 int enable_adj
, int assert_adj
)
24 int enable_changed
= 0;
25 int pending_changed
= 0;
28 if ((source
->enable_count
== source
->enable_max
) && (enable_adj
== -1))
31 source
->enable_count
+= enable_adj
;
33 if (source
->enable_count
== source
->enable_max
)
36 source
->asserted
+= assert_adj
;
38 old_pending
= source
->pending
;
39 source
->pending
= source
->asserted
&&
40 (source
->enable_count
== source
->enable_max
);
42 if (old_pending
!= source
->pending
)
45 if (pending_changed
) {
46 if (source
->pending
) {
47 source
->parent
->pending
++;
48 if (source
->parent
->pending
== 1)
49 cpu_interrupt(first_cpu
, CPU_INTERRUPT_HARD
);
52 source
->parent
->pending
--;
53 if (source
->parent
->pending
== 0)
54 cpu_reset_interrupt(first_cpu
, CPU_INTERRUPT_HARD
);
58 if (enable_changed
|| assert_adj
|| pending_changed
) {
59 #ifdef DEBUG_INTC_SOURCES
60 printf("sh_intc: (%d/%d/%d/%d) interrupt source 0x%x %s%s%s\n",
61 source
->parent
->pending
,
66 source
->asserted
? "asserted " :
67 assert_adj
? "deasserted" : "",
68 enable_changed
== 1 ? "enabled " :
69 enable_changed
== -1 ? "disabled " : "",
70 source
->pending
? "pending" : "");
75 static void sh_intc_set_irq (void *opaque
, int n
, int level
)
77 struct intc_desc
*desc
= opaque
;
78 struct intc_source
*source
= &(desc
->sources
[n
]);
80 if (level
&& !source
->asserted
)
81 sh_intc_toggle_source(source
, 0, 1);
82 else if (!level
&& source
->asserted
)
83 sh_intc_toggle_source(source
, 0, -1);
86 int sh_intc_get_pending_vector(struct intc_desc
*desc
, int imask
)
90 /* slow: use a linked lists of pending sources instead */
91 /* wrong: take interrupt priority into account (one list per priority) */
94 return -1; /* FIXME, update code to include priority per source */
97 for (i
= 0; i
< desc
->nr_sources
; i
++) {
98 struct intc_source
*source
= desc
->sources
+ i
;
100 if (source
->pending
) {
101 #ifdef DEBUG_INTC_SOURCES
102 printf("sh_intc: (%d) returning interrupt source 0x%x\n",
103 desc
->pending
, source
->vect
);
112 #define INTC_MODE_NONE 0
113 #define INTC_MODE_DUAL_SET 1
114 #define INTC_MODE_DUAL_CLR 2
115 #define INTC_MODE_ENABLE_REG 3
116 #define INTC_MODE_MASK_REG 4
117 #define INTC_MODE_IS_PRIO 8
119 static unsigned int sh_intc_mode(unsigned long address
,
120 unsigned long set_reg
, unsigned long clr_reg
)
122 if ((address
!= INTC_A7(set_reg
)) &&
123 (address
!= INTC_A7(clr_reg
)))
124 return INTC_MODE_NONE
;
126 if (set_reg
&& clr_reg
) {
127 if (address
== INTC_A7(set_reg
))
128 return INTC_MODE_DUAL_SET
;
130 return INTC_MODE_DUAL_CLR
;
134 return INTC_MODE_ENABLE_REG
;
136 return INTC_MODE_MASK_REG
;
139 static void sh_intc_locate(struct intc_desc
*desc
,
140 unsigned long address
,
141 unsigned long **datap
,
147 unsigned int i
, mode
;
149 /* this is slow but works for now */
151 if (desc
->mask_regs
) {
152 for (i
= 0; i
< desc
->nr_mask_regs
; i
++) {
153 struct intc_mask_reg
*mr
= desc
->mask_regs
+ i
;
155 mode
= sh_intc_mode(address
, mr
->set_reg
, mr
->clr_reg
);
156 if (mode
== INTC_MODE_NONE
)
161 *enums
= mr
->enum_ids
;
162 *first
= mr
->reg_width
- 1;
168 if (desc
->prio_regs
) {
169 for (i
= 0; i
< desc
->nr_prio_regs
; i
++) {
170 struct intc_prio_reg
*pr
= desc
->prio_regs
+ i
;
172 mode
= sh_intc_mode(address
, pr
->set_reg
, pr
->clr_reg
);
173 if (mode
== INTC_MODE_NONE
)
176 *modep
= mode
| INTC_MODE_IS_PRIO
;
178 *enums
= pr
->enum_ids
;
179 *first
= (pr
->reg_width
/ pr
->field_width
) - 1;
180 *width
= pr
->field_width
;
188 static void sh_intc_toggle_mask(struct intc_desc
*desc
, intc_enum id
,
189 int enable
, int is_group
)
191 struct intc_source
*source
= desc
->sources
+ id
;
196 if (!source
->next_enum_id
&& (!source
->enable_max
|| !source
->vect
)) {
197 #ifdef DEBUG_INTC_SOURCES
198 printf("sh_intc: reserved interrupt source %d modified\n", id
);
204 sh_intc_toggle_source(source
, enable
? 1 : -1, 0);
208 printf("setting interrupt group %d to %d\n", id
, !!enable
);
212 if ((is_group
|| !source
->vect
) && source
->next_enum_id
) {
213 sh_intc_toggle_mask(desc
, source
->next_enum_id
, enable
, 1);
218 printf("setting interrupt group %d to %d - done\n", id
, !!enable
);
223 static uint32_t sh_intc_read(void *opaque
, target_phys_addr_t offset
)
225 struct intc_desc
*desc
= opaque
;
226 intc_enum
*enum_ids
= NULL
;
227 unsigned int first
= 0;
228 unsigned int width
= 0;
229 unsigned int mode
= 0;
230 unsigned long *valuep
;
233 printf("sh_intc_read 0x%lx\n", (unsigned long) offset
);
236 sh_intc_locate(desc
, (unsigned long)offset
, &valuep
,
237 &enum_ids
, &first
, &width
, &mode
);
241 static void sh_intc_write(void *opaque
, target_phys_addr_t offset
,
244 struct intc_desc
*desc
= opaque
;
245 intc_enum
*enum_ids
= NULL
;
246 unsigned int first
= 0;
247 unsigned int width
= 0;
248 unsigned int mode
= 0;
250 unsigned long *valuep
;
254 printf("sh_intc_write 0x%lx 0x%08x\n", (unsigned long) offset
, value
);
257 sh_intc_locate(desc
, (unsigned long)offset
, &valuep
,
258 &enum_ids
, &first
, &width
, &mode
);
261 case INTC_MODE_ENABLE_REG
| INTC_MODE_IS_PRIO
: break;
262 case INTC_MODE_DUAL_SET
: value
|= *valuep
; break;
263 case INTC_MODE_DUAL_CLR
: value
= *valuep
& ~value
; break;
267 for (k
= 0; k
<= first
; k
++) {
268 mask
= ((1 << width
) - 1) << ((first
- k
) * width
);
270 if ((*valuep
& mask
) == (value
& mask
))
273 printf("k = %d, first = %d, enum = %d, mask = 0x%08x\n",
274 k
, first
, enum_ids
[k
], (unsigned int)mask
);
276 sh_intc_toggle_mask(desc
, enum_ids
[k
], value
& mask
, 0);
282 printf("sh_intc_write 0x%lx -> 0x%08x\n", (unsigned long) offset
, value
);
286 static CPUReadMemoryFunc
*sh_intc_readfn
[] = {
292 static CPUWriteMemoryFunc
*sh_intc_writefn
[] = {
298 struct intc_source
*sh_intc_source(struct intc_desc
*desc
, intc_enum id
)
301 return desc
->sources
+ id
;
306 static void sh_intc_register(struct intc_desc
*desc
,
307 unsigned long address
)
310 cpu_register_physical_memory_offset(P4ADDR(address
), 4,
311 desc
->iomemtype
, INTC_A7(address
));
312 cpu_register_physical_memory_offset(A7ADDR(address
), 4,
313 desc
->iomemtype
, INTC_A7(address
));
317 static void sh_intc_register_source(struct intc_desc
*desc
,
319 struct intc_group
*groups
,
323 struct intc_source
*s
;
325 if (desc
->mask_regs
) {
326 for (i
= 0; i
< desc
->nr_mask_regs
; i
++) {
327 struct intc_mask_reg
*mr
= desc
->mask_regs
+ i
;
329 for (k
= 0; k
< ARRAY_SIZE(mr
->enum_ids
); k
++) {
330 if (mr
->enum_ids
[k
] != source
)
333 s
= sh_intc_source(desc
, mr
->enum_ids
[k
]);
340 if (desc
->prio_regs
) {
341 for (i
= 0; i
< desc
->nr_prio_regs
; i
++) {
342 struct intc_prio_reg
*pr
= desc
->prio_regs
+ i
;
344 for (k
= 0; k
< ARRAY_SIZE(pr
->enum_ids
); k
++) {
345 if (pr
->enum_ids
[k
] != source
)
348 s
= sh_intc_source(desc
, pr
->enum_ids
[k
]);
356 for (i
= 0; i
< nr_groups
; i
++) {
357 struct intc_group
*gr
= groups
+ i
;
359 for (k
= 0; k
< ARRAY_SIZE(gr
->enum_ids
); k
++) {
360 if (gr
->enum_ids
[k
] != source
)
363 s
= sh_intc_source(desc
, gr
->enum_ids
[k
]);
372 void sh_intc_register_sources(struct intc_desc
*desc
,
373 struct intc_vect
*vectors
,
375 struct intc_group
*groups
,
379 struct intc_source
*s
;
381 for (i
= 0; i
< nr_vectors
; i
++) {
382 struct intc_vect
*vect
= vectors
+ i
;
384 sh_intc_register_source(desc
, vect
->enum_id
, groups
, nr_groups
);
385 s
= sh_intc_source(desc
, vect
->enum_id
);
387 s
->vect
= vect
->vect
;
389 #ifdef DEBUG_INTC_SOURCES
390 printf("sh_intc: registered source %d -> 0x%04x (%d/%d)\n",
391 vect
->enum_id
, s
->vect
, s
->enable_count
, s
->enable_max
);
396 for (i
= 0; i
< nr_groups
; i
++) {
397 struct intc_group
*gr
= groups
+ i
;
399 s
= sh_intc_source(desc
, gr
->enum_id
);
400 s
->next_enum_id
= gr
->enum_ids
[0];
402 for (k
= 1; k
< ARRAY_SIZE(gr
->enum_ids
); k
++) {
403 if (!gr
->enum_ids
[k
])
406 s
= sh_intc_source(desc
, gr
->enum_ids
[k
- 1]);
407 s
->next_enum_id
= gr
->enum_ids
[k
];
410 #ifdef DEBUG_INTC_SOURCES
411 printf("sh_intc: registered group %d (%d/%d)\n",
412 gr
->enum_id
, s
->enable_count
, s
->enable_max
);
418 int sh_intc_init(struct intc_desc
*desc
,
420 struct intc_mask_reg
*mask_regs
,
422 struct intc_prio_reg
*prio_regs
,
428 desc
->nr_sources
= nr_sources
;
429 desc
->mask_regs
= mask_regs
;
430 desc
->nr_mask_regs
= nr_mask_regs
;
431 desc
->prio_regs
= prio_regs
;
432 desc
->nr_prio_regs
= nr_prio_regs
;
434 i
= sizeof(struct intc_source
) * nr_sources
;
435 desc
->sources
= qemu_malloc(i
);
437 memset(desc
->sources
, 0, i
);
438 for (i
= 0; i
< desc
->nr_sources
; i
++) {
439 struct intc_source
*source
= desc
->sources
+ i
;
441 source
->parent
= desc
;
444 desc
->irqs
= qemu_allocate_irqs(sh_intc_set_irq
, desc
, nr_sources
);
446 desc
->iomemtype
= cpu_register_io_memory(0, sh_intc_readfn
,
447 sh_intc_writefn
, desc
);
448 if (desc
->mask_regs
) {
449 for (i
= 0; i
< desc
->nr_mask_regs
; i
++) {
450 struct intc_mask_reg
*mr
= desc
->mask_regs
+ i
;
452 sh_intc_register(desc
, mr
->set_reg
);
453 sh_intc_register(desc
, mr
->clr_reg
);
457 if (desc
->prio_regs
) {
458 for (i
= 0; i
< desc
->nr_prio_regs
; i
++) {
459 struct intc_prio_reg
*pr
= desc
->prio_regs
+ i
;
461 sh_intc_register(desc
, pr
->set_reg
);
462 sh_intc_register(desc
, pr
->clr_reg
);
469 /* Assert level <n> IRL interrupt.
470 0:deassert. 1:lowest priority,... 15:highest priority. */
471 void sh_intc_set_irl(void *opaque
, int n
, int level
)
473 struct intc_source
*s
= opaque
;
474 int i
, irl
= level
^ 15;
475 for (i
= 0; (s
= sh_intc_source(s
->parent
, s
->next_enum_id
)); i
++) {
477 sh_intc_toggle_source(s
, s
->enable_count
?0:1, s
->asserted
?0:1);
480 sh_intc_toggle_source(s
, 0, -1);