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$ 28 */ 29 30 #include <sys/param.h> 31 #include <sys/systm.h> 32 #include <sys/bus.h> 33 #include <sys/conf.h> 34 #include <sys/kernel.h> 35 36 #include <dev/ofw/openfirm.h> 37 #include <dev/ofw/ofw_pci.h> 38 39 #include <dev/pci/pcivar.h> 40 #include <dev/pci/pcireg.h> 41 42 #include <machine/bus.h> 43 #include <machine/md_var.h> 44 #include <machine/nexusvar.h> 45 #include <machine/resource.h> 46 47 #include <sys/rman.h> 48 49 #include <powerpc/ofw/ofw_pci.h> 50 #include <powerpc/powermac/gracklevar.h> 51 52 #include <vm/vm.h> 53 #include <vm/pmap.h> 54 55 #include "pcib_if.h" 56 57 /* 58 * Device interface. 59 */ 60 static int grackle_probe(device_t); 61 static int grackle_attach(device_t); 62 63 /* 64 * Bus interface. 65 */ 66 static int grackle_read_ivar(device_t, device_t, int, 67 uintptr_t *); 68 static struct resource * grackle_alloc_resource(device_t bus, 69 device_t child, int type, int *rid, u_long start, 70 u_long end, u_long count, u_int flags); 71 static int grackle_release_resource(device_t bus, device_t child, 72 int type, int rid, struct resource *res); 73 static int grackle_activate_resource(device_t bus, device_t child, 74 int type, int rid, struct resource *res); 75 static int grackle_deactivate_resource(device_t bus, 76 device_t child, int type, int rid, 77 struct resource *res); 78 79 80 /* 81 * pcib interface. 82 */ 83 static int grackle_maxslots(device_t); 84 static u_int32_t grackle_read_config(device_t, u_int, u_int, u_int, 85 u_int, int); 86 static void grackle_write_config(device_t, u_int, u_int, u_int, 87 u_int, u_int32_t, int); 88 static int grackle_route_interrupt(device_t, device_t, int); 89 90 /* 91 * Local routines. 92 */ 93 static int grackle_enable_config(struct grackle_softc *, u_int, 94 u_int, u_int, u_int); 95 static void grackle_disable_config(struct grackle_softc *); 96 97 /* 98 * Driver methods. 99 */ 100 static device_method_t grackle_methods[] = { 101 /* Device interface */ 102 DEVMETHOD(device_probe, grackle_probe), 103 DEVMETHOD(device_attach, grackle_attach), 104 105 /* Bus interface */ 106 DEVMETHOD(bus_print_child, bus_generic_print_child), 107 DEVMETHOD(bus_read_ivar, grackle_read_ivar), 108 DEVMETHOD(bus_setup_intr, bus_generic_setup_intr), 109 DEVMETHOD(bus_teardown_intr, bus_generic_teardown_intr), 110 DEVMETHOD(bus_alloc_resource, grackle_alloc_resource), 111 DEVMETHOD(bus_release_resource, grackle_release_resource), 112 DEVMETHOD(bus_activate_resource, grackle_activate_resource), 113 DEVMETHOD(bus_deactivate_resource, grackle_deactivate_resource), 114 115 /* pcib interface */ 116 DEVMETHOD(pcib_maxslots, grackle_maxslots), 117 DEVMETHOD(pcib_read_config, grackle_read_config), 118 DEVMETHOD(pcib_write_config, grackle_write_config), 119 DEVMETHOD(pcib_route_interrupt, grackle_route_interrupt), 120 121 { 0, 0 } 122 }; 123 124 static driver_t grackle_driver = { 125 "pcib", 126 grackle_methods, 127 sizeof(struct grackle_softc) 128 }; 129 130 static devclass_t grackle_devclass; 131 132 DRIVER_MODULE(grackle, nexus, grackle_driver, grackle_devclass, 0, 0); 133 134 static int 135 grackle_probe(device_t dev) 136 { 137 char *type, *compatible; 138 139 type = nexus_get_device_type(dev); 140 compatible = nexus_get_compatible(dev); 141 142 if (type == NULL || compatible == NULL) 143 return (ENXIO); 144 145 if (strcmp(type, "pci") != 0 || strcmp(compatible, "grackle") != 0) 146 return (ENXIO); 147 148 device_set_desc(dev, "MPC106 (Grackle) Host-PCI bridge"); 149 return (0); 150 } 151 152 static int 153 grackle_attach(device_t dev) 154 { 155 struct grackle_softc *sc; 156 phandle_t node; 157 u_int32_t busrange[2]; 158 struct grackle_range *rp, *io, *mem[2]; 159 int nmem, i; 160 161 node = nexus_get_node(dev); 162 sc = device_get_softc(dev); 163 164 if (OF_getprop(node, "bus-range", busrange, sizeof(busrange)) != 8) 165 return (ENXIO); 166 167 sc->sc_dev = dev; 168 sc->sc_node = node; 169 sc->sc_bus = busrange[0]; 170 171 /* 172 * The Grackle PCI config addr/data registers are actually in 173 * PCI space, but since they are needed to actually probe the 174 * PCI bus, use the fact that they are also available directly 175 * on the processor bus and map them 176 */ 177 sc->sc_addr = (vm_offset_t)pmap_mapdev(GRACKLE_ADDR, PAGE_SIZE); 178 sc->sc_data = (vm_offset_t)pmap_mapdev(GRACKLE_DATA, PAGE_SIZE); 179 180 bzero(sc->sc_range, sizeof(sc->sc_range)); 181 sc->sc_nrange = OF_getprop(node, "ranges", sc->sc_range, 182 sizeof(sc->sc_range)); 183 184 if (sc->sc_nrange == -1) { 185 device_printf(dev, "could not get ranges\n"); 186 return (ENXIO); 187 } 188 189 sc->sc_range[6].pci_hi = 0; 190 io = NULL; 191 nmem = 0; 192 193 for (rp = sc->sc_range; rp->pci_hi != 0; rp++) { 194 switch (rp->pci_hi & OFW_PCI_PHYS_HI_SPACEMASK) { 195 case OFW_PCI_PHYS_HI_SPACE_CONFIG: 196 break; 197 case OFW_PCI_PHYS_HI_SPACE_IO: 198 io = rp; 199 break; 200 case OFW_PCI_PHYS_HI_SPACE_MEM32: 201 mem[nmem] = rp; 202 nmem++; 203 break; 204 case OFW_PCI_PHYS_HI_SPACE_MEM64: 205 break; 206 } 207 } 208 209 if (io == NULL) { 210 device_printf(dev, "can't find io range\n"); 211 return (ENXIO); 212 } 213 sc->sc_io_rman.rm_type = RMAN_ARRAY; 214 sc->sc_io_rman.rm_descr = "Grackle PCI I/O Ports"; 215 sc->sc_iostart = io->pci_iospace; 216 if (rman_init(&sc->sc_io_rman) != 0 || 217 rman_manage_region(&sc->sc_io_rman, io->pci_lo, 218 io->pci_lo + io->size_lo) != 0) { 219 device_printf(dev, "failed to set up io range management\n"); 220 return (ENXIO); 221 } 222 223 if (nmem == 0) { 224 device_printf(dev, "can't find mem ranges\n"); 225 return (ENXIO); 226 } 227 sc->sc_mem_rman.rm_type = RMAN_ARRAY; 228 sc->sc_mem_rman.rm_descr = "Grackle PCI Memory"; 229 if (rman_init(&sc->sc_mem_rman) != 0) { 230 device_printf(dev, 231 "failed to init mem range resources\n"); 232 return (ENXIO); 233 } 234 for (i = 0; i < nmem; i++) { 235 if (rman_manage_region(&sc->sc_mem_rman, mem[i]->pci_lo, 236 mem[i]->pci_lo + mem[i]->size_lo) != 0) { 237 device_printf(dev, 238 "failed to set up memory range management\n"); 239 return (ENXIO); 240 } 241 } 242 243 /* 244 * Write out the correct PIC interrupt values to config space 245 * of all devices on the bus. 246 */ 247 ofw_pci_fixup(dev, sc->sc_bus, sc->sc_node); 248 249 device_add_child(dev, "pci", device_get_unit(dev)); 250 return (bus_generic_attach(dev)); 251 } 252 253 static int 254 grackle_maxslots(device_t dev) 255 { 256 257 return (PCI_SLOTMAX); 258 } 259 260 static u_int32_t 261 grackle_read_config(device_t dev, u_int bus, u_int slot, u_int func, u_int reg, 262 int width) 263 { 264 struct grackle_softc *sc; 265 vm_offset_t caoff; 266 u_int32_t retval = 0xffffffff; 267 268 sc = device_get_softc(dev); 269 caoff = sc->sc_data + (reg & 0x03); 270 271 if (grackle_enable_config(sc, bus, slot, func, reg) != 0) { 272 273 /* 274 * Config probes to non-existent devices on the 275 * secondary bus generates machine checks. Be sure 276 * to catch these. 277 */ 278 if (bus > 0) { 279 if (badaddr(sc->sc_data, 4)) { 280 return (retval); 281 } 282 } 283 284 switch (width) { 285 case 1: 286 retval = (in8rb(caoff)); 287 break; 288 case 2: 289 retval = (in16rb(caoff)); 290 break; 291 case 4: 292 retval = (in32rb(caoff)); 293 break; 294 } 295 } 296 grackle_disable_config(sc); 297 298 return (retval); 299 } 300 301 static void 302 grackle_write_config(device_t dev, u_int bus, u_int slot, u_int func, 303 u_int reg, u_int32_t val, int width) 304 { 305 struct grackle_softc *sc; 306 vm_offset_t caoff; 307 308 sc = device_get_softc(dev); 309 caoff = sc->sc_data + (reg & 0x03); 310 311 if (grackle_enable_config(sc, bus, slot, func, reg)) { 312 switch (width) { 313 case 1: 314 out8rb(caoff, val); 315 (void)in8rb(caoff); 316 break; 317 case 2: 318 out16rb(caoff, val); 319 (void)in16rb(caoff); 320 break; 321 case 4: 322 out32rb(caoff, val); 323 (void)in32rb(caoff); 324 break; 325 } 326 } 327 grackle_disable_config(sc); 328 } 329 330 static int 331 grackle_route_interrupt(device_t bus, device_t dev, int pin) 332 { 333 334 return (0); 335 } 336 337 static int 338 grackle_read_ivar(device_t dev, device_t child, int which, uintptr_t *result) 339 { 340 struct grackle_softc *sc; 341 342 sc = device_get_softc(dev); 343 344 switch (which) { 345 case PCIB_IVAR_BUS: 346 *result = sc->sc_bus; 347 return (0); 348 break; 349 } 350 351 return (ENOENT); 352 } 353 354 static struct resource * 355 grackle_alloc_resource(device_t bus, device_t child, int type, int *rid, 356 u_long start, u_long end, u_long count, u_int flags) 357 { 358 struct grackle_softc *sc; 359 struct resource *rv; 360 struct rman *rm; 361 bus_space_tag_t bt; 362 int needactivate; 363 364 needactivate = flags & RF_ACTIVE; 365 flags &= ~RF_ACTIVE; 366 367 sc = device_get_softc(bus); 368 369 switch (type) { 370 case SYS_RES_MEMORY: 371 rm = &sc->sc_mem_rman; 372 bt = PPC_BUS_SPACE_MEM; 373 break; 374 375 case SYS_RES_IOPORT: 376 rm = &sc->sc_io_rman; 377 bt = PPC_BUS_SPACE_IO; 378 break; 379 380 case SYS_RES_IRQ: 381 return (bus_alloc_resource(bus, type, rid, start, end, count, 382 flags)); 383 break; 384 385 default: 386 device_printf(bus, "unknown resource request from %s\n", 387 device_get_nameunit(child)); 388 return (NULL); 389 } 390 391 rv = rman_reserve_resource(rm, start, end, count, flags, child); 392 if (rv == NULL) { 393 device_printf(bus, "failed to reserve resource for %s\n", 394 device_get_nameunit(child)); 395 return (NULL); 396 } 397 398 rman_set_bustag(rv, bt); 399 rman_set_bushandle(rv, rman_get_start(rv)); 400 401 if (needactivate) { 402 if (bus_activate_resource(child, type, *rid, rv) != 0) { 403 device_printf(bus, 404 "failed to activate resource for %s\n", 405 device_get_nameunit(child)); 406 rman_release_resource(rv); 407 return (NULL); 408 } 409 } 410 411 return (rv); 412 } 413 414 static int 415 grackle_release_resource(device_t bus, device_t child, int type, int rid, 416 struct resource *res) 417 { 418 if (rman_get_flags(res) & RF_ACTIVE) { 419 int error = bus_deactivate_resource(child, type, rid, res); 420 if (error) 421 return error; 422 } 423 424 return (rman_release_resource(res)); 425 } 426 427 static int 428 grackle_activate_resource(device_t bus, device_t child, int type, int rid, 429 struct resource *res) 430 { 431 struct grackle_softc *sc; 432 void *p; 433 434 sc = device_get_softc(bus); 435 436 if (type == SYS_RES_IRQ) { 437 return (bus_activate_resource(bus, type, rid, res)); 438 } 439 if (type == SYS_RES_MEMORY || type == SYS_RES_IOPORT) { 440 vm_offset_t start; 441 442 start = (vm_offset_t)rman_get_start(res); 443 /* 444 * For i/o-ports, convert the start address to the 445 * MPC106 PCI i/o window 446 */ 447 if (type == SYS_RES_IOPORT) 448 start += sc->sc_iostart; 449 450 if (bootverbose) 451 printf("grackle mapdev: start %x, len %x\n", start, 452 rman_get_size(res)); 453 454 p = pmap_mapdev(start, (vm_size_t)rman_get_size(res)); 455 if (p == NULL) 456 return (ENOMEM); 457 458 rman_set_virtual(res, p); 459 rman_set_bushandle(res, (u_long)p); 460 } 461 462 return (rman_activate_resource(res)); 463 } 464 465 static int 466 grackle_deactivate_resource(device_t bus, device_t child, int type, int rid, 467 struct resource *res) 468 { 469 /* 470 * If this is a memory resource, unmap it. 471 */ 472 if ((type == SYS_RES_MEMORY) || (type == SYS_RES_IOPORT)) { 473 u_int32_t psize; 474 475 psize = rman_get_size(res); 476 pmap_unmapdev((vm_offset_t)rman_get_virtual(res), psize); 477 } 478 479 return (rman_deactivate_resource(res)); 480 } 481 482 483 static int 484 grackle_enable_config(struct grackle_softc *sc, u_int bus, u_int slot, 485 u_int func, u_int reg) 486 { 487 u_int32_t cfgval; 488 489 /* 490 * Unlike UniNorth, the format of the config word is the same 491 * for local (0) and remote busses. 492 */ 493 cfgval = (bus << 16) | (slot << 11) | (func << 8) | (reg & 0xFC) 494 | GRACKLE_CFG_ENABLE; 495 496 out32rb(sc->sc_addr, cfgval); 497 (void) in32rb(sc->sc_addr); 498 499 return (1); 500 } 501 502 static void 503 grackle_disable_config(struct grackle_softc *sc) 504 { 505 /* 506 * Clear the GRACKLE_CFG_ENABLE bit to prevent stray 507 * accesses from causing config cycles 508 */ 509 out32rb(sc->sc_addr, 0); 510 } 511 512 /* 513 * Driver to swallow Grackle host bridges from the PCI bus side. 514 * Same as the UniNorth 'swallower', so whoever probes first will win out. 515 * Maybe the UniNorth/Grackle code should be consolidated. 516 */ 517 static int 518 grackle_hb_probe(device_t dev) 519 { 520 521 if (pci_get_class(dev) == PCIC_BRIDGE && 522 pci_get_subclass(dev) == PCIS_BRIDGE_HOST) { 523 device_set_desc(dev, "Host to PCI bridge"); 524 device_quiet(dev); 525 return (-10000); 526 } 527 528 return (ENXIO); 529 } 530 531 static int 532 grackle_hb_attach(device_t dev) 533 { 534 535 return (0); 536 } 537 538 static device_method_t grackle_hb_methods[] = { 539 /* Device interface */ 540 DEVMETHOD(device_probe, grackle_hb_probe), 541 DEVMETHOD(device_attach, grackle_hb_attach), 542 543 { 0, 0 } 544 }; 545 546 static driver_t grackle_hb_driver = { 547 "grackle_hb", 548 grackle_hb_methods, 549 1, 550 }; 551 static devclass_t grackle_hb_devclass; 552 553 DRIVER_MODULE(grackle_hb, pci, grackle_hb_driver, grackle_hb_devclass, 0, 0); 554