No empty .Rs/.Re
[netbsd-mini2440.git] / sys / arch / evbarm / ifpga / ifpga_intr.c
blob929edcfc339d8e65b7c183fb13ed705d6f9a834f
1 /* $NetBSD$ */
3 /*
4 * Copyright (c) 2001, 2002 Wasabi Systems, Inc.
5 * All rights reserved.
7 * Written by Jason R. Thorpe for Wasabi Systems, Inc.
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
11 * are met:
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
17 * 3. All advertising materials mentioning features or use of this software
18 * must display the following acknowledgement:
19 * This product includes software developed for the NetBSD Project by
20 * Wasabi Systems, Inc.
21 * 4. The name of Wasabi Systems, Inc. may not be used to endorse
22 * or promote products derived from this software without specific prior
23 * written permission.
25 * THIS SOFTWARE IS PROVIDED BY WASABI SYSTEMS, INC. ``AS IS'' AND
26 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
27 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
28 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL WASABI SYSTEMS, INC
29 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
30 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
31 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
32 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
33 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
34 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
35 * POSSIBILITY OF SUCH DAMAGE.
38 #ifndef EVBARM_SPL_NOINLINE
39 #define EVBARM_SPL_NOINLINE
40 #endif
43 * Interrupt support for the Integrator FPGA.
46 #include <sys/param.h>
47 #include <sys/systm.h>
48 #include <sys/malloc.h>
49 #include <sys/bus.h>
50 #include <sys/intr.h>
52 #include <uvm/uvm_extern.h>
54 #include <arm/cpufunc.h>
56 #include <evbarm/ifpga/ifpgareg.h>
57 #include <evbarm/ifpga/ifpgavar.h>
59 /* Interrupt handler queues. */
60 struct intrq intrq[NIRQ];
62 /* Interrupts to mask at each level. */
63 int ifpga_imask[NIPL];
65 /* Interrupts pending. */
66 volatile int ifpga_ipending;
68 /* Software copy of the IRQs we have enabled. */
69 volatile uint32_t intr_enabled;
71 /* Mask if interrupts steered to FIQs. */
72 uint32_t intr_steer;
75 * Interrupt bit names.
77 const char * const ifpga_irqnames[] = {
78 "soft", /* 0 */
79 "uart 0", /* 1 */
80 "uart 1", /* 2 */
81 "kbd", /* 3 */
82 "mouse", /* 4 */
83 "tmr 0", /* 5 */
84 "tmr 1 hard", /* 6 */
85 "tmr 2 stat", /* 7 */
86 "rtc", /* 8 */
87 "exp 0", /* 9 */
88 "exp 1", /* 10 */
89 "exp 2", /* 11 */
90 "exp 3", /* 12 */
91 "pci 0", /* 13 */
92 "pci 1", /* 14 */
93 "pci 2", /* 15 */
94 "pci 3", /* 16 */
95 "V3 br", /* 17 */
96 "deg", /* 18 */
97 "enum", /* 19 */
98 "pci lb", /* 20 */
99 "autoPC", /* 21 */
100 "irq 22", /* 22 */
101 "irq 23", /* 23 */
102 "irq 24", /* 24 */
103 "irq 25", /* 25 */
104 "irq 26", /* 26 */
105 "irq 27", /* 27 */
106 "irq 28", /* 28 */
107 "irq 29", /* 29 */
108 "irq 30", /* 30 */
109 "irq 31", /* 31 */
112 void ifpga_intr_dispatch(struct clockframe *frame);
114 extern struct ifpga_softc *ifpga_sc;
116 static inline uint32_t
117 ifpga_iintsrc_read(void)
119 return bus_space_read_4(ifpga_sc->sc_iot, ifpga_sc->sc_irq_ioh,
120 IFPGA_INTR_STATUS);
123 static inline void
124 ifpga_enable_irq(int irq)
127 intr_enabled |= (1U << irq);
128 ifpga_set_intrmask();
131 static inline void
132 ifpga_disable_irq(int irq)
135 intr_enabled &= ~(1U << irq);
136 ifpga_set_intrmask();
140 * NOTE: This routine must be called with interrupts disabled in the CPSR.
142 static void
143 ifpga_intr_calculate_masks(void)
145 struct intrq *iq;
146 struct intrhand *ih;
147 int irq, ipl;
149 /* First, figure out which IPLs each IRQ has. */
150 for (irq = 0; irq < NIRQ; irq++) {
151 int levels = 0;
152 iq = &intrq[irq];
153 ifpga_disable_irq(irq);
154 for (ih = TAILQ_FIRST(&iq->iq_list); ih != NULL;
155 ih = TAILQ_NEXT(ih, ih_list))
156 levels |= (1U << ih->ih_ipl);
157 iq->iq_levels = levels;
160 /* Next, figure out which IRQs are used by each IPL. */
161 for (ipl = 0; ipl < NIPL; ipl++) {
162 int irqs = 0;
163 for (irq = 0; irq < NIRQ; irq++) {
164 if (intrq[irq].iq_levels & (1U << ipl))
165 irqs |= (1U << irq);
167 ifpga_imask[ipl] = irqs;
170 KASSERT(ifpga_imask[IPL_NONE] == 0);
173 * Enforce a hierarchy that gives "slow" device (or devices with
174 * limited input buffer space/"real-time" requirements) a better
175 * chance at not dropping data.
177 ifpga_imask[IPL_VM] |= 0;
178 ifpga_imask[IPL_SCHED] |= ifpga_imask[IPL_VM];
179 ifpga_imask[IPL_HIGH] |= ifpga_imask[IPL_SCHED];
182 * Now compute which IRQs must be blocked when servicing any
183 * given IRQ.
185 for (irq = 0; irq < NIRQ; irq++) {
186 int irqs = (1U << irq);
187 iq = &intrq[irq];
188 if (TAILQ_FIRST(&iq->iq_list) != NULL)
189 ifpga_enable_irq(irq);
190 for (ih = TAILQ_FIRST(&iq->iq_list); ih != NULL;
191 ih = TAILQ_NEXT(ih, ih_list))
192 irqs |= ifpga_imask[ih->ih_ipl];
193 iq->iq_mask = irqs;
197 void
198 splx(int new)
201 ifpga_splx(new);
205 _spllower(int ipl)
208 return (ifpga_spllower(ipl));
212 _splraise(int ipl)
215 return (ifpga_splraise(ipl));
219 * ifpga_intr_init:
221 * Initialize the rest of the interrupt subsystem, making it
222 * ready to handle interrupts from devices.
224 void
225 ifpga_intr_init(void)
227 struct intrq *iq;
228 int i;
230 intr_enabled = 0;
232 for (i = 0; i < NIRQ; i++) {
233 iq = &intrq[i];
234 TAILQ_INIT(&iq->iq_list);
236 evcnt_attach_dynamic(&iq->iq_ev, EVCNT_TYPE_INTR,
237 NULL, "ifpga", ifpga_irqnames[i]);
241 void
242 ifpga_intr_postinit(void)
244 ifpga_intr_calculate_masks();
246 /* Enable IRQs (don't yet use FIQs). */
247 enable_interrupts(I32_bit);
250 void *
251 ifpga_intr_establish(int irq, int ipl, int (*func)(void *), void *arg)
253 struct intrq *iq;
254 struct intrhand *ih;
255 u_int oldirqstate;
257 if (irq < 0 || irq > NIRQ)
258 panic("ifpga_intr_establish: IRQ %d out of range", irq);
260 ih = malloc(sizeof(*ih), M_DEVBUF, M_NOWAIT);
261 if (ih == NULL)
262 return (NULL);
264 ih->ih_func = func;
265 ih->ih_arg = arg;
266 ih->ih_ipl = ipl;
267 ih->ih_irq = irq;
269 iq = &intrq[irq];
271 /* All IOP321 interrupts are level-triggered. */
272 iq->iq_ist = IST_LEVEL;
274 oldirqstate = disable_interrupts(I32_bit);
276 TAILQ_INSERT_TAIL(&iq->iq_list, ih, ih_list);
278 ifpga_intr_calculate_masks();
280 restore_interrupts(oldirqstate);
282 return (ih);
285 void
286 ifpga_intr_disestablish(void *cookie)
288 struct intrhand *ih = cookie;
289 struct intrq *iq = &intrq[ih->ih_irq];
290 int oldirqstate;
292 oldirqstate = disable_interrupts(I32_bit);
294 TAILQ_REMOVE(&iq->iq_list, ih, ih_list);
296 ifpga_intr_calculate_masks();
298 restore_interrupts(oldirqstate);
301 void
302 ifpga_intr_dispatch(struct clockframe *frame)
304 struct intrq *iq;
305 struct intrhand *ih;
306 int oldirqstate, pcpl, irq, ibit, hwpend;
307 struct cpu_info * const ci = curcpu();
309 pcpl = ci->ci_cpl;
311 hwpend = ifpga_iintsrc_read();
314 * Disable all the interrupts that are pending. We will
315 * reenable them once they are processed and not masked.
317 intr_enabled &= ~hwpend;
318 ifpga_set_intrmask();
320 /* Wait for these interrupts to be suppressed. */
321 while ((ifpga_iintsrc_read() & hwpend) != 0)
324 while (hwpend != 0) {
325 irq = ffs(hwpend) - 1;
326 ibit = (1U << irq);
328 hwpend &= ~ibit;
330 if (pcpl & ibit) {
332 * IRQ is masked; mark it as pending and check
333 * the next one. Note: the IRQ is already disabled.
335 ifpga_ipending |= ibit;
336 continue;
339 ifpga_ipending &= ~ibit;
341 iq = &intrq[irq];
342 iq->iq_ev.ev_count++;
343 uvmexp.intrs++;
344 ci->ci_cpl |= iq->iq_mask;
345 oldirqstate = enable_interrupts(I32_bit);
346 for (ih = TAILQ_FIRST(&iq->iq_list); ih != NULL;
347 ih = TAILQ_NEXT(ih, ih_list)) {
348 (void) (*ih->ih_func)(ih->ih_arg ? ih->ih_arg : frame);
350 restore_interrupts(oldirqstate);
351 ci->ci_cpl = pcpl;
353 hwpend |= (ifpga_ipending & IFPGA_INTR_HWMASK) & ~pcpl;
355 /* Re-enable this interrupt now that's it's cleared. */
356 intr_enabled |= ibit;
357 ifpga_set_intrmask();
360 #ifdef __HAVE_FAST_SOFTINTS
361 cpu_dosoftints();
362 #endif