ofw_autoconf.c revision 1.6 1 /* $NetBSD: ofw_autoconf.c,v 1.6 2008/01/09 17:45:46 aymeric 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.6 2008/01/09 17:45:46 aymeric 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/pcireg.h>
50 #include <dev/pci/pcivar.h>
51 #include <dev/scsipi/scsi_all.h>
52 #include <dev/scsipi/scsipi_all.h>
53 #include <dev/scsipi/scsiconf.h>
54 #include <dev/ata/atavar.h>
55 #include <dev/ic/wdcvar.h>
56
57 extern char bootpath[256];
58 char cbootpath[256];
59 int console_node = 0, console_instance = 0;
60
61 static void canonicalize_bootpath(void);
62
63 /*
64 * Determine device configuration for a machine.
65 */
66 void
67 cpu_configure(void)
68 {
69 init_interrupt();
70 canonicalize_bootpath();
71
72 if (config_rootfound("mainbus", NULL) == NULL)
73 panic("configure: mainbus not configured");
74
75 genppc_cpu_configure();
76 }
77
78 static void
79 canonicalize_bootpath(void)
80 {
81 int node;
82 char *p, *lastp;
83 char last[32];
84
85 /*
86 * If the bootpath doesn't start with a / then it isn't
87 * an OFW path and probably is an alias, so look up the alias
88 * and regenerate the full bootpath so device_register will work.
89 */
90 if (bootpath[0] != '/' && bootpath[0] != '\0') {
91 int aliases = OF_finddevice("/aliases");
92 char tmpbuf[100];
93 char aliasbuf[256];
94 if (aliases != 0) {
95 char *cp1, *cp2, *cp;
96 char saved_ch = '\0';
97 int len;
98 cp1 = strchr(bootpath, ':');
99 cp2 = strchr(bootpath, ',');
100 cp = cp1;
101 if (cp1 == NULL || (cp2 != NULL && cp2 < cp1))
102 cp = cp2;
103 tmpbuf[0] = '\0';
104 if (cp != NULL) {
105 strcpy(tmpbuf, cp);
106 saved_ch = *cp;
107 *cp = '\0';
108 }
109 len = OF_getprop(aliases, bootpath, aliasbuf,
110 sizeof(aliasbuf));
111 if (len > 0) {
112 if (aliasbuf[len-1] == '\0')
113 len--;
114 memcpy(bootpath, aliasbuf, len);
115 strcpy(&bootpath[len], tmpbuf);
116 } else {
117 *cp = saved_ch;
118 }
119 }
120 }
121
122 /*
123 * Strip kernel name. bootpath contains "OF-path"/"kernel".
124 *
125 * for example:
126 * /bandit@F2000000/gc@10/53c94@10000/sd@0,0/netbsd (OF-1.x)
127 * /pci/mac-io/ata-3@2000/disk@0:0/netbsd.new (OF-3.x)
128 */
129 strcpy(cbootpath, bootpath);
130 while ((node = OF_finddevice(cbootpath)) == -1) {
131 if ((p = strrchr(cbootpath, '/')) == NULL)
132 break;
133 *p = '\0';
134 }
135
136 if (node == -1) {
137 /* Cannot canonicalize... use bootpath anyway. */
138 strcpy(cbootpath, bootpath);
139
140 return;
141 }
142
143 /*
144 * cbootpath is a valid OF path. Use package-to-path to
145 * canonicalize pathname.
146 */
147
148 /* Back up the last component for later use. */
149 if ((p = strrchr(cbootpath, '/')) != NULL)
150 strcpy(last, p + 1);
151 else
152 last[0] = '\0';
153
154 memset(cbootpath, 0, sizeof(cbootpath));
155 OF_package_to_path(node, cbootpath, sizeof(cbootpath) - 1);
156
157 /*
158 * OF_1.x (at least) always returns addr == 0 for
159 * SCSI disks (i.e. "/bandit (at) .../.../sd@0,0").
160 */
161 lastp = strrchr(cbootpath, '/');
162 if (lastp != NULL) {
163 lastp++;
164 if (strncmp(lastp, "sd@", 3) == 0
165 && strncmp(last, "sd@", 3) == 0)
166 strcpy(lastp, last);
167 } else {
168 lastp = cbootpath;
169 }
170
171 /*
172 * At this point, cbootpath contains like:
173 * "/pci@80000000/mac-io@10/ata-3@20000/disk"
174 *
175 * The last component may have no address... so append it.
176 */
177 if (strchr(lastp, '@') == NULL) {
178 /* Append it. */
179 if ((p = strrchr(last, '@')) != NULL)
180 strcat(cbootpath, p);
181 }
182
183 if ((p = strrchr(lastp, ':')) != NULL) {
184 *p++ = '\0';
185 /* booted_partition = *p - '0'; XXX correct? */
186 }
187
188 /* XXX Does this belong here, or device_register()? */
189 if ((p = strrchr(lastp, ',')) != NULL)
190 *p = '\0';
191 }
192
193 /*
194 * device_register is called from config_attach as each device is
195 * attached. We use it to find the NetBSD device corresponding to the
196 * known OF boot device.
197 */
198 void
199 device_register(dev, aux)
200 struct device *dev;
201 void *aux;
202 {
203 static struct device *parent;
204 static char *bp = bootpath + 1, *cp = cbootpath;
205 unsigned long addr;
206 char *p;
207
208 /* Skip over devices not represented in the OF tree. */
209 if (device_is_a(dev, "mainbus")) {
210 parent = dev;
211 return;
212 }
213 if (device_is_a(dev, "atapibus") || device_is_a(dev, "pci") ||
214 device_is_a(dev, "scsibus") || device_is_a(dev, "atabus"))
215 return;
216
217 if (device_is_a(device_parent(dev), "pci")) {
218 struct pci_attach_args *pa = aux;
219 prop_dictionary_t dict;
220 prop_bool_t b;
221 int node;
222 char name[32];
223
224 dict = device_properties(dev);
225 node = pcidev_to_ofdev(pa->pa_pc, pa->pa_tag);
226
227 /* enable configuration of irq 14/15 for VIA native IDE */
228 if (device_is_a(dev, "viaide") &&
229 strncmp(model_name, "Pegasos", 7) == 0) {
230 b = prop_bool_create(true);
231 KASSERT(b != NULL);
232 (void)prop_dictionary_set(dict,
233 "use-compat-native-irq", b);
234 prop_object_release(b);
235 }
236
237 if (node != 0) {
238 int pci_class = 0;
239
240 prop_dictionary_set_uint32(dict, "device_node", node);
241
242 /* see if this is going to be console */
243 memset(name, 0, sizeof(name));
244 OF_getprop(node, "device_type", name, sizeof(name));
245
246 OF_getprop(node, "class-code", &pci_class,
247 sizeof pci_class);
248 pci_class = (pci_class >> 16) & 0xff;
249
250 if (strcmp(name, "display") == 0 ||
251 pci_class == PCI_CLASS_DISPLAY) {
252 /* setup display properties for fb driver */
253 prop_dictionary_set_bool(dict, "is_console", 0);
254 copy_disp_props(dev, node, dict);
255 }
256 }
257 }
258
259 if (booted_device)
260 return;
261
262 if (device_is_a(device_parent(dev), "atapibus") ||
263 device_is_a(device_parent(dev), "atabus") ||
264 device_is_a(device_parent(dev), "pci") ||
265 device_is_a(device_parent(dev), "scsibus")) {
266 if (device_parent(device_parent(dev)) != parent)
267 return;
268 } else {
269 if (device_parent(dev) != parent)
270 return;
271 }
272
273 /* Get the address part of the current path component. The
274 * last component of the canonical bootpath may have no
275 * address (eg, "disk"), in which case we need to get the
276 * address from the original bootpath instead.
277 */
278 p = strchr(cp, '@');
279 if (!p) {
280 if (bp)
281 p = strchr(bp, '@');
282 if (!p)
283 addr = 0;
284 else {
285 addr = strtoul(p + 1, NULL, 16);
286 p = NULL;
287 }
288 } else
289 addr = strtoul(p + 1, &p, 16);
290
291 if (device_is_a(device_parent(dev), "mainbus")) {
292 struct confargs *ca = aux;
293
294 if (strcmp(ca->ca_name, "ofw") == 0) /* XXX */
295 return;
296 if (addr != ca->ca_reg[0])
297 return;
298 } else if (device_is_a(device_parent(dev), "pci")) {
299 struct pci_attach_args *pa = aux;
300
301 if (addr != pa->pa_device)
302 return;
303 } else if (device_is_a(device_parent(dev), "obio")) {
304 struct confargs *ca = aux;
305
306 if (addr != ca->ca_reg[0])
307 return;
308 } else if (device_is_a(device_parent(dev), "scsibus") ||
309 device_is_a(device_parent(dev), "atapibus")) {
310 struct scsipibus_attach_args *sa = aux;
311
312 /* periph_target is target for scsi, drive # for atapi */
313 if (addr != sa->sa_periph->periph_target)
314 return;
315 } else if (device_is_a(device_parent(device_parent(dev)), "pciide")) {
316 struct ata_device *adev = aux;
317
318 if (addr != adev->adev_drv_data->drive)
319 return;
320
321 /*
322 * OF splits channel and drive into separate path
323 * components, so check the addr part of the next
324 * component. (Ignore bp, because the canonical path
325 * will be complete in the pciide case.)
326 */
327 p = strchr(p, '@');
328 if (!p++)
329 return;
330 if (strtoul(p, &p, 16) != adev->adev_drv_data->drive)
331 return;
332 } else if (device_is_a(device_parent(device_parent(dev)), "wdc")) {
333 struct ata_device *adev = aux;
334
335 if (addr != adev->adev_drv_data->drive)
336 return;
337 } else
338 return;
339
340 /* If we reach this point, then dev is a match for the current
341 * path component.
342 */
343
344 if (p && *p) {
345 parent = dev;
346 cp = p;
347 bp = strchr(bp, '/');
348 if (bp)
349 bp++;
350 return;
351 } else {
352 booted_device = dev;
353 booted_partition = 0; /* XXX -- should be extracted from bootpath */
354 return;
355 }
356 }
357
358 /*
359 * Setup root device.
360 * Configure swap area.
361 */
362 void
363 cpu_rootconf()
364 {
365 printf("boot device: %s\n",
366 booted_device ? booted_device->dv_xname : "<unknown>");
367
368 setroot(booted_device, booted_partition);
369 }
370
371 /*
372 * Find OF-device corresponding to the PCI device.
373 */
374 int
375 pcidev_to_ofdev(pci_chipset_tag_t pc, pcitag_t tag)
376 {
377 int bus, dev, func;
378 u_int reg[5];
379 int p, q;
380 int l, b, d, f;
381
382 pci_decompose_tag(pc, tag, &bus, &dev, &func);
383
384 for (q = OF_peer(0); q; q = p) {
385 l = OF_getprop(q, "assigned-addresses", reg, sizeof(reg));
386 if (l > 4) {
387 b = (reg[0] >> 16) & 0xff;
388 d = (reg[0] >> 11) & 0x1f;
389 f = (reg[0] >> 8) & 0x07;
390
391 if (b == bus && d == dev && f == func)
392 return q;
393 }
394 if ((p = OF_child(q)))
395 continue;
396 while (q) {
397 if ((p = OF_peer(q)))
398 break;
399 q = OF_parent(q);
400 }
401 }
402 return 0;
403 }
404