参考博客:
https://www.cnblogs.com/zongzi10010/p/10793074.html
Device Tree 详解_pwl999的博客-CSDN博客_device tree
1 dtc命令
dtc -h
Usage: dtc [options] <input file>
Options: -[qI:O:o:V:d:R:S:p:a:fb:i:H:sW:E:@Ahv]
-q, --quiet
Quiet: -q suppress warnings, -qq errors, -qqq all
-I, --in-format <arg>
Input formats are:
dts - device tree source text
dtb - device tree blob
fs - /proc/device-tree style directory
-o, --out <arg>
Output file
-O, --out-format <arg>
Output formats are:
dts - device tree source text
dtb - device tree blob
asm - assembler source
-V, --out-version <arg>
Blob version to produce, defaults to 17 (for dtb and asm output)
-d, --out-dependency <arg>
Output dependency file
-R, --reserve <arg>
Make space for <number> reserve map entries (for dtb and asm output)
-S, --space <arg>
Make the blob at least <bytes> long (extra space)
-p, --pad <arg>
Add padding to the blob of <bytes> long (extra space)
-a, --align <arg>
Make the blob align to the <bytes> (extra space)
-b, --boot-cpu <arg>
Set the physical boot cpu
-f, --force
Try to produce output even if the input tree has errors
-i, --include <arg>
Add a path to search for include files
-s, --sort
Sort nodes and properties before outputting (useful for comparing trees)
-H, --phandle <arg>
Valid phandle formats are:
legacy - "linux,phandle" properties only
epapr - "phandle" properties only
both - Both "linux,phandle" and "phandle" properties
-W, --warning <arg>
Enable/disable warnings (prefix with "no-")
-E, --error <arg>
Enable/disable errors (prefix with "no-")
-@, --symbols
Enable generation of symbols
-A, --auto-alias
Enable auto-alias of labels
-h, --help
Print this help and exit
-v, --version
Print version and exit
常见用法
二进制文件编译为可读文件
dtc -I dtb -O dts -o output.dts arch/arm/boot/dts/jz2440.dtb
生成二进制文件
dtc -I dts -O dtb -o jz2440.dtb output.dts
2 dts文件
2.1 dts格式
设备树由两种元素组成:node
(节点);property
(属性)
node
节点,使用一对花括符来定义
node-name[@unit-address] {
}
property
属性
property-name=value
语法
Devicetree node格式:
[label:] node-name[@unit-address] {
[properties definitions]
[child nodes]
};
Property格式1:
[label:] property-name = value;
Property格式2(没有值):
[label:] property-name;
Property值为空,用property本身出现或者不出现来表示一个true/false值。
Property取值只有3种:
arrays of cells(1个或多个32位数据, 64位数据使用2个32位数据表示),
string(字符串),
bytestring(1个或多个字节)
示例:
/ { /* 定义根节点 / */
model = "mt6799";
compatible = "mediatek,mt6799";
interrupt-parent = <&gic>;
#address-cells = <2>;
#size-cells = <2>;
/* chosen */
chosen { /* 定义节点chosen */
bootargs = "console=tty0 console=ttyMT0,921600n1 root=/dev/ram";
};
}
注意:节点和属性名是可以自定义的,但是在设备树中。预定义了一些标准节点和属性
2.2 预设标准property
-
compatible
通常用来描述device
和driver
的适配compatible = "ns16550", "ns8250";
先去匹配ns16550,如果失败再去匹配ns8250 -
model
表示设备型号 -
phandle
引用node
。常见用法是定义一个label
来引用node
。在编译时系统会自动生成一个phandle
属性。
使用&
来引用label
label
的定义// cpu0是一个label代指cpu@0 cpu0: cpu@0 { device_type = "cpu"; compatible = "arm,cortex-a35"; reg = <0x000>; enable-method = "psci"; cpu-idle-states = <&LEGACY_MCDI &LEGACY_SODI &LEGACY_SODI3 &LEGACY_DPIDLE>, <&LEGACY_SUSPEND &MCDI &SODI &SODI3 &DPIDLE &SUSPEND>; cpu-release-addr = <0x0 0x40000200>; clock-frequency = <1248000000>; };
引用
cpu-map { cluster0 { core0 { cpu = <&cpu0>; }; core1 { cpu = <&cpu1>; }; core2 { cpu = <&cpu2>; }; core3 { cpu = <&cpu3>; }; };
-
#address-cells #size-cells
定义当前节点中reg
的属性和解析格式。选择解析reg
中的第几个数据。
#address-cells=<0>
:不解析
#address-cells=<1>
: 一个一个解析
#address-cells=<2>
: 解析第二个
#size-cells
同#address-cells
示例soc { #address-cells = <1>; #size-cells = <1>; serial { reg = <0x0 0x100 0x0 0x200>; } }
1、如果
node”soc”
中”#address-cells=<1>”、”#size-cells=<1>”
,那么子node”serial”
中”reg”
属性的解析为“addr1 = 0x0, size1 = 0x100, addr2 = 0x0, size2 = 0x200”
2、如果node”soc”
中”#address-cells=<2>”、”#size-cells=<2>”
,那么子node”serial”
中”reg”
属性的解析为“addr1 = 0x100, size1 = 0x200”
3、如果node”soc”
中”#address-cells=<2>”、”#size-cells=<0>”
,那么子node”serial”
中”reg”
属性的解析为“addr1 = 0x100, addr2 = 0x200”
-
reg
解析出address,length
。解析格式由#address-cells #size-cells
控制 -
ranges
当前节点和父节点之间的地址映射
格式:child-bus-address,parentbus-address,length
child-bus-address
解析的长度受当前节点#address-cells
控制
parentbus-address
解析的长度受父节点的#address-parentbus-address
控制
length
的解析受当前node
的#size-cells
控制 -
interrupt
中断节点分为3种
interrupt Gernerating Devices
,产生中断的设备
interrupt Controllers
中断控制器,处理中断的设备
interrupt Nexus
中断联结,路由中断给中断控制器#interrupt-cells
同#address-cells #size-cells
interrupt-controller
用来声明当前node
为中断控制器interrupt-map
用来描述interrupt nexus
设备对中断的路由。
解析格式为child unit address, child interrupt specifier, interrupt-parent, parent unit address, parent interrupt specifier
2.3 标准node
node一般由名字加@地址构成,这样可以防止node name冲突
node-name[@unit-address]{
}
msdc0:msdc@11240000
-
root node
根节点为每个deivce tree必备的
property成员为下:
#address-cells #size-cells model :详见property compatible
-
aliases node
给绝对路径取别名
aliases { serial0 = "/simple-bus@fe000000/serial@llc500"; ethernet0 = "/simple-bus@fe000000/ethernet@31c000"; };
-
memory node
用于传递内存布局
property成员
device_type : should be memory reg : 指定内存大小和物理地址范围 initial-mapped-area :是一个由(有效地址、物理地址、大小)三元组组成的prop编码数组
#address-cells = <2>; #size-cells = <2>; memory@0 { device_type = "memory"; reg = <0x000000000 0x00000000 0x00000000 0x80000000 0x000000001 0x00000000 0x00000001 0x00000000>; };
-
chosen node
property成员
bootargs :引导参数字符串 stdout-path :引导控制台输出的设备 stdin-path :引导控制台输入的设备 chosen { bootargs = "earlycon=cdns,0xfd000000,115200 console=tty0 console=ttyPS0,115200 root=/dev/ram0 rw earlyprintk xilinx_uartps.rx_trigger_level=32 loglevel=8 nohz=off ignore_loglevel"; };
-
cpus node
cpus { #address-cells = <1>; #size-cells = <0>; cpu@0 { compatible = "cdns,xtensa-cpu"; reg = <0>; }; };