ARM Linux 3.x的设备树(Device Tree)
社区必须改变这种局面,于是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使用情况
view plaincopy
本文引用地址:https://www.eepw.com.cn/article/201611/317984.htm- /{
- node1{
- a-string-property="Astring";
- a-string-list-property="firststring","secondstring";
- a-byte-data-property=[0x010x230x340x56];
- child-node1{
- first-child-property;
- second-child-property=<1>;
- a-string-property="Hello,world";
- };
- child-node2{
- };
- };
- node2{
- an-empty-property;
- a-cell-property=<1234>;/*eachnumber(cell)isauint32*/
- child-node1{
- };
- };
- };
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文件为:- /{
- compatible="acme,coyotes-revenge";
- #address-cells=<1>;
- #size-cells=<1>;
- interrupt-parent=<&intc>;
- cpus{
- #address-cells=<1>;
- #size-cells=<0>;
- cpu@0{
- compatible="arm,cortex-a9";
- reg=<0>;
- };
- cpu@1{
- compatible="arm,cortex-a9";
- reg=<1>;
- };
- };
- serial@101f0000{
- compatible="arm,pl011";
- reg=<0x101f00000x1000>;
- interrupts=<10>;
- };
- serial@101f2000{
- compatible="arm,pl011";
- reg=<0x101f20000x1000>;
- interrupts=<20>;
- };
- gpio@101f3000{
- compatible="arm,pl061";
- reg=<0x101f30000x1000
- 0x101f40000x0010>;
- interrupts=<30>;
- };
- intc:interrupt-controller@10140000{
- compatible="arm,pl190";
- reg=<0x101400000x1000>;
- interrupt-controller;
- #interrupt-cells=<2>;
- };
- spi@10115000{
- compatible="arm,pl022";
- reg=<0x101150000x1000>;
- interrupts=<40>;
- };
- external-bus{
- #address-cells=<2>
- #size-cells=<1>;
- ranges=<000x101000000x10000//Chipselect1,Ethernet
- 100x101600000x10000//Chipselect2,i2ccontroller
- 200x300000000x1000000>;//Chipselect3,NORFlash
- ethernet@0,0{
- compatible="smc,smc91c111";
- reg=<000x1000>;
- interrupts=<52>;
- };
- i2c@1,0{
- compatible="acme,a1234-i2c-bus";
- #address-cells=<1>;
- #size-cells=<0>;
- reg=<100x1000>;
- interrupts=<62>;
- rtc@58{
- compatible="maxim,ds1338";
- reg=<58>;
- interrupts=<73>;
- };
- };
- flash@2,0{
- compatible="samsung,k8f1315ebm","cfi-flash";
- reg=<200x4000000>;
- };
- };
- };
在.dts文件的每个设备,都有一个compatible 属性,compatible属性用户驱动和设备的绑定。compatible 属性是一个字符串的列表,列表中的第一个字符串表征了结点代表的确切设备,形式为"
- flash@0,00000000{
- compatible="arm,vexpress-flash","cfi-flash";
- reg=<00x000000000x04000000>,
- <10x000000000x04000000>;
- bank-width=<4>;
- };
接下来root结点"/"的cpus子结点下面又包含2个cpu子结点,描述了此machine上的2个CPU,并且二者的compatible 属性为"arm,cortex-a9"。
注意cpus和cpus的2个cpu子结点的命名,它们遵循的组织形式为:
可寻址的设备使用如下信息来在Device Tree中编码地址信息:
- reg
- #address-cells
- #size-cells
root结点的子结点描述的是CPU的视图,因此root子结点的address区域就直接位于CPU的memory区域。但是,经过总线桥后的address往往需要经过转换才能对应的CPU的memory映射。external-bus的ranges属性定义了经过external-bus桥后的地址范围如何映射到CPU的memory区域。
- ranges=<000x101000000x10000//Chipselect1,Ethernet
- 100x101600000x10000//Chipselect2,i2ccontroller
- 200x300000000x1000000>;//Chipselect3,NORFlash
Device Tree中还可以中断连接信息,对于中断控制器而言,它提供如下属性:
interrupt-controller – 这个属性为空,中断控制器应该加上此属性表明自己的身份;
#interrupt-cells – 与#address-cells 和 #size-cells相似,它表明连接此中断控制器的设备的interrupts属性的cell大小;
在整个Device Tree中,与中断相关的属性还包括:
interrupt-parent – 设备结点透过它来指定它所依附的中断控制器的phandle,当结点没有指定interrupt-parent 时,则从父级结点继承。对于本例而言,root结点指定了interrupt-parent = <&intc>;其对应于intc:interrupt-controller@10140000,而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就有如下文字说明:
- 01The1stcellistheinterrupttype;0forSPIinterrupts,1forPPI
- 02interrupts.
- 03
- 04The2ndcellcontainstheinterruptnumberfortheinterrupttype.
- 05SPIinterruptsareintherange[0-987].PPIinterruptsareinthe
- 06range[0-15].
- 07
- 08The3rdcellistheflags,encodedasfollows:
- 09bits[3:0]triggertypeandlevelflags.
- 101=low-to-highedgetriggered
- 112=high-to-lowedgetriggered
- 124=activehighlevel-sensitive
- 138=activelowlevel-sensitive
- 14bits[15:8]PPIinterruptcpumask.Eachbitcorrespondstoeachof
- 15the8possiblecpusattachedtotheGIC.Abitsetto1indicated
- 16theinterruptiswiredtothatCPU.OnlyvalidforPPIinterrupts.
除了中断以外,在ARM Linux中clock、GPIO、pinmux都可以透过.dts中的结点和属性进行描述。
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包括:
- dtb-$(CONFIG_ARCH_VEXPRESS)+=vexpress-v2p-ca5s.dtb
- vexpress-v2p-ca9.dtb
- vexpress-v2p-ca15-tc1.dtb
- vexpress-v2p-ca15_a7.dtb
- xenvm-4.2.dtb
Device Tree Blob (.dtb)
.dtb是.dts被DTC编译后的二进制格式的Device Tree描述,可由Linux内核解析。通常在我们为电路板制作NAND、SD启动image时,会为.dtb文件单独留下一个很小的区域以存放之,之后bootloader在引导kernel的过程中,会先读取该.dtb到内存。Binding
对于Device Tree中的结点和属性具体是如何来描述设备的硬件细节的,一般需要文档来进行讲解,文档的后缀名一般为.txt。这些文档位于内核的Documentation/devicetree/bindings目录,其下又分为很多子目录。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,可以用 -代替。
3. Device Tree引发的BSP和驱动变更
有了Device Tree后,大量的板级信息都不再需要,譬如过去经常在arch/arm/plat-xxx和arch/arm/mach-xxx实施的如下事情:1. 注册platform_device,绑定resource,即内存、IRQ等板级信息。
透过Device Tree后,形如
- 90staticstructresourcexxx_resources[]={
- 91[0]={
- 92.start=…,
- 93.end=…,
- 94.flags=IORESOURCE_MEM,
- 95},
- 96[1]={
- 97.start=…,
- 98.end=…,
- 99.flags=IORESOURCE_IRQ,
- 100},
- 101};
- 102
- 103staticstructplatform_devicexxx_device={
- 104.name="xxx",
- 105.id=-1,
- 106.dev={
- 107.platform_data=&xxx_data,
- 108},
- 109.resource=xxx_resources,
- 110.num_resources=ARRAY_SIZE(xxx_resources),
- 111};
- 18staticstructof_device_idxxx_of_bus_ids[]__initdata={
- 19{.compatible="simple-bus",},
- 20{},
- 21};
- 22
- 23void__initxxx_mach_init(void)
- 24{
- 25of_platform_bus_probe(NULL,xxx_of_bus_ids,NULL);
- 26}
- 32
- 33#ifdefCONFIG_ARCH_XXX
- 38
- 39DT_MACHINE_START(XXX_DT,"GenericXXX(FlattenedDeviceTree)")
- 41…
- 45.init_machine=xxx_mach_init,
- 46…
- 49MACHINE_END
- 50#endif
2.注册i2c_board_info,指定IRQ等板级信息。
形如
- 145staticstructi2c_board_info__initdataafeb9260_i2c_devices[]={
- 146{
- 147I2C_BOARD_INFO("tlv320aic23",0x1a),
- 148},{
- 149I2C_BOARD_INFO("fm3130",0x68),
- 150},{
- 151I2C_BOARD_INFO("24c64",0x50),
- 152},
- 153};
- i2c@1,0{
- compatible="acme,a1234-i2c-bus";
- …
- rtc@58{
- compatible="maxim,ds1338";
- reg=<58>;
- interrupts=<73>;
- };
- };
3. 注册spi_board_info,指定IRQ等板级信息。
形如
- 79staticstructspi_board_infoafeb9260_spi_devices[]={
- 80{/*DataFlashchip*/
- 81.modalias="mtd_dataflash",
- 82.chip_select=1,
- 83.max_speed_hz=15*1000*1000,
- 84.bus_num=0,
- 85},
- 86};
4.多个针对不同电路板的machine,以及相关的callback。
过去,ARM Linux针对不同的电路板会建立由MACHINE_START和MACHINE_END包围起来的针对这个machine的一系列callback,譬如:
- 373MACHINE_START(VEXPRESS,"ARM-VersatileExpress")
- 374.atag_offset=0x100,
- 375.smp=smp_ops(vexpress_smp_ops),
- 376.map_io=v2m_map_io,
- 377.init_early=v2m_init_early,
- 378.init_irq=v2m_init_irq,
- 379.timer=&v2m_timer,
- 380.handle_irq=gic_handle_irq,
- 381.init_machine=v2m_init,
- 382.restart=vexpress_restart,
- 383MACHINE_END
引入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的一系列初始化函数被执行。
- 489staticconstchar*constv2m_dt_match[]__initconst={
- 490"arm,vexpress",
- 491"xen,xenvm",
- 492NULL,
- 493};
- 495DT_MACHINE_START(VEXPRESS_DT,"ARM-VersatileExpress")
- 496.dt_compat=v2m_dt_match,
- 497.smp=smp_ops(vexpress_smp_ops),
- 498.map_io=v2m_dt_map_io,
- 499.init_early=v2m_dt_init_early,
- 500.init_irq=v2m_dt_init_irq,
- 501.timer=&v2m_dt_timer,
- 502.init_machine=v2m_dt_init,
- 503.handle_irq=gic_handle_irq,
- 504.restart=vexpress_restart,
- 505MACHINE_END
譬如arch/arm/mach-exynos/mach-exynos5-dt.c的EXYNOS5_DT machine同时兼容"samsung,exynos5250"和"samsung,exynos5440":
- 158staticcharconst*exynos5_dt_compat[]__initdata={
- 159"samsung,exynos5250",
- 160"samsung,exynos5440",
- 161NULL
- 162};
- 163
- 177DT_MACHINE_START(EXYNOS5_DT,"SAMSUNGEXYNOS5(FlattenedDeviceTree)")
- 178/*Maintainer:KukjinKim
*/ - 179.init_irq=exynos5_init_irq,
- 180.smp=smp_ops(exynos_smp_ops),
- 181.map_io=exynos5_dt_map_io,
- 182.handle_irq=gic_handle_irq,
- 183.init_machine=exynos5_dt_machine_init,
- 184.init_late=exynos_init_late,
- 185.timer=&exynos4_timer,
- 186.dt_compat=exynos5_dt_compat,
- 187.restart=exynos5_restart,
- 188.reserve=exynos5_reserve,
- 189MACHINE_END
它的.init_machine成员函数就针对不同的machine进行了不同的分支处理:
- 126staticvoid__initexynos5_dt_machine_init(void)
- 127{
- 128…
- 149
- 150if(of_machine_is_compatible("samsung,exynos5250"))
- 151of_platform_populate(NULL,of_default_bus_match_table,
- 152exynos5250_auxdata_lookup,NULL);
- 153elseif(of_machine_is_compatible("samsung,exynos5440"))
- 154of_platform_populate(NULL,of_default_bus_match_table,
- 155exynos5440_auxdata_lookup,NULL);
- 156}
使用Device Tree后,驱动需要与.dts中描述的设备结点进行匹配,从而引发驱动的probe()函数执行。对于platform_driver而言,需要添加一个OF匹配表,如前文的.dts文件的"acme,a1234-i2c-bus"兼容I2C控制器结点的OF匹配表可以是:
- 436staticconststructof_device_ida1234_i2c_of_match[]={
- 437{.compatible="acme,a1234-i2c-bus",},
- 438{},
- 439};
- 440MODULE_DEVICE_TABLE(of,a1234_i2c_of_match);
- 441
- 442staticstructplatform_driveri2c_a1234_driver={
- 443.driver={
- 444.name="a1234-i2c-bus",
- 445.owner=THIS_MODULE,
- 449.of_match_table=a1234_i2c_of_match,
- 450},
- 451.probe=i2c_a1234_probe,
- 452.remove=i2c_a1234_remove,
- 453};
- 454module_platform_driver(i2c_a1234_driver);
对于I2C和SPI从设备而言,同样也可以透过of_match_table添加匹配的.dts中的相关结点的compatible属性,如sound/soc/codecs/wm8753.c中的:
- 1533staticconststructof_device_idwm8753_of_match[]={
- 1534{.compatible="wlf,wm8753",},
- 1535{}
- 1536};
- 1537MODULE_DEVICE_TABLE(of,wm8753_of_match);
- 1587staticstructspi_driverwm8753_spi_driver={
- 1588.driver={
- 1589.name="wm8753",
- 1590.owner=THIS_MODULE,
- 1591.of_match_table=wm8753_of_match,
- 1592},
- 1593.probe=wm8753_spi_probe,
- 1594.remove=wm8753_spi_remove,
- 1595};
- 1640staticstructi2c_driverwm8753_i2c_driver={
- 1641.driver={
- 1642.name="wm8753",
- 1643.owner=THIS_MODULE,
- 1644.of_match_table=wm8753_of_match,
- 1645},
- 1646.probe=wm8753_i2c_probe,
- 1647.remove=wm8753_i2c_remove,
- 1648.id_table=wm8753_i2c_id,
- 1649};
- 90staticintspi_match_device(structdevice*dev,structdevice_driver*drv)
- 91{
- 92conststructspi_device*spi=to_spi_device(dev);
- 93conststructspi_driver*sdrv=to_spi_driver(drv);
- 94
- 95/*AttemptanOFstylematch*/
- 96if(of_driver_match_device(dev,drv))
- 97return1;
- 98
- 99/*ThentryACPI*/
- 100if(acpi_driver_match_device(dev,drv))
- 101return1;
- 102
- 103if(sdrv->id_table)
- 104return!!spi_match_id(sdrv->id_table,spi);
- 105
- 106returnstrcmp(spi->modalias,drv->name)==0;
- 107}
- 71staticconststructspi_device_id*spi_match_id(conststructspi_device_id*id,
- 72conststructspi_device*sdev)
- 73{
- 74while(id->name[0]){
- 75if(!strcmp(sdev->modalias,id->name))
- 76returnid;
- 77id++;
- 78}
- 79returnNULL;
- 80}
评论