Home | History | Annotate | Line # | Download | only in oea
ofw_autoconf.c revision 1.4.2.2
      1  1.4.2.2  ad /* $NetBSD: ofw_autoconf.c,v 1.4.2.2 2007/12/03 19:03:59 ad Exp $ */
      2  1.4.2.2  ad /*
      3  1.4.2.2  ad  * Copyright (C) 1995, 1996 Wolfgang Solfrank.
      4  1.4.2.2  ad  * Copyright (C) 1995, 1996 TooLs GmbH.
      5  1.4.2.2  ad  * All rights reserved.
      6  1.4.2.2  ad  *
      7  1.4.2.2  ad  * Redistribution and use in source and binary forms, with or without
      8  1.4.2.2  ad  * modification, are permitted provided that the following conditions
      9  1.4.2.2  ad  * are met:
     10  1.4.2.2  ad  * 1. Redistributions of source code must retain the above copyright
     11  1.4.2.2  ad  *    notice, this list of conditions and the following disclaimer.
     12  1.4.2.2  ad  * 2. Redistributions in binary form must reproduce the above copyright
     13  1.4.2.2  ad  *    notice, this list of conditions and the following disclaimer in the
     14  1.4.2.2  ad  *    documentation and/or other materials provided with the distribution.
     15  1.4.2.2  ad  * 3. All advertising materials mentioning features or use of this software
     16  1.4.2.2  ad  *    must display the following acknowledgement:
     17  1.4.2.2  ad  *      This product includes software developed by TooLs GmbH.
     18  1.4.2.2  ad  * 4. The name of TooLs GmbH may not be used to endorse or promote products
     19  1.4.2.2  ad  *    derived from this software without specific prior written permission.
     20  1.4.2.2  ad  *
     21  1.4.2.2  ad  * THIS SOFTWARE IS PROVIDED BY TOOLS GMBH ``AS IS'' AND ANY EXPRESS OR
     22  1.4.2.2  ad  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     23  1.4.2.2  ad  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     24  1.4.2.2  ad  * IN NO EVENT SHALL TOOLS GMBH BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
     25  1.4.2.2  ad  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
     26  1.4.2.2  ad  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
     27  1.4.2.2  ad  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
     28  1.4.2.2  ad  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
     29  1.4.2.2  ad  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
     30  1.4.2.2  ad  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     31  1.4.2.2  ad  */
     32  1.4.2.2  ad 
     33  1.4.2.2  ad #include <sys/cdefs.h>
     34  1.4.2.2  ad __KERNEL_RCSID(0, "$NetBSD: ofw_autoconf.c,v 1.4.2.2 2007/12/03 19:03:59 ad Exp $");
     35  1.4.2.2  ad 
     36  1.4.2.2  ad #include <sys/param.h>
     37  1.4.2.2  ad #include <sys/conf.h>
     38  1.4.2.2  ad #include <sys/device.h>
     39  1.4.2.2  ad #include <sys/reboot.h>
     40  1.4.2.2  ad #include <sys/systm.h>
     41  1.4.2.2  ad 
     42  1.4.2.2  ad #include <uvm/uvm_extern.h>
     43  1.4.2.2  ad 
     44  1.4.2.2  ad #include <machine/autoconf.h>
     45  1.4.2.2  ad #include <machine/bus.h>
     46  1.4.2.2  ad #include <machine/stdarg.h>
     47  1.4.2.2  ad 
     48  1.4.2.2  ad #include <dev/ofw/openfirm.h>
     49  1.4.2.2  ad #include <dev/pci/pcivar.h>
     50  1.4.2.2  ad #include <dev/scsipi/scsi_all.h>
     51  1.4.2.2  ad #include <dev/scsipi/scsipi_all.h>
     52  1.4.2.2  ad #include <dev/scsipi/scsiconf.h>
     53  1.4.2.2  ad #include <dev/ata/atavar.h>
     54  1.4.2.2  ad #include <dev/ic/wdcvar.h>
     55  1.4.2.2  ad 
     56  1.4.2.2  ad extern char bootpath[256];
     57  1.4.2.2  ad char cbootpath[256];
     58  1.4.2.2  ad int console_node = 0, console_instance = 0;
     59  1.4.2.2  ad 
     60  1.4.2.2  ad static void canonicalize_bootpath(void);
     61  1.4.2.2  ad 
     62  1.4.2.2  ad /*
     63  1.4.2.2  ad  * Determine device configuration for a machine.
     64  1.4.2.2  ad  */
     65  1.4.2.2  ad void
     66  1.4.2.2  ad cpu_configure(void)
     67  1.4.2.2  ad {
     68  1.4.2.2  ad 	init_interrupt();
     69  1.4.2.2  ad 	canonicalize_bootpath();
     70  1.4.2.2  ad 
     71  1.4.2.2  ad 	if (config_rootfound("mainbus", NULL) == NULL)
     72  1.4.2.2  ad 		panic("configure: mainbus not configured");
     73  1.4.2.2  ad 
     74  1.4.2.2  ad 	genppc_cpu_configure();
     75  1.4.2.2  ad }
     76  1.4.2.2  ad 
     77  1.4.2.2  ad static void
     78  1.4.2.2  ad canonicalize_bootpath(void)
     79  1.4.2.2  ad {
     80  1.4.2.2  ad 	int node;
     81  1.4.2.2  ad 	char *p, *lastp;
     82  1.4.2.2  ad 	char last[32];
     83  1.4.2.2  ad 
     84  1.4.2.2  ad 	/*
     85  1.4.2.2  ad 	 * If the bootpath doesn't start with a / then it isn't
     86  1.4.2.2  ad 	 * an OFW path and probably is an alias, so look up the alias
     87  1.4.2.2  ad 	 * and regenerate the full bootpath so device_register will work.
     88  1.4.2.2  ad 	 */
     89  1.4.2.2  ad 	if (bootpath[0] != '/' && bootpath[0] != '\0') {
     90  1.4.2.2  ad 		int aliases = OF_finddevice("/aliases");
     91  1.4.2.2  ad 		char tmpbuf[100];
     92  1.4.2.2  ad 		char aliasbuf[256];
     93  1.4.2.2  ad 		if (aliases != 0) {
     94  1.4.2.2  ad 			char *cp1, *cp2, *cp;
     95  1.4.2.2  ad 			char saved_ch = '\0';
     96  1.4.2.2  ad 			int len;
     97  1.4.2.2  ad 			cp1 = strchr(bootpath, ':');
     98  1.4.2.2  ad 			cp2 = strchr(bootpath, ',');
     99  1.4.2.2  ad 			cp = cp1;
    100  1.4.2.2  ad 			if (cp1 == NULL || (cp2 != NULL && cp2 < cp1))
    101  1.4.2.2  ad 				cp = cp2;
    102  1.4.2.2  ad 			tmpbuf[0] = '\0';
    103  1.4.2.2  ad 			if (cp != NULL) {
    104  1.4.2.2  ad 				strcpy(tmpbuf, cp);
    105  1.4.2.2  ad 				saved_ch = *cp;
    106  1.4.2.2  ad 				*cp = '\0';
    107  1.4.2.2  ad 			}
    108  1.4.2.2  ad 			len = OF_getprop(aliases, bootpath, aliasbuf,
    109  1.4.2.2  ad 			    sizeof(aliasbuf));
    110  1.4.2.2  ad 			if (len > 0) {
    111  1.4.2.2  ad 				if (aliasbuf[len-1] == '\0')
    112  1.4.2.2  ad 					len--;
    113  1.4.2.2  ad 				memcpy(bootpath, aliasbuf, len);
    114  1.4.2.2  ad 				strcpy(&bootpath[len], tmpbuf);
    115  1.4.2.2  ad 			} else {
    116  1.4.2.2  ad 				*cp = saved_ch;
    117  1.4.2.2  ad 			}
    118  1.4.2.2  ad 		}
    119  1.4.2.2  ad 	}
    120  1.4.2.2  ad 
    121  1.4.2.2  ad 	/*
    122  1.4.2.2  ad 	 * Strip kernel name.  bootpath contains "OF-path"/"kernel".
    123  1.4.2.2  ad 	 *
    124  1.4.2.2  ad 	 * for example:
    125  1.4.2.2  ad 	 *   /bandit@F2000000/gc@10/53c94@10000/sd@0,0/netbsd	(OF-1.x)
    126  1.4.2.2  ad 	 *   /pci/mac-io/ata-3@2000/disk@0:0/netbsd.new		(OF-3.x)
    127  1.4.2.2  ad 	 */
    128  1.4.2.2  ad 	strcpy(cbootpath, bootpath);
    129  1.4.2.2  ad 	while ((node = OF_finddevice(cbootpath)) == -1) {
    130  1.4.2.2  ad 		if ((p = strrchr(cbootpath, '/')) == NULL)
    131  1.4.2.2  ad 			break;
    132  1.4.2.2  ad 		*p = '\0';
    133  1.4.2.2  ad 	}
    134  1.4.2.2  ad 
    135  1.4.2.2  ad 	if (node == -1) {
    136  1.4.2.2  ad 		/* Cannot canonicalize... use bootpath anyway. */
    137  1.4.2.2  ad 		strcpy(cbootpath, bootpath);
    138  1.4.2.2  ad 
    139  1.4.2.2  ad 		return;
    140  1.4.2.2  ad 	}
    141  1.4.2.2  ad 
    142  1.4.2.2  ad 	/*
    143  1.4.2.2  ad 	 * cbootpath is a valid OF path.  Use package-to-path to
    144  1.4.2.2  ad 	 * canonicalize pathname.
    145  1.4.2.2  ad 	 */
    146  1.4.2.2  ad 
    147  1.4.2.2  ad 	/* Back up the last component for later use. */
    148  1.4.2.2  ad 	if ((p = strrchr(cbootpath, '/')) != NULL)
    149  1.4.2.2  ad 		strcpy(last, p + 1);
    150  1.4.2.2  ad 	else
    151  1.4.2.2  ad 		last[0] = '\0';
    152  1.4.2.2  ad 
    153  1.4.2.2  ad 	memset(cbootpath, 0, sizeof(cbootpath));
    154  1.4.2.2  ad 	OF_package_to_path(node, cbootpath, sizeof(cbootpath) - 1);
    155  1.4.2.2  ad 
    156  1.4.2.2  ad 	/*
    157  1.4.2.2  ad 	 * OF_1.x (at least) always returns addr == 0 for
    158  1.4.2.2  ad 	 * SCSI disks (i.e. "/bandit (at) .../.../sd@0,0").
    159  1.4.2.2  ad 	 */
    160  1.4.2.2  ad 	lastp = strrchr(cbootpath, '/');
    161  1.4.2.2  ad 	if (lastp != NULL) {
    162  1.4.2.2  ad 		lastp++;
    163  1.4.2.2  ad 		if (strncmp(lastp, "sd@", 3) == 0
    164  1.4.2.2  ad 		    && strncmp(last, "sd@", 3) == 0)
    165  1.4.2.2  ad 			strcpy(lastp, last);
    166  1.4.2.2  ad 	} else {
    167  1.4.2.2  ad 		lastp = cbootpath;
    168  1.4.2.2  ad 	}
    169  1.4.2.2  ad 
    170  1.4.2.2  ad 	/*
    171  1.4.2.2  ad 	 * At this point, cbootpath contains like:
    172  1.4.2.2  ad 	 * "/pci@80000000/mac-io@10/ata-3@20000/disk"
    173  1.4.2.2  ad 	 *
    174  1.4.2.2  ad 	 * The last component may have no address... so append it.
    175  1.4.2.2  ad 	 */
    176  1.4.2.2  ad 	if (strchr(lastp, '@') == NULL) {
    177  1.4.2.2  ad 		/* Append it. */
    178  1.4.2.2  ad 		if ((p = strrchr(last, '@')) != NULL)
    179  1.4.2.2  ad 			strcat(cbootpath, p);
    180  1.4.2.2  ad 	}
    181  1.4.2.2  ad 
    182  1.4.2.2  ad 	if ((p = strrchr(lastp, ':')) != NULL) {
    183  1.4.2.2  ad 		*p++ = '\0';
    184  1.4.2.2  ad 		/* booted_partition = *p - '0';		XXX correct? */
    185  1.4.2.2  ad 	}
    186  1.4.2.2  ad 
    187  1.4.2.2  ad 	/* XXX Does this belong here, or device_register()? */
    188  1.4.2.2  ad 	if ((p = strrchr(lastp, ',')) != NULL)
    189  1.4.2.2  ad 		*p = '\0';
    190  1.4.2.2  ad }
    191  1.4.2.2  ad 
    192  1.4.2.2  ad /*
    193  1.4.2.2  ad  * device_register is called from config_attach as each device is
    194  1.4.2.2  ad  * attached. We use it to find the NetBSD device corresponding to the
    195  1.4.2.2  ad  * known OF boot device.
    196  1.4.2.2  ad  */
    197  1.4.2.2  ad void
    198  1.4.2.2  ad device_register(dev, aux)
    199  1.4.2.2  ad 	struct device *dev;
    200  1.4.2.2  ad 	void *aux;
    201  1.4.2.2  ad {
    202  1.4.2.2  ad 	static struct device *parent;
    203  1.4.2.2  ad 	static char *bp = bootpath + 1, *cp = cbootpath;
    204  1.4.2.2  ad 	unsigned long addr;
    205  1.4.2.2  ad 	char *p;
    206  1.4.2.2  ad 
    207  1.4.2.2  ad 	/* Skip over devices not represented in the OF tree. */
    208  1.4.2.2  ad 	if (device_is_a(dev, "mainbus")) {
    209  1.4.2.2  ad 		parent = dev;
    210  1.4.2.2  ad 		return;
    211  1.4.2.2  ad 	}
    212  1.4.2.2  ad 	if (device_is_a(dev, "atapibus") || device_is_a(dev, "pci") ||
    213  1.4.2.2  ad 	    device_is_a(dev, "scsibus") || device_is_a(dev, "atabus"))
    214  1.4.2.2  ad 		return;
    215  1.4.2.2  ad 
    216  1.4.2.2  ad 	if (device_is_a(device_parent(dev), "pci")) {
    217  1.4.2.2  ad 		/* see if this is going to be console */
    218  1.4.2.2  ad 		struct pci_attach_args *pa = aux;
    219  1.4.2.2  ad 		prop_dictionary_t dict;
    220  1.4.2.2  ad 		int node;
    221  1.4.2.2  ad 		char name[32];
    222  1.4.2.2  ad 
    223  1.4.2.2  ad 		dict = device_properties(dev);
    224  1.4.2.2  ad 		node = pcidev_to_ofdev(pa->pa_pc, pa->pa_tag);
    225  1.4.2.2  ad 
    226  1.4.2.2  ad 		if (node != 0) {
    227  1.4.2.2  ad 			prop_dictionary_set_uint32(dict, "device_node", node);
    228  1.4.2.2  ad 
    229  1.4.2.2  ad 			memset(name, 0, sizeof(name));
    230  1.4.2.2  ad 			OF_getprop(node, "device_type", name, sizeof(name));
    231  1.4.2.2  ad 			if (strcmp(name, "display") == 0) {
    232  1.4.2.2  ad 				/* setup display properties for fb driver */
    233  1.4.2.2  ad 				prop_dictionary_set_bool(dict, "is_console", 0);
    234  1.4.2.2  ad 				copy_disp_props(dev, node, dict);
    235  1.4.2.2  ad 			}
    236  1.4.2.2  ad 		}
    237  1.4.2.2  ad 	}
    238  1.4.2.2  ad 
    239  1.4.2.2  ad 	if (booted_device)
    240  1.4.2.2  ad 		return;
    241  1.4.2.2  ad 
    242  1.4.2.2  ad 	if (device_is_a(device_parent(dev), "atapibus") ||
    243  1.4.2.2  ad 	    device_is_a(device_parent(dev), "atabus") ||
    244  1.4.2.2  ad 	    device_is_a(device_parent(dev), "pci") ||
    245  1.4.2.2  ad 	    device_is_a(device_parent(dev), "scsibus")) {
    246  1.4.2.2  ad 		if (device_parent(device_parent(dev)) != parent)
    247  1.4.2.2  ad 			return;
    248  1.4.2.2  ad 	} else {
    249  1.4.2.2  ad 		if (device_parent(dev) != parent)
    250  1.4.2.2  ad 			return;
    251  1.4.2.2  ad 	}
    252  1.4.2.2  ad 
    253  1.4.2.2  ad 	/* Get the address part of the current path component. The
    254  1.4.2.2  ad 	 * last component of the canonical bootpath may have no
    255  1.4.2.2  ad 	 * address (eg, "disk"), in which case we need to get the
    256  1.4.2.2  ad 	 * address from the original bootpath instead.
    257  1.4.2.2  ad 	 */
    258  1.4.2.2  ad 	p = strchr(cp, '@');
    259  1.4.2.2  ad 	if (!p) {
    260  1.4.2.2  ad 		if (bp)
    261  1.4.2.2  ad 			p = strchr(bp, '@');
    262  1.4.2.2  ad 		if (!p)
    263  1.4.2.2  ad 			addr = 0;
    264  1.4.2.2  ad 		else {
    265  1.4.2.2  ad 			addr = strtoul(p + 1, NULL, 16);
    266  1.4.2.2  ad 			p = NULL;
    267  1.4.2.2  ad 		}
    268  1.4.2.2  ad 	} else
    269  1.4.2.2  ad 		addr = strtoul(p + 1, &p, 16);
    270  1.4.2.2  ad 
    271  1.4.2.2  ad 	if (device_is_a(device_parent(dev), "mainbus")) {
    272  1.4.2.2  ad 		struct confargs *ca = aux;
    273  1.4.2.2  ad 
    274  1.4.2.2  ad 		if (strcmp(ca->ca_name, "ofw") == 0)		/* XXX */
    275  1.4.2.2  ad 			return;
    276  1.4.2.2  ad 		if (addr != ca->ca_reg[0])
    277  1.4.2.2  ad 			return;
    278  1.4.2.2  ad 	} else if (device_is_a(device_parent(dev), "pci")) {
    279  1.4.2.2  ad 		struct pci_attach_args *pa = aux;
    280  1.4.2.2  ad 
    281  1.4.2.2  ad 		if (addr != pa->pa_device)
    282  1.4.2.2  ad 			return;
    283  1.4.2.2  ad 	} else if (device_is_a(device_parent(dev), "obio")) {
    284  1.4.2.2  ad 		struct confargs *ca = aux;
    285  1.4.2.2  ad 
    286  1.4.2.2  ad 		if (addr != ca->ca_reg[0])
    287  1.4.2.2  ad 			return;
    288  1.4.2.2  ad 	} else if (device_is_a(device_parent(dev), "scsibus") ||
    289  1.4.2.2  ad 		   device_is_a(device_parent(dev), "atapibus")) {
    290  1.4.2.2  ad 		struct scsipibus_attach_args *sa = aux;
    291  1.4.2.2  ad 
    292  1.4.2.2  ad 		/* periph_target is target for scsi, drive # for atapi */
    293  1.4.2.2  ad 		if (addr != sa->sa_periph->periph_target)
    294  1.4.2.2  ad 			return;
    295  1.4.2.2  ad 	} else if (device_is_a(device_parent(device_parent(dev)), "pciide")) {
    296  1.4.2.2  ad 		struct ata_device *adev = aux;
    297  1.4.2.2  ad 
    298  1.4.2.2  ad 		if (addr != adev->adev_drv_data->drive)
    299  1.4.2.2  ad 			return;
    300  1.4.2.2  ad 
    301  1.4.2.2  ad 		/*
    302  1.4.2.2  ad 		 * OF splits channel and drive into separate path
    303  1.4.2.2  ad 		 * components, so check the addr part of the next
    304  1.4.2.2  ad 		 * component. (Ignore bp, because the canonical path
    305  1.4.2.2  ad 		 * will be complete in the pciide case.)
    306  1.4.2.2  ad 		 */
    307  1.4.2.2  ad 		p = strchr(p, '@');
    308  1.4.2.2  ad 		if (!p++)
    309  1.4.2.2  ad 			return;
    310  1.4.2.2  ad 		if (strtoul(p, &p, 16) != adev->adev_drv_data->drive)
    311  1.4.2.2  ad 			return;
    312  1.4.2.2  ad 	} else if (device_is_a(device_parent(device_parent(dev)), "wdc")) {
    313  1.4.2.2  ad 		struct ata_device *adev = aux;
    314  1.4.2.2  ad 
    315  1.4.2.2  ad 		if (addr != adev->adev_drv_data->drive)
    316  1.4.2.2  ad 			return;
    317  1.4.2.2  ad 	} else
    318  1.4.2.2  ad 		return;
    319  1.4.2.2  ad 
    320  1.4.2.2  ad 	/* If we reach this point, then dev is a match for the current
    321  1.4.2.2  ad 	 * path component.
    322  1.4.2.2  ad 	 */
    323  1.4.2.2  ad 
    324  1.4.2.2  ad 	if (p && *p) {
    325  1.4.2.2  ad 		parent = dev;
    326  1.4.2.2  ad 		cp = p;
    327  1.4.2.2  ad 		bp = strchr(bp, '/');
    328  1.4.2.2  ad 		if (bp)
    329  1.4.2.2  ad 			bp++;
    330  1.4.2.2  ad 		return;
    331  1.4.2.2  ad 	} else {
    332  1.4.2.2  ad 		booted_device = dev;
    333  1.4.2.2  ad 		booted_partition = 0; /* XXX -- should be extracted from bootpath */
    334  1.4.2.2  ad 		return;
    335  1.4.2.2  ad 	}
    336  1.4.2.2  ad }
    337  1.4.2.2  ad 
    338  1.4.2.2  ad /*
    339  1.4.2.2  ad  * Setup root device.
    340  1.4.2.2  ad  * Configure swap area.
    341  1.4.2.2  ad  */
    342  1.4.2.2  ad void
    343  1.4.2.2  ad cpu_rootconf()
    344  1.4.2.2  ad {
    345  1.4.2.2  ad 	printf("boot device: %s\n",
    346  1.4.2.2  ad 	    booted_device ? booted_device->dv_xname : "<unknown>");
    347  1.4.2.2  ad 
    348  1.4.2.2  ad 	setroot(booted_device, booted_partition);
    349  1.4.2.2  ad }
    350  1.4.2.2  ad 
    351  1.4.2.2  ad /*
    352  1.4.2.2  ad  * Find OF-device corresponding to the PCI device.
    353  1.4.2.2  ad  */
    354  1.4.2.2  ad int
    355  1.4.2.2  ad pcidev_to_ofdev(pci_chipset_tag_t pc, pcitag_t tag)
    356  1.4.2.2  ad {
    357  1.4.2.2  ad 	int bus, dev, func;
    358  1.4.2.2  ad 	u_int reg[5];
    359  1.4.2.2  ad 	int p, q;
    360  1.4.2.2  ad 	int l, b, d, f;
    361  1.4.2.2  ad 
    362  1.4.2.2  ad 	pci_decompose_tag(pc, tag, &bus, &dev, &func);
    363  1.4.2.2  ad 
    364  1.4.2.2  ad 	for (q = OF_peer(0); q; q = p) {
    365  1.4.2.2  ad 		l = OF_getprop(q, "assigned-addresses", reg, sizeof(reg));
    366  1.4.2.2  ad 		if (l > 4) {
    367  1.4.2.2  ad 			b = (reg[0] >> 16) & 0xff;
    368  1.4.2.2  ad 			d = (reg[0] >> 11) & 0x1f;
    369  1.4.2.2  ad 			f = (reg[0] >> 8) & 0x07;
    370  1.4.2.2  ad 
    371  1.4.2.2  ad 			if (b == bus && d == dev && f == func)
    372  1.4.2.2  ad 				return q;
    373  1.4.2.2  ad 		}
    374  1.4.2.2  ad 		if ((p = OF_child(q)))
    375  1.4.2.2  ad 			continue;
    376  1.4.2.2  ad 		while (q) {
    377  1.4.2.2  ad 			if ((p = OF_peer(q)))
    378  1.4.2.2  ad 				break;
    379  1.4.2.2  ad 			q = OF_parent(q);
    380  1.4.2.2  ad 		}
    381  1.4.2.2  ad 	}
    382  1.4.2.2  ad 	return 0;
    383  1.4.2.2  ad }
    384  1.4.2.2  ad /* $NetBSD: ofw_autoconf.c,v 1.4.2.2 2007/12/03 19:03:59 ad Exp $ */
    385  1.4.2.2  ad /*
    386  1.4.2.2  ad  * Copyright (C) 1995, 1996 Wolfgang Solfrank.
    387  1.4.2.2  ad  * Copyright (C) 1995, 1996 TooLs GmbH.
    388  1.4.2.2  ad  * All rights reserved.
    389  1.4.2.2  ad  *
    390  1.4.2.2  ad  * Redistribution and use in source and binary forms, with or without
    391  1.4.2.2  ad  * modification, are permitted provided that the following conditions
    392  1.4.2.2  ad  * are met:
    393  1.4.2.2  ad  * 1. Redistributions of source code must retain the above copyright
    394  1.4.2.2  ad  *    notice, this list of conditions and the following disclaimer.
    395  1.4.2.2  ad  * 2. Redistributions in binary form must reproduce the above copyright
    396  1.4.2.2  ad  *    notice, this list of conditions and the following disclaimer in the
    397  1.4.2.2  ad  *    documentation and/or other materials provided with the distribution.
    398  1.4.2.2  ad  * 3. All advertising materials mentioning features or use of this software
    399  1.4.2.2  ad  *    must display the following acknowledgement:
    400  1.4.2.2  ad  *      This product includes software developed by TooLs GmbH.
    401  1.4.2.2  ad  * 4. The name of TooLs GmbH may not be used to endorse or promote products
    402  1.4.2.2  ad  *    derived from this software without specific prior written permission.
    403  1.4.2.2  ad  *
    404  1.4.2.2  ad  * THIS SOFTWARE IS PROVIDED BY TOOLS GMBH ``AS IS'' AND ANY EXPRESS OR
    405  1.4.2.2  ad  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
    406  1.4.2.2  ad  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
    407  1.4.2.2  ad  * IN NO EVENT SHALL TOOLS GMBH BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
    408  1.4.2.2  ad  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
    409  1.4.2.2  ad  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
    410  1.4.2.2  ad  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
    411  1.4.2.2  ad  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
    412  1.4.2.2  ad  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
    413  1.4.2.2  ad  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
    414  1.4.2.2  ad  */
    415  1.4.2.2  ad 
    416  1.4.2.2  ad #include <sys/cdefs.h>
    417  1.4.2.2  ad __KERNEL_RCSID(0, "$NetBSD: ofw_autoconf.c,v 1.4.2.2 2007/12/03 19:03:59 ad Exp $");
    418  1.4.2.2  ad 
    419  1.4.2.2  ad #include <sys/param.h>
    420  1.4.2.2  ad #include <sys/conf.h>
    421  1.4.2.2  ad #include <sys/device.h>
    422  1.4.2.2  ad #include <sys/reboot.h>
    423  1.4.2.2  ad #include <sys/systm.h>
    424  1.4.2.2  ad 
    425  1.4.2.2  ad #include <uvm/uvm_extern.h>
    426  1.4.2.2  ad 
    427  1.4.2.2  ad #include <machine/autoconf.h>
    428  1.4.2.2  ad #include <machine/bus.h>
    429  1.4.2.2  ad #include <machine/stdarg.h>
    430  1.4.2.2  ad 
    431  1.4.2.2  ad #include <dev/ofw/openfirm.h>
    432  1.4.2.2  ad #include <dev/pci/pcivar.h>
    433  1.4.2.2  ad #include <dev/scsipi/scsi_all.h>
    434  1.4.2.2  ad #include <dev/scsipi/scsipi_all.h>
    435  1.4.2.2  ad #include <dev/scsipi/scsiconf.h>
    436  1.4.2.2  ad #include <dev/ata/atavar.h>
    437  1.4.2.2  ad #include <dev/ic/wdcvar.h>
    438  1.4.2.2  ad 
    439  1.4.2.2  ad extern char bootpath[256];
    440  1.4.2.2  ad char cbootpath[256];
    441  1.4.2.2  ad int console_node = 0, console_instance = 0;
    442  1.4.2.2  ad 
    443  1.4.2.2  ad static void canonicalize_bootpath(void);
    444  1.4.2.2  ad 
    445  1.4.2.2  ad /*
    446  1.4.2.2  ad  * Determine device configuration for a machine.
    447  1.4.2.2  ad  */
    448  1.4.2.2  ad void
    449  1.4.2.2  ad cpu_configure(void)
    450  1.4.2.2  ad {
    451  1.4.2.2  ad 	init_interrupt();
    452  1.4.2.2  ad 	canonicalize_bootpath();
    453  1.4.2.2  ad 
    454  1.4.2.2  ad 	if (config_rootfound("mainbus", NULL) == NULL)
    455  1.4.2.2  ad 		panic("configure: mainbus not configured");
    456  1.4.2.2  ad 
    457  1.4.2.2  ad 	genppc_cpu_configure();
    458  1.4.2.2  ad }
    459  1.4.2.2  ad 
    460  1.4.2.2  ad static void
    461  1.4.2.2  ad canonicalize_bootpath(void)
    462  1.4.2.2  ad {
    463  1.4.2.2  ad 	int node;
    464  1.4.2.2  ad 	char *p, *lastp;
    465  1.4.2.2  ad 	char last[32];
    466  1.4.2.2  ad 
    467  1.4.2.2  ad 	/*
    468  1.4.2.2  ad 	 * If the bootpath doesn't start with a / then it isn't
    469  1.4.2.2  ad 	 * an OFW path and probably is an alias, so look up the alias
    470  1.4.2.2  ad 	 * and regenerate the full bootpath so device_register will work.
    471  1.4.2.2  ad 	 */
    472  1.4.2.2  ad 	if (bootpath[0] != '/' && bootpath[0] != '\0') {
    473  1.4.2.2  ad 		int aliases = OF_finddevice("/aliases");
    474  1.4.2.2  ad 		char tmpbuf[100];
    475  1.4.2.2  ad 		char aliasbuf[256];
    476  1.4.2.2  ad 		if (aliases != 0) {
    477  1.4.2.2  ad 			char *cp1, *cp2, *cp;
    478  1.4.2.2  ad 			char saved_ch = '\0';
    479  1.4.2.2  ad 			int len;
    480  1.4.2.2  ad 			cp1 = strchr(bootpath, ':');
    481  1.4.2.2  ad 			cp2 = strchr(bootpath, ',');
    482  1.4.2.2  ad 			cp = cp1;
    483  1.4.2.2  ad 			if (cp1 == NULL || (cp2 != NULL && cp2 < cp1))
    484  1.4.2.2  ad 				cp = cp2;
    485  1.4.2.2  ad 			tmpbuf[0] = '\0';
    486  1.4.2.2  ad 			if (cp != NULL) {
    487  1.4.2.2  ad 				strcpy(tmpbuf, cp);
    488  1.4.2.2  ad 				saved_ch = *cp;
    489  1.4.2.2  ad 				*cp = '\0';
    490  1.4.2.2  ad 			}
    491  1.4.2.2  ad 			len = OF_getprop(aliases, bootpath, aliasbuf,
    492  1.4.2.2  ad 			    sizeof(aliasbuf));
    493  1.4.2.2  ad 			if (len > 0) {
    494  1.4.2.2  ad 				if (aliasbuf[len-1] == '\0')
    495  1.4.2.2  ad 					len--;
    496  1.4.2.2  ad 				memcpy(bootpath, aliasbuf, len);
    497  1.4.2.2  ad 				strcpy(&bootpath[len], tmpbuf);
    498  1.4.2.2  ad 			} else {
    499  1.4.2.2  ad 				*cp = saved_ch;
    500  1.4.2.2  ad 			}
    501  1.4.2.2  ad 		}
    502  1.4.2.2  ad 	}
    503  1.4.2.2  ad 
    504  1.4.2.2  ad 	/*
    505  1.4.2.2  ad 	 * Strip kernel name.  bootpath contains "OF-path"/"kernel".
    506  1.4.2.2  ad 	 *
    507  1.4.2.2  ad 	 * for example:
    508  1.4.2.2  ad 	 *   /bandit@F2000000/gc@10/53c94@10000/sd@0,0/netbsd	(OF-1.x)
    509  1.4.2.2  ad 	 *   /pci/mac-io/ata-3@2000/disk@0:0/netbsd.new		(OF-3.x)
    510  1.4.2.2  ad 	 */
    511  1.4.2.2  ad 	strcpy(cbootpath, bootpath);
    512  1.4.2.2  ad 	while ((node = OF_finddevice(cbootpath)) == -1) {
    513  1.4.2.2  ad 		if ((p = strrchr(cbootpath, '/')) == NULL)
    514  1.4.2.2  ad 			break;
    515  1.4.2.2  ad 		*p = '\0';
    516  1.4.2.2  ad 	}
    517  1.4.2.2  ad 
    518  1.4.2.2  ad 	if (node == -1) {
    519  1.4.2.2  ad 		/* Cannot canonicalize... use bootpath anyway. */
    520  1.4.2.2  ad 		strcpy(cbootpath, bootpath);
    521  1.4.2.2  ad 
    522  1.4.2.2  ad 		return;
    523  1.4.2.2  ad 	}
    524  1.4.2.2  ad 
    525  1.4.2.2  ad 	/*
    526  1.4.2.2  ad 	 * cbootpath is a valid OF path.  Use package-to-path to
    527  1.4.2.2  ad 	 * canonicalize pathname.
    528  1.4.2.2  ad 	 */
    529  1.4.2.2  ad 
    530  1.4.2.2  ad 	/* Back up the last component for later use. */
    531  1.4.2.2  ad 	if ((p = strrchr(cbootpath, '/')) != NULL)
    532  1.4.2.2  ad 		strcpy(last, p + 1);
    533  1.4.2.2  ad 	else
    534  1.4.2.2  ad 		last[0] = '\0';
    535  1.4.2.2  ad 
    536  1.4.2.2  ad 	memset(cbootpath, 0, sizeof(cbootpath));
    537  1.4.2.2  ad 	OF_package_to_path(node, cbootpath, sizeof(cbootpath) - 1);
    538  1.4.2.2  ad 
    539  1.4.2.2  ad 	/*
    540  1.4.2.2  ad 	 * OF_1.x (at least) always returns addr == 0 for
    541  1.4.2.2  ad 	 * SCSI disks (i.e. "/bandit (at) .../.../sd@0,0").
    542  1.4.2.2  ad 	 */
    543  1.4.2.2  ad 	lastp = strrchr(cbootpath, '/');
    544  1.4.2.2  ad 	if (lastp != NULL) {
    545  1.4.2.2  ad 		lastp++;
    546  1.4.2.2  ad 		if (strncmp(lastp, "sd@", 3) == 0
    547  1.4.2.2  ad 		    && strncmp(last, "sd@", 3) == 0)
    548  1.4.2.2  ad 			strcpy(lastp, last);
    549  1.4.2.2  ad 	} else {
    550  1.4.2.2  ad 		lastp = cbootpath;
    551  1.4.2.2  ad 	}
    552  1.4.2.2  ad 
    553  1.4.2.2  ad 	/*
    554  1.4.2.2  ad 	 * At this point, cbootpath contains like:
    555  1.4.2.2  ad 	 * "/pci@80000000/mac-io@10/ata-3@20000/disk"
    556  1.4.2.2  ad 	 *
    557  1.4.2.2  ad 	 * The last component may have no address... so append it.
    558  1.4.2.2  ad 	 */
    559  1.4.2.2  ad 	if (strchr(lastp, '@') == NULL) {
    560  1.4.2.2  ad 		/* Append it. */
    561  1.4.2.2  ad 		if ((p = strrchr(last, '@')) != NULL)
    562  1.4.2.2  ad 			strcat(cbootpath, p);
    563  1.4.2.2  ad 	}
    564  1.4.2.2  ad 
    565  1.4.2.2  ad 	if ((p = strrchr(lastp, ':')) != NULL) {
    566  1.4.2.2  ad 		*p++ = '\0';
    567  1.4.2.2  ad 		/* booted_partition = *p - '0';		XXX correct? */
    568  1.4.2.2  ad 	}
    569  1.4.2.2  ad 
    570  1.4.2.2  ad 	/* XXX Does this belong here, or device_register()? */
    571  1.4.2.2  ad 	if ((p = strrchr(lastp, ',')) != NULL)
    572  1.4.2.2  ad 		*p = '\0';
    573  1.4.2.2  ad }
    574  1.4.2.2  ad 
    575  1.4.2.2  ad /*
    576  1.4.2.2  ad  * device_register is called from config_attach as each device is
    577  1.4.2.2  ad  * attached. We use it to find the NetBSD device corresponding to the
    578  1.4.2.2  ad  * known OF boot device.
    579  1.4.2.2  ad  */
    580  1.4.2.2  ad void
    581  1.4.2.2  ad device_register(dev, aux)
    582  1.4.2.2  ad 	struct device *dev;
    583  1.4.2.2  ad 	void *aux;
    584  1.4.2.2  ad {
    585  1.4.2.2  ad 	static struct device *parent;
    586  1.4.2.2  ad 	static char *bp = bootpath + 1, *cp = cbootpath;
    587  1.4.2.2  ad 	unsigned long addr;
    588  1.4.2.2  ad 	char *p;
    589  1.4.2.2  ad 
    590  1.4.2.2  ad 	/* Skip over devices not represented in the OF tree. */
    591  1.4.2.2  ad 	if (device_is_a(dev, "mainbus")) {
    592  1.4.2.2  ad 		parent = dev;
    593  1.4.2.2  ad 		return;
    594  1.4.2.2  ad 	}
    595  1.4.2.2  ad 	if (device_is_a(dev, "atapibus") || device_is_a(dev, "pci") ||
    596  1.4.2.2  ad 	    device_is_a(dev, "scsibus") || device_is_a(dev, "atabus"))
    597  1.4.2.2  ad 		return;
    598  1.4.2.2  ad 
    599  1.4.2.2  ad 	if (device_is_a(device_parent(dev), "pci")) {
    600  1.4.2.2  ad 		/* see if this is going to be console */
    601  1.4.2.2  ad 		struct pci_attach_args *pa = aux;
    602  1.4.2.2  ad 		prop_dictionary_t dict;
    603  1.4.2.2  ad 		int node;
    604  1.4.2.2  ad 		char name[32];
    605  1.4.2.2  ad 
    606  1.4.2.2  ad 		dict = device_properties(dev);
    607  1.4.2.2  ad 		node = pcidev_to_ofdev(pa->pa_pc, pa->pa_tag);
    608  1.4.2.2  ad 
    609  1.4.2.2  ad 		if (node != 0) {
    610  1.4.2.2  ad 			prop_dictionary_set_uint32(dict, "device_node", node);
    611  1.4.2.2  ad 
    612  1.4.2.2  ad 			memset(name, 0, sizeof(name));
    613  1.4.2.2  ad 			OF_getprop(node, "device_type", name, sizeof(name));
    614  1.4.2.2  ad 			if (strcmp(name, "display") == 0) {
    615  1.4.2.2  ad 				/* setup display properties for fb driver */
    616  1.4.2.2  ad 				prop_dictionary_set_bool(dict, "is_console", 0);
    617  1.4.2.2  ad 				copy_disp_props(dev, node, dict);
    618  1.4.2.2  ad 			}
    619  1.4.2.2  ad 		}
    620  1.4.2.2  ad 	}
    621  1.4.2.2  ad 
    622  1.4.2.2  ad 	if (booted_device)
    623  1.4.2.2  ad 		return;
    624  1.4.2.2  ad 
    625  1.4.2.2  ad 	if (device_is_a(device_parent(dev), "atapibus") ||
    626  1.4.2.2  ad 	    device_is_a(device_parent(dev), "atabus") ||
    627  1.4.2.2  ad 	    device_is_a(device_parent(dev), "pci") ||
    628  1.4.2.2  ad 	    device_is_a(device_parent(dev), "scsibus")) {
    629  1.4.2.2  ad 		if (device_parent(device_parent(dev)) != parent)
    630  1.4.2.2  ad 			return;
    631  1.4.2.2  ad 	} else {
    632  1.4.2.2  ad 		if (device_parent(dev) != parent)
    633  1.4.2.2  ad 			return;
    634  1.4.2.2  ad 	}
    635  1.4.2.2  ad 
    636  1.4.2.2  ad 	/* Get the address part of the current path component. The
    637  1.4.2.2  ad 	 * last component of the canonical bootpath may have no
    638  1.4.2.2  ad 	 * address (eg, "disk"), in which case we need to get the
    639  1.4.2.2  ad 	 * address from the original bootpath instead.
    640  1.4.2.2  ad 	 */
    641  1.4.2.2  ad 	p = strchr(cp, '@');
    642  1.4.2.2  ad 	if (!p) {
    643  1.4.2.2  ad 		if (bp)
    644  1.4.2.2  ad 			p = strchr(bp, '@');
    645  1.4.2.2  ad 		if (!p)
    646  1.4.2.2  ad 			addr = 0;
    647  1.4.2.2  ad 		else {
    648  1.4.2.2  ad 			addr = strtoul(p + 1, NULL, 16);
    649  1.4.2.2  ad 			p = NULL;
    650  1.4.2.2  ad 		}
    651  1.4.2.2  ad 	} else
    652  1.4.2.2  ad 		addr = strtoul(p + 1, &p, 16);
    653  1.4.2.2  ad 
    654  1.4.2.2  ad 	if (device_is_a(device_parent(dev), "mainbus")) {
    655  1.4.2.2  ad 		struct confargs *ca = aux;
    656  1.4.2.2  ad 
    657  1.4.2.2  ad 		if (strcmp(ca->ca_name, "ofw") == 0)		/* XXX */
    658  1.4.2.2  ad 			return;
    659  1.4.2.2  ad 		if (addr != ca->ca_reg[0])
    660  1.4.2.2  ad 			return;
    661  1.4.2.2  ad 	} else if (device_is_a(device_parent(dev), "pci")) {
    662  1.4.2.2  ad 		struct pci_attach_args *pa = aux;
    663  1.4.2.2  ad 
    664  1.4.2.2  ad 		if (addr != pa->pa_device)
    665  1.4.2.2  ad 			return;
    666  1.4.2.2  ad 	} else if (device_is_a(device_parent(dev), "obio")) {
    667  1.4.2.2  ad 		struct confargs *ca = aux;
    668  1.4.2.2  ad 
    669  1.4.2.2  ad 		if (addr != ca->ca_reg[0])
    670  1.4.2.2  ad 			return;
    671  1.4.2.2  ad 	} else if (device_is_a(device_parent(dev), "scsibus") ||
    672  1.4.2.2  ad 		   device_is_a(device_parent(dev), "atapibus")) {
    673  1.4.2.2  ad 		struct scsipibus_attach_args *sa = aux;
    674  1.4.2.2  ad 
    675  1.4.2.2  ad 		/* periph_target is target for scsi, drive # for atapi */
    676  1.4.2.2  ad 		if (addr != sa->sa_periph->periph_target)
    677  1.4.2.2  ad 			return;
    678  1.4.2.2  ad 	} else if (device_is_a(device_parent(device_parent(dev)), "pciide")) {
    679  1.4.2.2  ad 		struct ata_device *adev = aux;
    680  1.4.2.2  ad 
    681  1.4.2.2  ad 		if (addr != adev->adev_drv_data->drive)
    682  1.4.2.2  ad 			return;
    683  1.4.2.2  ad 
    684  1.4.2.2  ad 		/*
    685  1.4.2.2  ad 		 * OF splits channel and drive into separate path
    686  1.4.2.2  ad 		 * components, so check the addr part of the next
    687  1.4.2.2  ad 		 * component. (Ignore bp, because the canonical path
    688  1.4.2.2  ad 		 * will be complete in the pciide case.)
    689  1.4.2.2  ad 		 */
    690  1.4.2.2  ad 		p = strchr(p, '@');
    691  1.4.2.2  ad 		if (!p++)
    692  1.4.2.2  ad 			return;
    693  1.4.2.2  ad 		if (strtoul(p, &p, 16) != adev->adev_drv_data->drive)
    694  1.4.2.2  ad 			return;
    695  1.4.2.2  ad 	} else if (device_is_a(device_parent(device_parent(dev)), "wdc")) {
    696  1.4.2.2  ad 		struct ata_device *adev = aux;
    697  1.4.2.2  ad 
    698  1.4.2.2  ad 		if (addr != adev->adev_drv_data->drive)
    699  1.4.2.2  ad 			return;
    700  1.4.2.2  ad 	} else
    701  1.4.2.2  ad 		return;
    702  1.4.2.2  ad 
    703  1.4.2.2  ad 	/* If we reach this point, then dev is a match for the current
    704  1.4.2.2  ad 	 * path component.
    705  1.4.2.2  ad 	 */
    706  1.4.2.2  ad 
    707  1.4.2.2  ad 	if (p && *p) {
    708  1.4.2.2  ad 		parent = dev;
    709  1.4.2.2  ad 		cp = p;
    710  1.4.2.2  ad 		bp = strchr(bp, '/');
    711  1.4.2.2  ad 		if (bp)
    712  1.4.2.2  ad 			bp++;
    713  1.4.2.2  ad 		return;
    714  1.4.2.2  ad 	} else {
    715  1.4.2.2  ad 		booted_device = dev;
    716  1.4.2.2  ad 		booted_partition = 0; /* XXX -- should be extracted from bootpath */
    717  1.4.2.2  ad 		return;
    718  1.4.2.2  ad 	}
    719  1.4.2.2  ad }
    720  1.4.2.2  ad 
    721  1.4.2.2  ad /*
    722  1.4.2.2  ad  * Setup root device.
    723  1.4.2.2  ad  * Configure swap area.
    724  1.4.2.2  ad  */
    725  1.4.2.2  ad void
    726  1.4.2.2  ad cpu_rootconf()
    727  1.4.2.2  ad {
    728  1.4.2.2  ad 	printf("boot device: %s\n",
    729  1.4.2.2  ad 	    booted_device ? booted_device->dv_xname : "<unknown>");
    730  1.4.2.2  ad 
    731  1.4.2.2  ad 	setroot(booted_device, booted_partition);
    732  1.4.2.2  ad }
    733  1.4.2.2  ad 
    734  1.4.2.2  ad /*
    735  1.4.2.2  ad  * Find OF-device corresponding to the PCI device.
    736  1.4.2.2  ad  */
    737  1.4.2.2  ad int
    738  1.4.2.2  ad pcidev_to_ofdev(pci_chipset_tag_t pc, pcitag_t tag)
    739  1.4.2.2  ad {
    740  1.4.2.2  ad 	int bus, dev, func;
    741  1.4.2.2  ad 	u_int reg[5];
    742  1.4.2.2  ad 	int p, q;
    743  1.4.2.2  ad 	int l, b, d, f;
    744  1.4.2.2  ad 
    745  1.4.2.2  ad 	pci_decompose_tag(pc, tag, &bus, &dev, &func);
    746  1.4.2.2  ad 
    747  1.4.2.2  ad 	for (q = OF_peer(0); q; q = p) {
    748  1.4.2.2  ad 		l = OF_getprop(q, "assigned-addresses", reg, sizeof(reg));
    749  1.4.2.2  ad 		if (l > 4) {
    750  1.4.2.2  ad 			b = (reg[0] >> 16) & 0xff;
    751  1.4.2.2  ad 			d = (reg[0] >> 11) & 0x1f;
    752  1.4.2.2  ad 			f = (reg[0] >> 8) & 0x07;
    753  1.4.2.2  ad 
    754  1.4.2.2  ad 			if (b == bus && d == dev && f == func)
    755  1.4.2.2  ad 				return q;
    756  1.4.2.2  ad 		}
    757  1.4.2.2  ad 		if ((p = OF_child(q)))
    758  1.4.2.2  ad 			continue;
    759  1.4.2.2  ad 		while (q) {
    760  1.4.2.2  ad 			if ((p = OF_peer(q)))
    761  1.4.2.2  ad 				break;
    762  1.4.2.2  ad 			q = OF_parent(q);
    763  1.4.2.2  ad 		}
    764  1.4.2.2  ad 	}
    765  1.4.2.2  ad 	return 0;
    766  1.4.2.2  ad }
    767  1.4.2.2  ad /* $NetBSD: ofw_autoconf.c,v 1.4.2.2 2007/12/03 19:03:59 ad Exp $ */
    768  1.4.2.2  ad /*
    769  1.4.2.2  ad  * Copyright (C) 1995, 1996 Wolfgang Solfrank.
    770  1.4.2.2  ad  * Copyright (C) 1995, 1996 TooLs GmbH.
    771  1.4.2.2  ad  * All rights reserved.
    772  1.4.2.2  ad  *
    773  1.4.2.2  ad  * Redistribution and use in source and binary forms, with or without
    774  1.4.2.2  ad  * modification, are permitted provided that the following conditions
    775  1.4.2.2  ad  * are met:
    776  1.4.2.2  ad  * 1. Redistributions of source code must retain the above copyright
    777  1.4.2.2  ad  *    notice, this list of conditions and the following disclaimer.
    778  1.4.2.2  ad  * 2. Redistributions in binary form must reproduce the above copyright
    779  1.4.2.2  ad  *    notice, this list of conditions and the following disclaimer in the
    780  1.4.2.2  ad  *    documentation and/or other materials provided with the distribution.
    781  1.4.2.2  ad  * 3. All advertising materials mentioning features or use of this software
    782  1.4.2.2  ad  *    must display the following acknowledgement:
    783  1.4.2.2  ad  *      This product includes software developed by TooLs GmbH.
    784  1.4.2.2  ad  * 4. The name of TooLs GmbH may not be used to endorse or promote products
    785  1.4.2.2  ad  *    derived from this software without specific prior written permission.
    786  1.4.2.2  ad  *
    787  1.4.2.2  ad  * THIS SOFTWARE IS PROVIDED BY TOOLS GMBH ``AS IS'' AND ANY EXPRESS OR
    788  1.4.2.2  ad  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
    789  1.4.2.2  ad  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
    790  1.4.2.2  ad  * IN NO EVENT SHALL TOOLS GMBH BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
    791  1.4.2.2  ad  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
    792  1.4.2.2  ad  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
    793  1.4.2.2  ad  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
    794  1.4.2.2  ad  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
    795  1.4.2.2  ad  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
    796  1.4.2.2  ad  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
    797  1.4.2.2  ad  */
    798  1.4.2.2  ad 
    799  1.4.2.2  ad #include <sys/cdefs.h>
    800  1.4.2.2  ad __KERNEL_RCSID(0, "$NetBSD: ofw_autoconf.c,v 1.4.2.2 2007/12/03 19:03:59 ad Exp $");
    801  1.4.2.2  ad 
    802  1.4.2.2  ad #include <sys/param.h>
    803  1.4.2.2  ad #include <sys/conf.h>
    804  1.4.2.2  ad #include <sys/device.h>
    805  1.4.2.2  ad #include <sys/reboot.h>
    806  1.4.2.2  ad #include <sys/systm.h>
    807  1.4.2.2  ad 
    808  1.4.2.2  ad #include <uvm/uvm_extern.h>
    809  1.4.2.2  ad 
    810  1.4.2.2  ad #include <machine/autoconf.h>
    811  1.4.2.2  ad #include <machine/bus.h>
    812  1.4.2.2  ad #include <machine/stdarg.h>
    813  1.4.2.2  ad 
    814  1.4.2.2  ad #include <dev/ofw/openfirm.h>
    815  1.4.2.2  ad #include <dev/pci/pcivar.h>
    816  1.4.2.2  ad #include <dev/scsipi/scsi_all.h>
    817  1.4.2.2  ad #include <dev/scsipi/scsipi_all.h>
    818  1.4.2.2  ad #include <dev/scsipi/scsiconf.h>
    819  1.4.2.2  ad #include <dev/ata/atavar.h>
    820  1.4.2.2  ad #include <dev/ic/wdcvar.h>
    821  1.4.2.2  ad 
    822  1.4.2.2  ad extern char bootpath[256];
    823  1.4.2.2  ad char cbootpath[256];
    824  1.4.2.2  ad int console_node = 0, console_instance = 0;
    825  1.4.2.2  ad 
    826  1.4.2.2  ad static void canonicalize_bootpath(void);
    827  1.4.2.2  ad 
    828  1.4.2.2  ad /*
    829  1.4.2.2  ad  * Determine device configuration for a machine.
    830  1.4.2.2  ad  */
    831  1.4.2.2  ad void
    832  1.4.2.2  ad cpu_configure(void)
    833  1.4.2.2  ad {
    834  1.4.2.2  ad 	init_interrupt();
    835  1.4.2.2  ad 	canonicalize_bootpath();
    836  1.4.2.2  ad 
    837  1.4.2.2  ad 	if (config_rootfound("mainbus", NULL) == NULL)
    838  1.4.2.2  ad 		panic("configure: mainbus not configured");
    839  1.4.2.2  ad 
    840  1.4.2.2  ad 	genppc_cpu_configure();
    841  1.4.2.2  ad }
    842  1.4.2.2  ad 
    843  1.4.2.2  ad static void
    844  1.4.2.2  ad canonicalize_bootpath(void)
    845  1.4.2.2  ad {
    846  1.4.2.2  ad 	int node;
    847  1.4.2.2  ad 	char *p, *lastp;
    848  1.4.2.2  ad 	char last[32];
    849  1.4.2.2  ad 
    850  1.4.2.2  ad 	/*
    851  1.4.2.2  ad 	 * If the bootpath doesn't start with a / then it isn't
    852  1.4.2.2  ad 	 * an OFW path and probably is an alias, so look up the alias
    853  1.4.2.2  ad 	 * and regenerate the full bootpath so device_register will work.
    854  1.4.2.2  ad 	 */
    855  1.4.2.2  ad 	if (bootpath[0] != '/' && bootpath[0] != '\0') {
    856  1.4.2.2  ad 		int aliases = OF_finddevice("/aliases");
    857  1.4.2.2  ad 		char tmpbuf[100];
    858  1.4.2.2  ad 		char aliasbuf[256];
    859  1.4.2.2  ad 		if (aliases != 0) {
    860  1.4.2.2  ad 			char *cp1, *cp2, *cp;
    861  1.4.2.2  ad 			char saved_ch = '\0';
    862  1.4.2.2  ad 			int len;
    863  1.4.2.2  ad 			cp1 = strchr(bootpath, ':');
    864  1.4.2.2  ad 			cp2 = strchr(bootpath, ',');
    865  1.4.2.2  ad 			cp = cp1;
    866  1.4.2.2  ad 			if (cp1 == NULL || (cp2 != NULL && cp2 < cp1))
    867  1.4.2.2  ad 				cp = cp2;
    868  1.4.2.2  ad 			tmpbuf[0] = '\0';
    869  1.4.2.2  ad 			if (cp != NULL) {
    870  1.4.2.2  ad 				strcpy(tmpbuf, cp);
    871  1.4.2.2  ad 				saved_ch = *cp;
    872  1.4.2.2  ad 				*cp = '\0';
    873  1.4.2.2  ad 			}
    874  1.4.2.2  ad 			len = OF_getprop(aliases, bootpath, aliasbuf,
    875  1.4.2.2  ad 			    sizeof(aliasbuf));
    876  1.4.2.2  ad 			if (len > 0) {
    877  1.4.2.2  ad 				if (aliasbuf[len-1] == '\0')
    878  1.4.2.2  ad 					len--;
    879  1.4.2.2  ad 				memcpy(bootpath, aliasbuf, len);
    880  1.4.2.2  ad 				strcpy(&bootpath[len], tmpbuf);
    881  1.4.2.2  ad 			} else {
    882  1.4.2.2  ad 				*cp = saved_ch;
    883  1.4.2.2  ad 			}
    884  1.4.2.2  ad 		}
    885  1.4.2.2  ad 	}
    886  1.4.2.2  ad 
    887  1.4.2.2  ad 	/*
    888  1.4.2.2  ad 	 * Strip kernel name.  bootpath contains "OF-path"/"kernel".
    889  1.4.2.2  ad 	 *
    890  1.4.2.2  ad 	 * for example:
    891  1.4.2.2  ad 	 *   /bandit@F2000000/gc@10/53c94@10000/sd@0,0/netbsd	(OF-1.x)
    892  1.4.2.2  ad 	 *   /pci/mac-io/ata-3@2000/disk@0:0/netbsd.new		(OF-3.x)
    893  1.4.2.2  ad 	 */
    894  1.4.2.2  ad 	strcpy(cbootpath, bootpath);
    895  1.4.2.2  ad 	while ((node = OF_finddevice(cbootpath)) == -1) {
    896  1.4.2.2  ad 		if ((p = strrchr(cbootpath, '/')) == NULL)
    897  1.4.2.2  ad 			break;
    898  1.4.2.2  ad 		*p = '\0';
    899  1.4.2.2  ad 	}
    900  1.4.2.2  ad 
    901  1.4.2.2  ad 	if (node == -1) {
    902  1.4.2.2  ad 		/* Cannot canonicalize... use bootpath anyway. */
    903  1.4.2.2  ad 		strcpy(cbootpath, bootpath);
    904  1.4.2.2  ad 
    905  1.4.2.2  ad 		return;
    906  1.4.2.2  ad 	}
    907  1.4.2.2  ad 
    908  1.4.2.2  ad 	/*
    909  1.4.2.2  ad 	 * cbootpath is a valid OF path.  Use package-to-path to
    910  1.4.2.2  ad 	 * canonicalize pathname.
    911  1.4.2.2  ad 	 */
    912  1.4.2.2  ad 
    913  1.4.2.2  ad 	/* Back up the last component for later use. */
    914  1.4.2.2  ad 	if ((p = strrchr(cbootpath, '/')) != NULL)
    915  1.4.2.2  ad 		strcpy(last, p + 1);
    916  1.4.2.2  ad 	else
    917  1.4.2.2  ad 		last[0] = '\0';
    918  1.4.2.2  ad 
    919  1.4.2.2  ad 	memset(cbootpath, 0, sizeof(cbootpath));
    920  1.4.2.2  ad 	OF_package_to_path(node, cbootpath, sizeof(cbootpath) - 1);
    921  1.4.2.2  ad 
    922  1.4.2.2  ad 	/*
    923  1.4.2.2  ad 	 * OF_1.x (at least) always returns addr == 0 for
    924  1.4.2.2  ad 	 * SCSI disks (i.e. "/bandit (at) .../.../sd@0,0").
    925  1.4.2.2  ad 	 */
    926  1.4.2.2  ad 	lastp = strrchr(cbootpath, '/');
    927  1.4.2.2  ad 	if (lastp != NULL) {
    928  1.4.2.2  ad 		lastp++;
    929  1.4.2.2  ad 		if (strncmp(lastp, "sd@", 3) == 0
    930  1.4.2.2  ad 		    && strncmp(last, "sd@", 3) == 0)
    931  1.4.2.2  ad 			strcpy(lastp, last);
    932  1.4.2.2  ad 	} else {
    933  1.4.2.2  ad 		lastp = cbootpath;
    934  1.4.2.2  ad 	}
    935  1.4.2.2  ad 
    936  1.4.2.2  ad 	/*
    937  1.4.2.2  ad 	 * At this point, cbootpath contains like:
    938  1.4.2.2  ad 	 * "/pci@80000000/mac-io@10/ata-3@20000/disk"
    939  1.4.2.2  ad 	 *
    940  1.4.2.2  ad 	 * The last component may have no address... so append it.
    941  1.4.2.2  ad 	 */
    942  1.4.2.2  ad 	if (strchr(lastp, '@') == NULL) {
    943  1.4.2.2  ad 		/* Append it. */
    944  1.4.2.2  ad 		if ((p = strrchr(last, '@')) != NULL)
    945  1.4.2.2  ad 			strcat(cbootpath, p);
    946  1.4.2.2  ad 	}
    947  1.4.2.2  ad 
    948  1.4.2.2  ad 	if ((p = strrchr(lastp, ':')) != NULL) {
    949  1.4.2.2  ad 		*p++ = '\0';
    950  1.4.2.2  ad 		/* booted_partition = *p - '0';		XXX correct? */
    951  1.4.2.2  ad 	}
    952  1.4.2.2  ad 
    953  1.4.2.2  ad 	/* XXX Does this belong here, or device_register()? */
    954  1.4.2.2  ad 	if ((p = strrchr(lastp, ',')) != NULL)
    955  1.4.2.2  ad 		*p = '\0';
    956  1.4.2.2  ad }
    957  1.4.2.2  ad 
    958  1.4.2.2  ad /*
    959  1.4.2.2  ad  * device_register is called from config_attach as each device is
    960  1.4.2.2  ad  * attached. We use it to find the NetBSD device corresponding to the
    961  1.4.2.2  ad  * known OF boot device.
    962  1.4.2.2  ad  */
    963  1.4.2.2  ad void
    964  1.4.2.2  ad device_register(dev, aux)
    965  1.4.2.2  ad 	struct device *dev;
    966  1.4.2.2  ad 	void *aux;
    967  1.4.2.2  ad {
    968  1.4.2.2  ad 	static struct device *parent;
    969  1.4.2.2  ad 	static char *bp = bootpath + 1, *cp = cbootpath;
    970  1.4.2.2  ad 	unsigned long addr;
    971  1.4.2.2  ad 	char *p;
    972  1.4.2.2  ad 
    973  1.4.2.2  ad 	/* Skip over devices not represented in the OF tree. */
    974  1.4.2.2  ad 	if (device_is_a(dev, "mainbus")) {
    975  1.4.2.2  ad 		parent = dev;
    976  1.4.2.2  ad 		return;
    977  1.4.2.2  ad 	}
    978  1.4.2.2  ad 	if (device_is_a(dev, "atapibus") || device_is_a(dev, "pci") ||
    979  1.4.2.2  ad 	    device_is_a(dev, "scsibus") || device_is_a(dev, "atabus"))
    980  1.4.2.2  ad 		return;
    981  1.4.2.2  ad 
    982  1.4.2.2  ad 	if (device_is_a(device_parent(dev), "pci")) {
    983  1.4.2.2  ad 		/* see if this is going to be console */
    984  1.4.2.2  ad 		struct pci_attach_args *pa = aux;
    985  1.4.2.2  ad 		prop_dictionary_t dict;
    986  1.4.2.2  ad 		int node;
    987  1.4.2.2  ad 		char name[32];
    988  1.4.2.2  ad 
    989  1.4.2.2  ad 		dict = device_properties(dev);
    990  1.4.2.2  ad 		node = pcidev_to_ofdev(pa->pa_pc, pa->pa_tag);
    991  1.4.2.2  ad 
    992  1.4.2.2  ad 		if (node != 0) {
    993  1.4.2.2  ad 			prop_dictionary_set_uint32(dict, "device_node", node);
    994  1.4.2.2  ad 
    995  1.4.2.2  ad 			memset(name, 0, sizeof(name));
    996  1.4.2.2  ad 			OF_getprop(node, "device_type", name, sizeof(name));
    997  1.4.2.2  ad 			if (strcmp(name, "display") == 0) {
    998  1.4.2.2  ad 				/* setup display properties for fb driver */
    999  1.4.2.2  ad 				prop_dictionary_set_bool(dict, "is_console", 0);
   1000  1.4.2.2  ad 				copy_disp_props(dev, node, dict);
   1001  1.4.2.2  ad 			}
   1002  1.4.2.2  ad 		}
   1003  1.4.2.2  ad 	}
   1004  1.4.2.2  ad 
   1005  1.4.2.2  ad 	if (booted_device)
   1006  1.4.2.2  ad 		return;
   1007  1.4.2.2  ad 
   1008  1.4.2.2  ad 	if (device_is_a(device_parent(dev), "atapibus") ||
   1009  1.4.2.2  ad 	    device_is_a(device_parent(dev), "atabus") ||
   1010  1.4.2.2  ad 	    device_is_a(device_parent(dev), "pci") ||
   1011  1.4.2.2  ad 	    device_is_a(device_parent(dev), "scsibus")) {
   1012  1.4.2.2  ad 		if (device_parent(device_parent(dev)) != parent)
   1013  1.4.2.2  ad 			return;
   1014  1.4.2.2  ad 	} else {
   1015  1.4.2.2  ad 		if (device_parent(dev) != parent)
   1016  1.4.2.2  ad 			return;
   1017  1.4.2.2  ad 	}
   1018  1.4.2.2  ad 
   1019  1.4.2.2  ad 	/* Get the address part of the current path component. The
   1020  1.4.2.2  ad 	 * last component of the canonical bootpath may have no
   1021  1.4.2.2  ad 	 * address (eg, "disk"), in which case we need to get the
   1022  1.4.2.2  ad 	 * address from the original bootpath instead.
   1023  1.4.2.2  ad 	 */
   1024  1.4.2.2  ad 	p = strchr(cp, '@');
   1025  1.4.2.2  ad 	if (!p) {
   1026  1.4.2.2  ad 		if (bp)
   1027  1.4.2.2  ad 			p = strchr(bp, '@');
   1028  1.4.2.2  ad 		if (!p)
   1029  1.4.2.2  ad 			addr = 0;
   1030  1.4.2.2  ad 		else {
   1031  1.4.2.2  ad 			addr = strtoul(p + 1, NULL, 16);
   1032  1.4.2.2  ad 			p = NULL;
   1033  1.4.2.2  ad 		}
   1034  1.4.2.2  ad 	} else
   1035  1.4.2.2  ad 		addr = strtoul(p + 1, &p, 16);
   1036  1.4.2.2  ad 
   1037  1.4.2.2  ad 	if (device_is_a(device_parent(dev), "mainbus")) {
   1038  1.4.2.2  ad 		struct confargs *ca = aux;
   1039  1.4.2.2  ad 
   1040  1.4.2.2  ad 		if (strcmp(ca->ca_name, "ofw") == 0)		/* XXX */
   1041  1.4.2.2  ad 			return;
   1042  1.4.2.2  ad 		if (addr != ca->ca_reg[0])
   1043  1.4.2.2  ad 			return;
   1044  1.4.2.2  ad 	} else if (device_is_a(device_parent(dev), "pci")) {
   1045  1.4.2.2  ad 		struct pci_attach_args *pa = aux;
   1046  1.4.2.2  ad 
   1047  1.4.2.2  ad 		if (addr != pa->pa_device)
   1048  1.4.2.2  ad 			return;
   1049  1.4.2.2  ad 	} else if (device_is_a(device_parent(dev), "obio")) {
   1050  1.4.2.2  ad 		struct confargs *ca = aux;
   1051  1.4.2.2  ad 
   1052  1.4.2.2  ad 		if (addr != ca->ca_reg[0])
   1053  1.4.2.2  ad 			return;
   1054  1.4.2.2  ad 	} else if (device_is_a(device_parent(dev), "scsibus") ||
   1055  1.4.2.2  ad 		   device_is_a(device_parent(dev), "atapibus")) {
   1056  1.4.2.2  ad 		struct scsipibus_attach_args *sa = aux;
   1057  1.4.2.2  ad 
   1058  1.4.2.2  ad 		/* periph_target is target for scsi, drive # for atapi */
   1059  1.4.2.2  ad 		if (addr != sa->sa_periph->periph_target)
   1060  1.4.2.2  ad 			return;
   1061  1.4.2.2  ad 	} else if (device_is_a(device_parent(device_parent(dev)), "pciide")) {
   1062  1.4.2.2  ad 		struct ata_device *adev = aux;
   1063  1.4.2.2  ad 
   1064  1.4.2.2  ad 		if (addr != adev->adev_drv_data->drive)
   1065  1.4.2.2  ad 			return;
   1066  1.4.2.2  ad 
   1067  1.4.2.2  ad 		/*
   1068  1.4.2.2  ad 		 * OF splits channel and drive into separate path
   1069  1.4.2.2  ad 		 * components, so check the addr part of the next
   1070  1.4.2.2  ad 		 * component. (Ignore bp, because the canonical path
   1071  1.4.2.2  ad 		 * will be complete in the pciide case.)
   1072  1.4.2.2  ad 		 */
   1073  1.4.2.2  ad 		p = strchr(p, '@');
   1074  1.4.2.2  ad 		if (!p++)
   1075  1.4.2.2  ad 			return;
   1076  1.4.2.2  ad 		if (strtoul(p, &p, 16) != adev->adev_drv_data->drive)
   1077  1.4.2.2  ad 			return;
   1078  1.4.2.2  ad 	} else if (device_is_a(device_parent(device_parent(dev)), "wdc")) {
   1079  1.4.2.2  ad 		struct ata_device *adev = aux;
   1080  1.4.2.2  ad 
   1081  1.4.2.2  ad 		if (addr != adev->adev_drv_data->drive)
   1082  1.4.2.2  ad 			return;
   1083  1.4.2.2  ad 	} else
   1084  1.4.2.2  ad 		return;
   1085  1.4.2.2  ad 
   1086  1.4.2.2  ad 	/* If we reach this point, then dev is a match for the current
   1087  1.4.2.2  ad 	 * path component.
   1088  1.4.2.2  ad 	 */
   1089  1.4.2.2  ad 
   1090  1.4.2.2  ad 	if (p && *p) {
   1091  1.4.2.2  ad 		parent = dev;
   1092  1.4.2.2  ad 		cp = p;
   1093  1.4.2.2  ad 		bp = strchr(bp, '/');
   1094  1.4.2.2  ad 		if (bp)
   1095  1.4.2.2  ad 			bp++;
   1096  1.4.2.2  ad 		return;
   1097  1.4.2.2  ad 	} else {
   1098  1.4.2.2  ad 		booted_device = dev;
   1099  1.4.2.2  ad 		booted_partition = 0; /* XXX -- should be extracted from bootpath */
   1100  1.4.2.2  ad 		return;
   1101  1.4.2.2  ad 	}
   1102  1.4.2.2  ad }
   1103  1.4.2.2  ad 
   1104  1.4.2.2  ad /*
   1105  1.4.2.2  ad  * Setup root device.
   1106  1.4.2.2  ad  * Configure swap area.
   1107  1.4.2.2  ad  */
   1108  1.4.2.2  ad void
   1109  1.4.2.2  ad cpu_rootconf()
   1110  1.4.2.2  ad {
   1111  1.4.2.2  ad 	printf("boot device: %s\n",
   1112  1.4.2.2  ad 	    booted_device ? booted_device->dv_xname : "<unknown>");
   1113  1.4.2.2  ad 
   1114  1.4.2.2  ad 	setroot(booted_device, booted_partition);
   1115  1.4.2.2  ad }
   1116  1.4.2.2  ad 
   1117  1.4.2.2  ad /*
   1118  1.4.2.2  ad  * Find OF-device corresponding to the PCI device.
   1119  1.4.2.2  ad  */
   1120  1.4.2.2  ad int
   1121  1.4.2.2  ad pcidev_to_ofdev(pci_chipset_tag_t pc, pcitag_t tag)
   1122  1.4.2.2  ad {
   1123  1.4.2.2  ad 	int bus, dev, func;
   1124  1.4.2.2  ad 	u_int reg[5];
   1125  1.4.2.2  ad 	int p, q;
   1126  1.4.2.2  ad 	int l, b, d, f;
   1127  1.4.2.2  ad 
   1128  1.4.2.2  ad 	pci_decompose_tag(pc, tag, &bus, &dev, &func);
   1129  1.4.2.2  ad 
   1130  1.4.2.2  ad 	for (q = OF_peer(0); q; q = p) {
   1131  1.4.2.2  ad 		l = OF_getprop(q, "assigned-addresses", reg, sizeof(reg));
   1132  1.4.2.2  ad 		if (l > 4) {
   1133  1.4.2.2  ad 			b = (reg[0] >> 16) & 0xff;
   1134  1.4.2.2  ad 			d = (reg[0] >> 11) & 0x1f;
   1135  1.4.2.2  ad 			f = (reg[0] >> 8) & 0x07;
   1136  1.4.2.2  ad 
   1137  1.4.2.2  ad 			if (b == bus && d == dev && f == func)
   1138  1.4.2.2  ad 				return q;
   1139  1.4.2.2  ad 		}
   1140  1.4.2.2  ad 		if ((p = OF_child(q)))
   1141  1.4.2.2  ad 			continue;
   1142  1.4.2.2  ad 		while (q) {
   1143  1.4.2.2  ad 			if ((p = OF_peer(q)))
   1144  1.4.2.2  ad 				break;
   1145  1.4.2.2  ad 			q = OF_parent(q);
   1146  1.4.2.2  ad 		}
   1147  1.4.2.2  ad 	}
   1148  1.4.2.2  ad 	return 0;
   1149  1.4.2.2  ad }
   1150