1 /*
2 * Copyright (c) 2006-2023, RT-Thread Development Team
3 *
4 * SPDX-License-Identifier: Apache-2.0
5 */
6 #ifndef _DTB_NODE_H__
7 #define _DTB_NODE_H__
8
9 #include "libfdt_env.h"
10 #include <rtthread.h>
11 #include <stdint.h>
12
13 #ifdef RT_USING_FDT_FWNODE
14 #include "dtb_fwnode.h"
15 #endif
16
17 //#define RT_DTB_DEBUG
18 #ifdef RT_DTB_DEBUG
19 #define debug(fmt, args...) rt_kprintf(fmt, ##args)
20 #else
21 #define debug(fmt, args...)
22 #endif
23
24 #define DTB_ERR_PTR(err) ((void *)((long)(err)))
25 #define DTB_PTR_ERR(ptr) ((long)(ptr))
26 #define DTB_IS_ERR(ptr) ((unsigned long)(ptr) > (unsigned long)(-1000))
27
28 #define DEV_ROOT_NODE_ADDR_CELLS_DEFAULT 2
29 #define DEV_ROOT_NODE_SIZE_CELLS_DEFAULT 1
30 /* will be optimized to only u64 or u32 by gcc */
31 #define IN_64BITS_MODE (sizeof(void *) == 8)
32
33 #define FDT_ROOT_ADDR_CELLS_DEFAULT 1
34 #define FDT_ROOT_SIZE_CELLS_DEFAULT 1
35
36 #define FDT_DTB_ALL_NODES_PATH_SIZE (32 * 1024)
37 #define FDT_DTB_PAD_SIZE 1024
38
39 #define FDT_RET_NO_MEMORY 2
40 #define FDT_RET_NO_LOADED 1
41 #define FDT_RET_GET_OK 0
42 #define FDT_RET_GET_EMPTY (-1)
43
44 typedef uint32_t phandle;
45
46 struct dtb_memreserve
47 {
48 uintptr_t address;
49 size_t size;
50 };
51
52 struct dtb_header
53 {
54 char root, zero; /* "/" */
55 struct dtb_memreserve *memreserve;
56 size_t memreserve_sz;
57 };
58
59 struct dtb_property
60 {
61 const char *name;
62 int size;
63 void *value;
64
65 struct dtb_property *next;
66 };
67
68 struct dtb_node
69 {
70 union
71 {
72 const char *name;
73 const struct dtb_header *header;
74 };
75 const char *path;
76 phandle handle;
77 int level;
78 #ifdef RT_USING_FDT_FWNODE
79 struct fwnode_handle fwnode;
80 unsigned long _flags;
81 #endif
82 struct dtb_property *properties;
83 struct dtb_node *parent;
84 struct dtb_node *child;
85 struct dtb_node *sibling;
86 };
87
88 #define FDT_MAX_PHANDLE_ARGS 16
89
90 /**
91 * struct dtb_node_phandle_args - structure to hold phandle and arguments
92 *
93 * This is used when decoding a phandle in a device tree property. Typically
94 * these look like this:
95 *
96 * wibble {
97 * phandle = <5>;
98 * };
99 *
100 * ...
101 * some-prop = <&wibble 1 2 3>
102 *
103 * Here &node is the phandle of the node 'wibble', i.e. 5. There are three
104 * arguments: 1, 2, 3.
105 *
106 * So when decoding the phandle in some-prop, np will point to wibble,
107 * args_count will be 3 and the three arguments will be in args.
108 *
109 * @np: Node that the phandle refers to
110 * @args_count: Number of arguments
111 * @args: Argument values
112 */
113 struct fdt_phandle_args
114 {
115 struct dtb_node *np;
116 int args_count;
117 uint32_t args[FDT_MAX_PHANDLE_ARGS];
118 };
119
120 /*
121 * A single signal can be specified via a range of minimal and maximal values
122 * with a typical value, that lies somewhere inbetween.
123 */
124 struct timing_entry
125 {
126 uint32_t min;
127 uint32_t typ;
128 uint32_t max;
129 };
130
131 void *get_fdt_blob(void);
132 struct dtb_node *get_dtb_node_head(void);
133 rt_bool_t dtb_node_active(void);
134 int device_tree_setup(void *mem_addr);
135
136 void *dtb_node_load_from_fs(char *dtb_filename);
137 void *dtb_node_load_from_memory(void *dtb_ptr, rt_bool_t is_clone);
138
139 size_t dtb_node_set_linux_cmdline(void *fdt, char *cmdline);
140 size_t dtb_node_set_linux_initrd(void *fdt, uint64_t initrd_addr, size_t initrd_size);
141
142 size_t dtb_node_set_dtb_property(void *fdt, char *pathname, char *property_name, uint32_t *cells, size_t cells_size);
143 size_t dtb_node_add_dtb_memreserve(void *fdt, uint64_t address, uint64_t size);
144 size_t dtb_node_del_dtb_memreserve(void *fdt, uint64_t address);
145
146 int dtb_node_get_exec_status();
147 struct dtb_node *dtb_node_get_dtb_list(void *fdt);
148 void dtb_node_free_dtb_list(struct dtb_node *dtb_node_head);
149 void dtb_node_get_dts_dump(struct dtb_node *dtb_node_head);
150 void dtb_node_get_enum_dtb_node(struct dtb_node *dtb_node_head, void(callback(struct dtb_node *dtb_node)));
151
152 struct dtb_node *dtb_node_get_dtb_node_by_name_DFS(struct dtb_node *dtb_node, const char *nodename);
153 struct dtb_node *dtb_node_get_dtb_node_by_name_BFS(struct dtb_node *dtb_node, const char *nodename);
154 struct dtb_node *dtb_node_get_dtb_node_by_path(struct dtb_node *dtb_node, const char *pathname);
155 struct dtb_node *dtb_node_get_dtb_node_by_phandle_DFS(struct dtb_node *dtb_node, phandle handle);
156 struct dtb_node *dtb_node_get_dtb_node_by_phandle_BFS(struct dtb_node *dtb_node, phandle handle);
157 void dtb_node_get_dtb_node_cells(struct dtb_node *dtb_node, int *addr_cells, int *size_cells);
158 struct dtb_memreserve *dtb_node_get_dtb_memreserve(struct dtb_node *dtb_node, int *memreserve_size);
159 uint8_t dtb_node_get_dtb_byte_value(void *value);
160
161 char *dtb_node_get_dtb_string_list_value(void *value, int size, int index);
162 char *dtb_node_get_dtb_string_list_value_next(void *value, void *end);
163 uint32_t dtb_node_get_dtb_cell_value(void *value);
164
165 rt_bool_t dtb_node_get_dtb_node_status(const struct dtb_node *dtb_node);
166 rt_bool_t dtb_node_get_dtb_node_compatible_match(const struct dtb_node *dtb_node, const char *compatibles);
167
168 /*dtb_node_access.c */
169 int dtb_node_read_u32(const struct dtb_node *dn, const char *propname, uint32_t *outp);
170 uint32_t dtb_node_read_u32_default(const struct dtb_node *node, const char *propname, uint32_t def);
171 int dtb_node_read_u32_index(const struct dtb_node *node, const char *propname, int index,
172 uint32_t *outp);
173 uint32_t dtb_node_read_u32_index_default(const struct dtb_node *node, const char *propname, int index,
174 uint32_t def);
175 int dtb_node_read_u32_array(const struct dtb_node *dn, const char *propname,
176 uint32_t *out_values, size_t sz);
177 int dtb_node_read_u32_index(const struct dtb_node *dn, const char *propname,
178 int index, uint32_t *outp);
179 int dtb_node_read_s32_default(const struct dtb_node *node, const char *propname, int32_t def);
180 int dtb_node_read_u64(const struct dtb_node *dn, const char *propname, uint64_t *outp);
181 uint64_t dtb_node_read_u64_default(const struct dtb_node *node, const char *propname, uint64_t def);
182
183 int dtb_node_n_addr_cells(const struct dtb_node *dn);
184 int dtb_node_n_size_cells(const struct dtb_node *dn);
185 int dtb_node_simple_addr_cells(const struct dtb_node *np);
186 int dtb_node_simple_size_cells(const struct dtb_node *np);
187
188 struct dtb_node *dtb_node_find_all_nodes(const struct dtb_node *prev);
189 struct dtb_node *dtb_node_find_node_by_phandle(phandle handle);
190 struct dtb_node *dtb_node_find_compatible_node(struct dtb_node *from, const char *compatible);
191 void *dtb_node_get_dtb_node_property_value(const struct dtb_node *dtb_node, const char *property_name, int *property_size);
192 struct dtb_property *dtb_node_get_dtb_node_property(const struct dtb_node *dtb_node, const char *property_name, int *property_size);
193 const struct dtb_node *dtb_node_find_node_by_prop_value(struct dtb_node *from, const char *propname, const void *propval, int proplen);
194 struct dtb_node *dtb_node_find_node_opts_by_path(const char *path,
195 const char **opts);
196
dtb_node_find_node_by_path(const char * path)197 static inline struct dtb_node *dtb_node_find_node_by_path(const char *path)
198 {
199 return dtb_node_find_node_opts_by_path(path, NULL);
200 }
201
202 rt_bool_t dtb_node_device_is_available(const struct dtb_node *device);
203 struct dtb_node *dtb_node_get_parent(const struct dtb_node *node);
204 int dtb_node_property_match_string(const struct dtb_node *dn, const char *propname,
205 const char *string);
206 int dtb_node_property_read_string_helper(const struct dtb_node *dn,
207 const char *propname, const char **out_strs,
208 size_t sz, int skip);
209 /**
210 * of_property_read_string_index() - Find and read a string from a multiple
211 * strings property.
212 * @np: device node from which the property value is to be read.
213 * @propname: name of the property to be searched.
214 * @index: index of the string in the list of strings
215 * @out_string: pointer to null terminated return string, modified only if
216 * return value is 0.
217 *
218 * Search for a property in a device tree node and retrieve a null
219 * terminated string value (pointer to data, not a copy) in the list of strings
220 * contained in that property.
221 * Returns 0 on success, -EINVAL if the property does not exist, -ENODATA if
222 * property does not have a value, and -EILSEQ if the string is not
223 * null-terminated within the length of the property data.
224 *
225 * The out_string pointer is modified only if a valid string can be decoded.
226 */
dtb_node_property_read_string_index(const struct dtb_node * dn,const char * propname,int index,const char ** output)227 static inline int dtb_node_property_read_string_index(const struct dtb_node *dn,
228 const char *propname,
229 int index, const char **output)
230 {
231 int rc = dtb_node_property_read_string_helper(dn, propname, output, 1, index);
232 return rc < 0 ? rc : 0;
233 }
234
235 /**
236 * of_property_count_strings() - Find and return the number of strings from a
237 * multiple strings property.
238 * @np: device node from which the property value is to be read.
239 * @propname: name of the property to be searched.
240 *
241 * Search for a property in a device tree node and retrieve the number of null
242 * terminated string contain in it. Returns the number of strings on
243 * success, -EINVAL if the property does not exist, -ENODATA if property
244 * does not have a value, and -EILSEQ if the string is not null-terminated
245 * within the length of the property data.
246 */
dtb_node_property_count_strings(const struct dtb_node * dn,const char * propname)247 static inline int dtb_node_property_count_strings(const struct dtb_node *dn,
248 const char *propname)
249 {
250 return dtb_node_property_read_string_helper(dn, propname, NULL, 0, 0);
251 }
252
253 struct dtb_node *dtb_node_parse_phandle(const struct dtb_node *dn,
254 const char *phandle_name, int index);
255 int dtb_node_parse_phandle_with_args(const struct dtb_node *dn,
256 const char *list_name, const char *cells_name,
257 int index, struct fdt_phandle_args *out_args);
258 int dtb_node_count_phandle_with_args(const struct dtb_node *dn,
259 const char *list_name, const char *cells_name);
260
261 /* dtb_node_addr.c */
262 const uint32_t *dtb_node_get_address(const struct dtb_node *dev, int index,
263 uint64_t *size, unsigned int *flags);
264
265 #define dtb_node_string_list(string, ...) ((char *[]){string, ##__VA_ARGS__, NULL})
266
267 #define for_each_property_string(node_ptr, property_name, str, size) \
268 for (str = dtb_node_get_dtb_node_property_value(node_ptr, property_name, &size), \
269 size += (typeof(size))(size_t)str; \
270 str && *str; \
271 str = dtb_node_get_dtb_string_list_value_next((void *)str, (void *)(size_t)size))
272
273 #define for_each_property_cell(node_ptr, property_name, value, list, size) \
274 for (list = dtb_node_get_dtb_node_property_value(node_ptr, property_name, &size), \
275 value = dtb_node_get_dtb_cell_value(list), \
276 size /= sizeof(uint32_t); \
277 size > 0; \
278 value = dtb_node_get_dtb_cell_value(++list), --size)
279
280 #define for_each_property_byte(node_ptr, property_name, value, list, size) \
281 for (list = dtb_node_get_dtb_node_property_value(node_ptr, property_name, &size), \
282 value = dtb_node_get_dtb_byte_value(list); \
283 size > 0; \
284 value = dtb_node_get_dtb_byte_value(++list), --size)
285
286 #define for_each_node_child(node_ptr) \
287 for (node_ptr = (node_ptr ? node_ptr->child : NULL); \
288 node_ptr != NULL; \
289 node_ptr = node_ptr->sibling)
290
291 #define for_each_node_sibling(node_ptr) \
292 for (node_ptr = (node_ptr ? node_ptr->sibling : NULL); \
293 node_ptr != NULL; \
294 node_ptr = node_ptr->sibling)
295
296 #define for_each_of_allnodes_from(from, dn) \
297 for (dn = dtb_node_find_all_nodes(from); dn; dn = dtb_node_find_all_nodes(dn))
298
299 #define for_each_of_all_child_nodes_from(from, dn) \
300 for (dn = dtb_node_find_all_nodes(from); dn && (dn->level > from->level); dn = dtb_node_find_all_nodes(dn))
301
302 #define for_each_of_allnodes(dn) for_each_of_allnodes_from(NULL, dn)
303
304 #define dtb_node_get(x) (x)
dtb_node_put(const struct dtb_node * np)305 static inline void dtb_node_put(const struct dtb_node *np)
306 {
307 }
308
309 /* Helper to read a big number; size is in cells (not bytes) */
dtb_node_read_number(const uint32_t * cell,int size)310 static inline uint64_t dtb_node_read_number(const uint32_t *cell, int size)
311 {
312 uint64_t r = 0;
313 while (size--)
314 r = (r << 32) | fdt32_to_cpu(*(cell++));
315 return r;
316 }
317
318 /**
319 * ofnode_valid() - check if an ofnode is valid
320 *
321 * @return true if the reference contains a valid ofnode, RT_FALSE if it is NULL
322 */
dtb_node_valid(const struct dtb_node * node)323 static inline rt_bool_t dtb_node_valid(const struct dtb_node *node)
324 {
325 if (dtb_node_active())
326 return node != NULL;
327 return RT_FALSE;
328 }
329
330 /*dtb_base.c */
331 rt_bool_t dtb_node_read_bool(const struct dtb_node *node, const char *propname);
332 const void *dtb_node_read_prop(const struct dtb_node *node, const char *propname, int *sizep);
333 const char *dtb_node_read_string(const struct dtb_node *node, const char *propname);
334 const struct dtb_node *dtb_node_find_subnode(const struct dtb_node *node, const char *subnode_name);
335 int dtb_node_read_u32_array(const struct dtb_node *node, const char *propname,
336 uint32_t *out_values, size_t sz);
337 struct dtb_node *dtb_node_first_subnode(const struct dtb_node *node);
338 struct dtb_node *dtb_node_next_subnode(const struct dtb_node *node);
339 struct dtb_node *dtb_node_get_parent(const struct dtb_node *node);
340 const char *dtb_node_get_name(const struct dtb_node *node);
341 struct dtb_node *dtb_node_get_by_phandle(uint32_t phandle);
342 int dtb_node_read_size(const struct dtb_node *node, const char *propname);
343 int dtb_node_get_addr_and_size_by_index(const struct dtb_node *node, int index, size_t *addr, size_t *size);
344 size_t dtb_node_get_addr_index(const struct dtb_node *node, int index);
345 size_t dtb_node_get_addr(const struct dtb_node *node);
346 int dtb_node_stringlist_search(const struct dtb_node *node, const char *property,
347 const char *string);
348 int dtb_node_read_string_index(const struct dtb_node *node, const char *property, int index,
349 const char **outp);
350 int dtb_node_read_string_count(const struct dtb_node *node, const char *property);
351 struct dtb_node *dtb_node_path(const char *path);
352 const char *dtb_node_get_chosen_prop(const char *name);
353 struct dtb_node *dtb_node_get_chosen_node(const char *name);
354 const void *dtb_node_get_property(const struct dtb_node *node, const char *propname, int *lenp);
355 rt_bool_t dtb_node_is_available(const struct dtb_node *node);
356 size_t dtb_node_get_addr_size(const struct dtb_node *node, const char *property,
357 size_t *sizep);
358 const uint8_t *dtb_node_read_u8_array_ptr(const struct dtb_node *node, const char *propname, size_t sz);
359 int dtb_node_find_all_compatible_node(const struct dtb_node *from, const char *compatible, struct dtb_node **node_table, int max_num, int *node_num);
360 int dtb_node_write_prop(const struct dtb_node *node, const char *propname, int len,
361 const void *value);
362 int dtb_node_write_string(const struct dtb_node *node, const char *propname, const char *value);
363 int dtb_node_set_enabled(const struct dtb_node *node, rt_bool_t value);
364 int dtb_node_irq_get(struct dtb_node *dev, int index);
365 int dtb_node_irq_get_byname(struct dtb_node *dev, const char *name);
366 int dtb_node_irq_count(struct dtb_node *dev);
367
368 /**
369 * dtb_node_for_each_subnode() - iterate over all subnodes of a parent
370 *
371 * @node: child node (ofnode, lvalue)
372 * @parent: parent node (ofnode)
373 *
374 * This is a wrapper around a for loop and is used like so:
375 *
376 * ofnode node;
377 *
378 * dtb_node_for_each_subnode(node, parent) {
379 * Use node
380 * ...
381 * }
382 *
383 * Note that this is implemented as a macro and @node is used as
384 * iterator in the loop. The parent variable can be a constant or even a
385 * literal.
386 */
387 #define dtb_node_for_each_subnode(node, parent) \
388 for (node = dtb_node_first_subnode(parent); \
389 dtb_node_valid(node); \
390 node = dtb_node_next_subnode(node))
391
392 #ifdef RT_USING_FDT_FWNODE
393 extern const struct fwnode_operations of_fwnode_ops;
dtb_fwnode_init(struct dtb_node * node)394 static inline void dtb_fwnode_init(struct dtb_node *node)
395 {
396 fwnode_init(&node->fwnode, &of_fwnode_ops);
397 }
398 #endif
399
400 #endif /* RT_FDT_H__ */
401