blob: af17ac1b7a1cdc24bd40f30dd2e1a2b149a42936 [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
Michal Simeke46431e2012-07-11 02:26:38 +000027#include <asm/gpio.h>
Simon Glassed3ee5c2012-02-27 10:52:36 +000028
Simon Glassb5220bc2011-10-24 19:15:32 +000029DECLARE_GLOBAL_DATA_PTR;
30
31/*
32 * Here are the type we know about. One day we might allow drivers to
33 * register. For now we just put them here. The COMPAT macro allows us to
34 * turn this into a sparse list later, and keeps the ID with the name.
35 */
36#define COMPAT(id, name) name
37static const char * const compat_names[COMPAT_COUNT] = {
Simon Glassf88fe2d2012-02-27 10:52:34 +000038 COMPAT(UNKNOWN, "<none>"),
Simon Glass87f938c2012-02-27 10:52:49 +000039 COMPAT(NVIDIA_TEGRA20_USB, "nvidia,tegra20-ehci"),
Yen Lin96a78ac2012-03-06 19:00:23 +000040 COMPAT(NVIDIA_TEGRA20_I2C, "nvidia,tegra20-i2c"),
41 COMPAT(NVIDIA_TEGRA20_DVC, "nvidia,tegra20-i2c-dvc"),
Jimmy Zhang0e35ad02012-04-02 13:18:52 +000042 COMPAT(NVIDIA_TEGRA20_EMC, "nvidia,tegra20-emc"),
43 COMPAT(NVIDIA_TEGRA20_EMC_TABLE, "nvidia,tegra20-emc-table"),
Rakesh Iyer6642a682012-04-17 09:01:35 +000044 COMPAT(NVIDIA_TEGRA20_KBC, "nvidia,tegra20-kbc"),
Simon Glassb5220bc2011-10-24 19:15:32 +000045};
46
Simon Glassa53f4a22012-01-17 08:20:50 +000047const char *fdtdec_get_compatible(enum fdt_compat_id id)
48{
49 /* We allow reading of the 'unknown' ID for testing purposes */
50 assert(id >= 0 && id < COMPAT_COUNT);
51 return compat_names[id];
52}
53
Simon Glassb5220bc2011-10-24 19:15:32 +000054/**
55 * Look in the FDT for an alias with the given name and return its node.
56 *
57 * @param blob FDT blob
58 * @param name alias name to look up
59 * @return node offset if found, or an error code < 0 otherwise
60 */
61static int find_alias_node(const void *blob, const char *name)
62{
63 const char *path;
64 int alias_node;
65
66 debug("find_alias_node: %s\n", name);
67 alias_node = fdt_path_offset(blob, "/aliases");
68 if (alias_node < 0)
69 return alias_node;
70 path = fdt_getprop(blob, alias_node, name, NULL);
71 if (!path)
72 return -FDT_ERR_NOTFOUND;
73 return fdt_path_offset(blob, path);
74}
75
76fdt_addr_t fdtdec_get_addr(const void *blob, int node,
77 const char *prop_name)
78{
79 const fdt_addr_t *cell;
80 int len;
81
82 debug("get_addr: %s\n", prop_name);
83 cell = fdt_getprop(blob, node, prop_name, &len);
84 if (cell && (len == sizeof(fdt_addr_t) ||
85 len == sizeof(fdt_addr_t) * 2))
86 return fdt_addr_to_cpu(*cell);
87 return FDT_ADDR_T_NONE;
88}
89
90s32 fdtdec_get_int(const void *blob, int node, const char *prop_name,
91 s32 default_val)
92{
93 const s32 *cell;
94 int len;
95
96 debug("get_size: %s\n", prop_name);
97 cell = fdt_getprop(blob, node, prop_name, &len);
98 if (cell && len >= sizeof(s32))
99 return fdt32_to_cpu(cell[0]);
100 return default_val;
101}
102
Simon Glassf88fe2d2012-02-27 10:52:34 +0000103int fdtdec_get_is_enabled(const void *blob, int node)
Simon Glassb5220bc2011-10-24 19:15:32 +0000104{
105 const char *cell;
106
Simon Glassf88fe2d2012-02-27 10:52:34 +0000107 /*
108 * It should say "okay", so only allow that. Some fdts use "ok" but
109 * this is a bug. Please fix your device tree source file. See here
110 * for discussion:
111 *
112 * http://www.mail-archive.com/u-boot@lists.denx.de/msg71598.html
113 */
Simon Glassb5220bc2011-10-24 19:15:32 +0000114 cell = fdt_getprop(blob, node, "status", NULL);
115 if (cell)
Simon Glassf88fe2d2012-02-27 10:52:34 +0000116 return 0 == strcmp(cell, "okay");
117 return 1;
Simon Glassb5220bc2011-10-24 19:15:32 +0000118}
119
120enum fdt_compat_id fd_dec_lookup(const void *blob, int node)
121{
122 enum fdt_compat_id id;
123
124 /* Search our drivers */
125 for (id = COMPAT_UNKNOWN; id < COMPAT_COUNT; id++)
126 if (0 == fdt_node_check_compatible(blob, node,
127 compat_names[id]))
128 return id;
129 return COMPAT_UNKNOWN;
130}
131
132int fdtdec_next_compatible(const void *blob, int node,
133 enum fdt_compat_id id)
134{
135 return fdt_node_offset_by_compatible(blob, node, compat_names[id]);
136}
137
Simon Glass3ddecfc2012-04-02 13:18:42 +0000138int fdtdec_next_compatible_subnode(const void *blob, int node,
139 enum fdt_compat_id id, int *depthp)
140{
141 do {
142 node = fdt_next_node(blob, node, depthp);
143 } while (*depthp > 1);
144
145 /* If this is a direct subnode, and compatible, return it */
146 if (*depthp == 1 && 0 == fdt_node_check_compatible(
147 blob, node, compat_names[id]))
148 return node;
149
150 return -FDT_ERR_NOTFOUND;
151}
152
Simon Glassb5220bc2011-10-24 19:15:32 +0000153int fdtdec_next_alias(const void *blob, const char *name,
154 enum fdt_compat_id id, int *upto)
155{
156#define MAX_STR_LEN 20
157 char str[MAX_STR_LEN + 20];
158 int node, err;
159
160 /* snprintf() is not available */
161 assert(strlen(name) < MAX_STR_LEN);
162 sprintf(str, "%.*s%d", MAX_STR_LEN, name, *upto);
Simon Glassb5220bc2011-10-24 19:15:32 +0000163 node = find_alias_node(blob, str);
164 if (node < 0)
165 return node;
166 err = fdt_node_check_compatible(blob, node, compat_names[id]);
167 if (err < 0)
168 return err;
Simon Glassf88fe2d2012-02-27 10:52:34 +0000169 if (err)
170 return -FDT_ERR_NOTFOUND;
171 (*upto)++;
172 return node;
Simon Glassb5220bc2011-10-24 19:15:32 +0000173}
174
Simon Glassa53f4a22012-01-17 08:20:50 +0000175int fdtdec_find_aliases_for_id(const void *blob, const char *name,
176 enum fdt_compat_id id, int *node_list, int maxcount)
177{
Simon Glassc6782272012-02-03 15:13:53 +0000178 memset(node_list, '\0', sizeof(*node_list) * maxcount);
179
180 return fdtdec_add_aliases_for_id(blob, name, id, node_list, maxcount);
181}
182
183/* TODO: Can we tighten this code up a little? */
184int fdtdec_add_aliases_for_id(const void *blob, const char *name,
185 enum fdt_compat_id id, int *node_list, int maxcount)
186{
Simon Glassa53f4a22012-01-17 08:20:50 +0000187 int name_len = strlen(name);
188 int nodes[maxcount];
189 int num_found = 0;
190 int offset, node;
191 int alias_node;
192 int count;
193 int i, j;
194
195 /* find the alias node if present */
196 alias_node = fdt_path_offset(blob, "/aliases");
197
198 /*
199 * start with nothing, and we can assume that the root node can't
200 * match
201 */
202 memset(nodes, '\0', sizeof(nodes));
203
204 /* First find all the compatible nodes */
205 for (node = count = 0; node >= 0 && count < maxcount;) {
206 node = fdtdec_next_compatible(blob, node, id);
207 if (node >= 0)
208 nodes[count++] = node;
209 }
210 if (node >= 0)
211 debug("%s: warning: maxcount exceeded with alias '%s'\n",
212 __func__, name);
213
214 /* Now find all the aliases */
Simon Glassa53f4a22012-01-17 08:20:50 +0000215 for (offset = fdt_first_property_offset(blob, alias_node);
216 offset > 0;
217 offset = fdt_next_property_offset(blob, offset)) {
218 const struct fdt_property *prop;
219 const char *path;
220 int number;
221 int found;
222
223 node = 0;
224 prop = fdt_get_property_by_offset(blob, offset, NULL);
225 path = fdt_string(blob, fdt32_to_cpu(prop->nameoff));
226 if (prop->len && 0 == strncmp(path, name, name_len))
227 node = fdt_path_offset(blob, prop->data);
228 if (node <= 0)
229 continue;
230
231 /* Get the alias number */
232 number = simple_strtoul(path + name_len, NULL, 10);
233 if (number < 0 || number >= maxcount) {
234 debug("%s: warning: alias '%s' is out of range\n",
235 __func__, path);
236 continue;
237 }
238
239 /* Make sure the node we found is actually in our list! */
240 found = -1;
241 for (j = 0; j < count; j++)
242 if (nodes[j] == node) {
243 found = j;
244 break;
245 }
246
247 if (found == -1) {
248 debug("%s: warning: alias '%s' points to a node "
249 "'%s' that is missing or is not compatible "
250 " with '%s'\n", __func__, path,
251 fdt_get_name(blob, node, NULL),
252 compat_names[id]);
253 continue;
254 }
255
256 /*
257 * Add this node to our list in the right place, and mark
258 * it as done.
259 */
260 if (fdtdec_get_is_enabled(blob, node)) {
Simon Glassc6782272012-02-03 15:13:53 +0000261 if (node_list[number]) {
262 debug("%s: warning: alias '%s' requires that "
263 "a node be placed in the list in a "
264 "position which is already filled by "
265 "node '%s'\n", __func__, path,
266 fdt_get_name(blob, node, NULL));
267 continue;
268 }
Simon Glassa53f4a22012-01-17 08:20:50 +0000269 node_list[number] = node;
270 if (number >= num_found)
271 num_found = number + 1;
272 }
Simon Glassc6782272012-02-03 15:13:53 +0000273 nodes[found] = 0;
Simon Glassa53f4a22012-01-17 08:20:50 +0000274 }
275
276 /* Add any nodes not mentioned by an alias */
277 for (i = j = 0; i < maxcount; i++) {
278 if (!node_list[i]) {
279 for (; j < maxcount; j++)
280 if (nodes[j] &&
281 fdtdec_get_is_enabled(blob, nodes[j]))
282 break;
283
284 /* Have we run out of nodes to add? */
285 if (j == maxcount)
286 break;
287
288 assert(!node_list[i]);
289 node_list[i] = nodes[j++];
290 if (i >= num_found)
291 num_found = i + 1;
292 }
293 }
294
295 return num_found;
296}
297
Simon Glass9a263e52012-03-28 10:08:24 +0000298int fdtdec_check_fdt(void)
299{
300 /*
301 * We must have an FDT, but we cannot panic() yet since the console
302 * is not ready. So for now, just assert(). Boards which need an early
303 * FDT (prior to console ready) will need to make their own
304 * arrangements and do their own checks.
305 */
306 assert(!fdtdec_prepare_fdt());
307 return 0;
308}
309
Simon Glassb5220bc2011-10-24 19:15:32 +0000310/*
311 * This function is a little odd in that it accesses global data. At some
312 * point if the architecture board.c files merge this will make more sense.
313 * Even now, it is common code.
314 */
Simon Glass9a263e52012-03-28 10:08:24 +0000315int fdtdec_prepare_fdt(void)
Simon Glassb5220bc2011-10-24 19:15:32 +0000316{
Simon Glass9a263e52012-03-28 10:08:24 +0000317 if (((uintptr_t)gd->fdt_blob & 3) || fdt_check_header(gd->fdt_blob)) {
318 printf("No valid FDT found - please append one to U-Boot "
319 "binary, use u-boot-dtb.bin or define "
320 "CONFIG_OF_EMBED\n");
321 return -1;
322 }
Simon Glassb5220bc2011-10-24 19:15:32 +0000323 return 0;
324}
Simon Glassd17da652012-02-27 10:52:35 +0000325
326int fdtdec_lookup_phandle(const void *blob, int node, const char *prop_name)
327{
328 const u32 *phandle;
329 int lookup;
330
331 phandle = fdt_getprop(blob, node, prop_name, NULL);
332 if (!phandle)
333 return -FDT_ERR_NOTFOUND;
334
335 lookup = fdt_node_offset_by_phandle(blob, fdt32_to_cpu(*phandle));
336 return lookup;
337}
338
339/**
340 * Look up a property in a node and check that it has a minimum length.
341 *
342 * @param blob FDT blob
343 * @param node node to examine
344 * @param prop_name name of property to find
345 * @param min_len minimum property length in bytes
346 * @param err 0 if ok, or -FDT_ERR_NOTFOUND if the property is not
347 found, or -FDT_ERR_BADLAYOUT if not enough data
348 * @return pointer to cell, which is only valid if err == 0
349 */
350static const void *get_prop_check_min_len(const void *blob, int node,
351 const char *prop_name, int min_len, int *err)
352{
353 const void *cell;
354 int len;
355
356 debug("%s: %s\n", __func__, prop_name);
357 cell = fdt_getprop(blob, node, prop_name, &len);
358 if (!cell)
359 *err = -FDT_ERR_NOTFOUND;
360 else if (len < min_len)
361 *err = -FDT_ERR_BADLAYOUT;
362 else
363 *err = 0;
364 return cell;
365}
366
367int fdtdec_get_int_array(const void *blob, int node, const char *prop_name,
368 u32 *array, int count)
369{
370 const u32 *cell;
371 int i, err = 0;
372
373 debug("%s: %s\n", __func__, prop_name);
374 cell = get_prop_check_min_len(blob, node, prop_name,
375 sizeof(u32) * count, &err);
376 if (!err) {
377 for (i = 0; i < count; i++)
378 array[i] = fdt32_to_cpu(cell[i]);
379 }
380 return err;
381}
382
Simon Glass96875e72012-04-02 13:18:41 +0000383const u32 *fdtdec_locate_array(const void *blob, int node,
384 const char *prop_name, int count)
385{
386 const u32 *cell;
387 int err;
388
389 cell = get_prop_check_min_len(blob, node, prop_name,
390 sizeof(u32) * count, &err);
391 return err ? NULL : cell;
392}
393
Simon Glassd17da652012-02-27 10:52:35 +0000394int fdtdec_get_bool(const void *blob, int node, const char *prop_name)
395{
396 const s32 *cell;
397 int len;
398
399 debug("%s: %s\n", __func__, prop_name);
400 cell = fdt_getprop(blob, node, prop_name, &len);
401 return cell != NULL;
402}
Simon Glassed3ee5c2012-02-27 10:52:36 +0000403
404/**
405 * Decode a list of GPIOs from an FDT. This creates a list of GPIOs with no
406 * terminating item.
407 *
408 * @param blob FDT blob to use
409 * @param node Node to look at
410 * @param prop_name Node property name
411 * @param gpio Array of gpio elements to fill from FDT. This will be
412 * untouched if either 0 or an error is returned
413 * @param max_count Maximum number of elements allowed
414 * @return number of GPIOs read if ok, -FDT_ERR_BADLAYOUT if max_count would
415 * be exceeded, or -FDT_ERR_NOTFOUND if the property is missing.
416 */
417static int fdtdec_decode_gpios(const void *blob, int node,
418 const char *prop_name, struct fdt_gpio_state *gpio,
419 int max_count)
420{
421 const struct fdt_property *prop;
422 const u32 *cell;
423 const char *name;
424 int len, i;
425
426 debug("%s: %s\n", __func__, prop_name);
427 assert(max_count > 0);
428 prop = fdt_get_property(blob, node, prop_name, &len);
429 if (!prop) {
430 debug("FDT: %s: property '%s' missing\n", __func__, prop_name);
431 return -FDT_ERR_NOTFOUND;
432 }
433
434 /* We will use the name to tag the GPIO */
435 name = fdt_string(blob, fdt32_to_cpu(prop->nameoff));
436 cell = (u32 *)prop->data;
437 len /= sizeof(u32) * 3; /* 3 cells per GPIO record */
438 if (len > max_count) {
439 debug("FDT: %s: too many GPIOs / cells for "
440 "property '%s'\n", __func__, prop_name);
441 return -FDT_ERR_BADLAYOUT;
442 }
443
444 /* Read out the GPIO data from the cells */
445 for (i = 0; i < len; i++, cell += 3) {
446 gpio[i].gpio = fdt32_to_cpu(cell[1]);
447 gpio[i].flags = fdt32_to_cpu(cell[2]);
448 gpio[i].name = name;
449 }
450
451 return len;
452}
453
454int fdtdec_decode_gpio(const void *blob, int node, const char *prop_name,
455 struct fdt_gpio_state *gpio)
456{
457 int err;
458
459 debug("%s: %s\n", __func__, prop_name);
460 gpio->gpio = FDT_GPIO_NONE;
461 gpio->name = NULL;
462 err = fdtdec_decode_gpios(blob, node, prop_name, gpio, 1);
463 return err == 1 ? 0 : err;
464}
465
466int fdtdec_setup_gpio(struct fdt_gpio_state *gpio)
467{
468 /*
469 * Return success if there is no GPIO defined. This is used for
470 * optional GPIOs)
471 */
472 if (!fdt_gpio_isvalid(gpio))
473 return 0;
474
475 if (gpio_request(gpio->gpio, gpio->name))
476 return -1;
477 return 0;
478}
Anton Staffbed4d892012-04-17 09:01:28 +0000479
480int fdtdec_get_byte_array(const void *blob, int node, const char *prop_name,
481 u8 *array, int count)
482{
483 const u8 *cell;
484 int err;
485
486 cell = get_prop_check_min_len(blob, node, prop_name, count, &err);
487 if (!err)
488 memcpy(array, cell, count);
489 return err;
490}
491
492const u8 *fdtdec_locate_byte_array(const void *blob, int node,
493 const char *prop_name, int count)
494{
495 const u8 *cell;
496 int err;
497
498 cell = get_prop_check_min_len(blob, node, prop_name, count, &err);
499 if (err)
500 return NULL;
501 return cell;
502}