1. 程式人生 > >Linux核心移植 part2:uboot 裝置樹--基本概念和原始碼介紹

Linux核心移植 part2:uboot 裝置樹--基本概念和原始碼介紹

arm uboot的裝置樹原始檔位於arch/arm/dts/目錄下,網路上有很多介紹Linux裝置樹概念的文章,這裡以dts相關的API為切入點,如果都懂了,裝置樹的東西就迎刃而解了。本篇文章首先記錄一些基本知識,下一篇進行原始碼分析。

一、裝置樹檔案基礎

這裡的內容根據kernel/Documentation/devicetree/booting-without-of.txt,翻譯加總結而來。有這些概念理解原始碼就不會有裝置樹相關知識性的問題了。

dtb檔案由dts檔案通過dtc工具編譯而來,用來描述硬體配置。

dts叫裝置樹,顧名思義,就是以樹結構來描述裝置。樹的成員稱為節點,有一個根節點,只有根節點沒有父親節點,其他節點有且只有一個父親節點。

節點名字

根節點沒有名字,路徑為”/”
每個節點有兩個名字,真正的名字包含在名為name的屬性中,還有一個unit name,用來區分同一層級上擁有相同name的節點,由node name,@和unit address構成。通常用來表示在裝置樹中的路徑。路徑用”/father1/father2/”表示。

compatible 屬性

每個表示實際裝置的節點需要compatible屬性,用來表示向後完全相容的硬體。

phandle or linux,phandle

可以通過其他節點屬性來引用的節點必須有這個屬性。

簡單示例

    / o device-tree
      |- name = "
device-tree" |- model = "MyBoardName" |- compatible = "MyBoardFamilyName" |- #address-cells = <2> |- #size-cells = <2> |- linux,phandle = <0> | o cpus | | - name = "cpus" | | - linux,phandle = <1> | | - #address-cells = <1>
| | - #size-cells = <0> | | | o PowerPC,[email protected] | |- name = "PowerPC,970" | |- device_type = "cpu" | |- reg = <0> | |- clock-frequency = <0x5f5e1000> | |- 64-bit | |- linux,phandle = <2> | o [email protected]0 | |- name = "memory" | |- device_type = "memory" | |- reg = <0x00000000 0x00000000 0x00000000 0x20000000> | |- linux,phandle = <3> | o chosen |- name = "chosen" |- bootargs = "root=/dev/sda2" |- linux,phandle = <4>

“/”是根節點,他的unit name為device tree,擁有name, model, compatible等屬性,其他的也類似。

1.2 裝置樹的結構塊

結構塊描述每個節點的結構。每個節點內容包含:
一個開始識別符號,全路徑,屬性列表,子節點列表,一個結束識別符號。

    the basic structure of a single node:

     * token OF_DT_BEGIN_NODE (that is 0x00000001)
     * for version 1 to 3, this is the node full path as a zero
       terminated string, starting with "/". For version 16 and later,
       this is the node unit name only (or an empty string for the
       root node)
     * [align gap to next 4 bytes boundary]
     * for each property:
     * token OF_DT_PROP (that is 0x00000003)
     * 32-bit value of property value size in bytes (or 0 if no
          value)
     * 32-bit value of offset in string block of property name
     * property value data if any
     * [align gap to next 4 bytes boundary]
     * [child nodes if any]
     * token OF_DT_END_NODE (that is 0x00000002)

1.3 裝置樹字串塊

為了節省空間,屬性名字被單獨放在strings block中,structure block中的屬性定義包含了屬性值在strings block中的偏移。

基本知識就簡單羅列到這裡,其實要理解裝置樹做到兩點就可以了:

  1. 理解節點組織方式
  2. 理解<屬性,值>對的含義

二. dts相關原始碼

位於$(tree)/lib/以及$(tree)/lib/libfdt/。完成Flattened Device Tree Decode的功能。

43e440a0 <__dtb_dt_begin>:
/* 這是magic number: 0xd00dfeed */
43e440a0:   edfe0dd0    ldcl    13, cr0, [lr, #832]!    ; 0x340
43e440a4:   2f360000    svccs   0x00360000
43e440a8:   78000000    stmdavc r0, {}  ; <UNPREDICTABLE>

libfdt介紹:

The libfdt functionality was written by David Gibson. The original
source came from the git repository:

URL: git://ozlabs.org/home/dgibson/git/libfdt.git

author David Gibson [email protected](none)
Fri, 23 Mar 2007 04:16:54 +0000 (15:16 +1100)
committer David Gibson [email protected](none)
Fri, 23 Mar 2007 04:16:54 +0000 (15:16 +1100)
commit 857f54e79f74429af20c2b5ecc00ee98af6a3b8b
tree 2f648f0f88225a51ded452968d28b4402df8ade0
parent 07a12a08005f3b5cd9337900a6551e450c07b515

To adapt for u-boot usage, only the applicable files were copied and
imported into the u-boot git repository.
Omitted:
* GPL - u-boot comes with a copy of the GPL license
* test subdirectory - not directly useful for u-boot

After importing, other customizations were performed. See the git log
for details.

Jerry Van Baren