uninorth.c revision 260674
1/*- 2 * Copyright (C) 2002 Benno Rice. 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer. 10 * 2. Redistributions in binary form must reproduce the above copyright 11 * notice, this list of conditions and the following disclaimer in the 12 * documentation and/or other materials provided with the distribution. 13 * 14 * THIS SOFTWARE IS PROVIDED BY Benno Rice ``AS IS'' AND ANY EXPRESS OR 15 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 16 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 17 * IN NO EVENT SHALL TOOLS GMBH BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 18 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 19 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; 20 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 21 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR 22 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF 23 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 24 * 25 * $FreeBSD: stable/10/sys/powerpc/powermac/uninorth.c 260674 2014-01-15 06:17:15Z jhibbits $ 26 */ 27 28#include <sys/param.h> 29#include <sys/systm.h> 30#include <sys/module.h> 31#include <sys/bus.h> 32#include <sys/conf.h> 33#include <sys/kernel.h> 34 35#include <dev/ofw/openfirm.h> 36#include <dev/ofw/ofw_pci.h> 37#include <dev/ofw/ofw_bus.h> 38#include <dev/ofw/ofw_bus_subr.h> 39 40#include <dev/pci/pcivar.h> 41#include <dev/pci/pcireg.h> 42 43#include <machine/bus.h> 44#include <machine/intr_machdep.h> 45#include <machine/md_var.h> 46#include <machine/pio.h> 47#include <machine/resource.h> 48 49#include <sys/rman.h> 50 51#include <powerpc/powermac/uninorthvar.h> 52 53#include <vm/vm.h> 54#include <vm/pmap.h> 55 56/* 57 * Driver for the Uninorth chip itself. 58 */ 59 60static MALLOC_DEFINE(M_UNIN, "unin", "unin device information"); 61 62/* 63 * Device interface. 64 */ 65 66static int unin_chip_probe(device_t); 67static int unin_chip_attach(device_t); 68 69/* 70 * Bus interface. 71 */ 72static int unin_chip_print_child(device_t dev, device_t child); 73static void unin_chip_probe_nomatch(device_t, device_t); 74static struct resource *unin_chip_alloc_resource(device_t, device_t, int, int *, 75 u_long, u_long, u_long, u_int); 76static int unin_chip_activate_resource(device_t, device_t, int, int, 77 struct resource *); 78static int unin_chip_deactivate_resource(device_t, device_t, int, int, 79 struct resource *); 80static int unin_chip_release_resource(device_t, device_t, int, int, 81 struct resource *); 82static struct resource_list *unin_chip_get_resource_list (device_t, device_t); 83 84/* 85 * OFW Bus interface 86 */ 87 88static ofw_bus_get_devinfo_t unin_chip_get_devinfo; 89 90/* 91 * Local routines 92 */ 93 94static void unin_enable_gmac(device_t dev); 95static void unin_enable_mpic(device_t dev); 96 97/* 98 * Driver methods. 99 */ 100static device_method_t unin_chip_methods[] = { 101 102 /* Device interface */ 103 DEVMETHOD(device_probe, unin_chip_probe), 104 DEVMETHOD(device_attach, unin_chip_attach), 105 106 /* Bus interface */ 107 DEVMETHOD(bus_print_child, unin_chip_print_child), 108 DEVMETHOD(bus_probe_nomatch, unin_chip_probe_nomatch), 109 DEVMETHOD(bus_setup_intr, bus_generic_setup_intr), 110 DEVMETHOD(bus_teardown_intr, bus_generic_teardown_intr), 111 112 DEVMETHOD(bus_alloc_resource, unin_chip_alloc_resource), 113 DEVMETHOD(bus_release_resource, unin_chip_release_resource), 114 DEVMETHOD(bus_activate_resource, unin_chip_activate_resource), 115 DEVMETHOD(bus_deactivate_resource, unin_chip_deactivate_resource), 116 DEVMETHOD(bus_get_resource_list, unin_chip_get_resource_list), 117 118 DEVMETHOD(bus_child_pnpinfo_str, ofw_bus_gen_child_pnpinfo_str), 119 120 /* ofw_bus interface */ 121 DEVMETHOD(ofw_bus_get_devinfo, unin_chip_get_devinfo), 122 DEVMETHOD(ofw_bus_get_compat, ofw_bus_gen_get_compat), 123 DEVMETHOD(ofw_bus_get_model, ofw_bus_gen_get_model), 124 DEVMETHOD(ofw_bus_get_name, ofw_bus_gen_get_name), 125 DEVMETHOD(ofw_bus_get_node, ofw_bus_gen_get_node), 126 DEVMETHOD(ofw_bus_get_type, ofw_bus_gen_get_type), 127 128 { 0, 0 } 129}; 130 131static driver_t unin_chip_driver = { 132 "unin", 133 unin_chip_methods, 134 sizeof(struct unin_chip_softc) 135}; 136 137static devclass_t unin_chip_devclass; 138 139/* 140 * Assume there is only one unin chip in a PowerMac, so that pmu.c functions can 141 * suspend the chip after the whole rest of the device tree is suspended, not 142 * earlier. 143 */ 144static device_t unin_chip; 145 146DRIVER_MODULE(unin, nexus, unin_chip_driver, unin_chip_devclass, 0, 0); 147 148/* 149 * Add an interrupt to the dev's resource list if present 150 */ 151static void 152unin_chip_add_intr(phandle_t devnode, struct unin_chip_devinfo *dinfo) 153{ 154 phandle_t iparent; 155 int *intr; 156 int i, nintr; 157 int icells; 158 159 if (dinfo->udi_ninterrupts >= 6) { 160 printf("unin: device has more than 6 interrupts\n"); 161 return; 162 } 163 164 nintr = OF_getprop_alloc(devnode, "interrupts", sizeof(*intr), 165 (void **)&intr); 166 if (nintr == -1) { 167 nintr = OF_getprop_alloc(devnode, "AAPL,interrupts", 168 sizeof(*intr), (void **)&intr); 169 if (nintr == -1) 170 return; 171 } 172 173 if (intr[0] == -1) 174 return; 175 176 if (OF_getprop(devnode, "interrupt-parent", &iparent, sizeof(iparent)) 177 <= 0) 178 panic("Interrupt but no interrupt parent!\n"); 179 180 if (OF_searchprop(iparent, "#interrupt-cells", &icells, sizeof(icells)) 181 <= 0) 182 icells = 1; 183 184 for (i = 0; i < nintr; i+=icells) { 185 u_int irq = MAP_IRQ(iparent, intr[i]); 186 187 resource_list_add(&dinfo->udi_resources, SYS_RES_IRQ, 188 dinfo->udi_ninterrupts, irq, irq, 1); 189 190 if (icells > 1) { 191 powerpc_config_intr(irq, 192 (intr[i+1] & 1) ? INTR_TRIGGER_LEVEL : 193 INTR_TRIGGER_EDGE, INTR_POLARITY_LOW); 194 } 195 196 dinfo->udi_interrupts[dinfo->udi_ninterrupts] = irq; 197 dinfo->udi_ninterrupts++; 198 } 199} 200 201static void 202unin_chip_add_reg(phandle_t devnode, struct unin_chip_devinfo *dinfo) 203{ 204 struct unin_chip_reg *reg; 205 int i, nreg; 206 207 nreg = OF_getprop_alloc(devnode, "reg", sizeof(*reg), (void **)®); 208 if (nreg == -1) 209 return; 210 211 for (i = 0; i < nreg; i++) { 212 resource_list_add(&dinfo->udi_resources, SYS_RES_MEMORY, i, 213 reg[i].mr_base, 214 reg[i].mr_base + reg[i].mr_size, 215 reg[i].mr_size); 216 } 217} 218 219static void 220unin_update_reg(device_t dev, uint32_t regoff, uint32_t set, uint32_t clr) 221{ 222 volatile u_int *reg; 223 struct unin_chip_softc *sc; 224 u_int32_t tmpl; 225 226 sc = device_get_softc(dev); 227 reg = (void *)(sc->sc_addr + regoff); 228 tmpl = inl(reg); 229 tmpl &= ~clr; 230 tmpl |= set; 231 outl(reg, tmpl); 232} 233 234static void 235unin_enable_gmac(device_t dev) 236{ 237 unin_update_reg(dev, UNIN_CLOCKCNTL, UNIN_CLOCKCNTL_GMAC, 0); 238} 239 240static void 241unin_enable_mpic(device_t dev) 242{ 243 unin_update_reg(dev, UNIN_TOGGLE_REG, UNIN_MPIC_RESET | UNIN_MPIC_OUTPUT_ENABLE, 0); 244} 245 246static int 247unin_chip_probe(device_t dev) 248{ 249 const char *name; 250 251 name = ofw_bus_get_name(dev); 252 253 if (name == NULL) 254 return (ENXIO); 255 256 if (strcmp(name, "uni-n") != 0 && strcmp(name, "u3") != 0 257 && strcmp(name, "u4") != 0) 258 return (ENXIO); 259 260 device_set_desc(dev, "Apple UniNorth System Controller"); 261 return (0); 262} 263 264static int 265unin_chip_attach(device_t dev) 266{ 267 struct unin_chip_softc *sc; 268 struct unin_chip_devinfo *dinfo; 269 phandle_t root; 270 phandle_t child; 271 phandle_t iparent; 272 device_t cdev; 273 char compat[32]; 274 char name[32]; 275 u_int irq, reg[3]; 276 int error, i = 0; 277 278 sc = device_get_softc(dev); 279 root = ofw_bus_get_node(dev); 280 281 if (OF_getprop(root, "reg", reg, sizeof(reg)) < 8) 282 return (ENXIO); 283 284 if (strcmp(ofw_bus_get_name(dev), "u3") == 0 285 || strcmp(ofw_bus_get_name(dev), "u4") == 0) 286 i = 1; /* #address-cells lies */ 287 288 sc->sc_physaddr = reg[i]; 289 sc->sc_size = reg[i+1]; 290 291 sc->sc_mem_rman.rm_type = RMAN_ARRAY; 292 sc->sc_mem_rman.rm_descr = "UniNorth Device Memory"; 293 294 error = rman_init(&sc->sc_mem_rman); 295 296 if (error) { 297 device_printf(dev, "rman_init() failed. error = %d\n", error); 298 return (error); 299 } 300 301 error = rman_manage_region(&sc->sc_mem_rman, sc->sc_physaddr, 302 sc->sc_physaddr + sc->sc_size - 1); 303 if (error) { 304 device_printf(dev, 305 "rman_manage_region() failed. error = %d\n", 306 error); 307 return (error); 308 } 309 310 if (unin_chip == NULL) 311 unin_chip = dev; 312 313 /* 314 * Iterate through the sub-devices 315 */ 316 for (child = OF_child(root); child != 0; child = OF_peer(child)) { 317 dinfo = malloc(sizeof(*dinfo), M_UNIN, M_WAITOK | M_ZERO); 318 if (ofw_bus_gen_setup_devinfo(&dinfo->udi_obdinfo, child) 319 != 0) 320 { 321 free(dinfo, M_UNIN); 322 continue; 323 } 324 325 resource_list_init(&dinfo->udi_resources); 326 dinfo->udi_ninterrupts = 0; 327 unin_chip_add_intr(child, dinfo); 328 329 /* 330 * Some Apple machines do have a bug in OF, they miss 331 * the interrupt entries on the U3 I2C node. That means they 332 * do not have an entry with number of interrupts nor the 333 * entry of the interrupt parent handle. 334 * We define an interrupt and hardwire it to the /u3/mpic 335 * handle. 336 */ 337 338 if (OF_getprop(child, "name", name, sizeof(name)) <= 0) 339 device_printf(dev, "device has no name!\n"); 340 if (dinfo->udi_ninterrupts == 0 && 341 (strcmp(name, "i2c-bus") == 0 || 342 strcmp(name, "i2c") == 0)) { 343 if (OF_getprop(child, "interrupt-parent", &iparent, 344 sizeof(iparent)) <= 0) { 345 iparent = OF_finddevice("/u3/mpic"); 346 device_printf(dev, "Set /u3/mpic as iparent!\n"); 347 } 348 /* Add an interrupt number 0 to the parent. */ 349 irq = MAP_IRQ(iparent, 0); 350 resource_list_add(&dinfo->udi_resources, SYS_RES_IRQ, 351 dinfo->udi_ninterrupts, irq, irq, 1); 352 dinfo->udi_interrupts[dinfo->udi_ninterrupts] = irq; 353 dinfo->udi_ninterrupts++; 354 } 355 356 unin_chip_add_reg(child, dinfo); 357 358 cdev = device_add_child(dev, NULL, -1); 359 if (cdev == NULL) { 360 device_printf(dev, "<%s>: device_add_child failed\n", 361 dinfo->udi_obdinfo.obd_name); 362 resource_list_free(&dinfo->udi_resources); 363 ofw_bus_gen_destroy_devinfo(&dinfo->udi_obdinfo); 364 free(dinfo, M_UNIN); 365 continue; 366 } 367 368 device_set_ivars(cdev, dinfo); 369 } 370 371 /* 372 * Only map the first page, since that is where the registers 373 * of interest lie. 374 */ 375 sc->sc_addr = (vm_offset_t)pmap_mapdev(sc->sc_physaddr, PAGE_SIZE); 376 377 sc->sc_version = *(u_int *)sc->sc_addr; 378 device_printf(dev, "Version %d\n", sc->sc_version); 379 380 /* 381 * Enable the GMAC Ethernet cell and the integrated OpenPIC 382 * if Open Firmware says they are used. 383 */ 384 for (child = OF_child(root); child; child = OF_peer(child)) { 385 memset(compat, 0, sizeof(compat)); 386 OF_getprop(child, "compatible", compat, sizeof(compat)); 387 if (strcmp(compat, "gmac") == 0) 388 unin_enable_gmac(dev); 389 if (strcmp(compat, "chrp,open-pic") == 0) 390 unin_enable_mpic(dev); 391 } 392 393 /* 394 * GMAC lives under the PCI bus, so just check if enet is gmac. 395 */ 396 child = OF_finddevice("enet"); 397 memset(compat, 0, sizeof(compat)); 398 OF_getprop(child, "compatible", compat, sizeof(compat)); 399 if (strcmp(compat, "gmac") == 0) 400 unin_enable_gmac(dev); 401 402 return (bus_generic_attach(dev)); 403} 404 405static int 406unin_chip_print_child(device_t dev, device_t child) 407{ 408 struct unin_chip_devinfo *dinfo; 409 struct resource_list *rl; 410 int retval = 0; 411 412 dinfo = device_get_ivars(child); 413 rl = &dinfo->udi_resources; 414 415 retval += bus_print_child_header(dev, child); 416 417 retval += resource_list_print_type(rl, "mem", SYS_RES_MEMORY, "%#lx"); 418 retval += resource_list_print_type(rl, "irq", SYS_RES_IRQ, "%ld"); 419 420 retval += bus_print_child_footer(dev, child); 421 422 return (retval); 423} 424 425static void 426unin_chip_probe_nomatch(device_t dev, device_t child) 427{ 428 struct unin_chip_devinfo *dinfo; 429 struct resource_list *rl; 430 const char *type; 431 432 if (bootverbose) { 433 dinfo = device_get_ivars(child); 434 rl = &dinfo->udi_resources; 435 436 if ((type = ofw_bus_get_type(child)) == NULL) 437 type = "(unknown)"; 438 device_printf(dev, "<%s, %s>", type, ofw_bus_get_name(child)); 439 resource_list_print_type(rl, "mem", SYS_RES_MEMORY, "%#lx"); 440 resource_list_print_type(rl, "irq", SYS_RES_IRQ, "%ld"); 441 printf(" (no driver attached)\n"); 442 } 443} 444 445 446static struct resource * 447unin_chip_alloc_resource(device_t bus, device_t child, int type, int *rid, 448 u_long start, u_long end, u_long count, u_int flags) 449{ 450 struct unin_chip_softc *sc; 451 int needactivate; 452 struct resource *rv; 453 struct rman *rm; 454 u_long adjstart, adjend, adjcount; 455 struct unin_chip_devinfo *dinfo; 456 struct resource_list_entry *rle; 457 458 sc = device_get_softc(bus); 459 dinfo = device_get_ivars(child); 460 461 needactivate = flags & RF_ACTIVE; 462 flags &= ~RF_ACTIVE; 463 464 switch (type) { 465 case SYS_RES_MEMORY: 466 case SYS_RES_IOPORT: 467 rle = resource_list_find(&dinfo->udi_resources, SYS_RES_MEMORY, 468 *rid); 469 if (rle == NULL) { 470 device_printf(bus, "no rle for %s memory %d\n", 471 device_get_nameunit(child), *rid); 472 return (NULL); 473 } 474 475 rle->end = rle->end - 1; /* Hack? */ 476 477 if (start < rle->start) 478 adjstart = rle->start; 479 else if (start > rle->end) 480 adjstart = rle->end; 481 else 482 adjstart = start; 483 484 if (end < rle->start) 485 adjend = rle->start; 486 else if (end > rle->end) 487 adjend = rle->end; 488 else 489 adjend = end; 490 491 adjcount = adjend - adjstart; 492 493 rm = &sc->sc_mem_rman; 494 break; 495 496 case SYS_RES_IRQ: 497 /* Check for passthrough from subattachments. */ 498 if (device_get_parent(child) != bus) 499 return BUS_ALLOC_RESOURCE(device_get_parent(bus), child, 500 type, rid, start, end, count, 501 flags); 502 503 rle = resource_list_find(&dinfo->udi_resources, SYS_RES_IRQ, 504 *rid); 505 if (rle == NULL) { 506 if (dinfo->udi_ninterrupts >= 6) { 507 device_printf(bus, 508 "%s has more than 6 interrupts\n", 509 device_get_nameunit(child)); 510 return (NULL); 511 } 512 resource_list_add(&dinfo->udi_resources, SYS_RES_IRQ, 513 dinfo->udi_ninterrupts, start, start, 514 1); 515 516 dinfo->udi_interrupts[dinfo->udi_ninterrupts] = start; 517 dinfo->udi_ninterrupts++; 518 } 519 520 return (resource_list_alloc(&dinfo->udi_resources, bus, child, 521 type, rid, start, end, count, 522 flags)); 523 default: 524 device_printf(bus, "unknown resource request from %s\n", 525 device_get_nameunit(child)); 526 return (NULL); 527 } 528 529 rv = rman_reserve_resource(rm, adjstart, adjend, adjcount, flags, 530 child); 531 if (rv == NULL) { 532 device_printf(bus, 533 "failed to reserve resource %#lx - %#lx (%#lx)" 534 " for %s\n", adjstart, adjend, adjcount, 535 device_get_nameunit(child)); 536 return (NULL); 537 } 538 539 rman_set_rid(rv, *rid); 540 541 if (needactivate) { 542 if (bus_activate_resource(child, type, *rid, rv) != 0) { 543 device_printf(bus, 544 "failed to activate resource for %s\n", 545 device_get_nameunit(child)); 546 rman_release_resource(rv); 547 return (NULL); 548 } 549 } 550 551 return (rv); 552} 553 554static int 555unin_chip_release_resource(device_t bus, device_t child, int type, int rid, 556 struct resource *res) 557{ 558 if (rman_get_flags(res) & RF_ACTIVE) { 559 int error = bus_deactivate_resource(child, type, rid, res); 560 if (error) 561 return error; 562 } 563 564 return (rman_release_resource(res)); 565} 566 567static int 568unin_chip_activate_resource(device_t bus, device_t child, int type, int rid, 569 struct resource *res) 570{ 571 void *p; 572 573 if (type == SYS_RES_IRQ) 574 return (bus_activate_resource(bus, type, rid, res)); 575 576 if ((type == SYS_RES_MEMORY) || (type == SYS_RES_IOPORT)) { 577 vm_offset_t start; 578 579 start = (vm_offset_t) rman_get_start(res); 580 581 if (bootverbose) 582 printf("unin mapdev: start %zx, len %ld\n", start, 583 rman_get_size(res)); 584 585 p = pmap_mapdev(start, (vm_size_t) rman_get_size(res)); 586 if (p == NULL) 587 return (ENOMEM); 588 rman_set_virtual(res, p); 589 rman_set_bustag(res, &bs_be_tag); 590 rman_set_bushandle(res, (u_long)p); 591 } 592 593 return (rman_activate_resource(res)); 594} 595 596 597static int 598unin_chip_deactivate_resource(device_t bus, device_t child, int type, int rid, 599 struct resource *res) 600{ 601 /* 602 * If this is a memory resource, unmap it. 603 */ 604 if ((type == SYS_RES_MEMORY) || (type == SYS_RES_IOPORT)) { 605 u_int32_t psize; 606 607 psize = rman_get_size(res); 608 pmap_unmapdev((vm_offset_t)rman_get_virtual(res), psize); 609 } 610 611 return (rman_deactivate_resource(res)); 612} 613 614 615static struct resource_list * 616unin_chip_get_resource_list (device_t dev, device_t child) 617{ 618 struct unin_chip_devinfo *dinfo; 619 620 dinfo = device_get_ivars(child); 621 return (&dinfo->udi_resources); 622} 623 624static const struct ofw_bus_devinfo * 625unin_chip_get_devinfo(device_t dev, device_t child) 626{ 627 struct unin_chip_devinfo *dinfo; 628 629 dinfo = device_get_ivars(child); 630 return (&dinfo->udi_obdinfo); 631} 632 633int 634unin_chip_wake(device_t dev) 635{ 636 637 if (dev == NULL) 638 dev = unin_chip; 639 unin_update_reg(dev, UNIN_PWR_MGMT, UNIN_PWR_NORMAL, UNIN_PWR_MASK); 640 DELAY(10); 641 unin_update_reg(dev, UNIN_HWINIT_STATE, UNIN_RUNNING, 0); 642 DELAY(100); 643 644 return (0); 645} 646 647int 648unin_chip_sleep(device_t dev, int idle) 649{ 650 if (dev == NULL) 651 dev = unin_chip; 652 653 unin_update_reg(dev, UNIN_HWINIT_STATE, UNIN_SLEEPING, 0); 654 DELAY(10); 655 if (idle) 656 unin_update_reg(dev, UNIN_PWR_MGMT, UNIN_PWR_IDLE2, UNIN_PWR_MASK); 657 else 658 unin_update_reg(dev, UNIN_PWR_MGMT, UNIN_PWR_SLEEP, UNIN_PWR_MASK); 659 DELAY(10); 660 661 return (0); 662} 663