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