ofw_autoconf.c revision 1.23 1 /* $NetBSD: ofw_autoconf.c,v 1.23 2018/11/21 17:54:42 scole 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.23 2018/11/21 17:54:42 scole 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 (at) .../.../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 /* skip over CPUs */
235 if (device_is_a(dev, "cpu")) {
236 return;
237 }
238
239 if (device_is_a(dev, "valkyriefb")) {
240 struct confargs *ca = aux;
241 prop_dictionary_t dict;
242
243 dict = device_properties(dev);
244 copy_disp_props(dev, ca->ca_node, dict);
245 }
246
247 /* cannot read useful display properties for platinum */
248 if (device_is_a(dev, "platinumfb")) {
249 return;
250 }
251
252 #if NGTPCI > 0
253 if (device_is_a(dev, "gtpci")) {
254 extern struct gtpci_prot gtpci0_prot, gtpci1_prot;
255 extern struct powerpc_bus_space
256 gtpci0_io_bs_tag, gtpci0_mem_bs_tag,
257 gtpci1_io_bs_tag, gtpci1_mem_bs_tag;
258 extern struct genppc_pci_chipset
259 genppc_gtpci0_chipset, genppc_gtpci1_chipset;
260
261 struct marvell_attach_args *mva = aux;
262 struct gtpci_prot *gtpci_prot;
263 struct powerpc_bus_space *gtpci_io_bs_tag;
264 struct genppc_pci_chipset *genppc_gtpci_chipset;
265 prop_dictionary_t dict = device_properties(dev);
266 prop_data_t prot, io_bs_tag, mem_bs_tag, pc;
267 int iostart, ioend;
268
269 if (mva->mva_unit == 0) {
270 gtpci_prot = >pci0_prot;
271 gtpci_io_bs_tag = >pci0_io_bs_tag;
272 gtpci_mem_bs_tag = >pci0_mem_bs_tag;
273 genppc_gtpci_chipset = &genppc_gtpci0_chipset;
274 iostart = 0;
275 ioend = 0;
276 } else {
277 gtpci_prot = >pci1_prot;
278 gtpci_io_bs_tag = >pci1_io_bs_tag;
279 gtpci_mem_bs_tag = >pci1_mem_bs_tag;
280 genppc_gtpci_chipset = &genppc_gtpci1_chipset;
281 iostart = 0x1400;
282 ioend = 0xffff;
283 }
284
285 prot = prop_data_create_data_nocopy(
286 gtpci_prot, sizeof(struct gtpci_prot));
287 KASSERT(prot != NULL);
288 prop_dictionary_set(dict, "prot", prot);
289 prop_object_release(prot);
290
291 io_bs_tag = prop_data_create_data_nocopy(
292 gtpci_io_bs_tag, sizeof(struct powerpc_bus_space));
293 KASSERT(io_bs_tag != NULL);
294 prop_dictionary_set(dict, "io-bus-tag", io_bs_tag);
295 prop_object_release(io_bs_tag);
296 mem_bs_tag = prop_data_create_data_nocopy(
297 gtpci_mem_bs_tag, sizeof(struct powerpc_bus_space));
298 KASSERT(mem_bs_tag != NULL);
299 prop_dictionary_set(dict, "mem-bus-tag", mem_bs_tag);
300 prop_object_release(mem_bs_tag);
301
302 genppc_gtpci_chipset->pc_conf_v = device_private(dev);
303 pc = prop_data_create_data_nocopy(genppc_gtpci_chipset,
304 sizeof(struct genppc_pci_chipset));
305 KASSERT(pc != NULL);
306 prop_dictionary_set(dict, "pci-chipset", pc);
307 prop_object_release(pc);
308
309 prop_dictionary_set_uint64(dict, "iostart", iostart);
310 prop_dictionary_set_uint64(dict, "ioend", ioend);
311 prop_dictionary_set_uint64(dict, "memstart",
312 gtpci_mem_bs_tag->pbs_base);
313 prop_dictionary_set_uint64(dict, "memend",
314 gtpci_mem_bs_tag->pbs_limit - 1);
315 prop_dictionary_set_uint32(dict, "cache-line-size",
316 CACHELINESIZE);
317 }
318 #endif
319 if (device_is_a(dev, "atapibus") ||
320 #ifndef PMAC_G5
321 device_is_a(dev, "pci") ||
322 #endif
323 device_is_a(dev, "scsibus") || device_is_a(dev, "atabus"))
324 return;
325
326 if (device_is_a(device_parent(dev), "pci")) {
327 struct pci_attach_args *pa = aux;
328 prop_dictionary_t dict;
329 prop_bool_t b;
330 int node;
331 char name[32];
332
333 dict = device_properties(dev);
334 node = pcidev_to_ofdev(pa->pa_pc, pa->pa_tag);
335
336 /* enable configuration of irq 14/15 for VIA native IDE */
337 if (device_is_a(dev, "viaide") &&
338 strncmp(model_name, "Pegasos", 7) == 0) {
339 b = prop_bool_create(true);
340 KASSERT(b != NULL);
341 (void)prop_dictionary_set(dict,
342 "use-compat-native-irq", b);
343 prop_object_release(b);
344 }
345
346 if (node != 0) {
347 int pci_class = 0;
348
349 prop_dictionary_set_uint32(dict, "device_node", node);
350
351 if (node == boot_node) {
352 /* we netbooted from whatever this is */
353 booted_device = dev;
354 }
355 /* see if this is going to be console */
356 memset(name, 0, sizeof(name));
357 OF_getprop(node, "device_type", name, sizeof(name));
358
359 OF_getprop(node, "class-code", &pci_class,
360 sizeof pci_class);
361 pci_class = (pci_class >> 16) & 0xff;
362
363 if (strcmp(name, "display") == 0 ||
364 strcmp(name, "ATY,DDParent") == 0 ||
365 pci_class == PCI_CLASS_DISPLAY) {
366 /* setup display properties for fb driver */
367 prop_dictionary_set_bool(dict, "is_console", 0);
368 copy_disp_props(dev, node, dict);
369 }
370 if (pci_class == PCI_CLASS_NETWORK) {
371 of_to_dataprop(dict, node, "local-mac-address",
372 "mac-address");
373 of_to_dataprop(dict, node, "shared-pins",
374 "shared-pins");
375 }
376 }
377 #ifdef macppc
378 /*
379 * XXX
380 * some macppc boxes have onboard devices where parts or all of
381 * the PCI_INTERRUPT register are hardwired to 0
382 */
383 if (pa->pa_intrpin == 0)
384 pa->pa_intrpin = 1;
385 #endif
386 }
387
388 if (booted_device)
389 return;
390
391 /*
392 * Skip over devices that are really just layers of NetBSD
393 * autoconf(9) we should just skip as they do not have any
394 * OFW devices.
395 * XXX except on G5, where we have /ht/pci* instead of /pci*
396 */
397 if (device_is_a(device_parent(dev), "atapibus") ||
398 device_is_a(device_parent(dev), "atabus") ||
399 #ifndef PMAC_G5
400 device_is_a(device_parent(dev), "pci") ||
401 #endif
402 device_is_a(device_parent(dev), "scsibus")) {
403 if (device_parent(device_parent(dev)) != parent) {
404 return;
405 }
406 } else {
407 if (device_parent(dev) != parent)
408 return;
409 }
410
411 /*
412 * Get the address part of the current path component. The
413 * last component of the canonical bootpath may have no
414 * address (eg, "disk"), in which case we need to get the
415 * address from the original bootpath instead.
416 */
417 p = strchr(cp, '@');
418 if (!p) {
419 if (bp)
420 p = strchr(bp, '@');
421 if (!p)
422 addr = 0;
423 else
424 addr = strtoul(p + 1, &p, 16);
425 } else
426 addr = strtoul(p + 1, &p, 16);
427
428 /* if the current path has more address, grab that too */
429 if (p && *p == ',')
430 addr2 = strtoul(p + 1, &p, 16);
431 else
432 addr2 = 0;
433
434 if (device_is_a(device_parent(dev), "mainbus")) {
435 struct confargs *ca = aux;
436 if (strcmp(ca->ca_name, "ofw") == 0) /* XXX */
437 return;
438 if (strcmp(ca->ca_name, "gt") == 0)
439 parent = dev;
440 if (addr != ca->ca_reg[0])
441 return;
442 if (addr2 != 0 && addr2 != ca->ca_reg[1])
443 return;
444 } else if (device_is_a(device_parent(dev), "gt")) {
445 /*
446 * Special handle for MV64361 on PegasosII(ofppc).
447 */
448 if (device_is_a(dev, "mvgbec")) {
449 /*
450 * Fix cp to /port@N from /ethernet/portN. (N is 0...2)
451 */
452 static char fix_cp[8] = "/port@N";
453
454 if (strlen(cp) != 15 ||
455 strncmp(cp, "/ethernet/port", 14) != 0)
456 return;
457 fix_cp[7] = *(cp + 15);
458 p = fix_cp;
459 #if NGTPCI > 0
460 } else if (device_is_a(dev, "gtpci")) {
461 if (gtpci_mem_bs_tag != NULL &&
462 addr != gtpci_mem_bs_tag->pbs_base)
463 return;
464 #endif
465 } else
466 return;
467 } else if (device_is_a(device_parent(dev), "pci")) {
468 struct pci_attach_args *pa = aux;
469
470 if (addr != pa->pa_device ||
471 addr2 != pa->pa_function)
472 return;
473 } else if (device_is_a(device_parent(dev), "obio")) {
474 struct confargs *ca = aux;
475
476 if (addr != ca->ca_reg[0])
477 return;
478 } else if (device_is_a(device_parent(dev), "scsibus") ||
479 device_is_a(device_parent(dev), "atapibus")) {
480 struct scsipibus_attach_args *sa = aux;
481
482 /* periph_target is target for scsi, drive # for atapi */
483 if (addr != sa->sa_periph->periph_target)
484 return;
485 } else if (device_is_a(device_parent(device_parent(dev)), "pciide") ||
486 device_is_a(device_parent(device_parent(dev)), "viaide") ||
487 device_is_a(device_parent(device_parent(dev)), "slide")) {
488 struct ata_device *adev = aux;
489
490 if (addr != adev->adev_channel ||
491 addr2 != adev->adev_drv_data->drive)
492 return;
493 } else if (device_is_a(device_parent(device_parent(dev)), "wdc")) {
494 struct ata_device *adev = aux;
495
496 if (addr != adev->adev_drv_data->drive)
497 return;
498 } else if (device_is_a(dev, "pci")) {
499 if (addr != device_unit(dev))
500 return;
501 } else if (device_is_a(device_parent(dev), "atabus")) {
502 /*
503 * XXX
504 * on svwsata this is the channel number and we ignore the
505 * drive number which is always 0 anyway
506 * needs to be revisited for other (S)ATA cards
507 */
508 struct ata_device *adev = aux;
509 if (addr != adev->adev_channel)
510 return;
511 /* we have our match, cut off the rest */
512 if (p) *p = 0;
513 } else
514 return;
515
516 /* If we reach this point, then dev is a match for the current
517 * path component.
518 */
519 if (p && *p) {
520 parent = dev;
521 cp = p;
522 bp = strchr(bp, '/');
523 if (bp)
524 bp++;
525 return;
526 } else {
527 booted_device = dev;
528 booted_partition = 0; /* XXX -- should be extracted from bootpath */
529 return;
530 }
531 }
532
533 /*
534 * Setup root device.
535 * Configure swap area.
536 */
537 void
538 cpu_rootconf(void)
539 {
540 printf("boot device: %s\n",
541 booted_device ? device_xname(booted_device) : "<unknown>");
542
543 rootconf();
544 }
545
546 /*
547 * Find OF-device corresponding to the PCI device.
548 */
549 int
550 pcidev_to_ofdev(pci_chipset_tag_t pc, pcitag_t tag)
551 {
552 int bus, dev, func;
553 u_int reg[5];
554 int p, q;
555 int l, b, d, f;
556
557 pci_decompose_tag(pc, tag, &bus, &dev, &func);
558
559 for (q = OF_peer(0); q; q = p) {
560 l = OF_getprop(q, "assigned-addresses", reg, sizeof(reg));
561 if (l > 4) {
562 b = (reg[0] >> 16) & 0xff;
563 d = (reg[0] >> 11) & 0x1f;
564 f = (reg[0] >> 8) & 0x07;
565
566 if (b == bus && d == dev && f == func)
567 return q;
568 }
569 if ((p = OF_child(q)))
570 continue;
571 while (q) {
572 if ((p = OF_peer(q)))
573 break;
574 q = OF_parent(q);
575 }
576 }
577 return 0;
578 }
579