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