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_alias(const void *blob, const char *name,
137 enum fdt_compat_id id, int *upto)
139 #define MAX_STR_LEN 20
140 char str[MAX_STR_LEN + 20];
143 /* snprintf() is not available */
144 assert(strlen(name) < MAX_STR_LEN);
145 sprintf(str, "%.*s%d", MAX_STR_LEN, name, *upto);
146 node = find_alias_node(blob, str);
149 err = fdt_node_check_compatible(blob, node, compat_names[id]);
153 return -FDT_ERR_NOTFOUND;
158 int fdtdec_find_aliases_for_id(const void *blob, const char *name,
159 enum fdt_compat_id id, int *node_list, int maxcount)
161 memset(node_list, '\0', sizeof(*node_list) * maxcount);
163 return fdtdec_add_aliases_for_id(blob, name, id, node_list, maxcount);
166 /* TODO: Can we tighten this code up a little? */
167 int fdtdec_add_aliases_for_id(const void *blob, const char *name,
168 enum fdt_compat_id id, int *node_list, int maxcount)
170 int name_len = strlen(name);
178 /* find the alias node if present */
179 alias_node = fdt_path_offset(blob, "/aliases");
182 * start with nothing, and we can assume that the root node can't
185 memset(nodes, '\0', sizeof(nodes));
187 /* First find all the compatible nodes */
188 for (node = count = 0; node >= 0 && count < maxcount;) {
189 node = fdtdec_next_compatible(blob, node, id);
191 nodes[count++] = node;
194 debug("%s: warning: maxcount exceeded with alias '%s'\n",
197 /* Now find all the aliases */
198 for (offset = fdt_first_property_offset(blob, alias_node);
200 offset = fdt_next_property_offset(blob, offset)) {
201 const struct fdt_property *prop;
207 prop = fdt_get_property_by_offset(blob, offset, NULL);
208 path = fdt_string(blob, fdt32_to_cpu(prop->nameoff));
209 if (prop->len && 0 == strncmp(path, name, name_len))
210 node = fdt_path_offset(blob, prop->data);
214 /* Get the alias number */
215 number = simple_strtoul(path + name_len, NULL, 10);
216 if (number < 0 || number >= maxcount) {
217 debug("%s: warning: alias '%s' is out of range\n",
222 /* Make sure the node we found is actually in our list! */
224 for (j = 0; j < count; j++)
225 if (nodes[j] == node) {
231 debug("%s: warning: alias '%s' points to a node "
232 "'%s' that is missing or is not compatible "
233 " with '%s'\n", __func__, path,
234 fdt_get_name(blob, node, NULL),
240 * Add this node to our list in the right place, and mark
243 if (fdtdec_get_is_enabled(blob, node)) {
244 if (node_list[number]) {
245 debug("%s: warning: alias '%s' requires that "
246 "a node be placed in the list in a "
247 "position which is already filled by "
248 "node '%s'\n", __func__, path,
249 fdt_get_name(blob, node, NULL));
252 node_list[number] = node;
253 if (number >= num_found)
254 num_found = number + 1;
259 /* Add any nodes not mentioned by an alias */
260 for (i = j = 0; i < maxcount; i++) {
262 for (; j < maxcount; j++)
264 fdtdec_get_is_enabled(blob, nodes[j]))
267 /* Have we run out of nodes to add? */
271 assert(!node_list[i]);
272 node_list[i] = nodes[j++];
281 int fdtdec_check_fdt(void)
284 * We must have an FDT, but we cannot panic() yet since the console
285 * is not ready. So for now, just assert(). Boards which need an early
286 * FDT (prior to console ready) will need to make their own
287 * arrangements and do their own checks.
289 assert(!fdtdec_prepare_fdt());
294 * This function is a little odd in that it accesses global data. At some
295 * point if the architecture board.c files merge this will make more sense.
296 * Even now, it is common code.
298 int fdtdec_prepare_fdt(void)
300 if (((uintptr_t)gd->fdt_blob & 3) || fdt_check_header(gd->fdt_blob)) {
301 printf("No valid FDT found - please append one to U-Boot "
302 "binary, use u-boot-dtb.bin or define "
303 "CONFIG_OF_EMBED\n");
309 int fdtdec_lookup_phandle(const void *blob, int node, const char *prop_name)
314 phandle = fdt_getprop(blob, node, prop_name, NULL);
316 return -FDT_ERR_NOTFOUND;
318 lookup = fdt_node_offset_by_phandle(blob, fdt32_to_cpu(*phandle));
323 * Look up a property in a node and check that it has a minimum length.
325 * @param blob FDT blob
326 * @param node node to examine
327 * @param prop_name name of property to find
328 * @param min_len minimum property length in bytes
329 * @param err 0 if ok, or -FDT_ERR_NOTFOUND if the property is not
330 found, or -FDT_ERR_BADLAYOUT if not enough data
331 * @return pointer to cell, which is only valid if err == 0
333 static const void *get_prop_check_min_len(const void *blob, int node,
334 const char *prop_name, int min_len, int *err)
339 debug("%s: %s\n", __func__, prop_name);
340 cell = fdt_getprop(blob, node, prop_name, &len);
342 *err = -FDT_ERR_NOTFOUND;
343 else if (len < min_len)
344 *err = -FDT_ERR_BADLAYOUT;
350 int fdtdec_get_int_array(const void *blob, int node, const char *prop_name,
351 u32 *array, int count)
356 debug("%s: %s\n", __func__, prop_name);
357 cell = get_prop_check_min_len(blob, node, prop_name,
358 sizeof(u32) * count, &err);
360 for (i = 0; i < count; i++)
361 array[i] = fdt32_to_cpu(cell[i]);
366 int fdtdec_get_bool(const void *blob, int node, const char *prop_name)
371 debug("%s: %s\n", __func__, prop_name);
372 cell = fdt_getprop(blob, node, prop_name, &len);
377 * Decode a list of GPIOs from an FDT. This creates a list of GPIOs with no
380 * @param blob FDT blob to use
381 * @param node Node to look at
382 * @param prop_name Node property name
383 * @param gpio Array of gpio elements to fill from FDT. This will be
384 * untouched if either 0 or an error is returned
385 * @param max_count Maximum number of elements allowed
386 * @return number of GPIOs read if ok, -FDT_ERR_BADLAYOUT if max_count would
387 * be exceeded, or -FDT_ERR_NOTFOUND if the property is missing.
389 static int fdtdec_decode_gpios(const void *blob, int node,
390 const char *prop_name, struct fdt_gpio_state *gpio,
393 const struct fdt_property *prop;
398 debug("%s: %s\n", __func__, prop_name);
399 assert(max_count > 0);
400 prop = fdt_get_property(blob, node, prop_name, &len);
402 debug("FDT: %s: property '%s' missing\n", __func__, prop_name);
403 return -FDT_ERR_NOTFOUND;
406 /* We will use the name to tag the GPIO */
407 name = fdt_string(blob, fdt32_to_cpu(prop->nameoff));
408 cell = (u32 *)prop->data;
409 len /= sizeof(u32) * 3; /* 3 cells per GPIO record */
410 if (len > max_count) {
411 debug("FDT: %s: too many GPIOs / cells for "
412 "property '%s'\n", __func__, prop_name);
413 return -FDT_ERR_BADLAYOUT;
416 /* Read out the GPIO data from the cells */
417 for (i = 0; i < len; i++, cell += 3) {
418 gpio[i].gpio = fdt32_to_cpu(cell[1]);
419 gpio[i].flags = fdt32_to_cpu(cell[2]);
426 int fdtdec_decode_gpio(const void *blob, int node, const char *prop_name,
427 struct fdt_gpio_state *gpio)
431 debug("%s: %s\n", __func__, prop_name);
432 gpio->gpio = FDT_GPIO_NONE;
434 err = fdtdec_decode_gpios(blob, node, prop_name, gpio, 1);
435 return err == 1 ? 0 : err;
438 int fdtdec_setup_gpio(struct fdt_gpio_state *gpio)
441 * Return success if there is no GPIO defined. This is used for
444 if (!fdt_gpio_isvalid(gpio))
447 if (gpio_request(gpio->gpio, gpio->name))