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,
29 DECLARE_GLOBAL_DATA_PTR;
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.
36 #define COMPAT(id, name) name
37 static const char * const compat_names[COMPAT_COUNT] = {
38 COMPAT(UNKNOWN, "<none>"),
39 COMPAT(NVIDIA_TEGRA20_USB, "nvidia,tegra20-ehci"),
40 COMPAT(NVIDIA_TEGRA20_I2C, "nvidia,tegra20-i2c"),
41 COMPAT(NVIDIA_TEGRA20_DVC, "nvidia,tegra20-i2c-dvc"),
42 COMPAT(NVIDIA_TEGRA20_EMC, "nvidia,tegra20-emc"),
43 COMPAT(NVIDIA_TEGRA20_EMC_TABLE, "nvidia,tegra20-emc-table"),
44 COMPAT(NVIDIA_TEGRA20_KBC, "nvidia,tegra20-kbc"),
47 const char *fdtdec_get_compatible(enum fdt_compat_id id)
49 /* We allow reading of the 'unknown' ID for testing purposes */
50 assert(id >= 0 && id < COMPAT_COUNT);
51 return compat_names[id];
55 * Look in the FDT for an alias with the given name and return its node.
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
61 static int find_alias_node(const void *blob, const char *name)
66 debug("find_alias_node: %s\n", name);
67 alias_node = fdt_path_offset(blob, "/aliases");
70 path = fdt_getprop(blob, alias_node, name, NULL);
72 return -FDT_ERR_NOTFOUND;
73 return fdt_path_offset(blob, path);
76 fdt_addr_t fdtdec_get_addr(const void *blob, int node,
77 const char *prop_name)
79 const fdt_addr_t *cell;
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;
90 s32 fdtdec_get_int(const void *blob, int node, const char *prop_name,
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]);
103 int fdtdec_get_is_enabled(const void *blob, int node)
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
112 * http://www.mail-archive.com/u-boot@lists.denx.de/msg71598.html
114 cell = fdt_getprop(blob, node, "status", NULL);
116 return 0 == strcmp(cell, "okay");
120 enum fdt_compat_id fd_dec_lookup(const void *blob, int node)
122 enum fdt_compat_id id;
124 /* Search our drivers */
125 for (id = COMPAT_UNKNOWN; id < COMPAT_COUNT; id++)
126 if (0 == fdt_node_check_compatible(blob, node,
129 return COMPAT_UNKNOWN;
132 int fdtdec_next_compatible(const void *blob, int node,
133 enum fdt_compat_id id)
135 return fdt_node_offset_by_compatible(blob, node, compat_names[id]);
138 int fdtdec_next_compatible_subnode(const void *blob, int node,
139 enum fdt_compat_id id, int *depthp)
142 node = fdt_next_node(blob, node, depthp);
143 } while (*depthp > 1);
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]))
150 return -FDT_ERR_NOTFOUND;
153 int fdtdec_next_alias(const void *blob, const char *name,
154 enum fdt_compat_id id, int *upto)
156 #define MAX_STR_LEN 20
157 char str[MAX_STR_LEN + 20];
160 /* snprintf() is not available */
161 assert(strlen(name) < MAX_STR_LEN);
162 sprintf(str, "%.*s%d", MAX_STR_LEN, name, *upto);
163 node = find_alias_node(blob, str);
166 err = fdt_node_check_compatible(blob, node, compat_names[id]);
170 return -FDT_ERR_NOTFOUND;
175 int fdtdec_find_aliases_for_id(const void *blob, const char *name,
176 enum fdt_compat_id id, int *node_list, int maxcount)
178 memset(node_list, '\0', sizeof(*node_list) * maxcount);
180 return fdtdec_add_aliases_for_id(blob, name, id, node_list, maxcount);
183 /* TODO: Can we tighten this code up a little? */
184 int fdtdec_add_aliases_for_id(const void *blob, const char *name,
185 enum fdt_compat_id id, int *node_list, int maxcount)
187 int name_len = strlen(name);
195 /* find the alias node if present */
196 alias_node = fdt_path_offset(blob, "/aliases");
199 * start with nothing, and we can assume that the root node can't
202 memset(nodes, '\0', sizeof(nodes));
204 /* First find all the compatible nodes */
205 for (node = count = 0; node >= 0 && count < maxcount;) {
206 node = fdtdec_next_compatible(blob, node, id);
208 nodes[count++] = node;
211 debug("%s: warning: maxcount exceeded with alias '%s'\n",
214 /* Now find all the aliases */
215 for (offset = fdt_first_property_offset(blob, alias_node);
217 offset = fdt_next_property_offset(blob, offset)) {
218 const struct fdt_property *prop;
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);
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",
239 /* Make sure the node we found is actually in our list! */
241 for (j = 0; j < count; j++)
242 if (nodes[j] == node) {
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),
257 * Add this node to our list in the right place, and mark
260 if (fdtdec_get_is_enabled(blob, node)) {
261 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));
269 node_list[number] = node;
270 if (number >= num_found)
271 num_found = number + 1;
276 /* Add any nodes not mentioned by an alias */
277 for (i = j = 0; i < maxcount; i++) {
279 for (; j < maxcount; j++)
281 fdtdec_get_is_enabled(blob, nodes[j]))
284 /* Have we run out of nodes to add? */
288 assert(!node_list[i]);
289 node_list[i] = nodes[j++];
298 int fdtdec_check_fdt(void)
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.
306 assert(!fdtdec_prepare_fdt());
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.
315 int fdtdec_prepare_fdt(void)
317 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");
326 int fdtdec_lookup_phandle(const void *blob, int node, const char *prop_name)
331 phandle = fdt_getprop(blob, node, prop_name, NULL);
333 return -FDT_ERR_NOTFOUND;
335 lookup = fdt_node_offset_by_phandle(blob, fdt32_to_cpu(*phandle));
340 * Look up a property in a node and check that it has a minimum length.
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
350 static const void *get_prop_check_min_len(const void *blob, int node,
351 const char *prop_name, int min_len, int *err)
356 debug("%s: %s\n", __func__, prop_name);
357 cell = fdt_getprop(blob, node, prop_name, &len);
359 *err = -FDT_ERR_NOTFOUND;
360 else if (len < min_len)
361 *err = -FDT_ERR_BADLAYOUT;
367 int fdtdec_get_int_array(const void *blob, int node, const char *prop_name,
368 u32 *array, int count)
373 debug("%s: %s\n", __func__, prop_name);
374 cell = get_prop_check_min_len(blob, node, prop_name,
375 sizeof(u32) * count, &err);
377 for (i = 0; i < count; i++)
378 array[i] = fdt32_to_cpu(cell[i]);
383 const u32 *fdtdec_locate_array(const void *blob, int node,
384 const char *prop_name, int count)
389 cell = get_prop_check_min_len(blob, node, prop_name,
390 sizeof(u32) * count, &err);
391 return err ? NULL : cell;
394 int fdtdec_get_bool(const void *blob, int node, const char *prop_name)
399 debug("%s: %s\n", __func__, prop_name);
400 cell = fdt_getprop(blob, node, prop_name, &len);
405 * Decode a list of GPIOs from an FDT. This creates a list of GPIOs with no
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.
417 static int fdtdec_decode_gpios(const void *blob, int node,
418 const char *prop_name, struct fdt_gpio_state *gpio,
421 const struct fdt_property *prop;
426 debug("%s: %s\n", __func__, prop_name);
427 assert(max_count > 0);
428 prop = fdt_get_property(blob, node, prop_name, &len);
430 debug("FDT: %s: property '%s' missing\n", __func__, prop_name);
431 return -FDT_ERR_NOTFOUND;
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;
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]);
454 int fdtdec_decode_gpio(const void *blob, int node, const char *prop_name,
455 struct fdt_gpio_state *gpio)
459 debug("%s: %s\n", __func__, prop_name);
460 gpio->gpio = FDT_GPIO_NONE;
462 err = fdtdec_decode_gpios(blob, node, prop_name, gpio, 1);
463 return err == 1 ? 0 : err;
466 int fdtdec_setup_gpio(struct fdt_gpio_state *gpio)
469 * Return success if there is no GPIO defined. This is used for
472 if (!fdt_gpio_isvalid(gpio))
475 if (gpio_request(gpio->gpio, gpio->name))
480 int fdtdec_get_byte_array(const void *blob, int node, const char *prop_name,
481 u8 *array, int count)
486 cell = get_prop_check_min_len(blob, node, prop_name, count, &err);
488 memcpy(array, cell, count);
492 const u8 *fdtdec_locate_byte_array(const void *blob, int node,
493 const char *prop_name, int count)
498 cell = get_prop_check_min_len(blob, node, prop_name, count, &err);