macio.c revision 186128
1/*- 2 * Copyright 2002 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/macio.c 186128 2008-12-15 15:31:10Z nwhitehorn $ 28 */ 29 30/* 31 * Driver for KeyLargo/Pangea, the MacPPC south bridge ASIC. 32 */ 33 34#include <sys/param.h> 35#include <sys/systm.h> 36#include <sys/kernel.h> 37#include <sys/malloc.h> 38#include <sys/module.h> 39#include <sys/bus.h> 40#include <machine/bus.h> 41#include <sys/rman.h> 42 43#include <machine/vmparam.h> 44#include <vm/vm.h> 45#include <vm/pmap.h> 46#include <machine/pmap.h> 47 48#include <machine/resource.h> 49 50#include <dev/ofw/ofw_bus.h> 51#include <dev/ofw/ofw_bus_subr.h> 52#include <dev/ofw/openfirm.h> 53 54#include <powerpc/powermac/maciovar.h> 55 56#include <dev/pci/pcivar.h> 57#include <dev/pci/pcireg.h> 58 59/* 60 * Macio softc 61 */ 62struct macio_softc { 63 phandle_t sc_node; 64 vm_offset_t sc_base; 65 vm_offset_t sc_size; 66 struct rman sc_mem_rman; 67}; 68 69static MALLOC_DEFINE(M_MACIO, "macio", "macio device information"); 70 71static int macio_probe(device_t); 72static int macio_attach(device_t); 73static int macio_print_child(device_t dev, device_t child); 74static void macio_probe_nomatch(device_t, device_t); 75static struct resource *macio_alloc_resource(device_t, device_t, int, int *, 76 u_long, u_long, u_long, u_int); 77static int macio_activate_resource(device_t, device_t, int, int, 78 struct resource *); 79static int macio_deactivate_resource(device_t, device_t, int, int, 80 struct resource *); 81static int macio_release_resource(device_t, device_t, int, int, 82 struct resource *); 83static struct resource_list *macio_get_resource_list (device_t, device_t); 84static ofw_bus_get_devinfo_t macio_get_devinfo; 85 86/* 87 * Bus interface definition 88 */ 89static device_method_t macio_methods[] = { 90 /* Device interface */ 91 DEVMETHOD(device_probe, macio_probe), 92 DEVMETHOD(device_attach, macio_attach), 93 DEVMETHOD(device_detach, bus_generic_detach), 94 DEVMETHOD(device_shutdown, bus_generic_shutdown), 95 DEVMETHOD(device_suspend, bus_generic_suspend), 96 DEVMETHOD(device_resume, bus_generic_resume), 97 98 /* Bus interface */ 99 DEVMETHOD(bus_print_child, macio_print_child), 100 DEVMETHOD(bus_probe_nomatch, macio_probe_nomatch), 101 DEVMETHOD(bus_setup_intr, bus_generic_setup_intr), 102 DEVMETHOD(bus_teardown_intr, bus_generic_teardown_intr), 103 104 DEVMETHOD(bus_alloc_resource, macio_alloc_resource), 105 DEVMETHOD(bus_release_resource, macio_release_resource), 106 DEVMETHOD(bus_activate_resource, macio_activate_resource), 107 DEVMETHOD(bus_deactivate_resource, macio_deactivate_resource), 108 DEVMETHOD(bus_get_resource_list, macio_get_resource_list), 109 110 DEVMETHOD(bus_child_pnpinfo_str, ofw_bus_gen_child_pnpinfo_str), 111 112 /* ofw_bus interface */ 113 DEVMETHOD(ofw_bus_get_devinfo, macio_get_devinfo), 114 DEVMETHOD(ofw_bus_get_compat, ofw_bus_gen_get_compat), 115 DEVMETHOD(ofw_bus_get_model, ofw_bus_gen_get_model), 116 DEVMETHOD(ofw_bus_get_name, ofw_bus_gen_get_name), 117 DEVMETHOD(ofw_bus_get_node, ofw_bus_gen_get_node), 118 DEVMETHOD(ofw_bus_get_type, ofw_bus_gen_get_type), 119 120 { 0, 0 } 121}; 122 123static driver_t macio_pci_driver = { 124 "macio", 125 macio_methods, 126 sizeof(struct macio_softc) 127}; 128 129devclass_t macio_devclass; 130 131DRIVER_MODULE(macio, pci, macio_pci_driver, macio_devclass, 0, 0); 132 133/* 134 * PCI ID search table 135 */ 136static struct macio_pci_dev { 137 u_int32_t mpd_devid; 138 char *mpd_desc; 139} macio_pci_devlist[] = { 140 { 0x0017106b, "Paddington I/O Controller" }, 141 { 0x0022106b, "KeyLargo I/O Controller" }, 142 { 0x0025106b, "Pangea I/O Controller" }, 143 { 0x003e106b, "Intrepid I/O Controller" }, 144 { 0x0041106b, "K2 KeyLargo I/O Controller" }, 145 { 0x004f106b, "Shasta I/O Controller" }, 146 { 0, NULL } 147}; 148 149/* 150 * Devices to exclude from the probe 151 * XXX some of these may be required in the future... 152 */ 153#define MACIO_QUIRK_IGNORE 0x00000001 154#define MACIO_QUIRK_CHILD_HAS_INTR 0x00000002 155 156struct macio_quirk_entry { 157 const char *mq_name; 158 int mq_quirks; 159}; 160 161static struct macio_quirk_entry macio_quirks[] = { 162 { "escc-legacy", MACIO_QUIRK_IGNORE }, 163 { "timer", MACIO_QUIRK_IGNORE }, 164 { "escc", MACIO_QUIRK_CHILD_HAS_INTR }, 165 { NULL, 0 } 166}; 167 168static int 169macio_get_quirks(const char *name) 170{ 171 struct macio_quirk_entry *mqe; 172 173 for (mqe = macio_quirks; mqe->mq_name != NULL; mqe++) 174 if (strcmp(name, mqe->mq_name) == 0) 175 return (mqe->mq_quirks); 176 return (0); 177} 178 179 180/* 181 * Add an interrupt to the dev's resource list if present 182 */ 183static void 184macio_add_intr(phandle_t devnode, struct macio_devinfo *dinfo) 185{ 186 int *intr; 187 int i, nintr; 188 phandle_t iparent; 189 int icells; 190 191 if (dinfo->mdi_ninterrupts >= 6) { 192 printf("macio: device has more than 6 interrupts\n"); 193 return; 194 } 195 196 icells = 1; 197 198 if (OF_getprop(devnode, "interrupt-parent", &iparent, sizeof(iparent)) == sizeof(iparent)) 199 OF_getprop(iparent, "#interrupt-cells", &icells, sizeof(icells)); 200 201 nintr = OF_getprop_alloc(devnode, "interrupts", sizeof(*intr), 202 (void **)&intr); 203 if (nintr == -1) { 204 nintr = OF_getprop_alloc(devnode, "AAPL,interrupts", 205 sizeof(*intr), (void **)&intr); 206 if (nintr == -1) 207 return; 208 } 209 210 if (intr[0] == -1) 211 return; 212 213 for (i = 0; i < nintr; i+=icells) { 214 resource_list_add(&dinfo->mdi_resources, SYS_RES_IRQ, 215 dinfo->mdi_ninterrupts, intr[i], intr[i], 1); 216 217 dinfo->mdi_interrupts[dinfo->mdi_ninterrupts] = intr[i]; 218 dinfo->mdi_ninterrupts++; 219 } 220} 221 222 223static void 224macio_add_reg(phandle_t devnode, struct macio_devinfo *dinfo) 225{ 226 struct macio_reg *reg; 227 int i, nreg; 228 229 nreg = OF_getprop_alloc(devnode, "reg", sizeof(*reg), (void **)®); 230 if (nreg == -1) 231 return; 232 233 for (i = 0; i < nreg; i++) { 234 resource_list_add(&dinfo->mdi_resources, SYS_RES_MEMORY, i, 235 reg[i].mr_base, reg[i].mr_base + reg[i].mr_size, 236 reg[i].mr_size); 237 } 238} 239 240/* 241 * PCI probe 242 */ 243static int 244macio_probe(device_t dev) 245{ 246 int i; 247 u_int32_t devid; 248 249 devid = pci_get_devid(dev); 250 for (i = 0; macio_pci_devlist[i].mpd_desc != NULL; i++) { 251 if (devid == macio_pci_devlist[i].mpd_devid) { 252 device_set_desc(dev, macio_pci_devlist[i].mpd_desc); 253 return (0); 254 } 255 } 256 257 return (ENXIO); 258} 259 260/* 261 * PCI attach: scan Open Firmware child nodes, and attach these as children 262 * of the macio bus 263 */ 264static int 265macio_attach(device_t dev) 266{ 267 struct macio_softc *sc; 268 struct macio_devinfo *dinfo; 269 phandle_t root; 270 phandle_t child; 271 phandle_t subchild; 272 device_t cdev; 273 u_int reg[3]; 274 int error, quirks; 275 276 sc = device_get_softc(dev); 277 root = sc->sc_node = ofw_bus_get_node(dev); 278 279 /* 280 * Locate the device node and it's base address 281 */ 282 if (OF_getprop(root, "assigned-addresses", 283 reg, sizeof(reg)) < sizeof(reg)) { 284 return (ENXIO); 285 } 286 287 sc->sc_base = reg[2]; 288 sc->sc_size = MACIO_REG_SIZE; 289 290 sc->sc_mem_rman.rm_type = RMAN_ARRAY; 291 sc->sc_mem_rman.rm_descr = "MacIO Device Memory"; 292 error = rman_init(&sc->sc_mem_rman); 293 if (error) { 294 device_printf(dev, "rman_init() failed. error = %d\n", error); 295 return (error); 296 } 297 error = rman_manage_region(&sc->sc_mem_rman, 0, sc->sc_size); 298 if (error) { 299 device_printf(dev, 300 "rman_manage_region() failed. error = %d\n", error); 301 return (error); 302 } 303 304 /* 305 * Iterate through the sub-devices 306 */ 307 for (child = OF_child(root); child != 0; child = OF_peer(child)) { 308 dinfo = malloc(sizeof(*dinfo), M_MACIO, M_WAITOK | M_ZERO); 309 if (ofw_bus_gen_setup_devinfo(&dinfo->mdi_obdinfo, child) != 310 0) { 311 free(dinfo, M_MACIO); 312 continue; 313 } 314 quirks = macio_get_quirks(dinfo->mdi_obdinfo.obd_name); 315 if ((quirks & MACIO_QUIRK_IGNORE) != 0) { 316 ofw_bus_gen_destroy_devinfo(&dinfo->mdi_obdinfo); 317 free(dinfo, M_MACIO); 318 continue; 319 } 320 resource_list_init(&dinfo->mdi_resources); 321 dinfo->mdi_ninterrupts = 0; 322 macio_add_intr(child, dinfo); 323 macio_add_reg(child, dinfo); 324 if ((quirks & MACIO_QUIRK_CHILD_HAS_INTR) != 0) 325 for (subchild = OF_child(child); subchild != 0; 326 subchild = OF_peer(subchild)) 327 macio_add_intr(subchild, dinfo); 328 cdev = device_add_child(dev, NULL, -1); 329 if (cdev == NULL) { 330 device_printf(dev, "<%s>: device_add_child failed\n", 331 dinfo->mdi_obdinfo.obd_name); 332 resource_list_free(&dinfo->mdi_resources); 333 ofw_bus_gen_destroy_devinfo(&dinfo->mdi_obdinfo); 334 free(dinfo, M_MACIO); 335 continue; 336 } 337 device_set_ivars(cdev, dinfo); 338 } 339 340 return (bus_generic_attach(dev)); 341} 342 343 344static int 345macio_print_child(device_t dev, device_t child) 346{ 347 struct macio_devinfo *dinfo; 348 struct resource_list *rl; 349 int retval = 0; 350 351 dinfo = device_get_ivars(child); 352 rl = &dinfo->mdi_resources; 353 354 retval += bus_print_child_header(dev, child); 355 356 retval += resource_list_print_type(rl, "mem", SYS_RES_MEMORY, "%#lx"); 357 retval += resource_list_print_type(rl, "irq", SYS_RES_IRQ, "%ld"); 358 359 retval += bus_print_child_footer(dev, child); 360 361 return (retval); 362} 363 364 365static void 366macio_probe_nomatch(device_t dev, device_t child) 367{ 368 struct macio_devinfo *dinfo; 369 struct resource_list *rl; 370 const char *type; 371 372 if (bootverbose) { 373 dinfo = device_get_ivars(child); 374 rl = &dinfo->mdi_resources; 375 376 if ((type = ofw_bus_get_type(child)) == NULL) 377 type = "(unknown)"; 378 device_printf(dev, "<%s, %s>", type, ofw_bus_get_name(child)); 379 resource_list_print_type(rl, "mem", SYS_RES_MEMORY, "%#lx"); 380 resource_list_print_type(rl, "irq", SYS_RES_IRQ, "%ld"); 381 printf(" (no driver attached)\n"); 382 } 383} 384 385 386static struct resource * 387macio_alloc_resource(device_t bus, device_t child, int type, int *rid, 388 u_long start, u_long end, u_long count, u_int flags) 389{ 390 struct macio_softc *sc; 391 int needactivate; 392 struct resource *rv; 393 struct rman *rm; 394 u_long adjstart, adjend, adjcount; 395 struct macio_devinfo *dinfo; 396 struct resource_list_entry *rle; 397 398 sc = device_get_softc(bus); 399 dinfo = device_get_ivars(child); 400 401 needactivate = flags & RF_ACTIVE; 402 flags &= ~RF_ACTIVE; 403 404 switch (type) { 405 case SYS_RES_MEMORY: 406 case SYS_RES_IOPORT: 407 rle = resource_list_find(&dinfo->mdi_resources, SYS_RES_MEMORY, 408 *rid); 409 if (rle == NULL) { 410 device_printf(bus, "no rle for %s memory %d\n", 411 device_get_nameunit(child), *rid); 412 return (NULL); 413 } 414 415 if (start < rle->start) 416 adjstart = rle->start; 417 else if (start > rle->end) 418 adjstart = rle->end; 419 else 420 adjstart = start; 421 422 if (end < rle->start) 423 adjend = rle->start; 424 else if (end > rle->end) 425 adjend = rle->end; 426 else 427 adjend = end; 428 429 adjcount = adjend - adjstart; 430 431 rm = &sc->sc_mem_rman; 432 break; 433 434 case SYS_RES_IRQ: 435 /* Check for passthrough from subattachments like macgpio */ 436 if (device_get_parent(child) != bus) 437 return BUS_ALLOC_RESOURCE(device_get_parent(bus), child, 438 type, rid, start, end, count, flags); 439 440 rle = resource_list_find(&dinfo->mdi_resources, SYS_RES_IRQ, 441 *rid); 442 if (rle == NULL) { 443 if (dinfo->mdi_ninterrupts >= 6) { 444 device_printf(bus, 445 "%s has more than 6 interrupts\n", 446 device_get_nameunit(child)); 447 return (NULL); 448 } 449 resource_list_add(&dinfo->mdi_resources, SYS_RES_IRQ, 450 dinfo->mdi_ninterrupts, start, start, 1); 451 452 dinfo->mdi_interrupts[dinfo->mdi_ninterrupts] = start; 453 dinfo->mdi_ninterrupts++; 454 } 455 456 return (resource_list_alloc(&dinfo->mdi_resources, bus, child, 457 type, rid, start, end, count, flags)); 458 459 default: 460 device_printf(bus, "unknown resource request from %s\n", 461 device_get_nameunit(child)); 462 return (NULL); 463 } 464 465 rv = rman_reserve_resource(rm, adjstart, adjend, adjcount, flags, 466 child); 467 if (rv == NULL) { 468 device_printf(bus, 469 "failed to reserve resource %#lx - %#lx (%#lx) for %s\n", 470 adjstart, adjend, adjcount, device_get_nameunit(child)); 471 return (NULL); 472 } 473 474 rman_set_rid(rv, *rid); 475 476 if (needactivate) { 477 if (bus_activate_resource(child, type, *rid, rv) != 0) { 478 device_printf(bus, 479 "failed to activate resource for %s\n", 480 device_get_nameunit(child)); 481 rman_release_resource(rv); 482 return (NULL); 483 } 484 } 485 486 return (rv); 487} 488 489 490static int 491macio_release_resource(device_t bus, device_t child, int type, int rid, 492 struct resource *res) 493{ 494 if (rman_get_flags(res) & RF_ACTIVE) { 495 int error = bus_deactivate_resource(child, type, rid, res); 496 if (error) 497 return error; 498 } 499 500 return (rman_release_resource(res)); 501} 502 503 504static int 505macio_activate_resource(device_t bus, device_t child, int type, int rid, 506 struct resource *res) 507{ 508 struct macio_softc *sc; 509 void *p; 510 511 sc = device_get_softc(bus); 512 513 if (type == SYS_RES_IRQ) 514 return (bus_activate_resource(bus, type, rid, res)); 515 516 if ((type == SYS_RES_MEMORY) || (type == SYS_RES_IOPORT)) { 517 p = pmap_mapdev((vm_offset_t)rman_get_start(res) + sc->sc_base, 518 (vm_size_t)rman_get_size(res)); 519 if (p == NULL) 520 return (ENOMEM); 521 rman_set_virtual(res, p); 522 rman_set_bustag(res, &bs_le_tag); 523 rman_set_bushandle(res, (u_long)p); 524 } 525 526 return (rman_activate_resource(res)); 527} 528 529 530static int 531macio_deactivate_resource(device_t bus, device_t child, int type, int rid, 532 struct resource *res) 533{ 534 /* 535 * If this is a memory resource, unmap it. 536 */ 537 if ((type == SYS_RES_MEMORY) || (type == SYS_RES_IOPORT)) { 538 u_int32_t psize; 539 540 psize = rman_get_size(res); 541 pmap_unmapdev((vm_offset_t)rman_get_virtual(res), psize); 542 } 543 544 return (rman_deactivate_resource(res)); 545} 546 547 548static struct resource_list * 549macio_get_resource_list (device_t dev, device_t child) 550{ 551 struct macio_devinfo *dinfo; 552 553 dinfo = device_get_ivars(child); 554 return (&dinfo->mdi_resources); 555} 556 557static const struct ofw_bus_devinfo * 558macio_get_devinfo(device_t dev, device_t child) 559{ 560 struct macio_devinfo *dinfo; 561 562 dinfo = device_get_ivars(child); 563 return (&dinfo->mdi_obdinfo); 564} 565