Tom Rini | 10e4779 | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | # SPDX-License-Identifier: GPL-2.0+ |
Simon Glass | 38d6b8d | 2011-10-15 05:48:21 +0000 | [diff] [blame] | 2 | # |
| 3 | # Copyright (c) 2011 The Chromium OS Authors. |
Simon Glass | 38d6b8d | 2011-10-15 05:48:21 +0000 | [diff] [blame] | 4 | |
| 5 | Device Tree Control in U-Boot |
| 6 | ============================= |
| 7 | |
| 8 | This feature provides for run-time configuration of U-Boot via a flat |
| 9 | device tree (fdt). U-Boot configuration has traditionally been done |
| 10 | using CONFIG options in the board config file. This feature aims to |
| 11 | make it possible for a single U-Boot binary to support multiple boards, |
| 12 | with the exact configuration of each board controlled by a flat device |
| 13 | tree (fdt). This is the approach recently taken by the ARM Linux kernel |
| 14 | and has been used by PowerPC for some time. |
| 15 | |
| 16 | The fdt is a convenient vehicle for implementing run-time configuration |
| 17 | for three reasons. Firstly it is easy to use, being a simple text file. |
| 18 | It is extensible since it consists of nodes and properties in a nice |
| 19 | hierarchical format. |
| 20 | |
| 21 | Finally, there is already excellent infrastructure for the fdt: a |
| 22 | compiler checks the text file and converts it to a compact binary |
| 23 | format, and a library is already available in U-Boot (libfdt) for |
| 24 | handling this format. |
| 25 | |
| 26 | The dts directory contains a Makefile for building the device tree blob |
| 27 | and embedding it in your U-Boot image. This is useful since it allows |
| 28 | U-Boot to configure itself according to what it finds there. If you have |
| 29 | a number of similar boards with different peripherals, you can describe |
| 30 | the features of each board in the device tree file, and have a single |
| 31 | generic source base. |
| 32 | |
| 33 | To enable this feature, add CONFIG_OF_CONTROL to your board config file. |
| 34 | |
| 35 | |
| 36 | What is a Flat Device Tree? |
| 37 | --------------------------- |
| 38 | |
| 39 | An fdt can be specified in source format as a text file. To read about |
| 40 | the fdt syntax, take a look at the specification here: |
| 41 | |
| 42 | https://www.power.org/resources/downloads/Power_ePAPR_APPROVED_v1.0.pdf |
| 43 | |
| 44 | You also might find this section of the Linux kernel documentation |
| 45 | useful: (access this in the Linux kernel source code) |
| 46 | |
| 47 | Documentation/devicetree/booting-without-of.txt |
| 48 | |
| 49 | There is also a mailing list: |
| 50 | |
| 51 | http://lists.ozlabs.org/listinfo/devicetree-discuss |
| 52 | |
| 53 | In case you are wondering, OF stands for Open Firmware. |
| 54 | |
| 55 | |
| 56 | Tools |
| 57 | ----- |
| 58 | |
Simon Glass | 9665f19 | 2018-10-01 12:22:16 -0600 | [diff] [blame] | 59 | To use this feature you will need to get the device tree compiler. This is |
| 60 | provided by U-Boot automatically. If you have a system version of dtc |
| 61 | (typically in the 'device-tree-compiler' package), it is currently not used. |
| 62 | |
| 63 | If you want to build your own dtc, it is kept here: |
Simon Glass | 38d6b8d | 2011-10-15 05:48:21 +0000 | [diff] [blame] | 64 | |
Jon Loeliger | b88a6ba | 2014-05-27 09:12:48 -0500 | [diff] [blame] | 65 | git://git.kernel.org/pub/scm/utils/dtc/dtc.git |
Simon Glass | 38d6b8d | 2011-10-15 05:48:21 +0000 | [diff] [blame] | 66 | |
| 67 | For example: |
| 68 | |
Jon Loeliger | b88a6ba | 2014-05-27 09:12:48 -0500 | [diff] [blame] | 69 | $ git clone git://git.kernel.org/pub/scm/utils/dtc/dtc.git |
Simon Glass | 38d6b8d | 2011-10-15 05:48:21 +0000 | [diff] [blame] | 70 | $ cd dtc |
| 71 | $ make |
| 72 | $ sudo make install |
| 73 | |
| 74 | Then run the compiler (your version will vary): |
| 75 | |
| 76 | $ dtc -v |
| 77 | Version: DTC 1.2.0-g2cb4b51f |
| 78 | $ make tests |
| 79 | $ cd tests |
| 80 | $ ./run_tests.sh |
| 81 | ********** TEST SUMMARY |
| 82 | * Total testcases: 1371 |
| 83 | * PASS: 1371 |
| 84 | * FAIL: 0 |
| 85 | * Bad configuration: 0 |
| 86 | * Strange test result: 0 |
| 87 | |
Simon Glass | 0618f98 | 2013-05-07 06:11:46 +0000 | [diff] [blame] | 88 | You will also find a useful fdtdump utility for decoding a binary file, as |
| 89 | well as fdtget/fdtput for reading and writing properties in a binary file. |
Simon Glass | 38d6b8d | 2011-10-15 05:48:21 +0000 | [diff] [blame] | 90 | |
| 91 | |
| 92 | Where do I get an fdt file for my board? |
| 93 | ---------------------------------------- |
| 94 | |
| 95 | You may find that the Linux kernel has a suitable file. Look in the |
| 96 | kernel source in arch/<arch>/boot/dts. |
| 97 | |
| 98 | If not you might find other boards with suitable files that you can |
| 99 | modify to your needs. Look in the board directories for files with a |
| 100 | .dts extension. |
| 101 | |
| 102 | Failing that, you could write one from scratch yourself! |
| 103 | |
| 104 | |
| 105 | Configuration |
| 106 | ------------- |
| 107 | |
| 108 | Use: |
| 109 | |
| 110 | #define CONFIG_DEFAULT_DEVICE_TREE "<name>" |
| 111 | |
| 112 | to set the filename of the device tree source. Then put your device tree |
| 113 | file into |
| 114 | |
| 115 | board/<vendor>/dts/<name>.dts |
| 116 | |
| 117 | This should include your CPU or SOC's device tree file, placed in |
Stephen Warren | 9215e0b | 2013-07-24 10:09:22 -0700 | [diff] [blame] | 118 | arch/<arch>/dts, and then make any adjustments required. |
Simon Glass | 38d6b8d | 2011-10-15 05:48:21 +0000 | [diff] [blame] | 119 | |
| 120 | If CONFIG_OF_EMBED is defined, then it will be picked up and built into |
Simon Glass | 67bce6b | 2014-06-02 22:04:50 -0600 | [diff] [blame] | 121 | the U-Boot image (including u-boot.bin). This is suitable for debugging |
| 122 | and development only and is not recommended for production devices. |
Simon Glass | 38d6b8d | 2011-10-15 05:48:21 +0000 | [diff] [blame] | 123 | |
| 124 | If CONFIG_OF_SEPARATE is defined, then it will be built and placed in |
Andy Shevchenko | df579da | 2019-01-08 16:17:42 +0200 | [diff] [blame] | 125 | a u-boot.dtb file alongside u-boot-nodtb.bin. A common approach is then to |
Simon Glass | 38d6b8d | 2011-10-15 05:48:21 +0000 | [diff] [blame] | 126 | join the two: |
| 127 | |
Andy Shevchenko | df579da | 2019-01-08 16:17:42 +0200 | [diff] [blame] | 128 | cat u-boot-nodtb.bin u-boot.dtb >image.bin |
Simon Glass | 38d6b8d | 2011-10-15 05:48:21 +0000 | [diff] [blame] | 129 | |
Simon Glass | 67bce6b | 2014-06-02 22:04:50 -0600 | [diff] [blame] | 130 | and then flash image.bin onto your board. Note that U-Boot creates |
Andy Shevchenko | df579da | 2019-01-08 16:17:42 +0200 | [diff] [blame] | 131 | u-boot-dtb.bin which does the above step for you also. Resulting |
| 132 | u-boot.bin is a copy of u-boot-dtb.bin in this case. If you are using |
Simon Glass | 67bce6b | 2014-06-02 22:04:50 -0600 | [diff] [blame] | 133 | CONFIG_SPL_FRAMEWORK, then u-boot.img will be built to include the device |
| 134 | tree binary. |
Simon Glass | 38d6b8d | 2011-10-15 05:48:21 +0000 | [diff] [blame] | 135 | |
Alex Deymo | 5b661ec | 2017-04-02 01:25:20 -0700 | [diff] [blame] | 136 | If CONFIG_OF_BOARD is defined, a board-specific routine will provide the |
| 137 | device tree at runtime, for example if an earlier bootloader stage creates |
| 138 | it and passes it to U-Boot. |
| 139 | |
Simon Glass | 1539343 | 2013-04-20 08:42:41 +0000 | [diff] [blame] | 140 | If CONFIG_OF_HOSTFILE is defined, then it will be read from a file on |
| 141 | startup. This is only useful for sandbox. Use the -d flag to U-Boot to |
| 142 | specify the file to read. |
| 143 | |
| 144 | You cannot use more than one of these options at the same time. |
Simon Glass | 38d6b8d | 2011-10-15 05:48:21 +0000 | [diff] [blame] | 145 | |
Simon Glass | 67bce6b | 2014-06-02 22:04:50 -0600 | [diff] [blame] | 146 | To use a device tree file that you have compiled yourself, pass |
Simon Glass | c3b0de4 | 2014-06-12 07:24:43 -0600 | [diff] [blame] | 147 | EXT_DTB=<filename> to 'make', as in: |
Simon Glass | 67bce6b | 2014-06-02 22:04:50 -0600 | [diff] [blame] | 148 | |
Simon Glass | c3b0de4 | 2014-06-12 07:24:43 -0600 | [diff] [blame] | 149 | make EXT_DTB=boot/am335x-boneblack-pubkey.dtb |
Simon Glass | 67bce6b | 2014-06-02 22:04:50 -0600 | [diff] [blame] | 150 | |
| 151 | Then U-Boot will copy that file to u-boot.dtb, put it in the .img file |
| 152 | if used, and u-boot-dtb.bin. |
| 153 | |
Simon Glass | dc6fa64 | 2011-10-24 19:15:34 +0000 | [diff] [blame] | 154 | If you wish to put the fdt at a different address in memory, you can |
| 155 | define the "fdtcontroladdr" environment variable. This is the hex |
| 156 | address of the fdt binary blob, and will override either of the options. |
| 157 | Be aware that this environment variable is checked prior to relocation, |
| 158 | when only the compiled-in environment is available. Therefore it is not |
| 159 | possible to define this variable in the saved SPI/NAND flash |
Thomas Chou | 4fda281 | 2015-10-16 08:44:51 +0800 | [diff] [blame] | 160 | environment, for example (it will be ignored). After relocation, this |
| 161 | variable will be set to the address of the newly relocated fdt blob. |
| 162 | It is read-only and cannot be changed. It can optionally be used to |
| 163 | control the boot process of Linux with bootm/bootz commands. |
Simon Glass | dc6fa64 | 2011-10-24 19:15:34 +0000 | [diff] [blame] | 164 | |
| 165 | To use this, put something like this in your board header file: |
| 166 | |
| 167 | #define CONFIG_EXTRA_ENV_SETTINGS "fdtcontroladdr=10000\0" |
| 168 | |
Jagannadha Sutradharudu Teki | 79e63a4 | 2013-02-28 10:20:18 +0000 | [diff] [blame] | 169 | Build: |
| 170 | |
| 171 | After board configuration is done, fdt supported u-boot can be build in two ways: |
| 172 | 1) build the default dts which is defined from CONFIG_DEFAULT_DEVICE_TREE |
| 173 | $ make |
| 174 | 2) build the user specified dts file |
| 175 | $ make DEVICE_TREE=<dts-file-name> |
| 176 | |
Simon Glass | 38d6b8d | 2011-10-15 05:48:21 +0000 | [diff] [blame] | 177 | |
Simon Glass | a31dc3d | 2018-10-01 12:22:17 -0600 | [diff] [blame] | 178 | Relocation, SPL and TPL |
| 179 | ----------------------- |
| 180 | |
| 181 | U-Boot can be divided into three phases: TPL, SPL and U-Boot proper. |
| 182 | |
| 183 | The full device tree is available to U-Boot proper, but normally only a subset |
| 184 | (or none at all) is available to TPL and SPL. See 'Pre-Relocation Support' and |
| 185 | 'SPL Support' in doc/driver-model/README.txt for more details. |
| 186 | |
| 187 | |
Jean-Jacques Hiblot | 7c530e3 | 2018-12-07 14:50:52 +0100 | [diff] [blame] | 188 | Using several DTBs in the SPL (CONFIG_SPL_MULTI_DTB) |
| 189 | ---------------------------------------------------- |
| 190 | In some rare cases it is desirable to let SPL be able to select one DTB among |
| 191 | many. This usually not very useful as the DTB for the SPL is small and usually |
| 192 | fits several platforms. However the DTB sometimes include information that do |
| 193 | work on several platforms (like IO tuning parameters). |
| 194 | In this case it is possible to use CONFIG_SPL_MULTI_DTB. This option appends to |
| 195 | the SPL a FIT image containing several DTBs listed in SPL_OF_LIST. |
| 196 | board_fit_config_name_match() is called to select the right DTB. |
| 197 | |
| 198 | If board_fit_config_name_match() relies on DM (DM driver to access an EEPROM |
| 199 | containing the board ID for example), it possible to start with a generic DTB |
| 200 | and then switch over to the right DTB after the detection. For this purpose, |
| 201 | the platform code must call fdtdec_resetup(). Based on the returned flag, the |
| 202 | platform may have to re-initiliaze the DM subusystem using dm_uninit() and |
| 203 | dm_init_and_scan(). |
| 204 | |
| 205 | |
Simon Glass | 38d6b8d | 2011-10-15 05:48:21 +0000 | [diff] [blame] | 206 | Limitations |
| 207 | ----------- |
| 208 | |
| 209 | U-Boot is designed to build with a single architecture type and CPU |
| 210 | type. So for example it is not possible to build a single ARM binary |
| 211 | which runs on your AT91 and OMAP boards, relying on an fdt to configure |
| 212 | the various features. This is because you must select one of |
| 213 | the CPU families within arch/arm/cpu/arm926ejs (omap or at91) at build |
| 214 | time. Similarly you cannot build for multiple cpu types or |
| 215 | architectures. |
| 216 | |
| 217 | That said the complexity reduction by using fdt to support variants of |
| 218 | boards which use the same SOC / CPU can be substantial. |
| 219 | |
| 220 | It is important to understand that the fdt only selects options |
| 221 | available in the platform / drivers. It cannot add new drivers (yet). So |
| 222 | you must still have the CONFIG option to enable the driver. For example, |
| 223 | you need to define CONFIG_SYS_NS16550 to bring in the NS16550 driver, |
| 224 | but can use the fdt to specific the UART clock, peripheral address, etc. |
| 225 | In very broad terms, the CONFIG options in general control *what* driver |
| 226 | files are pulled in, and the fdt controls *how* those files work. |
| 227 | |
| 228 | -- |
| 229 | Simon Glass <sjg@chromium.org> |
| 230 | 1-Sep-11 |