Home | History | Annotate | Line # | Download | only in oea
ofw_autoconf.c revision 1.11
      1 /* $NetBSD: ofw_autoconf.c,v 1.11 2010/01/20 16:36:55 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.11 2010/01/20 16:36:55 macallan Exp $");
     35 
     36 #include <sys/param.h>
     37 #include <sys/conf.h>
     38 #include <sys/device.h>
     39 #include <sys/reboot.h>
     40 #include <sys/systm.h>
     41 
     42 #include <uvm/uvm_extern.h>
     43 
     44 #include <machine/autoconf.h>
     45 #include <machine/bus.h>
     46 #include <machine/stdarg.h>
     47 
     48 #include <dev/ofw/openfirm.h>
     49 #include <dev/pci/pcireg.h>
     50 #include <dev/pci/pcivar.h>
     51 #include <dev/scsipi/scsi_all.h>
     52 #include <dev/scsipi/scsipi_all.h>
     53 #include <dev/scsipi/scsiconf.h>
     54 #include <dev/ata/atavar.h>
     55 #include <dev/ic/wdcvar.h>
     56 
     57 extern char bootpath[256];
     58 char cbootpath[256];
     59 int console_node = 0, console_instance = 0;
     60 
     61 static void canonicalize_bootpath(void);
     62 
     63 /*
     64  * Determine device configuration for a machine.
     65  */
     66 void
     67 cpu_configure(void)
     68 {
     69 	init_interrupt();
     70 	canonicalize_bootpath();
     71 
     72 	if (config_rootfound("mainbus", NULL) == NULL)
     73 		panic("configure: mainbus not configured");
     74 
     75 	genppc_cpu_configure();
     76 }
     77 
     78 static void
     79 canonicalize_bootpath(void)
     80 {
     81 	int node;
     82 	char *p, *lastp;
     83 	char last[32];
     84 
     85 	/*
     86 	 * If the bootpath doesn't start with a / then it isn't
     87 	 * an OFW path and probably is an alias, so look up the alias
     88 	 * and regenerate the full bootpath so device_register will work.
     89 	 */
     90 	if (bootpath[0] != '/' && bootpath[0] != '\0') {
     91 		int aliases = OF_finddevice("/aliases");
     92 		char tmpbuf[100];
     93 		char aliasbuf[256];
     94 		if (aliases != 0) {
     95 			char *cp1, *cp2, *cp;
     96 			char saved_ch = '\0';
     97 			int len;
     98 			cp1 = strchr(bootpath, ':');
     99 			cp2 = strchr(bootpath, ',');
    100 			cp = cp1;
    101 			if (cp1 == NULL || (cp2 != NULL && cp2 < cp1))
    102 				cp = cp2;
    103 			tmpbuf[0] = '\0';
    104 			if (cp != NULL) {
    105 				strcpy(tmpbuf, cp);
    106 				saved_ch = *cp;
    107 				*cp = '\0';
    108 			}
    109 			len = OF_getprop(aliases, bootpath, aliasbuf,
    110 			    sizeof(aliasbuf));
    111 			if (len > 0) {
    112 				if (aliasbuf[len-1] == '\0')
    113 					len--;
    114 				memcpy(bootpath, aliasbuf, len);
    115 				strcpy(&bootpath[len], tmpbuf);
    116 			} else {
    117 				*cp = saved_ch;
    118 			}
    119 		}
    120 	}
    121 
    122 	/*
    123 	 * Strip kernel name.  bootpath contains "OF-path"/"kernel".
    124 	 *
    125 	 * for example:
    126 	 *   /bandit@F2000000/gc@10/53c94@10000/sd@0,0/netbsd	(OF-1.x)
    127 	 *   /pci/mac-io/ata-3@2000/disk@0:0/netbsd.new		(OF-3.x)
    128 	 */
    129 	strcpy(cbootpath, bootpath);
    130 	while ((node = OF_finddevice(cbootpath)) == -1) {
    131 		if ((p = strrchr(cbootpath, '/')) == NULL)
    132 			break;
    133 		*p = '\0';
    134 	}
    135 
    136 	printf("bootpath: %s\n", bootpath);
    137 	if (node == -1) {
    138 		/* Cannot canonicalize... use bootpath anyway. */
    139 		strcpy(cbootpath, bootpath);
    140 
    141 		return;
    142 	}
    143 
    144 	/*
    145 	 * cbootpath is a valid OF path.  Use package-to-path to
    146 	 * canonicalize pathname.
    147 	 */
    148 
    149 	/* Back up the last component for later use. */
    150 	if ((p = strrchr(cbootpath, '/')) != NULL)
    151 		strcpy(last, p + 1);
    152 	else
    153 		last[0] = '\0';
    154 
    155 	memset(cbootpath, 0, sizeof(cbootpath));
    156 	OF_package_to_path(node, cbootpath, sizeof(cbootpath) - 1);
    157 
    158 	/*
    159 	 * OF_1.x (at least) always returns addr == 0 for
    160 	 * SCSI disks (i.e. "/bandit (at) .../.../sd@0,0").
    161 	 */
    162 	lastp = strrchr(cbootpath, '/');
    163 	if (lastp != NULL) {
    164 		lastp++;
    165 		if (strncmp(lastp, "sd@", 3) == 0
    166 		    && strncmp(last, "sd@", 3) == 0)
    167 			strcpy(lastp, last);
    168 	} else {
    169 		lastp = cbootpath;
    170 	}
    171 
    172 	/*
    173 	 * At this point, cbootpath contains like:
    174 	 * "/pci@80000000/mac-io@10/ata-3@20000/disk"
    175 	 *
    176 	 * The last component may have no address... so append it.
    177 	 */
    178 	if (strchr(lastp, '@') == NULL) {
    179 		/* Append it. */
    180 		if ((p = strrchr(last, '@')) != NULL)
    181 			strcat(cbootpath, p);
    182 	}
    183 
    184 	if ((p = strrchr(lastp, ':')) != NULL) {
    185 		*p++ = '\0';
    186 		/* booted_partition = *p - '0';		XXX correct? */
    187 	}
    188 }
    189 
    190 /*
    191  * device_register is called from config_attach as each device is
    192  * attached. We use it to find the NetBSD device corresponding to the
    193  * known OF boot device.
    194  */
    195 void
    196 device_register(struct device *dev, void *aux)
    197 {
    198 	static struct device *parent;
    199 	static char *bp = bootpath + 1, *cp = cbootpath;
    200 	unsigned long addr, addr2;
    201 	char *p;
    202 
    203 	/* Skip over devices not represented in the OF tree. */
    204 	if (device_is_a(dev, "mainbus")) {
    205 		parent = dev;
    206 		return;
    207 	}
    208 	if (device_is_a(dev, "atapibus") || device_is_a(dev, "pci") ||
    209 	    device_is_a(dev, "scsibus") || device_is_a(dev, "atabus"))
    210 		return;
    211 
    212 	if (device_is_a(device_parent(dev), "pci")) {
    213 		struct pci_attach_args *pa = aux;
    214 		prop_dictionary_t dict;
    215 		prop_bool_t b;
    216 		int node;
    217 		char name[32];
    218 
    219 		dict = device_properties(dev);
    220 		node = pcidev_to_ofdev(pa->pa_pc, pa->pa_tag);
    221 
    222 		/* enable configuration of irq 14/15 for VIA native IDE */
    223 		if (device_is_a(dev, "viaide") &&
    224 		    strncmp(model_name, "Pegasos", 7) == 0) {
    225 			b = prop_bool_create(true);
    226 			KASSERT(b != NULL);
    227 			(void)prop_dictionary_set(dict,
    228 			    "use-compat-native-irq", b);
    229 			prop_object_release(b);
    230 		}
    231 
    232 		if (node != 0) {
    233 			int pci_class = 0;
    234 
    235 			prop_dictionary_set_uint32(dict, "device_node", node);
    236 
    237 			/* see if this is going to be console */
    238 			memset(name, 0, sizeof(name));
    239 			OF_getprop(node, "device_type", name, sizeof(name));
    240 
    241 			OF_getprop(node, "class-code", &pci_class,
    242 				sizeof pci_class);
    243 			pci_class = (pci_class >> 16) & 0xff;
    244 
    245 			if (strcmp(name, "display") == 0 ||
    246 			    strcmp(name, "ATY,DDParent") == 0 ||
    247 			    pci_class == PCI_CLASS_DISPLAY) {
    248 				/* setup display properties for fb driver */
    249 				prop_dictionary_set_bool(dict, "is_console", 0);
    250 				copy_disp_props(dev, node, dict);
    251 			}
    252 			if (pci_class == PCI_CLASS_NETWORK) {
    253 				of_to_dataprop(dict, node, "local-mac-address",
    254 				    "mac-address");
    255 				of_to_dataprop(dict, node, "shared-pins",
    256 				    "shared-pins");
    257 			}
    258 		}
    259 	}
    260 
    261 	if (booted_device)
    262 		return;
    263 
    264 	/*
    265 	 * Skip over devices that are really just layers of NetBSD
    266 	 * autoconf(9) we should just skip as they do not have any
    267 	 * OFW devices.
    268 	 */
    269 	if (device_is_a(device_parent(dev), "atapibus") ||
    270 	    device_is_a(device_parent(dev), "atabus") ||
    271 	    device_is_a(device_parent(dev), "pci") ||
    272 	    device_is_a(device_parent(dev), "scsibus")) {
    273 		if (device_parent(device_parent(dev)) != parent)
    274 			return;
    275 	} else {
    276 		if (device_parent(dev) != parent)
    277 			return;
    278 	}
    279 
    280 	/*
    281 	 * Get the address part of the current path component. The
    282 	 * last component of the canonical bootpath may have no
    283 	 * address (eg, "disk"), in which case we need to get the
    284 	 * address from the original bootpath instead.
    285 	 */
    286 	p = strchr(cp, '@');
    287 	if (!p) {
    288 		if (bp)
    289 			p = strchr(bp, '@');
    290 		if (!p)
    291 			addr = 0;
    292 		else
    293 			addr = strtoul(p + 1, &p, 16);
    294 	} else
    295 		addr = strtoul(p + 1, &p, 16);
    296 
    297 	/* if the current path has more address, grab that too */
    298 	if (p && *p == ',')
    299 		addr2 = strtoul(p + 1, &p, 16);
    300 	else
    301 		addr2 = 0;
    302 
    303 	if (device_is_a(device_parent(dev), "mainbus")) {
    304 		struct confargs *ca = aux;
    305 
    306 		if (strcmp(ca->ca_name, "ofw") == 0)		/* XXX */
    307 			return;
    308 		if (addr != ca->ca_reg[0])
    309 			return;
    310 	} else if (device_is_a(device_parent(dev), "pci")) {
    311 		struct pci_attach_args *pa = aux;
    312 
    313 		if (addr != pa->pa_device ||
    314 		    addr2 != pa->pa_function)
    315 			return;
    316 	} else if (device_is_a(device_parent(dev), "obio")) {
    317 		struct confargs *ca = aux;
    318 
    319 		if (addr != ca->ca_reg[0])
    320 			return;
    321 	} else if (device_is_a(device_parent(dev), "scsibus") ||
    322 		   device_is_a(device_parent(dev), "atapibus")) {
    323 		struct scsipibus_attach_args *sa = aux;
    324 
    325 		/* periph_target is target for scsi, drive # for atapi */
    326 		if (addr != sa->sa_periph->periph_target)
    327 			return;
    328 	} else if (device_is_a(device_parent(device_parent(dev)), "pciide") ||
    329 		   device_is_a(device_parent(device_parent(dev)), "viaide") ||
    330 		   device_is_a(device_parent(device_parent(dev)), "slide")) {
    331 		struct ata_device *adev = aux;
    332 
    333 		if (addr != adev->adev_channel ||
    334 		    addr2 != adev->adev_drv_data->drive)
    335 			return;
    336 	} else if (device_is_a(device_parent(device_parent(dev)), "wdc")) {
    337 		struct ata_device *adev = aux;
    338 
    339 		if (addr != adev->adev_drv_data->drive)
    340 			return;
    341 	} else
    342 		return;
    343 
    344 	/* If we reach this point, then dev is a match for the current
    345 	 * path component.
    346 	 */
    347 
    348 	if (p && *p) {
    349 		parent = dev;
    350 		cp = p;
    351 		bp = strchr(bp, '/');
    352 		if (bp)
    353 			bp++;
    354 		return;
    355 	} else {
    356 		booted_device = dev;
    357 		booted_partition = 0; /* XXX -- should be extracted from bootpath */
    358 		return;
    359 	}
    360 }
    361 
    362 /*
    363  * Setup root device.
    364  * Configure swap area.
    365  */
    366 void
    367 cpu_rootconf(void)
    368 {
    369 	printf("boot device: %s\n",
    370 	    booted_device ? booted_device->dv_xname : "<unknown>");
    371 
    372 	setroot(booted_device, booted_partition);
    373 }
    374 
    375 /*
    376  * Find OF-device corresponding to the PCI device.
    377  */
    378 int
    379 pcidev_to_ofdev(pci_chipset_tag_t pc, pcitag_t tag)
    380 {
    381 	int bus, dev, func;
    382 	u_int reg[5];
    383 	int p, q;
    384 	int l, b, d, f;
    385 
    386 	pci_decompose_tag(pc, tag, &bus, &dev, &func);
    387 
    388 	for (q = OF_peer(0); q; q = p) {
    389 		l = OF_getprop(q, "assigned-addresses", reg, sizeof(reg));
    390 		if (l > 4) {
    391 			b = (reg[0] >> 16) & 0xff;
    392 			d = (reg[0] >> 11) & 0x1f;
    393 			f = (reg[0] >> 8) & 0x07;
    394 
    395 			if (b == bus && d == dev && f == func)
    396 				return q;
    397 		}
    398 		if ((p = OF_child(q)))
    399 			continue;
    400 		while (q) {
    401 			if ((p = OF_peer(q)))
    402 				break;
    403 			q = OF_parent(q);
    404 		}
    405 	}
    406 	return 0;
    407 }
    408