base.c 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  1. /*
  2. * Procedures for creating, accessing and interpreting the device tree.
  3. *
  4. * Paul Mackerras August 1996.
  5. * Copyright (C) 1996-2005 Paul Mackerras.
  6. *
  7. * Adapted for 64bit PowerPC by Dave Engebretsen and Peter Bergner.
  8. * {engebret|bergner}@us.ibm.com
  9. *
  10. * Adapted for sparc and sparc64 by David S. Miller davem@davemloft.net
  11. *
  12. * Reconsolidated from arch/x/kernel/prom.c by Stephen Rothwell.
  13. *
  14. * This program is free software; you can redistribute it and/or
  15. * modify it under the terms of the GNU General Public License
  16. * as published by the Free Software Foundation; either version
  17. * 2 of the License, or (at your option) any later version.
  18. */
  19. #include <linux/module.h>
  20. #include <linux/of.h>
  21. #include <linux/spinlock.h>
  22. struct device_node *allnodes;
  23. /* use when traversing tree through the allnext, child, sibling,
  24. * or parent members of struct device_node.
  25. */
  26. DEFINE_RWLOCK(devtree_lock);
  27. int of_n_addr_cells(struct device_node *np)
  28. {
  29. const int *ip;
  30. do {
  31. if (np->parent)
  32. np = np->parent;
  33. ip = of_get_property(np, "#address-cells", NULL);
  34. if (ip)
  35. return *ip;
  36. } while (np->parent);
  37. /* No #address-cells property for the root node */
  38. return OF_ROOT_NODE_ADDR_CELLS_DEFAULT;
  39. }
  40. EXPORT_SYMBOL(of_n_addr_cells);
  41. int of_n_size_cells(struct device_node *np)
  42. {
  43. const int *ip;
  44. do {
  45. if (np->parent)
  46. np = np->parent;
  47. ip = of_get_property(np, "#size-cells", NULL);
  48. if (ip)
  49. return *ip;
  50. } while (np->parent);
  51. /* No #size-cells property for the root node */
  52. return OF_ROOT_NODE_SIZE_CELLS_DEFAULT;
  53. }
  54. EXPORT_SYMBOL(of_n_size_cells);
  55. struct property *of_find_property(const struct device_node *np,
  56. const char *name,
  57. int *lenp)
  58. {
  59. struct property *pp;
  60. read_lock(&devtree_lock);
  61. for (pp = np->properties; pp != 0; pp = pp->next) {
  62. if (of_prop_cmp(pp->name, name) == 0) {
  63. if (lenp != 0)
  64. *lenp = pp->length;
  65. break;
  66. }
  67. }
  68. read_unlock(&devtree_lock);
  69. return pp;
  70. }
  71. EXPORT_SYMBOL(of_find_property);
  72. /*
  73. * Find a property with a given name for a given node
  74. * and return the value.
  75. */
  76. const void *of_get_property(const struct device_node *np, const char *name,
  77. int *lenp)
  78. {
  79. struct property *pp = of_find_property(np, name, lenp);
  80. return pp ? pp->value : NULL;
  81. }
  82. EXPORT_SYMBOL(of_get_property);
  83. /** Checks if the given "compat" string matches one of the strings in
  84. * the device's "compatible" property
  85. */
  86. int of_device_is_compatible(const struct device_node *device,
  87. const char *compat)
  88. {
  89. const char* cp;
  90. int cplen, l;
  91. cp = of_get_property(device, "compatible", &cplen);
  92. if (cp == NULL)
  93. return 0;
  94. while (cplen > 0) {
  95. if (of_compat_cmp(cp, compat, strlen(compat)) == 0)
  96. return 1;
  97. l = strlen(cp) + 1;
  98. cp += l;
  99. cplen -= l;
  100. }
  101. return 0;
  102. }
  103. EXPORT_SYMBOL(of_device_is_compatible);
  104. /**
  105. * of_get_parent - Get a node's parent if any
  106. * @node: Node to get parent
  107. *
  108. * Returns a node pointer with refcount incremented, use
  109. * of_node_put() on it when done.
  110. */
  111. struct device_node *of_get_parent(const struct device_node *node)
  112. {
  113. struct device_node *np;
  114. if (!node)
  115. return NULL;
  116. read_lock(&devtree_lock);
  117. np = of_node_get(node->parent);
  118. read_unlock(&devtree_lock);
  119. return np;
  120. }
  121. EXPORT_SYMBOL(of_get_parent);
  122. /**
  123. * of_get_next_child - Iterate a node childs
  124. * @node: parent node
  125. * @prev: previous child of the parent node, or NULL to get first
  126. *
  127. * Returns a node pointer with refcount incremented, use
  128. * of_node_put() on it when done.
  129. */
  130. struct device_node *of_get_next_child(const struct device_node *node,
  131. struct device_node *prev)
  132. {
  133. struct device_node *next;
  134. read_lock(&devtree_lock);
  135. next = prev ? prev->sibling : node->child;
  136. for (; next; next = next->sibling)
  137. if (of_node_get(next))
  138. break;
  139. of_node_put(prev);
  140. read_unlock(&devtree_lock);
  141. return next;
  142. }
  143. EXPORT_SYMBOL(of_get_next_child);
  144. /**
  145. * of_find_node_by_path - Find a node matching a full OF path
  146. * @path: The full path to match
  147. *
  148. * Returns a node pointer with refcount incremented, use
  149. * of_node_put() on it when done.
  150. */
  151. struct device_node *of_find_node_by_path(const char *path)
  152. {
  153. struct device_node *np = allnodes;
  154. read_lock(&devtree_lock);
  155. for (; np; np = np->allnext) {
  156. if (np->full_name && (of_node_cmp(np->full_name, path) == 0)
  157. && of_node_get(np))
  158. break;
  159. }
  160. read_unlock(&devtree_lock);
  161. return np;
  162. }
  163. EXPORT_SYMBOL(of_find_node_by_path);
  164. /**
  165. * of_find_node_by_name - Find a node by its "name" property
  166. * @from: The node to start searching from or NULL, the node
  167. * you pass will not be searched, only the next one
  168. * will; typically, you pass what the previous call
  169. * returned. of_node_put() will be called on it
  170. * @name: The name string to match against
  171. *
  172. * Returns a node pointer with refcount incremented, use
  173. * of_node_put() on it when done.
  174. */
  175. struct device_node *of_find_node_by_name(struct device_node *from,
  176. const char *name)
  177. {
  178. struct device_node *np;
  179. read_lock(&devtree_lock);
  180. np = from ? from->allnext : allnodes;
  181. for (; np; np = np->allnext)
  182. if (np->name && (of_node_cmp(np->name, name) == 0)
  183. && of_node_get(np))
  184. break;
  185. of_node_put(from);
  186. read_unlock(&devtree_lock);
  187. return np;
  188. }
  189. EXPORT_SYMBOL(of_find_node_by_name);
  190. /**
  191. * of_find_node_by_type - Find a node by its "device_type" property
  192. * @from: The node to start searching from, or NULL to start searching
  193. * the entire device tree. The node you pass will not be
  194. * searched, only the next one will; typically, you pass
  195. * what the previous call returned. of_node_put() will be
  196. * called on from for you.
  197. * @type: The type string to match against
  198. *
  199. * Returns a node pointer with refcount incremented, use
  200. * of_node_put() on it when done.
  201. */
  202. struct device_node *of_find_node_by_type(struct device_node *from,
  203. const char *type)
  204. {
  205. struct device_node *np;
  206. read_lock(&devtree_lock);
  207. np = from ? from->allnext : allnodes;
  208. for (; np; np = np->allnext)
  209. if (np->type && (of_node_cmp(np->type, type) == 0)
  210. && of_node_get(np))
  211. break;
  212. of_node_put(from);
  213. read_unlock(&devtree_lock);
  214. return np;
  215. }
  216. EXPORT_SYMBOL(of_find_node_by_type);
  217. /**
  218. * of_find_compatible_node - Find a node based on type and one of the
  219. * tokens in its "compatible" property
  220. * @from: The node to start searching from or NULL, the node
  221. * you pass will not be searched, only the next one
  222. * will; typically, you pass what the previous call
  223. * returned. of_node_put() will be called on it
  224. * @type: The type string to match "device_type" or NULL to ignore
  225. * @compatible: The string to match to one of the tokens in the device
  226. * "compatible" list.
  227. *
  228. * Returns a node pointer with refcount incremented, use
  229. * of_node_put() on it when done.
  230. */
  231. struct device_node *of_find_compatible_node(struct device_node *from,
  232. const char *type, const char *compatible)
  233. {
  234. struct device_node *np;
  235. read_lock(&devtree_lock);
  236. np = from ? from->allnext : allnodes;
  237. for (; np; np = np->allnext) {
  238. if (type
  239. && !(np->type && (of_node_cmp(np->type, type) == 0)))
  240. continue;
  241. if (of_device_is_compatible(np, compatible) && of_node_get(np))
  242. break;
  243. }
  244. of_node_put(from);
  245. read_unlock(&devtree_lock);
  246. return np;
  247. }
  248. EXPORT_SYMBOL(of_find_compatible_node);