fdtdec.h 35 KB

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