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>
18 int __initdata dt_root_addr_cells;
19 int __initdata dt_root_size_cells;
21 struct boot_param_header *initial_boot_params;
23 char *find_flat_dt_string(u32 offset)
25 return ((char *)initial_boot_params) +
26 initial_boot_params->off_dt_strings + offset;
30 * of_scan_flat_dt - scan flattened tree blob and call callback on each.
31 * @it: callback function
32 * @data: context data pointer
34 * This function is used to scan the flattened device-tree, it is
35 * used to extract the memory information at boot before we can
38 int __init of_scan_flat_dt(int (*it)(unsigned long node,
39 const char *uname, int depth,
43 unsigned long p = ((unsigned long)initial_boot_params) +
44 initial_boot_params->off_dt_struct;
49 u32 tag = *((u32 *)p);
53 if (tag == OF_DT_END_NODE) {
61 if (tag == OF_DT_PROP) {
64 if (initial_boot_params->version < 0x10)
65 p = _ALIGN(p, sz >= 8 ? 8 : 4);
70 if (tag != OF_DT_BEGIN_NODE) {
71 pr_err("Invalid tag %x in flat device tree!\n", tag);
76 p = _ALIGN(p + strlen(pathp) + 1, 4);
77 if ((*pathp) == '/') {
79 for (lp = NULL, np = pathp; *np; np++)
85 rc = it(p, pathp, depth, data);
94 * of_get_flat_dt_root - find the root node in the flat blob
96 unsigned long __init of_get_flat_dt_root(void)
98 unsigned long p = ((unsigned long)initial_boot_params) +
99 initial_boot_params->off_dt_struct;
101 while (*((u32 *)p) == OF_DT_NOP)
103 BUG_ON(*((u32 *)p) != OF_DT_BEGIN_NODE);
105 return _ALIGN(p + strlen((char *)p) + 1, 4);
109 * of_get_flat_dt_prop - Given a node in the flat blob, return the property ptr
111 * This function can be used within scan_flattened_dt callback to get
112 * access to properties
114 void *__init of_get_flat_dt_prop(unsigned long node, const char *name,
117 unsigned long p = node;
120 u32 tag = *((u32 *)p);
125 if (tag == OF_DT_NOP)
127 if (tag != OF_DT_PROP)
131 noff = *((u32 *)(p + 4));
133 if (initial_boot_params->version < 0x10)
134 p = _ALIGN(p, sz >= 8 ? 8 : 4);
136 nstr = find_flat_dt_string(noff);
138 pr_warning("Can't find property index name !\n");
141 if (strcmp(name, nstr) == 0) {
152 * of_flat_dt_is_compatible - Return true if given node has compat in compatible list
153 * @node: node to test
154 * @compat: compatible string to compare with compatible list.
156 int __init of_flat_dt_is_compatible(unsigned long node, const char *compat)
159 unsigned long cplen, l;
161 cp = of_get_flat_dt_prop(node, "compatible", &cplen);
165 if (strncasecmp(cp, compat, strlen(compat)) == 0)
175 static void *__init unflatten_dt_alloc(unsigned long *mem, unsigned long size,
180 *mem = _ALIGN(*mem, align);
188 * unflatten_dt_node - Alloc and populate a device_node from the flat tree
189 * @p: pointer to node in flat tree
190 * @dad: Parent struct device_node
191 * @allnextpp: pointer to ->allnext from last allocated device_node
192 * @fpsize: Size of the node path up at the current depth.
194 unsigned long __init unflatten_dt_node(unsigned long mem,
196 struct device_node *dad,
197 struct device_node ***allnextpp,
198 unsigned long fpsize)
200 struct device_node *np;
201 struct property *pp, **prev_pp = NULL;
204 unsigned int l, allocl;
208 tag = *((u32 *)(*p));
209 if (tag != OF_DT_BEGIN_NODE) {
210 pr_err("Weird tag at start of node: %x\n", tag);
215 l = allocl = strlen(pathp) + 1;
216 *p = _ALIGN(*p + l, 4);
218 /* version 0x10 has a more compact unit name here instead of the full
219 * path. we accumulate the full path size using "fpsize", we'll rebuild
220 * it later. We detect this because the first character of the name is
223 if ((*pathp) != '/') {
226 /* root node: special case. fpsize accounts for path
227 * plus terminating zero. root node only has '/', so
228 * fpsize should be 2, but we want to avoid the first
229 * level nodes to have two '/' so we use fpsize 1 here
234 /* account for '/' and path size minus terminal 0
242 np = unflatten_dt_alloc(&mem, sizeof(struct device_node) + allocl,
243 __alignof__(struct device_node));
245 memset(np, 0, sizeof(*np));
246 np->full_name = ((char *)np) + sizeof(struct device_node);
248 char *fn = np->full_name;
249 /* rebuild full path for new format */
250 if (dad && dad->parent) {
251 strcpy(fn, dad->full_name);
253 if ((strlen(fn) + l + 1) != allocl) {
254 pr_debug("%s: p: %d, l: %d, a: %d\n",
255 pathp, (int)strlen(fn),
262 memcpy(fn, pathp, l);
264 memcpy(np->full_name, pathp, l);
265 prev_pp = &np->properties;
267 *allnextpp = &np->allnext;
270 /* we temporarily use the next field as `last_child'*/
271 if (dad->next == NULL)
274 dad->next->sibling = np;
277 kref_init(&np->kref);
283 tag = *((u32 *)(*p));
284 if (tag == OF_DT_NOP) {
288 if (tag != OF_DT_PROP)
292 noff = *((u32 *)((*p) + 4));
294 if (initial_boot_params->version < 0x10)
295 *p = _ALIGN(*p, sz >= 8 ? 8 : 4);
297 pname = find_flat_dt_string(noff);
299 pr_info("Can't find property name in list !\n");
302 if (strcmp(pname, "name") == 0)
304 l = strlen(pname) + 1;
305 pp = unflatten_dt_alloc(&mem, sizeof(struct property),
306 __alignof__(struct property));
308 if (strcmp(pname, "linux,phandle") == 0) {
309 np->node = *((u32 *)*p);
310 if (np->linux_phandle == 0)
311 np->linux_phandle = np->node;
313 if (strcmp(pname, "ibm,phandle") == 0)
314 np->linux_phandle = *((u32 *)*p);
317 pp->value = (void *)*p;
321 *p = _ALIGN((*p) + sz, 4);
323 /* with version 0x10 we may not have the name property, recreate
324 * it here from the unit name if absent
327 char *p1 = pathp, *ps = pathp, *pa = NULL;
340 pp = unflatten_dt_alloc(&mem, sizeof(struct property) + sz,
341 __alignof__(struct property));
348 memcpy(pp->value, ps, sz - 1);
349 ((char *)pp->value)[sz - 1] = 0;
350 pr_debug("fixed up name for %s -> %s\n", pathp,
356 np->name = of_get_property(np, "name", NULL);
357 np->type = of_get_property(np, "device_type", NULL);
364 while (tag == OF_DT_BEGIN_NODE) {
365 mem = unflatten_dt_node(mem, p, np, allnextpp, fpsize);
366 tag = *((u32 *)(*p));
368 if (tag != OF_DT_END_NODE) {
369 pr_err("Weird tag at end of node: %x\n", tag);
376 #ifdef CONFIG_BLK_DEV_INITRD
378 * early_init_dt_check_for_initrd - Decode initrd location from flat tree
379 * @node: reference to node containing initrd location ('chosen')
381 void __init early_init_dt_check_for_initrd(unsigned long node)
386 pr_debug("Looking for initrd properties... ");
388 prop = of_get_flat_dt_prop(node, "linux,initrd-start", &len);
390 initrd_start = (unsigned long)
391 __va(of_read_ulong(prop, len/4));
393 prop = of_get_flat_dt_prop(node, "linux,initrd-end", &len);
395 initrd_end = (unsigned long)
396 __va(of_read_ulong(prop, len/4));
397 initrd_below_start_ok = 1;
403 pr_debug("initrd_start=0x%lx initrd_end=0x%lx\n",
404 initrd_start, initrd_end);
407 inline void early_init_dt_check_for_initrd(unsigned long node)
410 #endif /* CONFIG_BLK_DEV_INITRD */
413 * early_init_dt_scan_root - fetch the top level address and size cells
415 int __init early_init_dt_scan_root(unsigned long node, const char *uname,
416 int depth, void *data)
423 prop = of_get_flat_dt_prop(node, "#size-cells", NULL);
424 dt_root_size_cells = (prop == NULL) ? 1 : *prop;
425 pr_debug("dt_root_size_cells = %x\n", dt_root_size_cells);
427 prop = of_get_flat_dt_prop(node, "#address-cells", NULL);
428 dt_root_addr_cells = (prop == NULL) ? 2 : *prop;
429 pr_debug("dt_root_addr_cells = %x\n", dt_root_addr_cells);
435 u64 __init dt_mem_next_cell(int s, u32 **cellp)
440 return of_read_number(p, s);
444 * unflatten_device_tree - create tree of device_nodes from flat blob
446 * unflattens the device-tree passed by the firmware, creating the
447 * tree of struct device_node. It also fills the "name" and "type"
448 * pointers of the nodes so the normal device-tree walking functions
451 void __init unflatten_device_tree(void)
453 unsigned long start, mem, size;
454 struct device_node **allnextp = &allnodes;
456 pr_debug(" -> unflatten_device_tree()\n");
458 /* First pass, scan for size */
459 start = ((unsigned long)initial_boot_params) +
460 initial_boot_params->off_dt_struct;
461 size = unflatten_dt_node(0, &start, NULL, NULL, 0);
462 size = (size | 3) + 1;
464 pr_debug(" size is %lx, allocating...\n", size);
466 /* Allocate memory for the expanded device tree */
467 mem = lmb_alloc(size + 4, __alignof__(struct device_node));
468 mem = (unsigned long) __va(mem);
470 ((u32 *)mem)[size / 4] = 0xdeadbeef;
472 pr_debug(" unflattening %lx...\n", mem);
474 /* Second pass, do actual unflattening */
475 start = ((unsigned long)initial_boot_params) +
476 initial_boot_params->off_dt_struct;
477 unflatten_dt_node(mem, &start, NULL, &allnextp, 0);
478 if (*((u32 *)start) != OF_DT_END)
479 pr_warning("Weird tag at end of tree: %08x\n", *((u32 *)start));
480 if (((u32 *)mem)[size / 4] != 0xdeadbeef)
481 pr_warning("End of tree marker overwritten: %08x\n",
482 ((u32 *)mem)[size / 4]);
485 /* Get pointer to OF "/chosen" node for use everywhere */
486 of_chosen = of_find_node_by_path("/chosen");
487 if (of_chosen == NULL)
488 of_chosen = of_find_node_by_path("/chosen@0");
490 pr_debug(" <- unflatten_device_tree()\n");