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/kobject.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;
40 struct bin_attribute attr;
43 #if defined(CONFIG_SPARC)
44 struct of_irq_controller;
51 const char *full_name;
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 struct device_node *next; /* next device of same type */
59 struct device_node *allnext; /* next in list of all nodes */
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];
77 extern int of_node_add(struct device_node *node);
79 #ifdef CONFIG_OF_DYNAMIC
80 extern struct device_node *of_node_get(struct device_node *node);
81 extern void of_node_put(struct device_node *node);
82 #else /* CONFIG_OF_DYNAMIC */
83 /* Dummy ref counting routines - to be implemented later */
84 static inline struct device_node *of_node_get(struct device_node *node)
88 static inline void of_node_put(struct device_node *node) { }
89 #endif /* !CONFIG_OF_DYNAMIC */
93 /* Pointer for first entry in chain of all nodes. */
94 extern struct device_node *of_allnodes;
95 extern struct device_node *of_chosen;
96 extern struct device_node *of_aliases;
97 extern raw_spinlock_t devtree_lock;
99 static inline bool of_have_populated_dt(void)
101 return of_allnodes != NULL;
104 static inline bool of_node_is_root(const struct device_node *node)
106 return node && (node->parent == NULL);
109 static inline int of_node_check_flag(struct device_node *n, unsigned long flag)
111 return test_bit(flag, &n->_flags);
114 static inline void of_node_set_flag(struct device_node *n, unsigned long flag)
116 set_bit(flag, &n->_flags);
119 static inline void of_node_clear_flag(struct device_node *n, unsigned long flag)
121 clear_bit(flag, &n->_flags);
124 static inline int of_property_check_flag(struct property *p, unsigned long flag)
126 return test_bit(flag, &p->_flags);
129 static inline void of_property_set_flag(struct property *p, unsigned long flag)
131 set_bit(flag, &p->_flags);
134 static inline void of_property_clear_flag(struct property *p, unsigned long flag)
136 clear_bit(flag, &p->_flags);
139 extern struct device_node *of_find_all_nodes(struct device_node *prev);
142 * OF address retrieval & translation
145 /* Helper to read a big number; size is in cells (not bytes) */
146 static inline u64 of_read_number(const __be32 *cell, int size)
150 r = (r << 32) | be32_to_cpu(*(cell++));
154 /* Like of_read_number, but we want an unsigned long result */
155 static inline unsigned long of_read_ulong(const __be32 *cell, int size)
157 /* toss away upper bits if unsigned long is smaller than u64 */
158 return of_read_number(cell, size);
161 #if defined(CONFIG_SPARC)
162 #include <asm/prom.h>
165 /* Default #address and #size cells. Allow arch asm/prom.h to override */
166 #if !defined(OF_ROOT_NODE_ADDR_CELLS_DEFAULT)
167 #define OF_ROOT_NODE_ADDR_CELLS_DEFAULT 1
168 #define OF_ROOT_NODE_SIZE_CELLS_DEFAULT 1
171 /* Default string compare functions, Allow arch asm/prom.h to override */
172 #if !defined(of_compat_cmp)
173 #define of_compat_cmp(s1, s2, l) strcasecmp((s1), (s2))
174 #define of_prop_cmp(s1, s2) strcmp((s1), (s2))
175 #define of_node_cmp(s1, s2) strcasecmp((s1), (s2))
178 /* flag descriptions */
179 #define OF_DYNAMIC 1 /* node and properties were allocated via kmalloc */
180 #define OF_DETACHED 2 /* node has been detached from the device tree */
182 #define OF_IS_DYNAMIC(x) test_bit(OF_DYNAMIC, &x->_flags)
183 #define OF_MARK_DYNAMIC(x) set_bit(OF_DYNAMIC, &x->_flags)
185 #define OF_BAD_ADDR ((u64)-1)
187 static inline const char *of_node_full_name(const struct device_node *np)
189 return np ? np->full_name : "<no-node>";
192 #define for_each_of_allnodes(dn) \
193 for (dn = of_allnodes; dn; dn = dn->allnext)
194 extern struct device_node *of_find_node_by_name(struct device_node *from,
196 extern struct device_node *of_find_node_by_type(struct device_node *from,
198 extern struct device_node *of_find_compatible_node(struct device_node *from,
199 const char *type, const char *compat);
200 extern struct device_node *of_find_matching_node_and_match(
201 struct device_node *from,
202 const struct of_device_id *matches,
203 const struct of_device_id **match);
205 extern struct device_node *of_find_node_by_path(const char *path);
206 extern struct device_node *of_find_node_by_phandle(phandle handle);
207 extern struct device_node *of_get_parent(const struct device_node *node);
208 extern struct device_node *of_get_next_parent(struct device_node *node);
209 extern struct device_node *of_get_next_child(const struct device_node *node,
210 struct device_node *prev);
211 extern struct device_node *of_get_next_available_child(
212 const struct device_node *node, struct device_node *prev);
214 extern struct device_node *of_get_child_by_name(const struct device_node *node,
218 extern struct device_node *of_find_next_cache_node(const struct device_node *);
219 extern struct device_node *of_find_node_with_property(
220 struct device_node *from, const char *prop_name);
222 extern struct property *of_find_property(const struct device_node *np,
225 extern int of_property_read_u32_index(const struct device_node *np,
226 const char *propname,
227 u32 index, u32 *out_value);
228 extern int of_property_read_u8_array(const struct device_node *np,
229 const char *propname, u8 *out_values, size_t sz);
230 extern int of_property_read_u16_array(const struct device_node *np,
231 const char *propname, u16 *out_values, size_t sz);
232 extern int of_property_read_u32_array(const struct device_node *np,
233 const char *propname,
236 extern int of_property_read_u64(const struct device_node *np,
237 const char *propname, u64 *out_value);
239 extern int of_property_read_string(struct device_node *np,
240 const char *propname,
241 const char **out_string);
242 extern int of_property_read_string_index(struct device_node *np,
243 const char *propname,
244 int index, const char **output);
245 extern int of_property_match_string(struct device_node *np,
246 const char *propname,
248 extern int of_property_count_strings(struct device_node *np,
249 const char *propname);
250 extern int of_device_is_compatible(const struct device_node *device,
252 extern int of_device_is_available(const struct device_node *device);
253 extern const void *of_get_property(const struct device_node *node,
256 extern struct device_node *of_get_cpu_node(int cpu, unsigned int *thread);
257 #define for_each_property_of_node(dn, pp) \
258 for (pp = dn->properties; pp != NULL; pp = pp->next)
260 extern int of_n_addr_cells(struct device_node *np);
261 extern int of_n_size_cells(struct device_node *np);
262 extern const struct of_device_id *of_match_node(
263 const struct of_device_id *matches, const struct device_node *node);
264 extern int of_modalias_node(struct device_node *node, char *modalias, int len);
265 extern void of_print_phandle_args(const char *msg, const struct of_phandle_args *args);
266 extern struct device_node *of_parse_phandle(const struct device_node *np,
267 const char *phandle_name,
269 extern int of_parse_phandle_with_args(const struct device_node *np,
270 const char *list_name, const char *cells_name, int index,
271 struct of_phandle_args *out_args);
272 extern int of_parse_phandle_with_fixed_args(const struct device_node *np,
273 const char *list_name, int cells_count, int index,
274 struct of_phandle_args *out_args);
275 extern int of_count_phandle_with_args(const struct device_node *np,
276 const char *list_name, const char *cells_name);
278 extern void of_alias_scan(void * (*dt_alloc)(u64 size, u64 align));
279 extern int of_alias_get_id(struct device_node *np, const char *stem);
281 extern int of_machine_is_compatible(const char *compat);
283 extern int of_add_property(struct device_node *np, struct property *prop);
284 extern int of_remove_property(struct device_node *np, struct property *prop);
285 extern int of_update_property(struct device_node *np, struct property *newprop);
287 /* For updating the device tree at runtime */
288 #define OF_RECONFIG_ATTACH_NODE 0x0001
289 #define OF_RECONFIG_DETACH_NODE 0x0002
290 #define OF_RECONFIG_ADD_PROPERTY 0x0003
291 #define OF_RECONFIG_REMOVE_PROPERTY 0x0004
292 #define OF_RECONFIG_UPDATE_PROPERTY 0x0005
294 struct of_prop_reconfig {
295 struct device_node *dn;
296 struct property *prop;
299 extern int of_reconfig_notifier_register(struct notifier_block *);
300 extern int of_reconfig_notifier_unregister(struct notifier_block *);
301 extern int of_reconfig_notify(unsigned long, void *);
303 extern int of_attach_node(struct device_node *);
304 extern int of_detach_node(struct device_node *);
306 #define of_match_ptr(_ptr) (_ptr)
309 * struct property *prop;
313 * of_property_for_each_u32(np, "propname", prop, p, u)
314 * printk("U32 value: %x\n", u);
316 const __be32 *of_prop_next_u32(struct property *prop, const __be32 *cur,
319 * struct property *prop;
322 * of_property_for_each_string(np, "propname", prop, s)
323 * printk("String value: %s\n", s);
325 const char *of_prop_next_string(struct property *prop, const char *cur);
327 int of_device_is_stdout_path(struct device_node *dn);
329 #else /* CONFIG_OF */
331 static inline const char* of_node_full_name(struct device_node *np)
336 static inline struct device_node *of_find_node_by_name(struct device_node *from,
342 static inline struct device_node *of_find_node_by_type(struct device_node *from,
348 static inline struct device_node *of_find_matching_node_and_match(
349 struct device_node *from,
350 const struct of_device_id *matches,
351 const struct of_device_id **match)
356 static inline struct device_node *of_get_parent(const struct device_node *node)
361 static inline struct device_node *of_get_next_child(
362 const struct device_node *node, struct device_node *prev)
367 static inline struct device_node *of_get_next_available_child(
368 const struct device_node *node, struct device_node *prev)
373 static inline struct device_node *of_find_node_with_property(
374 struct device_node *from, const char *prop_name)
379 static inline bool of_have_populated_dt(void)
384 static inline struct device_node *of_get_child_by_name(
385 const struct device_node *node,
391 static inline int of_device_is_compatible(const struct device_node *device,
397 static inline int of_device_is_available(const struct device_node *device)
402 static inline struct property *of_find_property(const struct device_node *np,
409 static inline struct device_node *of_find_compatible_node(
410 struct device_node *from,
417 static inline int of_property_read_u32_index(const struct device_node *np,
418 const char *propname, u32 index, u32 *out_value)
423 static inline int of_property_read_u8_array(const struct device_node *np,
424 const char *propname, u8 *out_values, size_t sz)
429 static inline int of_property_read_u16_array(const struct device_node *np,
430 const char *propname, u16 *out_values, size_t sz)
435 static inline int of_property_read_u32_array(const struct device_node *np,
436 const char *propname,
437 u32 *out_values, size_t sz)
442 static inline int of_property_read_string(struct device_node *np,
443 const char *propname,
444 const char **out_string)
449 static inline int of_property_read_string_index(struct device_node *np,
450 const char *propname, int index,
451 const char **out_string)
456 static inline int of_property_count_strings(struct device_node *np,
457 const char *propname)
462 static inline const void *of_get_property(const struct device_node *node,
469 static inline struct device_node *of_get_cpu_node(int cpu,
470 unsigned int *thread)
475 static inline int of_property_read_u64(const struct device_node *np,
476 const char *propname, u64 *out_value)
481 static inline int of_property_match_string(struct device_node *np,
482 const char *propname,
488 static inline struct device_node *of_parse_phandle(const struct device_node *np,
489 const char *phandle_name,
495 static inline int of_parse_phandle_with_args(struct device_node *np,
496 const char *list_name,
497 const char *cells_name,
499 struct of_phandle_args *out_args)
504 static inline int of_parse_phandle_with_fixed_args(const struct device_node *np,
505 const char *list_name, int cells_count, int index,
506 struct of_phandle_args *out_args)
511 static inline int of_count_phandle_with_args(struct device_node *np,
512 const char *list_name,
513 const char *cells_name)
518 static inline int of_alias_get_id(struct device_node *np, const char *stem)
523 static inline int of_machine_is_compatible(const char *compat)
528 static inline int of_device_is_stdout_path(struct device_node *dn)
533 static inline const __be32 *of_prop_next_u32(struct property *prop,
534 const __be32 *cur, u32 *pu)
539 static inline const char *of_prop_next_string(struct property *prop,
545 #define of_match_ptr(_ptr) NULL
546 #define of_match_node(_matches, _node) NULL
547 #endif /* CONFIG_OF */
549 #if defined(CONFIG_OF) && defined(CONFIG_NUMA)
550 extern int of_node_to_nid(struct device_node *np);
552 static inline int of_node_to_nid(struct device_node *device) { return 0; }
555 static inline struct device_node *of_find_matching_node(
556 struct device_node *from,
557 const struct of_device_id *matches)
559 return of_find_matching_node_and_match(from, matches, NULL);
563 * of_property_read_bool - Findfrom a property
564 * @np: device node from which the property value is to be read.
565 * @propname: name of the property to be searched.
567 * Search for a property in a device node.
568 * Returns true if the property exist false otherwise.
570 static inline bool of_property_read_bool(const struct device_node *np,
571 const char *propname)
573 struct property *prop = of_find_property(np, propname, NULL);
575 return prop ? true : false;
578 static inline int of_property_read_u8(const struct device_node *np,
579 const char *propname,
582 return of_property_read_u8_array(np, propname, out_value, 1);
585 static inline int of_property_read_u16(const struct device_node *np,
586 const char *propname,
589 return of_property_read_u16_array(np, propname, out_value, 1);
592 static inline int of_property_read_u32(const struct device_node *np,
593 const char *propname,
596 return of_property_read_u32_array(np, propname, out_value, 1);
599 #define of_property_for_each_u32(np, propname, prop, p, u) \
600 for (prop = of_find_property(np, propname, NULL), \
601 p = of_prop_next_u32(prop, NULL, &u); \
603 p = of_prop_next_u32(prop, p, &u))
605 #define of_property_for_each_string(np, propname, prop, s) \
606 for (prop = of_find_property(np, propname, NULL), \
607 s = of_prop_next_string(prop, NULL); \
609 s = of_prop_next_string(prop, s))
611 #define for_each_node_by_name(dn, name) \
612 for (dn = of_find_node_by_name(NULL, name); dn; \
613 dn = of_find_node_by_name(dn, name))
614 #define for_each_node_by_type(dn, type) \
615 for (dn = of_find_node_by_type(NULL, type); dn; \
616 dn = of_find_node_by_type(dn, type))
617 #define for_each_compatible_node(dn, type, compatible) \
618 for (dn = of_find_compatible_node(NULL, type, compatible); dn; \
619 dn = of_find_compatible_node(dn, type, compatible))
620 #define for_each_matching_node(dn, matches) \
621 for (dn = of_find_matching_node(NULL, matches); dn; \
622 dn = of_find_matching_node(dn, matches))
623 #define for_each_matching_node_and_match(dn, matches, match) \
624 for (dn = of_find_matching_node_and_match(NULL, matches, match); \
625 dn; dn = of_find_matching_node_and_match(dn, matches, match))
627 #define for_each_child_of_node(parent, child) \
628 for (child = of_get_next_child(parent, NULL); child != NULL; \
629 child = of_get_next_child(parent, child))
630 #define for_each_available_child_of_node(parent, child) \
631 for (child = of_get_next_available_child(parent, NULL); child != NULL; \
632 child = of_get_next_available_child(parent, child))
634 #define for_each_node_with_property(dn, prop_name) \
635 for (dn = of_find_node_with_property(NULL, prop_name); dn; \
636 dn = of_find_node_with_property(dn, prop_name))
638 static inline int of_get_child_count(const struct device_node *np)
640 struct device_node *child;
643 for_each_child_of_node(np, child)
649 static inline int of_get_available_child_count(const struct device_node *np)
651 struct device_node *child;
654 for_each_available_child_of_node(np, child)
660 #endif /* _LINUX_OF_H */