blob: bdec1a0d962b1ce256b60f65e12774a42dde376c [file] [log] [blame]
Simon Glassb5220bc2011-10-24 19:15:32 +00001/*
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
Simon Glassed3ee5c2012-02-27 10:52:36 +000027/* we need the generic GPIO interface here */
28#include <asm-generic/gpio.h>
29
Simon Glassb5220bc2011-10-24 19:15:32 +000030DECLARE_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
38static const char * const compat_names[COMPAT_COUNT] = {
Simon Glassf88fe2d2012-02-27 10:52:34 +000039 COMPAT(UNKNOWN, "<none>"),
Simon Glass87f938c2012-02-27 10:52:49 +000040 COMPAT(NVIDIA_TEGRA20_USB, "nvidia,tegra20-ehci"),
Yen Lin96a78ac2012-03-06 19:00:23 +000041 COMPAT(NVIDIA_TEGRA20_I2C, "nvidia,tegra20-i2c"),
42 COMPAT(NVIDIA_TEGRA20_DVC, "nvidia,tegra20-i2c-dvc"),
Simon Glassb5220bc2011-10-24 19:15:32 +000043};
44
Simon Glassa53f4a22012-01-17 08:20:50 +000045const 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
Simon Glassb5220bc2011-10-24 19:15:32 +000052/**
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 */
59static 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
74fdt_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
88s32 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
Simon Glassf88fe2d2012-02-27 10:52:34 +0000101int fdtdec_get_is_enabled(const void *blob, int node)
Simon Glassb5220bc2011-10-24 19:15:32 +0000102{
103 const char *cell;
104
Simon Glassf88fe2d2012-02-27 10:52:34 +0000105 /*
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 */
Simon Glassb5220bc2011-10-24 19:15:32 +0000112 cell = fdt_getprop(blob, node, "status", NULL);
113 if (cell)
Simon Glassf88fe2d2012-02-27 10:52:34 +0000114 return 0 == strcmp(cell, "okay");
115 return 1;
Simon Glassb5220bc2011-10-24 19:15:32 +0000116}
117
118enum 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
130int 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
136int fdtdec_next_alias(const void *blob, const char *name,
137 enum fdt_compat_id id, int *upto)
138{
139#define MAX_STR_LEN 20
140 char str[MAX_STR_LEN + 20];
141 int node, err;
142
143 /* snprintf() is not available */
144 assert(strlen(name) < MAX_STR_LEN);
145 sprintf(str, "%.*s%d", MAX_STR_LEN, name, *upto);
Simon Glassb5220bc2011-10-24 19:15:32 +0000146 node = find_alias_node(blob, str);
147 if (node < 0)
148 return node;
149 err = fdt_node_check_compatible(blob, node, compat_names[id]);
150 if (err < 0)
151 return err;
Simon Glassf88fe2d2012-02-27 10:52:34 +0000152 if (err)
153 return -FDT_ERR_NOTFOUND;
154 (*upto)++;
155 return node;
Simon Glassb5220bc2011-10-24 19:15:32 +0000156}
157
Simon Glassa53f4a22012-01-17 08:20:50 +0000158int fdtdec_find_aliases_for_id(const void *blob, const char *name,
159 enum fdt_compat_id id, int *node_list, int maxcount)
160{
Simon Glassc6782272012-02-03 15:13:53 +0000161 memset(node_list, '\0', sizeof(*node_list) * maxcount);
162
163 return fdtdec_add_aliases_for_id(blob, name, id, node_list, maxcount);
164}
165
166/* TODO: Can we tighten this code up a little? */
167int fdtdec_add_aliases_for_id(const void *blob, const char *name,
168 enum fdt_compat_id id, int *node_list, int maxcount)
169{
Simon Glassa53f4a22012-01-17 08:20:50 +0000170 int name_len = strlen(name);
171 int nodes[maxcount];
172 int num_found = 0;
173 int offset, node;
174 int alias_node;
175 int count;
176 int i, j;
177
178 /* find the alias node if present */
179 alias_node = fdt_path_offset(blob, "/aliases");
180
181 /*
182 * start with nothing, and we can assume that the root node can't
183 * match
184 */
185 memset(nodes, '\0', sizeof(nodes));
186
187 /* First find all the compatible nodes */
188 for (node = count = 0; node >= 0 && count < maxcount;) {
189 node = fdtdec_next_compatible(blob, node, id);
190 if (node >= 0)
191 nodes[count++] = node;
192 }
193 if (node >= 0)
194 debug("%s: warning: maxcount exceeded with alias '%s'\n",
195 __func__, name);
196
197 /* Now find all the aliases */
Simon Glassa53f4a22012-01-17 08:20:50 +0000198 for (offset = fdt_first_property_offset(blob, alias_node);
199 offset > 0;
200 offset = fdt_next_property_offset(blob, offset)) {
201 const struct fdt_property *prop;
202 const char *path;
203 int number;
204 int found;
205
206 node = 0;
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);
211 if (node <= 0)
212 continue;
213
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",
218 __func__, path);
219 continue;
220 }
221
222 /* Make sure the node we found is actually in our list! */
223 found = -1;
224 for (j = 0; j < count; j++)
225 if (nodes[j] == node) {
226 found = j;
227 break;
228 }
229
230 if (found == -1) {
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),
235 compat_names[id]);
236 continue;
237 }
238
239 /*
240 * Add this node to our list in the right place, and mark
241 * it as done.
242 */
243 if (fdtdec_get_is_enabled(blob, node)) {
Simon Glassc6782272012-02-03 15:13:53 +0000244 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));
250 continue;
251 }
Simon Glassa53f4a22012-01-17 08:20:50 +0000252 node_list[number] = node;
253 if (number >= num_found)
254 num_found = number + 1;
255 }
Simon Glassc6782272012-02-03 15:13:53 +0000256 nodes[found] = 0;
Simon Glassa53f4a22012-01-17 08:20:50 +0000257 }
258
259 /* Add any nodes not mentioned by an alias */
260 for (i = j = 0; i < maxcount; i++) {
261 if (!node_list[i]) {
262 for (; j < maxcount; j++)
263 if (nodes[j] &&
264 fdtdec_get_is_enabled(blob, nodes[j]))
265 break;
266
267 /* Have we run out of nodes to add? */
268 if (j == maxcount)
269 break;
270
271 assert(!node_list[i]);
272 node_list[i] = nodes[j++];
273 if (i >= num_found)
274 num_found = i + 1;
275 }
276 }
277
278 return num_found;
279}
280
Simon Glass9a263e52012-03-28 10:08:24 +0000281int fdtdec_check_fdt(void)
282{
283 /*
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.
288 */
289 assert(!fdtdec_prepare_fdt());
290 return 0;
291}
292
Simon Glassb5220bc2011-10-24 19:15:32 +0000293/*
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.
297 */
Simon Glass9a263e52012-03-28 10:08:24 +0000298int fdtdec_prepare_fdt(void)
Simon Glassb5220bc2011-10-24 19:15:32 +0000299{
Simon Glass9a263e52012-03-28 10:08:24 +0000300 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");
304 return -1;
305 }
Simon Glassb5220bc2011-10-24 19:15:32 +0000306 return 0;
307}
Simon Glassd17da652012-02-27 10:52:35 +0000308
309int fdtdec_lookup_phandle(const void *blob, int node, const char *prop_name)
310{
311 const u32 *phandle;
312 int lookup;
313
314 phandle = fdt_getprop(blob, node, prop_name, NULL);
315 if (!phandle)
316 return -FDT_ERR_NOTFOUND;
317
318 lookup = fdt_node_offset_by_phandle(blob, fdt32_to_cpu(*phandle));
319 return lookup;
320}
321
322/**
323 * Look up a property in a node and check that it has a minimum length.
324 *
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
332 */
333static const void *get_prop_check_min_len(const void *blob, int node,
334 const char *prop_name, int min_len, int *err)
335{
336 const void *cell;
337 int len;
338
339 debug("%s: %s\n", __func__, prop_name);
340 cell = fdt_getprop(blob, node, prop_name, &len);
341 if (!cell)
342 *err = -FDT_ERR_NOTFOUND;
343 else if (len < min_len)
344 *err = -FDT_ERR_BADLAYOUT;
345 else
346 *err = 0;
347 return cell;
348}
349
350int fdtdec_get_int_array(const void *blob, int node, const char *prop_name,
351 u32 *array, int count)
352{
353 const u32 *cell;
354 int i, err = 0;
355
356 debug("%s: %s\n", __func__, prop_name);
357 cell = get_prop_check_min_len(blob, node, prop_name,
358 sizeof(u32) * count, &err);
359 if (!err) {
360 for (i = 0; i < count; i++)
361 array[i] = fdt32_to_cpu(cell[i]);
362 }
363 return err;
364}
365
366int fdtdec_get_bool(const void *blob, int node, const char *prop_name)
367{
368 const s32 *cell;
369 int len;
370
371 debug("%s: %s\n", __func__, prop_name);
372 cell = fdt_getprop(blob, node, prop_name, &len);
373 return cell != NULL;
374}
Simon Glassed3ee5c2012-02-27 10:52:36 +0000375
376/**
377 * Decode a list of GPIOs from an FDT. This creates a list of GPIOs with no
378 * terminating item.
379 *
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.
388 */
389static int fdtdec_decode_gpios(const void *blob, int node,
390 const char *prop_name, struct fdt_gpio_state *gpio,
391 int max_count)
392{
393 const struct fdt_property *prop;
394 const u32 *cell;
395 const char *name;
396 int len, i;
397
398 debug("%s: %s\n", __func__, prop_name);
399 assert(max_count > 0);
400 prop = fdt_get_property(blob, node, prop_name, &len);
401 if (!prop) {
402 debug("FDT: %s: property '%s' missing\n", __func__, prop_name);
403 return -FDT_ERR_NOTFOUND;
404 }
405
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;
414 }
415
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]);
420 gpio[i].name = name;
421 }
422
423 return len;
424}
425
426int fdtdec_decode_gpio(const void *blob, int node, const char *prop_name,
427 struct fdt_gpio_state *gpio)
428{
429 int err;
430
431 debug("%s: %s\n", __func__, prop_name);
432 gpio->gpio = FDT_GPIO_NONE;
433 gpio->name = NULL;
434 err = fdtdec_decode_gpios(blob, node, prop_name, gpio, 1);
435 return err == 1 ? 0 : err;
436}
437
438int fdtdec_setup_gpio(struct fdt_gpio_state *gpio)
439{
440 /*
441 * Return success if there is no GPIO defined. This is used for
442 * optional GPIOs)
443 */
444 if (!fdt_gpio_isvalid(gpio))
445 return 0;
446
447 if (gpio_request(gpio->gpio, gpio->name))
448 return -1;
449 return 0;
450}