Statistics
| Branch: | Revision:

root / hw / sh_intc.c @ d94f9486

History | View | Annotate | Download (11.9 kB)

1 80f515e6 balrog
/*
2 80f515e6 balrog
 * SuperH interrupt controller module
3 80f515e6 balrog
 *
4 80f515e6 balrog
 * Copyright (c) 2007 Magnus Damm
5 80f515e6 balrog
 * Based on sh_timer.c and arm_timer.c by Paul Brook
6 80f515e6 balrog
 * Copyright (c) 2005-2006 CodeSourcery.
7 80f515e6 balrog
 *
8 80f515e6 balrog
 * This code is licenced under the GPL.
9 80f515e6 balrog
 */
10 80f515e6 balrog
11 80f515e6 balrog
#include <assert.h>
12 80f515e6 balrog
#include "sh_intc.h"
13 87ecb68b pbrook
#include "hw.h"
14 87ecb68b pbrook
#include "sh.h"
15 80f515e6 balrog
16 80f515e6 balrog
//#define DEBUG_INTC
17 e96e2044 ths
//#define DEBUG_INTC_SOURCES
18 80f515e6 balrog
19 80f515e6 balrog
#define INTC_A7(x) ((x) & 0x1fffffff)
20 80f515e6 balrog
21 e96e2044 ths
void sh_intc_toggle_source(struct intc_source *source,
22 e96e2044 ths
                           int enable_adj, int assert_adj)
23 e96e2044 ths
{
24 e96e2044 ths
    int enable_changed = 0;
25 e96e2044 ths
    int pending_changed = 0;
26 e96e2044 ths
    int old_pending;
27 e96e2044 ths
28 e96e2044 ths
    if ((source->enable_count == source->enable_max) && (enable_adj == -1))
29 e96e2044 ths
        enable_changed = -1;
30 e96e2044 ths
31 e96e2044 ths
    source->enable_count += enable_adj;
32 e96e2044 ths
33 e96e2044 ths
    if (source->enable_count == source->enable_max)
34 e96e2044 ths
        enable_changed = 1;
35 e96e2044 ths
36 e96e2044 ths
    source->asserted += assert_adj;
37 e96e2044 ths
38 e96e2044 ths
    old_pending = source->pending;
39 e96e2044 ths
    source->pending = source->asserted &&
40 e96e2044 ths
      (source->enable_count == source->enable_max);
41 e96e2044 ths
42 e96e2044 ths
    if (old_pending != source->pending)
43 e96e2044 ths
        pending_changed = 1;
44 e96e2044 ths
45 e96e2044 ths
    if (pending_changed) {
46 e96e2044 ths
        if (source->pending) {
47 e96e2044 ths
            source->parent->pending++;
48 e96e2044 ths
            if (source->parent->pending == 1)
49 e96e2044 ths
                cpu_interrupt(first_cpu, CPU_INTERRUPT_HARD);
50 e96e2044 ths
        }
51 e96e2044 ths
        else {
52 e96e2044 ths
            source->parent->pending--;
53 e96e2044 ths
            if (source->parent->pending == 0)
54 e96e2044 ths
                cpu_reset_interrupt(first_cpu, CPU_INTERRUPT_HARD);
55 e96e2044 ths
        }
56 e96e2044 ths
    }
57 e96e2044 ths
58 e96e2044 ths
  if (enable_changed || assert_adj || pending_changed) {
59 e96e2044 ths
#ifdef DEBUG_INTC_SOURCES
60 e96e2044 ths
            printf("sh_intc: (%d/%d/%d/%d) interrupt source 0x%x %s%s%s\n",
61 e96e2044 ths
                   source->parent->pending,
62 e96e2044 ths
                   source->asserted,
63 e96e2044 ths
                   source->enable_count,
64 e96e2044 ths
                   source->enable_max,
65 e96e2044 ths
                   source->vect,
66 e96e2044 ths
                   source->asserted ? "asserted " :
67 e96e2044 ths
                   assert_adj ? "deasserted" : "",
68 e96e2044 ths
                   enable_changed == 1 ? "enabled " :
69 e96e2044 ths
                   enable_changed == -1 ? "disabled " : "",
70 e96e2044 ths
                   source->pending ? "pending" : "");
71 e96e2044 ths
#endif
72 e96e2044 ths
  }
73 e96e2044 ths
}
74 e96e2044 ths
75 b79e1752 aurel32
static void sh_intc_set_irq (void *opaque, int n, int level)
76 96e2fc41 aurel32
{
77 96e2fc41 aurel32
  struct intc_desc *desc = opaque;
78 96e2fc41 aurel32
  struct intc_source *source = &(desc->sources[n]);
79 96e2fc41 aurel32
80 4e7ed2d1 aurel32
  if (level && !source->asserted)
81 4e7ed2d1 aurel32
    sh_intc_toggle_source(source, 0, 1);
82 4e7ed2d1 aurel32
  else if (!level && source->asserted)
83 4e7ed2d1 aurel32
    sh_intc_toggle_source(source, 0, -1);
84 96e2fc41 aurel32
}
85 96e2fc41 aurel32
86 e96e2044 ths
int sh_intc_get_pending_vector(struct intc_desc *desc, int imask)
87 e96e2044 ths
{
88 e96e2044 ths
    unsigned int i;
89 e96e2044 ths
90 e96e2044 ths
    /* slow: use a linked lists of pending sources instead */
91 e96e2044 ths
    /* wrong: take interrupt priority into account (one list per priority) */
92 e96e2044 ths
93 e96e2044 ths
    if (imask == 0x0f) {
94 e96e2044 ths
        return -1; /* FIXME, update code to include priority per source */
95 e96e2044 ths
    }
96 e96e2044 ths
97 e96e2044 ths
    for (i = 0; i < desc->nr_sources; i++) {
98 e96e2044 ths
        struct intc_source *source = desc->sources + i;
99 e96e2044 ths
100 e96e2044 ths
        if (source->pending) {
101 e96e2044 ths
#ifdef DEBUG_INTC_SOURCES
102 e96e2044 ths
            printf("sh_intc: (%d) returning interrupt source 0x%x\n",
103 e96e2044 ths
                   desc->pending, source->vect);
104 e96e2044 ths
#endif
105 e96e2044 ths
            return source->vect;
106 e96e2044 ths
        }
107 e96e2044 ths
    }
108 e96e2044 ths
109 e96e2044 ths
    assert(0);
110 e96e2044 ths
}
111 e96e2044 ths
112 80f515e6 balrog
#define INTC_MODE_NONE       0
113 80f515e6 balrog
#define INTC_MODE_DUAL_SET   1
114 80f515e6 balrog
#define INTC_MODE_DUAL_CLR   2
115 80f515e6 balrog
#define INTC_MODE_ENABLE_REG 3
116 80f515e6 balrog
#define INTC_MODE_MASK_REG   4
117 80f515e6 balrog
#define INTC_MODE_IS_PRIO    8
118 80f515e6 balrog
119 80f515e6 balrog
static unsigned int sh_intc_mode(unsigned long address,
120 80f515e6 balrog
                                 unsigned long set_reg, unsigned long clr_reg)
121 80f515e6 balrog
{
122 80f515e6 balrog
    if ((address != INTC_A7(set_reg)) &&
123 80f515e6 balrog
        (address != INTC_A7(clr_reg)))
124 80f515e6 balrog
        return INTC_MODE_NONE;
125 80f515e6 balrog
126 80f515e6 balrog
    if (set_reg && clr_reg) {
127 80f515e6 balrog
        if (address == INTC_A7(set_reg))
128 80f515e6 balrog
            return INTC_MODE_DUAL_SET;
129 80f515e6 balrog
        else
130 80f515e6 balrog
            return INTC_MODE_DUAL_CLR;
131 80f515e6 balrog
    }
132 80f515e6 balrog
133 80f515e6 balrog
    if (set_reg)
134 80f515e6 balrog
        return INTC_MODE_ENABLE_REG;
135 80f515e6 balrog
    else
136 80f515e6 balrog
        return INTC_MODE_MASK_REG;
137 80f515e6 balrog
}
138 80f515e6 balrog
139 80f515e6 balrog
static void sh_intc_locate(struct intc_desc *desc,
140 80f515e6 balrog
                           unsigned long address,
141 80f515e6 balrog
                           unsigned long **datap,
142 80f515e6 balrog
                           intc_enum **enums,
143 80f515e6 balrog
                           unsigned int *first,
144 80f515e6 balrog
                           unsigned int *width,
145 80f515e6 balrog
                           unsigned int *modep)
146 80f515e6 balrog
{
147 80f515e6 balrog
    unsigned int i, mode;
148 80f515e6 balrog
149 80f515e6 balrog
    /* this is slow but works for now */
150 80f515e6 balrog
151 80f515e6 balrog
    if (desc->mask_regs) {
152 80f515e6 balrog
        for (i = 0; i < desc->nr_mask_regs; i++) {
153 80f515e6 balrog
            struct intc_mask_reg *mr = desc->mask_regs + i;
154 80f515e6 balrog
155 80f515e6 balrog
            mode = sh_intc_mode(address, mr->set_reg, mr->clr_reg);
156 80f515e6 balrog
            if (mode == INTC_MODE_NONE)
157 80f515e6 balrog
                continue;
158 80f515e6 balrog
159 80f515e6 balrog
            *modep = mode;
160 80f515e6 balrog
            *datap = &mr->value;
161 80f515e6 balrog
            *enums = mr->enum_ids;
162 80f515e6 balrog
            *first = mr->reg_width - 1;
163 80f515e6 balrog
            *width = 1;
164 80f515e6 balrog
            return;
165 80f515e6 balrog
        }
166 80f515e6 balrog
    }
167 80f515e6 balrog
168 80f515e6 balrog
    if (desc->prio_regs) {
169 80f515e6 balrog
        for (i = 0; i < desc->nr_prio_regs; i++) {
170 80f515e6 balrog
            struct intc_prio_reg *pr = desc->prio_regs + i;
171 80f515e6 balrog
172 80f515e6 balrog
            mode = sh_intc_mode(address, pr->set_reg, pr->clr_reg);
173 80f515e6 balrog
            if (mode == INTC_MODE_NONE)
174 80f515e6 balrog
                continue;
175 80f515e6 balrog
176 80f515e6 balrog
            *modep = mode | INTC_MODE_IS_PRIO;
177 80f515e6 balrog
            *datap = &pr->value;
178 80f515e6 balrog
            *enums = pr->enum_ids;
179 80f515e6 balrog
            *first = (pr->reg_width / pr->field_width) - 1;
180 80f515e6 balrog
            *width = pr->field_width;
181 80f515e6 balrog
            return;
182 80f515e6 balrog
        }
183 80f515e6 balrog
    }
184 80f515e6 balrog
185 80f515e6 balrog
    assert(0);
186 80f515e6 balrog
}
187 80f515e6 balrog
188 e96e2044 ths
static void sh_intc_toggle_mask(struct intc_desc *desc, intc_enum id,
189 e96e2044 ths
                                int enable, int is_group)
190 80f515e6 balrog
{
191 80f515e6 balrog
    struct intc_source *source = desc->sources + id;
192 80f515e6 balrog
193 80f515e6 balrog
    if (!id)
194 80f515e6 balrog
        return;
195 80f515e6 balrog
196 80f515e6 balrog
    if (!source->next_enum_id && (!source->enable_max || !source->vect)) {
197 e96e2044 ths
#ifdef DEBUG_INTC_SOURCES
198 80f515e6 balrog
        printf("sh_intc: reserved interrupt source %d modified\n", id);
199 80f515e6 balrog
#endif
200 80f515e6 balrog
        return;
201 80f515e6 balrog
    }
202 80f515e6 balrog
203 e96e2044 ths
    if (source->vect)
204 e96e2044 ths
        sh_intc_toggle_source(source, enable ? 1 : -1, 0);
205 80f515e6 balrog
206 80f515e6 balrog
#ifdef DEBUG_INTC
207 80f515e6 balrog
    else {
208 80f515e6 balrog
        printf("setting interrupt group %d to %d\n", id, !!enable);
209 80f515e6 balrog
    }
210 80f515e6 balrog
#endif
211 80f515e6 balrog
212 80f515e6 balrog
    if ((is_group || !source->vect) && source->next_enum_id) {
213 e96e2044 ths
        sh_intc_toggle_mask(desc, source->next_enum_id, enable, 1);
214 80f515e6 balrog
    }
215 80f515e6 balrog
216 80f515e6 balrog
#ifdef DEBUG_INTC
217 80f515e6 balrog
    if (!source->vect) {
218 80f515e6 balrog
        printf("setting interrupt group %d to %d - done\n", id, !!enable);
219 80f515e6 balrog
    }
220 80f515e6 balrog
#endif
221 80f515e6 balrog
}
222 80f515e6 balrog
223 80f515e6 balrog
static uint32_t sh_intc_read(void *opaque, target_phys_addr_t offset)
224 80f515e6 balrog
{
225 80f515e6 balrog
    struct intc_desc *desc = opaque;
226 80f515e6 balrog
    intc_enum *enum_ids = NULL;
227 80f515e6 balrog
    unsigned int first = 0;
228 80f515e6 balrog
    unsigned int width = 0;
229 80f515e6 balrog
    unsigned int mode = 0;
230 80f515e6 balrog
    unsigned long *valuep;
231 80f515e6 balrog
232 80f515e6 balrog
#ifdef DEBUG_INTC
233 80f515e6 balrog
    printf("sh_intc_read 0x%lx\n", (unsigned long) offset);
234 80f515e6 balrog
#endif
235 80f515e6 balrog
236 80f515e6 balrog
    sh_intc_locate(desc, (unsigned long)offset, &valuep, 
237 80f515e6 balrog
                   &enum_ids, &first, &width, &mode);
238 80f515e6 balrog
    return *valuep;
239 80f515e6 balrog
}
240 80f515e6 balrog
241 80f515e6 balrog
static void sh_intc_write(void *opaque, target_phys_addr_t offset,
242 80f515e6 balrog
                          uint32_t value)
243 80f515e6 balrog
{
244 80f515e6 balrog
    struct intc_desc *desc = opaque;
245 80f515e6 balrog
    intc_enum *enum_ids = NULL;
246 80f515e6 balrog
    unsigned int first = 0;
247 80f515e6 balrog
    unsigned int width = 0;
248 80f515e6 balrog
    unsigned int mode = 0;
249 80f515e6 balrog
    unsigned int k;
250 80f515e6 balrog
    unsigned long *valuep;
251 80f515e6 balrog
    unsigned long mask;
252 80f515e6 balrog
253 80f515e6 balrog
#ifdef DEBUG_INTC
254 80f515e6 balrog
    printf("sh_intc_write 0x%lx 0x%08x\n", (unsigned long) offset, value);
255 80f515e6 balrog
#endif
256 80f515e6 balrog
257 80f515e6 balrog
    sh_intc_locate(desc, (unsigned long)offset, &valuep, 
258 80f515e6 balrog
                   &enum_ids, &first, &width, &mode);
259 80f515e6 balrog
260 80f515e6 balrog
    switch (mode) {
261 80f515e6 balrog
    case INTC_MODE_ENABLE_REG | INTC_MODE_IS_PRIO: break;
262 80f515e6 balrog
    case INTC_MODE_DUAL_SET: value |= *valuep; break;
263 80f515e6 balrog
    case INTC_MODE_DUAL_CLR: value = *valuep & ~value; break;
264 80f515e6 balrog
    default: assert(0);
265 80f515e6 balrog
    }
266 80f515e6 balrog
267 80f515e6 balrog
    for (k = 0; k <= first; k++) {
268 80f515e6 balrog
        mask = ((1 << width) - 1) << ((first - k) * width);
269 80f515e6 balrog
270 80f515e6 balrog
        if ((*valuep & mask) == (value & mask))
271 80f515e6 balrog
            continue;
272 80f515e6 balrog
#if 0
273 80f515e6 balrog
        printf("k = %d, first = %d, enum = %d, mask = 0x%08x\n", 
274 80f515e6 balrog
               k, first, enum_ids[k], (unsigned int)mask);
275 80f515e6 balrog
#endif
276 e96e2044 ths
        sh_intc_toggle_mask(desc, enum_ids[k], value & mask, 0);
277 80f515e6 balrog
    }
278 80f515e6 balrog
279 80f515e6 balrog
    *valuep = value;
280 80f515e6 balrog
281 80f515e6 balrog
#ifdef DEBUG_INTC
282 80f515e6 balrog
    printf("sh_intc_write 0x%lx -> 0x%08x\n", (unsigned long) offset, value);
283 80f515e6 balrog
#endif
284 80f515e6 balrog
}
285 80f515e6 balrog
286 80f515e6 balrog
static CPUReadMemoryFunc *sh_intc_readfn[] = {
287 80f515e6 balrog
    sh_intc_read,
288 80f515e6 balrog
    sh_intc_read,
289 80f515e6 balrog
    sh_intc_read
290 80f515e6 balrog
};
291 80f515e6 balrog
292 80f515e6 balrog
static CPUWriteMemoryFunc *sh_intc_writefn[] = {
293 80f515e6 balrog
    sh_intc_write,
294 80f515e6 balrog
    sh_intc_write,
295 80f515e6 balrog
    sh_intc_write
296 80f515e6 balrog
};
297 80f515e6 balrog
298 80f515e6 balrog
struct intc_source *sh_intc_source(struct intc_desc *desc, intc_enum id)
299 80f515e6 balrog
{
300 80f515e6 balrog
    if (id)
301 80f515e6 balrog
        return desc->sources + id;
302 80f515e6 balrog
303 80f515e6 balrog
    return NULL;
304 80f515e6 balrog
}
305 80f515e6 balrog
306 80f515e6 balrog
static void sh_intc_register(struct intc_desc *desc, 
307 80f515e6 balrog
                             unsigned long address)
308 80f515e6 balrog
{
309 5c16736a balrog
    if (address) {
310 5c16736a balrog
        cpu_register_physical_memory_offset(P4ADDR(address), 4,
311 8da3ff18 pbrook
                                            desc->iomemtype, INTC_A7(address));
312 5c16736a balrog
        cpu_register_physical_memory_offset(A7ADDR(address), 4,
313 5c16736a balrog
                                            desc->iomemtype, INTC_A7(address));
314 5c16736a balrog
    }
315 80f515e6 balrog
}
316 80f515e6 balrog
317 80f515e6 balrog
static void sh_intc_register_source(struct intc_desc *desc,
318 80f515e6 balrog
                                    intc_enum source,
319 80f515e6 balrog
                                    struct intc_group *groups,
320 80f515e6 balrog
                                    int nr_groups)
321 80f515e6 balrog
{
322 80f515e6 balrog
    unsigned int i, k;
323 80f515e6 balrog
    struct intc_source *s;
324 80f515e6 balrog
325 80f515e6 balrog
    if (desc->mask_regs) {
326 80f515e6 balrog
        for (i = 0; i < desc->nr_mask_regs; i++) {
327 80f515e6 balrog
            struct intc_mask_reg *mr = desc->mask_regs + i;
328 80f515e6 balrog
329 b1503cda malc
            for (k = 0; k < ARRAY_SIZE(mr->enum_ids); k++) {
330 80f515e6 balrog
                if (mr->enum_ids[k] != source)
331 80f515e6 balrog
                    continue;
332 80f515e6 balrog
333 80f515e6 balrog
                s = sh_intc_source(desc, mr->enum_ids[k]);
334 80f515e6 balrog
                if (s)
335 80f515e6 balrog
                    s->enable_max++;
336 80f515e6 balrog
            }
337 80f515e6 balrog
        }
338 80f515e6 balrog
    }
339 80f515e6 balrog
340 80f515e6 balrog
    if (desc->prio_regs) {
341 80f515e6 balrog
        for (i = 0; i < desc->nr_prio_regs; i++) {
342 80f515e6 balrog
            struct intc_prio_reg *pr = desc->prio_regs + i;
343 80f515e6 balrog
344 b1503cda malc
            for (k = 0; k < ARRAY_SIZE(pr->enum_ids); k++) {
345 80f515e6 balrog
                if (pr->enum_ids[k] != source)
346 80f515e6 balrog
                    continue;
347 80f515e6 balrog
348 80f515e6 balrog
                s = sh_intc_source(desc, pr->enum_ids[k]);
349 80f515e6 balrog
                if (s)
350 80f515e6 balrog
                    s->enable_max++;
351 80f515e6 balrog
            }
352 80f515e6 balrog
        }
353 80f515e6 balrog
    }
354 80f515e6 balrog
355 80f515e6 balrog
    if (groups) {
356 80f515e6 balrog
        for (i = 0; i < nr_groups; i++) {
357 80f515e6 balrog
            struct intc_group *gr = groups + i;
358 80f515e6 balrog
359 b1503cda malc
            for (k = 0; k < ARRAY_SIZE(gr->enum_ids); k++) {
360 80f515e6 balrog
                if (gr->enum_ids[k] != source)
361 80f515e6 balrog
                    continue;
362 80f515e6 balrog
363 80f515e6 balrog
                s = sh_intc_source(desc, gr->enum_ids[k]);
364 80f515e6 balrog
                if (s)
365 80f515e6 balrog
                    s->enable_max++;
366 80f515e6 balrog
            }
367 80f515e6 balrog
        }
368 80f515e6 balrog
    }
369 80f515e6 balrog
370 80f515e6 balrog
}
371 80f515e6 balrog
372 80f515e6 balrog
void sh_intc_register_sources(struct intc_desc *desc,
373 80f515e6 balrog
                              struct intc_vect *vectors,
374 80f515e6 balrog
                              int nr_vectors,
375 80f515e6 balrog
                              struct intc_group *groups,
376 80f515e6 balrog
                              int nr_groups)
377 80f515e6 balrog
{
378 80f515e6 balrog
    unsigned int i, k;
379 80f515e6 balrog
    struct intc_source *s;
380 80f515e6 balrog
381 80f515e6 balrog
    for (i = 0; i < nr_vectors; i++) {
382 80f515e6 balrog
        struct intc_vect *vect = vectors + i;
383 80f515e6 balrog
384 80f515e6 balrog
        sh_intc_register_source(desc, vect->enum_id, groups, nr_groups);
385 80f515e6 balrog
        s = sh_intc_source(desc, vect->enum_id);
386 80f515e6 balrog
        if (s)
387 80f515e6 balrog
            s->vect = vect->vect;
388 80f515e6 balrog
389 e96e2044 ths
#ifdef DEBUG_INTC_SOURCES
390 80f515e6 balrog
        printf("sh_intc: registered source %d -> 0x%04x (%d/%d)\n",
391 80f515e6 balrog
               vect->enum_id, s->vect, s->enable_count, s->enable_max);
392 80f515e6 balrog
#endif
393 80f515e6 balrog
    }
394 80f515e6 balrog
395 80f515e6 balrog
    if (groups) {
396 80f515e6 balrog
        for (i = 0; i < nr_groups; i++) {
397 80f515e6 balrog
            struct intc_group *gr = groups + i;
398 80f515e6 balrog
399 80f515e6 balrog
            s = sh_intc_source(desc, gr->enum_id);
400 80f515e6 balrog
            s->next_enum_id = gr->enum_ids[0];
401 80f515e6 balrog
402 b1503cda malc
            for (k = 1; k < ARRAY_SIZE(gr->enum_ids); k++) {
403 80f515e6 balrog
                if (!gr->enum_ids[k])
404 80f515e6 balrog
                    continue;
405 80f515e6 balrog
406 80f515e6 balrog
                s = sh_intc_source(desc, gr->enum_ids[k - 1]);
407 80f515e6 balrog
                s->next_enum_id = gr->enum_ids[k];
408 80f515e6 balrog
            }
409 80f515e6 balrog
410 e96e2044 ths
#ifdef DEBUG_INTC_SOURCES
411 80f515e6 balrog
            printf("sh_intc: registered group %d (%d/%d)\n",
412 80f515e6 balrog
                   gr->enum_id, s->enable_count, s->enable_max);
413 80f515e6 balrog
#endif
414 80f515e6 balrog
        }
415 80f515e6 balrog
    }
416 80f515e6 balrog
}
417 80f515e6 balrog
418 80f515e6 balrog
int sh_intc_init(struct intc_desc *desc,
419 80f515e6 balrog
                 int nr_sources,
420 80f515e6 balrog
                 struct intc_mask_reg *mask_regs,
421 80f515e6 balrog
                 int nr_mask_regs,
422 80f515e6 balrog
                 struct intc_prio_reg *prio_regs,
423 80f515e6 balrog
                 int nr_prio_regs)
424 80f515e6 balrog
{
425 80f515e6 balrog
    unsigned int i;
426 80f515e6 balrog
427 e96e2044 ths
    desc->pending = 0;
428 80f515e6 balrog
    desc->nr_sources = nr_sources;
429 80f515e6 balrog
    desc->mask_regs = mask_regs;
430 80f515e6 balrog
    desc->nr_mask_regs = nr_mask_regs;
431 80f515e6 balrog
    desc->prio_regs = prio_regs;
432 80f515e6 balrog
    desc->nr_prio_regs = nr_prio_regs;
433 80f515e6 balrog
434 80f515e6 balrog
    i = sizeof(struct intc_source) * nr_sources;
435 487414f1 aliguori
    desc->sources = qemu_malloc(i);
436 80f515e6 balrog
437 80f515e6 balrog
    memset(desc->sources, 0, i);
438 e96e2044 ths
    for (i = 0; i < desc->nr_sources; i++) {
439 e96e2044 ths
        struct intc_source *source = desc->sources + i;
440 e96e2044 ths
441 e96e2044 ths
        source->parent = desc;
442 e96e2044 ths
    }
443 96e2fc41 aurel32
444 96e2fc41 aurel32
    desc->irqs = qemu_allocate_irqs(sh_intc_set_irq, desc, nr_sources);
445 80f515e6 balrog
 
446 80f515e6 balrog
    desc->iomemtype = cpu_register_io_memory(0, sh_intc_readfn,
447 80f515e6 balrog
                                             sh_intc_writefn, desc);
448 80f515e6 balrog
    if (desc->mask_regs) {
449 80f515e6 balrog
        for (i = 0; i < desc->nr_mask_regs; i++) {
450 80f515e6 balrog
            struct intc_mask_reg *mr = desc->mask_regs + i;
451 80f515e6 balrog
452 80f515e6 balrog
            sh_intc_register(desc, mr->set_reg);
453 80f515e6 balrog
            sh_intc_register(desc, mr->clr_reg);
454 80f515e6 balrog
        }
455 80f515e6 balrog
    }
456 80f515e6 balrog
457 80f515e6 balrog
    if (desc->prio_regs) {
458 80f515e6 balrog
        for (i = 0; i < desc->nr_prio_regs; i++) {
459 80f515e6 balrog
            struct intc_prio_reg *pr = desc->prio_regs + i;
460 80f515e6 balrog
461 80f515e6 balrog
            sh_intc_register(desc, pr->set_reg);
462 80f515e6 balrog
            sh_intc_register(desc, pr->clr_reg);
463 80f515e6 balrog
        }
464 80f515e6 balrog
    }
465 80f515e6 balrog
466 80f515e6 balrog
    return 0;
467 80f515e6 balrog
}
468 c6d86a33 balrog
469 c6d86a33 balrog
/* Assert level <n> IRL interrupt. 
470 c6d86a33 balrog
   0:deassert. 1:lowest priority,... 15:highest priority. */
471 c6d86a33 balrog
void sh_intc_set_irl(void *opaque, int n, int level)
472 c6d86a33 balrog
{
473 c6d86a33 balrog
    struct intc_source *s = opaque;
474 c6d86a33 balrog
    int i, irl = level ^ 15;
475 c6d86a33 balrog
    for (i = 0; (s = sh_intc_source(s->parent, s->next_enum_id)); i++) {
476 c6d86a33 balrog
        if (i == irl)
477 c6d86a33 balrog
            sh_intc_toggle_source(s, s->enable_count?0:1, s->asserted?0:1);
478 c6d86a33 balrog
        else
479 c6d86a33 balrog
            if (s->asserted)
480 c6d86a33 balrog
                sh_intc_toggle_source(s, 0, -1);
481 c6d86a33 balrog
    }
482 c6d86a33 balrog
}