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.
13 #include <linux/of_fdt.h>
15 struct boot_param_header *initial_boot_params;
17 char *find_flat_dt_string(u32 offset)
19 return ((char *)initial_boot_params) +
20 initial_boot_params->off_dt_strings + offset;
24 * of_scan_flat_dt - scan flattened tree blob and call callback on each.
25 * @it: callback function
26 * @data: context data pointer
28 * This function is used to scan the flattened device-tree, it is
29 * used to extract the memory information at boot before we can
32 int __init of_scan_flat_dt(int (*it)(unsigned long node,
33 const char *uname, int depth,
37 unsigned long p = ((unsigned long)initial_boot_params) +
38 initial_boot_params->off_dt_struct;
43 u32 tag = *((u32 *)p);
47 if (tag == OF_DT_END_NODE) {
55 if (tag == OF_DT_PROP) {
58 if (initial_boot_params->version < 0x10)
59 p = _ALIGN(p, sz >= 8 ? 8 : 4);
64 if (tag != OF_DT_BEGIN_NODE) {
65 pr_err("Invalid tag %x in flat device tree!\n", tag);
70 p = _ALIGN(p + strlen(pathp) + 1, 4);
71 if ((*pathp) == '/') {
73 for (lp = NULL, np = pathp; *np; np++)
79 rc = it(p, pathp, depth, data);
88 * of_get_flat_dt_root - find the root node in the flat blob
90 unsigned long __init of_get_flat_dt_root(void)
92 unsigned long p = ((unsigned long)initial_boot_params) +
93 initial_boot_params->off_dt_struct;
95 while (*((u32 *)p) == OF_DT_NOP)
97 BUG_ON(*((u32 *)p) != OF_DT_BEGIN_NODE);
99 return _ALIGN(p + strlen((char *)p) + 1, 4);
103 * of_get_flat_dt_prop - Given a node in the flat blob, return the property ptr
105 * This function can be used within scan_flattened_dt callback to get
106 * access to properties
108 void *__init of_get_flat_dt_prop(unsigned long node, const char *name,
111 unsigned long p = node;
114 u32 tag = *((u32 *)p);
119 if (tag == OF_DT_NOP)
121 if (tag != OF_DT_PROP)
125 noff = *((u32 *)(p + 4));
127 if (initial_boot_params->version < 0x10)
128 p = _ALIGN(p, sz >= 8 ? 8 : 4);
130 nstr = find_flat_dt_string(noff);
132 pr_warning("Can't find property index name !\n");
135 if (strcmp(name, nstr) == 0) {
146 * of_flat_dt_is_compatible - Return true if given node has compat in compatible list
147 * @node: node to test
148 * @compat: compatible string to compare with compatible list.
150 int __init of_flat_dt_is_compatible(unsigned long node, const char *compat)
153 unsigned long cplen, l;
155 cp = of_get_flat_dt_prop(node, "compatible", &cplen);
159 if (strncasecmp(cp, compat, strlen(compat)) == 0)