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