blob: 0a11d7b3f85414aedfd0f8c7a3d75c729b4755fc [file] [log] [blame]
Andy Gross252da092017-05-24 00:24:50 -05001.. _device-tree:
2
3Device Tree in Zephyr
4########################
5
6Introduction to Device Tree
7***************************
8
9Device tree is a way of describing hardware and configuration information
10for boards. Device tree was adopted for use in the Linux kernel for the
11PowerPC architecture. However, it is now in use for ARM and other
12architectures.
13
14The device tree is a data structure for dynamically describing hardware
15using a Device Tree Source (DTS) data structure language, and compiled
16into a compact Device Tree Blob (DTB) using a Device Tree Compiler (DTC).
17Rather than hard coding every detail of a board's hardware into the
18operating system, the hardware-describing DTB is passed to the operating
19system at boot time. This allows the same compiled Linux kernel to support
20different hardware configurations within an architecture family (e.g., ARM,
David B. Kinder547c74c2017-06-13 15:16:40 -070021x86, PowerPC) and moves a significant part of the hardware description out of
Andy Gross252da092017-05-24 00:24:50 -050022the kernel binary itself.
23
24Traditional usage of device tree involves storing of the Device Tree Blob.
25The DTB is then used during runtime for configuration of device drivers. In
26Zephyr, the DTS information will be used only during compile time.
27Information about the system is extracted from the compiled DTS and used to
28create the application image.
29
30Device tree uses a specific format to describe the device nodes in a system.
31This format is described in `EPAPR document`_.
32
33.. _EPAPR document: http://www.devicetree.org/specifications-pdf
34
35More device tree information can be found at the `device tree repository`_.
36
37.. _device tree repository: https://git.kernel.org/pub/scm/utils/dtc/dtc.git
38
39
40System build requirements
41*************************
42
43The Zephyr device tree feature requires a device tree compiler (DTC) and Python
David B. Kinder2670bd62017-06-19 10:13:50 -070044YAML packages. Refer to the installation guide for your specific host OS:
Andy Gross252da092017-05-24 00:24:50 -050045
David B. Kinder2670bd62017-06-19 10:13:50 -070046* :ref:`installing_zephyr_win`
47* :ref:`installation_linux`
48* :ref:`installing_zephyr_mac`
Andy Gross252da092017-05-24 00:24:50 -050049
50
51Zephyr and Device Tree
52**********************
53
54In Zephyr, device tree is used to not only describe hardware, but also to
55describe Zephyr-specific configuration information. The Zephyr-specific
56information is intended to augment the device tree descriptions. The main
57reason for this is to leverage existing device tree files that a SoC vendor may
58already have defined for a given platform.
59
60Today, configuration in Zephyr comes from a number of different places. It can
61come from Kconfig files, CMSIS header files, vendor header files, prj.conf
62files, and other miscellaneous sources. The intent of using device tree is to
63replace or curtail the use of Kconfig files throughout the system, and instead
64use device tree files to describe the configuration of device nodes. CMSIS and
65vendor header files can be used in conjunction with the device tree to fully
66describe hardware. Device tree is not intended to replace CMSIS or vendor
67include files.
68
69The device tree files are compiled using the device tree compiler. The compiler
70runs the .dts file through the C preprocessor to resolve any macro or #defines
71utilized in the file. The output of the compile is another dts formatted file.
72
73After compilation, a python script extracts information from the compiled device
74tree file using a set of rules specified in YAML files. The extracted
75information is placed in a header file that is used by the rest of the code as
76the project is compiled.
77
78A temporary fixup file is required for device tree support on most devices.
Kumar Gala2b50be62017-12-01 11:33:17 -060079This .fixup file by default resides in the board directory and is named
80dts.fixup. This fixup file maps the generated include information to the
81current driver/source usage.
Andy Gross252da092017-05-24 00:24:50 -050082
83Device tree file formats
84************************
David B. Kinder2670bd62017-06-19 10:13:50 -070085
Andy Gross252da092017-05-24 00:24:50 -050086Hardware and software is described inside of device tree files in clear text format.
87These files have the file suffix of .dtsi or .dts. The .dtsi files are meant to
88be included by other files. Typically for a given board you have some number of
89.dtsi include files that pull in common device descriptions that are used across
90a given SoC family.
91
Andy Gross252da092017-05-24 00:24:50 -050092Example: FRDM K64F Board and Hexiwear K64
David B. Kinder2670bd62017-06-19 10:13:50 -070093=========================================
94
Andy Gross252da092017-05-24 00:24:50 -050095Both of these boards are based on the same NXP Kinetis SoC family, the K6X. The
96following shows the include hierarchy for both boards.
97
Kumar Gala2b50be62017-12-01 11:33:17 -060098boards/arm/frdm_k64f/frdm_k64f.dts includes the following files::
Andy Gross252da092017-05-24 00:24:50 -050099
100 dts/arm/nxp/nxp_k6x.dtsi
101 dts/arm/armv7-m.dtsi
102
Kumar Gala2b50be62017-12-01 11:33:17 -0600103boards/arm/hexiwear_k64/hexiwear_k64.dts includes the same files::
Andy Gross252da092017-05-24 00:24:50 -0500104
105 dts/arm/nxp/nxp_k6x.dtsi
106 dts/arm/armv7-m.dtsi
107
108The board-specific .dts files enable nodes, define the Zephyr-specific items,
109and also adds board-specific changes like gpio/pinmux assignments. These types
110of things will vary based on the board layout and application use.
111
112Currently supported boards
113**************************
114
115Device tree is currently supported on all ARM targets. Support for all other
Kumar Gala2b50be62017-12-01 11:33:17 -0600116architectures is to be completed by release 1.11.
Andy Gross252da092017-05-24 00:24:50 -0500117
118Adding support for a board
119**************************
120
121Adding device tree support for a given board requires adding a number of files.
122These files will contain the DTS information that describes a platform, the
123YAML descriptions that define the contents of a given Device Tree peripheral
124node, and also any fixup files required to support the platform.
125
126When writing Device Tree Source files, it is good to separate out common
127peripheral information that could be used across multiple SoC families or
128boards. DTS files are identified by their file suffix. A .dtsi suffix denotes
129a DTS file that is used as an include in another DTS file. A .dts suffix
130denotes the primary DTS file for a given board.
131
132The primary DTS file will contain at a minimum a version line, optional
133includes, and the root node definition. The root node will contain a model and
134compatible that denotes the unique board described by the .dts file.
135
Andy Gross252da092017-05-24 00:24:50 -0500136Device Tree Source File Template
David B. Kinder2670bd62017-06-19 10:13:50 -0700137================================
Andy Gross252da092017-05-24 00:24:50 -0500138
David B. Kinder2670bd62017-06-19 10:13:50 -0700139.. code-block:: yaml
Andy Gross252da092017-05-24 00:24:50 -0500140
141 /dts-v1/
142 / {
143 model = "Model name for your board";
144 compatible = "compatible for your board";
145 /* rest of file */
146 };
147
148
149One suggestion for starting from scratch on a platform/board is to examine other
150boards and their device tree source files.
151
152The following is a more precise list of required files:
153
154* Base architecture support
155
156 * Add architecture-specific DTS directory, if not already present.
157 Example: dts/arm for ARM.
Andy Gross252da092017-05-24 00:24:50 -0500158 * Add target specific device tree files for base SoC. These should be
159 .dtsi files to be included in the board-specific device tree files.
Kumar Gala2b50be62017-12-01 11:33:17 -0600160 * Add target specific YAML files in the dts/bindings/ directory.
Andy Gross252da092017-05-24 00:24:50 -0500161 Create the yaml directory if not present.
162
163* SoC family support
164
165 * Add one or more SoC family .dtsi files that describe the hardware
166 for a set of devices. The file should contain all the relevant
167 nodes and base configuration that would be applicable to all boards
168 utilizing that SoC family.
169 * Add SoC family YAML files that describe the nodes present in the .dtsi file.
170
171* Board specific support
172
173 * Add a board level .dts file that includes the SoC family .dtsi files
174 and enables the nodes required for that specific board.
175 * Board .dts file should specify the SRAM and FLASH devices, if present.
176 * Add board-specific YAML files, if required. This would occur if the
177 board has additional hardware that is not covered by the SoC family
178 .dtsi/.yaml files.
179
180* Fixup files
181
182 * Fixup files contain mappings from existing Kconfig options to the actual
183 underlying DTS derived configuration #defines. Fixup files are temporary
184 artifacts until additional DTS changes are made to make them unnecessary.
185
186Adding support for device tree in drivers
187*****************************************
188
189As drivers and other source code is converted over to make use of device tree
190generated information, these drivers may require changes to match the generated
191#define information.
192
193
194Source Tree Hierarchy
195*********************
196
197The device tree files are located in a couple of different directories. The
198directory split is done based on architecture, and there is also a common
199directory where architecture agnostic device tree and yaml files are located.
200
201Assuming the current working directory is the ZEPHYR_BASE, the directory
202hierarchy looks like the following::
203
204 dts/common/
Andy Gross252da092017-05-24 00:24:50 -0500205 dts/<ARCH>/
Kumar Gala2b50be62017-12-01 11:33:17 -0600206 dts/bindings/
207 boards/<ARCH>/<BOARD>/
Andy Gross252da092017-05-24 00:24:50 -0500208
209The common directories contain a skeleton.dtsi include file that defines the
210address and size cells. The yaml subdirectory contains common yaml files for
211Zephyr-specific nodes/properties and generic device properties common across
212architectures.
213
Kumar Gala2b50be62017-12-01 11:33:17 -0600214Example: Subset of DTS/YAML files for NXP FRDM K64F (Subject to Change)::
Andy Gross252da092017-05-24 00:24:50 -0500215
216 dts/arm/armv7-m.dtsi
217 dts/arm/k6x/nxp_k6x.dtsi
Kumar Gala2b50be62017-12-01 11:33:17 -0600218 boards/arm/frdm_k64f/frdm_k64f.dts
219 dts/bindings/interrupt-controller/arm,v7m-nvic.yaml
220 dts/bindings/gpio/nxp,kinetis-gpio.yaml
221 dts/bindings/pinctrl/nxp,kinetis-pinmux.yaml
222 dts/bindings/serial/nxp,kinetis-uart.yaml
Andy Gross252da092017-05-24 00:24:50 -0500223
224YAML definitions for device nodes
225*********************************
226
227Device tree can describe hardware and configuration, but it doesn't tell the
228system which pieces of information are useful, or how to generate configuration
229data from the device tree nodes. For this, we rely on YAML files to describe
230the contents or definition of a device tree node.
231
232A YAML description must be provided for every device node that is to be a source
233of information for the system. This YAML description can be used for more than
234one purpose. It can be used in conjunction with the device tree to generate
235include information. It can also be used to validate the device tree files
236themselves. A device tree file can successfully compile and still not contain
237the necessary pieces required to build the rest of the software. YAML provides
238a means to detect that issue.
239
240YAML files reside in a subdirectory inside the common and architecture-specific
241device tree directories. A YAML template file is provided to show the required
David B. Kinder2670bd62017-06-19 10:13:50 -0700242format. This file is located at::
Andy Gross252da092017-05-24 00:24:50 -0500243
Kumar Gala2b50be62017-12-01 11:33:17 -0600244 dts/bindings/device_node.yaml.template
Andy Gross252da092017-05-24 00:24:50 -0500245
246YAML files must end in a .yaml suffix. YAML files are scanned during the
247information extraction phase and are matched to device tree nodes via the
248compatible property.