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