设备树之应用(一)

来源:互联网 发布:caffe 自定义数据层 编辑:程序博客网 时间:2024/05/21 16:57

参考http://devicetree.org/Device_Tree_Usage

本页面将介绍如何为新机器编写设备树。 它旨在提供设备树概念的概述以及它们如何用于描述机器。

有关设备树数据格式的完整技术说明,请参阅ePAPR v1.1规范。 ePAPR规范涵盖了本页面所涉及的基本主题的更多细节,请参考该页面以获得更多高级用途,本页面未涵盖。 ePAPR目前正在使用Devicetree规范文档的新名称进行更新。

1基本数据格式

设备树是节点和属性的简单树结构。 属性是键值对,节点可以包含属性和子节点。 例如,以下是.dts格式的简单树:

/dts-v1/;

 

/ {

    node1 {

       a-string-property = "A string";

       a-string-list-property = "first string", "secondstring";

        // hex isimplied in byte arrays. no '0x' prefix is required

       a-byte-data-property = [01 23 34 56];

        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{

        };

    };

};

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

·        a singleroot node: "/"

·        a coupleof child nodes: "node1" and "node2"

·        a coupleof children for node1: "child-node1" and "child-node2"

·        a bunch ofproperties scattered through the tree.

属性是简单的键值对,其中值可以是空的或包含任意字节流。 虽然数据类型未被编码到数据结构中,但是有一些基本的数据表示可以在设备树源文件中表达。

·        Textstrings (null terminated) are represented with double quotes:

  ·        string-property = "a string";

·        'Cells'are 32 bit unsigned integers delimited by angle brackets:

  ·        cell-property = <0xbeef 123 0xabcd1234>;

·        Binarydata is delimited with square brackets:

  ·        binary-property = [0x01 0x23 0x45 0x67];

·        Data ofdiffering representations can be concatenated together using a comma:

  ·        mixed-property = "a string", [0x01 0x23 0x45 0x67],<0x12345678>;

·        Commas arealso used to create lists of strings:

  ·        string-list = "red fish", "blue fish";

 

2基本概念

要了解如何使用设备树,我们将从一个简单的机器开始,并建立一个设备树来逐步描述它。

2.1样机

Consider the following imaginary machine (loosely basedon ARM Versatile), manufactured by "Acme" and named "Coyote'sRevenge":

·        One 32bit ARM CPU

·        processor local bus attached to memory mapped serialport, spi bus controller, i2c controller, interrupt controller, and externalbus bridge

·        256MB of SDRAM based at 0

·        2 Serial ports based at 0x101F1000 and 0x101F2000

·        GPIO controller based at 0x101F3000

·        SPI controller based at 0x10170000 with following devices

·        MMC slot with SS pin attached to GPIO #1

·        External bus bridge with following devices

·        SMCSMC91111 Ethernet device attached to external bus based at 0x10100000

·        i2ccontroller based at 0x10160000 with following devices

·        MaximDS1338 real time clock. Responds to slave address 1101000 (0x58)

·        64MB ofNOR flash based at 0x30000000

 

2.2初始结构

第一步是为机器铺设骨架结构。 这是有效设备树所需的最低限度结构。 在这个阶段你要唯一识别机器。

/dts-v1/;

 

/ {

    compatible ="acme,coyotes-revenge";

};

compatible指定系统的名称。 它包含一个字符串,形式为“<manufacturer>,<model>”。重要的是指定确切的设备,并包括制造商名称以避免命名空间冲突。由于操作系统将使用兼容值来决定如何 在机器上运行,将正确的数据放入此属性非常重要。

理论上,兼容是操作系统唯一识别机器所需的所有数据。 如果所有的机器细节都是硬编码的,那么操作系统可以专门查看顶级兼容属性中的“acme,coyotes-revenge”。

 

2.3 CPUs

下一步是描述每个CPU。 名为“cpus”的容器节点与每个CPU的子节点相加。 在这种情况下,该系统是ARM的双核Cortex A9系统。

/dts-v1/;

 

/ {

    compatible ="acme,coyotes-revenge";

 

    cpus {

        cpu@0 {

           compatible = "arm,cortex-a9";

        };

        cpu@1 {

           compatible = "arm,cortex-a9";

        };

    };

};

每个cpu节点中的兼容属性是一个字符串,它以<制造商>,<模型>的形式指定精确的cpu模型,就像顶层的兼容属性一样。

稍后将更多的属性添加到cpu节点,但是我们首先需要谈谈更多的基本概念。

 

2.4节点名称

要花点时间谈谈命名约定。 每个节点必须有一个名称,格式为<name> [@ <unit-address>]。

<name>是一个简单的ascii字符串,长度最多可达31个字符。 一般来说,节点根据它所代表的设备类型命名。 即 3com以太网适配器的节点将使用以太网名称,而不是3com509。

如果节点描述具有地址的设备,则包含单位地址。 通常,单元地址是用于访问设备的主地址,并列在节点的reg属性中。 我们稍后将在本文档中介绍reg属性。

有关节点命名的详细信息,请参阅ePAPR规范的2.2.1节。

 

2.5设备

系统中的每个设备都由设备树节点表示。 下一步是使用每个设备的节点填充树。 现在,新节点将保持为空,直到我们讨论如何处理地址范围和irq。

/dts-v1/;

 

/ {

    compatible ="acme,coyotes-revenge";

 

    cpus {

        cpu@0 {

            compatible ="arm,cortex-a9";

        };

        cpu@1 {

            compatible ="arm,cortex-a9";

        };

    };

 

    serial@101F0000 {

        compatible= "arm,pl011";

    };

 

    serial@101F2000{

        compatible= "arm,pl011";

    };

 

    gpio@101F3000 {

        compatible= "arm,pl061";

    };

 

   interrupt-controller@10140000 {

        compatible= "arm,pl190";

    };

 

    spi@10115000 {

        compatible= "arm,pl022";

    };

 

    external-bus {

       ethernet@0,0 {

            compatible= "smc,smc91c111";

        };

 

        i2c@1,0 {

           compatible = "acme,a1234-i2c-bus";

            rtc@58{

               compatible = "maxim,ds1338";

            };

        };

 

        flash@2,0 {

            compatible= "samsung,k8f1315ebm", "cfi-flash";

        };

    };

};

在此树中,为系统中的每个设备添加了一个节点,层次结构反映了设备如何连接到系统。 即 外部总线上的设备是外部总线节点的子节点,i2c设备是i2c总线控制器节点的子节点。 一般来说,层次结构表示从CPU的角度看系统的视图。

到目前位置,设备树还无效。 它缺少有关设备之间连接的信息。 该数据将在以后添加。

在这棵树上要注意的一些事情:

·        Every device node has a compatible property.

·        The flash node has 2 strings in the compatible property.Read on to the next section to learn why.

·        As mentioned earlier, node names reflect the type ofdevice, not the particular model. See section 2.2.2 of the ePAPR spec for alist of defined generic node names that should be used wherever possible.

 

2.6了解兼容属性

代表设备的树中的每个节点都需要具有compatible属性。compatible是操作系统用于决定将哪个设备驱动程序绑定到设备的关键。 compatible是字符串列表。 列表中的第一个字符串指定节点以“<manufacturer>,<model>”的形式表示的确切设备。 以下字符串表示设备兼容的其他设备。

例如,飞思卡尔MPC8349片上系统(SoC)具有实现National Semiconductor ns16550寄存器接口的串行设备。因此MPC8349串行设备的兼容属性应为:compatible =“fsl,mpc8349-uart”,“ns16550”。在这种情况下,fsl,mpc8349-uart指定了精确的器件,而ns16550表示它与National Semiconductor 16550 UART的寄存器级兼容。(PS:只有出于历史原因,ns16550没有制造商前缀。 所有新的兼容值应使用制造商前缀)

This practice allowsexisting device drivers to be bound to a newer device, while still uniquelyidentifying the exact hardware.

warning:不要使用通配符兼容的值,如“fsl,mpc83xx-uart”或类似的值。

    node2 {

       an-empty-property;

       a-cell-property = <1 2 3 4>; /* each number (cell) is a uint32 */

        child-node1{

        };

    };

};

0 0
原创粉丝点击