of.h 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302
  1. #ifndef _LINUX_OF_H
  2. #define _LINUX_OF_H
  3. /*
  4. * Definitions for talking to the Open Firmware PROM on
  5. * Power Macintosh and other computers.
  6. *
  7. * Copyright (C) 1996-2005 Paul Mackerras.
  8. *
  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.
  12. *
  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.
  17. */
  18. #include <linux/types.h>
  19. #include <linux/bitops.h>
  20. #include <linux/kref.h>
  21. #include <linux/mod_devicetable.h>
  22. #include <linux/spinlock.h>
  23. #include <asm/byteorder.h>
  24. typedef u32 phandle;
  25. typedef u32 ihandle;
  26. struct property {
  27. char *name;
  28. int length;
  29. void *value;
  30. struct property *next;
  31. unsigned long _flags;
  32. unsigned int unique_id;
  33. };
  34. #if defined(CONFIG_SPARC)
  35. struct of_irq_controller;
  36. #endif
  37. struct device_node {
  38. const char *name;
  39. const char *type;
  40. phandle phandle;
  41. char *full_name;
  42. struct property *properties;
  43. struct property *deadprops; /* removed properties */
  44. struct device_node *parent;
  45. struct device_node *child;
  46. struct device_node *sibling;
  47. struct device_node *next; /* next device of same type */
  48. struct device_node *allnext; /* next in list of all nodes */
  49. struct proc_dir_entry *pde; /* this node's proc directory */
  50. struct kref kref;
  51. unsigned long _flags;
  52. void *data;
  53. #if defined(CONFIG_SPARC)
  54. char *path_component_name;
  55. unsigned int unique_id;
  56. struct of_irq_controller *irq_trans;
  57. #endif
  58. };
  59. #ifdef CONFIG_OF
  60. /* Pointer for first entry in chain of all nodes. */
  61. extern struct device_node *allnodes;
  62. extern struct device_node *of_chosen;
  63. extern struct device_node *of_aliases;
  64. extern rwlock_t devtree_lock;
  65. static inline bool of_have_populated_dt(void)
  66. {
  67. return allnodes != NULL;
  68. }
  69. static inline bool of_node_is_root(const struct device_node *node)
  70. {
  71. return node && (node->parent == NULL);
  72. }
  73. static inline int of_node_check_flag(struct device_node *n, unsigned long flag)
  74. {
  75. return test_bit(flag, &n->_flags);
  76. }
  77. static inline void of_node_set_flag(struct device_node *n, unsigned long flag)
  78. {
  79. set_bit(flag, &n->_flags);
  80. }
  81. extern struct device_node *of_find_all_nodes(struct device_node *prev);
  82. #if defined(CONFIG_SPARC)
  83. /* Dummy ref counting routines - to be implemented later */
  84. static inline struct device_node *of_node_get(struct device_node *node)
  85. {
  86. return node;
  87. }
  88. static inline void of_node_put(struct device_node *node)
  89. {
  90. }
  91. #else
  92. extern struct device_node *of_node_get(struct device_node *node);
  93. extern void of_node_put(struct device_node *node);
  94. #endif
  95. /*
  96. * OF address retrieval & translation
  97. */
  98. /* Helper to read a big number; size is in cells (not bytes) */
  99. static inline u64 of_read_number(const __be32 *cell, int size)
  100. {
  101. u64 r = 0;
  102. while (size--)
  103. r = (r << 32) | be32_to_cpu(*(cell++));
  104. return r;
  105. }
  106. /* Like of_read_number, but we want an unsigned long result */
  107. static inline unsigned long of_read_ulong(const __be32 *cell, int size)
  108. {
  109. /* toss away upper bits if unsigned long is smaller than u64 */
  110. return of_read_number(cell, size);
  111. }
  112. #include <asm/prom.h>
  113. /* Default #address and #size cells. Allow arch asm/prom.h to override */
  114. #if !defined(OF_ROOT_NODE_ADDR_CELLS_DEFAULT)
  115. #define OF_ROOT_NODE_ADDR_CELLS_DEFAULT 1
  116. #define OF_ROOT_NODE_SIZE_CELLS_DEFAULT 1
  117. #endif
  118. /* Default string compare functions, Allow arch asm/prom.h to override */
  119. #if !defined(of_compat_cmp)
  120. #define of_compat_cmp(s1, s2, l) strcasecmp((s1), (s2))
  121. #define of_prop_cmp(s1, s2) strcmp((s1), (s2))
  122. #define of_node_cmp(s1, s2) strcasecmp((s1), (s2))
  123. #endif
  124. /* flag descriptions */
  125. #define OF_DYNAMIC 1 /* node and properties were allocated via kmalloc */
  126. #define OF_DETACHED 2 /* node has been detached from the device tree */
  127. #define OF_IS_DYNAMIC(x) test_bit(OF_DYNAMIC, &x->_flags)
  128. #define OF_MARK_DYNAMIC(x) set_bit(OF_DYNAMIC, &x->_flags)
  129. #define OF_BAD_ADDR ((u64)-1)
  130. #ifndef of_node_to_nid
  131. static inline int of_node_to_nid(struct device_node *np) { return -1; }
  132. #define of_node_to_nid of_node_to_nid
  133. #endif
  134. extern struct device_node *of_find_node_by_name(struct device_node *from,
  135. const char *name);
  136. #define for_each_node_by_name(dn, name) \
  137. for (dn = of_find_node_by_name(NULL, name); dn; \
  138. dn = of_find_node_by_name(dn, name))
  139. extern struct device_node *of_find_node_by_type(struct device_node *from,
  140. const char *type);
  141. #define for_each_node_by_type(dn, type) \
  142. for (dn = of_find_node_by_type(NULL, type); dn; \
  143. dn = of_find_node_by_type(dn, type))
  144. extern struct device_node *of_find_compatible_node(struct device_node *from,
  145. const char *type, const char *compat);
  146. #define for_each_compatible_node(dn, type, compatible) \
  147. for (dn = of_find_compatible_node(NULL, type, compatible); dn; \
  148. dn = of_find_compatible_node(dn, type, compatible))
  149. extern struct device_node *of_find_matching_node(struct device_node *from,
  150. const struct of_device_id *matches);
  151. #define for_each_matching_node(dn, matches) \
  152. for (dn = of_find_matching_node(NULL, matches); dn; \
  153. dn = of_find_matching_node(dn, matches))
  154. extern struct device_node *of_find_node_by_path(const char *path);
  155. extern struct device_node *of_find_node_by_phandle(phandle handle);
  156. extern struct device_node *of_get_parent(const struct device_node *node);
  157. extern struct device_node *of_get_next_parent(struct device_node *node);
  158. extern struct device_node *of_get_next_child(const struct device_node *node,
  159. struct device_node *prev);
  160. #define for_each_child_of_node(parent, child) \
  161. for (child = of_get_next_child(parent, NULL); child != NULL; \
  162. child = of_get_next_child(parent, child))
  163. extern struct device_node *of_find_node_with_property(
  164. struct device_node *from, const char *prop_name);
  165. #define for_each_node_with_property(dn, prop_name) \
  166. for (dn = of_find_node_with_property(NULL, prop_name); dn; \
  167. dn = of_find_node_with_property(dn, prop_name))
  168. extern struct property *of_find_property(const struct device_node *np,
  169. const char *name,
  170. int *lenp);
  171. extern int of_property_read_u32_array(const struct device_node *np,
  172. const char *propname,
  173. u32 *out_values,
  174. size_t sz);
  175. extern int of_property_read_u64(const struct device_node *np,
  176. const char *propname, u64 *out_value);
  177. extern int of_property_read_string(struct device_node *np,
  178. const char *propname,
  179. const char **out_string);
  180. extern int of_device_is_compatible(const struct device_node *device,
  181. const char *);
  182. extern int of_device_is_available(const struct device_node *device);
  183. extern const void *of_get_property(const struct device_node *node,
  184. const char *name,
  185. int *lenp);
  186. #define for_each_property(pp, properties) \
  187. for (pp = properties; pp != NULL; pp = pp->next)
  188. extern int of_n_addr_cells(struct device_node *np);
  189. extern int of_n_size_cells(struct device_node *np);
  190. extern const struct of_device_id *of_match_node(
  191. const struct of_device_id *matches, const struct device_node *node);
  192. extern int of_modalias_node(struct device_node *node, char *modalias, int len);
  193. extern struct device_node *of_parse_phandle(struct device_node *np,
  194. const char *phandle_name,
  195. int index);
  196. extern int of_parse_phandles_with_args(struct device_node *np,
  197. const char *list_name, const char *cells_name, int index,
  198. struct device_node **out_node, const void **out_args);
  199. extern void of_alias_scan(void * (*dt_alloc)(u64 size, u64 align));
  200. extern int of_alias_get_id(struct device_node *np, const char *stem);
  201. extern int of_machine_is_compatible(const char *compat);
  202. extern int prom_add_property(struct device_node* np, struct property* prop);
  203. extern int prom_remove_property(struct device_node *np, struct property *prop);
  204. extern int prom_update_property(struct device_node *np,
  205. struct property *newprop,
  206. struct property *oldprop);
  207. #if defined(CONFIG_OF_DYNAMIC)
  208. /* For updating the device tree at runtime */
  209. extern void of_attach_node(struct device_node *);
  210. extern void of_detach_node(struct device_node *);
  211. #endif
  212. #define of_match_ptr(_ptr) (_ptr)
  213. #else /* CONFIG_OF */
  214. static inline bool of_have_populated_dt(void)
  215. {
  216. return false;
  217. }
  218. #define for_each_child_of_node(parent, child) \
  219. while (0)
  220. static inline struct property *of_find_property(const struct device_node *np,
  221. const char *name,
  222. int *lenp)
  223. {
  224. return NULL;
  225. }
  226. static inline int of_property_read_u32_array(const struct device_node *np,
  227. const char *propname,
  228. u32 *out_values, size_t sz)
  229. {
  230. return -ENOSYS;
  231. }
  232. static inline int of_property_read_string(struct device_node *np,
  233. const char *propname,
  234. const char **out_string)
  235. {
  236. return -ENOSYS;
  237. }
  238. static inline const void *of_get_property(const struct device_node *node,
  239. const char *name,
  240. int *lenp)
  241. {
  242. return NULL;
  243. }
  244. static inline int of_property_read_u64(const struct device_node *np,
  245. const char *propname, u64 *out_value)
  246. {
  247. return -ENOSYS;
  248. }
  249. #define of_match_ptr(_ptr) NULL
  250. #endif /* CONFIG_OF */
  251. static inline int of_property_read_u32(const struct device_node *np,
  252. const char *propname,
  253. u32 *out_value)
  254. {
  255. return of_property_read_u32_array(np, propname, out_value, 1);
  256. }
  257. #endif /* _LINUX_OF_H */