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