blob: 1b3c810588b66a9064bc080da51776c5f8136768 [file] [log] [blame]
Simon Glass2d2af852011-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
Michal Simekbcd35ce2012-07-11 02:26:38 +000027#include <asm/gpio.h>
Simon Glass0d83c4d2012-02-27 10:52:36 +000028
Simon Glass2d2af852011-10-24 19:15:32 +000029DECLARE_GLOBAL_DATA_PTR;
30
31/*
32 * Here are the type we know about. One day we might allow drivers to
33 * register. For now we just put them here. The COMPAT macro allows us to
34 * turn this into a sparse list later, and keeps the ID with the name.
35 */
36#define COMPAT(id, name) name
37static const char * const compat_names[COMPAT_COUNT] = {
Simon Glass65444982012-02-27 10:52:34 +000038 COMPAT(UNKNOWN, "<none>"),
Simon Glass08d6ec22012-02-27 10:52:49 +000039 COMPAT(NVIDIA_TEGRA20_USB, "nvidia,tegra20-ehci"),
Jim Lin2fefb8b2013-06-21 19:05:47 +080040 COMPAT(NVIDIA_TEGRA30_USB, "nvidia,tegra30-ehci"),
41 COMPAT(NVIDIA_TEGRA114_USB, "nvidia,tegra114-ehci"),
Tom Warrenf8cf4b22013-02-08 07:25:30 +000042 COMPAT(NVIDIA_TEGRA114_I2C, "nvidia,tegra114-i2c"),
Yen Linccf7e902012-03-06 19:00:23 +000043 COMPAT(NVIDIA_TEGRA20_I2C, "nvidia,tegra20-i2c"),
44 COMPAT(NVIDIA_TEGRA20_DVC, "nvidia,tegra20-i2c-dvc"),
Jimmy Zhang5983ef82012-04-02 13:18:52 +000045 COMPAT(NVIDIA_TEGRA20_EMC, "nvidia,tegra20-emc"),
46 COMPAT(NVIDIA_TEGRA20_EMC_TABLE, "nvidia,tegra20-emc-table"),
Rakesh Iyer382abbd2012-04-17 09:01:35 +000047 COMPAT(NVIDIA_TEGRA20_KBC, "nvidia,tegra20-kbc"),
Jim Lin5d309e62012-07-29 20:53:29 +000048 COMPAT(NVIDIA_TEGRA20_NAND, "nvidia,tegra20-nand"),
Simon Glass1564f342012-10-17 13:24:49 +000049 COMPAT(NVIDIA_TEGRA20_PWM, "nvidia,tegra20-pwm"),
Wei Ni6555e692012-10-17 13:24:50 +000050 COMPAT(NVIDIA_TEGRA20_DC, "nvidia,tegra20-dc"),
Tom Warren7db0e2d2013-03-04 14:07:18 -070051 COMPAT(NVIDIA_TEGRA30_SDMMC, "nvidia,tegra30-sdhci"),
Tom Warren9745cf82013-02-21 12:31:30 +000052 COMPAT(NVIDIA_TEGRA20_SDMMC, "nvidia,tegra20-sdhci"),
Allen Martine7659522013-01-29 13:51:24 +000053 COMPAT(NVIDIA_TEGRA20_SFLASH, "nvidia,tegra20-sflash"),
Allen Martinba4fb9b2013-01-29 13:51:28 +000054 COMPAT(NVIDIA_TEGRA20_SLINK, "nvidia,tegra20-slink"),
Allen Martinb54f0d92013-03-16 18:58:09 +000055 COMPAT(NVIDIA_TEGRA114_SPI, "nvidia,tegra114-spi"),
Hatim RVbe5ad5a2012-12-11 00:52:46 +000056 COMPAT(SMSC_LAN9215, "smsc,lan9215"),
57 COMPAT(SAMSUNG_EXYNOS5_SROMC, "samsung,exynos-sromc"),
Rajeshwari Shinde2b71e9d2012-12-26 20:03:10 +000058 COMPAT(SAMSUNG_S3C2440_I2C, "samsung,s3c2440-i2c"),
Rajeshwari Shinde9d116732012-12-26 20:03:16 +000059 COMPAT(SAMSUNG_EXYNOS5_SOUND, "samsung,exynos-sound"),
60 COMPAT(WOLFSON_WM8994_CODEC, "wolfson,wm8994-codec"),
Rajeshwari Shinded95beaf2012-12-26 20:03:20 +000061 COMPAT(SAMSUNG_EXYNOS_SPI, "samsung,exynos-spi"),
Hung-ying Tyanc48ca88f2013-05-15 18:27:28 +080062 COMPAT(GOOGLE_CROS_EC, "google,cros-ec"),
Hung-ying Tyan4a48bcf2013-05-15 18:27:32 +080063 COMPAT(GOOGLE_CROS_EC_KEYB, "google,cros-ec-keyb"),
Rajeshwari Shinded6e9f442013-01-07 23:35:05 +000064 COMPAT(SAMSUNG_EXYNOS_EHCI, "samsung,exynos-ehci"),
65 COMPAT(SAMSUNG_EXYNOS_USB_PHY, "samsung,exynos-usb-phy"),
Akshay Saraswataa773182013-02-25 01:13:01 +000066 COMPAT(SAMSUNG_EXYNOS_TMU, "samsung,exynos-tmu"),
Ajay Kumar035a1cd2013-02-21 23:53:00 +000067 COMPAT(SAMSUNG_EXYNOS_FIMD, "samsung,exynos-fimd"),
Ajay Kumar39bac152013-02-21 23:53:05 +000068 COMPAT(SAMSUNG_EXYNOS5_DP, "samsung,exynos5-dp"),
Amar4b538c52013-04-27 11:42:52 +053069 COMPAT(SAMSUNG_EXYNOS5_DWMMC, "samsung,exynos5250-dwmmc"),
Rajeshwari Shindeffff1612013-06-24 16:47:20 +053070 COMPAT(SAMSUNG_EXYNOS_SERIAL, "samsung,exynos4210-uart"),
Rajeshwari Shinde1e9eeb02013-01-08 21:03:38 +000071 COMPAT(MAXIM_MAX77686_PMIC, "maxim,max77686_pmic"),
Simon Glass609560942013-03-11 06:08:08 +000072 COMPAT(GENERIC_SPI_FLASH, "spi-flash"),
Rajeshwari Shinde003c28c2013-02-14 19:46:15 +000073 COMPAT(MAXIM_98095_CODEC, "maxim,max98095-codec"),
Rong Chang8faa9452013-04-12 10:44:57 +000074 COMPAT(INFINEON_SLB9635_TPM, "infineon,slb9635-tpm"),
Vincent Palatin06883eb2013-04-12 11:04:36 +000075 COMPAT(INFINEON_SLB9645_TPM, "infineon,slb9645-tpm"),
Simon Glass2d2af852011-10-24 19:15:32 +000076};
77
Simon Glassb022ead2012-01-17 08:20:50 +000078const char *fdtdec_get_compatible(enum fdt_compat_id id)
79{
80 /* We allow reading of the 'unknown' ID for testing purposes */
81 assert(id >= 0 && id < COMPAT_COUNT);
82 return compat_names[id];
83}
84
Simon Glassf4299552013-03-19 04:58:51 +000085fdt_addr_t fdtdec_get_addr_size(const void *blob, int node,
86 const char *prop_name, fdt_size_t *sizep)
Simon Glass2d2af852011-10-24 19:15:32 +000087{
88 const fdt_addr_t *cell;
89 int len;
90
Simon Glassc8c86dd2012-07-12 05:25:01 +000091 debug("%s: %s: ", __func__, prop_name);
Simon Glass2d2af852011-10-24 19:15:32 +000092 cell = fdt_getprop(blob, node, prop_name, &len);
Simon Glassf4299552013-03-19 04:58:51 +000093 if (cell && ((!sizep && len == sizeof(fdt_addr_t)) ||
94 len == sizeof(fdt_addr_t) * 2)) {
Simon Glassc8c86dd2012-07-12 05:25:01 +000095 fdt_addr_t addr = fdt_addr_to_cpu(*cell);
Simon Glassf4299552013-03-19 04:58:51 +000096 if (sizep) {
97 const fdt_size_t *size;
Simon Glassc8c86dd2012-07-12 05:25:01 +000098
Simon Glassf4299552013-03-19 04:58:51 +000099 size = (fdt_size_t *)((char *)cell +
100 sizeof(fdt_addr_t));
101 *sizep = fdt_size_to_cpu(*size);
102 debug("addr=%p, size=%p\n", (void *)addr,
103 (void *)*sizep);
104 } else {
105 debug("%p\n", (void *)addr);
106 }
Simon Glassc8c86dd2012-07-12 05:25:01 +0000107 return addr;
108 }
109 debug("(not found)\n");
Simon Glass2d2af852011-10-24 19:15:32 +0000110 return FDT_ADDR_T_NONE;
111}
112
Simon Glassf4299552013-03-19 04:58:51 +0000113fdt_addr_t fdtdec_get_addr(const void *blob, int node,
114 const char *prop_name)
115{
116 return fdtdec_get_addr_size(blob, node, prop_name, NULL);
117}
118
Simon Glass2d2af852011-10-24 19:15:32 +0000119s32 fdtdec_get_int(const void *blob, int node, const char *prop_name,
120 s32 default_val)
121{
122 const s32 *cell;
123 int len;
124
Simon Glassc8c86dd2012-07-12 05:25:01 +0000125 debug("%s: %s: ", __func__, prop_name);
Simon Glass2d2af852011-10-24 19:15:32 +0000126 cell = fdt_getprop(blob, node, prop_name, &len);
Simon Glassc8c86dd2012-07-12 05:25:01 +0000127 if (cell && len >= sizeof(s32)) {
128 s32 val = fdt32_to_cpu(cell[0]);
129
130 debug("%#x (%d)\n", val, val);
131 return val;
132 }
133 debug("(not found)\n");
Simon Glass2d2af852011-10-24 19:15:32 +0000134 return default_val;
135}
136
Che-Liang Chioud81d0172012-10-25 16:31:05 +0000137uint64_t fdtdec_get_uint64(const void *blob, int node, const char *prop_name,
138 uint64_t default_val)
139{
140 const uint64_t *cell64;
141 int length;
142
143 cell64 = fdt_getprop(blob, node, prop_name, &length);
144 if (!cell64 || length < sizeof(*cell64))
145 return default_val;
146
147 return fdt64_to_cpu(*cell64);
148}
149
Simon Glass65444982012-02-27 10:52:34 +0000150int fdtdec_get_is_enabled(const void *blob, int node)
Simon Glass2d2af852011-10-24 19:15:32 +0000151{
152 const char *cell;
153
Simon Glass65444982012-02-27 10:52:34 +0000154 /*
155 * It should say "okay", so only allow that. Some fdts use "ok" but
156 * this is a bug. Please fix your device tree source file. See here
157 * for discussion:
158 *
159 * http://www.mail-archive.com/u-boot@lists.denx.de/msg71598.html
160 */
Simon Glass2d2af852011-10-24 19:15:32 +0000161 cell = fdt_getprop(blob, node, "status", NULL);
162 if (cell)
Simon Glass65444982012-02-27 10:52:34 +0000163 return 0 == strcmp(cell, "okay");
164 return 1;
Simon Glass2d2af852011-10-24 19:15:32 +0000165}
166
Gerald Van Barencdac3392012-11-12 23:13:54 -0500167enum fdt_compat_id fdtdec_lookup(const void *blob, int node)
Simon Glass2d2af852011-10-24 19:15:32 +0000168{
169 enum fdt_compat_id id;
170
171 /* Search our drivers */
172 for (id = COMPAT_UNKNOWN; id < COMPAT_COUNT; id++)
173 if (0 == fdt_node_check_compatible(blob, node,
174 compat_names[id]))
175 return id;
176 return COMPAT_UNKNOWN;
177}
178
179int fdtdec_next_compatible(const void *blob, int node,
180 enum fdt_compat_id id)
181{
182 return fdt_node_offset_by_compatible(blob, node, compat_names[id]);
183}
184
Simon Glass2e979b12012-04-02 13:18:42 +0000185int fdtdec_next_compatible_subnode(const void *blob, int node,
186 enum fdt_compat_id id, int *depthp)
187{
188 do {
189 node = fdt_next_node(blob, node, depthp);
190 } while (*depthp > 1);
191
192 /* If this is a direct subnode, and compatible, return it */
193 if (*depthp == 1 && 0 == fdt_node_check_compatible(
194 blob, node, compat_names[id]))
195 return node;
196
197 return -FDT_ERR_NOTFOUND;
198}
199
Simon Glass2d2af852011-10-24 19:15:32 +0000200int fdtdec_next_alias(const void *blob, const char *name,
201 enum fdt_compat_id id, int *upto)
202{
203#define MAX_STR_LEN 20
204 char str[MAX_STR_LEN + 20];
205 int node, err;
206
207 /* snprintf() is not available */
208 assert(strlen(name) < MAX_STR_LEN);
209 sprintf(str, "%.*s%d", MAX_STR_LEN, name, *upto);
Simon Glasse1d15ca2012-10-31 14:02:42 +0000210 node = fdt_path_offset(blob, str);
Simon Glass2d2af852011-10-24 19:15:32 +0000211 if (node < 0)
212 return node;
213 err = fdt_node_check_compatible(blob, node, compat_names[id]);
214 if (err < 0)
215 return err;
Simon Glass65444982012-02-27 10:52:34 +0000216 if (err)
217 return -FDT_ERR_NOTFOUND;
218 (*upto)++;
219 return node;
Simon Glass2d2af852011-10-24 19:15:32 +0000220}
221
Simon Glassb022ead2012-01-17 08:20:50 +0000222int fdtdec_find_aliases_for_id(const void *blob, const char *name,
223 enum fdt_compat_id id, int *node_list, int maxcount)
224{
Simon Glass1f772662012-02-03 15:13:53 +0000225 memset(node_list, '\0', sizeof(*node_list) * maxcount);
226
227 return fdtdec_add_aliases_for_id(blob, name, id, node_list, maxcount);
228}
229
230/* TODO: Can we tighten this code up a little? */
231int fdtdec_add_aliases_for_id(const void *blob, const char *name,
232 enum fdt_compat_id id, int *node_list, int maxcount)
233{
Simon Glassb022ead2012-01-17 08:20:50 +0000234 int name_len = strlen(name);
235 int nodes[maxcount];
236 int num_found = 0;
237 int offset, node;
238 int alias_node;
239 int count;
240 int i, j;
241
242 /* find the alias node if present */
243 alias_node = fdt_path_offset(blob, "/aliases");
244
245 /*
246 * start with nothing, and we can assume that the root node can't
247 * match
248 */
249 memset(nodes, '\0', sizeof(nodes));
250
251 /* First find all the compatible nodes */
252 for (node = count = 0; node >= 0 && count < maxcount;) {
253 node = fdtdec_next_compatible(blob, node, id);
254 if (node >= 0)
255 nodes[count++] = node;
256 }
257 if (node >= 0)
258 debug("%s: warning: maxcount exceeded with alias '%s'\n",
259 __func__, name);
260
261 /* Now find all the aliases */
Simon Glassb022ead2012-01-17 08:20:50 +0000262 for (offset = fdt_first_property_offset(blob, alias_node);
263 offset > 0;
264 offset = fdt_next_property_offset(blob, offset)) {
265 const struct fdt_property *prop;
266 const char *path;
267 int number;
268 int found;
269
270 node = 0;
271 prop = fdt_get_property_by_offset(blob, offset, NULL);
272 path = fdt_string(blob, fdt32_to_cpu(prop->nameoff));
273 if (prop->len && 0 == strncmp(path, name, name_len))
274 node = fdt_path_offset(blob, prop->data);
275 if (node <= 0)
276 continue;
277
278 /* Get the alias number */
279 number = simple_strtoul(path + name_len, NULL, 10);
280 if (number < 0 || number >= maxcount) {
281 debug("%s: warning: alias '%s' is out of range\n",
282 __func__, path);
283 continue;
284 }
285
286 /* Make sure the node we found is actually in our list! */
287 found = -1;
288 for (j = 0; j < count; j++)
289 if (nodes[j] == node) {
290 found = j;
291 break;
292 }
293
294 if (found == -1) {
295 debug("%s: warning: alias '%s' points to a node "
296 "'%s' that is missing or is not compatible "
297 " with '%s'\n", __func__, path,
298 fdt_get_name(blob, node, NULL),
299 compat_names[id]);
300 continue;
301 }
302
303 /*
304 * Add this node to our list in the right place, and mark
305 * it as done.
306 */
307 if (fdtdec_get_is_enabled(blob, node)) {
Simon Glass1f772662012-02-03 15:13:53 +0000308 if (node_list[number]) {
309 debug("%s: warning: alias '%s' requires that "
310 "a node be placed in the list in a "
311 "position which is already filled by "
312 "node '%s'\n", __func__, path,
313 fdt_get_name(blob, node, NULL));
314 continue;
315 }
Simon Glassb022ead2012-01-17 08:20:50 +0000316 node_list[number] = node;
317 if (number >= num_found)
318 num_found = number + 1;
319 }
Simon Glass1f772662012-02-03 15:13:53 +0000320 nodes[found] = 0;
Simon Glassb022ead2012-01-17 08:20:50 +0000321 }
322
323 /* Add any nodes not mentioned by an alias */
324 for (i = j = 0; i < maxcount; i++) {
325 if (!node_list[i]) {
326 for (; j < maxcount; j++)
327 if (nodes[j] &&
328 fdtdec_get_is_enabled(blob, nodes[j]))
329 break;
330
331 /* Have we run out of nodes to add? */
332 if (j == maxcount)
333 break;
334
335 assert(!node_list[i]);
336 node_list[i] = nodes[j++];
337 if (i >= num_found)
338 num_found = i + 1;
339 }
340 }
341
342 return num_found;
343}
344
Simon Glass1691f692012-03-28 10:08:24 +0000345int fdtdec_check_fdt(void)
346{
347 /*
348 * We must have an FDT, but we cannot panic() yet since the console
349 * is not ready. So for now, just assert(). Boards which need an early
350 * FDT (prior to console ready) will need to make their own
351 * arrangements and do their own checks.
352 */
353 assert(!fdtdec_prepare_fdt());
354 return 0;
355}
356
Simon Glass2d2af852011-10-24 19:15:32 +0000357/*
358 * This function is a little odd in that it accesses global data. At some
359 * point if the architecture board.c files merge this will make more sense.
360 * Even now, it is common code.
361 */
Simon Glass1691f692012-03-28 10:08:24 +0000362int fdtdec_prepare_fdt(void)
Simon Glass2d2af852011-10-24 19:15:32 +0000363{
Simon Glass71136852013-04-20 08:42:46 +0000364 if (!gd->fdt_blob || ((uintptr_t)gd->fdt_blob & 3) ||
365 fdt_check_header(gd->fdt_blob)) {
Simon Glass1691f692012-03-28 10:08:24 +0000366 printf("No valid FDT found - please append one to U-Boot "
367 "binary, use u-boot-dtb.bin or define "
Simon Glass62cf9122013-04-26 02:53:43 +0000368 "CONFIG_OF_EMBED. For sandbox, use -d <file.dtb>\n");
Simon Glass1691f692012-03-28 10:08:24 +0000369 return -1;
370 }
Simon Glass2d2af852011-10-24 19:15:32 +0000371 return 0;
372}
Simon Glassc4697362012-02-27 10:52:35 +0000373
374int fdtdec_lookup_phandle(const void *blob, int node, const char *prop_name)
375{
376 const u32 *phandle;
377 int lookup;
378
Simon Glassc8c86dd2012-07-12 05:25:01 +0000379 debug("%s: %s\n", __func__, prop_name);
Simon Glassc4697362012-02-27 10:52:35 +0000380 phandle = fdt_getprop(blob, node, prop_name, NULL);
381 if (!phandle)
382 return -FDT_ERR_NOTFOUND;
383
384 lookup = fdt_node_offset_by_phandle(blob, fdt32_to_cpu(*phandle));
385 return lookup;
386}
387
388/**
389 * Look up a property in a node and check that it has a minimum length.
390 *
391 * @param blob FDT blob
392 * @param node node to examine
393 * @param prop_name name of property to find
394 * @param min_len minimum property length in bytes
395 * @param err 0 if ok, or -FDT_ERR_NOTFOUND if the property is not
396 found, or -FDT_ERR_BADLAYOUT if not enough data
397 * @return pointer to cell, which is only valid if err == 0
398 */
399static const void *get_prop_check_min_len(const void *blob, int node,
400 const char *prop_name, int min_len, int *err)
401{
402 const void *cell;
403 int len;
404
405 debug("%s: %s\n", __func__, prop_name);
406 cell = fdt_getprop(blob, node, prop_name, &len);
407 if (!cell)
408 *err = -FDT_ERR_NOTFOUND;
409 else if (len < min_len)
410 *err = -FDT_ERR_BADLAYOUT;
411 else
412 *err = 0;
413 return cell;
414}
415
416int fdtdec_get_int_array(const void *blob, int node, const char *prop_name,
417 u32 *array, int count)
418{
419 const u32 *cell;
420 int i, err = 0;
421
422 debug("%s: %s\n", __func__, prop_name);
423 cell = get_prop_check_min_len(blob, node, prop_name,
424 sizeof(u32) * count, &err);
425 if (!err) {
426 for (i = 0; i < count; i++)
427 array[i] = fdt32_to_cpu(cell[i]);
428 }
429 return err;
430}
431
Simon Glass744ec232012-04-02 13:18:41 +0000432const u32 *fdtdec_locate_array(const void *blob, int node,
433 const char *prop_name, int count)
434{
435 const u32 *cell;
436 int err;
437
438 cell = get_prop_check_min_len(blob, node, prop_name,
439 sizeof(u32) * count, &err);
440 return err ? NULL : cell;
441}
442
Simon Glassc4697362012-02-27 10:52:35 +0000443int fdtdec_get_bool(const void *blob, int node, const char *prop_name)
444{
445 const s32 *cell;
446 int len;
447
448 debug("%s: %s\n", __func__, prop_name);
449 cell = fdt_getprop(blob, node, prop_name, &len);
450 return cell != NULL;
451}
Simon Glass0d83c4d2012-02-27 10:52:36 +0000452
453/**
454 * Decode a list of GPIOs from an FDT. This creates a list of GPIOs with no
455 * terminating item.
456 *
457 * @param blob FDT blob to use
458 * @param node Node to look at
459 * @param prop_name Node property name
460 * @param gpio Array of gpio elements to fill from FDT. This will be
461 * untouched if either 0 or an error is returned
462 * @param max_count Maximum number of elements allowed
463 * @return number of GPIOs read if ok, -FDT_ERR_BADLAYOUT if max_count would
464 * be exceeded, or -FDT_ERR_NOTFOUND if the property is missing.
465 */
Abhilash Kesavan4353ffc2012-10-25 16:31:01 +0000466int fdtdec_decode_gpios(const void *blob, int node, const char *prop_name,
467 struct fdt_gpio_state *gpio, int max_count)
Simon Glass0d83c4d2012-02-27 10:52:36 +0000468{
469 const struct fdt_property *prop;
470 const u32 *cell;
471 const char *name;
472 int len, i;
473
474 debug("%s: %s\n", __func__, prop_name);
475 assert(max_count > 0);
476 prop = fdt_get_property(blob, node, prop_name, &len);
477 if (!prop) {
Simon Glassc8c86dd2012-07-12 05:25:01 +0000478 debug("%s: property '%s' missing\n", __func__, prop_name);
Simon Glass0d83c4d2012-02-27 10:52:36 +0000479 return -FDT_ERR_NOTFOUND;
480 }
481
482 /* We will use the name to tag the GPIO */
483 name = fdt_string(blob, fdt32_to_cpu(prop->nameoff));
484 cell = (u32 *)prop->data;
485 len /= sizeof(u32) * 3; /* 3 cells per GPIO record */
486 if (len > max_count) {
Simon Glassc8c86dd2012-07-12 05:25:01 +0000487 debug(" %s: too many GPIOs / cells for "
Simon Glass0d83c4d2012-02-27 10:52:36 +0000488 "property '%s'\n", __func__, prop_name);
489 return -FDT_ERR_BADLAYOUT;
490 }
491
492 /* Read out the GPIO data from the cells */
493 for (i = 0; i < len; i++, cell += 3) {
494 gpio[i].gpio = fdt32_to_cpu(cell[1]);
495 gpio[i].flags = fdt32_to_cpu(cell[2]);
496 gpio[i].name = name;
497 }
498
499 return len;
500}
501
502int fdtdec_decode_gpio(const void *blob, int node, const char *prop_name,
503 struct fdt_gpio_state *gpio)
504{
505 int err;
506
507 debug("%s: %s\n", __func__, prop_name);
508 gpio->gpio = FDT_GPIO_NONE;
509 gpio->name = NULL;
510 err = fdtdec_decode_gpios(blob, node, prop_name, gpio, 1);
511 return err == 1 ? 0 : err;
512}
513
Sean Pauladbcfc92012-10-25 16:31:06 +0000514int fdtdec_get_gpio(struct fdt_gpio_state *gpio)
515{
516 int val;
517
518 if (!fdt_gpio_isvalid(gpio))
519 return -1;
520
521 val = gpio_get_value(gpio->gpio);
522 return gpio->flags & FDT_GPIO_ACTIVE_LOW ? val ^ 1 : val;
523}
524
525int fdtdec_set_gpio(struct fdt_gpio_state *gpio, int val)
526{
527 if (!fdt_gpio_isvalid(gpio))
528 return -1;
529
530 val = gpio->flags & FDT_GPIO_ACTIVE_LOW ? val ^ 1 : val;
531 return gpio_set_value(gpio->gpio, val);
532}
533
Simon Glass0d83c4d2012-02-27 10:52:36 +0000534int fdtdec_setup_gpio(struct fdt_gpio_state *gpio)
535{
536 /*
537 * Return success if there is no GPIO defined. This is used for
538 * optional GPIOs)
539 */
540 if (!fdt_gpio_isvalid(gpio))
541 return 0;
542
543 if (gpio_request(gpio->gpio, gpio->name))
544 return -1;
545 return 0;
546}
Anton Stafff2305ec2012-04-17 09:01:28 +0000547
548int fdtdec_get_byte_array(const void *blob, int node, const char *prop_name,
549 u8 *array, int count)
550{
551 const u8 *cell;
552 int err;
553
554 cell = get_prop_check_min_len(blob, node, prop_name, count, &err);
555 if (!err)
556 memcpy(array, cell, count);
557 return err;
558}
559
560const u8 *fdtdec_locate_byte_array(const void *blob, int node,
561 const char *prop_name, int count)
562{
563 const u8 *cell;
564 int err;
565
566 cell = get_prop_check_min_len(blob, node, prop_name, count, &err);
567 if (err)
568 return NULL;
569 return cell;
570}
Abhilash Kesavan67217af2012-10-25 16:30:58 +0000571
Abhilash Kesavan67217af2012-10-25 16:30:58 +0000572int fdtdec_get_config_int(const void *blob, const char *prop_name,
573 int default_val)
574{
575 int config_node;
576
577 debug("%s: %s\n", __func__, prop_name);
578 config_node = fdt_path_offset(blob, "/config");
579 if (config_node < 0)
580 return default_val;
581 return fdtdec_get_int(blob, config_node, prop_name, default_val);
582}
Simon Glassb0494472012-10-25 16:30:59 +0000583
Gabe Black3e1c1512012-10-25 16:31:04 +0000584int fdtdec_get_config_bool(const void *blob, const char *prop_name)
585{
586 int config_node;
587 const void *prop;
588
589 debug("%s: %s\n", __func__, prop_name);
590 config_node = fdt_path_offset(blob, "/config");
591 if (config_node < 0)
592 return 0;
593 prop = fdt_get_property(blob, config_node, prop_name, NULL);
594
595 return prop != NULL;
596}
597
Simon Glassb0494472012-10-25 16:30:59 +0000598char *fdtdec_get_config_string(const void *blob, const char *prop_name)
599{
600 const char *nodep;
601 int nodeoffset;
602 int len;
603
604 debug("%s: %s\n", __func__, prop_name);
605 nodeoffset = fdt_path_offset(blob, "/config");
606 if (nodeoffset < 0)
607 return NULL;
608
609 nodep = fdt_getprop(blob, nodeoffset, prop_name, &len);
610 if (!nodep)
611 return NULL;
612
613 return (char *)nodep;
614}
Simon Glassf909b9c2012-10-25 16:31:00 +0000615
616int fdtdec_decode_region(const void *blob, int node,
617 const char *prop_name, void **ptrp, size_t *size)
618{
619 const fdt_addr_t *cell;
620 int len;
621
622 debug("%s: %s\n", __func__, prop_name);
623 cell = fdt_getprop(blob, node, prop_name, &len);
624 if (!cell || (len != sizeof(fdt_addr_t) * 2))
625 return -1;
626
627 *ptrp = (void *)fdt_addr_to_cpu(*cell);
628 *size = fdt_size_to_cpu(cell[1]);
629 debug("%s: size=%zx\n", __func__, *size);
630 return 0;
631}