xref: /netbsd-src/sys/arch/powerpc/oea/ofw_autoconf.c (revision bdc22b2e01993381dcefeff2bc9b56ca75a4235c)
1 /* $NetBSD: ofw_autoconf.c,v 1.21 2018/03/04 00:21:20 macallan 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.21 2018/03/04 00:21:20 macallan Exp $");
35 
36 #ifdef ofppc
37 #include "gtpci.h"
38 #endif
39 
40 #include <sys/param.h>
41 #include <sys/conf.h>
42 #include <sys/device.h>
43 #include <sys/reboot.h>
44 #include <sys/systm.h>
45 
46 #include <uvm/uvm_extern.h>
47 
48 #include <machine/autoconf.h>
49 #include <sys/bus.h>
50 
51 #include <dev/ofw/openfirm.h>
52 #include <dev/marvell/marvellvar.h>
53 #include <dev/pci/pcireg.h>
54 #include <dev/pci/pcivar.h>
55 #if NGTPCI > 0
56 #include <dev/marvell/gtpcivar.h>
57 #endif
58 #include <dev/scsipi/scsi_all.h>
59 #include <dev/scsipi/scsipi_all.h>
60 #include <dev/scsipi/scsiconf.h>
61 #include <dev/ata/atavar.h>
62 #include <dev/ic/wdcvar.h>
63 
64 #include <machine/pci_machdep.h>
65 
66 #include <prop/proplib.h>
67 
68 extern char bootpath[256];
69 char cbootpath[256];
70 static int boot_node = 0;	/* points at boot device if we netboot */
71 
72 static void canonicalize_bootpath(void);
73 
74 /*
75  * Determine device configuration for a machine.
76  */
77 void
78 cpu_configure(void)
79 {
80 	init_interrupt();
81 	canonicalize_bootpath();
82 
83 	if (config_rootfound("mainbus", NULL) == NULL)
84 		panic("configure: mainbus not configured");
85 
86 	genppc_cpu_configure();
87 }
88 
89 static void
90 canonicalize_bootpath(void)
91 {
92 	int node, len;
93 	char *p, *lastp;
94 	char last[32], type[32];
95 
96 	/*
97 	 * If the bootpath doesn't start with a / then it isn't
98 	 * an OFW path and probably is an alias, so look up the alias
99 	 * and regenerate the full bootpath so device_register will work.
100 	 */
101 	if (bootpath[0] != '/' && bootpath[0] != '\0') {
102 		int aliases = OF_finddevice("/aliases");
103 		char tmpbuf[100];
104 		char aliasbuf[256];
105 		if (aliases != 0) {
106 			char *cp1, *cp2, *cp;
107 			char saved_ch = '\0';
108 			cp1 = strchr(bootpath, ':');
109 			cp2 = strchr(bootpath, ',');
110 			cp = cp1;
111 			if (cp1 == NULL || (cp2 != NULL && cp2 < cp1))
112 				cp = cp2;
113 			tmpbuf[0] = '\0';
114 			if (cp != NULL) {
115 				strcpy(tmpbuf, cp);
116 				saved_ch = *cp;
117 				*cp = '\0';
118 			}
119 			len = OF_getprop(aliases, bootpath, aliasbuf,
120 			    sizeof(aliasbuf));
121 			if (len > 0) {
122 				if (aliasbuf[len-1] == '\0')
123 					len--;
124 				memcpy(bootpath, aliasbuf, len);
125 				strcpy(&bootpath[len], tmpbuf);
126 			} else {
127 				*cp = saved_ch;
128 			}
129 		}
130 	}
131 
132 	/*
133 	 * Strip kernel name.  bootpath contains "OF-path"/"kernel".
134 	 *
135 	 * for example:
136 	 *   /bandit@F2000000/gc@10/53c94@10000/sd@0,0/netbsd	(OF-1.x)
137 	 *   /pci/mac-io/ata-3@2000/disk@0:0/netbsd.new		(OF-3.x)
138 	 */
139 	strcpy(cbootpath, bootpath);
140 	while ((node = OF_finddevice(cbootpath)) == -1) {
141 		if ((p = strrchr(cbootpath, '/')) == NULL)
142 			break;
143 		*p = '\0';
144 	}
145 
146 	printf("bootpath: %s\n", bootpath);
147 	if (node == -1) {
148 		/* Cannot canonicalize... use bootpath anyway. */
149 		strcpy(cbootpath, bootpath);
150 
151 		return;
152 	}
153 
154 	/* see if we netbooted */
155 	len = OF_getprop(node, "device_type", type, sizeof(type) - 1);
156 	if (len > -1) {
157 		type[len] = 0;
158 		if (strcmp(type, "network") == 0) {
159 			boot_node = node;
160 		}
161 	}
162 
163 	/*
164 	 * cbootpath is a valid OF path.  Use package-to-path to
165 	 * canonicalize pathname.
166 	 */
167 
168 	/* Back up the last component for later use. */
169 	if ((p = strrchr(cbootpath, '/')) != NULL)
170 		strcpy(last, p + 1);
171 	else
172 		last[0] = '\0';
173 
174 	memset(cbootpath, 0, sizeof(cbootpath));
175 	OF_package_to_path(node, cbootpath, sizeof(cbootpath) - 1);
176 
177 	/*
178 	 * OF_1.x (at least) always returns addr == 0 for
179 	 * SCSI disks (i.e. "/bandit@.../.../sd@0,0").
180 	 * also check for .../disk@ which some Adaptec firmware uses
181 	 */
182 	lastp = strrchr(cbootpath, '/');
183 	if (lastp != NULL) {
184 		lastp++;
185 		if ((strncmp(lastp, "sd@", 3) == 0
186 		     && strncmp(last, "sd@", 3) == 0) ||
187 		    (strncmp(lastp, "disk@", 5) == 0
188 		     && strncmp(last, "disk@", 5) == 0))
189 			strcpy(lastp, last);
190 	} else {
191 		lastp = cbootpath;
192 	}
193 
194 	/*
195 	 * At this point, cbootpath contains like:
196 	 * "/pci@80000000/mac-io@10/ata-3@20000/disk"
197 	 *
198 	 * The last component may have no address... so append it.
199 	 */
200 	if (strchr(lastp, '@') == NULL) {
201 		/* Append it. */
202 		if ((p = strrchr(last, '@')) != NULL)
203 			strcat(cbootpath, p);
204 	}
205 
206 	if ((p = strrchr(lastp, ':')) != NULL) {
207 		*p++ = '\0';
208 		/* booted_partition = *p - '0';		XXX correct? */
209 	}
210 }
211 
212 /*
213  * device_register is called from config_attach as each device is
214  * attached. We use it to find the NetBSD device corresponding to the
215  * known OF boot device.
216  */
217 void
218 device_register(device_t dev, void *aux)
219 {
220 	static device_t parent;
221 	static char *bp = bootpath + 1, *cp = cbootpath;
222 	unsigned long addr, addr2;
223 	char *p;
224 #if NGTPCI > 0
225 	struct powerpc_bus_space *gtpci_mem_bs_tag = NULL;
226 #endif
227 
228 	/* Skip over devices not represented in the OF tree. */
229 	if (device_is_a(dev, "mainbus")) {
230 		parent = dev;
231 		return;
232 	}
233 
234 	if (device_is_a(dev, "valkyriefb")) {
235 		struct confargs *ca = aux;
236 		prop_dictionary_t dict;
237 
238 		dict = device_properties(dev);
239 		copy_disp_props(dev, ca->ca_node, dict);
240 	}
241 
242 #if NGTPCI > 0
243 	if (device_is_a(dev, "gtpci")) {
244 		extern struct gtpci_prot gtpci0_prot, gtpci1_prot;
245 		extern struct powerpc_bus_space
246 		    gtpci0_io_bs_tag, gtpci0_mem_bs_tag,
247 		    gtpci1_io_bs_tag, gtpci1_mem_bs_tag;
248 		extern struct genppc_pci_chipset
249 		    genppc_gtpci0_chipset, genppc_gtpci1_chipset;
250 
251 		struct marvell_attach_args *mva = aux;
252 		struct gtpci_prot *gtpci_prot;
253 		struct powerpc_bus_space *gtpci_io_bs_tag;
254 		struct genppc_pci_chipset *genppc_gtpci_chipset;
255 		prop_dictionary_t dict = device_properties(dev);
256 		prop_data_t prot, io_bs_tag, mem_bs_tag, pc;
257 		int iostart, ioend;
258 
259 		if (mva->mva_unit == 0) {
260 			gtpci_prot = &gtpci0_prot;
261 			gtpci_io_bs_tag = &gtpci0_io_bs_tag;
262 			gtpci_mem_bs_tag = &gtpci0_mem_bs_tag;
263 			genppc_gtpci_chipset = &genppc_gtpci0_chipset;
264 			iostart = 0;
265 			ioend = 0;
266 		} else {
267 			gtpci_prot = &gtpci1_prot;
268 			gtpci_io_bs_tag = &gtpci1_io_bs_tag;
269 			gtpci_mem_bs_tag = &gtpci1_mem_bs_tag;
270 			genppc_gtpci_chipset = &genppc_gtpci1_chipset;
271 			iostart = 0x1400;
272 			ioend = 0xffff;
273 		}
274 
275 		prot = prop_data_create_data_nocopy(
276 		    gtpci_prot, sizeof(struct gtpci_prot));
277 		KASSERT(prot != NULL);
278 		prop_dictionary_set(dict, "prot", prot);
279 		prop_object_release(prot);
280 
281 		io_bs_tag = prop_data_create_data_nocopy(
282 		    gtpci_io_bs_tag, sizeof(struct powerpc_bus_space));
283 		KASSERT(io_bs_tag != NULL);
284 		prop_dictionary_set(dict, "io-bus-tag", io_bs_tag);
285 		prop_object_release(io_bs_tag);
286 		mem_bs_tag = prop_data_create_data_nocopy(
287 		    gtpci_mem_bs_tag, sizeof(struct powerpc_bus_space));
288 		KASSERT(mem_bs_tag != NULL);
289 		prop_dictionary_set(dict, "mem-bus-tag", mem_bs_tag);
290 		prop_object_release(mem_bs_tag);
291 
292 		genppc_gtpci_chipset->pc_conf_v = device_private(dev);
293 		pc = prop_data_create_data_nocopy(genppc_gtpci_chipset,
294 		    sizeof(struct genppc_pci_chipset));
295 		KASSERT(pc != NULL);
296 		prop_dictionary_set(dict, "pci-chipset", pc);
297 		prop_object_release(pc);
298 
299 		prop_dictionary_set_uint64(dict, "iostart", iostart);
300 		prop_dictionary_set_uint64(dict, "ioend", ioend);
301 		prop_dictionary_set_uint64(dict, "memstart",
302 		    gtpci_mem_bs_tag->pbs_base);
303 		prop_dictionary_set_uint64(dict, "memend",
304 		    gtpci_mem_bs_tag->pbs_limit - 1);
305 		prop_dictionary_set_uint32(dict, "cache-line-size",
306 		    CACHELINESIZE);
307 	}
308 #endif
309 	if (device_is_a(dev, "atapibus") || device_is_a(dev, "pci") ||
310 	    device_is_a(dev, "scsibus") || device_is_a(dev, "atabus"))
311 		return;
312 
313 	if (device_is_a(device_parent(dev), "pci")) {
314 		struct pci_attach_args *pa = aux;
315 		prop_dictionary_t dict;
316 		prop_bool_t b;
317 		int node;
318 		char name[32];
319 
320 		dict = device_properties(dev);
321 		node = pcidev_to_ofdev(pa->pa_pc, pa->pa_tag);
322 
323 		/* enable configuration of irq 14/15 for VIA native IDE */
324 		if (device_is_a(dev, "viaide") &&
325 		    strncmp(model_name, "Pegasos", 7) == 0) {
326 			b = prop_bool_create(true);
327 			KASSERT(b != NULL);
328 			(void)prop_dictionary_set(dict,
329 			    "use-compat-native-irq", b);
330 			prop_object_release(b);
331 		}
332 
333 		if (node != 0) {
334 			int pci_class = 0;
335 
336 			prop_dictionary_set_uint32(dict, "device_node", node);
337 
338 			if (node == boot_node) {
339 				/* we netbooted from whatever this is */
340 				booted_device = dev;
341 			}
342 			/* see if this is going to be console */
343 			memset(name, 0, sizeof(name));
344 			OF_getprop(node, "device_type", name, sizeof(name));
345 
346 			OF_getprop(node, "class-code", &pci_class,
347 				sizeof pci_class);
348 			pci_class = (pci_class >> 16) & 0xff;
349 
350 			if (strcmp(name, "display") == 0 ||
351 			    strcmp(name, "ATY,DDParent") == 0 ||
352 			    pci_class == PCI_CLASS_DISPLAY) {
353 				/* setup display properties for fb driver */
354 				prop_dictionary_set_bool(dict, "is_console", 0);
355 				copy_disp_props(dev, node, dict);
356 			}
357 			if (pci_class == PCI_CLASS_NETWORK) {
358 				of_to_dataprop(dict, node, "local-mac-address",
359 				    "mac-address");
360 				of_to_dataprop(dict, node, "shared-pins",
361 				    "shared-pins");
362 			}
363 		}
364 #ifdef macppc
365 		/*
366 		 * XXX
367 		 * some macppc boxes have onboard devices where parts or all of
368 		 * the PCI_INTERRUPT register are hardwired to 0
369 		 */
370 		if (pa->pa_intrpin == 0)
371 			pa->pa_intrpin = 1;
372 #endif
373 	}
374 
375 	if (booted_device)
376 		return;
377 
378 	/*
379 	 * Skip over devices that are really just layers of NetBSD
380 	 * autoconf(9) we should just skip as they do not have any
381 	 * OFW devices.
382 	 */
383 	if (device_is_a(device_parent(dev), "atapibus") ||
384 	    device_is_a(device_parent(dev), "atabus") ||
385 	    device_is_a(device_parent(dev), "pci") ||
386 	    device_is_a(device_parent(dev), "scsibus")) {
387 		if (device_parent(device_parent(dev)) != parent)
388 			return;
389 	} else {
390 		if (device_parent(dev) != parent)
391 			return;
392 	}
393 
394 	/*
395 	 * Get the address part of the current path component. The
396 	 * last component of the canonical bootpath may have no
397 	 * address (eg, "disk"), in which case we need to get the
398 	 * address from the original bootpath instead.
399 	 */
400 	p = strchr(cp, '@');
401 	if (!p) {
402 		if (bp)
403 			p = strchr(bp, '@');
404 		if (!p)
405 			addr = 0;
406 		else
407 			addr = strtoul(p + 1, &p, 16);
408 	} else
409 		addr = strtoul(p + 1, &p, 16);
410 
411 	/* if the current path has more address, grab that too */
412 	if (p && *p == ',')
413 		addr2 = strtoul(p + 1, &p, 16);
414 	else
415 		addr2 = 0;
416 
417 	if (device_is_a(device_parent(dev), "mainbus")) {
418 		struct confargs *ca = aux;
419 
420 		if (strcmp(ca->ca_name, "ofw") == 0)		/* XXX */
421 			return;
422 		if (strcmp(ca->ca_name, "gt") == 0)
423 			parent = dev;
424 		if (addr != ca->ca_reg[0])
425 			return;
426 	} else if (device_is_a(device_parent(dev), "gt")) {
427 		/*
428 		 * Special handle for MV64361 on PegasosII(ofppc).
429 		 */
430 		if (device_is_a(dev, "mvgbec")) {
431 			/*
432 			 * Fix cp to /port@N from /ethernet/portN. (N is 0...2)
433 			 */
434 			static char fix_cp[8] = "/port@N";
435 
436 			if (strlen(cp) != 15 ||
437 			    strncmp(cp, "/ethernet/port", 14) != 0)
438 				return;
439 			fix_cp[7] = *(cp + 15);
440 			p = fix_cp;
441 #if NGTPCI > 0
442 		} else if (device_is_a(dev, "gtpci")) {
443 			if (gtpci_mem_bs_tag != NULL &&
444 			    addr != gtpci_mem_bs_tag->pbs_base)
445 				return;
446 #endif
447 		} else
448 			return;
449 	} else if (device_is_a(device_parent(dev), "pci")) {
450 		struct pci_attach_args *pa = aux;
451 
452 		if (addr != pa->pa_device ||
453 		    addr2 != pa->pa_function)
454 			return;
455 	} else if (device_is_a(device_parent(dev), "obio")) {
456 		struct confargs *ca = aux;
457 
458 		if (addr != ca->ca_reg[0])
459 			return;
460 	} else if (device_is_a(device_parent(dev), "scsibus") ||
461 		   device_is_a(device_parent(dev), "atapibus")) {
462 		struct scsipibus_attach_args *sa = aux;
463 
464 		/* periph_target is target for scsi, drive # for atapi */
465 		if (addr != sa->sa_periph->periph_target)
466 			return;
467 	} else if (device_is_a(device_parent(device_parent(dev)), "pciide") ||
468 		   device_is_a(device_parent(device_parent(dev)), "viaide") ||
469 		   device_is_a(device_parent(device_parent(dev)), "slide")) {
470 		struct ata_device *adev = aux;
471 
472 		if (addr != adev->adev_channel ||
473 		    addr2 != adev->adev_drv_data->drive)
474 			return;
475 	} else if (device_is_a(device_parent(device_parent(dev)), "wdc")) {
476 		struct ata_device *adev = aux;
477 
478 		if (addr != adev->adev_drv_data->drive)
479 			return;
480 	} else
481 		return;
482 
483 	/* If we reach this point, then dev is a match for the current
484 	 * path component.
485 	 */
486 
487 	if (p && *p) {
488 		parent = dev;
489 		cp = p;
490 		bp = strchr(bp, '/');
491 		if (bp)
492 			bp++;
493 		return;
494 	} else {
495 		booted_device = dev;
496 		booted_partition = 0; /* XXX -- should be extracted from bootpath */
497 		return;
498 	}
499 }
500 
501 /*
502  * Setup root device.
503  * Configure swap area.
504  */
505 void
506 cpu_rootconf(void)
507 {
508 	printf("boot device: %s\n",
509 	    booted_device ? device_xname(booted_device) : "<unknown>");
510 
511 	rootconf();
512 }
513 
514 /*
515  * Find OF-device corresponding to the PCI device.
516  */
517 int
518 pcidev_to_ofdev(pci_chipset_tag_t pc, pcitag_t tag)
519 {
520 	int bus, dev, func;
521 	u_int reg[5];
522 	int p, q;
523 	int l, b, d, f;
524 
525 	pci_decompose_tag(pc, tag, &bus, &dev, &func);
526 
527 	for (q = OF_peer(0); q; q = p) {
528 		l = OF_getprop(q, "assigned-addresses", reg, sizeof(reg));
529 		if (l > 4) {
530 			b = (reg[0] >> 16) & 0xff;
531 			d = (reg[0] >> 11) & 0x1f;
532 			f = (reg[0] >> 8) & 0x07;
533 
534 			if (b == bus && d == dev && f == func)
535 				return q;
536 		}
537 		if ((p = OF_child(q)))
538 			continue;
539 		while (q) {
540 			if ((p = OF_peer(q)))
541 				break;
542 			q = OF_parent(q);
543 		}
544 	}
545 	return 0;
546 }
547