gt_pci.c revision 254983
1/* $NetBSD: gt_pci.c,v 1.4 2003/07/15 00:24:54 lukem Exp $ */ 2 3/*- 4 * Copyright (c) 2001, 2002 Wasabi Systems, Inc. 5 * All rights reserved. 6 * 7 * Written by Jason R. Thorpe for Wasabi Systems, Inc. 8 * 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. 24 * 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. 36 */ 37 38/* 39 * PCI configuration support for gt I/O Processor chip. 40 */ 41 42#include <sys/cdefs.h> 43__FBSDID("$FreeBSD: head/sys/mips/malta/gt_pci.c 254983 2013-08-28 01:10:51Z gonzo $"); 44 45#include <sys/param.h> 46#include <sys/systm.h> 47 48#include <sys/bus.h> 49#include <sys/endian.h> 50#include <sys/interrupt.h> 51#include <sys/malloc.h> 52#include <sys/kernel.h> 53#include <sys/module.h> 54#include <sys/rman.h> 55 56#include <vm/vm.h> 57#include <vm/pmap.h> 58#include <vm/vm_extern.h> 59 60#include <machine/bus.h> 61#include <machine/cpu.h> 62#include <machine/pmap.h> 63 64#include <mips/malta/maltareg.h> 65 66#include <mips/malta/gtreg.h> 67#include <mips/malta/gtvar.h> 68 69#include <isa/isareg.h> 70#include <dev/ic/i8259.h> 71 72#include <dev/pci/pcireg.h> 73#include <dev/pci/pcivar.h> 74 75#include <dev/pci/pcib_private.h> 76#include "pcib_if.h" 77 78 79#define ICU_LEN 16 /* number of ISA IRQs */ 80 81/* 82 * XXX: These defines are from NetBSD's <dev/ic/i8259reg.h>. Respective file 83 * from FreeBSD src tree <dev/ic/i8259.h> lacks some definitions. 84 */ 85#define PIC_OCW1 1 86#define PIC_OCW2 0 87#define PIC_OCW3 0 88 89#define OCW2_SELECT 0 90#define OCW2_ILS(x) ((x) << 0) /* interrupt level select */ 91 92#define OCW3_POLL_IRQ(x) ((x) & 0x7f) 93#define OCW3_POLL_PENDING (1U << 7) 94 95/* 96 * Galileo controller's registers are LE so convert to then 97 * to/from native byte order. We rely on boot loader or emulator 98 * to set "swap bytes" configuration correctly for us 99 */ 100#define GT_PCI_DATA(v) htole32((v)) 101#define GT_HOST_DATA(v) le32toh((v)) 102 103struct gt_pci_softc; 104 105struct gt_pci_intr_cookie { 106 int irq; 107 struct gt_pci_softc *sc; 108}; 109 110struct gt_pci_softc { 111 device_t sc_dev; 112 bus_space_tag_t sc_st; 113 bus_space_handle_t sc_ioh_icu1; 114 bus_space_handle_t sc_ioh_icu2; 115 bus_space_handle_t sc_ioh_elcr; 116 117 int sc_busno; 118 struct rman sc_mem_rman; 119 struct rman sc_io_rman; 120 struct rman sc_irq_rman; 121 unsigned long sc_mem; 122 bus_space_handle_t sc_io; 123 124 struct resource *sc_irq; 125 struct intr_event *sc_eventstab[ICU_LEN]; 126 struct gt_pci_intr_cookie sc_intr_cookies[ICU_LEN]; 127 uint16_t sc_imask; 128 uint16_t sc_elcr; 129 130 uint16_t sc_reserved; 131 132 void *sc_ih; 133}; 134 135static void gt_pci_set_icus(struct gt_pci_softc *); 136static int gt_pci_intr(void *v); 137static int gt_pci_probe(device_t); 138static int gt_pci_attach(device_t); 139static int gt_pci_activate_resource(device_t, device_t, int, int, 140 struct resource *); 141static int gt_pci_setup_intr(device_t, device_t, struct resource *, 142 int, driver_filter_t *, driver_intr_t *, void *, void **); 143static int gt_pci_teardown_intr(device_t, device_t, struct resource *, void*); 144static int gt_pci_maxslots(device_t ); 145static int gt_pci_conf_setup(struct gt_pci_softc *, int, int, int, int, 146 uint32_t *); 147static uint32_t gt_pci_read_config(device_t, u_int, u_int, u_int, u_int, int); 148static void gt_pci_write_config(device_t, u_int, u_int, u_int, u_int, 149 uint32_t, int); 150static int gt_pci_route_interrupt(device_t pcib, device_t dev, int pin); 151static struct resource * gt_pci_alloc_resource(device_t, device_t, int, 152 int *, u_long, u_long, u_long, u_int); 153 154static void 155gt_pci_mask_irq(void *source) 156{ 157 struct gt_pci_intr_cookie *cookie = source; 158 struct gt_pci_softc *sc = cookie->sc; 159 int irq = cookie->irq; 160 161 sc->sc_imask |= (1 << irq); 162 sc->sc_elcr |= (1 << irq); 163 164 gt_pci_set_icus(sc); 165} 166 167static void 168gt_pci_unmask_irq(void *source) 169{ 170 struct gt_pci_intr_cookie *cookie = source; 171 struct gt_pci_softc *sc = cookie->sc; 172 int irq = cookie->irq; 173 174 /* Enable it, set trigger mode. */ 175 sc->sc_imask &= ~(1 << irq); 176 sc->sc_elcr &= ~(1 << irq); 177 178 gt_pci_set_icus(sc); 179} 180 181static void 182gt_pci_set_icus(struct gt_pci_softc *sc) 183{ 184 /* Enable the cascade IRQ (2) if 8-15 is enabled. */ 185 if ((sc->sc_imask & 0xff00) != 0xff00) 186 sc->sc_imask &= ~(1U << 2); 187 else 188 sc->sc_imask |= (1U << 2); 189 190 bus_space_write_1(sc->sc_st, sc->sc_ioh_icu1, PIC_OCW1, 191 sc->sc_imask & 0xff); 192 bus_space_write_1(sc->sc_st, sc->sc_ioh_icu2, PIC_OCW1, 193 (sc->sc_imask >> 8) & 0xff); 194 195 bus_space_write_1(sc->sc_st, sc->sc_ioh_elcr, 0, 196 sc->sc_elcr & 0xff); 197 bus_space_write_1(sc->sc_st, sc->sc_ioh_elcr, 1, 198 (sc->sc_elcr >> 8) & 0xff); 199} 200 201static int 202gt_pci_intr(void *v) 203{ 204 struct gt_pci_softc *sc = v; 205 struct intr_event *event; 206 int irq; 207 208 for (;;) { 209 bus_space_write_1(sc->sc_st, sc->sc_ioh_icu1, PIC_OCW3, 210 OCW3_SEL | OCW3_P); 211 irq = bus_space_read_1(sc->sc_st, sc->sc_ioh_icu1, PIC_OCW3); 212 if ((irq & OCW3_POLL_PENDING) == 0) 213 { 214 return FILTER_HANDLED; 215 } 216 217 irq = OCW3_POLL_IRQ(irq); 218 219 if (irq == 2) { 220 bus_space_write_1(sc->sc_st, sc->sc_ioh_icu2, 221 PIC_OCW3, OCW3_SEL | OCW3_P); 222 irq = bus_space_read_1(sc->sc_st, sc->sc_ioh_icu2, 223 PIC_OCW3); 224 if (irq & OCW3_POLL_PENDING) 225 irq = OCW3_POLL_IRQ(irq) + 8; 226 else 227 irq = 2; 228 } 229 230 event = sc->sc_eventstab[irq]; 231 232 if (!event || TAILQ_EMPTY(&event->ie_handlers)) 233 continue; 234 235 /* TODO: frame instead of NULL? */ 236 intr_event_handle(event, NULL); 237 /* XXX: Log stray IRQs */ 238 239 /* Send a specific EOI to the 8259. */ 240 if (irq > 7) { 241 bus_space_write_1(sc->sc_st, sc->sc_ioh_icu2, 242 PIC_OCW2, OCW2_SELECT | OCW2_EOI | OCW2_SL | 243 OCW2_ILS(irq & 7)); 244 irq = 2; 245 } 246 247 bus_space_write_1(sc->sc_st, sc->sc_ioh_icu1, PIC_OCW2, 248 OCW2_SELECT | OCW2_EOI | OCW2_SL | OCW2_ILS(irq)); 249 } 250 251 return FILTER_HANDLED; 252} 253 254static int 255gt_pci_probe(device_t dev) 256{ 257 device_set_desc(dev, "GT64120 PCI bridge"); 258 return (0); 259} 260 261static int 262gt_pci_attach(device_t dev) 263{ 264 265 uint32_t busno; 266 struct gt_pci_softc *sc = device_get_softc(dev); 267 int rid; 268 269 busno = 0; 270 sc->sc_dev = dev; 271 sc->sc_busno = busno; 272 sc->sc_st = mips_bus_space_generic; 273 274 /* Use KSEG1 to access IO ports for it is uncached */ 275 sc->sc_io = MIPS_PHYS_TO_KSEG1(MALTA_PCI0_IO_BASE); 276 sc->sc_io_rman.rm_type = RMAN_ARRAY; 277 sc->sc_io_rman.rm_descr = "GT64120 PCI I/O Ports"; 278 /* 279 * First 256 bytes are ISA's registers: e.g. i8259's 280 * So do not use them for general purpose PCI I/O window 281 */ 282 if (rman_init(&sc->sc_io_rman) != 0 || 283 rman_manage_region(&sc->sc_io_rman, 0x100, 0xffff) != 0) { 284 panic("gt_pci_attach: failed to set up I/O rman"); 285 } 286 287 /* Use KSEG1 to access PCI memory for it is uncached */ 288 sc->sc_mem = MIPS_PHYS_TO_KSEG1(MALTA_PCIMEM1_BASE); 289 sc->sc_mem_rman.rm_type = RMAN_ARRAY; 290 sc->sc_mem_rman.rm_descr = "GT64120 PCI Memory"; 291 if (rman_init(&sc->sc_mem_rman) != 0 || 292 rman_manage_region(&sc->sc_mem_rman, 293 sc->sc_mem, sc->sc_mem + MALTA_PCIMEM1_SIZE) != 0) { 294 panic("gt_pci_attach: failed to set up memory rman"); 295 } 296 sc->sc_irq_rman.rm_type = RMAN_ARRAY; 297 sc->sc_irq_rman.rm_descr = "GT64120 PCI IRQs"; 298 if (rman_init(&sc->sc_irq_rman) != 0 || 299 rman_manage_region(&sc->sc_irq_rman, 1, 31) != 0) 300 panic("gt_pci_attach: failed to set up IRQ rman"); 301 302 /* 303 * Map the PIC/ELCR registers. 304 */ 305#if 0 306 if (bus_space_map(sc->sc_st, 0x4d0, 2, 0, &sc->sc_ioh_elcr) != 0) 307 device_printf(dev, "unable to map ELCR registers\n"); 308 if (bus_space_map(sc->sc_st, IO_ICU1, 2, 0, &sc->sc_ioh_icu1) != 0) 309 device_printf(dev, "unable to map ICU1 registers\n"); 310 if (bus_space_map(sc->sc_st, IO_ICU2, 2, 0, &sc->sc_ioh_icu2) != 0) 311 device_printf(dev, "unable to map ICU2 registers\n"); 312#else 313 sc->sc_ioh_elcr = sc->sc_io + 0x4d0; 314 sc->sc_ioh_icu1 = sc->sc_io + IO_ICU1; 315 sc->sc_ioh_icu2 = sc->sc_io + IO_ICU2; 316#endif 317 318 319 /* All interrupts default to "masked off". */ 320 sc->sc_imask = 0xffff; 321 322 /* All interrupts default to edge-triggered. */ 323 sc->sc_elcr = 0; 324 325 /* 326 * Initialize the 8259s. 327 */ 328 /* reset, program device, 4 bytes */ 329 bus_space_write_1(sc->sc_st, sc->sc_ioh_icu1, 0, 330 ICW1_RESET | ICW1_IC4); 331 /* 332 * XXX: values from NetBSD's <dev/ic/i8259reg.h> 333 */ 334 bus_space_write_1(sc->sc_st, sc->sc_ioh_icu1, 1, 335 0/*XXX*/); 336 bus_space_write_1(sc->sc_st, sc->sc_ioh_icu1, 1, 337 1 << 2); 338 bus_space_write_1(sc->sc_st, sc->sc_ioh_icu1, 1, 339 ICW4_8086); 340 341 /* mask all interrupts */ 342 bus_space_write_1(sc->sc_st, sc->sc_ioh_icu1, 1, 343 sc->sc_imask & 0xff); 344 345 /* enable special mask mode */ 346 bus_space_write_1(sc->sc_st, sc->sc_ioh_icu1, 0, 347 OCW3_SEL | OCW3_ESMM | OCW3_SMM); 348 349 /* read IRR by default */ 350 bus_space_write_1(sc->sc_st, sc->sc_ioh_icu1, 0, 351 OCW3_SEL | OCW3_RR); 352 353 /* reset, program device, 4 bytes */ 354 bus_space_write_1(sc->sc_st, sc->sc_ioh_icu2, 0, 355 ICW1_RESET | ICW1_IC4); 356 bus_space_write_1(sc->sc_st, sc->sc_ioh_icu2, 1, 357 0/*XXX*/); 358 bus_space_write_1(sc->sc_st, sc->sc_ioh_icu2, 1, 359 1 << 2); 360 bus_space_write_1(sc->sc_st, sc->sc_ioh_icu2, 1, 361 ICW4_8086); 362 363 /* mask all interrupts */ 364 bus_space_write_1(sc->sc_st, sc->sc_ioh_icu2, 1, 365 sc->sc_imask & 0xff); 366 367 /* enable special mask mode */ 368 bus_space_write_1(sc->sc_st, sc->sc_ioh_icu2, 0, 369 OCW3_SEL | OCW3_ESMM | OCW3_SMM); 370 371 /* read IRR by default */ 372 bus_space_write_1(sc->sc_st, sc->sc_ioh_icu2, 0, 373 OCW3_SEL | OCW3_RR); 374 375 /* 376 * Default all interrupts to edge-triggered. 377 */ 378 bus_space_write_1(sc->sc_st, sc->sc_ioh_elcr, 0, 379 sc->sc_elcr & 0xff); 380 bus_space_write_1(sc->sc_st, sc->sc_ioh_elcr, 1, 381 (sc->sc_elcr >> 8) & 0xff); 382 383 /* 384 * Some ISA interrupts are reserved for devices that 385 * we know are hard-wired to certain IRQs. 386 */ 387 sc->sc_reserved = 388 (1U << 0) | /* timer */ 389 (1U << 1) | /* keyboard controller (keyboard) */ 390 (1U << 2) | /* PIC cascade */ 391 (1U << 3) | /* COM 2 */ 392 (1U << 4) | /* COM 1 */ 393 (1U << 6) | /* floppy */ 394 (1U << 7) | /* centronics */ 395 (1U << 8) | /* RTC */ 396 (1U << 9) | /* I2C */ 397 (1U << 12) | /* keyboard controller (mouse) */ 398 (1U << 14) | /* IDE primary */ 399 (1U << 15); /* IDE secondary */ 400 401 /* Hook up our interrupt handler. */ 402 if ((sc->sc_irq = bus_alloc_resource(dev, SYS_RES_IRQ, &rid, 403 MALTA_SOUTHBRIDGE_INTR, MALTA_SOUTHBRIDGE_INTR, 1, 404 RF_SHAREABLE | RF_ACTIVE)) == NULL) { 405 device_printf(dev, "unable to allocate IRQ resource\n"); 406 return ENXIO; 407 } 408 409 if ((bus_setup_intr(dev, sc->sc_irq, INTR_TYPE_MISC, 410 gt_pci_intr, NULL, sc, &sc->sc_ih))) { 411 device_printf(dev, 412 "WARNING: unable to register interrupt handler\n"); 413 return ENXIO; 414 } 415 416 /* Initialize memory and i/o rmans. */ 417 device_add_child(dev, "pci", busno); 418 return (bus_generic_attach(dev)); 419} 420 421static int 422gt_pci_maxslots(device_t dev) 423{ 424 return (PCI_SLOTMAX); 425} 426 427static int 428gt_pci_conf_setup(struct gt_pci_softc *sc, int bus, int slot, int func, 429 int reg, uint32_t *addr) 430{ 431 *addr = (bus << 16) | (slot << 11) | (func << 8) | reg; 432 433 return (0); 434} 435 436static uint32_t 437gt_pci_read_config(device_t dev, u_int bus, u_int slot, u_int func, u_int reg, 438 int bytes) 439{ 440 struct gt_pci_softc *sc = device_get_softc(dev); 441 uint32_t data; 442 uint32_t addr; 443 uint32_t shift, mask; 444 445 if (gt_pci_conf_setup(sc, bus, slot, func, reg & ~3, &addr)) 446 return (uint32_t)(-1); 447 448 /* Clear cause register bits. */ 449 GT_REGVAL(GT_INTR_CAUSE) = GT_PCI_DATA(0); 450 GT_REGVAL(GT_PCI0_CFG_ADDR) = GT_PCI_DATA((1 << 31) | addr); 451 /* 452 * Galileo system controller is special 453 */ 454 if ((bus == 0) && (slot == 0)) 455 data = GT_PCI_DATA(GT_REGVAL(GT_PCI0_CFG_DATA)); 456 else 457 data = GT_REGVAL(GT_PCI0_CFG_DATA); 458 459 /* Check for master abort. */ 460 if (GT_HOST_DATA(GT_REGVAL(GT_INTR_CAUSE)) & (GTIC_MASABORT0 | GTIC_TARABORT0)) 461 data = (uint32_t) -1; 462 463 switch(reg % 4) 464 { 465 case 3: 466 shift = 24; 467 break; 468 case 2: 469 shift = 16; 470 break; 471 case 1: 472 shift = 8; 473 break; 474 default: 475 shift = 0; 476 break; 477 } 478 479 switch(bytes) 480 { 481 case 1: 482 mask = 0xff; 483 data = (data >> shift) & mask; 484 break; 485 case 2: 486 mask = 0xffff; 487 if(reg % 4 == 0) 488 data = data & mask; 489 else 490 data = (data >> 16) & mask; 491 break; 492 case 4: 493 break; 494 default: 495 panic("gt_pci_readconfig: wrong bytes count"); 496 break; 497 } 498#if 0 499 printf("PCICONF_READ(%02x:%02x.%02x[%04x] -> %02x(%d)\n", 500 bus, slot, func, reg, data, bytes); 501#endif 502 503 return (data); 504} 505 506static void 507gt_pci_write_config(device_t dev, u_int bus, u_int slot, u_int func, u_int reg, 508 uint32_t data, int bytes) 509{ 510 struct gt_pci_softc *sc = device_get_softc(dev); 511 uint32_t addr; 512 uint32_t reg_data; 513 uint32_t shift, mask; 514 515 if(bytes != 4) 516 { 517 reg_data = gt_pci_read_config(dev, bus, slot, func, reg, 4); 518 519 shift = 8 * (reg & 3); 520 521 switch(bytes) 522 { 523 case 1: 524 mask = 0xff; 525 data = (reg_data & ~ (mask << shift)) | (data << shift); 526 break; 527 case 2: 528 mask = 0xffff; 529 if(reg % 4 == 0) 530 data = (reg_data & ~mask) | data; 531 else 532 data = (reg_data & ~ (mask << shift)) | 533 (data << shift); 534 break; 535 case 4: 536 break; 537 default: 538 panic("gt_pci_readconfig: wrong bytes count"); 539 break; 540 } 541 } 542 543 if (gt_pci_conf_setup(sc, bus, slot, func, reg & ~3, &addr)) 544 return; 545 546 /* The galileo has problems accessing device 31. */ 547 if (bus == 0 && slot == 31) 548 return; 549 550 /* XXX: no support for bus > 0 yet */ 551 if (bus > 0) 552 return; 553 554 /* Clear cause register bits. */ 555 GT_REGVAL(GT_INTR_CAUSE) = GT_PCI_DATA(0); 556 557 GT_REGVAL(GT_PCI0_CFG_ADDR) = GT_PCI_DATA((1 << 31) | addr); 558 559 /* 560 * Galileo system controller is special 561 */ 562 if ((bus == 0) && (slot == 0)) 563 GT_REGVAL(GT_PCI0_CFG_DATA) = GT_PCI_DATA(data); 564 else 565 GT_REGVAL(GT_PCI0_CFG_DATA) = data; 566 567#if 0 568 printf("PCICONF_WRITE(%02x:%02x.%02x[%04x] -> %02x(%d)\n", 569 bus, slot, func, reg, data, bytes); 570#endif 571 572} 573 574static int 575gt_pci_route_interrupt(device_t pcib, device_t dev, int pin) 576{ 577 int bus; 578 int device; 579 int func; 580 /* struct gt_pci_softc *sc = device_get_softc(pcib); */ 581 bus = pci_get_bus(dev); 582 device = pci_get_slot(dev); 583 func = pci_get_function(dev); 584 /* 585 * XXXMIPS: We need routing logic. This is just a stub . 586 */ 587 switch (device) { 588 case 9: /* 589 * PIIX4 IDE adapter. HW IRQ0 590 */ 591 return 0; 592 case 11: /* Ethernet */ 593 return 10; 594 default: 595 device_printf(pcib, "no IRQ mapping for %d/%d/%d/%d\n", bus, device, func, pin); 596 597 } 598 return (0); 599 600} 601 602static int 603gt_read_ivar(device_t dev, device_t child, int which, uintptr_t *result) 604{ 605 struct gt_pci_softc *sc = device_get_softc(dev); 606 switch (which) { 607 case PCIB_IVAR_DOMAIN: 608 *result = 0; 609 return (0); 610 case PCIB_IVAR_BUS: 611 *result = sc->sc_busno; 612 return (0); 613 614 } 615 return (ENOENT); 616} 617 618static int 619gt_write_ivar(device_t dev, device_t child, int which, uintptr_t result) 620{ 621 struct gt_pci_softc * sc = device_get_softc(dev); 622 623 switch (which) { 624 case PCIB_IVAR_BUS: 625 sc->sc_busno = result; 626 return (0); 627 } 628 return (ENOENT); 629} 630 631static struct resource * 632gt_pci_alloc_resource(device_t bus, device_t child, int type, int *rid, 633 u_long start, u_long end, u_long count, u_int flags) 634{ 635 struct gt_pci_softc *sc = device_get_softc(bus); 636 struct resource *rv = NULL; 637 struct rman *rm; 638 bus_space_tag_t bt = 0; 639 bus_space_handle_t bh = 0; 640 641 switch (type) { 642 case SYS_RES_IRQ: 643 rm = &sc->sc_irq_rman; 644 break; 645 case SYS_RES_MEMORY: 646 rm = &sc->sc_mem_rman; 647 bt = sc->sc_st; 648 bh = sc->sc_mem; 649 break; 650 case SYS_RES_IOPORT: 651 rm = &sc->sc_io_rman; 652 bt = sc->sc_st; 653 bh = sc->sc_io; 654 break; 655 default: 656 return (NULL); 657 } 658 659 rv = rman_reserve_resource(rm, start, end, count, flags, child); 660 if (rv == NULL) 661 return (NULL); 662 rman_set_rid(rv, *rid); 663 if (type != SYS_RES_IRQ) { 664 bh += (rman_get_start(rv)); 665 666 rman_set_bustag(rv, bt); 667 rman_set_bushandle(rv, bh); 668 if (flags & RF_ACTIVE) { 669 if (bus_activate_resource(child, type, *rid, rv)) { 670 rman_release_resource(rv); 671 return (NULL); 672 } 673 } 674 } 675 return (rv); 676} 677 678static int 679gt_pci_activate_resource(device_t bus, device_t child, int type, int rid, 680 struct resource *r) 681{ 682 bus_space_handle_t p; 683 int error; 684 685 if ((type == SYS_RES_MEMORY) || (type == SYS_RES_IOPORT)) { 686 error = bus_space_map(rman_get_bustag(r), 687 rman_get_bushandle(r), rman_get_size(r), 0, &p); 688 if (error) 689 return (error); 690 rman_set_bushandle(r, p); 691 } 692 return (rman_activate_resource(r)); 693} 694 695static int 696gt_pci_setup_intr(device_t dev, device_t child, struct resource *ires, 697 int flags, driver_filter_t *filt, driver_intr_t *handler, 698 void *arg, void **cookiep) 699{ 700 struct gt_pci_softc *sc = device_get_softc(dev); 701 struct intr_event *event; 702 int irq, error; 703 704 irq = rman_get_start(ires); 705 if (irq >= ICU_LEN || irq == 2) 706 panic("%s: bad irq or type", __func__); 707 708 event = sc->sc_eventstab[irq]; 709 sc->sc_intr_cookies[irq].irq = irq; 710 sc->sc_intr_cookies[irq].sc = sc; 711 if (event == NULL) { 712 error = intr_event_create(&event, 713 (void *)&sc->sc_intr_cookies[irq], 0, irq, 714 gt_pci_mask_irq, gt_pci_unmask_irq, 715 NULL, NULL, "gt_pci intr%d:", irq); 716 if (error) 717 return 0; 718 sc->sc_eventstab[irq] = event; 719 } 720 721 intr_event_add_handler(event, device_get_nameunit(child), filt, 722 handler, arg, intr_priority(flags), flags, cookiep); 723 724 gt_pci_unmask_irq((void *)&sc->sc_intr_cookies[irq]); 725 return 0; 726} 727 728static int 729gt_pci_teardown_intr(device_t dev, device_t child, struct resource *res, 730 void *cookie) 731{ 732 struct gt_pci_softc *sc = device_get_softc(dev); 733 int irq; 734 735 irq = rman_get_start(res); 736 gt_pci_mask_irq((void *)&sc->sc_intr_cookies[irq]); 737 738 return (intr_event_remove_handler(cookie)); 739} 740 741static device_method_t gt_pci_methods[] = { 742 /* Device interface */ 743 DEVMETHOD(device_probe, gt_pci_probe), 744 DEVMETHOD(device_attach, gt_pci_attach), 745 DEVMETHOD(device_shutdown, bus_generic_shutdown), 746 DEVMETHOD(device_suspend, bus_generic_suspend), 747 DEVMETHOD(device_resume, bus_generic_resume), 748 749 /* Bus interface */ 750 DEVMETHOD(bus_read_ivar, gt_read_ivar), 751 DEVMETHOD(bus_write_ivar, gt_write_ivar), 752 DEVMETHOD(bus_alloc_resource, gt_pci_alloc_resource), 753 DEVMETHOD(bus_release_resource, bus_generic_release_resource), 754 DEVMETHOD(bus_activate_resource, gt_pci_activate_resource), 755 DEVMETHOD(bus_deactivate_resource, bus_generic_deactivate_resource), 756 DEVMETHOD(bus_setup_intr, gt_pci_setup_intr), 757 DEVMETHOD(bus_teardown_intr, gt_pci_teardown_intr), 758 759 /* pcib interface */ 760 DEVMETHOD(pcib_maxslots, gt_pci_maxslots), 761 DEVMETHOD(pcib_read_config, gt_pci_read_config), 762 DEVMETHOD(pcib_write_config, gt_pci_write_config), 763 DEVMETHOD(pcib_route_interrupt, gt_pci_route_interrupt), 764 765 DEVMETHOD_END 766}; 767 768static driver_t gt_pci_driver = { 769 "pcib", 770 gt_pci_methods, 771 sizeof(struct gt_pci_softc), 772}; 773 774static devclass_t gt_pci_devclass; 775 776DRIVER_MODULE(gt_pci, gt, gt_pci_driver, gt_pci_devclass, 0, 0); 777