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 /* we need the generic GPIO interface here */
28 #include <asm-generic/gpio.h>
30 DECLARE_GLOBAL_DATA_PTR;
33 * Here are the type we know about. One day we might allow drivers to
34 * register. For now we just put them here. The COMPAT macro allows us to
35 * turn this into a sparse list later, and keeps the ID with the name.
37 #define COMPAT(id, name) name
38 static const char * const compat_names[COMPAT_COUNT] = {
39 COMPAT(UNKNOWN, "<none>"),
40 COMPAT(NVIDIA_TEGRA20_USB, "nvidia,tegra20-ehci"),
41 COMPAT(NVIDIA_TEGRA20_I2C, "nvidia,tegra20-i2c"),
42 COMPAT(NVIDIA_TEGRA20_DVC, "nvidia,tegra20-i2c-dvc"),
43 COMPAT(NVIDIA_TEGRA20_EMC, "nvidia,tegra20-emc"),
44 COMPAT(NVIDIA_TEGRA20_EMC_TABLE, "nvidia,tegra20-emc-table"),
45 COMPAT(NVIDIA_TEGRA20_KBC, "nvidia,tegra20-kbc"),
48 const char *fdtdec_get_compatible(enum fdt_compat_id id)
50 /* We allow reading of the 'unknown' ID for testing purposes */
51 assert(id >= 0 && id < COMPAT_COUNT);
52 return compat_names[id];
56 * Look in the FDT for an alias with the given name and return its node.
58 * @param blob FDT blob
59 * @param name alias name to look up
60 * @return node offset if found, or an error code < 0 otherwise
62 static int find_alias_node(const void *blob, const char *name)
67 debug("find_alias_node: %s\n", name);
68 alias_node = fdt_path_offset(blob, "/aliases");
71 path = fdt_getprop(blob, alias_node, name, NULL);
73 return -FDT_ERR_NOTFOUND;
74 return fdt_path_offset(blob, path);
77 fdt_addr_t fdtdec_get_addr(const void *blob, int node,
78 const char *prop_name)
80 const fdt_addr_t *cell;
83 debug("get_addr: %s\n", prop_name);
84 cell = fdt_getprop(blob, node, prop_name, &len);
85 if (cell && (len == sizeof(fdt_addr_t) ||
86 len == sizeof(fdt_addr_t) * 2))
87 return fdt_addr_to_cpu(*cell);
88 return FDT_ADDR_T_NONE;
91 s32 fdtdec_get_int(const void *blob, int node, const char *prop_name,
97 debug("get_size: %s\n", prop_name);
98 cell = fdt_getprop(blob, node, prop_name, &len);
99 if (cell && len >= sizeof(s32))
100 return fdt32_to_cpu(cell[0]);
104 int fdtdec_get_is_enabled(const void *blob, int node)
109 * It should say "okay", so only allow that. Some fdts use "ok" but
110 * this is a bug. Please fix your device tree source file. See here
113 * http://www.mail-archive.com/u-boot@lists.denx.de/msg71598.html
115 cell = fdt_getprop(blob, node, "status", NULL);
117 return 0 == strcmp(cell, "okay");
121 enum fdt_compat_id fd_dec_lookup(const void *blob, int node)
123 enum fdt_compat_id id;
125 /* Search our drivers */
126 for (id = COMPAT_UNKNOWN; id < COMPAT_COUNT; id++)
127 if (0 == fdt_node_check_compatible(blob, node,
130 return COMPAT_UNKNOWN;
133 int fdtdec_next_compatible(const void *blob, int node,
134 enum fdt_compat_id id)
136 return fdt_node_offset_by_compatible(blob, node, compat_names[id]);
139 int fdtdec_next_compatible_subnode(const void *blob, int node,
140 enum fdt_compat_id id, int *depthp)
143 node = fdt_next_node(blob, node, depthp);
144 } while (*depthp > 1);
146 /* If this is a direct subnode, and compatible, return it */
147 if (*depthp == 1 && 0 == fdt_node_check_compatible(
148 blob, node, compat_names[id]))
151 return -FDT_ERR_NOTFOUND;
154 int fdtdec_next_alias(const void *blob, const char *name,
155 enum fdt_compat_id id, int *upto)
157 #define MAX_STR_LEN 20
158 char str[MAX_STR_LEN + 20];
161 /* snprintf() is not available */
162 assert(strlen(name) < MAX_STR_LEN);
163 sprintf(str, "%.*s%d", MAX_STR_LEN, name, *upto);
164 node = find_alias_node(blob, str);
167 err = fdt_node_check_compatible(blob, node, compat_names[id]);
171 return -FDT_ERR_NOTFOUND;
176 int fdtdec_find_aliases_for_id(const void *blob, const char *name,
177 enum fdt_compat_id id, int *node_list, int maxcount)
179 memset(node_list, '\0', sizeof(*node_list) * maxcount);
181 return fdtdec_add_aliases_for_id(blob, name, id, node_list, maxcount);
184 /* TODO: Can we tighten this code up a little? */
185 int fdtdec_add_aliases_for_id(const void *blob, const char *name,
186 enum fdt_compat_id id, int *node_list, int maxcount)
188 int name_len = strlen(name);
196 /* find the alias node if present */
197 alias_node = fdt_path_offset(blob, "/aliases");
200 * start with nothing, and we can assume that the root node can't
203 memset(nodes, '\0', sizeof(nodes));
205 /* First find all the compatible nodes */
206 for (node = count = 0; node >= 0 && count < maxcount;) {
207 node = fdtdec_next_compatible(blob, node, id);
209 nodes[count++] = node;
212 debug("%s: warning: maxcount exceeded with alias '%s'\n",
215 /* Now find all the aliases */
216 for (offset = fdt_first_property_offset(blob, alias_node);
218 offset = fdt_next_property_offset(blob, offset)) {
219 const struct fdt_property *prop;
225 prop = fdt_get_property_by_offset(blob, offset, NULL);
226 path = fdt_string(blob, fdt32_to_cpu(prop->nameoff));
227 if (prop->len && 0 == strncmp(path, name, name_len))
228 node = fdt_path_offset(blob, prop->data);
232 /* Get the alias number */
233 number = simple_strtoul(path + name_len, NULL, 10);
234 if (number < 0 || number >= maxcount) {
235 debug("%s: warning: alias '%s' is out of range\n",
240 /* Make sure the node we found is actually in our list! */
242 for (j = 0; j < count; j++)
243 if (nodes[j] == node) {
249 debug("%s: warning: alias '%s' points to a node "
250 "'%s' that is missing or is not compatible "
251 " with '%s'\n", __func__, path,
252 fdt_get_name(blob, node, NULL),
258 * Add this node to our list in the right place, and mark
261 if (fdtdec_get_is_enabled(blob, node)) {
262 if (node_list[number]) {
263 debug("%s: warning: alias '%s' requires that "
264 "a node be placed in the list in a "
265 "position which is already filled by "
266 "node '%s'\n", __func__, path,
267 fdt_get_name(blob, node, NULL));
270 node_list[number] = node;
271 if (number >= num_found)
272 num_found = number + 1;
277 /* Add any nodes not mentioned by an alias */
278 for (i = j = 0; i < maxcount; i++) {
280 for (; j < maxcount; j++)
282 fdtdec_get_is_enabled(blob, nodes[j]))
285 /* Have we run out of nodes to add? */
289 assert(!node_list[i]);
290 node_list[i] = nodes[j++];
299 int fdtdec_check_fdt(void)
302 * We must have an FDT, but we cannot panic() yet since the console
303 * is not ready. So for now, just assert(). Boards which need an early
304 * FDT (prior to console ready) will need to make their own
305 * arrangements and do their own checks.
307 assert(!fdtdec_prepare_fdt());
312 * This function is a little odd in that it accesses global data. At some
313 * point if the architecture board.c files merge this will make more sense.
314 * Even now, it is common code.
316 int fdtdec_prepare_fdt(void)
318 if (((uintptr_t)gd->fdt_blob & 3) || fdt_check_header(gd->fdt_blob)) {
319 printf("No valid FDT found - please append one to U-Boot "
320 "binary, use u-boot-dtb.bin or define "
321 "CONFIG_OF_EMBED\n");
327 int fdtdec_lookup_phandle(const void *blob, int node, const char *prop_name)
332 phandle = fdt_getprop(blob, node, prop_name, NULL);
334 return -FDT_ERR_NOTFOUND;
336 lookup = fdt_node_offset_by_phandle(blob, fdt32_to_cpu(*phandle));
341 * Look up a property in a node and check that it has a minimum length.
343 * @param blob FDT blob
344 * @param node node to examine
345 * @param prop_name name of property to find
346 * @param min_len minimum property length in bytes
347 * @param err 0 if ok, or -FDT_ERR_NOTFOUND if the property is not
348 found, or -FDT_ERR_BADLAYOUT if not enough data
349 * @return pointer to cell, which is only valid if err == 0
351 static const void *get_prop_check_min_len(const void *blob, int node,
352 const char *prop_name, int min_len, int *err)
357 debug("%s: %s\n", __func__, prop_name);
358 cell = fdt_getprop(blob, node, prop_name, &len);
360 *err = -FDT_ERR_NOTFOUND;
361 else if (len < min_len)
362 *err = -FDT_ERR_BADLAYOUT;
368 int fdtdec_get_int_array(const void *blob, int node, const char *prop_name,
369 u32 *array, int count)
374 debug("%s: %s\n", __func__, prop_name);
375 cell = get_prop_check_min_len(blob, node, prop_name,
376 sizeof(u32) * count, &err);
378 for (i = 0; i < count; i++)
379 array[i] = fdt32_to_cpu(cell[i]);
384 const u32 *fdtdec_locate_array(const void *blob, int node,
385 const char *prop_name, int count)
390 cell = get_prop_check_min_len(blob, node, prop_name,
391 sizeof(u32) * count, &err);
392 return err ? NULL : cell;
395 int fdtdec_get_bool(const void *blob, int node, const char *prop_name)
400 debug("%s: %s\n", __func__, prop_name);
401 cell = fdt_getprop(blob, node, prop_name, &len);
406 * Decode a list of GPIOs from an FDT. This creates a list of GPIOs with no
409 * @param blob FDT blob to use
410 * @param node Node to look at
411 * @param prop_name Node property name
412 * @param gpio Array of gpio elements to fill from FDT. This will be
413 * untouched if either 0 or an error is returned
414 * @param max_count Maximum number of elements allowed
415 * @return number of GPIOs read if ok, -FDT_ERR_BADLAYOUT if max_count would
416 * be exceeded, or -FDT_ERR_NOTFOUND if the property is missing.
418 static int fdtdec_decode_gpios(const void *blob, int node,
419 const char *prop_name, struct fdt_gpio_state *gpio,
422 const struct fdt_property *prop;
427 debug("%s: %s\n", __func__, prop_name);
428 assert(max_count > 0);
429 prop = fdt_get_property(blob, node, prop_name, &len);
431 debug("FDT: %s: property '%s' missing\n", __func__, prop_name);
432 return -FDT_ERR_NOTFOUND;
435 /* We will use the name to tag the GPIO */
436 name = fdt_string(blob, fdt32_to_cpu(prop->nameoff));
437 cell = (u32 *)prop->data;
438 len /= sizeof(u32) * 3; /* 3 cells per GPIO record */
439 if (len > max_count) {
440 debug("FDT: %s: too many GPIOs / cells for "
441 "property '%s'\n", __func__, prop_name);
442 return -FDT_ERR_BADLAYOUT;
445 /* Read out the GPIO data from the cells */
446 for (i = 0; i < len; i++, cell += 3) {
447 gpio[i].gpio = fdt32_to_cpu(cell[1]);
448 gpio[i].flags = fdt32_to_cpu(cell[2]);
455 int fdtdec_decode_gpio(const void *blob, int node, const char *prop_name,
456 struct fdt_gpio_state *gpio)
460 debug("%s: %s\n", __func__, prop_name);
461 gpio->gpio = FDT_GPIO_NONE;
463 err = fdtdec_decode_gpios(blob, node, prop_name, gpio, 1);
464 return err == 1 ? 0 : err;
467 int fdtdec_setup_gpio(struct fdt_gpio_state *gpio)
470 * Return success if there is no GPIO defined. This is used for
473 if (!fdt_gpio_isvalid(gpio))
476 if (gpio_request(gpio->gpio, gpio->name))
481 int fdtdec_get_byte_array(const void *blob, int node, const char *prop_name,
482 u8 *array, int count)
487 cell = get_prop_check_min_len(blob, node, prop_name, count, &err);
489 memcpy(array, cell, count);
493 const u8 *fdtdec_locate_byte_array(const void *blob, int node,
494 const char *prop_name, int count)
499 cell = get_prop_check_min_len(blob, node, prop_name, count, &err);