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