fdtdec.h 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  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. /*
  22. * This file contains convenience functions for decoding useful and
  23. * enlightening information from FDTs. It is intended to be used by device
  24. * drivers and board-specific code within U-Boot. It aims to reduce the
  25. * amount of FDT munging required within U-Boot itself, so that driver code
  26. * changes to support FDT are minimized.
  27. */
  28. #include <libfdt.h>
  29. /*
  30. * A typedef for a physical address. Note that fdt data is always big
  31. * endian even on a litle endian machine.
  32. */
  33. #ifdef CONFIG_PHYS_64BIT
  34. typedef u64 fdt_addr_t;
  35. #define FDT_ADDR_T_NONE (-1ULL)
  36. #define fdt_addr_to_cpu(reg) be64_to_cpu(reg)
  37. #else
  38. typedef u32 fdt_addr_t;
  39. #define FDT_ADDR_T_NONE (-1U)
  40. #define fdt_addr_to_cpu(reg) be32_to_cpu(reg)
  41. #endif
  42. /* Information obtained about memory from the FDT */
  43. struct fdt_memory {
  44. fdt_addr_t start;
  45. fdt_addr_t end;
  46. };
  47. /**
  48. * Compat types that we know about and for which we might have drivers.
  49. * Each is named COMPAT_<dir>_<filename> where <dir> is the directory
  50. * within drivers.
  51. */
  52. enum fdt_compat_id {
  53. COMPAT_UNKNOWN,
  54. COMPAT_COUNT,
  55. };
  56. /**
  57. * Find the next numbered alias for a peripheral. This is used to enumerate
  58. * all the peripherals of a certain type.
  59. *
  60. * Do the first call with *upto = 0. Assuming /aliases/<name>0 exists then
  61. * this function will return a pointer to the node the alias points to, and
  62. * then update *upto to 1. Next time you call this function, the next node
  63. * will be returned.
  64. *
  65. * All nodes returned will match the compatible ID, as it is assumed that
  66. * all peripherals use the same driver.
  67. *
  68. * @param blob FDT blob to use
  69. * @param name Root name of alias to search for
  70. * @param id Compatible ID to look for
  71. * @return offset of next compatible node, or -FDT_ERR_NOTFOUND if no more
  72. */
  73. int fdtdec_next_alias(const void *blob, const char *name,
  74. enum fdt_compat_id id, int *upto);
  75. /**
  76. * Find the next compatible node for a peripheral.
  77. *
  78. * Do the first call with node = 0. This function will return a pointer to
  79. * the next compatible node. Next time you call this function, pass the
  80. * value returned, and the next node will be provided.
  81. *
  82. * @param blob FDT blob to use
  83. * @param node Start node for search
  84. * @param id Compatible ID to look for (enum fdt_compat_id)
  85. * @return offset of next compatible node, or -FDT_ERR_NOTFOUND if no more
  86. */
  87. int fdtdec_next_compatible(const void *blob, int node,
  88. enum fdt_compat_id id);
  89. /**
  90. * Look up an address property in a node and return it as an address.
  91. * The property must hold either one address with no trailing data or
  92. * one address with a length. This is only tested on 32-bit machines.
  93. *
  94. * @param blob FDT blob
  95. * @param node node to examine
  96. * @param prop_name name of property to find
  97. * @return address, if found, or FDT_ADDR_T_NONE if not
  98. */
  99. fdt_addr_t fdtdec_get_addr(const void *blob, int node,
  100. const char *prop_name);
  101. /**
  102. * Look up a 32-bit integer property in a node and return it. The property
  103. * must have at least 4 bytes of data. The value of the first cell is
  104. * returned.
  105. *
  106. * @param blob FDT blob
  107. * @param node node to examine
  108. * @param prop_name name of property to find
  109. * @param default_val default value to return if the property is not found
  110. * @return integer value, if found, or default_val if not
  111. */
  112. s32 fdtdec_get_int(const void *blob, int node, const char *prop_name,
  113. s32 default_val);
  114. /**
  115. * Checks whether a node is enabled.
  116. * This looks for a 'status' property. If this exists, then returns 1 if
  117. * the status is 'ok' and 0 otherwise. If there is no status property,
  118. * it returns 1 on the assumption that anything mentioned should be enabled
  119. * by default.
  120. *
  121. * @param blob FDT blob
  122. * @param node node to examine
  123. * @return integer value 0 (not enabled) or 1 (enabled)
  124. */
  125. int fdtdec_get_is_enabled(const void *blob, int node);
  126. /**
  127. * Checks whether we have a valid fdt available to control U-Boot, and panic
  128. * if not.
  129. */
  130. int fdtdec_check_fdt(void);
  131. /**
  132. * Find the nodes for a peripheral and return a list of them in the correct
  133. * order. This is used to enumerate all the peripherals of a certain type.
  134. *
  135. * To use this, optionally set up a /aliases node with alias properties for
  136. * a peripheral. For example, for usb you could have:
  137. *
  138. * aliases {
  139. * usb0 = "/ehci@c5008000";
  140. * usb1 = "/ehci@c5000000";
  141. * };
  142. *
  143. * Pass "usb" as the name to this function and will return a list of two
  144. * nodes offsets: /ehci@c5008000 and ehci@c5000000.
  145. *
  146. * All nodes returned will match the compatible ID, as it is assumed that
  147. * all peripherals use the same driver.
  148. *
  149. * If no alias node is found, then the node list will be returned in the
  150. * order found in the fdt. If the aliases mention a node which doesn't
  151. * exist, then this will be ignored. If nodes are found with no aliases,
  152. * they will be added in any order.
  153. *
  154. * If there is a gap in the aliases, then this function return a 0 node at
  155. * that position. The return value will also count these gaps.
  156. *
  157. * This function checks node properties and will not return nodes which are
  158. * marked disabled (status = "disabled").
  159. *
  160. * @param blob FDT blob to use
  161. * @param name Root name of alias to search for
  162. * @param id Compatible ID to look for
  163. * @param node_list Place to put list of found nodes
  164. * @param maxcount Maximum number of nodes to find
  165. * @return number of nodes found on success, FTD_ERR_... on error
  166. */
  167. int fdtdec_find_aliases_for_id(const void *blob, const char *name,
  168. enum fdt_compat_id id, int *node_list, int maxcount);
  169. /*
  170. * Get the name for a compatible ID
  171. *
  172. * @param id Compatible ID to look for
  173. * @return compatible string for that id
  174. */
  175. const char *fdtdec_get_compatible(enum fdt_compat_id id);