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"),
43 const char *fdtdec_get_compatible(enum fdt_compat_id id)
45 /* We allow reading of the 'unknown' ID for testing purposes */
46 assert(id >= 0 && id < COMPAT_COUNT);
47 return compat_names[id];
51 * Look in the FDT for an alias with the given name and return its node.
53 * @param blob FDT blob
54 * @param name alias name to look up
55 * @return node offset if found, or an error code < 0 otherwise
57 static int find_alias_node(const void *blob, const char *name)
62 debug("find_alias_node: %s\n", name);
63 alias_node = fdt_path_offset(blob, "/aliases");
66 path = fdt_getprop(blob, alias_node, name, NULL);
68 return -FDT_ERR_NOTFOUND;
69 return fdt_path_offset(blob, path);
72 fdt_addr_t fdtdec_get_addr(const void *blob, int node,
73 const char *prop_name)
75 const fdt_addr_t *cell;
78 debug("get_addr: %s\n", prop_name);
79 cell = fdt_getprop(blob, node, prop_name, &len);
80 if (cell && (len == sizeof(fdt_addr_t) ||
81 len == sizeof(fdt_addr_t) * 2))
82 return fdt_addr_to_cpu(*cell);
83 return FDT_ADDR_T_NONE;
86 s32 fdtdec_get_int(const void *blob, int node, const char *prop_name,
92 debug("get_size: %s\n", prop_name);
93 cell = fdt_getprop(blob, node, prop_name, &len);
94 if (cell && len >= sizeof(s32))
95 return fdt32_to_cpu(cell[0]);
99 int fdtdec_get_is_enabled(const void *blob, int node)
104 * It should say "okay", so only allow that. Some fdts use "ok" but
105 * this is a bug. Please fix your device tree source file. See here
108 * http://www.mail-archive.com/u-boot@lists.denx.de/msg71598.html
110 cell = fdt_getprop(blob, node, "status", NULL);
112 return 0 == strcmp(cell, "okay");
116 enum fdt_compat_id fd_dec_lookup(const void *blob, int node)
118 enum fdt_compat_id id;
120 /* Search our drivers */
121 for (id = COMPAT_UNKNOWN; id < COMPAT_COUNT; id++)
122 if (0 == fdt_node_check_compatible(blob, node,
125 return COMPAT_UNKNOWN;
128 int fdtdec_next_compatible(const void *blob, int node,
129 enum fdt_compat_id id)
131 return fdt_node_offset_by_compatible(blob, node, compat_names[id]);
134 int fdtdec_next_alias(const void *blob, const char *name,
135 enum fdt_compat_id id, int *upto)
137 #define MAX_STR_LEN 20
138 char str[MAX_STR_LEN + 20];
141 /* snprintf() is not available */
142 assert(strlen(name) < MAX_STR_LEN);
143 sprintf(str, "%.*s%d", MAX_STR_LEN, name, *upto);
144 node = find_alias_node(blob, str);
147 err = fdt_node_check_compatible(blob, node, compat_names[id]);
151 return -FDT_ERR_NOTFOUND;
156 /* TODO: Can we tighten this code up a little? */
157 int fdtdec_find_aliases_for_id(const void *blob, const char *name,
158 enum fdt_compat_id id, int *node_list, int maxcount)
160 int name_len = strlen(name);
168 /* find the alias node if present */
169 alias_node = fdt_path_offset(blob, "/aliases");
172 * start with nothing, and we can assume that the root node can't
175 memset(nodes, '\0', sizeof(nodes));
177 /* First find all the compatible nodes */
178 for (node = count = 0; node >= 0 && count < maxcount;) {
179 node = fdtdec_next_compatible(blob, node, id);
181 nodes[count++] = node;
184 debug("%s: warning: maxcount exceeded with alias '%s'\n",
187 /* Now find all the aliases */
188 memset(node_list, '\0', sizeof(*node_list) * maxcount);
190 for (offset = fdt_first_property_offset(blob, alias_node);
192 offset = fdt_next_property_offset(blob, offset)) {
193 const struct fdt_property *prop;
199 prop = fdt_get_property_by_offset(blob, offset, NULL);
200 path = fdt_string(blob, fdt32_to_cpu(prop->nameoff));
201 if (prop->len && 0 == strncmp(path, name, name_len))
202 node = fdt_path_offset(blob, prop->data);
206 /* Get the alias number */
207 number = simple_strtoul(path + name_len, NULL, 10);
208 if (number < 0 || number >= maxcount) {
209 debug("%s: warning: alias '%s' is out of range\n",
214 /* Make sure the node we found is actually in our list! */
216 for (j = 0; j < count; j++)
217 if (nodes[j] == node) {
223 debug("%s: warning: alias '%s' points to a node "
224 "'%s' that is missing or is not compatible "
225 " with '%s'\n", __func__, path,
226 fdt_get_name(blob, node, NULL),
232 * Add this node to our list in the right place, and mark
235 if (fdtdec_get_is_enabled(blob, node)) {
236 node_list[number] = node;
237 if (number >= num_found)
238 num_found = number + 1;
243 /* Add any nodes not mentioned by an alias */
244 for (i = j = 0; i < maxcount; i++) {
246 for (; j < maxcount; j++)
248 fdtdec_get_is_enabled(blob, nodes[j]))
251 /* Have we run out of nodes to add? */
255 assert(!node_list[i]);
256 node_list[i] = nodes[j++];
265 int fdtdec_check_fdt(void)
268 * We must have an FDT, but we cannot panic() yet since the console
269 * is not ready. So for now, just assert(). Boards which need an early
270 * FDT (prior to console ready) will need to make their own
271 * arrangements and do their own checks.
273 assert(!fdtdec_prepare_fdt());
278 * This function is a little odd in that it accesses global data. At some
279 * point if the architecture board.c files merge this will make more sense.
280 * Even now, it is common code.
282 int fdtdec_prepare_fdt(void)
284 if (((uintptr_t)gd->fdt_blob & 3) || fdt_check_header(gd->fdt_blob)) {
285 printf("No valid FDT found - please append one to U-Boot "
286 "binary, use u-boot-dtb.bin or define "
287 "CONFIG_OF_EMBED\n");
293 int fdtdec_lookup_phandle(const void *blob, int node, const char *prop_name)
298 phandle = fdt_getprop(blob, node, prop_name, NULL);
300 return -FDT_ERR_NOTFOUND;
302 lookup = fdt_node_offset_by_phandle(blob, fdt32_to_cpu(*phandle));
307 * Look up a property in a node and check that it has a minimum length.
309 * @param blob FDT blob
310 * @param node node to examine
311 * @param prop_name name of property to find
312 * @param min_len minimum property length in bytes
313 * @param err 0 if ok, or -FDT_ERR_NOTFOUND if the property is not
314 found, or -FDT_ERR_BADLAYOUT if not enough data
315 * @return pointer to cell, which is only valid if err == 0
317 static const void *get_prop_check_min_len(const void *blob, int node,
318 const char *prop_name, int min_len, int *err)
323 debug("%s: %s\n", __func__, prop_name);
324 cell = fdt_getprop(blob, node, prop_name, &len);
326 *err = -FDT_ERR_NOTFOUND;
327 else if (len < min_len)
328 *err = -FDT_ERR_BADLAYOUT;
334 int fdtdec_get_int_array(const void *blob, int node, const char *prop_name,
335 u32 *array, int count)
340 debug("%s: %s\n", __func__, prop_name);
341 cell = get_prop_check_min_len(blob, node, prop_name,
342 sizeof(u32) * count, &err);
344 for (i = 0; i < count; i++)
345 array[i] = fdt32_to_cpu(cell[i]);
350 int fdtdec_get_bool(const void *blob, int node, const char *prop_name)
355 debug("%s: %s\n", __func__, prop_name);
356 cell = fdt_getprop(blob, node, prop_name, &len);
361 * Decode a list of GPIOs from an FDT. This creates a list of GPIOs with no
364 * @param blob FDT blob to use
365 * @param node Node to look at
366 * @param prop_name Node property name
367 * @param gpio Array of gpio elements to fill from FDT. This will be
368 * untouched if either 0 or an error is returned
369 * @param max_count Maximum number of elements allowed
370 * @return number of GPIOs read if ok, -FDT_ERR_BADLAYOUT if max_count would
371 * be exceeded, or -FDT_ERR_NOTFOUND if the property is missing.
373 static int fdtdec_decode_gpios(const void *blob, int node,
374 const char *prop_name, struct fdt_gpio_state *gpio,
377 const struct fdt_property *prop;
382 debug("%s: %s\n", __func__, prop_name);
383 assert(max_count > 0);
384 prop = fdt_get_property(blob, node, prop_name, &len);
386 debug("FDT: %s: property '%s' missing\n", __func__, prop_name);
387 return -FDT_ERR_NOTFOUND;
390 /* We will use the name to tag the GPIO */
391 name = fdt_string(blob, fdt32_to_cpu(prop->nameoff));
392 cell = (u32 *)prop->data;
393 len /= sizeof(u32) * 3; /* 3 cells per GPIO record */
394 if (len > max_count) {
395 debug("FDT: %s: too many GPIOs / cells for "
396 "property '%s'\n", __func__, prop_name);
397 return -FDT_ERR_BADLAYOUT;
400 /* Read out the GPIO data from the cells */
401 for (i = 0; i < len; i++, cell += 3) {
402 gpio[i].gpio = fdt32_to_cpu(cell[1]);
403 gpio[i].flags = fdt32_to_cpu(cell[2]);
410 int fdtdec_decode_gpio(const void *blob, int node, const char *prop_name,
411 struct fdt_gpio_state *gpio)
415 debug("%s: %s\n", __func__, prop_name);
416 gpio->gpio = FDT_GPIO_NONE;
418 err = fdtdec_decode_gpios(blob, node, prop_name, gpio, 1);
419 return err == 1 ? 0 : err;
422 int fdtdec_setup_gpio(struct fdt_gpio_state *gpio)
425 * Return success if there is no GPIO defined. This is used for
428 if (!fdt_gpio_isvalid(gpio))
431 if (gpio_request(gpio->gpio, gpio->name))