fdtdec.h 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781
  1. /*
  2. * Copyright (c) 2011 The Chromium OS Authors.
  3. * SPDX-License-Identifier: GPL-2.0+
  4. */
  5. #ifndef __fdtdec_h
  6. #define __fdtdec_h
  7. /*
  8. * This file contains convenience functions for decoding useful and
  9. * enlightening information from FDTs. It is intended to be used by device
  10. * drivers and board-specific code within U-Boot. It aims to reduce the
  11. * amount of FDT munging required within U-Boot itself, so that driver code
  12. * changes to support FDT are minimized.
  13. */
  14. #include <libfdt.h>
  15. #include <pci.h>
  16. /*
  17. * A typedef for a physical address. Note that fdt data is always big
  18. * endian even on a litle endian machine.
  19. */
  20. #ifdef CONFIG_PHYS_64BIT
  21. typedef u64 fdt_addr_t;
  22. typedef u64 fdt_size_t;
  23. #define FDT_ADDR_T_NONE (-1ULL)
  24. #define fdt_addr_to_cpu(reg) be64_to_cpu(reg)
  25. #define fdt_size_to_cpu(reg) be64_to_cpu(reg)
  26. #else
  27. typedef u32 fdt_addr_t;
  28. typedef u32 fdt_size_t;
  29. #define FDT_ADDR_T_NONE (-1U)
  30. #define fdt_addr_to_cpu(reg) be32_to_cpu(reg)
  31. #define fdt_size_to_cpu(reg) be32_to_cpu(reg)
  32. #endif
  33. /* Information obtained about memory from the FDT */
  34. struct fdt_memory {
  35. fdt_addr_t start;
  36. fdt_addr_t end;
  37. };
  38. /*
  39. * Information about a resource. start is the first address of the resource
  40. * and end is the last address (inclusive). The length of the resource will
  41. * be equal to: end - start + 1.
  42. */
  43. struct fdt_resource {
  44. fdt_addr_t start;
  45. fdt_addr_t end;
  46. };
  47. enum fdt_pci_space {
  48. FDT_PCI_SPACE_CONFIG = 0,
  49. FDT_PCI_SPACE_IO = 0x01000000,
  50. FDT_PCI_SPACE_MEM32 = 0x02000000,
  51. FDT_PCI_SPACE_MEM64 = 0x03000000,
  52. FDT_PCI_SPACE_MEM32_PREF = 0x42000000,
  53. FDT_PCI_SPACE_MEM64_PREF = 0x43000000,
  54. };
  55. #define FDT_PCI_ADDR_CELLS 3
  56. #define FDT_PCI_SIZE_CELLS 2
  57. #define FDT_PCI_REG_SIZE \
  58. ((FDT_PCI_ADDR_CELLS + FDT_PCI_SIZE_CELLS) * sizeof(u32))
  59. /*
  60. * The Open Firmware spec defines PCI physical address as follows:
  61. *
  62. * bits# 31 .... 24 23 .... 16 15 .... 08 07 .... 00
  63. *
  64. * phys.hi cell: npt000ss bbbbbbbb dddddfff rrrrrrrr
  65. * phys.mid cell: hhhhhhhh hhhhhhhh hhhhhhhh hhhhhhhh
  66. * phys.lo cell: llllllll llllllll llllllll llllllll
  67. *
  68. * where:
  69. *
  70. * n: is 0 if the address is relocatable, 1 otherwise
  71. * p: is 1 if addressable region is prefetchable, 0 otherwise
  72. * t: is 1 if the address is aliased (for non-relocatable I/O) below 1MB
  73. * (for Memory), or below 64KB (for relocatable I/O)
  74. * ss: is the space code, denoting the address space
  75. * bbbbbbbb: is the 8-bit Bus Number
  76. * ddddd: is the 5-bit Device Number
  77. * fff: is the 3-bit Function Number
  78. * rrrrrrrr: is the 8-bit Register Number
  79. * hhhhhhhh: is a 32-bit unsigned number
  80. * llllllll: is a 32-bit unsigned number
  81. */
  82. struct fdt_pci_addr {
  83. u32 phys_hi;
  84. u32 phys_mid;
  85. u32 phys_lo;
  86. };
  87. /**
  88. * Compute the size of a resource.
  89. *
  90. * @param res the resource to operate on
  91. * @return the size of the resource
  92. */
  93. static inline fdt_size_t fdt_resource_size(const struct fdt_resource *res)
  94. {
  95. return res->end - res->start + 1;
  96. }
  97. /**
  98. * Compat types that we know about and for which we might have drivers.
  99. * Each is named COMPAT_<dir>_<filename> where <dir> is the directory
  100. * within drivers.
  101. */
  102. enum fdt_compat_id {
  103. COMPAT_UNKNOWN,
  104. COMPAT_NVIDIA_TEGRA20_USB, /* Tegra20 USB port */
  105. COMPAT_NVIDIA_TEGRA30_USB, /* Tegra30 USB port */
  106. COMPAT_NVIDIA_TEGRA114_USB, /* Tegra114 USB port */
  107. COMPAT_NVIDIA_TEGRA20_EMC, /* Tegra20 memory controller */
  108. COMPAT_NVIDIA_TEGRA20_EMC_TABLE, /* Tegra20 memory timing table */
  109. COMPAT_NVIDIA_TEGRA20_KBC, /* Tegra20 Keyboard */
  110. COMPAT_NVIDIA_TEGRA20_NAND, /* Tegra2 NAND controller */
  111. COMPAT_NVIDIA_TEGRA20_PWM, /* Tegra 2 PWM controller */
  112. COMPAT_NVIDIA_TEGRA20_DC, /* Tegra 2 Display controller */
  113. COMPAT_NVIDIA_TEGRA124_SDMMC, /* Tegra124 SDMMC controller */
  114. COMPAT_NVIDIA_TEGRA30_SDMMC, /* Tegra30 SDMMC controller */
  115. COMPAT_NVIDIA_TEGRA20_SDMMC, /* Tegra20 SDMMC controller */
  116. COMPAT_NVIDIA_TEGRA124_PCIE, /* Tegra 124 PCIe controller */
  117. COMPAT_NVIDIA_TEGRA30_PCIE, /* Tegra 30 PCIe controller */
  118. COMPAT_NVIDIA_TEGRA20_PCIE, /* Tegra 20 PCIe controller */
  119. COMPAT_NVIDIA_TEGRA124_XUSB_PADCTL,
  120. /* Tegra124 XUSB pad controller */
  121. COMPAT_SMSC_LAN9215, /* SMSC 10/100 Ethernet LAN9215 */
  122. COMPAT_SAMSUNG_EXYNOS5_SROMC, /* Exynos5 SROMC */
  123. COMPAT_SAMSUNG_S3C2440_I2C, /* Exynos I2C Controller */
  124. COMPAT_SAMSUNG_EXYNOS5_SOUND, /* Exynos Sound */
  125. COMPAT_WOLFSON_WM8994_CODEC, /* Wolfson WM8994 Sound Codec */
  126. COMPAT_GOOGLE_CROS_EC, /* Google CROS_EC Protocol */
  127. COMPAT_GOOGLE_CROS_EC_KEYB, /* Google CROS_EC Keyboard */
  128. COMPAT_SAMSUNG_EXYNOS_EHCI, /* Exynos EHCI controller */
  129. COMPAT_SAMSUNG_EXYNOS5_XHCI, /* Exynos5 XHCI controller */
  130. COMPAT_SAMSUNG_EXYNOS_USB_PHY, /* Exynos phy controller for usb2.0 */
  131. COMPAT_SAMSUNG_EXYNOS5_USB3_PHY,/* Exynos phy controller for usb3.0 */
  132. COMPAT_SAMSUNG_EXYNOS_TMU, /* Exynos TMU */
  133. COMPAT_SAMSUNG_EXYNOS_FIMD, /* Exynos Display controller */
  134. COMPAT_SAMSUNG_EXYNOS_MIPI_DSI, /* Exynos mipi dsi */
  135. COMPAT_SAMSUNG_EXYNOS5_DP, /* Exynos Display port controller */
  136. COMPAT_SAMSUNG_EXYNOS_DWMMC, /* Exynos DWMMC controller */
  137. COMPAT_SAMSUNG_EXYNOS_MMC, /* Exynos MMC controller */
  138. COMPAT_SAMSUNG_EXYNOS_SERIAL, /* Exynos UART */
  139. COMPAT_MAXIM_MAX77686_PMIC, /* MAX77686 PMIC */
  140. COMPAT_GENERIC_SPI_FLASH, /* Generic SPI Flash chip */
  141. COMPAT_MAXIM_98095_CODEC, /* MAX98095 Codec */
  142. COMPAT_INFINEON_SLB9635_TPM, /* Infineon SLB9635 TPM */
  143. COMPAT_INFINEON_SLB9645_TPM, /* Infineon SLB9645 TPM */
  144. COMPAT_SAMSUNG_EXYNOS5_I2C, /* Exynos5 High Speed I2C Controller */
  145. COMPAT_SANDBOX_HOST_EMULATION, /* Sandbox emulation of a function */
  146. COMPAT_SANDBOX_LCD_SDL, /* Sandbox LCD emulation with SDL */
  147. COMPAT_TI_TPS65090, /* Texas Instrument TPS65090 */
  148. COMPAT_NXP_PTN3460, /* NXP PTN3460 DP/LVDS bridge */
  149. COMPAT_SAMSUNG_EXYNOS_SYSMMU, /* Exynos sysmmu */
  150. COMPAT_PARADE_PS8625, /* Parade PS8622 EDP->LVDS bridge */
  151. COMPAT_INTEL_LPC, /* Intel Low Pin Count I/F */
  152. COMPAT_INTEL_MICROCODE, /* Intel microcode update */
  153. COMPAT_MEMORY_SPD, /* Memory SPD information */
  154. COMPAT_INTEL_PANTHERPOINT_AHCI, /* Intel Pantherpoint AHCI */
  155. COMPAT_INTEL_MODEL_206AX, /* Intel Model 206AX CPU */
  156. COMPAT_INTEL_GMA, /* Intel Graphics Media Accelerator */
  157. COMPAT_AMS_AS3722, /* AMS AS3722 PMIC */
  158. COMPAT_INTEL_ICH_SPI, /* Intel ICH7/9 SPI controller */
  159. COMPAT_INTEL_QRK_MRC, /* Intel Quark MRC */
  160. COMPAT_COUNT,
  161. };
  162. #define MAX_PHANDLE_ARGS 16
  163. struct fdtdec_phandle_args {
  164. int node;
  165. int args_count;
  166. uint32_t args[MAX_PHANDLE_ARGS];
  167. };
  168. /**
  169. * fdtdec_parse_phandle_with_args() - Find a node pointed by phandle in a list
  170. *
  171. * This function is useful to parse lists of phandles and their arguments.
  172. *
  173. * Example:
  174. *
  175. * phandle1: node1 {
  176. * #list-cells = <2>;
  177. * }
  178. *
  179. * phandle2: node2 {
  180. * #list-cells = <1>;
  181. * }
  182. *
  183. * node3 {
  184. * list = <&phandle1 1 2 &phandle2 3>;
  185. * }
  186. *
  187. * To get a device_node of the `node2' node you may call this:
  188. * fdtdec_parse_phandle_with_args(blob, node3, "list", "#list-cells", 0, 1,
  189. * &args);
  190. *
  191. * (This function is a modified version of __of_parse_phandle_with_args() from
  192. * Linux 3.18)
  193. *
  194. * @blob: Pointer to device tree
  195. * @src_node: Offset of device tree node containing a list
  196. * @list_name: property name that contains a list
  197. * @cells_name: property name that specifies the phandles' arguments count,
  198. * or NULL to use @cells_count
  199. * @cells_count: Cell count to use if @cells_name is NULL
  200. * @index: index of a phandle to parse out
  201. * @out_args: optional pointer to output arguments structure (will be filled)
  202. * @return 0 on success (with @out_args filled out if not NULL), -ENOENT if
  203. * @list_name does not exist, a phandle was not found, @cells_name
  204. * could not be found, the arguments were truncated or there were too
  205. * many arguments.
  206. *
  207. */
  208. int fdtdec_parse_phandle_with_args(const void *blob, int src_node,
  209. const char *list_name,
  210. const char *cells_name,
  211. int cell_count, int index,
  212. struct fdtdec_phandle_args *out_args);
  213. /**
  214. * Find the next numbered alias for a peripheral. This is used to enumerate
  215. * all the peripherals of a certain type.
  216. *
  217. * Do the first call with *upto = 0. Assuming /aliases/<name>0 exists then
  218. * this function will return a pointer to the node the alias points to, and
  219. * then update *upto to 1. Next time you call this function, the next node
  220. * will be returned.
  221. *
  222. * All nodes returned will match the compatible ID, as it is assumed that
  223. * all peripherals use the same driver.
  224. *
  225. * @param blob FDT blob to use
  226. * @param name Root name of alias to search for
  227. * @param id Compatible ID to look for
  228. * @return offset of next compatible node, or -FDT_ERR_NOTFOUND if no more
  229. */
  230. int fdtdec_next_alias(const void *blob, const char *name,
  231. enum fdt_compat_id id, int *upto);
  232. /**
  233. * Find the compatible ID for a given node.
  234. *
  235. * Generally each node has at least one compatible string attached to it.
  236. * This function looks through our list of known compatible strings and
  237. * returns the corresponding ID which matches the compatible string.
  238. *
  239. * @param blob FDT blob to use
  240. * @param node Node containing compatible string to find
  241. * @return compatible ID, or COMPAT_UNKNOWN if we cannot find a match
  242. */
  243. enum fdt_compat_id fdtdec_lookup(const void *blob, int node);
  244. /**
  245. * Find the next compatible node for a peripheral.
  246. *
  247. * Do the first call with node = 0. This function will return a pointer to
  248. * the next compatible node. Next time you call this function, pass the
  249. * value returned, and the next node will be provided.
  250. *
  251. * @param blob FDT blob to use
  252. * @param node Start node for search
  253. * @param id Compatible ID to look for (enum fdt_compat_id)
  254. * @return offset of next compatible node, or -FDT_ERR_NOTFOUND if no more
  255. */
  256. int fdtdec_next_compatible(const void *blob, int node,
  257. enum fdt_compat_id id);
  258. /**
  259. * Find the next compatible subnode for a peripheral.
  260. *
  261. * Do the first call with node set to the parent and depth = 0. This
  262. * function will return the offset of the next compatible node. Next time
  263. * you call this function, pass the node value returned last time, with
  264. * depth unchanged, and the next node will be provided.
  265. *
  266. * @param blob FDT blob to use
  267. * @param node Start node for search
  268. * @param id Compatible ID to look for (enum fdt_compat_id)
  269. * @param depthp Current depth (set to 0 before first call)
  270. * @return offset of next compatible node, or -FDT_ERR_NOTFOUND if no more
  271. */
  272. int fdtdec_next_compatible_subnode(const void *blob, int node,
  273. enum fdt_compat_id id, int *depthp);
  274. /**
  275. * Look up an address property in a node and return it as an address.
  276. * The property must hold either one address with no trailing data or
  277. * one address with a length. This is only tested on 32-bit machines.
  278. *
  279. * @param blob FDT blob
  280. * @param node node to examine
  281. * @param prop_name name of property to find
  282. * @return address, if found, or FDT_ADDR_T_NONE if not
  283. */
  284. fdt_addr_t fdtdec_get_addr(const void *blob, int node,
  285. const char *prop_name);
  286. /**
  287. * Look up an address property in a node and return it as an address.
  288. * The property must hold one address with a length. This is only tested
  289. * on 32-bit machines.
  290. *
  291. * @param blob FDT blob
  292. * @param node node to examine
  293. * @param prop_name name of property to find
  294. * @return address, if found, or FDT_ADDR_T_NONE if not
  295. */
  296. fdt_addr_t fdtdec_get_addr_size(const void *blob, int node,
  297. const char *prop_name, fdt_size_t *sizep);
  298. /**
  299. * Look at an address property in a node and return the pci address which
  300. * corresponds to the given type in the form of fdt_pci_addr.
  301. * The property must hold one fdt_pci_addr with a lengh.
  302. *
  303. * @param blob FDT blob
  304. * @param node node to examine
  305. * @param type pci address type (FDT_PCI_SPACE_xxx)
  306. * @param prop_name name of property to find
  307. * @param addr returns pci address in the form of fdt_pci_addr
  308. * @return 0 if ok, negative on error
  309. */
  310. int fdtdec_get_pci_addr(const void *blob, int node, enum fdt_pci_space type,
  311. const char *prop_name, struct fdt_pci_addr *addr);
  312. /**
  313. * Look at the compatible property of a device node that represents a PCI
  314. * device and extract pci vendor id and device id from it.
  315. *
  316. * @param blob FDT blob
  317. * @param node node to examine
  318. * @param vendor vendor id of the pci device
  319. * @param device device id of the pci device
  320. * @return 0 if ok, negative on error
  321. */
  322. int fdtdec_get_pci_vendev(const void *blob, int node,
  323. u16 *vendor, u16 *device);
  324. /**
  325. * Look at the pci address of a device node that represents a PCI device
  326. * and parse the bus, device and function number from it.
  327. *
  328. * @param blob FDT blob
  329. * @param node node to examine
  330. * @param addr pci address in the form of fdt_pci_addr
  331. * @param bdf returns bus, device, function triplet
  332. * @return 0 if ok, negative on error
  333. */
  334. int fdtdec_get_pci_bdf(const void *blob, int node,
  335. struct fdt_pci_addr *addr, pci_dev_t *bdf);
  336. /**
  337. * Look at the pci address of a device node that represents a PCI device
  338. * and return base address of the pci device's registers.
  339. *
  340. * @param blob FDT blob
  341. * @param node node to examine
  342. * @param addr pci address in the form of fdt_pci_addr
  343. * @param bar returns base address of the pci device's registers
  344. * @return 0 if ok, negative on error
  345. */
  346. int fdtdec_get_pci_bar32(const void *blob, int node,
  347. struct fdt_pci_addr *addr, u32 *bar);
  348. /**
  349. * Look up a 32-bit integer property in a node and return it. The property
  350. * must have at least 4 bytes of data. The value of the first cell is
  351. * returned.
  352. *
  353. * @param blob FDT blob
  354. * @param node node to examine
  355. * @param prop_name name of property to find
  356. * @param default_val default value to return if the property is not found
  357. * @return integer value, if found, or default_val if not
  358. */
  359. s32 fdtdec_get_int(const void *blob, int node, const char *prop_name,
  360. s32 default_val);
  361. /**
  362. * Look up a 64-bit integer property in a node and return it. The property
  363. * must have at least 8 bytes of data (2 cells). The first two cells are
  364. * concatenated to form a 8 bytes value, where the first cell is top half and
  365. * the second cell is bottom half.
  366. *
  367. * @param blob FDT blob
  368. * @param node node to examine
  369. * @param prop_name name of property to find
  370. * @param default_val default value to return if the property is not found
  371. * @return integer value, if found, or default_val if not
  372. */
  373. uint64_t fdtdec_get_uint64(const void *blob, int node, const char *prop_name,
  374. uint64_t default_val);
  375. /**
  376. * Checks whether a node is enabled.
  377. * This looks for a 'status' property. If this exists, then returns 1 if
  378. * the status is 'ok' and 0 otherwise. If there is no status property,
  379. * it returns 1 on the assumption that anything mentioned should be enabled
  380. * by default.
  381. *
  382. * @param blob FDT blob
  383. * @param node node to examine
  384. * @return integer value 0 (not enabled) or 1 (enabled)
  385. */
  386. int fdtdec_get_is_enabled(const void *blob, int node);
  387. /**
  388. * Make sure we have a valid fdt available to control U-Boot.
  389. *
  390. * If not, a message is printed to the console if the console is ready.
  391. *
  392. * @return 0 if all ok, -1 if not
  393. */
  394. int fdtdec_prepare_fdt(void);
  395. /**
  396. * Checks that we have a valid fdt available to control U-Boot.
  397. * However, if not then for the moment nothing is done, since this function
  398. * is called too early to panic().
  399. *
  400. * @returns 0
  401. */
  402. int fdtdec_check_fdt(void);
  403. /**
  404. * Find the nodes for a peripheral and return a list of them in the correct
  405. * order. This is used to enumerate all the peripherals of a certain type.
  406. *
  407. * To use this, optionally set up a /aliases node with alias properties for
  408. * a peripheral. For example, for usb you could have:
  409. *
  410. * aliases {
  411. * usb0 = "/ehci@c5008000";
  412. * usb1 = "/ehci@c5000000";
  413. * };
  414. *
  415. * Pass "usb" as the name to this function and will return a list of two
  416. * nodes offsets: /ehci@c5008000 and ehci@c5000000.
  417. *
  418. * All nodes returned will match the compatible ID, as it is assumed that
  419. * all peripherals use the same driver.
  420. *
  421. * If no alias node is found, then the node list will be returned in the
  422. * order found in the fdt. If the aliases mention a node which doesn't
  423. * exist, then this will be ignored. If nodes are found with no aliases,
  424. * they will be added in any order.
  425. *
  426. * If there is a gap in the aliases, then this function return a 0 node at
  427. * that position. The return value will also count these gaps.
  428. *
  429. * This function checks node properties and will not return nodes which are
  430. * marked disabled (status = "disabled").
  431. *
  432. * @param blob FDT blob to use
  433. * @param name Root name of alias to search for
  434. * @param id Compatible ID to look for
  435. * @param node_list Place to put list of found nodes
  436. * @param maxcount Maximum number of nodes to find
  437. * @return number of nodes found on success, FTD_ERR_... on error
  438. */
  439. int fdtdec_find_aliases_for_id(const void *blob, const char *name,
  440. enum fdt_compat_id id, int *node_list, int maxcount);
  441. /*
  442. * This function is similar to fdtdec_find_aliases_for_id() except that it
  443. * adds to the node_list that is passed in. Any 0 elements are considered
  444. * available for allocation - others are considered already used and are
  445. * skipped.
  446. *
  447. * You can use this by calling fdtdec_find_aliases_for_id() with an
  448. * uninitialised array, then setting the elements that are returned to -1,
  449. * say, then calling this function, perhaps with a different compat id.
  450. * Any elements you get back that are >0 are new nodes added by the call
  451. * to this function.
  452. *
  453. * Note that if you have some nodes with aliases and some without, you are
  454. * sailing close to the wind. The call to fdtdec_find_aliases_for_id() with
  455. * one compat_id may fill in positions for which you have aliases defined
  456. * for another compat_id. When you later call *this* function with the second
  457. * compat_id, the alias positions may already be used. A debug warning may
  458. * be generated in this case, but it is safest to define aliases for all
  459. * nodes when you care about the ordering.
  460. */
  461. int fdtdec_add_aliases_for_id(const void *blob, const char *name,
  462. enum fdt_compat_id id, int *node_list, int maxcount);
  463. /**
  464. * Get the alias sequence number of a node
  465. *
  466. * This works out whether a node is pointed to by an alias, and if so, the
  467. * sequence number of that alias. Aliases are of the form <base><num> where
  468. * <num> is the sequence number. For example spi2 would be sequence number
  469. * 2.
  470. *
  471. * @param blob Device tree blob (if NULL, then error is returned)
  472. * @param base Base name for alias (before the underscore)
  473. * @param node Node to look up
  474. * @param seqp This is set to the sequence number if one is found,
  475. * but otherwise the value is left alone
  476. * @return 0 if a sequence was found, -ve if not
  477. */
  478. int fdtdec_get_alias_seq(const void *blob, const char *base, int node,
  479. int *seqp);
  480. /**
  481. * Get the offset of the given chosen node
  482. *
  483. * This looks up a property in /chosen containing the path to another node,
  484. * then finds the offset of that node.
  485. *
  486. * @param blob Device tree blob (if NULL, then error is returned)
  487. * @param name Property name, e.g. "stdout-path"
  488. * @return Node offset referred to by that chosen node, or -ve FDT_ERR_...
  489. */
  490. int fdtdec_get_chosen_node(const void *blob, const char *name);
  491. /*
  492. * Get the name for a compatible ID
  493. *
  494. * @param id Compatible ID to look for
  495. * @return compatible string for that id
  496. */
  497. const char *fdtdec_get_compatible(enum fdt_compat_id id);
  498. /* Look up a phandle and follow it to its node. Then return the offset
  499. * of that node.
  500. *
  501. * @param blob FDT blob
  502. * @param node node to examine
  503. * @param prop_name name of property to find
  504. * @return node offset if found, -ve error code on error
  505. */
  506. int fdtdec_lookup_phandle(const void *blob, int node, const char *prop_name);
  507. /**
  508. * Look up a property in a node and return its contents in an integer
  509. * array of given length. The property must have at least enough data for
  510. * the array (4*count bytes). It may have more, but this will be ignored.
  511. *
  512. * @param blob FDT blob
  513. * @param node node to examine
  514. * @param prop_name name of property to find
  515. * @param array array to fill with data
  516. * @param count number of array elements
  517. * @return 0 if ok, or -FDT_ERR_NOTFOUND if the property is not found,
  518. * or -FDT_ERR_BADLAYOUT if not enough data
  519. */
  520. int fdtdec_get_int_array(const void *blob, int node, const char *prop_name,
  521. u32 *array, int count);
  522. /**
  523. * Look up a property in a node and return its contents in an integer
  524. * array of given length. The property must exist but may have less data that
  525. * expected (4*count bytes). It may have more, but this will be ignored.
  526. *
  527. * @param blob FDT blob
  528. * @param node node to examine
  529. * @param prop_name name of property to find
  530. * @param array array to fill with data
  531. * @param count number of array elements
  532. * @return number of array elements if ok, or -FDT_ERR_NOTFOUND if the
  533. * property is not found
  534. */
  535. int fdtdec_get_int_array_count(const void *blob, int node,
  536. const char *prop_name, u32 *array, int count);
  537. /**
  538. * Look up a property in a node and return a pointer to its contents as a
  539. * unsigned int array of given length. The property must have at least enough
  540. * data for the array ('count' cells). It may have more, but this will be
  541. * ignored. The data is not copied.
  542. *
  543. * Note that you must access elements of the array with fdt32_to_cpu(),
  544. * since the elements will be big endian even on a little endian machine.
  545. *
  546. * @param blob FDT blob
  547. * @param node node to examine
  548. * @param prop_name name of property to find
  549. * @param count number of array elements
  550. * @return pointer to array if found, or NULL if the property is not
  551. * found or there is not enough data
  552. */
  553. const u32 *fdtdec_locate_array(const void *blob, int node,
  554. const char *prop_name, int count);
  555. /**
  556. * Look up a boolean property in a node and return it.
  557. *
  558. * A boolean properly is true if present in the device tree and false if not
  559. * present, regardless of its value.
  560. *
  561. * @param blob FDT blob
  562. * @param node node to examine
  563. * @param prop_name name of property to find
  564. * @return 1 if the properly is present; 0 if it isn't present
  565. */
  566. int fdtdec_get_bool(const void *blob, int node, const char *prop_name);
  567. /**
  568. * Look in the FDT for a config item with the given name and return its value
  569. * as a 32-bit integer. The property must have at least 4 bytes of data. The
  570. * value of the first cell is returned.
  571. *
  572. * @param blob FDT blob to use
  573. * @param prop_name Node property name
  574. * @param default_val default value to return if the property is not found
  575. * @return integer value, if found, or default_val if not
  576. */
  577. int fdtdec_get_config_int(const void *blob, const char *prop_name,
  578. int default_val);
  579. /**
  580. * Look in the FDT for a config item with the given name
  581. * and return whether it exists.
  582. *
  583. * @param blob FDT blob
  584. * @param prop_name property name to look up
  585. * @return 1, if it exists, or 0 if not
  586. */
  587. int fdtdec_get_config_bool(const void *blob, const char *prop_name);
  588. /**
  589. * Look in the FDT for a config item with the given name and return its value
  590. * as a string.
  591. *
  592. * @param blob FDT blob
  593. * @param prop_name property name to look up
  594. * @returns property string, NULL on error.
  595. */
  596. char *fdtdec_get_config_string(const void *blob, const char *prop_name);
  597. /*
  598. * Look up a property in a node and return its contents in a byte
  599. * array of given length. The property must have at least enough data for
  600. * the array (count bytes). It may have more, but this will be ignored.
  601. *
  602. * @param blob FDT blob
  603. * @param node node to examine
  604. * @param prop_name name of property to find
  605. * @param array array to fill with data
  606. * @param count number of array elements
  607. * @return 0 if ok, or -FDT_ERR_MISSING if the property is not found,
  608. * or -FDT_ERR_BADLAYOUT if not enough data
  609. */
  610. int fdtdec_get_byte_array(const void *blob, int node, const char *prop_name,
  611. u8 *array, int count);
  612. /**
  613. * Look up a property in a node and return a pointer to its contents as a
  614. * byte array of given length. The property must have at least enough data
  615. * for the array (count bytes). It may have more, but this will be ignored.
  616. * The data is not copied.
  617. *
  618. * @param blob FDT blob
  619. * @param node node to examine
  620. * @param prop_name name of property to find
  621. * @param count number of array elements
  622. * @return pointer to byte array if found, or NULL if the property is not
  623. * found or there is not enough data
  624. */
  625. const u8 *fdtdec_locate_byte_array(const void *blob, int node,
  626. const char *prop_name, int count);
  627. /**
  628. * Look up a property in a node which contains a memory region address and
  629. * size. Then return a pointer to this address.
  630. *
  631. * The property must hold one address with a length. This is only tested on
  632. * 32-bit machines.
  633. *
  634. * @param blob FDT blob
  635. * @param node node to examine
  636. * @param prop_name name of property to find
  637. * @param basep Returns base address of region
  638. * @param size Returns size of region
  639. * @return 0 if ok, -1 on error (property not found)
  640. */
  641. int fdtdec_decode_region(const void *blob, int node, const char *prop_name,
  642. fdt_addr_t *basep, fdt_size_t *sizep);
  643. enum fmap_compress_t {
  644. FMAP_COMPRESS_NONE,
  645. FMAP_COMPRESS_LZO,
  646. };
  647. enum fmap_hash_t {
  648. FMAP_HASH_NONE,
  649. FMAP_HASH_SHA1,
  650. FMAP_HASH_SHA256,
  651. };
  652. /* A flash map entry, containing an offset and length */
  653. struct fmap_entry {
  654. uint32_t offset;
  655. uint32_t length;
  656. uint32_t used; /* Number of bytes used in region */
  657. enum fmap_compress_t compress_algo; /* Compression type */
  658. enum fmap_hash_t hash_algo; /* Hash algorithm */
  659. const uint8_t *hash; /* Hash value */
  660. int hash_size; /* Hash size */
  661. };
  662. /**
  663. * Read a flash entry from the fdt
  664. *
  665. * @param blob FDT blob
  666. * @param node Offset of node to read
  667. * @param name Name of node being read
  668. * @param entry Place to put offset and size of this node
  669. * @return 0 if ok, -ve on error
  670. */
  671. int fdtdec_read_fmap_entry(const void *blob, int node, const char *name,
  672. struct fmap_entry *entry);
  673. /**
  674. * Obtain an indexed resource from a device property.
  675. *
  676. * @param fdt FDT blob
  677. * @param node node to examine
  678. * @param property name of the property to parse
  679. * @param index index of the resource to retrieve
  680. * @param res returns the resource
  681. * @return 0 if ok, negative on error
  682. */
  683. int fdt_get_resource(const void *fdt, int node, const char *property,
  684. unsigned int index, struct fdt_resource *res);
  685. /**
  686. * Obtain a named resource from a device property.
  687. *
  688. * Look up the index of the name in a list of strings and return the resource
  689. * at that index.
  690. *
  691. * @param fdt FDT blob
  692. * @param node node to examine
  693. * @param property name of the property to parse
  694. * @param prop_names name of the property containing the list of names
  695. * @param name the name of the entry to look up
  696. * @param res returns the resource
  697. */
  698. int fdt_get_named_resource(const void *fdt, int node, const char *property,
  699. const char *prop_names, const char *name,
  700. struct fdt_resource *res);
  701. /**
  702. * Decode a named region within a memory bank of a given type.
  703. *
  704. * This function handles selection of a memory region. The region is
  705. * specified as an offset/size within a particular type of memory.
  706. *
  707. * The properties used are:
  708. *
  709. * <mem_type>-memory<suffix> for the name of the memory bank
  710. * <mem_type>-offset<suffix> for the offset in that bank
  711. *
  712. * The property value must have an offset and a size. The function checks
  713. * that the region is entirely within the memory bank.5
  714. *
  715. * @param blob FDT blob
  716. * @param node Node containing the properties (-1 for /config)
  717. * @param mem_type Type of memory to use, which is a name, such as
  718. * "u-boot" or "kernel".
  719. * @param suffix String to append to the memory/offset
  720. * property names
  721. * @param basep Returns base of region
  722. * @param sizep Returns size of region
  723. * @return 0 if OK, -ive on error
  724. */
  725. int fdtdec_decode_memory_region(const void *blob, int node,
  726. const char *mem_type, const char *suffix,
  727. fdt_addr_t *basep, fdt_size_t *sizep);
  728. #endif