Commit bd8938c202c2d8c95c5b0551c312ac9aa2a14aea
1 parent
6c2e4d74
AT91 Advanced Interrupt Controller
Emulation of AIC has 32 input pins corresponding to the input interrupt sources and two output pins corresponding to the IRQ and FIQ pins on the processor. All features of the controller with the exception of external interrupt handling are emulated according to the documentation. This includes emulation of the debug register, edge and level triggered interrupts, software interrupts and interrupt nesting. External interrupt handling is not implemented because it differs between various AT91 family microcontrollers in the number of externally exposed pins. The only feature of the external interrupt processing that is not implemented is inverting the level of input signal. Should the need arise, adding proper implementation of external interrupt logic shouldn't be hard. Signed-off-by: Filip Navara <filip.navara@gmail.com>
Showing
2 changed files
with
384 additions
and
0 deletions
Makefile.target
... | ... | @@ -431,6 +431,7 @@ obj-arm-y += framebuffer.o |
431 | 431 | obj-arm-y += syborg.o syborg_fb.o syborg_interrupt.o syborg_keyboard.o |
432 | 432 | obj-arm-y += syborg_serial.o syborg_timer.o syborg_pointer.o syborg_rtc.o |
433 | 433 | obj-arm-y += syborg_virtio.o |
434 | +obj-arm-y += at91_aic.o | |
434 | 435 | obj-arm-y += gpio_rotary.o gpio_keypad.o |
435 | 436 | |
436 | 437 | ifeq ($(TARGET_BASE_ARCH), arm) | ... | ... |
hw/at91_aic.c
0 → 100644
1 | +/* | |
2 | + * AT91 Advanced Interrupt Controller. | |
3 | + * | |
4 | + * Copyright (c) 2009 Filip Navara | |
5 | + * | |
6 | + * Permission is hereby granted, free of charge, to any person obtaining a copy | |
7 | + * of this software and associated documentation files (the "Software"), to deal | |
8 | + * in the Software without restriction, including without limitation the rights | |
9 | + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | |
10 | + * copies of the Software, and to permit persons to whom the Software is | |
11 | + * furnished to do so, subject to the following conditions: | |
12 | + * | |
13 | + * The above copyright notice and this permission notice shall be included in | |
14 | + * all copies or substantial portions of the Software. | |
15 | + * | |
16 | + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |
17 | + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
18 | + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL | |
19 | + * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | |
20 | + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | |
21 | + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | |
22 | + * THE SOFTWARE. | |
23 | + */ | |
24 | + | |
25 | +#include "sysbus.h" | |
26 | + | |
27 | +/* TODO: Inverting external sources based on SRCTYPE in SMR register */ | |
28 | + | |
29 | +/* #define DEBUG_AIC */ | |
30 | + | |
31 | +#define AIC_SIZE 0x200 | |
32 | + | |
33 | +#define AIC_SMR 0x000 /* Source Mode Register */ | |
34 | +#define AIC_SVR 0x080 /* Source Vector Register */ | |
35 | +#define AIC_IVR 0x100 /* IRQ Vector Register */ | |
36 | +#define AIC_FVR 0x104 /* FIQ Vector Register */ | |
37 | +#define AIC_ISR 0x108 /* Interrupt Status Register */ | |
38 | +#define AIC_IPR 0x10c /* Interrupt Pending Register */ | |
39 | +#define AIC_IMR 0x110 /* Interrupt Mask Register */ | |
40 | +#define AIC_CISR 0x114 /* Core Interrupt Status Register */ | |
41 | +#define AIC_IECR 0x120 /* Interrupt Enable Command Register */ | |
42 | +#define AIC_IDCR 0x124 /* Interrupt Disable Command Register */ | |
43 | +#define AIC_ICCR 0x128 /* Interrupt Clear Command Register */ | |
44 | +#define AIC_ISCR 0x12c /* Interrupt Set Command Register */ | |
45 | +#define AIC_EOICR 0x130 /* End of Interrupt Command Register */ | |
46 | +#define AIC_SPU 0x134 /* Spurious Vector Register */ | |
47 | +#define AIC_DCR 0x138 /* Debug Control Register (Protect) */ | |
48 | +#define AIC_FFER 0x140 /* Fast Forcing Enable Register */ | |
49 | +#define AIC_FFDR 0x144 /* Fast Forcing Disable Register */ | |
50 | +#define AIC_FFSR 0x148 /* Fast Forcing Status Register */ | |
51 | + | |
52 | +#define DCR_PROT 0x01 /* Protect Mode Enabled */ | |
53 | +#define DCR_GMSK 0x02 /* General Mask */ | |
54 | + | |
55 | +#define AIC_GET_PRIORITY(s, irq) \ | |
56 | + ((int)((s)->smr[(irq)] & 7)) | |
57 | +#define AIC_IS_EDGE_TRIGGERED(s, irq) \ | |
58 | + ((int)((s)->smr[(irq)] & (1 << 5))) | |
59 | + | |
60 | +typedef struct AICState { | |
61 | + SysBusDevice busdev; | |
62 | + qemu_irq parent_irq; | |
63 | + qemu_irq parent_fiq; | |
64 | + uint32_t smr[32]; | |
65 | + uint32_t svr[32]; | |
66 | + uint32_t ipr; | |
67 | + uint32_t imr; | |
68 | + uint32_t cisr; | |
69 | + uint32_t spu; | |
70 | + uint32_t dcr; | |
71 | + uint32_t ffsr; | |
72 | + uint8_t stack_irq[8]; | |
73 | + uint8_t stack_pri[8]; | |
74 | + int8_t stack_pos; | |
75 | +} AICState; | |
76 | + | |
77 | +static uint8_t at91_aic_highest(struct AICState *s, uint8_t *priority) | |
78 | +{ | |
79 | + uint32_t pending_interrupts; | |
80 | + uint8_t highest_interrupt = 0; | |
81 | + uint8_t highest_priority = 0; | |
82 | + int i; | |
83 | + | |
84 | + pending_interrupts = s->ipr & s->imr & ~s->ffsr; | |
85 | + for (i = 31; i >= 1; i--) { | |
86 | + if (pending_interrupts & (1 << i)) { | |
87 | + if (AIC_GET_PRIORITY(s, i) >= highest_priority) { | |
88 | + highest_interrupt = i; | |
89 | + highest_priority = AIC_GET_PRIORITY(s, i); | |
90 | + } | |
91 | + } | |
92 | + } | |
93 | + | |
94 | + *priority = highest_priority; | |
95 | + return highest_interrupt; | |
96 | +} | |
97 | + | |
98 | +static void at91_aic_update(AICState *s) | |
99 | +{ | |
100 | + uint32_t requests = s->ipr & s->imr; | |
101 | + uint32_t fiq_mask = 1 | s->ffsr; | |
102 | + | |
103 | + if (s->dcr & DCR_GMSK) { | |
104 | + s->cisr = 0; | |
105 | + } else { | |
106 | + /* Fast interrupts */ | |
107 | + s->cisr = !!(requests & fiq_mask); | |
108 | + /* Priority-driven normal interrupts */ | |
109 | + if (requests & ~fiq_mask) { | |
110 | + uint8_t highest_priority; | |
111 | + at91_aic_highest(s, &highest_priority); | |
112 | + if (s->stack_pos < 0 || | |
113 | + s->stack_pri[s->stack_pos] < highest_priority) { | |
114 | + s->cisr |= 2; | |
115 | + } | |
116 | + } | |
117 | + } | |
118 | + | |
119 | + qemu_set_irq(s->parent_fiq, s->cisr & 1); | |
120 | + qemu_set_irq(s->parent_irq, s->cisr & 2); | |
121 | +} | |
122 | + | |
123 | +static void at91_aic_set_irq(void *opaque, int irq, int level) | |
124 | +{ | |
125 | + struct AICState *s = (struct AICState *) opaque; | |
126 | + int mask = 1 << irq; | |
127 | + | |
128 | + /* TODO: External egde-triggering */ | |
129 | + if (level) | |
130 | + s->ipr |= mask; | |
131 | + else if (!AIC_IS_EDGE_TRIGGERED(s, irq)) | |
132 | + s->ipr &= ~mask; | |
133 | + | |
134 | + at91_aic_update(s); | |
135 | +} | |
136 | + | |
137 | +static inline void at91_aic_irq_enter(AICState *s, uint8_t irq, uint8_t priority) | |
138 | +{ | |
139 | + if (s->stack_pos < 7 && irq != 0) { | |
140 | + s->stack_pos++; | |
141 | + s->stack_irq[s->stack_pos] = irq; | |
142 | + s->stack_pri[s->stack_pos] = priority; | |
143 | + | |
144 | + if (AIC_IS_EDGE_TRIGGERED(s, irq)) { | |
145 | + s->ipr &= ~(1 << irq); | |
146 | + at91_aic_update(s); | |
147 | + } | |
148 | + } | |
149 | +} | |
150 | + | |
151 | +static uint32_t at91_aic_mem_read(void *opaque, target_phys_addr_t offset) | |
152 | +{ | |
153 | + AICState *s = opaque; | |
154 | + uint8_t current_irq; | |
155 | + uint8_t current_pri; | |
156 | + | |
157 | + switch (offset) { | |
158 | + case AIC_IVR: /* Interrupt vector register */ | |
159 | + current_irq = at91_aic_highest(s, ¤t_pri); | |
160 | + if (!(s->dcr & DCR_PROT)) { | |
161 | + at91_aic_irq_enter(s, current_irq, current_pri); | |
162 | + } | |
163 | + return current_irq == 0 ? s->spu : s->svr[current_irq]; | |
164 | + case AIC_FVR: /* FIQ vector register */ | |
165 | + if (s->ipr & 1) { | |
166 | + s->ipr &= ~1; | |
167 | + at91_aic_update(s); | |
168 | + return s->svr[0]; | |
169 | + } else if (s->ipr & s->ffsr) { | |
170 | + return s->svr[0]; | |
171 | + } | |
172 | + return s->spu; | |
173 | + case AIC_ISR: /* Interrupt status register */ | |
174 | + if (s->stack_pos < 0) | |
175 | + return 0; | |
176 | + return s->stack_irq[s->stack_pos]; | |
177 | + case AIC_IPR: /* Interrupt pending register */ | |
178 | + return s->ipr; | |
179 | + case AIC_IMR: /* Interrupt mask register */ | |
180 | + return s->imr; | |
181 | + case AIC_CISR: /* Core interrupt status register */ | |
182 | + return s->cisr; | |
183 | + case AIC_SPU: /* Spurious interrupt vector register */ | |
184 | + return s->spu; | |
185 | + case AIC_DCR: | |
186 | + return s->dcr; | |
187 | + case AIC_FFSR: | |
188 | + return s->ffsr; | |
189 | + case AIC_SMR ... AIC_SMR + 127: | |
190 | + return s->smr[(offset - AIC_SMR) >> 2]; | |
191 | + case AIC_SVR ... AIC_SVR + 127: | |
192 | + return s->svr[(offset - AIC_SVR) >> 2]; | |
193 | + default: | |
194 | + return 0; | |
195 | + } | |
196 | +} | |
197 | + | |
198 | +static void at91_aic_mem_write(void *opaque, target_phys_addr_t offset, | |
199 | + uint32_t value) | |
200 | +{ | |
201 | + AICState *s = opaque; | |
202 | + uint8_t current_irq; | |
203 | + uint8_t current_pri; | |
204 | + int irq; | |
205 | + | |
206 | + switch (offset) { | |
207 | + case AIC_IVR: | |
208 | + if (s->dcr & DCR_PROT) { | |
209 | + current_irq = at91_aic_highest(s, ¤t_pri); | |
210 | + at91_aic_irq_enter(s, current_irq, current_pri); | |
211 | + } | |
212 | + break; | |
213 | + case AIC_IECR: | |
214 | + s->imr |= value; | |
215 | + break; | |
216 | + case AIC_IDCR: | |
217 | + s->imr &= ~value; | |
218 | + break; | |
219 | + case AIC_ICCR: | |
220 | + for (irq = 0; irq < 32; irq++) { | |
221 | + if (!AIC_IS_EDGE_TRIGGERED(s, irq)) | |
222 | + value &= ~(1 << irq); | |
223 | + } | |
224 | + s->ipr &= value; | |
225 | + break; | |
226 | + case AIC_ISCR: | |
227 | + for (irq = 0; irq < 32; irq++) { | |
228 | + if (!AIC_IS_EDGE_TRIGGERED(s, irq)) | |
229 | + value &= ~(1 << irq); | |
230 | + } | |
231 | + s->ipr |= value; | |
232 | + break; | |
233 | + case AIC_EOICR: /* End of interrupt */ | |
234 | + if (s->stack_pos >= 0) | |
235 | + s->stack_pos--; | |
236 | + break; | |
237 | + case AIC_SPU: | |
238 | + s->spu = value; | |
239 | + return; | |
240 | + case AIC_DCR: | |
241 | + s->dcr = value; | |
242 | + break; | |
243 | + case AIC_FFER: | |
244 | + s->ffsr |= value; | |
245 | + break; | |
246 | + case AIC_FFDR: | |
247 | + s->ffsr &= ~value; | |
248 | + break; | |
249 | + case AIC_SMR ... AIC_SMR + 127: | |
250 | + s->smr[(offset - AIC_SMR) >> 2] = value; | |
251 | + break; | |
252 | + case AIC_SVR ... AIC_SVR + 127: | |
253 | + s->svr[(offset - AIC_SVR) >> 2] = value; | |
254 | + return; | |
255 | + default: | |
256 | + return; | |
257 | + } | |
258 | + | |
259 | + at91_aic_update(s); | |
260 | +} | |
261 | + | |
262 | +#ifdef DEBUG_AIC | |
263 | +static uint32_t at91_aic_mem_read_dbg(void *opaque, target_phys_addr_t offset) | |
264 | +{ | |
265 | + uint32_t value = at91_aic_mem_read(opaque, offset); | |
266 | + printf("%s offset=%x val=%x\n", __func__, offset, value); | |
267 | + return value; | |
268 | +} | |
269 | + | |
270 | +static void at91_aic_mem_write_dbg(void *opaque, target_phys_addr_t offset, | |
271 | + uint32_t value) | |
272 | +{ | |
273 | + printf("%s offset=%x val=%x\n", __func__, offset, value); | |
274 | + at91_aic_mem_write(opaque, offset, value); | |
275 | +} | |
276 | + | |
277 | +#define at91_aic_mem_read at91_aic_mem_read_dbg | |
278 | +#define at91_aic_mem_write at91_aic_mem_write_dbg | |
279 | +#endif | |
280 | + | |
281 | +static CPUReadMemoryFunc *at91_aic_readfn[] = { | |
282 | + at91_aic_mem_read, | |
283 | + at91_aic_mem_read, | |
284 | + at91_aic_mem_read, | |
285 | +}; | |
286 | + | |
287 | +static CPUWriteMemoryFunc *at91_aic_writefn[] = { | |
288 | + at91_aic_mem_write, | |
289 | + at91_aic_mem_write, | |
290 | + at91_aic_mem_write, | |
291 | +}; | |
292 | + | |
293 | +static void at91_aic_save(QEMUFile *f, void *opaque) | |
294 | +{ | |
295 | + AICState *s = opaque; | |
296 | + int i; | |
297 | + | |
298 | + for (i = 0; i < 32; i++) { | |
299 | + qemu_put_be32(f, s->smr[i]); | |
300 | + qemu_put_be32(f, s->svr[i]); | |
301 | + } | |
302 | + qemu_put_be32(f, s->ipr); | |
303 | + qemu_put_be32(f, s->imr); | |
304 | + qemu_put_be32(f, s->cisr); | |
305 | + qemu_put_be32(f, s->spu); | |
306 | + qemu_put_be32(f, s->dcr); | |
307 | + qemu_put_be32(f, s->ffsr); | |
308 | + qemu_put_sbyte(f, s->stack_pos); | |
309 | + for (i = 0; i <= s->stack_pos; i++) { | |
310 | + qemu_put_byte(f, s->stack_irq[i]); | |
311 | + qemu_put_byte(f, s->stack_pri[i]); | |
312 | + } | |
313 | +} | |
314 | + | |
315 | +static int at91_aic_load(QEMUFile *f, void *opaque, int version_id) | |
316 | +{ | |
317 | + AICState *s = opaque; | |
318 | + int i; | |
319 | + | |
320 | + if (version_id != 1) | |
321 | + return -EINVAL; | |
322 | + | |
323 | + for (i = 0; i < 32; i++) { | |
324 | + s->smr[i] = qemu_get_be32(f); | |
325 | + s->svr[i] = qemu_get_be32(f); | |
326 | + } | |
327 | + s->ipr = qemu_get_be32(f); | |
328 | + s->imr = qemu_get_be32(f); | |
329 | + s->cisr = qemu_get_be32(f); | |
330 | + s->spu = qemu_get_be32(f); | |
331 | + s->dcr = qemu_get_be32(f); | |
332 | + s->ffsr = qemu_get_be32(f); | |
333 | + s->stack_pos = qemu_get_sbyte(f); | |
334 | + for (i = 0; i <= s->stack_pos; i++) { | |
335 | + s->stack_irq[i] = qemu_get_byte(f); | |
336 | + s->stack_pri[i] = qemu_get_byte(f); | |
337 | + } | |
338 | + | |
339 | + return 0; | |
340 | +} | |
341 | + | |
342 | +static void at91_aic_reset(void *opaque) | |
343 | +{ | |
344 | + AICState *s = opaque; | |
345 | + int i; | |
346 | + | |
347 | + for (i = 0; i < 32; i++) { | |
348 | + s->smr[i] = 0; | |
349 | + s->svr[i] = 0; | |
350 | + } | |
351 | + s->ipr = 0; | |
352 | + s->imr = 0; | |
353 | + s->cisr = 0; | |
354 | + s->spu = 0; | |
355 | + s->dcr = 0; | |
356 | + s->ffsr = 0; | |
357 | + s->stack_pos = -1; | |
358 | +} | |
359 | + | |
360 | +static void at91_aic_init(SysBusDevice *dev) | |
361 | +{ | |
362 | + AICState *s = FROM_SYSBUS(typeof (*s), dev); | |
363 | + int intr_vect_regs; | |
364 | + | |
365 | + qdev_init_gpio_in(&dev->qdev, at91_aic_set_irq, 32); | |
366 | + sysbus_init_irq(dev, &s->parent_irq); | |
367 | + sysbus_init_irq(dev, &s->parent_fiq); | |
368 | + | |
369 | + intr_vect_regs = cpu_register_io_memory(at91_aic_readfn, at91_aic_writefn, s); | |
370 | + sysbus_init_mmio(dev, AIC_SIZE, intr_vect_regs); | |
371 | + | |
372 | + at91_aic_reset(s); | |
373 | + qemu_register_reset(at91_aic_reset, s); | |
374 | + | |
375 | + register_savevm("at91_aic", -1, 1, at91_aic_save, at91_aic_load, s); | |
376 | +} | |
377 | + | |
378 | +static void at91_aic_register(void) | |
379 | +{ | |
380 | + sysbus_register_dev("at91,aic", sizeof(AICState), at91_aic_init); | |
381 | +} | |
382 | + | |
383 | +device_init(at91_aic_register) | ... | ... |