pxa2x0_intr.c revision 1.11.26.1 1 /* $NetBSD: pxa2x0_intr.c,v 1.11.26.1 2007/11/06 19:22:43 matt Exp $ */
2
3 /*
4 * Copyright (c) 2002 Genetec Corporation. All rights reserved.
5 * Written by Hiroyuki Bessho for Genetec Corporation.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. All advertising materials mentioning features or use of this software
16 * must display the following acknowledgement:
17 * This product includes software developed for the NetBSD Project by
18 * Genetec Corporation.
19 * 4. The name of Genetec Corporation may not be used to endorse or
20 * promote products derived from this software without specific prior
21 * written permission.
22 *
23 * THIS SOFTWARE IS PROVIDED BY GENETEC CORPORATION ``AS IS'' AND
24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
25 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
26 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GENETEC CORPORATION
27 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
28 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
29 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
30 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
31 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
32 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
33 * POSSIBILITY OF SUCH DAMAGE.
34 */
35
36 /*
37 * IRQ handler for the Intel PXA2X0 processor.
38 * It has integrated interrupt controller.
39 */
40
41 #include <sys/cdefs.h>
42 __KERNEL_RCSID(0, "$NetBSD: pxa2x0_intr.c,v 1.11.26.1 2007/11/06 19:22:43 matt Exp $");
43
44 #include <sys/param.h>
45 #include <sys/systm.h>
46 #include <sys/malloc.h>
47
48 #include <machine/bus.h>
49 #include <machine/intr.h>
50 #include <machine/lock.h>
51
52 #include <arm/xscale/pxa2x0cpu.h>
53 #include <arm/xscale/pxa2x0reg.h>
54 #include <arm/xscale/pxa2x0var.h>
55 #include <arm/xscale/pxa2x0_intr.h>
56 #include <arm/sa11x0/sa11x0_var.h>
57
58 /*
59 * INTC autoconf glue
60 */
61 static int pxaintc_match(struct device *, struct cfdata *, void *);
62 static void pxaintc_attach(struct device *, struct device *, void *);
63
64 CFATTACH_DECL(pxaintc, sizeof(struct device),
65 pxaintc_match, pxaintc_attach, NULL, NULL);
66
67 static int pxaintc_attached;
68
69 static int stray_interrupt(void *);
70 static void init_interrupt_masks(void);
71
72 /*
73 * interrupt dispatch table.
74 */
75 #ifdef MULTIPLE_HANDLERS_ON_ONE_IRQ
76 struct intrhand {
77 TAILQ_ENTRY(intrhand) ih_list; /* link on intrq list */
78 int (*ih_func)(void *); /* handler */
79 void *ih_arg; /* arg for handler */
80 };
81 #endif
82
83 static struct intrhandler {
84 #ifdef MULTIPLE_HANDLERS_ON_ONE_IRQ
85 TAILQ_HEAD(,intrhand) list;
86 #else
87 pxa2x0_irq_handler_t func;
88 #endif
89 void *cookie; /* NULL for stackframe */
90 /* struct evbnt ev; */
91 } handler[ICU_LEN];
92
93 volatile int softint_pending;
94 volatile int intr_mask;
95 /* interrupt masks for each level */
96 int pxa2x0_imask[NIPL];
97 static int extirq_level[ICU_LEN];
98
99
100 static int
101 pxaintc_match(struct device *parent, struct cfdata *cf, void *aux)
102 {
103 struct pxaip_attach_args *pxa = aux;
104
105 if (pxaintc_attached || pxa->pxa_addr != PXA2X0_INTCTL_BASE)
106 return (0);
107
108 return (1);
109 }
110
111 void
112 pxaintc_attach(struct device *parent, struct device *self, void *args)
113 {
114 int i;
115
116 pxaintc_attached = 1;
117
118 aprint_normal(": Interrupt Controller\n");
119
120 #define SAIPIC_ICCR 0x14
121
122 write_icu(SAIPIC_ICCR, 1);
123 write_icu(SAIPIC_MR, 0);
124
125 for(i = 0; i < sizeof handler / sizeof handler[0]; ++i){
126 handler[i].func = stray_interrupt;
127 handler[i].cookie = (void *)(intptr_t) i;
128 extirq_level[i] = IPL_SERIAL;
129 }
130
131 init_interrupt_masks();
132
133 _splraise(IPL_SERIAL);
134 enable_interrupts(I32_bit);
135 }
136
137 /*
138 * Invoked very early on from the board-specific initarm(), in order to
139 * inform us the virtual address of the interrupt controller's registers.
140 */
141 void
142 pxa2x0_intr_bootstrap(vaddr_t addr)
143 {
144
145 pxaic_base = addr;
146 }
147
148 static inline void
149 __raise(int ipl)
150 {
151
152 if (curcpu()->ci_cpl < ipl)
153 pxa2x0_setipl(ipl);
154 }
155
156
157 /*
158 * Map a software interrupt queue to an interrupt priority level.
159 */
160 static const int si_to_ipl[SI_NQUEUES] = {
161 IPL_SOFT, /* SI_SOFT */
162 IPL_SOFTCLOCK, /* SI_SOFTCLOCK */
163 IPL_SOFTNET, /* SI_SOFTNET */
164 IPL_SOFTSERIAL, /* SI_SOFTSERIAL */
165 };
166
167 /*
168 * called from irq_entry.
169 */
170 void
171 pxa2x0_irq_handler(void *arg)
172 {
173 struct clockframe *frame = arg;
174 uint32_t irqbits;
175 int irqno;
176 int saved_spl_level;
177
178 saved_spl_level = curcpu()->ci_cpl;
179
180 /* get pending IRQs */
181 irqbits = read_icu(SAIPIC_IP);
182
183 while ((irqno = find_first_bit(irqbits)) >= 0) {
184 /* XXX: Shuould we handle IRQs in priority order? */
185
186 /* raise spl to stop interrupts of lower priorities */
187 if (saved_spl_level < extirq_level[irqno])
188 pxa2x0_setipl(extirq_level[irqno]);
189
190 #ifdef notyet
191 /* Enable interrupt */
192 #endif
193 #ifndef MULTIPLE_HANDLERS_ON_ONE_IRQ
194 (* handler[irqno].func)(
195 handler[irqno].cookie == 0
196 ? frame : handler[irqno].cookie );
197 #else
198 /* process all handlers for this interrupt.
199 XXX not yet */
200 #endif
201
202 #ifdef notyet
203 /* Disable interrupt */
204 #endif
205
206 irqbits &= ~(1<<irqno);
207 }
208
209 /* restore spl to that was when this interrupt happen */
210 pxa2x0_setipl(saved_spl_level);
211
212 if(softint_pending & intr_mask)
213 pxa2x0_do_pending();
214 }
215
216 static int
217 stray_interrupt(void *cookie)
218 {
219 int irqno = (int)cookie;
220 printf("stray interrupt %d\n", irqno);
221
222 if (PXA270_IRQ_MIN <= irqno && irqno < ICU_LEN){
223 int save = disable_interrupts(I32_bit);
224 write_icu(SAIPIC_MR,
225 read_icu(SAIPIC_MR) & ~(1U<<irqno));
226 restore_interrupts(save);
227 }
228
229 return 0;
230 }
231
232
233
234 /*
235 * Interrupt Mask Handling
236 */
237
238 void
239 pxa2x0_update_intr_masks(int irqno, int level)
240 {
241 int mask = 1U<<irqno;
242 int psw = disable_interrupts(I32_bit);
243 int i;
244
245 for(i = 0; i < level; ++i)
246 pxa2x0_imask[i] |= mask; /* Enable interrupt at lower level */
247
248 for( ; i < NIPL-1; ++i)
249 pxa2x0_imask[i] &= ~mask; /* Disable itnerrupt at upper level */
250
251 /*
252 * Enforce a hierarchy that gives "slow" device (or devices with
253 * limited input buffer space/"real-time" requirements) a better
254 * chance at not dropping data.
255 */
256 pxa2x0_imask[IPL_BIO] &= pxa2x0_imask[IPL_SOFTNET];
257 pxa2x0_imask[IPL_NET] &= pxa2x0_imask[IPL_BIO];
258 pxa2x0_imask[IPL_SOFTSERIAL] &= pxa2x0_imask[IPL_NET];
259 pxa2x0_imask[IPL_TTY] &= pxa2x0_imask[IPL_SOFTSERIAL];
260
261 /*
262 * splvm() blocks all interrupts that use the kernel memory
263 * allocation facilities.
264 */
265 pxa2x0_imask[IPL_VM] &= pxa2x0_imask[IPL_TTY];
266
267 /*
268 * Audio devices are not allowed to perform memory allocation
269 * in their interrupt routines, and they have fairly "real-time"
270 * requirements, so give them a high interrupt priority.
271 */
272 pxa2x0_imask[IPL_AUDIO] &= pxa2x0_imask[IPL_VM];
273
274 /*
275 * splclock() must block anything that uses the scheduler.
276 */
277 pxa2x0_imask[IPL_CLOCK] &= pxa2x0_imask[IPL_AUDIO];
278
279 /*
280 * splhigh() must block "everything".
281 */
282 pxa2x0_imask[IPL_HIGH] &= pxa2x0_imask[IPL_STATCLOCK];
283
284 /*
285 * XXX We need serial drivers to run at the absolute highest priority
286 * in order to avoid overruns, so serial > high.
287 */
288 pxa2x0_imask[IPL_SERIAL] &= pxa2x0_imask[IPL_HIGH];
289
290 write_icu(SAIPIC_MR, pxa2x0_imask[curcpu()->ci_cpl]);
291
292 restore_interrupts(psw);
293 }
294
295
296 static void
297 init_interrupt_masks(void)
298 {
299
300 memset(pxa2x0_imask, 0, sizeof(pxa2x0_imask));
301
302 /*
303 * IPL_NONE has soft interrupts enabled only, at least until
304 * hardware handlers are installed.
305 */
306 pxa2x0_imask[IPL_NONE] =
307 SI_TO_IRQBIT(SI_SOFT) |
308 SI_TO_IRQBIT(SI_SOFTCLOCK) |
309 SI_TO_IRQBIT(SI_SOFTNET) |
310 SI_TO_IRQBIT(SI_SOFTSERIAL);
311
312 /*
313 * Initialize the soft interrupt masks to block themselves.
314 */
315 pxa2x0_imask[IPL_SOFT] = ~SI_TO_IRQBIT(SI_SOFT);
316 pxa2x0_imask[IPL_SOFTCLOCK] = ~SI_TO_IRQBIT(SI_SOFTCLOCK);
317 pxa2x0_imask[IPL_SOFTNET] = ~SI_TO_IRQBIT(SI_SOFTNET);
318 pxa2x0_imask[IPL_SOFTSERIAL] = ~SI_TO_IRQBIT(SI_SOFTSERIAL);
319
320 pxa2x0_imask[IPL_SOFT] &= pxa2x0_imask[IPL_NONE];
321
322 /*
323 * splsoftclock() is the only interface that users of the
324 * generic software interrupt facility have to block their
325 * soft intrs, so splsoftclock() must also block IPL_SOFT.
326 */
327 pxa2x0_imask[IPL_SOFTCLOCK] &= pxa2x0_imask[IPL_SOFT];
328
329 /*
330 * splsoftnet() must also block splsoftclock(), since we don't
331 * want timer-driven network events to occur while we're
332 * processing incoming packets.
333 */
334 pxa2x0_imask[IPL_SOFTNET] &= pxa2x0_imask[IPL_SOFTCLOCK];
335 }
336
337 void
338 pxa2x0_do_pending(void)
339 {
340 static __cpu_simple_lock_t processing = __SIMPLELOCK_UNLOCKED;
341 int oldirqstate, spl_save;
342
343 if (__cpu_simple_lock_try(&processing) == 0)
344 return;
345
346 spl_save = curcpu()->ci_cpl;
347
348 oldirqstate = disable_interrupts(I32_bit);
349
350 #if 1
351 #define DO_SOFTINT(si,ipl) \
352 if ((softint_pending & intr_mask) & SI_TO_IRQBIT(si)) { \
353 softint_pending &= ~SI_TO_IRQBIT(si); \
354 __raise(ipl); \
355 restore_interrupts(oldirqstate); \
356 softintr_dispatch(si); \
357 oldirqstate = disable_interrupts(I32_bit); \
358 pxa2x0_setipl(spl_save); \
359 }
360
361 do {
362 DO_SOFTINT(SI_SOFTSERIAL,IPL_SOFTSERIAL);
363 DO_SOFTINT(SI_SOFTNET, IPL_SOFTNET);
364 DO_SOFTINT(SI_SOFTCLOCK, IPL_SOFTCLOCK);
365 DO_SOFTINT(SI_SOFT, IPL_SOFT);
366 } while( softint_pending & intr_mask );
367 #else
368 while( (si = find_first_bit(softint_pending & intr_mask)) >= 0 ){
369 softint_pending &= ~SI_TO_IRQBIT(si);
370 __raise(si_to_ipl(si));
371 restore_interrupts(oldirqstate);
372 softintr_dispatch(si);
373 oldirqstate = disable_interrupts(I32_bit);
374 pxa2x0_setipl(spl_save);
375 }
376 #endif
377
378 __cpu_simple_unlock(&processing);
379
380 restore_interrupts(oldirqstate);
381 }
382
383
384 #undef splx
385 void
386 splx(int ipl)
387 {
388
389 pxa2x0_splx(ipl);
390 }
391
392 #undef _splraise
393 int
394 _splraise(int ipl)
395 {
396
397 return pxa2x0_splraise(ipl);
398 }
399
400 #undef _spllower
401 int
402 _spllower(int ipl)
403 {
404
405 return pxa2x0_spllower(ipl);
406 }
407
408 #undef _setsoftintr
409 void
410 _setsoftintr(int si)
411 {
412
413 return pxa2x0_setsoftintr(si);
414 }
415
416 void *
417 pxa2x0_intr_establish(int irqno, int level,
418 int (*func)(void *), void *cookie)
419 {
420 int psw;
421 int irqmin = CPU_IS_PXA250 ? PXA250_IRQ_MIN : PXA270_IRQ_MIN;
422
423 if (irqno < irqmin || irqno >= ICU_LEN)
424 panic("intr_establish: bogus irq number %d", irqno);
425
426 psw = disable_interrupts(I32_bit);
427
428 handler[irqno].cookie = cookie;
429 handler[irqno].func = func;
430 extirq_level[irqno] = level;
431 pxa2x0_update_intr_masks(irqno, level);
432
433 intr_mask = pxa2x0_imask[curcpu()->ci_cpl];
434
435 restore_interrupts(psw);
436
437 return (&handler[irqno]);
438 }
439
440 void
441 pxa2x0_intr_disestablish(void *cookie)
442 {
443 struct intrhandler *lhandler = cookie, *ih;
444 int irqmin = CPU_IS_PXA250 ? PXA250_IRQ_MIN : PXA270_IRQ_MIN;
445 int irqno = lhandler - handler;
446 int psw;
447
448 if (irqno < irqmin || irqno >= ICU_LEN)
449 panic("intr_disestablish: bogus irq number %d", irqno);
450
451 psw = disable_interrupts(I32_bit);
452
453 ih = &handler[irqno];
454 ih->func = stray_interrupt;
455 ih->cookie = (void *)(intptr_t)irqno;
456 extirq_level[irqno] = IPL_SERIAL;
457 pxa2x0_update_intr_masks(irqno, IPL_SERIAL);
458
459 restore_interrupts(psw);
460 }
461
462 /*
463 * Glue for drivers of sa11x0 compatible integrated logics.
464 */
465 void *
466 sa11x0_intr_establish(sa11x0_chipset_tag_t ic, int irq, int type, int level,
467 int (*ih_fun)(void *), void *ih_arg)
468 {
469
470 return pxa2x0_intr_establish(irq, level, ih_fun, ih_arg);
471 }
472