base.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  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. /* use when traversing tree through the allnext, child, sibling,
  23. * or parent members of struct device_node.
  24. */
  25. DEFINE_RWLOCK(devtree_lock);
  26. int of_n_addr_cells(struct device_node *np)
  27. {
  28. const int *ip;
  29. do {
  30. if (np->parent)
  31. np = np->parent;
  32. ip = of_get_property(np, "#address-cells", NULL);
  33. if (ip)
  34. return *ip;
  35. } while (np->parent);
  36. /* No #address-cells property for the root node */
  37. return OF_ROOT_NODE_ADDR_CELLS_DEFAULT;
  38. }
  39. EXPORT_SYMBOL(of_n_addr_cells);
  40. int of_n_size_cells(struct device_node *np)
  41. {
  42. const int *ip;
  43. do {
  44. if (np->parent)
  45. np = np->parent;
  46. ip = of_get_property(np, "#size-cells", NULL);
  47. if (ip)
  48. return *ip;
  49. } while (np->parent);
  50. /* No #size-cells property for the root node */
  51. return OF_ROOT_NODE_SIZE_CELLS_DEFAULT;
  52. }
  53. EXPORT_SYMBOL(of_n_size_cells);
  54. struct property *of_find_property(const struct device_node *np,
  55. const char *name,
  56. int *lenp)
  57. {
  58. struct property *pp;
  59. read_lock(&devtree_lock);
  60. for (pp = np->properties; pp != 0; pp = pp->next) {
  61. if (of_prop_cmp(pp->name, name) == 0) {
  62. if (lenp != 0)
  63. *lenp = pp->length;
  64. break;
  65. }
  66. }
  67. read_unlock(&devtree_lock);
  68. return pp;
  69. }
  70. EXPORT_SYMBOL(of_find_property);
  71. /*
  72. * Find a property with a given name for a given node
  73. * and return the value.
  74. */
  75. const void *of_get_property(const struct device_node *np, const char *name,
  76. int *lenp)
  77. {
  78. struct property *pp = of_find_property(np, name, lenp);
  79. return pp ? pp->value : NULL;
  80. }
  81. EXPORT_SYMBOL(of_get_property);
  82. /** Checks if the given "compat" string matches one of the strings in
  83. * the device's "compatible" property
  84. */
  85. int of_device_is_compatible(const struct device_node *device,
  86. const char *compat)
  87. {
  88. const char* cp;
  89. int cplen, l;
  90. cp = of_get_property(device, "compatible", &cplen);
  91. if (cp == NULL)
  92. return 0;
  93. while (cplen > 0) {
  94. if (of_compat_cmp(cp, compat, strlen(compat)) == 0)
  95. return 1;
  96. l = strlen(cp) + 1;
  97. cp += l;
  98. cplen -= l;
  99. }
  100. return 0;
  101. }
  102. EXPORT_SYMBOL(of_device_is_compatible);
  103. /**
  104. * of_get_parent - Get a node's parent if any
  105. * @node: Node to get parent
  106. *
  107. * Returns a node pointer with refcount incremented, use
  108. * of_node_put() on it when done.
  109. */
  110. struct device_node *of_get_parent(const struct device_node *node)
  111. {
  112. struct device_node *np;
  113. if (!node)
  114. return NULL;
  115. read_lock(&devtree_lock);
  116. np = of_node_get(node->parent);
  117. read_unlock(&devtree_lock);
  118. return np;
  119. }
  120. EXPORT_SYMBOL(of_get_parent);
  121. /**
  122. * of_get_next_child - Iterate a node childs
  123. * @node: parent node
  124. * @prev: previous child of the parent node, or NULL to get first
  125. *
  126. * Returns a node pointer with refcount incremented, use
  127. * of_node_put() on it when done.
  128. */
  129. struct device_node *of_get_next_child(const struct device_node *node,
  130. struct device_node *prev)
  131. {
  132. struct device_node *next;
  133. read_lock(&devtree_lock);
  134. next = prev ? prev->sibling : node->child;
  135. for (; next; next = next->sibling)
  136. if (of_node_get(next))
  137. break;
  138. of_node_put(prev);
  139. read_unlock(&devtree_lock);
  140. return next;
  141. }
  142. EXPORT_SYMBOL(of_get_next_child);