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