xref: /rk3399_ARM-atf/include/common/fdt_wrappers.h (revision e7c0f42a2e29cfc87cb22ed6f213c77172d95f42)
1e5674e1fSSoby Mathew /*
2*e7c0f42aSJuan Pablo Conde  * Copyright (c) 2018-2023, Arm Limited and Contributors. All rights reserved.
3e5674e1fSSoby Mathew  *
4e5674e1fSSoby Mathew  * SPDX-License-Identifier: BSD-3-Clause
5e5674e1fSSoby Mathew  */
6e5674e1fSSoby Mathew 
7e5674e1fSSoby Mathew /* Helper functions to offer easier navigation of Device Tree Blob */
8e5674e1fSSoby Mathew 
9c3cf06f1SAntonio Nino Diaz #ifndef FDT_WRAPPERS_H
10c3cf06f1SAntonio Nino Diaz #define FDT_WRAPPERS_H
11e5674e1fSSoby Mathew 
125a430c02SAndre Przywara #include <libfdt_env.h>
1349b268ceSAndre Przywara #include <libfdt.h>
145a430c02SAndre Przywara 
15e5674e1fSSoby Mathew /* Number of cells, given total length in bytes. Each cell is 4 bytes long */
1681542c00SAntonio Nino Diaz #define NCELLS(len) ((len) / 4U)
17e5674e1fSSoby Mathew 
18ff4e6c35SAndre Przywara int fdt_read_uint32(const void *dtb, int node, const char *prop_name,
19ff4e6c35SAndre Przywara 		    uint32_t *value);
20be858cffSAndre Przywara uint32_t fdt_read_uint32_default(const void *dtb, int node,
21be858cffSAndre Przywara 				 const char *prop_name, uint32_t dflt_value);
22ff4e6c35SAndre Przywara int fdt_read_uint64(const void *dtb, int node, const char *prop_name,
23ff4e6c35SAndre Przywara 		    uint64_t *value);
246e3a89f4SAndre Przywara int fdt_read_uint32_array(const void *dtb, int node, const char *prop_name,
256e3a89f4SAndre Przywara 			  unsigned int cells, uint32_t *value);
2627473620SAntonio Nino Diaz int fdtw_read_string(const void *dtb, int node, const char *prop,
2727473620SAntonio Nino Diaz 		char *str, size_t size);
28d13dbb6fSDavid Horstmann int fdtw_read_uuid(const void *dtb, int node, const char *prop,
29d13dbb6fSDavid Horstmann 		   unsigned int length, uint8_t *uuid);
30e5674e1fSSoby Mathew int fdtw_write_inplace_cells(void *dtb, int node, const char *prop,
31e5674e1fSSoby Mathew 		unsigned int cells, void *value);
320a2ab6e6SAlexei Fedorov int fdtw_read_bytes(const void *dtb, int node, const char *prop,
330a2ab6e6SAlexei Fedorov 		unsigned int length, void *value);
340a2ab6e6SAlexei Fedorov int fdtw_write_inplace_bytes(void *dtb, int node, const char *prop,
350a2ab6e6SAlexei Fedorov 		unsigned int length, const void *data);
36364ad245SAndre Przywara int fdt_get_reg_props_by_index(const void *dtb, int node, int index,
37364ad245SAndre Przywara 			       uintptr_t *base, size_t *size);
387ad6d362SAndre Przywara int fdt_get_reg_props_by_name(const void *dtb, int node, const char *name,
397ad6d362SAndre Przywara 			      uintptr_t *base, size_t *size);
4060e2e27dSAndre Przywara int fdt_get_stdout_node_offset(const void *dtb);
4127473620SAntonio Nino Diaz 
42447870bfSMadhukar Pappireddy uint64_t fdtw_translate_address(const void *dtb, int bus_node,
43447870bfSMadhukar Pappireddy 				uint64_t base_address);
44447870bfSMadhukar Pappireddy 
452d9ea360SChris Kay int fdtw_for_each_cpu(const void *fdt,
462d9ea360SChris Kay 		      int (*callback)(const void *dtb, int node, uintptr_t mpidr));
472d9ea360SChris Kay 
48dea8ee0dSRuchika Gupta int fdtw_find_or_add_subnode(void *fdt, int parentoffset, const char *name);
49dea8ee0dSRuchika Gupta 
505a430c02SAndre Przywara static inline uint32_t fdt_blob_size(const void *dtb)
515a430c02SAndre Przywara {
52*e7c0f42aSJuan Pablo Conde 	const uint32_t *dtb_header = (const uint32_t *)dtb;
535a430c02SAndre Przywara 
545a430c02SAndre Przywara 	return fdt32_to_cpu(dtb_header[1]);
555a430c02SAndre Przywara }
565a430c02SAndre Przywara 
5749b268ceSAndre Przywara static inline bool fdt_node_is_enabled(const void *fdt, int node)
5849b268ceSAndre Przywara {
5949b268ceSAndre Przywara 	int len;
6049b268ceSAndre Przywara 	const void *prop = fdt_getprop(fdt, node, "status", &len);
6149b268ceSAndre Przywara 
6249b268ceSAndre Przywara 	/* A non-existing status property means the device is enabled. */
63*e7c0f42aSJuan Pablo Conde 	return (prop == NULL) || (len == 5 && strcmp((const char *)prop,
64*e7c0f42aSJuan Pablo Conde 		"okay") == 0);
6549b268ceSAndre Przywara }
6649b268ceSAndre Przywara 
67ff766148SLaurent Carlier #define fdt_for_each_compatible_node(dtb, node, compatible_str)       \
68ff766148SLaurent Carlier for (node = fdt_node_offset_by_compatible(dtb, -1, compatible_str);   \
69ff766148SLaurent Carlier      node >= 0;                                                       \
70ff766148SLaurent Carlier      node = fdt_node_offset_by_compatible(dtb, node, compatible_str))
71ff766148SLaurent Carlier 
72c3cf06f1SAntonio Nino Diaz #endif /* FDT_WRAPPERS_H */
73