1 /*
2 * Copyright (c) 2018-2023, 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 = (const uint32_t *)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((const char *)prop,
64 "okay") == 0);
65 }
66
67 #define fdt_for_each_compatible_node(dtb, node, compatible_str) \
68 for (node = fdt_node_offset_by_compatible(dtb, -1, compatible_str); \
69 node >= 0; \
70 node = fdt_node_offset_by_compatible(dtb, node, compatible_str))
71
72 #endif /* FDT_WRAPPERS_H */
73