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;
142 * struct device_node flag descriptions
143 * (need to be visible even when !CONFIG_OF)
145 #define OF_DYNAMIC 1 /* (and properties) allocated via kmalloc */
146 #define OF_DETACHED 2 /* detached from the device tree */
147 #define OF_POPULATED 3 /* device already created */
148 #define OF_POPULATED_BUS 4 /* platform bus created for children */
149 #define OF_OVERLAY 5 /* allocated for an overlay */
150 #define OF_OVERLAY_FREE_CSET 6 /* in overlay cset being freed */
152 #define OF_BAD_ADDR ((u64)-1)
155 void of_core_init(void);
157 static inline bool is_of_node(const struct fwnode_handle *fwnode)
159 return !IS_ERR_OR_NULL(fwnode) && fwnode->ops == &of_fwnode_ops;
162 #define to_of_node(__fwnode) \
164 typeof(__fwnode) __to_of_node_fwnode = (__fwnode); \
166 is_of_node(__to_of_node_fwnode) ? \
167 container_of(__to_of_node_fwnode, \
168 struct device_node, fwnode) : \
172 #define of_fwnode_handle(node) \
174 typeof(node) __of_fwnode_handle_node = (node); \
176 __of_fwnode_handle_node ? \
177 &__of_fwnode_handle_node->fwnode : NULL; \
180 static inline bool of_have_populated_dt(void)
182 return of_root != NULL;
185 static inline bool of_node_is_root(const struct device_node *node)
187 return node && (node->parent == NULL);
190 static inline int of_node_check_flag(struct device_node *n, unsigned long flag)
192 return test_bit(flag, &n->_flags);
195 static inline int of_node_test_and_set_flag(struct device_node *n,
198 return test_and_set_bit(flag, &n->_flags);
201 static inline void of_node_set_flag(struct device_node *n, unsigned long flag)
203 set_bit(flag, &n->_flags);
206 static inline void of_node_clear_flag(struct device_node *n, unsigned long flag)
208 clear_bit(flag, &n->_flags);
211 #if defined(CONFIG_OF_DYNAMIC) || defined(CONFIG_SPARC)
212 static inline int of_property_check_flag(struct property *p, unsigned long flag)
214 return test_bit(flag, &p->_flags);
217 static inline void of_property_set_flag(struct property *p, unsigned long flag)
219 set_bit(flag, &p->_flags);
222 static inline void of_property_clear_flag(struct property *p, unsigned long flag)
224 clear_bit(flag, &p->_flags);
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);
232 * OF address retrieval & translation
235 /* Helper to read a big number; size is in cells (not bytes) */
236 static inline u64 of_read_number(const __be32 *cell, int size)
239 for (; size--; cell++)
240 r = (r << 32) | be32_to_cpu(*cell);
244 /* Like of_read_number, but we want an unsigned long result */
245 static inline unsigned long of_read_ulong(const __be32 *cell, int size)
247 /* toss away upper bits if unsigned long is smaller than u64 */
248 return of_read_number(cell, size);
251 #if defined(CONFIG_SPARC)
252 #include <asm/prom.h>
255 /* Default #address and #size cells. Allow arch asm/prom.h to override */
256 #if !defined(OF_ROOT_NODE_ADDR_CELLS_DEFAULT)
257 #define OF_ROOT_NODE_ADDR_CELLS_DEFAULT 1
258 #define OF_ROOT_NODE_SIZE_CELLS_DEFAULT 1
261 #define OF_IS_DYNAMIC(x) test_bit(OF_DYNAMIC, &x->_flags)
262 #define OF_MARK_DYNAMIC(x) set_bit(OF_DYNAMIC, &x->_flags)
264 extern bool of_node_name_eq(const struct device_node *np, const char *name);
265 extern bool of_node_name_prefix(const struct device_node *np, const char *prefix);
267 static inline const char *of_node_full_name(const struct device_node *np)
269 return np ? np->full_name : "<no-node>";
272 #define for_each_of_allnodes_from(from, dn) \
273 for (dn = __of_find_all_nodes(from); dn; dn = __of_find_all_nodes(dn))
274 #define for_each_of_allnodes(dn) for_each_of_allnodes_from(NULL, dn)
275 extern struct device_node *of_find_node_by_name(struct device_node *from,
277 extern struct device_node *of_find_node_by_type(struct device_node *from,
279 extern struct device_node *of_find_compatible_node(struct device_node *from,
280 const char *type, const char *compat);
281 extern struct device_node *of_find_matching_node_and_match(
282 struct device_node *from,
283 const struct of_device_id *matches,
284 const struct of_device_id **match);
286 extern struct device_node *of_find_node_opts_by_path(const char *path,
288 static inline struct device_node *of_find_node_by_path(const char *path)
290 return of_find_node_opts_by_path(path, NULL);
293 extern struct device_node *of_find_node_by_phandle(phandle handle);
294 extern struct device_node *of_get_parent(const struct device_node *node);
295 extern struct device_node *of_get_next_parent(struct device_node *node);
296 extern struct device_node *of_get_next_child(const struct device_node *node,
297 struct device_node *prev);
298 extern struct device_node *of_get_next_available_child(
299 const struct device_node *node, struct device_node *prev);
301 extern struct device_node *of_get_compatible_child(const struct device_node *parent,
302 const char *compatible);
303 extern struct device_node *of_get_child_by_name(const struct device_node *node,
307 extern struct device_node *of_find_next_cache_node(const struct device_node *);
308 extern int of_find_last_cache_level(unsigned int cpu);
309 extern struct device_node *of_find_node_with_property(
310 struct device_node *from, const char *prop_name);
312 extern struct property *of_find_property(const struct device_node *np,
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,
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,
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,
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,
353 extern int of_device_compatible_match(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,
360 extern struct device_node *of_get_cpu_node(int cpu, unsigned int *thread);
361 #define for_each_property_of_node(dn, pp) \
362 for (pp = dn->properties; pp != NULL; pp = pp->next)
364 extern int of_n_addr_cells(struct device_node *np);
365 extern int of_n_size_cells(struct device_node *np);
366 extern const struct of_device_id *of_match_node(
367 const struct of_device_id *matches, const struct device_node *node);
368 extern int of_modalias_node(struct device_node *node, char *modalias, int len);
369 extern void of_print_phandle_args(const char *msg, const struct of_phandle_args *args);
370 extern struct device_node *of_parse_phandle(const struct device_node *np,
371 const char *phandle_name,
373 extern int of_parse_phandle_with_args(const struct device_node *np,
374 const char *list_name, const char *cells_name, int index,
375 struct of_phandle_args *out_args);
376 extern int of_parse_phandle_with_args_map(const struct device_node *np,
377 const char *list_name, const char *stem_name, int index,
378 struct of_phandle_args *out_args);
379 extern int of_parse_phandle_with_fixed_args(const struct device_node *np,
380 const char *list_name, int cells_count, 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);
385 /* phandle iterator functions */
386 extern int of_phandle_iterator_init(struct of_phandle_iterator *it,
387 const struct device_node *np,
388 const char *list_name,
389 const char *cells_name,
392 extern int of_phandle_iterator_next(struct of_phandle_iterator *it);
393 extern int of_phandle_iterator_args(struct of_phandle_iterator *it,
397 extern void of_alias_scan(void * (*dt_alloc)(u64 size, u64 align));
398 extern int of_alias_get_id(struct device_node *np, const char *stem);
399 extern int of_alias_get_highest_id(const char *stem);
401 extern int of_machine_is_compatible(const char *compat);
403 extern int of_add_property(struct device_node *np, struct property *prop);
404 extern int of_remove_property(struct device_node *np, struct property *prop);
405 extern int of_update_property(struct device_node *np, struct property *newprop);
407 /* For updating the device tree at runtime */
408 #define OF_RECONFIG_ATTACH_NODE 0x0001
409 #define OF_RECONFIG_DETACH_NODE 0x0002
410 #define OF_RECONFIG_ADD_PROPERTY 0x0003
411 #define OF_RECONFIG_REMOVE_PROPERTY 0x0004
412 #define OF_RECONFIG_UPDATE_PROPERTY 0x0005
414 extern int of_attach_node(struct device_node *);
415 extern int of_detach_node(struct device_node *);
417 #define of_match_ptr(_ptr) (_ptr)
420 * of_property_read_u8_array - Find and read an array of u8 from a property.
422 * @np: device node from which the property value is to be read.
423 * @propname: name of the property to be searched.
424 * @out_values: pointer to return value, modified only if return value is 0.
425 * @sz: number of array elements to read
427 * Search for a property in a device node and read 8-bit value(s) from
428 * it. Returns 0 on success, -EINVAL if the property does not exist,
429 * -ENODATA if property does not have a value, and -EOVERFLOW if the
430 * property data isn't large enough.
432 * dts entry of array should be like:
433 * property = /bits/ 8 <0x50 0x60 0x70>;
435 * The out_values is modified only if a valid u8 value can be decoded.
437 static inline int of_property_read_u8_array(const struct device_node *np,
438 const char *propname,
439 u8 *out_values, size_t sz)
441 int ret = of_property_read_variable_u8_array(np, propname, out_values,
450 * of_property_read_u16_array - Find and read an array of u16 from a property.
452 * @np: device node from which the property value is to be read.
453 * @propname: name of the property to be searched.
454 * @out_values: pointer to return value, modified only if return value is 0.
455 * @sz: number of array elements to read
457 * Search for a property in a device node and read 16-bit value(s) from
458 * it. Returns 0 on success, -EINVAL if the property does not exist,
459 * -ENODATA if property does not have a value, and -EOVERFLOW if the
460 * property data isn't large enough.
462 * dts entry of array should be like:
463 * property = /bits/ 16 <0x5000 0x6000 0x7000>;
465 * The out_values is modified only if a valid u16 value can be decoded.
467 static inline int of_property_read_u16_array(const struct device_node *np,
468 const char *propname,
469 u16 *out_values, size_t sz)
471 int ret = of_property_read_variable_u16_array(np, propname, out_values,
480 * of_property_read_u32_array - Find and read an array of 32 bit integers
483 * @np: device node from which the property value is to be read.
484 * @propname: name of the property to be searched.
485 * @out_values: pointer to return value, modified only if return value is 0.
486 * @sz: number of array elements to read
488 * Search for a property in a device node and read 32-bit value(s) from
489 * it. Returns 0 on success, -EINVAL if the property does not exist,
490 * -ENODATA if property does not have a value, and -EOVERFLOW if the
491 * property data isn't large enough.
493 * The out_values is modified only if a valid u32 value can be decoded.
495 static inline int of_property_read_u32_array(const struct device_node *np,
496 const char *propname,
497 u32 *out_values, size_t sz)
499 int ret = of_property_read_variable_u32_array(np, propname, out_values,
508 * of_property_read_u64_array - Find and read an array of 64 bit integers
511 * @np: device node from which the property value is to be read.
512 * @propname: name of the property to be searched.
513 * @out_values: pointer to return value, modified only if return value is 0.
514 * @sz: number of array elements to read
516 * Search for a property in a device node and read 64-bit value(s) from
517 * it. Returns 0 on success, -EINVAL if the property does not exist,
518 * -ENODATA if property does not have a value, and -EOVERFLOW if the
519 * property data isn't large enough.
521 * The out_values is modified only if a valid u64 value can be decoded.
523 static inline int of_property_read_u64_array(const struct device_node *np,
524 const char *propname,
525 u64 *out_values, size_t sz)
527 int ret = of_property_read_variable_u64_array(np, propname, out_values,
536 * struct property *prop;
540 * of_property_for_each_u32(np, "propname", prop, p, u)
541 * printk("U32 value: %x\n", u);
543 const __be32 *of_prop_next_u32(struct property *prop, const __be32 *cur,
546 * struct property *prop;
549 * of_property_for_each_string(np, "propname", prop, s)
550 * printk("String value: %s\n", s);
552 const char *of_prop_next_string(struct property *prop, const char *cur);
554 bool of_console_check(struct device_node *dn, char *name, int index);
556 extern int of_cpu_node_to_id(struct device_node *np);
558 #else /* CONFIG_OF */
560 static inline void of_core_init(void)
564 static inline bool is_of_node(const struct fwnode_handle *fwnode)
569 static inline struct device_node *to_of_node(const struct fwnode_handle *fwnode)
574 static inline bool of_node_name_eq(const struct device_node *np, const char *name)
579 static inline bool of_node_name_prefix(const struct device_node *np, const char *prefix)
584 static inline const char* of_node_full_name(const struct device_node *np)
589 static inline struct device_node *of_find_node_by_name(struct device_node *from,
595 static inline struct device_node *of_find_node_by_type(struct device_node *from,
601 static inline struct device_node *of_find_matching_node_and_match(
602 struct device_node *from,
603 const struct of_device_id *matches,
604 const struct of_device_id **match)
609 static inline struct device_node *of_find_node_by_path(const char *path)
614 static inline struct device_node *of_find_node_opts_by_path(const char *path,
620 static inline struct device_node *of_find_node_by_phandle(phandle handle)
625 static inline struct device_node *of_get_parent(const struct device_node *node)
630 static inline struct device_node *of_get_next_child(
631 const struct device_node *node, struct device_node *prev)
636 static inline struct device_node *of_get_next_available_child(
637 const struct device_node *node, struct device_node *prev)
642 static inline struct device_node *of_find_node_with_property(
643 struct device_node *from, const char *prop_name)
648 #define of_fwnode_handle(node) NULL
650 static inline bool of_have_populated_dt(void)
655 static inline struct device_node *of_get_compatible_child(const struct device_node *parent,
656 const char *compatible)
661 static inline struct device_node *of_get_child_by_name(
662 const struct device_node *node,
668 static inline int of_device_is_compatible(const struct device_node *device,
674 static inline int of_device_compatible_match(struct device_node *device,
675 const char *const *compat)
680 static inline bool of_device_is_available(const struct device_node *device)
685 static inline bool of_device_is_big_endian(const struct device_node *device)
690 static inline struct property *of_find_property(const struct device_node *np,
697 static inline struct device_node *of_find_compatible_node(
698 struct device_node *from,
705 static inline int of_property_count_elems_of_size(const struct device_node *np,
706 const char *propname, int elem_size)
711 static inline int of_property_read_u8_array(const struct device_node *np,
712 const char *propname, u8 *out_values, size_t sz)
717 static inline int of_property_read_u16_array(const struct device_node *np,
718 const char *propname, u16 *out_values, size_t sz)
723 static inline int of_property_read_u32_array(const struct device_node *np,
724 const char *propname,
725 u32 *out_values, size_t sz)
730 static inline int of_property_read_u64_array(const struct device_node *np,
731 const char *propname,
732 u64 *out_values, size_t sz)
737 static inline int of_property_read_u32_index(const struct device_node *np,
738 const char *propname, u32 index, u32 *out_value)
743 static inline int of_property_read_u64_index(const struct device_node *np,
744 const char *propname, u32 index, u64 *out_value)
749 static inline const void *of_get_property(const struct device_node *node,
756 static inline struct device_node *of_get_cpu_node(int cpu,
757 unsigned int *thread)
762 static inline int of_n_addr_cells(struct device_node *np)
767 static inline int of_n_size_cells(struct device_node *np)
772 static inline int of_property_read_variable_u8_array(const struct device_node *np,
773 const char *propname, u8 *out_values,
774 size_t sz_min, size_t sz_max)
779 static inline int of_property_read_variable_u16_array(const struct device_node *np,
780 const char *propname, u16 *out_values,
781 size_t sz_min, size_t sz_max)
786 static inline int of_property_read_variable_u32_array(const struct device_node *np,
787 const char *propname,
795 static inline int of_property_read_u64(const struct device_node *np,
796 const char *propname, u64 *out_value)
801 static inline int of_property_read_variable_u64_array(const struct device_node *np,
802 const char *propname,
810 static inline int of_property_read_string(const struct device_node *np,
811 const char *propname,
812 const char **out_string)
817 static inline int of_property_match_string(const struct device_node *np,
818 const char *propname,
824 static inline int of_property_read_string_helper(const struct device_node *np,
825 const char *propname,
826 const char **out_strs, size_t sz, int index)
831 static inline struct device_node *of_parse_phandle(const struct device_node *np,
832 const char *phandle_name,
838 static inline int of_parse_phandle_with_args(const struct device_node *np,
839 const char *list_name,
840 const char *cells_name,
842 struct of_phandle_args *out_args)
847 static inline int of_parse_phandle_with_args_map(const struct device_node *np,
848 const char *list_name,
849 const char *stem_name,
851 struct of_phandle_args *out_args)
856 static inline int of_parse_phandle_with_fixed_args(const struct device_node *np,
857 const char *list_name, int cells_count, int index,
858 struct of_phandle_args *out_args)
863 static inline int of_count_phandle_with_args(struct device_node *np,
864 const char *list_name,
865 const char *cells_name)
870 static inline int of_phandle_iterator_init(struct of_phandle_iterator *it,
871 const struct device_node *np,
872 const char *list_name,
873 const char *cells_name,
879 static inline int of_phandle_iterator_next(struct of_phandle_iterator *it)
884 static inline int of_phandle_iterator_args(struct of_phandle_iterator *it,
891 static inline int of_alias_get_id(struct device_node *np, const char *stem)
896 static inline int of_alias_get_highest_id(const char *stem)
901 static inline int of_machine_is_compatible(const char *compat)
906 static inline bool of_console_check(const struct device_node *dn, const char *name, int index)
911 static inline const __be32 *of_prop_next_u32(struct property *prop,
912 const __be32 *cur, u32 *pu)
917 static inline const char *of_prop_next_string(struct property *prop,
923 static inline int of_node_check_flag(struct device_node *n, unsigned long flag)
928 static inline int of_node_test_and_set_flag(struct device_node *n,
934 static inline void of_node_set_flag(struct device_node *n, unsigned long flag)
938 static inline void of_node_clear_flag(struct device_node *n, unsigned long flag)
942 static inline int of_property_check_flag(struct property *p, unsigned long flag)
947 static inline void of_property_set_flag(struct property *p, unsigned long flag)
951 static inline void of_property_clear_flag(struct property *p, unsigned long flag)
955 static inline int of_cpu_node_to_id(struct device_node *np)
960 #define of_match_ptr(_ptr) NULL
961 #define of_match_node(_matches, _node) NULL
962 #endif /* CONFIG_OF */
964 /* Default string compare functions, Allow arch asm/prom.h to override */
965 #if !defined(of_compat_cmp)
966 #define of_compat_cmp(s1, s2, l) strcasecmp((s1), (s2))
967 #define of_prop_cmp(s1, s2) strcmp((s1), (s2))
968 #define of_node_cmp(s1, s2) strcasecmp((s1), (s2))
971 static inline int of_prop_val_eq(struct property *p1, struct property *p2)
973 return p1->length == p2->length &&
974 !memcmp(p1->value, p2->value, (size_t)p1->length);
977 #if defined(CONFIG_OF) && defined(CONFIG_NUMA)
978 extern int of_node_to_nid(struct device_node *np);
980 static inline int of_node_to_nid(struct device_node *device)
986 #ifdef CONFIG_OF_NUMA
987 extern int of_numa_init(void);
989 static inline int of_numa_init(void)
995 static inline struct device_node *of_find_matching_node(
996 struct device_node *from,
997 const struct of_device_id *matches)
999 return of_find_matching_node_and_match(from, matches, NULL);
1002 static inline const char *of_node_get_device_type(const struct device_node *np)
1004 return of_get_property(np, "device_type", NULL);
1007 static inline bool of_node_is_type(const struct device_node *np, const char *type)
1009 const char *match = of_node_get_device_type(np);
1011 return np && match && type && !strcmp(match, type);
1015 * of_property_count_u8_elems - Count the number of u8 elements in a property
1017 * @np: device node from which the property value is to be read.
1018 * @propname: name of the property to be searched.
1020 * Search for a property in a device node and count the number of u8 elements
1021 * in it. Returns number of elements on sucess, -EINVAL if the property does
1022 * not exist or its length does not match a multiple of u8 and -ENODATA if the
1023 * property does not have a value.
1025 static inline int of_property_count_u8_elems(const struct device_node *np,
1026 const char *propname)
1028 return of_property_count_elems_of_size(np, propname, sizeof(u8));
1032 * of_property_count_u16_elems - Count the number of u16 elements in a property
1034 * @np: device node from which the property value is to be read.
1035 * @propname: name of the property to be searched.
1037 * Search for a property in a device node and count the number of u16 elements
1038 * in it. Returns number of elements on sucess, -EINVAL if the property does
1039 * not exist or its length does not match a multiple of u16 and -ENODATA if the
1040 * property does not have a value.
1042 static inline int of_property_count_u16_elems(const struct device_node *np,
1043 const char *propname)
1045 return of_property_count_elems_of_size(np, propname, sizeof(u16));
1049 * of_property_count_u32_elems - Count the number of u32 elements in a property
1051 * @np: device node from which the property value is to be read.
1052 * @propname: name of the property to be searched.
1054 * Search for a property in a device node and count the number of u32 elements
1055 * in it. Returns number of elements on sucess, -EINVAL if the property does
1056 * not exist or its length does not match a multiple of u32 and -ENODATA if the
1057 * property does not have a value.
1059 static inline int of_property_count_u32_elems(const struct device_node *np,
1060 const char *propname)
1062 return of_property_count_elems_of_size(np, propname, sizeof(u32));
1066 * of_property_count_u64_elems - Count the number of u64 elements in a property
1068 * @np: device node from which the property value is to be read.
1069 * @propname: name of the property to be searched.
1071 * Search for a property in a device node and count the number of u64 elements
1072 * in it. Returns number of elements on sucess, -EINVAL if the property does
1073 * not exist or its length does not match a multiple of u64 and -ENODATA if the
1074 * property does not have a value.
1076 static inline int of_property_count_u64_elems(const struct device_node *np,
1077 const char *propname)
1079 return of_property_count_elems_of_size(np, propname, sizeof(u64));
1083 * of_property_read_string_array() - Read an array of strings from a multiple
1085 * @np: device node from which the property value is to be read.
1086 * @propname: name of the property to be searched.
1087 * @out_strs: output array of string pointers.
1088 * @sz: number of array elements to read.
1090 * Search for a property in a device tree node and retrieve a list of
1091 * terminated string values (pointer to data, not a copy) in that property.
1093 * If @out_strs is NULL, the number of strings in the property is returned.
1095 static inline int of_property_read_string_array(const struct device_node *np,
1096 const char *propname, const char **out_strs,
1099 return of_property_read_string_helper(np, propname, out_strs, sz, 0);
1103 * of_property_count_strings() - Find and return the number of strings from a
1104 * multiple strings property.
1105 * @np: device node from which the property value is to be read.
1106 * @propname: name of the property to be searched.
1108 * Search for a property in a device tree node and retrieve the number of null
1109 * terminated string contain in it. Returns the number of strings on
1110 * success, -EINVAL if the property does not exist, -ENODATA if property
1111 * does not have a value, and -EILSEQ if the string is not null-terminated
1112 * within the length of the property data.
1114 static inline int of_property_count_strings(const struct device_node *np,
1115 const char *propname)
1117 return of_property_read_string_helper(np, propname, NULL, 0, 0);
1121 * of_property_read_string_index() - Find and read a string from a multiple
1123 * @np: device node from which the property value is to be read.
1124 * @propname: name of the property to be searched.
1125 * @index: index of the string in the list of strings
1126 * @out_string: pointer to null terminated return string, modified only if
1127 * return value is 0.
1129 * Search for a property in a device tree node and retrieve a null
1130 * terminated string value (pointer to data, not a copy) in the list of strings
1131 * contained in that property.
1132 * Returns 0 on success, -EINVAL if the property does not exist, -ENODATA if
1133 * property does not have a value, and -EILSEQ if the string is not
1134 * null-terminated within the length of the property data.
1136 * The out_string pointer is modified only if a valid string can be decoded.
1138 static inline int of_property_read_string_index(const struct device_node *np,
1139 const char *propname,
1140 int index, const char **output)
1142 int rc = of_property_read_string_helper(np, propname, output, 1, index);
1143 return rc < 0 ? rc : 0;
1147 * of_property_read_bool - Findfrom a property
1148 * @np: device node from which the property value is to be read.
1149 * @propname: name of the property to be searched.
1151 * Search for a property in a device node.
1152 * Returns true if the property exists false otherwise.
1154 static inline bool of_property_read_bool(const struct device_node *np,
1155 const char *propname)
1157 struct property *prop = of_find_property(np, propname, NULL);
1159 return prop ? true : false;
1162 static inline int of_property_read_u8(const struct device_node *np,
1163 const char *propname,
1166 return of_property_read_u8_array(np, propname, out_value, 1);
1169 static inline int of_property_read_u16(const struct device_node *np,
1170 const char *propname,
1173 return of_property_read_u16_array(np, propname, out_value, 1);
1176 static inline int of_property_read_u32(const struct device_node *np,
1177 const char *propname,
1180 return of_property_read_u32_array(np, propname, out_value, 1);
1183 static inline int of_property_read_s32(const struct device_node *np,
1184 const char *propname,
1187 return of_property_read_u32(np, propname, (u32*) out_value);
1190 #define of_for_each_phandle(it, err, np, ln, cn, cc) \
1191 for (of_phandle_iterator_init((it), (np), (ln), (cn), (cc)), \
1192 err = of_phandle_iterator_next(it); \
1194 err = of_phandle_iterator_next(it))
1196 #define of_property_for_each_u32(np, propname, prop, p, u) \
1197 for (prop = of_find_property(np, propname, NULL), \
1198 p = of_prop_next_u32(prop, NULL, &u); \
1200 p = of_prop_next_u32(prop, p, &u))
1202 #define of_property_for_each_string(np, propname, prop, s) \
1203 for (prop = of_find_property(np, propname, NULL), \
1204 s = of_prop_next_string(prop, NULL); \
1206 s = of_prop_next_string(prop, s))
1208 #define for_each_node_by_name(dn, name) \
1209 for (dn = of_find_node_by_name(NULL, name); dn; \
1210 dn = of_find_node_by_name(dn, name))
1211 #define for_each_node_by_type(dn, type) \
1212 for (dn = of_find_node_by_type(NULL, type); dn; \
1213 dn = of_find_node_by_type(dn, type))
1214 #define for_each_compatible_node(dn, type, compatible) \
1215 for (dn = of_find_compatible_node(NULL, type, compatible); dn; \
1216 dn = of_find_compatible_node(dn, type, compatible))
1217 #define for_each_matching_node(dn, matches) \
1218 for (dn = of_find_matching_node(NULL, matches); dn; \
1219 dn = of_find_matching_node(dn, matches))
1220 #define for_each_matching_node_and_match(dn, matches, match) \
1221 for (dn = of_find_matching_node_and_match(NULL, matches, match); \
1222 dn; dn = of_find_matching_node_and_match(dn, matches, match))
1224 #define for_each_child_of_node(parent, child) \
1225 for (child = of_get_next_child(parent, NULL); child != NULL; \
1226 child = of_get_next_child(parent, child))
1227 #define for_each_available_child_of_node(parent, child) \
1228 for (child = of_get_next_available_child(parent, NULL); child != NULL; \
1229 child = of_get_next_available_child(parent, child))
1231 #define for_each_node_with_property(dn, prop_name) \
1232 for (dn = of_find_node_with_property(NULL, prop_name); dn; \
1233 dn = of_find_node_with_property(dn, prop_name))
1235 static inline int of_get_child_count(const struct device_node *np)
1237 struct device_node *child;
1240 for_each_child_of_node(np, child)
1246 static inline int of_get_available_child_count(const struct device_node *np)
1248 struct device_node *child;
1251 for_each_available_child_of_node(np, child)
1257 #if defined(CONFIG_OF) && !defined(MODULE)
1258 #define _OF_DECLARE(table, name, compat, fn, fn_type) \
1259 static const struct of_device_id __of_table_##name \
1260 __used __section(__##table##_of_table) \
1261 __aligned(__alignof__(struct of_device_id)) \
1262 = { .compatible = compat, \
1263 .data = (fn == (fn_type)NULL) ? fn : fn }
1265 #define _OF_DECLARE(table, name, compat, fn, fn_type) \
1266 static const struct of_device_id __of_table_##name \
1267 __attribute__((unused)) \
1268 = { .compatible = compat, \
1269 .data = (fn == (fn_type)NULL) ? fn : fn }
1272 typedef int (*of_init_fn_2)(struct device_node *, struct device_node *);
1273 typedef int (*of_init_fn_1_ret)(struct device_node *);
1274 typedef void (*of_init_fn_1)(struct device_node *);
1276 #define OF_DECLARE_1(table, name, compat, fn) \
1277 _OF_DECLARE(table, name, compat, fn, of_init_fn_1)
1278 #define OF_DECLARE_1_RET(table, name, compat, fn) \
1279 _OF_DECLARE(table, name, compat, fn, of_init_fn_1_ret)
1280 #define OF_DECLARE_2(table, name, compat, fn) \
1281 _OF_DECLARE(table, name, compat, fn, of_init_fn_2)
1284 * struct of_changeset_entry - Holds a changeset entry
1286 * @node: list_head for the log list
1287 * @action: notifier action
1288 * @np: pointer to the device node affected
1289 * @prop: pointer to the property affected
1290 * @old_prop: hold a pointer to the original property
1292 * Every modification of the device tree during a changeset
1293 * is held in a list of of_changeset_entry structures.
1294 * That way we can recover from a partial application, or we can
1295 * revert the changeset
1297 struct of_changeset_entry {
1298 struct list_head node;
1299 unsigned long action;
1300 struct device_node *np;
1301 struct property *prop;
1302 struct property *old_prop;
1306 * struct of_changeset - changeset tracker structure
1308 * @entries: list_head for the changeset entries
1310 * changesets are a convenient way to apply bulk changes to the
1311 * live tree. In case of an error, changes are rolled-back.
1312 * changesets live on after initial application, and if not
1313 * destroyed after use, they can be reverted in one single call.
1315 struct of_changeset {
1316 struct list_head entries;
1319 enum of_reconfig_change {
1320 OF_RECONFIG_NO_CHANGE = 0,
1321 OF_RECONFIG_CHANGE_ADD,
1322 OF_RECONFIG_CHANGE_REMOVE,
1325 #ifdef CONFIG_OF_DYNAMIC
1326 extern int of_reconfig_notifier_register(struct notifier_block *);
1327 extern int of_reconfig_notifier_unregister(struct notifier_block *);
1328 extern int of_reconfig_notify(unsigned long, struct of_reconfig_data *rd);
1329 extern int of_reconfig_get_state_change(unsigned long action,
1330 struct of_reconfig_data *arg);
1332 extern void of_changeset_init(struct of_changeset *ocs);
1333 extern void of_changeset_destroy(struct of_changeset *ocs);
1334 extern int of_changeset_apply(struct of_changeset *ocs);
1335 extern int of_changeset_revert(struct of_changeset *ocs);
1336 extern int of_changeset_action(struct of_changeset *ocs,
1337 unsigned long action, struct device_node *np,
1338 struct property *prop);
1340 static inline int of_changeset_attach_node(struct of_changeset *ocs,
1341 struct device_node *np)
1343 return of_changeset_action(ocs, OF_RECONFIG_ATTACH_NODE, np, NULL);
1346 static inline int of_changeset_detach_node(struct of_changeset *ocs,
1347 struct device_node *np)
1349 return of_changeset_action(ocs, OF_RECONFIG_DETACH_NODE, np, NULL);
1352 static inline int of_changeset_add_property(struct of_changeset *ocs,
1353 struct device_node *np, struct property *prop)
1355 return of_changeset_action(ocs, OF_RECONFIG_ADD_PROPERTY, np, prop);
1358 static inline int of_changeset_remove_property(struct of_changeset *ocs,
1359 struct device_node *np, struct property *prop)
1361 return of_changeset_action(ocs, OF_RECONFIG_REMOVE_PROPERTY, np, prop);
1364 static inline int of_changeset_update_property(struct of_changeset *ocs,
1365 struct device_node *np, struct property *prop)
1367 return of_changeset_action(ocs, OF_RECONFIG_UPDATE_PROPERTY, np, prop);
1369 #else /* CONFIG_OF_DYNAMIC */
1370 static inline int of_reconfig_notifier_register(struct notifier_block *nb)
1374 static inline int of_reconfig_notifier_unregister(struct notifier_block *nb)
1378 static inline int of_reconfig_notify(unsigned long action,
1379 struct of_reconfig_data *arg)
1383 static inline int of_reconfig_get_state_change(unsigned long action,
1384 struct of_reconfig_data *arg)
1388 #endif /* CONFIG_OF_DYNAMIC */
1391 * of_device_is_system_power_controller - Tells if system-power-controller is found for device_node
1392 * @np: Pointer to the given device_node
1394 * return true if present false otherwise
1396 static inline bool of_device_is_system_power_controller(const struct device_node *np)
1398 return of_property_read_bool(np, "system-power-controller");
1405 enum of_overlay_notify_action {
1406 OF_OVERLAY_PRE_APPLY = 0,
1407 OF_OVERLAY_POST_APPLY,
1408 OF_OVERLAY_PRE_REMOVE,
1409 OF_OVERLAY_POST_REMOVE,
1412 struct of_overlay_notify_data {
1413 struct device_node *overlay;
1414 struct device_node *target;
1417 #ifdef CONFIG_OF_OVERLAY
1419 int of_overlay_fdt_apply(const void *overlay_fdt, u32 overlay_fdt_size,
1421 int of_overlay_remove(int *ovcs_id);
1422 int of_overlay_remove_all(void);
1424 int of_overlay_notifier_register(struct notifier_block *nb);
1425 int of_overlay_notifier_unregister(struct notifier_block *nb);
1429 static inline int of_overlay_fdt_apply(void *overlay_fdt, u32 overlay_fdt_size,
1435 static inline int of_overlay_remove(int *ovcs_id)
1440 static inline int of_overlay_remove_all(void)
1445 static inline int of_overlay_notifier_register(struct notifier_block *nb)
1450 static inline int of_overlay_notifier_unregister(struct notifier_block *nb)
1457 #endif /* _LINUX_OF_H */