blob: fd46e94ce06b114b5dfe708278c5dc29fa8bc17b [file] [log] [blame]
David S. Miller372b07b2006-06-21 15:35:28 -07001/*
2 * Procedures for creating, accessing and interpreting the device tree.
3 *
4 * Paul Mackerras August 1996.
5 * Copyright (C) 1996-2005 Paul Mackerras.
6 *
7 * Adapted for 64bit PowerPC by Dave Engebretsen and Peter Bergner.
8 * {engebret|bergner}@us.ibm.com
9 *
10 * Adapted for sparc64 by David S. Miller davem@davemloft.net
11 *
12 * This program is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU General Public License
14 * as published by the Free Software Foundation; either version
15 * 2 of the License, or (at your option) any later version.
16 */
17
18#include <linux/kernel.h>
19#include <linux/types.h>
20#include <linux/string.h>
21#include <linux/mm.h>
22#include <linux/bootmem.h>
23
24#include <asm/prom.h>
25#include <asm/oplib.h>
26
27static struct device_node *allnodes;
28
29struct device_node *of_get_parent(const struct device_node *node)
30{
31 struct device_node *np;
32
33 if (!node)
34 return NULL;
35
36 np = node->parent;
37
38 return np;
39}
40
41struct device_node *of_get_next_child(const struct device_node *node,
42 struct device_node *prev)
43{
44 struct device_node *next;
45
46 next = prev ? prev->sibling : node->child;
47 for (; next != 0; next = next->sibling) {
48 break;
49 }
50
51 return next;
52}
53
54struct device_node *of_find_node_by_path(const char *path)
55{
56 struct device_node *np = allnodes;
57
58 for (; np != 0; np = np->allnext) {
59 if (np->full_name != 0 && strcmp(np->full_name, path) == 0)
60 break;
61 }
62
63 return np;
64}
65
David S. Milleraaf7cec2006-06-21 16:33:54 -070066struct device_node *of_find_node_by_name(struct device_node *from,
67 const char *name)
68{
69 struct device_node *np;
70
71 np = from ? from->allnext : allnodes;
72 for (; np != NULL; np = np->allnext)
73 if (np->name != NULL && strcmp(np->name, name) == 0)
74 break;
75
76 return np;
77}
78
79struct device_node *of_find_node_by_type(struct device_node *from,
80 const char *type)
81{
82 struct device_node *np;
83
84 np = from ? from->allnext : allnodes;
85 for (; np != 0; np = np->allnext)
86 if (np->type != 0 && strcmp(np->type, type) == 0)
87 break;
88
89 return np;
90}
91
David S. Miller372b07b2006-06-21 15:35:28 -070092struct property *of_find_property(struct device_node *np, const char *name,
93 int *lenp)
94{
95 struct property *pp;
96
97 for (pp = np->properties; pp != 0; pp = pp->next) {
98 if (strcmp(pp->name, name) == 0) {
99 if (lenp != 0)
100 *lenp = pp->length;
101 break;
102 }
103 }
104 return pp;
105}
106
107static unsigned int prom_early_allocated;
108
109static void * __init prom_early_alloc(unsigned long size)
110{
111 void *ret;
112
113 ret = __alloc_bootmem(size, SMP_CACHE_BYTES, 0UL);
114 if (ret != NULL)
115 memset(ret, 0, size);
116
117 prom_early_allocated += size;
118
119 return ret;
120}
121
122static int is_root_node(const struct device_node *dp)
123{
124 if (!dp)
125 return 0;
126
127 return (dp->parent == NULL);
128}
129
130/* The following routines deal with the black magic of fully naming a
131 * node.
132 *
133 * Certain well known named nodes are just the simple name string.
134 *
135 * Actual devices have an address specifier appended to the base name
136 * string, like this "foo@addr". The "addr" can be in any number of
137 * formats, and the platform plus the type of the node determine the
138 * format and how it is constructed.
139 *
140 * For children of the ROOT node, the naming convention is fixed and
141 * determined by whether this is a sun4u or sun4v system.
142 *
143 * For children of other nodes, it is bus type specific. So
144 * we walk up the tree until we discover a "device_type" property
145 * we recognize and we go from there.
146 *
147 * As an example, the boot device on my workstation has a full path:
148 *
149 * /pci@1e,600000/ide@d/disk@0,0:c
150 */
151static void __init sun4v_path_component(struct device_node *dp, char *tmp_buf)
152{
153 struct linux_prom64_registers *regs;
154 struct property *rprop;
155 u32 high_bits, low_bits, type;
156
157 rprop = of_find_property(dp, "reg", NULL);
158 if (!rprop)
159 return;
160
161 regs = rprop->value;
162 if (!is_root_node(dp->parent)) {
163 sprintf(tmp_buf, "%s@%x,%x",
164 dp->name,
165 (unsigned int) (regs->phys_addr >> 32UL),
166 (unsigned int) (regs->phys_addr & 0xffffffffUL));
167 return;
168 }
169
170 type = regs->phys_addr >> 60UL;
171 high_bits = (regs->phys_addr >> 32UL) & 0x0fffffffUL;
172 low_bits = (regs->phys_addr & 0xffffffffUL);
173
174 if (type == 0 || type == 8) {
175 const char *prefix = (type == 0) ? "m" : "i";
176
177 if (low_bits)
178 sprintf(tmp_buf, "%s@%s%x,%x",
179 dp->name, prefix,
180 high_bits, low_bits);
181 else
182 sprintf(tmp_buf, "%s@%s%x",
183 dp->name,
184 prefix,
185 high_bits);
186 } else if (type == 12) {
187 sprintf(tmp_buf, "%s@%x",
188 dp->name, high_bits);
189 }
190}
191
192static void __init sun4u_path_component(struct device_node *dp, char *tmp_buf)
193{
194 struct linux_prom64_registers *regs;
195 struct property *prop;
196
197 prop = of_find_property(dp, "reg", NULL);
198 if (!prop)
199 return;
200
201 regs = prop->value;
202 if (!is_root_node(dp->parent)) {
203 sprintf(tmp_buf, "%s@%x,%x",
204 dp->name,
205 (unsigned int) (regs->phys_addr >> 32UL),
206 (unsigned int) (regs->phys_addr & 0xffffffffUL));
207 return;
208 }
209
210 prop = of_find_property(dp, "upa-portid", NULL);
211 if (!prop)
212 prop = of_find_property(dp, "portid", NULL);
213 if (prop) {
214 unsigned long mask = 0xffffffffUL;
215
216 if (tlb_type >= cheetah)
217 mask = 0x7fffff;
218
219 sprintf(tmp_buf, "%s@%x,%x",
220 dp->name,
221 *(u32 *)prop->value,
222 (unsigned int) (regs->phys_addr & mask));
223 }
224}
225
226/* "name@slot,offset" */
227static void __init sbus_path_component(struct device_node *dp, char *tmp_buf)
228{
229 struct linux_prom_registers *regs;
230 struct property *prop;
231
232 prop = of_find_property(dp, "reg", NULL);
233 if (!prop)
234 return;
235
236 regs = prop->value;
237 sprintf(tmp_buf, "%s@%x,%x",
238 dp->name,
239 regs->which_io,
240 regs->phys_addr);
241}
242
243/* "name@devnum[,func]" */
244static void __init pci_path_component(struct device_node *dp, char *tmp_buf)
245{
246 struct linux_prom_pci_registers *regs;
247 struct property *prop;
248 unsigned int devfn;
249
250 prop = of_find_property(dp, "reg", NULL);
251 if (!prop)
252 return;
253
254 regs = prop->value;
255 devfn = (regs->phys_hi >> 8) & 0xff;
256 if (devfn & 0x07) {
257 sprintf(tmp_buf, "%s@%x,%x",
258 dp->name,
259 devfn >> 3,
260 devfn & 0x07);
261 } else {
262 sprintf(tmp_buf, "%s@%x",
263 dp->name,
264 devfn >> 3);
265 }
266}
267
268/* "name@UPA_PORTID,offset" */
269static void __init upa_path_component(struct device_node *dp, char *tmp_buf)
270{
271 struct linux_prom64_registers *regs;
272 struct property *prop;
273
274 prop = of_find_property(dp, "reg", NULL);
275 if (!prop)
276 return;
277
278 regs = prop->value;
279
280 prop = of_find_property(dp, "upa-portid", NULL);
281 if (!prop)
282 return;
283
284 sprintf(tmp_buf, "%s@%x,%x",
285 dp->name,
286 *(u32 *) prop->value,
287 (unsigned int) (regs->phys_addr & 0xffffffffUL));
288}
289
290/* "name@reg" */
291static void __init vdev_path_component(struct device_node *dp, char *tmp_buf)
292{
293 struct property *prop;
294 u32 *regs;
295
296 prop = of_find_property(dp, "reg", NULL);
297 if (!prop)
298 return;
299
300 regs = prop->value;
301
302 sprintf(tmp_buf, "%s@%x", dp->name, *regs);
303}
304
305/* "name@addrhi,addrlo" */
306static void __init ebus_path_component(struct device_node *dp, char *tmp_buf)
307{
308 struct linux_prom64_registers *regs;
309 struct property *prop;
310
311 prop = of_find_property(dp, "reg", NULL);
312 if (!prop)
313 return;
314
315 regs = prop->value;
316
317 sprintf(tmp_buf, "%s@%x,%x",
318 dp->name,
319 (unsigned int) (regs->phys_addr >> 32UL),
320 (unsigned int) (regs->phys_addr & 0xffffffffUL));
321}
322
323/* "name@bus,addr" */
324static void __init i2c_path_component(struct device_node *dp, char *tmp_buf)
325{
326 struct property *prop;
327 u32 *regs;
328
329 prop = of_find_property(dp, "reg", NULL);
330 if (!prop)
331 return;
332
333 regs = prop->value;
334
335 /* This actually isn't right... should look at the #address-cells
336 * property of the i2c bus node etc. etc.
337 */
338 sprintf(tmp_buf, "%s@%x,%x",
339 dp->name, regs[0], regs[1]);
340}
341
342/* "name@reg0[,reg1]" */
343static void __init usb_path_component(struct device_node *dp, char *tmp_buf)
344{
345 struct property *prop;
346 u32 *regs;
347
348 prop = of_find_property(dp, "reg", NULL);
349 if (!prop)
350 return;
351
352 regs = prop->value;
353
354 if (prop->length == sizeof(u32) || regs[1] == 1) {
355 sprintf(tmp_buf, "%s@%x",
356 dp->name, regs[0]);
357 } else {
358 sprintf(tmp_buf, "%s@%x,%x",
359 dp->name, regs[0], regs[1]);
360 }
361}
362
363/* "name@reg0reg1[,reg2reg3]" */
364static void __init ieee1394_path_component(struct device_node *dp, char *tmp_buf)
365{
366 struct property *prop;
367 u32 *regs;
368
369 prop = of_find_property(dp, "reg", NULL);
370 if (!prop)
371 return;
372
373 regs = prop->value;
374
375 if (regs[2] || regs[3]) {
376 sprintf(tmp_buf, "%s@%08x%08x,%04x%08x",
377 dp->name, regs[0], regs[1], regs[2], regs[3]);
378 } else {
379 sprintf(tmp_buf, "%s@%08x%08x",
380 dp->name, regs[0], regs[1]);
381 }
382}
383
384static void __init __build_path_component(struct device_node *dp, char *tmp_buf)
385{
386 struct device_node *parent = dp->parent;
387
388 if (parent != NULL) {
389 if (!strcmp(parent->type, "pci") ||
390 !strcmp(parent->type, "pciex"))
391 return pci_path_component(dp, tmp_buf);
392 if (!strcmp(parent->type, "sbus"))
393 return sbus_path_component(dp, tmp_buf);
394 if (!strcmp(parent->type, "upa"))
395 return upa_path_component(dp, tmp_buf);
396 if (!strcmp(parent->type, "ebus"))
397 return ebus_path_component(dp, tmp_buf);
398 if (!strcmp(parent->name, "usb") ||
399 !strcmp(parent->name, "hub"))
400 return usb_path_component(dp, tmp_buf);
401 if (!strcmp(parent->type, "i2c"))
402 return i2c_path_component(dp, tmp_buf);
403 if (!strcmp(parent->type, "firewire"))
404 return ieee1394_path_component(dp, tmp_buf);
405 if (!strcmp(parent->type, "virtual-devices"))
406 return vdev_path_component(dp, tmp_buf);
407
408 /* "isa" is handled with platform naming */
409 }
410
411 /* Use platform naming convention. */
412 if (tlb_type == hypervisor)
413 return sun4v_path_component(dp, tmp_buf);
414 else
415 return sun4u_path_component(dp, tmp_buf);
416}
417
418static char * __init build_path_component(struct device_node *dp)
419{
420 char tmp_buf[64], *n;
421
422 tmp_buf[0] = '\0';
423 __build_path_component(dp, tmp_buf);
424 if (tmp_buf[0] == '\0')
425 strcpy(tmp_buf, dp->name);
426
427 n = prom_early_alloc(strlen(tmp_buf) + 1);
428 strcpy(n, tmp_buf);
429
430 return n;
431}
432
433static char * __init build_full_name(struct device_node *dp)
434{
435 int len, ourlen, plen;
436 char *n;
437
438 plen = strlen(dp->parent->full_name);
439 ourlen = strlen(dp->path_component_name);
440 len = ourlen + plen + 2;
441
442 n = prom_early_alloc(len);
443 strcpy(n, dp->parent->full_name);
444 if (!is_root_node(dp->parent)) {
445 strcpy(n + plen, "/");
446 plen++;
447 }
448 strcpy(n + plen, dp->path_component_name);
449
450 return n;
451}
452
453static struct property * __init build_one_prop(phandle node, char *prev)
454{
455 static struct property *tmp = NULL;
456 struct property *p;
457
458 if (tmp) {
459 p = tmp;
460 memset(p, 0, sizeof(*p) + 32);
461 tmp = NULL;
462 } else
463 p = prom_early_alloc(sizeof(struct property) + 32);
464
465 p->name = (char *) (p + 1);
466 if (prev == NULL) {
467 prom_firstprop(node, p->name);
468 } else {
469 prom_nextprop(node, prev, p->name);
470 }
471 if (strlen(p->name) == 0) {
472 tmp = p;
473 return NULL;
474 }
475 p->length = prom_getproplen(node, p->name);
476 if (p->length <= 0) {
477 p->length = 0;
478 } else {
479 p->value = prom_early_alloc(p->length);
480 prom_getproperty(node, p->name, p->value, p->length);
481 }
482 return p;
483}
484
485static struct property * __init build_prop_list(phandle node)
486{
487 struct property *head, *tail;
488
489 head = tail = build_one_prop(node, NULL);
490 while(tail) {
491 tail->next = build_one_prop(node, tail->name);
492 tail = tail->next;
493 }
494
495 return head;
496}
497
498static char * __init get_one_property(phandle node, const char *name)
499{
500 char *buf = "<NULL>";
501 int len;
502
503 len = prom_getproplen(node, name);
504 if (len > 0) {
505 buf = prom_early_alloc(len);
506 prom_getproperty(node, name, buf, len);
507 }
508
509 return buf;
510}
511
512static struct device_node * __init create_node(phandle node)
513{
514 struct device_node *dp;
515
516 if (!node)
517 return NULL;
518
519 dp = prom_early_alloc(sizeof(*dp));
520
521 kref_init(&dp->kref);
522
523 dp->name = get_one_property(node, "name");
524 dp->type = get_one_property(node, "device_type");
525 dp->node = node;
526
527 /* Build interrupts later... */
528
529 dp->properties = build_prop_list(node);
530
531 return dp;
532}
533
534static struct device_node * __init build_tree(struct device_node *parent, phandle node, struct device_node ***nextp)
535{
536 struct device_node *dp;
537
538 dp = create_node(node);
539 if (dp) {
540 *(*nextp) = dp;
541 *nextp = &dp->allnext;
542
543 dp->parent = parent;
544 dp->path_component_name = build_path_component(dp);
545 dp->full_name = build_full_name(dp);
546
547 dp->child = build_tree(dp, prom_getchild(node), nextp);
548
549 dp->sibling = build_tree(parent, prom_getsibling(node), nextp);
550 }
551
552 return dp;
553}
554
555void __init prom_build_devicetree(void)
556{
557 struct device_node **nextp;
558
559 allnodes = create_node(prom_root_node);
560 allnodes->path_component_name = "";
561 allnodes->full_name = "/";
562
563 nextp = &allnodes->allnext;
564 allnodes->child = build_tree(allnodes,
565 prom_getchild(allnodes->node),
566 &nextp);
567 printk("PROM: Built device tree with %u bytes of memory.\n",
568 prom_early_allocated);
569}