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