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