base.c 9.8 KB

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