1Device Tree Compiler Manual 2=========================== 3 4I - "dtc", the device tree compiler 5 1) Obtaining Sources 6 1.1) Submitting Patches 7 2) Description 8 3) Command Line 9 4) Source File 10 4.1) Overview 11 4.2) Properties 12 4.3) Labels and References 13 14II - The DT block format 15 1) Header 16 2) Device tree generalities 17 3) Device tree "structure" block 18 4) Device tree "strings" block 19 20 21III - libfdt 22 23IV - Utility Tools 24 1) convert-dtsv0 -- Conversion to Version 1 25 1) fdtdump 26 27 28I - "dtc", the device tree compiler 29=================================== 30 311) Sources 32 33Source code for the Device Tree Compiler can be found at git.kernel.org. 34 35The upstream repository is here: 36 37 git://git.kernel.org/pub/scm/utils/dtc/dtc.git 38 https://git.kernel.org/pub/scm/utils/dtc/dtc.git 39 40The gitweb interface for the upstream respository is: 41 42 https://git.kernel.org/cgit/utils/dtc/dtc.git/ 43 441.1) Submitting Patches 45 46Patches should be sent to the maintainers: 47 David Gibson <david@gibson.dropbear.id.au> 48 Jon Loeliger <jdl@jdl.com> 49and CCed to <devicetree-compiler@vger.kernel.org>. 50 512) Description 52 53The Device Tree Compiler, dtc, takes as input a device-tree in 54a given format and outputs a device-tree in another format. 55Typically, the input format is "dts", a human readable source 56format, and creates a "dtb", or binary format as output. 57 58The currently supported Input Formats are: 59 60 - "dtb": "blob" format. A flattened device-tree block with 61 header in one binary blob. 62 63 - "dts": "source" format. A text file containing a "source" 64 for a device-tree. 65 66 - "fs" format. A representation equivalent to the output of 67 /proc/device-tree where nodes are directories and 68 properties are files. 69 70The currently supported Output Formats are: 71 72 - "dtb": "blob" format 73 74 - "dts": "source" format 75 76 - "asm": assembly language file. A file that can be sourced 77 by gas to generate a device-tree "blob". That file can 78 then simply be added to your Makefile. Additionally, the 79 assembly file exports some symbols that can be used. 80 81 823) Command Line 83 84The syntax of the dtc command line is: 85 86 dtc [options] [<input_filename>] 87 88Options: 89 90 <input_filename> 91 The name of the input source file. If no <input_filename> 92 or "-" is given, stdin is used. 93 94 -b <number> 95 Set the physical boot cpu. 96 97 -f 98 Force. Try to produce output even if the input tree has errors. 99 100 -h 101 Emit a brief usage and help message. 102 103 -I <input_format> 104 The source input format, as listed above. 105 106 -o <output_filename> 107 The name of the generated output file. Use "-" for stdout. 108 109 -O <output_format> 110 The generated output format, as listed above. 111 112 -d <dependency_filename> 113 Generate a dependency file during compilation. 114 115 -q 116 Quiet: -q suppress warnings, -qq errors, -qqq all 117 118 -R <number> 119 Make space for <number> reserve map entries 120 Relevant for dtb and asm output only. 121 122 -S <bytes> 123 Ensure the blob at least <bytes> long, adding additional 124 space if needed. 125 126 -v 127 Print DTC version and exit. 128 129 -V <output_version> 130 Generate output conforming to the given <output_version>. 131 By default the most recent version is generated. 132 Relevant for dtb and asm output only. 133 134 135The <output_version> defines what version of the "blob" format will be 136generated. Supported versions are 1, 2, 3, 16 and 17. The default is 137always the most recent version and is likely the highest number. 138 139Additionally, dtc performs various sanity checks on the tree. 140 141 1424) Device Tree Source file 143 1444.1) Overview 145 146Here is a very rough overview of the layout of a DTS source file: 147 148 149 sourcefile: list_of_memreserve devicetree 150 151 memreserve: label 'memreserve' ADDR ADDR ';' 152 | label 'memreserve' ADDR '-' ADDR ';' 153 154 devicetree: '/' nodedef 155 156 nodedef: '{' list_of_property list_of_subnode '}' ';' 157 158 property: label PROPNAME '=' propdata ';' 159 160 propdata: STRING 161 | '<' list_of_cells '>' 162 | '[' list_of_bytes ']' 163 164 subnode: label nodename nodedef 165 166That structure forms a hierarchical layout of nodes and properties 167rooted at an initial node as: 168 169 / { 170 } 171 172Both classic C style and C++ style comments are supported. 173 174Source files may be directly included using the syntax: 175 176 /include/ "filename" 177 178 1794.2) Properties 180 181Properties are named, possibly labeled, values. Each value 182is one of: 183 184 - A null-teminated C-like string, 185 - A numeric value fitting in 32 bits, 186 - A list of 32-bit values 187 - A byte sequence 188 189Here are some example property definitions: 190 191 - A property containing a 0 terminated string 192 193 property1 = "string_value"; 194 195 - A property containing a numerical 32-bit hexadecimal value 196 197 property2 = <1234abcd>; 198 199 - A property containing 3 numerical 32-bit hexadecimal values 200 201 property3 = <12345678 12345678 deadbeef>; 202 203 - A property whose content is an arbitrary array of bytes 204 205 property4 = [0a 0b 0c 0d de ea ad be ef]; 206 207 208Node may contain sub-nodes to obtain a hierarchical structure. 209For example: 210 211 - A child node named "childnode" whose unit name is 212 "childnode at address". It it turn has a string property 213 called "childprop". 214 215 childnode@addresss { 216 childprop = "hello\n"; 217 }; 218 219 220By default, all numeric values are hexadecimal. Alternate bases 221may be specified using a prefix "d#" for decimal, "b#" for binary, 222and "o#" for octal. 223 224Strings support common escape sequences from C: "\n", "\t", "\r", 225"\(octal value)", "\x(hex value)". 226 227 2284.3) Labels and References 229 230Labels may be applied to nodes or properties. Labels appear 231before a node name, and are referenced using an ampersand: &label. 232Absolute node path names are also allowed in node references. 233 234In this exmaple, a node is labled "mpic" and then referenced: 235 236 mpic: interrupt-controller@40000 { 237 ... 238 }; 239 240 ethernet-phy@3 { 241 interrupt-parent = <&mpic>; 242 ... 243 }; 244 245And used in properties, lables may appear before or after any value: 246 247 randomnode { 248 prop: string = data: "mystring\n" data_end: ; 249 ... 250 }; 251 252 253 254II - The DT block format 255======================== 256 257This chapter defines the format of the flattened device-tree 258passed to the kernel. The actual content of the device tree 259are described in the kernel documentation in the file 260 261 linux-2.6/Documentation/powerpc/booting-without-of.txt 262 263You can find example of code manipulating that format within 264the kernel. For example, the file: 265 266 including arch/powerpc/kernel/prom_init.c 267 268will generate a flattened device-tree from the Open Firmware 269representation. Other utilities such as fs2dt, which is part of 270the kexec tools, will generate one from a filesystem representation. 271Some bootloaders such as U-Boot provide a bit more support by 272using the libfdt code. 273 274For booting the kernel, the device tree block has to be in main memory. 275It has to be accessible in both real mode and virtual mode with no 276mapping other than main memory. If you are writing a simple flash 277bootloader, it should copy the block to RAM before passing it to 278the kernel. 279 280 2811) Header 282--------- 283 284The kernel is entered with r3 pointing to an area of memory that is 285roughly described in include/asm-powerpc/prom.h by the structure 286boot_param_header: 287 288 struct boot_param_header { 289 u32 magic; /* magic word OF_DT_HEADER */ 290 u32 totalsize; /* total size of DT block */ 291 u32 off_dt_struct; /* offset to structure */ 292 u32 off_dt_strings; /* offset to strings */ 293 u32 off_mem_rsvmap; /* offset to memory reserve map */ 294 u32 version; /* format version */ 295 u32 last_comp_version; /* last compatible version */ 296 297 /* version 2 fields below */ 298 u32 boot_cpuid_phys; /* Which physical CPU id we're 299 booting on */ 300 /* version 3 fields below */ 301 u32 size_dt_strings; /* size of the strings block */ 302 303 /* version 17 fields below */ 304 u32 size_dt_struct; /* size of the DT structure block */ 305 }; 306 307Along with the constants: 308 309 /* Definitions used by the flattened device tree */ 310 #define OF_DT_HEADER 0xd00dfeed /* 4: version, 311 4: total size */ 312 #define OF_DT_BEGIN_NODE 0x1 /* Start node: full name 313 */ 314 #define OF_DT_END_NODE 0x2 /* End node */ 315 #define OF_DT_PROP 0x3 /* Property: name off, 316 size, content */ 317 #define OF_DT_END 0x9 318 319All values in this header are in big endian format, the various 320fields in this header are defined more precisely below. All "offset" 321values are in bytes from the start of the header; that is from the 322value of r3. 323 324 - magic 325 326 This is a magic value that "marks" the beginning of the 327 device-tree block header. It contains the value 0xd00dfeed and is 328 defined by the constant OF_DT_HEADER 329 330 - totalsize 331 332 This is the total size of the DT block including the header. The 333 "DT" block should enclose all data structures defined in this 334 chapter (who are pointed to by offsets in this header). That is, 335 the device-tree structure, strings, and the memory reserve map. 336 337 - off_dt_struct 338 339 This is an offset from the beginning of the header to the start 340 of the "structure" part the device tree. (see 2) device tree) 341 342 - off_dt_strings 343 344 This is an offset from the beginning of the header to the start 345 of the "strings" part of the device-tree 346 347 - off_mem_rsvmap 348 349 This is an offset from the beginning of the header to the start 350 of the reserved memory map. This map is a list of pairs of 64- 351 bit integers. Each pair is a physical address and a size. The 352 list is terminated by an entry of size 0. This map provides the 353 kernel with a list of physical memory areas that are "reserved" 354 and thus not to be used for memory allocations, especially during 355 early initialization. The kernel needs to allocate memory during 356 boot for things like un-flattening the device-tree, allocating an 357 MMU hash table, etc... Those allocations must be done in such a 358 way to avoid overriding critical things like, on Open Firmware 359 capable machines, the RTAS instance, or on some pSeries, the TCE 360 tables used for the iommu. Typically, the reserve map should 361 contain _at least_ this DT block itself (header,total_size). If 362 you are passing an initrd to the kernel, you should reserve it as 363 well. You do not need to reserve the kernel image itself. The map 364 should be 64-bit aligned. 365 366 - version 367 368 This is the version of this structure. Version 1 stops 369 here. Version 2 adds an additional field boot_cpuid_phys. 370 Version 3 adds the size of the strings block, allowing the kernel 371 to reallocate it easily at boot and free up the unused flattened 372 structure after expansion. Version 16 introduces a new more 373 "compact" format for the tree itself that is however not backward 374 compatible. Version 17 adds an additional field, size_dt_struct, 375 allowing it to be reallocated or moved more easily (this is 376 particularly useful for bootloaders which need to make 377 adjustments to a device tree based on probed information). You 378 should always generate a structure of the highest version defined 379 at the time of your implementation. Currently that is version 17, 380 unless you explicitly aim at being backward compatible. 381 382 - last_comp_version 383 384 Last compatible version. This indicates down to what version of 385 the DT block you are backward compatible. For example, version 2 386 is backward compatible with version 1 (that is, a kernel build 387 for version 1 will be able to boot with a version 2 format). You 388 should put a 1 in this field if you generate a device tree of 389 version 1 to 3, or 16 if you generate a tree of version 16 or 17 390 using the new unit name format. 391 392 - boot_cpuid_phys 393 394 This field only exist on version 2 headers. It indicate which 395 physical CPU ID is calling the kernel entry point. This is used, 396 among others, by kexec. If you are on an SMP system, this value 397 should match the content of the "reg" property of the CPU node in 398 the device-tree corresponding to the CPU calling the kernel entry 399 point (see further chapters for more informations on the required 400 device-tree contents) 401 402 - size_dt_strings 403 404 This field only exists on version 3 and later headers. It 405 gives the size of the "strings" section of the device tree (which 406 starts at the offset given by off_dt_strings). 407 408 - size_dt_struct 409 410 This field only exists on version 17 and later headers. It gives 411 the size of the "structure" section of the device tree (which 412 starts at the offset given by off_dt_struct). 413 414So the typical layout of a DT block (though the various parts don't 415need to be in that order) looks like this (addresses go from top to 416bottom): 417 418 ------------------------------ 419 r3 -> | struct boot_param_header | 420 ------------------------------ 421 | (alignment gap) (*) | 422 ------------------------------ 423 | memory reserve map | 424 ------------------------------ 425 | (alignment gap) | 426 ------------------------------ 427 | | 428 | device-tree structure | 429 | | 430 ------------------------------ 431 | (alignment gap) | 432 ------------------------------ 433 | | 434 | device-tree strings | 435 | | 436 -----> ------------------------------ 437 | 438 | 439 --- (r3 + totalsize) 440 441 (*) The alignment gaps are not necessarily present; their presence 442 and size are dependent on the various alignment requirements of 443 the individual data blocks. 444 445 4462) Device tree generalities 447--------------------------- 448 449This device-tree itself is separated in two different blocks, a 450structure block and a strings block. Both need to be aligned to a 4 451byte boundary. 452 453First, let's quickly describe the device-tree concept before detailing 454the storage format. This chapter does _not_ describe the detail of the 455required types of nodes & properties for the kernel, this is done 456later in chapter III. 457 458The device-tree layout is strongly inherited from the definition of 459the Open Firmware IEEE 1275 device-tree. It's basically a tree of 460nodes, each node having two or more named properties. A property can 461have a value or not. 462 463It is a tree, so each node has one and only one parent except for the 464root node who has no parent. 465 466A node has 2 names. The actual node name is generally contained in a 467property of type "name" in the node property list whose value is a 468zero terminated string and is mandatory for version 1 to 3 of the 469format definition (as it is in Open Firmware). Version 16 makes it 470optional as it can generate it from the unit name defined below. 471 472There is also a "unit name" that is used to differentiate nodes with 473the same name at the same level, it is usually made of the node 474names, the "@" sign, and a "unit address", which definition is 475specific to the bus type the node sits on. 476 477The unit name doesn't exist as a property per-se but is included in 478the device-tree structure. It is typically used to represent "path" in 479the device-tree. More details about the actual format of these will be 480below. 481 482The kernel powerpc generic code does not make any formal use of the 483unit address (though some board support code may do) so the only real 484requirement here for the unit address is to ensure uniqueness of 485the node unit name at a given level of the tree. Nodes with no notion 486of address and no possible sibling of the same name (like /memory or 487/cpus) may omit the unit address in the context of this specification, 488or use the "@0" default unit address. The unit name is used to define 489a node "full path", which is the concatenation of all parent node 490unit names separated with "/". 491 492The root node doesn't have a defined name, and isn't required to have 493a name property either if you are using version 3 or earlier of the 494format. It also has no unit address (no @ symbol followed by a unit 495address). The root node unit name is thus an empty string. The full 496path to the root node is "/". 497 498Every node which actually represents an actual device (that is, a node 499which isn't only a virtual "container" for more nodes, like "/cpus" 500is) is also required to have a "device_type" property indicating the 501type of node . 502 503Finally, every node that can be referenced from a property in another 504node is required to have a "linux,phandle" property. Real open 505firmware implementations provide a unique "phandle" value for every 506node that the "prom_init()" trampoline code turns into 507"linux,phandle" properties. However, this is made optional if the 508flattened device tree is used directly. An example of a node 509referencing another node via "phandle" is when laying out the 510interrupt tree which will be described in a further version of this 511document. 512 513This "linux, phandle" property is a 32-bit value that uniquely 514identifies a node. You are free to use whatever values or system of 515values, internal pointers, or whatever to generate these, the only 516requirement is that every node for which you provide that property has 517a unique value for it. 518 519Here is an example of a simple device-tree. In this example, an "o" 520designates a node followed by the node unit name. Properties are 521presented with their name followed by their content. "content" 522represents an ASCII string (zero terminated) value, while <content> 523represents a 32-bit hexadecimal value. The various nodes in this 524example will be discussed in a later chapter. At this point, it is 525only meant to give you a idea of what a device-tree looks like. I have 526purposefully kept the "name" and "linux,phandle" properties which 527aren't necessary in order to give you a better idea of what the tree 528looks like in practice. 529 530 / o device-tree 531 |- name = "device-tree" 532 |- model = "MyBoardName" 533 |- compatible = "MyBoardFamilyName" 534 |- #address-cells = <2> 535 |- #size-cells = <2> 536 |- linux,phandle = <0> 537 | 538 o cpus 539 | | - name = "cpus" 540 | | - linux,phandle = <1> 541 | | - #address-cells = <1> 542 | | - #size-cells = <0> 543 | | 544 | o PowerPC,970@0 545 | |- name = "PowerPC,970" 546 | |- device_type = "cpu" 547 | |- reg = <0> 548 | |- clock-frequency = <5f5e1000> 549 | |- 64-bit 550 | |- linux,phandle = <2> 551 | 552 o memory@0 553 | |- name = "memory" 554 | |- device_type = "memory" 555 | |- reg = <00000000 00000000 00000000 20000000> 556 | |- linux,phandle = <3> 557 | 558 o chosen 559 |- name = "chosen" 560 |- bootargs = "root=/dev/sda2" 561 |- linux,phandle = <4> 562 563This tree is almost a minimal tree. It pretty much contains the 564minimal set of required nodes and properties to boot a linux kernel; 565that is, some basic model informations at the root, the CPUs, and the 566physical memory layout. It also includes misc information passed 567through /chosen, like in this example, the platform type (mandatory) 568and the kernel command line arguments (optional). 569 570The /cpus/PowerPC,970@0/64-bit property is an example of a 571property without a value. All other properties have a value. The 572significance of the #address-cells and #size-cells properties will be 573explained in chapter IV which defines precisely the required nodes and 574properties and their content. 575 576 5773) Device tree "structure" block 578 579The structure of the device tree is a linearized tree structure. The 580"OF_DT_BEGIN_NODE" token starts a new node, and the "OF_DT_END_NODE" 581ends that node definition. Child nodes are simply defined before 582"OF_DT_END_NODE" (that is nodes within the node). A 'token' is a 32 583bit value. The tree has to be "finished" with a OF_DT_END token 584 585Here's the basic structure of a single node: 586 587 * token OF_DT_BEGIN_NODE (that is 0x00000001) 588 * for version 1 to 3, this is the node full path as a zero 589 terminated string, starting with "/". For version 16 and later, 590 this is the node unit name only (or an empty string for the 591 root node) 592 * [align gap to next 4 bytes boundary] 593 * for each property: 594 * token OF_DT_PROP (that is 0x00000003) 595 * 32-bit value of property value size in bytes (or 0 if no 596 value) 597 * 32-bit value of offset in string block of property name 598 * property value data if any 599 * [align gap to next 4 bytes boundary] 600 * [child nodes if any] 601 * token OF_DT_END_NODE (that is 0x00000002) 602 603So the node content can be summarized as a start token, a full path, 604a list of properties, a list of child nodes, and an end token. Every 605child node is a full node structure itself as defined above. 606 607NOTE: The above definition requires that all property definitions for 608a particular node MUST precede any subnode definitions for that node. 609Although the structure would not be ambiguous if properties and 610subnodes were intermingled, the kernel parser requires that the 611properties come first (up until at least 2.6.22). Any tools 612manipulating a flattened tree must take care to preserve this 613constraint. 614 6154) Device tree "strings" block 616 617In order to save space, property names, which are generally redundant, 618are stored separately in the "strings" block. This block is simply the 619whole bunch of zero terminated strings for all property names 620concatenated together. The device-tree property definitions in the 621structure block will contain offset values from the beginning of the 622strings block. 623 624 625III - libfdt 626============ 627 628This library should be merged into dtc proper. 629This library should likely be worked into U-Boot and the kernel. 630 631 632IV - Utility Tools 633================== 634 6351) convert-dtsv0 -- Conversion to Version 1 636 637convert-dtsv0 is a small utility program which converts (DTS) 638Device Tree Source from the obsolete version 0 to version 1. 639 640Version 1 DTS files are marked by line "/dts-v1/;" at the top of the file. 641 642The syntax of the convert-dtsv0 command line is: 643 644 convert-dtsv0 [<input_filename ... >] 645 646Each file passed will be converted to the new /dts-v1/ version by creating 647a new file with a "v1" appended the filename. 648 649Comments, empty lines, etc. are preserved. 650 651 6522) fdtdump -- Flat Device Tree dumping utility 653 654The fdtdump program prints a readable version of a flat device tree file. 655 656The syntax of the fdtdump command line is: 657 658 fdtdump <DTB-file-name> 659