xref: /rk3399_rockchip-uboot/lib/fdtdec_common.c (revision 0e00a84cdedf7a1949486746225b35984b351eca)
1b047d671SHeiko Schocher /*
2b047d671SHeiko Schocher  * Copyright (c) 2014
3b047d671SHeiko Schocher  * Heiko Schocher, DENX Software Engineering, hs@denx.de.
4b047d671SHeiko Schocher  *
5b047d671SHeiko Schocher  * Based on lib/fdtdec.c:
6b047d671SHeiko Schocher  * Copyright (c) 2011 The Chromium OS Authors.
7b047d671SHeiko Schocher  *
8b047d671SHeiko Schocher  * SPDX-License-Identifier:	GPL-2.0+
9b047d671SHeiko Schocher  */
10b047d671SHeiko Schocher 
11b047d671SHeiko Schocher #ifndef USE_HOSTCC
12b047d671SHeiko Schocher #include <common.h>
13*0e00a84cSMasahiro Yamada #include <linux/libfdt.h>
14b047d671SHeiko Schocher #include <fdtdec.h>
15b047d671SHeiko Schocher #else
16b047d671SHeiko Schocher #include "libfdt.h"
17b047d671SHeiko Schocher #include "fdt_support.h"
18b047d671SHeiko Schocher 
19b047d671SHeiko Schocher #define debug(...)
20b047d671SHeiko Schocher #endif
21b047d671SHeiko Schocher 
fdtdec_get_int(const void * blob,int node,const char * prop_name,int default_val)22b047d671SHeiko Schocher int fdtdec_get_int(const void *blob, int node, const char *prop_name,
23b047d671SHeiko Schocher 		int default_val)
24b047d671SHeiko Schocher {
25b047d671SHeiko Schocher 	const int *cell;
26b047d671SHeiko Schocher 	int len;
27b047d671SHeiko Schocher 
28b047d671SHeiko Schocher 	debug("%s: %s: ", __func__, prop_name);
29b047d671SHeiko Schocher 	cell = fdt_getprop(blob, node, prop_name, &len);
30b047d671SHeiko Schocher 	if (cell && len >= sizeof(int)) {
31b047d671SHeiko Schocher 		int val = fdt32_to_cpu(cell[0]);
32b047d671SHeiko Schocher 
33b047d671SHeiko Schocher 		debug("%#x (%d)\n", val, val);
34b047d671SHeiko Schocher 		return val;
35b047d671SHeiko Schocher 	}
36b047d671SHeiko Schocher 	debug("(not found)\n");
37b047d671SHeiko Schocher 	return default_val;
38b047d671SHeiko Schocher }
39bfa3e55bSChin Liang See 
fdtdec_get_uint(const void * blob,int node,const char * prop_name,unsigned int default_val)40bfa3e55bSChin Liang See unsigned int fdtdec_get_uint(const void *blob, int node, const char *prop_name,
41bfa3e55bSChin Liang See 			unsigned int default_val)
42bfa3e55bSChin Liang See {
43bfa3e55bSChin Liang See 	const int *cell;
44bfa3e55bSChin Liang See 	int len;
45bfa3e55bSChin Liang See 
46bfa3e55bSChin Liang See 	debug("%s: %s: ", __func__, prop_name);
47bfa3e55bSChin Liang See 	cell = fdt_getprop(blob, node, prop_name, &len);
48bfa3e55bSChin Liang See 	if (cell && len >= sizeof(unsigned int)) {
49bfa3e55bSChin Liang See 		unsigned int val = fdt32_to_cpu(cell[0]);
50bfa3e55bSChin Liang See 
51bfa3e55bSChin Liang See 		debug("%#x (%d)\n", val, val);
52bfa3e55bSChin Liang See 		return val;
53bfa3e55bSChin Liang See 	}
54bfa3e55bSChin Liang See 	debug("(not found)\n");
55bfa3e55bSChin Liang See 	return default_val;
56bfa3e55bSChin Liang See }
57