1 1.10 thorpej /* $NetBSD: gdium_intr.c,v 1.10 2020/11/21 15:36:36 thorpej 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.10 thorpej __KERNEL_RCSID(0, "$NetBSD: gdium_intr.c,v 1.10 2020/11/21 15:36:36 thorpej 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.5 matt #include <sys/bus.h> 49 1.5 matt #include <sys/cpu.h> 50 1.5 matt #include <sys/device.h> 51 1.5 matt #include <sys/intr.h> 52 1.5 matt #include <sys/kernel.h> 53 1.10 thorpej #include <sys/kmem.h> 54 1.1 matt #include <sys/systm.h> 55 1.1 matt 56 1.1 matt #include <mips/locore.h> 57 1.1 matt 58 1.1 matt #include <mips/bonito/bonitoreg.h> 59 1.1 matt #include <evbmips/gdium/gdiumvar.h> 60 1.1 matt 61 1.1 matt #include <dev/pci/pcireg.h> 62 1.1 matt #include <dev/pci/pcivar.h> 63 1.1 matt 64 1.1 matt /* 65 1.1 matt * The GDIUM interrupts are wired up in the following way: 66 1.1 matt * 67 1.1 matt * GPIN0 ISA_NMI (in) 68 1.1 matt * GPIN1 ISA_INTR (in) 69 1.1 matt * GPIN2 ETH_INT~ (in) 70 1.1 matt * GPIN3 BONIDE_INT (in) 71 1.1 matt * 72 1.1 matt * PCI_INTA 73 1.1 matt * GPIN4 ISA IRQ3 (in, also on piix4) 74 1.1 matt * GPIN5 ISA IRQ4 (in, also on piix4) 75 1.1 matt * 76 1.1 matt * GPIO0 PIRQ A~ (in) 77 1.1 matt * GPIO1 PIRQ B~ (in) 78 1.1 matt * GPIO2 PIRQ C~ (in) 79 1.1 matt * GPIO3 PIRQ D~ (in) 80 1.1 matt */ 81 1.1 matt 82 1.1 matt struct gdium_irqmap { 83 1.1 matt const char *name; 84 1.1 matt uint8_t irqidx; 85 1.1 matt uint8_t flags; 86 1.1 matt }; 87 1.1 matt 88 1.1 matt #define IRQ_F_INVERT 0x80 /* invert polarity */ 89 1.1 matt #define IRQ_F_EDGE 0x40 /* edge trigger */ 90 1.1 matt #define IRQ_F_INT0 0x00 /* INT0 */ 91 1.1 matt #define IRQ_F_INT1 0x01 /* INT1 */ 92 1.1 matt #define IRQ_F_INT2 0x02 /* INT2 */ 93 1.1 matt #define IRQ_F_INT3 0x03 /* INT3 */ 94 1.1 matt #define IRQ_F_INTMASK 0x07 /* INT mask */ 95 1.1 matt 96 1.1 matt const struct gdium_irqmap gdium_irqmap[] = { 97 1.1 matt { "gpio0", GDIUM_IRQ_GPIO0, IRQ_F_INT0 }, 98 1.1 matt { "gpio1", GDIUM_IRQ_GPIO1, IRQ_F_INT0 }, 99 1.1 matt { "gpio2", GDIUM_IRQ_GPIO2, IRQ_F_INT0 }, 100 1.1 matt { "gpio3", GDIUM_IRQ_GPIO3, IRQ_F_INT0 }, 101 1.1 matt 102 1.1 matt { "pci inta", GDIUM_IRQ_PCI_INTA, IRQ_F_INT0 }, 103 1.1 matt { "pci intb", GDIUM_IRQ_PCI_INTB, IRQ_F_INT0 }, 104 1.1 matt { "pci intc", GDIUM_IRQ_PCI_INTC, IRQ_F_INT0 }, 105 1.1 matt { "pci intd", GDIUM_IRQ_PCI_INTD, IRQ_F_INT0 }, 106 1.1 matt 107 1.1 matt { "pci perr", GDIUM_IRQ_PCI_PERR, IRQ_F_EDGE|IRQ_F_INT1 }, 108 1.1 matt { "pci serr", GDIUM_IRQ_PCI_SERR, IRQ_F_EDGE|IRQ_F_INT1 }, 109 1.1 matt 110 1.1 matt { "denali", GDIUM_IRQ_DENALI, IRQ_F_INT1 }, 111 1.1 matt 112 1.2 matt { "mips int0", GDIUM_IRQ_INT0, IRQ_F_INT0 }, 113 1.2 matt { "mips int1", GDIUM_IRQ_INT1, IRQ_F_INT1 }, 114 1.2 matt { "mips int2", GDIUM_IRQ_INT2, IRQ_F_INT2 }, 115 1.2 matt { "mips int3", GDIUM_IRQ_INT3, IRQ_F_INT3 }, 116 1.1 matt }; 117 1.1 matt 118 1.1 matt struct gdium_intrhead { 119 1.1 matt struct evcnt intr_count; 120 1.1 matt int intr_refcnt; 121 1.1 matt }; 122 1.1 matt struct gdium_intrhead gdium_intrtab[__arraycount(gdium_irqmap)]; 123 1.1 matt 124 1.1 matt #define NINTRS 2 /* MIPS INT0 - INT1 */ 125 1.1 matt 126 1.1 matt struct gdium_cpuintr { 127 1.1 matt LIST_HEAD(, evbmips_intrhand) cintr_list; 128 1.1 matt struct evcnt cintr_count; 129 1.2 matt int cintr_refcnt; 130 1.1 matt }; 131 1.1 matt 132 1.1 matt struct gdium_cpuintr gdium_cpuintrs[NINTRS]; 133 1.3 bouyer const char * const gdium_cpuintrnames[NINTRS] = { 134 1.1 matt "int 0 (pci)", 135 1.2 matt "int 1 (errors)", 136 1.1 matt }; 137 1.1 matt 138 1.1 matt /* 139 1.1 matt * This is a mask of bits to clear in the SR when we go to a 140 1.1 matt * given hardware interrupt priority level. 141 1.1 matt */ 142 1.3 bouyer static const struct ipl_sr_map gdium_ipl_sr_map = { 143 1.3 bouyer .sr_bits = { 144 1.3 bouyer [IPL_NONE] = 0, 145 1.3 bouyer [IPL_SOFTCLOCK] = MIPS_SOFT_INT_MASK_0, 146 1.3 bouyer [IPL_SOFTNET] = MIPS_SOFT_INT_MASK_0 | MIPS_SOFT_INT_MASK_1, 147 1.1 matt [IPL_VM] = 148 1.1 matt MIPS_SOFT_INT_MASK_0 | MIPS_SOFT_INT_MASK_1 | 149 1.2 matt MIPS_INT_MASK_0 | 150 1.2 matt MIPS_INT_MASK_1 | 151 1.2 matt MIPS_INT_MASK_2 | 152 1.2 matt MIPS_INT_MASK_3 | 153 1.2 matt MIPS_INT_MASK_4, 154 1.1 matt [IPL_SCHED] = 155 1.1 matt MIPS_SOFT_INT_MASK_0 | MIPS_SOFT_INT_MASK_1 | 156 1.1 matt MIPS_INT_MASK_0 | 157 1.1 matt MIPS_INT_MASK_1 | 158 1.1 matt MIPS_INT_MASK_2 | 159 1.1 matt MIPS_INT_MASK_3 | 160 1.1 matt MIPS_INT_MASK_4 | 161 1.1 matt MIPS_INT_MASK_5, 162 1.3 bouyer [IPL_DDB] = MIPS_INT_MASK, 163 1.3 bouyer [IPL_HIGH] = MIPS_INT_MASK, 164 1.3 bouyer }, 165 1.1 matt }; 166 1.1 matt 167 1.3 bouyer int gdium_pci_intr_map(const struct pci_attach_args *, pci_intr_handle_t *); 168 1.7 christos const char *gdium_pci_intr_string(void *, pci_intr_handle_t, char *, size_t); 169 1.1 matt const struct evcnt *gdium_pci_intr_evcnt(void *, pci_intr_handle_t); 170 1.1 matt void *gdium_pci_intr_establish(void *, pci_intr_handle_t, int, 171 1.1 matt int (*)(void *), void *); 172 1.1 matt void gdium_pci_intr_disestablish(void *, void *); 173 1.1 matt void gdium_pci_conf_interrupt(void *, int, int, int, int, int *); 174 1.1 matt 175 1.1 matt void 176 1.1 matt evbmips_intr_init(void) 177 1.1 matt { 178 1.3 bouyer struct gdium_config * const gc = &gdium_configuration; 179 1.1 matt struct bonito_config *bc = &gc->gc_bonito; 180 1.1 matt const struct gdium_irqmap *irqmap; 181 1.1 matt uint32_t intbit; 182 1.3 bouyer size_t i; 183 1.3 bouyer 184 1.3 bouyer ipl_sr_map = gdium_ipl_sr_map; 185 1.1 matt 186 1.1 matt for (i = 0; i < NINTRS; i++) { 187 1.1 matt LIST_INIT(&gdium_cpuintrs[i].cintr_list); 188 1.1 matt evcnt_attach_dynamic(&gdium_cpuintrs[i].cintr_count, 189 1.1 matt EVCNT_TYPE_INTR, NULL, "mips", gdium_cpuintrnames[i]); 190 1.1 matt } 191 1.1 matt //evcnt_attach_static(&mips_int5_evcnt); 192 1.1 matt 193 1.1 matt for (i = 0; i < __arraycount(gdium_irqmap); i++) { 194 1.1 matt irqmap = &gdium_irqmap[i]; 195 1.1 matt intbit = 1 << irqmap->irqidx; 196 1.1 matt 197 1.1 matt evcnt_attach_dynamic(&gdium_intrtab[i].intr_count, 198 1.1 matt EVCNT_TYPE_INTR, NULL, "bonito", irqmap->name); 199 1.1 matt 200 1.1 matt if (irqmap->irqidx < 4) 201 1.1 matt bc->bc_gpioIE |= intbit; 202 1.1 matt if (irqmap->flags & IRQ_F_INVERT) 203 1.1 matt bc->bc_intPol |= intbit; 204 1.1 matt if (irqmap->flags & IRQ_F_EDGE) 205 1.1 matt bc->bc_intEdge |= intbit; 206 1.1 matt if ((irqmap->flags & IRQ_F_INTMASK) == IRQ_F_INT1) 207 1.1 matt bc->bc_intSteer |= intbit; 208 1.1 matt 209 1.1 matt REGVAL(BONITO_INTENCLR) = intbit; 210 1.1 matt } 211 1.1 matt 212 1.1 matt REGVAL(BONITO_GPIOIE) = bc->bc_gpioIE; 213 1.1 matt REGVAL(BONITO_INTEDGE) = bc->bc_intEdge; 214 1.1 matt REGVAL(BONITO_INTSTEER) = bc->bc_intSteer; 215 1.1 matt REGVAL(BONITO_INTPOL) = bc->bc_intPol; 216 1.1 matt 217 1.1 matt gc->gc_pc.pc_intr_v = NULL; 218 1.1 matt gc->gc_pc.pc_intr_map = gdium_pci_intr_map; 219 1.1 matt gc->gc_pc.pc_intr_string = gdium_pci_intr_string; 220 1.1 matt gc->gc_pc.pc_intr_evcnt = gdium_pci_intr_evcnt; 221 1.1 matt gc->gc_pc.pc_intr_establish = gdium_pci_intr_establish; 222 1.1 matt gc->gc_pc.pc_intr_disestablish = gdium_pci_intr_disestablish; 223 1.1 matt gc->gc_pc.pc_conf_interrupt = gdium_pci_conf_interrupt; 224 1.1 matt 225 1.1 matt /* We let the PCI-ISA bridge code handle this. */ 226 1.1 matt gc->gc_pc.pc_pciide_compat_intr_establish = NULL; 227 1.1 matt } 228 1.1 matt 229 1.1 matt void * 230 1.2 matt evbmips_intr_establish(int irq, int (*func)(void *), void *arg) 231 1.1 matt { 232 1.1 matt const struct gdium_irqmap *irqmap; 233 1.1 matt struct evbmips_intrhand *ih; 234 1.2 matt int level; 235 1.1 matt int s; 236 1.1 matt 237 1.1 matt irqmap = &gdium_irqmap[irq]; 238 1.1 matt KASSERT(irq < __arraycount(gdium_irqmap)); 239 1.1 matt 240 1.1 matt KASSERT(irq == irqmap->irqidx); 241 1.1 matt 242 1.10 thorpej ih = kmem_zalloc(sizeof(*ih), KM_SLEEP); 243 1.1 matt ih->ih_func = func; 244 1.1 matt ih->ih_arg = arg; 245 1.1 matt ih->ih_irq = irq; 246 1.1 matt 247 1.1 matt s = splhigh(); 248 1.1 matt 249 1.1 matt /* 250 1.1 matt * First, link it into the tables. 251 1.1 matt */ 252 1.2 matt level = (irqmap->flags & IRQ_F_INT1) != 0; 253 1.2 matt LIST_INSERT_HEAD(&gdium_cpuintrs[level].cintr_list, ih, ih_q); 254 1.2 matt gdium_cpuintrs[level].cintr_refcnt++; 255 1.1 matt 256 1.1 matt /* 257 1.1 matt * Now enable it. 258 1.1 matt */ 259 1.2 matt if (gdium_intrtab[ih->ih_irq].intr_refcnt++ == 0) 260 1.2 matt REGVAL(BONITO_INTENSET) = (1 << ih->ih_irq); 261 1.1 matt 262 1.1 matt splx(s); 263 1.1 matt 264 1.1 matt return (ih); 265 1.1 matt } 266 1.1 matt 267 1.1 matt void 268 1.2 matt evbmips_intr_disestablish(void *cookie) 269 1.1 matt { 270 1.1 matt const struct gdium_irqmap *irqmap; 271 1.1 matt struct evbmips_intrhand *ih = cookie; 272 1.1 matt int s; 273 1.1 matt 274 1.1 matt irqmap = &gdium_irqmap[ih->ih_irq]; 275 1.1 matt 276 1.1 matt s = splhigh(); 277 1.1 matt 278 1.1 matt /* 279 1.1 matt * First, remove it from the table. 280 1.1 matt */ 281 1.1 matt LIST_REMOVE(ih, ih_q); 282 1.2 matt gdium_cpuintrs[(irqmap->flags & IRQ_F_INT1) != 0].cintr_refcnt--; 283 1.1 matt 284 1.1 matt /* 285 1.1 matt * Now, disable it, if there is nothing remaining on the 286 1.1 matt * list. 287 1.1 matt */ 288 1.2 matt if (gdium_intrtab[ih->ih_irq].intr_refcnt-- == 1) 289 1.2 matt REGVAL(BONITO_INTENCLR) = (1 << ih->ih_irq); 290 1.1 matt 291 1.1 matt splx(s); 292 1.1 matt 293 1.10 thorpej kmem_free(ih, sizeof(*ih)); 294 1.1 matt } 295 1.1 matt 296 1.1 matt void 297 1.8 skrll evbmips_iointr(int ipl, uint32_t ipending, struct clockframe *cf) 298 1.1 matt { 299 1.1 matt struct evbmips_intrhand *ih; 300 1.1 matt int level; 301 1.1 matt uint32_t isr; 302 1.1 matt 303 1.1 matt /* 304 1.1 matt * Read the interrupt pending registers, mask them with the 305 1.1 matt * ones we have enabled, and service them in order of decreasing 306 1.1 matt * priority. 307 1.1 matt */ 308 1.1 matt isr = REGVAL(BONITO_INTISR) & REGVAL(BONITO_INTEN); 309 1.1 matt for (level = 1; level >= 0; level--) { 310 1.2 matt if ((ipending & (MIPS_INT_MASK_4 << level)) == 0) 311 1.1 matt continue; 312 1.1 matt gdium_cpuintrs[level].cintr_count.ev_count++; 313 1.2 matt LIST_FOREACH (ih, &gdium_cpuintrs[level].cintr_list, ih_q) { 314 1.1 matt if (isr & (1 << ih->ih_irq)) { 315 1.1 matt gdium_intrtab[ih->ih_irq].intr_count.ev_count++; 316 1.1 matt (*ih->ih_func)(ih->ih_arg); 317 1.1 matt } 318 1.1 matt } 319 1.1 matt } 320 1.1 matt } 321 1.1 matt 322 1.1 matt /***************************************************************************** 323 1.1 matt * PCI interrupt support 324 1.1 matt *****************************************************************************/ 325 1.1 matt 326 1.1 matt int 327 1.3 bouyer gdium_pci_intr_map(const struct pci_attach_args *pa, 328 1.1 matt pci_intr_handle_t *ihp) 329 1.1 matt { 330 1.1 matt static const int8_t pciirqmap[5/*device*/] = { 331 1.1 matt GDIUM_IRQ_PCI_INTC, /* 13: PCI 802.11 */ 332 1.1 matt GDIUM_IRQ_PCI_INTA, /* 14: SM501 */ 333 1.1 matt GDIUM_IRQ_PCI_INTB, /* 15: NEC USB (2 func) */ 334 1.1 matt GDIUM_IRQ_PCI_INTD, /* 16: Ethernet */ 335 1.1 matt GDIUM_IRQ_PCI_INTC, /* 17: NEC USB (2 func) */ 336 1.1 matt }; 337 1.1 matt pcitag_t bustag = pa->pa_intrtag; 338 1.1 matt int buspin = pa->pa_intrpin; 339 1.1 matt pci_chipset_tag_t pc = pa->pa_pc; 340 1.1 matt int device; 341 1.1 matt 342 1.1 matt if (buspin == 0) { 343 1.1 matt /* No IRQ used. */ 344 1.1 matt return (1); 345 1.1 matt } 346 1.1 matt 347 1.1 matt if (buspin > 4) { 348 1.1 matt printf("gdium_pci_intr_map: bad interrupt pin %d\n", 349 1.1 matt buspin); 350 1.1 matt return (1); 351 1.1 matt } 352 1.1 matt 353 1.1 matt pci_decompose_tag(pc, bustag, NULL, &device, NULL); 354 1.1 matt if (device < 13 || device > 17) { 355 1.1 matt printf("gdium_pci_intr_map: bad device %d\n", 356 1.1 matt device); 357 1.1 matt return (1); 358 1.1 matt } 359 1.1 matt 360 1.1 matt *ihp = pciirqmap[device - 13]; 361 1.1 matt return (0); 362 1.1 matt } 363 1.1 matt 364 1.1 matt const char * 365 1.7 christos gdium_pci_intr_string(void *v, pci_intr_handle_t ih, char *buf, size_t len) 366 1.1 matt { 367 1.1 matt 368 1.1 matt if (ih >= __arraycount(gdium_irqmap)) 369 1.1 matt panic("gdium_intr_string: bogus IRQ %ld", ih); 370 1.1 matt 371 1.7 christos strlcpy(buf, gdium_irqmap[ih].name, len); 372 1.7 christos return buf; 373 1.1 matt } 374 1.1 matt 375 1.1 matt const struct evcnt * 376 1.1 matt gdium_pci_intr_evcnt(void *v, pci_intr_handle_t ih) 377 1.1 matt { 378 1.1 matt 379 1.1 matt return &gdium_intrtab[ih].intr_count; 380 1.1 matt } 381 1.1 matt 382 1.1 matt void * 383 1.1 matt gdium_pci_intr_establish(void *v, pci_intr_handle_t ih, int level, 384 1.1 matt int (*func)(void *), void *arg) 385 1.1 matt { 386 1.1 matt 387 1.1 matt if (ih >= __arraycount(gdium_irqmap)) 388 1.2 matt panic("gdium_pci_intr_establish: bogus IRQ %ld", ih); 389 1.1 matt 390 1.2 matt return evbmips_intr_establish(ih, func, arg); 391 1.1 matt } 392 1.1 matt 393 1.1 matt void 394 1.1 matt gdium_pci_intr_disestablish(void *v, void *cookie) 395 1.1 matt { 396 1.1 matt 397 1.2 matt return (evbmips_intr_disestablish(cookie)); 398 1.1 matt } 399 1.1 matt 400 1.1 matt void 401 1.1 matt gdium_pci_conf_interrupt(void *v, int bus, int dev, int pin, int swiz, 402 1.1 matt int *iline) 403 1.1 matt { 404 1.1 matt 405 1.1 matt /* 406 1.1 matt * We actually don't need to do anything; everything is handled 407 1.1 matt * in pci_intr_map(). 408 1.1 matt */ 409 1.1 matt *iline = 0; 410 1.1 matt } 411