设备树 文档下载

设备树最初是在PowerPC架构上使用的一种硬件描述机制,后面才慢慢应用到其他架构下的,比如X86、ARM

如何编写设备树文件,如何设备树,需要参考设备树语法。

ePAPR 文档就是用来描述设备树语法的官方参考文档,下载地址:Power_ePAPR_APPROVED_v1.1.pdf
如果你想要下载最新的文档,可以到设备树官方仓库:https://github.com/devicetree-org

在这个文档中,定义了很多设备树语法

  • node的定义和名称定义格式
  • 属性的定义

一个设备树文件,经过设备树编译器(device tree compiler,简称DTC),编译成二进制设备树文件 xxx.dtb,然后和内核镜像一起加载到内存运行。

dtc编译器可以直接从网上下载:

# sudo apt-get install device-tree-compiler

Linux内核源码自带dtc编译器,在源码目录的scripts/dtc下面就是dtc编译器相关的源码,用户在编译内核和dtb文件时,会顺便编译dtc工具,然后在Makefile中被规则中的命令行调用。

对于初学者来说,在学习设备树时,需要注意的一个地方是,Linux dtc编译器和 ePAPR 标准文档也是有一些差异的:

#address-cells and #size-cells

在 ePAPR 文档中,这两个属性需要明确定义,不能继承父节点的#address-cells的值,而在Linux dtc 编译器规则下,如果在一个node中,这两个属性没有定义,#address-cells默认是2,#size-cells defaults to 1,编译器会给你警告,使用这些默认值。

aliases node

---  ePAPR

   3.3 aliases node
      'A client program may use an alias property name to refer to
       a full device path as all or part of its string value.'

      'A client program, when considering a string as a device path,
       shall detect and use the alias.'

---  Linux

   of_find_node_by_path() and of_find_node_opts_by_path() check
   whether the path begins with "/".  If it does not, then the first
   element must be a property in the aliases node.  That first element
   is replaced with the value of the property in the aliases node.

   It is not known whether other code (such as drivers) is aware of
   the requirement to be aware of aliases.

   Linux also has a concept of numeric id derived from the property
   names in the aliases node.  All trailing digits in the property
   name are used to create the "id".  The "stem" is the property
   name, with the trailing digits removed.  For each property in
   the aliases node, the tuple of stem, id, and the device tree
   node pointer for the path in the property's value is added to
   the aliases_lookup list.  A driver can use of_alias_get_id()
   to retrieve the id for a given stem and device tree node
   pointer.

   This is commonly used to number devices, although the practice
   is controversial.

chosen node

---  ePAPR

   3.5 chosen node

   The chosen node is not required.  If it was required, sections 3.1 and 3.5
   would explicitly state such (for example, section 3.6 CPUS Node Properties
   states 'A cpus node is required for all device trees.').

   The list of required nodes is listed in section 3.1:

   3.1 Base Device Node Types

      'The sections that follow specify the requirements for the base set of device nodes required in an
       ePAPR-compliant device tree.

       All device trees shall have a root node and the following nodes shall be present at the root of all
       device trees:
          • One cpus node
          • At least one memory node'

---  Linux

   The chosen node is required.

更多差异,可以参考这个文档:https://elinux.org/Device_Tree_Linux#Linux_vs_ePAPR_Version_1.1

驱动开发核心理论,Linux内核开发入门实战视频教程:《Linux内核编程》,具有一线芯片原厂开发经验的驱动工程师录制,内含Linux设备树课程,教你如何修改设备树文件和基于设备树接口开发驱动。详情点击:王利涛老师个人淘宝店:Linux内核编程