control.rst 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  1. .. SPDX-License-Identifier: GPL-2.0+
  2. .. sectionauthor:: Copyright 2011 The Chromium OS Authors
  3. Devicetree Control in U-Boot
  4. ============================
  5. This feature provides for run-time configuration of U-Boot via a flattened
  6. devicetree (fdt).
  7. This feature aims to make it possible for a single U-Boot binary to support
  8. multiple boards, with the exact configuration of each board controlled by
  9. a flattened devicetree (fdt). This is the approach taken by Linux kernel for
  10. ARM and RISC-V and has been used by PowerPC for some time.
  11. The fdt is a convenient vehicle for implementing run-time configuration
  12. for three reasons:
  13. - There is already excellent infrastructure for the fdt: a compiler checks
  14. the text file and converts it to a compact binary format, and a library
  15. is already available in U-Boot (libfdt) for handling this format
  16. - It is extensible since it consists of nodes and properties in a nice
  17. hierarchical format
  18. - It is fairly efficient to read incrementally
  19. The arch/<arch>/dts directories contains a Makefile for building the devicetree
  20. blob and embedding it in the U-Boot image. This is useful since it allows
  21. U-Boot to configure itself according to what it finds there. If you have
  22. a number of similar boards with different peripherals, you can describe
  23. the features of each board in the devicetree file, and have a single
  24. generic source base.
  25. To enable this feature, add CONFIG_OF_CONTROL to your board config file.
  26. What is a Flattened Devicetree?
  27. -------------------------------
  28. An fdt can be specified in source format as a text file. To read about
  29. the fdt syntax, take a look at the specification (dtspec_).
  30. There is also a mailing list (dtlist_) for the compiler and associated
  31. tools.
  32. In case you are wondering, OF stands for Open Firmware. This follows the
  33. convention used in Linux.
  34. Tools
  35. -----
  36. To create flattened device trees the device tree compiler is used. This is
  37. provided by U-Boot automatically. If you have a system version of dtc
  38. (typically in the 'device-tree-compiler' package), that system version is
  39. currently not used.
  40. If you want to build your own dtc, it is kept here::
  41. git://git.kernel.org/pub/scm/utils/dtc/dtc.git
  42. You can decode a binary file with::
  43. dtc -I dtb -O dts <filename.dtb>
  44. That repo also includes `fdtget`/`fdtput` for reading and writing properties in
  45. a binary file. U-Boot adds its own `fdtgrep` for creating subsets of the file.
  46. Where do I get a devicetree file for my board?
  47. ----------------------------------------------
  48. You may find that the Linux kernel has a suitable file. Look in the
  49. kernel source in arch/<arch>/boot/dts.
  50. If not you might find other boards with suitable files that you can
  51. modify to your needs. Look in the board directories for files with a
  52. .dts extension.
  53. Failing that, you could write one from scratch yourself!
  54. Configuration
  55. -------------
  56. Use::
  57. #define CONFIG_DEFAULT_DEVICE_TREE "<name>"
  58. to set the filename of the devicetree source. Then put your devicetree
  59. file into::
  60. arch/<arch>/dts/<name>.dts
  61. This should include your CPU or SOC's devicetree file, placed in
  62. `arch/<arch>/dts`, and then make any adjustments required using a u-boot-dtsi
  63. file for your board.
  64. If CONFIG_OF_EMBED is defined, then it will be picked up and built into
  65. the U-Boot image (including u-boot.bin). This is suitable for debugging
  66. and development only and is not recommended for production devices.
  67. If CONFIG_OF_SEPARATE is defined, then it will be built and placed in
  68. a u-boot.dtb file alongside u-boot-nodtb.bin with the combined result placed
  69. in u-boot.bin so you can still just flash u-boot,bin onto your board. If you are
  70. using CONFIG_SPL_FRAMEWORK, then u-boot.img will be built to include the device
  71. tree binary.
  72. If CONFIG_OF_BOARD is defined, a board-specific routine will provide the
  73. devicetree at runtime, for example if an earlier bootloader stage creates
  74. it and passes it to U-Boot.
  75. If CONFIG_OF_HOSTFILE is defined, then it will be read from a file on
  76. startup. This is only useful for sandbox. Use the -d flag to U-Boot to
  77. specify the file to read, -D for the default and -T for the test devicetree,
  78. used to run sandbox unit tests.
  79. You cannot use more than one of these options at the same time.
  80. To use a devicetree file that you have compiled yourself, pass
  81. EXT_DTB=<filename> to 'make', as in::
  82. make EXT_DTB=boot/am335x-boneblack-pubkey.dtb
  83. Then U-Boot will copy that file to u-boot.dtb, put it in the .img file
  84. if used, and u-boot-dtb.bin.
  85. If you wish to put the fdt at a different address in memory, you can
  86. define the "fdtcontroladdr" environment variable. This is the hex
  87. address of the fdt binary blob, and will override either of the options.
  88. Be aware that this environment variable is checked prior to relocation,
  89. when only the compiled-in environment is available. Therefore it is not
  90. possible to define this variable in the saved SPI/NAND flash
  91. environment, for example (it will be ignored). After relocation, this
  92. variable will be set to the address of the newly relocated fdt blob.
  93. It is read-only and cannot be changed. It can optionally be used to
  94. control the boot process of Linux with bootm/bootz commands.
  95. To use this, put something like this in your board header file::
  96. #define CONFIG_EXTRA_ENV_SETTINGS "fdtcontroladdr=10000\0"
  97. Build:
  98. After the board configuration is done, fdt supported u-boot can be built in two
  99. ways:
  100. # build the default dts which is defined from CONFIG_DEFAULT_DEVICE_TREE::
  101. $ make
  102. # build the user specified dts file::
  103. $ make DEVICE_TREE=<dts-file-name>
  104. .. _dttweaks:
  105. Adding tweaks for U-Boot
  106. ------------------------
  107. It is strongly recommended that devicetree files in U-Boot are an exact copy of
  108. those in Linux, so that it is easy to sync them up from time to time.
  109. U-Boot is of course a very different project from Linux, e.g. it operates under
  110. much more restrictive memory and code-size constraints. Where Linux may use a
  111. full clock driver with Common Clock Format (CCF) to find the input clock to the
  112. UART, U-Boot typically wants to output a banner as early as possible before too
  113. much code has run.
  114. A second difference is that U-Boot includes different phases. For SPL,
  115. constraints are even more extreme and the devicetree is shrunk to remove
  116. unwanted nodes, or even turned into C code to avoid access overhead.
  117. U-Boot automatically looks for and includes a file with updates to the standard
  118. devicetree for your board, searching for them in the same directory as the
  119. main file, in this order::
  120. <orig_filename>-u-boot.dtsi
  121. <CONFIG_SYS_SOC>-u-boot.dtsi
  122. <CONFIG_SYS_CPU>-u-boot.dtsi
  123. <CONFIG_SYS_VENDOR>-u-boot.dtsi
  124. u-boot.dtsi
  125. Only one of these is selected but of course you can #include another one within
  126. that file, to create a hierarchy of shared files.
  127. Relocation, SPL and TPL
  128. -----------------------
  129. U-Boot can be divided into three phases: TPL, SPL and U-Boot proper.
  130. The full devicetree is available to U-Boot proper, but normally only a subset
  131. (or none at all) is available to TPL and SPL. See 'Pre-Relocation Support' and
  132. 'SPL Support' in doc/driver-model/design.rst for more details.
  133. Using several DTBs in the SPL (CONFIG_SPL_MULTI_DTB)
  134. ----------------------------------------------------
  135. In some rare cases it is desirable to let SPL be able to select one DTB among
  136. many. This usually not very useful as the DTB for the SPL is small and usually
  137. fits several platforms. However the DTB sometimes include information that do
  138. work on several platforms (like IO tuning parameters).
  139. In this case it is possible to use CONFIG_SPL_MULTI_DTB. This option appends to
  140. the SPL a FIT image containing several DTBs listed in SPL_OF_LIST.
  141. board_fit_config_name_match() is called to select the right DTB.
  142. If board_fit_config_name_match() relies on DM (DM driver to access an EEPROM
  143. containing the board ID for example), it possible to start with a generic DTB
  144. and then switch over to the right DTB after the detection. For this purpose,
  145. the platform code must call fdtdec_resetup(). Based on the returned flag, the
  146. platform may have to re-initialise the DM subsystem using dm_uninit() and
  147. dm_init_and_scan().
  148. Limitations
  149. -----------
  150. Devicetrees can help reduce the complexity of supporting variants of boards
  151. which use the same SOC / CPU.
  152. However U-Boot is designed to build for a single architecture type and CPU
  153. type. So for example it is not possible to build a single ARM binary
  154. which runs on your AT91 and OMAP boards, relying on an fdt to configure
  155. the various features. This is because you must select one of
  156. the CPU families within arch/arm/cpu/arm926ejs (omap or at91) at build
  157. time. Similarly U-Boot cannot be built for multiple cpu types or
  158. architectures.
  159. It is important to understand that the fdt only selects options
  160. available in the platform / drivers. It cannot add new drivers (yet). So
  161. you must still have the CONFIG option to enable the driver. For example,
  162. you need to define CONFIG_SYS_NS16550 to bring in the NS16550 driver,
  163. but can use the fdt to specific the UART clock, peripheral address, etc.
  164. In very broad terms, the CONFIG options in general control *what* driver
  165. files are pulled in, and the fdt controls *how* those files work.
  166. History
  167. -------
  168. U-Boot configuration was previous done using CONFIG options in the board
  169. config file. This eventually got out of hand with nearly 10,000 options.
  170. U-Boot adopted devicetrees around the same time as Linux and early boards
  171. used it before Linux (e.g. snow). The two projects developed in parallel
  172. and there are still some differences in the bindings for certain boards.
  173. While there has been discussion of having a separate repository for devicetree
  174. files, in practice the Linux kernel Git repository has become the place where
  175. these are stored, with U-Boot taking copies and adding tweaks with u-boot.dtsi
  176. files.
  177. .. _dtspec: https://www.devicetree.org/specifications/
  178. .. _dtlist: https://www.spinics.net/lists/devicetree-compiler/