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/cleanup.h>
17 #include <linux/errno.h>
18 #include <linux/kobject.h>
19 #include <linux/mod_devicetable.h>
20 #include <linux/property.h>
21 #include <linux/list.h>
23 #include <asm/byteorder.h>
32 struct property *next;
33 #if defined(CONFIG_OF_DYNAMIC) || defined(CONFIG_SPARC)
36 #if defined(CONFIG_OF_PROMTREE)
37 unsigned int unique_id;
39 #if defined(CONFIG_OF_KOBJ)
40 struct bin_attribute attr;
44 #if defined(CONFIG_SPARC)
45 struct of_irq_controller;
51 const char *full_name;
52 struct fwnode_handle fwnode;
54 struct property *properties;
55 struct property *deadprops; /* removed properties */
56 struct device_node *parent;
57 struct device_node *child;
58 struct device_node *sibling;
59 #if defined(CONFIG_OF_KOBJ)
64 #if defined(CONFIG_SPARC)
65 unsigned int unique_id;
66 struct of_irq_controller *irq_trans;
70 #define MAX_PHANDLE_ARGS 16
71 struct of_phandle_args {
72 struct device_node *np;
74 uint32_t args[MAX_PHANDLE_ARGS];
77 struct of_phandle_iterator {
78 /* Common iterator information */
79 const char *cells_name;
81 const struct device_node *parent;
83 /* List size information */
84 const __be32 *list_end;
85 const __be32 *phandle_end;
87 /* Current position state */
91 struct device_node *node;
94 struct of_reconfig_data {
95 struct device_node *dn;
96 struct property *prop;
97 struct property *old_prop;
100 extern const struct kobj_type of_node_ktype;
101 extern const struct fwnode_operations of_fwnode_ops;
104 * of_node_init - initialize a devicetree node
105 * @node: Pointer to device node that has been created by kzalloc()
107 * On return the device_node refcount is set to one. Use of_node_put()
108 * on @node when done to free the memory allocated for it. If the node
109 * is NOT a dynamic node the memory will not be freed. The decision of
110 * whether to free the memory will be done by node->release(), which is
113 static inline void of_node_init(struct device_node *node)
115 #if defined(CONFIG_OF_KOBJ)
116 kobject_init(&node->kobj, &of_node_ktype);
118 fwnode_init(&node->fwnode, &of_fwnode_ops);
121 #if defined(CONFIG_OF_KOBJ)
122 #define of_node_kobj(n) (&(n)->kobj)
124 #define of_node_kobj(n) NULL
127 #ifdef CONFIG_OF_DYNAMIC
128 extern struct device_node *of_node_get(struct device_node *node);
129 extern void of_node_put(struct device_node *node);
130 #else /* CONFIG_OF_DYNAMIC */
131 /* Dummy ref counting routines - to be implemented later */
132 static inline struct device_node *of_node_get(struct device_node *node)
136 static inline void of_node_put(struct device_node *node) { }
137 #endif /* !CONFIG_OF_DYNAMIC */
138 DEFINE_FREE(device_node, struct device_node *, if (_T) of_node_put(_T))
140 /* Pointer for first entry in chain of all nodes. */
141 extern struct device_node *of_root;
142 extern struct device_node *of_chosen;
143 extern struct device_node *of_aliases;
144 extern struct device_node *of_stdout;
147 * struct device_node flag descriptions
148 * (need to be visible even when !CONFIG_OF)
150 #define OF_DYNAMIC 1 /* (and properties) allocated via kmalloc */
151 #define OF_DETACHED 2 /* detached from the device tree */
152 #define OF_POPULATED 3 /* device already created */
153 #define OF_POPULATED_BUS 4 /* platform bus created for children */
154 #define OF_OVERLAY 5 /* allocated for an overlay */
155 #define OF_OVERLAY_FREE_CSET 6 /* in overlay cset being freed */
157 #define OF_BAD_ADDR ((u64)-1)
160 void of_core_init(void);
162 static inline bool is_of_node(const struct fwnode_handle *fwnode)
164 return !IS_ERR_OR_NULL(fwnode) && fwnode->ops == &of_fwnode_ops;
167 #define to_of_node(__fwnode) \
169 typeof(__fwnode) __to_of_node_fwnode = (__fwnode); \
171 is_of_node(__to_of_node_fwnode) ? \
172 container_of(__to_of_node_fwnode, \
173 struct device_node, fwnode) : \
177 #define of_fwnode_handle(node) \
179 typeof(node) __of_fwnode_handle_node = (node); \
181 __of_fwnode_handle_node ? \
182 &__of_fwnode_handle_node->fwnode : 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(const 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(const 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 #define OF_IS_DYNAMIC(x) test_bit(OF_DYNAMIC, &x->_flags)
256 #define OF_MARK_DYNAMIC(x) set_bit(OF_DYNAMIC, &x->_flags)
258 extern bool of_node_name_eq(const struct device_node *np, const char *name);
259 extern bool of_node_name_prefix(const struct device_node *np, const char *prefix);
261 static inline const char *of_node_full_name(const struct device_node *np)
263 return np ? np->full_name : "<no-node>";
266 #define for_each_of_allnodes_from(from, dn) \
267 for (dn = __of_find_all_nodes(from); dn; dn = __of_find_all_nodes(dn))
268 #define for_each_of_allnodes(dn) for_each_of_allnodes_from(NULL, dn)
269 extern struct device_node *of_find_node_by_name(struct device_node *from,
271 extern struct device_node *of_find_node_by_type(struct device_node *from,
273 extern struct device_node *of_find_compatible_node(struct device_node *from,
274 const char *type, const char *compat);
275 extern struct device_node *of_find_matching_node_and_match(
276 struct device_node *from,
277 const struct of_device_id *matches,
278 const struct of_device_id **match);
280 extern struct device_node *of_find_node_opts_by_path(const char *path,
282 static inline struct device_node *of_find_node_by_path(const char *path)
284 return of_find_node_opts_by_path(path, NULL);
287 extern struct device_node *of_find_node_by_phandle(phandle handle);
288 extern struct device_node *of_get_parent(const struct device_node *node);
289 extern struct device_node *of_get_next_parent(struct device_node *node);
290 extern struct device_node *of_get_next_child(const struct device_node *node,
291 struct device_node *prev);
292 extern struct device_node *of_get_next_available_child(
293 const struct device_node *node, struct device_node *prev);
294 extern struct device_node *of_get_next_reserved_child(
295 const struct device_node *node, struct device_node *prev);
297 extern struct device_node *of_get_compatible_child(const struct device_node *parent,
298 const char *compatible);
299 extern struct device_node *of_get_child_by_name(const struct device_node *node,
303 extern struct device_node *of_find_next_cache_node(const struct device_node *);
304 extern int of_find_last_cache_level(unsigned int cpu);
305 extern struct device_node *of_find_node_with_property(
306 struct device_node *from, const char *prop_name);
308 extern struct property *of_find_property(const struct device_node *np,
311 extern int of_property_count_elems_of_size(const struct device_node *np,
312 const char *propname, int elem_size);
313 extern int of_property_read_u32_index(const struct device_node *np,
314 const char *propname,
315 u32 index, u32 *out_value);
316 extern int of_property_read_u64_index(const struct device_node *np,
317 const char *propname,
318 u32 index, u64 *out_value);
319 extern int of_property_read_variable_u8_array(const struct device_node *np,
320 const char *propname, u8 *out_values,
321 size_t sz_min, size_t sz_max);
322 extern int of_property_read_variable_u16_array(const struct device_node *np,
323 const char *propname, u16 *out_values,
324 size_t sz_min, size_t sz_max);
325 extern int of_property_read_variable_u32_array(const struct device_node *np,
326 const char *propname,
330 extern int of_property_read_u64(const struct device_node *np,
331 const char *propname, u64 *out_value);
332 extern int of_property_read_variable_u64_array(const struct device_node *np,
333 const char *propname,
338 extern int of_property_read_string(const struct device_node *np,
339 const char *propname,
340 const char **out_string);
341 extern int of_property_match_string(const struct device_node *np,
342 const char *propname,
344 extern int of_property_read_string_helper(const struct device_node *np,
345 const char *propname,
346 const char **out_strs, size_t sz, int index);
347 extern int of_device_is_compatible(const struct device_node *device,
349 extern int of_device_compatible_match(const struct device_node *device,
350 const char *const *compat);
351 extern bool of_device_is_available(const struct device_node *device);
352 extern bool of_device_is_big_endian(const struct device_node *device);
353 extern const void *of_get_property(const struct device_node *node,
356 extern struct device_node *of_get_cpu_node(int cpu, unsigned int *thread);
357 extern struct device_node *of_cpu_device_node_get(int cpu);
358 extern int of_cpu_node_to_id(struct device_node *np);
359 extern struct device_node *of_get_next_cpu_node(struct device_node *prev);
360 extern struct device_node *of_get_cpu_state_node(struct device_node *cpu_node,
362 extern u64 of_get_cpu_hwid(struct device_node *cpun, unsigned int thread);
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 const void *of_device_get_match_data(const struct device *dev);
369 extern int of_alias_from_compatible(const struct device_node *node, char *alias,
371 extern void of_print_phandle_args(const char *msg, const struct of_phandle_args *args);
372 extern int __of_parse_phandle_with_args(const struct device_node *np,
373 const char *list_name, const char *cells_name, int cell_count,
374 int index, struct of_phandle_args *out_args);
375 extern int of_parse_phandle_with_args_map(const struct device_node *np,
376 const char *list_name, const char *stem_name, int index,
377 struct of_phandle_args *out_args);
378 extern int of_count_phandle_with_args(const struct device_node *np,
379 const char *list_name, const char *cells_name);
381 /* module functions */
382 extern ssize_t of_modalias(const struct device_node *np, char *str, ssize_t len);
383 extern int of_request_module(const struct device_node *np);
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 bool of_machine_compatible_match(const char *const *compats);
404 * of_machine_is_compatible - Test root of device tree for a given compatible value
405 * @compat: compatible string to look for in root node's compatible property.
407 * Return: true if the root node has the given value in its compatible property.
409 static inline bool of_machine_is_compatible(const char *compat)
411 const char *compats[] = { compat, NULL };
413 return of_machine_compatible_match(compats);
416 extern int of_add_property(struct device_node *np, struct property *prop);
417 extern int of_remove_property(struct device_node *np, struct property *prop);
418 extern int of_update_property(struct device_node *np, struct property *newprop);
420 /* For updating the device tree at runtime */
421 #define OF_RECONFIG_ATTACH_NODE 0x0001
422 #define OF_RECONFIG_DETACH_NODE 0x0002
423 #define OF_RECONFIG_ADD_PROPERTY 0x0003
424 #define OF_RECONFIG_REMOVE_PROPERTY 0x0004
425 #define OF_RECONFIG_UPDATE_PROPERTY 0x0005
427 extern int of_attach_node(struct device_node *);
428 extern int of_detach_node(struct device_node *);
430 #define of_match_ptr(_ptr) (_ptr)
433 * struct property *prop;
437 * of_property_for_each_u32(np, "propname", prop, p, u)
438 * printk("U32 value: %x\n", u);
440 const __be32 *of_prop_next_u32(struct property *prop, const __be32 *cur,
443 * struct property *prop;
446 * of_property_for_each_string(np, "propname", prop, s)
447 * printk("String value: %s\n", s);
449 const char *of_prop_next_string(struct property *prop, const char *cur);
451 bool of_console_check(struct device_node *dn, char *name, int index);
453 int of_map_id(struct device_node *np, u32 id,
454 const char *map_name, const char *map_mask_name,
455 struct device_node **target, u32 *id_out);
457 phys_addr_t of_dma_get_max_cpu_address(struct device_node *np);
460 void *of_kexec_alloc_and_setup_fdt(const struct kimage *image,
461 unsigned long initrd_load_addr,
462 unsigned long initrd_len,
463 const char *cmdline, size_t extra_fdt_size);
464 #else /* CONFIG_OF */
466 static inline void of_core_init(void)
470 static inline bool is_of_node(const struct fwnode_handle *fwnode)
475 static inline struct device_node *to_of_node(const struct fwnode_handle *fwnode)
480 static inline bool of_node_name_eq(const struct device_node *np, const char *name)
485 static inline bool of_node_name_prefix(const struct device_node *np, const char *prefix)
490 static inline const char* of_node_full_name(const struct device_node *np)
495 static inline struct device_node *of_find_node_by_name(struct device_node *from,
501 static inline struct device_node *of_find_node_by_type(struct device_node *from,
507 static inline struct device_node *of_find_matching_node_and_match(
508 struct device_node *from,
509 const struct of_device_id *matches,
510 const struct of_device_id **match)
515 static inline struct device_node *of_find_node_by_path(const char *path)
520 static inline struct device_node *of_find_node_opts_by_path(const char *path,
526 static inline struct device_node *of_find_node_by_phandle(phandle handle)
531 static inline struct device_node *of_get_parent(const struct device_node *node)
536 static inline struct device_node *of_get_next_parent(struct device_node *node)
541 static inline struct device_node *of_get_next_child(
542 const struct device_node *node, struct device_node *prev)
547 static inline struct device_node *of_get_next_available_child(
548 const struct device_node *node, struct device_node *prev)
553 static inline struct device_node *of_get_next_reserved_child(
554 const struct device_node *node, struct device_node *prev)
559 static inline struct device_node *of_find_node_with_property(
560 struct device_node *from, const char *prop_name)
565 #define of_fwnode_handle(node) NULL
567 static inline struct device_node *of_get_compatible_child(const struct device_node *parent,
568 const char *compatible)
573 static inline struct device_node *of_get_child_by_name(
574 const struct device_node *node,
580 static inline int of_device_is_compatible(const struct device_node *device,
586 static inline int of_device_compatible_match(const struct device_node *device,
587 const char *const *compat)
592 static inline bool of_device_is_available(const struct device_node *device)
597 static inline bool of_device_is_big_endian(const struct device_node *device)
602 static inline struct property *of_find_property(const struct device_node *np,
609 static inline struct device_node *of_find_compatible_node(
610 struct device_node *from,
617 static inline int of_property_count_elems_of_size(const struct device_node *np,
618 const char *propname, int elem_size)
623 static inline int of_property_read_u32_index(const struct device_node *np,
624 const char *propname, u32 index, u32 *out_value)
629 static inline int of_property_read_u64_index(const struct device_node *np,
630 const char *propname, u32 index, u64 *out_value)
635 static inline const void *of_get_property(const struct device_node *node,
642 static inline struct device_node *of_get_cpu_node(int cpu,
643 unsigned int *thread)
648 static inline struct device_node *of_cpu_device_node_get(int cpu)
653 static inline int of_cpu_node_to_id(struct device_node *np)
658 static inline struct device_node *of_get_next_cpu_node(struct device_node *prev)
663 static inline struct device_node *of_get_cpu_state_node(struct device_node *cpu_node,
669 static inline int of_n_addr_cells(struct device_node *np)
674 static inline int of_n_size_cells(struct device_node *np)
679 static inline int of_property_read_variable_u8_array(const struct device_node *np,
680 const char *propname, u8 *out_values,
681 size_t sz_min, size_t sz_max)
686 static inline int of_property_read_variable_u16_array(const struct device_node *np,
687 const char *propname, u16 *out_values,
688 size_t sz_min, size_t sz_max)
693 static inline int of_property_read_variable_u32_array(const struct device_node *np,
694 const char *propname,
702 static inline int of_property_read_u64(const struct device_node *np,
703 const char *propname, u64 *out_value)
708 static inline int of_property_read_variable_u64_array(const struct device_node *np,
709 const char *propname,
717 static inline int of_property_read_string(const struct device_node *np,
718 const char *propname,
719 const char **out_string)
724 static inline int of_property_match_string(const struct device_node *np,
725 const char *propname,
731 static inline int of_property_read_string_helper(const struct device_node *np,
732 const char *propname,
733 const char **out_strs, size_t sz, int index)
738 static inline int __of_parse_phandle_with_args(const struct device_node *np,
739 const char *list_name,
740 const char *cells_name,
743 struct of_phandle_args *out_args)
748 static inline int of_parse_phandle_with_args_map(const struct device_node *np,
749 const char *list_name,
750 const char *stem_name,
752 struct of_phandle_args *out_args)
757 static inline int of_count_phandle_with_args(const struct device_node *np,
758 const char *list_name,
759 const char *cells_name)
764 static inline ssize_t of_modalias(const struct device_node *np, char *str,
770 static inline int of_request_module(const struct device_node *np)
775 static inline int of_phandle_iterator_init(struct of_phandle_iterator *it,
776 const struct device_node *np,
777 const char *list_name,
778 const char *cells_name,
784 static inline int of_phandle_iterator_next(struct of_phandle_iterator *it)
789 static inline int of_phandle_iterator_args(struct of_phandle_iterator *it,
796 static inline int of_alias_get_id(struct device_node *np, const char *stem)
801 static inline int of_alias_get_highest_id(const char *stem)
806 static inline int of_machine_is_compatible(const char *compat)
811 static inline int of_add_property(struct device_node *np, struct property *prop)
816 static inline int of_remove_property(struct device_node *np, struct property *prop)
821 static inline bool of_machine_compatible_match(const char *const *compats)
826 static inline bool of_console_check(const struct device_node *dn, const char *name, int index)
831 static inline const __be32 *of_prop_next_u32(struct property *prop,
832 const __be32 *cur, u32 *pu)
837 static inline const char *of_prop_next_string(struct property *prop,
843 static inline int of_node_check_flag(struct device_node *n, unsigned long flag)
848 static inline int of_node_test_and_set_flag(struct device_node *n,
854 static inline void of_node_set_flag(struct device_node *n, unsigned long flag)
858 static inline void of_node_clear_flag(struct device_node *n, unsigned long flag)
862 static inline int of_property_check_flag(const struct property *p,
868 static inline void of_property_set_flag(struct property *p, unsigned long flag)
872 static inline void of_property_clear_flag(struct property *p, unsigned long flag)
876 static inline int of_map_id(struct device_node *np, u32 id,
877 const char *map_name, const char *map_mask_name,
878 struct device_node **target, u32 *id_out)
883 static inline phys_addr_t of_dma_get_max_cpu_address(struct device_node *np)
885 return PHYS_ADDR_MAX;
888 static inline const void *of_device_get_match_data(const struct device *dev)
893 #define of_match_ptr(_ptr) NULL
894 #define of_match_node(_matches, _node) NULL
895 #endif /* CONFIG_OF */
897 /* Default string compare functions, Allow arch asm/prom.h to override */
898 #if !defined(of_compat_cmp)
899 #define of_compat_cmp(s1, s2, l) strcasecmp((s1), (s2))
900 #define of_prop_cmp(s1, s2) strcmp((s1), (s2))
901 #define of_node_cmp(s1, s2) strcasecmp((s1), (s2))
904 static inline int of_prop_val_eq(struct property *p1, struct property *p2)
906 return p1->length == p2->length &&
907 !memcmp(p1->value, p2->value, (size_t)p1->length);
910 #define for_each_property_of_node(dn, pp) \
911 for (pp = dn->properties; pp != NULL; pp = pp->next)
913 #if defined(CONFIG_OF) && defined(CONFIG_NUMA)
914 extern int of_node_to_nid(struct device_node *np);
916 static inline int of_node_to_nid(struct device_node *device)
922 #ifdef CONFIG_OF_NUMA
923 extern int of_numa_init(void);
925 static inline int of_numa_init(void)
931 static inline struct device_node *of_find_matching_node(
932 struct device_node *from,
933 const struct of_device_id *matches)
935 return of_find_matching_node_and_match(from, matches, NULL);
938 static inline const char *of_node_get_device_type(const struct device_node *np)
940 return of_get_property(np, "device_type", NULL);
943 static inline bool of_node_is_type(const struct device_node *np, const char *type)
945 const char *match = of_node_get_device_type(np);
947 return np && match && type && !strcmp(match, type);
951 * of_parse_phandle - Resolve a phandle property to a device_node pointer
952 * @np: Pointer to device node holding phandle property
953 * @phandle_name: Name of property holding a phandle value
954 * @index: For properties holding a table of phandles, this is the index into
957 * Return: The device_node pointer with refcount incremented. Use
958 * of_node_put() on it when done.
960 static inline struct device_node *of_parse_phandle(const struct device_node *np,
961 const char *phandle_name,
964 struct of_phandle_args args;
966 if (__of_parse_phandle_with_args(np, phandle_name, NULL, 0,
974 * of_parse_phandle_with_args() - Find a node pointed by phandle in a list
975 * @np: pointer to a device tree node containing a list
976 * @list_name: property name that contains a list
977 * @cells_name: property name that specifies phandles' arguments count
978 * @index: index of a phandle to parse out
979 * @out_args: optional pointer to output arguments structure (will be filled)
981 * This function is useful to parse lists of phandles and their arguments.
982 * Returns 0 on success and fills out_args, on error returns appropriate
985 * Caller is responsible to call of_node_put() on the returned out_args->np
999 * list = <&phandle1 1 2 &phandle2 3>;
1002 * To get a device_node of the ``node2`` node you may call this:
1003 * of_parse_phandle_with_args(node3, "list", "#list-cells", 1, &args);
1005 static inline int of_parse_phandle_with_args(const struct device_node *np,
1006 const char *list_name,
1007 const char *cells_name,
1009 struct of_phandle_args *out_args)
1011 int cell_count = -1;
1013 /* If cells_name is NULL we assume a cell count of 0 */
1017 return __of_parse_phandle_with_args(np, list_name, cells_name,
1018 cell_count, index, out_args);
1022 * of_parse_phandle_with_fixed_args() - Find a node pointed by phandle in a list
1023 * @np: pointer to a device tree node containing a list
1024 * @list_name: property name that contains a list
1025 * @cell_count: number of argument cells following the phandle
1026 * @index: index of a phandle to parse out
1027 * @out_args: optional pointer to output arguments structure (will be filled)
1029 * This function is useful to parse lists of phandles and their arguments.
1030 * Returns 0 on success and fills out_args, on error returns appropriate
1033 * Caller is responsible to call of_node_put() on the returned out_args->np
1045 * list = <&phandle1 0 2 &phandle2 2 3>;
1048 * To get a device_node of the ``node2`` node you may call this:
1049 * of_parse_phandle_with_fixed_args(node3, "list", 2, 1, &args);
1051 static inline int of_parse_phandle_with_fixed_args(const struct device_node *np,
1052 const char *list_name,
1055 struct of_phandle_args *out_args)
1057 return __of_parse_phandle_with_args(np, list_name, NULL, cell_count,
1062 * of_parse_phandle_with_optional_args() - Find a node pointed by phandle in a list
1063 * @np: pointer to a device tree node containing a list
1064 * @list_name: property name that contains a list
1065 * @cells_name: property name that specifies phandles' arguments count
1066 * @index: index of a phandle to parse out
1067 * @out_args: optional pointer to output arguments structure (will be filled)
1069 * Same as of_parse_phandle_with_args() except that if the cells_name property
1070 * is not found, cell_count of 0 is assumed.
1072 * This is used to useful, if you have a phandle which didn't have arguments
1073 * before and thus doesn't have a '#*-cells' property but is now migrated to
1074 * having arguments while retaining backwards compatibility.
1076 static inline int of_parse_phandle_with_optional_args(const struct device_node *np,
1077 const char *list_name,
1078 const char *cells_name,
1080 struct of_phandle_args *out_args)
1082 return __of_parse_phandle_with_args(np, list_name, cells_name,
1083 0, index, out_args);
1087 * of_phandle_args_equal() - Compare two of_phandle_args
1088 * @a1: First of_phandle_args to compare
1089 * @a2: Second of_phandle_args to compare
1091 * Return: True if a1 and a2 are the same (same node pointer, same phandle
1092 * args), false otherwise.
1094 static inline bool of_phandle_args_equal(const struct of_phandle_args *a1,
1095 const struct of_phandle_args *a2)
1097 return a1->np == a2->np &&
1098 a1->args_count == a2->args_count &&
1099 !memcmp(a1->args, a2->args, sizeof(a1->args[0]) * a1->args_count);
1103 * of_property_count_u8_elems - Count the number of u8 elements in a 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 node and count the number of u8 elements
1111 * Return: The number of elements on sucess, -EINVAL if the property does
1112 * not exist or its length does not match a multiple of u8 and -ENODATA if the
1113 * property does not have a value.
1115 static inline int of_property_count_u8_elems(const struct device_node *np,
1116 const char *propname)
1118 return of_property_count_elems_of_size(np, propname, sizeof(u8));
1122 * of_property_count_u16_elems - Count the number of u16 elements in a property
1124 * @np: device node from which the property value is to be read.
1125 * @propname: name of the property to be searched.
1127 * Search for a property in a device node and count the number of u16 elements
1130 * Return: The number of elements on sucess, -EINVAL if the property does
1131 * not exist or its length does not match a multiple of u16 and -ENODATA if the
1132 * property does not have a value.
1134 static inline int of_property_count_u16_elems(const struct device_node *np,
1135 const char *propname)
1137 return of_property_count_elems_of_size(np, propname, sizeof(u16));
1141 * of_property_count_u32_elems - Count the number of u32 elements in a property
1143 * @np: device node from which the property value is to be read.
1144 * @propname: name of the property to be searched.
1146 * Search for a property in a device node and count the number of u32 elements
1149 * Return: The number of elements on sucess, -EINVAL if the property does
1150 * not exist or its length does not match a multiple of u32 and -ENODATA if the
1151 * property does not have a value.
1153 static inline int of_property_count_u32_elems(const struct device_node *np,
1154 const char *propname)
1156 return of_property_count_elems_of_size(np, propname, sizeof(u32));
1160 * of_property_count_u64_elems - Count the number of u64 elements in a property
1162 * @np: device node from which the property value is to be read.
1163 * @propname: name of the property to be searched.
1165 * Search for a property in a device node and count the number of u64 elements
1168 * Return: The number of elements on sucess, -EINVAL if the property does
1169 * not exist or its length does not match a multiple of u64 and -ENODATA if the
1170 * property does not have a value.
1172 static inline int of_property_count_u64_elems(const struct device_node *np,
1173 const char *propname)
1175 return of_property_count_elems_of_size(np, propname, sizeof(u64));
1179 * of_property_read_string_array() - Read an array of strings from a multiple
1181 * @np: device node from which the property value is to be read.
1182 * @propname: name of the property to be searched.
1183 * @out_strs: output array of string pointers.
1184 * @sz: number of array elements to read.
1186 * Search for a property in a device tree node and retrieve a list of
1187 * terminated string values (pointer to data, not a copy) in that property.
1189 * Return: If @out_strs is NULL, the number of strings in the property is returned.
1191 static inline int of_property_read_string_array(const struct device_node *np,
1192 const char *propname, const char **out_strs,
1195 return of_property_read_string_helper(np, propname, out_strs, sz, 0);
1199 * of_property_count_strings() - Find and return the number of strings from a
1200 * multiple strings 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 property in a device tree node and retrieve the number of null
1205 * terminated string contain in it.
1207 * Return: The number of strings on success, -EINVAL if the property does not
1208 * exist, -ENODATA if property does not have a value, and -EILSEQ if the string
1209 * is not null-terminated within the length of the property data.
1211 static inline int of_property_count_strings(const struct device_node *np,
1212 const char *propname)
1214 return of_property_read_string_helper(np, propname, NULL, 0, 0);
1218 * of_property_read_string_index() - Find and read a string from a multiple
1220 * @np: device node from which the property value is to be read.
1221 * @propname: name of the property to be searched.
1222 * @index: index of the string in the list of strings
1223 * @output: pointer to null terminated return string, modified only if
1224 * return value is 0.
1226 * Search for a property in a device tree node and retrieve a null
1227 * terminated string value (pointer to data, not a copy) in the list of strings
1228 * contained in that property.
1230 * Return: 0 on success, -EINVAL if the property does not exist, -ENODATA if
1231 * property does not have a value, and -EILSEQ if the string is not
1232 * null-terminated within the length of the property data.
1234 * The out_string pointer is modified only if a valid string can be decoded.
1236 static inline int of_property_read_string_index(const struct device_node *np,
1237 const char *propname,
1238 int index, const char **output)
1240 int rc = of_property_read_string_helper(np, propname, output, 1, index);
1241 return rc < 0 ? rc : 0;
1245 * of_property_read_bool - Find a property
1246 * @np: device node from which the property value is to be read.
1247 * @propname: name of the property to be searched.
1249 * Search for a boolean property in a device node. Usage on non-boolean
1250 * property types is deprecated.
1252 * Return: true if the property exists false otherwise.
1254 static inline bool of_property_read_bool(const struct device_node *np,
1255 const char *propname)
1257 struct property *prop = of_find_property(np, propname, NULL);
1259 return prop ? true : false;
1263 * of_property_present - Test if a property is present in a node
1264 * @np: device node to search for the property.
1265 * @propname: name of the property to be searched.
1267 * Test for a property present in a device node.
1269 * Return: true if the property exists false otherwise.
1271 static inline bool of_property_present(const struct device_node *np, const char *propname)
1273 return of_property_read_bool(np, propname);
1277 * of_property_read_u8_array - Find and read an array of u8 from a property.
1279 * @np: device node from which the property value is to be read.
1280 * @propname: name of the property to be searched.
1281 * @out_values: pointer to return value, modified only if return value is 0.
1282 * @sz: number of array elements to read
1284 * Search for a property in a device node and read 8-bit value(s) from
1287 * dts entry of array should be like:
1288 * ``property = /bits/ 8 <0x50 0x60 0x70>;``
1290 * Return: 0 on success, -EINVAL if the property does not exist,
1291 * -ENODATA if property does not have a value, and -EOVERFLOW if the
1292 * property data isn't large enough.
1294 * The out_values is modified only if a valid u8 value can be decoded.
1296 static inline int of_property_read_u8_array(const struct device_node *np,
1297 const char *propname,
1298 u8 *out_values, size_t sz)
1300 int ret = of_property_read_variable_u8_array(np, propname, out_values,
1309 * of_property_read_u16_array - Find and read an array of u16 from a property.
1311 * @np: device node from which the property value is to be read.
1312 * @propname: name of the property to be searched.
1313 * @out_values: pointer to return value, modified only if return value is 0.
1314 * @sz: number of array elements to read
1316 * Search for a property in a device node and read 16-bit value(s) from
1319 * dts entry of array should be like:
1320 * ``property = /bits/ 16 <0x5000 0x6000 0x7000>;``
1322 * Return: 0 on success, -EINVAL if the property does not exist,
1323 * -ENODATA if property does not have a value, and -EOVERFLOW if the
1324 * property data isn't large enough.
1326 * The out_values is modified only if a valid u16 value can be decoded.
1328 static inline int of_property_read_u16_array(const struct device_node *np,
1329 const char *propname,
1330 u16 *out_values, size_t sz)
1332 int ret = of_property_read_variable_u16_array(np, propname, out_values,
1341 * of_property_read_u32_array - Find and read an array of 32 bit integers
1344 * @np: device node from which the property value is to be read.
1345 * @propname: name of the property to be searched.
1346 * @out_values: pointer to return value, modified only if return value is 0.
1347 * @sz: number of array elements to read
1349 * Search for a property in a device node and read 32-bit value(s) from
1352 * Return: 0 on success, -EINVAL if the property does not exist,
1353 * -ENODATA if property does not have a value, and -EOVERFLOW if the
1354 * property data isn't large enough.
1356 * The out_values is modified only if a valid u32 value can be decoded.
1358 static inline int of_property_read_u32_array(const struct device_node *np,
1359 const char *propname,
1360 u32 *out_values, size_t sz)
1362 int ret = of_property_read_variable_u32_array(np, propname, out_values,
1371 * of_property_read_u64_array - Find and read an array of 64 bit integers
1374 * @np: device node from which the property value is to be read.
1375 * @propname: name of the property to be searched.
1376 * @out_values: pointer to return value, modified only if return value is 0.
1377 * @sz: number of array elements to read
1379 * Search for a property in a device node and read 64-bit value(s) from
1382 * Return: 0 on success, -EINVAL if the property does not exist,
1383 * -ENODATA if property does not have a value, and -EOVERFLOW if the
1384 * property data isn't large enough.
1386 * The out_values is modified only if a valid u64 value can be decoded.
1388 static inline int of_property_read_u64_array(const struct device_node *np,
1389 const char *propname,
1390 u64 *out_values, size_t sz)
1392 int ret = of_property_read_variable_u64_array(np, propname, out_values,
1400 static inline int of_property_read_u8(const struct device_node *np,
1401 const char *propname,
1404 return of_property_read_u8_array(np, propname, out_value, 1);
1407 static inline int of_property_read_u16(const struct device_node *np,
1408 const char *propname,
1411 return of_property_read_u16_array(np, propname, out_value, 1);
1414 static inline int of_property_read_u32(const struct device_node *np,
1415 const char *propname,
1418 return of_property_read_u32_array(np, propname, out_value, 1);
1421 static inline int of_property_read_s32(const struct device_node *np,
1422 const char *propname,
1425 return of_property_read_u32(np, propname, (u32*) out_value);
1428 #define of_for_each_phandle(it, err, np, ln, cn, cc) \
1429 for (of_phandle_iterator_init((it), (np), (ln), (cn), (cc)), \
1430 err = of_phandle_iterator_next(it); \
1432 err = of_phandle_iterator_next(it))
1434 #define of_property_for_each_u32(np, propname, prop, p, u) \
1435 for (prop = of_find_property(np, propname, NULL), \
1436 p = of_prop_next_u32(prop, NULL, &u); \
1438 p = of_prop_next_u32(prop, p, &u))
1440 #define of_property_for_each_string(np, propname, prop, s) \
1441 for (prop = of_find_property(np, propname, NULL), \
1442 s = of_prop_next_string(prop, NULL); \
1444 s = of_prop_next_string(prop, s))
1446 #define for_each_node_by_name(dn, name) \
1447 for (dn = of_find_node_by_name(NULL, name); dn; \
1448 dn = of_find_node_by_name(dn, name))
1449 #define for_each_node_by_type(dn, type) \
1450 for (dn = of_find_node_by_type(NULL, type); dn; \
1451 dn = of_find_node_by_type(dn, type))
1452 #define for_each_compatible_node(dn, type, compatible) \
1453 for (dn = of_find_compatible_node(NULL, type, compatible); dn; \
1454 dn = of_find_compatible_node(dn, type, compatible))
1455 #define for_each_matching_node(dn, matches) \
1456 for (dn = of_find_matching_node(NULL, matches); dn; \
1457 dn = of_find_matching_node(dn, matches))
1458 #define for_each_matching_node_and_match(dn, matches, match) \
1459 for (dn = of_find_matching_node_and_match(NULL, matches, match); \
1460 dn; dn = of_find_matching_node_and_match(dn, matches, match))
1462 #define for_each_child_of_node(parent, child) \
1463 for (child = of_get_next_child(parent, NULL); child != NULL; \
1464 child = of_get_next_child(parent, child))
1466 #define for_each_child_of_node_scoped(parent, child) \
1467 for (struct device_node *child __free(device_node) = \
1468 of_get_next_child(parent, NULL); \
1470 child = of_get_next_child(parent, child))
1472 #define for_each_available_child_of_node(parent, child) \
1473 for (child = of_get_next_available_child(parent, NULL); child != NULL; \
1474 child = of_get_next_available_child(parent, child))
1475 #define for_each_reserved_child_of_node(parent, child) \
1476 for (child = of_get_next_reserved_child(parent, NULL); child != NULL; \
1477 child = of_get_next_reserved_child(parent, child))
1479 #define for_each_available_child_of_node_scoped(parent, child) \
1480 for (struct device_node *child __free(device_node) = \
1481 of_get_next_available_child(parent, NULL); \
1483 child = of_get_next_available_child(parent, child))
1485 #define for_each_of_cpu_node(cpu) \
1486 for (cpu = of_get_next_cpu_node(NULL); cpu != NULL; \
1487 cpu = of_get_next_cpu_node(cpu))
1489 #define for_each_node_with_property(dn, prop_name) \
1490 for (dn = of_find_node_with_property(NULL, prop_name); dn; \
1491 dn = of_find_node_with_property(dn, prop_name))
1493 static inline int of_get_child_count(const struct device_node *np)
1495 struct device_node *child;
1498 for_each_child_of_node(np, child)
1504 static inline int of_get_available_child_count(const struct device_node *np)
1506 struct device_node *child;
1509 for_each_available_child_of_node(np, child)
1515 #define _OF_DECLARE_STUB(table, name, compat, fn, fn_type) \
1516 static const struct of_device_id __of_table_##name \
1517 __attribute__((unused)) \
1518 = { .compatible = compat, \
1519 .data = (fn == (fn_type)NULL) ? fn : fn }
1521 #if defined(CONFIG_OF) && !defined(MODULE)
1522 #define _OF_DECLARE(table, name, compat, fn, fn_type) \
1523 static const struct of_device_id __of_table_##name \
1524 __used __section("__" #table "_of_table") \
1525 __aligned(__alignof__(struct of_device_id)) \
1526 = { .compatible = compat, \
1527 .data = (fn == (fn_type)NULL) ? fn : fn }
1529 #define _OF_DECLARE(table, name, compat, fn, fn_type) \
1530 _OF_DECLARE_STUB(table, name, compat, fn, fn_type)
1533 typedef int (*of_init_fn_2)(struct device_node *, struct device_node *);
1534 typedef int (*of_init_fn_1_ret)(struct device_node *);
1535 typedef void (*of_init_fn_1)(struct device_node *);
1537 #define OF_DECLARE_1(table, name, compat, fn) \
1538 _OF_DECLARE(table, name, compat, fn, of_init_fn_1)
1539 #define OF_DECLARE_1_RET(table, name, compat, fn) \
1540 _OF_DECLARE(table, name, compat, fn, of_init_fn_1_ret)
1541 #define OF_DECLARE_2(table, name, compat, fn) \
1542 _OF_DECLARE(table, name, compat, fn, of_init_fn_2)
1545 * struct of_changeset_entry - Holds a changeset entry
1547 * @node: list_head for the log list
1548 * @action: notifier action
1549 * @np: pointer to the device node affected
1550 * @prop: pointer to the property affected
1551 * @old_prop: hold a pointer to the original property
1553 * Every modification of the device tree during a changeset
1554 * is held in a list of of_changeset_entry structures.
1555 * That way we can recover from a partial application, or we can
1556 * revert the changeset
1558 struct of_changeset_entry {
1559 struct list_head node;
1560 unsigned long action;
1561 struct device_node *np;
1562 struct property *prop;
1563 struct property *old_prop;
1567 * struct of_changeset - changeset tracker structure
1569 * @entries: list_head for the changeset entries
1571 * changesets are a convenient way to apply bulk changes to the
1572 * live tree. In case of an error, changes are rolled-back.
1573 * changesets live on after initial application, and if not
1574 * destroyed after use, they can be reverted in one single call.
1576 struct of_changeset {
1577 struct list_head entries;
1580 enum of_reconfig_change {
1581 OF_RECONFIG_NO_CHANGE = 0,
1582 OF_RECONFIG_CHANGE_ADD,
1583 OF_RECONFIG_CHANGE_REMOVE,
1586 struct notifier_block;
1588 #ifdef CONFIG_OF_DYNAMIC
1589 extern int of_reconfig_notifier_register(struct notifier_block *);
1590 extern int of_reconfig_notifier_unregister(struct notifier_block *);
1591 extern int of_reconfig_notify(unsigned long, struct of_reconfig_data *rd);
1592 extern int of_reconfig_get_state_change(unsigned long action,
1593 struct of_reconfig_data *arg);
1595 extern void of_changeset_init(struct of_changeset *ocs);
1596 extern void of_changeset_destroy(struct of_changeset *ocs);
1597 extern int of_changeset_apply(struct of_changeset *ocs);
1598 extern int of_changeset_revert(struct of_changeset *ocs);
1599 extern int of_changeset_action(struct of_changeset *ocs,
1600 unsigned long action, struct device_node *np,
1601 struct property *prop);
1603 static inline int of_changeset_attach_node(struct of_changeset *ocs,
1604 struct device_node *np)
1606 return of_changeset_action(ocs, OF_RECONFIG_ATTACH_NODE, np, NULL);
1609 static inline int of_changeset_detach_node(struct of_changeset *ocs,
1610 struct device_node *np)
1612 return of_changeset_action(ocs, OF_RECONFIG_DETACH_NODE, np, NULL);
1615 static inline int of_changeset_add_property(struct of_changeset *ocs,
1616 struct device_node *np, struct property *prop)
1618 return of_changeset_action(ocs, OF_RECONFIG_ADD_PROPERTY, np, prop);
1621 static inline int of_changeset_remove_property(struct of_changeset *ocs,
1622 struct device_node *np, struct property *prop)
1624 return of_changeset_action(ocs, OF_RECONFIG_REMOVE_PROPERTY, np, prop);
1627 static inline int of_changeset_update_property(struct of_changeset *ocs,
1628 struct device_node *np, struct property *prop)
1630 return of_changeset_action(ocs, OF_RECONFIG_UPDATE_PROPERTY, np, prop);
1633 struct device_node *of_changeset_create_node(struct of_changeset *ocs,
1634 struct device_node *parent,
1635 const char *full_name);
1636 int of_changeset_add_prop_string(struct of_changeset *ocs,
1637 struct device_node *np,
1638 const char *prop_name, const char *str);
1639 int of_changeset_add_prop_string_array(struct of_changeset *ocs,
1640 struct device_node *np,
1641 const char *prop_name,
1642 const char **str_array, size_t sz);
1643 int of_changeset_add_prop_u32_array(struct of_changeset *ocs,
1644 struct device_node *np,
1645 const char *prop_name,
1646 const u32 *array, size_t sz);
1647 static inline int of_changeset_add_prop_u32(struct of_changeset *ocs,
1648 struct device_node *np,
1649 const char *prop_name,
1652 return of_changeset_add_prop_u32_array(ocs, np, prop_name, &val, 1);
1655 #else /* CONFIG_OF_DYNAMIC */
1656 static inline int of_reconfig_notifier_register(struct notifier_block *nb)
1660 static inline int of_reconfig_notifier_unregister(struct notifier_block *nb)
1664 static inline int of_reconfig_notify(unsigned long action,
1665 struct of_reconfig_data *arg)
1669 static inline int of_reconfig_get_state_change(unsigned long action,
1670 struct of_reconfig_data *arg)
1674 #endif /* CONFIG_OF_DYNAMIC */
1677 * of_device_is_system_power_controller - Tells if system-power-controller is found for device_node
1678 * @np: Pointer to the given device_node
1680 * Return: true if present false otherwise
1682 static inline bool of_device_is_system_power_controller(const struct device_node *np)
1684 return of_property_read_bool(np, "system-power-controller");
1688 * of_have_populated_dt() - Has DT been populated by bootloader
1690 * Return: True if a DTB has been populated by the bootloader and it isn't the
1691 * empty builtin one. False otherwise.
1693 static inline bool of_have_populated_dt(void)
1696 return of_property_present(of_root, "compatible");
1706 enum of_overlay_notify_action {
1707 OF_OVERLAY_INIT = 0, /* kzalloc() of ovcs sets this value */
1708 OF_OVERLAY_PRE_APPLY,
1709 OF_OVERLAY_POST_APPLY,
1710 OF_OVERLAY_PRE_REMOVE,
1711 OF_OVERLAY_POST_REMOVE,
1714 static inline const char *of_overlay_action_name(enum of_overlay_notify_action action)
1716 static const char *const of_overlay_action_name[] = {
1724 return of_overlay_action_name[action];
1727 struct of_overlay_notify_data {
1728 struct device_node *overlay;
1729 struct device_node *target;
1732 #ifdef CONFIG_OF_OVERLAY
1734 int of_overlay_fdt_apply(const void *overlay_fdt, u32 overlay_fdt_size,
1735 int *ovcs_id, struct device_node *target_base);
1736 int of_overlay_remove(int *ovcs_id);
1737 int of_overlay_remove_all(void);
1739 int of_overlay_notifier_register(struct notifier_block *nb);
1740 int of_overlay_notifier_unregister(struct notifier_block *nb);
1744 static inline int of_overlay_fdt_apply(const void *overlay_fdt, u32 overlay_fdt_size,
1745 int *ovcs_id, struct device_node *target_base)
1750 static inline int of_overlay_remove(int *ovcs_id)
1755 static inline int of_overlay_remove_all(void)
1760 static inline int of_overlay_notifier_register(struct notifier_block *nb)
1765 static inline int of_overlay_notifier_unregister(struct notifier_block *nb)
1772 #endif /* _LINUX_OF_H */