at91aic.c revision 1.2 1 1.2 matt /* $Id: at91aic.c,v 1.2 2008/07/03 01:15:38 matt Exp $ */
2 1.2 matt /* $NetBSD: at91aic.c,v 1.2 2008/07/03 01:15:38 matt Exp $ */
3 1.2 matt
4 1.2 matt /*
5 1.2 matt * Copyright (c) 2007 Embedtronics Oy.
6 1.2 matt * All rights reserved.
7 1.2 matt *
8 1.2 matt * Based on ep93xx_intr.c
9 1.2 matt * Copyright (c) 2002 The NetBSD Foundation, Inc.
10 1.2 matt * All rights reserved.
11 1.2 matt *
12 1.2 matt * This code is derived from software contributed to The NetBSD Foundation
13 1.2 matt * by Jesse Off
14 1.2 matt *
15 1.2 matt * This code is derived from software contributed to The NetBSD Foundation
16 1.2 matt * by Ichiro FUKUHARA and Naoto Shimazaki.
17 1.2 matt *
18 1.2 matt * Redistribution and use in source and binary forms, with or without
19 1.2 matt * modification, are permitted provided that the following conditions
20 1.2 matt * are met:
21 1.2 matt * 1. Redistributions of source code must retain the above copyright
22 1.2 matt * notice, this list of conditions and the following disclaimer.
23 1.2 matt * 2. Redistributions in binary form must reproduce the above copyright
24 1.2 matt * notice, this list of conditions and the following disclaimer in the
25 1.2 matt * documentation and/or other materials provided with the distribution.
26 1.2 matt * 3. All advertising materials mentioning features or use of this software
27 1.2 matt * must display the following acknowledgement:
28 1.2 matt * This product includes software developed by the NetBSD
29 1.2 matt * Foundation, Inc. and its contributors.
30 1.2 matt * 4. Neither the name of The NetBSD Foundation nor the names of its
31 1.2 matt * contributors may be used to endorse or promote products derived
32 1.2 matt * from this software without specific prior written permission.
33 1.2 matt *
34 1.2 matt * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
35 1.2 matt * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
36 1.2 matt * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
37 1.2 matt * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
38 1.2 matt * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
39 1.2 matt * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
40 1.2 matt * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
41 1.2 matt * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
42 1.2 matt * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
43 1.2 matt * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
44 1.2 matt * POSSIBILITY OF SUCH DAMAGE.
45 1.2 matt */
46 1.2 matt
47 1.2 matt
48 1.2 matt /*
49 1.2 matt * Interrupt support for the Atmel's AT91xx9xxx family controllers
50 1.2 matt */
51 1.2 matt
52 1.2 matt #include <sys/param.h>
53 1.2 matt #include <sys/systm.h>
54 1.2 matt #include <sys/malloc.h>
55 1.2 matt #include <sys/termios.h>
56 1.2 matt
57 1.2 matt #include <uvm/uvm_extern.h>
58 1.2 matt
59 1.2 matt #include <machine/bus.h>
60 1.2 matt #include <machine/intr.h>
61 1.2 matt
62 1.2 matt #include <arm/cpufunc.h>
63 1.2 matt
64 1.2 matt #include <arm/at91/at91reg.h>
65 1.2 matt #include <arm/at91/at91var.h>
66 1.2 matt #include <arm/at91/at91aicreg.h>
67 1.2 matt #include <arm/at91/at91aicvar.h>
68 1.2 matt
69 1.2 matt #define NIRQ 32
70 1.2 matt
71 1.2 matt /* Interrupt handler queues. */
72 1.2 matt struct intrq intrq[NIRQ];
73 1.2 matt
74 1.2 matt /* Interrupts to mask at each level. */
75 1.2 matt static u_int32_t aic_imask[NIPL];
76 1.2 matt
77 1.2 matt /* Software copy of the IRQs we have enabled. */
78 1.2 matt volatile u_int32_t aic_intr_enabled;
79 1.2 matt
80 1.2 matt #define AICREG(reg) *((volatile u_int32_t*) (AT91AIC_BASE + (reg)))
81 1.2 matt
82 1.2 matt static int at91aic_match(device_t, cfdata_t, void *);
83 1.2 matt static void at91aic_attach(device_t, device_t, void *);
84 1.2 matt
85 1.2 matt CFATTACH_DECL(at91aic, sizeof(struct device),
86 1.2 matt at91aic_match, at91aic_attach, NULL, NULL);
87 1.2 matt
88 1.2 matt static int
89 1.2 matt at91aic_match(device_t parent, cfdata_t match, void *aux)
90 1.2 matt {
91 1.2 matt if (strcmp(match->cf_name, "at91aic") == 0)
92 1.2 matt return 2;
93 1.2 matt return 0;
94 1.2 matt }
95 1.2 matt
96 1.2 matt static void
97 1.2 matt at91aic_attach(device_t parent, device_t self, void *aux)
98 1.2 matt {
99 1.2 matt (void)parent; (void)self; (void)aux;
100 1.2 matt printf("\n");
101 1.2 matt }
102 1.2 matt
103 1.2 matt static inline void
104 1.2 matt at91_set_intrmask(u_int32_t aic_irqs)
105 1.2 matt {
106 1.2 matt AICREG(AIC_IDCR) = aic_irqs;
107 1.2 matt AICREG(AIC_IECR) = aic_intr_enabled & ~aic_irqs;
108 1.2 matt }
109 1.2 matt
110 1.2 matt static inline void
111 1.2 matt at91_enable_irq(int irq)
112 1.2 matt {
113 1.2 matt aic_intr_enabled |= (1U << irq);
114 1.2 matt AICREG(AIC_IECR) = (1U << irq);
115 1.2 matt }
116 1.2 matt
117 1.2 matt static inline void
118 1.2 matt at91_disable_irq(int irq)
119 1.2 matt {
120 1.2 matt aic_intr_enabled &= ~(1U << irq);
121 1.2 matt AICREG(AIC_IDCR) = (1U << irq);
122 1.2 matt }
123 1.2 matt
124 1.2 matt /*
125 1.2 matt * NOTE: This routine must be called with interrupts disabled in the CPSR.
126 1.2 matt */
127 1.2 matt static void
128 1.2 matt at91aic_calculate_masks(void)
129 1.2 matt {
130 1.2 matt struct intrq *iq;
131 1.2 matt struct intrhand *ih;
132 1.2 matt int irq, ipl;
133 1.2 matt
134 1.2 matt /* First, figure out which IPLs each IRQ has. */
135 1.2 matt for (irq = 0; irq < NIRQ; irq++) {
136 1.2 matt int levels = 0;
137 1.2 matt iq = &intrq[irq];
138 1.2 matt at91_disable_irq(irq);
139 1.2 matt for (ih = TAILQ_FIRST(&iq->iq_list); ih != NULL;
140 1.2 matt ih = TAILQ_NEXT(ih, ih_list))
141 1.2 matt levels |= (1U << ih->ih_ipl);
142 1.2 matt iq->iq_levels = levels;
143 1.2 matt }
144 1.2 matt
145 1.2 matt /* Next, figure out which IRQs are used by each IPL. */
146 1.2 matt for (ipl = 0; ipl < NIPL; ipl++) {
147 1.2 matt int aic_irqs = 0;
148 1.2 matt for (irq = 0; irq < AIC_NIRQ; irq++) {
149 1.2 matt if (intrq[irq].iq_levels & (1U << ipl))
150 1.2 matt aic_irqs |= (1U << irq);
151 1.2 matt }
152 1.2 matt aic_imask[ipl] = aic_irqs;
153 1.2 matt }
154 1.2 matt
155 1.2 matt aic_imask[IPL_NONE] = 0;
156 1.2 matt
157 1.2 matt /*
158 1.2 matt * splvm() blocks all interrupts that use the kernel memory
159 1.2 matt * allocation facilities.
160 1.2 matt */
161 1.2 matt aic_imask[IPL_VM] |= aic_imask[IPL_NONE];
162 1.2 matt
163 1.2 matt /*
164 1.2 matt * splclock() must block anything that uses the scheduler.
165 1.2 matt */
166 1.2 matt aic_imask[IPL_CLOCK] |= aic_imask[IPL_VM];
167 1.2 matt
168 1.2 matt /*
169 1.2 matt * splhigh() must block "everything".
170 1.2 matt */
171 1.2 matt aic_imask[IPL_HIGH] |= aic_imask[IPL_CLOCK];
172 1.2 matt
173 1.2 matt /*
174 1.2 matt * Now compute which IRQs must be blocked when servicing any
175 1.2 matt * given IRQ.
176 1.2 matt */
177 1.2 matt for (irq = 0; irq < MIN(NIRQ, AIC_NIRQ); irq++) {
178 1.2 matt iq = &intrq[irq];
179 1.2 matt if (TAILQ_FIRST(&iq->iq_list) != NULL)
180 1.2 matt at91_enable_irq(irq);
181 1.2 matt }
182 1.2 matt /*
183 1.2 matt * update current mask
184 1.2 matt */
185 1.2 matt at91_set_intrmask(aic_imask[curcpl()]);
186 1.2 matt }
187 1.2 matt
188 1.2 matt inline void
189 1.2 matt splx(int new)
190 1.2 matt {
191 1.2 matt int old;
192 1.2 matt u_int oldirqstate;
193 1.2 matt
194 1.2 matt oldirqstate = disable_interrupts(I32_bit);
195 1.2 matt old = curcpl();
196 1.2 matt if (old != new) {
197 1.2 matt set_curcpl(new);
198 1.2 matt at91_set_intrmask(aic_imask[new]);
199 1.2 matt }
200 1.2 matt restore_interrupts(oldirqstate);
201 1.2 matt #ifdef __HAVE_FAST_SOFTINTS
202 1.2 matt cpu_dosoftints();
203 1.2 matt #endif
204 1.2 matt }
205 1.2 matt
206 1.2 matt int
207 1.2 matt _splraise(int ipl)
208 1.2 matt {
209 1.2 matt int old;
210 1.2 matt u_int oldirqstate;
211 1.2 matt
212 1.2 matt oldirqstate = disable_interrupts(I32_bit);
213 1.2 matt old = curcpl();
214 1.2 matt if (old != ipl) {
215 1.2 matt set_curcpl(ipl);
216 1.2 matt at91_set_intrmask(aic_imask[ipl]);
217 1.2 matt }
218 1.2 matt restore_interrupts(oldirqstate);
219 1.2 matt
220 1.2 matt return (old);
221 1.2 matt }
222 1.2 matt
223 1.2 matt int
224 1.2 matt _spllower(int ipl)
225 1.2 matt {
226 1.2 matt int old = curcpl();
227 1.2 matt
228 1.2 matt if (old <= ipl)
229 1.2 matt return (old);
230 1.2 matt splx(ipl);
231 1.2 matt #ifdef __HAVE_FAST_SOFTINTS
232 1.2 matt cpu_dosoftints();
233 1.2 matt #endif
234 1.2 matt return (old);
235 1.2 matt }
236 1.2 matt
237 1.2 matt /*
238 1.2 matt * at91aic_init:
239 1.2 matt *
240 1.2 matt * Initialize the rest of the interrupt subsystem, making it
241 1.2 matt * ready to handle interrupts from devices.
242 1.2 matt */
243 1.2 matt void
244 1.2 matt at91aic_init(void)
245 1.2 matt {
246 1.2 matt struct intrq *iq;
247 1.2 matt int i;
248 1.2 matt
249 1.2 matt aic_intr_enabled = 0;
250 1.2 matt
251 1.2 matt // disable intrrupts:
252 1.2 matt AICREG(AIC_IDCR) = -1;
253 1.2 matt
254 1.2 matt for (i = 0; i < NIRQ; i++) {
255 1.2 matt iq = &intrq[i];
256 1.2 matt TAILQ_INIT(&iq->iq_list);
257 1.2 matt
258 1.2 matt sprintf(iq->iq_name, "irq %d", i);
259 1.2 matt evcnt_attach_dynamic(&iq->iq_ev, EVCNT_TYPE_INTR,
260 1.2 matt NULL, "aic", iq->iq_name);
261 1.2 matt }
262 1.2 matt
263 1.2 matt /* All interrupts should use IRQ not FIQ */
264 1.2 matt
265 1.2 matt AICREG(AIC_IDCR) = -1; /* disable interrupts */
266 1.2 matt AICREG(AIC_ICCR) = -1; /* clear all interrupts */
267 1.2 matt AICREG(AIC_DCR) = 0; /* not in debug mode, just to make sure */
268 1.2 matt for (i = 0; i < NIRQ; i++) {
269 1.2 matt AICREG(AIC_SMR(i)) = 0; /* disable interrupt */
270 1.2 matt AICREG(AIC_SVR(i)) = (u_int32_t)&intrq[i]; // address of interrupt queue
271 1.2 matt }
272 1.2 matt AICREG(AIC_FVR) = 0; // fast interrupt...
273 1.2 matt AICREG(AIC_SPU) = 0; // spurious interrupt vector
274 1.2 matt
275 1.2 matt AICREG(AIC_EOICR) = 0; /* clear logic... */
276 1.2 matt AICREG(AIC_EOICR) = 0; /* clear logic... */
277 1.2 matt
278 1.2 matt at91aic_calculate_masks();
279 1.2 matt
280 1.2 matt /* Enable IRQs (don't yet use FIQs). */
281 1.2 matt enable_interrupts(I32_bit);
282 1.2 matt }
283 1.2 matt
284 1.2 matt void *
285 1.2 matt at91aic_intr_establish(int irq, int ipl, int type, int (*ih_func)(void *), void *arg)
286 1.2 matt {
287 1.2 matt struct intrq* iq;
288 1.2 matt struct intrhand* ih;
289 1.2 matt u_int oldirqstate;
290 1.2 matt unsigned ok;
291 1.2 matt uint32_t smr;
292 1.2 matt
293 1.2 matt if (irq < 0 || irq >= NIRQ)
294 1.2 matt panic("intr_establish: IRQ %d out of range", irq);
295 1.2 matt if (ipl < 0 || ipl >= NIPL)
296 1.2 matt panic("intr_establish: IPL %d out of range", ipl);
297 1.2 matt
298 1.2 matt smr = 1; // all interrupts have priority one.. ok?
299 1.2 matt switch (type) {
300 1.2 matt case _INTR_LOW_LEVEL:
301 1.2 matt smr |= AIC_SMR_SRCTYPE_LVL_LO;
302 1.2 matt break;
303 1.2 matt case INTR_HIGH_LEVEL:
304 1.2 matt smr |= AIC_SMR_SRCTYPE_LVL_HI;
305 1.2 matt break;
306 1.2 matt case INTR_FALLING_EDGE:
307 1.2 matt smr |= AIC_SMR_SRCTYPE_FALLING;
308 1.2 matt break;
309 1.2 matt case INTR_RISING_EDGE:
310 1.2 matt smr |= AIC_SMR_SRCTYPE_RISING;
311 1.2 matt break;
312 1.2 matt default:
313 1.2 matt panic("intr_establish: interrupt type %d is invalid", type);
314 1.2 matt }
315 1.2 matt
316 1.2 matt ih = malloc(sizeof(*ih), M_DEVBUF, M_NOWAIT);
317 1.2 matt if (ih == NULL)
318 1.2 matt return (NULL);
319 1.2 matt
320 1.2 matt ih->ih_func = ih_func;
321 1.2 matt ih->ih_arg = arg;
322 1.2 matt ih->ih_irq = irq;
323 1.2 matt ih->ih_ipl = ipl;
324 1.2 matt
325 1.2 matt iq = &intrq[irq];
326 1.2 matt
327 1.2 matt oldirqstate = disable_interrupts(I32_bit);
328 1.2 matt if (TAILQ_FIRST(&iq->iq_list) == NULL || (iq->iq_type & ~type) == 0) {
329 1.2 matt AICREG(AIC_SMR(irq)) = smr;
330 1.2 matt iq->iq_type = type;
331 1.2 matt TAILQ_INSERT_TAIL(&iq->iq_list, ih, ih_list);
332 1.2 matt at91aic_calculate_masks();
333 1.2 matt ok = 1;
334 1.2 matt } else
335 1.2 matt ok = 0;
336 1.2 matt restore_interrupts(oldirqstate);
337 1.2 matt
338 1.2 matt if (ok) {
339 1.2 matt #ifdef AT91AIC_DEBUG
340 1.2 matt int i;
341 1.2 matt printf("\n");
342 1.2 matt for (i = 0; i < NIPL; i++) {
343 1.2 matt printf("IPL%d: aic_imask=0x%08X\n", i, aic_imask[i]);
344 1.2 matt }
345 1.2 matt #endif
346 1.2 matt } else {
347 1.2 matt free(ih, M_DEVBUF);
348 1.2 matt ih = NULL;
349 1.2 matt }
350 1.2 matt
351 1.2 matt return (ih);
352 1.2 matt }
353 1.2 matt
354 1.2 matt void
355 1.2 matt at91aic_intr_disestablish(void *cookie)
356 1.2 matt {
357 1.2 matt struct intrhand* ih = cookie;
358 1.2 matt struct intrq* iq = &intrq[ih->ih_irq];
359 1.2 matt u_int oldirqstate;
360 1.2 matt
361 1.2 matt oldirqstate = disable_interrupts(I32_bit);
362 1.2 matt TAILQ_REMOVE(&iq->iq_list, ih, ih_list);
363 1.2 matt at91aic_calculate_masks();
364 1.2 matt restore_interrupts(oldirqstate);
365 1.2 matt }
366 1.2 matt
367 1.2 matt #include <arm/at91/at91reg.h>
368 1.2 matt #include <arm/at91/at91dbgureg.h>
369 1.2 matt #include <arm/at91/at91pdcreg.h>
370 1.2 matt
371 1.2 matt static inline void intr_process(struct intrq *iq, int pcpl, struct irqframe *frame);
372 1.2 matt
373 1.2 matt static inline void
374 1.2 matt intr_process(struct intrq *iq, int pcpl, struct irqframe *frame)
375 1.2 matt {
376 1.2 matt struct intrhand* ih;
377 1.2 matt u_int oldirqstate, intr;
378 1.2 matt
379 1.2 matt intr = iq - intrq;
380 1.2 matt
381 1.2 matt iq->iq_ev.ev_count++;
382 1.2 matt uvmexp.intrs++;
383 1.2 matt
384 1.2 matt if ((1U << intr) & aic_imask[pcpl]) {
385 1.2 matt panic("interrupt %d should be masked! (aic_imask=0x%X)", intr, aic_imask[pcpl]);
386 1.2 matt }
387 1.2 matt
388 1.2 matt if (iq->iq_busy) {
389 1.2 matt panic("interrupt %d busy!", intr);
390 1.2 matt }
391 1.2 matt
392 1.2 matt iq->iq_busy = 1;
393 1.2 matt
394 1.2 matt for (ih = TAILQ_FIRST(&iq->iq_list); ih != NULL;
395 1.2 matt ih = TAILQ_NEXT(ih, ih_list)) {
396 1.2 matt set_curcpl(ih->ih_ipl);
397 1.2 matt at91_set_intrmask(aic_imask[ih->ih_ipl]);
398 1.2 matt oldirqstate = enable_interrupts(I32_bit);
399 1.2 matt (void) (*ih->ih_func)(ih->ih_arg ? ih->ih_arg : frame);
400 1.2 matt restore_interrupts(oldirqstate);
401 1.2 matt }
402 1.2 matt
403 1.2 matt if (!iq->iq_busy) {
404 1.2 matt panic("interrupt %d not busy!", intr);
405 1.2 matt }
406 1.2 matt iq->iq_busy = 0;
407 1.2 matt
408 1.2 matt set_curcpl(pcpl);
409 1.2 matt at91_set_intrmask(aic_imask[pcpl]);
410 1.2 matt }
411 1.2 matt
412 1.2 matt void
413 1.2 matt at91aic_intr_dispatch(struct irqframe *frame)
414 1.2 matt {
415 1.2 matt struct intrq* iq;
416 1.2 matt int pcpl = curcpl();
417 1.2 matt
418 1.2 matt iq = (struct intrq *)AICREG(AIC_IVR); // get current queue
419 1.2 matt
420 1.2 matt // OK, service interrupt
421 1.2 matt if (iq)
422 1.2 matt intr_process(iq, pcpl, frame);
423 1.2 matt
424 1.2 matt AICREG(AIC_EOICR) = 0; // end of interrupt
425 1.2 matt }
426 1.2 matt
427 1.2 matt #if 0
428 1.2 matt void
429 1.2 matt at91aic_intr_poll(int irq)
430 1.2 matt {
431 1.2 matt u_int oldirqstate;
432 1.2 matt uint32_t ipr;
433 1.2 matt int pcpl = curcpl();
434 1.2 matt
435 1.2 matt oldirqstate = disable_interrupts(I32_bit);
436 1.2 matt ipr = AICREG(AIC_IPR);
437 1.2 matt if ((ipr & (1U << irq) & ~aic_imask[pcpl]))
438 1.2 matt intr_process(&intrq[irq], pcpl, NULL);
439 1.2 matt restore_interrupts(oldirqstate);
440 1.2 matt #ifdef __HAVE_FAST_SOFTINTS
441 1.2 matt cpu_dosoftints();
442 1.2 matt #endif
443 1.2 matt }
444 1.2 matt #endif
445 1.2 matt
446 1.2 matt void
447 1.2 matt at91aic_intr_poll(void *ihp, int flags)
448 1.2 matt {
449 1.2 matt struct intrhand* ih = ihp;
450 1.2 matt u_int oldirqstate, irq = ih->ih_irq;
451 1.2 matt uint32_t ipr;
452 1.2 matt int pcpl = curcpl();
453 1.2 matt
454 1.2 matt oldirqstate = disable_interrupts(I32_bit);
455 1.2 matt ipr = AICREG(AIC_IPR);
456 1.2 matt if ((ipr & (1U << irq))
457 1.2 matt && (flags || !(aic_imask[pcpl] & (1U << irq)))) {
458 1.2 matt set_curcpl(ih->ih_ipl);
459 1.2 matt at91_set_intrmask(aic_imask[ih->ih_ipl]);
460 1.2 matt (void)enable_interrupts(I32_bit);
461 1.2 matt (void)(*ih->ih_func)(ih->ih_arg ? ih->ih_arg : NULL);
462 1.2 matt (void)disable_interrupts(I32_bit);
463 1.2 matt set_curcpl(pcpl);
464 1.2 matt at91_set_intrmask(aic_imask[pcpl]);
465 1.2 matt }
466 1.2 matt restore_interrupts(oldirqstate);
467 1.2 matt
468 1.2 matt #ifdef __HAVE_FAST_SOFTINTS
469 1.2 matt cpu_dosoftints();
470 1.2 matt #endif
471 1.2 matt }
472