dts基本概念--spec解读-2.3节

2.3.3 phandle

“phandle”属性通用一个唯一的id来标识一个Node,在property可以使用这个id来引用Node。

在dts文件中中,可以使用类似c语言的Labels and References机制。定义一个lable,唯一标识一个node或者property,后续可以使用&来引用这个lable。DTC会将lable转换成u32的整数值放入到DTB中,用户层面就不再关心具体转换的整数值了。
举例:

 10 /dts-v1/;                                                                       
 11                                                                                                                                                                                                               
 12 / {                                                                             
 13         model = "V2P-CA9";                                                      
 14         arm,hbi = <0x191>;                                                      
 15         arm,vexpress,site = <0xf>;                                              
 16         compatible = "arm,vexpress,v2p-ca9", "arm,vexpress";                    
 17         interrupt-parent = <&gic>;                                              
 18         #address-cells = <1>;                                                   
 19         #size-cells = <1>;                                                      
 ....
160         gic: interrupt-controller@1e001000 {                                    
161                 compatible = "arm,cortex-a9-gic";                               
162                 #interrupt-cells = <3>;                                         
163                 #address-cells = <0>;                                           
164                 interrupt-controller;                                           
165                 reg = <0x1e001000 0x1000>,                                      
166                       <0x1e000100 0x100>;                                       
167         };
....
303         smb {                                                                   
304                 compatible = "simple-bus";                                      
305                                                                                 
306                 #address-cells = <2>;                                           
307                 #size-cells = <1>;                                              
308                 ranges = <0 0 0x40000000 0x04000000>,                           
309                          <1 0 0x44000000 0x04000000>,                           
310                          <2 0 0x48000000 0x04000000>,                           
311                          <3 0 0x4c000000 0x04000000>,                           
312                          <7 0 0x10000000 0x00020000>;                           
313                                                                                 
314                 #interrupt-cells = <1>;                                         
315                 interrupt-map-mask = <0 0 63>;                                  
316                 interrupt-map = <0 0  0 &gic 0  0 4>,                           
317                                 <0 0  1 &gic 0  1 4>,                           
                 
359                                                                                 
360                 /include/ "vexpress-v2m.dtsi"                                   
361         };                                                                      
362 };  
--------------------- 

interrupt-controller@1e001000的lab就是gic,&gic就是对标签的引用,interrupt-controller@1e001000会有一个phandle节点的值就是标签的值,一个32位的数字。

这个32位的数字会在,dtc编译dts的时候生成。

反编译dtb:
./dtc -I dtb -O dts -o vexpress-v2p-ca9.dts vexpress-v2p-ca9.dtb
164         interrupt-controller@1e001000 {                                         
165                 compatible = "arm,cortex-a9-gic";                               
166                 #interrupt-cells = <0x3>;                                       
167                 #address-cells = <0x0>;                                         
168                 interrupt-controller;                                           
169                 reg = <0x1e001000 0x1000 0x1e000100 0x100>;                     
170                 linux,phandle = <0x1>;                                          
171                 phandle = <0x1>;                                                
172         };   

可以在生成dts后在interrupt-controller@1e001000和原dts上多加上phandle property,property的value是1,这样&gic的值也是1,这样在其他节点中可以通过这个引用&gic,找到对应的节点interrupt-controller@1e001000

可以通过phandle,找到对应的节点,函数是of_find_node_by_phandle,就是轮询所有节点中phandle的值,如果与指定值相等,则返回相应的节点。

1108 struct device_node *of_find_node_by_phandle(phandle handle)                                                                                                                                                  
1109 {                                                                               
1110         struct device_node *np;                                                 
1111         unsigned long flags;                                                    
1112                                                                                 
1113         if (!handle)                                                            
1114                 return NULL;                                                    
1115                                                                                 
1116         raw_spin_lock_irqsave(&devtree_lock, flags);                                                                                                                                               
1117         for_each_of_allnodes(np)                                                
1118                 if (np->phandle == handle)                                      
1119                         break;                                                  
1120         of_node_get(np);                                                        
1121         raw_spin_unlock_irqrestore(&devtree_lock, flags);                       
1122         return np;                                                              
1123 }   

2.3.5 #address-cells and #size-cells

Property name: #address-cells, #size-cells
Value type:
“#address-cells, #size-cells”属性用来定义当前node的子node中”reg”属性的解析格式
#address-cells:用reg多少个32 bit表示一个地址
#size-celss: 用reg多少个32bit表示一个地址的长度
举例说明:

  1. 如果node”soc”中”#address-cells=<1>”、”#size-cells=<1>”,那么子node”serial”中”reg”属性的解析为“addr1 = 0x0, size1 = 0x100, addr2 = 0x0, size2 = 0x200”:
soc {
    #address-cells = <1>;
    #size-cells = <1>;
    serial {
        reg = <0x0 0x100 0x0 0x200>;
    }
}
  1. 如果node”soc”中”#address-cells=<2>”、”#size-cells=<2>”,那么子node”serial”中”reg”属性的解析为“addr1 = 0x100, size1 = 0x200”:
soc {
    #address-cells = <2>;
    #size-cells = <2>;
    serial {
        reg = <0x0 0x100 0x0 0x200>;
    }
}
  1. 如果node”soc”中”#address-cells=<2>”、”#size-cells=<0>”,那么子node”serial”中”reg”属性的解析为“addr1 = 0x100, addr2 = 0x200”:
soc {
    #address-cells = <2>;
    #size-cells = <0>;
    serial {
        reg = <0x0 0x100 0x0 0x200>;
    }
}

note:例子摘自参考1

参考

Device Tree详解析
devicetree-spec

你可能感兴趣的:(dts)