]> git.kernelconcepts.de Git - karo-tx-uboot.git/blob - lib/fdtdec.c
fdt: Add function to return next compatible subnode
[karo-tx-uboot.git] / lib / fdtdec.c
1 /*
2  * Copyright (c) 2011 The Chromium OS Authors.
3  * See file CREDITS for list of people who contributed to this
4  * project.
5  *
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.
10  *
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.
15  *
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,
19  * MA 02111-1307 USA
20  */
21
22 #include <common.h>
23 #include <serial.h>
24 #include <libfdt.h>
25 #include <fdtdec.h>
26
27 /* we need the generic GPIO interface here */
28 #include <asm-generic/gpio.h>
29
30 DECLARE_GLOBAL_DATA_PTR;
31
32 /*
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.
36  */
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 };
44
45 const char *fdtdec_get_compatible(enum fdt_compat_id id)
46 {
47         /* We allow reading of the 'unknown' ID for testing purposes */
48         assert(id >= 0 && id < COMPAT_COUNT);
49         return compat_names[id];
50 }
51
52 /**
53  * Look in the FDT for an alias with the given name and return its node.
54  *
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
58  */
59 static int find_alias_node(const void *blob, const char *name)
60 {
61         const char *path;
62         int alias_node;
63
64         debug("find_alias_node: %s\n", name);
65         alias_node = fdt_path_offset(blob, "/aliases");
66         if (alias_node < 0)
67                 return alias_node;
68         path = fdt_getprop(blob, alias_node, name, NULL);
69         if (!path)
70                 return -FDT_ERR_NOTFOUND;
71         return fdt_path_offset(blob, path);
72 }
73
74 fdt_addr_t fdtdec_get_addr(const void *blob, int node,
75                 const char *prop_name)
76 {
77         const fdt_addr_t *cell;
78         int len;
79
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;
86 }
87
88 s32 fdtdec_get_int(const void *blob, int node, const char *prop_name,
89                 s32 default_val)
90 {
91         const s32 *cell;
92         int len;
93
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]);
98         return default_val;
99 }
100
101 int fdtdec_get_is_enabled(const void *blob, int node)
102 {
103         const char *cell;
104
105         /*
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
108          * for discussion:
109          *
110          * http://www.mail-archive.com/u-boot@lists.denx.de/msg71598.html
111          */
112         cell = fdt_getprop(blob, node, "status", NULL);
113         if (cell)
114                 return 0 == strcmp(cell, "okay");
115         return 1;
116 }
117
118 enum fdt_compat_id fd_dec_lookup(const void *blob, int node)
119 {
120         enum fdt_compat_id id;
121
122         /* Search our drivers */
123         for (id = COMPAT_UNKNOWN; id < COMPAT_COUNT; id++)
124                 if (0 == fdt_node_check_compatible(blob, node,
125                                 compat_names[id]))
126                         return id;
127         return COMPAT_UNKNOWN;
128 }
129
130 int fdtdec_next_compatible(const void *blob, int node,
131                 enum fdt_compat_id id)
132 {
133         return fdt_node_offset_by_compatible(blob, node, compat_names[id]);
134 }
135
136 int fdtdec_next_compatible_subnode(const void *blob, int node,
137                 enum fdt_compat_id id, int *depthp)
138 {
139         do {
140                 node = fdt_next_node(blob, node, depthp);
141         } while (*depthp > 1);
142
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]))
146                 return node;
147
148         return -FDT_ERR_NOTFOUND;
149 }
150
151 int fdtdec_next_alias(const void *blob, const char *name,
152                 enum fdt_compat_id id, int *upto)
153 {
154 #define MAX_STR_LEN 20
155         char str[MAX_STR_LEN + 20];
156         int node, err;
157
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);
162         if (node < 0)
163                 return node;
164         err = fdt_node_check_compatible(blob, node, compat_names[id]);
165         if (err < 0)
166                 return err;
167         if (err)
168                 return -FDT_ERR_NOTFOUND;
169         (*upto)++;
170         return node;
171 }
172
173 int fdtdec_find_aliases_for_id(const void *blob, const char *name,
174                         enum fdt_compat_id id, int *node_list, int maxcount)
175 {
176         memset(node_list, '\0', sizeof(*node_list) * maxcount);
177
178         return fdtdec_add_aliases_for_id(blob, name, id, node_list, maxcount);
179 }
180
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)
184 {
185         int name_len = strlen(name);
186         int nodes[maxcount];
187         int num_found = 0;
188         int offset, node;
189         int alias_node;
190         int count;
191         int i, j;
192
193         /* find the alias node if present */
194         alias_node = fdt_path_offset(blob, "/aliases");
195
196         /*
197          * start with nothing, and we can assume that the root node can't
198          * match
199          */
200         memset(nodes, '\0', sizeof(nodes));
201
202         /* First find all the compatible nodes */
203         for (node = count = 0; node >= 0 && count < maxcount;) {
204                 node = fdtdec_next_compatible(blob, node, id);
205                 if (node >= 0)
206                         nodes[count++] = node;
207         }
208         if (node >= 0)
209                 debug("%s: warning: maxcount exceeded with alias '%s'\n",
210                        __func__, name);
211
212         /* Now find all the aliases */
213         for (offset = fdt_first_property_offset(blob, alias_node);
214                         offset > 0;
215                         offset = fdt_next_property_offset(blob, offset)) {
216                 const struct fdt_property *prop;
217                 const char *path;
218                 int number;
219                 int found;
220
221                 node = 0;
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);
226                 if (node <= 0)
227                         continue;
228
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",
233                                __func__, path);
234                         continue;
235                 }
236
237                 /* Make sure the node we found is actually in our list! */
238                 found = -1;
239                 for (j = 0; j < count; j++)
240                         if (nodes[j] == node) {
241                                 found = j;
242                                 break;
243                         }
244
245                 if (found == -1) {
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),
250                                compat_names[id]);
251                         continue;
252                 }
253
254                 /*
255                  * Add this node to our list in the right place, and mark
256                  * it as done.
257                  */
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));
265                                 continue;
266                         }
267                         node_list[number] = node;
268                         if (number >= num_found)
269                                 num_found = number + 1;
270                 }
271                 nodes[found] = 0;
272         }
273
274         /* Add any nodes not mentioned by an alias */
275         for (i = j = 0; i < maxcount; i++) {
276                 if (!node_list[i]) {
277                         for (; j < maxcount; j++)
278                                 if (nodes[j] &&
279                                         fdtdec_get_is_enabled(blob, nodes[j]))
280                                         break;
281
282                         /* Have we run out of nodes to add? */
283                         if (j == maxcount)
284                                 break;
285
286                         assert(!node_list[i]);
287                         node_list[i] = nodes[j++];
288                         if (i >= num_found)
289                                 num_found = i + 1;
290                 }
291         }
292
293         return num_found;
294 }
295
296 int fdtdec_check_fdt(void)
297 {
298         /*
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.
303          */
304         assert(!fdtdec_prepare_fdt());
305         return 0;
306 }
307
308 /*
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.
312  */
313 int fdtdec_prepare_fdt(void)
314 {
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");
319                 return -1;
320         }
321         return 0;
322 }
323
324 int fdtdec_lookup_phandle(const void *blob, int node, const char *prop_name)
325 {
326         const u32 *phandle;
327         int lookup;
328
329         phandle = fdt_getprop(blob, node, prop_name, NULL);
330         if (!phandle)
331                 return -FDT_ERR_NOTFOUND;
332
333         lookup = fdt_node_offset_by_phandle(blob, fdt32_to_cpu(*phandle));
334         return lookup;
335 }
336
337 /**
338  * Look up a property in a node and check that it has a minimum length.
339  *
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
347  */
348 static const void *get_prop_check_min_len(const void *blob, int node,
349                 const char *prop_name, int min_len, int *err)
350 {
351         const void *cell;
352         int len;
353
354         debug("%s: %s\n", __func__, prop_name);
355         cell = fdt_getprop(blob, node, prop_name, &len);
356         if (!cell)
357                 *err = -FDT_ERR_NOTFOUND;
358         else if (len < min_len)
359                 *err = -FDT_ERR_BADLAYOUT;
360         else
361                 *err = 0;
362         return cell;
363 }
364
365 int fdtdec_get_int_array(const void *blob, int node, const char *prop_name,
366                 u32 *array, int count)
367 {
368         const u32 *cell;
369         int i, err = 0;
370
371         debug("%s: %s\n", __func__, prop_name);
372         cell = get_prop_check_min_len(blob, node, prop_name,
373                                       sizeof(u32) * count, &err);
374         if (!err) {
375                 for (i = 0; i < count; i++)
376                         array[i] = fdt32_to_cpu(cell[i]);
377         }
378         return err;
379 }
380
381 const u32 *fdtdec_locate_array(const void *blob, int node,
382                                const char *prop_name, int count)
383 {
384         const u32 *cell;
385         int err;
386
387         cell = get_prop_check_min_len(blob, node, prop_name,
388                                       sizeof(u32) * count, &err);
389         return err ? NULL : cell;
390 }
391
392 int fdtdec_get_bool(const void *blob, int node, const char *prop_name)
393 {
394         const s32 *cell;
395         int len;
396
397         debug("%s: %s\n", __func__, prop_name);
398         cell = fdt_getprop(blob, node, prop_name, &len);
399         return cell != NULL;
400 }
401
402 /**
403  * Decode a list of GPIOs from an FDT. This creates a list of GPIOs with no
404  * terminating item.
405  *
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.
414  */
415 static int fdtdec_decode_gpios(const void *blob, int node,
416                 const char *prop_name, struct fdt_gpio_state *gpio,
417                 int max_count)
418 {
419         const struct fdt_property *prop;
420         const u32 *cell;
421         const char *name;
422         int len, i;
423
424         debug("%s: %s\n", __func__, prop_name);
425         assert(max_count > 0);
426         prop = fdt_get_property(blob, node, prop_name, &len);
427         if (!prop) {
428                 debug("FDT: %s: property '%s' missing\n", __func__, prop_name);
429                 return -FDT_ERR_NOTFOUND;
430         }
431
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;
440         }
441
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]);
446                 gpio[i].name = name;
447         }
448
449         return len;
450 }
451
452 int fdtdec_decode_gpio(const void *blob, int node, const char *prop_name,
453                 struct fdt_gpio_state *gpio)
454 {
455         int err;
456
457         debug("%s: %s\n", __func__, prop_name);
458         gpio->gpio = FDT_GPIO_NONE;
459         gpio->name = NULL;
460         err = fdtdec_decode_gpios(blob, node, prop_name, gpio, 1);
461         return err == 1 ? 0 : err;
462 }
463
464 int fdtdec_setup_gpio(struct fdt_gpio_state *gpio)
465 {
466         /*
467          * Return success if there is no GPIO defined. This is used for
468          * optional GPIOs)
469          */
470         if (!fdt_gpio_isvalid(gpio))
471                 return 0;
472
473         if (gpio_request(gpio->gpio, gpio->name))
474                 return -1;
475         return 0;
476 }