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/property.h>
20 #include <linux/list.h>
22 #include <asm/byteorder.h>
31 struct property *next;
32 #if defined(CONFIG_OF_DYNAMIC) || defined(CONFIG_SPARC)
35 #if defined(CONFIG_OF_PROMTREE)
36 unsigned int unique_id;
38 #if defined(CONFIG_OF_KOBJ)
39 struct bin_attribute attr;
43 #if defined(CONFIG_SPARC)
44 struct of_irq_controller;
50 const char *full_name;
51 struct fwnode_handle fwnode;
53 struct property *properties;
54 struct property *deadprops; /* removed properties */
55 struct device_node *parent;
56 struct device_node *child;
57 struct device_node *sibling;
58 #if defined(CONFIG_OF_KOBJ)
63 #if defined(CONFIG_SPARC)
64 unsigned int unique_id;
65 struct of_irq_controller *irq_trans;
69 #define MAX_PHANDLE_ARGS 16
70 struct of_phandle_args {
71 struct device_node *np;
73 uint32_t args[MAX_PHANDLE_ARGS];
76 struct of_phandle_iterator {
77 /* Common iterator information */
78 const char *cells_name;
80 const struct device_node *parent;
82 /* List size information */
83 const __be32 *list_end;
84 const __be32 *phandle_end;
86 /* Current position state */
90 struct device_node *node;
93 struct of_reconfig_data {
94 struct device_node *dn;
95 struct property *prop;
96 struct property *old_prop;
100 * of_node_init - initialize a devicetree node
101 * @node: Pointer to device node that has been created by kzalloc()
102 * @phandle_name: Name of property holding a phandle value
104 * On return the device_node refcount is set to one. Use of_node_put()
105 * on @node when done to free the memory allocated for it. If the node
106 * is NOT a dynamic node the memory will not be freed. The decision of
107 * whether to free the memory will be done by node->release(), which is
110 /* initialize a node */
111 extern const struct kobj_type of_node_ktype;
112 extern const struct fwnode_operations of_fwnode_ops;
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 */
139 /* Pointer for first entry in chain of all nodes. */
140 extern struct device_node *of_root;
141 extern struct device_node *of_chosen;
142 extern struct device_node *of_aliases;
143 extern struct device_node *of_stdout;
146 * struct device_node flag descriptions
147 * (need to be visible even when !CONFIG_OF)
149 #define OF_DYNAMIC 1 /* (and properties) allocated via kmalloc */
150 #define OF_DETACHED 2 /* detached from the device tree */
151 #define OF_POPULATED 3 /* device already created */
152 #define OF_POPULATED_BUS 4 /* platform bus created for children */
153 #define OF_OVERLAY 5 /* allocated for an overlay */
154 #define OF_OVERLAY_FREE_CSET 6 /* in overlay cset being freed */
156 #define OF_BAD_ADDR ((u64)-1)
159 void of_core_init(void);
161 static inline bool is_of_node(const struct fwnode_handle *fwnode)
163 return !IS_ERR_OR_NULL(fwnode) && fwnode->ops == &of_fwnode_ops;
166 #define to_of_node(__fwnode) \
168 typeof(__fwnode) __to_of_node_fwnode = (__fwnode); \
170 is_of_node(__to_of_node_fwnode) ? \
171 container_of(__to_of_node_fwnode, \
172 struct device_node, fwnode) : \
176 #define of_fwnode_handle(node) \
178 typeof(node) __of_fwnode_handle_node = (node); \
180 __of_fwnode_handle_node ? \
181 &__of_fwnode_handle_node->fwnode : NULL; \
184 static inline bool of_have_populated_dt(void)
186 return of_root != NULL;
189 static inline bool of_node_is_root(const struct device_node *node)
191 return node && (node->parent == NULL);
194 static inline int of_node_check_flag(const struct device_node *n, unsigned long flag)
196 return test_bit(flag, &n->_flags);
199 static inline int of_node_test_and_set_flag(struct device_node *n,
202 return test_and_set_bit(flag, &n->_flags);
205 static inline void of_node_set_flag(struct device_node *n, unsigned long flag)
207 set_bit(flag, &n->_flags);
210 static inline void of_node_clear_flag(struct device_node *n, unsigned long flag)
212 clear_bit(flag, &n->_flags);
215 #if defined(CONFIG_OF_DYNAMIC) || defined(CONFIG_SPARC)
216 static inline int of_property_check_flag(const struct property *p, unsigned long flag)
218 return test_bit(flag, &p->_flags);
221 static inline void of_property_set_flag(struct property *p, unsigned long flag)
223 set_bit(flag, &p->_flags);
226 static inline void of_property_clear_flag(struct property *p, unsigned long flag)
228 clear_bit(flag, &p->_flags);
232 extern struct device_node *__of_find_all_nodes(struct device_node *prev);
233 extern struct device_node *of_find_all_nodes(struct device_node *prev);
236 * OF address retrieval & translation
239 /* Helper to read a big number; size is in cells (not bytes) */
240 static inline u64 of_read_number(const __be32 *cell, int size)
243 for (; size--; cell++)
244 r = (r << 32) | be32_to_cpu(*cell);
248 /* Like of_read_number, but we want an unsigned long result */
249 static inline unsigned long of_read_ulong(const __be32 *cell, int size)
251 /* toss away upper bits if unsigned long is smaller than u64 */
252 return of_read_number(cell, size);
255 #if defined(CONFIG_SPARC)
256 #include <asm/prom.h>
259 #define OF_IS_DYNAMIC(x) test_bit(OF_DYNAMIC, &x->_flags)
260 #define OF_MARK_DYNAMIC(x) set_bit(OF_DYNAMIC, &x->_flags)
262 extern bool of_node_name_eq(const struct device_node *np, const char *name);
263 extern bool of_node_name_prefix(const struct device_node *np, const char *prefix);
265 static inline const char *of_node_full_name(const struct device_node *np)
267 return np ? np->full_name : "<no-node>";
270 #define for_each_of_allnodes_from(from, dn) \
271 for (dn = __of_find_all_nodes(from); dn; dn = __of_find_all_nodes(dn))
272 #define for_each_of_allnodes(dn) for_each_of_allnodes_from(NULL, dn)
273 extern struct device_node *of_find_node_by_name(struct device_node *from,
275 extern struct device_node *of_find_node_by_type(struct device_node *from,
277 extern struct device_node *of_find_compatible_node(struct device_node *from,
278 const char *type, const char *compat);
279 extern struct device_node *of_find_matching_node_and_match(
280 struct device_node *from,
281 const struct of_device_id *matches,
282 const struct of_device_id **match);
284 extern struct device_node *of_find_node_opts_by_path(const char *path,
286 static inline struct device_node *of_find_node_by_path(const char *path)
288 return of_find_node_opts_by_path(path, NULL);
291 extern struct device_node *of_find_node_by_phandle(phandle handle);
292 extern struct device_node *of_get_parent(const struct device_node *node);
293 extern struct device_node *of_get_next_parent(struct device_node *node);
294 extern struct device_node *of_get_next_child(const struct device_node *node,
295 struct device_node *prev);
296 extern struct device_node *of_get_next_available_child(
297 const struct device_node *node, struct device_node *prev);
299 extern struct device_node *of_get_compatible_child(const struct device_node *parent,
300 const char *compatible);
301 extern struct device_node *of_get_child_by_name(const struct device_node *node,
305 extern struct device_node *of_find_next_cache_node(const struct device_node *);
306 extern int of_find_last_cache_level(unsigned int cpu);
307 extern struct device_node *of_find_node_with_property(
308 struct device_node *from, const char *prop_name);
310 extern struct property *of_find_property(const struct device_node *np,
313 extern int of_property_count_elems_of_size(const struct device_node *np,
314 const char *propname, int elem_size);
315 extern int of_property_read_u32_index(const struct device_node *np,
316 const char *propname,
317 u32 index, u32 *out_value);
318 extern int of_property_read_u64_index(const struct device_node *np,
319 const char *propname,
320 u32 index, u64 *out_value);
321 extern int of_property_read_variable_u8_array(const struct device_node *np,
322 const char *propname, u8 *out_values,
323 size_t sz_min, size_t sz_max);
324 extern int of_property_read_variable_u16_array(const struct device_node *np,
325 const char *propname, u16 *out_values,
326 size_t sz_min, size_t sz_max);
327 extern int of_property_read_variable_u32_array(const struct device_node *np,
328 const char *propname,
332 extern int of_property_read_u64(const struct device_node *np,
333 const char *propname, u64 *out_value);
334 extern int of_property_read_variable_u64_array(const struct device_node *np,
335 const char *propname,
340 extern int of_property_read_string(const struct device_node *np,
341 const char *propname,
342 const char **out_string);
343 extern int of_property_match_string(const struct device_node *np,
344 const char *propname,
346 extern int of_property_read_string_helper(const struct device_node *np,
347 const char *propname,
348 const char **out_strs, size_t sz, int index);
349 extern int of_device_is_compatible(const struct device_node *device,
351 extern int of_device_compatible_match(const struct device_node *device,
352 const char *const *compat);
353 extern bool of_device_is_available(const struct device_node *device);
354 extern bool of_device_is_big_endian(const struct device_node *device);
355 extern const void *of_get_property(const struct device_node *node,
358 extern struct device_node *of_get_cpu_node(int cpu, unsigned int *thread);
359 extern struct device_node *of_cpu_device_node_get(int cpu);
360 extern int of_cpu_node_to_id(struct device_node *np);
361 extern struct device_node *of_get_next_cpu_node(struct device_node *prev);
362 extern struct device_node *of_get_cpu_state_node(struct device_node *cpu_node,
364 extern u64 of_get_cpu_hwid(struct device_node *cpun, unsigned int thread);
366 #define for_each_property_of_node(dn, pp) \
367 for (pp = dn->properties; pp != NULL; pp = pp->next)
369 extern int of_n_addr_cells(struct device_node *np);
370 extern int of_n_size_cells(struct device_node *np);
371 extern const struct of_device_id *of_match_node(
372 const struct of_device_id *matches, const struct device_node *node);
373 extern const void *of_device_get_match_data(const struct device *dev);
374 extern int of_alias_from_compatible(const struct device_node *node, char *alias,
376 extern void of_print_phandle_args(const char *msg, const struct of_phandle_args *args);
377 extern int __of_parse_phandle_with_args(const struct device_node *np,
378 const char *list_name, const char *cells_name, int cell_count,
379 int index, struct of_phandle_args *out_args);
380 extern int of_parse_phandle_with_args_map(const struct device_node *np,
381 const char *list_name, const char *stem_name, int index,
382 struct of_phandle_args *out_args);
383 extern int of_count_phandle_with_args(const struct device_node *np,
384 const char *list_name, const char *cells_name);
386 /* module functions */
387 extern ssize_t of_modalias(const struct device_node *np, char *str, ssize_t len);
388 extern int of_request_module(const struct device_node *np);
390 /* phandle iterator functions */
391 extern int of_phandle_iterator_init(struct of_phandle_iterator *it,
392 const struct device_node *np,
393 const char *list_name,
394 const char *cells_name,
397 extern int of_phandle_iterator_next(struct of_phandle_iterator *it);
398 extern int of_phandle_iterator_args(struct of_phandle_iterator *it,
402 extern void of_alias_scan(void * (*dt_alloc)(u64 size, u64 align));
403 extern int of_alias_get_id(struct device_node *np, const char *stem);
404 extern int of_alias_get_highest_id(const char *stem);
406 extern int of_machine_is_compatible(const char *compat);
408 extern int of_add_property(struct device_node *np, struct property *prop);
409 extern int of_remove_property(struct device_node *np, struct property *prop);
410 extern int of_update_property(struct device_node *np, struct property *newprop);
412 /* For updating the device tree at runtime */
413 #define OF_RECONFIG_ATTACH_NODE 0x0001
414 #define OF_RECONFIG_DETACH_NODE 0x0002
415 #define OF_RECONFIG_ADD_PROPERTY 0x0003
416 #define OF_RECONFIG_REMOVE_PROPERTY 0x0004
417 #define OF_RECONFIG_UPDATE_PROPERTY 0x0005
419 extern int of_attach_node(struct device_node *);
420 extern int of_detach_node(struct device_node *);
422 #define of_match_ptr(_ptr) (_ptr)
425 * struct property *prop;
429 * of_property_for_each_u32(np, "propname", prop, p, u)
430 * printk("U32 value: %x\n", u);
432 const __be32 *of_prop_next_u32(struct property *prop, const __be32 *cur,
435 * struct property *prop;
438 * of_property_for_each_string(np, "propname", prop, s)
439 * printk("String value: %s\n", s);
441 const char *of_prop_next_string(struct property *prop, const char *cur);
443 bool of_console_check(struct device_node *dn, char *name, int index);
445 int of_map_id(struct device_node *np, u32 id,
446 const char *map_name, const char *map_mask_name,
447 struct device_node **target, u32 *id_out);
449 phys_addr_t of_dma_get_max_cpu_address(struct device_node *np);
452 void *of_kexec_alloc_and_setup_fdt(const struct kimage *image,
453 unsigned long initrd_load_addr,
454 unsigned long initrd_len,
455 const char *cmdline, size_t extra_fdt_size);
456 #else /* CONFIG_OF */
458 static inline void of_core_init(void)
462 static inline bool is_of_node(const struct fwnode_handle *fwnode)
467 static inline struct device_node *to_of_node(const struct fwnode_handle *fwnode)
472 static inline bool of_node_name_eq(const struct device_node *np, const char *name)
477 static inline bool of_node_name_prefix(const struct device_node *np, const char *prefix)
482 static inline const char* of_node_full_name(const struct device_node *np)
487 static inline struct device_node *of_find_node_by_name(struct device_node *from,
493 static inline struct device_node *of_find_node_by_type(struct device_node *from,
499 static inline struct device_node *of_find_matching_node_and_match(
500 struct device_node *from,
501 const struct of_device_id *matches,
502 const struct of_device_id **match)
507 static inline struct device_node *of_find_node_by_path(const char *path)
512 static inline struct device_node *of_find_node_opts_by_path(const char *path,
518 static inline struct device_node *of_find_node_by_phandle(phandle handle)
523 static inline struct device_node *of_get_parent(const struct device_node *node)
528 static inline struct device_node *of_get_next_parent(struct device_node *node)
533 static inline struct device_node *of_get_next_child(
534 const struct device_node *node, struct device_node *prev)
539 static inline struct device_node *of_get_next_available_child(
540 const struct device_node *node, struct device_node *prev)
545 static inline struct device_node *of_find_node_with_property(
546 struct device_node *from, const char *prop_name)
551 #define of_fwnode_handle(node) NULL
553 static inline bool of_have_populated_dt(void)
558 static inline struct device_node *of_get_compatible_child(const struct device_node *parent,
559 const char *compatible)
564 static inline struct device_node *of_get_child_by_name(
565 const struct device_node *node,
571 static inline int of_device_is_compatible(const struct device_node *device,
577 static inline int of_device_compatible_match(const struct device_node *device,
578 const char *const *compat)
583 static inline bool of_device_is_available(const struct device_node *device)
588 static inline bool of_device_is_big_endian(const struct device_node *device)
593 static inline struct property *of_find_property(const struct device_node *np,
600 static inline struct device_node *of_find_compatible_node(
601 struct device_node *from,
608 static inline int of_property_count_elems_of_size(const struct device_node *np,
609 const char *propname, int elem_size)
614 static inline int of_property_read_u32_index(const struct device_node *np,
615 const char *propname, u32 index, u32 *out_value)
620 static inline int of_property_read_u64_index(const struct device_node *np,
621 const char *propname, u32 index, u64 *out_value)
626 static inline const void *of_get_property(const struct device_node *node,
633 static inline struct device_node *of_get_cpu_node(int cpu,
634 unsigned int *thread)
639 static inline struct device_node *of_cpu_device_node_get(int cpu)
644 static inline int of_cpu_node_to_id(struct device_node *np)
649 static inline struct device_node *of_get_next_cpu_node(struct device_node *prev)
654 static inline struct device_node *of_get_cpu_state_node(struct device_node *cpu_node,
660 static inline int of_n_addr_cells(struct device_node *np)
665 static inline int of_n_size_cells(struct device_node *np)
670 static inline int of_property_read_variable_u8_array(const struct device_node *np,
671 const char *propname, u8 *out_values,
672 size_t sz_min, size_t sz_max)
677 static inline int of_property_read_variable_u16_array(const struct device_node *np,
678 const char *propname, u16 *out_values,
679 size_t sz_min, size_t sz_max)
684 static inline int of_property_read_variable_u32_array(const struct device_node *np,
685 const char *propname,
693 static inline int of_property_read_u64(const struct device_node *np,
694 const char *propname, u64 *out_value)
699 static inline int of_property_read_variable_u64_array(const struct device_node *np,
700 const char *propname,
708 static inline int of_property_read_string(const struct device_node *np,
709 const char *propname,
710 const char **out_string)
715 static inline int of_property_match_string(const struct device_node *np,
716 const char *propname,
722 static inline int of_property_read_string_helper(const struct device_node *np,
723 const char *propname,
724 const char **out_strs, size_t sz, int index)
729 static inline int __of_parse_phandle_with_args(const struct device_node *np,
730 const char *list_name,
731 const char *cells_name,
734 struct of_phandle_args *out_args)
739 static inline int of_parse_phandle_with_args_map(const struct device_node *np,
740 const char *list_name,
741 const char *stem_name,
743 struct of_phandle_args *out_args)
748 static inline int of_count_phandle_with_args(const struct device_node *np,
749 const char *list_name,
750 const char *cells_name)
755 static inline ssize_t of_modalias(const struct device_node *np, char *str,
761 static inline int of_request_module(const struct device_node *np)
766 static inline int of_phandle_iterator_init(struct of_phandle_iterator *it,
767 const struct device_node *np,
768 const char *list_name,
769 const char *cells_name,
775 static inline int of_phandle_iterator_next(struct of_phandle_iterator *it)
780 static inline int of_phandle_iterator_args(struct of_phandle_iterator *it,
787 static inline int of_alias_get_id(struct device_node *np, const char *stem)
792 static inline int of_alias_get_highest_id(const char *stem)
797 static inline int of_machine_is_compatible(const char *compat)
802 static inline int of_add_property(struct device_node *np, struct property *prop)
807 static inline int of_remove_property(struct device_node *np, struct property *prop)
812 static inline bool of_console_check(const struct device_node *dn, const char *name, int index)
817 static inline const __be32 *of_prop_next_u32(struct property *prop,
818 const __be32 *cur, u32 *pu)
823 static inline const char *of_prop_next_string(struct property *prop,
829 static inline int of_node_check_flag(struct device_node *n, unsigned long flag)
834 static inline int of_node_test_and_set_flag(struct device_node *n,
840 static inline void of_node_set_flag(struct device_node *n, unsigned long flag)
844 static inline void of_node_clear_flag(struct device_node *n, unsigned long flag)
848 static inline int of_property_check_flag(const struct property *p,
854 static inline void of_property_set_flag(struct property *p, unsigned long flag)
858 static inline void of_property_clear_flag(struct property *p, unsigned long flag)
862 static inline int of_map_id(struct device_node *np, u32 id,
863 const char *map_name, const char *map_mask_name,
864 struct device_node **target, u32 *id_out)
869 static inline phys_addr_t of_dma_get_max_cpu_address(struct device_node *np)
871 return PHYS_ADDR_MAX;
874 static inline const void *of_device_get_match_data(const struct device *dev)
879 #define of_match_ptr(_ptr) NULL
880 #define of_match_node(_matches, _node) NULL
881 #endif /* CONFIG_OF */
883 /* Default string compare functions, Allow arch asm/prom.h to override */
884 #if !defined(of_compat_cmp)
885 #define of_compat_cmp(s1, s2, l) strcasecmp((s1), (s2))
886 #define of_prop_cmp(s1, s2) strcmp((s1), (s2))
887 #define of_node_cmp(s1, s2) strcasecmp((s1), (s2))
890 static inline int of_prop_val_eq(struct property *p1, struct property *p2)
892 return p1->length == p2->length &&
893 !memcmp(p1->value, p2->value, (size_t)p1->length);
896 #if defined(CONFIG_OF) && defined(CONFIG_NUMA)
897 extern int of_node_to_nid(struct device_node *np);
899 static inline int of_node_to_nid(struct device_node *device)
905 #ifdef CONFIG_OF_NUMA
906 extern int of_numa_init(void);
908 static inline int of_numa_init(void)
914 static inline struct device_node *of_find_matching_node(
915 struct device_node *from,
916 const struct of_device_id *matches)
918 return of_find_matching_node_and_match(from, matches, NULL);
921 static inline const char *of_node_get_device_type(const struct device_node *np)
923 return of_get_property(np, "device_type", NULL);
926 static inline bool of_node_is_type(const struct device_node *np, const char *type)
928 const char *match = of_node_get_device_type(np);
930 return np && match && type && !strcmp(match, type);
934 * of_parse_phandle - Resolve a phandle property to a device_node pointer
935 * @np: Pointer to device node holding phandle property
936 * @phandle_name: Name of property holding a phandle value
937 * @index: For properties holding a table of phandles, this is the index into
940 * Return: The device_node pointer with refcount incremented. Use
941 * of_node_put() on it when done.
943 static inline struct device_node *of_parse_phandle(const struct device_node *np,
944 const char *phandle_name,
947 struct of_phandle_args args;
949 if (__of_parse_phandle_with_args(np, phandle_name, NULL, 0,
957 * of_parse_phandle_with_args() - Find a node pointed by phandle in a list
958 * @np: pointer to a device tree node containing a list
959 * @list_name: property name that contains a list
960 * @cells_name: property name that specifies phandles' arguments count
961 * @index: index of a phandle to parse out
962 * @out_args: optional pointer to output arguments structure (will be filled)
964 * This function is useful to parse lists of phandles and their arguments.
965 * Returns 0 on success and fills out_args, on error returns appropriate
968 * Caller is responsible to call of_node_put() on the returned out_args->np
982 * list = <&phandle1 1 2 &phandle2 3>;
985 * To get a device_node of the ``node2`` node you may call this:
986 * of_parse_phandle_with_args(node3, "list", "#list-cells", 1, &args);
988 static inline int of_parse_phandle_with_args(const struct device_node *np,
989 const char *list_name,
990 const char *cells_name,
992 struct of_phandle_args *out_args)
996 /* If cells_name is NULL we assume a cell count of 0 */
1000 return __of_parse_phandle_with_args(np, list_name, cells_name,
1001 cell_count, index, out_args);
1005 * of_parse_phandle_with_fixed_args() - Find a node pointed by phandle in a list
1006 * @np: pointer to a device tree node containing a list
1007 * @list_name: property name that contains a list
1008 * @cell_count: number of argument cells following the phandle
1009 * @index: index of a phandle to parse out
1010 * @out_args: optional pointer to output arguments structure (will be filled)
1012 * This function is useful to parse lists of phandles and their arguments.
1013 * Returns 0 on success and fills out_args, on error returns appropriate
1016 * Caller is responsible to call of_node_put() on the returned out_args->np
1028 * list = <&phandle1 0 2 &phandle2 2 3>;
1031 * To get a device_node of the ``node2`` node you may call this:
1032 * of_parse_phandle_with_fixed_args(node3, "list", 2, 1, &args);
1034 static inline int of_parse_phandle_with_fixed_args(const struct device_node *np,
1035 const char *list_name,
1038 struct of_phandle_args *out_args)
1040 return __of_parse_phandle_with_args(np, list_name, NULL, cell_count,
1045 * of_parse_phandle_with_optional_args() - Find a node pointed by phandle in a list
1046 * @np: pointer to a device tree node containing a list
1047 * @list_name: property name that contains a list
1048 * @cells_name: property name that specifies phandles' arguments count
1049 * @index: index of a phandle to parse out
1050 * @out_args: optional pointer to output arguments structure (will be filled)
1052 * Same as of_parse_phandle_with_args() except that if the cells_name property
1053 * is not found, cell_count of 0 is assumed.
1055 * This is used to useful, if you have a phandle which didn't have arguments
1056 * before and thus doesn't have a '#*-cells' property but is now migrated to
1057 * having arguments while retaining backwards compatibility.
1059 static inline int of_parse_phandle_with_optional_args(const struct device_node *np,
1060 const char *list_name,
1061 const char *cells_name,
1063 struct of_phandle_args *out_args)
1065 return __of_parse_phandle_with_args(np, list_name, cells_name,
1066 0, index, out_args);
1070 * of_property_count_u8_elems - Count the number of u8 elements in a property
1072 * @np: device node from which the property value is to be read.
1073 * @propname: name of the property to be searched.
1075 * Search for a property in a device node and count the number of u8 elements
1078 * Return: The number of elements on sucess, -EINVAL if the property does
1079 * not exist or its length does not match a multiple of u8 and -ENODATA if the
1080 * property does not have a value.
1082 static inline int of_property_count_u8_elems(const struct device_node *np,
1083 const char *propname)
1085 return of_property_count_elems_of_size(np, propname, sizeof(u8));
1089 * of_property_count_u16_elems - Count the number of u16 elements in a property
1091 * @np: device node from which the property value is to be read.
1092 * @propname: name of the property to be searched.
1094 * Search for a property in a device node and count the number of u16 elements
1097 * Return: The number of elements on sucess, -EINVAL if the property does
1098 * not exist or its length does not match a multiple of u16 and -ENODATA if the
1099 * property does not have a value.
1101 static inline int of_property_count_u16_elems(const struct device_node *np,
1102 const char *propname)
1104 return of_property_count_elems_of_size(np, propname, sizeof(u16));
1108 * of_property_count_u32_elems - Count the number of u32 elements in a property
1110 * @np: device node from which the property value is to be read.
1111 * @propname: name of the property to be searched.
1113 * Search for a property in a device node and count the number of u32 elements
1116 * Return: The number of elements on sucess, -EINVAL if the property does
1117 * not exist or its length does not match a multiple of u32 and -ENODATA if the
1118 * property does not have a value.
1120 static inline int of_property_count_u32_elems(const struct device_node *np,
1121 const char *propname)
1123 return of_property_count_elems_of_size(np, propname, sizeof(u32));
1127 * of_property_count_u64_elems - Count the number of u64 elements in a property
1129 * @np: device node from which the property value is to be read.
1130 * @propname: name of the property to be searched.
1132 * Search for a property in a device node and count the number of u64 elements
1135 * Return: The number of elements on sucess, -EINVAL if the property does
1136 * not exist or its length does not match a multiple of u64 and -ENODATA if the
1137 * property does not have a value.
1139 static inline int of_property_count_u64_elems(const struct device_node *np,
1140 const char *propname)
1142 return of_property_count_elems_of_size(np, propname, sizeof(u64));
1146 * of_property_read_string_array() - Read an array of strings from a multiple
1148 * @np: device node from which the property value is to be read.
1149 * @propname: name of the property to be searched.
1150 * @out_strs: output array of string pointers.
1151 * @sz: number of array elements to read.
1153 * Search for a property in a device tree node and retrieve a list of
1154 * terminated string values (pointer to data, not a copy) in that property.
1156 * Return: If @out_strs is NULL, the number of strings in the property is returned.
1158 static inline int of_property_read_string_array(const struct device_node *np,
1159 const char *propname, const char **out_strs,
1162 return of_property_read_string_helper(np, propname, out_strs, sz, 0);
1166 * of_property_count_strings() - Find and return the number of strings from a
1167 * multiple strings property.
1168 * @np: device node from which the property value is to be read.
1169 * @propname: name of the property to be searched.
1171 * Search for a property in a device tree node and retrieve the number of null
1172 * terminated string contain in it.
1174 * Return: The number of strings on success, -EINVAL if the property does not
1175 * exist, -ENODATA if property does not have a value, and -EILSEQ if the string
1176 * is not null-terminated within the length of the property data.
1178 static inline int of_property_count_strings(const struct device_node *np,
1179 const char *propname)
1181 return of_property_read_string_helper(np, propname, NULL, 0, 0);
1185 * of_property_read_string_index() - Find and read a string from a multiple
1187 * @np: device node from which the property value is to be read.
1188 * @propname: name of the property to be searched.
1189 * @index: index of the string in the list of strings
1190 * @output: pointer to null terminated return string, modified only if
1191 * return value is 0.
1193 * Search for a property in a device tree node and retrieve a null
1194 * terminated string value (pointer to data, not a copy) in the list of strings
1195 * contained in that property.
1197 * Return: 0 on success, -EINVAL if the property does not exist, -ENODATA if
1198 * property does not have a value, and -EILSEQ if the string is not
1199 * null-terminated within the length of the property data.
1201 * The out_string pointer is modified only if a valid string can be decoded.
1203 static inline int of_property_read_string_index(const struct device_node *np,
1204 const char *propname,
1205 int index, const char **output)
1207 int rc = of_property_read_string_helper(np, propname, output, 1, index);
1208 return rc < 0 ? rc : 0;
1212 * of_property_read_bool - Find a property
1213 * @np: device node from which the property value is to be read.
1214 * @propname: name of the property to be searched.
1216 * Search for a boolean property in a device node. Usage on non-boolean
1217 * property types is deprecated.
1219 * Return: true if the property exists false otherwise.
1221 static inline bool of_property_read_bool(const struct device_node *np,
1222 const char *propname)
1224 struct property *prop = of_find_property(np, propname, NULL);
1226 return prop ? true : false;
1230 * of_property_present - Test if a property is present in a node
1231 * @np: device node to search for the property.
1232 * @propname: name of the property to be searched.
1234 * Test for a property present in a device node.
1236 * Return: true if the property exists false otherwise.
1238 static inline bool of_property_present(const struct device_node *np, const char *propname)
1240 return of_property_read_bool(np, propname);
1244 * of_property_read_u8_array - Find and read an array of u8 from a property.
1246 * @np: device node from which the property value is to be read.
1247 * @propname: name of the property to be searched.
1248 * @out_values: pointer to return value, modified only if return value is 0.
1249 * @sz: number of array elements to read
1251 * Search for a property in a device node and read 8-bit value(s) from
1254 * dts entry of array should be like:
1255 * ``property = /bits/ 8 <0x50 0x60 0x70>;``
1257 * Return: 0 on success, -EINVAL if the property does not exist,
1258 * -ENODATA if property does not have a value, and -EOVERFLOW if the
1259 * property data isn't large enough.
1261 * The out_values is modified only if a valid u8 value can be decoded.
1263 static inline int of_property_read_u8_array(const struct device_node *np,
1264 const char *propname,
1265 u8 *out_values, size_t sz)
1267 int ret = of_property_read_variable_u8_array(np, propname, out_values,
1276 * of_property_read_u16_array - Find and read an array of u16 from a property.
1278 * @np: device node from which the property value is to be read.
1279 * @propname: name of the property to be searched.
1280 * @out_values: pointer to return value, modified only if return value is 0.
1281 * @sz: number of array elements to read
1283 * Search for a property in a device node and read 16-bit value(s) from
1286 * dts entry of array should be like:
1287 * ``property = /bits/ 16 <0x5000 0x6000 0x7000>;``
1289 * Return: 0 on success, -EINVAL if the property does not exist,
1290 * -ENODATA if property does not have a value, and -EOVERFLOW if the
1291 * property data isn't large enough.
1293 * The out_values is modified only if a valid u16 value can be decoded.
1295 static inline int of_property_read_u16_array(const struct device_node *np,
1296 const char *propname,
1297 u16 *out_values, size_t sz)
1299 int ret = of_property_read_variable_u16_array(np, propname, out_values,
1308 * of_property_read_u32_array - Find and read an array of 32 bit integers
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 32-bit value(s) from
1319 * Return: 0 on success, -EINVAL if the property does not exist,
1320 * -ENODATA if property does not have a value, and -EOVERFLOW if the
1321 * property data isn't large enough.
1323 * The out_values is modified only if a valid u32 value can be decoded.
1325 static inline int of_property_read_u32_array(const struct device_node *np,
1326 const char *propname,
1327 u32 *out_values, size_t sz)
1329 int ret = of_property_read_variable_u32_array(np, propname, out_values,
1338 * of_property_read_u64_array - Find and read an array of 64 bit integers
1341 * @np: device node from which the property value is to be read.
1342 * @propname: name of the property to be searched.
1343 * @out_values: pointer to return value, modified only if return value is 0.
1344 * @sz: number of array elements to read
1346 * Search for a property in a device node and read 64-bit value(s) from
1349 * Return: 0 on success, -EINVAL if the property does not exist,
1350 * -ENODATA if property does not have a value, and -EOVERFLOW if the
1351 * property data isn't large enough.
1353 * The out_values is modified only if a valid u64 value can be decoded.
1355 static inline int of_property_read_u64_array(const struct device_node *np,
1356 const char *propname,
1357 u64 *out_values, size_t sz)
1359 int ret = of_property_read_variable_u64_array(np, propname, out_values,
1367 static inline int of_property_read_u8(const struct device_node *np,
1368 const char *propname,
1371 return of_property_read_u8_array(np, propname, out_value, 1);
1374 static inline int of_property_read_u16(const struct device_node *np,
1375 const char *propname,
1378 return of_property_read_u16_array(np, propname, out_value, 1);
1381 static inline int of_property_read_u32(const struct device_node *np,
1382 const char *propname,
1385 return of_property_read_u32_array(np, propname, out_value, 1);
1388 static inline int of_property_read_s32(const struct device_node *np,
1389 const char *propname,
1392 return of_property_read_u32(np, propname, (u32*) out_value);
1395 #define of_for_each_phandle(it, err, np, ln, cn, cc) \
1396 for (of_phandle_iterator_init((it), (np), (ln), (cn), (cc)), \
1397 err = of_phandle_iterator_next(it); \
1399 err = of_phandle_iterator_next(it))
1401 #define of_property_for_each_u32(np, propname, prop, p, u) \
1402 for (prop = of_find_property(np, propname, NULL), \
1403 p = of_prop_next_u32(prop, NULL, &u); \
1405 p = of_prop_next_u32(prop, p, &u))
1407 #define of_property_for_each_string(np, propname, prop, s) \
1408 for (prop = of_find_property(np, propname, NULL), \
1409 s = of_prop_next_string(prop, NULL); \
1411 s = of_prop_next_string(prop, s))
1413 #define for_each_node_by_name(dn, name) \
1414 for (dn = of_find_node_by_name(NULL, name); dn; \
1415 dn = of_find_node_by_name(dn, name))
1416 #define for_each_node_by_type(dn, type) \
1417 for (dn = of_find_node_by_type(NULL, type); dn; \
1418 dn = of_find_node_by_type(dn, type))
1419 #define for_each_compatible_node(dn, type, compatible) \
1420 for (dn = of_find_compatible_node(NULL, type, compatible); dn; \
1421 dn = of_find_compatible_node(dn, type, compatible))
1422 #define for_each_matching_node(dn, matches) \
1423 for (dn = of_find_matching_node(NULL, matches); dn; \
1424 dn = of_find_matching_node(dn, matches))
1425 #define for_each_matching_node_and_match(dn, matches, match) \
1426 for (dn = of_find_matching_node_and_match(NULL, matches, match); \
1427 dn; dn = of_find_matching_node_and_match(dn, matches, match))
1429 #define for_each_child_of_node(parent, child) \
1430 for (child = of_get_next_child(parent, NULL); child != NULL; \
1431 child = of_get_next_child(parent, child))
1432 #define for_each_available_child_of_node(parent, child) \
1433 for (child = of_get_next_available_child(parent, NULL); child != NULL; \
1434 child = of_get_next_available_child(parent, child))
1436 #define for_each_of_cpu_node(cpu) \
1437 for (cpu = of_get_next_cpu_node(NULL); cpu != NULL; \
1438 cpu = of_get_next_cpu_node(cpu))
1440 #define for_each_node_with_property(dn, prop_name) \
1441 for (dn = of_find_node_with_property(NULL, prop_name); dn; \
1442 dn = of_find_node_with_property(dn, prop_name))
1444 static inline int of_get_child_count(const struct device_node *np)
1446 struct device_node *child;
1449 for_each_child_of_node(np, child)
1455 static inline int of_get_available_child_count(const struct device_node *np)
1457 struct device_node *child;
1460 for_each_available_child_of_node(np, child)
1466 #define _OF_DECLARE_STUB(table, name, compat, fn, fn_type) \
1467 static const struct of_device_id __of_table_##name \
1468 __attribute__((unused)) \
1469 = { .compatible = compat, \
1470 .data = (fn == (fn_type)NULL) ? fn : fn }
1472 #if defined(CONFIG_OF) && !defined(MODULE)
1473 #define _OF_DECLARE(table, name, compat, fn, fn_type) \
1474 static const struct of_device_id __of_table_##name \
1475 __used __section("__" #table "_of_table") \
1476 __aligned(__alignof__(struct of_device_id)) \
1477 = { .compatible = compat, \
1478 .data = (fn == (fn_type)NULL) ? fn : fn }
1480 #define _OF_DECLARE(table, name, compat, fn, fn_type) \
1481 _OF_DECLARE_STUB(table, name, compat, fn, fn_type)
1484 typedef int (*of_init_fn_2)(struct device_node *, struct device_node *);
1485 typedef int (*of_init_fn_1_ret)(struct device_node *);
1486 typedef void (*of_init_fn_1)(struct device_node *);
1488 #define OF_DECLARE_1(table, name, compat, fn) \
1489 _OF_DECLARE(table, name, compat, fn, of_init_fn_1)
1490 #define OF_DECLARE_1_RET(table, name, compat, fn) \
1491 _OF_DECLARE(table, name, compat, fn, of_init_fn_1_ret)
1492 #define OF_DECLARE_2(table, name, compat, fn) \
1493 _OF_DECLARE(table, name, compat, fn, of_init_fn_2)
1496 * struct of_changeset_entry - Holds a changeset entry
1498 * @node: list_head for the log list
1499 * @action: notifier action
1500 * @np: pointer to the device node affected
1501 * @prop: pointer to the property affected
1502 * @old_prop: hold a pointer to the original property
1504 * Every modification of the device tree during a changeset
1505 * is held in a list of of_changeset_entry structures.
1506 * That way we can recover from a partial application, or we can
1507 * revert the changeset
1509 struct of_changeset_entry {
1510 struct list_head node;
1511 unsigned long action;
1512 struct device_node *np;
1513 struct property *prop;
1514 struct property *old_prop;
1518 * struct of_changeset - changeset tracker structure
1520 * @entries: list_head for the changeset entries
1522 * changesets are a convenient way to apply bulk changes to the
1523 * live tree. In case of an error, changes are rolled-back.
1524 * changesets live on after initial application, and if not
1525 * destroyed after use, they can be reverted in one single call.
1527 struct of_changeset {
1528 struct list_head entries;
1531 enum of_reconfig_change {
1532 OF_RECONFIG_NO_CHANGE = 0,
1533 OF_RECONFIG_CHANGE_ADD,
1534 OF_RECONFIG_CHANGE_REMOVE,
1537 struct notifier_block;
1539 #ifdef CONFIG_OF_DYNAMIC
1540 extern int of_reconfig_notifier_register(struct notifier_block *);
1541 extern int of_reconfig_notifier_unregister(struct notifier_block *);
1542 extern int of_reconfig_notify(unsigned long, struct of_reconfig_data *rd);
1543 extern int of_reconfig_get_state_change(unsigned long action,
1544 struct of_reconfig_data *arg);
1546 extern void of_changeset_init(struct of_changeset *ocs);
1547 extern void of_changeset_destroy(struct of_changeset *ocs);
1548 extern int of_changeset_apply(struct of_changeset *ocs);
1549 extern int of_changeset_revert(struct of_changeset *ocs);
1550 extern int of_changeset_action(struct of_changeset *ocs,
1551 unsigned long action, struct device_node *np,
1552 struct property *prop);
1554 static inline int of_changeset_attach_node(struct of_changeset *ocs,
1555 struct device_node *np)
1557 return of_changeset_action(ocs, OF_RECONFIG_ATTACH_NODE, np, NULL);
1560 static inline int of_changeset_detach_node(struct of_changeset *ocs,
1561 struct device_node *np)
1563 return of_changeset_action(ocs, OF_RECONFIG_DETACH_NODE, np, NULL);
1566 static inline int of_changeset_add_property(struct of_changeset *ocs,
1567 struct device_node *np, struct property *prop)
1569 return of_changeset_action(ocs, OF_RECONFIG_ADD_PROPERTY, np, prop);
1572 static inline int of_changeset_remove_property(struct of_changeset *ocs,
1573 struct device_node *np, struct property *prop)
1575 return of_changeset_action(ocs, OF_RECONFIG_REMOVE_PROPERTY, np, prop);
1578 static inline int of_changeset_update_property(struct of_changeset *ocs,
1579 struct device_node *np, struct property *prop)
1581 return of_changeset_action(ocs, OF_RECONFIG_UPDATE_PROPERTY, np, prop);
1583 #else /* CONFIG_OF_DYNAMIC */
1584 static inline int of_reconfig_notifier_register(struct notifier_block *nb)
1588 static inline int of_reconfig_notifier_unregister(struct notifier_block *nb)
1592 static inline int of_reconfig_notify(unsigned long action,
1593 struct of_reconfig_data *arg)
1597 static inline int of_reconfig_get_state_change(unsigned long action,
1598 struct of_reconfig_data *arg)
1602 #endif /* CONFIG_OF_DYNAMIC */
1605 * of_device_is_system_power_controller - Tells if system-power-controller is found for device_node
1606 * @np: Pointer to the given device_node
1608 * Return: true if present false otherwise
1610 static inline bool of_device_is_system_power_controller(const struct device_node *np)
1612 return of_property_read_bool(np, "system-power-controller");
1619 enum of_overlay_notify_action {
1620 OF_OVERLAY_INIT = 0, /* kzalloc() of ovcs sets this value */
1621 OF_OVERLAY_PRE_APPLY,
1622 OF_OVERLAY_POST_APPLY,
1623 OF_OVERLAY_PRE_REMOVE,
1624 OF_OVERLAY_POST_REMOVE,
1627 static inline const char *of_overlay_action_name(enum of_overlay_notify_action action)
1629 static const char *const of_overlay_action_name[] = {
1637 return of_overlay_action_name[action];
1640 struct of_overlay_notify_data {
1641 struct device_node *overlay;
1642 struct device_node *target;
1645 #ifdef CONFIG_OF_OVERLAY
1647 int of_overlay_fdt_apply(const void *overlay_fdt, u32 overlay_fdt_size,
1649 int of_overlay_remove(int *ovcs_id);
1650 int of_overlay_remove_all(void);
1652 int of_overlay_notifier_register(struct notifier_block *nb);
1653 int of_overlay_notifier_unregister(struct notifier_block *nb);
1657 static inline int of_overlay_fdt_apply(void *overlay_fdt, u32 overlay_fdt_size,
1663 static inline int of_overlay_remove(int *ovcs_id)
1668 static inline int of_overlay_remove_all(void)
1673 static inline int of_overlay_notifier_register(struct notifier_block *nb)
1678 static inline int of_overlay_notifier_unregister(struct notifier_block *nb)
1685 #endif /* _LINUX_OF_H */