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