fdtdec.c 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  1. /*
  2. * Copyright (c) 2011 The Chromium OS Authors.
  3. * See file CREDITS for list of people who contributed to this
  4. * project.
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU General Public License as
  8. * published by the Free Software Foundation; either version 2 of
  9. * the License, or (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program; if not, write to the Free Software
  18. * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
  19. * MA 02111-1307 USA
  20. */
  21. #include <common.h>
  22. #include <serial.h>
  23. #include <libfdt.h>
  24. #include <fdtdec.h>
  25. DECLARE_GLOBAL_DATA_PTR;
  26. /*
  27. * Here are the type we know about. One day we might allow drivers to
  28. * register. For now we just put them here. The COMPAT macro allows us to
  29. * turn this into a sparse list later, and keeps the ID with the name.
  30. */
  31. #define COMPAT(id, name) name
  32. static const char * const compat_names[COMPAT_COUNT] = {
  33. };
  34. const char *fdtdec_get_compatible(enum fdt_compat_id id)
  35. {
  36. /* We allow reading of the 'unknown' ID for testing purposes */
  37. assert(id >= 0 && id < COMPAT_COUNT);
  38. return compat_names[id];
  39. }
  40. /**
  41. * Look in the FDT for an alias with the given name and return its node.
  42. *
  43. * @param blob FDT blob
  44. * @param name alias name to look up
  45. * @return node offset if found, or an error code < 0 otherwise
  46. */
  47. static int find_alias_node(const void *blob, const char *name)
  48. {
  49. const char *path;
  50. int alias_node;
  51. debug("find_alias_node: %s\n", name);
  52. alias_node = fdt_path_offset(blob, "/aliases");
  53. if (alias_node < 0)
  54. return alias_node;
  55. path = fdt_getprop(blob, alias_node, name, NULL);
  56. if (!path)
  57. return -FDT_ERR_NOTFOUND;
  58. return fdt_path_offset(blob, path);
  59. }
  60. fdt_addr_t fdtdec_get_addr(const void *blob, int node,
  61. const char *prop_name)
  62. {
  63. const fdt_addr_t *cell;
  64. int len;
  65. debug("get_addr: %s\n", prop_name);
  66. cell = fdt_getprop(blob, node, prop_name, &len);
  67. if (cell && (len == sizeof(fdt_addr_t) ||
  68. len == sizeof(fdt_addr_t) * 2))
  69. return fdt_addr_to_cpu(*cell);
  70. return FDT_ADDR_T_NONE;
  71. }
  72. s32 fdtdec_get_int(const void *blob, int node, const char *prop_name,
  73. s32 default_val)
  74. {
  75. const s32 *cell;
  76. int len;
  77. debug("get_size: %s\n", prop_name);
  78. cell = fdt_getprop(blob, node, prop_name, &len);
  79. if (cell && len >= sizeof(s32))
  80. return fdt32_to_cpu(cell[0]);
  81. return default_val;
  82. }
  83. int fdtdec_get_is_enabled(const void *blob, int node, int default_val)
  84. {
  85. const char *cell;
  86. cell = fdt_getprop(blob, node, "status", NULL);
  87. if (cell)
  88. return 0 == strcmp(cell, "ok");
  89. return default_val;
  90. }
  91. enum fdt_compat_id fd_dec_lookup(const void *blob, int node)
  92. {
  93. enum fdt_compat_id id;
  94. /* Search our drivers */
  95. for (id = COMPAT_UNKNOWN; id < COMPAT_COUNT; id++)
  96. if (0 == fdt_node_check_compatible(blob, node,
  97. compat_names[id]))
  98. return id;
  99. return COMPAT_UNKNOWN;
  100. }
  101. int fdtdec_next_compatible(const void *blob, int node,
  102. enum fdt_compat_id id)
  103. {
  104. return fdt_node_offset_by_compatible(blob, node, compat_names[id]);
  105. }
  106. int fdtdec_next_alias(const void *blob, const char *name,
  107. enum fdt_compat_id id, int *upto)
  108. {
  109. #define MAX_STR_LEN 20
  110. char str[MAX_STR_LEN + 20];
  111. int node, err;
  112. /* snprintf() is not available */
  113. assert(strlen(name) < MAX_STR_LEN);
  114. sprintf(str, "%.*s%d", MAX_STR_LEN, name, *upto);
  115. (*upto)++;
  116. node = find_alias_node(blob, str);
  117. if (node < 0)
  118. return node;
  119. err = fdt_node_check_compatible(blob, node, compat_names[id]);
  120. if (err < 0)
  121. return err;
  122. return err ? -FDT_ERR_NOTFOUND : node;
  123. }
  124. /* TODO: Can we tighten this code up a little? */
  125. int fdtdec_find_aliases_for_id(const void *blob, const char *name,
  126. enum fdt_compat_id id, int *node_list, int maxcount)
  127. {
  128. int name_len = strlen(name);
  129. int nodes[maxcount];
  130. int num_found = 0;
  131. int offset, node;
  132. int alias_node;
  133. int count;
  134. int i, j;
  135. /* find the alias node if present */
  136. alias_node = fdt_path_offset(blob, "/aliases");
  137. /*
  138. * start with nothing, and we can assume that the root node can't
  139. * match
  140. */
  141. memset(nodes, '\0', sizeof(nodes));
  142. /* First find all the compatible nodes */
  143. for (node = count = 0; node >= 0 && count < maxcount;) {
  144. node = fdtdec_next_compatible(blob, node, id);
  145. if (node >= 0)
  146. nodes[count++] = node;
  147. }
  148. if (node >= 0)
  149. debug("%s: warning: maxcount exceeded with alias '%s'\n",
  150. __func__, name);
  151. /* Now find all the aliases */
  152. memset(node_list, '\0', sizeof(*node_list) * maxcount);
  153. for (offset = fdt_first_property_offset(blob, alias_node);
  154. offset > 0;
  155. offset = fdt_next_property_offset(blob, offset)) {
  156. const struct fdt_property *prop;
  157. const char *path;
  158. int number;
  159. int found;
  160. node = 0;
  161. prop = fdt_get_property_by_offset(blob, offset, NULL);
  162. path = fdt_string(blob, fdt32_to_cpu(prop->nameoff));
  163. if (prop->len && 0 == strncmp(path, name, name_len))
  164. node = fdt_path_offset(blob, prop->data);
  165. if (node <= 0)
  166. continue;
  167. /* Get the alias number */
  168. number = simple_strtoul(path + name_len, NULL, 10);
  169. if (number < 0 || number >= maxcount) {
  170. debug("%s: warning: alias '%s' is out of range\n",
  171. __func__, path);
  172. continue;
  173. }
  174. /* Make sure the node we found is actually in our list! */
  175. found = -1;
  176. for (j = 0; j < count; j++)
  177. if (nodes[j] == node) {
  178. found = j;
  179. break;
  180. }
  181. if (found == -1) {
  182. debug("%s: warning: alias '%s' points to a node "
  183. "'%s' that is missing or is not compatible "
  184. " with '%s'\n", __func__, path,
  185. fdt_get_name(blob, node, NULL),
  186. compat_names[id]);
  187. continue;
  188. }
  189. /*
  190. * Add this node to our list in the right place, and mark
  191. * it as done.
  192. */
  193. if (fdtdec_get_is_enabled(blob, node)) {
  194. node_list[number] = node;
  195. if (number >= num_found)
  196. num_found = number + 1;
  197. }
  198. nodes[j] = 0;
  199. }
  200. /* Add any nodes not mentioned by an alias */
  201. for (i = j = 0; i < maxcount; i++) {
  202. if (!node_list[i]) {
  203. for (; j < maxcount; j++)
  204. if (nodes[j] &&
  205. fdtdec_get_is_enabled(blob, nodes[j]))
  206. break;
  207. /* Have we run out of nodes to add? */
  208. if (j == maxcount)
  209. break;
  210. assert(!node_list[i]);
  211. node_list[i] = nodes[j++];
  212. if (i >= num_found)
  213. num_found = i + 1;
  214. }
  215. }
  216. return num_found;
  217. }
  218. /*
  219. * This function is a little odd in that it accesses global data. At some
  220. * point if the architecture board.c files merge this will make more sense.
  221. * Even now, it is common code.
  222. */
  223. int fdtdec_check_fdt(void)
  224. {
  225. /* We must have an fdt */
  226. if (fdt_check_header(gd->fdt_blob))
  227. panic("No valid fdt found - please append one to U-Boot\n"
  228. "binary or define CONFIG_OF_EMBED\n");
  229. return 0;
  230. }