2 * Functions for working with the Flattened Device Tree data format
4 * Copyright 2009 Benjamin Herrenschmidt, IBM Corp
5 * benh@kernel.crashing.org
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * version 2 as published by the Free Software Foundation.
12 #include <linux/kernel.h>
13 #include <linux/lmb.h>
14 #include <linux/initrd.h>
16 #include <linux/of_fdt.h>
19 #include <asm/machdep.h>
20 #endif /* CONFIG_PPC */
22 int __initdata dt_root_addr_cells;
23 int __initdata dt_root_size_cells;
25 struct boot_param_header *initial_boot_params;
27 char *find_flat_dt_string(u32 offset)
29 return ((char *)initial_boot_params) +
30 initial_boot_params->off_dt_strings + offset;
34 * of_scan_flat_dt - scan flattened tree blob and call callback on each.
35 * @it: callback function
36 * @data: context data pointer
38 * This function is used to scan the flattened device-tree, it is
39 * used to extract the memory information at boot before we can
42 int __init of_scan_flat_dt(int (*it)(unsigned long node,
43 const char *uname, int depth,
47 unsigned long p = ((unsigned long)initial_boot_params) +
48 initial_boot_params->off_dt_struct;
53 u32 tag = *((u32 *)p);
57 if (tag == OF_DT_END_NODE) {
65 if (tag == OF_DT_PROP) {
68 if (initial_boot_params->version < 0x10)
69 p = _ALIGN(p, sz >= 8 ? 8 : 4);
74 if (tag != OF_DT_BEGIN_NODE) {
75 pr_err("Invalid tag %x in flat device tree!\n", tag);
80 p = _ALIGN(p + strlen(pathp) + 1, 4);
81 if ((*pathp) == '/') {
83 for (lp = NULL, np = pathp; *np; np++)
89 rc = it(p, pathp, depth, data);
98 * of_get_flat_dt_root - find the root node in the flat blob
100 unsigned long __init of_get_flat_dt_root(void)
102 unsigned long p = ((unsigned long)initial_boot_params) +
103 initial_boot_params->off_dt_struct;
105 while (*((u32 *)p) == OF_DT_NOP)
107 BUG_ON(*((u32 *)p) != OF_DT_BEGIN_NODE);
109 return _ALIGN(p + strlen((char *)p) + 1, 4);
113 * of_get_flat_dt_prop - Given a node in the flat blob, return the property ptr
115 * This function can be used within scan_flattened_dt callback to get
116 * access to properties
118 void *__init of_get_flat_dt_prop(unsigned long node, const char *name,
121 unsigned long p = node;
124 u32 tag = *((u32 *)p);
129 if (tag == OF_DT_NOP)
131 if (tag != OF_DT_PROP)
135 noff = *((u32 *)(p + 4));
137 if (initial_boot_params->version < 0x10)
138 p = _ALIGN(p, sz >= 8 ? 8 : 4);
140 nstr = find_flat_dt_string(noff);
142 pr_warning("Can't find property index name !\n");
145 if (strcmp(name, nstr) == 0) {
156 * of_flat_dt_is_compatible - Return true if given node has compat in compatible list
157 * @node: node to test
158 * @compat: compatible string to compare with compatible list.
160 int __init of_flat_dt_is_compatible(unsigned long node, const char *compat)
163 unsigned long cplen, l;
165 cp = of_get_flat_dt_prop(node, "compatible", &cplen);
169 if (strncasecmp(cp, compat, strlen(compat)) == 0)
179 static void *__init unflatten_dt_alloc(unsigned long *mem, unsigned long size,
184 *mem = _ALIGN(*mem, align);
192 * unflatten_dt_node - Alloc and populate a device_node from the flat tree
193 * @p: pointer to node in flat tree
194 * @dad: Parent struct device_node
195 * @allnextpp: pointer to ->allnext from last allocated device_node
196 * @fpsize: Size of the node path up at the current depth.
198 unsigned long __init unflatten_dt_node(unsigned long mem,
200 struct device_node *dad,
201 struct device_node ***allnextpp,
202 unsigned long fpsize)
204 struct device_node *np;
205 struct property *pp, **prev_pp = NULL;
208 unsigned int l, allocl;
212 tag = *((u32 *)(*p));
213 if (tag != OF_DT_BEGIN_NODE) {
214 pr_err("Weird tag at start of node: %x\n", tag);
219 l = allocl = strlen(pathp) + 1;
220 *p = _ALIGN(*p + l, 4);
222 /* version 0x10 has a more compact unit name here instead of the full
223 * path. we accumulate the full path size using "fpsize", we'll rebuild
224 * it later. We detect this because the first character of the name is
227 if ((*pathp) != '/') {
230 /* root node: special case. fpsize accounts for path
231 * plus terminating zero. root node only has '/', so
232 * fpsize should be 2, but we want to avoid the first
233 * level nodes to have two '/' so we use fpsize 1 here
238 /* account for '/' and path size minus terminal 0
246 np = unflatten_dt_alloc(&mem, sizeof(struct device_node) + allocl,
247 __alignof__(struct device_node));
249 memset(np, 0, sizeof(*np));
250 np->full_name = ((char *)np) + sizeof(struct device_node);
252 char *fn = np->full_name;
253 /* rebuild full path for new format */
254 if (dad && dad->parent) {
255 strcpy(fn, dad->full_name);
257 if ((strlen(fn) + l + 1) != allocl) {
258 pr_debug("%s: p: %d, l: %d, a: %d\n",
259 pathp, (int)strlen(fn),
266 memcpy(fn, pathp, l);
268 memcpy(np->full_name, pathp, l);
269 prev_pp = &np->properties;
271 *allnextpp = &np->allnext;
274 /* we temporarily use the next field as `last_child'*/
275 if (dad->next == NULL)
278 dad->next->sibling = np;
281 kref_init(&np->kref);
287 tag = *((u32 *)(*p));
288 if (tag == OF_DT_NOP) {
292 if (tag != OF_DT_PROP)
296 noff = *((u32 *)((*p) + 4));
298 if (initial_boot_params->version < 0x10)
299 *p = _ALIGN(*p, sz >= 8 ? 8 : 4);
301 pname = find_flat_dt_string(noff);
303 pr_info("Can't find property name in list !\n");
306 if (strcmp(pname, "name") == 0)
308 l = strlen(pname) + 1;
309 pp = unflatten_dt_alloc(&mem, sizeof(struct property),
310 __alignof__(struct property));
312 if (strcmp(pname, "linux,phandle") == 0) {
313 np->node = *((u32 *)*p);
314 if (np->linux_phandle == 0)
315 np->linux_phandle = np->node;
317 if (strcmp(pname, "ibm,phandle") == 0)
318 np->linux_phandle = *((u32 *)*p);
321 pp->value = (void *)*p;
325 *p = _ALIGN((*p) + sz, 4);
327 /* with version 0x10 we may not have the name property, recreate
328 * it here from the unit name if absent
331 char *p1 = pathp, *ps = pathp, *pa = NULL;
344 pp = unflatten_dt_alloc(&mem, sizeof(struct property) + sz,
345 __alignof__(struct property));
352 memcpy(pp->value, ps, sz - 1);
353 ((char *)pp->value)[sz - 1] = 0;
354 pr_debug("fixed up name for %s -> %s\n", pathp,
360 np->name = of_get_property(np, "name", NULL);
361 np->type = of_get_property(np, "device_type", NULL);
368 while (tag == OF_DT_BEGIN_NODE) {
369 mem = unflatten_dt_node(mem, p, np, allnextpp, fpsize);
370 tag = *((u32 *)(*p));
372 if (tag != OF_DT_END_NODE) {
373 pr_err("Weird tag at end of node: %x\n", tag);
380 #ifdef CONFIG_BLK_DEV_INITRD
382 * early_init_dt_check_for_initrd - Decode initrd location from flat tree
383 * @node: reference to node containing initrd location ('chosen')
385 void __init early_init_dt_check_for_initrd(unsigned long node)
390 pr_debug("Looking for initrd properties... ");
392 prop = of_get_flat_dt_prop(node, "linux,initrd-start", &len);
394 initrd_start = (unsigned long)
395 __va(of_read_ulong(prop, len/4));
397 prop = of_get_flat_dt_prop(node, "linux,initrd-end", &len);
399 initrd_end = (unsigned long)
400 __va(of_read_ulong(prop, len/4));
401 initrd_below_start_ok = 1;
407 pr_debug("initrd_start=0x%lx initrd_end=0x%lx\n",
408 initrd_start, initrd_end);
411 inline void early_init_dt_check_for_initrd(unsigned long node)
414 #endif /* CONFIG_BLK_DEV_INITRD */
417 * early_init_dt_scan_root - fetch the top level address and size cells
419 int __init early_init_dt_scan_root(unsigned long node, const char *uname,
420 int depth, void *data)
427 prop = of_get_flat_dt_prop(node, "#size-cells", NULL);
428 dt_root_size_cells = (prop == NULL) ? 1 : *prop;
429 pr_debug("dt_root_size_cells = %x\n", dt_root_size_cells);
431 prop = of_get_flat_dt_prop(node, "#address-cells", NULL);
432 dt_root_addr_cells = (prop == NULL) ? 2 : *prop;
433 pr_debug("dt_root_addr_cells = %x\n", dt_root_addr_cells);
439 u64 __init dt_mem_next_cell(int s, u32 **cellp)
444 return of_read_number(p, s);
447 int __init early_init_dt_scan_chosen(unsigned long node, const char *uname,
448 int depth, void *data)
453 pr_debug("search \"chosen\", depth: %d, uname: %s\n", depth, uname);
456 (strcmp(uname, "chosen") != 0 && strcmp(uname, "chosen@0") != 0))
459 early_init_dt_check_for_initrd(node);
461 /* Retreive command line */
462 p = of_get_flat_dt_prop(node, "bootargs", &l);
463 if (p != NULL && l > 0)
464 strlcpy(cmd_line, p, min((int)l, COMMAND_LINE_SIZE));
466 #ifdef CONFIG_CMDLINE
467 #ifndef CONFIG_CMDLINE_FORCE
468 if (p == NULL || l == 0 || (l == 1 && (*p) == 0))
470 strlcpy(cmd_line, CONFIG_CMDLINE, COMMAND_LINE_SIZE);
471 #endif /* CONFIG_CMDLINE */
473 early_init_dt_scan_chosen_arch(node);
475 pr_debug("Command line is: %s\n", cmd_line);
482 * unflatten_device_tree - create tree of device_nodes from flat blob
484 * unflattens the device-tree passed by the firmware, creating the
485 * tree of struct device_node. It also fills the "name" and "type"
486 * pointers of the nodes so the normal device-tree walking functions
489 void __init unflatten_device_tree(void)
491 unsigned long start, mem, size;
492 struct device_node **allnextp = &allnodes;
494 pr_debug(" -> unflatten_device_tree()\n");
496 /* First pass, scan for size */
497 start = ((unsigned long)initial_boot_params) +
498 initial_boot_params->off_dt_struct;
499 size = unflatten_dt_node(0, &start, NULL, NULL, 0);
500 size = (size | 3) + 1;
502 pr_debug(" size is %lx, allocating...\n", size);
504 /* Allocate memory for the expanded device tree */
505 mem = lmb_alloc(size + 4, __alignof__(struct device_node));
506 mem = (unsigned long) __va(mem);
508 ((u32 *)mem)[size / 4] = 0xdeadbeef;
510 pr_debug(" unflattening %lx...\n", mem);
512 /* Second pass, do actual unflattening */
513 start = ((unsigned long)initial_boot_params) +
514 initial_boot_params->off_dt_struct;
515 unflatten_dt_node(mem, &start, NULL, &allnextp, 0);
516 if (*((u32 *)start) != OF_DT_END)
517 pr_warning("Weird tag at end of tree: %08x\n", *((u32 *)start));
518 if (((u32 *)mem)[size / 4] != 0xdeadbeef)
519 pr_warning("End of tree marker overwritten: %08x\n",
520 ((u32 *)mem)[size / 4]);
523 /* Get pointer to OF "/chosen" node for use everywhere */
524 of_chosen = of_find_node_by_path("/chosen");
525 if (of_chosen == NULL)
526 of_chosen = of_find_node_by_path("/chosen@0");
528 pr_debug(" <- unflatten_device_tree()\n");