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;
54 const char *full_name;
55 struct fwnode_handle fwnode;
57 struct property *properties;
58 struct property *deadprops; /* removed properties */
59 struct device_node *parent;
60 struct device_node *child;
61 struct device_node *sibling;
62 #if defined(CONFIG_OF_KOBJ)
67 #if defined(CONFIG_SPARC)
68 unsigned int unique_id;
69 struct of_irq_controller *irq_trans;
73 #define MAX_PHANDLE_ARGS 16
74 struct of_phandle_args {
75 struct device_node *np;
77 uint32_t args[MAX_PHANDLE_ARGS];
80 struct of_phandle_iterator {
81 /* Common iterator information */
82 const char *cells_name;
84 const struct device_node *parent;
86 /* List size information */
87 const __be32 *list_end;
88 const __be32 *phandle_end;
90 /* Current position state */
94 struct device_node *node;
97 struct of_reconfig_data {
98 struct device_node *dn;
99 struct property *prop;
100 struct property *old_prop;
104 * of_node_init - initialize a devicetree node
105 * @node: Pointer to device node that has been created by kzalloc()
106 * @phandle_name: Name of property holding a phandle value
108 * On return the device_node refcount is set to one. Use of_node_put()
109 * on @node when done to free the memory allocated for it. If the node
110 * is NOT a dynamic node the memory will not be freed. The decision of
111 * whether to free the memory will be done by node->release(), which is
114 /* initialize a node */
115 extern const struct kobj_type of_node_ktype;
116 extern const struct fwnode_operations of_fwnode_ops;
117 static inline void of_node_init(struct device_node *node)
119 #if defined(CONFIG_OF_KOBJ)
120 kobject_init(&node->kobj, &of_node_ktype);
122 fwnode_init(&node->fwnode, &of_fwnode_ops);
125 #if defined(CONFIG_OF_KOBJ)
126 #define of_node_kobj(n) (&(n)->kobj)
128 #define of_node_kobj(n) NULL
131 #ifdef CONFIG_OF_DYNAMIC
132 extern struct device_node *of_node_get(struct device_node *node);
133 extern void of_node_put(struct device_node *node);
134 #else /* CONFIG_OF_DYNAMIC */
135 /* Dummy ref counting routines - to be implemented later */
136 static inline struct device_node *of_node_get(struct device_node *node)
140 static inline void of_node_put(struct device_node *node) { }
141 #endif /* !CONFIG_OF_DYNAMIC */
143 /* Pointer for first entry in chain of all nodes. */
144 extern struct device_node *of_root;
145 extern struct device_node *of_chosen;
146 extern struct device_node *of_aliases;
147 extern struct device_node *of_stdout;
148 extern raw_spinlock_t devtree_lock;
151 * struct device_node flag descriptions
152 * (need to be visible even when !CONFIG_OF)
154 #define OF_DYNAMIC 1 /* (and properties) allocated via kmalloc */
155 #define OF_DETACHED 2 /* detached from the device tree */
156 #define OF_POPULATED 3 /* device already created */
157 #define OF_POPULATED_BUS 4 /* platform bus created for children */
158 #define OF_OVERLAY 5 /* allocated for an overlay */
159 #define OF_OVERLAY_FREE_CSET 6 /* in overlay cset being freed */
161 #define OF_BAD_ADDR ((u64)-1)
164 void of_core_init(void);
166 static inline bool is_of_node(const struct fwnode_handle *fwnode)
168 return !IS_ERR_OR_NULL(fwnode) && fwnode->ops == &of_fwnode_ops;
171 #define to_of_node(__fwnode) \
173 typeof(__fwnode) __to_of_node_fwnode = (__fwnode); \
175 is_of_node(__to_of_node_fwnode) ? \
176 container_of(__to_of_node_fwnode, \
177 struct device_node, fwnode) : \
181 #define of_fwnode_handle(node) \
183 typeof(node) __of_fwnode_handle_node = (node); \
185 __of_fwnode_handle_node ? \
186 &__of_fwnode_handle_node->fwnode : NULL; \
189 static inline bool of_have_populated_dt(void)
191 return of_root != NULL;
194 static inline bool of_node_is_root(const struct device_node *node)
196 return node && (node->parent == NULL);
199 static inline int of_node_check_flag(const struct device_node *n, unsigned long flag)
201 return test_bit(flag, &n->_flags);
204 static inline int of_node_test_and_set_flag(struct device_node *n,
207 return test_and_set_bit(flag, &n->_flags);
210 static inline void of_node_set_flag(struct device_node *n, unsigned long flag)
212 set_bit(flag, &n->_flags);
215 static inline void of_node_clear_flag(struct device_node *n, unsigned long flag)
217 clear_bit(flag, &n->_flags);
220 #if defined(CONFIG_OF_DYNAMIC) || defined(CONFIG_SPARC)
221 static inline int of_property_check_flag(const struct property *p, unsigned long flag)
223 return test_bit(flag, &p->_flags);
226 static inline void of_property_set_flag(struct property *p, unsigned long flag)
228 set_bit(flag, &p->_flags);
231 static inline void of_property_clear_flag(struct property *p, unsigned long flag)
233 clear_bit(flag, &p->_flags);
237 extern struct device_node *__of_find_all_nodes(struct device_node *prev);
238 extern struct device_node *of_find_all_nodes(struct device_node *prev);
241 * OF address retrieval & translation
244 /* Helper to read a big number; size is in cells (not bytes) */
245 static inline u64 of_read_number(const __be32 *cell, int size)
248 for (; size--; cell++)
249 r = (r << 32) | be32_to_cpu(*cell);
253 /* Like of_read_number, but we want an unsigned long result */
254 static inline unsigned long of_read_ulong(const __be32 *cell, int size)
256 /* toss away upper bits if unsigned long is smaller than u64 */
257 return of_read_number(cell, size);
260 #if defined(CONFIG_SPARC)
261 #include <asm/prom.h>
264 #define OF_IS_DYNAMIC(x) test_bit(OF_DYNAMIC, &x->_flags)
265 #define OF_MARK_DYNAMIC(x) set_bit(OF_DYNAMIC, &x->_flags)
267 extern bool of_node_name_eq(const struct device_node *np, const char *name);
268 extern bool of_node_name_prefix(const struct device_node *np, const char *prefix);
270 static inline const char *of_node_full_name(const struct device_node *np)
272 return np ? np->full_name : "<no-node>";
275 #define for_each_of_allnodes_from(from, dn) \
276 for (dn = __of_find_all_nodes(from); dn; dn = __of_find_all_nodes(dn))
277 #define for_each_of_allnodes(dn) for_each_of_allnodes_from(NULL, dn)
278 extern struct device_node *of_find_node_by_name(struct device_node *from,
280 extern struct device_node *of_find_node_by_type(struct device_node *from,
282 extern struct device_node *of_find_compatible_node(struct device_node *from,
283 const char *type, const char *compat);
284 extern struct device_node *of_find_matching_node_and_match(
285 struct device_node *from,
286 const struct of_device_id *matches,
287 const struct of_device_id **match);
289 extern struct device_node *of_find_node_opts_by_path(const char *path,
291 static inline struct device_node *of_find_node_by_path(const char *path)
293 return of_find_node_opts_by_path(path, NULL);
296 extern struct device_node *of_find_node_by_phandle(phandle handle);
297 extern struct device_node *of_get_parent(const struct device_node *node);
298 extern struct device_node *of_get_next_parent(struct device_node *node);
299 extern struct device_node *of_get_next_child(const struct device_node *node,
300 struct device_node *prev);
301 extern struct device_node *of_get_next_available_child(
302 const struct device_node *node, struct device_node *prev);
304 extern struct device_node *of_get_compatible_child(const struct device_node *parent,
305 const char *compatible);
306 extern struct device_node *of_get_child_by_name(const struct device_node *node,
310 extern struct device_node *of_find_next_cache_node(const struct device_node *);
311 extern int of_find_last_cache_level(unsigned int cpu);
312 extern struct device_node *of_find_node_with_property(
313 struct device_node *from, const char *prop_name);
315 extern struct property *of_find_property(const struct device_node *np,
318 extern int of_property_count_elems_of_size(const struct device_node *np,
319 const char *propname, int elem_size);
320 extern int of_property_read_u32_index(const struct device_node *np,
321 const char *propname,
322 u32 index, u32 *out_value);
323 extern int of_property_read_u64_index(const struct device_node *np,
324 const char *propname,
325 u32 index, u64 *out_value);
326 extern int of_property_read_variable_u8_array(const struct device_node *np,
327 const char *propname, u8 *out_values,
328 size_t sz_min, size_t sz_max);
329 extern int of_property_read_variable_u16_array(const struct device_node *np,
330 const char *propname, u16 *out_values,
331 size_t sz_min, size_t sz_max);
332 extern int of_property_read_variable_u32_array(const struct device_node *np,
333 const char *propname,
337 extern int of_property_read_u64(const struct device_node *np,
338 const char *propname, u64 *out_value);
339 extern int of_property_read_variable_u64_array(const struct device_node *np,
340 const char *propname,
345 extern int of_property_read_string(const struct device_node *np,
346 const char *propname,
347 const char **out_string);
348 extern int of_property_match_string(const struct device_node *np,
349 const char *propname,
351 extern int of_property_read_string_helper(const struct device_node *np,
352 const char *propname,
353 const char **out_strs, size_t sz, int index);
354 extern int of_device_is_compatible(const struct device_node *device,
356 extern int of_device_compatible_match(const struct device_node *device,
357 const char *const *compat);
358 extern bool of_device_is_available(const struct device_node *device);
359 extern bool of_device_is_big_endian(const struct device_node *device);
360 extern const void *of_get_property(const struct device_node *node,
363 extern struct device_node *of_get_cpu_node(int cpu, unsigned int *thread);
364 extern struct device_node *of_get_next_cpu_node(struct device_node *prev);
365 extern struct device_node *of_get_cpu_state_node(struct device_node *cpu_node,
367 extern u64 of_get_cpu_hwid(struct device_node *cpun, unsigned int thread);
369 #define for_each_property_of_node(dn, pp) \
370 for (pp = dn->properties; pp != NULL; pp = pp->next)
372 extern int of_n_addr_cells(struct device_node *np);
373 extern int of_n_size_cells(struct device_node *np);
374 extern const struct of_device_id *of_match_node(
375 const struct of_device_id *matches, const struct device_node *node);
376 extern int of_modalias_node(struct device_node *node, char *modalias, int len);
377 extern void of_print_phandle_args(const char *msg, const struct of_phandle_args *args);
378 extern int __of_parse_phandle_with_args(const struct device_node *np,
379 const char *list_name, const char *cells_name, int cell_count,
380 int index, struct of_phandle_args *out_args);
381 extern int of_parse_phandle_with_args_map(const struct device_node *np,
382 const char *list_name, const char *stem_name, int index,
383 struct of_phandle_args *out_args);
384 extern int of_count_phandle_with_args(const struct device_node *np,
385 const char *list_name, const char *cells_name);
387 /* phandle iterator functions */
388 extern int of_phandle_iterator_init(struct of_phandle_iterator *it,
389 const struct device_node *np,
390 const char *list_name,
391 const char *cells_name,
394 extern int of_phandle_iterator_next(struct of_phandle_iterator *it);
395 extern int of_phandle_iterator_args(struct of_phandle_iterator *it,
399 extern void of_alias_scan(void * (*dt_alloc)(u64 size, u64 align));
400 extern int of_alias_get_id(struct device_node *np, const char *stem);
401 extern int of_alias_get_highest_id(const char *stem);
403 extern int of_machine_is_compatible(const char *compat);
405 extern int of_add_property(struct device_node *np, struct property *prop);
406 extern int of_remove_property(struct device_node *np, struct property *prop);
407 extern int of_update_property(struct device_node *np, struct property *newprop);
409 /* For updating the device tree at runtime */
410 #define OF_RECONFIG_ATTACH_NODE 0x0001
411 #define OF_RECONFIG_DETACH_NODE 0x0002
412 #define OF_RECONFIG_ADD_PROPERTY 0x0003
413 #define OF_RECONFIG_REMOVE_PROPERTY 0x0004
414 #define OF_RECONFIG_UPDATE_PROPERTY 0x0005
416 extern int of_attach_node(struct device_node *);
417 extern int of_detach_node(struct device_node *);
419 #define of_match_ptr(_ptr) (_ptr)
422 * struct property *prop;
426 * of_property_for_each_u32(np, "propname", prop, p, u)
427 * printk("U32 value: %x\n", u);
429 const __be32 *of_prop_next_u32(struct property *prop, const __be32 *cur,
432 * struct property *prop;
435 * of_property_for_each_string(np, "propname", prop, s)
436 * printk("String value: %s\n", s);
438 const char *of_prop_next_string(struct property *prop, const char *cur);
440 bool of_console_check(struct device_node *dn, char *name, int index);
442 extern int of_cpu_node_to_id(struct device_node *np);
444 int of_map_id(struct device_node *np, u32 id,
445 const char *map_name, const char *map_mask_name,
446 struct device_node **target, u32 *id_out);
448 phys_addr_t of_dma_get_max_cpu_address(struct device_node *np);
451 void *of_kexec_alloc_and_setup_fdt(const struct kimage *image,
452 unsigned long initrd_load_addr,
453 unsigned long initrd_len,
454 const char *cmdline, size_t extra_fdt_size);
455 #else /* CONFIG_OF */
457 static inline void of_core_init(void)
461 static inline bool is_of_node(const struct fwnode_handle *fwnode)
466 static inline struct device_node *to_of_node(const struct fwnode_handle *fwnode)
471 static inline bool of_node_name_eq(const struct device_node *np, const char *name)
476 static inline bool of_node_name_prefix(const struct device_node *np, const char *prefix)
481 static inline const char* of_node_full_name(const struct device_node *np)
486 static inline struct device_node *of_find_node_by_name(struct device_node *from,
492 static inline struct device_node *of_find_node_by_type(struct device_node *from,
498 static inline struct device_node *of_find_matching_node_and_match(
499 struct device_node *from,
500 const struct of_device_id *matches,
501 const struct of_device_id **match)
506 static inline struct device_node *of_find_node_by_path(const char *path)
511 static inline struct device_node *of_find_node_opts_by_path(const char *path,
517 static inline struct device_node *of_find_node_by_phandle(phandle handle)
522 static inline struct device_node *of_get_parent(const struct device_node *node)
527 static inline struct device_node *of_get_next_parent(struct device_node *node)
532 static inline struct device_node *of_get_next_child(
533 const struct device_node *node, struct device_node *prev)
538 static inline struct device_node *of_get_next_available_child(
539 const struct device_node *node, struct device_node *prev)
544 static inline struct device_node *of_find_node_with_property(
545 struct device_node *from, const char *prop_name)
550 #define of_fwnode_handle(node) NULL
552 static inline bool of_have_populated_dt(void)
557 static inline struct device_node *of_get_compatible_child(const struct device_node *parent,
558 const char *compatible)
563 static inline struct device_node *of_get_child_by_name(
564 const struct device_node *node,
570 static inline int of_device_is_compatible(const struct device_node *device,
576 static inline int of_device_compatible_match(const struct device_node *device,
577 const char *const *compat)
582 static inline bool of_device_is_available(const struct device_node *device)
587 static inline bool of_device_is_big_endian(const struct device_node *device)
592 static inline struct property *of_find_property(const struct device_node *np,
599 static inline struct device_node *of_find_compatible_node(
600 struct device_node *from,
607 static inline int of_property_count_elems_of_size(const struct device_node *np,
608 const char *propname, int elem_size)
613 static inline int of_property_read_u32_index(const struct device_node *np,
614 const char *propname, u32 index, u32 *out_value)
619 static inline int of_property_read_u64_index(const struct device_node *np,
620 const char *propname, u32 index, u64 *out_value)
625 static inline const void *of_get_property(const struct device_node *node,
632 static inline struct device_node *of_get_cpu_node(int cpu,
633 unsigned int *thread)
638 static inline struct device_node *of_get_next_cpu_node(struct device_node *prev)
643 static inline struct device_node *of_get_cpu_state_node(struct device_node *cpu_node,
649 static inline int of_n_addr_cells(struct device_node *np)
654 static inline int of_n_size_cells(struct device_node *np)
659 static inline int of_property_read_variable_u8_array(const struct device_node *np,
660 const char *propname, u8 *out_values,
661 size_t sz_min, size_t sz_max)
666 static inline int of_property_read_variable_u16_array(const struct device_node *np,
667 const char *propname, u16 *out_values,
668 size_t sz_min, size_t sz_max)
673 static inline int of_property_read_variable_u32_array(const struct device_node *np,
674 const char *propname,
682 static inline int of_property_read_u64(const struct device_node *np,
683 const char *propname, u64 *out_value)
688 static inline int of_property_read_variable_u64_array(const struct device_node *np,
689 const char *propname,
697 static inline int of_property_read_string(const struct device_node *np,
698 const char *propname,
699 const char **out_string)
704 static inline int of_property_match_string(const struct device_node *np,
705 const char *propname,
711 static inline int of_property_read_string_helper(const struct device_node *np,
712 const char *propname,
713 const char **out_strs, size_t sz, int index)
718 static inline int __of_parse_phandle_with_args(const struct device_node *np,
719 const char *list_name,
720 const char *cells_name,
723 struct of_phandle_args *out_args)
728 static inline int of_parse_phandle_with_args_map(const struct device_node *np,
729 const char *list_name,
730 const char *stem_name,
732 struct of_phandle_args *out_args)
737 static inline int of_count_phandle_with_args(const struct device_node *np,
738 const char *list_name,
739 const char *cells_name)
744 static inline int of_phandle_iterator_init(struct of_phandle_iterator *it,
745 const struct device_node *np,
746 const char *list_name,
747 const char *cells_name,
753 static inline int of_phandle_iterator_next(struct of_phandle_iterator *it)
758 static inline int of_phandle_iterator_args(struct of_phandle_iterator *it,
765 static inline int of_alias_get_id(struct device_node *np, const char *stem)
770 static inline int of_alias_get_highest_id(const char *stem)
775 static inline int of_machine_is_compatible(const char *compat)
780 static inline int of_add_property(struct device_node *np, struct property *prop)
785 static inline int of_remove_property(struct device_node *np, struct property *prop)
790 static inline bool of_console_check(const struct device_node *dn, const char *name, int index)
795 static inline const __be32 *of_prop_next_u32(struct property *prop,
796 const __be32 *cur, u32 *pu)
801 static inline const char *of_prop_next_string(struct property *prop,
807 static inline int of_node_check_flag(struct device_node *n, unsigned long flag)
812 static inline int of_node_test_and_set_flag(struct device_node *n,
818 static inline void of_node_set_flag(struct device_node *n, unsigned long flag)
822 static inline void of_node_clear_flag(struct device_node *n, unsigned long flag)
826 static inline int of_property_check_flag(const struct property *p,
832 static inline void of_property_set_flag(struct property *p, unsigned long flag)
836 static inline void of_property_clear_flag(struct property *p, unsigned long flag)
840 static inline int of_cpu_node_to_id(struct device_node *np)
845 static inline int of_map_id(struct device_node *np, u32 id,
846 const char *map_name, const char *map_mask_name,
847 struct device_node **target, u32 *id_out)
852 static inline phys_addr_t of_dma_get_max_cpu_address(struct device_node *np)
854 return PHYS_ADDR_MAX;
857 #define of_match_ptr(_ptr) NULL
858 #define of_match_node(_matches, _node) NULL
859 #endif /* CONFIG_OF */
861 /* Default string compare functions, Allow arch asm/prom.h to override */
862 #if !defined(of_compat_cmp)
863 #define of_compat_cmp(s1, s2, l) strcasecmp((s1), (s2))
864 #define of_prop_cmp(s1, s2) strcmp((s1), (s2))
865 #define of_node_cmp(s1, s2) strcasecmp((s1), (s2))
868 static inline int of_prop_val_eq(struct property *p1, struct property *p2)
870 return p1->length == p2->length &&
871 !memcmp(p1->value, p2->value, (size_t)p1->length);
874 #if defined(CONFIG_OF) && defined(CONFIG_NUMA)
875 extern int of_node_to_nid(struct device_node *np);
877 static inline int of_node_to_nid(struct device_node *device)
883 #ifdef CONFIG_OF_NUMA
884 extern int of_numa_init(void);
886 static inline int of_numa_init(void)
892 static inline struct device_node *of_find_matching_node(
893 struct device_node *from,
894 const struct of_device_id *matches)
896 return of_find_matching_node_and_match(from, matches, NULL);
899 static inline const char *of_node_get_device_type(const struct device_node *np)
901 return of_get_property(np, "device_type", NULL);
904 static inline bool of_node_is_type(const struct device_node *np, const char *type)
906 const char *match = of_node_get_device_type(np);
908 return np && match && type && !strcmp(match, type);
912 * of_parse_phandle - Resolve a phandle property to a device_node pointer
913 * @np: Pointer to device node holding phandle property
914 * @phandle_name: Name of property holding a phandle value
915 * @index: For properties holding a table of phandles, this is the index into
918 * Return: The device_node pointer with refcount incremented. Use
919 * of_node_put() on it when done.
921 static inline struct device_node *of_parse_phandle(const struct device_node *np,
922 const char *phandle_name,
925 struct of_phandle_args args;
927 if (__of_parse_phandle_with_args(np, phandle_name, NULL, 0,
935 * of_parse_phandle_with_args() - Find a node pointed by phandle in a list
936 * @np: pointer to a device tree node containing a list
937 * @list_name: property name that contains a list
938 * @cells_name: property name that specifies phandles' arguments count
939 * @index: index of a phandle to parse out
940 * @out_args: optional pointer to output arguments structure (will be filled)
942 * This function is useful to parse lists of phandles and their arguments.
943 * Returns 0 on success and fills out_args, on error returns appropriate
946 * Caller is responsible to call of_node_put() on the returned out_args->np
960 * list = <&phandle1 1 2 &phandle2 3>;
963 * To get a device_node of the ``node2`` node you may call this:
964 * of_parse_phandle_with_args(node3, "list", "#list-cells", 1, &args);
966 static inline int of_parse_phandle_with_args(const struct device_node *np,
967 const char *list_name,
968 const char *cells_name,
970 struct of_phandle_args *out_args)
974 /* If cells_name is NULL we assume a cell count of 0 */
978 return __of_parse_phandle_with_args(np, list_name, cells_name,
979 cell_count, index, out_args);
983 * of_parse_phandle_with_fixed_args() - Find a node pointed by phandle in a list
984 * @np: pointer to a device tree node containing a list
985 * @list_name: property name that contains a list
986 * @cell_count: number of argument cells following the phandle
987 * @index: index of a phandle to parse out
988 * @out_args: optional pointer to output arguments structure (will be filled)
990 * This function is useful to parse lists of phandles and their arguments.
991 * Returns 0 on success and fills out_args, on error returns appropriate
994 * Caller is responsible to call of_node_put() on the returned out_args->np
1006 * list = <&phandle1 0 2 &phandle2 2 3>;
1009 * To get a device_node of the ``node2`` node you may call this:
1010 * of_parse_phandle_with_fixed_args(node3, "list", 2, 1, &args);
1012 static inline int of_parse_phandle_with_fixed_args(const struct device_node *np,
1013 const char *list_name,
1016 struct of_phandle_args *out_args)
1018 return __of_parse_phandle_with_args(np, list_name, NULL, cell_count,
1023 * of_parse_phandle_with_optional_args() - Find a node pointed by phandle in a list
1024 * @np: pointer to a device tree node containing a list
1025 * @list_name: property name that contains a list
1026 * @cells_name: property name that specifies phandles' arguments count
1027 * @index: index of a phandle to parse out
1028 * @out_args: optional pointer to output arguments structure (will be filled)
1030 * Same as of_parse_phandle_with_args() except that if the cells_name property
1031 * is not found, cell_count of 0 is assumed.
1033 * This is used to useful, if you have a phandle which didn't have arguments
1034 * before and thus doesn't have a '#*-cells' property but is now migrated to
1035 * having arguments while retaining backwards compatibility.
1037 static inline int of_parse_phandle_with_optional_args(const struct device_node *np,
1038 const char *list_name,
1039 const char *cells_name,
1041 struct of_phandle_args *out_args)
1043 return __of_parse_phandle_with_args(np, list_name, cells_name,
1044 0, index, out_args);
1048 * of_property_count_u8_elems - Count the number of u8 elements in a property
1050 * @np: device node from which the property value is to be read.
1051 * @propname: name of the property to be searched.
1053 * Search for a property in a device node and count the number of u8 elements
1056 * Return: The number of elements on sucess, -EINVAL if the property does
1057 * not exist or its length does not match a multiple of u8 and -ENODATA if the
1058 * property does not have a value.
1060 static inline int of_property_count_u8_elems(const struct device_node *np,
1061 const char *propname)
1063 return of_property_count_elems_of_size(np, propname, sizeof(u8));
1067 * of_property_count_u16_elems - Count the number of u16 elements in a property
1069 * @np: device node from which the property value is to be read.
1070 * @propname: name of the property to be searched.
1072 * Search for a property in a device node and count the number of u16 elements
1075 * Return: The number of elements on sucess, -EINVAL if the property does
1076 * not exist or its length does not match a multiple of u16 and -ENODATA if the
1077 * property does not have a value.
1079 static inline int of_property_count_u16_elems(const struct device_node *np,
1080 const char *propname)
1082 return of_property_count_elems_of_size(np, propname, sizeof(u16));
1086 * of_property_count_u32_elems - Count the number of u32 elements in a property
1088 * @np: device node from which the property value is to be read.
1089 * @propname: name of the property to be searched.
1091 * Search for a property in a device node and count the number of u32 elements
1094 * Return: The number of elements on sucess, -EINVAL if the property does
1095 * not exist or its length does not match a multiple of u32 and -ENODATA if the
1096 * property does not have a value.
1098 static inline int of_property_count_u32_elems(const struct device_node *np,
1099 const char *propname)
1101 return of_property_count_elems_of_size(np, propname, sizeof(u32));
1105 * of_property_count_u64_elems - Count the number of u64 elements in a property
1107 * @np: device node from which the property value is to be read.
1108 * @propname: name of the property to be searched.
1110 * Search for a property in a device node and count the number of u64 elements
1113 * Return: The number of elements on sucess, -EINVAL if the property does
1114 * not exist or its length does not match a multiple of u64 and -ENODATA if the
1115 * property does not have a value.
1117 static inline int of_property_count_u64_elems(const struct device_node *np,
1118 const char *propname)
1120 return of_property_count_elems_of_size(np, propname, sizeof(u64));
1124 * of_property_read_string_array() - Read an array of strings from a multiple
1126 * @np: device node from which the property value is to be read.
1127 * @propname: name of the property to be searched.
1128 * @out_strs: output array of string pointers.
1129 * @sz: number of array elements to read.
1131 * Search for a property in a device tree node and retrieve a list of
1132 * terminated string values (pointer to data, not a copy) in that property.
1134 * Return: If @out_strs is NULL, the number of strings in the property is returned.
1136 static inline int of_property_read_string_array(const struct device_node *np,
1137 const char *propname, const char **out_strs,
1140 return of_property_read_string_helper(np, propname, out_strs, sz, 0);
1144 * of_property_count_strings() - Find and return the number of strings from a
1145 * multiple strings property.
1146 * @np: device node from which the property value is to be read.
1147 * @propname: name of the property to be searched.
1149 * Search for a property in a device tree node and retrieve the number of null
1150 * terminated string contain in it.
1152 * Return: The number of strings on success, -EINVAL if the property does not
1153 * exist, -ENODATA if property does not have a value, and -EILSEQ if the string
1154 * is not null-terminated within the length of the property data.
1156 static inline int of_property_count_strings(const struct device_node *np,
1157 const char *propname)
1159 return of_property_read_string_helper(np, propname, NULL, 0, 0);
1163 * of_property_read_string_index() - Find and read a string from a multiple
1165 * @np: device node from which the property value is to be read.
1166 * @propname: name of the property to be searched.
1167 * @index: index of the string in the list of strings
1168 * @output: pointer to null terminated return string, modified only if
1169 * return value is 0.
1171 * Search for a property in a device tree node and retrieve a null
1172 * terminated string value (pointer to data, not a copy) in the list of strings
1173 * contained in that property.
1175 * Return: 0 on success, -EINVAL if the property does not exist, -ENODATA if
1176 * property does not have a value, and -EILSEQ if the string is not
1177 * null-terminated within the length of the property data.
1179 * The out_string pointer is modified only if a valid string can be decoded.
1181 static inline int of_property_read_string_index(const struct device_node *np,
1182 const char *propname,
1183 int index, const char **output)
1185 int rc = of_property_read_string_helper(np, propname, output, 1, index);
1186 return rc < 0 ? rc : 0;
1190 * of_property_read_bool - Find a property
1191 * @np: device node from which the property value is to be read.
1192 * @propname: name of the property to be searched.
1194 * Search for a boolean property in a device node. Usage on non-boolean
1195 * property types is deprecated.
1197 * Return: true if the property exists false otherwise.
1199 static inline bool of_property_read_bool(const struct device_node *np,
1200 const char *propname)
1202 struct property *prop = of_find_property(np, propname, NULL);
1204 return prop ? true : false;
1208 * of_property_present - Test if a property is present in a node
1209 * @np: device node to search for the property.
1210 * @propname: name of the property to be searched.
1212 * Test for a property present in a device node.
1214 * Return: true if the property exists false otherwise.
1216 static inline bool of_property_present(const struct device_node *np, const char *propname)
1218 return of_property_read_bool(np, propname);
1222 * of_property_read_u8_array - Find and read an array of u8 from a property.
1224 * @np: device node from which the property value is to be read.
1225 * @propname: name of the property to be searched.
1226 * @out_values: pointer to return value, modified only if return value is 0.
1227 * @sz: number of array elements to read
1229 * Search for a property in a device node and read 8-bit value(s) from
1232 * dts entry of array should be like:
1233 * ``property = /bits/ 8 <0x50 0x60 0x70>;``
1235 * Return: 0 on success, -EINVAL if the property does not exist,
1236 * -ENODATA if property does not have a value, and -EOVERFLOW if the
1237 * property data isn't large enough.
1239 * The out_values is modified only if a valid u8 value can be decoded.
1241 static inline int of_property_read_u8_array(const struct device_node *np,
1242 const char *propname,
1243 u8 *out_values, size_t sz)
1245 int ret = of_property_read_variable_u8_array(np, propname, out_values,
1254 * of_property_read_u16_array - Find and read an array of u16 from a property.
1256 * @np: device node from which the property value is to be read.
1257 * @propname: name of the property to be searched.
1258 * @out_values: pointer to return value, modified only if return value is 0.
1259 * @sz: number of array elements to read
1261 * Search for a property in a device node and read 16-bit value(s) from
1264 * dts entry of array should be like:
1265 * ``property = /bits/ 16 <0x5000 0x6000 0x7000>;``
1267 * Return: 0 on success, -EINVAL if the property does not exist,
1268 * -ENODATA if property does not have a value, and -EOVERFLOW if the
1269 * property data isn't large enough.
1271 * The out_values is modified only if a valid u16 value can be decoded.
1273 static inline int of_property_read_u16_array(const struct device_node *np,
1274 const char *propname,
1275 u16 *out_values, size_t sz)
1277 int ret = of_property_read_variable_u16_array(np, propname, out_values,
1286 * of_property_read_u32_array - Find and read an array of 32 bit integers
1289 * @np: device node from which the property value is to be read.
1290 * @propname: name of the property to be searched.
1291 * @out_values: pointer to return value, modified only if return value is 0.
1292 * @sz: number of array elements to read
1294 * Search for a property in a device node and read 32-bit value(s) from
1297 * Return: 0 on success, -EINVAL if the property does not exist,
1298 * -ENODATA if property does not have a value, and -EOVERFLOW if the
1299 * property data isn't large enough.
1301 * The out_values is modified only if a valid u32 value can be decoded.
1303 static inline int of_property_read_u32_array(const struct device_node *np,
1304 const char *propname,
1305 u32 *out_values, size_t sz)
1307 int ret = of_property_read_variable_u32_array(np, propname, out_values,
1316 * of_property_read_u64_array - Find and read an array of 64 bit integers
1319 * @np: device node from which the property value is to be read.
1320 * @propname: name of the property to be searched.
1321 * @out_values: pointer to return value, modified only if return value is 0.
1322 * @sz: number of array elements to read
1324 * Search for a property in a device node and read 64-bit value(s) from
1327 * Return: 0 on success, -EINVAL if the property does not exist,
1328 * -ENODATA if property does not have a value, and -EOVERFLOW if the
1329 * property data isn't large enough.
1331 * The out_values is modified only if a valid u64 value can be decoded.
1333 static inline int of_property_read_u64_array(const struct device_node *np,
1334 const char *propname,
1335 u64 *out_values, size_t sz)
1337 int ret = of_property_read_variable_u64_array(np, propname, out_values,
1345 static inline int of_property_read_u8(const struct device_node *np,
1346 const char *propname,
1349 return of_property_read_u8_array(np, propname, out_value, 1);
1352 static inline int of_property_read_u16(const struct device_node *np,
1353 const char *propname,
1356 return of_property_read_u16_array(np, propname, out_value, 1);
1359 static inline int of_property_read_u32(const struct device_node *np,
1360 const char *propname,
1363 return of_property_read_u32_array(np, propname, out_value, 1);
1366 static inline int of_property_read_s32(const struct device_node *np,
1367 const char *propname,
1370 return of_property_read_u32(np, propname, (u32*) out_value);
1373 #define of_for_each_phandle(it, err, np, ln, cn, cc) \
1374 for (of_phandle_iterator_init((it), (np), (ln), (cn), (cc)), \
1375 err = of_phandle_iterator_next(it); \
1377 err = of_phandle_iterator_next(it))
1379 #define of_property_for_each_u32(np, propname, prop, p, u) \
1380 for (prop = of_find_property(np, propname, NULL), \
1381 p = of_prop_next_u32(prop, NULL, &u); \
1383 p = of_prop_next_u32(prop, p, &u))
1385 #define of_property_for_each_string(np, propname, prop, s) \
1386 for (prop = of_find_property(np, propname, NULL), \
1387 s = of_prop_next_string(prop, NULL); \
1389 s = of_prop_next_string(prop, s))
1391 #define for_each_node_by_name(dn, name) \
1392 for (dn = of_find_node_by_name(NULL, name); dn; \
1393 dn = of_find_node_by_name(dn, name))
1394 #define for_each_node_by_type(dn, type) \
1395 for (dn = of_find_node_by_type(NULL, type); dn; \
1396 dn = of_find_node_by_type(dn, type))
1397 #define for_each_compatible_node(dn, type, compatible) \
1398 for (dn = of_find_compatible_node(NULL, type, compatible); dn; \
1399 dn = of_find_compatible_node(dn, type, compatible))
1400 #define for_each_matching_node(dn, matches) \
1401 for (dn = of_find_matching_node(NULL, matches); dn; \
1402 dn = of_find_matching_node(dn, matches))
1403 #define for_each_matching_node_and_match(dn, matches, match) \
1404 for (dn = of_find_matching_node_and_match(NULL, matches, match); \
1405 dn; dn = of_find_matching_node_and_match(dn, matches, match))
1407 #define for_each_child_of_node(parent, child) \
1408 for (child = of_get_next_child(parent, NULL); child != NULL; \
1409 child = of_get_next_child(parent, child))
1410 #define for_each_available_child_of_node(parent, child) \
1411 for (child = of_get_next_available_child(parent, NULL); child != NULL; \
1412 child = of_get_next_available_child(parent, child))
1414 #define for_each_of_cpu_node(cpu) \
1415 for (cpu = of_get_next_cpu_node(NULL); cpu != NULL; \
1416 cpu = of_get_next_cpu_node(cpu))
1418 #define for_each_node_with_property(dn, prop_name) \
1419 for (dn = of_find_node_with_property(NULL, prop_name); dn; \
1420 dn = of_find_node_with_property(dn, prop_name))
1422 static inline int of_get_child_count(const struct device_node *np)
1424 struct device_node *child;
1427 for_each_child_of_node(np, child)
1433 static inline int of_get_available_child_count(const struct device_node *np)
1435 struct device_node *child;
1438 for_each_available_child_of_node(np, child)
1444 #define _OF_DECLARE_STUB(table, name, compat, fn, fn_type) \
1445 static const struct of_device_id __of_table_##name \
1446 __attribute__((unused)) \
1447 = { .compatible = compat, \
1448 .data = (fn == (fn_type)NULL) ? fn : fn }
1450 #if defined(CONFIG_OF) && !defined(MODULE)
1451 #define _OF_DECLARE(table, name, compat, fn, fn_type) \
1452 static const struct of_device_id __of_table_##name \
1453 __used __section("__" #table "_of_table") \
1454 __aligned(__alignof__(struct of_device_id)) \
1455 = { .compatible = compat, \
1456 .data = (fn == (fn_type)NULL) ? fn : fn }
1458 #define _OF_DECLARE(table, name, compat, fn, fn_type) \
1459 _OF_DECLARE_STUB(table, name, compat, fn, fn_type)
1462 typedef int (*of_init_fn_2)(struct device_node *, struct device_node *);
1463 typedef int (*of_init_fn_1_ret)(struct device_node *);
1464 typedef void (*of_init_fn_1)(struct device_node *);
1466 #define OF_DECLARE_1(table, name, compat, fn) \
1467 _OF_DECLARE(table, name, compat, fn, of_init_fn_1)
1468 #define OF_DECLARE_1_RET(table, name, compat, fn) \
1469 _OF_DECLARE(table, name, compat, fn, of_init_fn_1_ret)
1470 #define OF_DECLARE_2(table, name, compat, fn) \
1471 _OF_DECLARE(table, name, compat, fn, of_init_fn_2)
1474 * struct of_changeset_entry - Holds a changeset entry
1476 * @node: list_head for the log list
1477 * @action: notifier action
1478 * @np: pointer to the device node affected
1479 * @prop: pointer to the property affected
1480 * @old_prop: hold a pointer to the original property
1482 * Every modification of the device tree during a changeset
1483 * is held in a list of of_changeset_entry structures.
1484 * That way we can recover from a partial application, or we can
1485 * revert the changeset
1487 struct of_changeset_entry {
1488 struct list_head node;
1489 unsigned long action;
1490 struct device_node *np;
1491 struct property *prop;
1492 struct property *old_prop;
1496 * struct of_changeset - changeset tracker structure
1498 * @entries: list_head for the changeset entries
1500 * changesets are a convenient way to apply bulk changes to the
1501 * live tree. In case of an error, changes are rolled-back.
1502 * changesets live on after initial application, and if not
1503 * destroyed after use, they can be reverted in one single call.
1505 struct of_changeset {
1506 struct list_head entries;
1509 enum of_reconfig_change {
1510 OF_RECONFIG_NO_CHANGE = 0,
1511 OF_RECONFIG_CHANGE_ADD,
1512 OF_RECONFIG_CHANGE_REMOVE,
1515 #ifdef CONFIG_OF_DYNAMIC
1516 extern int of_reconfig_notifier_register(struct notifier_block *);
1517 extern int of_reconfig_notifier_unregister(struct notifier_block *);
1518 extern int of_reconfig_notify(unsigned long, struct of_reconfig_data *rd);
1519 extern int of_reconfig_get_state_change(unsigned long action,
1520 struct of_reconfig_data *arg);
1522 extern void of_changeset_init(struct of_changeset *ocs);
1523 extern void of_changeset_destroy(struct of_changeset *ocs);
1524 extern int of_changeset_apply(struct of_changeset *ocs);
1525 extern int of_changeset_revert(struct of_changeset *ocs);
1526 extern int of_changeset_action(struct of_changeset *ocs,
1527 unsigned long action, struct device_node *np,
1528 struct property *prop);
1530 static inline int of_changeset_attach_node(struct of_changeset *ocs,
1531 struct device_node *np)
1533 return of_changeset_action(ocs, OF_RECONFIG_ATTACH_NODE, np, NULL);
1536 static inline int of_changeset_detach_node(struct of_changeset *ocs,
1537 struct device_node *np)
1539 return of_changeset_action(ocs, OF_RECONFIG_DETACH_NODE, np, NULL);
1542 static inline int of_changeset_add_property(struct of_changeset *ocs,
1543 struct device_node *np, struct property *prop)
1545 return of_changeset_action(ocs, OF_RECONFIG_ADD_PROPERTY, np, prop);
1548 static inline int of_changeset_remove_property(struct of_changeset *ocs,
1549 struct device_node *np, struct property *prop)
1551 return of_changeset_action(ocs, OF_RECONFIG_REMOVE_PROPERTY, np, prop);
1554 static inline int of_changeset_update_property(struct of_changeset *ocs,
1555 struct device_node *np, struct property *prop)
1557 return of_changeset_action(ocs, OF_RECONFIG_UPDATE_PROPERTY, np, prop);
1559 #else /* CONFIG_OF_DYNAMIC */
1560 static inline int of_reconfig_notifier_register(struct notifier_block *nb)
1564 static inline int of_reconfig_notifier_unregister(struct notifier_block *nb)
1568 static inline int of_reconfig_notify(unsigned long action,
1569 struct of_reconfig_data *arg)
1573 static inline int of_reconfig_get_state_change(unsigned long action,
1574 struct of_reconfig_data *arg)
1578 #endif /* CONFIG_OF_DYNAMIC */
1581 * of_device_is_system_power_controller - Tells if system-power-controller is found for device_node
1582 * @np: Pointer to the given device_node
1584 * Return: true if present false otherwise
1586 static inline bool of_device_is_system_power_controller(const struct device_node *np)
1588 return of_property_read_bool(np, "system-power-controller");
1595 enum of_overlay_notify_action {
1596 OF_OVERLAY_INIT = 0, /* kzalloc() of ovcs sets this value */
1597 OF_OVERLAY_PRE_APPLY,
1598 OF_OVERLAY_POST_APPLY,
1599 OF_OVERLAY_PRE_REMOVE,
1600 OF_OVERLAY_POST_REMOVE,
1603 static inline const char *of_overlay_action_name(enum of_overlay_notify_action action)
1605 static const char *const of_overlay_action_name[] = {
1613 return of_overlay_action_name[action];
1616 struct of_overlay_notify_data {
1617 struct device_node *overlay;
1618 struct device_node *target;
1621 #ifdef CONFIG_OF_OVERLAY
1623 int of_overlay_fdt_apply(const void *overlay_fdt, u32 overlay_fdt_size,
1625 int of_overlay_remove(int *ovcs_id);
1626 int of_overlay_remove_all(void);
1628 int of_overlay_notifier_register(struct notifier_block *nb);
1629 int of_overlay_notifier_unregister(struct notifier_block *nb);
1633 static inline int of_overlay_fdt_apply(void *overlay_fdt, u32 overlay_fdt_size,
1639 static inline int of_overlay_remove(int *ovcs_id)
1644 static inline int of_overlay_remove_all(void)
1649 static inline int of_overlay_notifier_register(struct notifier_block *nb)
1654 static inline int of_overlay_notifier_unregister(struct notifier_block *nb)
1661 #endif /* _LINUX_OF_H */