Linux ARM Device Tree

来源:互联网 发布:mac yosemite u盘 编辑:程序博客网 时间:2024/05/18 00:39
 一、ARM Device Tree起源 


历史问题:
1、在过去的ARM Linux中,arch/arm/plat-xxx和arch/arm/mach-xxx中充斥着大量的垃圾代码;
  相当多数的代码只是在描述板级细节,而这些板级细节对于内核来讲,不过是垃圾,
  如板上的platform设备、resource、i2c_board_info、spi_board_info以及各种硬件的platform_data;
2、常见的s3c2410、s3c6410等板级目录,代码量在数万行。

ARM社区必须改变这种局面!

牛人发话,炮击驱动垃圾太多:
1、Linus Torvalds在2011年3月17日的ARM Linux邮件列表宣称“this whole ARM thing is a f*cking pain in the ass”,引发ARM Linux社区的地震;
2、随后ARM社区进行了一系列的重大修正。

借鉴PowerPC等其他体系架构下已经使用的Flattened Device Tree(FDT)进入ARM社区的视野;它起源于 OpenFirmware (OF)。


二、描述和定义
定义:
1、Device Tree是一种描述硬件的数据结构;
2、Device Tree一系列被命名的结点(node)和属性(property)组成,而结点本身可包含子结点;
3、所谓属性,其实就是成对出现的name和value,可描述的信息包括:
  CPU的数量和类别;
  内存基地址和大小;
  总线和桥;
  外设连接;
  中断控制器和中断使用情况;
  GPIO控制器和GPIO使用情况;
  Clock控制器和Clock使用情况;
  
4、DT基本上就是画一棵电路板上CPU、总线、设备组成的树;
5、Bootloader会将这棵树传递给内核,内核可以识别这棵树,并根据它展开出Linux内核中的platform_device、i2c_client、spi_device等设备;
6、所以硬件的细节可以直接透过Device Tree传递给Linux,而不再需要在kernel中进行大量的冗余编码。


三、Device Tree组成和结构
1、描述设备硬件信息的文本格式;
2、编译这一文本的工具,同时Bootloader也需要支持将编译后的Device Tree传递给Linux内核;


.dtsi    公共头文件,可以被include;
.dts     一个机器对应的设备树文件,一般放置在内核的arch/arm/boot/dts/目录。

说明:.dtsi也可以include其他的.dtsi,譬如几乎所有的ARM SoC的.dtsi都引用了skeleton.dtsi。

例如:

/ {                                                         //1个root结点"/";
    compatible = "acme,coyotes-revenge";  
    #address-cells = <1>;  
    #size-cells = <1>;  
    interrupt-parent = <&intc>;  
  
    cpus {                                                  //1个双核ARM Cortex-A9 32位处理器;
        #address-cells = <1>;  
        #size-cells = <0>;  
        cpu@0 {  
            compatible = "arm,cortex-a9";  
            reg = <0>;  
        };  
        cpu@1 {  
            compatible = "arm,cortex-a9";  
            reg = <1>;  
        };  
    };  
                                                           //ARM的local bus上的内存映射区域
    serial@101f0000 {                                      //分布了2个串口(分别位于0x101F1000 和 0x101F2000)
        compatible = "arm,pl011";  
        reg = <0x101f0000 0x1000 >;  
        interrupts = < 1 0 >;  
    };  
  
    serial@101f2000 {  
        compatible = "arm,pl011";  
        reg = <0x101f2000 0x1000 >;  
        interrupts = < 2 0 >;  
    };  
  
    gpio@101f3000 {                                       //GPIO控制器(位于0x101F3000)SPI控制器(位于0x10170000)
        compatible = "arm,pl061";  
        reg = <0x101f3000 0x1000  
               0x101f4000 0x0010>;  
        interrupts = < 3 0 >;  
    };  
  
    intc: interrupt-controller@10140000 {                //中断控制器(位于0x10140000)
        compatible = "arm,pl190";  
        reg = <0x10140000 0x1000 >;  
        interrupt-controller;  
        #interrupt-cells = <2>;  
    };  
  
    spi@10115000 {                  
        compatible = "arm,pl022";  
        reg = <0x10115000 0x1000 >;  
        interrupts = < 4 0 >;  
    };  
  
    external-bus {                                                                    //一个external bus桥  ,External bus桥上又连接了:
        #address-cells = <2>  
        #size-cells = <1>;  
        ranges = <0 0  0x10100000   0x10000     // Chipselect 1, Ethernet             //SMC SMC91111 Ethernet(位于0x10100000)
                  1 0  0x10160000   0x10000     // Chipselect 2, i2c controller       //I2C控制器(位于0x10160000)
                  2 0  0x30000000   0x1000000>; // Chipselect 3, NOR Flash            //64MB NOR Flash(位于0x30000000)
  
        ethernet@0,0 {  
            compatible = "smc,smc91c111";  
            reg = <0 0 0x1000>;  
            interrupts = < 5 2 >;  
        };  
  
        i2c@1,0 {                                    //External bus桥上连接的I2C控制器所对应的I2C总线上又连接了:
            compatible = "acme,a1234-i2c-bus";       
            #address-cells = <1>;  
            #size-cells = <0>;  
            reg = <1 0 0x1000>;  
            interrupts = < 6 2 >;  
            rtc@58 {                                //Maxim DS1338实时钟(I2C地址为0x58)。  
                compatible = "maxim,ds1338";  
                reg = <58>;  
                interrupts = < 7 3 >;  
            };  
        };  
  
        flash@2,0 {  
            compatible = "samsung,k8f1315ebm", "cfi-flash";  
            reg = <2 0 0x4000000>;  
        };  
    };  
};  

compatible 属性:

1、root结点"/"的compatible 属性:

compatible = "acme,coyotes-revenge";
它的组织形式为:<manufacturer>,<model>。

Linux内核透过root结点"/"的compatible 属性即可判断它启动的是什么machine。
  
2、每个设备的compatible 属性:
  列表中的第一个字符串表征了结点代表的确切设备,形式为"<manufacturer>,<model>",其后的字符串表征可兼容的其他设备。
前面的是特指,后面的则涵盖更广的范围。如在arch/arm/boot/dts/vexpress-v2m.dtsi中的Flash结点:

flash@0,00000000 {  
     compatible = "arm,vexpress-flash", "cfi-flash";  
     reg = <0 0x00000000 0x04000000>,  
     <1 0x00000000 0x04000000>;  
     bank-width = <4>;  
};  


    比如,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>[@<unit-address>],<>中的内容是必选项,
[]中的则为可选项。name是一个ASCII字符串,用于描述结点对应的设备类型,如3com Ethernet适配器对应的结点name宜为ethernet,
而不是3com509。如果一个结点描述的设备有地址,则应该给出@unit-address。
    多个相同类型设备结点的name可以一样,只要unit-address不同即可,如本例中含有cpu@0cpu@1以及serial@101f0000
serial@101f2000这样的同名结点。设备的unit-address地址也经常在其对应结点的reg属性中给出。
    ePAPR标准给出了结点命名的规范。可寻址的设备使用如下信息来在Device Tree中编码地址信息:

    reg
    #address-cells
    #size-cells
    
    其中reg的组织形式为reg = <address1 length1 [address2 length2] [address3 length3] ... >,
其中的每一组address length表明了设备使用的一个地址范围。address为1个或多个32位的整型(即cell),
而length则为cell的列表或者为空(若#size-cells = 0)。address 和 length 字段是可变长的,
父结点的#address-cells和#size-cells分别决定了子结点的reg属性的address和length字段的长度。

    root结点:
#address-cells = <1>;
#size-cells = <1>;
两句话决定了serial、gpio、spi等结点的address和length字段的长度分别为1。


  cpus结点:
#address-cells = <1>;
#size-cells = <0>;
两句话决定了2个cpu子结点的address为1,length为空,于是形成了2个cpu的reg = <0>;和reg = <1>。

     
    external-bus结点:
#address-cells = <2>
#size-cells = <1>;
两句话决定了其下的ethernet、i2c、flash的reg字段形如reg = <0 0 0x1000>; reg = <1 0 0x1000>;和reg = <2 0 0x4000000>;。

    其中,address字段长度为0,开始的第一个cell(0、1、2)是对应的片选,第2个cell(0,0,0)是相对该片选的基地址,第3个cell(0x1000、
0x1000、0x4000000)为length。
    特别要留意的是i2c结点中定义的 #address-cells = <1>;和#size-cells = <0>;又作用到了I2C总线上连接的RTC,它的address字段为0x58,是设备的I2C地址。

    root结点的子结点描述的是CPU的视图,因此root子结点的address区域就直接位于CPU的memory区域。但是,经过总线桥后的address往往需要经过转换才能对应
的CPU的memory映射。external-bus的ranges属性定义了经过external-bus桥后的地址范围如何映射到CPU的memory区域。

ranges = <0 0  0x10100000   0x10000     // Chipselect 1, Ethernet  
          1 0  0x10160000   0x10000     // Chipselect 2, i2c controller  
          2 0  0x30000000   0x1000000>; // Chipselect 3, NOR Flash  

是地址转换表,其中的每个项目是一个子地址、父地址以及在子地址空间的大小的映射。

映射表中的子地址、父地址分别采用子地址空间的#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: 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就有如下文字说明:

The 1st cell is the interrupt type; 0 for SPI interrupts, 1 for PPI
interrupts.

The 2nd cell contains the interrupt number for the interrupt type.
SPI interrupts are in the range [0-987].  PPI interrupts are in the
range [0-15].

The 3rd cell is the flags, encoded as follows:
      bits[3:0] trigger type and level flags.
              1 = low-to-high edge triggered
              2 = high-to-low edge triggered
              4 = active high level-sensitive
              8 = active low level-sensitive
      bits[15:8] PPI interrupt cpu mask.  Each bit corresponds to each of
      the 8 possible cpus attached to the GIC.  A bit set to '1' indicated
      the interrupt is wired to that CPU.  Only valid for PPI interrupts.

    另外,值得注意的是,一个设备还可能用到多个中断号。对于ARM GIC而言,若某设备使用了SPI的168、169号2个中断,而言都是高电平触发,则该
设备结点的interrupts属性可定义为:interrupts = <0 168 4>, <0 169 4>;

除了中断以外,在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

     在Linux下,我们可以单独编译Device Tree文件。当我们在Linux内核下运行make dtbs时,若我们之前选择了ARCH_VEXPRESS,上述.dtb都会由对应的.dts
编译出来。因为arch/arm/Makefile中含有一个dtbs编译target项目。


1、.dtb是.dts被DTC编译后的二进制格式的Device Tree描述,可由Linux内核解析;
2、在我们制作NAND、SD启动image时,会为.dtb文件单独留下一个很小的区域以存放;
3、bootloader在引导kernel的过程中,会先读取该.dtb到内存。

五、Binding
1、对于Device Tree中的结点和属性具体是如何来描述设备的硬件细节的,一般需要文档来进行讲解,文档的后缀名一般为.txt。
2、这些文档位于内核的Documentation/devicetree/bindings目录,其下又分为很多子目录。



六、Bootloader
1、Uboot mainline 从 v1.1.3开始支持Device Tree,其对ARM的支持则是和ARM内核支持Device Tree同期完成;
2、为了使能Device Tree,需要编译Uboot的时候在config文件中加入:#define CONFIG_OF_LIBFDT ;
3、在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,可以用 -代替。


七、Device Tree引发的BSP和驱动变更:

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

1.注册platform_device,绑定resource,即内存、IRQ等板级信息。透过Device Tree后,形如

static struct resource xxx_resources[] = {
        [0] = {
                .start  = …,
                .end    = …,
                .flags  = IORESOURCE_MEM,
        },
        [1] = {
                .start  = …,
                .end    = …,
                .flags  = IORESOURCE_IRQ,
         },
};

static struct platform_device xxx_device = {
         .name           = "xxx",
         .id             = -1,
         .dev            = {
                                 .platform_data          = &xxx_data,
         },
         .resource       = xxx_resources,
         .num_resources  = ARRAY_SIZE(xxx_resources),
};
之类的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:

static struct of_device_id xxx_of_bus_ids[] __initdata = {  
         { .compatible = "simple-bus", },  
         {},  
};  
  
void __init xxx_mach_init(void)  
{  
         of_platform_bus_probe(NULL, xxx_of_bus_ids, NULL);  
}  
  
#ifdef CONFIG_ARCH_XXX  
DT_MACHINE_START(XXX_DT, "Generic XXX (Flattened Device Tree)")  
         …  
         .init_machine   = xxx_mach_init,  
         …  
MACHINE_END  
#endif  

static struct of_device_id xxx_of_bus_ids[] __initdata = {
         { .compatible = "simple-bus", },
         {},
};

void __init xxx_mach_init(void)
{
         of_platform_bus_probe(NULL, xxx_of_bus_ids, NULL);
}

#ifdef CONFIG_ARCH_XXX
DT_MACHINE_START(XXX_DT, "Generic XXX (Flattened Device Tree)")
         …
         .init_machine   = xxx_mach_init,
         …
MACHINE_END
#endif


2. 注册i2c_board_info,指定IRQ等板级信息。

形如:

static struct i2c_board_info __initdata afeb9260_i2c_devices[] = {  
        {  
                I2C_BOARD_INFO("tlv320aic23", 0x1a),  
        }, {  
                I2C_BOARD_INFO("fm3130", 0x68),  
        }, {  
                I2C_BOARD_INFO("24c64", 0x50),  
        },  
};  
static struct i2c_board_info __initdata afeb9260_i2c_devices[] = {
        {
                I2C_BOARD_INFO("tlv320aic23", 0x1a),
        }, {
                I2C_BOARD_INFO("fm3130", 0x68),
        }, {
                I2C_BOARD_INFO("24c64", 0x50),
        },
};

之类的i2c_board_info代码,目前不再需要出现,现在只需要把tlv320aic23、fm3130、24c64这些设备结点填充作为相应的I2C controller结点的子结点即可
,类似于前面的
i2c@1,0 {  
      compatible = "acme,a1234-i2c-bus";  
      …  
      rtc@58 {  
          compatible = "maxim,ds1338";  
          reg = <58>;  
          interrupts = < 7 3 >;  
      };  
  };  
     Device Tree中的I2C client会透过I2C host驱动的probe()函数中调用of_i2c_register_devices(&i2c_dev->adapter);被自动展开。

3. 注册spi_board_info,指定IRQ等板级信息。
形如:
static struct spi_board_info afeb9260_spi_devices[] = {  
        {       /* DataFlash chip */  
                .modalias       = "mtd_dataflash",  
                .chip_select    = 1,  
                .max_speed_hz   = 15 * 1000 * 1000,  
                .bus_num        = 0,  
        },  
};  
之类的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,譬如:
MACHINE_START(VEXPRESS, "ARM-Versatile Express")
        .atag_offset    = 0x100,
        .smp            = smp_ops(vexpress_smp_ops),
        .map_io         = v2m_map_io,
        .init_early     = v2m_init_early,
        .init_irq       = v2m_init_irq,
        .timer          = &v2m_timer,
        .handle_irq     = gic_handle_irq,
        .init_machine   = v2m_init,
        .restart        = vexpress_restart,
MACHINE_END
    这些不同的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的一系列初始化函数被执行。

static const char * const v2m_dt_match[] __initconst = {  
        "arm,vexpress",  
        "xen,xenvm",  
        NULL,  
};  
DT_MACHINE_START(VEXPRESS_DT, "ARM-Versatile Express")  
        .dt_compat      = v2m_dt_match,  
        .smp            = smp_ops(vexpress_smp_ops),  
        .map_io         = v2m_dt_map_io,  
        .init_early     = v2m_dt_init_early,  
        .init_irq       = v2m_dt_init_irq,  
        .timer          = &v2m_dt_timer,  
        .init_machine   = v2m_dt_init,  
        .handle_irq     = gic_handle_irq,  
        .restart        = vexpress_restart,  
MACHINE_END  

    倡导针对多个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":
static char const *exynos5_dt_compat[] __initdata = {
        "samsung,exynos5250",
        "samsung,exynos5440",
        NULL
};

DT_MACHINE_START(EXYNOS5_DT, "SAMSUNG EXYNOS5 (Flattened Device Tree)")
        /* Maintainer: Kukjin Kim <kgene.kim@samsung.com> */
        .init_irq       = exynos5_init_irq,
        .smp            = smp_ops(exynos_smp_ops),
        .map_io         = exynos5_dt_map_io,
        .handle_irq     = gic_handle_irq,
        .init_machine   = exynos5_dt_machine_init,
        .init_late      = exynos_init_late,
        .timer          = &exynos4_timer,
        .dt_compat      = exynos5_dt_compat,
        .restart        = exynos5_restart,
        .reserve        = exynos5_reserve,
MACHINE_END

     它的.init_machine成员函数就针对不同的machine进行了不同的分支处理:

static void __init exynos5_dt_machine_init(void)  
{  
        …  

        if (of_machine_is_compatible("samsung,exynos5250"))  
                of_platform_populate(NULL, of_default_bus_match_table,  
                                     exynos5250_auxdata_lookup, NULL);  
        else if (of_machine_is_compatible("samsung,exynos5440"))  
                of_platform_populate(NULL, of_default_bus_match_table,  
                                     exynos5440_auxdata_lookup, NULL);  
}  

    使用Device Tree后,驱动需要与.dts中描述的设备结点进行匹配,从而引发驱动的probe()函数执行。对于platform_driver而言,
需要添加一个OF匹配表,如前文的.dts文件的"acme,a1234-i2c-bus"兼容I2C控制器结点的OF匹配表可以是:

static const struct of_device_id a1234_i2c_of_match[] = {
        { .compatible = "acme,a1234-i2c-bus ", },
        {},
};
MODULE_DEVICE_TABLE(of, a1234_i2c_of_match);

static struct platform_driver i2c_a1234_driver = {
        .driver = {
                .name = "a1234-i2c-bus ",
                .owner = THIS_MODULE,
                .of_match_table = a1234_i2c_of_match,
        },
        .probe = i2c_a1234_probe,
        .remove = i2c_a1234_remove,
};
module_platform_driver(i2c_a1234_driver);

     对于I2C和SPI从设备而言,同样也可以透过of_match_table添加匹配的.dts中的相关结点的compatible属性,如sound/soc/codecs/wm8753.c中的:

static const struct of_device_id wm8753_of_match[] = {
        { .compatible = "wlf,wm8753", },
        { }
};
MODULE_DEVICE_TABLE(of, wm8753_of_match);
static struct spi_driver wm8753_spi_driver = {
        .driver = {
                .name   = "wm8753",
                .owner  = THIS_MODULE,
                .of_match_table = wm8753_of_match,
        },
        .probe          = wm8753_spi_probe,
        .remove         = wm8753_spi_remove,
};
static struct i2c_driver wm8753_i2c_driver = {
        .driver = {
                .name = "wm8753",
                .owner = THIS_MODULE,
                .of_match_table = wm8753_of_match,
        },
        .probe =    wm8753_i2c_probe,
        .remove =   wm8753_i2c_remove,
        .id_table = wm8753_i2c_id,
};

    不过这边有一点需要提醒的是,I2C和SPI外设驱动和Device Tree中设备结点的compatible 属性还有一种弱式匹配方法,就是别名匹配。compatible
属性的组织形式为<manufacturer>,<model>,别名其实就是去掉compatible 属性中逗号前的manufacturer前缀。关于这一点,可查看drivers/spi/spi.c
的源代码,函数spi_match_device()暴露了更多的细节,如果别名出现在设备spi_driver的id_table里面,或者别名与spi_driver的name字段相同,SPI设
备和驱动都可以匹配上: 

static int spi_match_device(struct device *dev, struct device_driver *drv)  
{  
        const struct spi_device *spi = to_spi_device(dev);  
        const struct spi_driver *sdrv = to_spi_driver(drv);  

        /* Attempt an OF style match */  
        if (of_driver_match_device(dev, drv))  
                return 1;  

        /* Then try ACPI */  
         if (acpi_driver_match_device(dev, drv))  
                 return 1;  
  
         if (sdrv->id_table)  
                 return !!spi_match_id(sdrv->id_table, spi);  
  
         return strcmp(spi->modalias, drv->name) == 0;  
}  
static const struct spi_device_id *spi_match_id(const struct spi_device_id *id,  
                                                const struct spi_device *sdev)  
{  
        while (id->name[0]) {  
                if (!strcmp(sdev->modalias, id->name))  
                        return id;  
                id++;  
        }  
        return NULL;  
}  

八、常用OF API:
    在Linux的BSP和驱动代码中,还经常会使用到Linux中一组Device Tree的API,这些API通常被冠以of_前缀,它们的实现代码位于内核的drivers/of目录,
这些常用的API包括:

//判断设备结点的compatible 属性是否包含compat指定的字符串。
int of_device_is_compatible(const struct device_node *device,const char *compat);

  当一个驱动支持2个或多个设备的时候,这些不同.dts文件中设备的compatible 属性
都会进入驱动 OF匹配表。因此驱动可以透过Bootloader传递给内核的Device Tree中的真正结点的compatible 属性以确定究竟是哪一种设备,从而根据不
同的设备类型进行不同的处理。如drivers/pinctrl/pinctrl-sirf.c即兼容于"sirf,prima2-pinctrl",又兼容于"sirf,prima2-pinctrl",在驱动中就有
相应分支处理:
if (of_device_is_compatible(np, "sirf,marco-pinctrl"))  
      is_marco = 1;  

//根据compatible属性,获得设备结点。遍历Device Tree中所有的设备结点,看看哪个结点的类型、compatible属性与本函数的输入参数匹配,
//大多数情况下,from、type为NULL。
struct device_node *of_find_compatible_node(struct device_node *from, const char *type, const char *compatible);

//读取设备结点np的属性名为propname,类型为8、16、32、64位整型数组的属性。
//对于32位处理器来讲,最常用的是of_property_read_u32_array()。
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);

//如在arch/arm/mm/cache-l2x0.c中,透过如下语句读取L2 cache的"arm,data-latency"属性:
of_property_read_u32_array(np, "arm,data-latency",data, ARRAY_SIZE(data));


    在arch/arm/boot/dts/vexpress-v2p-ca9.dts中,含有"arm,data-latency"属性的L2 cache结点如下:

L2: cache-controller@1e00a000 {
        compatible = "arm,pl310-cache";
        reg = <0x1e00a000 0x1000>;
        interrupts = <0 43 4>;
        cache-level = <2>;
        arm,data-latency = <1 1 1>;
        arm,tag-latency = <1 1 1>;
}
    有些情况下,整形属性的长度可能为1,于是内核为了方便调用者,又在上述API的基础上封装出了更加简单的读单一整形属性的API
它们为int of_property_read_u8()、of_property_read_u16()等,实现于include/linux/of.h: 

static inline int of_property_read_u8(const struct device_node *np,const char *propname,u8 *out_value)
{
        return of_property_read_u8_array(np, propname, out_value, 1);
}
static inline int of_property_read_u16(const struct device_node *np,const char *propname,u16 *out_value)
{
        return of_property_read_u16_array(np, propname, out_value, 1);
}
static inline int of_property_read_u32(const struct device_node *np,const char *propname,u32 *out_value)
{
        return of_property_read_u32_array(np, propname, out_value, 1);
}

//前者读取字符串属性,后者读取字符串数组属性中的第index个字符串。
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);

如drivers/clk/clk.c中的of_clk_get_parent_name()
透过:of_property_read_string_index()
遍历clkspec结点的所有"clock-output-names"字符串数组属性。


const char *of_clk_get_parent_name(struct device_node *np, int index)
{
        struct of_phandle_args clkspec;
        const char *clk_name;
        int rc;

        if (index < 0)
                return NULL;

        rc = of_parse_phandle_with_args(np, "clocks", "#clock-cells", index,
                                        &clkspec);
        if (rc)
                return NULL;

        if (of_property_read_string_index(clkspec.np, "clock-output-names",
                                  clkspec.args_count ? clkspec.args[0] : 0,
                                          &clk_name) < 0)
                clk_name = clkspec.np->name;

        of_node_put(clkspec.np);
        return clk_name;
}
EXPORT_SYMBOL_GPL(of_clk_get_parent_name);

//如果设备结点np含有propname属性,则返回true,否则返回false。一般用于检查空属性是否存在。
static inline bool of_property_read_bool(const struct device_node *np, const char *propname);

//通过设备结点直接进行设备内存区间的 ioremap(),index是内存段的索引。
//若设备结点的reg属性有多段,可通过index标示要ioremap的是哪一段,只有1段的情况,index为0。
//采用Device Tree后,大量的设备驱动通过of_iomap()进行映射,而不再通过传统的ioremap。
void __iomem *of_iomap(struct device_node *node, int index);


//透过Device Tree或者设备的中断号,实际上是从.dts中的interrupts属性解析出中断号。若设备使用了多个中断,index指定中断的索引号。
//还有一些OF API,这里不一一列举,具体可参考include/linux/of.h头文件。
unsigned int irq_of_parse_and_map(struct device_node *dev, int index);


九、总结:

    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中定义的设备结点进行匹配。
0 0
原创粉丝点击