base.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637
  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_find_node_with_property - Find a node which has a property with
  297. * the given name.
  298. * @from: The node to start searching from or NULL, the node
  299. * you pass will not be searched, only the next one
  300. * will; typically, you pass what the previous call
  301. * returned. of_node_put() will be called on it
  302. * @prop_name: The name of the property to look for.
  303. *
  304. * Returns a node pointer with refcount incremented, use
  305. * of_node_put() on it when done.
  306. */
  307. struct device_node *of_find_node_with_property(struct device_node *from,
  308. const char *prop_name)
  309. {
  310. struct device_node *np;
  311. struct property *pp;
  312. read_lock(&devtree_lock);
  313. np = from ? from->allnext : allnodes;
  314. for (; np; np = np->allnext) {
  315. for (pp = np->properties; pp != 0; pp = pp->next) {
  316. if (of_prop_cmp(pp->name, prop_name) == 0) {
  317. of_node_get(np);
  318. goto out;
  319. }
  320. }
  321. }
  322. out:
  323. of_node_put(from);
  324. read_unlock(&devtree_lock);
  325. return np;
  326. }
  327. EXPORT_SYMBOL(of_find_node_with_property);
  328. /**
  329. * of_match_node - Tell if an device_node has a matching of_match structure
  330. * @matches: array of of device match structures to search in
  331. * @node: the of device structure to match against
  332. *
  333. * Low level utility function used by device matching.
  334. */
  335. const struct of_device_id *of_match_node(const struct of_device_id *matches,
  336. const struct device_node *node)
  337. {
  338. while (matches->name[0] || matches->type[0] || matches->compatible[0]) {
  339. int match = 1;
  340. if (matches->name[0])
  341. match &= node->name
  342. && !strcmp(matches->name, node->name);
  343. if (matches->type[0])
  344. match &= node->type
  345. && !strcmp(matches->type, node->type);
  346. if (matches->compatible[0])
  347. match &= of_device_is_compatible(node,
  348. matches->compatible);
  349. if (match)
  350. return matches;
  351. matches++;
  352. }
  353. return NULL;
  354. }
  355. EXPORT_SYMBOL(of_match_node);
  356. /**
  357. * of_find_matching_node - Find a node based on an of_device_id match
  358. * table.
  359. * @from: The node to start searching from or NULL, the node
  360. * you pass will not be searched, only the next one
  361. * will; typically, you pass what the previous call
  362. * returned. of_node_put() will be called on it
  363. * @matches: array of of device match structures to search in
  364. *
  365. * Returns a node pointer with refcount incremented, use
  366. * of_node_put() on it when done.
  367. */
  368. struct device_node *of_find_matching_node(struct device_node *from,
  369. const struct of_device_id *matches)
  370. {
  371. struct device_node *np;
  372. read_lock(&devtree_lock);
  373. np = from ? from->allnext : allnodes;
  374. for (; np; np = np->allnext) {
  375. if (of_match_node(matches, np) && of_node_get(np))
  376. break;
  377. }
  378. of_node_put(from);
  379. read_unlock(&devtree_lock);
  380. return np;
  381. }
  382. EXPORT_SYMBOL(of_find_matching_node);
  383. /**
  384. * of_modalias_table: Table of explicit compatible ==> modalias mappings
  385. *
  386. * This table allows particulare compatible property values to be mapped
  387. * to modalias strings. This is useful for busses which do not directly
  388. * understand the OF device tree but are populated based on data contained
  389. * within the device tree. SPI and I2C are the two current users of this
  390. * table.
  391. *
  392. * In most cases, devices do not need to be listed in this table because
  393. * the modalias value can be derived directly from the compatible table.
  394. * However, if for any reason a value cannot be derived, then this table
  395. * provides a method to override the implicit derivation.
  396. *
  397. * At the moment, a single table is used for all bus types because it is
  398. * assumed that the data size is small and that the compatible values
  399. * should already be distinct enough to differentiate between SPI, I2C
  400. * and other devices.
  401. */
  402. struct of_modalias_table {
  403. char *of_device;
  404. char *modalias;
  405. };
  406. static struct of_modalias_table of_modalias_table[] = {
  407. { "fsl,mcu-mpc8349emitx", "mcu-mpc8349emitx" },
  408. { "mmc-spi-slot", "mmc_spi" },
  409. { "stm,m25p40", "m25p80" },
  410. };
  411. /**
  412. * of_modalias_node - Lookup appropriate modalias for a device node
  413. * @node: pointer to a device tree node
  414. * @modalias: Pointer to buffer that modalias value will be copied into
  415. * @len: Length of modalias value
  416. *
  417. * Based on the value of the compatible property, this routine will determine
  418. * an appropriate modalias value for a particular device tree node. Two
  419. * separate methods are attempted to derive a modalias value.
  420. *
  421. * First method is to lookup the compatible value in of_modalias_table.
  422. * Second is to strip off the manufacturer prefix from the first
  423. * compatible entry and use the remainder as modalias
  424. *
  425. * This routine returns 0 on success
  426. */
  427. int of_modalias_node(struct device_node *node, char *modalias, int len)
  428. {
  429. int i, cplen;
  430. const char *compatible;
  431. const char *p;
  432. /* 1. search for exception list entry */
  433. for (i = 0; i < ARRAY_SIZE(of_modalias_table); i++) {
  434. compatible = of_modalias_table[i].of_device;
  435. if (!of_device_is_compatible(node, compatible))
  436. continue;
  437. strlcpy(modalias, of_modalias_table[i].modalias, len);
  438. return 0;
  439. }
  440. compatible = of_get_property(node, "compatible", &cplen);
  441. if (!compatible)
  442. return -ENODEV;
  443. /* 2. take first compatible entry and strip manufacturer */
  444. p = strchr(compatible, ',');
  445. if (!p)
  446. return -ENODEV;
  447. p++;
  448. strlcpy(modalias, p, len);
  449. return 0;
  450. }
  451. EXPORT_SYMBOL_GPL(of_modalias_node);
  452. /**
  453. * of_parse_phandle - Resolve a phandle property to a device_node pointer
  454. * @np: Pointer to device node holding phandle property
  455. * @phandle_name: Name of property holding a phandle value
  456. * @index: For properties holding a table of phandles, this is the index into
  457. * the table
  458. *
  459. * Returns the device_node pointer with refcount incremented. Use
  460. * of_node_put() on it when done.
  461. */
  462. struct device_node *
  463. of_parse_phandle(struct device_node *np, const char *phandle_name, int index)
  464. {
  465. const phandle *phandle;
  466. int size;
  467. phandle = of_get_property(np, phandle_name, &size);
  468. if ((!phandle) || (size < sizeof(*phandle) * (index + 1)))
  469. return NULL;
  470. return of_find_node_by_phandle(phandle[index]);
  471. }
  472. EXPORT_SYMBOL(of_parse_phandle);
  473. /**
  474. * of_parse_phandles_with_args - Find a node pointed by phandle in a list
  475. * @np: pointer to a device tree node containing a list
  476. * @list_name: property name that contains a list
  477. * @cells_name: property name that specifies phandles' arguments count
  478. * @index: index of a phandle to parse out
  479. * @out_node: optional pointer to device_node struct pointer (will be filled)
  480. * @out_args: optional pointer to arguments pointer (will be filled)
  481. *
  482. * This function is useful to parse lists of phandles and their arguments.
  483. * Returns 0 on success and fills out_node and out_args, on error returns
  484. * appropriate errno value.
  485. *
  486. * Example:
  487. *
  488. * phandle1: node1 {
  489. * #list-cells = <2>;
  490. * }
  491. *
  492. * phandle2: node2 {
  493. * #list-cells = <1>;
  494. * }
  495. *
  496. * node3 {
  497. * list = <&phandle1 1 2 &phandle2 3>;
  498. * }
  499. *
  500. * To get a device_node of the `node2' node you may call this:
  501. * of_parse_phandles_with_args(node3, "list", "#list-cells", 2, &node2, &args);
  502. */
  503. int of_parse_phandles_with_args(struct device_node *np, const char *list_name,
  504. const char *cells_name, int index,
  505. struct device_node **out_node,
  506. const void **out_args)
  507. {
  508. int ret = -EINVAL;
  509. const u32 *list;
  510. const u32 *list_end;
  511. int size;
  512. int cur_index = 0;
  513. struct device_node *node = NULL;
  514. const void *args = NULL;
  515. list = of_get_property(np, list_name, &size);
  516. if (!list) {
  517. ret = -ENOENT;
  518. goto err0;
  519. }
  520. list_end = list + size / sizeof(*list);
  521. while (list < list_end) {
  522. const u32 *cells;
  523. const phandle *phandle;
  524. phandle = list++;
  525. args = list;
  526. /* one cell hole in the list = <>; */
  527. if (!*phandle)
  528. goto next;
  529. node = of_find_node_by_phandle(*phandle);
  530. if (!node) {
  531. pr_debug("%s: could not find phandle\n",
  532. np->full_name);
  533. goto err0;
  534. }
  535. cells = of_get_property(node, cells_name, &size);
  536. if (!cells || size != sizeof(*cells)) {
  537. pr_debug("%s: could not get %s for %s\n",
  538. np->full_name, cells_name, node->full_name);
  539. goto err1;
  540. }
  541. list += *cells;
  542. if (list > list_end) {
  543. pr_debug("%s: insufficient arguments length\n",
  544. np->full_name);
  545. goto err1;
  546. }
  547. next:
  548. if (cur_index == index)
  549. break;
  550. of_node_put(node);
  551. node = NULL;
  552. args = NULL;
  553. cur_index++;
  554. }
  555. if (!node) {
  556. /*
  557. * args w/o node indicates that the loop above has stopped at
  558. * the 'hole' cell. Report this differently.
  559. */
  560. if (args)
  561. ret = -EEXIST;
  562. else
  563. ret = -ENOENT;
  564. goto err0;
  565. }
  566. if (out_node)
  567. *out_node = node;
  568. if (out_args)
  569. *out_args = args;
  570. return 0;
  571. err1:
  572. of_node_put(node);
  573. err0:
  574. pr_debug("%s failed with status %d\n", __func__, ret);
  575. return ret;
  576. }
  577. EXPORT_SYMBOL(of_parse_phandles_with_args);