在过去的ARM Linux源码中,arch/arm/plat-xxx和arch/arm/mach-xxx中充斥着大量的垃圾代码,很多代码只是在描述板级设备硬件细节,而这些代码对内核来说就是垃圾
由此引出了设备树。
DTS(设备树)描述板级设备,起源自OpenFimware(OF)
——图片来自野火Linux笔记
设备树的中节点会转化为linux中device,会代替平台驱动中的device和平台驱动进行匹配。
设备树文件也可以包含引用C语言的头文件
//编译dts
make ARCH=arm -j4 CROSS_COMPILE=arm-linux-gnueabihf- dtbs
./scripts/dtc/dtc -I dts -O dtb -o xxx.dtb arch/arm/boot/dts/xxx.dts // 编译 dts 为 dtb
./scripts/dtc/dtc -I dtb -O dts -o xxx.dts arch/arm/boot/dts/xxx.dtb // 反编译 dtb 为 dts
设备树由bootloader传递给内核
将xxx.dts编译为xxx.dtb的工具
类似C语言的编译器
DTC位于内核scripts/dtc/目录下
DTC可以在ubuntu中单独安装
sudo apt-get install device-tree-compiler
编译dts文件为dtb文件
make dtbs
反汇编dtb文件为dts文件
./scripts/dtc/dtc -I dtb -O dts -o xxx.dts arch/arm/boot/dts/xxx.dtb
设备树节点和属性如何描述设备硬件细节需要文档来说明,一般是txt格式的文档
该文档描述对应节点的兼容性、必须的属性和可选的属性
这些文档位于内核目录Documentation/devicetree/bindings目录下
一般使用的Uboot,Uboot从v1.1.3开始支持设备树
使能设备,需要在编译uboot时在config文件加入如下宏定义
#define CONFIG_OF_LIBFDT
设置xxx.dtb设备树文件的地址
UBoot> fdt addr 0x71000000 //假设dtb文件存放在0x71000000地址
参考Devicetree SpecificationV0.2.pdf
设备树由一系列被命名的节点(Node)和属性(Property)组成。节点还可以包含子节点。
属性:成对出现的名称和值
设备树文件内容:
/ {
model = "Seeed i.MX6 ULL NPi Board";
compatible = "fsl,imx6ull-14x14-evk", "fsl,imx6ull";
aliases {
pwm0 = &pwm1;
pwm1 = &pwm2;
pwm2 = &pwm3;
pwm3 = &pwm4;
};
chosen {
stdout-path = &uart1;
};
memory {
reg = <0x80000000 0x20000000>;
};
reserved-memory {
#address-cells = <1>;
#size-cells = <1>;
ranges;
linux,cma {
compatible = "shared-dma-pool";
reusable;
size = <0x14000000>;
linux,cma-default;
};
};
...
};
&cpu0 {
dc-supply = <®_gpio_dvfs>;
clock-frequency = <800000000>;
};
&clks {
assigned-clocks = <&clks IMX6UL_CLK_PLL4_AUDIO_DIV>;
assigned-clock-rates = <786432000>;
};
在节点名称前加&符号
表示向该节点追加内容
node-name@unit-address {
属性1 = …
属性2 = …
属性3= …
子节点…
}
所谓节点标签就是给节点起别名
cpu0: cpu@0 {
compatible = "arm,cortex-a7";
device_type = "cpu";
reg = <0>;
}
给节点cpu起一个别名为cpu0
当节点名字很长时,可以通过给节点起别名,方便访问
aliases {
can0 = &flexcan1;
can1 = &flexcan2;
ethernet0 = &fec1;
ethernet1 = &fec2;
...
}
统一在一个节点中给其它节点定义"别名"
chosen 节点主要是为了 uboot 向 Linux 内核传递数据
一般.dts 文件中 chosen 节点通常为空或者内容很少
设备树文件chosen节点内容:
chosen {
stdout-path = &uart1;
};
设置了stdout-path = &uart1,表示标准输出使用uart1
查看内核中的chosen子节点:
debian@npi:/proc/device-tree$ ls chosen/
bootargs linux,initrd-end linux,initrd-start name stdout-path
该子节点bootargs linux,initrd-end linux,initrd-start name几个属性。
bootargs是uboot传递给内核的启动参数,可以得知多出来的几个属性是uboot设置的
interrupt-controller
表明自己是中断控制器,该属性为空
#interrupt-cells
设备中断属性的cell大小
interrupt-parent
通过它来指定它所依附的中断控制器的phandle
如:
intc:interrupt-controller@10140000
interrupt-parent=<&intc>
内核启动会解析DTB,在/proc/device-tree/目录生成节点对应的和设备树节点名字相同的文件
内核解析设备树文件流程:
start_kernel() -> setup_arch() -> unflatten_device_tree() -> __unflatten_device_tree() -> unflatten_dt_node()
最终解析设备树节点的函数为unflatten_dt_node()
include/linux/of.h
描述设备节点数据结构:
struct device_node {
const char *name; /* 节点名字 */
const char *type; /* 设备类型 */
phandle phandle;
const char *full_name;
struct fwnode_handle fwnode;
struct property *properties; /* 属性 */
struct property *deadprops; /* removed properties */
struct device_node *parent; /* 父节点 */
struct device_node *child; /* 子节点 */
struct device_node *sibling;
#if defined(CONFIG_OF_KOBJ)
struct kobject kobj;
#endif
unsigned long _flags;
void *data;
#if defined(CONFIG_SPARC)
const char *path_component_name;
unsigned int unique_id;
struct of_irq_controller *irq_trans;
#endif
};
描述属性的数据结构:
struct property {
char *name;
int length;
void *value;
struct property *next;
#if defined(CONFIG_OF_DYNAMIC) || defined(CONFIG_SPARC)
unsigned long _flags;
#endif
#if defined(CONFIG_OF_PROMTREE)
unsigned int unique_id;
#endif
#if defined(CONFIG_OF_KOBJ)
struct bin_attribute attr;
#endif
};
查找指定节点函数:
extern struct device_node *of_find_node_by_name(struct device_node *from,
const char *name);
extern struct device_node *of_find_node_by_type(struct device_node *from,
const char *type);
extern struct device_node *of_find_compatible_node(struct device_node *from,
const char *type, const char *compat);
static inline struct device_node *of_find_node_by_path(const char *path)
查找父/子节点 :
extern struct device_node *of_get_parent(const struct device_node *node);
extern struct device_node *of_get_next_parent(struct device_node *node);
extern struct device_node *of_get_next_child(const struct device_node *node,
struct device_node *prev);
extern struct device_node *of_get_compatible_child(const struct device_node *parent,
const char *compatible);
extern struct device_node *of_get_child_by_name(const struct device_node *node,
const char *name);
获取属性值:
extern int of_property_read_u32_index(const struct device_node *np,
const char *propname,
u32 index, u32 *out_value);
static inline int of_property_read_u8_array(const struct device_node *np,
const char *propname,
u8 *out_values, size_t sz)
static inline int of_property_read_u16_array(const struct device_node *np,
const char *propname,
u16 *out_values, size_t sz)
static inline int of_property_read_u32_array(const struct device_node *np,
const char *propname,
u32 *out_values, size_t sz)
static inline int of_property_read_u64_array(const struct device_node *np,
const char *propname,
u64 *out_values, size_t sz)
-EINVAL
表示属性不存在,-ENODATA
表示没有要读取的数据,-EOVERFLOW
表示属性值列表太小示例:
/*添加led节点*/
rgb_led_red@0x020C406C{
compatible = "red_led";
reg = <0x020C406C 0x00000004
0x020E006C 0x00000004
0x020E02F8 0x00000004
0x0209C000 0x00000004
0x0209C004 0x00000004>;
status = "okay";
};
//设备树的匹配条件
static struct of_device_id dts_match_table[] = {
{.compatible = "red_led", }, //通过设备树来匹配
};
static int led_red_driver_probe(struct platform_device *dev)
{
int err;
int ret;
u32 regdata[8];
int i;
struct device *tmpdev;
led_dev.dev_node = of_find_node_by_path(RED_LED_DTS_NODE); //找到red_led的设备树节点
if (!led_dev.dev_node) {
printk("red led can not found!\r\n");
return -EINVAL;
}
/* 获取设备中寄存器属性值 */
ret = of_property_read_u32_array(led_dev.dev_node, "reg", regdata, 8);
......
/* 将寄存器物理地址转换成虚拟地址 */
led_dev.virtual_ccgr1 = ioremap(regdata[0], regdata[1]);
led_dev.virtual_gpio1_io4 = ioremap(regdata[2], regdata[3]);
led_dev.virtual_dr = ioremap(regdata[4], regdata[5]);
led_dev.virtual_gdir = ioremap(regdata[6], regdata[7]);
}
获取属性值为字符串的函数:
extern int of_property_read_string(const struct device_node *np,
const char *propname,
const char **out_string);
更多函数可以查看 内核文件include/linux/of.h