Commit | Line | Data |
---|---|---|
942a6bdd DM |
1 | /* |
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 sparc32 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 | #include <linux/module.h> | |
24 | ||
25 | #include <asm/prom.h> | |
26 | #include <asm/oplib.h> | |
27 | ||
28 | static struct device_node *allnodes; | |
29 | ||
fb7cd9d9 DM |
30 | /* use when traversing tree through the allnext, child, sibling, |
31 | * or parent members of struct device_node. | |
32 | */ | |
33 | static DEFINE_RWLOCK(devtree_lock); | |
34 | ||
942a6bdd DM |
35 | int of_device_is_compatible(struct device_node *device, const char *compat) |
36 | { | |
37 | const char* cp; | |
38 | int cplen, l; | |
39 | ||
40 | cp = (char *) of_get_property(device, "compatible", &cplen); | |
41 | if (cp == NULL) | |
42 | return 0; | |
43 | while (cplen > 0) { | |
44 | if (strncmp(cp, compat, strlen(compat)) == 0) | |
45 | return 1; | |
46 | l = strlen(cp) + 1; | |
47 | cp += l; | |
48 | cplen -= l; | |
49 | } | |
50 | ||
51 | return 0; | |
52 | } | |
53 | EXPORT_SYMBOL(of_device_is_compatible); | |
54 | ||
55 | struct device_node *of_get_parent(const struct device_node *node) | |
56 | { | |
57 | struct device_node *np; | |
58 | ||
59 | if (!node) | |
60 | return NULL; | |
61 | ||
62 | np = node->parent; | |
63 | ||
64 | return np; | |
65 | } | |
66 | EXPORT_SYMBOL(of_get_parent); | |
67 | ||
68 | struct device_node *of_get_next_child(const struct device_node *node, | |
69 | struct device_node *prev) | |
70 | { | |
71 | struct device_node *next; | |
72 | ||
73 | next = prev ? prev->sibling : node->child; | |
74 | for (; next != 0; next = next->sibling) { | |
75 | break; | |
76 | } | |
77 | ||
78 | return next; | |
79 | } | |
80 | EXPORT_SYMBOL(of_get_next_child); | |
81 | ||
82 | struct device_node *of_find_node_by_path(const char *path) | |
83 | { | |
84 | struct device_node *np = allnodes; | |
85 | ||
86 | for (; np != 0; np = np->allnext) { | |
87 | if (np->full_name != 0 && strcmp(np->full_name, path) == 0) | |
88 | break; | |
89 | } | |
90 | ||
91 | return np; | |
92 | } | |
93 | EXPORT_SYMBOL(of_find_node_by_path); | |
94 | ||
95 | struct device_node *of_find_node_by_phandle(phandle handle) | |
96 | { | |
97 | struct device_node *np; | |
98 | ||
99 | for (np = allnodes; np != 0; np = np->allnext) | |
100 | if (np->node == handle) | |
101 | break; | |
102 | ||
103 | return np; | |
104 | } | |
105 | EXPORT_SYMBOL(of_find_node_by_phandle); | |
106 | ||
107 | struct device_node *of_find_node_by_name(struct device_node *from, | |
108 | const char *name) | |
109 | { | |
110 | struct device_node *np; | |
111 | ||
112 | np = from ? from->allnext : allnodes; | |
113 | for (; np != NULL; np = np->allnext) | |
114 | if (np->name != NULL && strcmp(np->name, name) == 0) | |
115 | break; | |
116 | ||
117 | return np; | |
118 | } | |
119 | EXPORT_SYMBOL(of_find_node_by_name); | |
120 | ||
121 | struct device_node *of_find_node_by_type(struct device_node *from, | |
122 | const char *type) | |
123 | { | |
124 | struct device_node *np; | |
125 | ||
126 | np = from ? from->allnext : allnodes; | |
127 | for (; np != 0; np = np->allnext) | |
128 | if (np->type != 0 && strcmp(np->type, type) == 0) | |
129 | break; | |
130 | ||
131 | return np; | |
132 | } | |
133 | EXPORT_SYMBOL(of_find_node_by_type); | |
134 | ||
135 | struct device_node *of_find_compatible_node(struct device_node *from, | |
136 | const char *type, const char *compatible) | |
137 | { | |
138 | struct device_node *np; | |
139 | ||
140 | np = from ? from->allnext : allnodes; | |
141 | for (; np != 0; np = np->allnext) { | |
142 | if (type != NULL | |
143 | && !(np->type != 0 && strcmp(np->type, type) == 0)) | |
144 | continue; | |
145 | if (of_device_is_compatible(np, compatible)) | |
146 | break; | |
147 | } | |
148 | ||
149 | return np; | |
150 | } | |
151 | EXPORT_SYMBOL(of_find_compatible_node); | |
152 | ||
153 | struct property *of_find_property(struct device_node *np, const char *name, | |
154 | int *lenp) | |
155 | { | |
156 | struct property *pp; | |
157 | ||
158 | for (pp = np->properties; pp != 0; pp = pp->next) { | |
159 | if (strcmp(pp->name, name) == 0) { | |
160 | if (lenp != 0) | |
161 | *lenp = pp->length; | |
162 | break; | |
163 | } | |
164 | } | |
165 | return pp; | |
166 | } | |
167 | EXPORT_SYMBOL(of_find_property); | |
168 | ||
169 | /* | |
170 | * Find a property with a given name for a given node | |
171 | * and return the value. | |
172 | */ | |
173 | void *of_get_property(struct device_node *np, const char *name, int *lenp) | |
174 | { | |
175 | struct property *pp = of_find_property(np,name,lenp); | |
176 | return pp ? pp->value : NULL; | |
177 | } | |
178 | EXPORT_SYMBOL(of_get_property); | |
179 | ||
180 | int of_getintprop_default(struct device_node *np, const char *name, int def) | |
181 | { | |
182 | struct property *prop; | |
183 | int len; | |
184 | ||
185 | prop = of_find_property(np, name, &len); | |
186 | if (!prop || len != 4) | |
187 | return def; | |
188 | ||
189 | return *(int *) prop->value; | |
190 | } | |
191 | EXPORT_SYMBOL(of_getintprop_default); | |
192 | ||
fb7cd9d9 DM |
193 | int of_set_property(struct device_node *dp, const char *name, void *val, int len) |
194 | { | |
195 | struct property **prevp; | |
196 | void *new_val; | |
197 | int err; | |
198 | ||
199 | new_val = kmalloc(len, GFP_KERNEL); | |
200 | if (!new_val) | |
201 | return -ENOMEM; | |
202 | ||
203 | memcpy(new_val, val, len); | |
204 | ||
205 | err = -ENODEV; | |
206 | ||
207 | write_lock(&devtree_lock); | |
208 | prevp = &dp->properties; | |
209 | while (*prevp) { | |
210 | struct property *prop = *prevp; | |
211 | ||
212 | if (!strcmp(prop->name, name)) { | |
213 | void *old_val = prop->value; | |
214 | int ret; | |
215 | ||
216 | ret = prom_setprop(dp->node, name, val, len); | |
217 | err = -EINVAL; | |
218 | if (ret >= 0) { | |
219 | prop->value = new_val; | |
220 | prop->length = len; | |
221 | ||
222 | if (OF_IS_DYNAMIC(prop)) | |
223 | kfree(old_val); | |
224 | ||
225 | OF_MARK_DYNAMIC(prop); | |
226 | ||
227 | err = 0; | |
228 | } | |
229 | break; | |
230 | } | |
231 | prevp = &(*prevp)->next; | |
232 | } | |
233 | write_unlock(&devtree_lock); | |
234 | ||
235 | /* XXX Upate procfs if necessary... */ | |
236 | ||
237 | return err; | |
238 | } | |
239 | EXPORT_SYMBOL(of_set_property); | |
240 | ||
942a6bdd DM |
241 | static unsigned int prom_early_allocated; |
242 | ||
243 | static void * __init prom_early_alloc(unsigned long size) | |
244 | { | |
245 | void *ret; | |
246 | ||
247 | ret = __alloc_bootmem(size, SMP_CACHE_BYTES, 0UL); | |
248 | if (ret != NULL) | |
249 | memset(ret, 0, size); | |
250 | ||
251 | prom_early_allocated += size; | |
252 | ||
253 | return ret; | |
254 | } | |
255 | ||
256 | static int is_root_node(const struct device_node *dp) | |
257 | { | |
258 | if (!dp) | |
259 | return 0; | |
260 | ||
261 | return (dp->parent == NULL); | |
262 | } | |
263 | ||
264 | /* The following routines deal with the black magic of fully naming a | |
265 | * node. | |
266 | * | |
267 | * Certain well known named nodes are just the simple name string. | |
268 | * | |
269 | * Actual devices have an address specifier appended to the base name | |
270 | * string, like this "foo@addr". The "addr" can be in any number of | |
271 | * formats, and the platform plus the type of the node determine the | |
272 | * format and how it is constructed. | |
273 | * | |
274 | * For children of the ROOT node, the naming convention is fixed and | |
275 | * determined by whether this is a sun4u or sun4v system. | |
276 | * | |
277 | * For children of other nodes, it is bus type specific. So | |
278 | * we walk up the tree until we discover a "device_type" property | |
279 | * we recognize and we go from there. | |
280 | */ | |
281 | static void __init sparc32_path_component(struct device_node *dp, char *tmp_buf) | |
282 | { | |
283 | struct linux_prom_registers *regs; | |
284 | struct property *rprop; | |
285 | ||
286 | rprop = of_find_property(dp, "reg", NULL); | |
287 | if (!rprop) | |
288 | return; | |
289 | ||
290 | regs = rprop->value; | |
291 | sprintf(tmp_buf, "%s@%x,%x", | |
292 | dp->name, | |
293 | regs->which_io, regs->phys_addr); | |
294 | } | |
295 | ||
296 | /* "name@slot,offset" */ | |
297 | static void __init sbus_path_component(struct device_node *dp, char *tmp_buf) | |
298 | { | |
299 | struct linux_prom_registers *regs; | |
300 | struct property *prop; | |
301 | ||
302 | prop = of_find_property(dp, "reg", NULL); | |
303 | if (!prop) | |
304 | return; | |
305 | ||
306 | regs = prop->value; | |
307 | sprintf(tmp_buf, "%s@%x,%x", | |
308 | dp->name, | |
309 | regs->which_io, | |
310 | regs->phys_addr); | |
311 | } | |
312 | ||
313 | /* "name@devnum[,func]" */ | |
314 | static void __init pci_path_component(struct device_node *dp, char *tmp_buf) | |
315 | { | |
316 | struct linux_prom_pci_registers *regs; | |
317 | struct property *prop; | |
318 | unsigned int devfn; | |
319 | ||
320 | prop = of_find_property(dp, "reg", NULL); | |
321 | if (!prop) | |
322 | return; | |
323 | ||
324 | regs = prop->value; | |
325 | devfn = (regs->phys_hi >> 8) & 0xff; | |
326 | if (devfn & 0x07) { | |
327 | sprintf(tmp_buf, "%s@%x,%x", | |
328 | dp->name, | |
329 | devfn >> 3, | |
330 | devfn & 0x07); | |
331 | } else { | |
332 | sprintf(tmp_buf, "%s@%x", | |
333 | dp->name, | |
334 | devfn >> 3); | |
335 | } | |
336 | } | |
337 | ||
338 | /* "name@addrhi,addrlo" */ | |
339 | static void __init ebus_path_component(struct device_node *dp, char *tmp_buf) | |
340 | { | |
341 | struct linux_prom_registers *regs; | |
342 | struct property *prop; | |
343 | ||
344 | prop = of_find_property(dp, "reg", NULL); | |
345 | if (!prop) | |
346 | return; | |
347 | ||
348 | regs = prop->value; | |
349 | ||
350 | sprintf(tmp_buf, "%s@%x,%x", | |
351 | dp->name, | |
352 | regs->which_io, regs->phys_addr); | |
353 | } | |
354 | ||
355 | static void __init __build_path_component(struct device_node *dp, char *tmp_buf) | |
356 | { | |
357 | struct device_node *parent = dp->parent; | |
358 | ||
359 | if (parent != NULL) { | |
360 | if (!strcmp(parent->type, "pci") || | |
361 | !strcmp(parent->type, "pciex")) | |
362 | return pci_path_component(dp, tmp_buf); | |
363 | if (!strcmp(parent->type, "sbus")) | |
364 | return sbus_path_component(dp, tmp_buf); | |
365 | if (!strcmp(parent->type, "ebus")) | |
366 | return ebus_path_component(dp, tmp_buf); | |
367 | ||
368 | /* "isa" is handled with platform naming */ | |
369 | } | |
370 | ||
371 | /* Use platform naming convention. */ | |
372 | return sparc32_path_component(dp, tmp_buf); | |
373 | } | |
374 | ||
375 | static char * __init build_path_component(struct device_node *dp) | |
376 | { | |
377 | char tmp_buf[64], *n; | |
378 | ||
379 | tmp_buf[0] = '\0'; | |
380 | __build_path_component(dp, tmp_buf); | |
381 | if (tmp_buf[0] == '\0') | |
382 | strcpy(tmp_buf, dp->name); | |
383 | ||
384 | n = prom_early_alloc(strlen(tmp_buf) + 1); | |
385 | strcpy(n, tmp_buf); | |
386 | ||
387 | return n; | |
388 | } | |
389 | ||
390 | static char * __init build_full_name(struct device_node *dp) | |
391 | { | |
392 | int len, ourlen, plen; | |
393 | char *n; | |
394 | ||
395 | plen = strlen(dp->parent->full_name); | |
396 | ourlen = strlen(dp->path_component_name); | |
397 | len = ourlen + plen + 2; | |
398 | ||
399 | n = prom_early_alloc(len); | |
400 | strcpy(n, dp->parent->full_name); | |
401 | if (!is_root_node(dp->parent)) { | |
402 | strcpy(n + plen, "/"); | |
403 | plen++; | |
404 | } | |
405 | strcpy(n + plen, dp->path_component_name); | |
406 | ||
407 | return n; | |
408 | } | |
409 | ||
87b385da DM |
410 | static unsigned int unique_id; |
411 | ||
412 | static struct property * __init build_one_prop(phandle node, char *prev, char *special_name, void *special_val, int special_len) | |
942a6bdd DM |
413 | { |
414 | static struct property *tmp = NULL; | |
415 | struct property *p; | |
416 | int len; | |
417 | ||
418 | if (tmp) { | |
419 | p = tmp; | |
420 | memset(p, 0, sizeof(*p) + 32); | |
421 | tmp = NULL; | |
87b385da | 422 | } else { |
942a6bdd | 423 | p = prom_early_alloc(sizeof(struct property) + 32); |
87b385da DM |
424 | p->unique_id = unique_id++; |
425 | } | |
942a6bdd DM |
426 | |
427 | p->name = (char *) (p + 1); | |
87b385da DM |
428 | if (special_name) { |
429 | p->length = special_len; | |
430 | p->value = prom_early_alloc(special_len); | |
431 | memcpy(p->value, special_val, special_len); | |
942a6bdd | 432 | } else { |
87b385da DM |
433 | if (prev == NULL) { |
434 | prom_firstprop(node, p->name); | |
435 | } else { | |
436 | prom_nextprop(node, prev, p->name); | |
437 | } | |
438 | if (strlen(p->name) == 0) { | |
439 | tmp = p; | |
440 | return NULL; | |
441 | } | |
442 | p->length = prom_getproplen(node, p->name); | |
443 | if (p->length <= 0) { | |
444 | p->length = 0; | |
445 | } else { | |
446 | p->value = prom_early_alloc(p->length + 1); | |
447 | prom_getproperty(node, p->name, p->value, p->length); | |
448 | ((unsigned char *)p->value)[p->length] = '\0'; | |
449 | } | |
942a6bdd DM |
450 | } |
451 | return p; | |
452 | } | |
453 | ||
454 | static struct property * __init build_prop_list(phandle node) | |
455 | { | |
456 | struct property *head, *tail; | |
457 | ||
87b385da DM |
458 | head = tail = build_one_prop(node, NULL, |
459 | ".node", &node, sizeof(node)); | |
460 | ||
461 | tail->next = build_one_prop(node, NULL, NULL, NULL, 0); | |
462 | tail = tail->next; | |
942a6bdd | 463 | while(tail) { |
87b385da DM |
464 | tail->next = build_one_prop(node, tail->name, |
465 | NULL, NULL, 0); | |
942a6bdd DM |
466 | tail = tail->next; |
467 | } | |
468 | ||
469 | return head; | |
470 | } | |
471 | ||
472 | static char * __init get_one_property(phandle node, char *name) | |
473 | { | |
474 | char *buf = "<NULL>"; | |
475 | int len; | |
476 | ||
477 | len = prom_getproplen(node, name); | |
478 | if (len > 0) { | |
479 | buf = prom_early_alloc(len); | |
480 | len = prom_getproperty(node, name, buf, len); | |
481 | } | |
482 | ||
483 | return buf; | |
484 | } | |
485 | ||
486 | static struct device_node * __init create_node(phandle node) | |
487 | { | |
488 | struct device_node *dp; | |
489 | ||
490 | if (!node) | |
491 | return NULL; | |
492 | ||
493 | dp = prom_early_alloc(sizeof(*dp)); | |
87b385da | 494 | dp->unique_id = unique_id++; |
942a6bdd DM |
495 | |
496 | kref_init(&dp->kref); | |
497 | ||
498 | dp->name = get_one_property(node, "name"); | |
499 | dp->type = get_one_property(node, "device_type"); | |
500 | dp->node = node; | |
501 | ||
502 | /* Build interrupts later... */ | |
503 | ||
504 | dp->properties = build_prop_list(node); | |
505 | ||
506 | return dp; | |
507 | } | |
508 | ||
509 | static struct device_node * __init build_tree(struct device_node *parent, phandle node, struct device_node ***nextp) | |
510 | { | |
511 | struct device_node *dp; | |
512 | ||
513 | dp = create_node(node); | |
514 | if (dp) { | |
515 | *(*nextp) = dp; | |
516 | *nextp = &dp->allnext; | |
517 | ||
518 | dp->parent = parent; | |
519 | dp->path_component_name = build_path_component(dp); | |
520 | dp->full_name = build_full_name(dp); | |
521 | ||
522 | dp->child = build_tree(dp, prom_getchild(node), nextp); | |
523 | ||
524 | dp->sibling = build_tree(parent, prom_getsibling(node), nextp); | |
525 | } | |
526 | ||
527 | return dp; | |
528 | } | |
529 | ||
530 | void __init prom_build_devicetree(void) | |
531 | { | |
532 | struct device_node **nextp; | |
533 | ||
534 | allnodes = create_node(prom_root_node); | |
535 | allnodes->path_component_name = ""; | |
536 | allnodes->full_name = "/"; | |
537 | ||
538 | nextp = &allnodes->allnext; | |
539 | allnodes->child = build_tree(allnodes, | |
540 | prom_getchild(allnodes->node), | |
541 | &nextp); | |
542 | printk("PROM: Built device tree with %u bytes of memory.\n", | |
543 | prom_early_allocated); | |
544 | } |