base.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660
  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 and
  13. * Grant Likely.
  14. *
  15. * This program is free software; you can redistribute it and/or
  16. * modify it under the terms of the GNU General Public License
  17. * as published by the Free Software Foundation; either version
  18. * 2 of the License, or (at your option) any later version.
  19. */
  20. #include <linux/module.h>
  21. #include <linux/of.h>
  22. #include <linux/spinlock.h>
  23. struct device_node *allnodes;
  24. /* use when traversing tree through the allnext, child, sibling,
  25. * or parent members of struct device_node.
  26. */
  27. DEFINE_RWLOCK(devtree_lock);
  28. int of_n_addr_cells(struct device_node *np)
  29. {
  30. const int *ip;
  31. do {
  32. if (np->parent)
  33. np = np->parent;
  34. ip = of_get_property(np, "#address-cells", NULL);
  35. if (ip)
  36. return *ip;
  37. } while (np->parent);
  38. /* No #address-cells property for the root node */
  39. return OF_ROOT_NODE_ADDR_CELLS_DEFAULT;
  40. }
  41. EXPORT_SYMBOL(of_n_addr_cells);
  42. int of_n_size_cells(struct device_node *np)
  43. {
  44. const int *ip;
  45. do {
  46. if (np->parent)
  47. np = np->parent;
  48. ip = of_get_property(np, "#size-cells", NULL);
  49. if (ip)
  50. return *ip;
  51. } while (np->parent);
  52. /* No #size-cells property for the root node */
  53. return OF_ROOT_NODE_SIZE_CELLS_DEFAULT;
  54. }
  55. EXPORT_SYMBOL(of_n_size_cells);
  56. struct property *of_find_property(const struct device_node *np,
  57. const char *name,
  58. int *lenp)
  59. {
  60. struct property *pp;
  61. if (!np)
  62. return NULL;
  63. read_lock(&devtree_lock);
  64. for (pp = np->properties; pp != 0; pp = pp->next) {
  65. if (of_prop_cmp(pp->name, name) == 0) {
  66. if (lenp != 0)
  67. *lenp = pp->length;
  68. break;
  69. }
  70. }
  71. read_unlock(&devtree_lock);
  72. return pp;
  73. }
  74. EXPORT_SYMBOL(of_find_property);
  75. /**
  76. * of_find_all_nodes - Get next node in global list
  77. * @prev: Previous node or NULL to start iteration
  78. * of_node_put() will be called on it
  79. *
  80. * Returns a node pointer with refcount incremented, use
  81. * of_node_put() on it when done.
  82. */
  83. struct device_node *of_find_all_nodes(struct device_node *prev)
  84. {
  85. struct device_node *np;
  86. read_lock(&devtree_lock);
  87. np = prev ? prev->allnext : allnodes;
  88. for (; np != NULL; np = np->allnext)
  89. if (of_node_get(np))
  90. break;
  91. of_node_put(prev);
  92. read_unlock(&devtree_lock);
  93. return np;
  94. }
  95. EXPORT_SYMBOL(of_find_all_nodes);
  96. /*
  97. * Find a property with a given name for a given node
  98. * and return the value.
  99. */
  100. const void *of_get_property(const struct device_node *np, const char *name,
  101. int *lenp)
  102. {
  103. struct property *pp = of_find_property(np, name, lenp);
  104. return pp ? pp->value : NULL;
  105. }
  106. EXPORT_SYMBOL(of_get_property);
  107. /** Checks if the given "compat" string matches one of the strings in
  108. * the device's "compatible" property
  109. */
  110. int of_device_is_compatible(const struct device_node *device,
  111. const char *compat)
  112. {
  113. const char* cp;
  114. int cplen, l;
  115. cp = of_get_property(device, "compatible", &cplen);
  116. if (cp == NULL)
  117. return 0;
  118. while (cplen > 0) {
  119. if (of_compat_cmp(cp, compat, strlen(compat)) == 0)
  120. return 1;
  121. l = strlen(cp) + 1;
  122. cp += l;
  123. cplen -= l;
  124. }
  125. return 0;
  126. }
  127. EXPORT_SYMBOL(of_device_is_compatible);
  128. /**
  129. * of_device_is_available - check if a device is available for use
  130. *
  131. * @device: Node to check for availability
  132. *
  133. * Returns 1 if the status property is absent or set to "okay" or "ok",
  134. * 0 otherwise
  135. */
  136. int of_device_is_available(const struct device_node *device)
  137. {
  138. const char *status;
  139. int statlen;
  140. status = of_get_property(device, "status", &statlen);
  141. if (status == NULL)
  142. return 1;
  143. if (statlen > 0) {
  144. if (!strcmp(status, "okay") || !strcmp(status, "ok"))
  145. return 1;
  146. }
  147. return 0;
  148. }
  149. EXPORT_SYMBOL(of_device_is_available);
  150. /**
  151. * of_get_parent - Get a node's parent if any
  152. * @node: Node to get parent
  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_parent(const struct device_node *node)
  158. {
  159. struct device_node *np;
  160. if (!node)
  161. return NULL;
  162. read_lock(&devtree_lock);
  163. np = of_node_get(node->parent);
  164. read_unlock(&devtree_lock);
  165. return np;
  166. }
  167. EXPORT_SYMBOL(of_get_parent);
  168. /**
  169. * of_get_next_parent - Iterate to a node's parent
  170. * @node: Node to get parent of
  171. *
  172. * This is like of_get_parent() except that it drops the
  173. * refcount on the passed node, making it suitable for iterating
  174. * through a node's parents.
  175. *
  176. * Returns a node pointer with refcount incremented, use
  177. * of_node_put() on it when done.
  178. */
  179. struct device_node *of_get_next_parent(struct device_node *node)
  180. {
  181. struct device_node *parent;
  182. if (!node)
  183. return NULL;
  184. read_lock(&devtree_lock);
  185. parent = of_node_get(node->parent);
  186. of_node_put(node);
  187. read_unlock(&devtree_lock);
  188. return parent;
  189. }
  190. /**
  191. * of_get_next_child - Iterate a node childs
  192. * @node: parent node
  193. * @prev: previous child of the parent node, or NULL to get first
  194. *
  195. * Returns a node pointer with refcount incremented, use
  196. * of_node_put() on it when done.
  197. */
  198. struct device_node *of_get_next_child(const struct device_node *node,
  199. struct device_node *prev)
  200. {
  201. struct device_node *next;
  202. read_lock(&devtree_lock);
  203. next = prev ? prev->sibling : node->child;
  204. for (; next; next = next->sibling)
  205. if (of_node_get(next))
  206. break;
  207. of_node_put(prev);
  208. read_unlock(&devtree_lock);
  209. return next;
  210. }
  211. EXPORT_SYMBOL(of_get_next_child);
  212. /**
  213. * of_find_node_by_path - Find a node matching a full OF path
  214. * @path: The full path to match
  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_path(const char *path)
  220. {
  221. struct device_node *np = allnodes;
  222. read_lock(&devtree_lock);
  223. for (; np; np = np->allnext) {
  224. if (np->full_name && (of_node_cmp(np->full_name, path) == 0)
  225. && of_node_get(np))
  226. break;
  227. }
  228. read_unlock(&devtree_lock);
  229. return np;
  230. }
  231. EXPORT_SYMBOL(of_find_node_by_path);
  232. /**
  233. * of_find_node_by_name - Find a node by its "name" property
  234. * @from: The node to start searching from or NULL, the node
  235. * you pass will not be searched, only the next one
  236. * will; typically, you pass what the previous call
  237. * returned. of_node_put() will be called on it
  238. * @name: The name string to match against
  239. *
  240. * Returns a node pointer with refcount incremented, use
  241. * of_node_put() on it when done.
  242. */
  243. struct device_node *of_find_node_by_name(struct device_node *from,
  244. const char *name)
  245. {
  246. struct device_node *np;
  247. read_lock(&devtree_lock);
  248. np = from ? from->allnext : allnodes;
  249. for (; np; np = np->allnext)
  250. if (np->name && (of_node_cmp(np->name, name) == 0)
  251. && of_node_get(np))
  252. break;
  253. of_node_put(from);
  254. read_unlock(&devtree_lock);
  255. return np;
  256. }
  257. EXPORT_SYMBOL(of_find_node_by_name);
  258. /**
  259. * of_find_node_by_type - Find a node by its "device_type" property
  260. * @from: The node to start searching from, or NULL to start searching
  261. * the entire device tree. The node you pass will not be
  262. * searched, only the next one will; typically, you pass
  263. * what the previous call returned. of_node_put() will be
  264. * called on from for you.
  265. * @type: The type string to match against
  266. *
  267. * Returns a node pointer with refcount incremented, use
  268. * of_node_put() on it when done.
  269. */
  270. struct device_node *of_find_node_by_type(struct device_node *from,
  271. const char *type)
  272. {
  273. struct device_node *np;
  274. read_lock(&devtree_lock);
  275. np = from ? from->allnext : allnodes;
  276. for (; np; np = np->allnext)
  277. if (np->type && (of_node_cmp(np->type, type) == 0)
  278. && of_node_get(np))
  279. break;
  280. of_node_put(from);
  281. read_unlock(&devtree_lock);
  282. return np;
  283. }
  284. EXPORT_SYMBOL(of_find_node_by_type);
  285. /**
  286. * of_find_compatible_node - Find a node based on type and one of the
  287. * tokens in its "compatible" property
  288. * @from: The node to start searching from or NULL, the node
  289. * you pass will not be searched, only the next one
  290. * will; typically, you pass what the previous call
  291. * returned. of_node_put() will be called on it
  292. * @type: The type string to match "device_type" or NULL to ignore
  293. * @compatible: The string to match to one of the tokens in the device
  294. * "compatible" list.
  295. *
  296. * Returns a node pointer with refcount incremented, use
  297. * of_node_put() on it when done.
  298. */
  299. struct device_node *of_find_compatible_node(struct device_node *from,
  300. const char *type, const char *compatible)
  301. {
  302. struct device_node *np;
  303. read_lock(&devtree_lock);
  304. np = from ? from->allnext : allnodes;
  305. for (; np; np = np->allnext) {
  306. if (type
  307. && !(np->type && (of_node_cmp(np->type, type) == 0)))
  308. continue;
  309. if (of_device_is_compatible(np, compatible) && of_node_get(np))
  310. break;
  311. }
  312. of_node_put(from);
  313. read_unlock(&devtree_lock);
  314. return np;
  315. }
  316. EXPORT_SYMBOL(of_find_compatible_node);
  317. /**
  318. * of_find_node_with_property - Find a node which has a property with
  319. * the given name.
  320. * @from: The node to start searching from or NULL, the node
  321. * you pass will not be searched, only the next one
  322. * will; typically, you pass what the previous call
  323. * returned. of_node_put() will be called on it
  324. * @prop_name: The name of the property to look for.
  325. *
  326. * Returns a node pointer with refcount incremented, use
  327. * of_node_put() on it when done.
  328. */
  329. struct device_node *of_find_node_with_property(struct device_node *from,
  330. const char *prop_name)
  331. {
  332. struct device_node *np;
  333. struct property *pp;
  334. read_lock(&devtree_lock);
  335. np = from ? from->allnext : allnodes;
  336. for (; np; np = np->allnext) {
  337. for (pp = np->properties; pp != 0; pp = pp->next) {
  338. if (of_prop_cmp(pp->name, prop_name) == 0) {
  339. of_node_get(np);
  340. goto out;
  341. }
  342. }
  343. }
  344. out:
  345. of_node_put(from);
  346. read_unlock(&devtree_lock);
  347. return np;
  348. }
  349. EXPORT_SYMBOL(of_find_node_with_property);
  350. /**
  351. * of_match_node - Tell if an device_node has a matching of_match structure
  352. * @matches: array of of device match structures to search in
  353. * @node: the of device structure to match against
  354. *
  355. * Low level utility function used by device matching.
  356. */
  357. const struct of_device_id *of_match_node(const struct of_device_id *matches,
  358. const struct device_node *node)
  359. {
  360. while (matches->name[0] || matches->type[0] || matches->compatible[0]) {
  361. int match = 1;
  362. if (matches->name[0])
  363. match &= node->name
  364. && !strcmp(matches->name, node->name);
  365. if (matches->type[0])
  366. match &= node->type
  367. && !strcmp(matches->type, node->type);
  368. if (matches->compatible[0])
  369. match &= of_device_is_compatible(node,
  370. matches->compatible);
  371. if (match)
  372. return matches;
  373. matches++;
  374. }
  375. return NULL;
  376. }
  377. EXPORT_SYMBOL(of_match_node);
  378. /**
  379. * of_find_matching_node - Find a node based on an of_device_id match
  380. * table.
  381. * @from: The node to start searching from or NULL, the node
  382. * you pass will not be searched, only the next one
  383. * will; typically, you pass what the previous call
  384. * returned. of_node_put() will be called on it
  385. * @matches: array of of device match structures to search in
  386. *
  387. * Returns a node pointer with refcount incremented, use
  388. * of_node_put() on it when done.
  389. */
  390. struct device_node *of_find_matching_node(struct device_node *from,
  391. const struct of_device_id *matches)
  392. {
  393. struct device_node *np;
  394. read_lock(&devtree_lock);
  395. np = from ? from->allnext : allnodes;
  396. for (; np; np = np->allnext) {
  397. if (of_match_node(matches, np) && of_node_get(np))
  398. break;
  399. }
  400. of_node_put(from);
  401. read_unlock(&devtree_lock);
  402. return np;
  403. }
  404. EXPORT_SYMBOL(of_find_matching_node);
  405. /**
  406. * of_modalias_table: Table of explicit compatible ==> modalias mappings
  407. *
  408. * This table allows particulare compatible property values to be mapped
  409. * to modalias strings. This is useful for busses which do not directly
  410. * understand the OF device tree but are populated based on data contained
  411. * within the device tree. SPI and I2C are the two current users of this
  412. * table.
  413. *
  414. * In most cases, devices do not need to be listed in this table because
  415. * the modalias value can be derived directly from the compatible table.
  416. * However, if for any reason a value cannot be derived, then this table
  417. * provides a method to override the implicit derivation.
  418. *
  419. * At the moment, a single table is used for all bus types because it is
  420. * assumed that the data size is small and that the compatible values
  421. * should already be distinct enough to differentiate between SPI, I2C
  422. * and other devices.
  423. */
  424. struct of_modalias_table {
  425. char *of_device;
  426. char *modalias;
  427. };
  428. static struct of_modalias_table of_modalias_table[] = {
  429. { "fsl,mcu-mpc8349emitx", "mcu-mpc8349emitx" },
  430. { "mmc-spi-slot", "mmc_spi" },
  431. };
  432. /**
  433. * of_modalias_node - Lookup appropriate modalias for a device node
  434. * @node: pointer to a device tree node
  435. * @modalias: Pointer to buffer that modalias value will be copied into
  436. * @len: Length of modalias value
  437. *
  438. * Based on the value of the compatible property, this routine will determine
  439. * an appropriate modalias value for a particular device tree node. Two
  440. * separate methods are attempted to derive a modalias value.
  441. *
  442. * First method is to lookup the compatible value in of_modalias_table.
  443. * Second is to strip off the manufacturer prefix from the first
  444. * compatible entry and use the remainder as modalias
  445. *
  446. * This routine returns 0 on success
  447. */
  448. int of_modalias_node(struct device_node *node, char *modalias, int len)
  449. {
  450. int i, cplen;
  451. const char *compatible;
  452. const char *p;
  453. /* 1. search for exception list entry */
  454. for (i = 0; i < ARRAY_SIZE(of_modalias_table); i++) {
  455. compatible = of_modalias_table[i].of_device;
  456. if (!of_device_is_compatible(node, compatible))
  457. continue;
  458. strlcpy(modalias, of_modalias_table[i].modalias, len);
  459. return 0;
  460. }
  461. compatible = of_get_property(node, "compatible", &cplen);
  462. if (!compatible)
  463. return -ENODEV;
  464. /* 2. take first compatible entry and strip manufacturer */
  465. p = strchr(compatible, ',');
  466. if (!p)
  467. return -ENODEV;
  468. p++;
  469. strlcpy(modalias, p, len);
  470. return 0;
  471. }
  472. EXPORT_SYMBOL_GPL(of_modalias_node);
  473. /**
  474. * of_parse_phandle - Resolve a phandle property to a device_node pointer
  475. * @np: Pointer to device node holding phandle property
  476. * @phandle_name: Name of property holding a phandle value
  477. * @index: For properties holding a table of phandles, this is the index into
  478. * the table
  479. *
  480. * Returns the device_node pointer with refcount incremented. Use
  481. * of_node_put() on it when done.
  482. */
  483. struct device_node *
  484. of_parse_phandle(struct device_node *np, const char *phandle_name, int index)
  485. {
  486. const phandle *phandle;
  487. int size;
  488. phandle = of_get_property(np, phandle_name, &size);
  489. if ((!phandle) || (size < sizeof(*phandle) * (index + 1)))
  490. return NULL;
  491. return of_find_node_by_phandle(phandle[index]);
  492. }
  493. EXPORT_SYMBOL(of_parse_phandle);
  494. /**
  495. * of_parse_phandles_with_args - Find a node pointed by phandle in a list
  496. * @np: pointer to a device tree node containing a list
  497. * @list_name: property name that contains a list
  498. * @cells_name: property name that specifies phandles' arguments count
  499. * @index: index of a phandle to parse out
  500. * @out_node: optional pointer to device_node struct pointer (will be filled)
  501. * @out_args: optional pointer to arguments pointer (will be filled)
  502. *
  503. * This function is useful to parse lists of phandles and their arguments.
  504. * Returns 0 on success and fills out_node and out_args, on error returns
  505. * appropriate errno value.
  506. *
  507. * Example:
  508. *
  509. * phandle1: node1 {
  510. * #list-cells = <2>;
  511. * }
  512. *
  513. * phandle2: node2 {
  514. * #list-cells = <1>;
  515. * }
  516. *
  517. * node3 {
  518. * list = <&phandle1 1 2 &phandle2 3>;
  519. * }
  520. *
  521. * To get a device_node of the `node2' node you may call this:
  522. * of_parse_phandles_with_args(node3, "list", "#list-cells", 2, &node2, &args);
  523. */
  524. int of_parse_phandles_with_args(struct device_node *np, const char *list_name,
  525. const char *cells_name, int index,
  526. struct device_node **out_node,
  527. const void **out_args)
  528. {
  529. int ret = -EINVAL;
  530. const u32 *list;
  531. const u32 *list_end;
  532. int size;
  533. int cur_index = 0;
  534. struct device_node *node = NULL;
  535. const void *args = NULL;
  536. list = of_get_property(np, list_name, &size);
  537. if (!list) {
  538. ret = -ENOENT;
  539. goto err0;
  540. }
  541. list_end = list + size / sizeof(*list);
  542. while (list < list_end) {
  543. const u32 *cells;
  544. const phandle *phandle;
  545. phandle = list++;
  546. args = list;
  547. /* one cell hole in the list = <>; */
  548. if (!*phandle)
  549. goto next;
  550. node = of_find_node_by_phandle(*phandle);
  551. if (!node) {
  552. pr_debug("%s: could not find phandle\n",
  553. np->full_name);
  554. goto err0;
  555. }
  556. cells = of_get_property(node, cells_name, &size);
  557. if (!cells || size != sizeof(*cells)) {
  558. pr_debug("%s: could not get %s for %s\n",
  559. np->full_name, cells_name, node->full_name);
  560. goto err1;
  561. }
  562. list += *cells;
  563. if (list > list_end) {
  564. pr_debug("%s: insufficient arguments length\n",
  565. np->full_name);
  566. goto err1;
  567. }
  568. next:
  569. if (cur_index == index)
  570. break;
  571. of_node_put(node);
  572. node = NULL;
  573. args = NULL;
  574. cur_index++;
  575. }
  576. if (!node) {
  577. /*
  578. * args w/o node indicates that the loop above has stopped at
  579. * the 'hole' cell. Report this differently.
  580. */
  581. if (args)
  582. ret = -EEXIST;
  583. else
  584. ret = -ENOENT;
  585. goto err0;
  586. }
  587. if (out_node)
  588. *out_node = node;
  589. if (out_args)
  590. *out_args = args;
  591. return 0;
  592. err1:
  593. of_node_put(node);
  594. err0:
  595. pr_debug("%s failed with status %d\n", __func__, ret);
  596. return ret;
  597. }
  598. EXPORT_SYMBOL(of_parse_phandles_with_args);