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