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