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"),
45 const char *fdtdec_get_compatible(enum fdt_compat_id id)
47 /* We allow reading of the 'unknown' ID for testing purposes */
48 assert(id >= 0 && id < COMPAT_COUNT);
49 return compat_names[id];
53 * Look in the FDT for an alias with the given name and return its node.
55 * @param blob FDT blob
56 * @param name alias name to look up
57 * @return node offset if found, or an error code < 0 otherwise
59 static int find_alias_node(const void *blob, const char *name)
64 debug("find_alias_node: %s\n", name);
65 alias_node = fdt_path_offset(blob, "/aliases");
68 path = fdt_getprop(blob, alias_node, name, NULL);
70 return -FDT_ERR_NOTFOUND;
71 return fdt_path_offset(blob, path);
74 fdt_addr_t fdtdec_get_addr(const void *blob, int node,
75 const char *prop_name)
77 const fdt_addr_t *cell;
80 debug("get_addr: %s\n", prop_name);
81 cell = fdt_getprop(blob, node, prop_name, &len);
82 if (cell && (len == sizeof(fdt_addr_t) ||
83 len == sizeof(fdt_addr_t) * 2))
84 return fdt_addr_to_cpu(*cell);
85 return FDT_ADDR_T_NONE;
88 s32 fdtdec_get_int(const void *blob, int node, const char *prop_name,
94 debug("get_size: %s\n", prop_name);
95 cell = fdt_getprop(blob, node, prop_name, &len);
96 if (cell && len >= sizeof(s32))
97 return fdt32_to_cpu(cell[0]);
101 int fdtdec_get_is_enabled(const void *blob, int node)
106 * It should say "okay", so only allow that. Some fdts use "ok" but
107 * this is a bug. Please fix your device tree source file. See here
110 * http://www.mail-archive.com/u-boot@lists.denx.de/msg71598.html
112 cell = fdt_getprop(blob, node, "status", NULL);
114 return 0 == strcmp(cell, "okay");
118 enum fdt_compat_id fd_dec_lookup(const void *blob, int node)
120 enum fdt_compat_id id;
122 /* Search our drivers */
123 for (id = COMPAT_UNKNOWN; id < COMPAT_COUNT; id++)
124 if (0 == fdt_node_check_compatible(blob, node,
127 return COMPAT_UNKNOWN;
130 int fdtdec_next_compatible(const void *blob, int node,
131 enum fdt_compat_id id)
133 return fdt_node_offset_by_compatible(blob, node, compat_names[id]);
136 int fdtdec_next_compatible_subnode(const void *blob, int node,
137 enum fdt_compat_id id, int *depthp)
140 node = fdt_next_node(blob, node, depthp);
141 } while (*depthp > 1);
143 /* If this is a direct subnode, and compatible, return it */
144 if (*depthp == 1 && 0 == fdt_node_check_compatible(
145 blob, node, compat_names[id]))
148 return -FDT_ERR_NOTFOUND;
151 int fdtdec_next_alias(const void *blob, const char *name,
152 enum fdt_compat_id id, int *upto)
154 #define MAX_STR_LEN 20
155 char str[MAX_STR_LEN + 20];
158 /* snprintf() is not available */
159 assert(strlen(name) < MAX_STR_LEN);
160 sprintf(str, "%.*s%d", MAX_STR_LEN, name, *upto);
161 node = find_alias_node(blob, str);
164 err = fdt_node_check_compatible(blob, node, compat_names[id]);
168 return -FDT_ERR_NOTFOUND;
173 int fdtdec_find_aliases_for_id(const void *blob, const char *name,
174 enum fdt_compat_id id, int *node_list, int maxcount)
176 memset(node_list, '\0', sizeof(*node_list) * maxcount);
178 return fdtdec_add_aliases_for_id(blob, name, id, node_list, maxcount);
181 /* TODO: Can we tighten this code up a little? */
182 int fdtdec_add_aliases_for_id(const void *blob, const char *name,
183 enum fdt_compat_id id, int *node_list, int maxcount)
185 int name_len = strlen(name);
193 /* find the alias node if present */
194 alias_node = fdt_path_offset(blob, "/aliases");
197 * start with nothing, and we can assume that the root node can't
200 memset(nodes, '\0', sizeof(nodes));
202 /* First find all the compatible nodes */
203 for (node = count = 0; node >= 0 && count < maxcount;) {
204 node = fdtdec_next_compatible(blob, node, id);
206 nodes[count++] = node;
209 debug("%s: warning: maxcount exceeded with alias '%s'\n",
212 /* Now find all the aliases */
213 for (offset = fdt_first_property_offset(blob, alias_node);
215 offset = fdt_next_property_offset(blob, offset)) {
216 const struct fdt_property *prop;
222 prop = fdt_get_property_by_offset(blob, offset, NULL);
223 path = fdt_string(blob, fdt32_to_cpu(prop->nameoff));
224 if (prop->len && 0 == strncmp(path, name, name_len))
225 node = fdt_path_offset(blob, prop->data);
229 /* Get the alias number */
230 number = simple_strtoul(path + name_len, NULL, 10);
231 if (number < 0 || number >= maxcount) {
232 debug("%s: warning: alias '%s' is out of range\n",
237 /* Make sure the node we found is actually in our list! */
239 for (j = 0; j < count; j++)
240 if (nodes[j] == node) {
246 debug("%s: warning: alias '%s' points to a node "
247 "'%s' that is missing or is not compatible "
248 " with '%s'\n", __func__, path,
249 fdt_get_name(blob, node, NULL),
255 * Add this node to our list in the right place, and mark
258 if (fdtdec_get_is_enabled(blob, node)) {
259 if (node_list[number]) {
260 debug("%s: warning: alias '%s' requires that "
261 "a node be placed in the list in a "
262 "position which is already filled by "
263 "node '%s'\n", __func__, path,
264 fdt_get_name(blob, node, NULL));
267 node_list[number] = node;
268 if (number >= num_found)
269 num_found = number + 1;
274 /* Add any nodes not mentioned by an alias */
275 for (i = j = 0; i < maxcount; i++) {
277 for (; j < maxcount; j++)
279 fdtdec_get_is_enabled(blob, nodes[j]))
282 /* Have we run out of nodes to add? */
286 assert(!node_list[i]);
287 node_list[i] = nodes[j++];
296 int fdtdec_check_fdt(void)
299 * We must have an FDT, but we cannot panic() yet since the console
300 * is not ready. So for now, just assert(). Boards which need an early
301 * FDT (prior to console ready) will need to make their own
302 * arrangements and do their own checks.
304 assert(!fdtdec_prepare_fdt());
309 * This function is a little odd in that it accesses global data. At some
310 * point if the architecture board.c files merge this will make more sense.
311 * Even now, it is common code.
313 int fdtdec_prepare_fdt(void)
315 if (((uintptr_t)gd->fdt_blob & 3) || fdt_check_header(gd->fdt_blob)) {
316 printf("No valid FDT found - please append one to U-Boot "
317 "binary, use u-boot-dtb.bin or define "
318 "CONFIG_OF_EMBED\n");
324 int fdtdec_lookup_phandle(const void *blob, int node, const char *prop_name)
329 phandle = fdt_getprop(blob, node, prop_name, NULL);
331 return -FDT_ERR_NOTFOUND;
333 lookup = fdt_node_offset_by_phandle(blob, fdt32_to_cpu(*phandle));
338 * Look up a property in a node and check that it has a minimum length.
340 * @param blob FDT blob
341 * @param node node to examine
342 * @param prop_name name of property to find
343 * @param min_len minimum property length in bytes
344 * @param err 0 if ok, or -FDT_ERR_NOTFOUND if the property is not
345 found, or -FDT_ERR_BADLAYOUT if not enough data
346 * @return pointer to cell, which is only valid if err == 0
348 static const void *get_prop_check_min_len(const void *blob, int node,
349 const char *prop_name, int min_len, int *err)
354 debug("%s: %s\n", __func__, prop_name);
355 cell = fdt_getprop(blob, node, prop_name, &len);
357 *err = -FDT_ERR_NOTFOUND;
358 else if (len < min_len)
359 *err = -FDT_ERR_BADLAYOUT;
365 int fdtdec_get_int_array(const void *blob, int node, const char *prop_name,
366 u32 *array, int count)
371 debug("%s: %s\n", __func__, prop_name);
372 cell = get_prop_check_min_len(blob, node, prop_name,
373 sizeof(u32) * count, &err);
375 for (i = 0; i < count; i++)
376 array[i] = fdt32_to_cpu(cell[i]);
381 const u32 *fdtdec_locate_array(const void *blob, int node,
382 const char *prop_name, int count)
387 cell = get_prop_check_min_len(blob, node, prop_name,
388 sizeof(u32) * count, &err);
389 return err ? NULL : cell;
392 int fdtdec_get_bool(const void *blob, int node, const char *prop_name)
397 debug("%s: %s\n", __func__, prop_name);
398 cell = fdt_getprop(blob, node, prop_name, &len);
403 * Decode a list of GPIOs from an FDT. This creates a list of GPIOs with no
406 * @param blob FDT blob to use
407 * @param node Node to look at
408 * @param prop_name Node property name
409 * @param gpio Array of gpio elements to fill from FDT. This will be
410 * untouched if either 0 or an error is returned
411 * @param max_count Maximum number of elements allowed
412 * @return number of GPIOs read if ok, -FDT_ERR_BADLAYOUT if max_count would
413 * be exceeded, or -FDT_ERR_NOTFOUND if the property is missing.
415 static int fdtdec_decode_gpios(const void *blob, int node,
416 const char *prop_name, struct fdt_gpio_state *gpio,
419 const struct fdt_property *prop;
424 debug("%s: %s\n", __func__, prop_name);
425 assert(max_count > 0);
426 prop = fdt_get_property(blob, node, prop_name, &len);
428 debug("FDT: %s: property '%s' missing\n", __func__, prop_name);
429 return -FDT_ERR_NOTFOUND;
432 /* We will use the name to tag the GPIO */
433 name = fdt_string(blob, fdt32_to_cpu(prop->nameoff));
434 cell = (u32 *)prop->data;
435 len /= sizeof(u32) * 3; /* 3 cells per GPIO record */
436 if (len > max_count) {
437 debug("FDT: %s: too many GPIOs / cells for "
438 "property '%s'\n", __func__, prop_name);
439 return -FDT_ERR_BADLAYOUT;
442 /* Read out the GPIO data from the cells */
443 for (i = 0; i < len; i++, cell += 3) {
444 gpio[i].gpio = fdt32_to_cpu(cell[1]);
445 gpio[i].flags = fdt32_to_cpu(cell[2]);
452 int fdtdec_decode_gpio(const void *blob, int node, const char *prop_name,
453 struct fdt_gpio_state *gpio)
457 debug("%s: %s\n", __func__, prop_name);
458 gpio->gpio = FDT_GPIO_NONE;
460 err = fdtdec_decode_gpios(blob, node, prop_name, gpio, 1);
461 return err == 1 ? 0 : err;
464 int fdtdec_setup_gpio(struct fdt_gpio_state *gpio)
467 * Return success if there is no GPIO defined. This is used for
470 if (!fdt_gpio_isvalid(gpio))
473 if (gpio_request(gpio->gpio, gpio->name))