Documentation: Fix sphinx configuration
[coreboot.git] / src / include / device_tree.h
blobb70f5aadbd189fa5e8504af0b779f28ea8691cb2
1 /* Taken from depthcharge: src/base/device_tree.h */
2 /* SPDX-License-Identifier: GPL-2.0-or-later */
4 #ifndef __DEVICE_TREE_H__
5 #define __DEVICE_TREE_H__
7 #include <stddef.h>
8 #include <stdint.h>
9 #include <list.h>
12 * Flattened device tree structures/constants.
15 struct fdt_header {
16 uint32_t magic;
17 uint32_t totalsize;
18 uint32_t structure_offset;
19 uint32_t strings_offset;
20 uint32_t reserve_map_offset;
22 uint32_t version;
23 uint32_t last_comp_version;
25 uint32_t boot_cpuid_phys;
27 uint32_t strings_size;
28 uint32_t structure_size;
31 #define FDT_HEADER_MAGIC 0xd00dfeed
32 #define FDT_SUPPORTED_VERSION 17
33 #define FDT_TOKEN_BEGIN_NODE 1
34 #define FDT_TOKEN_END_NODE 2
35 #define FDT_TOKEN_PROPERTY 3
36 #define FDT_TOKEN_END 9
37 #define FDT_PHANDLE_ILLEGAL 0xdeadbeef
39 struct fdt_property
41 const char *name;
42 void *data;
43 uint32_t size;
49 * Unflattened device tree structures.
52 struct device_tree_property
54 struct fdt_property prop;
56 struct list_node list_node;
59 struct device_tree_node
61 const char *name;
62 uint32_t phandle;
64 /* List of struct device_tree_property-s. */
65 struct list_node properties;
66 /* List of struct device_tree_nodes. */
67 struct list_node children;
69 struct list_node list_node;
72 struct device_tree_reserve_map_entry
74 uint64_t start;
75 uint64_t size;
77 struct list_node list_node;
80 struct device_tree
82 const void *header;
83 uint32_t header_size;
84 uint32_t max_phandle;
86 struct list_node reserve_map;
88 struct device_tree_node *root;
94 * Flattened device tree functions. These generally return the number of bytes
95 * which were consumed reading the requested value.
98 /* Read the property, if any, at offset offset. */
99 int fdt_next_property(const void *blob, uint32_t offset,
100 struct fdt_property *prop);
101 /* Read the name of the node, if any, at offset offset. */
102 int fdt_node_name(const void *blob, uint32_t offset, const char **name);
104 void fdt_print_node(const void *blob, uint32_t offset);
105 int fdt_skip_node(const void *blob, uint32_t offset);
107 /* Read a flattened device tree into a hierarchical structure which refers to
108 the contents of the flattened tree in place. Modifying the flat tree
109 invalidates the unflattened one. */
110 struct device_tree *fdt_unflatten(const void *blob);
115 * Unflattened device tree functions.
118 /* Figure out how big a device tree would be if it were flattened. */
119 uint32_t dt_flat_size(const struct device_tree *tree);
120 /* Flatten a device tree into the buffer pointed to by dest. */
121 void dt_flatten(const struct device_tree *tree, void *dest);
122 void dt_print_node(const struct device_tree_node *node);
123 /* Read #address-cells and #size-cells properties from a node. */
124 void dt_read_cell_props(const struct device_tree_node *node, u32 *addrcp,
125 u32 *sizecp);
126 /* Look up or create a node relative to a parent node, through its path
127 represented as an array of strings. */
128 struct device_tree_node *dt_find_node(struct device_tree_node *parent, const char **path,
129 u32 *addrcp, u32 *sizecp, int create);
130 struct device_tree_node *dt_find_node_by_phandle(struct device_tree_node *root,
131 uint32_t phandle);
132 /* Look up or create a node in the tree, through its path
133 represented as a string of '/' separated node names. */
134 struct device_tree_node *dt_find_node_by_path(struct device_tree *tree,
135 const char *path, u32 *addrcp, u32 *sizecp, int create);
136 /* Look up a node through an alias. */
137 struct device_tree_node *dt_find_node_by_alias(struct device_tree *tree,
138 const char *alias);
139 /* Look up a node relative to a parent node, through its compatible string. */
140 struct device_tree_node *dt_find_compat(struct device_tree_node *parent, const char *compatible);
141 /* Look up the next child of a parent node, through its compatible string. It
142 uses child pointer as the marker to find next. */
143 struct device_tree_node *dt_find_next_compat_child(struct device_tree_node *parent,
144 struct device_tree_node *child,
145 const char *compat);
146 /* Look up a node relative to a parent node, through its property value. */
147 struct device_tree_node *dt_find_prop_value(struct device_tree_node *parent, const char *name,
148 void *data, size_t size);
149 /* Write src into *dest as a 'length'-byte big-endian integer. */
150 void dt_write_int(u8 *dest, u64 src, size_t length);
151 /* Delete a property */
152 void dt_delete_prop(struct device_tree_node *node, const char *name);
153 /* Add different kinds of properties to a node, or update existing ones. */
154 void dt_add_bin_prop(struct device_tree_node *node, const char *name,
155 void *data, size_t size);
156 void dt_add_string_prop(struct device_tree_node *node, const char *name,
157 const char *str);
158 void dt_add_u32_prop(struct device_tree_node *node, const char *name, u32 val);
159 void dt_add_u64_prop(struct device_tree_node *node, const char *name, u64 val);
160 void dt_add_reg_prop(struct device_tree_node *node, u64 *addrs, u64 *sizes,
161 int count, u32 addr_cells, u32 size_cells);
162 int dt_set_bin_prop_by_path(struct device_tree *tree, const char *path,
163 void *data, size_t size, int create);
165 void dt_find_bin_prop(const struct device_tree_node *node, const char *name,
166 const void **data, size_t *size);
167 const char *dt_find_string_prop(const struct device_tree_node *node,
168 const char *name);
170 /* Apply an overlay to a base device tree. Ownership of the overlay data passes
171 to the newly combined base tree -- do not free() or access it afterwards! */
172 int dt_apply_overlay(struct device_tree *tree, struct device_tree *overlay);
175 * Fixups to apply to a kernel's device tree before booting it.
178 struct device_tree_fixup
181 * The function which does the fixing.
182 * 0 on success, non-zero on error.
184 int (*fixup)(struct device_tree_fixup *fixup,
185 struct device_tree *tree);
187 struct list_node list_node;
190 extern struct list_node device_tree_fixups;
193 * Function to apply fixups.
194 * 0 on success, non-zero on error.
196 int dt_apply_fixups(struct device_tree *tree);
199 * Init/retrieve the /reserved-memory/ node.
201 struct device_tree_node *dt_init_reserved_memory_node(struct device_tree *tree);
203 #endif /* __DEVICE_TREE_H__ */