grackle.c revision 186128
1/*- 2 * Copyright 2003 by Peter Grehan. All rights reserved. 3 * 4 * Redistribution and use in source and binary forms, with or without 5 * modification, are permitted provided that the following conditions 6 * are met: 7 * 1. Redistributions of source code must retain the above copyright 8 * notice, this list of conditions and the following disclaimer. 9 * 2. Redistributions in binary form must reproduce the above copyright 10 * notice, this list of conditions and the following disclaimer in the 11 * documentation and/or other materials provided with the distribution. 12 * 3. The name of the author may not be used to endorse or promote products 13 * derived from this software without specific prior written permission. 14 * 15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 16 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 17 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 18 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 19 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 20 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 21 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED 22 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 23 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 24 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 25 * SUCH DAMAGE. 26 * 27 * $FreeBSD: head/sys/powerpc/powermac/grackle.c 186128 2008-12-15 15:31:10Z nwhitehorn $ 28 */ 29 30#include <sys/param.h> 31#include <sys/systm.h> 32#include <sys/module.h> 33#include <sys/bus.h> 34#include <sys/conf.h> 35#include <sys/kernel.h> 36 37#include <dev/ofw/openfirm.h> 38#include <dev/ofw/ofw_pci.h> 39#include <dev/ofw/ofw_bus.h> 40#include <dev/ofw/ofw_bus_subr.h> 41 42#include <dev/pci/pcivar.h> 43#include <dev/pci/pcireg.h> 44 45#include <machine/bus.h> 46#include <machine/md_var.h> 47#include <machine/pio.h> 48#include <machine/resource.h> 49 50#include <sys/rman.h> 51 52#include <powerpc/powermac/gracklevar.h> 53 54#include <vm/vm.h> 55#include <vm/pmap.h> 56 57#include "pcib_if.h" 58 59int badaddr(void *, size_t); /* XXX */ 60 61/* 62 * Device interface. 63 */ 64static int grackle_probe(device_t); 65static int grackle_attach(device_t); 66 67/* 68 * Bus interface. 69 */ 70static int grackle_read_ivar(device_t, device_t, int, 71 uintptr_t *); 72static struct resource * grackle_alloc_resource(device_t bus, 73 device_t child, int type, int *rid, u_long start, 74 u_long end, u_long count, u_int flags); 75static int grackle_release_resource(device_t bus, device_t child, 76 int type, int rid, struct resource *res); 77static int grackle_activate_resource(device_t bus, device_t child, 78 int type, int rid, struct resource *res); 79static int grackle_deactivate_resource(device_t bus, 80 device_t child, int type, int rid, 81 struct resource *res); 82 83 84/* 85 * pcib interface. 86 */ 87static int grackle_maxslots(device_t); 88static u_int32_t grackle_read_config(device_t, u_int, u_int, u_int, 89 u_int, int); 90static void grackle_write_config(device_t, u_int, u_int, u_int, 91 u_int, u_int32_t, int); 92static int grackle_route_interrupt(device_t, device_t, int); 93 94/* 95 * ofw_bus interface 96 */ 97static phandle_t grackle_get_node(device_t bus, device_t dev); 98 99/* 100 * Local routines. 101 */ 102static int grackle_enable_config(struct grackle_softc *, u_int, 103 u_int, u_int, u_int); 104static void grackle_disable_config(struct grackle_softc *); 105 106/* 107 * Driver methods. 108 */ 109static device_method_t grackle_methods[] = { 110 /* Device interface */ 111 DEVMETHOD(device_probe, grackle_probe), 112 DEVMETHOD(device_attach, grackle_attach), 113 114 /* Bus interface */ 115 DEVMETHOD(bus_print_child, bus_generic_print_child), 116 DEVMETHOD(bus_read_ivar, grackle_read_ivar), 117 DEVMETHOD(bus_setup_intr, bus_generic_setup_intr), 118 DEVMETHOD(bus_teardown_intr, bus_generic_teardown_intr), 119 DEVMETHOD(bus_alloc_resource, grackle_alloc_resource), 120 DEVMETHOD(bus_release_resource, grackle_release_resource), 121 DEVMETHOD(bus_activate_resource, grackle_activate_resource), 122 DEVMETHOD(bus_deactivate_resource, grackle_deactivate_resource), 123 124 /* pcib interface */ 125 DEVMETHOD(pcib_maxslots, grackle_maxslots), 126 DEVMETHOD(pcib_read_config, grackle_read_config), 127 DEVMETHOD(pcib_write_config, grackle_write_config), 128 DEVMETHOD(pcib_route_interrupt, grackle_route_interrupt), 129 130 /* ofw_bus interface */ 131 DEVMETHOD(ofw_bus_get_node, grackle_get_node), 132 133 { 0, 0 } 134}; 135 136static driver_t grackle_driver = { 137 "pcib", 138 grackle_methods, 139 sizeof(struct grackle_softc) 140}; 141 142static devclass_t grackle_devclass; 143 144DRIVER_MODULE(grackle, nexus, grackle_driver, grackle_devclass, 0, 0); 145 146static int 147grackle_probe(device_t dev) 148{ 149 const char *type, *compatible; 150 151 type = ofw_bus_get_type(dev); 152 compatible = ofw_bus_get_compat(dev); 153 154 if (type == NULL || compatible == NULL) 155 return (ENXIO); 156 157 if (strcmp(type, "pci") != 0 || strcmp(compatible, "grackle") != 0) 158 return (ENXIO); 159 160 device_set_desc(dev, "MPC106 (Grackle) Host-PCI bridge"); 161 return (0); 162} 163 164static int 165grackle_attach(device_t dev) 166{ 167 struct grackle_softc *sc; 168 phandle_t node, iparent; 169 u_int32_t busrange[2]; 170 struct grackle_range *rp, *io, *mem[2]; 171 int nmem, i, error; 172 173 node = ofw_bus_get_node(dev); 174 sc = device_get_softc(dev); 175 176 if (OF_getprop(node, "bus-range", busrange, sizeof(busrange)) != 8) 177 return (ENXIO); 178 179 sc->sc_dev = dev; 180 sc->sc_node = node; 181 sc->sc_bus = busrange[0]; 182 183 /* 184 * The Grackle PCI config addr/data registers are actually in 185 * PCI space, but since they are needed to actually probe the 186 * PCI bus, use the fact that they are also available directly 187 * on the processor bus and map them 188 */ 189 sc->sc_addr = (vm_offset_t)pmap_mapdev(GRACKLE_ADDR, PAGE_SIZE); 190 sc->sc_data = (vm_offset_t)pmap_mapdev(GRACKLE_DATA, PAGE_SIZE); 191 192 bzero(sc->sc_range, sizeof(sc->sc_range)); 193 sc->sc_nrange = OF_getprop(node, "ranges", sc->sc_range, 194 sizeof(sc->sc_range)); 195 196 if (sc->sc_nrange == -1) { 197 device_printf(dev, "could not get ranges\n"); 198 return (ENXIO); 199 } 200 201 sc->sc_range[6].pci_hi = 0; 202 io = NULL; 203 nmem = 0; 204 205 for (rp = sc->sc_range; rp->pci_hi != 0; rp++) { 206 switch (rp->pci_hi & OFW_PCI_PHYS_HI_SPACEMASK) { 207 case OFW_PCI_PHYS_HI_SPACE_CONFIG: 208 break; 209 case OFW_PCI_PHYS_HI_SPACE_IO: 210 io = rp; 211 break; 212 case OFW_PCI_PHYS_HI_SPACE_MEM32: 213 mem[nmem] = rp; 214 nmem++; 215 break; 216 case OFW_PCI_PHYS_HI_SPACE_MEM64: 217 break; 218 } 219 } 220 221 if (io == NULL) { 222 device_printf(dev, "can't find io range\n"); 223 return (ENXIO); 224 } 225 sc->sc_io_rman.rm_type = RMAN_ARRAY; 226 sc->sc_io_rman.rm_descr = "Grackle PCI I/O Ports"; 227 sc->sc_iostart = io->pci_iospace; 228 if (rman_init(&sc->sc_io_rman) != 0 || 229 rman_manage_region(&sc->sc_io_rman, io->pci_lo, 230 io->pci_lo + io->size_lo) != 0) { 231 panic("grackle_attach: failed to set up I/O rman"); 232 } 233 234 if (nmem == 0) { 235 device_printf(dev, "can't find mem ranges\n"); 236 return (ENXIO); 237 } 238 sc->sc_mem_rman.rm_type = RMAN_ARRAY; 239 sc->sc_mem_rman.rm_descr = "Grackle PCI Memory"; 240 error = rman_init(&sc->sc_mem_rman); 241 if (error) { 242 device_printf(dev, "rman_init() failed. error = %d\n", error); 243 return (error); 244 } 245 for (i = 0; i < nmem; i++) { 246 error = rman_manage_region(&sc->sc_mem_rman, mem[i]->pci_lo, 247 mem[i]->pci_lo + mem[i]->size_lo); 248 if (error) { 249 device_printf(dev, 250 "rman_manage_region() failed. error = %d\n", error); 251 return (error); 252 } 253 } 254 255 ofw_bus_setup_iinfo(node, &sc->sc_pci_iinfo, sizeof(cell_t)); 256 257 /* We need the number of interrupt cells to read the imap */ 258 if (OF_getprop(node, "interrupt-parent", &iparent,sizeof(iparent)) <= 0) 259 iparent = node; 260 261 if (OF_getprop(iparent,"#interrupt-cells",&sc->sc_icells, 262 sizeof(sc->sc_icells)) <= 0) 263 sc->sc_icells = 1; 264 265 device_add_child(dev, "pci", device_get_unit(dev)); 266 return (bus_generic_attach(dev)); 267} 268 269static int 270grackle_maxslots(device_t dev) 271{ 272 273 return (PCI_SLOTMAX); 274} 275 276static u_int32_t 277grackle_read_config(device_t dev, u_int bus, u_int slot, u_int func, u_int reg, 278 int width) 279{ 280 struct grackle_softc *sc; 281 vm_offset_t caoff; 282 u_int32_t retval = 0xffffffff; 283 284 sc = device_get_softc(dev); 285 caoff = sc->sc_data + (reg & 0x03); 286 287 if (grackle_enable_config(sc, bus, slot, func, reg) != 0) { 288 289 /* 290 * Config probes to non-existent devices on the 291 * secondary bus generates machine checks. Be sure 292 * to catch these. 293 */ 294 if (bus > 0) { 295 if (badaddr((void *)sc->sc_data, 4)) { 296 return (retval); 297 } 298 } 299 300 switch (width) { 301 case 1: 302 retval = (in8rb(caoff)); 303 break; 304 case 2: 305 retval = (in16rb(caoff)); 306 break; 307 case 4: 308 retval = (in32rb(caoff)); 309 break; 310 } 311 } 312 grackle_disable_config(sc); 313 314 return (retval); 315} 316 317static void 318grackle_write_config(device_t dev, u_int bus, u_int slot, u_int func, 319 u_int reg, u_int32_t val, int width) 320{ 321 struct grackle_softc *sc; 322 vm_offset_t caoff; 323 324 sc = device_get_softc(dev); 325 caoff = sc->sc_data + (reg & 0x03); 326 327 if (grackle_enable_config(sc, bus, slot, func, reg)) { 328 switch (width) { 329 case 1: 330 out8rb(caoff, val); 331 (void)in8rb(caoff); 332 break; 333 case 2: 334 out16rb(caoff, val); 335 (void)in16rb(caoff); 336 break; 337 case 4: 338 out32rb(caoff, val); 339 (void)in32rb(caoff); 340 break; 341 } 342 } 343 grackle_disable_config(sc); 344} 345 346static int 347grackle_route_interrupt(device_t bus, device_t dev, int pin) 348{ 349 struct grackle_softc *sc; 350 struct ofw_pci_register reg; 351 uint32_t pintr, mintr[2]; 352 uint8_t maskbuf[sizeof(reg) + sizeof(pintr)]; 353 354 sc = device_get_softc(bus); 355 pintr = pin; 356 if (ofw_bus_lookup_imap(ofw_bus_get_node(dev), &sc->sc_pci_iinfo, ®, 357 sizeof(reg), &pintr, sizeof(pintr), &mintr, 358 sizeof(mintr[0])*sc->sc_icells, maskbuf)) 359 return (mintr[0]); 360 361 /* Maybe it's a real interrupt, not an intpin */ 362 if (pin > 4) 363 return (pin); 364 365 device_printf(bus, "could not route pin %d for device %d.%d\n", 366 pin, pci_get_slot(dev), pci_get_function(dev)); 367 return (PCI_INVALID_IRQ); 368} 369 370static int 371grackle_read_ivar(device_t dev, device_t child, int which, uintptr_t *result) 372{ 373 struct grackle_softc *sc; 374 375 sc = device_get_softc(dev); 376 377 switch (which) { 378 case PCIB_IVAR_DOMAIN: 379 *result = 0; 380 return (0); 381 case PCIB_IVAR_BUS: 382 *result = sc->sc_bus; 383 return (0); 384 } 385 386 return (ENOENT); 387} 388 389static struct resource * 390grackle_alloc_resource(device_t bus, device_t child, int type, int *rid, 391 u_long start, u_long end, u_long count, u_int flags) 392{ 393 struct grackle_softc *sc; 394 struct resource *rv; 395 struct rman *rm; 396 int needactivate; 397 398 needactivate = flags & RF_ACTIVE; 399 flags &= ~RF_ACTIVE; 400 401 sc = device_get_softc(bus); 402 403 switch (type) { 404 case SYS_RES_MEMORY: 405 rm = &sc->sc_mem_rman; 406 break; 407 408 case SYS_RES_IOPORT: 409 rm = &sc->sc_io_rman; 410 break; 411 412 case SYS_RES_IRQ: 413 return (bus_alloc_resource(bus, type, rid, start, end, count, 414 flags)); 415 416 default: 417 device_printf(bus, "unknown resource request from %s\n", 418 device_get_nameunit(child)); 419 return (NULL); 420 } 421 422 rv = rman_reserve_resource(rm, start, end, count, flags, child); 423 if (rv == NULL) { 424 device_printf(bus, "failed to reserve resource for %s\n", 425 device_get_nameunit(child)); 426 return (NULL); 427 } 428 429 rman_set_rid(rv, *rid); 430 431 if (needactivate) { 432 if (bus_activate_resource(child, type, *rid, rv) != 0) { 433 device_printf(bus, 434 "failed to activate resource for %s\n", 435 device_get_nameunit(child)); 436 rman_release_resource(rv); 437 return (NULL); 438 } 439 } 440 441 return (rv); 442} 443 444static int 445grackle_release_resource(device_t bus, device_t child, int type, int rid, 446 struct resource *res) 447{ 448 if (rman_get_flags(res) & RF_ACTIVE) { 449 int error = bus_deactivate_resource(child, type, rid, res); 450 if (error) 451 return error; 452 } 453 454 return (rman_release_resource(res)); 455} 456 457static int 458grackle_activate_resource(device_t bus, device_t child, int type, int rid, 459 struct resource *res) 460{ 461 struct grackle_softc *sc; 462 void *p; 463 464 sc = device_get_softc(bus); 465 466 if (type == SYS_RES_IRQ) { 467 return (bus_activate_resource(bus, type, rid, res)); 468 } 469 if (type == SYS_RES_MEMORY || type == SYS_RES_IOPORT) { 470 vm_offset_t start; 471 472 start = (vm_offset_t)rman_get_start(res); 473 /* 474 * For i/o-ports, convert the start address to the 475 * MPC106 PCI i/o window 476 */ 477 if (type == SYS_RES_IOPORT) 478 start += sc->sc_iostart; 479 480 if (bootverbose) 481 printf("grackle mapdev: start %x, len %ld\n", start, 482 rman_get_size(res)); 483 484 p = pmap_mapdev(start, (vm_size_t)rman_get_size(res)); 485 if (p == NULL) 486 return (ENOMEM); 487 488 rman_set_virtual(res, p); 489 rman_set_bustag(res, &bs_le_tag); 490 rman_set_bushandle(res, (u_long)p); 491 } 492 493 return (rman_activate_resource(res)); 494} 495 496static int 497grackle_deactivate_resource(device_t bus, device_t child, int type, int rid, 498 struct resource *res) 499{ 500 /* 501 * If this is a memory resource, unmap it. 502 */ 503 if ((type == SYS_RES_MEMORY) || (type == SYS_RES_IOPORT)) { 504 u_int32_t psize; 505 506 psize = rman_get_size(res); 507 pmap_unmapdev((vm_offset_t)rman_get_virtual(res), psize); 508 } 509 510 return (rman_deactivate_resource(res)); 511} 512 513 514static int 515grackle_enable_config(struct grackle_softc *sc, u_int bus, u_int slot, 516 u_int func, u_int reg) 517{ 518 u_int32_t cfgval; 519 520 /* 521 * Unlike UniNorth, the format of the config word is the same 522 * for local (0) and remote busses. 523 */ 524 cfgval = (bus << 16) | (slot << 11) | (func << 8) | (reg & 0xFC) 525 | GRACKLE_CFG_ENABLE; 526 527 out32rb(sc->sc_addr, cfgval); 528 (void) in32rb(sc->sc_addr); 529 530 return (1); 531} 532 533static void 534grackle_disable_config(struct grackle_softc *sc) 535{ 536 /* 537 * Clear the GRACKLE_CFG_ENABLE bit to prevent stray 538 * accesses from causing config cycles 539 */ 540 out32rb(sc->sc_addr, 0); 541} 542 543static phandle_t 544grackle_get_node(device_t bus, device_t dev) 545{ 546 struct grackle_softc *sc; 547 548 sc = device_get_softc(bus); 549 /* We only have one child, the PCI bus, which needs our own node. */ 550 551 return sc->sc_node; 552} 553 554/* 555 * Driver to swallow Grackle host bridges from the PCI bus side. 556 */ 557static int 558grackle_hb_probe(device_t dev) 559{ 560 561 if (pci_get_devid(dev) == 0x00021057) { 562 device_set_desc(dev, "Grackle Host to PCI bridge"); 563 device_quiet(dev); 564 return (0); 565 } 566 567 return (ENXIO); 568} 569 570static int 571grackle_hb_attach(device_t dev) 572{ 573 574 return (0); 575} 576 577static device_method_t grackle_hb_methods[] = { 578 /* Device interface */ 579 DEVMETHOD(device_probe, grackle_hb_probe), 580 DEVMETHOD(device_attach, grackle_hb_attach), 581 582 { 0, 0 } 583}; 584 585static driver_t grackle_hb_driver = { 586 "grackle_hb", 587 grackle_hb_methods, 588 1, 589}; 590static devclass_t grackle_hb_devclass; 591 592DRIVER_MODULE(grackle_hb, pci, grackle_hb_driver, grackle_hb_devclass, 0, 0); 593