fdtdec.h 38 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038
  1. /* SPDX-License-Identifier: GPL-2.0+ */
  2. /*
  3. * Copyright (c) 2011 The Chromium OS Authors.
  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 <linux/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. typedef phys_addr_t fdt_addr_t;
  21. typedef phys_size_t fdt_size_t;
  22. #ifdef CONFIG_PHYS_64BIT
  23. #define FDT_ADDR_T_NONE (-1U)
  24. #define fdt_addr_to_cpu(reg) be64_to_cpu(reg)
  25. #define fdt_size_to_cpu(reg) be64_to_cpu(reg)
  26. typedef fdt64_t fdt_val_t;
  27. #else
  28. #define FDT_ADDR_T_NONE (-1U)
  29. #define fdt_addr_to_cpu(reg) be32_to_cpu(reg)
  30. #define fdt_size_to_cpu(reg) be32_to_cpu(reg)
  31. typedef fdt32_t fdt_val_t;
  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. struct bd_info;
  39. #ifdef CONFIG_SPL_BUILD
  40. #define SPL_BUILD 1
  41. #else
  42. #define SPL_BUILD 0
  43. #endif
  44. #if CONFIG_IS_ENABLED(OF_PRIOR_STAGE)
  45. extern phys_addr_t prior_stage_fdt_address;
  46. #endif
  47. /*
  48. * Information about a resource. start is the first address of the resource
  49. * and end is the last address (inclusive). The length of the resource will
  50. * be equal to: end - start + 1.
  51. */
  52. struct fdt_resource {
  53. fdt_addr_t start;
  54. fdt_addr_t end;
  55. };
  56. enum fdt_pci_space {
  57. FDT_PCI_SPACE_CONFIG = 0,
  58. FDT_PCI_SPACE_IO = 0x01000000,
  59. FDT_PCI_SPACE_MEM32 = 0x02000000,
  60. FDT_PCI_SPACE_MEM64 = 0x03000000,
  61. FDT_PCI_SPACE_MEM32_PREF = 0x42000000,
  62. FDT_PCI_SPACE_MEM64_PREF = 0x43000000,
  63. };
  64. #define FDT_PCI_ADDR_CELLS 3
  65. #define FDT_PCI_SIZE_CELLS 2
  66. #define FDT_PCI_REG_SIZE \
  67. ((FDT_PCI_ADDR_CELLS + FDT_PCI_SIZE_CELLS) * sizeof(u32))
  68. /*
  69. * The Open Firmware spec defines PCI physical address as follows:
  70. *
  71. * bits# 31 .... 24 23 .... 16 15 .... 08 07 .... 00
  72. *
  73. * phys.hi cell: npt000ss bbbbbbbb dddddfff rrrrrrrr
  74. * phys.mid cell: hhhhhhhh hhhhhhhh hhhhhhhh hhhhhhhh
  75. * phys.lo cell: llllllll llllllll llllllll llllllll
  76. *
  77. * where:
  78. *
  79. * n: is 0 if the address is relocatable, 1 otherwise
  80. * p: is 1 if addressable region is prefetchable, 0 otherwise
  81. * t: is 1 if the address is aliased (for non-relocatable I/O) below 1MB
  82. * (for Memory), or below 64KB (for relocatable I/O)
  83. * ss: is the space code, denoting the address space
  84. * bbbbbbbb: is the 8-bit Bus Number
  85. * ddddd: is the 5-bit Device Number
  86. * fff: is the 3-bit Function Number
  87. * rrrrrrrr: is the 8-bit Register Number
  88. * hhhhhhhh: is a 32-bit unsigned number
  89. * llllllll: is a 32-bit unsigned number
  90. */
  91. struct fdt_pci_addr {
  92. u32 phys_hi;
  93. u32 phys_mid;
  94. u32 phys_lo;
  95. };
  96. /**
  97. * Compute the size of a resource.
  98. *
  99. * @param res the resource to operate on
  100. * @return the size of the resource
  101. */
  102. static inline fdt_size_t fdt_resource_size(const struct fdt_resource *res)
  103. {
  104. return res->end - res->start + 1;
  105. }
  106. /**
  107. * Compat types that we know about and for which we might have drivers.
  108. * Each is named COMPAT_<dir>_<filename> where <dir> is the directory
  109. * within drivers.
  110. */
  111. enum fdt_compat_id {
  112. COMPAT_UNKNOWN,
  113. COMPAT_NVIDIA_TEGRA20_EMC, /* Tegra20 memory controller */
  114. COMPAT_NVIDIA_TEGRA20_EMC_TABLE, /* Tegra20 memory timing table */
  115. COMPAT_NVIDIA_TEGRA20_NAND, /* Tegra2 NAND controller */
  116. COMPAT_NVIDIA_TEGRA124_XUSB_PADCTL,
  117. /* Tegra124 XUSB pad controller */
  118. COMPAT_NVIDIA_TEGRA210_XUSB_PADCTL,
  119. /* Tegra210 XUSB pad controller */
  120. COMPAT_SMSC_LAN9215, /* SMSC 10/100 Ethernet LAN9215 */
  121. COMPAT_SAMSUNG_EXYNOS5_SROMC, /* Exynos5 SROMC */
  122. COMPAT_SAMSUNG_S3C2440_I2C, /* Exynos I2C Controller */
  123. COMPAT_SAMSUNG_EXYNOS5_SOUND, /* Exynos Sound */
  124. COMPAT_WOLFSON_WM8994_CODEC, /* Wolfson WM8994 Sound Codec */
  125. COMPAT_SAMSUNG_EXYNOS_USB_PHY, /* Exynos phy controller for usb2.0 */
  126. COMPAT_SAMSUNG_EXYNOS5_USB3_PHY,/* Exynos phy controller for usb3.0 */
  127. COMPAT_SAMSUNG_EXYNOS_TMU, /* Exynos TMU */
  128. COMPAT_SAMSUNG_EXYNOS_MIPI_DSI, /* Exynos mipi dsi */
  129. COMPAT_SAMSUNG_EXYNOS_DWMMC, /* Exynos DWMMC controller */
  130. COMPAT_SAMSUNG_EXYNOS_MMC, /* Exynos MMC controller */
  131. COMPAT_GENERIC_SPI_FLASH, /* Generic SPI Flash chip */
  132. COMPAT_MAXIM_98095_CODEC, /* MAX98095 Codec */
  133. COMPAT_SAMSUNG_EXYNOS5_I2C, /* Exynos5 High Speed I2C Controller */
  134. COMPAT_SAMSUNG_EXYNOS_SYSMMU, /* Exynos sysmmu */
  135. COMPAT_INTEL_MICROCODE, /* Intel microcode update */
  136. COMPAT_AMS_AS3722, /* AMS AS3722 PMIC */
  137. COMPAT_INTEL_QRK_MRC, /* Intel Quark MRC */
  138. COMPAT_ALTERA_SOCFPGA_DWMAC, /* SoCFPGA Ethernet controller */
  139. COMPAT_ALTERA_SOCFPGA_DWMMC, /* SoCFPGA DWMMC controller */
  140. COMPAT_ALTERA_SOCFPGA_DWC2USB, /* SoCFPGA DWC2 USB controller */
  141. COMPAT_INTEL_BAYTRAIL_FSP, /* Intel Bay Trail FSP */
  142. COMPAT_INTEL_BAYTRAIL_FSP_MDP, /* Intel FSP memory-down params */
  143. COMPAT_INTEL_IVYBRIDGE_FSP, /* Intel Ivy Bridge FSP */
  144. COMPAT_SUNXI_NAND, /* SUNXI NAND controller */
  145. COMPAT_ALTERA_SOCFPGA_CLK, /* SoCFPGA Clock initialization */
  146. COMPAT_ALTERA_SOCFPGA_PINCTRL_SINGLE, /* SoCFPGA pinctrl-single */
  147. COMPAT_ALTERA_SOCFPGA_H2F_BRG, /* SoCFPGA hps2fpga bridge */
  148. COMPAT_ALTERA_SOCFPGA_LWH2F_BRG, /* SoCFPGA lwhps2fpga bridge */
  149. COMPAT_ALTERA_SOCFPGA_F2H_BRG, /* SoCFPGA fpga2hps bridge */
  150. COMPAT_ALTERA_SOCFPGA_F2SDR0, /* SoCFPGA fpga2SDRAM0 bridge */
  151. COMPAT_ALTERA_SOCFPGA_F2SDR1, /* SoCFPGA fpga2SDRAM1 bridge */
  152. COMPAT_ALTERA_SOCFPGA_F2SDR2, /* SoCFPGA fpga2SDRAM2 bridge */
  153. COMPAT_ALTERA_SOCFPGA_FPGA0, /* SOCFPGA FPGA manager */
  154. COMPAT_ALTERA_SOCFPGA_NOC, /* SOCFPGA Arria 10 NOC */
  155. COMPAT_ALTERA_SOCFPGA_CLK_INIT, /* SOCFPGA Arria 10 clk init */
  156. COMPAT_COUNT,
  157. };
  158. #define MAX_PHANDLE_ARGS 16
  159. struct fdtdec_phandle_args {
  160. int node;
  161. int args_count;
  162. uint32_t args[MAX_PHANDLE_ARGS];
  163. };
  164. /**
  165. * fdtdec_parse_phandle_with_args() - Find a node pointed by phandle in a list
  166. *
  167. * This function is useful to parse lists of phandles and their arguments.
  168. *
  169. * Example:
  170. *
  171. * phandle1: node1 {
  172. * #list-cells = <2>;
  173. * }
  174. *
  175. * phandle2: node2 {
  176. * #list-cells = <1>;
  177. * }
  178. *
  179. * node3 {
  180. * list = <&phandle1 1 2 &phandle2 3>;
  181. * }
  182. *
  183. * To get a device_node of the `node2' node you may call this:
  184. * fdtdec_parse_phandle_with_args(blob, node3, "list", "#list-cells", 0, 1,
  185. * &args);
  186. *
  187. * (This function is a modified version of __of_parse_phandle_with_args() from
  188. * Linux 3.18)
  189. *
  190. * @blob: Pointer to device tree
  191. * @src_node: Offset of device tree node containing a list
  192. * @list_name: property name that contains a list
  193. * @cells_name: property name that specifies the phandles' arguments count,
  194. * or NULL to use @cells_count
  195. * @cells_count: Cell count to use if @cells_name is NULL
  196. * @index: index of a phandle to parse out
  197. * @out_args: optional pointer to output arguments structure (will be filled)
  198. * @return 0 on success (with @out_args filled out if not NULL), -ENOENT if
  199. * @list_name does not exist, a phandle was not found, @cells_name
  200. * could not be found, the arguments were truncated or there were too
  201. * many arguments.
  202. *
  203. */
  204. int fdtdec_parse_phandle_with_args(const void *blob, int src_node,
  205. const char *list_name,
  206. const char *cells_name,
  207. int cell_count, int index,
  208. struct fdtdec_phandle_args *out_args);
  209. /**
  210. * Find the next numbered alias for a peripheral. This is used to enumerate
  211. * all the peripherals of a certain type.
  212. *
  213. * Do the first call with *upto = 0. Assuming /aliases/<name>0 exists then
  214. * this function will return a pointer to the node the alias points to, and
  215. * then update *upto to 1. Next time you call this function, the next node
  216. * will be returned.
  217. *
  218. * All nodes returned will match the compatible ID, as it is assumed that
  219. * all peripherals use the same driver.
  220. *
  221. * @param blob FDT blob to use
  222. * @param name Root name of alias to search for
  223. * @param id Compatible ID to look for
  224. * @return offset of next compatible node, or -FDT_ERR_NOTFOUND if no more
  225. */
  226. int fdtdec_next_alias(const void *blob, const char *name,
  227. enum fdt_compat_id id, int *upto);
  228. /**
  229. * Find the compatible ID for a given node.
  230. *
  231. * Generally each node has at least one compatible string attached to it.
  232. * This function looks through our list of known compatible strings and
  233. * returns the corresponding ID which matches the compatible string.
  234. *
  235. * @param blob FDT blob to use
  236. * @param node Node containing compatible string to find
  237. * @return compatible ID, or COMPAT_UNKNOWN if we cannot find a match
  238. */
  239. enum fdt_compat_id fdtdec_lookup(const void *blob, int node);
  240. /**
  241. * Find the next compatible node for a peripheral.
  242. *
  243. * Do the first call with node = 0. This function will return a pointer to
  244. * the next compatible node. Next time you call this function, pass the
  245. * value returned, and the next node will be provided.
  246. *
  247. * @param blob FDT blob to use
  248. * @param node Start node for search
  249. * @param id Compatible ID to look for (enum fdt_compat_id)
  250. * @return offset of next compatible node, or -FDT_ERR_NOTFOUND if no more
  251. */
  252. int fdtdec_next_compatible(const void *blob, int node,
  253. enum fdt_compat_id id);
  254. /**
  255. * Find the next compatible subnode for a peripheral.
  256. *
  257. * Do the first call with node set to the parent and depth = 0. This
  258. * function will return the offset of the next compatible node. Next time
  259. * you call this function, pass the node value returned last time, with
  260. * depth unchanged, and the next node will be provided.
  261. *
  262. * @param blob FDT blob to use
  263. * @param node Start node for search
  264. * @param id Compatible ID to look for (enum fdt_compat_id)
  265. * @param depthp Current depth (set to 0 before first call)
  266. * @return offset of next compatible node, or -FDT_ERR_NOTFOUND if no more
  267. */
  268. int fdtdec_next_compatible_subnode(const void *blob, int node,
  269. enum fdt_compat_id id, int *depthp);
  270. /*
  271. * Look up an address property in a node and return the parsed address, and
  272. * optionally the parsed size.
  273. *
  274. * This variant assumes a known and fixed number of cells are used to
  275. * represent the address and size.
  276. *
  277. * You probably don't want to use this function directly except to parse
  278. * non-standard properties, and never to parse the "reg" property. Instead,
  279. * use one of the "auto" variants below, which automatically honor the
  280. * #address-cells and #size-cells properties in the parent node.
  281. *
  282. * @param blob FDT blob
  283. * @param node node to examine
  284. * @param prop_name name of property to find
  285. * @param index which address to retrieve from a list of addresses. Often 0.
  286. * @param na the number of cells used to represent an address
  287. * @param ns the number of cells used to represent a size
  288. * @param sizep a pointer to store the size into. Use NULL if not required
  289. * @param translate Indicates whether to translate the returned value
  290. * using the parent node's ranges property.
  291. * @return address, if found, or FDT_ADDR_T_NONE if not
  292. */
  293. fdt_addr_t fdtdec_get_addr_size_fixed(const void *blob, int node,
  294. const char *prop_name, int index, int na, int ns,
  295. fdt_size_t *sizep, bool translate);
  296. /*
  297. * Look up an address property in a node and return the parsed address, and
  298. * optionally the parsed size.
  299. *
  300. * This variant automatically determines the number of cells used to represent
  301. * the address and size by parsing the provided parent node's #address-cells
  302. * and #size-cells properties.
  303. *
  304. * @param blob FDT blob
  305. * @param parent parent node of @node
  306. * @param node node to examine
  307. * @param prop_name name of property to find
  308. * @param index which address to retrieve from a list of addresses. Often 0.
  309. * @param sizep a pointer to store the size into. Use NULL if not required
  310. * @param translate Indicates whether to translate the returned value
  311. * using the parent node's ranges property.
  312. * @return address, if found, or FDT_ADDR_T_NONE if not
  313. */
  314. fdt_addr_t fdtdec_get_addr_size_auto_parent(const void *blob, int parent,
  315. int node, const char *prop_name, int index, fdt_size_t *sizep,
  316. bool translate);
  317. /*
  318. * Look up an address property in a node and return the parsed address, and
  319. * optionally the parsed size.
  320. *
  321. * This variant automatically determines the number of cells used to represent
  322. * the address and size by parsing the parent node's #address-cells
  323. * and #size-cells properties. The parent node is automatically found.
  324. *
  325. * The automatic parent lookup implemented by this function is slow.
  326. * Consequently, fdtdec_get_addr_size_auto_parent() should be used where
  327. * possible.
  328. *
  329. * @param blob FDT blob
  330. * @param parent parent node of @node
  331. * @param node node to examine
  332. * @param prop_name name of property to find
  333. * @param index which address to retrieve from a list of addresses. Often 0.
  334. * @param sizep a pointer to store the size into. Use NULL if not required
  335. * @param translate Indicates whether to translate the returned value
  336. * using the parent node's ranges property.
  337. * @return address, if found, or FDT_ADDR_T_NONE if not
  338. */
  339. fdt_addr_t fdtdec_get_addr_size_auto_noparent(const void *blob, int node,
  340. const char *prop_name, int index, fdt_size_t *sizep,
  341. bool translate);
  342. /*
  343. * Look up an address property in a node and return the parsed address.
  344. *
  345. * This variant hard-codes the number of cells used to represent the address
  346. * and size based on sizeof(fdt_addr_t) and sizeof(fdt_size_t). It also
  347. * always returns the first address value in the property (index 0).
  348. *
  349. * Use of this function is not recommended due to the hard-coding of cell
  350. * counts. There is no programmatic validation that these hard-coded values
  351. * actually match the device tree content in any way at all. This assumption
  352. * can be satisfied by manually ensuring CONFIG_PHYS_64BIT is appropriately
  353. * set in the U-Boot build and exercising strict control over DT content to
  354. * ensure use of matching #address-cells/#size-cells properties. However, this
  355. * approach is error-prone; those familiar with DT will not expect the
  356. * assumption to exist, and could easily invalidate it. If the assumption is
  357. * invalidated, this function will not report the issue, and debugging will
  358. * be required. Instead, use fdtdec_get_addr_size_auto_parent().
  359. *
  360. * @param blob FDT blob
  361. * @param node node to examine
  362. * @param prop_name name of property to find
  363. * @return address, if found, or FDT_ADDR_T_NONE if not
  364. */
  365. fdt_addr_t fdtdec_get_addr(const void *blob, int node,
  366. const char *prop_name);
  367. /*
  368. * Look up an address property in a node and return the parsed address, and
  369. * optionally the parsed size.
  370. *
  371. * This variant hard-codes the number of cells used to represent the address
  372. * and size based on sizeof(fdt_addr_t) and sizeof(fdt_size_t). It also
  373. * always returns the first address value in the property (index 0).
  374. *
  375. * Use of this function is not recommended due to the hard-coding of cell
  376. * counts. There is no programmatic validation that these hard-coded values
  377. * actually match the device tree content in any way at all. This assumption
  378. * can be satisfied by manually ensuring CONFIG_PHYS_64BIT is appropriately
  379. * set in the U-Boot build and exercising strict control over DT content to
  380. * ensure use of matching #address-cells/#size-cells properties. However, this
  381. * approach is error-prone; those familiar with DT will not expect the
  382. * assumption to exist, and could easily invalidate it. If the assumption is
  383. * invalidated, this function will not report the issue, and debugging will
  384. * be required. Instead, use fdtdec_get_addr_size_auto_parent().
  385. *
  386. * @param blob FDT blob
  387. * @param node node to examine
  388. * @param prop_name name of property to find
  389. * @param sizep a pointer to store the size into. Use NULL if not required
  390. * @return address, if found, or FDT_ADDR_T_NONE if not
  391. */
  392. fdt_addr_t fdtdec_get_addr_size(const void *blob, int node,
  393. const char *prop_name, fdt_size_t *sizep);
  394. /**
  395. * Look at an address property in a node and return the pci address which
  396. * corresponds to the given type in the form of fdt_pci_addr.
  397. * The property must hold one fdt_pci_addr with a lengh.
  398. *
  399. * @param blob FDT blob
  400. * @param node node to examine
  401. * @param type pci address type (FDT_PCI_SPACE_xxx)
  402. * @param prop_name name of property to find
  403. * @param addr returns pci address in the form of fdt_pci_addr
  404. * @return 0 if ok, -ENOENT if the property did not exist, -EINVAL if the
  405. * format of the property was invalid, -ENXIO if the requested
  406. * address type was not found
  407. */
  408. int fdtdec_get_pci_addr(const void *blob, int node, enum fdt_pci_space type,
  409. const char *prop_name, struct fdt_pci_addr *addr);
  410. /**
  411. * Look at the compatible property of a device node that represents a PCI
  412. * device and extract pci vendor id and device id from it.
  413. *
  414. * @param blob FDT blob
  415. * @param node node to examine
  416. * @param vendor vendor id of the pci device
  417. * @param device device id of the pci device
  418. * @return 0 if ok, negative on error
  419. */
  420. int fdtdec_get_pci_vendev(const void *blob, int node,
  421. u16 *vendor, u16 *device);
  422. /**
  423. * Look at the pci address of a device node that represents a PCI device
  424. * and return base address of the pci device's registers.
  425. *
  426. * @param dev device to examine
  427. * @param addr pci address in the form of fdt_pci_addr
  428. * @param bar returns base address of the pci device's registers
  429. * @return 0 if ok, negative on error
  430. */
  431. int fdtdec_get_pci_bar32(struct udevice *dev, struct fdt_pci_addr *addr,
  432. u32 *bar);
  433. /**
  434. * Look up a 32-bit integer property in a node and return it. The property
  435. * must have at least 4 bytes of data. The value of the first cell is
  436. * returned.
  437. *
  438. * @param blob FDT blob
  439. * @param node node to examine
  440. * @param prop_name name of property to find
  441. * @param default_val default value to return if the property is not found
  442. * @return integer value, if found, or default_val if not
  443. */
  444. s32 fdtdec_get_int(const void *blob, int node, const char *prop_name,
  445. s32 default_val);
  446. /**
  447. * Unsigned version of fdtdec_get_int. The property must have at least
  448. * 4 bytes of data. The value of the first cell is returned.
  449. *
  450. * @param blob FDT blob
  451. * @param node node to examine
  452. * @param prop_name name of property to find
  453. * @param default_val default value to return if the property is not found
  454. * @return unsigned integer value, if found, or default_val if not
  455. */
  456. unsigned int fdtdec_get_uint(const void *blob, int node, const char *prop_name,
  457. unsigned int default_val);
  458. /**
  459. * Get a variable-sized number from a property
  460. *
  461. * This reads a number from one or more cells.
  462. *
  463. * @param ptr Pointer to property
  464. * @param cells Number of cells containing the number
  465. * @return the value in the cells
  466. */
  467. u64 fdtdec_get_number(const fdt32_t *ptr, unsigned int cells);
  468. /**
  469. * Look up a 64-bit integer property in a node and return it. The property
  470. * must have at least 8 bytes of data (2 cells). The first two cells are
  471. * concatenated to form a 8 bytes value, where the first cell is top half and
  472. * the second cell is bottom half.
  473. *
  474. * @param blob FDT blob
  475. * @param node node to examine
  476. * @param prop_name name of property to find
  477. * @param default_val default value to return if the property is not found
  478. * @return integer value, if found, or default_val if not
  479. */
  480. uint64_t fdtdec_get_uint64(const void *blob, int node, const char *prop_name,
  481. uint64_t default_val);
  482. /**
  483. * Checks whether a node is enabled.
  484. * This looks for a 'status' property. If this exists, then returns 1 if
  485. * the status is 'ok' and 0 otherwise. If there is no status property,
  486. * it returns 1 on the assumption that anything mentioned should be enabled
  487. * by default.
  488. *
  489. * @param blob FDT blob
  490. * @param node node to examine
  491. * @return integer value 0 (not enabled) or 1 (enabled)
  492. */
  493. int fdtdec_get_is_enabled(const void *blob, int node);
  494. /**
  495. * Make sure we have a valid fdt available to control U-Boot.
  496. *
  497. * If not, a message is printed to the console if the console is ready.
  498. *
  499. * @return 0 if all ok, -1 if not
  500. */
  501. int fdtdec_prepare_fdt(void);
  502. /**
  503. * Checks that we have a valid fdt available to control U-Boot.
  504. * However, if not then for the moment nothing is done, since this function
  505. * is called too early to panic().
  506. *
  507. * @returns 0
  508. */
  509. int fdtdec_check_fdt(void);
  510. /**
  511. * Find the nodes for a peripheral and return a list of them in the correct
  512. * order. This is used to enumerate all the peripherals of a certain type.
  513. *
  514. * To use this, optionally set up a /aliases node with alias properties for
  515. * a peripheral. For example, for usb you could have:
  516. *
  517. * aliases {
  518. * usb0 = "/ehci@c5008000";
  519. * usb1 = "/ehci@c5000000";
  520. * };
  521. *
  522. * Pass "usb" as the name to this function and will return a list of two
  523. * nodes offsets: /ehci@c5008000 and ehci@c5000000.
  524. *
  525. * All nodes returned will match the compatible ID, as it is assumed that
  526. * all peripherals use the same driver.
  527. *
  528. * If no alias node is found, then the node list will be returned in the
  529. * order found in the fdt. If the aliases mention a node which doesn't
  530. * exist, then this will be ignored. If nodes are found with no aliases,
  531. * they will be added in any order.
  532. *
  533. * If there is a gap in the aliases, then this function return a 0 node at
  534. * that position. The return value will also count these gaps.
  535. *
  536. * This function checks node properties and will not return nodes which are
  537. * marked disabled (status = "disabled").
  538. *
  539. * @param blob FDT blob to use
  540. * @param name Root name of alias to search for
  541. * @param id Compatible ID to look for
  542. * @param node_list Place to put list of found nodes
  543. * @param maxcount Maximum number of nodes to find
  544. * @return number of nodes found on success, FDT_ERR_... on error
  545. */
  546. int fdtdec_find_aliases_for_id(const void *blob, const char *name,
  547. enum fdt_compat_id id, int *node_list, int maxcount);
  548. /*
  549. * This function is similar to fdtdec_find_aliases_for_id() except that it
  550. * adds to the node_list that is passed in. Any 0 elements are considered
  551. * available for allocation - others are considered already used and are
  552. * skipped.
  553. *
  554. * You can use this by calling fdtdec_find_aliases_for_id() with an
  555. * uninitialised array, then setting the elements that are returned to -1,
  556. * say, then calling this function, perhaps with a different compat id.
  557. * Any elements you get back that are >0 are new nodes added by the call
  558. * to this function.
  559. *
  560. * Note that if you have some nodes with aliases and some without, you are
  561. * sailing close to the wind. The call to fdtdec_find_aliases_for_id() with
  562. * one compat_id may fill in positions for which you have aliases defined
  563. * for another compat_id. When you later call *this* function with the second
  564. * compat_id, the alias positions may already be used. A debug warning may
  565. * be generated in this case, but it is safest to define aliases for all
  566. * nodes when you care about the ordering.
  567. */
  568. int fdtdec_add_aliases_for_id(const void *blob, const char *name,
  569. enum fdt_compat_id id, int *node_list, int maxcount);
  570. /**
  571. * Get the alias sequence number of a node
  572. *
  573. * This works out whether a node is pointed to by an alias, and if so, the
  574. * sequence number of that alias. Aliases are of the form <base><num> where
  575. * <num> is the sequence number. For example spi2 would be sequence number
  576. * 2.
  577. *
  578. * @param blob Device tree blob (if NULL, then error is returned)
  579. * @param base Base name for alias (before the underscore)
  580. * @param node Node to look up
  581. * @param seqp This is set to the sequence number if one is found,
  582. * but otherwise the value is left alone
  583. * @return 0 if a sequence was found, -ve if not
  584. */
  585. int fdtdec_get_alias_seq(const void *blob, const char *base, int node,
  586. int *seqp);
  587. /**
  588. * Get a property from the /chosen node
  589. *
  590. * @param blob Device tree blob (if NULL, then NULL is returned)
  591. * @param name Property name to look up
  592. * @return Value of property, or NULL if it does not exist
  593. */
  594. const char *fdtdec_get_chosen_prop(const void *blob, const char *name);
  595. /**
  596. * Get the offset of the given /chosen node
  597. *
  598. * This looks up a property in /chosen containing the path to another node,
  599. * then finds the offset of that node.
  600. *
  601. * @param blob Device tree blob (if NULL, then error is returned)
  602. * @param name Property name, e.g. "stdout-path"
  603. * @return Node offset referred to by that chosen node, or -ve FDT_ERR_...
  604. */
  605. int fdtdec_get_chosen_node(const void *blob, const char *name);
  606. /*
  607. * Get the name for a compatible ID
  608. *
  609. * @param id Compatible ID to look for
  610. * @return compatible string for that id
  611. */
  612. const char *fdtdec_get_compatible(enum fdt_compat_id id);
  613. /* Look up a phandle and follow it to its node. Then return the offset
  614. * of that node.
  615. *
  616. * @param blob FDT blob
  617. * @param node node to examine
  618. * @param prop_name name of property to find
  619. * @return node offset if found, -ve error code on error
  620. */
  621. int fdtdec_lookup_phandle(const void *blob, int node, const char *prop_name);
  622. /**
  623. * Look up a property in a node and return its contents in an integer
  624. * array of given length. The property must have at least enough data for
  625. * the array (4*count bytes). It may have more, but this will be ignored.
  626. *
  627. * @param blob FDT blob
  628. * @param node node to examine
  629. * @param prop_name name of property to find
  630. * @param array array to fill with data
  631. * @param count number of array elements
  632. * @return 0 if ok, or -FDT_ERR_NOTFOUND if the property is not found,
  633. * or -FDT_ERR_BADLAYOUT if not enough data
  634. */
  635. int fdtdec_get_int_array(const void *blob, int node, const char *prop_name,
  636. u32 *array, int count);
  637. /**
  638. * Look up a property in a node and return its contents in an integer
  639. * array of given length. The property must exist but may have less data that
  640. * expected (4*count bytes). It may have more, but this will be ignored.
  641. *
  642. * @param blob FDT blob
  643. * @param node node to examine
  644. * @param prop_name name of property to find
  645. * @param array array to fill with data
  646. * @param count number of array elements
  647. * @return number of array elements if ok, or -FDT_ERR_NOTFOUND if the
  648. * property is not found
  649. */
  650. int fdtdec_get_int_array_count(const void *blob, int node,
  651. const char *prop_name, u32 *array, int count);
  652. /**
  653. * Look up a property in a node and return a pointer to its contents as a
  654. * unsigned int array of given length. The property must have at least enough
  655. * data for the array ('count' cells). It may have more, but this will be
  656. * ignored. The data is not copied.
  657. *
  658. * Note that you must access elements of the array with fdt32_to_cpu(),
  659. * since the elements will be big endian even on a little endian machine.
  660. *
  661. * @param blob FDT blob
  662. * @param node node to examine
  663. * @param prop_name name of property to find
  664. * @param count number of array elements
  665. * @return pointer to array if found, or NULL if the property is not
  666. * found or there is not enough data
  667. */
  668. const u32 *fdtdec_locate_array(const void *blob, int node,
  669. const char *prop_name, int count);
  670. /**
  671. * Look up a boolean property in a node and return it.
  672. *
  673. * A boolean properly is true if present in the device tree and false if not
  674. * present, regardless of its value.
  675. *
  676. * @param blob FDT blob
  677. * @param node node to examine
  678. * @param prop_name name of property to find
  679. * @return 1 if the properly is present; 0 if it isn't present
  680. */
  681. int fdtdec_get_bool(const void *blob, int node, const char *prop_name);
  682. /*
  683. * Count child nodes of one parent node.
  684. *
  685. * @param blob FDT blob
  686. * @param node parent node
  687. * @return number of child node; 0 if there is not child node
  688. */
  689. int fdtdec_get_child_count(const void *blob, int node);
  690. /**
  691. * Look in the FDT for a config item with the given name and return its value
  692. * as a 32-bit integer. The property must have at least 4 bytes of data. The
  693. * value of the first cell is returned.
  694. *
  695. * @param blob FDT blob to use
  696. * @param prop_name Node property name
  697. * @param default_val default value to return if the property is not found
  698. * @return integer value, if found, or default_val if not
  699. */
  700. int fdtdec_get_config_int(const void *blob, const char *prop_name,
  701. int default_val);
  702. /**
  703. * Look in the FDT for a config item with the given name
  704. * and return whether it exists.
  705. *
  706. * @param blob FDT blob
  707. * @param prop_name property name to look up
  708. * @return 1, if it exists, or 0 if not
  709. */
  710. int fdtdec_get_config_bool(const void *blob, const char *prop_name);
  711. /**
  712. * Look in the FDT for a config item with the given name and return its value
  713. * as a string.
  714. *
  715. * @param blob FDT blob
  716. * @param prop_name property name to look up
  717. * @returns property string, NULL on error.
  718. */
  719. char *fdtdec_get_config_string(const void *blob, const char *prop_name);
  720. /*
  721. * Look up a property in a node and return its contents in a byte
  722. * array of given length. The property must have at least enough data for
  723. * the array (count bytes). It may have more, but this will be ignored.
  724. *
  725. * @param blob FDT blob
  726. * @param node node to examine
  727. * @param prop_name name of property to find
  728. * @param array array to fill with data
  729. * @param count number of array elements
  730. * @return 0 if ok, or -FDT_ERR_MISSING if the property is not found,
  731. * or -FDT_ERR_BADLAYOUT if not enough data
  732. */
  733. int fdtdec_get_byte_array(const void *blob, int node, const char *prop_name,
  734. u8 *array, int count);
  735. /**
  736. * Look up a property in a node and return a pointer to its contents as a
  737. * byte array of given length. The property must have at least enough data
  738. * for the array (count bytes). It may have more, but this will be ignored.
  739. * The data is not copied.
  740. *
  741. * @param blob FDT blob
  742. * @param node node to examine
  743. * @param prop_name name of property to find
  744. * @param count number of array elements
  745. * @return pointer to byte array if found, or NULL if the property is not
  746. * found or there is not enough data
  747. */
  748. const u8 *fdtdec_locate_byte_array(const void *blob, int node,
  749. const char *prop_name, int count);
  750. /**
  751. * Look up a property in a node which contains a memory region address and
  752. * size. Then return a pointer to this address.
  753. *
  754. * The property must hold one address with a length. This is only tested on
  755. * 32-bit machines.
  756. *
  757. * @param blob FDT blob
  758. * @param node node to examine
  759. * @param prop_name name of property to find
  760. * @param basep Returns base address of region
  761. * @param size Returns size of region
  762. * @return 0 if ok, -1 on error (property not found)
  763. */
  764. int fdtdec_decode_region(const void *blob, int node, const char *prop_name,
  765. fdt_addr_t *basep, fdt_size_t *sizep);
  766. /**
  767. * Obtain an indexed resource from a device property.
  768. *
  769. * @param fdt FDT blob
  770. * @param node node to examine
  771. * @param property name of the property to parse
  772. * @param index index of the resource to retrieve
  773. * @param res returns the resource
  774. * @return 0 if ok, negative on error
  775. */
  776. int fdt_get_resource(const void *fdt, int node, const char *property,
  777. unsigned int index, struct fdt_resource *res);
  778. /**
  779. * Obtain a named resource from a device property.
  780. *
  781. * Look up the index of the name in a list of strings and return the resource
  782. * at that index.
  783. *
  784. * @param fdt FDT blob
  785. * @param node node to examine
  786. * @param property name of the property to parse
  787. * @param prop_names name of the property containing the list of names
  788. * @param name the name of the entry to look up
  789. * @param res returns the resource
  790. */
  791. int fdt_get_named_resource(const void *fdt, int node, const char *property,
  792. const char *prop_names, const char *name,
  793. struct fdt_resource *res);
  794. /**
  795. * Decode a named region within a memory bank of a given type.
  796. *
  797. * This function handles selection of a memory region. The region is
  798. * specified as an offset/size within a particular type of memory.
  799. *
  800. * The properties used are:
  801. *
  802. * <mem_type>-memory<suffix> for the name of the memory bank
  803. * <mem_type>-offset<suffix> for the offset in that bank
  804. *
  805. * The property value must have an offset and a size. The function checks
  806. * that the region is entirely within the memory bank.5
  807. *
  808. * @param blob FDT blob
  809. * @param node Node containing the properties (-1 for /config)
  810. * @param mem_type Type of memory to use, which is a name, such as
  811. * "u-boot" or "kernel".
  812. * @param suffix String to append to the memory/offset
  813. * property names
  814. * @param basep Returns base of region
  815. * @param sizep Returns size of region
  816. * @return 0 if OK, -ive on error
  817. */
  818. int fdtdec_decode_memory_region(const void *blob, int node,
  819. const char *mem_type, const char *suffix,
  820. fdt_addr_t *basep, fdt_size_t *sizep);
  821. /* Display timings from linux include/video/display_timing.h */
  822. enum display_flags {
  823. DISPLAY_FLAGS_HSYNC_LOW = 1 << 0,
  824. DISPLAY_FLAGS_HSYNC_HIGH = 1 << 1,
  825. DISPLAY_FLAGS_VSYNC_LOW = 1 << 2,
  826. DISPLAY_FLAGS_VSYNC_HIGH = 1 << 3,
  827. /* data enable flag */
  828. DISPLAY_FLAGS_DE_LOW = 1 << 4,
  829. DISPLAY_FLAGS_DE_HIGH = 1 << 5,
  830. /* drive data on pos. edge */
  831. DISPLAY_FLAGS_PIXDATA_POSEDGE = 1 << 6,
  832. /* drive data on neg. edge */
  833. DISPLAY_FLAGS_PIXDATA_NEGEDGE = 1 << 7,
  834. DISPLAY_FLAGS_INTERLACED = 1 << 8,
  835. DISPLAY_FLAGS_DOUBLESCAN = 1 << 9,
  836. DISPLAY_FLAGS_DOUBLECLK = 1 << 10,
  837. };
  838. /*
  839. * A single signal can be specified via a range of minimal and maximal values
  840. * with a typical value, that lies somewhere inbetween.
  841. */
  842. struct timing_entry {
  843. u32 min;
  844. u32 typ;
  845. u32 max;
  846. };
  847. /*
  848. * Single "mode" entry. This describes one set of signal timings a display can
  849. * have in one setting. This struct can later be converted to struct videomode
  850. * (see include/video/videomode.h). As each timing_entry can be defined as a
  851. * range, one struct display_timing may become multiple struct videomodes.
  852. *
  853. * Example: hsync active high, vsync active low
  854. *
  855. * Active Video
  856. * Video ______________________XXXXXXXXXXXXXXXXXXXXXX_____________________
  857. * |<- sync ->|<- back ->|<----- active ----->|<- front ->|<- sync..
  858. * | | porch | | porch |
  859. *
  860. * HSync _|¯¯¯¯¯¯¯¯¯¯|___________________________________________|¯¯¯¯¯¯¯¯¯
  861. *
  862. * VSync ¯|__________|¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯|_________
  863. */
  864. struct display_timing {
  865. struct timing_entry pixelclock;
  866. struct timing_entry hactive; /* hor. active video */
  867. struct timing_entry hfront_porch; /* hor. front porch */
  868. struct timing_entry hback_porch; /* hor. back porch */
  869. struct timing_entry hsync_len; /* hor. sync len */
  870. struct timing_entry vactive; /* ver. active video */
  871. struct timing_entry vfront_porch; /* ver. front porch */
  872. struct timing_entry vback_porch; /* ver. back porch */
  873. struct timing_entry vsync_len; /* ver. sync len */
  874. enum display_flags flags; /* display flags */
  875. bool hdmi_monitor; /* is hdmi monitor? */
  876. };
  877. /**
  878. * fdtdec_decode_display_timing() - decode display timings
  879. *
  880. * Decode display timings from the supplied 'display-timings' node.
  881. * See doc/device-tree-bindings/video/display-timing.txt for binding
  882. * information.
  883. *
  884. * @param blob FDT blob
  885. * @param node 'display-timing' node containing the timing subnodes
  886. * @param index Index number to read (0=first timing subnode)
  887. * @param config Place to put timings
  888. * @return 0 if OK, -FDT_ERR_NOTFOUND if not found
  889. */
  890. int fdtdec_decode_display_timing(const void *blob, int node, int index,
  891. struct display_timing *config);
  892. /**
  893. * fdtdec_setup_memory_size() - decode and setup gd->ram_size
  894. *
  895. * Decode the /memory 'reg' property to determine the size of the first memory
  896. * bank, populate the global data with the size of the first bank of memory.
  897. *
  898. * This function should be called from a boards dram_init(). This helper
  899. * function allows for boards to query the device tree for DRAM size instead of
  900. * hard coding the value in the case where the memory size cannot be detected
  901. * automatically.
  902. *
  903. * @return 0 if OK, -EINVAL if the /memory node or reg property is missing or
  904. * invalid
  905. */
  906. int fdtdec_setup_memory_size(void);
  907. /**
  908. * fdtdec_setup_memory_banksize() - decode and populate gd->bd->bi_dram
  909. *
  910. * Decode the /memory 'reg' property to determine the address and size of the
  911. * memory banks. Use this data to populate the global data board info with the
  912. * phys address and size of memory banks.
  913. *
  914. * This function should be called from a boards dram_init_banksize(). This
  915. * helper function allows for boards to query the device tree for memory bank
  916. * information instead of hard coding the information in cases where it cannot
  917. * be detected automatically.
  918. *
  919. * @return 0 if OK, -EINVAL if the /memory node or reg property is missing or
  920. * invalid
  921. */
  922. int fdtdec_setup_memory_banksize(void);
  923. /**
  924. * Set up the device tree ready for use
  925. */
  926. int fdtdec_setup(void);
  927. /**
  928. * Board-specific FDT initialization. Returns the address to a device tree blob.
  929. * Called when CONFIG_OF_BOARD is defined, or if CONFIG_OF_SEPARATE is defined
  930. * and the board implements it.
  931. */
  932. /*
  933. * Decode the size of memory
  934. *
  935. * RAM size is normally set in a /memory node and consists of a list of
  936. * (base, size) cells in the 'reg' property. This information is used to
  937. * determine the total available memory as well as the address and size
  938. * of each bank.
  939. *
  940. * Optionally the memory configuration can vary depending on a board id,
  941. * typically read from strapping resistors or an EEPROM on the board.
  942. *
  943. * Finally, memory size can be detected (within certain limits) by probing
  944. * the available memory. It is safe to do so within the limits provides by
  945. * the board's device tree information. This makes it possible to produce
  946. * boards with different memory sizes, where the device tree specifies the
  947. * maximum memory configuration, and the smaller memory configuration is
  948. * probed.
  949. *
  950. * This function decodes that information, returning the memory base address,
  951. * size and bank information. See the memory.txt binding for full
  952. * documentation.
  953. *
  954. * @param blob Device tree blob
  955. * @param area Name of node to check (NULL means "/memory")
  956. * @param board_id Board ID to look up
  957. * @param basep Returns base address of first memory bank (NULL to
  958. * ignore)
  959. * @param sizep Returns total memory size (NULL to ignore)
  960. * @param bd Updated with the memory bank information (NULL to skip)
  961. * @return 0 if OK, -ve on error
  962. */
  963. int fdtdec_decode_ram_size(const void *blob, const char *area, int board_id,
  964. phys_addr_t *basep, phys_size_t *sizep,
  965. struct bd_info *bd);
  966. #endif