1 /* $NetBSD: ofw_autoconf.c,v 1.9 2009/03/14 15:36:12 dsl Exp $ */
3 * Copyright (C) 1995, 1996 Wolfgang Solfrank.
4 * Copyright (C) 1995, 1996 TooLs GmbH.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
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.
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.
33 #include <sys/cdefs.h>
34 __KERNEL_RCSID(0, "$NetBSD: ofw_autoconf.c,v 1.9 2009/03/14 15:36:12 dsl Exp $");
36 #include <sys/param.h>
38 #include <sys/device.h>
39 #include <sys/reboot.h>
40 #include <sys/systm.h>
42 #include <uvm/uvm_extern.h>
44 #include <machine/autoconf.h>
45 #include <machine/bus.h>
46 #include <machine/stdarg.h>
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>
57 extern char bootpath
[256];
59 int console_node
= 0, console_instance
= 0;
61 static void canonicalize_bootpath(void);
64 * Determine device configuration for a machine.
70 canonicalize_bootpath();
72 if (config_rootfound("mainbus", NULL
) == NULL
)
73 panic("configure: mainbus not configured");
75 genppc_cpu_configure();
79 canonicalize_bootpath(void)
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.
90 if (bootpath
[0] != '/' && bootpath
[0] != '\0') {
91 int aliases
= OF_finddevice("/aliases");
98 cp1
= strchr(bootpath
, ':');
99 cp2
= strchr(bootpath
, ',');
101 if (cp1
== NULL
|| (cp2
!= NULL
&& cp2
< cp1
))
109 len
= OF_getprop(aliases
, bootpath
, aliasbuf
,
112 if (aliasbuf
[len
-1] == '\0')
114 memcpy(bootpath
, aliasbuf
, len
);
115 strcpy(&bootpath
[len
], tmpbuf
);
123 * Strip kernel name. bootpath contains "OF-path"/"kernel".
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)
129 strcpy(cbootpath
, bootpath
);
130 while ((node
= OF_finddevice(cbootpath
)) == -1) {
131 if ((p
= strrchr(cbootpath
, '/')) == NULL
)
136 printf("bootpath: %s\n", bootpath
);
138 /* Cannot canonicalize... use bootpath anyway. */
139 strcpy(cbootpath
, bootpath
);
145 * cbootpath is a valid OF path. Use package-to-path to
146 * canonicalize pathname.
149 /* Back up the last component for later use. */
150 if ((p
= strrchr(cbootpath
, '/')) != NULL
)
155 memset(cbootpath
, 0, sizeof(cbootpath
));
156 OF_package_to_path(node
, cbootpath
, sizeof(cbootpath
) - 1);
159 * OF_1.x (at least) always returns addr == 0 for
160 * SCSI disks (i.e. "/bandit@.../.../sd@0,0").
162 lastp
= strrchr(cbootpath
, '/');
165 if (strncmp(lastp
, "sd@", 3) == 0
166 && strncmp(last
, "sd@", 3) == 0)
173 * At this point, cbootpath contains like:
174 * "/pci@80000000/mac-io@10/ata-3@20000/disk"
176 * The last component may have no address... so append it.
178 if (strchr(lastp
, '@') == NULL
) {
180 if ((p
= strrchr(last
, '@')) != NULL
)
181 strcat(cbootpath
, p
);
184 if ((p
= strrchr(lastp
, ':')) != NULL
) {
186 /* booted_partition = *p - '0'; XXX correct? */
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.
196 device_register(struct device
*dev
, void *aux
)
198 static struct device
*parent
;
199 static char *bp
= bootpath
+ 1, *cp
= cbootpath
;
200 unsigned long addr
, addr2
;
203 /* Skip over devices not represented in the OF tree. */
204 if (device_is_a(dev
, "mainbus")) {
208 if (device_is_a(dev
, "atapibus") || device_is_a(dev
, "pci") ||
209 device_is_a(dev
, "scsibus") || device_is_a(dev
, "atabus"))
212 if (device_is_a(device_parent(dev
), "pci")) {
213 struct pci_attach_args
*pa
= aux
;
214 prop_dictionary_t dict
;
219 dict
= device_properties(dev
);
220 node
= pcidev_to_ofdev(pa
->pa_pc
, pa
->pa_tag
);
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);
227 (void)prop_dictionary_set(dict
,
228 "use-compat-native-irq", b
);
229 prop_object_release(b
);
235 prop_dictionary_set_uint32(dict
, "device_node", node
);
237 /* see if this is going to be console */
238 memset(name
, 0, sizeof(name
));
239 OF_getprop(node
, "device_type", name
, sizeof(name
));
241 OF_getprop(node
, "class-code", &pci_class
,
243 pci_class
= (pci_class
>> 16) & 0xff;
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
);
259 * Skip over devices that are really just layers of NetBSD
260 * autoconf(9) we should just skip as they do not have any
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
)
270 if (device_parent(dev
) != parent
)
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.
287 addr
= strtoul(p
+ 1, &p
, 16);
289 addr
= strtoul(p
+ 1, &p
, 16);
291 /* if the current path has more address, grab that too */
293 addr2
= strtoul(p
+ 1, &p
, 16);
297 if (device_is_a(device_parent(dev
), "mainbus")) {
298 struct confargs
*ca
= aux
;
300 if (strcmp(ca
->ca_name
, "ofw") == 0) /* XXX */
302 if (addr
!= ca
->ca_reg
[0])
304 } else if (device_is_a(device_parent(dev
), "pci")) {
305 struct pci_attach_args
*pa
= aux
;
307 if (addr
!= pa
->pa_device
||
308 addr2
!= pa
->pa_function
)
310 } else if (device_is_a(device_parent(dev
), "obio")) {
311 struct confargs
*ca
= aux
;
313 if (addr
!= ca
->ca_reg
[0])
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
;
319 /* periph_target is target for scsi, drive # for atapi */
320 if (addr
!= sa
->sa_periph
->periph_target
)
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
;
327 if (addr
!= adev
->adev_channel
||
328 addr2
!= adev
->adev_drv_data
->drive
)
330 } else if (device_is_a(device_parent(device_parent(dev
)), "wdc")) {
331 struct ata_device
*adev
= aux
;
333 if (addr
!= adev
->adev_drv_data
->drive
)
338 /* If we reach this point, then dev is a match for the current
345 bp
= strchr(bp
, '/');
351 booted_partition
= 0; /* XXX -- should be extracted from bootpath */
358 * Configure swap area.
363 printf("boot device: %s\n",
364 booted_device
? booted_device
->dv_xname
: "<unknown>");
366 setroot(booted_device
, booted_partition
);
370 * Find OF-device corresponding to the PCI device.
373 pcidev_to_ofdev(pci_chipset_tag_t pc
, pcitag_t tag
)
380 pci_decompose_tag(pc
, tag
, &bus
, &dev
, &func
);
382 for (q
= OF_peer(0); q
; q
= p
) {
383 l
= OF_getprop(q
, "assigned-addresses", reg
, sizeof(reg
));
385 b
= (reg
[0] >> 16) & 0xff;
386 d
= (reg
[0] >> 11) & 0x1f;
387 f
= (reg
[0] >> 8) & 0x07;
389 if (b
== bus
&& d
== dev
&& f
== func
)
392 if ((p
= OF_child(q
)))
395 if ((p
= OF_peer(q
)))