fdtdec.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351
  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_NVIDIA_TEGRA20_USB, /* Tegra2 USB port */
  55. COMPAT_NVIDIA_TEGRA20_I2C, /* Tegra2 i2c */
  56. COMPAT_NVIDIA_TEGRA20_DVC, /* Tegra2 dvc (really just i2c) */
  57. COMPAT_NVIDIA_TEGRA20_EMC, /* Tegra2 memory controller */
  58. COMPAT_NVIDIA_TEGRA20_EMC_TABLE, /* Tegra2 memory timing table */
  59. COMPAT_COUNT,
  60. };
  61. /* GPIOs are numbered from 0 */
  62. enum {
  63. FDT_GPIO_NONE = -1U, /* an invalid GPIO used to end our list */
  64. FDT_GPIO_ACTIVE_LOW = 1 << 0, /* input is active low (else high) */
  65. };
  66. /* This is the state of a GPIO pin as defined by the fdt */
  67. struct fdt_gpio_state {
  68. const char *name; /* name of the fdt property defining this */
  69. uint gpio; /* GPIO number, or FDT_GPIO_NONE if none */
  70. u8 flags; /* FDT_GPIO_... flags */
  71. };
  72. /* This tells us whether a fdt_gpio_state record is valid or not */
  73. #define fdt_gpio_isvalid(x) ((x)->gpio != FDT_GPIO_NONE)
  74. /**
  75. * Find the next numbered alias for a peripheral. This is used to enumerate
  76. * all the peripherals of a certain type.
  77. *
  78. * Do the first call with *upto = 0. Assuming /aliases/<name>0 exists then
  79. * this function will return a pointer to the node the alias points to, and
  80. * then update *upto to 1. Next time you call this function, the next node
  81. * will be returned.
  82. *
  83. * All nodes returned will match the compatible ID, as it is assumed that
  84. * all peripherals use the same driver.
  85. *
  86. * @param blob FDT blob to use
  87. * @param name Root name of alias to search for
  88. * @param id Compatible ID to look for
  89. * @return offset of next compatible node, or -FDT_ERR_NOTFOUND if no more
  90. */
  91. int fdtdec_next_alias(const void *blob, const char *name,
  92. enum fdt_compat_id id, int *upto);
  93. /**
  94. * Find the next compatible node for a peripheral.
  95. *
  96. * Do the first call with node = 0. This function will return a pointer to
  97. * the next compatible node. Next time you call this function, pass the
  98. * value returned, and the next node will be provided.
  99. *
  100. * @param blob FDT blob to use
  101. * @param node Start node for search
  102. * @param id Compatible ID to look for (enum fdt_compat_id)
  103. * @return offset of next compatible node, or -FDT_ERR_NOTFOUND if no more
  104. */
  105. int fdtdec_next_compatible(const void *blob, int node,
  106. enum fdt_compat_id id);
  107. /**
  108. * Find the next compatible subnode for a peripheral.
  109. *
  110. * Do the first call with node set to the parent and depth = 0. This
  111. * function will return the offset of the next compatible node. Next time
  112. * you call this function, pass the node value returned last time, with
  113. * depth unchanged, and the next node will be provided.
  114. *
  115. * @param blob FDT blob to use
  116. * @param node Start node for search
  117. * @param id Compatible ID to look for (enum fdt_compat_id)
  118. * @param depthp Current depth (set to 0 before first call)
  119. * @return offset of next compatible node, or -FDT_ERR_NOTFOUND if no more
  120. */
  121. int fdtdec_next_compatible_subnode(const void *blob, int node,
  122. enum fdt_compat_id id, int *depthp);
  123. /**
  124. * Look up an address property in a node and return it as an address.
  125. * The property must hold either one address with no trailing data or
  126. * one address with a length. This is only tested on 32-bit machines.
  127. *
  128. * @param blob FDT blob
  129. * @param node node to examine
  130. * @param prop_name name of property to find
  131. * @return address, if found, or FDT_ADDR_T_NONE if not
  132. */
  133. fdt_addr_t fdtdec_get_addr(const void *blob, int node,
  134. const char *prop_name);
  135. /**
  136. * Look up a 32-bit integer property in a node and return it. The property
  137. * must have at least 4 bytes of data. The value of the first cell is
  138. * returned.
  139. *
  140. * @param blob FDT blob
  141. * @param node node to examine
  142. * @param prop_name name of property to find
  143. * @param default_val default value to return if the property is not found
  144. * @return integer value, if found, or default_val if not
  145. */
  146. s32 fdtdec_get_int(const void *blob, int node, const char *prop_name,
  147. s32 default_val);
  148. /**
  149. * Checks whether a node is enabled.
  150. * This looks for a 'status' property. If this exists, then returns 1 if
  151. * the status is 'ok' and 0 otherwise. If there is no status property,
  152. * it returns 1 on the assumption that anything mentioned should be enabled
  153. * by default.
  154. *
  155. * @param blob FDT blob
  156. * @param node node to examine
  157. * @return integer value 0 (not enabled) or 1 (enabled)
  158. */
  159. int fdtdec_get_is_enabled(const void *blob, int node);
  160. /**
  161. * Make sure we have a valid fdt available to control U-Boot.
  162. *
  163. * If not, a message is printed to the console if the console is ready.
  164. *
  165. * @return 0 if all ok, -1 if not
  166. */
  167. int fdtdec_prepare_fdt(void);
  168. /**
  169. * Checks that we have a valid fdt available to control U-Boot.
  170. * However, if not then for the moment nothing is done, since this function
  171. * is called too early to panic().
  172. *
  173. * @returns 0
  174. */
  175. int fdtdec_check_fdt(void);
  176. /**
  177. * Find the nodes for a peripheral and return a list of them in the correct
  178. * order. This is used to enumerate all the peripherals of a certain type.
  179. *
  180. * To use this, optionally set up a /aliases node with alias properties for
  181. * a peripheral. For example, for usb you could have:
  182. *
  183. * aliases {
  184. * usb0 = "/ehci@c5008000";
  185. * usb1 = "/ehci@c5000000";
  186. * };
  187. *
  188. * Pass "usb" as the name to this function and will return a list of two
  189. * nodes offsets: /ehci@c5008000 and ehci@c5000000.
  190. *
  191. * All nodes returned will match the compatible ID, as it is assumed that
  192. * all peripherals use the same driver.
  193. *
  194. * If no alias node is found, then the node list will be returned in the
  195. * order found in the fdt. If the aliases mention a node which doesn't
  196. * exist, then this will be ignored. If nodes are found with no aliases,
  197. * they will be added in any order.
  198. *
  199. * If there is a gap in the aliases, then this function return a 0 node at
  200. * that position. The return value will also count these gaps.
  201. *
  202. * This function checks node properties and will not return nodes which are
  203. * marked disabled (status = "disabled").
  204. *
  205. * @param blob FDT blob to use
  206. * @param name Root name of alias to search for
  207. * @param id Compatible ID to look for
  208. * @param node_list Place to put list of found nodes
  209. * @param maxcount Maximum number of nodes to find
  210. * @return number of nodes found on success, FTD_ERR_... on error
  211. */
  212. int fdtdec_find_aliases_for_id(const void *blob, const char *name,
  213. enum fdt_compat_id id, int *node_list, int maxcount);
  214. /*
  215. * This function is similar to fdtdec_find_aliases_for_id() except that it
  216. * adds to the node_list that is passed in. Any 0 elements are considered
  217. * available for allocation - others are considered already used and are
  218. * skipped.
  219. *
  220. * You can use this by calling fdtdec_find_aliases_for_id() with an
  221. * uninitialised array, then setting the elements that are returned to -1,
  222. * say, then calling this function, perhaps with a different compat id.
  223. * Any elements you get back that are >0 are new nodes added by the call
  224. * to this function.
  225. *
  226. * Note that if you have some nodes with aliases and some without, you are
  227. * sailing close to the wind. The call to fdtdec_find_aliases_for_id() with
  228. * one compat_id may fill in positions for which you have aliases defined
  229. * for another compat_id. When you later call *this* function with the second
  230. * compat_id, the alias positions may already be used. A debug warning may
  231. * be generated in this case, but it is safest to define aliases for all
  232. * nodes when you care about the ordering.
  233. */
  234. int fdtdec_add_aliases_for_id(const void *blob, const char *name,
  235. enum fdt_compat_id id, int *node_list, int maxcount);
  236. /*
  237. * Get the name for a compatible ID
  238. *
  239. * @param id Compatible ID to look for
  240. * @return compatible string for that id
  241. */
  242. const char *fdtdec_get_compatible(enum fdt_compat_id id);
  243. /* Look up a phandle and follow it to its node. Then return the offset
  244. * of that node.
  245. *
  246. * @param blob FDT blob
  247. * @param node node to examine
  248. * @param prop_name name of property to find
  249. * @return node offset if found, -ve error code on error
  250. */
  251. int fdtdec_lookup_phandle(const void *blob, int node, const char *prop_name);
  252. /**
  253. * Look up a property in a node and return its contents in an integer
  254. * array of given length. The property must have at least enough data for
  255. * the array (4*count bytes). It may have more, but this will be ignored.
  256. *
  257. * @param blob FDT blob
  258. * @param node node to examine
  259. * @param prop_name name of property to find
  260. * @param array array to fill with data
  261. * @param count number of array elements
  262. * @return 0 if ok, or -FDT_ERR_NOTFOUND if the property is not found,
  263. * or -FDT_ERR_BADLAYOUT if not enough data
  264. */
  265. int fdtdec_get_int_array(const void *blob, int node, const char *prop_name,
  266. u32 *array, int count);
  267. /**
  268. * Look up a property in a node and return a pointer to its contents as a
  269. * unsigned int array of given length. The property must have at least enough
  270. * data for the array ('count' cells). It may have more, but this will be
  271. * ignored. The data is not copied.
  272. *
  273. * Note that you must access elements of the array with fdt32_to_cpu(),
  274. * since the elements will be big endian even on a little endian machine.
  275. *
  276. * @param blob FDT blob
  277. * @param node node to examine
  278. * @param prop_name name of property to find
  279. * @param count number of array elements
  280. * @return pointer to array if found, or NULL if the property is not
  281. * found or there is not enough data
  282. */
  283. const u32 *fdtdec_locate_array(const void *blob, int node,
  284. const char *prop_name, int count);
  285. /**
  286. * Look up a boolean property in a node and return it.
  287. *
  288. * A boolean properly is true if present in the device tree and false if not
  289. * present, regardless of its value.
  290. *
  291. * @param blob FDT blob
  292. * @param node node to examine
  293. * @param prop_name name of property to find
  294. * @return 1 if the properly is present; 0 if it isn't present
  295. */
  296. int fdtdec_get_bool(const void *blob, int node, const char *prop_name);
  297. /**
  298. * Decode a single GPIOs from an FDT.
  299. *
  300. * If the property is not found, then the GPIO structure will still be
  301. * initialised, with gpio set to FDT_GPIO_NONE. This makes it easy to
  302. * provide optional GPIOs.
  303. *
  304. * @param blob FDT blob to use
  305. * @param node Node to look at
  306. * @param prop_name Node property name
  307. * @param gpio gpio elements to fill from FDT
  308. * @return 0 if ok, -FDT_ERR_NOTFOUND if the property is missing.
  309. */
  310. int fdtdec_decode_gpio(const void *blob, int node, const char *prop_name,
  311. struct fdt_gpio_state *gpio);
  312. /**
  313. * Set up a GPIO pin according to the provided gpio information. At present this
  314. * just requests the GPIO.
  315. *
  316. * If the gpio is FDT_GPIO_NONE, no action is taken. This makes it easy to
  317. * deal with optional GPIOs.
  318. *
  319. * @param gpio GPIO info to use for set up
  320. * @return 0 if all ok or gpio was FDT_GPIO_NONE; -1 on error
  321. */
  322. int fdtdec_setup_gpio(struct fdt_gpio_state *gpio);