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