fdtdec.h 38 KB

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