Home | History | Annotate | Line # | Download | only in oea
ofw_autoconf.c revision 1.10
      1 /* $NetBSD: ofw_autoconf.c,v 1.10 2009/03/18 10:22:34 cegger 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.10 2009/03/18 10:22:34 cegger 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 		}
    253 	}
    254 
    255 	if (booted_device)
    256 		return;
    257 
    258 	/*
    259 	 * Skip over devices that are really just layers of NetBSD
    260 	 * autoconf(9) we should just skip as they do not have any
    261 	 * OFW devices.
    262 	 */
    263 	if (device_is_a(device_parent(dev), "atapibus") ||
    264 	    device_is_a(device_parent(dev), "atabus") ||
    265 	    device_is_a(device_parent(dev), "pci") ||
    266 	    device_is_a(device_parent(dev), "scsibus")) {
    267 		if (device_parent(device_parent(dev)) != parent)
    268 			return;
    269 	} else {
    270 		if (device_parent(dev) != parent)
    271 			return;
    272 	}
    273 
    274 	/*
    275 	 * Get the address part of the current path component. The
    276 	 * last component of the canonical bootpath may have no
    277 	 * address (eg, "disk"), in which case we need to get the
    278 	 * address from the original bootpath instead.
    279 	 */
    280 	p = strchr(cp, '@');
    281 	if (!p) {
    282 		if (bp)
    283 			p = strchr(bp, '@');
    284 		if (!p)
    285 			addr = 0;
    286 		else
    287 			addr = strtoul(p + 1, &p, 16);
    288 	} else
    289 		addr = strtoul(p + 1, &p, 16);
    290 
    291 	/* if the current path has more address, grab that too */
    292 	if (p && *p == ',')
    293 		addr2 = strtoul(p + 1, &p, 16);
    294 	else
    295 		addr2 = 0;
    296 
    297 	if (device_is_a(device_parent(dev), "mainbus")) {
    298 		struct confargs *ca = aux;
    299 
    300 		if (strcmp(ca->ca_name, "ofw") == 0)		/* XXX */
    301 			return;
    302 		if (addr != ca->ca_reg[0])
    303 			return;
    304 	} else if (device_is_a(device_parent(dev), "pci")) {
    305 		struct pci_attach_args *pa = aux;
    306 
    307 		if (addr != pa->pa_device ||
    308 		    addr2 != pa->pa_function)
    309 			return;
    310 	} else if (device_is_a(device_parent(dev), "obio")) {
    311 		struct confargs *ca = aux;
    312 
    313 		if (addr != ca->ca_reg[0])
    314 			return;
    315 	} else if (device_is_a(device_parent(dev), "scsibus") ||
    316 		   device_is_a(device_parent(dev), "atapibus")) {
    317 		struct scsipibus_attach_args *sa = aux;
    318 
    319 		/* periph_target is target for scsi, drive # for atapi */
    320 		if (addr != sa->sa_periph->periph_target)
    321 			return;
    322 	} else if (device_is_a(device_parent(device_parent(dev)), "pciide") ||
    323 		   device_is_a(device_parent(device_parent(dev)), "viaide") ||
    324 		   device_is_a(device_parent(device_parent(dev)), "slide")) {
    325 		struct ata_device *adev = aux;
    326 
    327 		if (addr != adev->adev_channel ||
    328 		    addr2 != adev->adev_drv_data->drive)
    329 			return;
    330 	} else if (device_is_a(device_parent(device_parent(dev)), "wdc")) {
    331 		struct ata_device *adev = aux;
    332 
    333 		if (addr != adev->adev_drv_data->drive)
    334 			return;
    335 	} else
    336 		return;
    337 
    338 	/* If we reach this point, then dev is a match for the current
    339 	 * path component.
    340 	 */
    341 
    342 	if (p && *p) {
    343 		parent = dev;
    344 		cp = p;
    345 		bp = strchr(bp, '/');
    346 		if (bp)
    347 			bp++;
    348 		return;
    349 	} else {
    350 		booted_device = dev;
    351 		booted_partition = 0; /* XXX -- should be extracted from bootpath */
    352 		return;
    353 	}
    354 }
    355 
    356 /*
    357  * Setup root device.
    358  * Configure swap area.
    359  */
    360 void
    361 cpu_rootconf(void)
    362 {
    363 	printf("boot device: %s\n",
    364 	    booted_device ? booted_device->dv_xname : "<unknown>");
    365 
    366 	setroot(booted_device, booted_partition);
    367 }
    368 
    369 /*
    370  * Find OF-device corresponding to the PCI device.
    371  */
    372 int
    373 pcidev_to_ofdev(pci_chipset_tag_t pc, pcitag_t tag)
    374 {
    375 	int bus, dev, func;
    376 	u_int reg[5];
    377 	int p, q;
    378 	int l, b, d, f;
    379 
    380 	pci_decompose_tag(pc, tag, &bus, &dev, &func);
    381 
    382 	for (q = OF_peer(0); q; q = p) {
    383 		l = OF_getprop(q, "assigned-addresses", reg, sizeof(reg));
    384 		if (l > 4) {
    385 			b = (reg[0] >> 16) & 0xff;
    386 			d = (reg[0] >> 11) & 0x1f;
    387 			f = (reg[0] >> 8) & 0x07;
    388 
    389 			if (b == bus && d == dev && f == func)
    390 				return q;
    391 		}
    392 		if ((p = OF_child(q)))
    393 			continue;
    394 		while (q) {
    395 			if ((p = OF_peer(q)))
    396 				break;
    397 			q = OF_parent(q);
    398 		}
    399 	}
    400 	return 0;
    401 }
    402