4 * Definitions for talking to the Open Firmware PROM on
5 * Power Macintosh and other computers.
7 * Copyright (C) 1996-2005 Paul Mackerras.
9 * Updates for PPC64 by Peter Bergner & David Engebretsen, IBM Corp.
10 * Updates for SPARC64 by David S. Miller
11 * Derived from PowerPC and Sparc prom.h files by Stephen Rothwell, IBM Corp.
13 * This program is free software; you can redistribute it and/or
14 * modify it under the terms of the GNU General Public License
15 * as published by the Free Software Foundation; either version
16 * 2 of the License, or (at your option) any later version.
18 #include <linux/types.h>
19 #include <linux/bitops.h>
20 #include <linux/errno.h>
21 #include <linux/kref.h>
22 #include <linux/mod_devicetable.h>
23 #include <linux/spinlock.h>
24 #include <linux/topology.h>
25 #include <linux/notifier.h>
27 #include <asm/byteorder.h>
28 #include <asm/errno.h>
37 struct property *next;
39 unsigned int unique_id;
42 #if defined(CONFIG_SPARC)
43 struct of_irq_controller;
50 const char *full_name;
52 struct property *properties;
53 struct property *deadprops; /* removed properties */
54 struct device_node *parent;
55 struct device_node *child;
56 struct device_node *sibling;
57 struct device_node *next; /* next device of same type */
58 struct device_node *allnext; /* next in list of all nodes */
59 struct proc_dir_entry *pde; /* this node's proc directory */
63 #if defined(CONFIG_SPARC)
64 const char *path_component_name;
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];
78 * keep the state at iterating a list of phandles with variable number
81 struct of_phandle_iter {
82 const __be32 *cur; /* current phandle */
83 const __be32 *end; /* end of the last phandle */
84 const char *cells_name;
86 struct of_phandle_args out_args;
89 #ifdef CONFIG_OF_DYNAMIC
90 extern struct device_node *of_node_get(struct device_node *node);
91 extern void of_node_put(struct device_node *node);
92 #else /* CONFIG_OF_DYNAMIC */
93 /* Dummy ref counting routines - to be implemented later */
94 static inline struct device_node *of_node_get(struct device_node *node)
98 static inline void of_node_put(struct device_node *node) { }
99 #endif /* !CONFIG_OF_DYNAMIC */
103 /* Pointer for first entry in chain of all nodes. */
104 extern struct device_node *of_allnodes;
105 extern struct device_node *of_chosen;
106 extern struct device_node *of_aliases;
107 extern raw_spinlock_t devtree_lock;
109 static inline bool of_have_populated_dt(void)
111 return of_allnodes != NULL;
114 static inline bool of_node_is_root(const struct device_node *node)
116 return node && (node->parent == NULL);
119 static inline int of_node_check_flag(struct device_node *n, unsigned long flag)
121 return test_bit(flag, &n->_flags);
124 static inline void of_node_set_flag(struct device_node *n, unsigned long flag)
126 set_bit(flag, &n->_flags);
129 extern struct device_node *of_find_all_nodes(struct device_node *prev);
132 * OF address retrieval & translation
135 /* Helper to read a big number; size is in cells (not bytes) */
136 static inline u64 of_read_number(const __be32 *cell, int size)
140 r = (r << 32) | be32_to_cpu(*(cell++));
144 /* Like of_read_number, but we want an unsigned long result */
145 static inline unsigned long of_read_ulong(const __be32 *cell, int size)
147 /* toss away upper bits if unsigned long is smaller than u64 */
148 return of_read_number(cell, size);
151 #include <asm/prom.h>
153 /* Default #address and #size cells. Allow arch asm/prom.h to override */
154 #if !defined(OF_ROOT_NODE_ADDR_CELLS_DEFAULT)
155 #define OF_ROOT_NODE_ADDR_CELLS_DEFAULT 1
156 #define OF_ROOT_NODE_SIZE_CELLS_DEFAULT 1
159 /* Default string compare functions, Allow arch asm/prom.h to override */
160 #if !defined(of_compat_cmp)
161 #define of_compat_cmp(s1, s2, l) strcasecmp((s1), (s2))
162 #define of_prop_cmp(s1, s2) strcmp((s1), (s2))
163 #define of_node_cmp(s1, s2) strcasecmp((s1), (s2))
166 /* flag descriptions */
167 #define OF_DYNAMIC 1 /* node and properties were allocated via kmalloc */
168 #define OF_DETACHED 2 /* node has been detached from the device tree */
170 #define OF_IS_DYNAMIC(x) test_bit(OF_DYNAMIC, &x->_flags)
171 #define OF_MARK_DYNAMIC(x) set_bit(OF_DYNAMIC, &x->_flags)
173 #define OF_BAD_ADDR ((u64)-1)
175 static inline const char *of_node_full_name(const struct device_node *np)
177 return np ? np->full_name : "<no-node>";
180 extern struct device_node *of_find_node_by_name(struct device_node *from,
182 #define for_each_node_by_name(dn, name) \
183 for (dn = of_find_node_by_name(NULL, name); dn; \
184 dn = of_find_node_by_name(dn, name))
185 extern struct device_node *of_find_node_by_type(struct device_node *from,
187 #define for_each_node_by_type(dn, type) \
188 for (dn = of_find_node_by_type(NULL, type); dn; \
189 dn = of_find_node_by_type(dn, type))
190 extern struct device_node *of_find_compatible_node(struct device_node *from,
191 const char *type, const char *compat);
192 #define for_each_compatible_node(dn, type, compatible) \
193 for (dn = of_find_compatible_node(NULL, type, compatible); dn; \
194 dn = of_find_compatible_node(dn, type, compatible))
195 extern struct device_node *of_find_matching_node_and_match(
196 struct device_node *from,
197 const struct of_device_id *matches,
198 const struct of_device_id **match);
199 static inline struct device_node *of_find_matching_node(
200 struct device_node *from,
201 const struct of_device_id *matches)
203 return of_find_matching_node_and_match(from, matches, NULL);
205 #define for_each_matching_node(dn, matches) \
206 for (dn = of_find_matching_node(NULL, matches); dn; \
207 dn = of_find_matching_node(dn, matches))
208 #define for_each_matching_node_and_match(dn, matches, match) \
209 for (dn = of_find_matching_node_and_match(NULL, matches, match); \
210 dn; dn = of_find_matching_node_and_match(dn, matches, match))
211 extern struct device_node *of_find_node_by_path(const char *path);
212 extern struct device_node *of_find_node_by_phandle(phandle handle);
213 extern struct device_node *of_get_parent(const struct device_node *node);
214 extern struct device_node *of_get_next_parent(struct device_node *node);
215 extern struct device_node *of_get_next_child(const struct device_node *node,
216 struct device_node *prev);
217 extern struct device_node *of_get_next_available_child(
218 const struct device_node *node, struct device_node *prev);
220 extern struct device_node *of_get_child_by_name(const struct device_node *node,
222 #define for_each_child_of_node(parent, child) \
223 for (child = of_get_next_child(parent, NULL); child != NULL; \
224 child = of_get_next_child(parent, child))
226 #define for_each_available_child_of_node(parent, child) \
227 for (child = of_get_next_available_child(parent, NULL); child != NULL; \
228 child = of_get_next_available_child(parent, child))
230 static inline int of_get_child_count(const struct device_node *np)
232 struct device_node *child;
235 for_each_child_of_node(np, child)
241 extern struct device_node *of_find_node_with_property(
242 struct device_node *from, const char *prop_name);
243 #define for_each_node_with_property(dn, prop_name) \
244 for (dn = of_find_node_with_property(NULL, prop_name); dn; \
245 dn = of_find_node_with_property(dn, prop_name))
247 extern struct property *of_find_property(const struct device_node *np,
250 extern int of_property_read_u32_index(const struct device_node *np,
251 const char *propname,
252 u32 index, u32 *out_value);
253 extern int of_property_read_u8_array(const struct device_node *np,
254 const char *propname, u8 *out_values, size_t sz);
255 extern int of_property_read_u16_array(const struct device_node *np,
256 const char *propname, u16 *out_values, size_t sz);
257 extern int of_property_read_u32_array(const struct device_node *np,
258 const char *propname,
261 extern int of_property_read_u64(const struct device_node *np,
262 const char *propname, u64 *out_value);
263 extern int of_property_read_u64_array(const struct device_node *np,
264 const char *propname,
268 extern int of_property_read_string(struct device_node *np,
269 const char *propname,
270 const char **out_string);
271 extern int of_property_match_string(struct device_node *np,
272 const char *propname,
274 extern int of_property_read_string_helper(struct device_node *np,
275 const char *propname,
276 const char **out_strs, size_t sz, int index);
277 extern int of_device_is_compatible(const struct device_node *device,
279 extern int of_device_is_available(const struct device_node *device);
280 extern const void *of_get_property(const struct device_node *node,
283 #define for_each_property_of_node(dn, pp) \
284 for (pp = dn->properties; pp != NULL; pp = pp->next)
286 extern int of_n_addr_cells(struct device_node *np);
287 extern int of_n_size_cells(struct device_node *np);
288 extern const struct of_device_id *of_match_node(
289 const struct of_device_id *matches, const struct device_node *node);
290 extern int of_modalias_node(struct device_node *node, char *modalias, int len);
291 extern struct device_node *of_parse_phandle(const struct device_node *np,
292 const char *phandle_name,
294 extern int of_parse_phandle_with_args(const struct device_node *np,
295 const char *list_name, const char *cells_name, int index,
296 struct of_phandle_args *out_args);
297 extern int of_count_phandle_with_args(const struct device_node *np,
298 const char *list_name, const char *cells_name);
300 extern void of_phandle_iter_start(struct of_phandle_iter *iter,
301 const struct device_node *np,
302 const char *list_name,
303 const char *cells_name, int cell_count);
304 extern void of_phandle_iter_next(struct of_phandle_iter *iter);
306 extern void of_alias_scan(void * (*dt_alloc)(u64 size, u64 align));
307 extern int of_alias_get_id(struct device_node *np, const char *stem);
308 extern int of_alias_get_max_id(const char *stem);
310 extern int of_machine_is_compatible(const char *compat);
312 extern int of_add_property(struct device_node *np, struct property *prop);
313 extern int of_remove_property(struct device_node *np, struct property *prop);
314 extern int of_update_property(struct device_node *np, struct property *newprop);
316 /* For updating the device tree at runtime */
317 #define OF_RECONFIG_ATTACH_NODE 0x0001
318 #define OF_RECONFIG_DETACH_NODE 0x0002
319 #define OF_RECONFIG_ADD_PROPERTY 0x0003
320 #define OF_RECONFIG_REMOVE_PROPERTY 0x0004
321 #define OF_RECONFIG_UPDATE_PROPERTY 0x0005
323 struct of_prop_reconfig {
324 struct device_node *dn;
325 struct property *prop;
328 extern int of_reconfig_notifier_register(struct notifier_block *);
329 extern int of_reconfig_notifier_unregister(struct notifier_block *);
330 extern int of_reconfig_notify(unsigned long, void *);
332 extern int of_attach_node(struct device_node *);
333 extern int of_detach_node(struct device_node *);
335 #define of_match_ptr(_ptr) (_ptr)
338 * struct property *prop;
342 * of_property_for_each_u32(np, "propname", prop, p, u)
343 * printk("U32 value: %x\n", u);
345 const __be32 *of_prop_next_u32(struct property *prop, const __be32 *cur,
347 #define of_property_for_each_u32(np, propname, prop, p, u) \
348 for (prop = of_find_property(np, propname, NULL), \
349 p = of_prop_next_u32(prop, NULL, &u); \
351 p = of_prop_next_u32(prop, p, &u))
354 * struct property *prop;
357 * of_property_for_each_string(np, "propname", prop, s)
358 * printk("String value: %s\n", s);
360 const char *of_prop_next_string(struct property *prop, const char *cur);
361 #define of_property_for_each_string(np, propname, prop, s) \
362 for (prop = of_find_property(np, propname, NULL), \
363 s = of_prop_next_string(prop, NULL); \
365 s = of_prop_next_string(prop, s))
367 #define of_property_for_each_phandle_with_args(iter, np, list_name, \
368 cells_name, cell_count) \
369 for (of_phandle_iter_start(&iter, np, list_name, \
370 cells_name, cell_count); \
371 iter.cur; of_phandle_iter_next(&iter))
373 #else /* CONFIG_OF */
375 static inline const char* of_node_full_name(struct device_node *np)
380 static inline struct device_node *of_find_node_by_name(struct device_node *from,
386 static inline struct device_node *of_get_parent(const struct device_node *node)
391 static inline bool of_have_populated_dt(void)
396 #define for_each_child_of_node(parent, child) \
399 static inline struct device_node *of_get_child_by_name(
400 const struct device_node *node,
406 static inline int of_get_child_count(const struct device_node *np)
411 static inline int of_device_is_compatible(const struct device_node *device,
417 static inline int of_device_is_available(const struct device_node *device)
422 static inline struct property *of_find_property(const struct device_node *np,
429 static inline struct device_node *of_find_compatible_node(
430 struct device_node *from,
437 static inline int of_property_read_u32_index(const struct device_node *np,
438 const char *propname, u32 index, u32 *out_value)
443 static inline int of_property_read_u8_array(const struct device_node *np,
444 const char *propname, u8 *out_values, size_t sz)
449 static inline int of_property_read_u16_array(const struct device_node *np,
450 const char *propname, u16 *out_values, size_t sz)
455 static inline int of_property_read_u32_array(const struct device_node *np,
456 const char *propname,
457 u32 *out_values, size_t sz)
462 static inline int of_property_read_u64_array(const struct device_node *np,
463 const char *propname,
464 u64 *out_values, size_t sz)
469 static inline int of_property_read_string(struct device_node *np,
470 const char *propname,
471 const char **out_string)
476 static inline int of_property_read_string_helper(struct device_node *np,
477 const char *propname,
478 const char **out_strs, size_t sz, int index)
483 static inline const void *of_get_property(const struct device_node *node,
490 static inline int of_property_read_u64(const struct device_node *np,
491 const char *propname, u64 *out_value)
496 static inline int of_property_match_string(struct device_node *np,
497 const char *propname,
503 static inline struct device_node *of_parse_phandle(const struct device_node *np,
504 const char *phandle_name,
510 static inline int of_parse_phandle_with_args(struct device_node *np,
511 const char *list_name,
512 const char *cells_name,
514 struct of_phandle_args *out_args)
519 static inline int of_count_phandle_with_args(struct device_node *np,
520 const char *list_name,
521 const char *cells_name)
526 static inline void of_phandle_iter_start(struct of_phandle_iter *iter,
527 const struct device_node *np,
528 const char *list_name,
529 const char *cells_name,
534 static inline void of_phandle_iter_next(struct of_phandle_iter *iter)
538 static inline int of_alias_get_id(struct device_node *np, const char *stem)
543 static inline int of_alias_get_max_id(const char *stem)
548 static inline int of_machine_is_compatible(const char *compat)
553 #define of_match_ptr(_ptr) NULL
554 #define of_match_node(_matches, _node) NULL
555 #define of_property_for_each_u32(np, propname, prop, p, u) \
557 #define of_property_for_each_string(np, propname, prop, s) \
559 #endif /* CONFIG_OF */
561 #ifndef of_node_to_nid
562 static inline int of_node_to_nid(struct device_node *np)
564 return numa_node_id();
567 #define of_node_to_nid of_node_to_nid
571 * of_property_read_string_array() - Read an array of strings from a multiple
573 * @np: device node from which the property value is to be read.
574 * @propname: name of the property to be searched.
575 * @out_strs: output array of string pointers.
576 * @sz: number of array elements to read.
578 * Search for a property in a device tree node and retrieve a list of
579 * terminated string values (pointer to data, not a copy) in that property.
581 * If @out_strs is NULL, the number of strings in the property is returned.
583 static inline int of_property_read_string_array(struct device_node *np,
584 const char *propname, const char **out_strs,
587 return of_property_read_string_helper(np, propname, out_strs, sz, 0);
591 * of_property_count_strings() - Find and return the number of strings from a
592 * multiple strings property.
593 * @np: device node from which the property value is to be read.
594 * @propname: name of the property to be searched.
596 * Search for a property in a device tree node and retrieve the number of null
597 * terminated string contain in it. Returns the number of strings on
598 * success, -EINVAL if the property does not exist, -ENODATA if property
599 * does not have a value, and -EILSEQ if the string is not null-terminated
600 * within the length of the property data.
602 static inline int of_property_count_strings(struct device_node *np,
603 const char *propname)
605 return of_property_read_string_helper(np, propname, NULL, 0, 0);
609 * of_property_read_string_index() - Find and read a string from a multiple
611 * @np: device node from which the property value is to be read.
612 * @propname: name of the property to be searched.
613 * @index: index of the string in the list of strings
614 * @out_string: pointer to null terminated return string, modified only if
617 * Search for a property in a device tree node and retrieve a null
618 * terminated string value (pointer to data, not a copy) in the list of strings
619 * contained in that property.
620 * Returns 0 on success, -EINVAL if the property does not exist, -ENODATA if
621 * property does not have a value, and -EILSEQ if the string is not
622 * null-terminated within the length of the property data.
624 * The out_string pointer is modified only if a valid string can be decoded.
626 static inline int of_property_read_string_index(struct device_node *np,
627 const char *propname,
628 int index, const char **output)
630 int rc = of_property_read_string_helper(np, propname, output, 1, index);
631 return rc < 0 ? rc : 0;
635 * of_property_read_bool - Findfrom a property
636 * @np: device node from which the property value is to be read.
637 * @propname: name of the property to be searched.
639 * Search for a property in a device node.
640 * Returns true if the property exist false otherwise.
642 static inline bool of_property_read_bool(const struct device_node *np,
643 const char *propname)
645 struct property *prop = of_find_property(np, propname, NULL);
647 return prop ? true : false;
650 static inline int of_property_read_u8(const struct device_node *np,
651 const char *propname,
654 return of_property_read_u8_array(np, propname, out_value, 1);
657 static inline int of_property_read_u16(const struct device_node *np,
658 const char *propname,
661 return of_property_read_u16_array(np, propname, out_value, 1);
664 static inline int of_property_read_u32(const struct device_node *np,
665 const char *propname,
668 return of_property_read_u32_array(np, propname, out_value, 1);
671 static inline int of_property_count_u32(const struct device_node *np,
672 const char *propname)
674 const void *prop_ptr;
677 prop_ptr = of_get_property(np, propname, &prop_len);
681 return prop_len / sizeof(u32);
684 static inline int of_property_read_s32(const struct device_node *np,
685 const char *propname,
691 ret = of_property_read_u32(np, propname, &val);
694 *out_value = (val & 0x80000000U) ? -((val ^ 0xFFFFFFFFU) + 1) : val;
698 #if defined(CONFIG_PROC_FS) && defined(CONFIG_PROC_DEVICETREE)
699 extern void proc_device_tree_add_node(struct device_node *, struct proc_dir_entry *);
700 extern void proc_device_tree_add_prop(struct proc_dir_entry *pde, struct property *prop);
701 extern void proc_device_tree_remove_prop(struct proc_dir_entry *pde,
702 struct property *prop);
703 extern void proc_device_tree_update_prop(struct proc_dir_entry *pde,
704 struct property *newprop,
705 struct property *oldprop);
708 #endif /* _LINUX_OF_H */