blob: 4ca442a2afd76a6643c4abb2e0861799904a3350 [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"),
Simon Glassb5220bc2011-10-24 19:15:32 +000041};
42
Simon Glassa53f4a22012-01-17 08:20:50 +000043const char *fdtdec_get_compatible(enum fdt_compat_id id)
44{
45 /* We allow reading of the 'unknown' ID for testing purposes */
46 assert(id >= 0 && id < COMPAT_COUNT);
47 return compat_names[id];
48}
49
Simon Glassb5220bc2011-10-24 19:15:32 +000050/**
51 * Look in the FDT for an alias with the given name and return its node.
52 *
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
56 */
57static int find_alias_node(const void *blob, const char *name)
58{
59 const char *path;
60 int alias_node;
61
62 debug("find_alias_node: %s\n", name);
63 alias_node = fdt_path_offset(blob, "/aliases");
64 if (alias_node < 0)
65 return alias_node;
66 path = fdt_getprop(blob, alias_node, name, NULL);
67 if (!path)
68 return -FDT_ERR_NOTFOUND;
69 return fdt_path_offset(blob, path);
70}
71
72fdt_addr_t fdtdec_get_addr(const void *blob, int node,
73 const char *prop_name)
74{
75 const fdt_addr_t *cell;
76 int len;
77
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;
84}
85
86s32 fdtdec_get_int(const void *blob, int node, const char *prop_name,
87 s32 default_val)
88{
89 const s32 *cell;
90 int len;
91
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]);
96 return default_val;
97}
98
Simon Glassf88fe2d2012-02-27 10:52:34 +000099int fdtdec_get_is_enabled(const void *blob, int node)
Simon Glassb5220bc2011-10-24 19:15:32 +0000100{
101 const char *cell;
102
Simon Glassf88fe2d2012-02-27 10:52:34 +0000103 /*
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
106 * for discussion:
107 *
108 * http://www.mail-archive.com/u-boot@lists.denx.de/msg71598.html
109 */
Simon Glassb5220bc2011-10-24 19:15:32 +0000110 cell = fdt_getprop(blob, node, "status", NULL);
111 if (cell)
Simon Glassf88fe2d2012-02-27 10:52:34 +0000112 return 0 == strcmp(cell, "okay");
113 return 1;
Simon Glassb5220bc2011-10-24 19:15:32 +0000114}
115
116enum fdt_compat_id fd_dec_lookup(const void *blob, int node)
117{
118 enum fdt_compat_id id;
119
120 /* Search our drivers */
121 for (id = COMPAT_UNKNOWN; id < COMPAT_COUNT; id++)
122 if (0 == fdt_node_check_compatible(blob, node,
123 compat_names[id]))
124 return id;
125 return COMPAT_UNKNOWN;
126}
127
128int fdtdec_next_compatible(const void *blob, int node,
129 enum fdt_compat_id id)
130{
131 return fdt_node_offset_by_compatible(blob, node, compat_names[id]);
132}
133
134int fdtdec_next_alias(const void *blob, const char *name,
135 enum fdt_compat_id id, int *upto)
136{
137#define MAX_STR_LEN 20
138 char str[MAX_STR_LEN + 20];
139 int node, err;
140
141 /* snprintf() is not available */
142 assert(strlen(name) < MAX_STR_LEN);
143 sprintf(str, "%.*s%d", MAX_STR_LEN, name, *upto);
Simon Glassb5220bc2011-10-24 19:15:32 +0000144 node = find_alias_node(blob, str);
145 if (node < 0)
146 return node;
147 err = fdt_node_check_compatible(blob, node, compat_names[id]);
148 if (err < 0)
149 return err;
Simon Glassf88fe2d2012-02-27 10:52:34 +0000150 if (err)
151 return -FDT_ERR_NOTFOUND;
152 (*upto)++;
153 return node;
Simon Glassb5220bc2011-10-24 19:15:32 +0000154}
155
Simon Glassa53f4a22012-01-17 08:20:50 +0000156/* TODO: Can we tighten this code up a little? */
157int fdtdec_find_aliases_for_id(const void *blob, const char *name,
158 enum fdt_compat_id id, int *node_list, int maxcount)
159{
160 int name_len = strlen(name);
161 int nodes[maxcount];
162 int num_found = 0;
163 int offset, node;
164 int alias_node;
165 int count;
166 int i, j;
167
168 /* find the alias node if present */
169 alias_node = fdt_path_offset(blob, "/aliases");
170
171 /*
172 * start with nothing, and we can assume that the root node can't
173 * match
174 */
175 memset(nodes, '\0', sizeof(nodes));
176
177 /* First find all the compatible nodes */
178 for (node = count = 0; node >= 0 && count < maxcount;) {
179 node = fdtdec_next_compatible(blob, node, id);
180 if (node >= 0)
181 nodes[count++] = node;
182 }
183 if (node >= 0)
184 debug("%s: warning: maxcount exceeded with alias '%s'\n",
185 __func__, name);
186
187 /* Now find all the aliases */
188 memset(node_list, '\0', sizeof(*node_list) * maxcount);
189
190 for (offset = fdt_first_property_offset(blob, alias_node);
191 offset > 0;
192 offset = fdt_next_property_offset(blob, offset)) {
193 const struct fdt_property *prop;
194 const char *path;
195 int number;
196 int found;
197
198 node = 0;
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);
203 if (node <= 0)
204 continue;
205
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",
210 __func__, path);
211 continue;
212 }
213
214 /* Make sure the node we found is actually in our list! */
215 found = -1;
216 for (j = 0; j < count; j++)
217 if (nodes[j] == node) {
218 found = j;
219 break;
220 }
221
222 if (found == -1) {
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),
227 compat_names[id]);
228 continue;
229 }
230
231 /*
232 * Add this node to our list in the right place, and mark
233 * it as done.
234 */
235 if (fdtdec_get_is_enabled(blob, node)) {
236 node_list[number] = node;
237 if (number >= num_found)
238 num_found = number + 1;
239 }
240 nodes[j] = 0;
241 }
242
243 /* Add any nodes not mentioned by an alias */
244 for (i = j = 0; i < maxcount; i++) {
245 if (!node_list[i]) {
246 for (; j < maxcount; j++)
247 if (nodes[j] &&
248 fdtdec_get_is_enabled(blob, nodes[j]))
249 break;
250
251 /* Have we run out of nodes to add? */
252 if (j == maxcount)
253 break;
254
255 assert(!node_list[i]);
256 node_list[i] = nodes[j++];
257 if (i >= num_found)
258 num_found = i + 1;
259 }
260 }
261
262 return num_found;
263}
264
Simon Glassb5220bc2011-10-24 19:15:32 +0000265/*
266 * This function is a little odd in that it accesses global data. At some
267 * point if the architecture board.c files merge this will make more sense.
268 * Even now, it is common code.
269 */
270int fdtdec_check_fdt(void)
271{
272 /* We must have an fdt */
Simon Glassf88fe2d2012-02-27 10:52:34 +0000273 if (((uintptr_t)gd->fdt_blob & 3) || fdt_check_header(gd->fdt_blob))
Simon Glassb5220bc2011-10-24 19:15:32 +0000274 panic("No valid fdt found - please append one to U-Boot\n"
275 "binary or define CONFIG_OF_EMBED\n");
276 return 0;
277}
Simon Glassd17da652012-02-27 10:52:35 +0000278
279int fdtdec_lookup_phandle(const void *blob, int node, const char *prop_name)
280{
281 const u32 *phandle;
282 int lookup;
283
284 phandle = fdt_getprop(blob, node, prop_name, NULL);
285 if (!phandle)
286 return -FDT_ERR_NOTFOUND;
287
288 lookup = fdt_node_offset_by_phandle(blob, fdt32_to_cpu(*phandle));
289 return lookup;
290}
291
292/**
293 * Look up a property in a node and check that it has a minimum length.
294 *
295 * @param blob FDT blob
296 * @param node node to examine
297 * @param prop_name name of property to find
298 * @param min_len minimum property length in bytes
299 * @param err 0 if ok, or -FDT_ERR_NOTFOUND if the property is not
300 found, or -FDT_ERR_BADLAYOUT if not enough data
301 * @return pointer to cell, which is only valid if err == 0
302 */
303static const void *get_prop_check_min_len(const void *blob, int node,
304 const char *prop_name, int min_len, int *err)
305{
306 const void *cell;
307 int len;
308
309 debug("%s: %s\n", __func__, prop_name);
310 cell = fdt_getprop(blob, node, prop_name, &len);
311 if (!cell)
312 *err = -FDT_ERR_NOTFOUND;
313 else if (len < min_len)
314 *err = -FDT_ERR_BADLAYOUT;
315 else
316 *err = 0;
317 return cell;
318}
319
320int fdtdec_get_int_array(const void *blob, int node, const char *prop_name,
321 u32 *array, int count)
322{
323 const u32 *cell;
324 int i, err = 0;
325
326 debug("%s: %s\n", __func__, prop_name);
327 cell = get_prop_check_min_len(blob, node, prop_name,
328 sizeof(u32) * count, &err);
329 if (!err) {
330 for (i = 0; i < count; i++)
331 array[i] = fdt32_to_cpu(cell[i]);
332 }
333 return err;
334}
335
336int fdtdec_get_bool(const void *blob, int node, const char *prop_name)
337{
338 const s32 *cell;
339 int len;
340
341 debug("%s: %s\n", __func__, prop_name);
342 cell = fdt_getprop(blob, node, prop_name, &len);
343 return cell != NULL;
344}
Simon Glassed3ee5c2012-02-27 10:52:36 +0000345
346/**
347 * Decode a list of GPIOs from an FDT. This creates a list of GPIOs with no
348 * terminating item.
349 *
350 * @param blob FDT blob to use
351 * @param node Node to look at
352 * @param prop_name Node property name
353 * @param gpio Array of gpio elements to fill from FDT. This will be
354 * untouched if either 0 or an error is returned
355 * @param max_count Maximum number of elements allowed
356 * @return number of GPIOs read if ok, -FDT_ERR_BADLAYOUT if max_count would
357 * be exceeded, or -FDT_ERR_NOTFOUND if the property is missing.
358 */
359static int fdtdec_decode_gpios(const void *blob, int node,
360 const char *prop_name, struct fdt_gpio_state *gpio,
361 int max_count)
362{
363 const struct fdt_property *prop;
364 const u32 *cell;
365 const char *name;
366 int len, i;
367
368 debug("%s: %s\n", __func__, prop_name);
369 assert(max_count > 0);
370 prop = fdt_get_property(blob, node, prop_name, &len);
371 if (!prop) {
372 debug("FDT: %s: property '%s' missing\n", __func__, prop_name);
373 return -FDT_ERR_NOTFOUND;
374 }
375
376 /* We will use the name to tag the GPIO */
377 name = fdt_string(blob, fdt32_to_cpu(prop->nameoff));
378 cell = (u32 *)prop->data;
379 len /= sizeof(u32) * 3; /* 3 cells per GPIO record */
380 if (len > max_count) {
381 debug("FDT: %s: too many GPIOs / cells for "
382 "property '%s'\n", __func__, prop_name);
383 return -FDT_ERR_BADLAYOUT;
384 }
385
386 /* Read out the GPIO data from the cells */
387 for (i = 0; i < len; i++, cell += 3) {
388 gpio[i].gpio = fdt32_to_cpu(cell[1]);
389 gpio[i].flags = fdt32_to_cpu(cell[2]);
390 gpio[i].name = name;
391 }
392
393 return len;
394}
395
396int fdtdec_decode_gpio(const void *blob, int node, const char *prop_name,
397 struct fdt_gpio_state *gpio)
398{
399 int err;
400
401 debug("%s: %s\n", __func__, prop_name);
402 gpio->gpio = FDT_GPIO_NONE;
403 gpio->name = NULL;
404 err = fdtdec_decode_gpios(blob, node, prop_name, gpio, 1);
405 return err == 1 ? 0 : err;
406}
407
408int fdtdec_setup_gpio(struct fdt_gpio_state *gpio)
409{
410 /*
411 * Return success if there is no GPIO defined. This is used for
412 * optional GPIOs)
413 */
414 if (!fdt_gpio_isvalid(gpio))
415 return 0;
416
417 if (gpio_request(gpio->gpio, gpio->name))
418 return -1;
419 return 0;
420}