【Linux驱动】设备树

参考【正点原子】I.MX6U嵌入式Linux驱动开发指南V1.8
参考【韦东山】嵌入式Linux应用开发完全手册V5_IMX6ULL_Pro开发板
主要是学习其中的内容。

设备树描述的是硬件资源,上一节platform_device表示的是设备的硬件资源,将其写成模板的形式,然后将device设备的数组抽离出来就形成了设备资源,因为模板具有普世性,所以修改硬件资源的时候代码不需要重新编译,只需要修改设备资源,在启动的时候引用该设备资源即可。所以需要另外一个文件格式来表达设备。
系统作为总控制线,多个设备挂在系统上,离开系统设备就没什么作用,所以暂且规定这些设备挂在系统线上,就叫做设备树。类似于C语言,设备树文件有自己的源文件,称为dts:device tree source。编译为dtb:device tree blob。
因为只需要描述一些设备,所以不需要c语言那样有头文件、变量函数等等内容,以最简单的方式进行命名

Devicetree格式

DTS文件格式

1、DTS文件布局

/dts-v1/; // 表示版本
[memory reservations] // 格式为: /memreserve/ <address> <length>;
/ {
 [property definitions]
 [child nodes]
};

2、node格式

[label:] node-name[@unit-address] {
 [properties definitions]
 [child nodes]
};

label 是标号,可以省略。label 的作用是为了方便地引用 node,比如:

/dts-v1/;
/ {
    uart0: uart@fe001000 {
    compatible="ns16550";
    reg=<0xfe001000 0x100>;
};
};

修改node的方式可以使用label或者node-name[@unit-address]

// 在根节点之外使用 label 引用 node:
&uart0 {
 status = “disabled”;
};
或在根节点之外使用全路径:
&{/uart@fe001000} {
 status = “disabled”;
};

properties的格式

  • Property 格式 1:
[label:] property-name = value;
  • Property 格式2:
[label:] property-name;

Property的取值有三种

arrays of cells(1 个或多个 32 位数据, 64 位数据使用 232 位数据表示), 
string(字符串), 
bytestring(1 个或多个字节)
  • arrays of cells

Arrays of cells : cell 就是一个 32 位的数据,用尖括号包围起来
64bit 数据使用 2 个 cell 来表示,用尖括号包围起来:

clock-frequency = <0x00000001 0x00000000>;
  • string
compatible = "simple-bus";
  • A bytestring 字节序列
local-mac-address = [00 00 12 34 56 78]; // 每个 byte 使用 2 个 16 进制数来表示
local-mac-address = [000012345678]; // 每个 byte 使用 2 个 16 进制数来表示

可以是各种值的组合, 用逗号隔开:

compatible = "ns16550", "ns8250";
example = <0xf00f0000 19>, "a strange property format";

设备树文件也可以像C语言文件那样包含一个头文件,一般被命名为dtsi,“i"表示"include”,被别的文件引用的
dtsi和dts中的语法一致

/dts-v1/;
#include <dt-bindings/input/input.h>
#include "imx6ull.dtsi"
/ {
……
};

常用的属性

#address-cells、#size-cells

  • cell 指一个32位的数值
  • address-cells:address要用多少个32位表示
  • size-cells:size要用多少个32为数来表示
/ {
#address-cells = <1>;
#size-cells = <1>;
memory {
	reg = <0x80000000 0x20000000>;
 	};
};

所以表示的是一个32位表示起始地址,另一个32位表示size

compatible

compatible表示兼容,对于某个LED,内核中可能有A、B、C三个驱动都支持它,所以可以这样写

led {
	compatible = “A”, “B”, “C”;
};

启动内核时就会按照先后顺序找到对应的驱动
使用compatible的时候建议取值的形式为:"manufacturer,model",即“厂家名,模块名”。

model

如果说compatible是内核兼容的名称,那么model表示的就是描述的硬件是什么

{
	compatible = "samsung,smdk2440", "samsung,mini2440";
	model = "jz2440_v3";
};

表示它自己为jz2440_v3,但是兼容smdk2440mini2440

status

dtsi文件中定义了很多设备,但是在板子上的某些设备是没有的,所以可以disable

&uart1 {
 status = "disabled";
};

image.png

reg

reg的本意是register,用来表示寄存器地址,主要是用来访问该设备的,相当于寄存器地址,由adress-cellsize-cells决定。

/dts-v1/;
/ {
	#address-cells = <1>;
	#size-cells = <1>;
	memory {
	reg = <0x80000000 0x20000000>;
};
};

常用的节点

根节点

dts文件中必须有一个根节点

/dts-v1/;
/ {
model = "SMDK24440";
compatible = "samsung,smdk2440";
#address-cells = <1>;
#size-cells = <1>;
};

根节点必须有的属性

#address-cells // 在它的子节点的 reg 属性中, 使用多少个 u32 整数来描述地址(address)
#size-cells // 在它的子节点的 reg 属性中, 使用多少个 u32 整数来描述大小(size)
compatible // 定义一系列的字符串, 用来指定内核中哪个 machine_desc 可以支持本设备
 // 即这个板子兼容哪些平台
 // uImage : smdk2410 smdk2440 mini2440 ==> machine_desc 
model // 咱这个板子是什么
// 比如有 2 款板子配置基本一致, 它们的 compatible 是一样的
 // 那么就通过 model 来分辨这 2 款板子

CPU节点

一般不需要我们设置,在dtsi节点中都定义好了

cpus {
	#address-cells = <1>;
	#size-cells = <0>;
	cpu0: cpu@0 {
	 .......
	 }
};

memery节点

memory {
reg = <0x80000000 0x20000000>;
};

chosen节点

chosen {
bootargs = "noinitrd root=/dev/mtdblock4 rw init=/linuxrc console=ttySAC0,115200";
};

设备树编译、更换

dts 编译成 dtb 来给内核使用

make dtbs V=1

image.png
之所以可以使用#include命令是因为一开始使用了预编译
如果使用dtc命令直接进行编译就不能直接写#include命令进行包含

设备树是使用bootloader进行传输的
image.png
在内核中的boot可以看到设备树文件

在内核中有一个fdt文件

# ls /sys/firmware/
devicetree fdt

/sys/firmware/devicetree 目录下是以目录结构程现的 dtb 文件, 根节点对应 base 目录, 每一个节点对应一个目录, 每一个属性对应一个文件。这些属性的值如果是字符串,可以使用 cat 命令把它打印出来;对于数值,可以用 hexdump 把它打印出来。

可以拷贝出来反编译成dts文件

./scripts/dtc/dtc -I dts -O dtb -o tmp.dtb arch/arm/boot/dts/xxx.dts // 编译 dts 为 dt
b
./scripts/dtc/dtc -I dtb -O dts -o tmp.dts arch/arm/boot/dts/xxx.dtb // 反编译 dtb 为
dts

还可以看到/sys/firmware/fdt 文件,它就是 dtb 格式的设备树文件,可以把它复制出来放到 ubuntu 上,执行下面的命令反编译出来(-I dtb:输入格式是 dtb,-O dts:输出格式是 dts)

cd 板子所用的内核源码目录
./scripts/dtc/dtc -I dtb -O dts /从板子上/复制出来的/fdt -o tmp.dts

设备树的处理过程

设备树的处理过程是dtb->device->platform_device
1、处理DTB

of_fdt.h // dtb 文件的相关操作函数, 我们一般用不到, 
// 因为 dtb 文件在内核中已经被转换为 device_node 树(它更易于使用)

2、处理 device_node

of.h // 提供设备树的一般处理函数, 
// 比如 of_property_read_u32(读取某个属性的 u32 值),
// of_get_child_count(获取某个 device_node 的子节点数)
of_address.h // 地址相关的函数, 
// 比如 of_get_address(获得 reg 属性中的 addr, size 值)
// of_match_device (从 matches 数组中取出与当前设备最匹配的一项)
of_dma.h // 设备树中 DMA 相关属性的函数
of_gpio.h // GPIO 相关的函数
of_graph.h // GPU 相关驱动中用到的函数, 从设备树中获得 GPU 信息
of_iommu.h // 很少用到
of_irq.h // 中断相关的函数
of_mdio.h // MDIO (Ethernet PHY) API
of_net.h // OF helpers for network devices. 
of_pci.h // PCI 相关函数
of_pdt.h // 很少用到
of_reserved_mem.h // reserved_mem 的相关函数

3、处理platform_device

of_platform.h // 把 device_node 转换为 platform_device 时用到的函数, 
 // 比如 of_device_alloc(根据 device_node 分配设置 platform_device), 
 // of_find_device_by_node (根据 device_node 查找到 platform_device),
 // of_platform_bus_probe (处理 device_node 及它的子节点)
of_device.h // 设备相关的函数, 比如 of_match_device

如何转换称为platform_device
内核处理设备树的函数调用过程,这里不去分析;我们只需要得到如下结论:
◼ platform_device 中含有 resource 数组, 它来自 device_node 的 reg, interrupts 属性;
◼ platform_device.dev.of_node 指向 device_node, 可以通过它获得其他属性

of_platform.h 中的函数
1、of_find_device_by_node

extern struct platform_device *of_find_device_by_node(struct device_node *np);

设备树中的每一个节点,在内核里都有一个 device_node;你可以使用device_node 去找到对应的 platform_device。

2、platform_get_resource
设备节点转换为playform_device之后,设备树中的reg属性、interrupt属性也会转换成resource

/**
* platform_get_resource - get a resource for a device
* @dev: platform device
* @type: resource type // 取哪类资源?IORESOURCE_MEM、IORESOURCE_REG
* // IORESOURCE_IRQ 等
* @num: resource index // 这类资源中的哪一个?
*/
struct resource *platform_get_resource(struct platform_device *dev,unsigned int type, unsigned int num);

platform_device如何与platform_driver配对

static int platform_match(struct device *dev, struct device_driver *drv)
{
    struct platform_device *pdev = to_platform_device(dev);
    struct platform_driver *pdrv = to_platform_driver(drv);

    /* When driver_override is set, only bind to the matching driver */
    if (pdev->driver_override)
        return !strcmp(pdev->driver_override, drv->name);

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

    /* Then try ACPI style match */
    if (acpi_driver_match_device(dev, drv))
        return 1;

    /* Then try to match against the id table */
    if (pdrv->id_table)
        return platform_match_id(pdrv->id_table, pdev) != NULL;

    /* fall-back to driver name match */
    return (strcmp(pdev->name, drv->name) == 0);
}

1、最先比较:是否强制选择某个driver

比较:
**platform_device.driver_override 和 platform_driver.driver.name**
可以设置 platform_device 的 driver_override,强制选择某个 platform_driver。

2、然后比较设备树信息

platform_device.dev.of_node 和 platform_driver.driver.of_match_table。 由设备树节点转换得来的 platform_device 中,含有一个结构体:of_node。 它的类型如下
image.png
如果一个 platform_driver 支 持 设 备 树 , 它 的platform_driver.driver.of_match_table 是一个数组,类型如下:
image.png
使用设备树信息来判断 dev 和 drv 是否配对时:
首先,如果 of_match_table 中含有 compatible 值,就跟 dev 的 compatile属性比较,若一致则成功,否则返回失败;
其次,如果 of_match_table 中含有 type 值,就跟 dev 的 device_type 属性比较,若一致则成功,否则返回失败;
最后,如果 of_match_table 中含有 name 值,就跟 dev 的 name 属性比较,若一致则成功,否则返回失败。
而设备树中建议不再使用 devcie_type 和 name 属性,所以基本上只使用设备节点的 compatible 属性来寻找匹配的 platform_driver。

3、比较:platform_device_id

比较 platform_device. name 和 platform_driver.id_table[i].name,id_table 中可能有多项。platform_driver.id_table 是“platform_device_id”指针,表示该 drv支持若干个 device,它里面列出了各个 device 的{.name, .driver_data},其中的“name”表示该 drv 支持的设备的名字,driver_data 是些提供给该device 的私有数据。

4、最后比较

platform_device.name 和 platform_driver.driver.nameplatform_driver.id_table 可能为空,这 时 可 以 根 据 platform_driver.driver.name 来 寻 找 同 名platform_device。
image.png
image.png

其他不会生成platform_device,怎么访问他们

内核会把 dtb 文件解析出一系列的 device_node 结构体,我们可以直接访问这些 device_node。内核源码 incldue/linux/of.h 中声明了 device_node 和属性 property的操作函数,device_node 和 property 的结构体定义如下
在驱动中每一个节点都被转换为device_node结构体
image.png
根节点被保存在全局变量of_root中,从of_root开始可以访问到任意节点

一些特定的设备树节点会被转换成platform_device

  • 根节点下含有 compatile 属性的子节点
  • 含有特定 compatile 属性的节点的子节点
    • 如果一个节点的 compatile 属性,它的值是这 4 者之一:“simple-bus”,“simple-mfd”,“isa”,“arm,amba-bus”, 那 么 它 的 子结点 ( 需 含 compatile 属性)也可以转换为 platform_device。
  • 总线 I2C、SPI 节点下的子节点:不转换为 platform_device
    • 某个总线下的子节点,应该交给对应的总线驱动程序来处理, 它们不应该被转换为 platform_device。
{
 	mytest {
     compatile = "mytest", "simple-bus";
     mytest@0 {
		compatile = "mytest_0";
 		};
 	};
 
	i2c {
 		compatile = "samsung,i2c";
 		at24c02 {
			compatile = "at24c02"; 
		 };
 	};
 	spi {
 		compatile = "samsung,spi"; 
 		flash@0 {
			compatible = "winbond,w25q32dw";
			spi-max-frequency = <25000000>;
			reg = <0>;
 		};
 	};
 };

分析:

  • mytest会转换成platform_device,因为他兼容simple-busi2c一般表示i2c控制器,他会被转换成platform_device,在内核中有的对应的platform_device
  • at24c02不会被转换称为platform_device,他被如何处理由父节点platform_driver决定,一般是被创建为一个i2c_client,spi节点也一样。

1、找到节点

1、of_find_node_by_path
根据路径找到节点,比如“/”就对应根节点,“/memory”对应 memory 节点。
**static inline struct device_node *of_find_node_by_path(const char *path);**
2、of_find_node_by_name
根据名字找到节点,节点如果定义了 name 属性,那我们可以根据名字找到它。
函数原型:
**extern struct device_node *of_find_node_by_name(struct device_node *from,const char*name);**
参数 from 表示从哪一个节点开始寻找,传入 NULL 表示从根节点开始寻找。但是在设备树的官方规范中不建议使用“name”属性,所以这函数也不建议使用。
3、of_find_node_by_type
根据类型找到节点,节点如果定义了 device_type 属性,那我们可以根据类型找到它。
函数原型:
**extern struct device_node *of_find_node_by_type(struct device_node *from, const char *type);**
参数 from 表示从哪一个节点开始寻找,传入 NULL 表示从根节点开始寻找。但是在设备树的官方规范中不建议使用“device_type”属性,所以这函数也不建议使用。
4、of_find_compatible_node
根据 compatible 找到节点,节点如果定义了 compatible 属性,那我们可以根据 compatible 属性找到它。
函数原型:
extern struct device_node *of_find_compatible_node(struct device_node *from, const char *type, const char *compat);

  • 参数 from 表示从哪一个节点开始寻找,传入 NULL 表示从根节点开始寻找。
  • 参数 compat 是一个字符串,用来指定 compatible 属性的值;
  • 参数 type 是一个字符串,用来指定 device_type 属性的值,可以传入 NULL。

5、of_find_node_by_phandle
根据 phandle 找到节点。dts 文件被编译为 dtb 文件时,每一个节点都有一个数字 ID,这些数字 ID 彼此不同。可以使用数字 ID 来找到 device_node。这些数字 ID 就是 phandle。
函数原型:
**extern struct device_node *of_find_node_by_phandle(phandle handle);**
参数 from 表示从哪一个节点开始寻找,传入 NULL 表示从根节点开始寻找。
6、of_get_parent
找到 devicenode 的父节点。
函数原型:
**extern struct device_node *of_get_parent(const struct device_node *node);**
参数 from 表示从哪一个节点开始寻找,传入 NULL 表示从根节点开始寻找。
7、of_get_next_parent
这个函数名比较奇怪,怎么可能有“next parent”?它实际上也是找到 device_node 的父节点,跟 of_get_parent 的返回结果是一样的。
差别在于它多调用下列函数,把 node 节点的引用计数减少了 1。这意味着调用 of_get_next_parent 之后,你不再需要调用 of_node_put 释放 node 节点。
**of_node_put(node);**
函数原型:
**extern struct device_node *of_get_next_parent(struct device_node *node);**
8、of_get_next_child
取出下一个子节点
extern struct device_node *of_get_next_child(const struct device_node *node,struct device_node *prev);

  • 参数 node 表示父节点;
  • prev 表示上一个子节点,设为 NULL 时表示想找到第 1 个子节点。不断调用 of_get_next_child 时,不断更新 pre 参数,就可以得到所有的子节点。

9、of_get_next_available_child
取出下一个“可用”的子节点,有些节点的 status 是“disabled”,那就会跳过这些节点。
函数原型:
struct device_node *of_get_next_available_child( const struct device_node *node,struct device_node *prev);

  • 参数 node 表示父节点;
  • prev 表示上一个子节点,设为 NULL 时表示想找到第 1 个子节点。

10、of_get_child_by_name
根据名字取出子节点
extern struct device_node *of_get_child_by_name(const struct device_node *node,const char *name);

  • 参数node表示父节点
  • name表示子节点的名字

2、找到属性–of_find_property

内核源码 incldue/linux/of.h 中声明了 device_node 的操作函数,当然也包括属性的操作函数:of_find_property
找到节点的属性
extern struct property *of_find_property(const struct device_node *np,const char *name,int *lenp);
参数 np 表示节点,我们要在这个节点中找到名为 name 的属性。
lenp 用来保存这个属性的长度,即它的值的长度。
在设备树中的节点为:

xxx_node {
 xxx_pp_name = “hello”;
};

上述节点中,“xxx_pp_name”就是属性的名字,值的长度是 6。

3、获取属性的值

1、of_get_property
根据名字找到节点的属性,并返回它的值
函数原型

/*
* Find a property with a given name for a given node
* and return the value.
*/
const void *of_get_property(const struct device_node *np,const char *name,int *lenp)

参数 np 表示节点,我们要在这个节点中找到名为 name 的属性,然后返回它的值。
lenp 用来保存这个属性的长度,即它的值的长度。

2、of_property_count_elems_of_size
根据名字找到节点的属性,确定它的值有多少个元素(elem)

* of_property_count_elems_of_size - Count the number of elements in a property
*
* @np: device node from which the property value is to be read.
* @propname: name of the property to be searched.
* @elem_size: size of the individual element
*
* Search for a property in a device node and count the number of elements of
* size elem_size in it. Returns number of elements on sucess, -EINVAL if the
* property does not exist or its length does not match a multiple of elem_size
* and -ENODATA if the property does not have a value.
*/
int of_property_count_elems_of_size(const struct device_node *np,const char *propname,int elem_size)

3、参数np表示节点,我们要在这个节点中找到名为popname的属性,然后返回下列结果
return prop->length / elem_size;
在设备树中,节点大概是这样

xxx_node {
 xxx_pp_name = <0x50000000 1024> <0x60000000 2048>;
};
  • 调用 of_property_count_elems_of_size(np, “xxx_pp_name”, 8)时,返回值是 2;
  • 调用 of_property_count_elems_of_size(np, “xxx_pp_name”, 4)时,返回值是 4;

表示的是8位字数量

4、读整数u32/u64

函数原型为:

static inline int of_property_read_u32(const struct device_node *np,const char *propname,u32 *out_value);
extern int of_property_read_u64(const struct device_node *np,
const char *propname,u64 *out_value);

设备树中的节点为:

xxx_node {
 name1 = <0x50000000>;
 name2 = <0x50000000 0x60000000>;
};
  • 调用 of_property_read_u32 (np, “name1”, &val)时,val 将得到值 0x50000000;
  • 调用 of_property_read_u64 (np, “name2”, &val)时,val 将得到值 0x6000000050000000。

5、读某个整数u32/u64

函数原型为:

extern int of_property_read_u32_index(const struct device_node *np,const char *propname,u32 index, u32 *out_value);

在设备树中,节点大概是这样:

xxx_node {
 name2 = <0x50000000 0x60000000>;
};

调用of_property_read_u32 (np, “name2”, 1, &val)时,val将得到值0x60000000。

6、读数组

函数原型为:

int of_property_read_variable_u8_array(const struct device_node *np,const char *propname,u8 *out_values,size_t sz_min, size_t sz_max);
int of_property_read_variable_u16_array(const struct device_node *np,const char *propname,u16 *out_values,size_t sz_min, size_t sz_max);
int of_property_read_variable_u32_array(const struct device_node *np,const char *propname,u32 *out_values,size_t sz_min, size_t sz_max);
int of_property_read_variable_u64_array(const struct device_node *np,const char *propname,u64 *out_values,size_t sz_min, size_t sz_max);

在设备树种:

xxx_node {
 name2 = <0x50000012 0x60000034>;
}

属性name2的值,长度为8

  • 调用 of_property_read_variable_u8_array (np, “name2”, out_values, 1, 10)时,out_values 中将会保存这 8 个字节: 0x12,0x00,0x00,0x50,0x34,0x00,0x00,0x60。
  • 调用 of_property_read_variable_u16_array (np, “name2”, out_values, 1, 10)时,out_values 中将会保存这 4 个 16 位数值: 0x0012, 0x5000,0x0034,0x6000。

总之,这些函数要么能取到全部的数值,要么一个数值都取不到;

  • 如果值的长度在 sz_min 和 sz_max 之间,就返回全部的数值;
  • 否则一个数值都不返回。

7、读字符串

函数原型为:

int of_property_read_string(const struct device_node *np, const char *propname,const char **out_string);
  • 返回节点 np 的属性(名为 propname)的值;
  • (*out_string)指向这个值,把它当作字符串。
  • 11
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值