xref: /netbsd-src/sys/arch/powerpc/oea/ofw_autoconf.c (revision e77448e07be3174235c13f58032a0d6d0ab7638d)
1 /* $NetBSD: ofw_autoconf.c,v 1.7 2008/01/11 05:11:18 mrg 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.7 2008/01/11 05:11:18 mrg 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@.../.../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(dev, aux)
197 	struct device *dev;
198 	void *aux;
199 {
200 	static struct device *parent;
201 	static char *bp = bootpath + 1, *cp = cbootpath;
202 	unsigned long addr, addr2;
203 	char *p;
204 
205 	/* Skip over devices not represented in the OF tree. */
206 	if (device_is_a(dev, "mainbus")) {
207 		parent = dev;
208 		return;
209 	}
210 	if (device_is_a(dev, "atapibus") || device_is_a(dev, "pci") ||
211 	    device_is_a(dev, "scsibus") || device_is_a(dev, "atabus"))
212 		return;
213 
214 	if (device_is_a(device_parent(dev), "pci")) {
215 		struct pci_attach_args *pa = aux;
216 		prop_dictionary_t dict;
217 		prop_bool_t b;
218 		int node;
219 		char name[32];
220 
221 		dict = device_properties(dev);
222 		node = pcidev_to_ofdev(pa->pa_pc, pa->pa_tag);
223 
224 		/* enable configuration of irq 14/15 for VIA native IDE */
225 		if (device_is_a(dev, "viaide") &&
226 		    strncmp(model_name, "Pegasos", 7) == 0) {
227 			b = prop_bool_create(true);
228 			KASSERT(b != NULL);
229 			(void)prop_dictionary_set(dict,
230 			    "use-compat-native-irq", b);
231 			prop_object_release(b);
232 		}
233 
234 		if (node != 0) {
235 			int pci_class = 0;
236 
237 			prop_dictionary_set_uint32(dict, "device_node", node);
238 
239 			/* see if this is going to be console */
240 			memset(name, 0, sizeof(name));
241 			OF_getprop(node, "device_type", name, sizeof(name));
242 
243 			OF_getprop(node, "class-code", &pci_class,
244 				sizeof pci_class);
245 			pci_class = (pci_class >> 16) & 0xff;
246 
247 			if (strcmp(name, "display") == 0 ||
248 					pci_class == PCI_CLASS_DISPLAY) {
249 				/* setup display properties for fb driver */
250 				prop_dictionary_set_bool(dict, "is_console", 0);
251 				copy_disp_props(dev, node, dict);
252 			}
253 		}
254 	}
255 
256 	if (booted_device)
257 		return;
258 
259 	/*
260 	 * Skip over devices that are really just layers of NetBSD
261 	 * autoconf(9) we should just skip as they do not have any
262 	 * OFW devices.
263 	 */
264 	if (device_is_a(device_parent(dev), "atapibus") ||
265 	    device_is_a(device_parent(dev), "atabus") ||
266 	    device_is_a(device_parent(dev), "pci") ||
267 	    device_is_a(device_parent(dev), "scsibus")) {
268 		if (device_parent(device_parent(dev)) != parent)
269 			return;
270 	} else {
271 		if (device_parent(dev) != parent)
272 			return;
273 	}
274 
275 	/*
276 	 * Get the address part of the current path component. The
277 	 * last component of the canonical bootpath may have no
278 	 * address (eg, "disk"), in which case we need to get the
279 	 * address from the original bootpath instead.
280 	 */
281 	p = strchr(cp, '@');
282 	if (!p) {
283 		if (bp)
284 			p = strchr(bp, '@');
285 		if (!p)
286 			addr = 0;
287 		else
288 			addr = strtoul(p + 1, &p, 16);
289 	} else
290 		addr = strtoul(p + 1, &p, 16);
291 
292 	/* if the current path has more address, grab that too */
293 	if (p && *p == ',')
294 		addr2 = strtoul(p + 1, &p, 16);
295 	else
296 		addr2 = 0;
297 
298 	if (device_is_a(device_parent(dev), "mainbus")) {
299 		struct confargs *ca = aux;
300 
301 		if (strcmp(ca->ca_name, "ofw") == 0)		/* XXX */
302 			return;
303 		if (addr != ca->ca_reg[0])
304 			return;
305 	} else if (device_is_a(device_parent(dev), "pci")) {
306 		struct pci_attach_args *pa = aux;
307 
308 		if (addr != pa->pa_device ||
309 		    addr2 != pa->pa_function)
310 			return;
311 	} else if (device_is_a(device_parent(dev), "obio")) {
312 		struct confargs *ca = aux;
313 
314 		if (addr != ca->ca_reg[0])
315 			return;
316 	} else if (device_is_a(device_parent(dev), "scsibus") ||
317 		   device_is_a(device_parent(dev), "atapibus")) {
318 		struct scsipibus_attach_args *sa = aux;
319 
320 		/* periph_target is target for scsi, drive # for atapi */
321 		if (addr != sa->sa_periph->periph_target)
322 			return;
323 	} else if (device_is_a(device_parent(device_parent(dev)), "pciide") ||
324 		   device_is_a(device_parent(device_parent(dev)), "viaide") ||
325 		   device_is_a(device_parent(device_parent(dev)), "slide")) {
326 		struct ata_device *adev = aux;
327 
328 		if (addr != adev->adev_channel ||
329 		    addr2 != adev->adev_drv_data->drive)
330 			return;
331 	} else if (device_is_a(device_parent(device_parent(dev)), "wdc")) {
332 		struct ata_device *adev = aux;
333 
334 		if (addr != adev->adev_drv_data->drive)
335 			return;
336 	} else
337 		return;
338 
339 	/* If we reach this point, then dev is a match for the current
340 	 * path component.
341 	 */
342 
343 	if (p && *p) {
344 		parent = dev;
345 		cp = p;
346 		bp = strchr(bp, '/');
347 		if (bp)
348 			bp++;
349 		return;
350 	} else {
351 		booted_device = dev;
352 		booted_partition = 0; /* XXX -- should be extracted from bootpath */
353 		return;
354 	}
355 }
356 
357 /*
358  * Setup root device.
359  * Configure swap area.
360  */
361 void
362 cpu_rootconf()
363 {
364 	printf("boot device: %s\n",
365 	    booted_device ? booted_device->dv_xname : "<unknown>");
366 
367 	setroot(booted_device, booted_partition);
368 }
369 
370 /*
371  * Find OF-device corresponding to the PCI device.
372  */
373 int
374 pcidev_to_ofdev(pci_chipset_tag_t pc, pcitag_t tag)
375 {
376 	int bus, dev, func;
377 	u_int reg[5];
378 	int p, q;
379 	int l, b, d, f;
380 
381 	pci_decompose_tag(pc, tag, &bus, &dev, &func);
382 
383 	for (q = OF_peer(0); q; q = p) {
384 		l = OF_getprop(q, "assigned-addresses", reg, sizeof(reg));
385 		if (l > 4) {
386 			b = (reg[0] >> 16) & 0xff;
387 			d = (reg[0] >> 11) & 0x1f;
388 			f = (reg[0] >> 8) & 0x07;
389 
390 			if (b == bus && d == dev && f == func)
391 				return q;
392 		}
393 		if ((p = OF_child(q)))
394 			continue;
395 		while (q) {
396 			if ((p = OF_peer(q)))
397 				break;
398 			q = OF_parent(q);
399 		}
400 	}
401 	return 0;
402 }
403