xref: /linux/include/linux/of.h (revision 9722c3b6)
1 /* SPDX-License-Identifier: GPL-2.0+ */
2 #ifndef _LINUX_OF_H
3 #define _LINUX_OF_H
4 /*
5  * Definitions for talking to the Open Firmware PROM on
6  * Power Macintosh and other computers.
7  *
8  * Copyright (C) 1996-2005 Paul Mackerras.
9  *
10  * Updates for PPC64 by Peter Bergner & David Engebretsen, IBM Corp.
11  * Updates for SPARC64 by David S. Miller
12  * Derived from PowerPC and Sparc prom.h files by Stephen Rothwell, IBM Corp.
13  */
14 #include <linux/types.h>
15 #include <linux/bitops.h>
16 #include <linux/cleanup.h>
17 #include <linux/errno.h>
18 #include <linux/kobject.h>
19 #include <linux/mod_devicetable.h>
20 #include <linux/property.h>
21 #include <linux/list.h>
22 
23 #include <asm/byteorder.h>
24 
25 typedef u32 phandle;
26 typedef u32 ihandle;
27 
28 struct property {
29 	char	*name;
30 	int	length;
31 	void	*value;
32 	struct property *next;
33 #if defined(CONFIG_OF_DYNAMIC) || defined(CONFIG_SPARC)
34 	unsigned long _flags;
35 #endif
36 #if defined(CONFIG_OF_PROMTREE)
37 	unsigned int unique_id;
38 #endif
39 #if defined(CONFIG_OF_KOBJ)
40 	struct bin_attribute attr;
41 #endif
42 };
43 
44 #if defined(CONFIG_SPARC)
45 struct of_irq_controller;
46 #endif
47 
48 struct device_node {
49 	const char *name;
50 	phandle phandle;
51 	const char *full_name;
52 	struct fwnode_handle fwnode;
53 
54 	struct	property *properties;
55 	struct	property *deadprops;	/* removed properties */
56 	struct	device_node *parent;
57 	struct	device_node *child;
58 	struct	device_node *sibling;
59 #if defined(CONFIG_OF_KOBJ)
60 	struct	kobject kobj;
61 #endif
62 	unsigned long _flags;
63 	void	*data;
64 #if defined(CONFIG_SPARC)
65 	unsigned int unique_id;
66 	struct of_irq_controller *irq_trans;
67 #endif
68 };
69 
70 #define MAX_PHANDLE_ARGS 16
71 struct of_phandle_args {
72 	struct device_node *np;
73 	int args_count;
74 	uint32_t args[MAX_PHANDLE_ARGS];
75 };
76 
77 struct of_phandle_iterator {
78 	/* Common iterator information */
79 	const char *cells_name;
80 	int cell_count;
81 	const struct device_node *parent;
82 
83 	/* List size information */
84 	const __be32 *list_end;
85 	const __be32 *phandle_end;
86 
87 	/* Current position state */
88 	const __be32 *cur;
89 	uint32_t cur_count;
90 	phandle phandle;
91 	struct device_node *node;
92 };
93 
94 struct of_reconfig_data {
95 	struct device_node	*dn;
96 	struct property		*prop;
97 	struct property		*old_prop;
98 };
99 
100 extern const struct kobj_type of_node_ktype;
101 extern const struct fwnode_operations of_fwnode_ops;
102 
103 /**
104  * of_node_init - initialize a devicetree node
105  * @node: Pointer to device node that has been created by kzalloc()
106  *
107  * On return the device_node refcount is set to one.  Use of_node_put()
108  * on @node when done to free the memory allocated for it.  If the node
109  * is NOT a dynamic node the memory will not be freed. The decision of
110  * whether to free the memory will be done by node->release(), which is
111  * of_node_release().
112  */
of_node_init(struct device_node * node)113 static inline void of_node_init(struct device_node *node)
114 {
115 #if defined(CONFIG_OF_KOBJ)
116 	kobject_init(&node->kobj, &of_node_ktype);
117 #endif
118 	fwnode_init(&node->fwnode, &of_fwnode_ops);
119 }
120 
121 #if defined(CONFIG_OF_KOBJ)
122 #define of_node_kobj(n) (&(n)->kobj)
123 #else
124 #define of_node_kobj(n) NULL
125 #endif
126 
127 #ifdef CONFIG_OF_DYNAMIC
128 extern struct device_node *of_node_get(struct device_node *node);
129 extern void of_node_put(struct device_node *node);
130 #else /* CONFIG_OF_DYNAMIC */
131 /* Dummy ref counting routines - to be implemented later */
of_node_get(struct device_node * node)132 static inline struct device_node *of_node_get(struct device_node *node)
133 {
134 	return node;
135 }
of_node_put(struct device_node * node)136 static inline void of_node_put(struct device_node *node) { }
137 #endif /* !CONFIG_OF_DYNAMIC */
138 DEFINE_FREE(device_node, struct device_node *, if (_T) of_node_put(_T))
139 
140 /* Pointer for first entry in chain of all nodes. */
141 extern struct device_node *of_root;
142 extern struct device_node *of_chosen;
143 extern struct device_node *of_aliases;
144 extern struct device_node *of_stdout;
145 
146 /*
147  * struct device_node flag descriptions
148  * (need to be visible even when !CONFIG_OF)
149  */
150 #define OF_DYNAMIC		1 /* (and properties) allocated via kmalloc */
151 #define OF_DETACHED		2 /* detached from the device tree */
152 #define OF_POPULATED		3 /* device already created */
153 #define OF_POPULATED_BUS	4 /* platform bus created for children */
154 #define OF_OVERLAY		5 /* allocated for an overlay */
155 #define OF_OVERLAY_FREE_CSET	6 /* in overlay cset being freed */
156 
157 #define OF_BAD_ADDR	((u64)-1)
158 
159 #ifdef CONFIG_OF
160 void of_core_init(void);
161 
is_of_node(const struct fwnode_handle * fwnode)162 static inline bool is_of_node(const struct fwnode_handle *fwnode)
163 {
164 	return !IS_ERR_OR_NULL(fwnode) && fwnode->ops == &of_fwnode_ops;
165 }
166 
167 #define to_of_node(__fwnode)						\
168 	({								\
169 		typeof(__fwnode) __to_of_node_fwnode = (__fwnode);	\
170 									\
171 		is_of_node(__to_of_node_fwnode) ?			\
172 			container_of(__to_of_node_fwnode,		\
173 				     struct device_node, fwnode) :	\
174 			NULL;						\
175 	})
176 
177 #define of_fwnode_handle(node)						\
178 	({								\
179 		typeof(node) __of_fwnode_handle_node = (node);		\
180 									\
181 		__of_fwnode_handle_node ?				\
182 			&__of_fwnode_handle_node->fwnode : NULL;	\
183 	})
184 
of_node_is_root(const struct device_node * node)185 static inline bool of_node_is_root(const struct device_node *node)
186 {
187 	return node && (node->parent == NULL);
188 }
189 
of_node_check_flag(const struct device_node * n,unsigned long flag)190 static inline int of_node_check_flag(const struct device_node *n, unsigned long flag)
191 {
192 	return test_bit(flag, &n->_flags);
193 }
194 
of_node_test_and_set_flag(struct device_node * n,unsigned long flag)195 static inline int of_node_test_and_set_flag(struct device_node *n,
196 					    unsigned long flag)
197 {
198 	return test_and_set_bit(flag, &n->_flags);
199 }
200 
of_node_set_flag(struct device_node * n,unsigned long flag)201 static inline void of_node_set_flag(struct device_node *n, unsigned long flag)
202 {
203 	set_bit(flag, &n->_flags);
204 }
205 
of_node_clear_flag(struct device_node * n,unsigned long flag)206 static inline void of_node_clear_flag(struct device_node *n, unsigned long flag)
207 {
208 	clear_bit(flag, &n->_flags);
209 }
210 
211 #if defined(CONFIG_OF_DYNAMIC) || defined(CONFIG_SPARC)
of_property_check_flag(const struct property * p,unsigned long flag)212 static inline int of_property_check_flag(const struct property *p, unsigned long flag)
213 {
214 	return test_bit(flag, &p->_flags);
215 }
216 
of_property_set_flag(struct property * p,unsigned long flag)217 static inline void of_property_set_flag(struct property *p, unsigned long flag)
218 {
219 	set_bit(flag, &p->_flags);
220 }
221 
of_property_clear_flag(struct property * p,unsigned long flag)222 static inline void of_property_clear_flag(struct property *p, unsigned long flag)
223 {
224 	clear_bit(flag, &p->_flags);
225 }
226 #endif
227 
228 extern struct device_node *__of_find_all_nodes(struct device_node *prev);
229 extern struct device_node *of_find_all_nodes(struct device_node *prev);
230 
231 /*
232  * OF address retrieval & translation
233  */
234 
235 /* Helper to read a big number; size is in cells (not bytes) */
of_read_number(const __be32 * cell,int size)236 static inline u64 of_read_number(const __be32 *cell, int size)
237 {
238 	u64 r = 0;
239 	for (; size--; cell++)
240 		r = (r << 32) | be32_to_cpu(*cell);
241 	return r;
242 }
243 
244 /* Like of_read_number, but we want an unsigned long result */
of_read_ulong(const __be32 * cell,int size)245 static inline unsigned long of_read_ulong(const __be32 *cell, int size)
246 {
247 	/* toss away upper bits if unsigned long is smaller than u64 */
248 	return of_read_number(cell, size);
249 }
250 
251 #if defined(CONFIG_SPARC)
252 #include <asm/prom.h>
253 #endif
254 
255 #define OF_IS_DYNAMIC(x) test_bit(OF_DYNAMIC, &x->_flags)
256 #define OF_MARK_DYNAMIC(x) set_bit(OF_DYNAMIC, &x->_flags)
257 
258 extern bool of_node_name_eq(const struct device_node *np, const char *name);
259 extern bool of_node_name_prefix(const struct device_node *np, const char *prefix);
260 
of_node_full_name(const struct device_node * np)261 static inline const char *of_node_full_name(const struct device_node *np)
262 {
263 	return np ? np->full_name : "<no-node>";
264 }
265 
266 #define for_each_of_allnodes_from(from, dn) \
267 	for (dn = __of_find_all_nodes(from); dn; dn = __of_find_all_nodes(dn))
268 #define for_each_of_allnodes(dn) for_each_of_allnodes_from(NULL, dn)
269 extern struct device_node *of_find_node_by_name(struct device_node *from,
270 	const char *name);
271 extern struct device_node *of_find_node_by_type(struct device_node *from,
272 	const char *type);
273 extern struct device_node *of_find_compatible_node(struct device_node *from,
274 	const char *type, const char *compat);
275 extern struct device_node *of_find_matching_node_and_match(
276 	struct device_node *from,
277 	const struct of_device_id *matches,
278 	const struct of_device_id **match);
279 
280 extern struct device_node *of_find_node_opts_by_path(const char *path,
281 	const char **opts);
of_find_node_by_path(const char * path)282 static inline struct device_node *of_find_node_by_path(const char *path)
283 {
284 	return of_find_node_opts_by_path(path, NULL);
285 }
286 
287 extern struct device_node *of_find_node_by_phandle(phandle handle);
288 extern struct device_node *of_get_parent(const struct device_node *node);
289 extern struct device_node *of_get_next_parent(struct device_node *node);
290 extern struct device_node *of_get_next_child(const struct device_node *node,
291 					     struct device_node *prev);
292 extern struct device_node *of_get_next_available_child(
293 	const struct device_node *node, struct device_node *prev);
294 extern struct device_node *of_get_next_reserved_child(
295 	const struct device_node *node, struct device_node *prev);
296 
297 extern struct device_node *of_get_compatible_child(const struct device_node *parent,
298 					const char *compatible);
299 extern struct device_node *of_get_child_by_name(const struct device_node *node,
300 					const char *name);
301 
302 /* cache lookup */
303 extern struct device_node *of_find_next_cache_node(const struct device_node *);
304 extern int of_find_last_cache_level(unsigned int cpu);
305 extern struct device_node *of_find_node_with_property(
306 	struct device_node *from, const char *prop_name);
307 
308 extern struct property *of_find_property(const struct device_node *np,
309 					 const char *name,
310 					 int *lenp);
311 extern int of_property_count_elems_of_size(const struct device_node *np,
312 				const char *propname, int elem_size);
313 extern int of_property_read_u32_index(const struct device_node *np,
314 				       const char *propname,
315 				       u32 index, u32 *out_value);
316 extern int of_property_read_u64_index(const struct device_node *np,
317 				       const char *propname,
318 				       u32 index, u64 *out_value);
319 extern int of_property_read_variable_u8_array(const struct device_node *np,
320 					const char *propname, u8 *out_values,
321 					size_t sz_min, size_t sz_max);
322 extern int of_property_read_variable_u16_array(const struct device_node *np,
323 					const char *propname, u16 *out_values,
324 					size_t sz_min, size_t sz_max);
325 extern int of_property_read_variable_u32_array(const struct device_node *np,
326 					const char *propname,
327 					u32 *out_values,
328 					size_t sz_min,
329 					size_t sz_max);
330 extern int of_property_read_u64(const struct device_node *np,
331 				const char *propname, u64 *out_value);
332 extern int of_property_read_variable_u64_array(const struct device_node *np,
333 					const char *propname,
334 					u64 *out_values,
335 					size_t sz_min,
336 					size_t sz_max);
337 
338 extern int of_property_read_string(const struct device_node *np,
339 				   const char *propname,
340 				   const char **out_string);
341 extern int of_property_match_string(const struct device_node *np,
342 				    const char *propname,
343 				    const char *string);
344 extern int of_property_read_string_helper(const struct device_node *np,
345 					      const char *propname,
346 					      const char **out_strs, size_t sz, int index);
347 extern int of_device_is_compatible(const struct device_node *device,
348 				   const char *);
349 extern int of_device_compatible_match(const struct device_node *device,
350 				      const char *const *compat);
351 extern bool of_device_is_available(const struct device_node *device);
352 extern bool of_device_is_big_endian(const struct device_node *device);
353 extern const void *of_get_property(const struct device_node *node,
354 				const char *name,
355 				int *lenp);
356 extern struct device_node *of_get_cpu_node(int cpu, unsigned int *thread);
357 extern struct device_node *of_cpu_device_node_get(int cpu);
358 extern int of_cpu_node_to_id(struct device_node *np);
359 extern struct device_node *of_get_next_cpu_node(struct device_node *prev);
360 extern struct device_node *of_get_cpu_state_node(struct device_node *cpu_node,
361 						 int index);
362 extern u64 of_get_cpu_hwid(struct device_node *cpun, unsigned int thread);
363 
364 extern int of_n_addr_cells(struct device_node *np);
365 extern int of_n_size_cells(struct device_node *np);
366 extern const struct of_device_id *of_match_node(
367 	const struct of_device_id *matches, const struct device_node *node);
368 extern const void *of_device_get_match_data(const struct device *dev);
369 extern int of_alias_from_compatible(const struct device_node *node, char *alias,
370 				    int len);
371 extern void of_print_phandle_args(const char *msg, const struct of_phandle_args *args);
372 extern int __of_parse_phandle_with_args(const struct device_node *np,
373 	const char *list_name, const char *cells_name, int cell_count,
374 	int index, struct of_phandle_args *out_args);
375 extern int of_parse_phandle_with_args_map(const struct device_node *np,
376 	const char *list_name, const char *stem_name, int index,
377 	struct of_phandle_args *out_args);
378 extern int of_count_phandle_with_args(const struct device_node *np,
379 	const char *list_name, const char *cells_name);
380 
381 /* module functions */
382 extern ssize_t of_modalias(const struct device_node *np, char *str, ssize_t len);
383 extern int of_request_module(const struct device_node *np);
384 
385 /* phandle iterator functions */
386 extern int of_phandle_iterator_init(struct of_phandle_iterator *it,
387 				    const struct device_node *np,
388 				    const char *list_name,
389 				    const char *cells_name,
390 				    int cell_count);
391 
392 extern int of_phandle_iterator_next(struct of_phandle_iterator *it);
393 extern int of_phandle_iterator_args(struct of_phandle_iterator *it,
394 				    uint32_t *args,
395 				    int size);
396 
397 extern void of_alias_scan(void * (*dt_alloc)(u64 size, u64 align));
398 extern int of_alias_get_id(struct device_node *np, const char *stem);
399 extern int of_alias_get_highest_id(const char *stem);
400 
401 bool of_machine_compatible_match(const char *const *compats);
402 
403 /**
404  * of_machine_is_compatible - Test root of device tree for a given compatible value
405  * @compat: compatible string to look for in root node's compatible property.
406  *
407  * Return: true if the root node has the given value in its compatible property.
408  */
of_machine_is_compatible(const char * compat)409 static inline bool of_machine_is_compatible(const char *compat)
410 {
411 	const char *compats[] = { compat, NULL };
412 
413 	return of_machine_compatible_match(compats);
414 }
415 
416 extern int of_add_property(struct device_node *np, struct property *prop);
417 extern int of_remove_property(struct device_node *np, struct property *prop);
418 extern int of_update_property(struct device_node *np, struct property *newprop);
419 
420 /* For updating the device tree at runtime */
421 #define OF_RECONFIG_ATTACH_NODE		0x0001
422 #define OF_RECONFIG_DETACH_NODE		0x0002
423 #define OF_RECONFIG_ADD_PROPERTY	0x0003
424 #define OF_RECONFIG_REMOVE_PROPERTY	0x0004
425 #define OF_RECONFIG_UPDATE_PROPERTY	0x0005
426 
427 extern int of_attach_node(struct device_node *);
428 extern int of_detach_node(struct device_node *);
429 
430 #define of_match_ptr(_ptr)	(_ptr)
431 
432 /*
433  * u32 u;
434  *
435  * of_property_for_each_u32(np, "propname", u)
436  *         printk("U32 value: %x\n", u);
437  */
438 const __be32 *of_prop_next_u32(struct property *prop, const __be32 *cur,
439 			       u32 *pu);
440 /*
441  * struct property *prop;
442  * const char *s;
443  *
444  * of_property_for_each_string(np, "propname", prop, s)
445  *         printk("String value: %s\n", s);
446  */
447 const char *of_prop_next_string(struct property *prop, const char *cur);
448 
449 bool of_console_check(struct device_node *dn, char *name, int index);
450 
451 int of_map_id(struct device_node *np, u32 id,
452 	       const char *map_name, const char *map_mask_name,
453 	       struct device_node **target, u32 *id_out);
454 
455 phys_addr_t of_dma_get_max_cpu_address(struct device_node *np);
456 
457 struct kimage;
458 void *of_kexec_alloc_and_setup_fdt(const struct kimage *image,
459 				   unsigned long initrd_load_addr,
460 				   unsigned long initrd_len,
461 				   const char *cmdline, size_t extra_fdt_size);
462 #else /* CONFIG_OF */
463 
of_core_init(void)464 static inline void of_core_init(void)
465 {
466 }
467 
is_of_node(const struct fwnode_handle * fwnode)468 static inline bool is_of_node(const struct fwnode_handle *fwnode)
469 {
470 	return false;
471 }
472 
to_of_node(const struct fwnode_handle * fwnode)473 static inline struct device_node *to_of_node(const struct fwnode_handle *fwnode)
474 {
475 	return NULL;
476 }
477 
of_node_name_eq(const struct device_node * np,const char * name)478 static inline bool of_node_name_eq(const struct device_node *np, const char *name)
479 {
480 	return false;
481 }
482 
of_node_name_prefix(const struct device_node * np,const char * prefix)483 static inline bool of_node_name_prefix(const struct device_node *np, const char *prefix)
484 {
485 	return false;
486 }
487 
of_node_full_name(const struct device_node * np)488 static inline const char* of_node_full_name(const struct device_node *np)
489 {
490 	return "<no-node>";
491 }
492 
of_find_node_by_name(struct device_node * from,const char * name)493 static inline struct device_node *of_find_node_by_name(struct device_node *from,
494 	const char *name)
495 {
496 	return NULL;
497 }
498 
of_find_node_by_type(struct device_node * from,const char * type)499 static inline struct device_node *of_find_node_by_type(struct device_node *from,
500 	const char *type)
501 {
502 	return NULL;
503 }
504 
of_find_matching_node_and_match(struct device_node * from,const struct of_device_id * matches,const struct of_device_id ** match)505 static inline struct device_node *of_find_matching_node_and_match(
506 	struct device_node *from,
507 	const struct of_device_id *matches,
508 	const struct of_device_id **match)
509 {
510 	return NULL;
511 }
512 
of_find_node_by_path(const char * path)513 static inline struct device_node *of_find_node_by_path(const char *path)
514 {
515 	return NULL;
516 }
517 
of_find_node_opts_by_path(const char * path,const char ** opts)518 static inline struct device_node *of_find_node_opts_by_path(const char *path,
519 	const char **opts)
520 {
521 	return NULL;
522 }
523 
of_find_node_by_phandle(phandle handle)524 static inline struct device_node *of_find_node_by_phandle(phandle handle)
525 {
526 	return NULL;
527 }
528 
of_get_parent(const struct device_node * node)529 static inline struct device_node *of_get_parent(const struct device_node *node)
530 {
531 	return NULL;
532 }
533 
of_get_next_parent(struct device_node * node)534 static inline struct device_node *of_get_next_parent(struct device_node *node)
535 {
536 	return NULL;
537 }
538 
of_get_next_child(const struct device_node * node,struct device_node * prev)539 static inline struct device_node *of_get_next_child(
540 	const struct device_node *node, struct device_node *prev)
541 {
542 	return NULL;
543 }
544 
of_get_next_available_child(const struct device_node * node,struct device_node * prev)545 static inline struct device_node *of_get_next_available_child(
546 	const struct device_node *node, struct device_node *prev)
547 {
548 	return NULL;
549 }
550 
of_get_next_reserved_child(const struct device_node * node,struct device_node * prev)551 static inline struct device_node *of_get_next_reserved_child(
552 	const struct device_node *node, struct device_node *prev)
553 {
554 	return NULL;
555 }
556 
of_find_node_with_property(struct device_node * from,const char * prop_name)557 static inline struct device_node *of_find_node_with_property(
558 	struct device_node *from, const char *prop_name)
559 {
560 	return NULL;
561 }
562 
563 #define of_fwnode_handle(node) NULL
564 
of_get_compatible_child(const struct device_node * parent,const char * compatible)565 static inline struct device_node *of_get_compatible_child(const struct device_node *parent,
566 					const char *compatible)
567 {
568 	return NULL;
569 }
570 
of_get_child_by_name(const struct device_node * node,const char * name)571 static inline struct device_node *of_get_child_by_name(
572 					const struct device_node *node,
573 					const char *name)
574 {
575 	return NULL;
576 }
577 
of_device_is_compatible(const struct device_node * device,const char * name)578 static inline int of_device_is_compatible(const struct device_node *device,
579 					  const char *name)
580 {
581 	return 0;
582 }
583 
of_device_compatible_match(const struct device_node * device,const char * const * compat)584 static inline  int of_device_compatible_match(const struct device_node *device,
585 					      const char *const *compat)
586 {
587 	return 0;
588 }
589 
of_device_is_available(const struct device_node * device)590 static inline bool of_device_is_available(const struct device_node *device)
591 {
592 	return false;
593 }
594 
of_device_is_big_endian(const struct device_node * device)595 static inline bool of_device_is_big_endian(const struct device_node *device)
596 {
597 	return false;
598 }
599 
of_find_property(const struct device_node * np,const char * name,int * lenp)600 static inline struct property *of_find_property(const struct device_node *np,
601 						const char *name,
602 						int *lenp)
603 {
604 	return NULL;
605 }
606 
of_find_compatible_node(struct device_node * from,const char * type,const char * compat)607 static inline struct device_node *of_find_compatible_node(
608 						struct device_node *from,
609 						const char *type,
610 						const char *compat)
611 {
612 	return NULL;
613 }
614 
of_property_count_elems_of_size(const struct device_node * np,const char * propname,int elem_size)615 static inline int of_property_count_elems_of_size(const struct device_node *np,
616 			const char *propname, int elem_size)
617 {
618 	return -ENOSYS;
619 }
620 
of_property_read_u32_index(const struct device_node * np,const char * propname,u32 index,u32 * out_value)621 static inline int of_property_read_u32_index(const struct device_node *np,
622 			const char *propname, u32 index, u32 *out_value)
623 {
624 	return -ENOSYS;
625 }
626 
of_property_read_u64_index(const struct device_node * np,const char * propname,u32 index,u64 * out_value)627 static inline int of_property_read_u64_index(const struct device_node *np,
628 			const char *propname, u32 index, u64 *out_value)
629 {
630 	return -ENOSYS;
631 }
632 
of_get_property(const struct device_node * node,const char * name,int * lenp)633 static inline const void *of_get_property(const struct device_node *node,
634 				const char *name,
635 				int *lenp)
636 {
637 	return NULL;
638 }
639 
of_get_cpu_node(int cpu,unsigned int * thread)640 static inline struct device_node *of_get_cpu_node(int cpu,
641 					unsigned int *thread)
642 {
643 	return NULL;
644 }
645 
of_cpu_device_node_get(int cpu)646 static inline struct device_node *of_cpu_device_node_get(int cpu)
647 {
648 	return NULL;
649 }
650 
of_cpu_node_to_id(struct device_node * np)651 static inline int of_cpu_node_to_id(struct device_node *np)
652 {
653 	return -ENODEV;
654 }
655 
of_get_next_cpu_node(struct device_node * prev)656 static inline struct device_node *of_get_next_cpu_node(struct device_node *prev)
657 {
658 	return NULL;
659 }
660 
of_get_cpu_state_node(struct device_node * cpu_node,int index)661 static inline struct device_node *of_get_cpu_state_node(struct device_node *cpu_node,
662 					int index)
663 {
664 	return NULL;
665 }
666 
of_n_addr_cells(struct device_node * np)667 static inline int of_n_addr_cells(struct device_node *np)
668 {
669 	return 0;
670 
671 }
of_n_size_cells(struct device_node * np)672 static inline int of_n_size_cells(struct device_node *np)
673 {
674 	return 0;
675 }
676 
of_property_read_variable_u8_array(const struct device_node * np,const char * propname,u8 * out_values,size_t sz_min,size_t sz_max)677 static inline int of_property_read_variable_u8_array(const struct device_node *np,
678 					const char *propname, u8 *out_values,
679 					size_t sz_min, size_t sz_max)
680 {
681 	return -ENOSYS;
682 }
683 
of_property_read_variable_u16_array(const struct device_node * np,const char * propname,u16 * out_values,size_t sz_min,size_t sz_max)684 static inline int of_property_read_variable_u16_array(const struct device_node *np,
685 					const char *propname, u16 *out_values,
686 					size_t sz_min, size_t sz_max)
687 {
688 	return -ENOSYS;
689 }
690 
of_property_read_variable_u32_array(const struct device_node * np,const char * propname,u32 * out_values,size_t sz_min,size_t sz_max)691 static inline int of_property_read_variable_u32_array(const struct device_node *np,
692 					const char *propname,
693 					u32 *out_values,
694 					size_t sz_min,
695 					size_t sz_max)
696 {
697 	return -ENOSYS;
698 }
699 
of_property_read_u64(const struct device_node * np,const char * propname,u64 * out_value)700 static inline int of_property_read_u64(const struct device_node *np,
701 				       const char *propname, u64 *out_value)
702 {
703 	return -ENOSYS;
704 }
705 
of_property_read_variable_u64_array(const struct device_node * np,const char * propname,u64 * out_values,size_t sz_min,size_t sz_max)706 static inline int of_property_read_variable_u64_array(const struct device_node *np,
707 					const char *propname,
708 					u64 *out_values,
709 					size_t sz_min,
710 					size_t sz_max)
711 {
712 	return -ENOSYS;
713 }
714 
of_property_read_string(const struct device_node * np,const char * propname,const char ** out_string)715 static inline int of_property_read_string(const struct device_node *np,
716 					  const char *propname,
717 					  const char **out_string)
718 {
719 	return -ENOSYS;
720 }
721 
of_property_match_string(const struct device_node * np,const char * propname,const char * string)722 static inline int of_property_match_string(const struct device_node *np,
723 					   const char *propname,
724 					   const char *string)
725 {
726 	return -ENOSYS;
727 }
728 
of_property_read_string_helper(const struct device_node * np,const char * propname,const char ** out_strs,size_t sz,int index)729 static inline int of_property_read_string_helper(const struct device_node *np,
730 						 const char *propname,
731 						 const char **out_strs, size_t sz, int index)
732 {
733 	return -ENOSYS;
734 }
735 
__of_parse_phandle_with_args(const struct device_node * np,const char * list_name,const char * cells_name,int cell_count,int index,struct of_phandle_args * out_args)736 static inline int __of_parse_phandle_with_args(const struct device_node *np,
737 					       const char *list_name,
738 					       const char *cells_name,
739 					       int cell_count,
740 					       int index,
741 					       struct of_phandle_args *out_args)
742 {
743 	return -ENOSYS;
744 }
745 
of_parse_phandle_with_args_map(const struct device_node * np,const char * list_name,const char * stem_name,int index,struct of_phandle_args * out_args)746 static inline int of_parse_phandle_with_args_map(const struct device_node *np,
747 						 const char *list_name,
748 						 const char *stem_name,
749 						 int index,
750 						 struct of_phandle_args *out_args)
751 {
752 	return -ENOSYS;
753 }
754 
of_count_phandle_with_args(const struct device_node * np,const char * list_name,const char * cells_name)755 static inline int of_count_phandle_with_args(const struct device_node *np,
756 					     const char *list_name,
757 					     const char *cells_name)
758 {
759 	return -ENOSYS;
760 }
761 
of_modalias(const struct device_node * np,char * str,ssize_t len)762 static inline ssize_t of_modalias(const struct device_node *np, char *str,
763 				  ssize_t len)
764 {
765 	return -ENODEV;
766 }
767 
of_request_module(const struct device_node * np)768 static inline int of_request_module(const struct device_node *np)
769 {
770 	return -ENODEV;
771 }
772 
of_phandle_iterator_init(struct of_phandle_iterator * it,const struct device_node * np,const char * list_name,const char * cells_name,int cell_count)773 static inline int of_phandle_iterator_init(struct of_phandle_iterator *it,
774 					   const struct device_node *np,
775 					   const char *list_name,
776 					   const char *cells_name,
777 					   int cell_count)
778 {
779 	return -ENOSYS;
780 }
781 
of_phandle_iterator_next(struct of_phandle_iterator * it)782 static inline int of_phandle_iterator_next(struct of_phandle_iterator *it)
783 {
784 	return -ENOSYS;
785 }
786 
of_phandle_iterator_args(struct of_phandle_iterator * it,uint32_t * args,int size)787 static inline int of_phandle_iterator_args(struct of_phandle_iterator *it,
788 					   uint32_t *args,
789 					   int size)
790 {
791 	return 0;
792 }
793 
of_alias_get_id(struct device_node * np,const char * stem)794 static inline int of_alias_get_id(struct device_node *np, const char *stem)
795 {
796 	return -ENOSYS;
797 }
798 
of_alias_get_highest_id(const char * stem)799 static inline int of_alias_get_highest_id(const char *stem)
800 {
801 	return -ENOSYS;
802 }
803 
of_machine_is_compatible(const char * compat)804 static inline int of_machine_is_compatible(const char *compat)
805 {
806 	return 0;
807 }
808 
of_add_property(struct device_node * np,struct property * prop)809 static inline int of_add_property(struct device_node *np, struct property *prop)
810 {
811 	return 0;
812 }
813 
of_remove_property(struct device_node * np,struct property * prop)814 static inline int of_remove_property(struct device_node *np, struct property *prop)
815 {
816 	return 0;
817 }
818 
of_machine_compatible_match(const char * const * compats)819 static inline bool of_machine_compatible_match(const char *const *compats)
820 {
821 	return false;
822 }
823 
of_console_check(const struct device_node * dn,const char * name,int index)824 static inline bool of_console_check(const struct device_node *dn, const char *name, int index)
825 {
826 	return false;
827 }
828 
of_prop_next_u32(struct property * prop,const __be32 * cur,u32 * pu)829 static inline const __be32 *of_prop_next_u32(struct property *prop,
830 		const __be32 *cur, u32 *pu)
831 {
832 	return NULL;
833 }
834 
of_prop_next_string(struct property * prop,const char * cur)835 static inline const char *of_prop_next_string(struct property *prop,
836 		const char *cur)
837 {
838 	return NULL;
839 }
840 
of_node_check_flag(struct device_node * n,unsigned long flag)841 static inline int of_node_check_flag(struct device_node *n, unsigned long flag)
842 {
843 	return 0;
844 }
845 
of_node_test_and_set_flag(struct device_node * n,unsigned long flag)846 static inline int of_node_test_and_set_flag(struct device_node *n,
847 					    unsigned long flag)
848 {
849 	return 0;
850 }
851 
of_node_set_flag(struct device_node * n,unsigned long flag)852 static inline void of_node_set_flag(struct device_node *n, unsigned long flag)
853 {
854 }
855 
of_node_clear_flag(struct device_node * n,unsigned long flag)856 static inline void of_node_clear_flag(struct device_node *n, unsigned long flag)
857 {
858 }
859 
of_property_check_flag(const struct property * p,unsigned long flag)860 static inline int of_property_check_flag(const struct property *p,
861 					 unsigned long flag)
862 {
863 	return 0;
864 }
865 
of_property_set_flag(struct property * p,unsigned long flag)866 static inline void of_property_set_flag(struct property *p, unsigned long flag)
867 {
868 }
869 
of_property_clear_flag(struct property * p,unsigned long flag)870 static inline void of_property_clear_flag(struct property *p, unsigned long flag)
871 {
872 }
873 
of_map_id(struct device_node * np,u32 id,const char * map_name,const char * map_mask_name,struct device_node ** target,u32 * id_out)874 static inline int of_map_id(struct device_node *np, u32 id,
875 			     const char *map_name, const char *map_mask_name,
876 			     struct device_node **target, u32 *id_out)
877 {
878 	return -EINVAL;
879 }
880 
of_dma_get_max_cpu_address(struct device_node * np)881 static inline phys_addr_t of_dma_get_max_cpu_address(struct device_node *np)
882 {
883 	return PHYS_ADDR_MAX;
884 }
885 
of_device_get_match_data(const struct device * dev)886 static inline const void *of_device_get_match_data(const struct device *dev)
887 {
888 	return NULL;
889 }
890 
891 #define of_match_ptr(_ptr)	NULL
892 #define of_match_node(_matches, _node)	NULL
893 #endif /* CONFIG_OF */
894 
895 /* Default string compare functions, Allow arch asm/prom.h to override */
896 #if !defined(of_compat_cmp)
897 #define of_compat_cmp(s1, s2, l)	strcasecmp((s1), (s2))
898 #define of_prop_cmp(s1, s2)		strcmp((s1), (s2))
899 #define of_node_cmp(s1, s2)		strcasecmp((s1), (s2))
900 #endif
901 
of_prop_val_eq(struct property * p1,struct property * p2)902 static inline int of_prop_val_eq(struct property *p1, struct property *p2)
903 {
904 	return p1->length == p2->length &&
905 	       !memcmp(p1->value, p2->value, (size_t)p1->length);
906 }
907 
908 #define for_each_property_of_node(dn, pp) \
909 	for (pp = dn->properties; pp != NULL; pp = pp->next)
910 
911 #if defined(CONFIG_OF) && defined(CONFIG_NUMA)
912 extern int of_node_to_nid(struct device_node *np);
913 #else
of_node_to_nid(struct device_node * device)914 static inline int of_node_to_nid(struct device_node *device)
915 {
916 	return NUMA_NO_NODE;
917 }
918 #endif
919 
920 #ifdef CONFIG_OF_NUMA
921 extern int of_numa_init(void);
922 #else
of_numa_init(void)923 static inline int of_numa_init(void)
924 {
925 	return -ENOSYS;
926 }
927 #endif
928 
of_find_matching_node(struct device_node * from,const struct of_device_id * matches)929 static inline struct device_node *of_find_matching_node(
930 	struct device_node *from,
931 	const struct of_device_id *matches)
932 {
933 	return of_find_matching_node_and_match(from, matches, NULL);
934 }
935 
of_node_get_device_type(const struct device_node * np)936 static inline const char *of_node_get_device_type(const struct device_node *np)
937 {
938 	return of_get_property(np, "device_type", NULL);
939 }
940 
of_node_is_type(const struct device_node * np,const char * type)941 static inline bool of_node_is_type(const struct device_node *np, const char *type)
942 {
943 	const char *match = of_node_get_device_type(np);
944 
945 	return np && match && type && !strcmp(match, type);
946 }
947 
948 /**
949  * of_parse_phandle - Resolve a phandle property to a device_node pointer
950  * @np: Pointer to device node holding phandle property
951  * @phandle_name: Name of property holding a phandle value
952  * @index: For properties holding a table of phandles, this is the index into
953  *         the table
954  *
955  * Return: The device_node pointer with refcount incremented.  Use
956  * of_node_put() on it when done.
957  */
of_parse_phandle(const struct device_node * np,const char * phandle_name,int index)958 static inline struct device_node *of_parse_phandle(const struct device_node *np,
959 						   const char *phandle_name,
960 						   int index)
961 {
962 	struct of_phandle_args args;
963 
964 	if (__of_parse_phandle_with_args(np, phandle_name, NULL, 0,
965 					 index, &args))
966 		return NULL;
967 
968 	return args.np;
969 }
970 
971 /**
972  * of_parse_phandle_with_args() - Find a node pointed by phandle in a list
973  * @np:		pointer to a device tree node containing a list
974  * @list_name:	property name that contains a list
975  * @cells_name:	property name that specifies phandles' arguments count
976  * @index:	index of a phandle to parse out
977  * @out_args:	optional pointer to output arguments structure (will be filled)
978  *
979  * This function is useful to parse lists of phandles and their arguments.
980  * Returns 0 on success and fills out_args, on error returns appropriate
981  * errno value.
982  *
983  * Caller is responsible to call of_node_put() on the returned out_args->np
984  * pointer.
985  *
986  * Example::
987  *
988  *  phandle1: node1 {
989  *	#list-cells = <2>;
990  *  };
991  *
992  *  phandle2: node2 {
993  *	#list-cells = <1>;
994  *  };
995  *
996  *  node3 {
997  *	list = <&phandle1 1 2 &phandle2 3>;
998  *  };
999  *
1000  * To get a device_node of the ``node2`` node you may call this:
1001  * of_parse_phandle_with_args(node3, "list", "#list-cells", 1, &args);
1002  */
of_parse_phandle_with_args(const struct device_node * np,const char * list_name,const char * cells_name,int index,struct of_phandle_args * out_args)1003 static inline int of_parse_phandle_with_args(const struct device_node *np,
1004 					     const char *list_name,
1005 					     const char *cells_name,
1006 					     int index,
1007 					     struct of_phandle_args *out_args)
1008 {
1009 	int cell_count = -1;
1010 
1011 	/* If cells_name is NULL we assume a cell count of 0 */
1012 	if (!cells_name)
1013 		cell_count = 0;
1014 
1015 	return __of_parse_phandle_with_args(np, list_name, cells_name,
1016 					    cell_count, index, out_args);
1017 }
1018 
1019 /**
1020  * of_parse_phandle_with_fixed_args() - Find a node pointed by phandle in a list
1021  * @np:		pointer to a device tree node containing a list
1022  * @list_name:	property name that contains a list
1023  * @cell_count: number of argument cells following the phandle
1024  * @index:	index of a phandle to parse out
1025  * @out_args:	optional pointer to output arguments structure (will be filled)
1026  *
1027  * This function is useful to parse lists of phandles and their arguments.
1028  * Returns 0 on success and fills out_args, on error returns appropriate
1029  * errno value.
1030  *
1031  * Caller is responsible to call of_node_put() on the returned out_args->np
1032  * pointer.
1033  *
1034  * Example::
1035  *
1036  *  phandle1: node1 {
1037  *  };
1038  *
1039  *  phandle2: node2 {
1040  *  };
1041  *
1042  *  node3 {
1043  *	list = <&phandle1 0 2 &phandle2 2 3>;
1044  *  };
1045  *
1046  * To get a device_node of the ``node2`` node you may call this:
1047  * of_parse_phandle_with_fixed_args(node3, "list", 2, 1, &args);
1048  */
of_parse_phandle_with_fixed_args(const struct device_node * np,const char * list_name,int cell_count,int index,struct of_phandle_args * out_args)1049 static inline int of_parse_phandle_with_fixed_args(const struct device_node *np,
1050 						   const char *list_name,
1051 						   int cell_count,
1052 						   int index,
1053 						   struct of_phandle_args *out_args)
1054 {
1055 	return __of_parse_phandle_with_args(np, list_name, NULL, cell_count,
1056 					    index, out_args);
1057 }
1058 
1059 /**
1060  * of_parse_phandle_with_optional_args() - Find a node pointed by phandle in a list
1061  * @np:		pointer to a device tree node containing a list
1062  * @list_name:	property name that contains a list
1063  * @cells_name:	property name that specifies phandles' arguments count
1064  * @index:	index of a phandle to parse out
1065  * @out_args:	optional pointer to output arguments structure (will be filled)
1066  *
1067  * Same as of_parse_phandle_with_args() except that if the cells_name property
1068  * is not found, cell_count of 0 is assumed.
1069  *
1070  * This is used to useful, if you have a phandle which didn't have arguments
1071  * before and thus doesn't have a '#*-cells' property but is now migrated to
1072  * having arguments while retaining backwards compatibility.
1073  */
of_parse_phandle_with_optional_args(const struct device_node * np,const char * list_name,const char * cells_name,int index,struct of_phandle_args * out_args)1074 static inline int of_parse_phandle_with_optional_args(const struct device_node *np,
1075 						      const char *list_name,
1076 						      const char *cells_name,
1077 						      int index,
1078 						      struct of_phandle_args *out_args)
1079 {
1080 	return __of_parse_phandle_with_args(np, list_name, cells_name,
1081 					    0, index, out_args);
1082 }
1083 
1084 /**
1085  * of_phandle_args_equal() - Compare two of_phandle_args
1086  * @a1:		First of_phandle_args to compare
1087  * @a2:		Second of_phandle_args to compare
1088  *
1089  * Return: True if a1 and a2 are the same (same node pointer, same phandle
1090  * args), false otherwise.
1091  */
of_phandle_args_equal(const struct of_phandle_args * a1,const struct of_phandle_args * a2)1092 static inline bool of_phandle_args_equal(const struct of_phandle_args *a1,
1093 					 const struct of_phandle_args *a2)
1094 {
1095 	return a1->np == a2->np &&
1096 	       a1->args_count == a2->args_count &&
1097 	       !memcmp(a1->args, a2->args, sizeof(a1->args[0]) * a1->args_count);
1098 }
1099 
1100 /**
1101  * of_property_count_u8_elems - Count the number of u8 elements in a property
1102  *
1103  * @np:		device node from which the property value is to be read.
1104  * @propname:	name of the property to be searched.
1105  *
1106  * Search for a property in a device node and count the number of u8 elements
1107  * in it.
1108  *
1109  * Return: The number of elements on sucess, -EINVAL if the property does
1110  * not exist or its length does not match a multiple of u8 and -ENODATA if the
1111  * property does not have a value.
1112  */
of_property_count_u8_elems(const struct device_node * np,const char * propname)1113 static inline int of_property_count_u8_elems(const struct device_node *np,
1114 				const char *propname)
1115 {
1116 	return of_property_count_elems_of_size(np, propname, sizeof(u8));
1117 }
1118 
1119 /**
1120  * of_property_count_u16_elems - Count the number of u16 elements in a property
1121  *
1122  * @np:		device node from which the property value is to be read.
1123  * @propname:	name of the property to be searched.
1124  *
1125  * Search for a property in a device node and count the number of u16 elements
1126  * in it.
1127  *
1128  * Return: The number of elements on sucess, -EINVAL if the property does
1129  * not exist or its length does not match a multiple of u16 and -ENODATA if the
1130  * property does not have a value.
1131  */
of_property_count_u16_elems(const struct device_node * np,const char * propname)1132 static inline int of_property_count_u16_elems(const struct device_node *np,
1133 				const char *propname)
1134 {
1135 	return of_property_count_elems_of_size(np, propname, sizeof(u16));
1136 }
1137 
1138 /**
1139  * of_property_count_u32_elems - Count the number of u32 elements in a property
1140  *
1141  * @np:		device node from which the property value is to be read.
1142  * @propname:	name of the property to be searched.
1143  *
1144  * Search for a property in a device node and count the number of u32 elements
1145  * in it.
1146  *
1147  * Return: The number of elements on sucess, -EINVAL if the property does
1148  * not exist or its length does not match a multiple of u32 and -ENODATA if the
1149  * property does not have a value.
1150  */
of_property_count_u32_elems(const struct device_node * np,const char * propname)1151 static inline int of_property_count_u32_elems(const struct device_node *np,
1152 				const char *propname)
1153 {
1154 	return of_property_count_elems_of_size(np, propname, sizeof(u32));
1155 }
1156 
1157 /**
1158  * of_property_count_u64_elems - Count the number of u64 elements in a property
1159  *
1160  * @np:		device node from which the property value is to be read.
1161  * @propname:	name of the property to be searched.
1162  *
1163  * Search for a property in a device node and count the number of u64 elements
1164  * in it.
1165  *
1166  * Return: The number of elements on sucess, -EINVAL if the property does
1167  * not exist or its length does not match a multiple of u64 and -ENODATA if the
1168  * property does not have a value.
1169  */
of_property_count_u64_elems(const struct device_node * np,const char * propname)1170 static inline int of_property_count_u64_elems(const struct device_node *np,
1171 				const char *propname)
1172 {
1173 	return of_property_count_elems_of_size(np, propname, sizeof(u64));
1174 }
1175 
1176 /**
1177  * of_property_read_string_array() - Read an array of strings from a multiple
1178  * strings property.
1179  * @np:		device node from which the property value is to be read.
1180  * @propname:	name of the property to be searched.
1181  * @out_strs:	output array of string pointers.
1182  * @sz:		number of array elements to read.
1183  *
1184  * Search for a property in a device tree node and retrieve a list of
1185  * terminated string values (pointer to data, not a copy) in that property.
1186  *
1187  * Return: If @out_strs is NULL, the number of strings in the property is returned.
1188  */
of_property_read_string_array(const struct device_node * np,const char * propname,const char ** out_strs,size_t sz)1189 static inline int of_property_read_string_array(const struct device_node *np,
1190 						const char *propname, const char **out_strs,
1191 						size_t sz)
1192 {
1193 	return of_property_read_string_helper(np, propname, out_strs, sz, 0);
1194 }
1195 
1196 /**
1197  * of_property_count_strings() - Find and return the number of strings from a
1198  * multiple strings property.
1199  * @np:		device node from which the property value is to be read.
1200  * @propname:	name of the property to be searched.
1201  *
1202  * Search for a property in a device tree node and retrieve the number of null
1203  * terminated string contain in it.
1204  *
1205  * Return: The number of strings on success, -EINVAL if the property does not
1206  * exist, -ENODATA if property does not have a value, and -EILSEQ if the string
1207  * is not null-terminated within the length of the property data.
1208  */
of_property_count_strings(const struct device_node * np,const char * propname)1209 static inline int of_property_count_strings(const struct device_node *np,
1210 					    const char *propname)
1211 {
1212 	return of_property_read_string_helper(np, propname, NULL, 0, 0);
1213 }
1214 
1215 /**
1216  * of_property_read_string_index() - Find and read a string from a multiple
1217  * strings property.
1218  * @np:		device node from which the property value is to be read.
1219  * @propname:	name of the property to be searched.
1220  * @index:	index of the string in the list of strings
1221  * @output:	pointer to null terminated return string, modified only if
1222  *		return value is 0.
1223  *
1224  * Search for a property in a device tree node and retrieve a null
1225  * terminated string value (pointer to data, not a copy) in the list of strings
1226  * contained in that property.
1227  *
1228  * Return: 0 on success, -EINVAL if the property does not exist, -ENODATA if
1229  * property does not have a value, and -EILSEQ if the string is not
1230  * null-terminated within the length of the property data.
1231  *
1232  * The out_string pointer is modified only if a valid string can be decoded.
1233  */
of_property_read_string_index(const struct device_node * np,const char * propname,int index,const char ** output)1234 static inline int of_property_read_string_index(const struct device_node *np,
1235 						const char *propname,
1236 						int index, const char **output)
1237 {
1238 	int rc = of_property_read_string_helper(np, propname, output, 1, index);
1239 	return rc < 0 ? rc : 0;
1240 }
1241 
1242 /**
1243  * of_property_read_bool - Find a property
1244  * @np:		device node from which the property value is to be read.
1245  * @propname:	name of the property to be searched.
1246  *
1247  * Search for a boolean property in a device node. Usage on non-boolean
1248  * property types is deprecated.
1249  *
1250  * Return: true if the property exists false otherwise.
1251  */
of_property_read_bool(const struct device_node * np,const char * propname)1252 static inline bool of_property_read_bool(const struct device_node *np,
1253 					 const char *propname)
1254 {
1255 	struct property *prop = of_find_property(np, propname, NULL);
1256 
1257 	return prop ? true : false;
1258 }
1259 
1260 /**
1261  * of_property_present - Test if a property is present in a node
1262  * @np:		device node to search for the property.
1263  * @propname:	name of the property to be searched.
1264  *
1265  * Test for a property present in a device node.
1266  *
1267  * Return: true if the property exists false otherwise.
1268  */
of_property_present(const struct device_node * np,const char * propname)1269 static inline bool of_property_present(const struct device_node *np, const char *propname)
1270 {
1271 	return of_property_read_bool(np, propname);
1272 }
1273 
1274 /**
1275  * of_property_read_u8_array - Find and read an array of u8 from a property.
1276  *
1277  * @np:		device node from which the property value is to be read.
1278  * @propname:	name of the property to be searched.
1279  * @out_values:	pointer to return value, modified only if return value is 0.
1280  * @sz:		number of array elements to read
1281  *
1282  * Search for a property in a device node and read 8-bit value(s) from
1283  * it.
1284  *
1285  * dts entry of array should be like:
1286  *  ``property = /bits/ 8 <0x50 0x60 0x70>;``
1287  *
1288  * Return: 0 on success, -EINVAL if the property does not exist,
1289  * -ENODATA if property does not have a value, and -EOVERFLOW if the
1290  * property data isn't large enough.
1291  *
1292  * The out_values is modified only if a valid u8 value can be decoded.
1293  */
of_property_read_u8_array(const struct device_node * np,const char * propname,u8 * out_values,size_t sz)1294 static inline int of_property_read_u8_array(const struct device_node *np,
1295 					    const char *propname,
1296 					    u8 *out_values, size_t sz)
1297 {
1298 	int ret = of_property_read_variable_u8_array(np, propname, out_values,
1299 						     sz, 0);
1300 	if (ret >= 0)
1301 		return 0;
1302 	else
1303 		return ret;
1304 }
1305 
1306 /**
1307  * of_property_read_u16_array - Find and read an array of u16 from a property.
1308  *
1309  * @np:		device node from which the property value is to be read.
1310  * @propname:	name of the property to be searched.
1311  * @out_values:	pointer to return value, modified only if return value is 0.
1312  * @sz:		number of array elements to read
1313  *
1314  * Search for a property in a device node and read 16-bit value(s) from
1315  * it.
1316  *
1317  * dts entry of array should be like:
1318  *  ``property = /bits/ 16 <0x5000 0x6000 0x7000>;``
1319  *
1320  * Return: 0 on success, -EINVAL if the property does not exist,
1321  * -ENODATA if property does not have a value, and -EOVERFLOW if the
1322  * property data isn't large enough.
1323  *
1324  * The out_values is modified only if a valid u16 value can be decoded.
1325  */
of_property_read_u16_array(const struct device_node * np,const char * propname,u16 * out_values,size_t sz)1326 static inline int of_property_read_u16_array(const struct device_node *np,
1327 					     const char *propname,
1328 					     u16 *out_values, size_t sz)
1329 {
1330 	int ret = of_property_read_variable_u16_array(np, propname, out_values,
1331 						      sz, 0);
1332 	if (ret >= 0)
1333 		return 0;
1334 	else
1335 		return ret;
1336 }
1337 
1338 /**
1339  * of_property_read_u32_array - Find and read an array of 32 bit integers
1340  * from a property.
1341  *
1342  * @np:		device node from which the property value is to be read.
1343  * @propname:	name of the property to be searched.
1344  * @out_values:	pointer to return value, modified only if return value is 0.
1345  * @sz:		number of array elements to read
1346  *
1347  * Search for a property in a device node and read 32-bit value(s) from
1348  * it.
1349  *
1350  * Return: 0 on success, -EINVAL if the property does not exist,
1351  * -ENODATA if property does not have a value, and -EOVERFLOW if the
1352  * property data isn't large enough.
1353  *
1354  * The out_values is modified only if a valid u32 value can be decoded.
1355  */
of_property_read_u32_array(const struct device_node * np,const char * propname,u32 * out_values,size_t sz)1356 static inline int of_property_read_u32_array(const struct device_node *np,
1357 					     const char *propname,
1358 					     u32 *out_values, size_t sz)
1359 {
1360 	int ret = of_property_read_variable_u32_array(np, propname, out_values,
1361 						      sz, 0);
1362 	if (ret >= 0)
1363 		return 0;
1364 	else
1365 		return ret;
1366 }
1367 
1368 /**
1369  * of_property_read_u64_array - Find and read an array of 64 bit integers
1370  * from a property.
1371  *
1372  * @np:		device node from which the property value is to be read.
1373  * @propname:	name of the property to be searched.
1374  * @out_values:	pointer to return value, modified only if return value is 0.
1375  * @sz:		number of array elements to read
1376  *
1377  * Search for a property in a device node and read 64-bit value(s) from
1378  * it.
1379  *
1380  * Return: 0 on success, -EINVAL if the property does not exist,
1381  * -ENODATA if property does not have a value, and -EOVERFLOW if the
1382  * property data isn't large enough.
1383  *
1384  * The out_values is modified only if a valid u64 value can be decoded.
1385  */
of_property_read_u64_array(const struct device_node * np,const char * propname,u64 * out_values,size_t sz)1386 static inline int of_property_read_u64_array(const struct device_node *np,
1387 					     const char *propname,
1388 					     u64 *out_values, size_t sz)
1389 {
1390 	int ret = of_property_read_variable_u64_array(np, propname, out_values,
1391 						      sz, 0);
1392 	if (ret >= 0)
1393 		return 0;
1394 	else
1395 		return ret;
1396 }
1397 
of_property_read_u8(const struct device_node * np,const char * propname,u8 * out_value)1398 static inline int of_property_read_u8(const struct device_node *np,
1399 				       const char *propname,
1400 				       u8 *out_value)
1401 {
1402 	return of_property_read_u8_array(np, propname, out_value, 1);
1403 }
1404 
of_property_read_u16(const struct device_node * np,const char * propname,u16 * out_value)1405 static inline int of_property_read_u16(const struct device_node *np,
1406 				       const char *propname,
1407 				       u16 *out_value)
1408 {
1409 	return of_property_read_u16_array(np, propname, out_value, 1);
1410 }
1411 
of_property_read_u32(const struct device_node * np,const char * propname,u32 * out_value)1412 static inline int of_property_read_u32(const struct device_node *np,
1413 				       const char *propname,
1414 				       u32 *out_value)
1415 {
1416 	return of_property_read_u32_array(np, propname, out_value, 1);
1417 }
1418 
of_property_read_s32(const struct device_node * np,const char * propname,s32 * out_value)1419 static inline int of_property_read_s32(const struct device_node *np,
1420 				       const char *propname,
1421 				       s32 *out_value)
1422 {
1423 	return of_property_read_u32(np, propname, (u32*) out_value);
1424 }
1425 
1426 #define of_for_each_phandle(it, err, np, ln, cn, cc)			\
1427 	for (of_phandle_iterator_init((it), (np), (ln), (cn), (cc)),	\
1428 	     err = of_phandle_iterator_next(it);			\
1429 	     err == 0;							\
1430 	     err = of_phandle_iterator_next(it))
1431 
1432 #define of_property_for_each_u32(np, propname, u)			\
1433 	for (struct {struct property *prop; const __be32 *item; } _it =	\
1434 		{of_find_property(np, propname, NULL),			\
1435 		 of_prop_next_u32(_it.prop, NULL, &u)};			\
1436 	     _it.item;							\
1437 	     _it.item = of_prop_next_u32(_it.prop, _it.item, &u))
1438 
1439 #define of_property_for_each_string(np, propname, prop, s)	\
1440 	for (prop = of_find_property(np, propname, NULL),	\
1441 		s = of_prop_next_string(prop, NULL);		\
1442 		s;						\
1443 		s = of_prop_next_string(prop, s))
1444 
1445 #define for_each_node_by_name(dn, name) \
1446 	for (dn = of_find_node_by_name(NULL, name); dn; \
1447 	     dn = of_find_node_by_name(dn, name))
1448 #define for_each_node_by_type(dn, type) \
1449 	for (dn = of_find_node_by_type(NULL, type); dn; \
1450 	     dn = of_find_node_by_type(dn, type))
1451 #define for_each_compatible_node(dn, type, compatible) \
1452 	for (dn = of_find_compatible_node(NULL, type, compatible); dn; \
1453 	     dn = of_find_compatible_node(dn, type, compatible))
1454 #define for_each_matching_node(dn, matches) \
1455 	for (dn = of_find_matching_node(NULL, matches); dn; \
1456 	     dn = of_find_matching_node(dn, matches))
1457 #define for_each_matching_node_and_match(dn, matches, match) \
1458 	for (dn = of_find_matching_node_and_match(NULL, matches, match); \
1459 	     dn; dn = of_find_matching_node_and_match(dn, matches, match))
1460 
1461 #define for_each_child_of_node(parent, child) \
1462 	for (child = of_get_next_child(parent, NULL); child != NULL; \
1463 	     child = of_get_next_child(parent, child))
1464 
1465 #define for_each_child_of_node_scoped(parent, child) \
1466 	for (struct device_node *child __free(device_node) =		\
1467 	     of_get_next_child(parent, NULL);				\
1468 	     child != NULL;						\
1469 	     child = of_get_next_child(parent, child))
1470 
1471 #define for_each_available_child_of_node(parent, child) \
1472 	for (child = of_get_next_available_child(parent, NULL); child != NULL; \
1473 	     child = of_get_next_available_child(parent, child))
1474 #define for_each_reserved_child_of_node(parent, child)			\
1475 	for (child = of_get_next_reserved_child(parent, NULL); child != NULL; \
1476 	     child = of_get_next_reserved_child(parent, child))
1477 
1478 #define for_each_available_child_of_node_scoped(parent, child) \
1479 	for (struct device_node *child __free(device_node) =		\
1480 	     of_get_next_available_child(parent, NULL);			\
1481 	     child != NULL;						\
1482 	     child = of_get_next_available_child(parent, child))
1483 
1484 #define for_each_of_cpu_node(cpu) \
1485 	for (cpu = of_get_next_cpu_node(NULL); cpu != NULL; \
1486 	     cpu = of_get_next_cpu_node(cpu))
1487 
1488 #define for_each_node_with_property(dn, prop_name) \
1489 	for (dn = of_find_node_with_property(NULL, prop_name); dn; \
1490 	     dn = of_find_node_with_property(dn, prop_name))
1491 
of_get_child_count(const struct device_node * np)1492 static inline int of_get_child_count(const struct device_node *np)
1493 {
1494 	struct device_node *child;
1495 	int num = 0;
1496 
1497 	for_each_child_of_node(np, child)
1498 		num++;
1499 
1500 	return num;
1501 }
1502 
of_get_available_child_count(const struct device_node * np)1503 static inline int of_get_available_child_count(const struct device_node *np)
1504 {
1505 	struct device_node *child;
1506 	int num = 0;
1507 
1508 	for_each_available_child_of_node(np, child)
1509 		num++;
1510 
1511 	return num;
1512 }
1513 
1514 #define _OF_DECLARE_STUB(table, name, compat, fn, fn_type)		\
1515 	static const struct of_device_id __of_table_##name		\
1516 		__attribute__((unused))					\
1517 		 = { .compatible = compat,				\
1518 		     .data = (fn == (fn_type)NULL) ? fn : fn }
1519 
1520 #if defined(CONFIG_OF) && !defined(MODULE)
1521 #define _OF_DECLARE(table, name, compat, fn, fn_type)			\
1522 	static const struct of_device_id __of_table_##name		\
1523 		__used __section("__" #table "_of_table")		\
1524 		__aligned(__alignof__(struct of_device_id))		\
1525 		 = { .compatible = compat,				\
1526 		     .data = (fn == (fn_type)NULL) ? fn : fn  }
1527 #else
1528 #define _OF_DECLARE(table, name, compat, fn, fn_type)			\
1529 	_OF_DECLARE_STUB(table, name, compat, fn, fn_type)
1530 #endif
1531 
1532 typedef int (*of_init_fn_2)(struct device_node *, struct device_node *);
1533 typedef int (*of_init_fn_1_ret)(struct device_node *);
1534 typedef void (*of_init_fn_1)(struct device_node *);
1535 
1536 #define OF_DECLARE_1(table, name, compat, fn) \
1537 		_OF_DECLARE(table, name, compat, fn, of_init_fn_1)
1538 #define OF_DECLARE_1_RET(table, name, compat, fn) \
1539 		_OF_DECLARE(table, name, compat, fn, of_init_fn_1_ret)
1540 #define OF_DECLARE_2(table, name, compat, fn) \
1541 		_OF_DECLARE(table, name, compat, fn, of_init_fn_2)
1542 
1543 /**
1544  * struct of_changeset_entry	- Holds a changeset entry
1545  *
1546  * @node:	list_head for the log list
1547  * @action:	notifier action
1548  * @np:		pointer to the device node affected
1549  * @prop:	pointer to the property affected
1550  * @old_prop:	hold a pointer to the original property
1551  *
1552  * Every modification of the device tree during a changeset
1553  * is held in a list of of_changeset_entry structures.
1554  * That way we can recover from a partial application, or we can
1555  * revert the changeset
1556  */
1557 struct of_changeset_entry {
1558 	struct list_head node;
1559 	unsigned long action;
1560 	struct device_node *np;
1561 	struct property *prop;
1562 	struct property *old_prop;
1563 };
1564 
1565 /**
1566  * struct of_changeset - changeset tracker structure
1567  *
1568  * @entries:	list_head for the changeset entries
1569  *
1570  * changesets are a convenient way to apply bulk changes to the
1571  * live tree. In case of an error, changes are rolled-back.
1572  * changesets live on after initial application, and if not
1573  * destroyed after use, they can be reverted in one single call.
1574  */
1575 struct of_changeset {
1576 	struct list_head entries;
1577 };
1578 
1579 enum of_reconfig_change {
1580 	OF_RECONFIG_NO_CHANGE = 0,
1581 	OF_RECONFIG_CHANGE_ADD,
1582 	OF_RECONFIG_CHANGE_REMOVE,
1583 };
1584 
1585 struct notifier_block;
1586 
1587 #ifdef CONFIG_OF_DYNAMIC
1588 extern int of_reconfig_notifier_register(struct notifier_block *);
1589 extern int of_reconfig_notifier_unregister(struct notifier_block *);
1590 extern int of_reconfig_notify(unsigned long, struct of_reconfig_data *rd);
1591 extern int of_reconfig_get_state_change(unsigned long action,
1592 					struct of_reconfig_data *arg);
1593 
1594 extern void of_changeset_init(struct of_changeset *ocs);
1595 extern void of_changeset_destroy(struct of_changeset *ocs);
1596 extern int of_changeset_apply(struct of_changeset *ocs);
1597 extern int of_changeset_revert(struct of_changeset *ocs);
1598 extern int of_changeset_action(struct of_changeset *ocs,
1599 		unsigned long action, struct device_node *np,
1600 		struct property *prop);
1601 
of_changeset_attach_node(struct of_changeset * ocs,struct device_node * np)1602 static inline int of_changeset_attach_node(struct of_changeset *ocs,
1603 		struct device_node *np)
1604 {
1605 	return of_changeset_action(ocs, OF_RECONFIG_ATTACH_NODE, np, NULL);
1606 }
1607 
of_changeset_detach_node(struct of_changeset * ocs,struct device_node * np)1608 static inline int of_changeset_detach_node(struct of_changeset *ocs,
1609 		struct device_node *np)
1610 {
1611 	return of_changeset_action(ocs, OF_RECONFIG_DETACH_NODE, np, NULL);
1612 }
1613 
of_changeset_add_property(struct of_changeset * ocs,struct device_node * np,struct property * prop)1614 static inline int of_changeset_add_property(struct of_changeset *ocs,
1615 		struct device_node *np, struct property *prop)
1616 {
1617 	return of_changeset_action(ocs, OF_RECONFIG_ADD_PROPERTY, np, prop);
1618 }
1619 
of_changeset_remove_property(struct of_changeset * ocs,struct device_node * np,struct property * prop)1620 static inline int of_changeset_remove_property(struct of_changeset *ocs,
1621 		struct device_node *np, struct property *prop)
1622 {
1623 	return of_changeset_action(ocs, OF_RECONFIG_REMOVE_PROPERTY, np, prop);
1624 }
1625 
of_changeset_update_property(struct of_changeset * ocs,struct device_node * np,struct property * prop)1626 static inline int of_changeset_update_property(struct of_changeset *ocs,
1627 		struct device_node *np, struct property *prop)
1628 {
1629 	return of_changeset_action(ocs, OF_RECONFIG_UPDATE_PROPERTY, np, prop);
1630 }
1631 
1632 struct device_node *of_changeset_create_node(struct of_changeset *ocs,
1633 					     struct device_node *parent,
1634 					     const char *full_name);
1635 int of_changeset_add_prop_string(struct of_changeset *ocs,
1636 				 struct device_node *np,
1637 				 const char *prop_name, const char *str);
1638 int of_changeset_add_prop_string_array(struct of_changeset *ocs,
1639 				       struct device_node *np,
1640 				       const char *prop_name,
1641 				       const char * const *str_array, size_t sz);
1642 int of_changeset_add_prop_u32_array(struct of_changeset *ocs,
1643 				    struct device_node *np,
1644 				    const char *prop_name,
1645 				    const u32 *array, size_t sz);
of_changeset_add_prop_u32(struct of_changeset * ocs,struct device_node * np,const char * prop_name,const u32 val)1646 static inline int of_changeset_add_prop_u32(struct of_changeset *ocs,
1647 					    struct device_node *np,
1648 					    const char *prop_name,
1649 					    const u32 val)
1650 {
1651 	return of_changeset_add_prop_u32_array(ocs, np, prop_name, &val, 1);
1652 }
1653 
1654 int of_changeset_add_prop_bool(struct of_changeset *ocs, struct device_node *np,
1655 			       const char *prop_name);
1656 
1657 #else /* CONFIG_OF_DYNAMIC */
of_reconfig_notifier_register(struct notifier_block * nb)1658 static inline int of_reconfig_notifier_register(struct notifier_block *nb)
1659 {
1660 	return -EINVAL;
1661 }
of_reconfig_notifier_unregister(struct notifier_block * nb)1662 static inline int of_reconfig_notifier_unregister(struct notifier_block *nb)
1663 {
1664 	return -EINVAL;
1665 }
of_reconfig_notify(unsigned long action,struct of_reconfig_data * arg)1666 static inline int of_reconfig_notify(unsigned long action,
1667 				     struct of_reconfig_data *arg)
1668 {
1669 	return -EINVAL;
1670 }
of_reconfig_get_state_change(unsigned long action,struct of_reconfig_data * arg)1671 static inline int of_reconfig_get_state_change(unsigned long action,
1672 						struct of_reconfig_data *arg)
1673 {
1674 	return -EINVAL;
1675 }
1676 #endif /* CONFIG_OF_DYNAMIC */
1677 
1678 /**
1679  * of_device_is_system_power_controller - Tells if system-power-controller is found for device_node
1680  * @np: Pointer to the given device_node
1681  *
1682  * Return: true if present false otherwise
1683  */
of_device_is_system_power_controller(const struct device_node * np)1684 static inline bool of_device_is_system_power_controller(const struct device_node *np)
1685 {
1686 	return of_property_read_bool(np, "system-power-controller");
1687 }
1688 
1689 /**
1690  * of_have_populated_dt() - Has DT been populated by bootloader
1691  *
1692  * Return: True if a DTB has been populated by the bootloader and it isn't the
1693  * empty builtin one. False otherwise.
1694  */
of_have_populated_dt(void)1695 static inline bool of_have_populated_dt(void)
1696 {
1697 #ifdef CONFIG_OF
1698 	return of_property_present(of_root, "compatible");
1699 #else
1700 	return false;
1701 #endif
1702 }
1703 
1704 /*
1705  * Overlay support
1706  */
1707 
1708 enum of_overlay_notify_action {
1709 	OF_OVERLAY_INIT = 0,	/* kzalloc() of ovcs sets this value */
1710 	OF_OVERLAY_PRE_APPLY,
1711 	OF_OVERLAY_POST_APPLY,
1712 	OF_OVERLAY_PRE_REMOVE,
1713 	OF_OVERLAY_POST_REMOVE,
1714 };
1715 
of_overlay_action_name(enum of_overlay_notify_action action)1716 static inline const char *of_overlay_action_name(enum of_overlay_notify_action action)
1717 {
1718 	static const char *const of_overlay_action_name[] = {
1719 		"init",
1720 		"pre-apply",
1721 		"post-apply",
1722 		"pre-remove",
1723 		"post-remove",
1724 	};
1725 
1726 	return of_overlay_action_name[action];
1727 }
1728 
1729 struct of_overlay_notify_data {
1730 	struct device_node *overlay;
1731 	struct device_node *target;
1732 };
1733 
1734 #ifdef CONFIG_OF_OVERLAY
1735 
1736 int of_overlay_fdt_apply(const void *overlay_fdt, u32 overlay_fdt_size,
1737 			 int *ovcs_id, struct device_node *target_base);
1738 int of_overlay_remove(int *ovcs_id);
1739 int of_overlay_remove_all(void);
1740 
1741 int of_overlay_notifier_register(struct notifier_block *nb);
1742 int of_overlay_notifier_unregister(struct notifier_block *nb);
1743 
1744 #else
1745 
of_overlay_fdt_apply(const void * overlay_fdt,u32 overlay_fdt_size,int * ovcs_id,struct device_node * target_base)1746 static inline int of_overlay_fdt_apply(const void *overlay_fdt, u32 overlay_fdt_size,
1747 				       int *ovcs_id, struct device_node *target_base)
1748 {
1749 	return -ENOTSUPP;
1750 }
1751 
of_overlay_remove(int * ovcs_id)1752 static inline int of_overlay_remove(int *ovcs_id)
1753 {
1754 	return -ENOTSUPP;
1755 }
1756 
of_overlay_remove_all(void)1757 static inline int of_overlay_remove_all(void)
1758 {
1759 	return -ENOTSUPP;
1760 }
1761 
of_overlay_notifier_register(struct notifier_block * nb)1762 static inline int of_overlay_notifier_register(struct notifier_block *nb)
1763 {
1764 	return 0;
1765 }
1766 
of_overlay_notifier_unregister(struct notifier_block * nb)1767 static inline int of_overlay_notifier_unregister(struct notifier_block *nb)
1768 {
1769 	return 0;
1770 }
1771 
1772 #endif
1773 
1774 #endif /* _LINUX_OF_H */
1775