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