xref: /linux-6.15/include/linux/of.h (revision d0807da7)
1 #ifndef _LINUX_OF_H
2 #define _LINUX_OF_H
3 /*
4  * Definitions for talking to the Open Firmware PROM on
5  * Power Macintosh and other computers.
6  *
7  * Copyright (C) 1996-2005 Paul Mackerras.
8  *
9  * Updates for PPC64 by Peter Bergner & David Engebretsen, IBM Corp.
10  * Updates for SPARC64 by David S. Miller
11  * Derived from PowerPC and Sparc prom.h files by Stephen Rothwell, IBM Corp.
12  *
13  * This program is free software; you can redistribute it and/or
14  * modify it under the terms of the GNU General Public License
15  * as published by the Free Software Foundation; either version
16  * 2 of the License, or (at your option) any later version.
17  */
18 #include <linux/types.h>
19 #include <linux/bitops.h>
20 #include <linux/errno.h>
21 #include <linux/kobject.h>
22 #include <linux/mod_devicetable.h>
23 #include <linux/spinlock.h>
24 #include <linux/topology.h>
25 #include <linux/notifier.h>
26 #include <linux/property.h>
27 #include <linux/list.h>
28 
29 #include <asm/byteorder.h>
30 #include <asm/errno.h>
31 
32 typedef u32 phandle;
33 typedef u32 ihandle;
34 
35 struct property {
36 	char	*name;
37 	int	length;
38 	void	*value;
39 	struct property *next;
40 #if defined(CONFIG_OF_DYNAMIC) || defined(CONFIG_SPARC)
41 	unsigned long _flags;
42 #endif
43 #if defined(CONFIG_OF_PROMTREE)
44 	unsigned int unique_id;
45 #endif
46 #if defined(CONFIG_OF_KOBJ)
47 	struct bin_attribute attr;
48 #endif
49 };
50 
51 #if defined(CONFIG_SPARC)
52 struct of_irq_controller;
53 #endif
54 
55 struct device_node {
56 	const char *name;
57 	const char *type;
58 	phandle phandle;
59 	const char *full_name;
60 	struct fwnode_handle fwnode;
61 
62 	struct	property *properties;
63 	struct	property *deadprops;	/* removed properties */
64 	struct	device_node *parent;
65 	struct	device_node *child;
66 	struct	device_node *sibling;
67 #if defined(CONFIG_OF_KOBJ)
68 	struct	kobject kobj;
69 #endif
70 	unsigned long _flags;
71 	void	*data;
72 #if defined(CONFIG_SPARC)
73 	const char *path_component_name;
74 	unsigned int unique_id;
75 	struct of_irq_controller *irq_trans;
76 #endif
77 };
78 
79 #define MAX_PHANDLE_ARGS 16
80 struct of_phandle_args {
81 	struct device_node *np;
82 	int args_count;
83 	uint32_t args[MAX_PHANDLE_ARGS];
84 };
85 
86 struct of_phandle_iterator {
87 	/* Common iterator information */
88 	const char *cells_name;
89 	int cell_count;
90 	const struct device_node *parent;
91 
92 	/* List size information */
93 	const __be32 *list_end;
94 	const __be32 *phandle_end;
95 
96 	/* Current position state */
97 	const __be32 *cur;
98 	uint32_t cur_count;
99 	phandle phandle;
100 	struct device_node *node;
101 };
102 
103 struct of_reconfig_data {
104 	struct device_node	*dn;
105 	struct property		*prop;
106 	struct property		*old_prop;
107 };
108 
109 /* initialize a node */
110 extern struct kobj_type of_node_ktype;
111 extern const struct fwnode_operations of_fwnode_ops;
112 static inline void of_node_init(struct device_node *node)
113 {
114 #if defined(CONFIG_OF_KOBJ)
115 	kobject_init(&node->kobj, &of_node_ktype);
116 #endif
117 	node->fwnode.ops = &of_fwnode_ops;
118 }
119 
120 #if defined(CONFIG_OF_KOBJ)
121 #define of_node_kobj(n) (&(n)->kobj)
122 #else
123 #define of_node_kobj(n) NULL
124 #endif
125 
126 #ifdef CONFIG_OF_DYNAMIC
127 extern struct device_node *of_node_get(struct device_node *node);
128 extern void of_node_put(struct device_node *node);
129 #else /* CONFIG_OF_DYNAMIC */
130 /* Dummy ref counting routines - to be implemented later */
131 static inline struct device_node *of_node_get(struct device_node *node)
132 {
133 	return node;
134 }
135 static inline void of_node_put(struct device_node *node) { }
136 #endif /* !CONFIG_OF_DYNAMIC */
137 
138 /* Pointer for first entry in chain of all nodes. */
139 extern struct device_node *of_root;
140 extern struct device_node *of_chosen;
141 extern struct device_node *of_aliases;
142 extern struct device_node *of_stdout;
143 extern raw_spinlock_t devtree_lock;
144 
145 /* flag descriptions (need to be visible even when !CONFIG_OF) */
146 #define OF_DYNAMIC	1 /* node and properties were allocated via kmalloc */
147 #define OF_DETACHED	2 /* node has been detached from the device tree */
148 #define OF_POPULATED	3 /* device already created for the node */
149 #define OF_POPULATED_BUS	4 /* of_platform_populate recursed to children of this node */
150 
151 #define OF_BAD_ADDR	((u64)-1)
152 
153 #ifdef CONFIG_OF
154 void of_core_init(void);
155 
156 static inline bool is_of_node(const struct fwnode_handle *fwnode)
157 {
158 	return !IS_ERR_OR_NULL(fwnode) && fwnode->ops == &of_fwnode_ops;
159 }
160 
161 #define to_of_node(__fwnode)						\
162 	({								\
163 		typeof(__fwnode) __to_of_node_fwnode = (__fwnode);	\
164 									\
165 		is_of_node(__to_of_node_fwnode) ?			\
166 			container_of(__to_of_node_fwnode,		\
167 				     struct device_node, fwnode) :	\
168 			NULL;						\
169 	})
170 
171 #define of_fwnode_handle(node)						\
172 	({								\
173 		typeof(node) __of_fwnode_handle_node = (node);		\
174 									\
175 		__of_fwnode_handle_node ?				\
176 			&__of_fwnode_handle_node->fwnode : NULL;	\
177 	})
178 
179 static inline bool of_have_populated_dt(void)
180 {
181 	return of_root != NULL;
182 }
183 
184 static inline bool of_node_is_root(const struct device_node *node)
185 {
186 	return node && (node->parent == NULL);
187 }
188 
189 static inline int of_node_check_flag(struct device_node *n, unsigned long flag)
190 {
191 	return test_bit(flag, &n->_flags);
192 }
193 
194 static inline int of_node_test_and_set_flag(struct device_node *n,
195 					    unsigned long flag)
196 {
197 	return test_and_set_bit(flag, &n->_flags);
198 }
199 
200 static inline void of_node_set_flag(struct device_node *n, unsigned long flag)
201 {
202 	set_bit(flag, &n->_flags);
203 }
204 
205 static inline void of_node_clear_flag(struct device_node *n, unsigned long flag)
206 {
207 	clear_bit(flag, &n->_flags);
208 }
209 
210 #if defined(CONFIG_OF_DYNAMIC) || defined(CONFIG_SPARC)
211 static inline int of_property_check_flag(struct property *p, unsigned long flag)
212 {
213 	return test_bit(flag, &p->_flags);
214 }
215 
216 static inline void of_property_set_flag(struct property *p, unsigned long flag)
217 {
218 	set_bit(flag, &p->_flags);
219 }
220 
221 static inline void of_property_clear_flag(struct property *p, unsigned long flag)
222 {
223 	clear_bit(flag, &p->_flags);
224 }
225 #endif
226 
227 extern struct device_node *__of_find_all_nodes(struct device_node *prev);
228 extern struct device_node *of_find_all_nodes(struct device_node *prev);
229 
230 /*
231  * OF address retrieval & translation
232  */
233 
234 /* Helper to read a big number; size is in cells (not bytes) */
235 static inline u64 of_read_number(const __be32 *cell, int size)
236 {
237 	u64 r = 0;
238 	while (size--)
239 		r = (r << 32) | be32_to_cpu(*(cell++));
240 	return r;
241 }
242 
243 /* Like of_read_number, but we want an unsigned long result */
244 static inline unsigned long of_read_ulong(const __be32 *cell, int size)
245 {
246 	/* toss away upper bits if unsigned long is smaller than u64 */
247 	return of_read_number(cell, size);
248 }
249 
250 #if defined(CONFIG_SPARC)
251 #include <asm/prom.h>
252 #endif
253 
254 /* Default #address and #size cells.  Allow arch asm/prom.h to override */
255 #if !defined(OF_ROOT_NODE_ADDR_CELLS_DEFAULT)
256 #define OF_ROOT_NODE_ADDR_CELLS_DEFAULT 1
257 #define OF_ROOT_NODE_SIZE_CELLS_DEFAULT 1
258 #endif
259 
260 #define OF_IS_DYNAMIC(x) test_bit(OF_DYNAMIC, &x->_flags)
261 #define OF_MARK_DYNAMIC(x) set_bit(OF_DYNAMIC, &x->_flags)
262 
263 static inline const char *of_node_full_name(const struct device_node *np)
264 {
265 	return np ? np->full_name : "<no-node>";
266 }
267 
268 #define for_each_of_allnodes_from(from, dn) \
269 	for (dn = __of_find_all_nodes(from); dn; dn = __of_find_all_nodes(dn))
270 #define for_each_of_allnodes(dn) for_each_of_allnodes_from(NULL, dn)
271 extern struct device_node *of_find_node_by_name(struct device_node *from,
272 	const char *name);
273 extern struct device_node *of_find_node_by_type(struct device_node *from,
274 	const char *type);
275 extern struct device_node *of_find_compatible_node(struct device_node *from,
276 	const char *type, const char *compat);
277 extern struct device_node *of_find_matching_node_and_match(
278 	struct device_node *from,
279 	const struct of_device_id *matches,
280 	const struct of_device_id **match);
281 
282 extern struct device_node *of_find_node_opts_by_path(const char *path,
283 	const char **opts);
284 static inline struct device_node *of_find_node_by_path(const char *path)
285 {
286 	return of_find_node_opts_by_path(path, NULL);
287 }
288 
289 extern struct device_node *of_find_node_by_phandle(phandle handle);
290 extern struct device_node *of_get_parent(const struct device_node *node);
291 extern struct device_node *of_get_next_parent(struct device_node *node);
292 extern struct device_node *of_get_next_child(const struct device_node *node,
293 					     struct device_node *prev);
294 extern struct device_node *of_get_next_available_child(
295 	const struct device_node *node, struct device_node *prev);
296 
297 extern struct device_node *of_get_child_by_name(const struct device_node *node,
298 					const char *name);
299 
300 /* cache lookup */
301 extern struct device_node *of_find_next_cache_node(const struct device_node *);
302 extern int of_find_last_cache_level(unsigned int cpu);
303 extern struct device_node *of_find_node_with_property(
304 	struct device_node *from, const char *prop_name);
305 
306 extern struct property *of_find_property(const struct device_node *np,
307 					 const char *name,
308 					 int *lenp);
309 extern int of_property_count_elems_of_size(const struct device_node *np,
310 				const char *propname, int elem_size);
311 extern int of_property_read_u32_index(const struct device_node *np,
312 				       const char *propname,
313 				       u32 index, u32 *out_value);
314 extern int of_property_read_u64_index(const struct device_node *np,
315 				       const char *propname,
316 				       u32 index, u64 *out_value);
317 extern int of_property_read_variable_u8_array(const struct device_node *np,
318 					const char *propname, u8 *out_values,
319 					size_t sz_min, size_t sz_max);
320 extern int of_property_read_variable_u16_array(const struct device_node *np,
321 					const char *propname, u16 *out_values,
322 					size_t sz_min, size_t sz_max);
323 extern int of_property_read_variable_u32_array(const struct device_node *np,
324 					const char *propname,
325 					u32 *out_values,
326 					size_t sz_min,
327 					size_t sz_max);
328 extern int of_property_read_u64(const struct device_node *np,
329 				const char *propname, u64 *out_value);
330 extern int of_property_read_variable_u64_array(const struct device_node *np,
331 					const char *propname,
332 					u64 *out_values,
333 					size_t sz_min,
334 					size_t sz_max);
335 
336 extern int of_property_read_string(const struct device_node *np,
337 				   const char *propname,
338 				   const char **out_string);
339 extern int of_property_match_string(const struct device_node *np,
340 				    const char *propname,
341 				    const char *string);
342 extern int of_property_read_string_helper(const struct device_node *np,
343 					      const char *propname,
344 					      const char **out_strs, size_t sz, int index);
345 extern int of_device_is_compatible(const struct device_node *device,
346 				   const char *);
347 extern int of_device_compatible_match(struct device_node *device,
348 				      const char *const *compat);
349 extern bool of_device_is_available(const struct device_node *device);
350 extern bool of_device_is_big_endian(const struct device_node *device);
351 extern const void *of_get_property(const struct device_node *node,
352 				const char *name,
353 				int *lenp);
354 extern struct device_node *of_get_cpu_node(int cpu, unsigned int *thread);
355 #define for_each_property_of_node(dn, pp) \
356 	for (pp = dn->properties; pp != NULL; pp = pp->next)
357 
358 extern int of_n_addr_cells(struct device_node *np);
359 extern int of_n_size_cells(struct device_node *np);
360 extern const struct of_device_id *of_match_node(
361 	const struct of_device_id *matches, const struct device_node *node);
362 extern int of_modalias_node(struct device_node *node, char *modalias, int len);
363 extern void of_print_phandle_args(const char *msg, const struct of_phandle_args *args);
364 extern struct device_node *of_parse_phandle(const struct device_node *np,
365 					    const char *phandle_name,
366 					    int index);
367 extern int of_parse_phandle_with_args(const struct device_node *np,
368 	const char *list_name, const char *cells_name, int index,
369 	struct of_phandle_args *out_args);
370 extern int of_parse_phandle_with_fixed_args(const struct device_node *np,
371 	const char *list_name, int cells_count, int index,
372 	struct of_phandle_args *out_args);
373 extern int of_count_phandle_with_args(const struct device_node *np,
374 	const char *list_name, const char *cells_name);
375 
376 /* phandle iterator functions */
377 extern int of_phandle_iterator_init(struct of_phandle_iterator *it,
378 				    const struct device_node *np,
379 				    const char *list_name,
380 				    const char *cells_name,
381 				    int cell_count);
382 
383 extern int of_phandle_iterator_next(struct of_phandle_iterator *it);
384 extern int of_phandle_iterator_args(struct of_phandle_iterator *it,
385 				    uint32_t *args,
386 				    int size);
387 
388 extern void of_alias_scan(void * (*dt_alloc)(u64 size, u64 align));
389 extern int of_alias_get_id(struct device_node *np, const char *stem);
390 extern int of_alias_get_highest_id(const char *stem);
391 
392 extern int of_machine_is_compatible(const char *compat);
393 
394 extern int of_add_property(struct device_node *np, struct property *prop);
395 extern int of_remove_property(struct device_node *np, struct property *prop);
396 extern int of_update_property(struct device_node *np, struct property *newprop);
397 
398 /* For updating the device tree at runtime */
399 #define OF_RECONFIG_ATTACH_NODE		0x0001
400 #define OF_RECONFIG_DETACH_NODE		0x0002
401 #define OF_RECONFIG_ADD_PROPERTY	0x0003
402 #define OF_RECONFIG_REMOVE_PROPERTY	0x0004
403 #define OF_RECONFIG_UPDATE_PROPERTY	0x0005
404 
405 extern int of_attach_node(struct device_node *);
406 extern int of_detach_node(struct device_node *);
407 
408 #define of_match_ptr(_ptr)	(_ptr)
409 
410 /**
411  * of_property_read_u8_array - Find and read an array of u8 from a property.
412  *
413  * @np:		device node from which the property value is to be read.
414  * @propname:	name of the property to be searched.
415  * @out_values:	pointer to return value, modified only if return value is 0.
416  * @sz:		number of array elements to read
417  *
418  * Search for a property in a device node and read 8-bit value(s) from
419  * it. Returns 0 on success, -EINVAL if the property does not exist,
420  * -ENODATA if property does not have a value, and -EOVERFLOW if the
421  * property data isn't large enough.
422  *
423  * dts entry of array should be like:
424  *	property = /bits/ 8 <0x50 0x60 0x70>;
425  *
426  * The out_values is modified only if a valid u8 value can be decoded.
427  */
428 static inline int of_property_read_u8_array(const struct device_node *np,
429 					    const char *propname,
430 					    u8 *out_values, size_t sz)
431 {
432 	int ret = of_property_read_variable_u8_array(np, propname, out_values,
433 						     sz, 0);
434 	if (ret >= 0)
435 		return 0;
436 	else
437 		return ret;
438 }
439 
440 /**
441  * of_property_read_u16_array - Find and read an array of u16 from a property.
442  *
443  * @np:		device node from which the property value is to be read.
444  * @propname:	name of the property to be searched.
445  * @out_values:	pointer to return value, modified only if return value is 0.
446  * @sz:		number of array elements to read
447  *
448  * Search for a property in a device node and read 16-bit value(s) from
449  * it. Returns 0 on success, -EINVAL if the property does not exist,
450  * -ENODATA if property does not have a value, and -EOVERFLOW if the
451  * property data isn't large enough.
452  *
453  * dts entry of array should be like:
454  *	property = /bits/ 16 <0x5000 0x6000 0x7000>;
455  *
456  * The out_values is modified only if a valid u16 value can be decoded.
457  */
458 static inline int of_property_read_u16_array(const struct device_node *np,
459 					     const char *propname,
460 					     u16 *out_values, size_t sz)
461 {
462 	int ret = of_property_read_variable_u16_array(np, propname, out_values,
463 						      sz, 0);
464 	if (ret >= 0)
465 		return 0;
466 	else
467 		return ret;
468 }
469 
470 /**
471  * of_property_read_u32_array - Find and read an array of 32 bit integers
472  * from a property.
473  *
474  * @np:		device node from which the property value is to be read.
475  * @propname:	name of the property to be searched.
476  * @out_values:	pointer to return value, modified only if return value is 0.
477  * @sz:		number of array elements to read
478  *
479  * Search for a property in a device node and read 32-bit value(s) from
480  * it. Returns 0 on success, -EINVAL if the property does not exist,
481  * -ENODATA if property does not have a value, and -EOVERFLOW if the
482  * property data isn't large enough.
483  *
484  * The out_values is modified only if a valid u32 value can be decoded.
485  */
486 static inline int of_property_read_u32_array(const struct device_node *np,
487 					     const char *propname,
488 					     u32 *out_values, size_t sz)
489 {
490 	int ret = of_property_read_variable_u32_array(np, propname, out_values,
491 						      sz, 0);
492 	if (ret >= 0)
493 		return 0;
494 	else
495 		return ret;
496 }
497 
498 /**
499  * of_property_read_u64_array - Find and read an array of 64 bit integers
500  * from a property.
501  *
502  * @np:		device node from which the property value is to be read.
503  * @propname:	name of the property to be searched.
504  * @out_values:	pointer to return value, modified only if return value is 0.
505  * @sz:		number of array elements to read
506  *
507  * Search for a property in a device node and read 64-bit value(s) from
508  * it. Returns 0 on success, -EINVAL if the property does not exist,
509  * -ENODATA if property does not have a value, and -EOVERFLOW if the
510  * property data isn't large enough.
511  *
512  * The out_values is modified only if a valid u64 value can be decoded.
513  */
514 static inline int of_property_read_u64_array(const struct device_node *np,
515 					     const char *propname,
516 					     u64 *out_values, size_t sz)
517 {
518 	int ret = of_property_read_variable_u64_array(np, propname, out_values,
519 						      sz, 0);
520 	if (ret >= 0)
521 		return 0;
522 	else
523 		return ret;
524 }
525 
526 /*
527  * struct property *prop;
528  * const __be32 *p;
529  * u32 u;
530  *
531  * of_property_for_each_u32(np, "propname", prop, p, u)
532  *         printk("U32 value: %x\n", u);
533  */
534 const __be32 *of_prop_next_u32(struct property *prop, const __be32 *cur,
535 			       u32 *pu);
536 /*
537  * struct property *prop;
538  * const char *s;
539  *
540  * of_property_for_each_string(np, "propname", prop, s)
541  *         printk("String value: %s\n", s);
542  */
543 const char *of_prop_next_string(struct property *prop, const char *cur);
544 
545 bool of_console_check(struct device_node *dn, char *name, int index);
546 
547 #else /* CONFIG_OF */
548 
549 static inline void of_core_init(void)
550 {
551 }
552 
553 static inline bool is_of_node(const struct fwnode_handle *fwnode)
554 {
555 	return false;
556 }
557 
558 static inline struct device_node *to_of_node(const struct fwnode_handle *fwnode)
559 {
560 	return NULL;
561 }
562 
563 static inline const char* of_node_full_name(const struct device_node *np)
564 {
565 	return "<no-node>";
566 }
567 
568 static inline struct device_node *of_find_node_by_name(struct device_node *from,
569 	const char *name)
570 {
571 	return NULL;
572 }
573 
574 static inline struct device_node *of_find_node_by_type(struct device_node *from,
575 	const char *type)
576 {
577 	return NULL;
578 }
579 
580 static inline struct device_node *of_find_matching_node_and_match(
581 	struct device_node *from,
582 	const struct of_device_id *matches,
583 	const struct of_device_id **match)
584 {
585 	return NULL;
586 }
587 
588 static inline struct device_node *of_find_node_by_path(const char *path)
589 {
590 	return NULL;
591 }
592 
593 static inline struct device_node *of_find_node_opts_by_path(const char *path,
594 	const char **opts)
595 {
596 	return NULL;
597 }
598 
599 static inline struct device_node *of_find_node_by_phandle(phandle handle)
600 {
601 	return NULL;
602 }
603 
604 static inline struct device_node *of_get_parent(const struct device_node *node)
605 {
606 	return NULL;
607 }
608 
609 static inline struct device_node *of_get_next_child(
610 	const struct device_node *node, struct device_node *prev)
611 {
612 	return NULL;
613 }
614 
615 static inline struct device_node *of_get_next_available_child(
616 	const struct device_node *node, struct device_node *prev)
617 {
618 	return NULL;
619 }
620 
621 static inline struct device_node *of_find_node_with_property(
622 	struct device_node *from, const char *prop_name)
623 {
624 	return NULL;
625 }
626 
627 #define of_fwnode_handle(node) NULL
628 
629 static inline bool of_have_populated_dt(void)
630 {
631 	return false;
632 }
633 
634 static inline struct device_node *of_get_child_by_name(
635 					const struct device_node *node,
636 					const char *name)
637 {
638 	return NULL;
639 }
640 
641 static inline int of_device_is_compatible(const struct device_node *device,
642 					  const char *name)
643 {
644 	return 0;
645 }
646 
647 static inline  int of_device_compatible_match(struct device_node *device,
648 					      const char *const *compat)
649 {
650 	return 0;
651 }
652 
653 static inline bool of_device_is_available(const struct device_node *device)
654 {
655 	return false;
656 }
657 
658 static inline bool of_device_is_big_endian(const struct device_node *device)
659 {
660 	return false;
661 }
662 
663 static inline struct property *of_find_property(const struct device_node *np,
664 						const char *name,
665 						int *lenp)
666 {
667 	return NULL;
668 }
669 
670 static inline struct device_node *of_find_compatible_node(
671 						struct device_node *from,
672 						const char *type,
673 						const char *compat)
674 {
675 	return NULL;
676 }
677 
678 static inline int of_property_count_elems_of_size(const struct device_node *np,
679 			const char *propname, int elem_size)
680 {
681 	return -ENOSYS;
682 }
683 
684 static inline int of_property_read_u32_index(const struct device_node *np,
685 			const char *propname, u32 index, u32 *out_value)
686 {
687 	return -ENOSYS;
688 }
689 
690 static inline int of_property_read_u8_array(const struct device_node *np,
691 			const char *propname, u8 *out_values, size_t sz)
692 {
693 	return -ENOSYS;
694 }
695 
696 static inline int of_property_read_u16_array(const struct device_node *np,
697 			const char *propname, u16 *out_values, size_t sz)
698 {
699 	return -ENOSYS;
700 }
701 
702 static inline int of_property_read_u32_array(const struct device_node *np,
703 					     const char *propname,
704 					     u32 *out_values, size_t sz)
705 {
706 	return -ENOSYS;
707 }
708 
709 static inline int of_property_read_u64_array(const struct device_node *np,
710 					     const char *propname,
711 					     u64 *out_values, size_t sz)
712 {
713 	return -ENOSYS;
714 }
715 
716 static inline int of_property_read_string(const struct device_node *np,
717 					  const char *propname,
718 					  const char **out_string)
719 {
720 	return -ENOSYS;
721 }
722 
723 static inline int of_property_read_string_helper(const struct device_node *np,
724 						 const char *propname,
725 						 const char **out_strs, size_t sz, int index)
726 {
727 	return -ENOSYS;
728 }
729 
730 static inline const void *of_get_property(const struct device_node *node,
731 				const char *name,
732 				int *lenp)
733 {
734 	return NULL;
735 }
736 
737 static inline struct device_node *of_get_cpu_node(int cpu,
738 					unsigned int *thread)
739 {
740 	return NULL;
741 }
742 
743 static inline int of_n_addr_cells(struct device_node *np)
744 {
745 	return 0;
746 
747 }
748 static inline int of_n_size_cells(struct device_node *np)
749 {
750 	return 0;
751 }
752 
753 static inline int of_property_read_u64(const struct device_node *np,
754 				       const char *propname, u64 *out_value)
755 {
756 	return -ENOSYS;
757 }
758 
759 static inline int of_property_match_string(const struct device_node *np,
760 					   const char *propname,
761 					   const char *string)
762 {
763 	return -ENOSYS;
764 }
765 
766 static inline struct device_node *of_parse_phandle(const struct device_node *np,
767 						   const char *phandle_name,
768 						   int index)
769 {
770 	return NULL;
771 }
772 
773 static inline int of_parse_phandle_with_args(const struct device_node *np,
774 					     const char *list_name,
775 					     const char *cells_name,
776 					     int index,
777 					     struct of_phandle_args *out_args)
778 {
779 	return -ENOSYS;
780 }
781 
782 static inline int of_parse_phandle_with_fixed_args(const struct device_node *np,
783 	const char *list_name, int cells_count, int index,
784 	struct of_phandle_args *out_args)
785 {
786 	return -ENOSYS;
787 }
788 
789 static inline int of_count_phandle_with_args(struct device_node *np,
790 					     const char *list_name,
791 					     const char *cells_name)
792 {
793 	return -ENOSYS;
794 }
795 
796 static inline int of_phandle_iterator_init(struct of_phandle_iterator *it,
797 					   const struct device_node *np,
798 					   const char *list_name,
799 					   const char *cells_name,
800 					   int cell_count)
801 {
802 	return -ENOSYS;
803 }
804 
805 static inline int of_phandle_iterator_next(struct of_phandle_iterator *it)
806 {
807 	return -ENOSYS;
808 }
809 
810 static inline int of_phandle_iterator_args(struct of_phandle_iterator *it,
811 					   uint32_t *args,
812 					   int size)
813 {
814 	return 0;
815 }
816 
817 static inline int of_alias_get_id(struct device_node *np, const char *stem)
818 {
819 	return -ENOSYS;
820 }
821 
822 static inline int of_alias_get_highest_id(const char *stem)
823 {
824 	return -ENOSYS;
825 }
826 
827 static inline int of_machine_is_compatible(const char *compat)
828 {
829 	return 0;
830 }
831 
832 static inline bool of_console_check(const struct device_node *dn, const char *name, int index)
833 {
834 	return false;
835 }
836 
837 static inline const __be32 *of_prop_next_u32(struct property *prop,
838 		const __be32 *cur, u32 *pu)
839 {
840 	return NULL;
841 }
842 
843 static inline const char *of_prop_next_string(struct property *prop,
844 		const char *cur)
845 {
846 	return NULL;
847 }
848 
849 static inline int of_node_check_flag(struct device_node *n, unsigned long flag)
850 {
851 	return 0;
852 }
853 
854 static inline int of_node_test_and_set_flag(struct device_node *n,
855 					    unsigned long flag)
856 {
857 	return 0;
858 }
859 
860 static inline void of_node_set_flag(struct device_node *n, unsigned long flag)
861 {
862 }
863 
864 static inline void of_node_clear_flag(struct device_node *n, unsigned long flag)
865 {
866 }
867 
868 static inline int of_property_check_flag(struct property *p, unsigned long flag)
869 {
870 	return 0;
871 }
872 
873 static inline void of_property_set_flag(struct property *p, unsigned long flag)
874 {
875 }
876 
877 static inline void of_property_clear_flag(struct property *p, unsigned long flag)
878 {
879 }
880 
881 #define of_match_ptr(_ptr)	NULL
882 #define of_match_node(_matches, _node)	NULL
883 #endif /* CONFIG_OF */
884 
885 /* Default string compare functions, Allow arch asm/prom.h to override */
886 #if !defined(of_compat_cmp)
887 #define of_compat_cmp(s1, s2, l)	strcasecmp((s1), (s2))
888 #define of_prop_cmp(s1, s2)		strcmp((s1), (s2))
889 #define of_node_cmp(s1, s2)		strcasecmp((s1), (s2))
890 #endif
891 
892 #if defined(CONFIG_OF) && defined(CONFIG_NUMA)
893 extern int of_node_to_nid(struct device_node *np);
894 #else
895 static inline int of_node_to_nid(struct device_node *device)
896 {
897 	return NUMA_NO_NODE;
898 }
899 #endif
900 
901 #ifdef CONFIG_OF_NUMA
902 extern int of_numa_init(void);
903 #else
904 static inline int of_numa_init(void)
905 {
906 	return -ENOSYS;
907 }
908 #endif
909 
910 static inline struct device_node *of_find_matching_node(
911 	struct device_node *from,
912 	const struct of_device_id *matches)
913 {
914 	return of_find_matching_node_and_match(from, matches, NULL);
915 }
916 
917 /**
918  * of_property_count_u8_elems - Count the number of u8 elements in a property
919  *
920  * @np:		device node from which the property value is to be read.
921  * @propname:	name of the property to be searched.
922  *
923  * Search for a property in a device node and count the number of u8 elements
924  * in it. Returns number of elements on sucess, -EINVAL if the property does
925  * not exist or its length does not match a multiple of u8 and -ENODATA if the
926  * property does not have a value.
927  */
928 static inline int of_property_count_u8_elems(const struct device_node *np,
929 				const char *propname)
930 {
931 	return of_property_count_elems_of_size(np, propname, sizeof(u8));
932 }
933 
934 /**
935  * of_property_count_u16_elems - Count the number of u16 elements in a property
936  *
937  * @np:		device node from which the property value is to be read.
938  * @propname:	name of the property to be searched.
939  *
940  * Search for a property in a device node and count the number of u16 elements
941  * in it. Returns number of elements on sucess, -EINVAL if the property does
942  * not exist or its length does not match a multiple of u16 and -ENODATA if the
943  * property does not have a value.
944  */
945 static inline int of_property_count_u16_elems(const struct device_node *np,
946 				const char *propname)
947 {
948 	return of_property_count_elems_of_size(np, propname, sizeof(u16));
949 }
950 
951 /**
952  * of_property_count_u32_elems - Count the number of u32 elements in a property
953  *
954  * @np:		device node from which the property value is to be read.
955  * @propname:	name of the property to be searched.
956  *
957  * Search for a property in a device node and count the number of u32 elements
958  * in it. Returns number of elements on sucess, -EINVAL if the property does
959  * not exist or its length does not match a multiple of u32 and -ENODATA if the
960  * property does not have a value.
961  */
962 static inline int of_property_count_u32_elems(const struct device_node *np,
963 				const char *propname)
964 {
965 	return of_property_count_elems_of_size(np, propname, sizeof(u32));
966 }
967 
968 /**
969  * of_property_count_u64_elems - Count the number of u64 elements in a property
970  *
971  * @np:		device node from which the property value is to be read.
972  * @propname:	name of the property to be searched.
973  *
974  * Search for a property in a device node and count the number of u64 elements
975  * in it. Returns number of elements on sucess, -EINVAL if the property does
976  * not exist or its length does not match a multiple of u64 and -ENODATA if the
977  * property does not have a value.
978  */
979 static inline int of_property_count_u64_elems(const struct device_node *np,
980 				const char *propname)
981 {
982 	return of_property_count_elems_of_size(np, propname, sizeof(u64));
983 }
984 
985 /**
986  * of_property_read_string_array() - Read an array of strings from a multiple
987  * strings property.
988  * @np:		device node from which the property value is to be read.
989  * @propname:	name of the property to be searched.
990  * @out_strs:	output array of string pointers.
991  * @sz:		number of array elements to read.
992  *
993  * Search for a property in a device tree node and retrieve a list of
994  * terminated string values (pointer to data, not a copy) in that property.
995  *
996  * If @out_strs is NULL, the number of strings in the property is returned.
997  */
998 static inline int of_property_read_string_array(const struct device_node *np,
999 						const char *propname, const char **out_strs,
1000 						size_t sz)
1001 {
1002 	return of_property_read_string_helper(np, propname, out_strs, sz, 0);
1003 }
1004 
1005 /**
1006  * of_property_count_strings() - Find and return the number of strings from a
1007  * multiple strings property.
1008  * @np:		device node from which the property value is to be read.
1009  * @propname:	name of the property to be searched.
1010  *
1011  * Search for a property in a device tree node and retrieve the number of null
1012  * terminated string contain in it. Returns the number of strings on
1013  * success, -EINVAL if the property does not exist, -ENODATA if property
1014  * does not have a value, and -EILSEQ if the string is not null-terminated
1015  * within the length of the property data.
1016  */
1017 static inline int of_property_count_strings(const struct device_node *np,
1018 					    const char *propname)
1019 {
1020 	return of_property_read_string_helper(np, propname, NULL, 0, 0);
1021 }
1022 
1023 /**
1024  * of_property_read_string_index() - Find and read a string from a multiple
1025  * strings property.
1026  * @np:		device node from which the property value is to be read.
1027  * @propname:	name of the property to be searched.
1028  * @index:	index of the string in the list of strings
1029  * @out_string:	pointer to null terminated return string, modified only if
1030  *		return value is 0.
1031  *
1032  * Search for a property in a device tree node and retrieve a null
1033  * terminated string value (pointer to data, not a copy) in the list of strings
1034  * contained in that property.
1035  * Returns 0 on success, -EINVAL if the property does not exist, -ENODATA if
1036  * property does not have a value, and -EILSEQ if the string is not
1037  * null-terminated within the length of the property data.
1038  *
1039  * The out_string pointer is modified only if a valid string can be decoded.
1040  */
1041 static inline int of_property_read_string_index(const struct device_node *np,
1042 						const char *propname,
1043 						int index, const char **output)
1044 {
1045 	int rc = of_property_read_string_helper(np, propname, output, 1, index);
1046 	return rc < 0 ? rc : 0;
1047 }
1048 
1049 /**
1050  * of_property_read_bool - Findfrom a property
1051  * @np:		device node from which the property value is to be read.
1052  * @propname:	name of the property to be searched.
1053  *
1054  * Search for a property in a device node.
1055  * Returns true if the property exists false otherwise.
1056  */
1057 static inline bool of_property_read_bool(const struct device_node *np,
1058 					 const char *propname)
1059 {
1060 	struct property *prop = of_find_property(np, propname, NULL);
1061 
1062 	return prop ? true : false;
1063 }
1064 
1065 static inline int of_property_read_u8(const struct device_node *np,
1066 				       const char *propname,
1067 				       u8 *out_value)
1068 {
1069 	return of_property_read_u8_array(np, propname, out_value, 1);
1070 }
1071 
1072 static inline int of_property_read_u16(const struct device_node *np,
1073 				       const char *propname,
1074 				       u16 *out_value)
1075 {
1076 	return of_property_read_u16_array(np, propname, out_value, 1);
1077 }
1078 
1079 static inline int of_property_read_u32(const struct device_node *np,
1080 				       const char *propname,
1081 				       u32 *out_value)
1082 {
1083 	return of_property_read_u32_array(np, propname, out_value, 1);
1084 }
1085 
1086 static inline int of_property_read_s32(const struct device_node *np,
1087 				       const char *propname,
1088 				       s32 *out_value)
1089 {
1090 	return of_property_read_u32(np, propname, (u32*) out_value);
1091 }
1092 
1093 #define of_for_each_phandle(it, err, np, ln, cn, cc)			\
1094 	for (of_phandle_iterator_init((it), (np), (ln), (cn), (cc)),	\
1095 	     err = of_phandle_iterator_next(it);			\
1096 	     err == 0;							\
1097 	     err = of_phandle_iterator_next(it))
1098 
1099 #define of_property_for_each_u32(np, propname, prop, p, u)	\
1100 	for (prop = of_find_property(np, propname, NULL),	\
1101 		p = of_prop_next_u32(prop, NULL, &u);		\
1102 		p;						\
1103 		p = of_prop_next_u32(prop, p, &u))
1104 
1105 #define of_property_for_each_string(np, propname, prop, s)	\
1106 	for (prop = of_find_property(np, propname, NULL),	\
1107 		s = of_prop_next_string(prop, NULL);		\
1108 		s;						\
1109 		s = of_prop_next_string(prop, s))
1110 
1111 #define for_each_node_by_name(dn, name) \
1112 	for (dn = of_find_node_by_name(NULL, name); dn; \
1113 	     dn = of_find_node_by_name(dn, name))
1114 #define for_each_node_by_type(dn, type) \
1115 	for (dn = of_find_node_by_type(NULL, type); dn; \
1116 	     dn = of_find_node_by_type(dn, type))
1117 #define for_each_compatible_node(dn, type, compatible) \
1118 	for (dn = of_find_compatible_node(NULL, type, compatible); dn; \
1119 	     dn = of_find_compatible_node(dn, type, compatible))
1120 #define for_each_matching_node(dn, matches) \
1121 	for (dn = of_find_matching_node(NULL, matches); dn; \
1122 	     dn = of_find_matching_node(dn, matches))
1123 #define for_each_matching_node_and_match(dn, matches, match) \
1124 	for (dn = of_find_matching_node_and_match(NULL, matches, match); \
1125 	     dn; dn = of_find_matching_node_and_match(dn, matches, match))
1126 
1127 #define for_each_child_of_node(parent, child) \
1128 	for (child = of_get_next_child(parent, NULL); child != NULL; \
1129 	     child = of_get_next_child(parent, child))
1130 #define for_each_available_child_of_node(parent, child) \
1131 	for (child = of_get_next_available_child(parent, NULL); child != NULL; \
1132 	     child = of_get_next_available_child(parent, child))
1133 
1134 #define for_each_node_with_property(dn, prop_name) \
1135 	for (dn = of_find_node_with_property(NULL, prop_name); dn; \
1136 	     dn = of_find_node_with_property(dn, prop_name))
1137 
1138 static inline int of_get_child_count(const struct device_node *np)
1139 {
1140 	struct device_node *child;
1141 	int num = 0;
1142 
1143 	for_each_child_of_node(np, child)
1144 		num++;
1145 
1146 	return num;
1147 }
1148 
1149 static inline int of_get_available_child_count(const struct device_node *np)
1150 {
1151 	struct device_node *child;
1152 	int num = 0;
1153 
1154 	for_each_available_child_of_node(np, child)
1155 		num++;
1156 
1157 	return num;
1158 }
1159 
1160 #if defined(CONFIG_OF) && !defined(MODULE)
1161 #define _OF_DECLARE(table, name, compat, fn, fn_type)			\
1162 	static const struct of_device_id __of_table_##name		\
1163 		__used __section(__##table##_of_table)			\
1164 		 = { .compatible = compat,				\
1165 		     .data = (fn == (fn_type)NULL) ? fn : fn  }
1166 #else
1167 #define _OF_DECLARE(table, name, compat, fn, fn_type)			\
1168 	static const struct of_device_id __of_table_##name		\
1169 		__attribute__((unused))					\
1170 		 = { .compatible = compat,				\
1171 		     .data = (fn == (fn_type)NULL) ? fn : fn }
1172 #endif
1173 
1174 typedef int (*of_init_fn_2)(struct device_node *, struct device_node *);
1175 typedef int (*of_init_fn_1_ret)(struct device_node *);
1176 typedef void (*of_init_fn_1)(struct device_node *);
1177 
1178 #define OF_DECLARE_1(table, name, compat, fn) \
1179 		_OF_DECLARE(table, name, compat, fn, of_init_fn_1)
1180 #define OF_DECLARE_1_RET(table, name, compat, fn) \
1181 		_OF_DECLARE(table, name, compat, fn, of_init_fn_1_ret)
1182 #define OF_DECLARE_2(table, name, compat, fn) \
1183 		_OF_DECLARE(table, name, compat, fn, of_init_fn_2)
1184 
1185 /**
1186  * struct of_changeset_entry	- Holds a changeset entry
1187  *
1188  * @node:	list_head for the log list
1189  * @action:	notifier action
1190  * @np:		pointer to the device node affected
1191  * @prop:	pointer to the property affected
1192  * @old_prop:	hold a pointer to the original property
1193  *
1194  * Every modification of the device tree during a changeset
1195  * is held in a list of of_changeset_entry structures.
1196  * That way we can recover from a partial application, or we can
1197  * revert the changeset
1198  */
1199 struct of_changeset_entry {
1200 	struct list_head node;
1201 	unsigned long action;
1202 	struct device_node *np;
1203 	struct property *prop;
1204 	struct property *old_prop;
1205 };
1206 
1207 /**
1208  * struct of_changeset - changeset tracker structure
1209  *
1210  * @entries:	list_head for the changeset entries
1211  *
1212  * changesets are a convenient way to apply bulk changes to the
1213  * live tree. In case of an error, changes are rolled-back.
1214  * changesets live on after initial application, and if not
1215  * destroyed after use, they can be reverted in one single call.
1216  */
1217 struct of_changeset {
1218 	struct list_head entries;
1219 };
1220 
1221 enum of_reconfig_change {
1222 	OF_RECONFIG_NO_CHANGE = 0,
1223 	OF_RECONFIG_CHANGE_ADD,
1224 	OF_RECONFIG_CHANGE_REMOVE,
1225 };
1226 
1227 #ifdef CONFIG_OF_DYNAMIC
1228 extern int of_reconfig_notifier_register(struct notifier_block *);
1229 extern int of_reconfig_notifier_unregister(struct notifier_block *);
1230 extern int of_reconfig_notify(unsigned long, struct of_reconfig_data *rd);
1231 extern int of_reconfig_get_state_change(unsigned long action,
1232 					struct of_reconfig_data *arg);
1233 
1234 extern void of_changeset_init(struct of_changeset *ocs);
1235 extern void of_changeset_destroy(struct of_changeset *ocs);
1236 extern int of_changeset_apply(struct of_changeset *ocs);
1237 extern int of_changeset_revert(struct of_changeset *ocs);
1238 extern int of_changeset_action(struct of_changeset *ocs,
1239 		unsigned long action, struct device_node *np,
1240 		struct property *prop);
1241 
1242 static inline int of_changeset_attach_node(struct of_changeset *ocs,
1243 		struct device_node *np)
1244 {
1245 	return of_changeset_action(ocs, OF_RECONFIG_ATTACH_NODE, np, NULL);
1246 }
1247 
1248 static inline int of_changeset_detach_node(struct of_changeset *ocs,
1249 		struct device_node *np)
1250 {
1251 	return of_changeset_action(ocs, OF_RECONFIG_DETACH_NODE, np, NULL);
1252 }
1253 
1254 static inline int of_changeset_add_property(struct of_changeset *ocs,
1255 		struct device_node *np, struct property *prop)
1256 {
1257 	return of_changeset_action(ocs, OF_RECONFIG_ADD_PROPERTY, np, prop);
1258 }
1259 
1260 static inline int of_changeset_remove_property(struct of_changeset *ocs,
1261 		struct device_node *np, struct property *prop)
1262 {
1263 	return of_changeset_action(ocs, OF_RECONFIG_REMOVE_PROPERTY, np, prop);
1264 }
1265 
1266 static inline int of_changeset_update_property(struct of_changeset *ocs,
1267 		struct device_node *np, struct property *prop)
1268 {
1269 	return of_changeset_action(ocs, OF_RECONFIG_UPDATE_PROPERTY, np, prop);
1270 }
1271 #else /* CONFIG_OF_DYNAMIC */
1272 static inline int of_reconfig_notifier_register(struct notifier_block *nb)
1273 {
1274 	return -EINVAL;
1275 }
1276 static inline int of_reconfig_notifier_unregister(struct notifier_block *nb)
1277 {
1278 	return -EINVAL;
1279 }
1280 static inline int of_reconfig_notify(unsigned long action,
1281 				     struct of_reconfig_data *arg)
1282 {
1283 	return -EINVAL;
1284 }
1285 static inline int of_reconfig_get_state_change(unsigned long action,
1286 						struct of_reconfig_data *arg)
1287 {
1288 	return -EINVAL;
1289 }
1290 #endif /* CONFIG_OF_DYNAMIC */
1291 
1292 /**
1293  * of_device_is_system_power_controller - Tells if system-power-controller is found for device_node
1294  * @np: Pointer to the given device_node
1295  *
1296  * return true if present false otherwise
1297  */
1298 static inline bool of_device_is_system_power_controller(const struct device_node *np)
1299 {
1300 	return of_property_read_bool(np, "system-power-controller");
1301 }
1302 
1303 /**
1304  * Overlay support
1305  */
1306 
1307 enum of_overlay_notify_action {
1308 	OF_OVERLAY_PRE_APPLY = 0,
1309 	OF_OVERLAY_POST_APPLY,
1310 	OF_OVERLAY_PRE_REMOVE,
1311 	OF_OVERLAY_POST_REMOVE,
1312 };
1313 
1314 struct of_overlay_notify_data {
1315 	struct device_node *overlay;
1316 	struct device_node *target;
1317 };
1318 
1319 #ifdef CONFIG_OF_OVERLAY
1320 
1321 /* ID based overlays; the API for external users */
1322 int of_overlay_apply(struct device_node *tree, int *ovcs_id);
1323 int of_overlay_remove(int *ovcs_id);
1324 int of_overlay_remove_all(void);
1325 
1326 int of_overlay_notifier_register(struct notifier_block *nb);
1327 int of_overlay_notifier_unregister(struct notifier_block *nb);
1328 
1329 #else
1330 
1331 static inline int of_overlay_apply(struct device_node *tree, int *ovcs_id)
1332 {
1333 	return -ENOTSUPP;
1334 }
1335 
1336 static inline int of_overlay_remove(int *ovcs_id)
1337 {
1338 	return -ENOTSUPP;
1339 }
1340 
1341 static inline int of_overlay_remove_all(void)
1342 {
1343 	return -ENOTSUPP;
1344 }
1345 
1346 static inline int of_overlay_notifier_register(struct notifier_block *nb)
1347 {
1348 	return 0;
1349 }
1350 
1351 static inline int of_overlay_notifier_unregister(struct notifier_block *nb)
1352 {
1353 	return 0;
1354 }
1355 
1356 #endif
1357 
1358 #endif /* _LINUX_OF_H */
1359