2 * Copyright (c) 2011 The Chromium OS Authors.
3 * See file CREDITS for list of people who contributed to this
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License as
8 * published by the Free Software Foundation; either version 2 of
9 * the License, or (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
27 DECLARE_GLOBAL_DATA_PTR;
30 * Here are the type we know about. One day we might allow drivers to
31 * register. For now we just put them here. The COMPAT macro allows us to
32 * turn this into a sparse list later, and keeps the ID with the name.
34 #define COMPAT(id, name) name
35 static const char * const compat_names[COMPAT_COUNT] = {
36 COMPAT(UNKNOWN, "<none>"),
39 const char *fdtdec_get_compatible(enum fdt_compat_id id)
41 /* We allow reading of the 'unknown' ID for testing purposes */
42 assert(id >= 0 && id < COMPAT_COUNT);
43 return compat_names[id];
47 * Look in the FDT for an alias with the given name and return its node.
49 * @param blob FDT blob
50 * @param name alias name to look up
51 * @return node offset if found, or an error code < 0 otherwise
53 static int find_alias_node(const void *blob, const char *name)
58 debug("find_alias_node: %s\n", name);
59 alias_node = fdt_path_offset(blob, "/aliases");
62 path = fdt_getprop(blob, alias_node, name, NULL);
64 return -FDT_ERR_NOTFOUND;
65 return fdt_path_offset(blob, path);
68 fdt_addr_t fdtdec_get_addr(const void *blob, int node,
69 const char *prop_name)
71 const fdt_addr_t *cell;
74 debug("get_addr: %s\n", prop_name);
75 cell = fdt_getprop(blob, node, prop_name, &len);
76 if (cell && (len == sizeof(fdt_addr_t) ||
77 len == sizeof(fdt_addr_t) * 2))
78 return fdt_addr_to_cpu(*cell);
79 return FDT_ADDR_T_NONE;
82 s32 fdtdec_get_int(const void *blob, int node, const char *prop_name,
88 debug("get_size: %s\n", prop_name);
89 cell = fdt_getprop(blob, node, prop_name, &len);
90 if (cell && len >= sizeof(s32))
91 return fdt32_to_cpu(cell[0]);
95 int fdtdec_get_is_enabled(const void *blob, int node)
100 * It should say "okay", so only allow that. Some fdts use "ok" but
101 * this is a bug. Please fix your device tree source file. See here
104 * http://www.mail-archive.com/u-boot@lists.denx.de/msg71598.html
106 cell = fdt_getprop(blob, node, "status", NULL);
108 return 0 == strcmp(cell, "okay");
112 enum fdt_compat_id fd_dec_lookup(const void *blob, int node)
114 enum fdt_compat_id id;
116 /* Search our drivers */
117 for (id = COMPAT_UNKNOWN; id < COMPAT_COUNT; id++)
118 if (0 == fdt_node_check_compatible(blob, node,
121 return COMPAT_UNKNOWN;
124 int fdtdec_next_compatible(const void *blob, int node,
125 enum fdt_compat_id id)
127 return fdt_node_offset_by_compatible(blob, node, compat_names[id]);
130 int fdtdec_next_alias(const void *blob, const char *name,
131 enum fdt_compat_id id, int *upto)
133 #define MAX_STR_LEN 20
134 char str[MAX_STR_LEN + 20];
137 /* snprintf() is not available */
138 assert(strlen(name) < MAX_STR_LEN);
139 sprintf(str, "%.*s%d", MAX_STR_LEN, name, *upto);
140 node = find_alias_node(blob, str);
143 err = fdt_node_check_compatible(blob, node, compat_names[id]);
147 return -FDT_ERR_NOTFOUND;
152 /* TODO: Can we tighten this code up a little? */
153 int fdtdec_find_aliases_for_id(const void *blob, const char *name,
154 enum fdt_compat_id id, int *node_list, int maxcount)
156 int name_len = strlen(name);
164 /* find the alias node if present */
165 alias_node = fdt_path_offset(blob, "/aliases");
168 * start with nothing, and we can assume that the root node can't
171 memset(nodes, '\0', sizeof(nodes));
173 /* First find all the compatible nodes */
174 for (node = count = 0; node >= 0 && count < maxcount;) {
175 node = fdtdec_next_compatible(blob, node, id);
177 nodes[count++] = node;
180 debug("%s: warning: maxcount exceeded with alias '%s'\n",
183 /* Now find all the aliases */
184 memset(node_list, '\0', sizeof(*node_list) * maxcount);
186 for (offset = fdt_first_property_offset(blob, alias_node);
188 offset = fdt_next_property_offset(blob, offset)) {
189 const struct fdt_property *prop;
195 prop = fdt_get_property_by_offset(blob, offset, NULL);
196 path = fdt_string(blob, fdt32_to_cpu(prop->nameoff));
197 if (prop->len && 0 == strncmp(path, name, name_len))
198 node = fdt_path_offset(blob, prop->data);
202 /* Get the alias number */
203 number = simple_strtoul(path + name_len, NULL, 10);
204 if (number < 0 || number >= maxcount) {
205 debug("%s: warning: alias '%s' is out of range\n",
210 /* Make sure the node we found is actually in our list! */
212 for (j = 0; j < count; j++)
213 if (nodes[j] == node) {
219 debug("%s: warning: alias '%s' points to a node "
220 "'%s' that is missing or is not compatible "
221 " with '%s'\n", __func__, path,
222 fdt_get_name(blob, node, NULL),
228 * Add this node to our list in the right place, and mark
231 if (fdtdec_get_is_enabled(blob, node)) {
232 node_list[number] = node;
233 if (number >= num_found)
234 num_found = number + 1;
239 /* Add any nodes not mentioned by an alias */
240 for (i = j = 0; i < maxcount; i++) {
242 for (; j < maxcount; j++)
244 fdtdec_get_is_enabled(blob, nodes[j]))
247 /* Have we run out of nodes to add? */
251 assert(!node_list[i]);
252 node_list[i] = nodes[j++];
262 * This function is a little odd in that it accesses global data. At some
263 * point if the architecture board.c files merge this will make more sense.
264 * Even now, it is common code.
266 int fdtdec_check_fdt(void)
268 /* We must have an fdt */
269 if (((uintptr_t)gd->fdt_blob & 3) || fdt_check_header(gd->fdt_blob))
270 panic("No valid fdt found - please append one to U-Boot\n"
271 "binary or define CONFIG_OF_EMBED\n");
275 int fdtdec_lookup_phandle(const void *blob, int node, const char *prop_name)
280 phandle = fdt_getprop(blob, node, prop_name, NULL);
282 return -FDT_ERR_NOTFOUND;
284 lookup = fdt_node_offset_by_phandle(blob, fdt32_to_cpu(*phandle));
289 * Look up a property in a node and check that it has a minimum length.
291 * @param blob FDT blob
292 * @param node node to examine
293 * @param prop_name name of property to find
294 * @param min_len minimum property length in bytes
295 * @param err 0 if ok, or -FDT_ERR_NOTFOUND if the property is not
296 found, or -FDT_ERR_BADLAYOUT if not enough data
297 * @return pointer to cell, which is only valid if err == 0
299 static const void *get_prop_check_min_len(const void *blob, int node,
300 const char *prop_name, int min_len, int *err)
305 debug("%s: %s\n", __func__, prop_name);
306 cell = fdt_getprop(blob, node, prop_name, &len);
308 *err = -FDT_ERR_NOTFOUND;
309 else if (len < min_len)
310 *err = -FDT_ERR_BADLAYOUT;
316 int fdtdec_get_int_array(const void *blob, int node, const char *prop_name,
317 u32 *array, int count)
322 debug("%s: %s\n", __func__, prop_name);
323 cell = get_prop_check_min_len(blob, node, prop_name,
324 sizeof(u32) * count, &err);
326 for (i = 0; i < count; i++)
327 array[i] = fdt32_to_cpu(cell[i]);
332 int fdtdec_get_bool(const void *blob, int node, const char *prop_name)
337 debug("%s: %s\n", __func__, prop_name);
338 cell = fdt_getprop(blob, node, prop_name, &len);