1.. _dt-syntax: 2 3Syntax and structure 4#################### 5 6As the name indicates, a devicetree is a tree. The human-readable text format 7for this tree is called DTS (for devicetree source), and is defined in the 8`Devicetree specification`_. 9 10.. _Devicetree specification: https://www.devicetree.org/ 11 12This page's purpose is to introduce devicetree in a more gradual way than the 13specification. However, you may still need to refer to the specification to 14understand some detailed cases. 15 16.. contents:: Contents 17 :local: 18 19Example 20******* 21 22Here is an example DTS file: 23 24.. code-block:: devicetree 25 26 /dts-v1/; 27 28 / { 29 a-node { 30 subnode_nodelabel: a-sub-node { 31 foo = <3>; 32 }; 33 }; 34 }; 35 36The ``/dts-v1/;`` line means the file's contents are in version 1 of the DTS 37syntax, which has replaced a now-obsolete "version 0". 38 39Nodes 40***** 41 42Like any tree data structure, a devicetree has a hierarchy of *nodes*. 43The above tree has three nodes: 44 45#. A root node: ``/`` 46#. A node named ``a-node``, which is a child of the root node 47#. A node named ``a-sub-node``, which is a child of ``a-node`` 48 49.. _dt-node-labels: 50 51Nodes can be assigned *node labels*, which are unique shorthands that refer to 52the labeled node. Above, ``a-sub-node`` has the node label 53``subnode_nodelabel``. A node can have zero, one, or multiple node labels. You 54can use node labels to refer to the node elsewhere in the devicetree. 55 56Devicetree nodes have *paths* identifying their locations in the tree. Like 57Unix file system paths, devicetree paths are strings separated by slashes 58(``/``), and the root node's path is a single slash: ``/``. Otherwise, each 59node's path is formed by concatenating the node's ancestors' names with the 60node's own name, separated by slashes. For example, the full path to 61``a-sub-node`` is ``/a-node/a-sub-node``. 62 63Properties 64********** 65 66Devicetree nodes can also have *properties*. Properties are name/value pairs. 67Property values can be any sequence of bytes. In some cases, the values are an 68array of what are called *cells*. A cell is just a 32-bit unsigned integer. 69 70Node ``a-sub-node`` has a property named ``foo``, whose value is a cell with 71value 3. The size and type of ``foo``\ 's value are implied by the enclosing 72angle brackets (``<`` and ``>``) in the DTS. 73 74See :ref:`dt-writing-property-values` below for more example property values. 75 76Devicetrees reflect hardware 77**************************** 78 79In practice, devicetree nodes usually correspond to some hardware, and the node 80hierarchy reflects the hardware's physical layout. For example, let's consider 81a board with three I2C peripherals connected to an I2C bus controller on an SoC, 82like this: 83 84.. figure:: zephyr_dt_i2c_high_level.png 85 :alt: representation of a board with three I2C peripherals 86 :figclass: align-center 87 88Nodes corresponding to the I2C bus controller and each I2C peripheral would be 89present in the devicetree. Reflecting the hardware layout, the 90I2C peripheral nodes would be children of the bus controller node. 91Similar conventions exist for representing other types of hardware. 92 93The DTS would look something like this: 94 95.. code-block:: devicetree 96 97 /dts-v1/; 98 99 / { 100 soc { 101 i2c-bus-controller { 102 i2c-peripheral-1 { 103 }; 104 i2c-peripheral-2 { 105 }; 106 i2c-peripheral-3 { 107 }; 108 }; 109 }; 110 }; 111 112Properties in practice 113********************** 114 115In practice, properties usually describe or configure the hardware the node 116represents. For example, an I2C peripheral's node has a property whose value is 117the peripheral's address on the bus. 118 119Here's a tree representing the same example, but with real-world node 120names and properties you might see when working with I2C devices. 121 122.. figure:: zephyr_dt_i2c_example.png 123 :figclass: align-center 124 125 I2C devicetree example with real-world names and properties. 126 Node names are at the top of each node with a gray background. 127 Properties are shown as "name=value" lines. 128 129This is the corresponding DTS: 130 131.. code-block:: devicetree 132 133 /dts-v1/; 134 135 / { 136 soc { 137 i2c@40003000 { 138 compatible = "nordic,nrf-twim"; 139 reg = <0x40003000 0x1000>; 140 141 apds9960@39 { 142 compatible = "avago,apds9960"; 143 reg = <0x39>; 144 }; 145 ti_hdc@43 { 146 compatible = "ti,hdc", "ti,hdc1010"; 147 reg = <0x43>; 148 }; 149 mma8652fc@1d { 150 compatible = "nxp,fxos8700", "nxp,mma8652fc"; 151 reg = <0x1d>; 152 }; 153 }; 154 }; 155 }; 156 157.. _dt-unit-address: 158 159Unit addresses 160************** 161 162In addition to showing more real-world names and properties, the above example 163introduces a new devicetree concept: unit addresses. Unit addresses are the 164parts of node names after an "at" sign (``@``), like ``40003000`` in 165``i2c@40003000``, or ``39`` in ``apds9960@39``. Unit addresses are optional: 166the ``soc`` node does not have one. 167 168In devicetree, unit addresses give a node's address in the 169address space of its parent node. Here are some example unit addresses for 170different types of hardware. 171 172Memory-mapped peripherals 173 The peripheral's register map base address. 174 For example, the node named ``i2c@40003000`` represents an I2C controller 175 whose register map base address is 0x40003000. 176 177I2C peripherals 178 The peripheral's address on the I2C bus. 179 For example, the child node ``apds9960@39`` of the I2C controller 180 in the previous section has I2C address 0x39. 181 182SPI peripherals 183 An index representing the peripheral's chip select line number. 184 (If there is no chip select line, 0 is used.) 185 186Memory 187 The physical start address. 188 For example, a node named ``memory@2000000`` represents RAM starting at 189 physical address 0x2000000. 190 191Memory-mapped flash 192 Like RAM, the physical start address. 193 For example, a node named ``flash@8000000`` represents a flash device 194 whose physical start address is 0x8000000. 195 196Fixed flash partitions 197 This applies when the devicetree is used to store a flash partition table. 198 The unit address is the partition's start offset within the flash memory. 199 For example, take this flash device and its partitions: 200 201 .. code-block:: devicetree 202 203 flash@8000000 { 204 /* ... */ 205 partitions { 206 partition@0 { /* ... */ }; 207 partition@20000 { /* ... */ }; 208 /* ... */ 209 }; 210 }; 211 212 The node named ``partition@0`` has offset 0 from the start of its flash 213 device, so its base address is 0x8000000. Similarly, the base address of 214 the node named ``partition@20000`` is 0x8020000. 215 216.. _dt-important-props: 217 218Important properties 219******************** 220 221.. Documentation maintainers: If you add a property to this list, 222 make sure it gets linked to from gen_devicetree_rest.py too. 223 224The devicetree specification defines several standard properties. 225Some of the most important ones are: 226 227compatible 228 The name of the hardware device the node represents. 229 230 The recommended format is ``"vendor,device"``, like ``"avago,apds9960"``, 231 or a sequence of these, like ``"ti,hdc", "ti,hdc1010"``. The ``vendor`` 232 part is an abbreviated name of the vendor. The file 233 :zephyr_file:`dts/bindings/vendor-prefixes.txt` contains a list of commonly 234 accepted ``vendor`` names. The ``device`` part is usually taken from the 235 datasheet. 236 237 It is also sometimes a value like ``gpio-keys``, ``mmio-sram``, or 238 ``fixed-clock`` when the hardware's behavior is generic. 239 240 The build system uses the compatible property to find the right 241 :ref:`bindings <dt-bindings>` for the node. Device drivers use 242 ``devicetree.h`` to find nodes with relevant compatibles, in order to 243 determine the available hardware to manage. 244 245 The ``compatible`` property can have multiple values. Additional values are 246 useful when the device is a specific instance of a more general family, to 247 allow the system to match from most- to least-specific device drivers. 248 249 Within Zephyr's bindings syntax, this property has type ``string-array``. 250 251reg 252 Information used to address the device. The value is specific to the device 253 (i.e. is different depending on the compatible property). 254 255 The ``reg`` property is a sequence of ``(address, length)`` pairs. Each 256 pair is called a "register block". Values are conventionally written 257 in hex. 258 259 Here are some common patterns: 260 261 - Devices accessed via memory-mapped I/O registers (like ``i2c@40003000``): 262 ``address`` is usually the base address of the I/O register space, and 263 ``length`` is the number of bytes occupied by the registers. 264 - I2C devices (like ``apds9960@39`` and its siblings): 265 ``address`` is a slave address on the I2C bus. There is no ``length`` 266 value. 267 - SPI devices: ``address`` is a chip select line number; there is no 268 ``length``. 269 270 You may notice some similarities between the ``reg`` property and common 271 unit addresses described above. This is not a coincidence. The ``reg`` 272 property can be seen as a more detailed view of the addressable resources 273 within a device than its unit address. 274 275status 276 A string which describes whether the node is enabled. 277 278 The devicetree specification allows this property to have values 279 ``"okay"``, ``"disabled"``, ``"reserved"``, ``"fail"``, and ``"fail-sss"``. 280 Only the values ``"okay"`` and ``"disabled"`` are currently relevant to 281 Zephyr; use of other values currently results in undefined behavior. 282 283 A node is considered enabled if its status property is either ``"okay"`` or 284 not defined (i.e. does not exist in the devicetree source). Nodes with 285 status ``"disabled"`` are explicitly disabled. (For backwards 286 compatibility, the value ``"ok"`` is treated the same as ``"okay"``, but 287 this usage is deprecated.) Devicetree nodes which correspond to physical 288 devices must be enabled for the corresponding ``struct device`` in the 289 Zephyr driver model to be allocated and initialized. 290 291 Note that a child node is not implicitly disabled when its parent node is 292 disabled, i.e. child nodes should be disabled explicitly if desired. 293 294interrupts 295 Information about interrupts generated by the device, encoded as an array 296 of one or more *interrupt specifiers*. Each interrupt specifier has some 297 number of cells. See section 2.4, *Interrupts and Interrupt Mapping*, in the 298 `Devicetree Specification release v0.3`_ for more details. 299 300 Zephyr's devicetree bindings language lets you give a name to each cell in 301 an interrupt specifier. 302 303.. _Devicetree Specification release v0.3: 304 https://www.devicetree.org/specifications/ 305 306.. highlight:: none 307 308.. note:: 309 310 Earlier versions of Zephyr made frequent use of the ``label`` property, 311 which is distinct from the standard :ref:`node label <dt-node-labels>`. 312 Use of the label *property* in new devicetree bindings, as well as use of 313 the :c:macro:`DT_LABEL` macro in new code, are actively discouraged. Label 314 properties continue to persist for historical reasons in some existing 315 bindings and overlays, but should not be used in new bindings or device 316 implementations. 317 318.. _dt-writing-property-values: 319 320Writing property values 321*********************** 322 323This section describes how to write property values in DTS format. The property 324types in the table below are described in detail in :ref:`dt-bindings`. 325 326Some specifics are skipped in the interest of keeping things simple; if you're 327curious about details, see the devicetree specification. 328 329.. list-table:: 330 :header-rows: 1 331 :widths: 1 4 4 332 333 * - Property type 334 - How to write 335 - Example 336 337 * - string 338 - Double quoted 339 - ``a-string = "hello, world!";`` 340 341 * - int 342 - between angle brackets (``<`` and ``>``) 343 - ``an-int = <1>;`` 344 345 * - boolean 346 - for ``true``, with no value (for ``false``, use ``/delete-property/``) 347 - ``my-true-boolean;`` 348 349 * - array 350 - between angle brackets (``<`` and ``>``), separated by spaces 351 - ``foo = <0xdeadbeef 1234 0>;`` 352 353 * - uint8-array 354 - in hexadecimal *without* leading ``0x``, between square brackets (``[`` and ``]``). 355 - ``a-byte-array = [00 01 ab];`` 356 357 * - string-array 358 - separated by commas 359 - ``a-string-array = "string one", "string two", "string three";`` 360 361 * - phandle 362 - between angle brackets (``<`` and ``>``) 363 - ``a-phandle = <&mynode>;`` 364 365 * - phandles 366 - between angle brackets (``<`` and ``>``), separated by spaces 367 - ``some-phandles = <&mynode0 &mynode1 &mynode2>;`` 368 369 * - phandle-array 370 - between angle brackets (``<`` and ``>``), separated by spaces 371 - ``a-phandle-array = <&mynode0 1 2>, <&mynode1 3 4>;`` 372 373Additional notes on the above: 374 375- The values in the ``phandle``, ``phandles``, and ``phandle-array`` types are 376 described further in :ref:`dt-phandles` 377 378- Boolean properties are true if present. They should not have a value. 379 A boolean property is only false if it is completely missing in the DTS. 380 381- The ``foo`` property value above has three *cells* with values 0xdeadbeef, 1234, 382 and 0, in that order. Note that hexadecimal and decimal numbers are allowed and 383 can be intermixed. Since Zephyr transforms DTS to C sources, it is not 384 necessary to specify the endianness of an individual cell here. 385 386- 64-bit integers are written as two 32-bit cells in big-endian order. The value 387 0xaaaa0000bbbb1111 would be written ``<0xaaaa0000 0xbbbb1111>``. 388 389- The ``a-byte-array`` property value is the three bytes 0x00, 0x01, and 0xab, in 390 that order. 391 392- Parentheses, arithmetic operators, and bitwise operators are allowed. The 393 ``bar`` property contains a single cell with value 64: 394 395 .. code-block:: devicetree 396 397 bar = <(2 * (1 << 5))>; 398 399 Note that the entire expression must be parenthesized. 400 401- Property values refer to other nodes in the devicetree by their *phandles*. 402 You can write a phandle using ``&foo``, where ``foo`` is a :ref:`node label 403 <dt-node-labels>`. Here is an example devicetree fragment: 404 405 .. code-block:: devicetree 406 407 foo: device@0 { }; 408 device@1 { 409 sibling = <&foo 1 2>; 410 }; 411 412 The ``sibling`` property of node ``device@1`` contains three cells, in this order: 413 414 #. The ``device@0`` node's phandle, which is written here as ``&foo`` since 415 the ``device@0`` node has a node label ``foo`` 416 #. The value 1 417 #. The value 2 418 419 In the devicetree, a phandle value is a cell -- which again is just a 32-bit 420 unsigned int. However, the Zephyr devicetree API generally exposes these 421 values as *node identifiers*. Node identifiers are covered in more detail in 422 :ref:`dt-from-c`. 423 424- Array and similar type property values can be split into several ``<>`` 425 blocks, like this: 426 427 .. code-block:: devicetree 428 429 foo = <1 2>, <3 4>; // Okay for 'type: array' 430 foo = <&label1 &label2>, <&label3 &label4>; // Okay for 'type: phandles' 431 foo = <&label1 1 2>, <&label2 3 4>; // Okay for 'type: phandle-array' 432 433 This is recommended for readability when possible if the value can be 434 logically grouped into blocks of sub-values. 435 436.. _dt-alias-chosen: 437 438Aliases and chosen nodes 439************************ 440 441There are two additional ways beyond :ref:`node labels <dt-node-labels>` to 442refer to a particular node without specifying its entire path: by alias, or by 443chosen node. 444 445Here is an example devicetree which uses both: 446 447.. code-block:: devicetree 448 449 /dts-v1/; 450 451 / { 452 chosen { 453 zephyr,console = &uart0; 454 }; 455 456 aliases { 457 my-uart = &uart0; 458 }; 459 460 soc { 461 uart0: serial@12340000 { 462 ... 463 }; 464 }; 465 }; 466 467The ``/aliases`` and ``/chosen`` nodes do not refer to an actual hardware 468device. Their purpose is to specify other nodes in the devicetree. 469 470Above, ``my-uart`` is an alias for the node with path ``/soc/serial@12340000``. 471Using its node label ``uart0``, the same node is set as the value of the chosen 472``zephyr,console`` node. 473 474Zephyr sample applications sometimes use aliases to allow overriding the 475particular hardware device used by the application in a generic way. For 476example, :zephyr:code-sample:`blinky` uses this to abstract the LED to blink via the 477``led0`` alias. 478 479The ``/chosen`` node's properties are used to configure system- or 480subsystem-wide values. See :ref:`devicetree-chosen-nodes` for more information. 481