Home | History | Annotate | Line # | Download | only in oea
      1 /* $NetBSD: ofw_autoconf.c,v 1.28 2025/10/04 04:45:57 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.28 2025/10/04 04:45:57 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 = &gtpci0_prot;
    287 			gtpci_io_bs_tag = &gtpci0_io_bs_tag;
    288 			gtpci_mem_bs_tag = &gtpci0_mem_bs_tag;
    289 			genppc_gtpci_chipset = &genppc_gtpci0_chipset;
    290 			iostart = 0;
    291 			ioend = 0;
    292 		} else {
    293 			gtpci_prot = &gtpci1_prot;
    294 			gtpci_io_bs_tag = &gtpci1_io_bs_tag;
    295 			gtpci_mem_bs_tag = &gtpci1_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 		}
    387 #ifdef macppc
    388 		/*
    389 		 * XXX
    390 		 * some macppc boxes have onboard devices where parts or all of
    391 		 * the PCI_INTERRUPT register are hardwired to 0
    392 		 */
    393 		if (pa->pa_intrpin == 0)
    394 			pa->pa_intrpin = 1;
    395 #endif
    396 	}
    397 
    398 	if (booted_device)
    399 		return;
    400 
    401 	/*
    402 	 * Skip over devices that are really just layers of NetBSD
    403 	 * autoconf(9) we should just skip as they do not have any
    404 	 * OFW devices.
    405 	 * XXX except on G5, where we have /ht/pci* instead of /pci*
    406 	 */
    407 	if (device_is_a(device_parent(dev), "atapibus") ||
    408 	    device_is_a(device_parent(dev), "atabus") ||
    409 #ifndef PMAC_G5
    410 	    device_is_a(device_parent(dev), "pci") ||
    411 #endif
    412 	    device_is_a(device_parent(dev), "scsibus")) {
    413 		if (device_parent(device_parent(dev)) != parent) {
    414 			return;
    415 		}
    416 	} else {
    417 		if (device_parent(dev) != parent)
    418 			return;
    419 	}
    420 
    421 	/*
    422 	 * Get the address part of the current path component. The
    423 	 * last component of the canonical bootpath may have no
    424 	 * address (eg, "disk"), in which case we need to get the
    425 	 * address from the original bootpath instead.
    426 	 */
    427 	p = strchr(cp, '@');
    428 	if (!p) {
    429 		if (bp)
    430 			p = strchr(bp, '@');
    431 		if (!p)
    432 			addr = 0;
    433 		else
    434 			addr = strtoul(p + 1, &p, 16);
    435 	} else
    436 		addr = strtoul(p + 1, &p, 16);
    437 
    438 	/* if the current path has more address, grab that too */
    439 	if (p && *p == ',')
    440 		addr2 = strtoul(p + 1, &p, 16);
    441 	else
    442 		addr2 = 0;
    443 
    444 	if (device_is_a(device_parent(dev), "mainbus")) {
    445 		struct confargs *ca = aux;
    446 		if (strcmp(ca->ca_name, "ofw") == 0)		/* XXX */
    447 			return;
    448 		if (strcmp(ca->ca_name, "gt") == 0)
    449 			parent = dev;
    450 		if (addr != ca->ca_reg[0])
    451 			return;
    452 		if (addr2 != 0 && addr2 != ca->ca_reg[1])
    453 			return;
    454 	} else if (device_is_a(device_parent(dev), "gt")) {
    455 		/*
    456 		 * Special handle for MV64361 on PegasosII(ofppc).
    457 		 */
    458 		if (device_is_a(dev, "mvgbec")) {
    459 			/*
    460 			 * Fix cp to /port@N from /ethernet/portN. (N is 0...2)
    461 			 */
    462 			static char fix_cp[8] = "/port@N";
    463 
    464 			if (strlen(cp) != 15 ||
    465 			    strncmp(cp, "/ethernet/port", 14) != 0)
    466 				return;
    467 			fix_cp[7] = *(cp + 15);
    468 			p = fix_cp;
    469 #if NGTPCI > 0
    470 		} else if (device_is_a(dev, "gtpci")) {
    471 			if (gtpci_mem_bs_tag != NULL &&
    472 			    addr != gtpci_mem_bs_tag->pbs_base)
    473 				return;
    474 #endif
    475 		} else
    476 			return;
    477 	} else if (device_is_a(device_parent(dev), "pci")) {
    478 		struct pci_attach_args *pa = aux;
    479 
    480 		if (addr != pa->pa_device ||
    481 		    addr2 != pa->pa_function)
    482 			return;
    483 	} else if (device_is_a(device_parent(dev), "obio")) {
    484 		struct confargs *ca = aux;
    485 
    486 		if (addr != ca->ca_reg[0])
    487 			return;
    488 	} else if (device_is_a(device_parent(dev), "scsibus") ||
    489 		   device_is_a(device_parent(dev), "atapibus")) {
    490 		struct scsipibus_attach_args *sa = aux;
    491 
    492 		/* periph_target is target for scsi, drive # for atapi */
    493 		if (addr != sa->sa_periph->periph_target)
    494 			return;
    495 	} else if (device_is_a(device_parent(device_parent(dev)), "pciide") ||
    496 		   device_is_a(device_parent(device_parent(dev)), "viaide") ||
    497 		   device_is_a(device_parent(device_parent(dev)), "slide")) {
    498 		struct ata_device *adev = aux;
    499 
    500 		if (addr != adev->adev_channel ||
    501 		    addr2 != adev->adev_drv_data->drive)
    502 			return;
    503 	} else if (device_is_a(device_parent(device_parent(dev)), "wdc")) {
    504 		struct ata_device *adev = aux;
    505 
    506 		if (addr != adev->adev_drv_data->drive)
    507 			return;
    508 	} else if (device_is_a(dev, "pci")) {
    509 		if (addr != device_unit(dev))
    510 			return;
    511 	} else if (device_is_a(device_parent(dev), "atabus")) {
    512 		/*
    513 		 * XXX
    514 		 * on svwsata this is the channel number and we ignore the
    515 		 * drive number which is always 0 anyway
    516 		 * needs to be revisited for other (S)ATA cards
    517 		 */
    518 		struct ata_device *adev = aux;
    519 		if (addr != adev->adev_channel)
    520 			return;
    521 		/* we have our match, cut off the rest */
    522 		if (p) *p = 0;
    523 	} else
    524 		return;
    525 
    526 	/* If we reach this point, then dev is a match for the current
    527 	 * path component.
    528 	 */
    529 	if (p && *p) {
    530 		parent = dev;
    531 		cp = p;
    532 		bp = strchr(bp, '/');
    533 		if (bp)
    534 			bp++;
    535 		return;
    536 	} else {
    537 		booted_device = dev;
    538 		booted_partition = 0; /* XXX -- should be extracted from bootpath */
    539 		return;
    540 	}
    541 }
    542 
    543 /*
    544  * Setup root device.
    545  * Configure swap area.
    546  */
    547 void
    548 cpu_rootconf(void)
    549 {
    550 	printf("boot device: %s\n",
    551 	    booted_device ? device_xname(booted_device) : "<unknown>");
    552 
    553 	rootconf();
    554 }
    555 
    556 /*
    557  * Find OF-device corresponding to the PCI device.
    558  */
    559 int
    560 pcidev_to_ofdev(pci_chipset_tag_t pc, pcitag_t tag)
    561 {
    562 	int bus, dev, func;
    563 	u_int reg[5];
    564 	int p, q;
    565 	int l, b, d, f;
    566 
    567 	pci_decompose_tag(pc, tag, &bus, &dev, &func);
    568 
    569 	for (q = OF_peer(0); q; q = p) {
    570 		l = OF_getprop(q, "assigned-addresses", reg, sizeof(reg));
    571 		if (l > 4) {
    572 			b = (reg[0] >> 16) & 0xff;
    573 			d = (reg[0] >> 11) & 0x1f;
    574 			f = (reg[0] >> 8) & 0x07;
    575 
    576 			if (b == bus && d == dev && f == func)
    577 				return q;
    578 		}
    579 		if ((p = OF_child(q)))
    580 			continue;
    581 		while (q) {
    582 			if ((p = OF_peer(q)))
    583 				break;
    584 			q = OF_parent(q);
    585 		}
    586 	}
    587 	return 0;
    588 }
    589