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>"),
42 const char *fdtdec_get_compatible(enum fdt_compat_id id)
44 /* We allow reading of the 'unknown' ID for testing purposes */
45 assert(id >= 0 && id < COMPAT_COUNT);
46 return compat_names[id];
50 * Look in the FDT for an alias with the given name and return its node.
52 * @param blob FDT blob
53 * @param name alias name to look up
54 * @return node offset if found, or an error code < 0 otherwise
56 static int find_alias_node(const void *blob, const char *name)
61 debug("find_alias_node: %s\n", name);
62 alias_node = fdt_path_offset(blob, "/aliases");
65 path = fdt_getprop(blob, alias_node, name, NULL);
67 return -FDT_ERR_NOTFOUND;
68 return fdt_path_offset(blob, path);
71 fdt_addr_t fdtdec_get_addr(const void *blob, int node,
72 const char *prop_name)
74 const fdt_addr_t *cell;
77 debug("get_addr: %s\n", prop_name);
78 cell = fdt_getprop(blob, node, prop_name, &len);
79 if (cell && (len == sizeof(fdt_addr_t) ||
80 len == sizeof(fdt_addr_t) * 2))
81 return fdt_addr_to_cpu(*cell);
82 return FDT_ADDR_T_NONE;
85 s32 fdtdec_get_int(const void *blob, int node, const char *prop_name,
91 debug("get_size: %s\n", prop_name);
92 cell = fdt_getprop(blob, node, prop_name, &len);
93 if (cell && len >= sizeof(s32))
94 return fdt32_to_cpu(cell[0]);
98 int fdtdec_get_is_enabled(const void *blob, int node)
103 * It should say "okay", so only allow that. Some fdts use "ok" but
104 * this is a bug. Please fix your device tree source file. See here
107 * http://www.mail-archive.com/u-boot@lists.denx.de/msg71598.html
109 cell = fdt_getprop(blob, node, "status", NULL);
111 return 0 == strcmp(cell, "okay");
115 enum fdt_compat_id fd_dec_lookup(const void *blob, int node)
117 enum fdt_compat_id id;
119 /* Search our drivers */
120 for (id = COMPAT_UNKNOWN; id < COMPAT_COUNT; id++)
121 if (0 == fdt_node_check_compatible(blob, node,
124 return COMPAT_UNKNOWN;
127 int fdtdec_next_compatible(const void *blob, int node,
128 enum fdt_compat_id id)
130 return fdt_node_offset_by_compatible(blob, node, compat_names[id]);
133 int fdtdec_next_alias(const void *blob, const char *name,
134 enum fdt_compat_id id, int *upto)
136 #define MAX_STR_LEN 20
137 char str[MAX_STR_LEN + 20];
140 /* snprintf() is not available */
141 assert(strlen(name) < MAX_STR_LEN);
142 sprintf(str, "%.*s%d", MAX_STR_LEN, name, *upto);
143 node = find_alias_node(blob, str);
146 err = fdt_node_check_compatible(blob, node, compat_names[id]);
150 return -FDT_ERR_NOTFOUND;
155 /* TODO: Can we tighten this code up a little? */
156 int fdtdec_find_aliases_for_id(const void *blob, const char *name,
157 enum fdt_compat_id id, int *node_list, int maxcount)
159 int name_len = strlen(name);
167 /* find the alias node if present */
168 alias_node = fdt_path_offset(blob, "/aliases");
171 * start with nothing, and we can assume that the root node can't
174 memset(nodes, '\0', sizeof(nodes));
176 /* First find all the compatible nodes */
177 for (node = count = 0; node >= 0 && count < maxcount;) {
178 node = fdtdec_next_compatible(blob, node, id);
180 nodes[count++] = node;
183 debug("%s: warning: maxcount exceeded with alias '%s'\n",
186 /* Now find all the aliases */
187 memset(node_list, '\0', sizeof(*node_list) * maxcount);
189 for (offset = fdt_first_property_offset(blob, alias_node);
191 offset = fdt_next_property_offset(blob, offset)) {
192 const struct fdt_property *prop;
198 prop = fdt_get_property_by_offset(blob, offset, NULL);
199 path = fdt_string(blob, fdt32_to_cpu(prop->nameoff));
200 if (prop->len && 0 == strncmp(path, name, name_len))
201 node = fdt_path_offset(blob, prop->data);
205 /* Get the alias number */
206 number = simple_strtoul(path + name_len, NULL, 10);
207 if (number < 0 || number >= maxcount) {
208 debug("%s: warning: alias '%s' is out of range\n",
213 /* Make sure the node we found is actually in our list! */
215 for (j = 0; j < count; j++)
216 if (nodes[j] == node) {
222 debug("%s: warning: alias '%s' points to a node "
223 "'%s' that is missing or is not compatible "
224 " with '%s'\n", __func__, path,
225 fdt_get_name(blob, node, NULL),
231 * Add this node to our list in the right place, and mark
234 if (fdtdec_get_is_enabled(blob, node)) {
235 node_list[number] = node;
236 if (number >= num_found)
237 num_found = number + 1;
242 /* Add any nodes not mentioned by an alias */
243 for (i = j = 0; i < maxcount; i++) {
245 for (; j < maxcount; j++)
247 fdtdec_get_is_enabled(blob, nodes[j]))
250 /* Have we run out of nodes to add? */
254 assert(!node_list[i]);
255 node_list[i] = nodes[j++];
265 * This function is a little odd in that it accesses global data. At some
266 * point if the architecture board.c files merge this will make more sense.
267 * Even now, it is common code.
269 int fdtdec_check_fdt(void)
271 /* We must have an fdt */
272 if (((uintptr_t)gd->fdt_blob & 3) || fdt_check_header(gd->fdt_blob))
273 panic("No valid fdt found - please append one to U-Boot\n"
274 "binary or define CONFIG_OF_EMBED\n");
278 int fdtdec_lookup_phandle(const void *blob, int node, const char *prop_name)
283 phandle = fdt_getprop(blob, node, prop_name, NULL);
285 return -FDT_ERR_NOTFOUND;
287 lookup = fdt_node_offset_by_phandle(blob, fdt32_to_cpu(*phandle));
292 * Look up a property in a node and check that it has a minimum length.
294 * @param blob FDT blob
295 * @param node node to examine
296 * @param prop_name name of property to find
297 * @param min_len minimum property length in bytes
298 * @param err 0 if ok, or -FDT_ERR_NOTFOUND if the property is not
299 found, or -FDT_ERR_BADLAYOUT if not enough data
300 * @return pointer to cell, which is only valid if err == 0
302 static const void *get_prop_check_min_len(const void *blob, int node,
303 const char *prop_name, int min_len, int *err)
308 debug("%s: %s\n", __func__, prop_name);
309 cell = fdt_getprop(blob, node, prop_name, &len);
311 *err = -FDT_ERR_NOTFOUND;
312 else if (len < min_len)
313 *err = -FDT_ERR_BADLAYOUT;
319 int fdtdec_get_int_array(const void *blob, int node, const char *prop_name,
320 u32 *array, int count)
325 debug("%s: %s\n", __func__, prop_name);
326 cell = get_prop_check_min_len(blob, node, prop_name,
327 sizeof(u32) * count, &err);
329 for (i = 0; i < count; i++)
330 array[i] = fdt32_to_cpu(cell[i]);
335 int fdtdec_get_bool(const void *blob, int node, const char *prop_name)
340 debug("%s: %s\n", __func__, prop_name);
341 cell = fdt_getprop(blob, node, prop_name, &len);
346 * Decode a list of GPIOs from an FDT. This creates a list of GPIOs with no
349 * @param blob FDT blob to use
350 * @param node Node to look at
351 * @param prop_name Node property name
352 * @param gpio Array of gpio elements to fill from FDT. This will be
353 * untouched if either 0 or an error is returned
354 * @param max_count Maximum number of elements allowed
355 * @return number of GPIOs read if ok, -FDT_ERR_BADLAYOUT if max_count would
356 * be exceeded, or -FDT_ERR_NOTFOUND if the property is missing.
358 static int fdtdec_decode_gpios(const void *blob, int node,
359 const char *prop_name, struct fdt_gpio_state *gpio,
362 const struct fdt_property *prop;
367 debug("%s: %s\n", __func__, prop_name);
368 assert(max_count > 0);
369 prop = fdt_get_property(blob, node, prop_name, &len);
371 debug("FDT: %s: property '%s' missing\n", __func__, prop_name);
372 return -FDT_ERR_NOTFOUND;
375 /* We will use the name to tag the GPIO */
376 name = fdt_string(blob, fdt32_to_cpu(prop->nameoff));
377 cell = (u32 *)prop->data;
378 len /= sizeof(u32) * 3; /* 3 cells per GPIO record */
379 if (len > max_count) {
380 debug("FDT: %s: too many GPIOs / cells for "
381 "property '%s'\n", __func__, prop_name);
382 return -FDT_ERR_BADLAYOUT;
385 /* Read out the GPIO data from the cells */
386 for (i = 0; i < len; i++, cell += 3) {
387 gpio[i].gpio = fdt32_to_cpu(cell[1]);
388 gpio[i].flags = fdt32_to_cpu(cell[2]);
395 int fdtdec_decode_gpio(const void *blob, int node, const char *prop_name,
396 struct fdt_gpio_state *gpio)
400 debug("%s: %s\n", __func__, prop_name);
401 gpio->gpio = FDT_GPIO_NONE;
403 err = fdtdec_decode_gpios(blob, node, prop_name, gpio, 1);
404 return err == 1 ? 0 : err;
407 int fdtdec_setup_gpio(struct fdt_gpio_state *gpio)
410 * Return success if there is no GPIO defined. This is used for
413 if (!fdt_gpio_isvalid(gpio))
416 if (gpio_request(gpio->gpio, gpio->name))