fdtdec.h 36 KB

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