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 const char *path_component_name;
70 unsigned int unique_id;
71 struct of_irq_controller *irq_trans;
75 #define MAX_PHANDLE_ARGS 16
76 struct of_phandle_args {
77 struct device_node *np;
79 uint32_t args[MAX_PHANDLE_ARGS];
82 struct of_phandle_iterator {
83 /* Common iterator information */
84 const char *cells_name;
86 const struct device_node *parent;
88 /* List size information */
89 const __be32 *list_end;
90 const __be32 *phandle_end;
92 /* Current position state */
96 struct device_node *node;
99 struct of_reconfig_data {
100 struct device_node *dn;
101 struct property *prop;
102 struct property *old_prop;
105 /* initialize a node */
106 extern struct kobj_type of_node_ktype;
107 extern const struct fwnode_operations of_fwnode_ops;
108 static inline void of_node_init(struct device_node *node)
110 #if defined(CONFIG_OF_KOBJ)
111 kobject_init(&node->kobj, &of_node_ktype);
113 node->fwnode.ops = &of_fwnode_ops;
116 #if defined(CONFIG_OF_KOBJ)
117 #define of_node_kobj(n) (&(n)->kobj)
119 #define of_node_kobj(n) NULL
122 #ifdef CONFIG_OF_DYNAMIC
123 extern struct device_node *of_node_get(struct device_node *node);
124 extern void of_node_put(struct device_node *node);
125 #else /* CONFIG_OF_DYNAMIC */
126 /* Dummy ref counting routines - to be implemented later */
127 static inline struct device_node *of_node_get(struct device_node *node)
131 static inline void of_node_put(struct device_node *node) { }
132 #endif /* !CONFIG_OF_DYNAMIC */
134 /* Pointer for first entry in chain of all nodes. */
135 extern struct device_node *of_root;
136 extern struct device_node *of_chosen;
137 extern struct device_node *of_aliases;
138 extern struct device_node *of_stdout;
139 extern raw_spinlock_t devtree_lock;
141 /* flag descriptions (need to be visible even when !CONFIG_OF) */
142 #define OF_DYNAMIC 1 /* node and properties were allocated via kmalloc */
143 #define OF_DETACHED 2 /* node has been detached from the device tree */
144 #define OF_POPULATED 3 /* device already created for the node */
145 #define OF_POPULATED_BUS 4 /* of_platform_populate recursed to children of this node */
147 #define OF_BAD_ADDR ((u64)-1)
150 void of_core_init(void);
152 static inline bool is_of_node(const struct fwnode_handle *fwnode)
154 return !IS_ERR_OR_NULL(fwnode) && fwnode->ops == &of_fwnode_ops;
157 #define to_of_node(__fwnode) \
159 typeof(__fwnode) __to_of_node_fwnode = (__fwnode); \
161 is_of_node(__to_of_node_fwnode) ? \
162 container_of(__to_of_node_fwnode, \
163 struct device_node, fwnode) : \
167 #define of_fwnode_handle(node) \
169 typeof(node) __of_fwnode_handle_node = (node); \
171 __of_fwnode_handle_node ? \
172 &__of_fwnode_handle_node->fwnode : NULL; \
175 static inline bool of_have_populated_dt(void)
177 return of_root != NULL;
180 static inline bool of_node_is_root(const struct device_node *node)
182 return node && (node->parent == NULL);
185 static inline int of_node_check_flag(struct device_node *n, unsigned long flag)
187 return test_bit(flag, &n->_flags);
190 static inline int of_node_test_and_set_flag(struct device_node *n,
193 return test_and_set_bit(flag, &n->_flags);
196 static inline void of_node_set_flag(struct device_node *n, unsigned long flag)
198 set_bit(flag, &n->_flags);
201 static inline void of_node_clear_flag(struct device_node *n, unsigned long flag)
203 clear_bit(flag, &n->_flags);
206 #if defined(CONFIG_OF_DYNAMIC) || defined(CONFIG_SPARC)
207 static inline int of_property_check_flag(struct property *p, unsigned long flag)
209 return test_bit(flag, &p->_flags);
212 static inline void of_property_set_flag(struct property *p, unsigned long flag)
214 set_bit(flag, &p->_flags);
217 static inline void of_property_clear_flag(struct property *p, unsigned long flag)
219 clear_bit(flag, &p->_flags);
223 extern struct device_node *__of_find_all_nodes(struct device_node *prev);
224 extern struct device_node *of_find_all_nodes(struct device_node *prev);
227 * OF address retrieval & translation
230 /* Helper to read a big number; size is in cells (not bytes) */
231 static inline u64 of_read_number(const __be32 *cell, int size)
235 r = (r << 32) | be32_to_cpu(*(cell++));
239 /* Like of_read_number, but we want an unsigned long result */
240 static inline unsigned long of_read_ulong(const __be32 *cell, int size)
242 /* toss away upper bits if unsigned long is smaller than u64 */
243 return of_read_number(cell, size);
246 #if defined(CONFIG_SPARC)
247 #include <asm/prom.h>
250 /* Default #address and #size cells. Allow arch asm/prom.h to override */
251 #if !defined(OF_ROOT_NODE_ADDR_CELLS_DEFAULT)
252 #define OF_ROOT_NODE_ADDR_CELLS_DEFAULT 1
253 #define OF_ROOT_NODE_SIZE_CELLS_DEFAULT 1
256 #define OF_IS_DYNAMIC(x) test_bit(OF_DYNAMIC, &x->_flags)
257 #define OF_MARK_DYNAMIC(x) set_bit(OF_DYNAMIC, &x->_flags)
259 static inline const char *of_node_full_name(const struct device_node *np)
261 return np ? np->full_name : "<no-node>";
264 #define for_each_of_allnodes_from(from, dn) \
265 for (dn = __of_find_all_nodes(from); dn; dn = __of_find_all_nodes(dn))
266 #define for_each_of_allnodes(dn) for_each_of_allnodes_from(NULL, dn)
267 extern struct device_node *of_find_node_by_name(struct device_node *from,
269 extern struct device_node *of_find_node_by_type(struct device_node *from,
271 extern struct device_node *of_find_compatible_node(struct device_node *from,
272 const char *type, const char *compat);
273 extern struct device_node *of_find_matching_node_and_match(
274 struct device_node *from,
275 const struct of_device_id *matches,
276 const struct of_device_id **match);
278 extern struct device_node *of_find_node_opts_by_path(const char *path,
280 static inline struct device_node *of_find_node_by_path(const char *path)
282 return of_find_node_opts_by_path(path, NULL);
285 extern struct device_node *of_find_node_by_phandle(phandle handle);
286 extern struct device_node *of_get_parent(const struct device_node *node);
287 extern struct device_node *of_get_next_parent(struct device_node *node);
288 extern struct device_node *of_get_next_child(const struct device_node *node,
289 struct device_node *prev);
290 extern struct device_node *of_get_next_available_child(
291 const struct device_node *node, struct device_node *prev);
293 extern struct device_node *of_get_child_by_name(const struct device_node *node,
297 extern struct device_node *of_find_next_cache_node(const struct device_node *);
298 extern int of_find_last_cache_level(unsigned int cpu);
299 extern struct device_node *of_find_node_with_property(
300 struct device_node *from, const char *prop_name);
302 extern struct property *of_find_property(const struct device_node *np,
305 extern int of_property_count_elems_of_size(const struct device_node *np,
306 const char *propname, int elem_size);
307 extern int of_property_read_u32_index(const struct device_node *np,
308 const char *propname,
309 u32 index, u32 *out_value);
310 extern int of_property_read_u64_index(const struct device_node *np,
311 const char *propname,
312 u32 index, u64 *out_value);
313 extern int of_property_read_variable_u8_array(const struct device_node *np,
314 const char *propname, u8 *out_values,
315 size_t sz_min, size_t sz_max);
316 extern int of_property_read_variable_u16_array(const struct device_node *np,
317 const char *propname, u16 *out_values,
318 size_t sz_min, size_t sz_max);
319 extern int of_property_read_variable_u32_array(const struct device_node *np,
320 const char *propname,
324 extern int of_property_read_u64(const struct device_node *np,
325 const char *propname, u64 *out_value);
326 extern int of_property_read_variable_u64_array(const struct device_node *np,
327 const char *propname,
332 extern int of_property_read_string(const struct device_node *np,
333 const char *propname,
334 const char **out_string);
335 extern int of_property_match_string(const struct device_node *np,
336 const char *propname,
338 extern int of_property_read_string_helper(const struct device_node *np,
339 const char *propname,
340 const char **out_strs, size_t sz, int index);
341 extern int of_device_is_compatible(const struct device_node *device,
343 extern int of_device_compatible_match(struct device_node *device,
344 const char *const *compat);
345 extern bool of_device_is_available(const struct device_node *device);
346 extern bool of_device_is_big_endian(const struct device_node *device);
347 extern const void *of_get_property(const struct device_node *node,
350 extern struct device_node *of_get_cpu_node(int cpu, unsigned int *thread);
351 #define for_each_property_of_node(dn, pp) \
352 for (pp = dn->properties; pp != NULL; pp = pp->next)
354 extern int of_n_addr_cells(struct device_node *np);
355 extern int of_n_size_cells(struct device_node *np);
356 extern const struct of_device_id *of_match_node(
357 const struct of_device_id *matches, const struct device_node *node);
358 extern int of_modalias_node(struct device_node *node, char *modalias, int len);
359 extern void of_print_phandle_args(const char *msg, const struct of_phandle_args *args);
360 extern struct device_node *of_parse_phandle(const struct device_node *np,
361 const char *phandle_name,
363 extern int of_parse_phandle_with_args(const struct device_node *np,
364 const char *list_name, const char *cells_name, int index,
365 struct of_phandle_args *out_args);
366 extern int of_parse_phandle_with_args_map(const struct device_node *np,
367 const char *list_name, const char *stem_name, int index,
368 struct of_phandle_args *out_args);
369 extern int of_parse_phandle_with_fixed_args(const struct device_node *np,
370 const char *list_name, int cells_count, int index,
371 struct of_phandle_args *out_args);
372 extern int of_count_phandle_with_args(const struct device_node *np,
373 const char *list_name, const char *cells_name);
375 /* phandle iterator functions */
376 extern int of_phandle_iterator_init(struct of_phandle_iterator *it,
377 const struct device_node *np,
378 const char *list_name,
379 const char *cells_name,
382 extern int of_phandle_iterator_next(struct of_phandle_iterator *it);
383 extern int of_phandle_iterator_args(struct of_phandle_iterator *it,
387 extern void of_alias_scan(void * (*dt_alloc)(u64 size, u64 align));
388 extern int of_alias_get_id(struct device_node *np, const char *stem);
389 extern int of_alias_get_highest_id(const char *stem);
391 extern int of_machine_is_compatible(const char *compat);
393 extern int of_add_property(struct device_node *np, struct property *prop);
394 extern int of_remove_property(struct device_node *np, struct property *prop);
395 extern int of_update_property(struct device_node *np, struct property *newprop);
397 /* For updating the device tree at runtime */
398 #define OF_RECONFIG_ATTACH_NODE 0x0001
399 #define OF_RECONFIG_DETACH_NODE 0x0002
400 #define OF_RECONFIG_ADD_PROPERTY 0x0003
401 #define OF_RECONFIG_REMOVE_PROPERTY 0x0004
402 #define OF_RECONFIG_UPDATE_PROPERTY 0x0005
404 extern int of_attach_node(struct device_node *);
405 extern int of_detach_node(struct device_node *);
407 #define of_match_ptr(_ptr) (_ptr)
410 * of_property_read_u8_array - Find and read an array of u8 from a property.
412 * @np: device node from which the property value is to be read.
413 * @propname: name of the property to be searched.
414 * @out_values: pointer to return value, modified only if return value is 0.
415 * @sz: number of array elements to read
417 * Search for a property in a device node and read 8-bit value(s) from
418 * it. Returns 0 on success, -EINVAL if the property does not exist,
419 * -ENODATA if property does not have a value, and -EOVERFLOW if the
420 * property data isn't large enough.
422 * dts entry of array should be like:
423 * property = /bits/ 8 <0x50 0x60 0x70>;
425 * The out_values is modified only if a valid u8 value can be decoded.
427 static inline int of_property_read_u8_array(const struct device_node *np,
428 const char *propname,
429 u8 *out_values, size_t sz)
431 int ret = of_property_read_variable_u8_array(np, propname, out_values,
440 * of_property_read_u16_array - Find and read an array of u16 from a property.
442 * @np: device node from which the property value is to be read.
443 * @propname: name of the property to be searched.
444 * @out_values: pointer to return value, modified only if return value is 0.
445 * @sz: number of array elements to read
447 * Search for a property in a device node and read 16-bit value(s) from
448 * it. Returns 0 on success, -EINVAL if the property does not exist,
449 * -ENODATA if property does not have a value, and -EOVERFLOW if the
450 * property data isn't large enough.
452 * dts entry of array should be like:
453 * property = /bits/ 16 <0x5000 0x6000 0x7000>;
455 * The out_values is modified only if a valid u16 value can be decoded.
457 static inline int of_property_read_u16_array(const struct device_node *np,
458 const char *propname,
459 u16 *out_values, size_t sz)
461 int ret = of_property_read_variable_u16_array(np, propname, out_values,
470 * of_property_read_u32_array - Find and read an array of 32 bit integers
473 * @np: device node from which the property value is to be read.
474 * @propname: name of the property to be searched.
475 * @out_values: pointer to return value, modified only if return value is 0.
476 * @sz: number of array elements to read
478 * Search for a property in a device node and read 32-bit value(s) from
479 * it. Returns 0 on success, -EINVAL if the property does not exist,
480 * -ENODATA if property does not have a value, and -EOVERFLOW if the
481 * property data isn't large enough.
483 * The out_values is modified only if a valid u32 value can be decoded.
485 static inline int of_property_read_u32_array(const struct device_node *np,
486 const char *propname,
487 u32 *out_values, size_t sz)
489 int ret = of_property_read_variable_u32_array(np, propname, out_values,
498 * of_property_read_u64_array - Find and read an array of 64 bit integers
501 * @np: device node from which the property value is to be read.
502 * @propname: name of the property to be searched.
503 * @out_values: pointer to return value, modified only if return value is 0.
504 * @sz: number of array elements to read
506 * Search for a property in a device node and read 64-bit value(s) from
507 * it. Returns 0 on success, -EINVAL if the property does not exist,
508 * -ENODATA if property does not have a value, and -EOVERFLOW if the
509 * property data isn't large enough.
511 * The out_values is modified only if a valid u64 value can be decoded.
513 static inline int of_property_read_u64_array(const struct device_node *np,
514 const char *propname,
515 u64 *out_values, size_t sz)
517 int ret = of_property_read_variable_u64_array(np, propname, out_values,
526 * struct property *prop;
530 * of_property_for_each_u32(np, "propname", prop, p, u)
531 * printk("U32 value: %x\n", u);
533 const __be32 *of_prop_next_u32(struct property *prop, const __be32 *cur,
536 * struct property *prop;
539 * of_property_for_each_string(np, "propname", prop, s)
540 * printk("String value: %s\n", s);
542 const char *of_prop_next_string(struct property *prop, const char *cur);
544 bool of_console_check(struct device_node *dn, char *name, int index);
546 extern int of_cpu_node_to_id(struct device_node *np);
548 #else /* CONFIG_OF */
550 static inline void of_core_init(void)
554 static inline bool is_of_node(const struct fwnode_handle *fwnode)
559 static inline struct device_node *to_of_node(const struct fwnode_handle *fwnode)
564 static inline const char* of_node_full_name(const struct device_node *np)
569 static inline struct device_node *of_find_node_by_name(struct device_node *from,
575 static inline struct device_node *of_find_node_by_type(struct device_node *from,
581 static inline struct device_node *of_find_matching_node_and_match(
582 struct device_node *from,
583 const struct of_device_id *matches,
584 const struct of_device_id **match)
589 static inline struct device_node *of_find_node_by_path(const char *path)
594 static inline struct device_node *of_find_node_opts_by_path(const char *path,
600 static inline struct device_node *of_find_node_by_phandle(phandle handle)
605 static inline struct device_node *of_get_parent(const struct device_node *node)
610 static inline struct device_node *of_get_next_child(
611 const struct device_node *node, struct device_node *prev)
616 static inline struct device_node *of_get_next_available_child(
617 const struct device_node *node, struct device_node *prev)
622 static inline struct device_node *of_find_node_with_property(
623 struct device_node *from, const char *prop_name)
628 #define of_fwnode_handle(node) NULL
630 static inline bool of_have_populated_dt(void)
635 static inline struct device_node *of_get_child_by_name(
636 const struct device_node *node,
642 static inline int of_device_is_compatible(const struct device_node *device,
648 static inline int of_device_compatible_match(struct device_node *device,
649 const char *const *compat)
654 static inline bool of_device_is_available(const struct device_node *device)
659 static inline bool of_device_is_big_endian(const struct device_node *device)
664 static inline struct property *of_find_property(const struct device_node *np,
671 static inline struct device_node *of_find_compatible_node(
672 struct device_node *from,
679 static inline int of_property_count_elems_of_size(const struct device_node *np,
680 const char *propname, int elem_size)
685 static inline int of_property_read_u8_array(const struct device_node *np,
686 const char *propname, u8 *out_values, size_t sz)
691 static inline int of_property_read_u16_array(const struct device_node *np,
692 const char *propname, u16 *out_values, size_t sz)
697 static inline int of_property_read_u32_array(const struct device_node *np,
698 const char *propname,
699 u32 *out_values, size_t sz)
704 static inline int of_property_read_u64_array(const struct device_node *np,
705 const char *propname,
706 u64 *out_values, size_t sz)
711 static inline int of_property_read_u32_index(const struct device_node *np,
712 const char *propname, u32 index, u32 *out_value)
717 static inline int of_property_read_u64_index(const struct device_node *np,
718 const char *propname, u32 index, u64 *out_value)
723 static inline const void *of_get_property(const struct device_node *node,
730 static inline struct device_node *of_get_cpu_node(int cpu,
731 unsigned int *thread)
736 static inline int of_n_addr_cells(struct device_node *np)
741 static inline int of_n_size_cells(struct device_node *np)
746 static inline int of_property_read_variable_u8_array(const struct device_node *np,
747 const char *propname, u8 *out_values,
748 size_t sz_min, size_t sz_max)
753 static inline int of_property_read_variable_u16_array(const struct device_node *np,
754 const char *propname, u16 *out_values,
755 size_t sz_min, size_t sz_max)
760 static inline int of_property_read_variable_u32_array(const struct device_node *np,
761 const char *propname,
769 static inline int of_property_read_u64(const struct device_node *np,
770 const char *propname, u64 *out_value)
775 static inline int of_property_read_variable_u64_array(const struct device_node *np,
776 const char *propname,
784 static inline int of_property_read_string(const struct device_node *np,
785 const char *propname,
786 const char **out_string)
791 static inline int of_property_match_string(const struct device_node *np,
792 const char *propname,
798 static inline int of_property_read_string_helper(const struct device_node *np,
799 const char *propname,
800 const char **out_strs, size_t sz, int index)
805 static inline struct device_node *of_parse_phandle(const struct device_node *np,
806 const char *phandle_name,
812 static inline int of_parse_phandle_with_args(const struct device_node *np,
813 const char *list_name,
814 const char *cells_name,
816 struct of_phandle_args *out_args)
821 static inline int of_parse_phandle_with_args_map(const struct device_node *np,
822 const char *list_name,
823 const char *stem_name,
825 struct of_phandle_args *out_args)
830 static inline int of_parse_phandle_with_fixed_args(const struct device_node *np,
831 const char *list_name, int cells_count, int index,
832 struct of_phandle_args *out_args)
837 static inline int of_count_phandle_with_args(struct device_node *np,
838 const char *list_name,
839 const char *cells_name)
844 static inline int of_phandle_iterator_init(struct of_phandle_iterator *it,
845 const struct device_node *np,
846 const char *list_name,
847 const char *cells_name,
853 static inline int of_phandle_iterator_next(struct of_phandle_iterator *it)
858 static inline int of_phandle_iterator_args(struct of_phandle_iterator *it,
865 static inline int of_alias_get_id(struct device_node *np, const char *stem)
870 static inline int of_alias_get_highest_id(const char *stem)
875 static inline int of_machine_is_compatible(const char *compat)
880 static inline bool of_console_check(const struct device_node *dn, const char *name, int index)
885 static inline const __be32 *of_prop_next_u32(struct property *prop,
886 const __be32 *cur, u32 *pu)
891 static inline const char *of_prop_next_string(struct property *prop,
897 static inline int of_node_check_flag(struct device_node *n, unsigned long flag)
902 static inline int of_node_test_and_set_flag(struct device_node *n,
908 static inline void of_node_set_flag(struct device_node *n, unsigned long flag)
912 static inline void of_node_clear_flag(struct device_node *n, unsigned long flag)
916 static inline int of_property_check_flag(struct property *p, unsigned long flag)
921 static inline void of_property_set_flag(struct property *p, unsigned long flag)
925 static inline void of_property_clear_flag(struct property *p, unsigned long flag)
929 static inline int of_cpu_node_to_id(struct device_node *np)
934 #define of_match_ptr(_ptr) NULL
935 #define of_match_node(_matches, _node) NULL
936 #endif /* CONFIG_OF */
938 /* Default string compare functions, Allow arch asm/prom.h to override */
939 #if !defined(of_compat_cmp)
940 #define of_compat_cmp(s1, s2, l) strcasecmp((s1), (s2))
941 #define of_prop_cmp(s1, s2) strcmp((s1), (s2))
942 #define of_node_cmp(s1, s2) strcasecmp((s1), (s2))
945 #if defined(CONFIG_OF) && defined(CONFIG_NUMA)
946 extern int of_node_to_nid(struct device_node *np);
948 static inline int of_node_to_nid(struct device_node *device)
954 #ifdef CONFIG_OF_NUMA
955 extern int of_numa_init(void);
957 static inline int of_numa_init(void)
963 static inline struct device_node *of_find_matching_node(
964 struct device_node *from,
965 const struct of_device_id *matches)
967 return of_find_matching_node_and_match(from, matches, NULL);
971 * of_property_count_u8_elems - Count the number of u8 elements in a property
973 * @np: device node from which the property value is to be read.
974 * @propname: name of the property to be searched.
976 * Search for a property in a device node and count the number of u8 elements
977 * in it. Returns number of elements on sucess, -EINVAL if the property does
978 * not exist or its length does not match a multiple of u8 and -ENODATA if the
979 * property does not have a value.
981 static inline int of_property_count_u8_elems(const struct device_node *np,
982 const char *propname)
984 return of_property_count_elems_of_size(np, propname, sizeof(u8));
988 * of_property_count_u16_elems - Count the number of u16 elements in a property
990 * @np: device node from which the property value is to be read.
991 * @propname: name of the property to be searched.
993 * Search for a property in a device node and count the number of u16 elements
994 * in it. Returns number of elements on sucess, -EINVAL if the property does
995 * not exist or its length does not match a multiple of u16 and -ENODATA if the
996 * property does not have a value.
998 static inline int of_property_count_u16_elems(const struct device_node *np,
999 const char *propname)
1001 return of_property_count_elems_of_size(np, propname, sizeof(u16));
1005 * of_property_count_u32_elems - Count the number of u32 elements in a property
1007 * @np: device node from which the property value is to be read.
1008 * @propname: name of the property to be searched.
1010 * Search for a property in a device node and count the number of u32 elements
1011 * in it. Returns number of elements on sucess, -EINVAL if the property does
1012 * not exist or its length does not match a multiple of u32 and -ENODATA if the
1013 * property does not have a value.
1015 static inline int of_property_count_u32_elems(const struct device_node *np,
1016 const char *propname)
1018 return of_property_count_elems_of_size(np, propname, sizeof(u32));
1022 * of_property_count_u64_elems - Count the number of u64 elements in a property
1024 * @np: device node from which the property value is to be read.
1025 * @propname: name of the property to be searched.
1027 * Search for a property in a device node and count the number of u64 elements
1028 * in it. Returns number of elements on sucess, -EINVAL if the property does
1029 * not exist or its length does not match a multiple of u64 and -ENODATA if the
1030 * property does not have a value.
1032 static inline int of_property_count_u64_elems(const struct device_node *np,
1033 const char *propname)
1035 return of_property_count_elems_of_size(np, propname, sizeof(u64));
1039 * of_property_read_string_array() - Read an array of strings from a multiple
1041 * @np: device node from which the property value is to be read.
1042 * @propname: name of the property to be searched.
1043 * @out_strs: output array of string pointers.
1044 * @sz: number of array elements to read.
1046 * Search for a property in a device tree node and retrieve a list of
1047 * terminated string values (pointer to data, not a copy) in that property.
1049 * If @out_strs is NULL, the number of strings in the property is returned.
1051 static inline int of_property_read_string_array(const struct device_node *np,
1052 const char *propname, const char **out_strs,
1055 return of_property_read_string_helper(np, propname, out_strs, sz, 0);
1059 * of_property_count_strings() - Find and return the number of strings from a
1060 * multiple strings property.
1061 * @np: device node from which the property value is to be read.
1062 * @propname: name of the property to be searched.
1064 * Search for a property in a device tree node and retrieve the number of null
1065 * terminated string contain in it. Returns the number of strings on
1066 * success, -EINVAL if the property does not exist, -ENODATA if property
1067 * does not have a value, and -EILSEQ if the string is not null-terminated
1068 * within the length of the property data.
1070 static inline int of_property_count_strings(const struct device_node *np,
1071 const char *propname)
1073 return of_property_read_string_helper(np, propname, NULL, 0, 0);
1077 * of_property_read_string_index() - Find and read a string from a multiple
1079 * @np: device node from which the property value is to be read.
1080 * @propname: name of the property to be searched.
1081 * @index: index of the string in the list of strings
1082 * @out_string: pointer to null terminated return string, modified only if
1083 * return value is 0.
1085 * Search for a property in a device tree node and retrieve a null
1086 * terminated string value (pointer to data, not a copy) in the list of strings
1087 * contained in that property.
1088 * Returns 0 on success, -EINVAL if the property does not exist, -ENODATA if
1089 * property does not have a value, and -EILSEQ if the string is not
1090 * null-terminated within the length of the property data.
1092 * The out_string pointer is modified only if a valid string can be decoded.
1094 static inline int of_property_read_string_index(const struct device_node *np,
1095 const char *propname,
1096 int index, const char **output)
1098 int rc = of_property_read_string_helper(np, propname, output, 1, index);
1099 return rc < 0 ? rc : 0;
1103 * of_property_read_bool - Findfrom a property
1104 * @np: device node from which the property value is to be read.
1105 * @propname: name of the property to be searched.
1107 * Search for a property in a device node.
1108 * Returns true if the property exists false otherwise.
1110 static inline bool of_property_read_bool(const struct device_node *np,
1111 const char *propname)
1113 struct property *prop = of_find_property(np, propname, NULL);
1115 return prop ? true : false;
1118 static inline int of_property_read_u8(const struct device_node *np,
1119 const char *propname,
1122 return of_property_read_u8_array(np, propname, out_value, 1);
1125 static inline int of_property_read_u16(const struct device_node *np,
1126 const char *propname,
1129 return of_property_read_u16_array(np, propname, out_value, 1);
1132 static inline int of_property_read_u32(const struct device_node *np,
1133 const char *propname,
1136 return of_property_read_u32_array(np, propname, out_value, 1);
1139 static inline int of_property_read_s32(const struct device_node *np,
1140 const char *propname,
1143 return of_property_read_u32(np, propname, (u32*) out_value);
1146 #define of_for_each_phandle(it, err, np, ln, cn, cc) \
1147 for (of_phandle_iterator_init((it), (np), (ln), (cn), (cc)), \
1148 err = of_phandle_iterator_next(it); \
1150 err = of_phandle_iterator_next(it))
1152 #define of_property_for_each_u32(np, propname, prop, p, u) \
1153 for (prop = of_find_property(np, propname, NULL), \
1154 p = of_prop_next_u32(prop, NULL, &u); \
1156 p = of_prop_next_u32(prop, p, &u))
1158 #define of_property_for_each_string(np, propname, prop, s) \
1159 for (prop = of_find_property(np, propname, NULL), \
1160 s = of_prop_next_string(prop, NULL); \
1162 s = of_prop_next_string(prop, s))
1164 #define for_each_node_by_name(dn, name) \
1165 for (dn = of_find_node_by_name(NULL, name); dn; \
1166 dn = of_find_node_by_name(dn, name))
1167 #define for_each_node_by_type(dn, type) \
1168 for (dn = of_find_node_by_type(NULL, type); dn; \
1169 dn = of_find_node_by_type(dn, type))
1170 #define for_each_compatible_node(dn, type, compatible) \
1171 for (dn = of_find_compatible_node(NULL, type, compatible); dn; \
1172 dn = of_find_compatible_node(dn, type, compatible))
1173 #define for_each_matching_node(dn, matches) \
1174 for (dn = of_find_matching_node(NULL, matches); dn; \
1175 dn = of_find_matching_node(dn, matches))
1176 #define for_each_matching_node_and_match(dn, matches, match) \
1177 for (dn = of_find_matching_node_and_match(NULL, matches, match); \
1178 dn; dn = of_find_matching_node_and_match(dn, matches, match))
1180 #define for_each_child_of_node(parent, child) \
1181 for (child = of_get_next_child(parent, NULL); child != NULL; \
1182 child = of_get_next_child(parent, child))
1183 #define for_each_available_child_of_node(parent, child) \
1184 for (child = of_get_next_available_child(parent, NULL); child != NULL; \
1185 child = of_get_next_available_child(parent, child))
1187 #define for_each_node_with_property(dn, prop_name) \
1188 for (dn = of_find_node_with_property(NULL, prop_name); dn; \
1189 dn = of_find_node_with_property(dn, prop_name))
1191 static inline int of_get_child_count(const struct device_node *np)
1193 struct device_node *child;
1196 for_each_child_of_node(np, child)
1202 static inline int of_get_available_child_count(const struct device_node *np)
1204 struct device_node *child;
1207 for_each_available_child_of_node(np, child)
1213 #if defined(CONFIG_OF) && !defined(MODULE)
1214 #define _OF_DECLARE(table, name, compat, fn, fn_type) \
1215 static const struct of_device_id __of_table_##name \
1216 __used __section(__##table##_of_table) \
1217 = { .compatible = compat, \
1218 .data = (fn == (fn_type)NULL) ? fn : fn }
1220 #define _OF_DECLARE(table, name, compat, fn, fn_type) \
1221 static const struct of_device_id __of_table_##name \
1222 __attribute__((unused)) \
1223 = { .compatible = compat, \
1224 .data = (fn == (fn_type)NULL) ? fn : fn }
1227 typedef int (*of_init_fn_2)(struct device_node *, struct device_node *);
1228 typedef int (*of_init_fn_1_ret)(struct device_node *);
1229 typedef void (*of_init_fn_1)(struct device_node *);
1231 #define OF_DECLARE_1(table, name, compat, fn) \
1232 _OF_DECLARE(table, name, compat, fn, of_init_fn_1)
1233 #define OF_DECLARE_1_RET(table, name, compat, fn) \
1234 _OF_DECLARE(table, name, compat, fn, of_init_fn_1_ret)
1235 #define OF_DECLARE_2(table, name, compat, fn) \
1236 _OF_DECLARE(table, name, compat, fn, of_init_fn_2)
1239 * struct of_changeset_entry - Holds a changeset entry
1241 * @node: list_head for the log list
1242 * @action: notifier action
1243 * @np: pointer to the device node affected
1244 * @prop: pointer to the property affected
1245 * @old_prop: hold a pointer to the original property
1247 * Every modification of the device tree during a changeset
1248 * is held in a list of of_changeset_entry structures.
1249 * That way we can recover from a partial application, or we can
1250 * revert the changeset
1252 struct of_changeset_entry {
1253 struct list_head node;
1254 unsigned long action;
1255 struct device_node *np;
1256 struct property *prop;
1257 struct property *old_prop;
1261 * struct of_changeset - changeset tracker structure
1263 * @entries: list_head for the changeset entries
1265 * changesets are a convenient way to apply bulk changes to the
1266 * live tree. In case of an error, changes are rolled-back.
1267 * changesets live on after initial application, and if not
1268 * destroyed after use, they can be reverted in one single call.
1270 struct of_changeset {
1271 struct list_head entries;
1274 enum of_reconfig_change {
1275 OF_RECONFIG_NO_CHANGE = 0,
1276 OF_RECONFIG_CHANGE_ADD,
1277 OF_RECONFIG_CHANGE_REMOVE,
1280 #ifdef CONFIG_OF_DYNAMIC
1281 extern int of_reconfig_notifier_register(struct notifier_block *);
1282 extern int of_reconfig_notifier_unregister(struct notifier_block *);
1283 extern int of_reconfig_notify(unsigned long, struct of_reconfig_data *rd);
1284 extern int of_reconfig_get_state_change(unsigned long action,
1285 struct of_reconfig_data *arg);
1287 extern void of_changeset_init(struct of_changeset *ocs);
1288 extern void of_changeset_destroy(struct of_changeset *ocs);
1289 extern int of_changeset_apply(struct of_changeset *ocs);
1290 extern int of_changeset_revert(struct of_changeset *ocs);
1291 extern int of_changeset_action(struct of_changeset *ocs,
1292 unsigned long action, struct device_node *np,
1293 struct property *prop);
1295 static inline int of_changeset_attach_node(struct of_changeset *ocs,
1296 struct device_node *np)
1298 return of_changeset_action(ocs, OF_RECONFIG_ATTACH_NODE, np, NULL);
1301 static inline int of_changeset_detach_node(struct of_changeset *ocs,
1302 struct device_node *np)
1304 return of_changeset_action(ocs, OF_RECONFIG_DETACH_NODE, np, NULL);
1307 static inline int of_changeset_add_property(struct of_changeset *ocs,
1308 struct device_node *np, struct property *prop)
1310 return of_changeset_action(ocs, OF_RECONFIG_ADD_PROPERTY, np, prop);
1313 static inline int of_changeset_remove_property(struct of_changeset *ocs,
1314 struct device_node *np, struct property *prop)
1316 return of_changeset_action(ocs, OF_RECONFIG_REMOVE_PROPERTY, np, prop);
1319 static inline int of_changeset_update_property(struct of_changeset *ocs,
1320 struct device_node *np, struct property *prop)
1322 return of_changeset_action(ocs, OF_RECONFIG_UPDATE_PROPERTY, np, prop);
1324 #else /* CONFIG_OF_DYNAMIC */
1325 static inline int of_reconfig_notifier_register(struct notifier_block *nb)
1329 static inline int of_reconfig_notifier_unregister(struct notifier_block *nb)
1333 static inline int of_reconfig_notify(unsigned long action,
1334 struct of_reconfig_data *arg)
1338 static inline int of_reconfig_get_state_change(unsigned long action,
1339 struct of_reconfig_data *arg)
1343 #endif /* CONFIG_OF_DYNAMIC */
1346 * of_device_is_system_power_controller - Tells if system-power-controller is found for device_node
1347 * @np: Pointer to the given device_node
1349 * return true if present false otherwise
1351 static inline bool of_device_is_system_power_controller(const struct device_node *np)
1353 return of_property_read_bool(np, "system-power-controller");
1360 enum of_overlay_notify_action {
1361 OF_OVERLAY_PRE_APPLY = 0,
1362 OF_OVERLAY_POST_APPLY,
1363 OF_OVERLAY_PRE_REMOVE,
1364 OF_OVERLAY_POST_REMOVE,
1367 struct of_overlay_notify_data {
1368 struct device_node *overlay;
1369 struct device_node *target;
1372 #ifdef CONFIG_OF_OVERLAY
1374 int of_overlay_fdt_apply(const void *overlay_fdt, u32 overlay_fdt_size,
1376 int of_overlay_remove(int *ovcs_id);
1377 int of_overlay_remove_all(void);
1379 int of_overlay_notifier_register(struct notifier_block *nb);
1380 int of_overlay_notifier_unregister(struct notifier_block *nb);
1384 static inline int of_overlay_fdt_apply(void *overlay_fdt, int *ovcs_id)
1389 static inline int of_overlay_remove(int *ovcs_id)
1394 static inline int of_overlay_remove_all(void)
1399 static inline int of_overlay_notifier_register(struct notifier_block *nb)
1404 static inline int of_overlay_notifier_unregister(struct notifier_block *nb)
1411 #endif /* _LINUX_OF_H */