转载:Linux DTS 介绍

2019年12月30日 Jerry 3309 2019年12月31日

一. 设备树的由来

    1.1. 什么是设备树

        1.1.1. Device Tree 可以描述的信息包括CPU的数量和类别,内存基地址和大小,总线和桥,外设连接,中断控制器和中断使用情况,Clock控制器和Clock使用情况。

        1.1.2. 设备树信息被保持在一个ASCII文本文件中,适合人类阅读习惯。类似于XML文件,在ARM linux中,一个.dts文件对应一个arm的machine放置在内核的arch/arm/boot/dts/ 目录中

        1.1.3. 设备树是一种数据结构,用于描述设备信息的语言,具体而言,是用于操作系统中描述硬件,使得不需要对设备进行硬编码

        1.1.4. Devices Tree是由一系列被命名的节点(node)和属性(property)组成, 而节点本身可包含子节点。所谓属性,其实就是成对出现的name和value

        1.1.5. 设备树源文件dts被编译成dtb二进制,在bootloader运行时传递给操作系统,操作系统对其进行解析展开,从而产生一个硬件设备的拓扑图,有了这个拓扑图,在编成过程可以直接通过系统提供的接口获取到设备树的节点和属性信息

    1.2. 为什么需要设备树

        1.2.1 在linux 2.6中,arch/arm/plat-xxx和arch/arm/mach-xxx中充斥着大量的垃圾代码,相当于多数的代码只是在描述板级信息,而这些细节对于内核来说不过是垃圾。如板上的platform设备、resource、i2c_board_info、spi_board_info以及各种硬件platform_data。常见的s3c2410、s3c6410等板级目录,代码量在数万行。

        1.2.2. linus Torvalds对于此种情况大发雷霆,在2011年的ARM Linux邮件列表宣称this whole ARM thing is a f*cking pain in the ass”。所以Linux开发社区就开始整改,设备树最早用于PowerPC等其他体系架构,ARM架构开发社区就开始采用设备树来描述设备的信息

    1.3. 相关名称缩写

        <1>. DT   :Device Tree

        <2>. FDT : Flattened Device Tree

        <3>. OF   :Open Firmware

        <4>. DTS :device tree source

        <5>. DTSI:device tree source include

        <6>. DTB :device tree blob

        <7>. DTC :device tree compiler

二. 设备树的基本知识

    2.1. DTS加载过程

        如果要使用Device Tree,首先用户要了解自己的硬件配置和系统运行参数,并把这些信息组织成Device Tree source file。通过DTC(Device Tree Compiler),可以将这些适合人类阅读的Device Tree

source file变成适合机器处理的Device Tree binary file(有一个更好听的名字,DTB,device tree blob)。在系统启动的时候,boot program(例如:firmware、bootloader)可以将保存在flash中的DTB

copy到内存(当然也可以通过其他方式,例如可以通过bootloader的交互式命令加载DTB,或者firmware可以探测到device的信 息,组织成DTB保存在内存中),并把DTB的起始地址传递给client program(例

如OS kernel,bootloader或者其他特殊功能的程序)。对于计算机系统(computer system),一般是firmware->bootloader->OS,对于嵌入式系统,一般是bootloader->OS。

    2.2. DTS基本语法

         基本数据格式

        <1>. 设备树是节点和属性的简单树型结构。属性是键-值对,节点可以包含属性和子节点。

/ DTS-V1 /;
/ {
    node1 {
        a-string-property =“A string”;
        a-string-list-property =“first string”,“second string”;
        //十六进制隐含在字节数组中。不需要'0x'前缀.
        a-byte-data-property = [01 23 34 56];

        child-node1 {
            //boolean,first-child-property定义为true,不定义为false.
            first-child-boolean-property;
            second-child-cell-property = <1>;
            a-string-property =“Hello,world”;
    };
        child-node2 {

        };
    };
    node2 {
        an-empty-property;
        a-cell-property = <1 2 3 4>;
    };
};

        <2>. 这棵树显然没用,因为它没有描述任何东西,但它确实显示了节点和属性的结构。即: 

            - 单个根节点:“ / ” 
            - 几个子节点:“ node1 ”和“ node2 ” 
            - node1的几个孩子:“ child-node1 ”和“ child-node2 ” 
            - 一堆散落在树上的属性。

        <3>属性是简单的键值对,其中值可以为空或包含任意字节流。虽然数据类型未编码到数据结构中,但有一些基本数据表示可以在设备树源文件中表示。 
            - 文本字符串用双引号表示:

                string-property = “a string”;

            ‘Cells’是由尖括号分隔的32位无符号整数:
                cell-property = <0xbeef 123 0xabcd1234>;

            二进制数据用方括号分隔:
                binary-property = [0x01 0x23 0x45 0x67];

            可以使用逗号将不同表示的数据连接在一起:
                mixed-property = “a string”, [0x01 0x23 0x45 0x67], <0x12345678>;

            逗号还用于创建字符串列表:
                string-list = “red fish”, “blue fish”

    2.3. DTS基本构成

        下面以一个最简单的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";定义了系统的名称,它的组织形式 为:,。Linux内核透过root结点"/"的compatible 属性即可判断它启动的是什么machine。 在.dts文件的每个设备,都有一个compatible属性,compatible属性用户驱动和设备的绑定。compatible 属性是一个字符串的列表,列表中的第一个字符串表征了结点代表的确切设备,形式为",",其后的字符串表征可兼容的其他设备。

 


转载自:https://www.cnblogs.com/linux-37ge/p/11135617.html



微信
jerry微信赞助
支付宝
jerry支付宝赞助

您尚未登录,暂时无法评论。请先 登录 或者 注册

0 人参与 | 0 条评论