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