1 /*
2 * Copyright (c) 2018-2022, ARM Limited and Contributors. All rights reserved.
3 *
4 * SPDX-License-Identifier: BSD-3-Clause
5 */
6
7 /* Helper functions to offer easier navigation of Device Tree Blob */
8
9 #ifndef FDT_WRAPPERS_H
10 #define FDT_WRAPPERS_H
11
12 #include <libfdt_env.h>
13 #include <libfdt.h>
14
15 /* Number of cells, given total length in bytes. Each cell is 4 bytes long */
16 #define NCELLS(len) ((len) / 4U)
17
18 int fdt_read_uint32(const void *dtb, int node, const char *prop_name,
19 uint32_t *value);
20 uint32_t fdt_read_uint32_default(const void *dtb, int node,
21 const char *prop_name, uint32_t dflt_value);
22 int fdt_read_uint64(const void *dtb, int node, const char *prop_name,
23 uint64_t *value);
24 int fdt_read_uint32_array(const void *dtb, int node, const char *prop_name,
25 unsigned int cells, uint32_t *value);
26 int fdtw_read_string(const void *dtb, int node, const char *prop,
27 char *str, size_t size);
28 int fdtw_read_uuid(const void *dtb, int node, const char *prop,
29 unsigned int length, uint8_t *uuid);
30 int fdtw_write_inplace_cells(void *dtb, int node, const char *prop,
31 unsigned int cells, void *value);
32 int fdtw_read_bytes(const void *dtb, int node, const char *prop,
33 unsigned int length, void *value);
34 int fdtw_write_inplace_bytes(void *dtb, int node, const char *prop,
35 unsigned int length, const void *data);
36 int fdt_get_reg_props_by_index(const void *dtb, int node, int index,
37 uintptr_t *base, size_t *size);
38 int fdt_get_reg_props_by_name(const void *dtb, int node, const char *name,
39 uintptr_t *base, size_t *size);
40 int fdt_get_stdout_node_offset(const void *dtb);
41
42 uint64_t fdtw_translate_address(const void *dtb, int bus_node,
43 uint64_t base_address);
44
45 int fdtw_for_each_cpu(const void *fdt,
46 int (*callback)(const void *dtb, int node, uintptr_t mpidr));
47
48 int fdtw_find_or_add_subnode(void *fdt, int parentoffset, const char *name);
49
fdt_blob_size(const void * dtb)50 static inline uint32_t fdt_blob_size(const void *dtb)
51 {
52 const uint32_t *dtb_header = dtb;
53
54 return fdt32_to_cpu(dtb_header[1]);
55 }
56
fdt_node_is_enabled(const void * fdt,int node)57 static inline bool fdt_node_is_enabled(const void *fdt, int node)
58 {
59 int len;
60 const void *prop = fdt_getprop(fdt, node, "status", &len);
61
62 /* A non-existing status property means the device is enabled. */
63 return (prop == NULL) || (len == 5 && strcmp(prop, "okay") == 0);
64 }
65
66 #define fdt_for_each_compatible_node(dtb, node, compatible_str) \
67 for (node = fdt_node_offset_by_compatible(dtb, -1, compatible_str); \
68 node >= 0; \
69 node = fdt_node_offset_by_compatible(dtb, node, compatible_str))
70
71 #endif /* FDT_WRAPPERS_H */
72