Table of Contents
5、节点属性分析" >5、节点属性分析
6、特殊节点" >6、特殊节点
7、节点相关操作" >7、节点相关操作

What is the use of linux dts

Mar 14, 2023 am 10:34 AM
linux

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!

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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
1 months ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
1 months ago By 尊渡假赌尊渡假赌尊渡假赌
Will R.E.P.O. Have Crossplay?
1 months ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Difference between centos and ubuntu Difference between centos and ubuntu Apr 14, 2025 pm 09:09 PM

The key differences between CentOS and Ubuntu are: origin (CentOS originates from Red Hat, for enterprises; Ubuntu originates from Debian, for individuals), package management (CentOS uses yum, focusing on stability; Ubuntu uses apt, for high update frequency), support cycle (CentOS provides 10 years of support, Ubuntu provides 5 years of LTS support), community support (CentOS focuses on stability, Ubuntu provides a wide range of tutorials and documents), uses (CentOS is biased towards servers, Ubuntu is suitable for servers and desktops), other differences include installation simplicity (CentOS is thin)

How to install centos How to install centos Apr 14, 2025 pm 09:03 PM

CentOS installation steps: Download the ISO image and burn bootable media; boot and select the installation source; select the language and keyboard layout; configure the network; partition the hard disk; set the system clock; create the root user; select the software package; start the installation; restart and boot from the hard disk after the installation is completed.

Centos options after stopping maintenance Centos options after stopping maintenance Apr 14, 2025 pm 08:51 PM

CentOS has been discontinued, alternatives include: 1. Rocky Linux (best compatibility); 2. AlmaLinux (compatible with CentOS); 3. Ubuntu Server (configuration required); 4. Red Hat Enterprise Linux (commercial version, paid license); 5. Oracle Linux (compatible with CentOS and RHEL). When migrating, considerations are: compatibility, availability, support, cost, and community support.

How to use docker desktop How to use docker desktop Apr 15, 2025 am 11:45 AM

How to use Docker Desktop? Docker Desktop is a tool for running Docker containers on local machines. The steps to use include: 1. Install Docker Desktop; 2. Start Docker Desktop; 3. Create Docker image (using Dockerfile); 4. Build Docker image (using docker build); 5. Run Docker container (using docker run).

Detailed explanation of docker principle Detailed explanation of docker principle Apr 14, 2025 pm 11:57 PM

Docker uses Linux kernel features to provide an efficient and isolated application running environment. Its working principle is as follows: 1. The mirror is used as a read-only template, which contains everything you need to run the application; 2. The Union File System (UnionFS) stacks multiple file systems, only storing the differences, saving space and speeding up; 3. The daemon manages the mirrors and containers, and the client uses them for interaction; 4. Namespaces and cgroups implement container isolation and resource limitations; 5. Multiple network modes support container interconnection. Only by understanding these core concepts can you better utilize Docker.

What to do after centos stops maintenance What to do after centos stops maintenance Apr 14, 2025 pm 08:48 PM

After CentOS is stopped, users can take the following measures to deal with it: Select a compatible distribution: such as AlmaLinux, Rocky Linux, and CentOS Stream. Migrate to commercial distributions: such as Red Hat Enterprise Linux, Oracle Linux. Upgrade to CentOS 9 Stream: Rolling distribution, providing the latest technology. Select other Linux distributions: such as Ubuntu, Debian. Evaluate other options such as containers, virtual machines, or cloud platforms.

What computer configuration is required for vscode What computer configuration is required for vscode Apr 15, 2025 pm 09:48 PM

VS Code system requirements: Operating system: Windows 10 and above, macOS 10.12 and above, Linux distribution processor: minimum 1.6 GHz, recommended 2.0 GHz and above memory: minimum 512 MB, recommended 4 GB and above storage space: minimum 250 MB, recommended 1 GB and above other requirements: stable network connection, Xorg/Wayland (Linux)

What underlying technologies does Docker use? What underlying technologies does Docker use? Apr 15, 2025 am 07:09 AM

Docker uses container engines, mirror formats, storage drivers, network models, container orchestration tools, operating system virtualization, and container registry to support its containerization capabilities, providing lightweight, portable and automated application deployment and management.

See all articles