blob: 13bb47002e463e9e24af33b377b66380952088cf [file] [log] [blame]
Simon Glassb5220bc2011-10-24 19:15:32 +00001/*
2 * Copyright (c) 2011 The Chromium OS Authors.
3 * See file CREDITS for list of people who contributed to this
4 * project.
5 *
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.
10 *
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.
15 *
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,
19 * MA 02111-1307 USA
20 */
21
22#include <common.h>
23#include <serial.h>
24#include <libfdt.h>
25#include <fdtdec.h>
26
27DECLARE_GLOBAL_DATA_PTR;
28
29/*
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.
33 */
34#define COMPAT(id, name) name
35static const char * const compat_names[COMPAT_COUNT] = {
Simon Glassf88fe2d2012-02-27 10:52:34 +000036 COMPAT(UNKNOWN, "<none>"),
Simon Glassb5220bc2011-10-24 19:15:32 +000037};
38
Simon Glassa53f4a22012-01-17 08:20:50 +000039const char *fdtdec_get_compatible(enum fdt_compat_id id)
40{
41 /* We allow reading of the 'unknown' ID for testing purposes */
42 assert(id >= 0 && id < COMPAT_COUNT);
43 return compat_names[id];
44}
45
Simon Glassb5220bc2011-10-24 19:15:32 +000046/**
47 * Look in the FDT for an alias with the given name and return its node.
48 *
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
52 */
53static int find_alias_node(const void *blob, const char *name)
54{
55 const char *path;
56 int alias_node;
57
58 debug("find_alias_node: %s\n", name);
59 alias_node = fdt_path_offset(blob, "/aliases");
60 if (alias_node < 0)
61 return alias_node;
62 path = fdt_getprop(blob, alias_node, name, NULL);
63 if (!path)
64 return -FDT_ERR_NOTFOUND;
65 return fdt_path_offset(blob, path);
66}
67
68fdt_addr_t fdtdec_get_addr(const void *blob, int node,
69 const char *prop_name)
70{
71 const fdt_addr_t *cell;
72 int len;
73
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;
80}
81
82s32 fdtdec_get_int(const void *blob, int node, const char *prop_name,
83 s32 default_val)
84{
85 const s32 *cell;
86 int len;
87
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]);
92 return default_val;
93}
94
Simon Glassf88fe2d2012-02-27 10:52:34 +000095int fdtdec_get_is_enabled(const void *blob, int node)
Simon Glassb5220bc2011-10-24 19:15:32 +000096{
97 const char *cell;
98
Simon Glassf88fe2d2012-02-27 10:52:34 +000099 /*
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
102 * for discussion:
103 *
104 * http://www.mail-archive.com/u-boot@lists.denx.de/msg71598.html
105 */
Simon Glassb5220bc2011-10-24 19:15:32 +0000106 cell = fdt_getprop(blob, node, "status", NULL);
107 if (cell)
Simon Glassf88fe2d2012-02-27 10:52:34 +0000108 return 0 == strcmp(cell, "okay");
109 return 1;
Simon Glassb5220bc2011-10-24 19:15:32 +0000110}
111
112enum fdt_compat_id fd_dec_lookup(const void *blob, int node)
113{
114 enum fdt_compat_id id;
115
116 /* Search our drivers */
117 for (id = COMPAT_UNKNOWN; id < COMPAT_COUNT; id++)
118 if (0 == fdt_node_check_compatible(blob, node,
119 compat_names[id]))
120 return id;
121 return COMPAT_UNKNOWN;
122}
123
124int fdtdec_next_compatible(const void *blob, int node,
125 enum fdt_compat_id id)
126{
127 return fdt_node_offset_by_compatible(blob, node, compat_names[id]);
128}
129
130int fdtdec_next_alias(const void *blob, const char *name,
131 enum fdt_compat_id id, int *upto)
132{
133#define MAX_STR_LEN 20
134 char str[MAX_STR_LEN + 20];
135 int node, err;
136
137 /* snprintf() is not available */
138 assert(strlen(name) < MAX_STR_LEN);
139 sprintf(str, "%.*s%d", MAX_STR_LEN, name, *upto);
Simon Glassb5220bc2011-10-24 19:15:32 +0000140 node = find_alias_node(blob, str);
141 if (node < 0)
142 return node;
143 err = fdt_node_check_compatible(blob, node, compat_names[id]);
144 if (err < 0)
145 return err;
Simon Glassf88fe2d2012-02-27 10:52:34 +0000146 if (err)
147 return -FDT_ERR_NOTFOUND;
148 (*upto)++;
149 return node;
Simon Glassb5220bc2011-10-24 19:15:32 +0000150}
151
Simon Glassa53f4a22012-01-17 08:20:50 +0000152/* TODO: Can we tighten this code up a little? */
153int fdtdec_find_aliases_for_id(const void *blob, const char *name,
154 enum fdt_compat_id id, int *node_list, int maxcount)
155{
156 int name_len = strlen(name);
157 int nodes[maxcount];
158 int num_found = 0;
159 int offset, node;
160 int alias_node;
161 int count;
162 int i, j;
163
164 /* find the alias node if present */
165 alias_node = fdt_path_offset(blob, "/aliases");
166
167 /*
168 * start with nothing, and we can assume that the root node can't
169 * match
170 */
171 memset(nodes, '\0', sizeof(nodes));
172
173 /* First find all the compatible nodes */
174 for (node = count = 0; node >= 0 && count < maxcount;) {
175 node = fdtdec_next_compatible(blob, node, id);
176 if (node >= 0)
177 nodes[count++] = node;
178 }
179 if (node >= 0)
180 debug("%s: warning: maxcount exceeded with alias '%s'\n",
181 __func__, name);
182
183 /* Now find all the aliases */
184 memset(node_list, '\0', sizeof(*node_list) * maxcount);
185
186 for (offset = fdt_first_property_offset(blob, alias_node);
187 offset > 0;
188 offset = fdt_next_property_offset(blob, offset)) {
189 const struct fdt_property *prop;
190 const char *path;
191 int number;
192 int found;
193
194 node = 0;
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);
199 if (node <= 0)
200 continue;
201
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",
206 __func__, path);
207 continue;
208 }
209
210 /* Make sure the node we found is actually in our list! */
211 found = -1;
212 for (j = 0; j < count; j++)
213 if (nodes[j] == node) {
214 found = j;
215 break;
216 }
217
218 if (found == -1) {
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),
223 compat_names[id]);
224 continue;
225 }
226
227 /*
228 * Add this node to our list in the right place, and mark
229 * it as done.
230 */
231 if (fdtdec_get_is_enabled(blob, node)) {
232 node_list[number] = node;
233 if (number >= num_found)
234 num_found = number + 1;
235 }
236 nodes[j] = 0;
237 }
238
239 /* Add any nodes not mentioned by an alias */
240 for (i = j = 0; i < maxcount; i++) {
241 if (!node_list[i]) {
242 for (; j < maxcount; j++)
243 if (nodes[j] &&
244 fdtdec_get_is_enabled(blob, nodes[j]))
245 break;
246
247 /* Have we run out of nodes to add? */
248 if (j == maxcount)
249 break;
250
251 assert(!node_list[i]);
252 node_list[i] = nodes[j++];
253 if (i >= num_found)
254 num_found = i + 1;
255 }
256 }
257
258 return num_found;
259}
260
Simon Glassb5220bc2011-10-24 19:15:32 +0000261/*
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.
265 */
266int fdtdec_check_fdt(void)
267{
268 /* We must have an fdt */
Simon Glassf88fe2d2012-02-27 10:52:34 +0000269 if (((uintptr_t)gd->fdt_blob & 3) || fdt_check_header(gd->fdt_blob))
Simon Glassb5220bc2011-10-24 19:15:32 +0000270 panic("No valid fdt found - please append one to U-Boot\n"
271 "binary or define CONFIG_OF_EMBED\n");
272 return 0;
273}
Simon Glassd17da652012-02-27 10:52:35 +0000274
275int fdtdec_lookup_phandle(const void *blob, int node, const char *prop_name)
276{
277 const u32 *phandle;
278 int lookup;
279
280 phandle = fdt_getprop(blob, node, prop_name, NULL);
281 if (!phandle)
282 return -FDT_ERR_NOTFOUND;
283
284 lookup = fdt_node_offset_by_phandle(blob, fdt32_to_cpu(*phandle));
285 return lookup;
286}
287
288/**
289 * Look up a property in a node and check that it has a minimum length.
290 *
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
298 */
299static const void *get_prop_check_min_len(const void *blob, int node,
300 const char *prop_name, int min_len, int *err)
301{
302 const void *cell;
303 int len;
304
305 debug("%s: %s\n", __func__, prop_name);
306 cell = fdt_getprop(blob, node, prop_name, &len);
307 if (!cell)
308 *err = -FDT_ERR_NOTFOUND;
309 else if (len < min_len)
310 *err = -FDT_ERR_BADLAYOUT;
311 else
312 *err = 0;
313 return cell;
314}
315
316int fdtdec_get_int_array(const void *blob, int node, const char *prop_name,
317 u32 *array, int count)
318{
319 const u32 *cell;
320 int i, err = 0;
321
322 debug("%s: %s\n", __func__, prop_name);
323 cell = get_prop_check_min_len(blob, node, prop_name,
324 sizeof(u32) * count, &err);
325 if (!err) {
326 for (i = 0; i < count; i++)
327 array[i] = fdt32_to_cpu(cell[i]);
328 }
329 return err;
330}
331
332int fdtdec_get_bool(const void *blob, int node, const char *prop_name)
333{
334 const s32 *cell;
335 int len;
336
337 debug("%s: %s\n", __func__, prop_name);
338 cell = fdt_getprop(blob, node, prop_name, &len);
339 return cell != NULL;
340}