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