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