1 /* SPDX-License-Identifier: GPL-2.0+ */
5 * Definitions for talking to the Open Firmware PROM on
6 * Power Macintosh and other computers.
8 * Copyright (C) 1996-2005 Paul Mackerras.
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.
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>
25 #include <asm/byteorder.h>
26 #include <asm/errno.h>
35 struct property
*next
;
36 #if defined(CONFIG_OF_DYNAMIC) || defined(CONFIG_SPARC)
39 #if defined(CONFIG_OF_PROMTREE)
40 unsigned int unique_id
;
42 #if defined(CONFIG_OF_KOBJ)
43 struct bin_attribute attr
;
47 #if defined(CONFIG_SPARC)
48 struct of_irq_controller
;
55 const char *full_name
;
56 struct fwnode_handle fwnode
;
58 struct property
*properties
;
59 struct property
*deadprops
; /* removed properties */
60 struct device_node
*parent
;
61 struct device_node
*child
;
62 struct device_node
*sibling
;
63 #if defined(CONFIG_OF_KOBJ)
68 #if defined(CONFIG_SPARC)
69 unsigned int unique_id
;
70 struct of_irq_controller
*irq_trans
;
74 #define MAX_PHANDLE_ARGS 16
75 struct of_phandle_args
{
76 struct device_node
*np
;
78 uint32_t args
[MAX_PHANDLE_ARGS
];
81 struct of_phandle_iterator
{
82 /* Common iterator information */
83 const char *cells_name
;
85 const struct device_node
*parent
;
87 /* List size information */
88 const __be32
*list_end
;
89 const __be32
*phandle_end
;
91 /* Current position state */
95 struct device_node
*node
;
98 struct of_reconfig_data
{
99 struct device_node
*dn
;
100 struct property
*prop
;
101 struct property
*old_prop
;
104 /* initialize a node */
105 extern struct kobj_type of_node_ktype
;
106 extern const struct fwnode_operations of_fwnode_ops
;
107 static inline void of_node_init(struct device_node
*node
)
109 #if defined(CONFIG_OF_KOBJ)
110 kobject_init(&node
->kobj
, &of_node_ktype
);
112 node
->fwnode
.ops
= &of_fwnode_ops
;
115 #if defined(CONFIG_OF_KOBJ)
116 #define of_node_kobj(n) (&(n)->kobj)
118 #define of_node_kobj(n) NULL
121 #ifdef CONFIG_OF_DYNAMIC
122 extern struct device_node
*of_node_get(struct device_node
*node
);
123 extern void of_node_put(struct device_node
*node
);
124 #else /* CONFIG_OF_DYNAMIC */
125 /* Dummy ref counting routines - to be implemented later */
126 static inline struct device_node
*of_node_get(struct device_node
*node
)
130 static inline void of_node_put(struct device_node
*node
) { }
131 #endif /* !CONFIG_OF_DYNAMIC */
133 /* Pointer for first entry in chain of all nodes. */
134 extern struct device_node
*of_root
;
135 extern struct device_node
*of_chosen
;
136 extern struct device_node
*of_aliases
;
137 extern struct device_node
*of_stdout
;
138 extern raw_spinlock_t devtree_lock
;
141 * struct device_node flag descriptions
142 * (need to be visible even when !CONFIG_OF)
144 #define OF_DYNAMIC 1 /* (and properties) allocated via kmalloc */
145 #define OF_DETACHED 2 /* detached from the device tree */
146 #define OF_POPULATED 3 /* device already created */
147 #define OF_POPULATED_BUS 4 /* platform bus created for children */
148 #define OF_OVERLAY 5 /* allocated for an overlay */
149 #define OF_OVERLAY_FREE_CSET 6 /* in overlay cset being freed */
151 #define OF_BAD_ADDR ((u64)-1)
154 void of_core_init(void);
156 static inline bool is_of_node(const struct fwnode_handle
*fwnode
)
158 return !IS_ERR_OR_NULL(fwnode
) && fwnode
->ops
== &of_fwnode_ops
;
161 #define to_of_node(__fwnode) \
163 typeof(__fwnode) __to_of_node_fwnode = (__fwnode); \
165 is_of_node(__to_of_node_fwnode) ? \
166 container_of(__to_of_node_fwnode, \
167 struct device_node, fwnode) : \
171 #define of_fwnode_handle(node) \
173 typeof(node) __of_fwnode_handle_node = (node); \
175 __of_fwnode_handle_node ? \
176 &__of_fwnode_handle_node->fwnode : NULL; \
179 static inline bool of_have_populated_dt(void)
181 return of_root
!= NULL
;
184 static inline bool of_node_is_root(const struct device_node
*node
)
186 return node
&& (node
->parent
== NULL
);
189 static inline int of_node_check_flag(struct device_node
*n
, unsigned long flag
)
191 return test_bit(flag
, &n
->_flags
);
194 static inline int of_node_test_and_set_flag(struct device_node
*n
,
197 return test_and_set_bit(flag
, &n
->_flags
);
200 static inline void of_node_set_flag(struct device_node
*n
, unsigned long flag
)
202 set_bit(flag
, &n
->_flags
);
205 static inline void of_node_clear_flag(struct device_node
*n
, unsigned long flag
)
207 clear_bit(flag
, &n
->_flags
);
210 #if defined(CONFIG_OF_DYNAMIC) || defined(CONFIG_SPARC)
211 static inline int of_property_check_flag(struct property
*p
, unsigned long flag
)
213 return test_bit(flag
, &p
->_flags
);
216 static inline void of_property_set_flag(struct property
*p
, unsigned long flag
)
218 set_bit(flag
, &p
->_flags
);
221 static inline void of_property_clear_flag(struct property
*p
, unsigned long flag
)
223 clear_bit(flag
, &p
->_flags
);
227 extern struct device_node
*__of_find_all_nodes(struct device_node
*prev
);
228 extern struct device_node
*of_find_all_nodes(struct device_node
*prev
);
231 * OF address retrieval & translation
234 /* Helper to read a big number; size is in cells (not bytes) */
235 static inline u64
of_read_number(const __be32
*cell
, int size
)
239 r
= (r
<< 32) | be32_to_cpu(*(cell
++));
243 /* Like of_read_number, but we want an unsigned long result */
244 static inline unsigned long of_read_ulong(const __be32
*cell
, int size
)
246 /* toss away upper bits if unsigned long is smaller than u64 */
247 return of_read_number(cell
, size
);
250 #if defined(CONFIG_SPARC)
251 #include <asm/prom.h>
254 #define OF_IS_DYNAMIC(x) test_bit(OF_DYNAMIC, &x->_flags)
255 #define OF_MARK_DYNAMIC(x) set_bit(OF_DYNAMIC, &x->_flags)
257 extern bool of_node_name_eq(const struct device_node
*np
, const char *name
);
258 extern bool of_node_name_prefix(const struct device_node
*np
, const char *prefix
);
260 static inline const char *of_node_full_name(const struct device_node
*np
)
262 return np
? np
->full_name
: "<no-node>";
265 #define for_each_of_allnodes_from(from, dn) \
266 for (dn = __of_find_all_nodes(from); dn; dn = __of_find_all_nodes(dn))
267 #define for_each_of_allnodes(dn) for_each_of_allnodes_from(NULL, dn)
268 extern struct device_node
*of_find_node_by_name(struct device_node
*from
,
270 extern struct device_node
*of_find_node_by_type(struct device_node
*from
,
272 extern struct device_node
*of_find_compatible_node(struct device_node
*from
,
273 const char *type
, const char *compat
);
274 extern struct device_node
*of_find_matching_node_and_match(
275 struct device_node
*from
,
276 const struct of_device_id
*matches
,
277 const struct of_device_id
**match
);
279 extern struct device_node
*of_find_node_opts_by_path(const char *path
,
281 static inline struct device_node
*of_find_node_by_path(const char *path
)
283 return of_find_node_opts_by_path(path
, NULL
);
286 extern struct device_node
*of_find_node_by_phandle(phandle handle
);
287 extern struct device_node
*of_get_parent(const struct device_node
*node
);
288 extern struct device_node
*of_get_next_parent(struct device_node
*node
);
289 extern struct device_node
*of_get_next_child(const struct device_node
*node
,
290 struct device_node
*prev
);
291 extern struct device_node
*of_get_next_available_child(
292 const struct device_node
*node
, struct device_node
*prev
);
294 extern struct device_node
*of_get_compatible_child(const struct device_node
*parent
,
295 const char *compatible
);
296 extern struct device_node
*of_get_child_by_name(const struct device_node
*node
,
300 extern struct device_node
*of_find_next_cache_node(const struct device_node
*);
301 extern int of_find_last_cache_level(unsigned int cpu
);
302 extern struct device_node
*of_find_node_with_property(
303 struct device_node
*from
, const char *prop_name
);
305 extern struct property
*of_find_property(const struct device_node
*np
,
308 extern int of_property_count_elems_of_size(const struct device_node
*np
,
309 const char *propname
, int elem_size
);
310 extern int of_property_read_u32_index(const struct device_node
*np
,
311 const char *propname
,
312 u32 index
, u32
*out_value
);
313 extern int of_property_read_u64_index(const struct device_node
*np
,
314 const char *propname
,
315 u32 index
, u64
*out_value
);
316 extern int of_property_read_variable_u8_array(const struct device_node
*np
,
317 const char *propname
, u8
*out_values
,
318 size_t sz_min
, size_t sz_max
);
319 extern int of_property_read_variable_u16_array(const struct device_node
*np
,
320 const char *propname
, u16
*out_values
,
321 size_t sz_min
, size_t sz_max
);
322 extern int of_property_read_variable_u32_array(const struct device_node
*np
,
323 const char *propname
,
327 extern int of_property_read_u64(const struct device_node
*np
,
328 const char *propname
, u64
*out_value
);
329 extern int of_property_read_variable_u64_array(const struct device_node
*np
,
330 const char *propname
,
335 extern int of_property_read_string(const struct device_node
*np
,
336 const char *propname
,
337 const char **out_string
);
338 extern int of_property_match_string(const struct device_node
*np
,
339 const char *propname
,
341 extern int of_property_read_string_helper(const struct device_node
*np
,
342 const char *propname
,
343 const char **out_strs
, size_t sz
, int index
);
344 extern int of_device_is_compatible(const struct device_node
*device
,
346 extern int of_device_compatible_match(struct device_node
*device
,
347 const char *const *compat
);
348 extern bool of_device_is_available(const struct device_node
*device
);
349 extern bool of_device_is_big_endian(const struct device_node
*device
);
350 extern const void *of_get_property(const struct device_node
*node
,
353 extern struct device_node
*of_get_cpu_node(int cpu
, unsigned int *thread
);
354 extern struct device_node
*of_get_next_cpu_node(struct device_node
*prev
);
356 #define for_each_property_of_node(dn, pp) \
357 for (pp = dn->properties; pp != NULL; pp = pp->next)
359 extern int of_n_addr_cells(struct device_node
*np
);
360 extern int of_n_size_cells(struct device_node
*np
);
361 extern const struct of_device_id
*of_match_node(
362 const struct of_device_id
*matches
, const struct device_node
*node
);
363 extern int of_modalias_node(struct device_node
*node
, char *modalias
, int len
);
364 extern void of_print_phandle_args(const char *msg
, const struct of_phandle_args
*args
);
365 extern struct device_node
*of_parse_phandle(const struct device_node
*np
,
366 const char *phandle_name
,
368 extern int of_parse_phandle_with_args(const struct device_node
*np
,
369 const char *list_name
, const char *cells_name
, int index
,
370 struct of_phandle_args
*out_args
);
371 extern int of_parse_phandle_with_args_map(const struct device_node
*np
,
372 const char *list_name
, const char *stem_name
, int index
,
373 struct of_phandle_args
*out_args
);
374 extern int of_parse_phandle_with_fixed_args(const struct device_node
*np
,
375 const char *list_name
, int cells_count
, int index
,
376 struct of_phandle_args
*out_args
);
377 extern int of_count_phandle_with_args(const struct device_node
*np
,
378 const char *list_name
, const char *cells_name
);
380 /* phandle iterator functions */
381 extern int of_phandle_iterator_init(struct of_phandle_iterator
*it
,
382 const struct device_node
*np
,
383 const char *list_name
,
384 const char *cells_name
,
387 extern int of_phandle_iterator_next(struct of_phandle_iterator
*it
);
388 extern int of_phandle_iterator_args(struct of_phandle_iterator
*it
,
392 extern void of_alias_scan(void * (*dt_alloc
)(u64 size
, u64 align
));
393 extern int of_alias_get_id(struct device_node
*np
, const char *stem
);
394 extern int of_alias_get_highest_id(const char *stem
);
395 extern int of_alias_get_alias_list(const struct of_device_id
*matches
,
396 const char *stem
, unsigned long *bitmap
,
399 extern int of_machine_is_compatible(const char *compat
);
401 extern int of_add_property(struct device_node
*np
, struct property
*prop
);
402 extern int of_remove_property(struct device_node
*np
, struct property
*prop
);
403 extern int of_update_property(struct device_node
*np
, struct property
*newprop
);
405 /* For updating the device tree at runtime */
406 #define OF_RECONFIG_ATTACH_NODE 0x0001
407 #define OF_RECONFIG_DETACH_NODE 0x0002
408 #define OF_RECONFIG_ADD_PROPERTY 0x0003
409 #define OF_RECONFIG_REMOVE_PROPERTY 0x0004
410 #define OF_RECONFIG_UPDATE_PROPERTY 0x0005
412 extern int of_attach_node(struct device_node
*);
413 extern int of_detach_node(struct device_node
*);
415 #define of_match_ptr(_ptr) (_ptr)
418 * of_property_read_u8_array - Find and read an array of u8 from a property.
420 * @np: device node from which the property value is to be read.
421 * @propname: name of the property to be searched.
422 * @out_values: pointer to return value, modified only if return value is 0.
423 * @sz: number of array elements to read
425 * Search for a property in a device node and read 8-bit value(s) from
426 * it. Returns 0 on success, -EINVAL if the property does not exist,
427 * -ENODATA if property does not have a value, and -EOVERFLOW if the
428 * property data isn't large enough.
430 * dts entry of array should be like:
431 * property = /bits/ 8 <0x50 0x60 0x70>;
433 * The out_values is modified only if a valid u8 value can be decoded.
435 static inline int of_property_read_u8_array(const struct device_node
*np
,
436 const char *propname
,
437 u8
*out_values
, size_t sz
)
439 int ret
= of_property_read_variable_u8_array(np
, propname
, out_values
,
448 * of_property_read_u16_array - Find and read an array of u16 from a property.
450 * @np: device node from which the property value is to be read.
451 * @propname: name of the property to be searched.
452 * @out_values: pointer to return value, modified only if return value is 0.
453 * @sz: number of array elements to read
455 * Search for a property in a device node and read 16-bit value(s) from
456 * it. Returns 0 on success, -EINVAL if the property does not exist,
457 * -ENODATA if property does not have a value, and -EOVERFLOW if the
458 * property data isn't large enough.
460 * dts entry of array should be like:
461 * property = /bits/ 16 <0x5000 0x6000 0x7000>;
463 * The out_values is modified only if a valid u16 value can be decoded.
465 static inline int of_property_read_u16_array(const struct device_node
*np
,
466 const char *propname
,
467 u16
*out_values
, size_t sz
)
469 int ret
= of_property_read_variable_u16_array(np
, propname
, out_values
,
478 * of_property_read_u32_array - Find and read an array of 32 bit integers
481 * @np: device node from which the property value is to be read.
482 * @propname: name of the property to be searched.
483 * @out_values: pointer to return value, modified only if return value is 0.
484 * @sz: number of array elements to read
486 * Search for a property in a device node and read 32-bit value(s) from
487 * it. Returns 0 on success, -EINVAL if the property does not exist,
488 * -ENODATA if property does not have a value, and -EOVERFLOW if the
489 * property data isn't large enough.
491 * The out_values is modified only if a valid u32 value can be decoded.
493 static inline int of_property_read_u32_array(const struct device_node
*np
,
494 const char *propname
,
495 u32
*out_values
, size_t sz
)
497 int ret
= of_property_read_variable_u32_array(np
, propname
, out_values
,
506 * of_property_read_u64_array - Find and read an array of 64 bit integers
509 * @np: device node from which the property value is to be read.
510 * @propname: name of the property to be searched.
511 * @out_values: pointer to return value, modified only if return value is 0.
512 * @sz: number of array elements to read
514 * Search for a property in a device node and read 64-bit value(s) from
515 * it. Returns 0 on success, -EINVAL if the property does not exist,
516 * -ENODATA if property does not have a value, and -EOVERFLOW if the
517 * property data isn't large enough.
519 * The out_values is modified only if a valid u64 value can be decoded.
521 static inline int of_property_read_u64_array(const struct device_node
*np
,
522 const char *propname
,
523 u64
*out_values
, size_t sz
)
525 int ret
= of_property_read_variable_u64_array(np
, propname
, out_values
,
534 * struct property *prop;
538 * of_property_for_each_u32(np, "propname", prop, p, u)
539 * printk("U32 value: %x\n", u);
541 const __be32
*of_prop_next_u32(struct property
*prop
, const __be32
*cur
,
544 * struct property *prop;
547 * of_property_for_each_string(np, "propname", prop, s)
548 * printk("String value: %s\n", s);
550 const char *of_prop_next_string(struct property
*prop
, const char *cur
);
552 bool of_console_check(struct device_node
*dn
, char *name
, int index
);
554 extern int of_cpu_node_to_id(struct device_node
*np
);
556 int of_map_rid(struct device_node
*np
, u32 rid
,
557 const char *map_name
, const char *map_mask_name
,
558 struct device_node
**target
, u32
*id_out
);
560 #else /* CONFIG_OF */
562 static inline void of_core_init(void)
566 static inline bool is_of_node(const struct fwnode_handle
*fwnode
)
571 static inline struct device_node
*to_of_node(const struct fwnode_handle
*fwnode
)
576 static inline bool of_node_name_eq(const struct device_node
*np
, const char *name
)
581 static inline bool of_node_name_prefix(const struct device_node
*np
, const char *prefix
)
586 static inline const char* of_node_full_name(const struct device_node
*np
)
591 static inline struct device_node
*of_find_node_by_name(struct device_node
*from
,
597 static inline struct device_node
*of_find_node_by_type(struct device_node
*from
,
603 static inline struct device_node
*of_find_matching_node_and_match(
604 struct device_node
*from
,
605 const struct of_device_id
*matches
,
606 const struct of_device_id
**match
)
611 static inline struct device_node
*of_find_node_by_path(const char *path
)
616 static inline struct device_node
*of_find_node_opts_by_path(const char *path
,
622 static inline struct device_node
*of_find_node_by_phandle(phandle handle
)
627 static inline struct device_node
*of_get_parent(const struct device_node
*node
)
632 static inline struct device_node
*of_get_next_child(
633 const struct device_node
*node
, struct device_node
*prev
)
638 static inline struct device_node
*of_get_next_available_child(
639 const struct device_node
*node
, struct device_node
*prev
)
644 static inline struct device_node
*of_find_node_with_property(
645 struct device_node
*from
, const char *prop_name
)
650 #define of_fwnode_handle(node) NULL
652 static inline bool of_have_populated_dt(void)
657 static inline struct device_node
*of_get_compatible_child(const struct device_node
*parent
,
658 const char *compatible
)
663 static inline struct device_node
*of_get_child_by_name(
664 const struct device_node
*node
,
670 static inline int of_device_is_compatible(const struct device_node
*device
,
676 static inline int of_device_compatible_match(struct device_node
*device
,
677 const char *const *compat
)
682 static inline bool of_device_is_available(const struct device_node
*device
)
687 static inline bool of_device_is_big_endian(const struct device_node
*device
)
692 static inline struct property
*of_find_property(const struct device_node
*np
,
699 static inline struct device_node
*of_find_compatible_node(
700 struct device_node
*from
,
707 static inline int of_property_count_elems_of_size(const struct device_node
*np
,
708 const char *propname
, int elem_size
)
713 static inline int of_property_read_u8_array(const struct device_node
*np
,
714 const char *propname
, u8
*out_values
, size_t sz
)
719 static inline int of_property_read_u16_array(const struct device_node
*np
,
720 const char *propname
, u16
*out_values
, size_t sz
)
725 static inline int of_property_read_u32_array(const struct device_node
*np
,
726 const char *propname
,
727 u32
*out_values
, size_t sz
)
732 static inline int of_property_read_u64_array(const struct device_node
*np
,
733 const char *propname
,
734 u64
*out_values
, size_t sz
)
739 static inline int of_property_read_u32_index(const struct device_node
*np
,
740 const char *propname
, u32 index
, u32
*out_value
)
745 static inline int of_property_read_u64_index(const struct device_node
*np
,
746 const char *propname
, u32 index
, u64
*out_value
)
751 static inline const void *of_get_property(const struct device_node
*node
,
758 static inline struct device_node
*of_get_cpu_node(int cpu
,
759 unsigned int *thread
)
764 static inline struct device_node
*of_get_next_cpu_node(struct device_node
*prev
)
769 static inline int of_n_addr_cells(struct device_node
*np
)
774 static inline int of_n_size_cells(struct device_node
*np
)
779 static inline int of_property_read_variable_u8_array(const struct device_node
*np
,
780 const char *propname
, u8
*out_values
,
781 size_t sz_min
, size_t sz_max
)
786 static inline int of_property_read_variable_u16_array(const struct device_node
*np
,
787 const char *propname
, u16
*out_values
,
788 size_t sz_min
, size_t sz_max
)
793 static inline int of_property_read_variable_u32_array(const struct device_node
*np
,
794 const char *propname
,
802 static inline int of_property_read_u64(const struct device_node
*np
,
803 const char *propname
, u64
*out_value
)
808 static inline int of_property_read_variable_u64_array(const struct device_node
*np
,
809 const char *propname
,
817 static inline int of_property_read_string(const struct device_node
*np
,
818 const char *propname
,
819 const char **out_string
)
824 static inline int of_property_match_string(const struct device_node
*np
,
825 const char *propname
,
831 static inline int of_property_read_string_helper(const struct device_node
*np
,
832 const char *propname
,
833 const char **out_strs
, size_t sz
, int index
)
838 static inline struct device_node
*of_parse_phandle(const struct device_node
*np
,
839 const char *phandle_name
,
845 static inline int of_parse_phandle_with_args(const struct device_node
*np
,
846 const char *list_name
,
847 const char *cells_name
,
849 struct of_phandle_args
*out_args
)
854 static inline int of_parse_phandle_with_args_map(const struct device_node
*np
,
855 const char *list_name
,
856 const char *stem_name
,
858 struct of_phandle_args
*out_args
)
863 static inline int of_parse_phandle_with_fixed_args(const struct device_node
*np
,
864 const char *list_name
, int cells_count
, int index
,
865 struct of_phandle_args
*out_args
)
870 static inline int of_count_phandle_with_args(struct device_node
*np
,
871 const char *list_name
,
872 const char *cells_name
)
877 static inline int of_phandle_iterator_init(struct of_phandle_iterator
*it
,
878 const struct device_node
*np
,
879 const char *list_name
,
880 const char *cells_name
,
886 static inline int of_phandle_iterator_next(struct of_phandle_iterator
*it
)
891 static inline int of_phandle_iterator_args(struct of_phandle_iterator
*it
,
898 static inline int of_alias_get_id(struct device_node
*np
, const char *stem
)
903 static inline int of_alias_get_highest_id(const char *stem
)
908 static inline int of_alias_get_alias_list(const struct of_device_id
*matches
,
909 const char *stem
, unsigned long *bitmap
,
915 static inline int of_machine_is_compatible(const char *compat
)
920 static inline bool of_console_check(const struct device_node
*dn
, const char *name
, int index
)
925 static inline const __be32
*of_prop_next_u32(struct property
*prop
,
926 const __be32
*cur
, u32
*pu
)
931 static inline const char *of_prop_next_string(struct property
*prop
,
937 static inline int of_node_check_flag(struct device_node
*n
, unsigned long flag
)
942 static inline int of_node_test_and_set_flag(struct device_node
*n
,
948 static inline void of_node_set_flag(struct device_node
*n
, unsigned long flag
)
952 static inline void of_node_clear_flag(struct device_node
*n
, unsigned long flag
)
956 static inline int of_property_check_flag(struct property
*p
, unsigned long flag
)
961 static inline void of_property_set_flag(struct property
*p
, unsigned long flag
)
965 static inline void of_property_clear_flag(struct property
*p
, unsigned long flag
)
969 static inline int of_cpu_node_to_id(struct device_node
*np
)
974 static inline int of_map_rid(struct device_node
*np
, u32 rid
,
975 const char *map_name
, const char *map_mask_name
,
976 struct device_node
**target
, u32
*id_out
)
981 #define of_match_ptr(_ptr) NULL
982 #define of_match_node(_matches, _node) NULL
983 #endif /* CONFIG_OF */
985 /* Default string compare functions, Allow arch asm/prom.h to override */
986 #if !defined(of_compat_cmp)
987 #define of_compat_cmp(s1, s2, l) strcasecmp((s1), (s2))
988 #define of_prop_cmp(s1, s2) strcmp((s1), (s2))
989 #define of_node_cmp(s1, s2) strcasecmp((s1), (s2))
992 static inline int of_prop_val_eq(struct property
*p1
, struct property
*p2
)
994 return p1
->length
== p2
->length
&&
995 !memcmp(p1
->value
, p2
->value
, (size_t)p1
->length
);
998 #if defined(CONFIG_OF) && defined(CONFIG_NUMA)
999 extern int of_node_to_nid(struct device_node
*np
);
1001 static inline int of_node_to_nid(struct device_node
*device
)
1003 return NUMA_NO_NODE
;
1007 #ifdef CONFIG_OF_NUMA
1008 extern int of_numa_init(void);
1010 static inline int of_numa_init(void)
1016 static inline struct device_node
*of_find_matching_node(
1017 struct device_node
*from
,
1018 const struct of_device_id
*matches
)
1020 return of_find_matching_node_and_match(from
, matches
, NULL
);
1023 static inline const char *of_node_get_device_type(const struct device_node
*np
)
1025 return of_get_property(np
, "device_type", NULL
);
1028 static inline bool of_node_is_type(const struct device_node
*np
, const char *type
)
1030 const char *match
= of_node_get_device_type(np
);
1032 return np
&& match
&& type
&& !strcmp(match
, type
);
1036 * of_property_count_u8_elems - Count the number of u8 elements in a property
1038 * @np: device node from which the property value is to be read.
1039 * @propname: name of the property to be searched.
1041 * Search for a property in a device node and count the number of u8 elements
1042 * in it. Returns number of elements on sucess, -EINVAL if the property does
1043 * not exist or its length does not match a multiple of u8 and -ENODATA if the
1044 * property does not have a value.
1046 static inline int of_property_count_u8_elems(const struct device_node
*np
,
1047 const char *propname
)
1049 return of_property_count_elems_of_size(np
, propname
, sizeof(u8
));
1053 * of_property_count_u16_elems - Count the number of u16 elements in a property
1055 * @np: device node from which the property value is to be read.
1056 * @propname: name of the property to be searched.
1058 * Search for a property in a device node and count the number of u16 elements
1059 * in it. Returns number of elements on sucess, -EINVAL if the property does
1060 * not exist or its length does not match a multiple of u16 and -ENODATA if the
1061 * property does not have a value.
1063 static inline int of_property_count_u16_elems(const struct device_node
*np
,
1064 const char *propname
)
1066 return of_property_count_elems_of_size(np
, propname
, sizeof(u16
));
1070 * of_property_count_u32_elems - Count the number of u32 elements in a property
1072 * @np: device node from which the property value is to be read.
1073 * @propname: name of the property to be searched.
1075 * Search for a property in a device node and count the number of u32 elements
1076 * in it. Returns number of elements on sucess, -EINVAL if the property does
1077 * not exist or its length does not match a multiple of u32 and -ENODATA if the
1078 * property does not have a value.
1080 static inline int of_property_count_u32_elems(const struct device_node
*np
,
1081 const char *propname
)
1083 return of_property_count_elems_of_size(np
, propname
, sizeof(u32
));
1087 * of_property_count_u64_elems - Count the number of u64 elements in a property
1089 * @np: device node from which the property value is to be read.
1090 * @propname: name of the property to be searched.
1092 * Search for a property in a device node and count the number of u64 elements
1093 * in it. Returns number of elements on sucess, -EINVAL if the property does
1094 * not exist or its length does not match a multiple of u64 and -ENODATA if the
1095 * property does not have a value.
1097 static inline int of_property_count_u64_elems(const struct device_node
*np
,
1098 const char *propname
)
1100 return of_property_count_elems_of_size(np
, propname
, sizeof(u64
));
1104 * of_property_read_string_array() - Read an array of strings from a multiple
1106 * @np: device node from which the property value is to be read.
1107 * @propname: name of the property to be searched.
1108 * @out_strs: output array of string pointers.
1109 * @sz: number of array elements to read.
1111 * Search for a property in a device tree node and retrieve a list of
1112 * terminated string values (pointer to data, not a copy) in that property.
1114 * If @out_strs is NULL, the number of strings in the property is returned.
1116 static inline int of_property_read_string_array(const struct device_node
*np
,
1117 const char *propname
, const char **out_strs
,
1120 return of_property_read_string_helper(np
, propname
, out_strs
, sz
, 0);
1124 * of_property_count_strings() - Find and return the number of strings from a
1125 * multiple strings property.
1126 * @np: device node from which the property value is to be read.
1127 * @propname: name of the property to be searched.
1129 * Search for a property in a device tree node and retrieve the number of null
1130 * terminated string contain in it. Returns the number of strings on
1131 * success, -EINVAL if the property does not exist, -ENODATA if property
1132 * does not have a value, and -EILSEQ if the string is not null-terminated
1133 * within the length of the property data.
1135 static inline int of_property_count_strings(const struct device_node
*np
,
1136 const char *propname
)
1138 return of_property_read_string_helper(np
, propname
, NULL
, 0, 0);
1142 * of_property_read_string_index() - Find and read a string from a multiple
1144 * @np: device node from which the property value is to be read.
1145 * @propname: name of the property to be searched.
1146 * @index: index of the string in the list of strings
1147 * @out_string: pointer to null terminated return string, modified only if
1148 * return value is 0.
1150 * Search for a property in a device tree node and retrieve a null
1151 * terminated string value (pointer to data, not a copy) in the list of strings
1152 * contained in that property.
1153 * Returns 0 on success, -EINVAL if the property does not exist, -ENODATA if
1154 * property does not have a value, and -EILSEQ if the string is not
1155 * null-terminated within the length of the property data.
1157 * The out_string pointer is modified only if a valid string can be decoded.
1159 static inline int of_property_read_string_index(const struct device_node
*np
,
1160 const char *propname
,
1161 int index
, const char **output
)
1163 int rc
= of_property_read_string_helper(np
, propname
, output
, 1, index
);
1164 return rc
< 0 ? rc
: 0;
1168 * of_property_read_bool - Findfrom a property
1169 * @np: device node from which the property value is to be read.
1170 * @propname: name of the property to be searched.
1172 * Search for a property in a device node.
1173 * Returns true if the property exists false otherwise.
1175 static inline bool of_property_read_bool(const struct device_node
*np
,
1176 const char *propname
)
1178 struct property
*prop
= of_find_property(np
, propname
, NULL
);
1180 return prop
? true : false;
1183 static inline int of_property_read_u8(const struct device_node
*np
,
1184 const char *propname
,
1187 return of_property_read_u8_array(np
, propname
, out_value
, 1);
1190 static inline int of_property_read_u16(const struct device_node
*np
,
1191 const char *propname
,
1194 return of_property_read_u16_array(np
, propname
, out_value
, 1);
1197 static inline int of_property_read_u32(const struct device_node
*np
,
1198 const char *propname
,
1201 return of_property_read_u32_array(np
, propname
, out_value
, 1);
1204 static inline int of_property_read_s32(const struct device_node
*np
,
1205 const char *propname
,
1208 return of_property_read_u32(np
, propname
, (u32
*) out_value
);
1211 #define of_for_each_phandle(it, err, np, ln, cn, cc) \
1212 for (of_phandle_iterator_init((it), (np), (ln), (cn), (cc)), \
1213 err = of_phandle_iterator_next(it); \
1215 err = of_phandle_iterator_next(it))
1217 #define of_property_for_each_u32(np, propname, prop, p, u) \
1218 for (prop = of_find_property(np, propname, NULL), \
1219 p = of_prop_next_u32(prop, NULL, &u); \
1221 p = of_prop_next_u32(prop, p, &u))
1223 #define of_property_for_each_string(np, propname, prop, s) \
1224 for (prop = of_find_property(np, propname, NULL), \
1225 s = of_prop_next_string(prop, NULL); \
1227 s = of_prop_next_string(prop, s))
1229 #define for_each_node_by_name(dn, name) \
1230 for (dn = of_find_node_by_name(NULL, name); dn; \
1231 dn = of_find_node_by_name(dn, name))
1232 #define for_each_node_by_type(dn, type) \
1233 for (dn = of_find_node_by_type(NULL, type); dn; \
1234 dn = of_find_node_by_type(dn, type))
1235 #define for_each_compatible_node(dn, type, compatible) \
1236 for (dn = of_find_compatible_node(NULL, type, compatible); dn; \
1237 dn = of_find_compatible_node(dn, type, compatible))
1238 #define for_each_matching_node(dn, matches) \
1239 for (dn = of_find_matching_node(NULL, matches); dn; \
1240 dn = of_find_matching_node(dn, matches))
1241 #define for_each_matching_node_and_match(dn, matches, match) \
1242 for (dn = of_find_matching_node_and_match(NULL, matches, match); \
1243 dn; dn = of_find_matching_node_and_match(dn, matches, match))
1245 #define for_each_child_of_node(parent, child) \
1246 for (child = of_get_next_child(parent, NULL); child != NULL; \
1247 child = of_get_next_child(parent, child))
1248 #define for_each_available_child_of_node(parent, child) \
1249 for (child = of_get_next_available_child(parent, NULL); child != NULL; \
1250 child = of_get_next_available_child(parent, child))
1252 #define for_each_of_cpu_node(cpu) \
1253 for (cpu = of_get_next_cpu_node(NULL); cpu != NULL; \
1254 cpu = of_get_next_cpu_node(cpu))
1256 #define for_each_node_with_property(dn, prop_name) \
1257 for (dn = of_find_node_with_property(NULL, prop_name); dn; \
1258 dn = of_find_node_with_property(dn, prop_name))
1260 static inline int of_get_child_count(const struct device_node
*np
)
1262 struct device_node
*child
;
1265 for_each_child_of_node(np
, child
)
1271 static inline int of_get_available_child_count(const struct device_node
*np
)
1273 struct device_node
*child
;
1276 for_each_available_child_of_node(np
, child
)
1282 #if defined(CONFIG_OF) && !defined(MODULE)
1283 #define _OF_DECLARE(table, name, compat, fn, fn_type) \
1284 static const struct of_device_id __of_table_##name \
1285 __used __section(__##table##_of_table) \
1286 = { .compatible = compat, \
1287 .data = (fn == (fn_type)NULL) ? fn : fn }
1289 #define _OF_DECLARE(table, name, compat, fn, fn_type) \
1290 static const struct of_device_id __of_table_##name \
1291 __attribute__((unused)) \
1292 = { .compatible = compat, \
1293 .data = (fn == (fn_type)NULL) ? fn : fn }
1296 typedef int (*of_init_fn_2
)(struct device_node
*, struct device_node
*);
1297 typedef int (*of_init_fn_1_ret
)(struct device_node
*);
1298 typedef void (*of_init_fn_1
)(struct device_node
*);
1300 #define OF_DECLARE_1(table, name, compat, fn) \
1301 _OF_DECLARE(table, name, compat, fn, of_init_fn_1)
1302 #define OF_DECLARE_1_RET(table, name, compat, fn) \
1303 _OF_DECLARE(table, name, compat, fn, of_init_fn_1_ret)
1304 #define OF_DECLARE_2(table, name, compat, fn) \
1305 _OF_DECLARE(table, name, compat, fn, of_init_fn_2)
1308 * struct of_changeset_entry - Holds a changeset entry
1310 * @node: list_head for the log list
1311 * @action: notifier action
1312 * @np: pointer to the device node affected
1313 * @prop: pointer to the property affected
1314 * @old_prop: hold a pointer to the original property
1316 * Every modification of the device tree during a changeset
1317 * is held in a list of of_changeset_entry structures.
1318 * That way we can recover from a partial application, or we can
1319 * revert the changeset
1321 struct of_changeset_entry
{
1322 struct list_head node
;
1323 unsigned long action
;
1324 struct device_node
*np
;
1325 struct property
*prop
;
1326 struct property
*old_prop
;
1330 * struct of_changeset - changeset tracker structure
1332 * @entries: list_head for the changeset entries
1334 * changesets are a convenient way to apply bulk changes to the
1335 * live tree. In case of an error, changes are rolled-back.
1336 * changesets live on after initial application, and if not
1337 * destroyed after use, they can be reverted in one single call.
1339 struct of_changeset
{
1340 struct list_head entries
;
1343 enum of_reconfig_change
{
1344 OF_RECONFIG_NO_CHANGE
= 0,
1345 OF_RECONFIG_CHANGE_ADD
,
1346 OF_RECONFIG_CHANGE_REMOVE
,
1349 #ifdef CONFIG_OF_DYNAMIC
1350 extern int of_reconfig_notifier_register(struct notifier_block
*);
1351 extern int of_reconfig_notifier_unregister(struct notifier_block
*);
1352 extern int of_reconfig_notify(unsigned long, struct of_reconfig_data
*rd
);
1353 extern int of_reconfig_get_state_change(unsigned long action
,
1354 struct of_reconfig_data
*arg
);
1356 extern void of_changeset_init(struct of_changeset
*ocs
);
1357 extern void of_changeset_destroy(struct of_changeset
*ocs
);
1358 extern int of_changeset_apply(struct of_changeset
*ocs
);
1359 extern int of_changeset_revert(struct of_changeset
*ocs
);
1360 extern int of_changeset_action(struct of_changeset
*ocs
,
1361 unsigned long action
, struct device_node
*np
,
1362 struct property
*prop
);
1364 static inline int of_changeset_attach_node(struct of_changeset
*ocs
,
1365 struct device_node
*np
)
1367 return of_changeset_action(ocs
, OF_RECONFIG_ATTACH_NODE
, np
, NULL
);
1370 static inline int of_changeset_detach_node(struct of_changeset
*ocs
,
1371 struct device_node
*np
)
1373 return of_changeset_action(ocs
, OF_RECONFIG_DETACH_NODE
, np
, NULL
);
1376 static inline int of_changeset_add_property(struct of_changeset
*ocs
,
1377 struct device_node
*np
, struct property
*prop
)
1379 return of_changeset_action(ocs
, OF_RECONFIG_ADD_PROPERTY
, np
, prop
);
1382 static inline int of_changeset_remove_property(struct of_changeset
*ocs
,
1383 struct device_node
*np
, struct property
*prop
)
1385 return of_changeset_action(ocs
, OF_RECONFIG_REMOVE_PROPERTY
, np
, prop
);
1388 static inline int of_changeset_update_property(struct of_changeset
*ocs
,
1389 struct device_node
*np
, struct property
*prop
)
1391 return of_changeset_action(ocs
, OF_RECONFIG_UPDATE_PROPERTY
, np
, prop
);
1393 #else /* CONFIG_OF_DYNAMIC */
1394 static inline int of_reconfig_notifier_register(struct notifier_block
*nb
)
1398 static inline int of_reconfig_notifier_unregister(struct notifier_block
*nb
)
1402 static inline int of_reconfig_notify(unsigned long action
,
1403 struct of_reconfig_data
*arg
)
1407 static inline int of_reconfig_get_state_change(unsigned long action
,
1408 struct of_reconfig_data
*arg
)
1412 #endif /* CONFIG_OF_DYNAMIC */
1415 * of_device_is_system_power_controller - Tells if system-power-controller is found for device_node
1416 * @np: Pointer to the given device_node
1418 * return true if present false otherwise
1420 static inline bool of_device_is_system_power_controller(const struct device_node
*np
)
1422 return of_property_read_bool(np
, "system-power-controller");
1429 enum of_overlay_notify_action
{
1430 OF_OVERLAY_PRE_APPLY
= 0,
1431 OF_OVERLAY_POST_APPLY
,
1432 OF_OVERLAY_PRE_REMOVE
,
1433 OF_OVERLAY_POST_REMOVE
,
1436 struct of_overlay_notify_data
{
1437 struct device_node
*overlay
;
1438 struct device_node
*target
;
1441 #ifdef CONFIG_OF_OVERLAY
1443 int of_overlay_fdt_apply(const void *overlay_fdt
, u32 overlay_fdt_size
,
1445 int of_overlay_remove(int *ovcs_id
);
1446 int of_overlay_remove_all(void);
1448 int of_overlay_notifier_register(struct notifier_block
*nb
);
1449 int of_overlay_notifier_unregister(struct notifier_block
*nb
);
1453 static inline int of_overlay_fdt_apply(void *overlay_fdt
, int *ovcs_id
)
1458 static inline int of_overlay_remove(int *ovcs_id
)
1463 static inline int of_overlay_remove_all(void)
1468 static inline int of_overlay_notifier_register(struct notifier_block
*nb
)
1473 static inline int of_overlay_notifier_unregister(struct notifier_block
*nb
)
1480 #endif /* _LINUX_OF_H */