gdium_intr.c revision 1.3 1 1.3 bouyer /* $NetBSD: gdium_intr.c,v 1.3 2011/06/08 17:47:48 bouyer Exp $ */
2 1.1 matt
3 1.1 matt /*-
4 1.1 matt * Copyright (c) 2001 The NetBSD Foundation, Inc.
5 1.1 matt * All rights reserved.
6 1.1 matt *
7 1.1 matt * This code is derived from software contributed to The NetBSD Foundation
8 1.1 matt * by Jason R. Thorpe.
9 1.1 matt *
10 1.1 matt * Redistribution and use in source and binary forms, with or without
11 1.1 matt * modification, are permitted provided that the following conditions
12 1.1 matt * are met:
13 1.1 matt * 1. Redistributions of source code must retain the above copyright
14 1.1 matt * notice, this list of conditions and the following disclaimer.
15 1.1 matt * 2. Redistributions in binary form must reproduce the above copyright
16 1.1 matt * notice, this list of conditions and the following disclaimer in the
17 1.1 matt * documentation and/or other materials provided with the distribution.
18 1.1 matt *
19 1.1 matt * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 1.1 matt * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 1.1 matt * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 1.1 matt * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 1.1 matt * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 1.1 matt * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 1.1 matt * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 1.1 matt * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 1.1 matt * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 1.1 matt * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 1.1 matt * POSSIBILITY OF SUCH DAMAGE.
30 1.1 matt */
31 1.1 matt
32 1.1 matt /*
33 1.1 matt * Platform-specific interrupt support for the Algorithmics P-6032.
34 1.1 matt *
35 1.1 matt * The Algorithmics P-6032's interrupts are wired to GPIO pins
36 1.1 matt * on the BONITO system controller.
37 1.1 matt */
38 1.1 matt
39 1.1 matt #include <sys/cdefs.h>
40 1.3 bouyer __KERNEL_RCSID(0, "$NetBSD: gdium_intr.c,v 1.3 2011/06/08 17:47:48 bouyer Exp $");
41 1.3 bouyer
42 1.3 bouyer #define __INTR_PRIVATE
43 1.3 bouyer
44 1.1 matt
45 1.1 matt #include "opt_ddb.h"
46 1.1 matt
47 1.1 matt #include <sys/param.h>
48 1.1 matt #include <sys/queue.h>
49 1.1 matt #include <sys/malloc.h>
50 1.1 matt #include <sys/systm.h>
51 1.1 matt #include <sys/device.h>
52 1.1 matt #include <sys/kernel.h>
53 1.1 matt #include <sys/cpu.h>
54 1.1 matt
55 1.1 matt #include <machine/bus.h>
56 1.1 matt #include <machine/intr.h>
57 1.1 matt
58 1.1 matt #include <mips/locore.h>
59 1.1 matt
60 1.1 matt #include <mips/bonito/bonitoreg.h>
61 1.1 matt #include <evbmips/gdium/gdiumvar.h>
62 1.1 matt
63 1.1 matt #include <dev/pci/pcireg.h>
64 1.1 matt #include <dev/pci/pcivar.h>
65 1.1 matt
66 1.1 matt /*
67 1.1 matt * The GDIUM interrupts are wired up in the following way:
68 1.1 matt *
69 1.1 matt * GPIN0 ISA_NMI (in)
70 1.1 matt * GPIN1 ISA_INTR (in)
71 1.1 matt * GPIN2 ETH_INT~ (in)
72 1.1 matt * GPIN3 BONIDE_INT (in)
73 1.1 matt *
74 1.1 matt * PCI_INTA
75 1.1 matt * GPIN4 ISA IRQ3 (in, also on piix4)
76 1.1 matt * GPIN5 ISA IRQ4 (in, also on piix4)
77 1.1 matt *
78 1.1 matt * GPIO0 PIRQ A~ (in)
79 1.1 matt * GPIO1 PIRQ B~ (in)
80 1.1 matt * GPIO2 PIRQ C~ (in)
81 1.1 matt * GPIO3 PIRQ D~ (in)
82 1.1 matt */
83 1.1 matt
84 1.1 matt struct gdium_irqmap {
85 1.1 matt const char *name;
86 1.1 matt uint8_t irqidx;
87 1.1 matt uint8_t flags;
88 1.1 matt };
89 1.1 matt
90 1.1 matt #define IRQ_F_INVERT 0x80 /* invert polarity */
91 1.1 matt #define IRQ_F_EDGE 0x40 /* edge trigger */
92 1.1 matt #define IRQ_F_INT0 0x00 /* INT0 */
93 1.1 matt #define IRQ_F_INT1 0x01 /* INT1 */
94 1.1 matt #define IRQ_F_INT2 0x02 /* INT2 */
95 1.1 matt #define IRQ_F_INT3 0x03 /* INT3 */
96 1.1 matt #define IRQ_F_INTMASK 0x07 /* INT mask */
97 1.1 matt
98 1.1 matt const struct gdium_irqmap gdium_irqmap[] = {
99 1.1 matt { "gpio0", GDIUM_IRQ_GPIO0, IRQ_F_INT0 },
100 1.1 matt { "gpio1", GDIUM_IRQ_GPIO1, IRQ_F_INT0 },
101 1.1 matt { "gpio2", GDIUM_IRQ_GPIO2, IRQ_F_INT0 },
102 1.1 matt { "gpio3", GDIUM_IRQ_GPIO3, IRQ_F_INT0 },
103 1.1 matt
104 1.1 matt { "pci inta", GDIUM_IRQ_PCI_INTA, IRQ_F_INT0 },
105 1.1 matt { "pci intb", GDIUM_IRQ_PCI_INTB, IRQ_F_INT0 },
106 1.1 matt { "pci intc", GDIUM_IRQ_PCI_INTC, IRQ_F_INT0 },
107 1.1 matt { "pci intd", GDIUM_IRQ_PCI_INTD, IRQ_F_INT0 },
108 1.1 matt
109 1.1 matt { "pci perr", GDIUM_IRQ_PCI_PERR, IRQ_F_EDGE|IRQ_F_INT1 },
110 1.1 matt { "pci serr", GDIUM_IRQ_PCI_SERR, IRQ_F_EDGE|IRQ_F_INT1 },
111 1.1 matt
112 1.1 matt { "denali", GDIUM_IRQ_DENALI, IRQ_F_INT1 },
113 1.1 matt
114 1.2 matt { "mips int0", GDIUM_IRQ_INT0, IRQ_F_INT0 },
115 1.2 matt { "mips int1", GDIUM_IRQ_INT1, IRQ_F_INT1 },
116 1.2 matt { "mips int2", GDIUM_IRQ_INT2, IRQ_F_INT2 },
117 1.2 matt { "mips int3", GDIUM_IRQ_INT3, IRQ_F_INT3 },
118 1.1 matt };
119 1.1 matt
120 1.1 matt struct gdium_intrhead {
121 1.1 matt struct evcnt intr_count;
122 1.1 matt int intr_refcnt;
123 1.1 matt };
124 1.1 matt struct gdium_intrhead gdium_intrtab[__arraycount(gdium_irqmap)];
125 1.1 matt
126 1.1 matt #define NINTRS 2 /* MIPS INT0 - INT1 */
127 1.1 matt
128 1.1 matt struct gdium_cpuintr {
129 1.1 matt LIST_HEAD(, evbmips_intrhand) cintr_list;
130 1.1 matt struct evcnt cintr_count;
131 1.2 matt int cintr_refcnt;
132 1.1 matt };
133 1.1 matt
134 1.1 matt struct gdium_cpuintr gdium_cpuintrs[NINTRS];
135 1.3 bouyer const char * const gdium_cpuintrnames[NINTRS] = {
136 1.1 matt "int 0 (pci)",
137 1.2 matt "int 1 (errors)",
138 1.1 matt };
139 1.1 matt
140 1.1 matt /*
141 1.1 matt * This is a mask of bits to clear in the SR when we go to a
142 1.1 matt * given hardware interrupt priority level.
143 1.1 matt */
144 1.3 bouyer static const struct ipl_sr_map gdium_ipl_sr_map = {
145 1.3 bouyer .sr_bits = {
146 1.3 bouyer [IPL_NONE] = 0,
147 1.3 bouyer [IPL_SOFTCLOCK] = MIPS_SOFT_INT_MASK_0,
148 1.3 bouyer [IPL_SOFTNET] = MIPS_SOFT_INT_MASK_0 | MIPS_SOFT_INT_MASK_1,
149 1.1 matt [IPL_VM] =
150 1.1 matt MIPS_SOFT_INT_MASK_0 | MIPS_SOFT_INT_MASK_1 |
151 1.2 matt MIPS_INT_MASK_0 |
152 1.2 matt MIPS_INT_MASK_1 |
153 1.2 matt MIPS_INT_MASK_2 |
154 1.2 matt MIPS_INT_MASK_3 |
155 1.2 matt MIPS_INT_MASK_4,
156 1.1 matt [IPL_SCHED] =
157 1.1 matt MIPS_SOFT_INT_MASK_0 | MIPS_SOFT_INT_MASK_1 |
158 1.1 matt MIPS_INT_MASK_0 |
159 1.1 matt MIPS_INT_MASK_1 |
160 1.1 matt MIPS_INT_MASK_2 |
161 1.1 matt MIPS_INT_MASK_3 |
162 1.1 matt MIPS_INT_MASK_4 |
163 1.1 matt MIPS_INT_MASK_5,
164 1.3 bouyer [IPL_DDB] = MIPS_INT_MASK,
165 1.3 bouyer [IPL_HIGH] = MIPS_INT_MASK,
166 1.3 bouyer },
167 1.1 matt };
168 1.1 matt
169 1.3 bouyer int gdium_pci_intr_map(const struct pci_attach_args *, pci_intr_handle_t *);
170 1.1 matt const char *gdium_pci_intr_string(void *, pci_intr_handle_t);
171 1.1 matt const struct evcnt *gdium_pci_intr_evcnt(void *, pci_intr_handle_t);
172 1.1 matt void *gdium_pci_intr_establish(void *, pci_intr_handle_t, int,
173 1.1 matt int (*)(void *), void *);
174 1.1 matt void gdium_pci_intr_disestablish(void *, void *);
175 1.1 matt void gdium_pci_conf_interrupt(void *, int, int, int, int, int *);
176 1.1 matt
177 1.1 matt void
178 1.1 matt evbmips_intr_init(void)
179 1.1 matt {
180 1.3 bouyer struct gdium_config * const gc = &gdium_configuration;
181 1.1 matt struct bonito_config *bc = &gc->gc_bonito;
182 1.1 matt const struct gdium_irqmap *irqmap;
183 1.1 matt uint32_t intbit;
184 1.3 bouyer size_t i;
185 1.3 bouyer
186 1.3 bouyer ipl_sr_map = gdium_ipl_sr_map;
187 1.1 matt
188 1.1 matt for (i = 0; i < NINTRS; i++) {
189 1.1 matt LIST_INIT(&gdium_cpuintrs[i].cintr_list);
190 1.1 matt evcnt_attach_dynamic(&gdium_cpuintrs[i].cintr_count,
191 1.1 matt EVCNT_TYPE_INTR, NULL, "mips", gdium_cpuintrnames[i]);
192 1.1 matt }
193 1.1 matt //evcnt_attach_static(&mips_int5_evcnt);
194 1.1 matt
195 1.1 matt for (i = 0; i < __arraycount(gdium_irqmap); i++) {
196 1.1 matt irqmap = &gdium_irqmap[i];
197 1.1 matt intbit = 1 << irqmap->irqidx;
198 1.1 matt
199 1.1 matt evcnt_attach_dynamic(&gdium_intrtab[i].intr_count,
200 1.1 matt EVCNT_TYPE_INTR, NULL, "bonito", irqmap->name);
201 1.1 matt
202 1.1 matt if (irqmap->irqidx < 4)
203 1.1 matt bc->bc_gpioIE |= intbit;
204 1.1 matt if (irqmap->flags & IRQ_F_INVERT)
205 1.1 matt bc->bc_intPol |= intbit;
206 1.1 matt if (irqmap->flags & IRQ_F_EDGE)
207 1.1 matt bc->bc_intEdge |= intbit;
208 1.1 matt if ((irqmap->flags & IRQ_F_INTMASK) == IRQ_F_INT1)
209 1.1 matt bc->bc_intSteer |= intbit;
210 1.1 matt
211 1.1 matt REGVAL(BONITO_INTENCLR) = intbit;
212 1.1 matt }
213 1.1 matt
214 1.1 matt REGVAL(BONITO_GPIOIE) = bc->bc_gpioIE;
215 1.1 matt REGVAL(BONITO_INTEDGE) = bc->bc_intEdge;
216 1.1 matt REGVAL(BONITO_INTSTEER) = bc->bc_intSteer;
217 1.1 matt REGVAL(BONITO_INTPOL) = bc->bc_intPol;
218 1.1 matt
219 1.1 matt gc->gc_pc.pc_intr_v = NULL;
220 1.1 matt gc->gc_pc.pc_intr_map = gdium_pci_intr_map;
221 1.1 matt gc->gc_pc.pc_intr_string = gdium_pci_intr_string;
222 1.1 matt gc->gc_pc.pc_intr_evcnt = gdium_pci_intr_evcnt;
223 1.1 matt gc->gc_pc.pc_intr_establish = gdium_pci_intr_establish;
224 1.1 matt gc->gc_pc.pc_intr_disestablish = gdium_pci_intr_disestablish;
225 1.1 matt gc->gc_pc.pc_conf_interrupt = gdium_pci_conf_interrupt;
226 1.1 matt
227 1.1 matt /* We let the PCI-ISA bridge code handle this. */
228 1.1 matt gc->gc_pc.pc_pciide_compat_intr_establish = NULL;
229 1.1 matt }
230 1.1 matt
231 1.1 matt void *
232 1.2 matt evbmips_intr_establish(int irq, int (*func)(void *), void *arg)
233 1.1 matt {
234 1.1 matt const struct gdium_irqmap *irqmap;
235 1.1 matt struct evbmips_intrhand *ih;
236 1.2 matt int level;
237 1.1 matt int s;
238 1.1 matt
239 1.1 matt irqmap = &gdium_irqmap[irq];
240 1.1 matt KASSERT(irq < __arraycount(gdium_irqmap));
241 1.1 matt
242 1.1 matt KASSERT(irq == irqmap->irqidx);
243 1.1 matt
244 1.1 matt ih = malloc(sizeof(*ih), M_DEVBUF, M_NOWAIT|M_ZERO);
245 1.1 matt if (ih == NULL)
246 1.2 matt return NULL;
247 1.1 matt
248 1.1 matt ih->ih_func = func;
249 1.1 matt ih->ih_arg = arg;
250 1.1 matt ih->ih_irq = irq;
251 1.1 matt
252 1.1 matt s = splhigh();
253 1.1 matt
254 1.1 matt /*
255 1.1 matt * First, link it into the tables.
256 1.1 matt */
257 1.2 matt level = (irqmap->flags & IRQ_F_INT1) != 0;
258 1.2 matt LIST_INSERT_HEAD(&gdium_cpuintrs[level].cintr_list, ih, ih_q);
259 1.2 matt gdium_cpuintrs[level].cintr_refcnt++;
260 1.1 matt
261 1.1 matt /*
262 1.1 matt * Now enable it.
263 1.1 matt */
264 1.2 matt if (gdium_intrtab[ih->ih_irq].intr_refcnt++ == 0)
265 1.2 matt REGVAL(BONITO_INTENSET) = (1 << ih->ih_irq);
266 1.1 matt
267 1.1 matt splx(s);
268 1.1 matt
269 1.1 matt return (ih);
270 1.1 matt }
271 1.1 matt
272 1.1 matt void
273 1.2 matt evbmips_intr_disestablish(void *cookie)
274 1.1 matt {
275 1.1 matt const struct gdium_irqmap *irqmap;
276 1.1 matt struct evbmips_intrhand *ih = cookie;
277 1.1 matt int s;
278 1.1 matt
279 1.1 matt irqmap = &gdium_irqmap[ih->ih_irq];
280 1.1 matt
281 1.1 matt s = splhigh();
282 1.1 matt
283 1.1 matt /*
284 1.1 matt * First, remove it from the table.
285 1.1 matt */
286 1.1 matt LIST_REMOVE(ih, ih_q);
287 1.2 matt gdium_cpuintrs[(irqmap->flags & IRQ_F_INT1) != 0].cintr_refcnt--;
288 1.1 matt
289 1.1 matt /*
290 1.1 matt * Now, disable it, if there is nothing remaining on the
291 1.1 matt * list.
292 1.1 matt */
293 1.2 matt if (gdium_intrtab[ih->ih_irq].intr_refcnt-- == 1)
294 1.2 matt REGVAL(BONITO_INTENCLR) = (1 << ih->ih_irq);
295 1.1 matt
296 1.1 matt splx(s);
297 1.1 matt
298 1.1 matt free(ih, M_DEVBUF);
299 1.1 matt }
300 1.1 matt
301 1.1 matt void
302 1.3 bouyer evbmips_iointr(int ipl, vaddr_t pc, uint32_t ipending)
303 1.1 matt {
304 1.1 matt const struct gdium_irqmap *irqmap;
305 1.1 matt struct evbmips_intrhand *ih;
306 1.1 matt int level;
307 1.1 matt uint32_t isr;
308 1.1 matt
309 1.1 matt /*
310 1.1 matt * Read the interrupt pending registers, mask them with the
311 1.1 matt * ones we have enabled, and service them in order of decreasing
312 1.1 matt * priority.
313 1.1 matt */
314 1.1 matt isr = REGVAL(BONITO_INTISR) & REGVAL(BONITO_INTEN);
315 1.1 matt for (level = 1; level >= 0; level--) {
316 1.2 matt if ((ipending & (MIPS_INT_MASK_4 << level)) == 0)
317 1.1 matt continue;
318 1.1 matt gdium_cpuintrs[level].cintr_count.ev_count++;
319 1.2 matt LIST_FOREACH (ih, &gdium_cpuintrs[level].cintr_list, ih_q) {
320 1.1 matt irqmap = &gdium_irqmap[ih->ih_irq];
321 1.1 matt if (isr & (1 << ih->ih_irq)) {
322 1.1 matt gdium_intrtab[ih->ih_irq].intr_count.ev_count++;
323 1.1 matt (*ih->ih_func)(ih->ih_arg);
324 1.1 matt }
325 1.1 matt }
326 1.1 matt }
327 1.1 matt }
328 1.1 matt
329 1.1 matt /*****************************************************************************
330 1.1 matt * PCI interrupt support
331 1.1 matt *****************************************************************************/
332 1.1 matt
333 1.1 matt int
334 1.3 bouyer gdium_pci_intr_map(const struct pci_attach_args *pa,
335 1.1 matt pci_intr_handle_t *ihp)
336 1.1 matt {
337 1.1 matt static const int8_t pciirqmap[5/*device*/] = {
338 1.1 matt GDIUM_IRQ_PCI_INTC, /* 13: PCI 802.11 */
339 1.1 matt GDIUM_IRQ_PCI_INTA, /* 14: SM501 */
340 1.1 matt GDIUM_IRQ_PCI_INTB, /* 15: NEC USB (2 func) */
341 1.1 matt GDIUM_IRQ_PCI_INTD, /* 16: Ethernet */
342 1.1 matt GDIUM_IRQ_PCI_INTC, /* 17: NEC USB (2 func) */
343 1.1 matt };
344 1.1 matt pcitag_t bustag = pa->pa_intrtag;
345 1.1 matt int buspin = pa->pa_intrpin;
346 1.1 matt pci_chipset_tag_t pc = pa->pa_pc;
347 1.1 matt int device;
348 1.1 matt
349 1.1 matt if (buspin == 0) {
350 1.1 matt /* No IRQ used. */
351 1.1 matt return (1);
352 1.1 matt }
353 1.1 matt
354 1.1 matt if (buspin > 4) {
355 1.1 matt printf("gdium_pci_intr_map: bad interrupt pin %d\n",
356 1.1 matt buspin);
357 1.1 matt return (1);
358 1.1 matt }
359 1.1 matt
360 1.1 matt pci_decompose_tag(pc, bustag, NULL, &device, NULL);
361 1.1 matt if (device < 13 || device > 17) {
362 1.1 matt printf("gdium_pci_intr_map: bad device %d\n",
363 1.1 matt device);
364 1.1 matt return (1);
365 1.1 matt }
366 1.1 matt
367 1.1 matt *ihp = pciirqmap[device - 13];
368 1.1 matt return (0);
369 1.1 matt }
370 1.1 matt
371 1.1 matt const char *
372 1.1 matt gdium_pci_intr_string(void *v, pci_intr_handle_t ih)
373 1.1 matt {
374 1.1 matt
375 1.1 matt if (ih >= __arraycount(gdium_irqmap))
376 1.1 matt panic("gdium_intr_string: bogus IRQ %ld", ih);
377 1.1 matt
378 1.1 matt return gdium_irqmap[ih].name;
379 1.1 matt }
380 1.1 matt
381 1.1 matt const struct evcnt *
382 1.1 matt gdium_pci_intr_evcnt(void *v, pci_intr_handle_t ih)
383 1.1 matt {
384 1.1 matt
385 1.1 matt return &gdium_intrtab[ih].intr_count;
386 1.1 matt }
387 1.1 matt
388 1.1 matt void *
389 1.1 matt gdium_pci_intr_establish(void *v, pci_intr_handle_t ih, int level,
390 1.1 matt int (*func)(void *), void *arg)
391 1.1 matt {
392 1.1 matt
393 1.1 matt if (ih >= __arraycount(gdium_irqmap))
394 1.2 matt panic("gdium_pci_intr_establish: bogus IRQ %ld", ih);
395 1.1 matt
396 1.2 matt return evbmips_intr_establish(ih, func, arg);
397 1.1 matt }
398 1.1 matt
399 1.1 matt void
400 1.1 matt gdium_pci_intr_disestablish(void *v, void *cookie)
401 1.1 matt {
402 1.1 matt
403 1.2 matt return (evbmips_intr_disestablish(cookie));
404 1.1 matt }
405 1.1 matt
406 1.1 matt void
407 1.1 matt gdium_pci_conf_interrupt(void *v, int bus, int dev, int pin, int swiz,
408 1.1 matt int *iline)
409 1.1 matt {
410 1.1 matt
411 1.1 matt /*
412 1.1 matt * We actually don't need to do anything; everything is handled
413 1.1 matt * in pci_intr_map().
414 1.1 matt */
415 1.1 matt *iline = 0;
416 1.1 matt }
417