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