blob: ca4fb0b5b10fd753a3d756ab798d31e312462a2f [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
Sumit Garg7a87a442024-02-22 15:06:04 +05303.. Copyright 2023-2024 Linaro Ltd.
Simon Glass38d6b8d2011-10-15 05:48:21 +00004
Simon Glassb4fbaaf2021-08-01 18:57:11 -06005Devicetree Control in U-Boot
6============================
Simon Glass38d6b8d2011-10-15 05:48:21 +00007
Simon Glassb4fbaaf2021-08-01 18:57:11 -06008This feature provides for run-time configuration of U-Boot via a flattened
9devicetree (fdt).
10
11This feature aims to make it possible for a single U-Boot binary to support
12multiple boards, with the exact configuration of each board controlled by
13a flattened devicetree (fdt). This is the approach taken by Linux kernel for
14ARM and RISC-V and has been used by PowerPC for some time.
Simon Glass38d6b8d2011-10-15 05:48:21 +000015
16The fdt is a convenient vehicle for implementing run-time configuration
Simon Glassb4fbaaf2021-08-01 18:57:11 -060017for three reasons:
Simon Glass38d6b8d2011-10-15 05:48:21 +000018
Simon Glassb4fbaaf2021-08-01 18:57:11 -060019- There is already excellent infrastructure for the fdt: a compiler checks
20 the text file and converts it to a compact binary format, and a library
21 is already available in U-Boot (libfdt) for handling this format
22- It is extensible since it consists of nodes and properties in a nice
23 hierarchical format
24- It is fairly efficient to read incrementally
Simon Glass38d6b8d2011-10-15 05:48:21 +000025
Sumit Garg7a87a442024-02-22 15:06:04 +053026The U-Boot Makefile infrastructure allows for building the devicetree blob
27and embedding it in the U-Boot image. This is useful since it allows U-Boot
28to configure itself according to what it finds there. If you have a number
29of similar boards with different peripherals, you can describe the features
30of each board in the devicetree file, and have a single generic source base.
Simon Glass38d6b8d2011-10-15 05:48:21 +000031
Sumit Garg86f4ccc2024-02-22 15:06:03 +053032To enable this feature, select `OF_CONTROL` via Kconfig.
Simon Glass38d6b8d2011-10-15 05:48:21 +000033
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
Sumit Garg879c61c2024-03-01 18:54:53 +053039the fdt syntax, take a look at `the devicetree specification`_.
Simon Glass38d6b8d2011-10-15 05:48:21 +000040
Sumit Garg879c61c2024-03-01 18:54:53 +053041There is also a `devicetree compiler mailing list`_ for the compiler and
42associated tools.
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
Sumit Garg7a87a442024-02-22 15:06:04 +053071The devicetree files and devicetree bindings are maintained as part of the Linux
72kernel git repository. Traditionally, U-Boot placed copies of devicetree source
73files from the Linux kernel into `arch/<arch>/dts/<name>.dts`. However, this
74required each board maintainer to manually keep their devicetree in sync with
75the Linux kernel and often led to divergence between these copies.
76
77U-Boot rather maintains a Git subtree as `dts/upstream/` sub-directory. It is
78regularly synced with the Linux kernel and hence no need for manual devicetree
79sync. You may find that the `dts/upstream/` already has a suitable devicetree
80file for your board. Look in `dts/upstream/src/<arch>/<vendor>`.
Simon Glass38d6b8d2011-10-15 05:48:21 +000081
82If not you might find other boards with suitable files that you can
83modify to your needs. Look in the board directories for files with a
84.dts extension.
85
86Failing that, you could write one from scratch yourself!
87
Sumit Garg7a87a442024-02-22 15:06:04 +053088
89Resyncing with devicetree-rebasing
90----------------------------------
91
Sumit Garg879c61c2024-03-01 18:54:53 +053092The `devicetree-rebasing repository`_ maintains a fork cum mirror copy of
Sumit Garg7a87a442024-02-22 15:06:04 +053093devicetree files along with the bindings synced at every Linux kernel major
94release or intermediate release candidates. The U-Boot maintainers regularly
95sync the `dts/upstream/` subtree from the devicetree-rebasing repo whenever
96the next branch opens (refer: :doc:`../release_cycle`) with the latest mainline
97Linux kernel release. To sync the `dts/upstream/` subtree, run::
98
99 ./dts/update-dts-subtree.sh pull <devicetree-rebasing-release-tag>
100
101If required it is also possible to cherry-pick fixes from the
102devicetree-rebasing repository prior to next sync, usage::
103
104 ./dts/update-dts-subtree.sh pick <devicetree-rebasing-commit-id>
105
Simon Glass38d6b8d2011-10-15 05:48:21 +0000106
107Configuration
108-------------
109
Sumit Garg7a87a442024-02-22 15:06:04 +0530110SoC/board maintainers are encouraged to migrate to use synced copies from
111`dts/upstream/src/<arch>/<vendor>`. To do that add `imply OF_UPSTREAM` for the
112SoC being used via Kconfig and set `DEFAULT_DEVICE_TREE=<vendor>/<name>` when
113prompted by Kconfig.
Simon Glass38d6b8d2011-10-15 05:48:21 +0000114
Sumit Garg7a87a442024-02-22 15:06:04 +0530115However, if `dts/upstream/` hasn't yet received devicetree source file for your
Tom Rini916f7382024-05-17 11:49:30 -0600116newly added board support then one option is that you can add the corresponding
117devicetree source file as `arch/<arch>/dts/<name>.dts`. To select that add `#
118CONFIG_OF_UPSTREAM is not set` and set `DEFAULT_DEVICE_TREE=<name>` when
119prompted by Kconfig. Another option is that you can use use the "pick" option of
120`dts/update-dts-subtree.sh` mentioned above to bring in the commits that you
121need.
Simon Glass38d6b8d2011-10-15 05:48:21 +0000122
Sumit Garg7a87a442024-02-22 15:06:04 +0530123This should include your CPU or SoC's devicetree file. On top of that any U-Boot
Sumit Garg879c61c2024-03-01 18:54:53 +0530124specific tweaks (see: :ref:`dttweaks`) can be made for your board.
Simon Glass38d6b8d2011-10-15 05:48:21 +0000125
Sumit Garg86f4ccc2024-02-22 15:06:03 +0530126If `OF_EMBED` is selected by Kconfig, then it will be picked up and built into
Simon Glass67bce6b2014-06-02 22:04:50 -0600127the U-Boot image (including u-boot.bin). This is suitable for debugging
128and development only and is not recommended for production devices.
Simon Glass38d6b8d2011-10-15 05:48:21 +0000129
Sumit Garg86f4ccc2024-02-22 15:06:03 +0530130If `OF_SEPARATE` is selected by Kconfig, then it will be built and placed in
Simon Glassb4fbaaf2021-08-01 18:57:11 -0600131a u-boot.dtb file alongside u-boot-nodtb.bin with the combined result placed
Sumit Garg86f4ccc2024-02-22 15:06:03 +0530132in u-boot.bin so you can still just flash u-boot.bin onto your board. If Kconfig
133option `SPL_FRAMEWORK` is enabled, then u-boot.img will be built to include the
134device tree binary.
Simon Glass38d6b8d2011-10-15 05:48:21 +0000135
Sumit Garg86f4ccc2024-02-22 15:06:03 +0530136If `OF_BOARD` is selected by Kconfig, a board-specific routine will provide the
Simon Glassb4fbaaf2021-08-01 18:57:11 -0600137devicetree at runtime, for example if an earlier bootloader stage creates
Alex Deymo5b661ec2017-04-02 01:25:20 -0700138it and passes it to U-Boot.
139
Sumit Garg86f4ccc2024-02-22 15:06:03 +0530140If `BLOBLIST` is selected by Kconfig, the devicetree may come from a bloblist
141passed from a previous stage, if present.
Simon Glass4a30a572024-01-03 18:49:19 -0700142
Sumit Garg86f4ccc2024-02-22 15:06:03 +0530143If `SANDBOX` is selected by Kconfig, then it will be read from a file on
Ilias Apalodimasab5348a2021-10-26 09:12:33 +0300144startup. Use the -d flag to U-Boot to specify the file to read, -D for the
145default and -T for the test devicetree, used to run sandbox unit tests.
Simon Glass15393432013-04-20 08:42:41 +0000146
147You cannot use more than one of these options at the same time.
Simon Glass38d6b8d2011-10-15 05:48:21 +0000148
Simon Glassb4fbaaf2021-08-01 18:57:11 -0600149To use a devicetree file that you have compiled yourself, pass
Simon Glassac35c2f2021-08-01 18:57:10 -0600150EXT_DTB=<filename> to 'make', as in::
Simon Glass67bce6b2014-06-02 22:04:50 -0600151
Simon Glassac35c2f2021-08-01 18:57:10 -0600152 make EXT_DTB=boot/am335x-boneblack-pubkey.dtb
Simon Glass67bce6b2014-06-02 22:04:50 -0600153
154Then U-Boot will copy that file to u-boot.dtb, put it in the .img file
155if used, and u-boot-dtb.bin.
156
Simon Glassdc6fa642011-10-24 19:15:34 +0000157If you wish to put the fdt at a different address in memory, you can
158define the "fdtcontroladdr" environment variable. This is the hex
159address of the fdt binary blob, and will override either of the options.
160Be aware that this environment variable is checked prior to relocation,
161when only the compiled-in environment is available. Therefore it is not
162possible to define this variable in the saved SPI/NAND flash
Thomas Chou4fda2812015-10-16 08:44:51 +0800163environment, for example (it will be ignored). After relocation, this
164variable will be set to the address of the newly relocated fdt blob.
165It is read-only and cannot be changed. It can optionally be used to
166control the boot process of Linux with bootm/bootz commands.
Simon Glassdc6fa642011-10-24 19:15:34 +0000167
Simon Glassac35c2f2021-08-01 18:57:10 -0600168To use this, put something like this in your board header file::
Simon Glassdc6fa642011-10-24 19:15:34 +0000169
Tom Rinic9edebe2022-12-04 10:03:50 -0500170 #define CFG_EXTRA_ENV_SETTINGS "fdtcontroladdr=10000\0"
Simon Glassdc6fa642011-10-24 19:15:34 +0000171
Jagannadha Sutradharudu Teki79e63a42013-02-28 10:20:18 +0000172Build:
173
Simon Glassb4fbaaf2021-08-01 18:57:11 -0600174After the board configuration is done, fdt supported u-boot can be built in two
Simon Glassac35c2f2021-08-01 18:57:10 -0600175ways:
176
Sumit Garg86f4ccc2024-02-22 15:06:03 +0530177# build the default dts which is selected by DEFAULT_DEVICE_TREE Kconfig::
Simon Glassac35c2f2021-08-01 18:57:10 -0600178
Jagannadha Sutradharudu Teki79e63a42013-02-28 10:20:18 +0000179 $ make
Simon Glassac35c2f2021-08-01 18:57:10 -0600180
181# build the user specified dts file::
182
Jagannadha Sutradharudu Teki79e63a42013-02-28 10:20:18 +0000183 $ make DEVICE_TREE=<dts-file-name>
184
Simon Glassb4fbaaf2021-08-01 18:57:11 -0600185
Simon Glassc5f9e162021-08-01 18:57:12 -0600186.. _dttweaks:
187
Simon Glassb4fbaaf2021-08-01 18:57:11 -0600188Adding tweaks for U-Boot
189------------------------
190
Sumit Garg7a87a442024-02-22 15:06:04 +0530191With `dts/upstream` Git subtree, it is ensured that devicetree files in U-Boot
192are an exact copy of those in Linux kernel available under
193`dts/upstream/src/<arch>/<vendor>`.
Simon Glassb4fbaaf2021-08-01 18:57:11 -0600194
195U-Boot is of course a very different project from Linux, e.g. it operates under
196much more restrictive memory and code-size constraints. Where Linux may use a
197full clock driver with Common Clock Format (CCF) to find the input clock to the
198UART, U-Boot typically wants to output a banner as early as possible before too
199much code has run.
200
201A second difference is that U-Boot includes different phases. For SPL,
202constraints are even more extreme and the devicetree is shrunk to remove
203unwanted nodes, or even turned into C code to avoid access overhead.
204
205U-Boot automatically looks for and includes a file with updates to the standard
Sumit Garg7a87a442024-02-22 15:06:04 +0530206devicetree for your board, searching for them in `arch/<arch>/dts/` in this
207order::
Simon Glassb4fbaaf2021-08-01 18:57:11 -0600208
209 <orig_filename>-u-boot.dtsi
210 <CONFIG_SYS_SOC>-u-boot.dtsi
211 <CONFIG_SYS_CPU>-u-boot.dtsi
212 <CONFIG_SYS_VENDOR>-u-boot.dtsi
213 u-boot.dtsi
214
215Only one of these is selected but of course you can #include another one within
216that file, to create a hierarchy of shared files.
Simon Glass38d6b8d2011-10-15 05:48:21 +0000217
Rasmus Villemoes303d98d2021-11-21 14:52:51 +0100218
219External .dtsi fragments
220------------------------
221
222Apart from describing the hardware present, U-Boot also uses its
223control dtb for various configuration purposes. For example, the
224public key(s) used for Verified Boot are embedded in a specific format
225in a /signature node.
226
227As mentioned above, the U-Boot build system automatically includes a
228`*-u-boot.dtsi` file, if found, containing U-Boot specific
229quirks. However, some data, such as the mentioned public keys, are not
230appropriate for upstream U-Boot but are better kept and maintained
Sumit Garg86f4ccc2024-02-22 15:06:03 +0530231outside the U-Boot repository. You can use `DEVICE_TREE_INCLUDES` Kconfig
232option to specify a list of .dtsi files that will also be included when
Rasmus Villemoes303d98d2021-11-21 14:52:51 +0100233building .dtb files.
234
Sumit Garg7a87a442024-02-22 15:06:04 +0530235
236Devicetree bindings schema checks
237---------------------------------
238
239With devicetee-rebasing Git subtree, the devicetree bindings are also regularly
240synced with Linux kernel as `dts/upstream/Bindings/` sub-directory. This
241allows U-Boot to run devicetree bindings schema checks which will bring
242compliance to U-Boot core/drivers regarding usage of devicetree.
243
244Dependencies
245~~~~~~~~~~~~
246
247The DT schema project must be installed in order to validate the DT schema
248binding documents and validate DTS files using the DT schema. For installation
Sumit Garg879c61c2024-03-01 18:54:53 +0530249instructions, refer to the `DT schema project page`_.
Sumit Garg7a87a442024-02-22 15:06:04 +0530250
251Several executables (dt-doc-validate, dt-mk-schema, dt-validate) will be
252installed. Ensure they are in your PATH (~/.local/bin by default).
253
254You should also install yamllint (used by dtschema when present). On Debian/
255Ubuntu systems::
256
257 apt install yamllint
258
259Running checks
260~~~~~~~~~~~~~~
261
262In order to perform validation of DTB files, use the ``dtbs_check`` target::
263
264 make dtbs_check
265
266It is also possible to run checks with a subset of matching schema files by
267setting the ``DT_SCHEMA_FILES`` variable to 1 or more specific schema files or
268patterns (partial match of a fixed string). Each file or pattern should be
269separated by ':'.
270
271::
272
273 make dtbs_check DT_SCHEMA_FILES=trivial-devices.yaml:rtc.yaml
274 make dtbs_check DT_SCHEMA_FILES=/gpio/
275 make dtbs_check DT_SCHEMA_FILES=trivial-devices.yaml
276
Rasmus Villemoes303d98d2021-11-21 14:52:51 +0100277
Simon Glassa31dc3d2018-10-01 12:22:17 -0600278Relocation, SPL and TPL
279-----------------------
280
281U-Boot can be divided into three phases: TPL, SPL and U-Boot proper.
282
Simon Glassb4fbaaf2021-08-01 18:57:11 -0600283The full devicetree is available to U-Boot proper, but normally only a subset
Simon Glassa31dc3d2018-10-01 12:22:17 -0600284(or none at all) is available to TPL and SPL. See 'Pre-Relocation Support' and
Heinrich Schuchardtc79f03c2020-02-25 21:35:39 +0100285'SPL Support' in doc/driver-model/design.rst for more details.
Simon Glassa31dc3d2018-10-01 12:22:17 -0600286
287
Sumit Garg86f4ccc2024-02-22 15:06:03 +0530288Using several DTBs in the SPL (SPL_MULTI_DTB_FIT Kconfig option)
289----------------------------------------------------------------
Jean-Jacques Hiblot7c530e32018-12-07 14:50:52 +0100290In some rare cases it is desirable to let SPL be able to select one DTB among
291many. This usually not very useful as the DTB for the SPL is small and usually
292fits several platforms. However the DTB sometimes include information that do
293work on several platforms (like IO tuning parameters).
Sumit Garg86f4ccc2024-02-22 15:06:03 +0530294In this case it is possible to use SPL_MULTI_DTB_FIT Kconfig option. This option
295appends to the SPL a FIT image containing several DTBs listed in SPL_OF_LIST.
Jean-Jacques Hiblot7c530e32018-12-07 14:50:52 +0100296board_fit_config_name_match() is called to select the right DTB.
297
298If board_fit_config_name_match() relies on DM (DM driver to access an EEPROM
299containing the board ID for example), it possible to start with a generic DTB
300and then switch over to the right DTB after the detection. For this purpose,
301the platform code must call fdtdec_resetup(). Based on the returned flag, the
Simon Glassb4fbaaf2021-08-01 18:57:11 -0600302platform may have to re-initialise the DM subsystem using dm_uninit() and
Jean-Jacques Hiblot7c530e32018-12-07 14:50:52 +0100303dm_init_and_scan().
304
305
Simon Glass38d6b8d2011-10-15 05:48:21 +0000306Limitations
307-----------
308
Simon Glassb4fbaaf2021-08-01 18:57:11 -0600309Devicetrees can help reduce the complexity of supporting variants of boards
310which use the same SOC / CPU.
311
312However U-Boot is designed to build for a single architecture type and CPU
Simon Glass38d6b8d2011-10-15 05:48:21 +0000313type. So for example it is not possible to build a single ARM binary
314which runs on your AT91 and OMAP boards, relying on an fdt to configure
315the various features. This is because you must select one of
316the CPU families within arch/arm/cpu/arm926ejs (omap or at91) at build
Simon Glassb4fbaaf2021-08-01 18:57:11 -0600317time. Similarly U-Boot cannot be built for multiple cpu types or
Simon Glass38d6b8d2011-10-15 05:48:21 +0000318architectures.
319
Simon Glass38d6b8d2011-10-15 05:48:21 +0000320It is important to understand that the fdt only selects options
321available in the platform / drivers. It cannot add new drivers (yet). So
Sumit Garg86f4ccc2024-02-22 15:06:03 +0530322you must still have the Kconfig option to enable the driver. For example,
323you need to enable SYS_NS16550 Kconfig option to bring in the NS16550 driver,
Simon Glass38d6b8d2011-10-15 05:48:21 +0000324but can use the fdt to specific the UART clock, peripheral address, etc.
Sumit Garg86f4ccc2024-02-22 15:06:03 +0530325In very broad terms, the Kconfig options in general control *what* driver
Simon Glass38d6b8d2011-10-15 05:48:21 +0000326files are pulled in, and the fdt controls *how* those files work.
327
Simon Glassb4fbaaf2021-08-01 18:57:11 -0600328History
329-------
330
331U-Boot configuration was previous done using CONFIG options in the board
332config file. This eventually got out of hand with nearly 10,000 options.
333
334U-Boot adopted devicetrees around the same time as Linux and early boards
335used it before Linux (e.g. snow). The two projects developed in parallel
336and there are still some differences in the bindings for certain boards.
337While there has been discussion of having a separate repository for devicetree
338files, in practice the Linux kernel Git repository has become the place where
Sumit Garg879c61c2024-03-01 18:54:53 +0530339these are stored, with U-Boot taking copies via
340`devicetree-rebasing repository`_ and adding tweaks with u-boot.dtsi files.
Simon Glassb4fbaaf2021-08-01 18:57:11 -0600341
Sumit Garg879c61c2024-03-01 18:54:53 +0530342.. _the devicetree specification: https://www.devicetree.org/specifications/
343.. _devicetree compiler mailing list: https://www.spinics.net/lists/devicetree-compiler/
344.. _devicetree-rebasing repository: https://git.kernel.org/pub/scm/linux/kernel/git/devicetree/devicetree-rebasing.git
345.. _DT schema project page: https://github.com/devicetree-org/dt-schema/tree/main