fdtdec.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  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. /**
  35. * Look in the FDT for an alias with the given name and return its node.
  36. *
  37. * @param blob FDT blob
  38. * @param name alias name to look up
  39. * @return node offset if found, or an error code < 0 otherwise
  40. */
  41. static int find_alias_node(const void *blob, const char *name)
  42. {
  43. const char *path;
  44. int alias_node;
  45. debug("find_alias_node: %s\n", name);
  46. alias_node = fdt_path_offset(blob, "/aliases");
  47. if (alias_node < 0)
  48. return alias_node;
  49. path = fdt_getprop(blob, alias_node, name, NULL);
  50. if (!path)
  51. return -FDT_ERR_NOTFOUND;
  52. return fdt_path_offset(blob, path);
  53. }
  54. fdt_addr_t fdtdec_get_addr(const void *blob, int node,
  55. const char *prop_name)
  56. {
  57. const fdt_addr_t *cell;
  58. int len;
  59. debug("get_addr: %s\n", prop_name);
  60. cell = fdt_getprop(blob, node, prop_name, &len);
  61. if (cell && (len == sizeof(fdt_addr_t) ||
  62. len == sizeof(fdt_addr_t) * 2))
  63. return fdt_addr_to_cpu(*cell);
  64. return FDT_ADDR_T_NONE;
  65. }
  66. s32 fdtdec_get_int(const void *blob, int node, const char *prop_name,
  67. s32 default_val)
  68. {
  69. const s32 *cell;
  70. int len;
  71. debug("get_size: %s\n", prop_name);
  72. cell = fdt_getprop(blob, node, prop_name, &len);
  73. if (cell && len >= sizeof(s32))
  74. return fdt32_to_cpu(cell[0]);
  75. return default_val;
  76. }
  77. int fdtdec_get_is_enabled(const void *blob, int node, int default_val)
  78. {
  79. const char *cell;
  80. cell = fdt_getprop(blob, node, "status", NULL);
  81. if (cell)
  82. return 0 == strcmp(cell, "ok");
  83. return default_val;
  84. }
  85. enum fdt_compat_id fd_dec_lookup(const void *blob, int node)
  86. {
  87. enum fdt_compat_id id;
  88. /* Search our drivers */
  89. for (id = COMPAT_UNKNOWN; id < COMPAT_COUNT; id++)
  90. if (0 == fdt_node_check_compatible(blob, node,
  91. compat_names[id]))
  92. return id;
  93. return COMPAT_UNKNOWN;
  94. }
  95. int fdtdec_next_compatible(const void *blob, int node,
  96. enum fdt_compat_id id)
  97. {
  98. return fdt_node_offset_by_compatible(blob, node, compat_names[id]);
  99. }
  100. int fdtdec_next_alias(const void *blob, const char *name,
  101. enum fdt_compat_id id, int *upto)
  102. {
  103. #define MAX_STR_LEN 20
  104. char str[MAX_STR_LEN + 20];
  105. int node, err;
  106. /* snprintf() is not available */
  107. assert(strlen(name) < MAX_STR_LEN);
  108. sprintf(str, "%.*s%d", MAX_STR_LEN, name, *upto);
  109. (*upto)++;
  110. node = find_alias_node(blob, str);
  111. if (node < 0)
  112. return node;
  113. err = fdt_node_check_compatible(blob, node, compat_names[id]);
  114. if (err < 0)
  115. return err;
  116. return err ? -FDT_ERR_NOTFOUND : node;
  117. }
  118. /*
  119. * This function is a little odd in that it accesses global data. At some
  120. * point if the architecture board.c files merge this will make more sense.
  121. * Even now, it is common code.
  122. */
  123. int fdtdec_check_fdt(void)
  124. {
  125. /* We must have an fdt */
  126. if (fdt_check_header(gd->fdt_blob))
  127. panic("No valid fdt found - please append one to U-Boot\n"
  128. "binary or define CONFIG_OF_EMBED\n");
  129. return 0;
  130. }