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