fdtdec.h 36 KB

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