blob: 1289b6156feb44db9b2b8bb3d6e591a86248a6e9 [file] [log] [blame]
Simon Glassac35c2f2021-08-01 18:57:10 -06001.. SPDX-License-Identifier: GPL-2.0+
2.. sectionauthor:: Copyright 2011 The Chromium OS Authors
Simon Glass38d6b8d2011-10-15 05:48:21 +00003
4Device Tree Control in U-Boot
5=============================
6
7This feature provides for run-time configuration of U-Boot via a flat
8device tree (fdt). U-Boot configuration has traditionally been done
9using CONFIG options in the board config file. This feature aims to
10make it possible for a single U-Boot binary to support multiple boards,
11with the exact configuration of each board controlled by a flat device
12tree (fdt). This is the approach recently taken by the ARM Linux kernel
13and has been used by PowerPC for some time.
14
15The fdt is a convenient vehicle for implementing run-time configuration
16for three reasons. Firstly it is easy to use, being a simple text file.
17It is extensible since it consists of nodes and properties in a nice
18hierarchical format.
19
20Finally, there is already excellent infrastructure for the fdt: a
21compiler checks the text file and converts it to a compact binary
22format, and a library is already available in U-Boot (libfdt) for
23handling this format.
24
25The dts directory contains a Makefile for building the device tree blob
26and embedding it in your U-Boot image. This is useful since it allows
27U-Boot to configure itself according to what it finds there. If you have
28a number of similar boards with different peripherals, you can describe
29the features of each board in the device tree file, and have a single
30generic source base.
31
32To enable this feature, add CONFIG_OF_CONTROL to your board config file.
33
34
35What is a Flat Device Tree?
36---------------------------
37
38An fdt can be specified in source format as a text file. To read about
Simon Glassac35c2f2021-08-01 18:57:10 -060039the fdt syntax, take a look at the specification (dtspec_).
Simon Glass38d6b8d2011-10-15 05:48:21 +000040
41You also might find this section of the Linux kernel documentation
42useful: (access this in the Linux kernel source code)
43
44 Documentation/devicetree/booting-without-of.txt
45
46There is also a mailing list:
47
48 http://lists.ozlabs.org/listinfo/devicetree-discuss
49
50In case you are wondering, OF stands for Open Firmware.
51
52
53Tools
54-----
55
Simon Glass9665f192018-10-01 12:22:16 -060056To use this feature you will need to get the device tree compiler. This is
57provided by U-Boot automatically. If you have a system version of dtc
58(typically in the 'device-tree-compiler' package), it is currently not used.
59
Simon Glassac35c2f2021-08-01 18:57:10 -060060If you want to build your own dtc, it is kept here::
Simon Glass38d6b8d2011-10-15 05:48:21 +000061
Jon Loeligerb88a6ba2014-05-27 09:12:48 -050062 git://git.kernel.org/pub/scm/utils/dtc/dtc.git
Simon Glass38d6b8d2011-10-15 05:48:21 +000063
Simon Glassac35c2f2021-08-01 18:57:10 -060064For example::
Simon Glass38d6b8d2011-10-15 05:48:21 +000065
Jon Loeligerb88a6ba2014-05-27 09:12:48 -050066 $ git clone git://git.kernel.org/pub/scm/utils/dtc/dtc.git
Simon Glass38d6b8d2011-10-15 05:48:21 +000067 $ cd dtc
68 $ make
69 $ sudo make install
70
Simon Glassac35c2f2021-08-01 18:57:10 -060071Then run the compiler (your version will vary)::
Simon Glass38d6b8d2011-10-15 05:48:21 +000072
73 $ dtc -v
74 Version: DTC 1.2.0-g2cb4b51f
75 $ make tests
76 $ cd tests
77 $ ./run_tests.sh
78 ********** TEST SUMMARY
79 * Total testcases: 1371
80 * PASS: 1371
81 * FAIL: 0
82 * Bad configuration: 0
83 * Strange test result: 0
84
Simon Glass0618f982013-05-07 06:11:46 +000085You will also find a useful fdtdump utility for decoding a binary file, as
86well as fdtget/fdtput for reading and writing properties in a binary file.
Simon Glass38d6b8d2011-10-15 05:48:21 +000087
88
89Where do I get an fdt file for my board?
90----------------------------------------
91
92You may find that the Linux kernel has a suitable file. Look in the
93kernel source in arch/<arch>/boot/dts.
94
95If not you might find other boards with suitable files that you can
96modify to your needs. Look in the board directories for files with a
97.dts extension.
98
99Failing that, you could write one from scratch yourself!
100
101
102Configuration
103-------------
104
Simon Glassac35c2f2021-08-01 18:57:10 -0600105Use::
Simon Glass38d6b8d2011-10-15 05:48:21 +0000106
Simon Glassac35c2f2021-08-01 18:57:10 -0600107 #define CONFIG_DEFAULT_DEVICE_TREE "<name>"
Simon Glass38d6b8d2011-10-15 05:48:21 +0000108
109to set the filename of the device tree source. Then put your device tree
Simon Glassac35c2f2021-08-01 18:57:10 -0600110file into::
Simon Glass38d6b8d2011-10-15 05:48:21 +0000111
Simon Glassac35c2f2021-08-01 18:57:10 -0600112 board/<vendor>/dts/<name>.dts
Simon Glass38d6b8d2011-10-15 05:48:21 +0000113
114This should include your CPU or SOC's device tree file, placed in
Stephen Warren9215e0b2013-07-24 10:09:22 -0700115arch/<arch>/dts, and then make any adjustments required.
Simon Glass38d6b8d2011-10-15 05:48:21 +0000116
117If CONFIG_OF_EMBED is defined, then it will be picked up and built into
Simon Glass67bce6b2014-06-02 22:04:50 -0600118the U-Boot image (including u-boot.bin). This is suitable for debugging
119and development only and is not recommended for production devices.
Simon Glass38d6b8d2011-10-15 05:48:21 +0000120
121If CONFIG_OF_SEPARATE is defined, then it will be built and placed in
Andy Shevchenkodf579da2019-01-08 16:17:42 +0200122a u-boot.dtb file alongside u-boot-nodtb.bin. A common approach is then to
Simon Glassac35c2f2021-08-01 18:57:10 -0600123join the two::
Simon Glass38d6b8d2011-10-15 05:48:21 +0000124
Simon Glassac35c2f2021-08-01 18:57:10 -0600125 cat u-boot-nodtb.bin u-boot.dtb >image.bin
Simon Glass38d6b8d2011-10-15 05:48:21 +0000126
Simon Glass67bce6b2014-06-02 22:04:50 -0600127and then flash image.bin onto your board. Note that U-Boot creates
Andy Shevchenkodf579da2019-01-08 16:17:42 +0200128u-boot-dtb.bin which does the above step for you also. Resulting
129u-boot.bin is a copy of u-boot-dtb.bin in this case. If you are using
Simon Glass67bce6b2014-06-02 22:04:50 -0600130CONFIG_SPL_FRAMEWORK, then u-boot.img will be built to include the device
131tree binary.
Simon Glass38d6b8d2011-10-15 05:48:21 +0000132
Alex Deymo5b661ec2017-04-02 01:25:20 -0700133If CONFIG_OF_BOARD is defined, a board-specific routine will provide the
134device tree at runtime, for example if an earlier bootloader stage creates
135it and passes it to U-Boot.
136
Simon Glass15393432013-04-20 08:42:41 +0000137If CONFIG_OF_HOSTFILE is defined, then it will be read from a file on
138startup. This is only useful for sandbox. Use the -d flag to U-Boot to
139specify the file to read.
140
141You cannot use more than one of these options at the same time.
Simon Glass38d6b8d2011-10-15 05:48:21 +0000142
Simon Glass67bce6b2014-06-02 22:04:50 -0600143To use a device tree file that you have compiled yourself, pass
Simon Glassac35c2f2021-08-01 18:57:10 -0600144EXT_DTB=<filename> to 'make', as in::
Simon Glass67bce6b2014-06-02 22:04:50 -0600145
Simon Glassac35c2f2021-08-01 18:57:10 -0600146 make EXT_DTB=boot/am335x-boneblack-pubkey.dtb
Simon Glass67bce6b2014-06-02 22:04:50 -0600147
148Then U-Boot will copy that file to u-boot.dtb, put it in the .img file
149if used, and u-boot-dtb.bin.
150
Simon Glassdc6fa642011-10-24 19:15:34 +0000151If you wish to put the fdt at a different address in memory, you can
152define the "fdtcontroladdr" environment variable. This is the hex
153address of the fdt binary blob, and will override either of the options.
154Be aware that this environment variable is checked prior to relocation,
155when only the compiled-in environment is available. Therefore it is not
156possible to define this variable in the saved SPI/NAND flash
Thomas Chou4fda2812015-10-16 08:44:51 +0800157environment, for example (it will be ignored). After relocation, this
158variable will be set to the address of the newly relocated fdt blob.
159It is read-only and cannot be changed. It can optionally be used to
160control the boot process of Linux with bootm/bootz commands.
Simon Glassdc6fa642011-10-24 19:15:34 +0000161
Simon Glassac35c2f2021-08-01 18:57:10 -0600162To use this, put something like this in your board header file::
Simon Glassdc6fa642011-10-24 19:15:34 +0000163
Simon Glassac35c2f2021-08-01 18:57:10 -0600164 #define CONFIG_EXTRA_ENV_SETTINGS "fdtcontroladdr=10000\0"
Simon Glassdc6fa642011-10-24 19:15:34 +0000165
Jagannadha Sutradharudu Teki79e63a42013-02-28 10:20:18 +0000166Build:
167
Simon Glassac35c2f2021-08-01 18:57:10 -0600168After board configuration is done, fdt supported u-boot can be build in two
169ways:
170
171# build the default dts which is defined from CONFIG_DEFAULT_DEVICE_TREE::
172
Jagannadha Sutradharudu Teki79e63a42013-02-28 10:20:18 +0000173 $ make
Simon Glassac35c2f2021-08-01 18:57:10 -0600174
175# build the user specified dts file::
176
Jagannadha Sutradharudu Teki79e63a42013-02-28 10:20:18 +0000177 $ make DEVICE_TREE=<dts-file-name>
178
Simon Glass38d6b8d2011-10-15 05:48:21 +0000179
Simon Glassa31dc3d2018-10-01 12:22:17 -0600180Relocation, SPL and TPL
181-----------------------
182
183U-Boot can be divided into three phases: TPL, SPL and U-Boot proper.
184
185The full device tree is available to U-Boot proper, but normally only a subset
186(or none at all) is available to TPL and SPL. See 'Pre-Relocation Support' and
Heinrich Schuchardtc79f03c2020-02-25 21:35:39 +0100187'SPL Support' in doc/driver-model/design.rst for more details.
Simon Glassa31dc3d2018-10-01 12:22:17 -0600188
189
Jean-Jacques Hiblot7c530e32018-12-07 14:50:52 +0100190Using several DTBs in the SPL (CONFIG_SPL_MULTI_DTB)
191----------------------------------------------------
192In some rare cases it is desirable to let SPL be able to select one DTB among
193many. This usually not very useful as the DTB for the SPL is small and usually
194fits several platforms. However the DTB sometimes include information that do
195work on several platforms (like IO tuning parameters).
196In this case it is possible to use CONFIG_SPL_MULTI_DTB. This option appends to
197the SPL a FIT image containing several DTBs listed in SPL_OF_LIST.
198board_fit_config_name_match() is called to select the right DTB.
199
200If board_fit_config_name_match() relies on DM (DM driver to access an EEPROM
201containing the board ID for example), it possible to start with a generic DTB
202and then switch over to the right DTB after the detection. For this purpose,
203the platform code must call fdtdec_resetup(). Based on the returned flag, the
204platform may have to re-initiliaze the DM subusystem using dm_uninit() and
205dm_init_and_scan().
206
207
Simon Glass38d6b8d2011-10-15 05:48:21 +0000208Limitations
209-----------
210
211U-Boot is designed to build with a single architecture type and CPU
212type. So for example it is not possible to build a single ARM binary
213which runs on your AT91 and OMAP boards, relying on an fdt to configure
214the various features. This is because you must select one of
215the CPU families within arch/arm/cpu/arm926ejs (omap or at91) at build
216time. Similarly you cannot build for multiple cpu types or
217architectures.
218
219That said the complexity reduction by using fdt to support variants of
220boards which use the same SOC / CPU can be substantial.
221
222It is important to understand that the fdt only selects options
223available in the platform / drivers. It cannot add new drivers (yet). So
224you must still have the CONFIG option to enable the driver. For example,
225you need to define CONFIG_SYS_NS16550 to bring in the NS16550 driver,
226but can use the fdt to specific the UART clock, peripheral address, etc.
227In very broad terms, the CONFIG options in general control *what* driver
228files are pulled in, and the fdt controls *how* those files work.
229
Simon Glassac35c2f2021-08-01 18:57:10 -0600230.. _dtspec: https://www.power.org/resources/downloads/Power_ePAPR_APPROVED_v1.0.pdf