Andy Gross | 252da09 | 2017-05-24 00:24:50 -0500 | [diff] [blame] | 1 | .. _device-tree: |
| 2 | |
| 3 | Device Tree in Zephyr |
| 4 | ######################## |
| 5 | |
| 6 | Introduction to Device Tree |
| 7 | *************************** |
| 8 | |
| 9 | Device tree is a way of describing hardware and configuration information |
| 10 | for boards. Device tree was adopted for use in the Linux kernel for the |
| 11 | PowerPC architecture. However, it is now in use for ARM and other |
| 12 | architectures. |
| 13 | |
| 14 | The device tree is a data structure for dynamically describing hardware |
| 15 | using a Device Tree Source (DTS) data structure language, and compiled |
| 16 | into a compact Device Tree Blob (DTB) using a Device Tree Compiler (DTC). |
| 17 | Rather than hard coding every detail of a board's hardware into the |
| 18 | operating system, the hardware-describing DTB is passed to the operating |
| 19 | system at boot time. This allows the same compiled Linux kernel to support |
| 20 | different hardware configurations within an architecture family (e.g., ARM, |
David B. Kinder | 547c74c | 2017-06-13 15:16:40 -0700 | [diff] [blame] | 21 | x86, PowerPC) and moves a significant part of the hardware description out of |
Andy Gross | 252da09 | 2017-05-24 00:24:50 -0500 | [diff] [blame] | 22 | the kernel binary itself. |
| 23 | |
| 24 | Traditional usage of device tree involves storing of the Device Tree Blob. |
| 25 | The DTB is then used during runtime for configuration of device drivers. In |
| 26 | Zephyr, the DTS information will be used only during compile time. |
| 27 | Information about the system is extracted from the compiled DTS and used to |
| 28 | create the application image. |
| 29 | |
| 30 | Device tree uses a specific format to describe the device nodes in a system. |
| 31 | This format is described in `EPAPR document`_. |
| 32 | |
| 33 | .. _EPAPR document: http://www.devicetree.org/specifications-pdf |
| 34 | |
| 35 | More 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 | |
| 40 | System build requirements |
| 41 | ************************* |
| 42 | |
| 43 | The Zephyr device tree feature requires a device tree compiler (DTC) and Python |
David B. Kinder | 2670bd6 | 2017-06-19 10:13:50 -0700 | [diff] [blame] | 44 | YAML packages. Refer to the installation guide for your specific host OS: |
Andy Gross | 252da09 | 2017-05-24 00:24:50 -0500 | [diff] [blame] | 45 | |
David B. Kinder | 2670bd6 | 2017-06-19 10:13:50 -0700 | [diff] [blame] | 46 | * :ref:`installing_zephyr_win` |
| 47 | * :ref:`installation_linux` |
| 48 | * :ref:`installing_zephyr_mac` |
Andy Gross | 252da09 | 2017-05-24 00:24:50 -0500 | [diff] [blame] | 49 | |
| 50 | |
| 51 | Zephyr and Device Tree |
| 52 | ********************** |
| 53 | |
| 54 | In Zephyr, device tree is used to not only describe hardware, but also to |
| 55 | describe Zephyr-specific configuration information. The Zephyr-specific |
| 56 | information is intended to augment the device tree descriptions. The main |
| 57 | reason for this is to leverage existing device tree files that a SoC vendor may |
| 58 | already have defined for a given platform. |
| 59 | |
| 60 | Today, configuration in Zephyr comes from a number of different places. It can |
| 61 | come from Kconfig files, CMSIS header files, vendor header files, prj.conf |
| 62 | files, and other miscellaneous sources. The intent of using device tree is to |
| 63 | replace or curtail the use of Kconfig files throughout the system, and instead |
| 64 | use device tree files to describe the configuration of device nodes. CMSIS and |
| 65 | vendor header files can be used in conjunction with the device tree to fully |
| 66 | describe hardware. Device tree is not intended to replace CMSIS or vendor |
| 67 | include files. |
| 68 | |
| 69 | The device tree files are compiled using the device tree compiler. The compiler |
| 70 | runs the .dts file through the C preprocessor to resolve any macro or #defines |
| 71 | utilized in the file. The output of the compile is another dts formatted file. |
| 72 | |
| 73 | After compilation, a python script extracts information from the compiled device |
| 74 | tree file using a set of rules specified in YAML files. The extracted |
| 75 | information is placed in a header file that is used by the rest of the code as |
| 76 | the project is compiled. |
| 77 | |
| 78 | A temporary fixup file is required for device tree support on most devices. |
Kumar Gala | 2b50be6 | 2017-12-01 11:33:17 -0600 | [diff] [blame^] | 79 | This .fixup file by default resides in the board directory and is named |
| 80 | dts.fixup. This fixup file maps the generated include information to the |
| 81 | current driver/source usage. |
Andy Gross | 252da09 | 2017-05-24 00:24:50 -0500 | [diff] [blame] | 82 | |
| 83 | Device tree file formats |
| 84 | ************************ |
David B. Kinder | 2670bd6 | 2017-06-19 10:13:50 -0700 | [diff] [blame] | 85 | |
Andy Gross | 252da09 | 2017-05-24 00:24:50 -0500 | [diff] [blame] | 86 | Hardware and software is described inside of device tree files in clear text format. |
| 87 | These files have the file suffix of .dtsi or .dts. The .dtsi files are meant to |
| 88 | be 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 |
| 90 | a given SoC family. |
| 91 | |
Andy Gross | 252da09 | 2017-05-24 00:24:50 -0500 | [diff] [blame] | 92 | Example: FRDM K64F Board and Hexiwear K64 |
David B. Kinder | 2670bd6 | 2017-06-19 10:13:50 -0700 | [diff] [blame] | 93 | ========================================= |
| 94 | |
Andy Gross | 252da09 | 2017-05-24 00:24:50 -0500 | [diff] [blame] | 95 | Both of these boards are based on the same NXP Kinetis SoC family, the K6X. The |
| 96 | following shows the include hierarchy for both boards. |
| 97 | |
Kumar Gala | 2b50be6 | 2017-12-01 11:33:17 -0600 | [diff] [blame^] | 98 | boards/arm/frdm_k64f/frdm_k64f.dts includes the following files:: |
Andy Gross | 252da09 | 2017-05-24 00:24:50 -0500 | [diff] [blame] | 99 | |
| 100 | dts/arm/nxp/nxp_k6x.dtsi |
| 101 | dts/arm/armv7-m.dtsi |
| 102 | |
Kumar Gala | 2b50be6 | 2017-12-01 11:33:17 -0600 | [diff] [blame^] | 103 | boards/arm/hexiwear_k64/hexiwear_k64.dts includes the same files:: |
Andy Gross | 252da09 | 2017-05-24 00:24:50 -0500 | [diff] [blame] | 104 | |
| 105 | dts/arm/nxp/nxp_k6x.dtsi |
| 106 | dts/arm/armv7-m.dtsi |
| 107 | |
| 108 | The board-specific .dts files enable nodes, define the Zephyr-specific items, |
| 109 | and also adds board-specific changes like gpio/pinmux assignments. These types |
| 110 | of things will vary based on the board layout and application use. |
| 111 | |
| 112 | Currently supported boards |
| 113 | ************************** |
| 114 | |
| 115 | Device tree is currently supported on all ARM targets. Support for all other |
Kumar Gala | 2b50be6 | 2017-12-01 11:33:17 -0600 | [diff] [blame^] | 116 | architectures is to be completed by release 1.11. |
Andy Gross | 252da09 | 2017-05-24 00:24:50 -0500 | [diff] [blame] | 117 | |
| 118 | Adding support for a board |
| 119 | ************************** |
| 120 | |
| 121 | Adding device tree support for a given board requires adding a number of files. |
| 122 | These files will contain the DTS information that describes a platform, the |
| 123 | YAML descriptions that define the contents of a given Device Tree peripheral |
| 124 | node, and also any fixup files required to support the platform. |
| 125 | |
| 126 | When writing Device Tree Source files, it is good to separate out common |
| 127 | peripheral information that could be used across multiple SoC families or |
| 128 | boards. DTS files are identified by their file suffix. A .dtsi suffix denotes |
| 129 | a DTS file that is used as an include in another DTS file. A .dts suffix |
| 130 | denotes the primary DTS file for a given board. |
| 131 | |
| 132 | The primary DTS file will contain at a minimum a version line, optional |
| 133 | includes, and the root node definition. The root node will contain a model and |
| 134 | compatible that denotes the unique board described by the .dts file. |
| 135 | |
Andy Gross | 252da09 | 2017-05-24 00:24:50 -0500 | [diff] [blame] | 136 | Device Tree Source File Template |
David B. Kinder | 2670bd6 | 2017-06-19 10:13:50 -0700 | [diff] [blame] | 137 | ================================ |
Andy Gross | 252da09 | 2017-05-24 00:24:50 -0500 | [diff] [blame] | 138 | |
David B. Kinder | 2670bd6 | 2017-06-19 10:13:50 -0700 | [diff] [blame] | 139 | .. code-block:: yaml |
Andy Gross | 252da09 | 2017-05-24 00:24:50 -0500 | [diff] [blame] | 140 | |
| 141 | /dts-v1/ |
| 142 | / { |
| 143 | model = "Model name for your board"; |
| 144 | compatible = "compatible for your board"; |
| 145 | /* rest of file */ |
| 146 | }; |
| 147 | |
| 148 | |
| 149 | One suggestion for starting from scratch on a platform/board is to examine other |
| 150 | boards and their device tree source files. |
| 151 | |
| 152 | The 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 Gross | 252da09 | 2017-05-24 00:24:50 -0500 | [diff] [blame] | 158 | * 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 Gala | 2b50be6 | 2017-12-01 11:33:17 -0600 | [diff] [blame^] | 160 | * Add target specific YAML files in the dts/bindings/ directory. |
Andy Gross | 252da09 | 2017-05-24 00:24:50 -0500 | [diff] [blame] | 161 | 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 | |
| 186 | Adding support for device tree in drivers |
| 187 | ***************************************** |
| 188 | |
| 189 | As drivers and other source code is converted over to make use of device tree |
| 190 | generated information, these drivers may require changes to match the generated |
| 191 | #define information. |
| 192 | |
| 193 | |
| 194 | Source Tree Hierarchy |
| 195 | ********************* |
| 196 | |
| 197 | The device tree files are located in a couple of different directories. The |
| 198 | directory split is done based on architecture, and there is also a common |
| 199 | directory where architecture agnostic device tree and yaml files are located. |
| 200 | |
| 201 | Assuming the current working directory is the ZEPHYR_BASE, the directory |
| 202 | hierarchy looks like the following:: |
| 203 | |
| 204 | dts/common/ |
Andy Gross | 252da09 | 2017-05-24 00:24:50 -0500 | [diff] [blame] | 205 | dts/<ARCH>/ |
Kumar Gala | 2b50be6 | 2017-12-01 11:33:17 -0600 | [diff] [blame^] | 206 | dts/bindings/ |
| 207 | boards/<ARCH>/<BOARD>/ |
Andy Gross | 252da09 | 2017-05-24 00:24:50 -0500 | [diff] [blame] | 208 | |
| 209 | The common directories contain a skeleton.dtsi include file that defines the |
| 210 | address and size cells. The yaml subdirectory contains common yaml files for |
| 211 | Zephyr-specific nodes/properties and generic device properties common across |
| 212 | architectures. |
| 213 | |
Kumar Gala | 2b50be6 | 2017-12-01 11:33:17 -0600 | [diff] [blame^] | 214 | Example: Subset of DTS/YAML files for NXP FRDM K64F (Subject to Change):: |
Andy Gross | 252da09 | 2017-05-24 00:24:50 -0500 | [diff] [blame] | 215 | |
| 216 | dts/arm/armv7-m.dtsi |
| 217 | dts/arm/k6x/nxp_k6x.dtsi |
Kumar Gala | 2b50be6 | 2017-12-01 11:33:17 -0600 | [diff] [blame^] | 218 | 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 Gross | 252da09 | 2017-05-24 00:24:50 -0500 | [diff] [blame] | 223 | |
| 224 | YAML definitions for device nodes |
| 225 | ********************************* |
| 226 | |
| 227 | Device tree can describe hardware and configuration, but it doesn't tell the |
| 228 | system which pieces of information are useful, or how to generate configuration |
| 229 | data from the device tree nodes. For this, we rely on YAML files to describe |
| 230 | the contents or definition of a device tree node. |
| 231 | |
| 232 | A YAML description must be provided for every device node that is to be a source |
| 233 | of information for the system. This YAML description can be used for more than |
| 234 | one purpose. It can be used in conjunction with the device tree to generate |
| 235 | include information. It can also be used to validate the device tree files |
| 236 | themselves. A device tree file can successfully compile and still not contain |
| 237 | the necessary pieces required to build the rest of the software. YAML provides |
| 238 | a means to detect that issue. |
| 239 | |
| 240 | YAML files reside in a subdirectory inside the common and architecture-specific |
| 241 | device tree directories. A YAML template file is provided to show the required |
David B. Kinder | 2670bd6 | 2017-06-19 10:13:50 -0700 | [diff] [blame] | 242 | format. This file is located at:: |
Andy Gross | 252da09 | 2017-05-24 00:24:50 -0500 | [diff] [blame] | 243 | |
Kumar Gala | 2b50be6 | 2017-12-01 11:33:17 -0600 | [diff] [blame^] | 244 | dts/bindings/device_node.yaml.template |
Andy Gross | 252da09 | 2017-05-24 00:24:50 -0500 | [diff] [blame] | 245 | |
| 246 | YAML files must end in a .yaml suffix. YAML files are scanned during the |
| 247 | information extraction phase and are matched to device tree nodes via the |
| 248 | compatible property. |