ARM Linux 3.x 的設備樹(Device Tree)之DTB、DTS

ARM 224瀏覽

1、ARM Device Tree起源

Linus Torvalds在2011年3月17日的ARM Linux郵件列表宣稱“this whole ARM thing is a f*cking pain in the ass”,引發ARM Linux社區的地震,隨后ARM社區進行了一系列的重大修正。在過去的ARM Linux中,arch/arm/plat-xxx和arch/arm/mach-xxx中充斥著大量的垃圾代碼,相當多數的代碼只是在描述板級細節,而這些板級細節對于內核來講,不過是垃圾,如板上的platform設備、resource、i2c_board_info、spi_board_info以及各種硬件的platform_data。讀者有興趣可以統計下常見的s3c2410、s3c6410等板級目錄,代碼量在數萬行。 

社區必須改變這種局面,于是PowerPC等其他體系架構下已經使用的Flattened Device Tree(FDT)進入ARM社區的視野。Device Tree是一種描述硬件的數據結構,它起源于 OpenFirmware (OF)。在Linux 2.6中,ARM架構的板極硬件細節過多地被硬編碼在arch/arm/plat-xxx和arch/arm/mach-xxx,采用Device Tree后,許多硬件的細節可以直接透過它傳遞給Linux,而不再需要在kernel中進行大量的冗余編碼。 

Device Tree由一系列被命名的結點(node)和屬性(property)組成,而結點本身可包含子結點。所謂屬性,其實就是成對出現的name和value。在Device Tree中,可描述的信息包括(原先這些信息大多被hard code到kernel中):

CPU的數量和類別 

內存基地址和大小 

總線和橋 

外設連接 

中斷控制器和中斷使用情況 

GPIO控制器和GPIO使用情況 

Clock控制器和Clock使用情況

它基本上就是畫一棵電路板上CPU、總線、設備組成的樹,Bootloader會將這棵樹傳遞給內核,然后內核可以識別這棵樹,并根據它展開出Linux內核中的platform_device、i2c_client、spi_device等設備,而這些設備用到的內存、IRQ等資源,也被傳遞給了內核,內核會將這些資源綁定給展開的相應的設備。

2、Device Tree組成和結構

整個Device Tree牽涉面比較廣,即增加了新的用于描述設備硬件信息的文本格式,又增加了編譯這一文本的工具,同時Bootloader也需要支持將編譯后的Device Tree傳遞給Linux內核。 

DTS (device tree source) 

.dts文件是一種ASCII 文本格式的Device Tree描述,此文本格式非常人性化,適合人類的閱讀習慣。基本上,在ARM Linux在,一個.dts文件對應一個ARM的machine,一般放置在內核的arch/arm/boot/dts/目錄。由于一個SoC可能對應多個machine(一個SoC可以對應多個產品和電路板),勢必這些.dts文件需包含許多共同的部分,Linux內核為了簡化,把SoC公用的部分或者多個machine共同的部分一般提煉為.dtsi,類似于C語言的頭文件。其他的machine對應的.dts就include這個.dtsi。譬如,對于VEXPRESS而言,vexpress-v2m.dtsi就被vexpress-v2p-ca9.dts所引用,
vexpress-v2p-ca9.dts有如下一行: 

/include/ “vexpress-v2m.dtsi” 

當然,和C語言的頭文件類似,.dtsi也可以include其他的.dtsi,譬如幾乎所有的ARM SoC的.dtsi都引用了skeleton.dtsi。 

.dts(或者其include的.dtsi)基本元素即為前文所述的結點和屬性:

<code class="hljs applescript has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;">/ {
    node1 {
        a-<span class="hljs-type" style="box-sizing: border-box;">string</span>-<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">property</span> = <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"A string"</span>;
        a-<span class="hljs-type" style="box-sizing: border-box;">string</span>-<span class="hljs-type" style="box-sizing: border-box;">list</span>-<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">property</span> = <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"first string"</span>, <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"second string"</span>;
        a-byte-data-<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">property</span> = [<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0x01</span> <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0x23</span> <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0x34</span> <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0x56</span>];
        child-node1 {
            <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">first</span>-child-<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">property</span>;
            <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">second</span>-child-<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">property</span> = <<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">1</span>>;
            a-<span class="hljs-type" style="box-sizing: border-box;">string</span>-<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">property</span> = <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"Hello, world"</span>;
        };
        child-node2 {
        };
    };
    node2 {
        an-empty-<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">property</span>;
        a-cell-<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">property</span> = <<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">1</span> <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">2</span> <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">3</span> <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">4</span>>; /* each <span class="hljs-type" style="box-sizing: border-box;">number</span> (cell) <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">is</span> a uint32 */
        child-node1 {
        };
    };
};</code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li><li style="box-sizing: border-box; padding: 0px 5px;">4</li><li style="box-sizing: border-box; padding: 0px 5px;">5</li><li style="box-sizing: border-box; padding: 0px 5px;">6</li><li style="box-sizing: border-box; padding: 0px 5px;">7</li><li style="box-sizing: border-box; padding: 0px 5px;">8</li><li style="box-sizing: border-box; padding: 0px 5px;">9</li><li style="box-sizing: border-box; padding: 0px 5px;">10</li><li style="box-sizing: border-box; padding: 0px 5px;">11</li><li style="box-sizing: border-box; padding: 0px 5px;">12</li><li style="box-sizing: border-box; padding: 0px 5px;">13</li><li style="box-sizing: border-box; padding: 0px 5px;">14</li><li style="box-sizing: border-box; padding: 0px 5px;">15</li><li style="box-sizing: border-box; padding: 0px 5px;">16</li><li style="box-sizing: border-box; padding: 0px 5px;">17</li><li style="box-sizing: border-box; padding: 0px 5px;">18</li><li style="box-sizing: border-box; padding: 0px 5px;">19</li><li style="box-sizing: border-box; padding: 0px 5px;">20</li></ul>

上述.dts文件并沒有什么真實的用途,但它基本表征了一個Device Tree源文件的結構: 

1個root結點”/”; 

root結點下面含一系列子結點,本例中為”node1” 和 “node2”; 

結點”node1”下又含有一系列子結點,本例中為”child-node1” 和 “child-node2”; 

各結點都有一系列屬性。這些屬性可能為空,如” an-empty-property”;可能為字符串,如”a-string-property”;可能為字符串數組,如”a-string-list-property”;可能為Cells(由u32整數組成),如”second-child-property”,可能為二進制數,如”a-byte-data-property”。 

下面以一個最簡單的machine為例來看如何寫一個.dts文件。假設此machine的配置如下: 

1個雙核ARM Cortex-A9 32位處理器; 

ARM的local bus上的內存映射區域分布了2個串口(分別位于0x101F1000 和 0x101F2000)、GPIO控制器(位于0x101F3000)、SPI控制器(位于0x10170000)、中斷控制器(位于0x10140000)和一個external bus橋; 

External bus橋上又連接了SMC SMC91111 Ethernet(位于0x10100000)、I2C控制器(位于0x10160000)、64MB NOR Flash(位于0x30000000); 

External bus橋上連接的I2C控制器所對應的I2C總線上又連接了Maxim DS1338實時鐘(I2C地址為0x58)。 

其對應的.dts文件為:

<code class="hljs lasso has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;"><span class="hljs-subst" style="color: rgb(0, 0, 0); box-sizing: border-box;">/</span> {
    compatible <span class="hljs-subst" style="color: rgb(0, 0, 0); box-sizing: border-box;">=</span> <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"acme,coyotes-revenge"</span>;
    <span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">#address</span><span class="hljs-attribute" style="box-sizing: border-box;">-cells</span> <span class="hljs-subst" style="color: rgb(0, 0, 0); box-sizing: border-box;">=</span> <span class="hljs-subst" style="color: rgb(0, 0, 0); box-sizing: border-box;"><</span><span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">1</span><span class="hljs-subst" style="color: rgb(0, 0, 0); box-sizing: border-box;">></span>;
    <span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">#size</span><span class="hljs-attribute" style="box-sizing: border-box;">-cells</span> <span class="hljs-subst" style="color: rgb(0, 0, 0); box-sizing: border-box;">=</span> <span class="hljs-subst" style="color: rgb(0, 0, 0); box-sizing: border-box;"><</span><span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">1</span><span class="hljs-subst" style="color: rgb(0, 0, 0); box-sizing: border-box;">></span>;
    interrupt<span class="hljs-attribute" style="box-sizing: border-box;">-parent</span> <span class="hljs-subst" style="color: rgb(0, 0, 0); box-sizing: border-box;">=</span> <span class="hljs-subst" style="color: rgb(0, 0, 0); box-sizing: border-box;"><&</span>intc<span class="hljs-subst" style="color: rgb(0, 0, 0); box-sizing: border-box;">></span>;

    cpus {
        <span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">#address</span><span class="hljs-attribute" style="box-sizing: border-box;">-cells</span> <span class="hljs-subst" style="color: rgb(0, 0, 0); box-sizing: border-box;">=</span> <span class="hljs-subst" style="color: rgb(0, 0, 0); box-sizing: border-box;"><</span><span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">1</span><span class="hljs-subst" style="color: rgb(0, 0, 0); box-sizing: border-box;">></span>;
        <span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">#size</span><span class="hljs-attribute" style="box-sizing: border-box;">-cells</span> <span class="hljs-subst" style="color: rgb(0, 0, 0); box-sizing: border-box;">=</span> <span class="hljs-subst" style="color: rgb(0, 0, 0); box-sizing: border-box;"><</span><span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0</span><span class="hljs-subst" style="color: rgb(0, 0, 0); box-sizing: border-box;">></span>;
        [email protected]<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0</span> {
            compatible <span class="hljs-subst" style="color: rgb(0, 0, 0); box-sizing: border-box;">=</span> <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"arm,cortex-a9"</span>;
            reg <span class="hljs-subst" style="color: rgb(0, 0, 0); box-sizing: border-box;">=</span> <span class="hljs-subst" style="color: rgb(0, 0, 0); box-sizing: border-box;"><</span><span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0</span><span class="hljs-subst" style="color: rgb(0, 0, 0); box-sizing: border-box;">></span>;
        };
        [email protected]<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">1</span> {
            compatible <span class="hljs-subst" style="color: rgb(0, 0, 0); box-sizing: border-box;">=</span> <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"arm,cortex-a9"</span>;
            reg <span class="hljs-subst" style="color: rgb(0, 0, 0); box-sizing: border-box;">=</span> <span class="hljs-subst" style="color: rgb(0, 0, 0); box-sizing: border-box;"><</span><span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">1</span><span class="hljs-subst" style="color: rgb(0, 0, 0); box-sizing: border-box;">></span>;
        };
    };

    [email protected]<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">101</span>f0000 {
        compatible <span class="hljs-subst" style="color: rgb(0, 0, 0); box-sizing: border-box;">=</span> <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"arm,pl011"</span>;
        reg <span class="hljs-subst" style="color: rgb(0, 0, 0); box-sizing: border-box;">=</span> <span class="hljs-subst" style="color: rgb(0, 0, 0); box-sizing: border-box;"><</span><span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0x101f0000</span> <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0x1000</span> <span class="hljs-subst" style="color: rgb(0, 0, 0); box-sizing: border-box;">></span>;
        interrupts <span class="hljs-subst" style="color: rgb(0, 0, 0); box-sizing: border-box;">=</span> <span class="hljs-subst" style="color: rgb(0, 0, 0); box-sizing: border-box;"><</span> <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">1</span> <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0</span> <span class="hljs-subst" style="color: rgb(0, 0, 0); box-sizing: border-box;">></span>;
    };

    [email protected]<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">101</span>f2000 {
        compatible <span class="hljs-subst" style="color: rgb(0, 0, 0); box-sizing: border-box;">=</span> <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"arm,pl011"</span>;
        reg <span class="hljs-subst" style="color: rgb(0, 0, 0); box-sizing: border-box;">=</span> <span class="hljs-subst" style="color: rgb(0, 0, 0); box-sizing: border-box;"><</span><span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0x101f2000</span> <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0x1000</span> <span class="hljs-subst" style="color: rgb(0, 0, 0); box-sizing: border-box;">></span>;
        interrupts <span class="hljs-subst" style="color: rgb(0, 0, 0); box-sizing: border-box;">=</span> <span class="hljs-subst" style="color: rgb(0, 0, 0); box-sizing: border-box;"><</span> <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">2</span> <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0</span> <span class="hljs-subst" style="color: rgb(0, 0, 0); box-sizing: border-box;">></span>;
    };

    [email protected]<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">101</span>f3000 {
        compatible <span class="hljs-subst" style="color: rgb(0, 0, 0); box-sizing: border-box;">=</span> <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"arm,pl061"</span>;
        reg <span class="hljs-subst" style="color: rgb(0, 0, 0); box-sizing: border-box;">=</span> <span class="hljs-subst" style="color: rgb(0, 0, 0); box-sizing: border-box;"><</span><span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0x101f3000</span> <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0x1000</span>
               <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0x101f4000</span> <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0x0010</span><span class="hljs-subst" style="color: rgb(0, 0, 0); box-sizing: border-box;">></span>;
        interrupts <span class="hljs-subst" style="color: rgb(0, 0, 0); box-sizing: border-box;">=</span> <span class="hljs-subst" style="color: rgb(0, 0, 0); box-sizing: border-box;"><</span> <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">3</span> <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0</span> <span class="hljs-subst" style="color: rgb(0, 0, 0); box-sizing: border-box;">></span>;
    };

    intc: interrupt<span class="hljs-attribute" style="box-sizing: border-box;">-controller</span>@<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">10140000</span> {
        compatible <span class="hljs-subst" style="color: rgb(0, 0, 0); box-sizing: border-box;">=</span> <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"arm,pl190"</span>;
        reg <span class="hljs-subst" style="color: rgb(0, 0, 0); box-sizing: border-box;">=</span> <span class="hljs-subst" style="color: rgb(0, 0, 0); box-sizing: border-box;"><</span><span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0x10140000</span> <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0x1000</span> <span class="hljs-subst" style="color: rgb(0, 0, 0); box-sizing: border-box;">></span>;
        interrupt<span class="hljs-attribute" style="box-sizing: border-box;">-controller</span>;
        <span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">#interrupt</span><span class="hljs-attribute" style="box-sizing: border-box;">-cells</span> <span class="hljs-subst" style="color: rgb(0, 0, 0); box-sizing: border-box;">=</span> <span class="hljs-subst" style="color: rgb(0, 0, 0); box-sizing: border-box;"><</span><span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">2</span><span class="hljs-subst" style="color: rgb(0, 0, 0); box-sizing: border-box;">></span>;
    };

    [email protected]<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">10115000</span> {
        compatible <span class="hljs-subst" style="color: rgb(0, 0, 0); box-sizing: border-box;">=</span> <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"arm,pl022"</span>;
        reg <span class="hljs-subst" style="color: rgb(0, 0, 0); box-sizing: border-box;">=</span> <span class="hljs-subst" style="color: rgb(0, 0, 0); box-sizing: border-box;"><</span><span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0x10115000</span> <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0x1000</span> <span class="hljs-subst" style="color: rgb(0, 0, 0); box-sizing: border-box;">></span>;
        interrupts <span class="hljs-subst" style="color: rgb(0, 0, 0); box-sizing: border-box;">=</span> <span class="hljs-subst" style="color: rgb(0, 0, 0); box-sizing: border-box;"><</span> <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">4</span> <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0</span> <span class="hljs-subst" style="color: rgb(0, 0, 0); box-sizing: border-box;">></span>;
    };

    external<span class="hljs-attribute" style="box-sizing: border-box;">-bus</span> {
        <span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">#address</span><span class="hljs-attribute" style="box-sizing: border-box;">-cells</span> <span class="hljs-subst" style="color: rgb(0, 0, 0); box-sizing: border-box;">=</span> <span class="hljs-subst" style="color: rgb(0, 0, 0); box-sizing: border-box;"><</span><span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">2</span><span class="hljs-subst" style="color: rgb(0, 0, 0); box-sizing: border-box;">></span>
        <span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">#size</span><span class="hljs-attribute" style="box-sizing: border-box;">-cells</span> <span class="hljs-subst" style="color: rgb(0, 0, 0); box-sizing: border-box;">=</span> <span class="hljs-subst" style="color: rgb(0, 0, 0); box-sizing: border-box;"><</span><span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">1</span><span class="hljs-subst" style="color: rgb(0, 0, 0); box-sizing: border-box;">></span>;
        ranges <span class="hljs-subst" style="color: rgb(0, 0, 0); box-sizing: border-box;">=</span> <span class="hljs-subst" style="color: rgb(0, 0, 0); box-sizing: border-box;"><</span><span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0</span> <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0</span>  <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0x10100000</span>   <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0x10000</span>     <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">// Chipselect 1, Ethernet</span>
                  <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">1</span> <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0</span>  <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0x10160000</span>   <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0x10000</span>     <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">// Chipselect 2, i2c controller</span>
                  <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">2</span> <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0</span>  <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0x30000000</span>   <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0x1000000</span><span class="hljs-subst" style="color: rgb(0, 0, 0); box-sizing: border-box;">></span>; <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">// Chipselect 3, NOR Flash</span>

        [email protected]<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0</span>,<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0</span> {
            compatible <span class="hljs-subst" style="color: rgb(0, 0, 0); box-sizing: border-box;">=</span> <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"smc,smc91c111"</span>;
            reg <span class="hljs-subst" style="color: rgb(0, 0, 0); box-sizing: border-box;">=</span> <span class="hljs-subst" style="color: rgb(0, 0, 0); box-sizing: border-box;"><</span><span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0</span> <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0</span> <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0x1000</span><span class="hljs-subst" style="color: rgb(0, 0, 0); box-sizing: border-box;">></span>;
            interrupts <span class="hljs-subst" style="color: rgb(0, 0, 0); box-sizing: border-box;">=</span> <span class="hljs-subst" style="color: rgb(0, 0, 0); box-sizing: border-box;"><</span> <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">5</span> <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">2</span> <span class="hljs-subst" style="color: rgb(0, 0, 0); box-sizing: border-box;">></span>;
        };

        [email protected]<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">1</span>,<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0</span> {
            compatible <span class="hljs-subst" style="color: rgb(0, 0, 0); box-sizing: border-box;">=</span> <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"acme,a1234-i2c-bus"</span>;
            <span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">#address</span><span class="hljs-attribute" style="box-sizing: border-box;">-cells</span> <span class="hljs-subst" style="color: rgb(0, 0, 0); box-sizing: border-box;">=</span> <span class="hljs-subst" style="color: rgb(0, 0, 0); box-sizing: border-box;"><</span><span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">1</span><span class="hljs-subst" style="color: rgb(0, 0, 0); box-sizing: border-box;">></span>;
            <span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">#size</span><span class="hljs-attribute" style="box-sizing: border-box;">-cells</span> <span class="hljs-subst" style="color: rgb(0, 0, 0); box-sizing: border-box;">=</span> <span class="hljs-subst" style="color: rgb(0, 0, 0); box-sizing: border-box;"><</span><span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0</span><span class="hljs-subst" style="color: rgb(0, 0, 0); box-sizing: border-box;">></span>;
            reg <span class="hljs-subst" style="color: rgb(0, 0, 0); box-sizing: border-box;">=</span> <span class="hljs-subst" style="color: rgb(0, 0, 0); box-sizing: border-box;"><</span><span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">1</span> <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0</span> <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0x1000</span><span class="hljs-subst" style="color: rgb(0, 0, 0); box-sizing: border-box;">></span>;
            interrupts <span class="hljs-subst" style="color: rgb(0, 0, 0); box-sizing: border-box;">=</span> <span class="hljs-subst" style="color: rgb(0, 0, 0); box-sizing: border-box;"><</span> <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">6</span> <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">2</span> <span class="hljs-subst" style="color: rgb(0, 0, 0); box-sizing: border-box;">></span>;
            [email protected]<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">58</span> {
                compatible <span class="hljs-subst" style="color: rgb(0, 0, 0); box-sizing: border-box;">=</span> <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"maxim,ds1338"</span>;
                reg <span class="hljs-subst" style="color: rgb(0, 0, 0); box-sizing: border-box;">=</span> <span class="hljs-subst" style="color: rgb(0, 0, 0); box-sizing: border-box;"><</span><span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">58</span><span class="hljs-subst" style="color: rgb(0, 0, 0); box-sizing: border-box;">></span>;
                interrupts <span class="hljs-subst" style="color: rgb(0, 0, 0); box-sizing: border-box;">=</span> <span class="hljs-subst" style="color: rgb(0, 0, 0); box-sizing: border-box;"><</span> <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">7</span> <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">3</span> <span class="hljs-subst" style="color: rgb(0, 0, 0); box-sizing: border-box;">></span>;
            };
        };

        [email protected]<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">2</span>,<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0</span> {
            compatible <span class="hljs-subst" style="color: rgb(0, 0, 0); box-sizing: border-box;">=</span> <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"samsung,k8f1315ebm"</span>, <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"cfi-flash"</span>;
            reg <span class="hljs-subst" style="color: rgb(0, 0, 0); box-sizing: border-box;">=</span> <span class="hljs-subst" style="color: rgb(0, 0, 0); box-sizing: border-box;"><</span><span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">2</span> <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0</span> <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0x4000000</span><span class="hljs-subst" style="color: rgb(0, 0, 0); box-sizing: border-box;">></span>;
        };
    };
};</code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li><li style="box-sizing: border-box; padding: 0px 5px;">4</li><li style="box-sizing: border-box; padding: 0px 5px;">5</li><li style="box-sizing: border-box; padding: 0px 5px;">6</li><li style="box-sizing: border-box; padding: 0px 5px;">7</li><li style="box-sizing: border-box; padding: 0px 5px;">8</li><li style="box-sizing: border-box; padding: 0px 5px;">9</li><li style="box-sizing: border-box; padding: 0px 5px;">10</li><li style="box-sizing: border-box; padding: 0px 5px;">11</li><li style="box-sizing: border-box; padding: 0px 5px;">12</li><li style="box-sizing: border-box; padding: 0px 5px;">13</li><li style="box-sizing: border-box; padding: 0px 5px;">14</li><li style="box-sizing: border-box; padding: 0px 5px;">15</li><li style="box-sizing: border-box; padding: 0px 5px;">16</li><li style="box-sizing: border-box; padding: 0px 5px;">17</li><li style="box-sizing: border-box; padding: 0px 5px;">18</li><li style="box-sizing: border-box; padding: 0px 5px;">19</li><li style="box-sizing: border-box; padding: 0px 5px;">20</li><li style="box-sizing: border-box; padding: 0px 5px;">21</li><li style="box-sizing: border-box; padding: 0px 5px;">22</li><li style="box-sizing: border-box; padding: 0px 5px;">23</li><li style="box-sizing: border-box; padding: 0px 5px;">24</li><li style="box-sizing: border-box; padding: 0px 5px;">25</li><li style="box-sizing: border-box; padding: 0px 5px;">26</li><li style="box-sizing: border-box; padding: 0px 5px;">27</li><li style="box-sizing: border-box; padding: 0px 5px;">28</li><li style="box-sizing: border-box; padding: 0px 5px;">29</li><li style="box-sizing: border-box; padding: 0px 5px;">30</li><li style="box-sizing: border-box; padding: 0px 5px;">31</li><li style="box-sizing: border-box; padding: 0px 5px;">32</li><li style="box-sizing: border-box; padding: 0px 5px;">33</li><li style="box-sizing: border-box; padding: 0px 5px;">34</li><li style="box-sizing: border-box; padding: 0px 5px;">35</li><li style="box-sizing: border-box; padding: 0px 5px;">36</li><li style="box-sizing: border-box; padding: 0px 5px;">37</li><li style="box-sizing: border-box; padding: 0px 5px;">38</li><li style="box-sizing: border-box; padding: 0px 5px;">39</li><li style="box-sizing: border-box; padding: 0px 5px;">40</li><li style="box-sizing: border-box; padding: 0px 5px;">41</li><li style="box-sizing: border-box; padding: 0px 5px;">42</li><li style="box-sizing: border-box; padding: 0px 5px;">43</li><li style="box-sizing: border-box; padding: 0px 5px;">44</li><li style="box-sizing: border-box; padding: 0px 5px;">45</li><li style="box-sizing: border-box; padding: 0px 5px;">46</li><li style="box-sizing: border-box; padding: 0px 5px;">47</li><li style="box-sizing: border-box; padding: 0px 5px;">48</li><li style="box-sizing: border-box; padding: 0px 5px;">49</li><li style="box-sizing: border-box; padding: 0px 5px;">50</li><li style="box-sizing: border-box; padding: 0px 5px;">51</li><li style="box-sizing: border-box; padding: 0px 5px;">52</li><li style="box-sizing: border-box; padding: 0px 5px;">53</li><li style="box-sizing: border-box; padding: 0px 5px;">54</li><li style="box-sizing: border-box; padding: 0px 5px;">55</li><li style="box-sizing: border-box; padding: 0px 5px;">56</li><li style="box-sizing: border-box; padding: 0px 5px;">57</li><li style="box-sizing: border-box; padding: 0px 5px;">58</li><li style="box-sizing: border-box; padding: 0px 5px;">59</li><li style="box-sizing: border-box; padding: 0px 5px;">60</li><li style="box-sizing: border-box; padding: 0px 5px;">61</li><li style="box-sizing: border-box; padding: 0px 5px;">62</li><li style="box-sizing: border-box; padding: 0px 5px;">63</li><li style="box-sizing: border-box; padding: 0px 5px;">64</li><li style="box-sizing: border-box; padding: 0px 5px;">65</li><li style="box-sizing: border-box; padding: 0px 5px;">66</li><li style="box-sizing: border-box; padding: 0px 5px;">67</li><li style="box-sizing: border-box; padding: 0px 5px;">68</li><li style="box-sizing: border-box; padding: 0px 5px;">69</li><li style="box-sizing: border-box; padding: 0px 5px;">70</li><li style="box-sizing: border-box; padding: 0px 5px;">71</li><li style="box-sizing: border-box; padding: 0px 5px;">72</li><li style="box-sizing: border-box; padding: 0px 5px;">73</li><li style="box-sizing: border-box; padding: 0px 5px;">74</li><li style="box-sizing: border-box; padding: 0px 5px;">75</li><li style="box-sizing: border-box; padding: 0px 5px;">76</li><li style="box-sizing: border-box; padding: 0px 5px;">77</li><li style="box-sizing: border-box; padding: 0px 5px;">78</li><li style="box-sizing: border-box; padding: 0px 5px;">79</li><li style="box-sizing: border-box; padding: 0px 5px;">80</li><li style="box-sizing: border-box; padding: 0px 5px;">81</li><li style="box-sizing: border-box; padding: 0px 5px;">82</li><li style="box-sizing: border-box; padding: 0px 5px;">83</li></ul>

上述.dts文件中,root結點”/”的compatible 屬性compatible = “acme,coyotes-revenge”;定義了系統的名稱,它的組織形式為:,。Linux內核透過root結點”/”的compatible 屬性即可判斷它啟動的是什么machine。 

在.dts文件的每個設備,都有一個compatible 屬性,compatible屬性用戶驅動和設備的綁定。compatible 屬性是一個字符串的列表,列表中的第一個字符串表征了結點代表的確切設備,形式為”,”,其后的字符串表征可兼容的其他設備。可以說前面的是特指,后面的則涵蓋更廣的范圍。如在arch/arm/boot/dts/vexpress-v2m.dtsi中的Flash結點:

<code class="hljs ruby has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;">flash<span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">@0</span>,<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">00000000</span> {
     compatible = <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"arm,vexpress-flash"</span>, <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"cfi-flash"</span>;
     reg = <<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0</span> <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0x00000000</span> <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0x04000000</span>>,
     <<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">1</span> <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0x00000000</span> <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0x04000000</span>>;
     bank-width = <<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">4</span>>;
 };</code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li><li style="box-sizing: border-box; padding: 0px 5px;">4</li><li style="box-sizing: border-box; padding: 0px 5px;">5</li><li style="box-sizing: border-box; padding: 0px 5px;">6</li></ul>

compatible屬性的第2個字符串”cfi-flash”明顯比第1個字符串”arm,vexpress-flash”涵蓋的范圍更廣。 

再比如,Freescale MPC8349 SoC含一個串口設備,它實現了國家半導體(National Semiconductor)的ns16550 寄存器接口。則MPC8349串口設備的compatible屬性為compatible = “fsl,mpc8349-uart”, “ns16550”。其中,fsl,mpc8349-uart指代了確切的設備, ns16550代表該設備與National Semiconductor 的16550 UART保持了寄存器兼容。 

接下來root結點”/”的cpus子結點下面又包含2個cpu子結點,描述了此machine上的2個CPU,并且二者的compatible 屬性為”arm,cortex-a9”。 

注意cpus和cpus的2個cpu子結點的命名,它們遵循的組織形式為:[@],<>中的內容是必選項,[]中的則為可選項。name是一個ASCII字符串,用于描述結點對應的設備類型,如3com Ethernet適配器對應的結點name宜為ethernet,而不是3com509。如果一個結點描述的設備有地址,則應該給出@unit-address。多個相同類型設備結點的name可以一樣,只要unit-address不同即可,如本例中含有[email protected][email protected]以及[email protected][email protected]這樣的同名結點。設備的unit-address地址也經常在其對應結點的reg屬性中給出。ePAPR標準給出了結點命名的規范。 

可尋址的設備使用如下信息來在Device Tree中編碼地址信息: 

reg 

#address-cells 

#size-cells 

其中reg的組織形式為reg =

<code class="hljs cs has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;"> ranges = <<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0</span> <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0</span>  <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0x10100000</span>   <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0x10000</span>     <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">// Chipselect 1, Ethernet</span>
 <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">1</span> <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0</span>  <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0x10160000</span>   <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0x10000</span>     <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">// Chipselect 2, i2c controller</span>
 <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">2</span> <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0</span>  <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0x30000000</span>   <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0x1000000</span>>; <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">// Chipselect 3, NOR Flash</span></code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li></ul>

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個項目的含義可以類推。

Device Tree中還可以中斷連接信息,對于中斷控制器而言,它提供如下屬性:

interrupt-controller – 這個屬性為空,中斷控制器應該加上此屬性表明自己的身份;

interrupt-cells – 與#address-cells 和 #size-cells相似,它表明連接此中斷控制器的設備的interrupts屬性的cell大小。 

在整個Device Tree中,與中斷相關的屬性還包括:

interrupt-parent – 設備結點透過它來指定它所依附的中斷控制器的phandle,當結點沒有指定interrupt-parent 時,則從父級結點繼承。對于本例而言,root結點指定了interrupt-parent = <&intc>;其對應于intc: [email protected],而root結點的子結點并未指定interrupt-parent,因此它們都繼承了intc,即位于0x10140000的中斷控制器。

interrupts – 用到了中斷的設備結點透過它指定中斷號、觸發方法等,具體這個屬性含有多少個cell,由它依附的中斷控制器結點的#interrupt-cells屬性決定。而具體每個cell又是什么含義,一般由驅動的實現決定,而且也會在Device Tree的binding文檔中說明。譬如,對于ARM GIC中斷控制器而言,#interrupt-cells為3,它3個cell的具體含義Documentation/devicetree/bindings/arm/gic.txt就有如下文字說明:

<code class="hljs applescript has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;"><span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">01</span>   The <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">1</span>st cell <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">is</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">the</span> interrupt type; <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">for</span> SPI interrupts, <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">1</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">for</span> PPI
<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">02</span>   interrupts.
<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">03</span>
<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">04</span>   The <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">2</span>nd cell <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">contains</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">the</span> interrupt <span class="hljs-type" style="box-sizing: border-box;">number</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">for</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">the</span> interrupt type.
<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">05</span>   SPI interrupts are <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">in</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">the</span> range [<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0</span>-<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">987</span>].  PPI interrupts are <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">in</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">the</span>
<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">06</span>   range [<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0</span>-<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">15</span>].
<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">07</span>
<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">08</span>   The <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">3</span>rd cell <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">is</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">the</span> flags, encoded <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">as</span> follows:
<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">09</span>         bits[<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">3</span>:<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0</span>] trigger type <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">and</span> level flags.
<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">10</span>                 <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">1</span> = low-<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">to</span>-high edge triggered
<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">11</span>                 <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">2</span> = high-<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">to</span>-low edge triggered
<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">12</span>                 <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">4</span> = active high level-sensitive
<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">13</span>                 <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">8</span> = active low level-sensitive
<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">14</span>         bits[<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">15</span>:<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">8</span>] PPI interrupt cpu mask.  Each bit corresponds <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">to</span> each <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">of</span>
<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">15</span>         <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">the</span> <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">8</span> possible cpus attached <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">to</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">the</span> GIC.  A bit <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">set</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">to</span> '<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">1</span>' indicated
<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">16</span>         <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">the</span> interrupt <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">is</span> wired <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">to</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">that</span> CPU.  Only valid <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">for</span> PPI interrupts.</code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li><li style="box-sizing: border-box; padding: 0px 5px;">4</li><li style="box-sizing: border-box; padding: 0px 5px;">5</li><li style="box-sizing: border-box; padding: 0px 5px;">6</li><li style="box-sizing: border-box; padding: 0px 5px;">7</li><li style="box-sizing: border-box; padding: 0px 5px;">8</li><li style="box-sizing: border-box; padding: 0px 5px;">9</li><li style="box-sizing: border-box; padding: 0px 5px;">10</li><li style="box-sizing: border-box; padding: 0px 5px;">11</li><li style="box-sizing: border-box; padding: 0px 5px;">12</li><li style="box-sizing: border-box; padding: 0px 5px;">13</li><li style="box-sizing: border-box; padding: 0px 5px;">14</li><li style="box-sizing: border-box; padding: 0px 5px;">15</li><li style="box-sizing: border-box; padding: 0px 5px;">16</li></ul>

另外,值得注意的是,一個設備還可能用到多個中斷號。對于ARM GIC而言,若某設備使用了SPI的168、169號2個中斷,而言都是高電平觸發,則該設備結點的interrupts屬性可定義為:interrupts = <0 168 4>, <0 169 4>; 

除了中斷以外,在ARM Linux中clock、GPIO、pinmux都可以透過.dts中的結點和屬性進行描述。

3、DTC (device tree compiler)

將.dts編譯為.dtb的工具。DTC的源代碼位于內核的scripts/dtc目錄,在Linux內核使能了Device Tree的情況下,編譯內核的時候主機工具dtc會被編譯出來,對應scripts/dtc/Makefile中的“hostprogs-y := dtc”這一hostprogs編譯target。 

在Linux內核的arch/arm/boot/dts/Makefile中,描述了當某種SoC被選中后,哪些.dtb文件會被編譯出來,如與VEXPRESS對應的.dtb包括:

<code class="hljs lasso has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;">dtb<span class="hljs-subst" style="color: rgb(0, 0, 0); box-sizing: border-box;">-</span>$(CONFIG_ARCH_VEXPRESS) <span class="hljs-subst" style="color: rgb(0, 0, 0); box-sizing: border-box;">+=</span> vexpress<span class="hljs-attribute" style="box-sizing: border-box;">-v2p</span><span class="hljs-attribute" style="box-sizing: border-box;">-ca5s</span><span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">.</span>dtb <span class="hljs-subst" style="color: rgb(0, 0, 0); box-sizing: border-box;"></span>
        vexpress<span class="hljs-attribute" style="box-sizing: border-box;">-v2p</span><span class="hljs-attribute" style="box-sizing: border-box;">-ca9</span><span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">.</span>dtb <span class="hljs-subst" style="color: rgb(0, 0, 0); box-sizing: border-box;"></span>
        vexpress<span class="hljs-attribute" style="box-sizing: border-box;">-v2p</span><span class="hljs-attribute" style="box-sizing: border-box;">-ca15</span><span class="hljs-attribute" style="box-sizing: border-box;">-tc1</span><span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">.</span>dtb <span class="hljs-subst" style="color: rgb(0, 0, 0); box-sizing: border-box;"></span>
        vexpress<span class="hljs-attribute" style="box-sizing: border-box;">-v2p</span><span class="hljs-attribute" style="box-sizing: border-box;">-ca15_a7</span><span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">.</span>dtb <span class="hljs-subst" style="color: rgb(0, 0, 0); box-sizing: border-box;"></span>
        xenvm<span class="hljs-subst" style="color: rgb(0, 0, 0); box-sizing: border-box;">-</span><span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">4.2</span><span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">.</span>dtb</code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li><li style="box-sizing: border-box; padding: 0px 5px;">4</li><li style="box-sizing: border-box; padding: 0px 5px;">5</li></ul>

在Linux下,我們可以單獨編譯Device Tree文件。當我們在Linux內核下運行make dtbs時,若我們之前選擇了ARCH_VEXPRESS,上述.dtb都會由對應的.dts編譯出來。因為arch/arm/Makefile中含有一個dtbs編譯target項目。

4、Device Tree Blob (.dtb)

.dtb是.dts被DTC編譯后的二進制格式的Device Tree描述,可由Linux內核解析。通常在我們為電路板制作NAND、SD啟動image時,會為.dtb文件單獨留下一個很小的區域以存放之,之后bootloader在引導kernel的過程中,會先讀取該.dtb到內存。 

Binding 

對于Device Tree中的結點和屬性具體是如何來描述設備的硬件細節的,一般需要文檔來進行講解,文檔的后綴名一般為.txt。這些文檔位于內核的Documentation/devicetree/bindings目錄,其下又分為很多子目錄。

5、Bootloader

Uboot mainline 從 v1.1.3開始支持Device Tree,其對ARM的支持則是和ARM內核支持Device Tree同期完成。 

為了使能Device Tree,需要編譯Uboot的時候在config文件中加入#define CONFIG_OF_LIBFDT

在Uboot中,可以從NAND、SD或者TFTP等任意介質將.dtb讀入內存,假設.dtb放入的內存地址為0x71000000,之后可在Uboot運行命令fdt addr命令設置.dtb的地址,如: 

U-Boot> fdt addr 0x71000000 

fdt的其他命令就變地可以使用,如fdt resize、fdt print等。 

對于ARM來講,可以透過bootz kernel_addr initrd_address dtb_address的命令來啟動內核,即dtb_address作為bootz或者bootm的最后一次參數,第一個參數為內核映像的地址,第二個參數為initrd的地址,若不存在initrd,可以用 -代替。

6、Device Tree引發的BSP和驅動變更

有了Device Tree后,大量的板級信息都不再需要,譬如過去經常在arch/arm/plat-xxx和arch/arm/mach-xxx實施的如下事情: 

1. 注冊platform_device,綁定resource,即內存、IRQ等板級信息。 

透過Device Tree后,形如

<code class="hljs avrasm has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;"><span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">90</span> static struct resource xxx_resources[] = {
<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">91</span>         [<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0</span>] = {
<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">92</span>                 <span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.start</span>  = …,
<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">93</span>                 <span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.end</span>    = …,
<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">94</span>                 <span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.flags</span>  = IORESOURCE_MEM,
<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">95</span>         },
<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">96</span>         [<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">1</span>] = {
<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">97</span>                 <span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.start</span>  = …,
<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">98</span>                 <span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.end</span>    = …,
<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">99</span>                 <span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.flags</span>  = IORESOURCE_IRQ,
<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">100</span>         },
<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">101</span> }<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">;</span>
<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">102</span>
<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">103</span> static struct platform_device xxx_device = {
<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">104</span>         <span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.name</span>           = <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"xxx"</span>,
<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">105</span>         <span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.id</span>             = -<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">1</span>,
<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">106</span>         <span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.dev</span>            = {
<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">107</span>                                 <span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.platform</span>_data          = &xxx_data,
<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">108</span>         },
<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">109</span>         <span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.resource</span>       = xxx_resources,
<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">110</span>         <span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.num</span>_resources  = ARRAY_SIZE(xxx_resources),
<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">111</span> }<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">;</span></code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li><li style="box-sizing: border-box; padding: 0px 5px;">4</li><li style="box-sizing: border-box; padding: 0px 5px;">5</li><li style="box-sizing: border-box; padding: 0px 5px;">6</li><li style="box-sizing: border-box; padding: 0px 5px;">7</li><li style="box-sizing: border-box; padding: 0px 5px;">8</li><li style="box-sizing: border-box; padding: 0px 5px;">9</li><li style="box-sizing: border-box; padding: 0px 5px;">10</li><li style="box-sizing: border-box; padding: 0px 5px;">11</li><li style="box-sizing: border-box; padding: 0px 5px;">12</li><li style="box-sizing: border-box; padding: 0px 5px;">13</li><li style="box-sizing: border-box; padding: 0px 5px;">14</li><li style="box-sizing: border-box; padding: 0px 5px;">15</li><li style="box-sizing: border-box; padding: 0px 5px;">16</li><li style="box-sizing: border-box; padding: 0px 5px;">17</li><li style="box-sizing: border-box; padding: 0px 5px;">18</li><li style="box-sizing: border-box; padding: 0px 5px;">19</li><li style="box-sizing: border-box; padding: 0px 5px;">20</li><li style="box-sizing: border-box; padding: 0px 5px;">21</li><li style="box-sizing: border-box; padding: 0px 5px;">22</li></ul>

之類的platform_device代碼都不再需要,其中platform_device會由kernel自動展開。而這些resource實際來源于.dts中設備結點的reg、interrupts屬性。典型地,大多數總線都與“simple_bus”兼容,而在SoC對應的machine的.init_machine成員函數中,調用of_platform_bus_probe(NULL, xxx_of_bus_ids, NULL);即可自動展開所有的platform_device。譬如,假設我們有個XXX SoC,則可在arch/arm/mach-xxx/的板文件中透過如下方式展開.dts中的設備結點對應的platform_device:

<code class="hljs objectivec has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;"><span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">18</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">static</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">struct</span> of_device_id xxx_of_bus_ids[] __initdata = {
<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">19</span>         { <span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">.compatible</span> = <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"simple-bus"</span>, },
<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">20</span>         {},
<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">21</span> };
<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">22</span>
<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">23</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">void</span> __init xxx_mach_init(<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">void</span>)
<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">24</span> {
<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">25</span>         of_platform_bus_probe(<span class="hljs-literal" style="color: rgb(0, 102, 102); box-sizing: border-box;">NULL</span>, xxx_of_bus_ids, <span class="hljs-literal" style="color: rgb(0, 102, 102); box-sizing: border-box;">NULL</span>);
<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">26</span> }
<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">32</span>
<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">33</span> <span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">#ifdef CONFIG_ARCH_XXX</span>
<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">38</span>
<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">39</span> DT_MACHINE_START(XXX_DT, <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"Generic XXX (Flattened Device Tree)"</span>)
<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">41</span>         …
<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">45</span>         <span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">.init_machine</span>   = xxx_mach_init,
<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">46</span>         …
<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">49</span> MACHINE_END
<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">50</span> <span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">#endif</span></code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li><li style="box-sizing: border-box; padding: 0px 5px;">4</li><li style="box-sizing: border-box; padding: 0px 5px;">5</li><li style="box-sizing: border-box; padding: 0px 5px;">6</li><li style="box-sizing: border-box; padding: 0px 5px;">7</li><li style="box-sizing: border-box; padding: 0px 5px;">8</li><li style="box-sizing: border-box; padding: 0px 5px;">9</li><li style="box-sizing: border-box; padding: 0px 5px;">10</li><li style="box-sizing: border-box; padding: 0px 5px;">11</li><li style="box-sizing: border-box; padding: 0px 5px;">12</li><li style="box-sizing: border-box; padding: 0px 5px;">13</li><li style="box-sizing: border-box; padding: 0px 5px;">14</li><li style="box-sizing: border-box; padding: 0px 5px;">15</li><li style="box-sizing: border-box; padding: 0px 5px;">16</li><li style="box-sizing: border-box; padding: 0px 5px;">17</li><li style="box-sizing: border-box; padding: 0px 5px;">18</li></ul>
  1. 注冊i2c_board_info,指定IRQ等板級信息。 

    形如
<code class="hljs cs has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;"><span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">145</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">static</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">struct</span> i2c_board_info __initdata afeb9260_i2c_devices[] = {
<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">146</span>         {
<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">147</span>                 I2C_BOARD_INFO(<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"tlv320aic23"</span>, <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0x1a</span>),
<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">148</span>         }, {
<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">149</span>                 I2C_BOARD_INFO(<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"fm3130"</span>, <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0x68</span>),
<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">150</span>         }, {
<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">151</span>                 I2C_BOARD_INFO(<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"24c64"</span>, <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0x50</span>),
<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">152</span>         },
<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">153</span> };</code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li><li style="box-sizing: border-box; padding: 0px 5px;">4</li><li style="box-sizing: border-box; padding: 0px 5px;">5</li><li style="box-sizing: border-box; padding: 0px 5px;">6</li><li style="box-sizing: border-box; padding: 0px 5px;">7</li><li style="box-sizing: border-box; padding: 0px 5px;">8</li><li style="box-sizing: border-box; padding: 0px 5px;">9</li></ul>

之類的i2c_board_info代碼,目前不再需要出現,現在只需要把tlv320aic23、fm3130、24c64這些設備結點填充作為相應的I2C controller結點的子結點即可,類似于前面的

<code class="hljs ruby has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;">      i2c<span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">@1</span>,<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0</span> {
            compatible = <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"acme,a1234-i2c-bus"</span>;
            …
            rtc<span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">@58</span> {
                compatible = <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"maxim,ds1338"</span>;
                reg = <<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">58</span>>;
                interrupts = < <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">7</span> <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">3</span> >;
            };
        };</code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li><li style="box-sizing: border-box; padding: 0px 5px;">4</li><li style="box-sizing: border-box; padding: 0px 5px;">5</li><li style="box-sizing: border-box; padding: 0px 5px;">6</li><li style="box-sizing: border-box; padding: 0px 5px;">7</li><li style="box-sizing: border-box; padding: 0px 5px;">8</li><li style="box-sizing: border-box; padding: 0px 5px;">9</li></ul>

Device Tree中的I2C client會透過I2C host驅動的probe()函數中調用of_i2c_register_devices(&i2c_dev->adapter);被自動展開。 

3. 注冊spi_board_info,指定IRQ等板級信息。 

形如

<code class="hljs avrasm has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;"><span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">79</span> static struct spi_board_info afeb9260_spi_devices[] = {
<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">80</span>         {       <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">/* DataFlash chip */</span>
<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">81</span>                 <span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.modalias</span>       = <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"mtd_dataflash"</span>,
<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">82</span>                 <span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.chip</span>_select    = <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">1</span>,
<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">83</span>                 <span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.max</span>_speed_hz   = <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">15</span> * <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">1000</span> * <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">1000</span>,
<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">84</span>                 <span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.bus</span>_num        = <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0</span>,
<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">85</span>         },
<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">86</span> }<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">;</span></code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li><li style="box-sizing: border-box; padding: 0px 5px;">4</li><li style="box-sizing: border-box; padding: 0px 5px;">5</li><li style="box-sizing: border-box; padding: 0px 5px;">6</li><li style="box-sizing: border-box; padding: 0px 5px;">7</li><li style="box-sizing: border-box; padding: 0px 5px;">8</li></ul>

之類的spi_board_info代碼,目前不再需要出現,與I2C類似,現在只需要把mtd_dataflash之類的結點,作為SPI控制器的子結點即可,SPI host驅動的probe函數透過spi_register_master()注冊master的時候,會自動展開依附于它的slave。 

4. 多個針對不同電路板的machine,以及相關的callback。 

過去,ARM Linux針對不同的電路板會建立由MACHINE_START和MACHINE_END包圍起來的針對這個machine的一系列callback,譬如:

<code class="hljs avrasm has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;"><span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">373</span> MACHINE_START(VEXPRESS, <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"ARM-Versatile Express"</span>)
<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">374</span>         <span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.atag</span>_offset    = <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0x100</span>,
<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">375</span>         <span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.smp</span>            = smp_ops(vexpress_smp_ops),
<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">376</span>         <span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.map</span>_io         = v2m_map_io,
<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">377</span>         <span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.init</span>_early     = v2m_init_early,
<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">378</span>         <span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.init</span>_irq       = v2m_init_irq,
<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">379</span>         <span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.timer</span>          = &v2m_timer,
<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">380</span>         <span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.handle</span>_irq     = gic_handle_irq,
<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">381</span>         <span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.init</span>_machine   = v2m_init,
<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">382</span>         <span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.restart</span>        = vexpress_restart,
<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">383</span> MACHINE_END</code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li><li style="box-sizing: border-box; padding: 0px 5px;">4</li><li style="box-sizing: border-box; padding: 0px 5px;">5</li><li style="box-sizing: border-box; padding: 0px 5px;">6</li><li style="box-sizing: border-box; padding: 0px 5px;">7</li><li style="box-sizing: border-box; padding: 0px 5px;">8</li><li style="box-sizing: border-box; padding: 0px 5px;">9</li><li style="box-sizing: border-box; padding: 0px 5px;">10</li><li style="box-sizing: border-box; padding: 0px 5px;">11</li></ul>

這些不同的machine會有不同的MACHINE ID,Uboot在啟動Linux內核時會將MACHINE ID存放在r1寄存器,Linux啟動時會匹配Bootloader傳遞的MACHINE ID和MACHINE_START聲明的MACHINE ID,然后執行相應machine的一系列初始化函數。 

引入Device Tree之后,MACHINE_START變更為DT_MACHINE_START,其中含有一個.dt_compat成員,用于表明相關的machine與.dts中root結點的compatible屬性兼容關系。如果Bootloader傳遞給內核的Device Tree中root結點的compatible屬性出現在某machine的.dt_compat表中,相關的machine就與對應的Device Tree匹配,從而引發這一machine的一系列初始化函數被執行。

<code class="hljs avrasm has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;"><span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">489</span> static const char * const v2m_dt_match[] __initconst = {
<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">490</span>         <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"arm,vexpress"</span>,
<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">491</span>         <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"xen,xenvm"</span>,
<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">492</span>         NULL,
<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">493</span> }<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">;</span>
<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">495</span> DT_MACHINE_START(VEXPRESS_DT, <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"ARM-Versatile Express"</span>)
<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">496</span>         <span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.dt</span>_compat      = v2m_dt_match,
<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">497</span>         <span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.smp</span>            = smp_ops(vexpress_smp_ops),
<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">498</span>         <span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.map</span>_io         = v2m_dt_map_io,
<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">499</span>         <span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.init</span>_early     = v2m_dt_init_early,
<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">500</span>         <span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.init</span>_irq       = v2m_dt_init_irq,
<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">501</span>         <span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.timer</span>          = &v2m_dt_timer,
<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">502</span>         <span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.init</span>_machine   = v2m_dt_init,
<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">503</span>         <span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.handle</span>_irq     = gic_handle_irq,
<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">504</span>         <span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.restart</span>        = vexpress_restart,
<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">505</span> MACHINE_END</code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li><li style="box-sizing: border-box; padding: 0px 5px;">4</li><li style="box-sizing: border-box; padding: 0px 5px;">5</li><li style="box-sizing: border-box; padding: 0px 5px;">6</li><li style="box-sizing: border-box; padding: 0px 5px;">7</li><li style="box-sizing: border-box; padding: 0px 5px;">8</li><li style="box-sizing: border-box; padding: 0px 5px;">9</li><li style="box-sizing: border-box; padding: 0px 5px;">10</li><li style="box-sizing: border-box; padding: 0px 5px;">11</li><li style="box-sizing: border-box; padding: 0px 5px;">12</li><li style="box-sizing: border-box; padding: 0px 5px;">13</li><li style="box-sizing: border-box; padding: 0px 5px;">14</li><li style="box-sizing: border-box; padding: 0px 5px;">15</li><li style="box-sizing: border-box; padding: 0px 5px;">16</li></ul>

Linux倡導針對多個SoC、多個電路板的通用DT machine,即一個DT machine的.dt_compat表含多個電路板.dts文件的root結點compatible屬性字符串。之后,如果的電路板的初始化序列不一樣,可以透過int of_machine_is_compatible(const char *compat) API判斷具體的電路板是什么。 

譬如arch/arm/mach-exynos/mach-exynos5-dt.c的EXYNOS5_DT machine同時兼容”samsung,exynos5250”和”samsung,exynos5440”:

<code class="hljs avrasm has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;"><span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">158</span> static char const *exynos5_dt_compat[] __initdata = {
<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">159</span>         <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"samsung,exynos5250"</span>,
<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">160</span>         <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"samsung,exynos5440"</span>,
<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">161</span>         NULL
<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">162</span> }<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">;</span>
<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">163</span>
<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">177</span> DT_MACHINE_START(EXYNOS5_DT, <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"SAMSUNG EXYNOS5 (Flattened Device Tree)"</span>)
<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">178</span>         <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">/* Maintainer: Kukjin Kim <[email protected]> */</span>
<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">179</span>         <span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.init</span>_irq       = exynos5_init_irq,
<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">180</span>         <span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.smp</span>            = smp_ops(exynos_smp_ops),
<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">181</span>         <span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.map</span>_io         = exynos5_dt_map_io,
<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">182</span>         <span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.handle</span>_irq     = gic_handle_irq,
<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">183</span>         <span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.init</span>_machine   = exynos5_dt_machine_init,
<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">184</span>         <span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.init</span>_late      = exynos_init_late,
<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">185</span>         <span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.timer</span>          = &exynos4_timer,
<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">186</span>         <span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.dt</span>_compat      = exynos5_dt_compat,
<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">187</span>         <span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.restart</span>        = exynos5_restart,
<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">188</span>         <span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.reserve</span>        = exynos5_reserve,
<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">189</span> MACHINE_END</code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li><li style="box-sizing: border-box; padding: 0px 5px;">4</li><li style="box-sizing: border-box; padding: 0px 5px;">5</li><li style="box-sizing: border-box; padding: 0px 5px;">6</li><li style="box-sizing: border-box; padding: 0px 5px;">7</li><li style="box-sizing: border-box; padding: 0px 5px;">8</li><li style="box-sizing: border-box; padding: 0px 5px;">9</li><li style="box-sizing: border-box; padding: 0px 5px;">10</li><li style="box-sizing: border-box; padding: 0px 5px;">11</li><li style="box-sizing: border-box; padding: 0px 5px;">12</li><li style="box-sizing: border-box; padding: 0px 5px;">13</li><li style="box-sizing: border-box; padding: 0px 5px;">14</li><li style="box-sizing: border-box; padding: 0px 5px;">15</li><li style="box-sizing: border-box; padding: 0px 5px;">16</li><li style="box-sizing: border-box; padding: 0px 5px;">17</li><li style="box-sizing: border-box; padding: 0px 5px;">18</li><li style="box-sizing: border-box; padding: 0px 5px;">19</li></ul>

它的.init_machine成員函數就針對不同的machine進行了不同的分支處理:

<code class="hljs objectivec has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;"><span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">126</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">static</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">void</span> __init exynos5_dt_machine_init(<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">void</span>)
<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">127</span> {
<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">128</span>         …
<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">149</span>
<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">150</span>         <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">if</span> (of_machine_is_compatible(<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"samsung,exynos5250"</span>))
<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">151</span>                 of_platform_populate(<span class="hljs-literal" style="color: rgb(0, 102, 102); box-sizing: border-box;">NULL</span>, of_default_bus_match_table,
<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">152</span>                                      exynos5250_auxdata_lookup, <span class="hljs-literal" style="color: rgb(0, 102, 102); box-sizing: border-box;">NULL</span>);
<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">153</span>         <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">else</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">if</span> (of_machine_is_compatible(<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"samsung,exynos5440"</span>))
<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">154</span>                 of_platform_populate(<span class="hljs-literal" style="color: rgb(0, 102, 102); box-sizing: border-box;">NULL</span>, of_default_bus_match_table,
<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">155</span>                                      exynos5440_auxdata_lookup, <span class="hljs-literal" style="color: rgb(0, 102, 102); box-sizing: border-box;">NULL</span>);
<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">156</span> }</code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li><li style="box-sizing: border-box; padding: 0px 5px;">4</li><li style="box-sizing: border-box; padding: 0px 5px;">5</li><li style="box-sizing: border-box; padding: 0px 5px;">6</li><li style="box-sizing: border-box; padding: 0px 5px;">7</li><li style="box-sizing: border-box; padding: 0px 5px;">8</li><li style="box-sizing: border-box; padding: 0px 5px;">9</li><li style="box-sizing: border-box; padding: 0px 5px;">10</li><li style="box-sizing: border-box; padding: 0px 5px;">11</li></ul>

使用Device Tree后,驅動需要與.dts中描述的設備結點進行匹配,從而引發驅動的probe()函數執行。對于platform_driver而言,需要添加一個OF匹配表,如前文的.dts文件的”acme,a1234-i2c-bus”兼容I2C控制器結點的OF匹配表可以是:

<code class="hljs avrasm has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;"><span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">436</span> static const struct of_device_id a1234_i2c_of_match[] = {
<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">437</span>         { <span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.compatible</span> = <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"acme,a1234-i2c-bus "</span>, },
<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">438</span>         {},
<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">439</span> }<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">;</span>
<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">440</span> MODULE_DEVICE_TABLE(of, a1234_i2c_of_match)<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">;</span>
<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">441</span>
<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">442</span> static struct platform_driver i2c_a1234_driver = {
<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">443</span>         <span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.driver</span> = {
<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">444</span>                 <span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.name</span> = <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"a1234-i2c-bus "</span>,
<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">445</span>                 <span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.owner</span> = THIS_MODULE,
<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">449</span>                 <span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.of</span>_match_table = a1234_i2c_of_match,
<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">450</span>         },
<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">451</span>         <span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.probe</span> = i2c_a1234_probe,
<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">452</span>         <span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.remove</span> = i2c_a1234_remove,
<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">453</span> }<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">;</span>
<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">454</span> module_platform_driver(i2c_a1234_driver)<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">;</span></code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li><li style="box-sizing: border-box; padding: 0px 5px;">4</li><li style="box-sizing: border-box; padding: 0px 5px;">5</li><li style="box-sizing: border-box; padding: 0px 5px;">6</li><li style="box-sizing: border-box; padding: 0px 5px;">7</li><li style="box-sizing: border-box; padding: 0px 5px;">8</li><li style="box-sizing: border-box; padding: 0px 5px;">9</li><li style="box-sizing: border-box; padding: 0px 5px;">10</li><li style="box-sizing: border-box; padding: 0px 5px;">11</li><li style="box-sizing: border-box; padding: 0px 5px;">12</li><li style="box-sizing: border-box; padding: 0px 5px;">13</li><li style="box-sizing: border-box; padding: 0px 5px;">14</li><li style="box-sizing: border-box; padding: 0px 5px;">15</li><li style="box-sizing: border-box; padding: 0px 5px;">16</li></ul>

對于I2C和SPI從設備而言,同樣也可以透過of_match_table添加匹配的.dts中的相關結點的compatible屬性,如sound/soc/codecs/wm8753.c中的:

<code class="hljs avrasm has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;"><span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">1533</span> static const struct of_device_id wm8753_of_match[] = {
<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">1534</span>         { <span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.compatible</span> = <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"wlf,wm8753"</span>, },
<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">1535</span>         { }
<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">1536</span> }<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">;</span>
<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">1537</span> MODULE_DEVICE_TABLE(of, wm8753_of_match)<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">;</span>
<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">1587</span> static struct spi_driver wm8753_spi_driver = {
<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">1588</span>         <span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.driver</span> = {
<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">1589</span>                 <span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.name</span>   = <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"wm8753"</span>,
<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">1590</span>                 <span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.owner</span>  = THIS_MODULE,
<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">1591</span>                 <span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.of</span>_match_table = wm8753_of_match,
<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">1592</span>         },
<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">1593</span>         <span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.probe</span>          = wm8753_spi_probe,
<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">1594</span>         <span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.remove</span>         = wm8753_spi_remove,
<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">1595</span> }<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">;</span>
<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">1640</span> static struct i2c_driver wm8753_i2c_driver = {
<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">1641</span>         <span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.driver</span> = {
<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">1642</span>                 <span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.name</span> = <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"wm8753"</span>,
<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">1643</span>                 <span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.owner</span> = THIS_MODULE,
<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">1644</span>                 <span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.of</span>_match_table = wm8753_of_match,
<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">1645</span>         },
<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">1646</span>         <span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.probe</span> =    wm8753_i2c_probe,
<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">1647</span>         <span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.remove</span> =   wm8753_i2c_remove,
<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">1648</span>         <span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.id</span>_table = wm8753_i2c_id,
<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">1649</span> }<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">;</span></code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li><li style="box-sizing: border-box; padding: 0px 5px;">4</li><li style="box-sizing: border-box; padding: 0px 5px;">5</li><li style="box-sizing: border-box; padding: 0px 5px;">6</li><li style="box-sizing: border-box; padding: 0px 5px;">7</li><li style="box-sizing: border-box; padding: 0px 5px;">8</li><li style="box-sizing: border-box; padding: 0px 5px;">9</li><li style="box-sizing: border-box; padding: 0px 5px;">10</li><li style="box-sizing: border-box; padding: 0px 5px;">11</li><li style="box-sizing: border-box; padding: 0px 5px;">12</li><li style="box-sizing: border-box; padding: 0px 5px;">13</li><li style="box-sizing: border-box; padding: 0px 5px;">14</li><li style="box-sizing: border-box; padding: 0px 5px;">15</li><li style="box-sizing: border-box; padding: 0px 5px;">16</li><li style="box-sizing: border-box; padding: 0px 5px;">17</li><li style="box-sizing: border-box; padding: 0px 5px;">18</li><li style="box-sizing: border-box; padding: 0px 5px;">19</li><li style="box-sizing: border-box; padding: 0px 5px;">20</li><li style="box-sizing: border-box; padding: 0px 5px;">21</li><li style="box-sizing: border-box; padding: 0px 5px;">22</li><li style="box-sizing: border-box; padding: 0px 5px;">23</li><li style="box-sizing: border-box; padding: 0px 5px;">24</li></ul>

不過這邊有一點需要提醒的是,I2C和SPI外設驅動和Device Tree中設備結點的compatible 屬性還有一種弱式匹配方法,就是別名匹配。compatible 屬性的組織形式為,,別名其實就是去掉compatible 屬性中逗號前的manufacturer前綴。關于這一點,可查看drivers/spi/spi.c的源代碼,函數spi_match_device()暴露了更多的細節,如果別名出現在設備spi_driver的id_table里面,或者別名與spi_driver的name字段相同,SPI設備和驅動都可以匹配上:

<code class="hljs objectivec has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;"><span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">90</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">static</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">int</span> spi_match_device(<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">struct</span> device *dev, <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">struct</span> device_driver *drv)
<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">91</span> {
<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">92</span>         <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">const</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">struct</span> spi_device *spi = to_spi_device(dev);
<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">93</span>         <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">const</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">struct</span> spi_driver *sdrv = to_spi_driver(drv);
<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">94</span>
<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">95</span>         <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">/* Attempt an OF style match */</span>
<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">96</span>         <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">if</span> (of_driver_match_device(dev, drv))
<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">97</span>                 <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">return</span> <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">1</span>;
<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">98</span>
<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">99</span>         <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">/* Then try ACPI */</span>
<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">100</span>         <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">if</span> (acpi_driver_match_device(dev, drv))
<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">101</span>                 <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">return</span> <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">1</span>;
<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">102</span>
<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">103</span>         <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">if</span> (sdrv->id_table)
<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">104</span>                 <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">return</span> !!spi_match_id(sdrv->id_table, spi);
<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">105</span>
<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">106</span>         <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">return</span> strcmp(spi->modalias, drv->name) == <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0</span>;
<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">107</span> }
<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">71</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">static</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">const</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">struct</span> spi_device_id *spi_match_id(<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">const</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">struct</span> spi_device_id *<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">id</span>,
<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">72</span>                                                 <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">const</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">struct</span> spi_device *sdev)
<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">73</span> {
<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">74</span>         <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">while</span> (<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">id</span>->name[<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0</span>]) {
<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">75</span>                 <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">if</span> (!strcmp(sdev->modalias, <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">id</span>->name))
<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">76</span>                         <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">return</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">id</span>;
<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">77</span>                 <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">id</span>++;
<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">78</span>         }
<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">79</span>         <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">return</span> <span class="hljs-literal" style="color: rgb(0, 102, 102); box-sizing: border-box;">NULL</span>;
<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">80</span> }</code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li><li style="box-sizing: border-box; padding: 0px 5px;">4</li><li style="box-sizing: border-box; padding: 0px 5px;">5</li><li style="box-sizing: border-box; padding: 0px 5px;">6</li><li style="box-sizing: border-box; padding: 0px 5px;">7</li><li style="box-sizing: border-box; padding: 0px 5px;">8</li><li style="box-sizing: border-box; padding: 0px 5px;">9</li><li style="box-sizing: border-box; padding: 0px 5px;">10</li><li style="box-sizing: border-box; padding: 0px 5px;">11</li><li style="box-sizing: border-box; padding: 0px 5px;">12</li><li style="box-sizing: border-box; padding: 0px 5px;">13</li><li style="box-sizing: border-box; padding: 0px 5px;">14</li><li style="box-sizing: border-box; padding: 0px 5px;">15</li><li style="box-sizing: border-box; padding: 0px 5px;">16</li><li style="box-sizing: border-box; padding: 0px 5px;">17</li><li style="box-sizing: border-box; padding: 0px 5px;">18</li><li style="box-sizing: border-box; padding: 0px 5px;">19</li><li style="box-sizing: border-box; padding: 0px 5px;">20</li><li style="box-sizing: border-box; padding: 0px 5px;">21</li><li style="box-sizing: border-box; padding: 0px 5px;">22</li><li style="box-sizing: border-box; padding: 0px 5px;">23</li><li style="box-sizing: border-box; padding: 0px 5px;">24</li><li style="box-sizing: border-box; padding: 0px 5px;">25</li><li style="box-sizing: border-box; padding: 0px 5px;">26</li><li style="box-sizing: border-box; padding: 0px 5px;">27</li><li style="box-sizing: border-box; padding: 0px 5px;">28</li></ul>

7、 常用OF API

在Linux的BSP和驅動代碼中,還經常會使用到Linux中一組Device Tree的API,這些API通常被冠以of_前綴,它們的實現代碼位于內核的drivers/of目錄。這些常用的API包括: 

int of_device_is_compatible(const struct device_node *device,const char *compat); 

判斷設備結點的compatible 屬性是否包含compat指定的字符串。當一個驅動支持2個或多個設備的時候,這些不同.dts文件中設備的compatible 屬性都會進入驅動 OF匹配表。因此驅動可以透過Bootloader傳遞給內核的Device Tree中的真正結點的compatible 屬性以確定究竟是哪一種設備,從而根據不同的設備類型進行不同的處理。如drivers/pinctrl/pinctrl-sirf.c即兼容于”sirf,prima2-pinctrl”,又兼容于”sirf,prima2-pinctrl”,在驅動中就有相應分支處理:

<code class="hljs bash has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;"><span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">1682</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">if</span> (of_device_is_compatible(np, <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"sirf,marco-pinctrl"</span>))
<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">1683</span>      is_marco = <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">1</span>;</code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li></ul>

struct device_node *of_find_compatible_node(struct device_node *from, 

const char *type, const char *compatible); 

根據compatible屬性,獲得設備結點。遍歷Device Tree中所有的設備結點,看看哪個結點的類型、compatible屬性與本函數的輸入參數匹配,大多數情況下,from、type為NULL。 

int of_property_read_u8_array(const struct device_node *np, 

const char *propname, u8 *out_values, size_t sz); 

int of_property_read_u16_array(const struct device_node *np, 

const char *propname, u16 *out_values, size_t sz); 

int of_property_read_u32_array(const struct device_node *np, 

const char *propname, u32 *out_values, size_t sz); 

int of_property_read_u64(const struct device_node *np, const char 

*propname, u64 *out_value); 

讀取設備結點np的屬性名為propname,類型為8、16、32、64位整型數組的屬性。對于32位處理器來講,最常用的是of_property_read_u32_array()。如在arch/arm/mm/cache-l2x0.c中,透過如下語句讀取L2 cache的”arm,data-latency”屬性:

<code class="hljs haskell has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;"><span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">534</span>         of_property_read_u32_array(np, <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"arm,data-latency"</span>,
<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">535</span>                                    <span class="hljs-typedef" style="box-sizing: border-box;"><span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">data</span>, <span class="hljs-type" style="box-sizing: border-box; color: rgb(102, 0, 102);">ARRAY_SIZE</span><span class="hljs-container" style="box-sizing: border-box;">(<span class="hljs-title" style="box-sizing: border-box;">data</span>)</span>);</span></code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li></ul>

在arch/arm/boot/dts/vexpress-v2p-ca9.dts中,含有”arm,data-latency”屬性的L2 cache結點如下:

<code class="hljs lasso has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;"><span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">137</span>         L2: <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">cache</span><span class="hljs-attribute" style="box-sizing: border-box;">-controller</span>@<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">1e00</span>a000 {
<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">138</span>                 compatible <span class="hljs-subst" style="color: rgb(0, 0, 0); box-sizing: border-box;">=</span> <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"arm,pl310-cache"</span>;
<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">139</span>                 reg <span class="hljs-subst" style="color: rgb(0, 0, 0); box-sizing: border-box;">=</span> <span class="hljs-subst" style="color: rgb(0, 0, 0); box-sizing: border-box;"><</span><span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0x1e00a000</span> <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0x1000</span><span class="hljs-subst" style="color: rgb(0, 0, 0); box-sizing: border-box;">></span>;
<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">140</span>                 interrupts <span class="hljs-subst" style="color: rgb(0, 0, 0); box-sizing: border-box;">=</span> <span class="hljs-subst" style="color: rgb(0, 0, 0); box-sizing: border-box;"><</span><span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0</span> <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">43</span> <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">4</span><span class="hljs-subst" style="color: rgb(0, 0, 0); box-sizing: border-box;">></span>;
<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">141</span>                 <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">cache</span><span class="hljs-attribute" style="box-sizing: border-box;">-level</span> <span class="hljs-subst" style="color: rgb(0, 0, 0); box-sizing: border-box;">=</span> <span class="hljs-subst" style="color: rgb(0, 0, 0); box-sizing: border-box;"><</span><span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">2</span><span class="hljs-subst" style="color: rgb(0, 0, 0); box-sizing: border-box;">></span>;
<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">142</span>                 arm,<span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">data</span><span class="hljs-attribute" style="box-sizing: border-box;">-latency</span> <span class="hljs-subst" style="color: rgb(0, 0, 0); box-sizing: border-box;">=</span> <span class="hljs-subst" style="color: rgb(0, 0, 0); box-sizing: border-box;"><</span><span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">1</span> <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">1</span> <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">1</span><span class="hljs-subst" style="color: rgb(0, 0, 0); box-sizing: border-box;">></span>;
<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">143</span>                 arm,<span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">tag</span><span class="hljs-attribute" style="box-sizing: border-box;">-latency</span> <span class="hljs-subst" style="color: rgb(0, 0, 0); box-sizing: border-box;">=</span> <span class="hljs-subst" style="color: rgb(0, 0, 0); box-sizing: border-box;"><</span><span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">1</span> <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">1</span> <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">1</span><span class="hljs-subst" style="color: rgb(0, 0, 0); box-sizing: border-box;">></span>;
<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">144</span>         }</code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li><li style="box-sizing: border-box; padding: 0px 5px;">4</li><li style="box-sizing: border-box; padding: 0px 5px;">5</li><li style="box-sizing: border-box; padding: 0px 5px;">6</li><li style="box-sizing: border-box; padding: 0px 5px;">7</li><li style="box-sizing: border-box; padding: 0px 5px;">8</li></ul>

有些情況下,整形屬性的長度可能為1,于是內核為了方便調用者,又在上述API的基礎上封裝出了更加簡單的讀單一整形屬性的API,它們為int of_property_read_u8()、of_property_read_u16()等,實現于include/linux/of.h:

<code class="hljs rust has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;"><span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">513</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">static</span> inline <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">int</span> of_property_read_u8(<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">const</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">struct</span> device_node *np,
<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">514</span>                                        <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">const</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">char</span> *propname,
<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">515</span>                                        <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">u8</span> *out_value)
<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">516</span> {
<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">517</span>         <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">return</span> of_property_read_u8_array(np, propname, out_value, <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">1</span>);
<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">518</span> }
<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">519</span>
<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">520</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">static</span> inline <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">int</span> of_property_read_u16(<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">const</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">struct</span> device_node *np,
<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">521</span>                                        <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">const</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">char</span> *propname,
<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">522</span>                                        <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">u16</span> *out_value)
<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">523</span> {
<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">524</span>         <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">return</span> of_property_read_u16_array(np, propname, out_value, <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">1</span>);
<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">525</span> }
<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">526</span>
<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">527</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">static</span> inline <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">int</span> of_property_read_u32(<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">const</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">struct</span> device_node *np,
<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">528</span>                                        <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">const</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">char</span> *propname,
<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">529</span>                                        <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">u32</span> *out_value)
<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">530</span> {
<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">531</span>         <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">return</span> of_property_read_u32_array(np, propname, out_value, <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">1</span>);
<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">532</span> }</code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li><li style="box-sizing: border-box; padding: 0px 5px;">4</li><li style="box-sizing: border-box; padding: 0px 5px;">5</li><li style="box-sizing: border-box; padding: 0px 5px;">6</li><li style="box-sizing: border-box; padding: 0px 5px;">7</li><li style="box-sizing: border-box; padding: 0px 5px;">8</li><li style="box-sizing: border-box; padding: 0px 5px;">9</li><li style="box-sizing: border-box; padding: 0px 5px;">10</li><li style="box-sizing: border-box; padding: 0px 5px;">11</li><li style="box-sizing: border-box; padding: 0px 5px;">12</li><li style="box-sizing: border-box; padding: 0px 5px;">13</li><li style="box-sizing: border-box; padding: 0px 5px;">14</li><li style="box-sizing: border-box; padding: 0px 5px;">15</li><li style="box-sizing: border-box; padding: 0px 5px;">16</li><li style="box-sizing: border-box; padding: 0px 5px;">17</li><li style="box-sizing: border-box; padding: 0px 5px;">18</li><li style="box-sizing: border-box; padding: 0px 5px;">19</li><li style="box-sizing: border-box; padding: 0px 5px;">20</li></ul>

int of_property_read_string(struct device_node *np, const char 

*propname, const char **out_string); 

int of_property_read_string_index(struct device_node *np, const char 

*propname, int index, const char **output); 

前者讀取字符串屬性,后者讀取字符串數組屬性中的第index個字符串。如drivers/clk/clk.c中的of_clk_get_parent_name()透過of_property_read_string_index()遍歷clkspec結點的所有”clock-output-names”字符串數組屬性。

<code class="hljs objectivec has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;"><span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">1759</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">const</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">char</span> *of_clk_get_parent_name(<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">struct</span> device_node *np, <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">int</span> index)
<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">1760</span> {
<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">1761</span>         <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">struct</span> of_phandle_args clkspec;
<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">1762</span>         <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">const</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">char</span> *clk_name;
<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">1763</span>         <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">int</span> rc;
<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">1764</span>
<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">1765</span>         <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">if</span> (index < <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0</span>)
<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">1766</span>                 <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">return</span> <span class="hljs-literal" style="color: rgb(0, 102, 102); box-sizing: border-box;">NULL</span>;
<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">1767</span>
<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">1768</span>         rc = of_parse_phandle_with_args(np, <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"clocks"</span>, <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"#clock-cells"</span>, index,
<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">1769</span>                                         &clkspec);
<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">1770</span>         <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">if</span> (rc)
<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">1771</span>                 <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">return</span> <span class="hljs-literal" style="color: rgb(0, 102, 102); box-sizing: border-box;">NULL</span>;
<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">1772</span>
<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">1773</span>         <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">if</span> (of_property_read_string_index(clkspec<span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">.np</span>, <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"clock-output-names"</span>,
<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">1774</span>                                   clkspec<span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">.args_count</span> ? clkspec<span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">.args</span>[<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0</span>] : <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0</span>,
<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">1775</span>                                           &clk_name) < <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0</span>)
<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">1776</span>                 clk_name = clkspec<span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">.np</span>->name;
<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">1777</span>
<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">1778</span>         of_node_put(clkspec<span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">.np</span>);
<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">1779</span>         <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">return</span> clk_name;
<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">1780</span> }
<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">1781</span> EXPORT_SYMBOL_GPL(of_clk_get_parent_name);</code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li><li style="box-sizing: border-box; padding: 0px 5px;">4</li><li style="box-sizing: border-box; padding: 0px 5px;">5</li><li style="box-sizing: border-box; padding: 0px 5px;">6</li><li style="box-sizing: border-box; padding: 0px 5px;">7</li><li style="box-sizing: border-box; padding: 0px 5px;">8</li><li style="box-sizing: border-box; padding: 0px 5px;">9</li><li style="box-sizing: border-box; padding: 0px 5px;">10</li><li style="box-sizing: border-box; padding: 0px 5px;">11</li><li style="box-sizing: border-box; padding: 0px 5px;">12</li><li style="box-sizing: border-box; padding: 0px 5px;">13</li><li style="box-sizing: border-box; padding: 0px 5px;">14</li><li style="box-sizing: border-box; padding: 0px 5px;">15</li><li style="box-sizing: border-box; padding: 0px 5px;">16</li><li style="box-sizing: border-box; padding: 0px 5px;">17</li><li style="box-sizing: border-box; padding: 0px 5px;">18</li><li style="box-sizing: border-box; padding: 0px 5px;">19</li><li style="box-sizing: border-box; padding: 0px 5px;">20</li><li style="box-sizing: border-box; padding: 0px 5px;">21</li><li style="box-sizing: border-box; padding: 0px 5px;">22</li><li style="box-sizing: border-box; padding: 0px 5px;">23</li></ul>

static inline bool of_property_read_bool(const struct device_node *np, 

const char *propname); 

如果設備結點np含有propname屬性,則返回true,否則返回false。一般用于檢查空屬性是否存在。

void __iomem *of_iomap(struct device_node *node, int index); 

通過設備結點直接進行設備內存區間的 ioremap(),index是內存段的索引。若設備結點的reg屬性有多段,可通過index標示要ioremap的是哪一段,只有1段的情況,index為0。采用Device Tree后,大量的設備驅動通過of_iomap()進行映射,而不再通過傳統的ioremap。

unsigned int irq_of_parse_and_map(struct device_node *dev, int index); 

透過Device Tree或者設備的中斷號,實際上是從.dts中的interrupts屬性解析出中斷號。若設備使用了多個中斷,index指定中斷的索引號。 

還有一些OF API,這里不一一列舉,具體可參考include/linux/of.h頭文件。

8、總結

ARM社區一貫充斥的大量垃圾代碼導致Linus盛怒,因此社區在2011年到2012年進行了大量的工作。ARM Linux開始圍繞Device Tree展開,Device Tree有自己的獨立的語法,它的源文件為.dts,編譯后得到.dtb,Bootloader在引導Linux內核的時候會將.dtb地址告知內核。之后內核會展開Device Tree并創建和注冊相關的設備,因此arch/arm/mach-xxx和arch/arm/plat-xxx中大量的用于注冊platform、I2C、SPI板級信息的代碼被刪除,而驅動也以新的方式和.dts中定義的設備結點進行匹配。

七星彩走势图2元网官网