xref: /rk3399_ARM-atf/include/common/fdt_wrappers.h (revision ff766148b52bfecf09728a83fc3becc7941d943c)
1e5674e1fSSoby Mathew /*
2d13dbb6fSDavid Horstmann  * Copyright (c) 2018-2021, 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>
135a430c02SAndre Przywara 
14e5674e1fSSoby Mathew /* Number of cells, given total length in bytes. Each cell is 4 bytes long */
1581542c00SAntonio Nino Diaz #define NCELLS(len) ((len) / 4U)
16e5674e1fSSoby Mathew 
17ff4e6c35SAndre Przywara int fdt_read_uint32(const void *dtb, int node, const char *prop_name,
18ff4e6c35SAndre Przywara 		    uint32_t *value);
19be858cffSAndre Przywara uint32_t fdt_read_uint32_default(const void *dtb, int node,
20be858cffSAndre Przywara 				 const char *prop_name, uint32_t dflt_value);
21ff4e6c35SAndre Przywara int fdt_read_uint64(const void *dtb, int node, const char *prop_name,
22ff4e6c35SAndre Przywara 		    uint64_t *value);
236e3a89f4SAndre Przywara int fdt_read_uint32_array(const void *dtb, int node, const char *prop_name,
246e3a89f4SAndre Przywara 			  unsigned int cells, uint32_t *value);
2527473620SAntonio Nino Diaz int fdtw_read_string(const void *dtb, int node, const char *prop,
2627473620SAntonio Nino Diaz 		char *str, size_t size);
27d13dbb6fSDavid Horstmann int fdtw_read_uuid(const void *dtb, int node, const char *prop,
28d13dbb6fSDavid Horstmann 		   unsigned int length, uint8_t *uuid);
29e5674e1fSSoby Mathew int fdtw_write_inplace_cells(void *dtb, int node, const char *prop,
30e5674e1fSSoby Mathew 		unsigned int cells, void *value);
310a2ab6e6SAlexei Fedorov int fdtw_read_bytes(const void *dtb, int node, const char *prop,
320a2ab6e6SAlexei Fedorov 		unsigned int length, void *value);
330a2ab6e6SAlexei Fedorov int fdtw_write_inplace_bytes(void *dtb, int node, const char *prop,
340a2ab6e6SAlexei Fedorov 		unsigned int length, const void *data);
35364ad245SAndre Przywara int fdt_get_reg_props_by_index(const void *dtb, int node, int index,
36364ad245SAndre Przywara 			       uintptr_t *base, size_t *size);
377ad6d362SAndre Przywara int fdt_get_reg_props_by_name(const void *dtb, int node, const char *name,
387ad6d362SAndre Przywara 			      uintptr_t *base, size_t *size);
3960e2e27dSAndre Przywara int fdt_get_stdout_node_offset(const void *dtb);
4027473620SAntonio Nino Diaz 
41447870bfSMadhukar Pappireddy uint64_t fdtw_translate_address(const void *dtb, int bus_node,
42447870bfSMadhukar Pappireddy 				uint64_t base_address);
43447870bfSMadhukar Pappireddy 
445a430c02SAndre Przywara static inline uint32_t fdt_blob_size(const void *dtb)
455a430c02SAndre Przywara {
465a430c02SAndre Przywara 	const uint32_t *dtb_header = dtb;
475a430c02SAndre Przywara 
485a430c02SAndre Przywara 	return fdt32_to_cpu(dtb_header[1]);
495a430c02SAndre Przywara }
505a430c02SAndre Przywara 
51*ff766148SLaurent Carlier #define fdt_for_each_compatible_node(dtb, node, compatible_str)       \
52*ff766148SLaurent Carlier for (node = fdt_node_offset_by_compatible(dtb, -1, compatible_str);   \
53*ff766148SLaurent Carlier      node >= 0;                                                       \
54*ff766148SLaurent Carlier      node = fdt_node_offset_by_compatible(dtb, node, compatible_str))
55*ff766148SLaurent Carlier 
56c3cf06f1SAntonio Nino Diaz #endif /* FDT_WRAPPERS_H */
57