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