base.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475
  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. if (!np)
  61. return NULL;
  62. read_lock(&devtree_lock);
  63. for (pp = np->properties; pp != 0; pp = pp->next) {
  64. if (of_prop_cmp(pp->name, name) == 0) {
  65. if (lenp != 0)
  66. *lenp = pp->length;
  67. break;
  68. }
  69. }
  70. read_unlock(&devtree_lock);
  71. return pp;
  72. }
  73. EXPORT_SYMBOL(of_find_property);
  74. /*
  75. * Find a property with a given name for a given node
  76. * and return the value.
  77. */
  78. const void *of_get_property(const struct device_node *np, const char *name,
  79. int *lenp)
  80. {
  81. struct property *pp = of_find_property(np, name, lenp);
  82. return pp ? pp->value : NULL;
  83. }
  84. EXPORT_SYMBOL(of_get_property);
  85. /** Checks if the given "compat" string matches one of the strings in
  86. * the device's "compatible" property
  87. */
  88. int of_device_is_compatible(const struct device_node *device,
  89. const char *compat)
  90. {
  91. const char* cp;
  92. int cplen, l;
  93. cp = of_get_property(device, "compatible", &cplen);
  94. if (cp == NULL)
  95. return 0;
  96. while (cplen > 0) {
  97. if (of_compat_cmp(cp, compat, strlen(compat)) == 0)
  98. return 1;
  99. l = strlen(cp) + 1;
  100. cp += l;
  101. cplen -= l;
  102. }
  103. return 0;
  104. }
  105. EXPORT_SYMBOL(of_device_is_compatible);
  106. /**
  107. * of_device_is_available - check if a device is available for use
  108. *
  109. * @device: Node to check for availability
  110. *
  111. * Returns 1 if the status property is absent or set to "okay" or "ok",
  112. * 0 otherwise
  113. */
  114. int of_device_is_available(const struct device_node *device)
  115. {
  116. const char *status;
  117. int statlen;
  118. status = of_get_property(device, "status", &statlen);
  119. if (status == NULL)
  120. return 1;
  121. if (statlen > 0) {
  122. if (!strcmp(status, "okay") || !strcmp(status, "ok"))
  123. return 1;
  124. }
  125. return 0;
  126. }
  127. EXPORT_SYMBOL(of_device_is_available);
  128. /**
  129. * of_get_parent - Get a node's parent if any
  130. * @node: Node to get parent
  131. *
  132. * Returns a node pointer with refcount incremented, use
  133. * of_node_put() on it when done.
  134. */
  135. struct device_node *of_get_parent(const struct device_node *node)
  136. {
  137. struct device_node *np;
  138. if (!node)
  139. return NULL;
  140. read_lock(&devtree_lock);
  141. np = of_node_get(node->parent);
  142. read_unlock(&devtree_lock);
  143. return np;
  144. }
  145. EXPORT_SYMBOL(of_get_parent);
  146. /**
  147. * of_get_next_parent - Iterate to a node's parent
  148. * @node: Node to get parent of
  149. *
  150. * This is like of_get_parent() except that it drops the
  151. * refcount on the passed node, making it suitable for iterating
  152. * through a node's parents.
  153. *
  154. * Returns a node pointer with refcount incremented, use
  155. * of_node_put() on it when done.
  156. */
  157. struct device_node *of_get_next_parent(struct device_node *node)
  158. {
  159. struct device_node *parent;
  160. if (!node)
  161. return NULL;
  162. read_lock(&devtree_lock);
  163. parent = of_node_get(node->parent);
  164. of_node_put(node);
  165. read_unlock(&devtree_lock);
  166. return parent;
  167. }
  168. /**
  169. * of_get_next_child - Iterate a node childs
  170. * @node: parent node
  171. * @prev: previous child of the parent node, or NULL to get first
  172. *
  173. * Returns a node pointer with refcount incremented, use
  174. * of_node_put() on it when done.
  175. */
  176. struct device_node *of_get_next_child(const struct device_node *node,
  177. struct device_node *prev)
  178. {
  179. struct device_node *next;
  180. read_lock(&devtree_lock);
  181. next = prev ? prev->sibling : node->child;
  182. for (; next; next = next->sibling)
  183. if (of_node_get(next))
  184. break;
  185. of_node_put(prev);
  186. read_unlock(&devtree_lock);
  187. return next;
  188. }
  189. EXPORT_SYMBOL(of_get_next_child);
  190. /**
  191. * of_find_node_by_path - Find a node matching a full OF path
  192. * @path: The full path to match
  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_path(const char *path)
  198. {
  199. struct device_node *np = allnodes;
  200. read_lock(&devtree_lock);
  201. for (; np; np = np->allnext) {
  202. if (np->full_name && (of_node_cmp(np->full_name, path) == 0)
  203. && of_node_get(np))
  204. break;
  205. }
  206. read_unlock(&devtree_lock);
  207. return np;
  208. }
  209. EXPORT_SYMBOL(of_find_node_by_path);
  210. /**
  211. * of_find_node_by_name - Find a node by its "name" property
  212. * @from: The node to start searching from or NULL, the node
  213. * you pass will not be searched, only the next one
  214. * will; typically, you pass what the previous call
  215. * returned. of_node_put() will be called on it
  216. * @name: The name string to match against
  217. *
  218. * Returns a node pointer with refcount incremented, use
  219. * of_node_put() on it when done.
  220. */
  221. struct device_node *of_find_node_by_name(struct device_node *from,
  222. const char *name)
  223. {
  224. struct device_node *np;
  225. read_lock(&devtree_lock);
  226. np = from ? from->allnext : allnodes;
  227. for (; np; np = np->allnext)
  228. if (np->name && (of_node_cmp(np->name, name) == 0)
  229. && of_node_get(np))
  230. break;
  231. of_node_put(from);
  232. read_unlock(&devtree_lock);
  233. return np;
  234. }
  235. EXPORT_SYMBOL(of_find_node_by_name);
  236. /**
  237. * of_find_node_by_type - Find a node by its "device_type" property
  238. * @from: The node to start searching from, or NULL to start searching
  239. * the entire device tree. The node you pass will not be
  240. * searched, only the next one will; typically, you pass
  241. * what the previous call returned. of_node_put() will be
  242. * called on from for you.
  243. * @type: The type string to match against
  244. *
  245. * Returns a node pointer with refcount incremented, use
  246. * of_node_put() on it when done.
  247. */
  248. struct device_node *of_find_node_by_type(struct device_node *from,
  249. const char *type)
  250. {
  251. struct device_node *np;
  252. read_lock(&devtree_lock);
  253. np = from ? from->allnext : allnodes;
  254. for (; np; np = np->allnext)
  255. if (np->type && (of_node_cmp(np->type, type) == 0)
  256. && of_node_get(np))
  257. break;
  258. of_node_put(from);
  259. read_unlock(&devtree_lock);
  260. return np;
  261. }
  262. EXPORT_SYMBOL(of_find_node_by_type);
  263. /**
  264. * of_find_compatible_node - Find a node based on type and one of the
  265. * tokens in its "compatible" property
  266. * @from: The node to start searching from or NULL, the node
  267. * you pass will not be searched, only the next one
  268. * will; typically, you pass what the previous call
  269. * returned. of_node_put() will be called on it
  270. * @type: The type string to match "device_type" or NULL to ignore
  271. * @compatible: The string to match to one of the tokens in the device
  272. * "compatible" list.
  273. *
  274. * Returns a node pointer with refcount incremented, use
  275. * of_node_put() on it when done.
  276. */
  277. struct device_node *of_find_compatible_node(struct device_node *from,
  278. const char *type, const char *compatible)
  279. {
  280. struct device_node *np;
  281. read_lock(&devtree_lock);
  282. np = from ? from->allnext : allnodes;
  283. for (; np; np = np->allnext) {
  284. if (type
  285. && !(np->type && (of_node_cmp(np->type, type) == 0)))
  286. continue;
  287. if (of_device_is_compatible(np, compatible) && of_node_get(np))
  288. break;
  289. }
  290. of_node_put(from);
  291. read_unlock(&devtree_lock);
  292. return np;
  293. }
  294. EXPORT_SYMBOL(of_find_compatible_node);
  295. /**
  296. * of_match_node - Tell if an device_node has a matching of_match structure
  297. * @matches: array of of device match structures to search in
  298. * @node: the of device structure to match against
  299. *
  300. * Low level utility function used by device matching.
  301. */
  302. const struct of_device_id *of_match_node(const struct of_device_id *matches,
  303. const struct device_node *node)
  304. {
  305. while (matches->name[0] || matches->type[0] || matches->compatible[0]) {
  306. int match = 1;
  307. if (matches->name[0])
  308. match &= node->name
  309. && !strcmp(matches->name, node->name);
  310. if (matches->type[0])
  311. match &= node->type
  312. && !strcmp(matches->type, node->type);
  313. if (matches->compatible[0])
  314. match &= of_device_is_compatible(node,
  315. matches->compatible);
  316. if (match)
  317. return matches;
  318. matches++;
  319. }
  320. return NULL;
  321. }
  322. EXPORT_SYMBOL(of_match_node);
  323. /**
  324. * of_find_matching_node - Find a node based on an of_device_id match
  325. * table.
  326. * @from: The node to start searching from or NULL, the node
  327. * you pass will not be searched, only the next one
  328. * will; typically, you pass what the previous call
  329. * returned. of_node_put() will be called on it
  330. * @matches: array of of device match structures to search in
  331. *
  332. * Returns a node pointer with refcount incremented, use
  333. * of_node_put() on it when done.
  334. */
  335. struct device_node *of_find_matching_node(struct device_node *from,
  336. const struct of_device_id *matches)
  337. {
  338. struct device_node *np;
  339. read_lock(&devtree_lock);
  340. np = from ? from->allnext : allnodes;
  341. for (; np; np = np->allnext) {
  342. if (of_match_node(matches, np) && of_node_get(np))
  343. break;
  344. }
  345. of_node_put(from);
  346. read_unlock(&devtree_lock);
  347. return np;
  348. }
  349. EXPORT_SYMBOL(of_find_matching_node);
  350. /**
  351. * of_modalias_table: Table of explicit compatible ==> modalias mappings
  352. *
  353. * This table allows particulare compatible property values to be mapped
  354. * to modalias strings. This is useful for busses which do not directly
  355. * understand the OF device tree but are populated based on data contained
  356. * within the device tree. SPI and I2C are the two current users of this
  357. * table.
  358. *
  359. * In most cases, devices do not need to be listed in this table because
  360. * the modalias value can be derived directly from the compatible table.
  361. * However, if for any reason a value cannot be derived, then this table
  362. * provides a method to override the implicit derivation.
  363. *
  364. * At the moment, a single table is used for all bus types because it is
  365. * assumed that the data size is small and that the compatible values
  366. * should already be distinct enough to differentiate between SPI, I2C
  367. * and other devices.
  368. */
  369. struct of_modalias_table {
  370. char *of_device;
  371. char *modalias;
  372. };
  373. static struct of_modalias_table of_modalias_table[] = {
  374. /* Empty for now; add entries as needed */
  375. };
  376. /**
  377. * of_modalias_node - Lookup appropriate modalias for a device node
  378. * @node: pointer to a device tree node
  379. * @modalias: Pointer to buffer that modalias value will be copied into
  380. * @len: Length of modalias value
  381. *
  382. * Based on the value of the compatible property, this routine will determine
  383. * an appropriate modalias value for a particular device tree node. Three
  384. * separate methods are used to derive a modalias value.
  385. *
  386. * First method is to lookup the compatible value in of_modalias_table.
  387. * Second is to look for a "linux,<modalias>" entry in the compatible list
  388. * and used that for modalias. Third is to strip off the manufacturer
  389. * prefix from the first compatible entry and use the remainder as modalias
  390. *
  391. * This routine returns 0 on success
  392. */
  393. int of_modalias_node(struct device_node *node, char *modalias, int len)
  394. {
  395. int i, cplen;
  396. const char *compatible;
  397. const char *p;
  398. /* 1. search for exception list entry */
  399. for (i = 0; i < ARRAY_SIZE(of_modalias_table); i++) {
  400. compatible = of_modalias_table[i].of_device;
  401. if (!of_device_is_compatible(node, compatible))
  402. continue;
  403. strlcpy(modalias, of_modalias_table[i].modalias, len);
  404. return 0;
  405. }
  406. compatible = of_get_property(node, "compatible", &cplen);
  407. if (!compatible)
  408. return -ENODEV;
  409. /* 2. search for linux,<modalias> entry */
  410. p = compatible;
  411. while (cplen > 0) {
  412. if (!strncmp(p, "linux,", 6)) {
  413. p += 6;
  414. strlcpy(modalias, p, len);
  415. return 0;
  416. }
  417. i = strlen(p) + 1;
  418. p += i;
  419. cplen -= i;
  420. }
  421. /* 3. take first compatible entry and strip manufacturer */
  422. p = strchr(compatible, ',');
  423. if (!p)
  424. return -ENODEV;
  425. p++;
  426. strlcpy(modalias, p, len);
  427. return 0;
  428. }
  429. EXPORT_SYMBOL_GPL(of_modalias_node);