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