ofw_autoconf.c revision 1.1 1 /* $NetBSD: ofw_autoconf.c,v 1.1 2007/11/07 19:29:11 garbled 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.1 2007/11/07 19:29:11 garbled Exp $");
35
36 #include <sys/param.h>
37 #include <sys/conf.h>
38 #include <sys/device.h>
39 #include <sys/reboot.h>
40 #include <sys/systm.h>
41
42 #include <uvm/uvm_extern.h>
43
44 #include <machine/autoconf.h>
45 #include <machine/bus.h>
46 #include <machine/stdarg.h>
47
48 #include <dev/ofw/openfirm.h>
49 #include <dev/pci/pcivar.h>
50 #include <dev/scsipi/scsi_all.h>
51 #include <dev/scsipi/scsipi_all.h>
52 #include <dev/scsipi/scsiconf.h>
53 #include <dev/ata/atavar.h>
54 #include <dev/ic/wdcvar.h>
55 #include <dev/wsfb/genfbvar.h>
56
57 extern char bootpath[256];
58 char cbootpath[256];
59 int console_node = 0, console_instance = 0;
60
61 struct genfb_colormap_callback gfb_cb;
62 static void of_set_palette(void *, int, int, int, int);
63 static void add_model_specifics(prop_dictionary_t);
64 static void copyprops(int, prop_dictionary_t);
65 static void canonicalize_bootpath(void);
66
67 /*
68 * Determine device configuration for a machine.
69 */
70 void
71 cpu_configure(void)
72 {
73 init_interrupt();
74 canonicalize_bootpath();
75
76 if (config_rootfound("mainbus", NULL) == NULL)
77 panic("configure: mainbus not configured");
78
79 genppc_cpu_configure();
80 }
81
82 static void
83 canonicalize_bootpath(void)
84 {
85 int node;
86 char *p, *lastp;
87 char last[32];
88
89 /*
90 * If the bootpath doesn't start with a / then it isn't
91 * an OFW path and probably is an alias, so look up the alias
92 * and regenerate the full bootpath so device_register will work.
93 */
94 if (bootpath[0] != '/' && bootpath[0] != '\0') {
95 int aliases = OF_finddevice("/aliases");
96 char tmpbuf[100];
97 char aliasbuf[256];
98 if (aliases != 0) {
99 char *cp1, *cp2, *cp;
100 char saved_ch = '\0';
101 int len;
102 cp1 = strchr(bootpath, ':');
103 cp2 = strchr(bootpath, ',');
104 cp = cp1;
105 if (cp1 == NULL || (cp2 != NULL && cp2 < cp1))
106 cp = cp2;
107 tmpbuf[0] = '\0';
108 if (cp != NULL) {
109 strcpy(tmpbuf, cp);
110 saved_ch = *cp;
111 *cp = '\0';
112 }
113 len = OF_getprop(aliases, bootpath, aliasbuf,
114 sizeof(aliasbuf));
115 if (len > 0) {
116 if (aliasbuf[len-1] == '\0')
117 len--;
118 memcpy(bootpath, aliasbuf, len);
119 strcpy(&bootpath[len], tmpbuf);
120 } else {
121 *cp = saved_ch;
122 }
123 }
124 }
125
126 /*
127 * Strip kernel name. bootpath contains "OF-path"/"kernel".
128 *
129 * for example:
130 * /bandit@F2000000/gc@10/53c94@10000/sd@0,0/netbsd (OF-1.x)
131 * /pci/mac-io/ata-3@2000/disk@0:0/netbsd.new (OF-3.x)
132 */
133 strcpy(cbootpath, bootpath);
134 while ((node = OF_finddevice(cbootpath)) == -1) {
135 if ((p = strrchr(cbootpath, '/')) == NULL)
136 break;
137 *p = '\0';
138 }
139
140 if (node == -1) {
141 /* Cannot canonicalize... use bootpath anyway. */
142 strcpy(cbootpath, bootpath);
143
144 return;
145 }
146
147 /*
148 * cbootpath is a valid OF path. Use package-to-path to
149 * canonicalize pathname.
150 */
151
152 /* Back up the last component for later use. */
153 if ((p = strrchr(cbootpath, '/')) != NULL)
154 strcpy(last, p + 1);
155 else
156 last[0] = '\0';
157
158 memset(cbootpath, 0, sizeof(cbootpath));
159 OF_package_to_path(node, cbootpath, sizeof(cbootpath) - 1);
160
161 /*
162 * OF_1.x (at least) always returns addr == 0 for
163 * SCSI disks (i.e. "/bandit (at) .../.../sd@0,0").
164 */
165 lastp = strrchr(cbootpath, '/');
166 if (lastp != NULL) {
167 lastp++;
168 if (strncmp(lastp, "sd@", 3) == 0
169 && strncmp(last, "sd@", 3) == 0)
170 strcpy(lastp, last);
171 } else {
172 lastp = cbootpath;
173 }
174
175 /*
176 * At this point, cbootpath contains like:
177 * "/pci@80000000/mac-io@10/ata-3@20000/disk"
178 *
179 * The last component may have no address... so append it.
180 */
181 if (strchr(lastp, '@') == NULL) {
182 /* Append it. */
183 if ((p = strrchr(last, '@')) != NULL)
184 strcat(cbootpath, p);
185 }
186
187 if ((p = strrchr(lastp, ':')) != NULL) {
188 *p++ = '\0';
189 /* booted_partition = *p - '0'; XXX correct? */
190 }
191
192 /* XXX Does this belong here, or device_register()? */
193 if ((p = strrchr(lastp, ',')) != NULL)
194 *p = '\0';
195 }
196
197 /*
198 * device_register is called from config_attach as each device is
199 * attached. We use it to find the NetBSD device corresponding to the
200 * known OF boot device.
201 */
202 void
203 device_register(dev, aux)
204 struct device *dev;
205 void *aux;
206 {
207 static struct device *parent;
208 static char *bp = bootpath + 1, *cp = cbootpath;
209 unsigned long addr;
210 char *p;
211
212 if (booted_device)
213 return;
214
215 /* Skip over devices not represented in the OF tree. */
216 if (device_is_a(dev, "mainbus")) {
217 parent = dev;
218 return;
219 }
220 if (device_is_a(dev, "atapibus") || device_is_a(dev, "pci") ||
221 device_is_a(dev, "scsibus") || device_is_a(dev, "atabus"))
222 return;
223
224 if (device_is_a(device_parent(dev), "pci")) {
225 /* see if this is going to be console */
226 struct pci_attach_args *pa = aux;
227 prop_dictionary_t dict;
228 int node, sub;
229 int console = 0;
230
231 dict = device_properties(dev);
232 node = pcidev_to_ofdev(pa->pa_pc, pa->pa_tag);
233 prop_dictionary_set_uint32(dict, "device_node", node);
234
235 console = (node == console_node);
236
237 if (!console) {
238 /*
239 * see if any child matches since OF attaches nodes for
240 * each head and /chosen/stdout points to the head
241 * rather than the device itself in this case
242 */
243 sub = OF_child(node);
244 while ((sub != 0) && (sub != console_node)) {
245 sub = OF_peer(sub);
246 }
247 if (sub == console_node) {
248 console = true;
249 }
250 }
251
252 if (console) {
253 uint64_t cmap_cb;
254
255 prop_dictionary_set_uint32(dict, "instance_handle",
256 console_instance);
257 copyprops(console_node, dict);
258
259 gfb_cb.gcc_cookie = (void *)console_instance;
260 gfb_cb.gcc_set_mapreg = of_set_palette;
261 cmap_cb = (uint64_t)&gfb_cb;
262 prop_dictionary_set_uint64(dict, "cmap_callback",
263 cmap_cb);
264 }
265 }
266
267 if (device_is_a(device_parent(dev), "atapibus") ||
268 device_is_a(device_parent(dev), "atabus") ||
269 device_is_a(device_parent(dev), "pci") ||
270 device_is_a(device_parent(dev), "scsibus")) {
271 if (device_parent(device_parent(dev)) != parent)
272 return;
273 } else {
274 if (device_parent(dev) != parent)
275 return;
276 }
277
278 /* Get the address part of the current path component. The
279 * last component of the canonical bootpath may have no
280 * address (eg, "disk"), in which case we need to get the
281 * address from the original bootpath instead.
282 */
283 p = strchr(cp, '@');
284 if (!p) {
285 if (bp)
286 p = strchr(bp, '@');
287 if (!p)
288 addr = 0;
289 else {
290 addr = strtoul(p + 1, NULL, 16);
291 p = NULL;
292 }
293 } else
294 addr = strtoul(p + 1, &p, 16);
295
296 if (device_is_a(device_parent(dev), "mainbus")) {
297 struct confargs *ca = aux;
298
299 if (strcmp(ca->ca_name, "ofw") == 0) /* XXX */
300 return;
301 if (addr != ca->ca_reg[0])
302 return;
303 } else if (device_is_a(device_parent(dev), "pci")) {
304 struct pci_attach_args *pa = aux;
305
306 if (addr != pa->pa_device)
307 return;
308 } else if (device_is_a(device_parent(dev), "obio")) {
309 struct confargs *ca = aux;
310
311 if (addr != ca->ca_reg[0])
312 return;
313 } else if (device_is_a(device_parent(dev), "scsibus") ||
314 device_is_a(device_parent(dev), "atapibus")) {
315 struct scsipibus_attach_args *sa = aux;
316
317 /* periph_target is target for scsi, drive # for atapi */
318 if (addr != sa->sa_periph->periph_target)
319 return;
320 } else if (device_is_a(device_parent(device_parent(dev)), "pciide")) {
321 struct ata_device *adev = aux;
322
323 if (addr != adev->adev_drv_data->drive)
324 return;
325
326 /*
327 * OF splits channel and drive into separate path
328 * components, so check the addr part of the next
329 * component. (Ignore bp, because the canonical path
330 * will be complete in the pciide case.)
331 */
332 p = strchr(p, '@');
333 if (!p++)
334 return;
335 if (strtoul(p, &p, 16) != adev->adev_drv_data->drive)
336 return;
337 } else if (device_is_a(device_parent(device_parent(dev)), "wdc")) {
338 struct ata_device *adev = aux;
339
340 if (addr != adev->adev_drv_data->drive)
341 return;
342 } else
343 return;
344
345 /* If we reach this point, then dev is a match for the current
346 * path component.
347 */
348
349 if (p && *p) {
350 parent = dev;
351 cp = p;
352 bp = strchr(bp, '/');
353 if (bp)
354 bp++;
355 return;
356 } else {
357 booted_device = dev;
358 booted_partition = 0; /* XXX -- should be extracted from bootpath */
359 return;
360 }
361 }
362
363 /*
364 * Setup root device.
365 * Configure swap area.
366 */
367 void
368 cpu_rootconf()
369 {
370 printf("boot device: %s\n",
371 booted_device ? booted_device->dv_xname : "<unknown>");
372
373 setroot(booted_device, booted_partition);
374 }
375
376 /*
377 * Find OF-device corresponding to the PCI device.
378 */
379 int
380 pcidev_to_ofdev(pci_chipset_tag_t pc, pcitag_t tag)
381 {
382 int bus, dev, func;
383 u_int reg[5];
384 int p, q;
385 int l, b, d, f;
386
387 pci_decompose_tag(pc, tag, &bus, &dev, &func);
388
389 for (q = OF_peer(0); q; q = p) {
390 l = OF_getprop(q, "assigned-addresses", reg, sizeof(reg));
391 if (l > 4) {
392 b = (reg[0] >> 16) & 0xff;
393 d = (reg[0] >> 11) & 0x1f;
394 f = (reg[0] >> 8) & 0x07;
395
396 if (b == bus && d == dev && f == func)
397 return q;
398 }
399 if ((p = OF_child(q)))
400 continue;
401 while (q) {
402 if ((p = OF_peer(q)))
403 break;
404 q = OF_parent(q);
405 }
406 }
407 return 0;
408 }
409
410 static void
411 add_model_specifics(prop_dictionary_t dict)
412 {
413 const char *bl_rev_models[] = {
414 "PowerBook4,3", "PowerBook6,3", "PowerBook6,5", NULL};
415 int node;
416
417 node = OF_finddevice("/");
418
419 if (of_compatible(node, bl_rev_models) != -1) {
420 prop_dictionary_set_bool(dict, "backlight_level_reverted", 1);
421 }
422 }
423
424 static void
425 copyprops(int node, prop_dictionary_t dict)
426 {
427 uint32_t temp;
428
429 prop_dictionary_set_bool(dict, "is_console", 1);
430 if (!of_to_uint32_prop(dict, node, "width", "width")) {
431
432 OF_interpret("screen-width", 0, 1, &temp);
433 prop_dictionary_set_uint32(dict, "width", temp);
434 }
435 if (!of_to_uint32_prop(dict, console_node, "height", "height")) {
436
437 OF_interpret("screen-height", 0, 1, &temp);
438 prop_dictionary_set_uint32(dict, "height", temp);
439 }
440 of_to_uint32_prop(dict, console_node, "linebytes", "linebytes");
441 if (!of_to_uint32_prop(dict, console_node, "depth", "depth")) {
442 /*
443 * XXX we should check linebytes vs. width but those
444 * FBs that don't have a depth property ( /chaos/control... )
445 * won't have linebytes either
446 */
447 prop_dictionary_set_uint32(dict, "depth", 8);
448 }
449 if (!of_to_uint32_prop(dict, console_node, "address", "address")) {
450 uint32_t fbaddr = 0;
451 OF_interpret("frame-buffer-adr", 0, 1, &fbaddr);
452 if (fbaddr != 0)
453 prop_dictionary_set_uint32(dict, "address", fbaddr);
454 }
455 of_to_dataprop(dict, console_node, "EDID", "EDID");
456 add_model_specifics(dict);
457
458 temp = 0;
459 if (OF_getprop(console_node, "ATY,RefCLK", &temp, sizeof(temp)) != 4) {
460
461 OF_getprop(OF_parent(console_node), "ATY,RefCLK", &temp,
462 sizeof(temp));
463 }
464 if (temp != 0)
465 prop_dictionary_set_uint32(dict, "refclk", temp / 10);
466 }
467
468 static void
469 of_set_palette(void *cookie, int index, int r, int g, int b)
470 {
471 int ih = (int)cookie;
472
473 OF_call_method_1("color!", ih, 4, r, g, b, index);
474 }
475