blob: 11c92d440f4daab285213594d42841c7a9c6e074 [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
Simon Glassb4fbaaf2021-08-01 18:57:11 -06004Devicetree Control in U-Boot
5============================
Simon Glass38d6b8d2011-10-15 05:48:21 +00006
Simon Glassb4fbaaf2021-08-01 18:57:11 -06007This feature provides for run-time configuration of U-Boot via a flattened
8devicetree (fdt).
9
10This feature aims to make it possible for a single U-Boot binary to support
11multiple boards, with the exact configuration of each board controlled by
12a flattened devicetree (fdt). This is the approach taken by Linux kernel for
13ARM and RISC-V and has been used by PowerPC for some time.
Simon Glass38d6b8d2011-10-15 05:48:21 +000014
15The fdt is a convenient vehicle for implementing run-time configuration
Simon Glassb4fbaaf2021-08-01 18:57:11 -060016for three reasons:
Simon Glass38d6b8d2011-10-15 05:48:21 +000017
Simon Glassb4fbaaf2021-08-01 18:57:11 -060018- There is already excellent infrastructure for the fdt: a compiler checks
19 the text file and converts it to a compact binary format, and a library
20 is already available in U-Boot (libfdt) for handling this format
21- It is extensible since it consists of nodes and properties in a nice
22 hierarchical format
23- It is fairly efficient to read incrementally
Simon Glass38d6b8d2011-10-15 05:48:21 +000024
Simon Glassb4fbaaf2021-08-01 18:57:11 -060025The arch/<arch>/dts directories contains a Makefile for building the devicetree
26blob and embedding it in the U-Boot image. This is useful since it allows
Simon Glass38d6b8d2011-10-15 05:48:21 +000027U-Boot to configure itself according to what it finds there. If you have
28a number of similar boards with different peripherals, you can describe
Simon Glassb4fbaaf2021-08-01 18:57:11 -060029the features of each board in the devicetree file, and have a single
Simon Glass38d6b8d2011-10-15 05:48:21 +000030generic source base.
31
32To enable this feature, add CONFIG_OF_CONTROL to your board config file.
33
34
Simon Glassb4fbaaf2021-08-01 18:57:11 -060035What is a Flattened Devicetree?
36-------------------------------
Simon Glass38d6b8d2011-10-15 05:48:21 +000037
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
Simon Glassb4fbaaf2021-08-01 18:57:11 -060041There is also a mailing list (dtlist_) for the compiler and associated
42tools.
Simon Glass38d6b8d2011-10-15 05:48:21 +000043
Simon Glassb4fbaaf2021-08-01 18:57:11 -060044In case you are wondering, OF stands for Open Firmware. This follows the
45convention used in Linux.
Simon Glass38d6b8d2011-10-15 05:48:21 +000046
Simon Glass38d6b8d2011-10-15 05:48:21 +000047
48Tools
49-----
50
Simon Glassb4fbaaf2021-08-01 18:57:11 -060051To create flattened device trees the device tree compiler is used. This is
Simon Glass9665f192018-10-01 12:22:16 -060052provided by U-Boot automatically. If you have a system version of dtc
Simon Glassb4fbaaf2021-08-01 18:57:11 -060053(typically in the 'device-tree-compiler' package), that system version is
54currently not used.
Simon Glass9665f192018-10-01 12:22:16 -060055
Simon Glassac35c2f2021-08-01 18:57:10 -060056If you want to build your own dtc, it is kept here::
Simon Glass38d6b8d2011-10-15 05:48:21 +000057
Simon Glassb4fbaaf2021-08-01 18:57:11 -060058 git://git.kernel.org/pub/scm/utils/dtc/dtc.git
Simon Glass38d6b8d2011-10-15 05:48:21 +000059
Simon Glassb4fbaaf2021-08-01 18:57:11 -060060You can decode a binary file with::
Simon Glass38d6b8d2011-10-15 05:48:21 +000061
Simon Glassb4fbaaf2021-08-01 18:57:11 -060062 dtc -I dtb -O dts <filename.dtb>
Simon Glass38d6b8d2011-10-15 05:48:21 +000063
Simon Glassb4fbaaf2021-08-01 18:57:11 -060064That repo also includes `fdtget`/`fdtput` for reading and writing properties in
65a binary file. U-Boot adds its own `fdtgrep` for creating subsets of the file.
Simon Glass38d6b8d2011-10-15 05:48:21 +000066
Simon Glass38d6b8d2011-10-15 05:48:21 +000067
Simon Glassb4fbaaf2021-08-01 18:57:11 -060068Where do I get a devicetree file for my board?
69----------------------------------------------
Simon Glass38d6b8d2011-10-15 05:48:21 +000070
71You may find that the Linux kernel has a suitable file. Look in the
72kernel source in arch/<arch>/boot/dts.
73
74If not you might find other boards with suitable files that you can
75modify to your needs. Look in the board directories for files with a
76.dts extension.
77
78Failing that, you could write one from scratch yourself!
79
80
81Configuration
82-------------
83
Simon Glassac35c2f2021-08-01 18:57:10 -060084Use::
Simon Glass38d6b8d2011-10-15 05:48:21 +000085
Simon Glassac35c2f2021-08-01 18:57:10 -060086 #define CONFIG_DEFAULT_DEVICE_TREE "<name>"
Simon Glass38d6b8d2011-10-15 05:48:21 +000087
Simon Glassb4fbaaf2021-08-01 18:57:11 -060088to set the filename of the devicetree source. Then put your devicetree
Simon Glassac35c2f2021-08-01 18:57:10 -060089file into::
Simon Glass38d6b8d2011-10-15 05:48:21 +000090
Simon Glassb4fbaaf2021-08-01 18:57:11 -060091 arch/<arch>/dts/<name>.dts
Simon Glass38d6b8d2011-10-15 05:48:21 +000092
Simon Glassb4fbaaf2021-08-01 18:57:11 -060093This should include your CPU or SOC's devicetree file, placed in
94`arch/<arch>/dts`, and then make any adjustments required using a u-boot-dtsi
95file for your board.
Simon Glass38d6b8d2011-10-15 05:48:21 +000096
97If CONFIG_OF_EMBED is defined, then it will be picked up and built into
Simon Glass67bce6b2014-06-02 22:04:50 -060098the U-Boot image (including u-boot.bin). This is suitable for debugging
99and development only and is not recommended for production devices.
Simon Glass38d6b8d2011-10-15 05:48:21 +0000100
101If CONFIG_OF_SEPARATE is defined, then it will be built and placed in
Simon Glassb4fbaaf2021-08-01 18:57:11 -0600102a u-boot.dtb file alongside u-boot-nodtb.bin with the combined result placed
Baruch Siach6b0fcc02023-05-02 13:21:46 +0300103in u-boot.bin so you can still just flash u-boot.bin onto your board. If you are
Simon Glassb4fbaaf2021-08-01 18:57:11 -0600104using CONFIG_SPL_FRAMEWORK, then u-boot.img will be built to include the device
Simon Glass67bce6b2014-06-02 22:04:50 -0600105tree binary.
Simon Glass38d6b8d2011-10-15 05:48:21 +0000106
Alex Deymo5b661ec2017-04-02 01:25:20 -0700107If CONFIG_OF_BOARD is defined, a board-specific routine will provide the
Simon Glassb4fbaaf2021-08-01 18:57:11 -0600108devicetree at runtime, for example if an earlier bootloader stage creates
Alex Deymo5b661ec2017-04-02 01:25:20 -0700109it and passes it to U-Boot.
110
Simon Glass4a30a572024-01-03 18:49:19 -0700111If CONFIG_BLOBLIST is defined, the devicetree may come from a bloblist passed
112from a previous stage, if present.
113
Ilias Apalodimasab5348a2021-10-26 09:12:33 +0300114If CONFIG_SANDBOX is defined, then it will be read from a file on
115startup. Use the -d flag to U-Boot to specify the file to read, -D for the
116default and -T for the test devicetree, used to run sandbox unit tests.
Simon Glass15393432013-04-20 08:42:41 +0000117
118You cannot use more than one of these options at the same time.
Simon Glass38d6b8d2011-10-15 05:48:21 +0000119
Simon Glassb4fbaaf2021-08-01 18:57:11 -0600120To use a devicetree file that you have compiled yourself, pass
Simon Glassac35c2f2021-08-01 18:57:10 -0600121EXT_DTB=<filename> to 'make', as in::
Simon Glass67bce6b2014-06-02 22:04:50 -0600122
Simon Glassac35c2f2021-08-01 18:57:10 -0600123 make EXT_DTB=boot/am335x-boneblack-pubkey.dtb
Simon Glass67bce6b2014-06-02 22:04:50 -0600124
125Then U-Boot will copy that file to u-boot.dtb, put it in the .img file
126if used, and u-boot-dtb.bin.
127
Simon Glassdc6fa642011-10-24 19:15:34 +0000128If you wish to put the fdt at a different address in memory, you can
129define the "fdtcontroladdr" environment variable. This is the hex
130address of the fdt binary blob, and will override either of the options.
131Be aware that this environment variable is checked prior to relocation,
132when only the compiled-in environment is available. Therefore it is not
133possible to define this variable in the saved SPI/NAND flash
Thomas Chou4fda2812015-10-16 08:44:51 +0800134environment, for example (it will be ignored). After relocation, this
135variable will be set to the address of the newly relocated fdt blob.
136It is read-only and cannot be changed. It can optionally be used to
137control the boot process of Linux with bootm/bootz commands.
Simon Glassdc6fa642011-10-24 19:15:34 +0000138
Simon Glassac35c2f2021-08-01 18:57:10 -0600139To use this, put something like this in your board header file::
Simon Glassdc6fa642011-10-24 19:15:34 +0000140
Tom Rinic9edebe2022-12-04 10:03:50 -0500141 #define CFG_EXTRA_ENV_SETTINGS "fdtcontroladdr=10000\0"
Simon Glassdc6fa642011-10-24 19:15:34 +0000142
Jagannadha Sutradharudu Teki79e63a42013-02-28 10:20:18 +0000143Build:
144
Simon Glassb4fbaaf2021-08-01 18:57:11 -0600145After the board configuration is done, fdt supported u-boot can be built in two
Simon Glassac35c2f2021-08-01 18:57:10 -0600146ways:
147
148# build the default dts which is defined from CONFIG_DEFAULT_DEVICE_TREE::
149
Jagannadha Sutradharudu Teki79e63a42013-02-28 10:20:18 +0000150 $ make
Simon Glassac35c2f2021-08-01 18:57:10 -0600151
152# build the user specified dts file::
153
Jagannadha Sutradharudu Teki79e63a42013-02-28 10:20:18 +0000154 $ make DEVICE_TREE=<dts-file-name>
155
Simon Glassb4fbaaf2021-08-01 18:57:11 -0600156
Simon Glassc5f9e162021-08-01 18:57:12 -0600157.. _dttweaks:
158
Simon Glassb4fbaaf2021-08-01 18:57:11 -0600159Adding tweaks for U-Boot
160------------------------
161
162It is strongly recommended that devicetree files in U-Boot are an exact copy of
163those in Linux, so that it is easy to sync them up from time to time.
164
165U-Boot is of course a very different project from Linux, e.g. it operates under
166much more restrictive memory and code-size constraints. Where Linux may use a
167full clock driver with Common Clock Format (CCF) to find the input clock to the
168UART, U-Boot typically wants to output a banner as early as possible before too
169much code has run.
170
171A second difference is that U-Boot includes different phases. For SPL,
172constraints are even more extreme and the devicetree is shrunk to remove
173unwanted nodes, or even turned into C code to avoid access overhead.
174
175U-Boot automatically looks for and includes a file with updates to the standard
176devicetree for your board, searching for them in the same directory as the
177main file, in this order::
178
179 <orig_filename>-u-boot.dtsi
180 <CONFIG_SYS_SOC>-u-boot.dtsi
181 <CONFIG_SYS_CPU>-u-boot.dtsi
182 <CONFIG_SYS_VENDOR>-u-boot.dtsi
183 u-boot.dtsi
184
185Only one of these is selected but of course you can #include another one within
186that file, to create a hierarchy of shared files.
Simon Glass38d6b8d2011-10-15 05:48:21 +0000187
Rasmus Villemoes303d98d2021-11-21 14:52:51 +0100188
189External .dtsi fragments
190------------------------
191
192Apart from describing the hardware present, U-Boot also uses its
193control dtb for various configuration purposes. For example, the
194public key(s) used for Verified Boot are embedded in a specific format
195in a /signature node.
196
197As mentioned above, the U-Boot build system automatically includes a
198`*-u-boot.dtsi` file, if found, containing U-Boot specific
199quirks. However, some data, such as the mentioned public keys, are not
200appropriate for upstream U-Boot but are better kept and maintained
201outside the U-Boot repository. You can use CONFIG_DEVICE_TREE_INCLUDES
202to specify a list of .dtsi files that will also be included when
203building .dtb files.
204
205
Simon Glassa31dc3d2018-10-01 12:22:17 -0600206Relocation, SPL and TPL
207-----------------------
208
209U-Boot can be divided into three phases: TPL, SPL and U-Boot proper.
210
Simon Glassb4fbaaf2021-08-01 18:57:11 -0600211The full devicetree is available to U-Boot proper, but normally only a subset
Simon Glassa31dc3d2018-10-01 12:22:17 -0600212(or none at all) is available to TPL and SPL. See 'Pre-Relocation Support' and
Heinrich Schuchardtc79f03c2020-02-25 21:35:39 +0100213'SPL Support' in doc/driver-model/design.rst for more details.
Simon Glassa31dc3d2018-10-01 12:22:17 -0600214
215
Jean-Jacques Hiblot7c530e32018-12-07 14:50:52 +0100216Using several DTBs in the SPL (CONFIG_SPL_MULTI_DTB)
217----------------------------------------------------
218In some rare cases it is desirable to let SPL be able to select one DTB among
219many. This usually not very useful as the DTB for the SPL is small and usually
220fits several platforms. However the DTB sometimes include information that do
221work on several platforms (like IO tuning parameters).
222In this case it is possible to use CONFIG_SPL_MULTI_DTB. This option appends to
223the SPL a FIT image containing several DTBs listed in SPL_OF_LIST.
224board_fit_config_name_match() is called to select the right DTB.
225
226If board_fit_config_name_match() relies on DM (DM driver to access an EEPROM
227containing the board ID for example), it possible to start with a generic DTB
228and then switch over to the right DTB after the detection. For this purpose,
229the platform code must call fdtdec_resetup(). Based on the returned flag, the
Simon Glassb4fbaaf2021-08-01 18:57:11 -0600230platform may have to re-initialise the DM subsystem using dm_uninit() and
Jean-Jacques Hiblot7c530e32018-12-07 14:50:52 +0100231dm_init_and_scan().
232
233
Simon Glass38d6b8d2011-10-15 05:48:21 +0000234Limitations
235-----------
236
Simon Glassb4fbaaf2021-08-01 18:57:11 -0600237Devicetrees can help reduce the complexity of supporting variants of boards
238which use the same SOC / CPU.
239
240However U-Boot is designed to build for a single architecture type and CPU
Simon Glass38d6b8d2011-10-15 05:48:21 +0000241type. So for example it is not possible to build a single ARM binary
242which runs on your AT91 and OMAP boards, relying on an fdt to configure
243the various features. This is because you must select one of
244the CPU families within arch/arm/cpu/arm926ejs (omap or at91) at build
Simon Glassb4fbaaf2021-08-01 18:57:11 -0600245time. Similarly U-Boot cannot be built for multiple cpu types or
Simon Glass38d6b8d2011-10-15 05:48:21 +0000246architectures.
247
Simon Glass38d6b8d2011-10-15 05:48:21 +0000248It is important to understand that the fdt only selects options
249available in the platform / drivers. It cannot add new drivers (yet). So
250you must still have the CONFIG option to enable the driver. For example,
251you need to define CONFIG_SYS_NS16550 to bring in the NS16550 driver,
252but can use the fdt to specific the UART clock, peripheral address, etc.
253In very broad terms, the CONFIG options in general control *what* driver
254files are pulled in, and the fdt controls *how* those files work.
255
Simon Glassb4fbaaf2021-08-01 18:57:11 -0600256History
257-------
258
259U-Boot configuration was previous done using CONFIG options in the board
260config file. This eventually got out of hand with nearly 10,000 options.
261
262U-Boot adopted devicetrees around the same time as Linux and early boards
263used it before Linux (e.g. snow). The two projects developed in parallel
264and there are still some differences in the bindings for certain boards.
265While there has been discussion of having a separate repository for devicetree
266files, in practice the Linux kernel Git repository has become the place where
267these are stored, with U-Boot taking copies and adding tweaks with u-boot.dtsi
268files.
269
270.. _dtspec: https://www.devicetree.org/specifications/
271.. _dtlist: https://www.spinics.net/lists/devicetree-compiler/