1、DTS的加载过程node
若是要使用Device Tree,首先用户要了解本身的硬件配置和系统运行参数,并把这些信息组织成Device Tree source file。经过DTC(Device Tree Compiler),能够将这些适合人类阅读的Device Tree source file变成适合机器处理的Device Tree binary file(device tree blob)。ios
在系统启动时,boot program(例如:firmware、bootloader)能够将保存在flash中的DTB copy到内存(固然也能够经过其余方式,例如经过bootloader的交互式命令加载DTB,或者firmware能够探测到device的信息,组织成DTB保存在内存中),并把DTB的起始地址传递给client program(例如OS kernel,bootloader或者其余特殊功能的程序)。chrome
对于计算机系统(computer system),通常是firmware->bootloader->OS,对于嵌入式系统,通常是bootloader->OS。express
它基本上就是画一棵电路板上CPU、总线、设备组成的树,Bootloader会将这棵树传递给内核,而后内核能够识别这棵树,并根据它展开出Linux内核中的platform_device、i2c_client、spi_device等设备,而这些设备用到的内存、IRQ等资源,也被传递给了内核,内核会将这些资源绑定给展开的相应的设备。数组
Device Tree是否要描述系统中的全部硬件信息?答案是否认的。基本上,不须要描述那些能够动态探测到的设备,例如USB device。不过对于SOC上的usb hostcontroller,它没法被动态识别,须要在device tree中描述。app
同理,在computersystem中,PCI device能够被动态探测到,不须要在device tree中描述,可是PCI bridge若是不能被探测,那么就须要描述它。函数
.dts文件是一种ASCII 文本格式的Device Tree描述,此文本格式很是人性化,适合人类的阅读习惯。工具
基本上,在ARM Linux中,一个.dts文件对应一个ARM的machine,通常放置在内核的arch/arm/boot/dts/目录。ui
因为一个SoC可能对应多个machine(一个SoC能够对应多个产品和电路板),势必这些.dts文件需包含许多共同的部分,Linux内核为了简化,把SoC公用的部分或者多个machine共同的部分通常提炼为.dtsi,相似于C语言的头文件。其余的machine对应的.dts能够include这个.dtsi。spa
譬如,对于RK3288而言, rk3288.dtsi就被rk3288-chrome.dts所引用,rk3288-chrome.dts有以下一行:#include“rk3288.dtsi”。
再如rtd1195, 在 rtd-119x-nas.dts中就包含了/include/ "rtd-119x.dtsi"。
固然,和C语言的头文件相似,.dtsi也能够include其余的.dtsi,譬如几乎全部的ARM SoC的.dtsi都引用了skeleton.dtsi,即#include"skeleton.dtsi“
或者 /include/ "skeleton.dtsi"
{ node1 { a-string-property = "A string"; a-string-list-property = "first string", "second string"; a-byte-data-property = [0x01 0x23 0x34 0x56]; child-node1 { first-child-property; second-child-property = <1>; a-string-property = "Hello, world"; }; child-node2 { }; }; node2 { an-empty-property; a-cell-property = <1 2 3 4>; /* each number (cell) is a uint32 */ child-node1 { }; }; };
下面以一个最简单的machine为例来看如何写一个.dts文件。假设此machine的配置以下:
一、1个双核ARM Cortex-A9 32位处理器;
二、ARM的local bus上的内存映射区域分布了2个串口(分别位于0x101F1000 和 0x101F2000)、GPIO控制器(位于0x101F3000)、SPI控制器(位于0x10115000)、中断控制器(位于0x10140000)和一个external bus桥;
三、External bus桥上又链接了SMC SMC91111 Ethernet(位于0x10100000)、I2C控制器(位于0x10160000)、64MB NOR Flash(位于0x30000000);
四、External bus桥上链接的I2C控制器所对应的I2C总线上又链接了Maxim DS1338实时钟(I2C地址为0x58)。
其对应的.dts文件为:
{ compatible = "acme,coyotes-revenge"; #address-cells = <1>; #size-cells = <1>; interrupt-parent = <&intc>; cpus { #address-cells = <1>; #size-cells = <0>; cpu@0 { compatible = "arm,cortex-a9"; reg = <0>; }; cpu@1 { compatible = "arm,cortex-a9"; reg = <1>; }; }; serial@101f0000 { compatible = "arm,pl011"; reg = <0x101f0000 0x1000 >; interrupts = < 1 0 >; }; serial@101f2000 { compatible = "arm,pl011"; reg = <0x101f2000 0x1000 >; interrupts = < 2 0 >; }; gpio@101f3000 { compatible = "arm,pl061"; reg = <0x101f3000 0x1000 0x101f4000 0x0010>; interrupts = < 3 0 >; }; intc: interrupt-controller@10140000 { compatible = "arm,pl190"; reg = <0x10140000 0x1000 >; interrupt-controller; #interrupt-cells = <2>; }; spi@10115000 { compatible = "arm,pl022"; reg = <0x10115000 0x1000 >; interrupts = < 4 0 >; }; external-bus { #address-cells = <2> #size-cells = <1>; ranges = <0 0 0x10100000 0x10000 // Chipselect 1, Ethernet 1 0 0x10160000 0x10000 // Chipselect 2, i2c controller 2 0 0x30000000 0x1000000>; // Chipselect 3, NOR Flash ethernet@0,0 { compatible = "smc,smc91c111"; reg = <0 0 0x1000>; interrupts = < 5 2 >; }; i2c@1,0 { compatible = "acme,a1234-i2c-bus"; #address-cells = <1>; #size-cells = <0>; reg = <1 0 0x1000>; rtc@58 { compatible = "maxim,ds1338"; reg = <58>; interrupts = < 7 3 >; }; }; flash@2,0 { compatible = "samsung,k8f1315ebm", "cfi-flash"; reg = <2 0 0x4000000>; }; }; };
上述.dts文件中, root结点"/"的compatible 属性compatible = "acme,coyotes-revenge";定义了系统的名称,它的组织形式为:<manufacturer>,<model>。
Linux内核透过root结点"/"的compatible 属性便可判断它启动的是什么machine。
如在arch/arm/boot/dts/vexpress-v2m.dtsi中的Flash结点:
flash@0,00000000 { compatible = "arm,vexpress-flash", "cfi-flash"; reg = <0 0x00000000 0x04000000>, <1 0x00000000 0x04000000>; bank-width = <4>; };
compatible属性的第2个字符串"cfi-flash"明显比第1个字符串"arm,vexpress-flash"涵盖的范围更广。
ranges = <0 0 0x10100000 0x10000 // Chipselect 1, Ethernet
1 0 0x10160000 0x10000 // Chipselect 2, i2c controller
2 0 0x30000000 0x1000000>; // Chipselect 3, NOR Flash
ranges是地址转换表,其中的每一个项目是一个子地址、父地址以及在子地址空间的大小的映射。映射表中的子地址、父地址分别采用子地址空间的#address-cells和父地址空间的#address-cells大小。
对于本例而言,子地址空间的#address-cells为2,父地址空间的#address-cells值为1,所以0 0 0x10100000 0x10000的前2个cell为external-bus后片选0上偏移0,第3个cell表示external-bus后片选0上偏移0的地址空间被映射到CPU的0x10100000位置,第4个cell表示映射的大小为0x10000。ranges的后面2个项目的含义能够类推。
printk(“now dts node name is %s\n",pdev->dev.of_node->name);
of_get_named_gpio_flags()
of_get_gpio_flags()
static struct i2c_board_info __initdata afeb9260_i2c_devices[] = { { I2C_BOARD_INFO("tlv320aic23", 0x1a), }, { I2C_BOARD_INFO("fm3130", 0x68), }, { I2C_BOARD_INFO("24c64", 0x50), } };
之类的i2c_board_info代码,目前再也不须要出现,如今只须要把tlv320aic2三、fm3130、24c64这些设备结点填充做为相应的I2C controller结点的子结点便可,相似于前面的
i2c@1,0 { compatible = "acme,a1234-i2c-bus"; … rtc@58 { compatible = "maxim,ds1338"; reg = <58>; interrupts = < 7 3 >; }; };
Device Tree中的I2C client会透过I2C host驱动的probe()函数中调用of_i2c_register_devices(&i2c_dev->adapter); 而后被自动展开。
void __iomem*of_iomap(struct device_node *node, int index)
int of_get_named_gpio_flags(struct device_node *np,const char *propname,
int index, enum of_gpio_flags *flags)
static inline int of_get_gpio_flags(structdevice_node *np, int index,
enum of_gpio_flags *flags)
{
return of_get_named_gpio_flags(np, "gpios", index,flags);
}
从设备树中读取相关GPIO的配置编号和标志,返回值为 gpio number。
dtb-$(CONFIG_ARCH_VEXPRESS) += vexpress-v2p-ca5s.dtb \
vexpress-v2p-ca9.dtb \
vexpress-v2p-ca15-tc1.dtb \
vexpress-v2p-ca15_a7.dtb \
xenvm-4.2.dtb
当咱们在Linux内核下运行make dtbs时,若以前选择了ARCH_VEXPRESS,上述.dtb都会由对应的.dts编译出来。由于arch/arm/Makefile中含有一个dtbs编译target项目。固然也能够单独编译Device Tree文件。命令由读者自行去找。