What is the use of linux dts

青灯夜游
Release: 2023-03-14 10:44:27
Original
1528 people have browsed it

In Linux, dts is a device tree source file, used to describe device information; device tree technology writes the device's hardware resource information in the dts file. The device tree source file dts is compiled into a dtb binary and passed to the operating system when the bootloader is running. The operating system parses and expands it to generate a topology diagram of the hardware device. With this topology diagram, the compilation process can be directly passed through The interface provided by the system obtains the node and attribute information of the device tree.

What is the use of linux dts

#The operating environment of this tutorial: linux7.3 system, Dell G3 computer.

1. What is a device tree?

Device tree (dt: device tree) is a parameter representation and transfer technology used by the Linux kernel. During device initialization during the system boot phase, the hardware information described in the device tree is transferred to the operation System;

  • dts (device tree source): device tree source file, describing device information;

    Device tree source file dts is compiled into dtb binary, in the bootloader It is passed to the operating system at runtime, and the operating system parses and expands it to generate a topology diagram of the hardware device. With this topology diagram, the node and attribute information of the device tree can be obtained directly through the interface provided by the system during the compilation process.

  • dtc(device tree compiler): device tree compilation/decompilation/debugging tool;

  • dtb(device tree binary) : Binary device tree image;

  • dtsi (device tree source include): A header file that functions like a device tree file and can be referenced by the dts file through include. The dtsi file generally describes the common part ;

2. What problem does the device tree solve?

  • In the device driver source code, It is divided into driver code and device code. The driver code is the method of operating the hardware. The device code is the hardware resources and data. When the driver code and the device code match, the driver's probe function will be called. The probe function will use the resources of the device code to initialize. Device;

  • Before the device tree, the device code was written directly in the kernel source code and existed in the form of platform_device structure. The driver code and device code were also matched on the platform bus. When you need to modify the device resources, you need to modify the kernel source code;

  • Device tree technology writes the hardware resource information of the device in the dts file. If you need to modify it, modify the dts file. There is no need to Modify the kernel source code;

  • Does not use device tree technology: the kernel source code will be filled with a large amount of device hardware description information, causing the kernel source code to continue to increase, but the increased hardware description information code and kernel functions Not relevant;

  • After using device tree technology: the hardware description information of the device is in the dts file, which is easy to modify, but the kernel needs to add code to parse the dts file format;

3. How does the device tree work?

What is the use of linux dts

  • Driver development Or write/modify the dts file according to the hardware so that the driver code can match the appropriate device hardware information in the future;

  • When compiling the kernel, the kernel will first compile the dtc, and then use the dtc to The dts file is compiled into dtb;

  • When uboot starts the kernel, it relocates the kernel image and dtb to the memory and tells the kernel the memory address of the dtb;

  • In the early stage of kernel startup, the internal function is called to parse the dtb, and after obtaining the hardware information, it is assembled into a hardware function, and finally matched with the driver code;

4. Explanation of the dts file format of the device tree source code

##4.1. The storage location of the dts file in the kernel source code

arm architecture: in the arch/arm/boot/dts directory

4.2, Introduction to dts file format

  • Use /* */ for comments. Note that the ones starting with # are not comments.

  • The semicolon is the separation between paragraph blocks. symbols, {}, [] and are the encapsulation symbols of paragraph blocks, and the C language language class

  • /dts-v1/ node represents the version number of dts, currently They are all v1

  • /{} is the root node. In theory, there should be only one root node. It is said that dtc will merge all root nodes into the same

  • dts is a tree-like multi-node organization. The basic unit is node. Except for root, other nodes have parents and can also have children

4.3. Node format

##4.3.1. Format definition

[label:] <node-name> [@<unit-address>]{
  [property]
  [child nodes]
  [child nodes]
  ......
};</unit-address></node-name>
Copy after login

4.3.2. Format interpretation

    []: indicates that the item can be omitted, : indicates that it cannot be omitted;
  • [label:]: label is the label name. In order to facilitate access to the node, you can access the node directly through &label later.

  • node-name:节点名称。根节点的名称必须是/

  • [@unit-address]:unit-address是设备地址,如cpu node就是0、1这种,reg node就是0x12010000这种;

4.3.3、示例代码

cpus {
	/* 下面三项是cpus节点的属性 */
	#address-cells = <1>;
	#size-cells = <0>;
	enable-method = "hisilicon,hi3516dv300";

	/* 下面是子节点 */
	cpu@0 {
		device_type = "cpu";
		compatible = "arm,cortex-a7";
		clock-frequency = <HI3516DV300_FIXED_1000M>;
		reg = <0>;
	};
};
Copy after login
  • cpus是cpu的父节点,从形式来能直观的看出来,cpu节点是被cpus节点的大括号括起来的;

  • cpus节点省略了标签名和设备地址,只有节点名称;

5、节点属性分析

5.1、GPIO属性格式

/{
	gpx1:gpx1{
		controller;
		#gpio-cells=<2>;
	};
	
	key@11400c24{
		compatible="fs4412,key";
		reg=<0x11400c24 0x4>;
		intn-key=<&gpx1 2 2>;
	}
}
Copy after login
  • gpio-controller:说明该节点描述的是一个gpio控制器;

  • #gpio-cells:描述gpio使用节点的属性一个cell的内容;

5.2、compatible属性格式

uart0: uart@120a0000 {
	compatible = "arm,pl011", "arm,primecell";
	reg = <0x120a0000 0x1000>;
	interrupts = <0 6 4>;
	clocks = <&clock HI3516DV300_UART0_CLK>;
	clock-names = "apb_pclk";
	status = "disabled";
};

/* 在驱动中对应的结构体*/

//struct device_driver->of_match_table->compatible

struct of_device_id {
	char	name[32];
	char	type[32];
	char	compatible[128];
	const void *data;
};
Copy after login

(1)compatible属性是用于设备节点和设备驱动匹配用的,在内核描述驱动的structdevice_driver结构体中,compatible变量中就会保存用于匹配的字符串,当设备节点和驱动的

compatible相同时就匹配成功;

(2)compatible后面可以有多个字符串,优先匹配靠前的字符串,靠前的字符串匹配不上才会匹配后面的字符串;

5.3、model属性格式

/ {
	model = "Tyr DEMO Board";
	compatible = "hisilicon,hi3516dv300";

	memory {
		device_type = "memory";
		reg = <0x82000000 0x20000000>;
	};};
Copy after login

(1)model是描述模块信息的,一般只有根节点才有,标明设备树文件对应的开发板的名称;

(2)在内核的启动打印中可以看到model的值:“OF: fdt:Machine model: Tyr DEMO Board”;

5.4、status属性格式

&uart0 {
	status = "okay";
};
Copy after login
状态值含义
okey表示设备是可操作的
disabled表示当前不可操作,但是后续是可以更改为可操作性的
fail、failed表示有严重错误,几乎不可能再可操作了

(1)status描述设备信息状态,在设备树文件中可以根据需求设置模块的状态,功能就是开启/关闭某个模块;

(2)在dtsi文件中,默认都是关闭模块的,在开发板对应的dts文件中自己去打开需要的模块;

5.5、reg属性格式

clock: clock@12010000 {
	compatible = "hisilicon,hi3516dv300-clock";
	#address-cells = <1>;	/* 表示reg里面的数据address占用一个字长*/
	#size-cells = <1>;		/* 表示reg里面的数据size占用一个字长,注意字长不是字节*/
	#clock-cells = <1>;
	#reset-cells = <2>;
	reg = <0x12010000 0x1000>;	/*起始地址是0x12010000,长度是0x1000*/
};
Copy after login
  • reg属性:配置某个硬件模块对应的地址范围信息;

  • #address-cells属性:表示reg里面的数据address占用的字长,注意字长不是字节;

  • #size-cells:表示reg里面的数据size占用的字长,注意字长不是字节;

  • reg = :address一般用来表示起始地址,length一般表示持续长度;

5.6、中断属性格式

gic: interrupt-controller@10300000 {
	compatible = "arm,cortex-a7-gic";
	#interrupt-cells = <3>;	/*表示interrupts用三个cell来描述中断*/
	#address-cells = <0>;
	interrupt-controller;	/*标明gic节点是中断控制器*/
	/* gic dist base, gic cpu base , no virtual support */
	reg = <0x10301000 0x1000>, <0x10302000 0x100>;
 };
	
ipcm: ipcm@045E0000 {
	compatible = "hisilicon,ipcm-interrupt";
	interrupt-parent = <&gic>;	/*父节点是gic节点*/
	interrupts = <0 10 4>;	/*<中断域 中断 触发方式>*/
	reg = <0x10300000 0x4000>;	
	status = "okay";
};
Copy after login

(1)interrupt-controller:无值属性,表示这是个中断控制器node
(2)#interrupt-cells:这是中断控制器节点的属性,用来标识这个控制器需要几个cell做中断描述符
(3)interrupt-parent:标识此设备节点属于哪一个中断控制器,如果没有这个属性,会自动依附父节点
(4)interrupts :一个中断标识符列表,表示每一个中断输出信号

6、特殊节点

6.1、chosen子节点

6.1.1、chosen子节点功能介绍

chosen {
	stdout-path = "serial0:115200n8";
};
Copy after login

(1)chosen子节点不对应真实的设备,是用来描述内核启动参数的,对应于uboot启动内核时传递的bootargs参数;
(2)上面是摘抄的内核dts文件中的chosen子节点,里面只设置了stdout-path属性,也就是把输出设置成串口0,波特率是115200;
(3)dts文件中设置的属性会被覆盖点,具体就是uboot在启动内核时,会将bootargs启动参数转换成chosen子节点的属性,替换掉dts文件中设置的属性;

6.1.2、chosen子节点在内核中的体现

~ # ls /proc/device-tree/chosen/
bootargs  name
~ # 
~ # cat /proc/device-tree/chosen/bootargs 
mem=1408M console=ttyS0,115200 root=/dev/mmcblk0p7 rootfstype=squashfs rootwait
~ # 
~ # cat /proc/device-tree/chosen/name 
chosen
~ #
Copy after login

6.2、aliases子节点

	aliases {
		serial0 = &uart0;
		gpio0 = &gpio_chip0;
		gpio1 = &gpio_chip1;
		gpio2 = &gpio_chip2;
		······	
	};
Copy after login

aliases就是别名的意思,aliases节点主要功能就是给节点定义别名,为了方便访问节点。不过我们在节点命名的时候可以加上label标签,直接通过&label引用标签来访问也很方便,aliases节点内部其实也是通过引用标签名来定义别名;

7、节点相关操作

7.1、节点引用和内容替换

gpio_chip1: gpio_chip@120d1000 {
	compatible = "arm,pl061", "arm,primecell";
	reg = <0x120d1000 0x1000>;
	interrupts = <0 17 4>;
	clocks = <&clock  HI3516DV300_SYSAPB_CLK>;
	clock-names = "apb_pclk";
	#gpio-cells = <2>;
	status = "disabled";
};

/*引用gpio_chip1节点*/
&gpio_chip1 {
	status = "okay";	/*替换status属性内容*/
};
Copy after login

对于已经定义好的节点,我们通过引用节点的方式,重新定义某些属性,效果上看就是替换掉某些属性的值;

7.2、合并节点内容

/{
	node{
		key1=value1;
	}
}

/{
	node{
		key2=value2;
	}
}

//合并的结果
/{
	node{
		key1=value1;
		key2=value2;
	}
}
Copy after login

有时候我们需要增加硬件描述的信息,这时候就可以在后面创新定义该节点,最后解析的时候会把同名节点不同的部分进行合并;

相关推荐:《Linux视频教程

The above is the detailed content of What is the use of linux dts. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template