base.c 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358
  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_parent - Iterate to a node's parent
  124. * @node: Node to get parent of
  125. *
  126. * This is like of_get_parent() except that it drops the
  127. * refcount on the passed node, making it suitable for iterating
  128. * through a node's parents.
  129. *
  130. * Returns a node pointer with refcount incremented, use
  131. * of_node_put() on it when done.
  132. */
  133. struct device_node *of_get_next_parent(struct device_node *node)
  134. {
  135. struct device_node *parent;
  136. if (!node)
  137. return NULL;
  138. read_lock(&devtree_lock);
  139. parent = of_node_get(node->parent);
  140. of_node_put(node);
  141. read_unlock(&devtree_lock);
  142. return parent;
  143. }
  144. /**
  145. * of_get_next_child - Iterate a node childs
  146. * @node: parent node
  147. * @prev: previous child of the parent node, or NULL to get first
  148. *
  149. * Returns a node pointer with refcount incremented, use
  150. * of_node_put() on it when done.
  151. */
  152. struct device_node *of_get_next_child(const struct device_node *node,
  153. struct device_node *prev)
  154. {
  155. struct device_node *next;
  156. read_lock(&devtree_lock);
  157. next = prev ? prev->sibling : node->child;
  158. for (; next; next = next->sibling)
  159. if (of_node_get(next))
  160. break;
  161. of_node_put(prev);
  162. read_unlock(&devtree_lock);
  163. return next;
  164. }
  165. EXPORT_SYMBOL(of_get_next_child);
  166. /**
  167. * of_find_node_by_path - Find a node matching a full OF path
  168. * @path: The full path to match
  169. *
  170. * Returns a node pointer with refcount incremented, use
  171. * of_node_put() on it when done.
  172. */
  173. struct device_node *of_find_node_by_path(const char *path)
  174. {
  175. struct device_node *np = allnodes;
  176. read_lock(&devtree_lock);
  177. for (; np; np = np->allnext) {
  178. if (np->full_name && (of_node_cmp(np->full_name, path) == 0)
  179. && of_node_get(np))
  180. break;
  181. }
  182. read_unlock(&devtree_lock);
  183. return np;
  184. }
  185. EXPORT_SYMBOL(of_find_node_by_path);
  186. /**
  187. * of_find_node_by_name - Find a node by its "name" property
  188. * @from: The node to start searching from or NULL, the node
  189. * you pass will not be searched, only the next one
  190. * will; typically, you pass what the previous call
  191. * returned. of_node_put() will be called on it
  192. * @name: The name string to match against
  193. *
  194. * Returns a node pointer with refcount incremented, use
  195. * of_node_put() on it when done.
  196. */
  197. struct device_node *of_find_node_by_name(struct device_node *from,
  198. const char *name)
  199. {
  200. struct device_node *np;
  201. read_lock(&devtree_lock);
  202. np = from ? from->allnext : allnodes;
  203. for (; np; np = np->allnext)
  204. if (np->name && (of_node_cmp(np->name, name) == 0)
  205. && of_node_get(np))
  206. break;
  207. of_node_put(from);
  208. read_unlock(&devtree_lock);
  209. return np;
  210. }
  211. EXPORT_SYMBOL(of_find_node_by_name);
  212. /**
  213. * of_find_node_by_type - Find a node by its "device_type" property
  214. * @from: The node to start searching from, or NULL to start searching
  215. * the entire device tree. The node you pass will not be
  216. * searched, only the next one will; typically, you pass
  217. * what the previous call returned. of_node_put() will be
  218. * called on from for you.
  219. * @type: The type string to match against
  220. *
  221. * Returns a node pointer with refcount incremented, use
  222. * of_node_put() on it when done.
  223. */
  224. struct device_node *of_find_node_by_type(struct device_node *from,
  225. const char *type)
  226. {
  227. struct device_node *np;
  228. read_lock(&devtree_lock);
  229. np = from ? from->allnext : allnodes;
  230. for (; np; np = np->allnext)
  231. if (np->type && (of_node_cmp(np->type, type) == 0)
  232. && of_node_get(np))
  233. break;
  234. of_node_put(from);
  235. read_unlock(&devtree_lock);
  236. return np;
  237. }
  238. EXPORT_SYMBOL(of_find_node_by_type);
  239. /**
  240. * of_find_compatible_node - Find a node based on type and one of the
  241. * tokens in its "compatible" property
  242. * @from: The node to start searching from or NULL, the node
  243. * you pass will not be searched, only the next one
  244. * will; typically, you pass what the previous call
  245. * returned. of_node_put() will be called on it
  246. * @type: The type string to match "device_type" or NULL to ignore
  247. * @compatible: The string to match to one of the tokens in the device
  248. * "compatible" list.
  249. *
  250. * Returns a node pointer with refcount incremented, use
  251. * of_node_put() on it when done.
  252. */
  253. struct device_node *of_find_compatible_node(struct device_node *from,
  254. const char *type, const char *compatible)
  255. {
  256. struct device_node *np;
  257. read_lock(&devtree_lock);
  258. np = from ? from->allnext : allnodes;
  259. for (; np; np = np->allnext) {
  260. if (type
  261. && !(np->type && (of_node_cmp(np->type, type) == 0)))
  262. continue;
  263. if (of_device_is_compatible(np, compatible) && of_node_get(np))
  264. break;
  265. }
  266. of_node_put(from);
  267. read_unlock(&devtree_lock);
  268. return np;
  269. }
  270. EXPORT_SYMBOL(of_find_compatible_node);
  271. /**
  272. * of_match_node - Tell if an device_node has a matching of_match structure
  273. * @matches: array of of device match structures to search in
  274. * @node: the of device structure to match against
  275. *
  276. * Low level utility function used by device matching.
  277. */
  278. const struct of_device_id *of_match_node(const struct of_device_id *matches,
  279. const struct device_node *node)
  280. {
  281. while (matches->name[0] || matches->type[0] || matches->compatible[0]) {
  282. int match = 1;
  283. if (matches->name[0])
  284. match &= node->name
  285. && !strcmp(matches->name, node->name);
  286. if (matches->type[0])
  287. match &= node->type
  288. && !strcmp(matches->type, node->type);
  289. if (matches->compatible[0])
  290. match &= of_device_is_compatible(node,
  291. matches->compatible);
  292. if (match)
  293. return matches;
  294. matches++;
  295. }
  296. return NULL;
  297. }
  298. EXPORT_SYMBOL(of_match_node);
  299. /**
  300. * of_find_matching_node - Find a node based on an of_device_id match
  301. * table.
  302. * @from: The node to start searching from or NULL, the node
  303. * you pass will not be searched, only the next one
  304. * will; typically, you pass what the previous call
  305. * returned. of_node_put() will be called on it
  306. * @matches: array of of device match structures to search in
  307. *
  308. * Returns a node pointer with refcount incremented, use
  309. * of_node_put() on it when done.
  310. */
  311. struct device_node *of_find_matching_node(struct device_node *from,
  312. const struct of_device_id *matches)
  313. {
  314. struct device_node *np;
  315. read_lock(&devtree_lock);
  316. np = from ? from->allnext : allnodes;
  317. for (; np; np = np->allnext) {
  318. if (of_match_node(matches, np) && of_node_get(np))
  319. break;
  320. }
  321. of_node_put(from);
  322. read_unlock(&devtree_lock);
  323. return np;
  324. }
  325. EXPORT_SYMBOL(of_find_matching_node);