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