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