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