fdtdec.c 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  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. COMPAT(UNKNOWN, "<none>"),
  34. };
  35. const char *fdtdec_get_compatible(enum fdt_compat_id id)
  36. {
  37. /* We allow reading of the 'unknown' ID for testing purposes */
  38. assert(id >= 0 && id < COMPAT_COUNT);
  39. return compat_names[id];
  40. }
  41. /**
  42. * Look in the FDT for an alias with the given name and return its node.
  43. *
  44. * @param blob FDT blob
  45. * @param name alias name to look up
  46. * @return node offset if found, or an error code < 0 otherwise
  47. */
  48. static int find_alias_node(const void *blob, const char *name)
  49. {
  50. const char *path;
  51. int alias_node;
  52. debug("find_alias_node: %s\n", name);
  53. alias_node = fdt_path_offset(blob, "/aliases");
  54. if (alias_node < 0)
  55. return alias_node;
  56. path = fdt_getprop(blob, alias_node, name, NULL);
  57. if (!path)
  58. return -FDT_ERR_NOTFOUND;
  59. return fdt_path_offset(blob, path);
  60. }
  61. fdt_addr_t fdtdec_get_addr(const void *blob, int node,
  62. const char *prop_name)
  63. {
  64. const fdt_addr_t *cell;
  65. int len;
  66. debug("get_addr: %s\n", prop_name);
  67. cell = fdt_getprop(blob, node, prop_name, &len);
  68. if (cell && (len == sizeof(fdt_addr_t) ||
  69. len == sizeof(fdt_addr_t) * 2))
  70. return fdt_addr_to_cpu(*cell);
  71. return FDT_ADDR_T_NONE;
  72. }
  73. s32 fdtdec_get_int(const void *blob, int node, const char *prop_name,
  74. s32 default_val)
  75. {
  76. const s32 *cell;
  77. int len;
  78. debug("get_size: %s\n", prop_name);
  79. cell = fdt_getprop(blob, node, prop_name, &len);
  80. if (cell && len >= sizeof(s32))
  81. return fdt32_to_cpu(cell[0]);
  82. return default_val;
  83. }
  84. int fdtdec_get_is_enabled(const void *blob, int node)
  85. {
  86. const char *cell;
  87. /*
  88. * It should say "okay", so only allow that. Some fdts use "ok" but
  89. * this is a bug. Please fix your device tree source file. See here
  90. * for discussion:
  91. *
  92. * http://www.mail-archive.com/u-boot@lists.denx.de/msg71598.html
  93. */
  94. cell = fdt_getprop(blob, node, "status", NULL);
  95. if (cell)
  96. return 0 == strcmp(cell, "okay");
  97. return 1;
  98. }
  99. enum fdt_compat_id fd_dec_lookup(const void *blob, int node)
  100. {
  101. enum fdt_compat_id id;
  102. /* Search our drivers */
  103. for (id = COMPAT_UNKNOWN; id < COMPAT_COUNT; id++)
  104. if (0 == fdt_node_check_compatible(blob, node,
  105. compat_names[id]))
  106. return id;
  107. return COMPAT_UNKNOWN;
  108. }
  109. int fdtdec_next_compatible(const void *blob, int node,
  110. enum fdt_compat_id id)
  111. {
  112. return fdt_node_offset_by_compatible(blob, node, compat_names[id]);
  113. }
  114. int fdtdec_next_alias(const void *blob, const char *name,
  115. enum fdt_compat_id id, int *upto)
  116. {
  117. #define MAX_STR_LEN 20
  118. char str[MAX_STR_LEN + 20];
  119. int node, err;
  120. /* snprintf() is not available */
  121. assert(strlen(name) < MAX_STR_LEN);
  122. sprintf(str, "%.*s%d", MAX_STR_LEN, name, *upto);
  123. node = find_alias_node(blob, str);
  124. if (node < 0)
  125. return node;
  126. err = fdt_node_check_compatible(blob, node, compat_names[id]);
  127. if (err < 0)
  128. return err;
  129. if (err)
  130. return -FDT_ERR_NOTFOUND;
  131. (*upto)++;
  132. return node;
  133. }
  134. /* TODO: Can we tighten this code up a little? */
  135. int fdtdec_find_aliases_for_id(const void *blob, const char *name,
  136. enum fdt_compat_id id, int *node_list, int maxcount)
  137. {
  138. int name_len = strlen(name);
  139. int nodes[maxcount];
  140. int num_found = 0;
  141. int offset, node;
  142. int alias_node;
  143. int count;
  144. int i, j;
  145. /* find the alias node if present */
  146. alias_node = fdt_path_offset(blob, "/aliases");
  147. /*
  148. * start with nothing, and we can assume that the root node can't
  149. * match
  150. */
  151. memset(nodes, '\0', sizeof(nodes));
  152. /* First find all the compatible nodes */
  153. for (node = count = 0; node >= 0 && count < maxcount;) {
  154. node = fdtdec_next_compatible(blob, node, id);
  155. if (node >= 0)
  156. nodes[count++] = node;
  157. }
  158. if (node >= 0)
  159. debug("%s: warning: maxcount exceeded with alias '%s'\n",
  160. __func__, name);
  161. /* Now find all the aliases */
  162. memset(node_list, '\0', sizeof(*node_list) * maxcount);
  163. for (offset = fdt_first_property_offset(blob, alias_node);
  164. offset > 0;
  165. offset = fdt_next_property_offset(blob, offset)) {
  166. const struct fdt_property *prop;
  167. const char *path;
  168. int number;
  169. int found;
  170. node = 0;
  171. prop = fdt_get_property_by_offset(blob, offset, NULL);
  172. path = fdt_string(blob, fdt32_to_cpu(prop->nameoff));
  173. if (prop->len && 0 == strncmp(path, name, name_len))
  174. node = fdt_path_offset(blob, prop->data);
  175. if (node <= 0)
  176. continue;
  177. /* Get the alias number */
  178. number = simple_strtoul(path + name_len, NULL, 10);
  179. if (number < 0 || number >= maxcount) {
  180. debug("%s: warning: alias '%s' is out of range\n",
  181. __func__, path);
  182. continue;
  183. }
  184. /* Make sure the node we found is actually in our list! */
  185. found = -1;
  186. for (j = 0; j < count; j++)
  187. if (nodes[j] == node) {
  188. found = j;
  189. break;
  190. }
  191. if (found == -1) {
  192. debug("%s: warning: alias '%s' points to a node "
  193. "'%s' that is missing or is not compatible "
  194. " with '%s'\n", __func__, path,
  195. fdt_get_name(blob, node, NULL),
  196. compat_names[id]);
  197. continue;
  198. }
  199. /*
  200. * Add this node to our list in the right place, and mark
  201. * it as done.
  202. */
  203. if (fdtdec_get_is_enabled(blob, node)) {
  204. node_list[number] = node;
  205. if (number >= num_found)
  206. num_found = number + 1;
  207. }
  208. nodes[j] = 0;
  209. }
  210. /* Add any nodes not mentioned by an alias */
  211. for (i = j = 0; i < maxcount; i++) {
  212. if (!node_list[i]) {
  213. for (; j < maxcount; j++)
  214. if (nodes[j] &&
  215. fdtdec_get_is_enabled(blob, nodes[j]))
  216. break;
  217. /* Have we run out of nodes to add? */
  218. if (j == maxcount)
  219. break;
  220. assert(!node_list[i]);
  221. node_list[i] = nodes[j++];
  222. if (i >= num_found)
  223. num_found = i + 1;
  224. }
  225. }
  226. return num_found;
  227. }
  228. /*
  229. * This function is a little odd in that it accesses global data. At some
  230. * point if the architecture board.c files merge this will make more sense.
  231. * Even now, it is common code.
  232. */
  233. int fdtdec_check_fdt(void)
  234. {
  235. /* We must have an fdt */
  236. if (((uintptr_t)gd->fdt_blob & 3) || fdt_check_header(gd->fdt_blob))
  237. panic("No valid fdt found - please append one to U-Boot\n"
  238. "binary or define CONFIG_OF_EMBED\n");
  239. return 0;
  240. }