1 /* $NetBSD: algor_p4032_intr.c,v 1.19 2008/04/28 20:23:10 martin Exp $ */ 2 3 /*- 4 * Copyright (c) 2001 The NetBSD Foundation, Inc. 5 * All rights reserved. 6 * 7 * This code is derived from software contributed to The NetBSD Foundation 8 * by Jason R. Thorpe. 9 * 10 * Redistribution and use in source and binary forms, with or without 11 * modification, are permitted provided that the following conditions 12 * are met: 13 * 1. Redistributions of source code must retain the above copyright 14 * notice, this list of conditions and the following disclaimer. 15 * 2. Redistributions in binary form must reproduce the above copyright 16 * notice, this list of conditions and the following disclaimer in the 17 * documentation and/or other materials provided with the distribution. 18 * 19 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 29 * POSSIBILITY OF SUCH DAMAGE. 30 */ 31 32 /* 33 * Platform-specific interrupt support for the Algorithmics P-4032. 34 * 35 * The Algorithmics P-4032 has an interrupt controller that is pretty 36 * flexible -- it can take an interrupt source and route it to an 37 * arbitrary MIPS CPU hardware interrupt pin. 38 */ 39 40 #include <sys/cdefs.h> 41 __KERNEL_RCSID(0, "$NetBSD: algor_p4032_intr.c,v 1.19 2008/04/28 20:23:10 martin Exp $"); 42 43 #include "opt_ddb.h" 44 45 #include <sys/param.h> 46 #include <sys/queue.h> 47 #include <sys/malloc.h> 48 #include <sys/systm.h> 49 #include <sys/device.h> 50 #include <sys/kernel.h> 51 #include <sys/cpu.h> 52 53 #include <machine/bus.h> 54 #include <machine/autoconf.h> 55 #include <machine/intr.h> 56 57 #include <mips/locore.h> 58 59 #include <dev/ic/mc146818reg.h> 60 61 #include <algor/algor/algor_p4032reg.h> 62 #include <algor/algor/algor_p4032var.h> 63 64 #include <dev/pci/pcireg.h> 65 #include <dev/pci/pcivar.h> 66 67 #define REGVAL(x) *((volatile u_int32_t *)(MIPS_PHYS_TO_KSEG1((x)))) 68 69 struct p4032_irqreg { 70 bus_addr_t addr; 71 u_int32_t val; 72 }; 73 74 #define IRQREG_8BIT 0 75 #define IRQREG_ERROR 1 76 #define IRQREG_PCI 2 77 #define NIRQREG 3 78 79 struct p4032_irqreg p4032_irqregs[NIRQREG] = { 80 { P4032_IRR0, 0 }, 81 { P4032_IRR1, 0 }, 82 { P4032_IRR2, 0 }, 83 }; 84 85 #define NSTEERREG 3 86 87 struct p4032_irqreg p4032_irqsteer[NSTEERREG] = { 88 { P4032_XBAR0, 0 }, 89 { P4032_XBAR1, 0 }, 90 { P4032_XBAR2, 0 }, 91 }; 92 93 #define NPCIIRQS 4 94 95 /* See algor_p4032var.h */ 96 #define N8BITIRQS 8 97 98 #define IRQMAP_PCIBASE 0 99 #define IRQMAP_8BITBASE NPCIIRQS 100 #define NIRQMAPS (IRQMAP_8BITBASE + N8BITIRQS) 101 102 const char *p4032_intrnames[NIRQMAPS] = { 103 /* 104 * PCI INTERRUPTS 105 */ 106 "PCIIRQ 0", 107 "PCIIRQ 1", 108 "PCIIRQ 2", 109 "PCIIRQ 3", 110 111 /* 112 * 8-BIT DEVICE INTERRUPTS 113 */ 114 "PCI ctlr", 115 "floppy", 116 "pckbc", 117 "com 1", 118 "com 2", 119 "centronics", 120 "gpio", 121 "mcclock", 122 }; 123 124 struct p4032_irqmap { 125 int irqidx; 126 int cpuintr; 127 int irqreg; 128 int irqbit; 129 int xbarreg; 130 int xbarshift; 131 }; 132 133 const struct p4032_irqmap p4032_irqmap[NIRQMAPS] = { 134 /* 135 * PCI INTERRUPTS 136 */ 137 /* PCIIRQ 0 */ 138 { 0, 0, 139 IRQREG_PCI, IRR2_PCIIRQ0, 140 2, 0 }, 141 142 /* PCIIRQ 1 */ 143 { 1, 0, 144 IRQREG_PCI, IRR2_PCIIRQ1, 145 2, 2 }, 146 147 /* PCIIRQ 2 */ 148 { 2, 0, 149 IRQREG_PCI, IRR2_PCIIRQ2, 150 2, 4 }, 151 152 /* PCIIRQ 3 */ 153 { 3, 0, 154 IRQREG_PCI, IRR2_PCIIRQ3, 155 2, 6 }, 156 157 /* 158 * 8-BIT DEVICE INTERRUPTS 159 */ 160 { P4032_IRQ_PCICTLR, 1, 161 IRQREG_8BIT, IRR0_PCICTLR, 162 0, 0 }, 163 164 { P4032_IRQ_FLOPPY, 1, 165 IRQREG_8BIT, IRR0_FLOPPY, 166 0, 2 }, 167 168 { P4032_IRQ_PCKBC, 1, 169 IRQREG_8BIT, IRR0_PCKBC, 170 0, 4 }, 171 172 { P4032_IRQ_COM1, 1, 173 IRQREG_8BIT, IRR0_COM1, 174 0, 6 }, 175 176 { P4032_IRQ_COM2, 1, 177 IRQREG_8BIT, IRR0_COM2, 178 1, 0 }, 179 180 { P4032_IRQ_LPT, 1, 181 IRQREG_8BIT, IRR0_LPT, 182 1, 2 }, 183 184 { P4032_IRQ_GPIO, 1, 185 IRQREG_8BIT, IRR0_GPIO, 186 1, 4 }, 187 188 { P4032_IRQ_RTC, 1, 189 IRQREG_8BIT, IRR0_RTC, 190 1, 6 }, 191 }; 192 193 struct p4032_intrhead { 194 struct evcnt intr_count; 195 int intr_refcnt; 196 }; 197 struct p4032_intrhead p4032_intrtab[NIRQMAPS]; 198 199 #define NINTRS 2 /* MIPS INT0 - INT1 */ 200 201 202 struct p4032_cpuintr { 203 LIST_HEAD(, algor_intrhand) cintr_list; 204 struct evcnt cintr_count; 205 }; 206 207 struct p4032_cpuintr p4032_cpuintrs[NINTRS]; 208 const char *p4032_cpuintrnames[NINTRS] = { 209 "int 0 (pci)", 210 "int 1 (8-bit)", 211 }; 212 213 const char *p4032_intrgroups[NINTRS] = { 214 "pci", 215 "8-bit", 216 }; 217 218 void *algor_p4032_intr_establish(int, int (*)(void *), void *); 219 void algor_p4032_intr_disestablish(void *); 220 221 int algor_p4032_pci_intr_map(struct pci_attach_args *, pci_intr_handle_t *); 222 const char *algor_p4032_pci_intr_string(void *, pci_intr_handle_t); 223 const struct evcnt *algor_p4032_pci_intr_evcnt(void *, pci_intr_handle_t); 224 void *algor_p4032_pci_intr_establish(void *, pci_intr_handle_t, int, 225 int (*)(void *), void *); 226 void algor_p4032_pci_intr_disestablish(void *, void *); 227 void algor_p4032_pci_conf_interrupt(void *, int, int, int, int, int *); 228 229 void algor_p4032_iointr(u_int32_t, u_int32_t, u_int32_t, u_int32_t); 230 231 void 232 algor_p4032_intr_init(struct p4032_config *acp) 233 { 234 const struct p4032_irqmap *irqmap; 235 int i; 236 237 for (i = 0; i < NIRQREG; i++) 238 REGVAL(p4032_irqregs[i].addr) = p4032_irqregs[i].val; 239 240 for (i = 0; i < NINTRS; i++) { 241 LIST_INIT(&p4032_cpuintrs[i].cintr_list); 242 evcnt_attach_dynamic(&p4032_cpuintrs[i].cintr_count, 243 EVCNT_TYPE_INTR, NULL, "mips", p4032_cpuintrnames[i]); 244 } 245 evcnt_attach_static(&mips_int5_evcnt); 246 247 for (i = 0; i < NIRQMAPS; i++) { 248 irqmap = &p4032_irqmap[i]; 249 250 p4032_irqsteer[irqmap->xbarreg].val |= 251 irqmap->cpuintr << irqmap->xbarshift; 252 253 evcnt_attach_dynamic(&p4032_intrtab[i].intr_count, 254 EVCNT_TYPE_INTR, NULL, p4032_intrgroups[irqmap->cpuintr], 255 p4032_intrnames[i]); 256 } 257 258 for (i = 0; i < NSTEERREG; i++) 259 REGVAL(p4032_irqsteer[i].addr) = p4032_irqsteer[i].val; 260 261 acp->ac_pc.pc_intr_v = NULL; 262 acp->ac_pc.pc_intr_map = algor_p4032_pci_intr_map; 263 acp->ac_pc.pc_intr_string = algor_p4032_pci_intr_string; 264 acp->ac_pc.pc_intr_evcnt = algor_p4032_pci_intr_evcnt; 265 acp->ac_pc.pc_intr_establish = algor_p4032_pci_intr_establish; 266 acp->ac_pc.pc_intr_disestablish = algor_p4032_pci_intr_disestablish; 267 acp->ac_pc.pc_conf_interrupt = algor_p4032_pci_conf_interrupt; 268 acp->ac_pc.pc_pciide_compat_intr_establish = NULL; 269 270 algor_intr_establish = algor_p4032_intr_establish; 271 algor_intr_disestablish = algor_p4032_intr_disestablish; 272 algor_iointr = algor_p4032_iointr; 273 } 274 275 void 276 algor_p4032_cal_timer(bus_space_tag_t st, bus_space_handle_t sh) 277 { 278 u_long ctrdiff[4], startctr, endctr, cps; 279 u_int32_t irr; 280 int i; 281 282 /* Disable interrupts first. */ 283 bus_space_write_1(st, sh, 0, MC_REGB); 284 bus_space_write_1(st, sh, 1, MC_REGB_SQWE | MC_REGB_BINARY | 285 MC_REGB_24HR); 286 287 /* Initialize for 16Hz. */ 288 bus_space_write_1(st, sh, 0, MC_REGA); 289 bus_space_write_1(st, sh, 1, MC_BASE_32_KHz | MC_RATE_16_Hz); 290 291 REGVAL(P4032_IRR0) = IRR0_RTC; 292 293 /* Run the loop an extra time to prime the cache. */ 294 for (i = 0; i < 4; i++) { 295 led_display('h', 'z', '0' + i, ' '); 296 297 /* Enable the interrupt. */ 298 bus_space_write_1(st, sh, 0, MC_REGB); 299 bus_space_write_1(st, sh, 1, MC_REGB_PIE | MC_REGB_SQWE | 300 MC_REGB_BINARY | MC_REGB_24HR); 301 302 /* Wait for it to happen. */ 303 startctr = mips3_cp0_count_read(); 304 do { 305 irr = REGVAL(P4032_IRR0); 306 endctr = mips3_cp0_count_read(); 307 } while ((irr & IRR0_RTC) == 0); 308 309 /* ACK. */ 310 bus_space_write_1(st, sh, 0, MC_REGC); 311 (void) bus_space_read_1(st, sh, 1); 312 313 /* Disable. */ 314 bus_space_write_1(st, sh, 0, MC_REGB); 315 bus_space_write_1(st, sh, 1, MC_REGB_SQWE | MC_REGB_BINARY | 316 MC_REGB_24HR); 317 318 ctrdiff[i] = endctr - startctr; 319 } 320 321 REGVAL(P4032_IRR0) = 0; 322 323 /* Update CPU frequency values */ 324 cps = ((ctrdiff[2] + ctrdiff[3]) / 2) * 16; 325 /* XXX mips_cpu_flags isn't set here; assume CPU_MIPS_DOUBLE_COUNT */ 326 curcpu()->ci_cpu_freq = cps * 2; 327 curcpu()->ci_cycles_per_hz = (curcpu()->ci_cpu_freq + hz / 2) / hz; 328 curcpu()->ci_divisor_delay = 329 ((curcpu()->ci_cpu_freq + (1000000 / 2)) / 1000000); 330 /* XXX assume CPU_MIPS_DOUBLE_COUNT */ 331 curcpu()->ci_cycles_per_hz /= 2; 332 curcpu()->ci_divisor_delay /= 2; 333 MIPS_SET_CI_RECIPROCAL(curcpu()); 334 335 printf("Timer calibration: %lu cycles/sec [(%lu, %lu) * 16]\n", 336 cps, ctrdiff[2], ctrdiff[3]); 337 printf("CPU clock speed = %lu.%02luMHz " 338 "(hz cycles = %lu, delay divisor = %lu)\n", 339 curcpu()->ci_cpu_freq / 1000000, 340 (curcpu()->ci_cpu_freq % 1000000) / 10000, 341 curcpu()->ci_cycles_per_hz, curcpu()->ci_divisor_delay); 342 } 343 344 void * 345 algor_p4032_intr_establish(int irq, int (*func)(void *), void *arg) 346 { 347 const struct p4032_irqmap *irqmap; 348 struct algor_intrhand *ih; 349 int s; 350 351 irqmap = &p4032_irqmap[irq]; 352 353 KASSERT(irq == irqmap->irqidx); 354 355 ih = malloc(sizeof(*ih), M_DEVBUF, M_NOWAIT); 356 if (ih == NULL) 357 return (NULL); 358 359 ih->ih_func = func; 360 ih->ih_arg = arg; 361 ih->ih_irq = 0; 362 ih->ih_irqmap = irqmap; 363 364 s = splhigh(); 365 366 /* 367 * First, link it into the tables. 368 */ 369 LIST_INSERT_HEAD(&p4032_cpuintrs[irqmap->cpuintr].cintr_list, 370 ih, ih_q); 371 372 /* 373 * Now enable it. 374 */ 375 if (p4032_intrtab[irqmap->irqidx].intr_refcnt++ == 0) { 376 p4032_irqregs[irqmap->irqreg].val |= irqmap->irqbit; 377 REGVAL(p4032_irqregs[irqmap->irqreg].addr) = 378 p4032_irqregs[irqmap->irqreg].val; 379 } 380 381 splx(s); 382 383 return (ih); 384 } 385 386 void 387 algor_p4032_intr_disestablish(void *cookie) 388 { 389 const struct p4032_irqmap *irqmap; 390 struct algor_intrhand *ih = cookie; 391 int s; 392 393 irqmap = ih->ih_irqmap; 394 395 s = splhigh(); 396 397 /* 398 * First, remove it from the table. 399 */ 400 LIST_REMOVE(ih, ih_q); 401 402 /* 403 * Now, disable it, if there is nothing remaining on the 404 * list. 405 */ 406 if (p4032_intrtab[irqmap->irqidx].intr_refcnt-- == 1) { 407 p4032_irqregs[irqmap->irqreg].val &= ~irqmap->irqbit; 408 REGVAL(p4032_irqregs[irqmap->irqreg].addr) = 409 p4032_irqregs[irqmap->irqreg].val; 410 } 411 412 splx(s); 413 414 free(ih, M_DEVBUF); 415 } 416 417 void 418 algor_p4032_iointr(u_int32_t status, u_int32_t cause, u_int32_t pc, 419 u_int32_t ipending) 420 { 421 const struct p4032_irqmap *irqmap; 422 struct algor_intrhand *ih; 423 int level, i; 424 u_int32_t irr[NIRQREG]; 425 426 /* Check for ERROR interrupts. */ 427 if (ipending & MIPS_INT_MASK_4) { 428 irr[IRQREG_ERROR] = REGVAL(p4032_irqregs[IRQREG_ERROR].addr); 429 if (irr[IRQREG_ERROR] & IRR1_BUSERR) 430 printf("WARNING: Bus error\n"); 431 if (irr[IRQREG_ERROR] & IRR1_POWERFAIL) 432 printf("WARNING: Power failure\n"); 433 if (irr[IRQREG_ERROR] & IRR1_DEBUG) { 434 #ifdef DDB 435 printf("Debug switch -- entering debugger\n"); 436 led_display('D','D','B',' '); 437 Debugger(); 438 led_display('N','B','S','D'); 439 #else 440 printf("Debug switch ignored -- " 441 "no debugger configured\n"); 442 #endif 443 } 444 445 /* Clear them. */ 446 REGVAL(p4032_irqregs[IRQREG_ERROR].addr) = irr[IRQREG_ERROR]; 447 } 448 449 /* Do floppy DMA request interrupts. */ 450 if (ipending & MIPS_INT_MASK_3) { 451 /* 452 * XXX Hi, um, yah, we need to deal with 453 * XXX the floppy interrupt here. 454 */ 455 456 cause &= ~MIPS_INT_MASK_3; 457 _splset(MIPS_SR_INT_IE | 458 ((status & ~cause) & MIPS_HARD_INT_MASK)); 459 } 460 461 /* 462 * Read the interrupt pending registers, mask them with the 463 * ones we have enabled, and service them in order of decreasing 464 * priority. 465 */ 466 for (i = 0; i < NIRQREG; i++) { 467 if (i == IRQREG_ERROR) 468 continue; 469 irr[i] = REGVAL(p4032_irqregs[i].addr) & p4032_irqregs[i].val; 470 } 471 472 for (level = (NINTRS - 1); level >= 0; level--) { 473 if ((ipending & (MIPS_INT_MASK_0 << level)) == 0) 474 continue; 475 p4032_cpuintrs[level].cintr_count.ev_count++; 476 for (ih = LIST_FIRST(&p4032_cpuintrs[level].cintr_list); 477 ih != NULL; ih = LIST_NEXT(ih, ih_q)) { 478 irqmap = ih->ih_irqmap; 479 if (irr[irqmap->irqreg] & irqmap->irqbit) { 480 p4032_intrtab[ 481 irqmap->irqidx].intr_count.ev_count++; 482 (*ih->ih_func)(ih->ih_arg); 483 } 484 } 485 cause &= ~(MIPS_INT_MASK_0 << level); 486 } 487 488 /* Re-enable anything that we have processed. */ 489 _splset(MIPS_SR_INT_IE | ((status & ~cause) & MIPS_HARD_INT_MASK)); 490 } 491 492 /***************************************************************************** 493 * PCI interrupt support 494 *****************************************************************************/ 495 496 int 497 algor_p4032_pci_intr_map(struct pci_attach_args *pa, 498 pci_intr_handle_t *ihp) 499 { 500 static const int pciirqmap[6/*device*/][4/*pin*/] = { 501 { 1, -1, -1, -1 }, /* 5: Ethernet */ 502 { 2, 3, 0, 1 }, /* 6: PCI slot 1 */ 503 { 3, 0, 1, 2 }, /* 7: PCI slot 2 */ 504 { 0, -1, -1, -1 }, /* 8: SCSI */ 505 { -1, -1, -1, -1 }, /* 9: not used */ 506 { 0, 1, 2, 3 }, /* 10: custom connector */ 507 }; 508 pcitag_t bustag = pa->pa_intrtag; 509 int buspin = pa->pa_intrpin; 510 pci_chipset_tag_t pc = pa->pa_pc; 511 int device, irq; 512 513 if (buspin == 0) { 514 /* No IRQ used. */ 515 return (1); 516 } 517 518 if (buspin > 4) { 519 printf("algor_p4032_pci_intr_map: bad interrupt pin %d\n", 520 buspin); 521 return (1); 522 } 523 524 pci_decompose_tag(pc, bustag, NULL, &device, NULL); 525 if (device < 5 || device > 10) { 526 printf("algor_p4032_pci_intr_map: bad device %d\n", 527 device); 528 return (1); 529 } 530 531 irq = pciirqmap[device - 5][buspin - 1]; 532 if (irq == -1) { 533 printf("algor_p4032_pci_intr_map: no mapping for " 534 "device %d pin %d\n", device, buspin); 535 return (1); 536 } 537 538 *ihp = irq; 539 return (0); 540 } 541 542 const char * 543 algor_p4032_pci_intr_string(void *v, pci_intr_handle_t ih) 544 { 545 546 if (ih >= NPCIIRQS) 547 panic("algor_p4032_intr_string: bogus IRQ %ld", ih); 548 549 return (p4032_intrnames[ih]); 550 } 551 552 const struct evcnt * 553 algor_p4032_pci_intr_evcnt(void *v, pci_intr_handle_t ih) 554 { 555 556 return (&p4032_intrtab[ih].intr_count); 557 } 558 559 void * 560 algor_p4032_pci_intr_establish(void *v, pci_intr_handle_t ih, int level, 561 int (*func)(void *), void *arg) 562 { 563 564 if (ih >= NPCIIRQS) 565 panic("algor_p4032_intr_establish: bogus IRQ %ld", ih); 566 567 return (algor_p4032_intr_establish(ih, func, arg)); 568 } 569 570 void 571 algor_p4032_pci_intr_disestablish(void *v, void *cookie) 572 { 573 574 return (algor_p4032_intr_disestablish(cookie)); 575 } 576 577 void 578 algor_p4032_pci_conf_interrupt(void *v, int bus, int dev, int pin, int swiz, 579 int *iline) 580 { 581 582 /* 583 * We actually don't need to do anything; everything is handled 584 * in pci_intr_map(). 585 */ 586 *iline = 0; 587 } 588