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