1 /* $NetBSD: ofw_autoconf.c,v 1.27 2025/09/21 13:51:50 thorpej Exp $ */ 2 /* 3 * Copyright (C) 1995, 1996 Wolfgang Solfrank. 4 * Copyright (C) 1995, 1996 TooLs GmbH. 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 * 3. All advertising materials mentioning features or use of this software 16 * must display the following acknowledgement: 17 * This product includes software developed by TooLs GmbH. 18 * 4. The name of TooLs GmbH may not be used to endorse or promote products 19 * derived from this software without specific prior written permission. 20 * 21 * THIS SOFTWARE IS PROVIDED BY TOOLS GMBH ``AS IS'' AND ANY EXPRESS OR 22 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 23 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 24 * IN NO EVENT SHALL TOOLS GMBH BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 25 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 26 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; 27 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 28 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR 29 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF 30 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 31 */ 32 33 #include <sys/cdefs.h> 34 __KERNEL_RCSID(0, "$NetBSD: ofw_autoconf.c,v 1.27 2025/09/21 13:51:50 thorpej Exp $"); 35 36 #ifdef ofppc 37 #include "gtpci.h" 38 #endif 39 40 #include <sys/param.h> 41 #include <sys/conf.h> 42 #include <sys/device.h> 43 #include <sys/reboot.h> 44 #include <sys/systm.h> 45 46 #include <uvm/uvm_extern.h> 47 48 #include <machine/autoconf.h> 49 #include <sys/bus.h> 50 51 #include <dev/ofw/openfirm.h> 52 #include <dev/marvell/marvellvar.h> 53 #include <dev/pci/pcireg.h> 54 #include <dev/pci/pcivar.h> 55 #if NGTPCI > 0 56 #include <dev/marvell/gtpcivar.h> 57 #endif 58 #include <dev/scsipi/scsi_all.h> 59 #include <dev/scsipi/scsipi_all.h> 60 #include <dev/scsipi/scsiconf.h> 61 #include <dev/ata/atavar.h> 62 #include <dev/ic/wdcvar.h> 63 64 #include <dev/wscons/wsconsio.h> 65 #include <dev/wscons/wsdisplayvar.h> 66 #include <dev/rasops/rasops.h> 67 #include <powerpc/oea/ofw_rasconsvar.h> 68 #include <powerpc/ofw_machdep.h> 69 70 #include <machine/pci_machdep.h> 71 72 #include <prop/proplib.h> 73 74 extern char bootpath[256]; 75 char cbootpath[256]; 76 static int boot_node = 0; /* points at boot device if we netboot */ 77 78 static void canonicalize_bootpath(void); 79 80 /* 81 * Determine device configuration for a machine. 82 */ 83 void 84 cpu_configure(void) 85 { 86 #if NWSDISPLAY > 0 87 rascons_add_rom_font(); 88 #endif 89 init_interrupt(); 90 canonicalize_bootpath(); 91 92 if (config_rootfound("mainbus", NULL) == NULL) 93 panic("configure: mainbus not configured"); 94 95 genppc_cpu_configure(); 96 } 97 98 static void 99 canonicalize_bootpath(void) 100 { 101 int node, len; 102 char *p, *lastp; 103 char last[32], type[32]; 104 105 /* 106 * If the bootpath doesn't start with a / then it isn't 107 * an OFW path and probably is an alias, so look up the alias 108 * and regenerate the full bootpath so device_register will work. 109 */ 110 if (bootpath[0] != '/' && bootpath[0] != '\0') { 111 int aliases = OF_finddevice("/aliases"); 112 char tmpbuf[100]; 113 char aliasbuf[256]; 114 if (aliases != 0) { 115 char *cp1, *cp2, *cp; 116 char saved_ch = '\0'; 117 cp1 = strchr(bootpath, ':'); 118 cp2 = strchr(bootpath, ','); 119 cp = cp1; 120 if (cp1 == NULL || (cp2 != NULL && cp2 < cp1)) 121 cp = cp2; 122 tmpbuf[0] = '\0'; 123 if (cp != NULL) { 124 strcpy(tmpbuf, cp); 125 saved_ch = *cp; 126 *cp = '\0'; 127 } 128 len = OF_getprop(aliases, bootpath, aliasbuf, 129 sizeof(aliasbuf)); 130 if (len > 0) { 131 if (aliasbuf[len-1] == '\0') 132 len--; 133 memcpy(bootpath, aliasbuf, len); 134 strcpy(&bootpath[len], tmpbuf); 135 } else { 136 *cp = saved_ch; 137 } 138 } 139 } 140 141 /* 142 * Strip kernel name. bootpath contains "OF-path"/"kernel". 143 * 144 * for example: 145 * /bandit@F2000000/gc@10/53c94@10000/sd@0,0/netbsd (OF-1.x) 146 * /pci/mac-io/ata-3@2000/disk@0:0/netbsd.new (OF-3.x) 147 */ 148 strcpy(cbootpath, bootpath); 149 150 if ((node = OF_finddevice("/options")) == -1 || 151 OF_getprop(node, "qemu_boot_hack", type, sizeof(type) - 1) == -1 || 152 type[0] != 'y') { 153 while ((node = OF_finddevice(cbootpath)) == -1) { 154 if ((p = strrchr(cbootpath, '/')) == NULL) 155 break; 156 *p = '\0'; 157 } 158 } else { 159 node = -1; 160 } 161 162 printf("bootpath: %s\n", bootpath); 163 if (node == -1) { 164 /* Cannot canonicalize... use bootpath anyway. */ 165 strcpy(cbootpath, bootpath); 166 167 return; 168 } 169 170 /* see if we netbooted */ 171 len = OF_getprop(node, "device_type", type, sizeof(type) - 1); 172 if (len > -1) { 173 type[len] = 0; 174 if (strcmp(type, "network") == 0) { 175 boot_node = node; 176 } 177 } 178 179 /* 180 * cbootpath is a valid OF path. Use package-to-path to 181 * canonicalize pathname. 182 */ 183 184 /* Back up the last component for later use. */ 185 if ((p = strrchr(cbootpath, '/')) != NULL) 186 strcpy(last, p + 1); 187 else 188 last[0] = '\0'; 189 190 memset(cbootpath, 0, sizeof(cbootpath)); 191 OF_package_to_path(node, cbootpath, sizeof(cbootpath) - 1); 192 193 /* 194 * OF_1.x (at least) always returns addr == 0 for 195 * SCSI disks (i.e. "/bandit (at) .../.../sd@0,0"). 196 * also check for .../disk@ which some Adaptec firmware uses 197 */ 198 lastp = strrchr(cbootpath, '/'); 199 if (lastp != NULL) { 200 lastp++; 201 if ((strncmp(lastp, "sd@", 3) == 0 202 && strncmp(last, "sd@", 3) == 0) || 203 (strncmp(lastp, "disk@", 5) == 0 204 && strncmp(last, "disk@", 5) == 0)) 205 strcpy(lastp, last); 206 } else { 207 lastp = cbootpath; 208 } 209 210 /* 211 * At this point, cbootpath contains like: 212 * "/pci@80000000/mac-io@10/ata-3@20000/disk" 213 * 214 * The last component may have no address... so append it. 215 */ 216 if (strchr(lastp, '@') == NULL) { 217 /* Append it. */ 218 if ((p = strrchr(last, '@')) != NULL) 219 strcat(cbootpath, p); 220 } 221 222 if ((p = strrchr(lastp, ':')) != NULL) { 223 *p++ = '\0'; 224 /* booted_partition = *p - '0'; XXX correct? */ 225 } 226 } 227 228 /* 229 * device_register is called from config_attach as each device is 230 * attached. We use it to find the NetBSD device corresponding to the 231 * known OF boot device. 232 */ 233 void 234 ofw_device_register(device_t dev, void *aux) 235 { 236 static device_t parent; 237 static char *bp = bootpath + 1, *cp = cbootpath; 238 unsigned long addr, addr2; 239 char *p; 240 #if NGTPCI > 0 241 struct powerpc_bus_space *gtpci_mem_bs_tag = NULL; 242 #endif 243 244 /* Skip over devices not represented in the OF tree. */ 245 if (device_is_a(dev, "mainbus")) { 246 parent = dev; 247 return; 248 } 249 250 /* skip over CPUs */ 251 if (device_is_a(dev, "cpu")) { 252 return; 253 } 254 255 if (device_is_a(dev, "valkyriefb")) { 256 struct confargs *ca = aux; 257 prop_dictionary_t dict; 258 259 dict = device_properties(dev); 260 copy_disp_props(dev, ca->ca_node, dict); 261 } 262 263 /* cannot read useful display properties for platinum */ 264 if (device_is_a(dev, "platinumfb")) { 265 return; 266 } 267 268 #if NGTPCI > 0 269 if (device_is_a(dev, "gtpci")) { 270 extern struct gtpci_prot gtpci0_prot, gtpci1_prot; 271 extern struct powerpc_bus_space 272 gtpci0_io_bs_tag, gtpci0_mem_bs_tag, 273 gtpci1_io_bs_tag, gtpci1_mem_bs_tag; 274 extern struct genppc_pci_chipset 275 genppc_gtpci0_chipset, genppc_gtpci1_chipset; 276 277 struct marvell_attach_args *mva = aux; 278 struct gtpci_prot *gtpci_prot; 279 struct powerpc_bus_space *gtpci_io_bs_tag; 280 struct genppc_pci_chipset *genppc_gtpci_chipset; 281 prop_dictionary_t dict = device_properties(dev); 282 prop_data_t prot, io_bs_tag, mem_bs_tag, pc; 283 int iostart, ioend; 284 285 if (mva->mva_unit == 0) { 286 gtpci_prot = >pci0_prot; 287 gtpci_io_bs_tag = >pci0_io_bs_tag; 288 gtpci_mem_bs_tag = >pci0_mem_bs_tag; 289 genppc_gtpci_chipset = &genppc_gtpci0_chipset; 290 iostart = 0; 291 ioend = 0; 292 } else { 293 gtpci_prot = >pci1_prot; 294 gtpci_io_bs_tag = >pci1_io_bs_tag; 295 gtpci_mem_bs_tag = >pci1_mem_bs_tag; 296 genppc_gtpci_chipset = &genppc_gtpci1_chipset; 297 iostart = 0x1400; 298 ioend = 0xffff; 299 } 300 301 prot = prop_data_create_data_nocopy( 302 gtpci_prot, sizeof(struct gtpci_prot)); 303 KASSERT(prot != NULL); 304 prop_dictionary_set(dict, "prot", prot); 305 prop_object_release(prot); 306 307 io_bs_tag = prop_data_create_data_nocopy( 308 gtpci_io_bs_tag, sizeof(struct powerpc_bus_space)); 309 KASSERT(io_bs_tag != NULL); 310 prop_dictionary_set(dict, "io-bus-tag", io_bs_tag); 311 prop_object_release(io_bs_tag); 312 mem_bs_tag = prop_data_create_data_nocopy( 313 gtpci_mem_bs_tag, sizeof(struct powerpc_bus_space)); 314 KASSERT(mem_bs_tag != NULL); 315 prop_dictionary_set(dict, "mem-bus-tag", mem_bs_tag); 316 prop_object_release(mem_bs_tag); 317 318 genppc_gtpci_chipset->pc_conf_v = device_private(dev); 319 pc = prop_data_create_data_nocopy(genppc_gtpci_chipset, 320 sizeof(struct genppc_pci_chipset)); 321 KASSERT(pc != NULL); 322 prop_dictionary_set(dict, "pci-chipset", pc); 323 prop_object_release(pc); 324 325 prop_dictionary_set_uint64(dict, "iostart", iostart); 326 prop_dictionary_set_uint64(dict, "ioend", ioend); 327 prop_dictionary_set_uint64(dict, "memstart", 328 gtpci_mem_bs_tag->pbs_base); 329 prop_dictionary_set_uint64(dict, "memend", 330 gtpci_mem_bs_tag->pbs_limit - 1); 331 prop_dictionary_set_uint32(dict, "cache-line-size", 332 CACHELINESIZE); 333 } 334 #endif 335 if (device_is_a(dev, "atapibus") || 336 #ifndef PMAC_G5 337 device_is_a(dev, "pci") || 338 #endif 339 device_is_a(dev, "scsibus") || device_is_a(dev, "atabus")) 340 return; 341 342 if (device_is_a(device_parent(dev), "pci")) { 343 struct pci_attach_args *pa = aux; 344 prop_dictionary_t dict; 345 prop_bool_t b; 346 int node; 347 char name[32]; 348 349 dict = device_properties(dev); 350 node = pcidev_to_ofdev(pa->pa_pc, pa->pa_tag); 351 352 /* enable configuration of irq 14/15 for VIA native IDE */ 353 if (device_is_a(dev, "viaide") && 354 strncmp(model_name, "Pegasos", 7) == 0) { 355 b = prop_bool_create(true); 356 KASSERT(b != NULL); 357 (void)prop_dictionary_set(dict, 358 "use-compat-native-irq", b); 359 prop_object_release(b); 360 } 361 362 if (node != 0) { 363 int pci_class = 0; 364 365 prop_dictionary_set_uint32(dict, "device_node", node); 366 367 if (node == boot_node) { 368 /* we netbooted from whatever this is */ 369 booted_device = dev; 370 } 371 /* see if this is going to be console */ 372 memset(name, 0, sizeof(name)); 373 OF_getprop(node, "device_type", name, sizeof(name)); 374 375 OF_getprop(node, "class-code", &pci_class, 376 sizeof pci_class); 377 pci_class = (pci_class >> 16) & 0xff; 378 379 if (strcmp(name, "display") == 0 || 380 strcmp(name, "ATY,DDParent") == 0 || 381 pci_class == PCI_CLASS_DISPLAY) { 382 /* setup display properties for fb driver */ 383 prop_dictionary_set_bool(dict, "is_console", 0); 384 copy_disp_props(dev, node, dict); 385 } 386 if (pci_class == PCI_CLASS_NETWORK) { 387 of_to_dataprop(dict, node, "local-mac-address", 388 "mac-address"); 389 of_to_dataprop(dict, node, "shared-pins", 390 "shared-pins"); 391 } 392 } 393 #ifdef macppc 394 /* 395 * XXX 396 * some macppc boxes have onboard devices where parts or all of 397 * the PCI_INTERRUPT register are hardwired to 0 398 */ 399 if (pa->pa_intrpin == 0) 400 pa->pa_intrpin = 1; 401 #endif 402 } 403 404 if (booted_device) 405 return; 406 407 /* 408 * Skip over devices that are really just layers of NetBSD 409 * autoconf(9) we should just skip as they do not have any 410 * OFW devices. 411 * XXX except on G5, where we have /ht/pci* instead of /pci* 412 */ 413 if (device_is_a(device_parent(dev), "atapibus") || 414 device_is_a(device_parent(dev), "atabus") || 415 #ifndef PMAC_G5 416 device_is_a(device_parent(dev), "pci") || 417 #endif 418 device_is_a(device_parent(dev), "scsibus")) { 419 if (device_parent(device_parent(dev)) != parent) { 420 return; 421 } 422 } else { 423 if (device_parent(dev) != parent) 424 return; 425 } 426 427 /* 428 * Get the address part of the current path component. The 429 * last component of the canonical bootpath may have no 430 * address (eg, "disk"), in which case we need to get the 431 * address from the original bootpath instead. 432 */ 433 p = strchr(cp, '@'); 434 if (!p) { 435 if (bp) 436 p = strchr(bp, '@'); 437 if (!p) 438 addr = 0; 439 else 440 addr = strtoul(p + 1, &p, 16); 441 } else 442 addr = strtoul(p + 1, &p, 16); 443 444 /* if the current path has more address, grab that too */ 445 if (p && *p == ',') 446 addr2 = strtoul(p + 1, &p, 16); 447 else 448 addr2 = 0; 449 450 if (device_is_a(device_parent(dev), "mainbus")) { 451 struct confargs *ca = aux; 452 if (strcmp(ca->ca_name, "ofw") == 0) /* XXX */ 453 return; 454 if (strcmp(ca->ca_name, "gt") == 0) 455 parent = dev; 456 if (addr != ca->ca_reg[0]) 457 return; 458 if (addr2 != 0 && addr2 != ca->ca_reg[1]) 459 return; 460 } else if (device_is_a(device_parent(dev), "gt")) { 461 /* 462 * Special handle for MV64361 on PegasosII(ofppc). 463 */ 464 if (device_is_a(dev, "mvgbec")) { 465 /* 466 * Fix cp to /port@N from /ethernet/portN. (N is 0...2) 467 */ 468 static char fix_cp[8] = "/port@N"; 469 470 if (strlen(cp) != 15 || 471 strncmp(cp, "/ethernet/port", 14) != 0) 472 return; 473 fix_cp[7] = *(cp + 15); 474 p = fix_cp; 475 #if NGTPCI > 0 476 } else if (device_is_a(dev, "gtpci")) { 477 if (gtpci_mem_bs_tag != NULL && 478 addr != gtpci_mem_bs_tag->pbs_base) 479 return; 480 #endif 481 } else 482 return; 483 } else if (device_is_a(device_parent(dev), "pci")) { 484 struct pci_attach_args *pa = aux; 485 486 if (addr != pa->pa_device || 487 addr2 != pa->pa_function) 488 return; 489 } else if (device_is_a(device_parent(dev), "obio")) { 490 struct confargs *ca = aux; 491 492 if (addr != ca->ca_reg[0]) 493 return; 494 } else if (device_is_a(device_parent(dev), "scsibus") || 495 device_is_a(device_parent(dev), "atapibus")) { 496 struct scsipibus_attach_args *sa = aux; 497 498 /* periph_target is target for scsi, drive # for atapi */ 499 if (addr != sa->sa_periph->periph_target) 500 return; 501 } else if (device_is_a(device_parent(device_parent(dev)), "pciide") || 502 device_is_a(device_parent(device_parent(dev)), "viaide") || 503 device_is_a(device_parent(device_parent(dev)), "slide")) { 504 struct ata_device *adev = aux; 505 506 if (addr != adev->adev_channel || 507 addr2 != adev->adev_drv_data->drive) 508 return; 509 } else if (device_is_a(device_parent(device_parent(dev)), "wdc")) { 510 struct ata_device *adev = aux; 511 512 if (addr != adev->adev_drv_data->drive) 513 return; 514 } else if (device_is_a(dev, "pci")) { 515 if (addr != device_unit(dev)) 516 return; 517 } else if (device_is_a(device_parent(dev), "atabus")) { 518 /* 519 * XXX 520 * on svwsata this is the channel number and we ignore the 521 * drive number which is always 0 anyway 522 * needs to be revisited for other (S)ATA cards 523 */ 524 struct ata_device *adev = aux; 525 if (addr != adev->adev_channel) 526 return; 527 /* we have our match, cut off the rest */ 528 if (p) *p = 0; 529 } else 530 return; 531 532 /* If we reach this point, then dev is a match for the current 533 * path component. 534 */ 535 if (p && *p) { 536 parent = dev; 537 cp = p; 538 bp = strchr(bp, '/'); 539 if (bp) 540 bp++; 541 return; 542 } else { 543 booted_device = dev; 544 booted_partition = 0; /* XXX -- should be extracted from bootpath */ 545 return; 546 } 547 } 548 549 /* 550 * Setup root device. 551 * Configure swap area. 552 */ 553 void 554 cpu_rootconf(void) 555 { 556 printf("boot device: %s\n", 557 booted_device ? device_xname(booted_device) : "<unknown>"); 558 559 rootconf(); 560 } 561 562 /* 563 * Find OF-device corresponding to the PCI device. 564 */ 565 int 566 pcidev_to_ofdev(pci_chipset_tag_t pc, pcitag_t tag) 567 { 568 int bus, dev, func; 569 u_int reg[5]; 570 int p, q; 571 int l, b, d, f; 572 573 pci_decompose_tag(pc, tag, &bus, &dev, &func); 574 575 for (q = OF_peer(0); q; q = p) { 576 l = OF_getprop(q, "assigned-addresses", reg, sizeof(reg)); 577 if (l > 4) { 578 b = (reg[0] >> 16) & 0xff; 579 d = (reg[0] >> 11) & 0x1f; 580 f = (reg[0] >> 8) & 0x07; 581 582 if (b == bus && d == dev && f == func) 583 return q; 584 } 585 if ((p = OF_child(q))) 586 continue; 587 while (q) { 588 if ((p = OF_peer(q))) 589 break; 590 q = OF_parent(q); 591 } 592 } 593 return 0; 594 } 595