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_alias_from_compatible(const struct device_node *node, char *alias,
378 extern void of_print_phandle_args(const char *msg, const struct of_phandle_args *args);
379 extern int __of_parse_phandle_with_args(const struct device_node *np,
380 const char *list_name, const char *cells_name, int cell_count,
381 int index, struct of_phandle_args *out_args);
382 extern int of_parse_phandle_with_args_map(const struct device_node *np,
383 const char *list_name, const char *stem_name, int index,
384 struct of_phandle_args *out_args);
385 extern int of_count_phandle_with_args(const struct device_node *np,
386 const char *list_name, const char *cells_name);
388 /* module functions */
389 extern ssize_t of_modalias(const struct device_node *np, char *str, ssize_t len);
391 /* phandle iterator functions */
392 extern int of_phandle_iterator_init(struct of_phandle_iterator *it,
393 const struct device_node *np,
394 const char *list_name,
395 const char *cells_name,
398 extern int of_phandle_iterator_next(struct of_phandle_iterator *it);
399 extern int of_phandle_iterator_args(struct of_phandle_iterator *it,
403 extern void of_alias_scan(void * (*dt_alloc)(u64 size, u64 align));
404 extern int of_alias_get_id(struct device_node *np, const char *stem);
405 extern int of_alias_get_highest_id(const char *stem);
407 extern int of_machine_is_compatible(const char *compat);
409 extern int of_add_property(struct device_node *np, struct property *prop);
410 extern int of_remove_property(struct device_node *np, struct property *prop);
411 extern int of_update_property(struct device_node *np, struct property *newprop);
413 /* For updating the device tree at runtime */
414 #define OF_RECONFIG_ATTACH_NODE 0x0001
415 #define OF_RECONFIG_DETACH_NODE 0x0002
416 #define OF_RECONFIG_ADD_PROPERTY 0x0003
417 #define OF_RECONFIG_REMOVE_PROPERTY 0x0004
418 #define OF_RECONFIG_UPDATE_PROPERTY 0x0005
420 extern int of_attach_node(struct device_node *);
421 extern int of_detach_node(struct device_node *);
423 #define of_match_ptr(_ptr) (_ptr)
426 * struct property *prop;
430 * of_property_for_each_u32(np, "propname", prop, p, u)
431 * printk("U32 value: %x\n", u);
433 const __be32 *of_prop_next_u32(struct property *prop, const __be32 *cur,
436 * struct property *prop;
439 * of_property_for_each_string(np, "propname", prop, s)
440 * printk("String value: %s\n", s);
442 const char *of_prop_next_string(struct property *prop, const char *cur);
444 bool of_console_check(struct device_node *dn, char *name, int index);
446 extern int of_cpu_node_to_id(struct device_node *np);
448 int of_map_id(struct device_node *np, u32 id,
449 const char *map_name, const char *map_mask_name,
450 struct device_node **target, u32 *id_out);
452 phys_addr_t of_dma_get_max_cpu_address(struct device_node *np);
455 void *of_kexec_alloc_and_setup_fdt(const struct kimage *image,
456 unsigned long initrd_load_addr,
457 unsigned long initrd_len,
458 const char *cmdline, size_t extra_fdt_size);
459 #else /* CONFIG_OF */
461 static inline void of_core_init(void)
465 static inline bool is_of_node(const struct fwnode_handle *fwnode)
470 static inline struct device_node *to_of_node(const struct fwnode_handle *fwnode)
475 static inline bool of_node_name_eq(const struct device_node *np, const char *name)
480 static inline bool of_node_name_prefix(const struct device_node *np, const char *prefix)
485 static inline const char* of_node_full_name(const struct device_node *np)
490 static inline struct device_node *of_find_node_by_name(struct device_node *from,
496 static inline struct device_node *of_find_node_by_type(struct device_node *from,
502 static inline struct device_node *of_find_matching_node_and_match(
503 struct device_node *from,
504 const struct of_device_id *matches,
505 const struct of_device_id **match)
510 static inline struct device_node *of_find_node_by_path(const char *path)
515 static inline struct device_node *of_find_node_opts_by_path(const char *path,
521 static inline struct device_node *of_find_node_by_phandle(phandle handle)
526 static inline struct device_node *of_get_parent(const struct device_node *node)
531 static inline struct device_node *of_get_next_parent(struct device_node *node)
536 static inline struct device_node *of_get_next_child(
537 const struct device_node *node, struct device_node *prev)
542 static inline struct device_node *of_get_next_available_child(
543 const struct device_node *node, struct device_node *prev)
548 static inline struct device_node *of_find_node_with_property(
549 struct device_node *from, const char *prop_name)
554 #define of_fwnode_handle(node) NULL
556 static inline bool of_have_populated_dt(void)
561 static inline struct device_node *of_get_compatible_child(const struct device_node *parent,
562 const char *compatible)
567 static inline struct device_node *of_get_child_by_name(
568 const struct device_node *node,
574 static inline int of_device_is_compatible(const struct device_node *device,
580 static inline int of_device_compatible_match(const struct device_node *device,
581 const char *const *compat)
586 static inline bool of_device_is_available(const struct device_node *device)
591 static inline bool of_device_is_big_endian(const struct device_node *device)
596 static inline struct property *of_find_property(const struct device_node *np,
603 static inline struct device_node *of_find_compatible_node(
604 struct device_node *from,
611 static inline int of_property_count_elems_of_size(const struct device_node *np,
612 const char *propname, int elem_size)
617 static inline int of_property_read_u32_index(const struct device_node *np,
618 const char *propname, u32 index, u32 *out_value)
623 static inline int of_property_read_u64_index(const struct device_node *np,
624 const char *propname, u32 index, u64 *out_value)
629 static inline const void *of_get_property(const struct device_node *node,
636 static inline struct device_node *of_get_cpu_node(int cpu,
637 unsigned int *thread)
642 static inline struct device_node *of_get_next_cpu_node(struct device_node *prev)
647 static inline struct device_node *of_get_cpu_state_node(struct device_node *cpu_node,
653 static inline int of_n_addr_cells(struct device_node *np)
658 static inline int of_n_size_cells(struct device_node *np)
663 static inline int of_property_read_variable_u8_array(const struct device_node *np,
664 const char *propname, u8 *out_values,
665 size_t sz_min, size_t sz_max)
670 static inline int of_property_read_variable_u16_array(const struct device_node *np,
671 const char *propname, u16 *out_values,
672 size_t sz_min, size_t sz_max)
677 static inline int of_property_read_variable_u32_array(const struct device_node *np,
678 const char *propname,
686 static inline int of_property_read_u64(const struct device_node *np,
687 const char *propname, u64 *out_value)
692 static inline int of_property_read_variable_u64_array(const struct device_node *np,
693 const char *propname,
701 static inline int of_property_read_string(const struct device_node *np,
702 const char *propname,
703 const char **out_string)
708 static inline int of_property_match_string(const struct device_node *np,
709 const char *propname,
715 static inline int of_property_read_string_helper(const struct device_node *np,
716 const char *propname,
717 const char **out_strs, size_t sz, int index)
722 static inline int __of_parse_phandle_with_args(const struct device_node *np,
723 const char *list_name,
724 const char *cells_name,
727 struct of_phandle_args *out_args)
732 static inline int of_parse_phandle_with_args_map(const struct device_node *np,
733 const char *list_name,
734 const char *stem_name,
736 struct of_phandle_args *out_args)
741 static inline int of_count_phandle_with_args(const struct device_node *np,
742 const char *list_name,
743 const char *cells_name)
748 static inline ssize_t of_modalias(const struct device_node *np, char *str,
754 static inline int of_phandle_iterator_init(struct of_phandle_iterator *it,
755 const struct device_node *np,
756 const char *list_name,
757 const char *cells_name,
763 static inline int of_phandle_iterator_next(struct of_phandle_iterator *it)
768 static inline int of_phandle_iterator_args(struct of_phandle_iterator *it,
775 static inline int of_alias_get_id(struct device_node *np, const char *stem)
780 static inline int of_alias_get_highest_id(const char *stem)
785 static inline int of_machine_is_compatible(const char *compat)
790 static inline int of_add_property(struct device_node *np, struct property *prop)
795 static inline int of_remove_property(struct device_node *np, struct property *prop)
800 static inline bool of_console_check(const struct device_node *dn, const char *name, int index)
805 static inline const __be32 *of_prop_next_u32(struct property *prop,
806 const __be32 *cur, u32 *pu)
811 static inline const char *of_prop_next_string(struct property *prop,
817 static inline int of_node_check_flag(struct device_node *n, unsigned long flag)
822 static inline int of_node_test_and_set_flag(struct device_node *n,
828 static inline void of_node_set_flag(struct device_node *n, unsigned long flag)
832 static inline void of_node_clear_flag(struct device_node *n, unsigned long flag)
836 static inline int of_property_check_flag(const struct property *p,
842 static inline void of_property_set_flag(struct property *p, unsigned long flag)
846 static inline void of_property_clear_flag(struct property *p, unsigned long flag)
850 static inline int of_cpu_node_to_id(struct device_node *np)
855 static inline int of_map_id(struct device_node *np, u32 id,
856 const char *map_name, const char *map_mask_name,
857 struct device_node **target, u32 *id_out)
862 static inline phys_addr_t of_dma_get_max_cpu_address(struct device_node *np)
864 return PHYS_ADDR_MAX;
867 #define of_match_ptr(_ptr) NULL
868 #define of_match_node(_matches, _node) NULL
869 #endif /* CONFIG_OF */
871 /* Default string compare functions, Allow arch asm/prom.h to override */
872 #if !defined(of_compat_cmp)
873 #define of_compat_cmp(s1, s2, l) strcasecmp((s1), (s2))
874 #define of_prop_cmp(s1, s2) strcmp((s1), (s2))
875 #define of_node_cmp(s1, s2) strcasecmp((s1), (s2))
878 static inline int of_prop_val_eq(struct property *p1, struct property *p2)
880 return p1->length == p2->length &&
881 !memcmp(p1->value, p2->value, (size_t)p1->length);
884 #if defined(CONFIG_OF) && defined(CONFIG_NUMA)
885 extern int of_node_to_nid(struct device_node *np);
887 static inline int of_node_to_nid(struct device_node *device)
893 #ifdef CONFIG_OF_NUMA
894 extern int of_numa_init(void);
896 static inline int of_numa_init(void)
902 static inline struct device_node *of_find_matching_node(
903 struct device_node *from,
904 const struct of_device_id *matches)
906 return of_find_matching_node_and_match(from, matches, NULL);
909 static inline const char *of_node_get_device_type(const struct device_node *np)
911 return of_get_property(np, "device_type", NULL);
914 static inline bool of_node_is_type(const struct device_node *np, const char *type)
916 const char *match = of_node_get_device_type(np);
918 return np && match && type && !strcmp(match, type);
922 * of_parse_phandle - Resolve a phandle property to a device_node pointer
923 * @np: Pointer to device node holding phandle property
924 * @phandle_name: Name of property holding a phandle value
925 * @index: For properties holding a table of phandles, this is the index into
928 * Return: The device_node pointer with refcount incremented. Use
929 * of_node_put() on it when done.
931 static inline struct device_node *of_parse_phandle(const struct device_node *np,
932 const char *phandle_name,
935 struct of_phandle_args args;
937 if (__of_parse_phandle_with_args(np, phandle_name, NULL, 0,
945 * of_parse_phandle_with_args() - Find a node pointed by phandle in a list
946 * @np: pointer to a device tree node containing a list
947 * @list_name: property name that contains a list
948 * @cells_name: property name that specifies phandles' arguments count
949 * @index: index of a phandle to parse out
950 * @out_args: optional pointer to output arguments structure (will be filled)
952 * This function is useful to parse lists of phandles and their arguments.
953 * Returns 0 on success and fills out_args, on error returns appropriate
956 * Caller is responsible to call of_node_put() on the returned out_args->np
970 * list = <&phandle1 1 2 &phandle2 3>;
973 * To get a device_node of the ``node2`` node you may call this:
974 * of_parse_phandle_with_args(node3, "list", "#list-cells", 1, &args);
976 static inline int of_parse_phandle_with_args(const struct device_node *np,
977 const char *list_name,
978 const char *cells_name,
980 struct of_phandle_args *out_args)
984 /* If cells_name is NULL we assume a cell count of 0 */
988 return __of_parse_phandle_with_args(np, list_name, cells_name,
989 cell_count, index, out_args);
993 * of_parse_phandle_with_fixed_args() - Find a node pointed by phandle in a list
994 * @np: pointer to a device tree node containing a list
995 * @list_name: property name that contains a list
996 * @cell_count: number of argument cells following the phandle
997 * @index: index of a phandle to parse out
998 * @out_args: optional pointer to output arguments structure (will be filled)
1000 * This function is useful to parse lists of phandles and their arguments.
1001 * Returns 0 on success and fills out_args, on error returns appropriate
1004 * Caller is responsible to call of_node_put() on the returned out_args->np
1016 * list = <&phandle1 0 2 &phandle2 2 3>;
1019 * To get a device_node of the ``node2`` node you may call this:
1020 * of_parse_phandle_with_fixed_args(node3, "list", 2, 1, &args);
1022 static inline int of_parse_phandle_with_fixed_args(const struct device_node *np,
1023 const char *list_name,
1026 struct of_phandle_args *out_args)
1028 return __of_parse_phandle_with_args(np, list_name, NULL, cell_count,
1033 * of_parse_phandle_with_optional_args() - Find a node pointed by phandle in a list
1034 * @np: pointer to a device tree node containing a list
1035 * @list_name: property name that contains a list
1036 * @cells_name: property name that specifies phandles' arguments count
1037 * @index: index of a phandle to parse out
1038 * @out_args: optional pointer to output arguments structure (will be filled)
1040 * Same as of_parse_phandle_with_args() except that if the cells_name property
1041 * is not found, cell_count of 0 is assumed.
1043 * This is used to useful, if you have a phandle which didn't have arguments
1044 * before and thus doesn't have a '#*-cells' property but is now migrated to
1045 * having arguments while retaining backwards compatibility.
1047 static inline int of_parse_phandle_with_optional_args(const struct device_node *np,
1048 const char *list_name,
1049 const char *cells_name,
1051 struct of_phandle_args *out_args)
1053 return __of_parse_phandle_with_args(np, list_name, cells_name,
1054 0, index, out_args);
1058 * of_property_count_u8_elems - Count the number of u8 elements in a property
1060 * @np: device node from which the property value is to be read.
1061 * @propname: name of the property to be searched.
1063 * Search for a property in a device node and count the number of u8 elements
1066 * Return: The number of elements on sucess, -EINVAL if the property does
1067 * not exist or its length does not match a multiple of u8 and -ENODATA if the
1068 * property does not have a value.
1070 static inline int of_property_count_u8_elems(const struct device_node *np,
1071 const char *propname)
1073 return of_property_count_elems_of_size(np, propname, sizeof(u8));
1077 * of_property_count_u16_elems - Count the number of u16 elements in a property
1079 * @np: device node from which the property value is to be read.
1080 * @propname: name of the property to be searched.
1082 * Search for a property in a device node and count the number of u16 elements
1085 * Return: The number of elements on sucess, -EINVAL if the property does
1086 * not exist or its length does not match a multiple of u16 and -ENODATA if the
1087 * property does not have a value.
1089 static inline int of_property_count_u16_elems(const struct device_node *np,
1090 const char *propname)
1092 return of_property_count_elems_of_size(np, propname, sizeof(u16));
1096 * of_property_count_u32_elems - Count the number of u32 elements in a property
1098 * @np: device node from which the property value is to be read.
1099 * @propname: name of the property to be searched.
1101 * Search for a property in a device node and count the number of u32 elements
1104 * Return: The number of elements on sucess, -EINVAL if the property does
1105 * not exist or its length does not match a multiple of u32 and -ENODATA if the
1106 * property does not have a value.
1108 static inline int of_property_count_u32_elems(const struct device_node *np,
1109 const char *propname)
1111 return of_property_count_elems_of_size(np, propname, sizeof(u32));
1115 * of_property_count_u64_elems - Count the number of u64 elements in a property
1117 * @np: device node from which the property value is to be read.
1118 * @propname: name of the property to be searched.
1120 * Search for a property in a device node and count the number of u64 elements
1123 * Return: The number of elements on sucess, -EINVAL if the property does
1124 * not exist or its length does not match a multiple of u64 and -ENODATA if the
1125 * property does not have a value.
1127 static inline int of_property_count_u64_elems(const struct device_node *np,
1128 const char *propname)
1130 return of_property_count_elems_of_size(np, propname, sizeof(u64));
1134 * of_property_read_string_array() - Read an array of strings from a multiple
1136 * @np: device node from which the property value is to be read.
1137 * @propname: name of the property to be searched.
1138 * @out_strs: output array of string pointers.
1139 * @sz: number of array elements to read.
1141 * Search for a property in a device tree node and retrieve a list of
1142 * terminated string values (pointer to data, not a copy) in that property.
1144 * Return: If @out_strs is NULL, the number of strings in the property is returned.
1146 static inline int of_property_read_string_array(const struct device_node *np,
1147 const char *propname, const char **out_strs,
1150 return of_property_read_string_helper(np, propname, out_strs, sz, 0);
1154 * of_property_count_strings() - Find and return the number of strings from a
1155 * multiple strings property.
1156 * @np: device node from which the property value is to be read.
1157 * @propname: name of the property to be searched.
1159 * Search for a property in a device tree node and retrieve the number of null
1160 * terminated string contain in it.
1162 * Return: The number of strings on success, -EINVAL if the property does not
1163 * exist, -ENODATA if property does not have a value, and -EILSEQ if the string
1164 * is not null-terminated within the length of the property data.
1166 static inline int of_property_count_strings(const struct device_node *np,
1167 const char *propname)
1169 return of_property_read_string_helper(np, propname, NULL, 0, 0);
1173 * of_property_read_string_index() - Find and read a string from a multiple
1175 * @np: device node from which the property value is to be read.
1176 * @propname: name of the property to be searched.
1177 * @index: index of the string in the list of strings
1178 * @output: pointer to null terminated return string, modified only if
1179 * return value is 0.
1181 * Search for a property in a device tree node and retrieve a null
1182 * terminated string value (pointer to data, not a copy) in the list of strings
1183 * contained in that property.
1185 * Return: 0 on success, -EINVAL if the property does not exist, -ENODATA if
1186 * property does not have a value, and -EILSEQ if the string is not
1187 * null-terminated within the length of the property data.
1189 * The out_string pointer is modified only if a valid string can be decoded.
1191 static inline int of_property_read_string_index(const struct device_node *np,
1192 const char *propname,
1193 int index, const char **output)
1195 int rc = of_property_read_string_helper(np, propname, output, 1, index);
1196 return rc < 0 ? rc : 0;
1200 * of_property_read_bool - Find a property
1201 * @np: device node from which the property value is to be read.
1202 * @propname: name of the property to be searched.
1204 * Search for a boolean property in a device node. Usage on non-boolean
1205 * property types is deprecated.
1207 * Return: true if the property exists false otherwise.
1209 static inline bool of_property_read_bool(const struct device_node *np,
1210 const char *propname)
1212 struct property *prop = of_find_property(np, propname, NULL);
1214 return prop ? true : false;
1218 * of_property_present - Test if a property is present in a node
1219 * @np: device node to search for the property.
1220 * @propname: name of the property to be searched.
1222 * Test for a property present in a device node.
1224 * Return: true if the property exists false otherwise.
1226 static inline bool of_property_present(const struct device_node *np, const char *propname)
1228 return of_property_read_bool(np, propname);
1232 * of_property_read_u8_array - Find and read an array of u8 from a property.
1234 * @np: device node from which the property value is to be read.
1235 * @propname: name of the property to be searched.
1236 * @out_values: pointer to return value, modified only if return value is 0.
1237 * @sz: number of array elements to read
1239 * Search for a property in a device node and read 8-bit value(s) from
1242 * dts entry of array should be like:
1243 * ``property = /bits/ 8 <0x50 0x60 0x70>;``
1245 * Return: 0 on success, -EINVAL if the property does not exist,
1246 * -ENODATA if property does not have a value, and -EOVERFLOW if the
1247 * property data isn't large enough.
1249 * The out_values is modified only if a valid u8 value can be decoded.
1251 static inline int of_property_read_u8_array(const struct device_node *np,
1252 const char *propname,
1253 u8 *out_values, size_t sz)
1255 int ret = of_property_read_variable_u8_array(np, propname, out_values,
1264 * of_property_read_u16_array - Find and read an array of u16 from a property.
1266 * @np: device node from which the property value is to be read.
1267 * @propname: name of the property to be searched.
1268 * @out_values: pointer to return value, modified only if return value is 0.
1269 * @sz: number of array elements to read
1271 * Search for a property in a device node and read 16-bit value(s) from
1274 * dts entry of array should be like:
1275 * ``property = /bits/ 16 <0x5000 0x6000 0x7000>;``
1277 * Return: 0 on success, -EINVAL if the property does not exist,
1278 * -ENODATA if property does not have a value, and -EOVERFLOW if the
1279 * property data isn't large enough.
1281 * The out_values is modified only if a valid u16 value can be decoded.
1283 static inline int of_property_read_u16_array(const struct device_node *np,
1284 const char *propname,
1285 u16 *out_values, size_t sz)
1287 int ret = of_property_read_variable_u16_array(np, propname, out_values,
1296 * of_property_read_u32_array - Find and read an array of 32 bit integers
1299 * @np: device node from which the property value is to be read.
1300 * @propname: name of the property to be searched.
1301 * @out_values: pointer to return value, modified only if return value is 0.
1302 * @sz: number of array elements to read
1304 * Search for a property in a device node and read 32-bit value(s) from
1307 * Return: 0 on success, -EINVAL if the property does not exist,
1308 * -ENODATA if property does not have a value, and -EOVERFLOW if the
1309 * property data isn't large enough.
1311 * The out_values is modified only if a valid u32 value can be decoded.
1313 static inline int of_property_read_u32_array(const struct device_node *np,
1314 const char *propname,
1315 u32 *out_values, size_t sz)
1317 int ret = of_property_read_variable_u32_array(np, propname, out_values,
1326 * of_property_read_u64_array - Find and read an array of 64 bit integers
1329 * @np: device node from which the property value is to be read.
1330 * @propname: name of the property to be searched.
1331 * @out_values: pointer to return value, modified only if return value is 0.
1332 * @sz: number of array elements to read
1334 * Search for a property in a device node and read 64-bit value(s) from
1337 * Return: 0 on success, -EINVAL if the property does not exist,
1338 * -ENODATA if property does not have a value, and -EOVERFLOW if the
1339 * property data isn't large enough.
1341 * The out_values is modified only if a valid u64 value can be decoded.
1343 static inline int of_property_read_u64_array(const struct device_node *np,
1344 const char *propname,
1345 u64 *out_values, size_t sz)
1347 int ret = of_property_read_variable_u64_array(np, propname, out_values,
1355 static inline int of_property_read_u8(const struct device_node *np,
1356 const char *propname,
1359 return of_property_read_u8_array(np, propname, out_value, 1);
1362 static inline int of_property_read_u16(const struct device_node *np,
1363 const char *propname,
1366 return of_property_read_u16_array(np, propname, out_value, 1);
1369 static inline int of_property_read_u32(const struct device_node *np,
1370 const char *propname,
1373 return of_property_read_u32_array(np, propname, out_value, 1);
1376 static inline int of_property_read_s32(const struct device_node *np,
1377 const char *propname,
1380 return of_property_read_u32(np, propname, (u32*) out_value);
1383 #define of_for_each_phandle(it, err, np, ln, cn, cc) \
1384 for (of_phandle_iterator_init((it), (np), (ln), (cn), (cc)), \
1385 err = of_phandle_iterator_next(it); \
1387 err = of_phandle_iterator_next(it))
1389 #define of_property_for_each_u32(np, propname, prop, p, u) \
1390 for (prop = of_find_property(np, propname, NULL), \
1391 p = of_prop_next_u32(prop, NULL, &u); \
1393 p = of_prop_next_u32(prop, p, &u))
1395 #define of_property_for_each_string(np, propname, prop, s) \
1396 for (prop = of_find_property(np, propname, NULL), \
1397 s = of_prop_next_string(prop, NULL); \
1399 s = of_prop_next_string(prop, s))
1401 #define for_each_node_by_name(dn, name) \
1402 for (dn = of_find_node_by_name(NULL, name); dn; \
1403 dn = of_find_node_by_name(dn, name))
1404 #define for_each_node_by_type(dn, type) \
1405 for (dn = of_find_node_by_type(NULL, type); dn; \
1406 dn = of_find_node_by_type(dn, type))
1407 #define for_each_compatible_node(dn, type, compatible) \
1408 for (dn = of_find_compatible_node(NULL, type, compatible); dn; \
1409 dn = of_find_compatible_node(dn, type, compatible))
1410 #define for_each_matching_node(dn, matches) \
1411 for (dn = of_find_matching_node(NULL, matches); dn; \
1412 dn = of_find_matching_node(dn, matches))
1413 #define for_each_matching_node_and_match(dn, matches, match) \
1414 for (dn = of_find_matching_node_and_match(NULL, matches, match); \
1415 dn; dn = of_find_matching_node_and_match(dn, matches, match))
1417 #define for_each_child_of_node(parent, child) \
1418 for (child = of_get_next_child(parent, NULL); child != NULL; \
1419 child = of_get_next_child(parent, child))
1420 #define for_each_available_child_of_node(parent, child) \
1421 for (child = of_get_next_available_child(parent, NULL); child != NULL; \
1422 child = of_get_next_available_child(parent, child))
1424 #define for_each_of_cpu_node(cpu) \
1425 for (cpu = of_get_next_cpu_node(NULL); cpu != NULL; \
1426 cpu = of_get_next_cpu_node(cpu))
1428 #define for_each_node_with_property(dn, prop_name) \
1429 for (dn = of_find_node_with_property(NULL, prop_name); dn; \
1430 dn = of_find_node_with_property(dn, prop_name))
1432 static inline int of_get_child_count(const struct device_node *np)
1434 struct device_node *child;
1437 for_each_child_of_node(np, child)
1443 static inline int of_get_available_child_count(const struct device_node *np)
1445 struct device_node *child;
1448 for_each_available_child_of_node(np, child)
1454 #define _OF_DECLARE_STUB(table, name, compat, fn, fn_type) \
1455 static const struct of_device_id __of_table_##name \
1456 __attribute__((unused)) \
1457 = { .compatible = compat, \
1458 .data = (fn == (fn_type)NULL) ? fn : fn }
1460 #if defined(CONFIG_OF) && !defined(MODULE)
1461 #define _OF_DECLARE(table, name, compat, fn, fn_type) \
1462 static const struct of_device_id __of_table_##name \
1463 __used __section("__" #table "_of_table") \
1464 __aligned(__alignof__(struct of_device_id)) \
1465 = { .compatible = compat, \
1466 .data = (fn == (fn_type)NULL) ? fn : fn }
1468 #define _OF_DECLARE(table, name, compat, fn, fn_type) \
1469 _OF_DECLARE_STUB(table, name, compat, fn, fn_type)
1472 typedef int (*of_init_fn_2)(struct device_node *, struct device_node *);
1473 typedef int (*of_init_fn_1_ret)(struct device_node *);
1474 typedef void (*of_init_fn_1)(struct device_node *);
1476 #define OF_DECLARE_1(table, name, compat, fn) \
1477 _OF_DECLARE(table, name, compat, fn, of_init_fn_1)
1478 #define OF_DECLARE_1_RET(table, name, compat, fn) \
1479 _OF_DECLARE(table, name, compat, fn, of_init_fn_1_ret)
1480 #define OF_DECLARE_2(table, name, compat, fn) \
1481 _OF_DECLARE(table, name, compat, fn, of_init_fn_2)
1484 * struct of_changeset_entry - Holds a changeset entry
1486 * @node: list_head for the log list
1487 * @action: notifier action
1488 * @np: pointer to the device node affected
1489 * @prop: pointer to the property affected
1490 * @old_prop: hold a pointer to the original property
1492 * Every modification of the device tree during a changeset
1493 * is held in a list of of_changeset_entry structures.
1494 * That way we can recover from a partial application, or we can
1495 * revert the changeset
1497 struct of_changeset_entry {
1498 struct list_head node;
1499 unsigned long action;
1500 struct device_node *np;
1501 struct property *prop;
1502 struct property *old_prop;
1506 * struct of_changeset - changeset tracker structure
1508 * @entries: list_head for the changeset entries
1510 * changesets are a convenient way to apply bulk changes to the
1511 * live tree. In case of an error, changes are rolled-back.
1512 * changesets live on after initial application, and if not
1513 * destroyed after use, they can be reverted in one single call.
1515 struct of_changeset {
1516 struct list_head entries;
1519 enum of_reconfig_change {
1520 OF_RECONFIG_NO_CHANGE = 0,
1521 OF_RECONFIG_CHANGE_ADD,
1522 OF_RECONFIG_CHANGE_REMOVE,
1525 #ifdef CONFIG_OF_DYNAMIC
1526 extern int of_reconfig_notifier_register(struct notifier_block *);
1527 extern int of_reconfig_notifier_unregister(struct notifier_block *);
1528 extern int of_reconfig_notify(unsigned long, struct of_reconfig_data *rd);
1529 extern int of_reconfig_get_state_change(unsigned long action,
1530 struct of_reconfig_data *arg);
1532 extern void of_changeset_init(struct of_changeset *ocs);
1533 extern void of_changeset_destroy(struct of_changeset *ocs);
1534 extern int of_changeset_apply(struct of_changeset *ocs);
1535 extern int of_changeset_revert(struct of_changeset *ocs);
1536 extern int of_changeset_action(struct of_changeset *ocs,
1537 unsigned long action, struct device_node *np,
1538 struct property *prop);
1540 static inline int of_changeset_attach_node(struct of_changeset *ocs,
1541 struct device_node *np)
1543 return of_changeset_action(ocs, OF_RECONFIG_ATTACH_NODE, np, NULL);
1546 static inline int of_changeset_detach_node(struct of_changeset *ocs,
1547 struct device_node *np)
1549 return of_changeset_action(ocs, OF_RECONFIG_DETACH_NODE, np, NULL);
1552 static inline int of_changeset_add_property(struct of_changeset *ocs,
1553 struct device_node *np, struct property *prop)
1555 return of_changeset_action(ocs, OF_RECONFIG_ADD_PROPERTY, np, prop);
1558 static inline int of_changeset_remove_property(struct of_changeset *ocs,
1559 struct device_node *np, struct property *prop)
1561 return of_changeset_action(ocs, OF_RECONFIG_REMOVE_PROPERTY, np, prop);
1564 static inline int of_changeset_update_property(struct of_changeset *ocs,
1565 struct device_node *np, struct property *prop)
1567 return of_changeset_action(ocs, OF_RECONFIG_UPDATE_PROPERTY, np, prop);
1569 #else /* CONFIG_OF_DYNAMIC */
1570 static inline int of_reconfig_notifier_register(struct notifier_block *nb)
1574 static inline int of_reconfig_notifier_unregister(struct notifier_block *nb)
1578 static inline int of_reconfig_notify(unsigned long action,
1579 struct of_reconfig_data *arg)
1583 static inline int of_reconfig_get_state_change(unsigned long action,
1584 struct of_reconfig_data *arg)
1588 #endif /* CONFIG_OF_DYNAMIC */
1591 * of_device_is_system_power_controller - Tells if system-power-controller is found for device_node
1592 * @np: Pointer to the given device_node
1594 * Return: true if present false otherwise
1596 static inline bool of_device_is_system_power_controller(const struct device_node *np)
1598 return of_property_read_bool(np, "system-power-controller");
1605 enum of_overlay_notify_action {
1606 OF_OVERLAY_INIT = 0, /* kzalloc() of ovcs sets this value */
1607 OF_OVERLAY_PRE_APPLY,
1608 OF_OVERLAY_POST_APPLY,
1609 OF_OVERLAY_PRE_REMOVE,
1610 OF_OVERLAY_POST_REMOVE,
1613 static inline const char *of_overlay_action_name(enum of_overlay_notify_action action)
1615 static const char *const of_overlay_action_name[] = {
1623 return of_overlay_action_name[action];
1626 struct of_overlay_notify_data {
1627 struct device_node *overlay;
1628 struct device_node *target;
1631 #ifdef CONFIG_OF_OVERLAY
1633 int of_overlay_fdt_apply(const void *overlay_fdt, u32 overlay_fdt_size,
1635 int of_overlay_remove(int *ovcs_id);
1636 int of_overlay_remove_all(void);
1638 int of_overlay_notifier_register(struct notifier_block *nb);
1639 int of_overlay_notifier_unregister(struct notifier_block *nb);
1643 static inline int of_overlay_fdt_apply(void *overlay_fdt, u32 overlay_fdt_size,
1649 static inline int of_overlay_remove(int *ovcs_id)
1654 static inline int of_overlay_remove_all(void)
1659 static inline int of_overlay_notifier_register(struct notifier_block *nb)
1664 static inline int of_overlay_notifier_unregister(struct notifier_block *nb)
1671 #endif /* _LINUX_OF_H */