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