首頁  >  文章  >  系統教程  >  Linux裝置驅動之devicetree:一種描述和管理硬體設備的高效方法

Linux裝置驅動之devicetree:一種描述和管理硬體設備的高效方法

PHPz
PHPz轉載
2024-02-10 17:33:31602瀏覽

你是否想過如何在Linux系統中為你的硬體設備編寫驅動程式?你是否想過如何在Linux系統中讓你的驅動程式適應不同的硬體平台和配置?你是否想過如何在Linux系統中讓你的驅動程式實現一些進階的功能,例如熱插拔、電源管理、裝置共享等?如果你對這些問題感興趣,那麼本文將為你介紹一種實現這些目標的有效方法——Linux裝置驅動之devicetree。 devicetree是一種用來描述硬體設備的資料結構,它可以讓你用一種簡單而統一的方式,將硬體設備的資訊和屬性傳遞給內核,從而實現設備的識別和驅動。 devicetree也是一種用來實現硬體無關性的機制,它可以讓你用一種靈活且可移植的方式,將硬體設備的配置和管理從驅動程式程式碼中分離出來,從而實現多平台的支援。 devicetree也是一種用於實現高級功能的框架,它可以讓你用一種標準而通用的方式,定義和使用各種硬體設備的接口和協議,從而實現熱插拔、電源管理、設備共享等功能。本文將從devicetree的基本概念、語法規則、寫作方法、編譯過程、載入方式等方面,為你詳細地介紹devicetree在Linux裝置驅動中的應用和作用,幫助你掌握這種有用且強大的方法。

Devicetree(設備樹)是用來描述系統硬體資訊的樹模型,其旨在unify核心。透過bootloader將devicetree的資訊傳給kernel,然後kernel根據這些裝置描述初始化對應的板級驅動,達到一個核心多個平台共享的目的。


Overview

Devicetree主要為描述不可插拔(非動態)設備的板級硬體資訊而設計的。它由分層的描述設備資訊的節點(node)組成樹結構。每個node包含的內容透過property/value對來表示。除root節點外,每個節點都有parent。如圖:
Linux裝置驅動之devicetree:一種描述和管理硬體設備的高效方法

Node Names

#除root節點名稱用’/’表示外,其餘節點都由node-name@unit-address來命名,且同一層級必須是唯一的。

  • node-name

    表示节点名,由1-31个字符组成。如非必须,推荐使用以下通用的node-name: 
    cpu、memory、memory-controller、gpio、serial、watchdog、flash、compact-flash、
    
    rtc、interrupt-controller、dma-controller、ethernet、ethernet-phy、timer、
    
    mdio、spi、i2c、usb、can、keyboard、ide、disk、display、sound、atm、cache-
    
    controller、crypto、fdc、isa、mouse、nvram、parallel、pc-card、pci、pcie、sata、
    scsi、vme。
    
  • unit-address

    表示这个节点所在的bus类型。它必须和节点中reg属性的第一个地址一致。如果这个节点没有
    
    reg属性,则不需“@unit-address”。
    

Path Names

表示一個節點的完整路徑(full path)。型如:

    /node-name-1/node-name-2/node-name-N

Properties

#每個節點所包含的主要內容就是這個所描述的設備的屬性訊息,由name和value組成:

  • Property Names

    1-31个字符,可包含字母、数字、及‘,’,‘.’,‘_’,‘+’,‘?’,‘#’。
    
  • Property Values

Value Description
empty 屬性值為空,用來表示true-false資訊
u32/u64 32/64位元大端位元組序的無符號整形,表示時需加
#string,stringlist null-terminated字串或其組成的清單
#

Standard Properties

  • compatible

    Value type: 
    Description:
        表示兼容的设备类型,内核据此选择合适的驱动程序。由多个字符串组成,从左到由列出
    
    这个设备兼容的驱动(from most specific to most general)。
        推荐的格式为:“制造商名,具体型号”。
    Example:
        compatible = "fsl,mpc8641-uart", "ns16550";
        内核先搜索支持“fsl,mpc8641-uart”的驱动,如未找到,则搜索支持更通用的“ns16550”
    
    
    设备类型的驱动。
    
  • model

    Value type: 
    Description:
        表明设备型号。
        推荐的格式为:“制造商名,具体型号”。
    Example:
        model = "fsl,MPC8349EMITX";
    
  • phandle

    Value type: 
    Description:
        用一个树内唯一的数字标识所在的这个节点,其他节点可以直接通过这个数字标识来引用
    
    这个节点。
    Example:
        pic@10000000 {
            phandle = ;
            interrupt-controller;
        };
        interrupt-parent = ;
    
  • status

    Value type: 
    Description:
        表示设备的可用状态:
        "okay" -> 设备可用
        "disabled" -> 目前不可用,但以后可能会可用
        "fail" -> 不可用。出现严重问题,得修一下
        "fail-sss" -> 不可用。出现严重问题,得修一下。sss指明错误类型。
    
  • #address-cells and #size-cells

    Value type: 
    Description:
        在拥有子节点的节点中使用,来描述它的字节点的地址分配问题。即分别表示子节点中使
    
    用多少个u32大小的cell来编码reg属性中的address域和size域。
    
        这两个属性不会继承,必须明确指出。如未指出,默认#address-cells=2,#size-
    cells=1。
    Example:
        soc {
            #address-cells = ;
            #size-cells = ;
            serial {
                reg = ;
            };
        };
    
  • reg

    Value type:  encoded as an arbitraty number of (address, length) pairs.
    Description:
        描述该设备在parent bus定义的地址空间中的地址资源分配。
    Example:
        reg = ;
        a 32-byte block at offset 0x3000 and a 256-byte block at offset 0xFE00。
    
  • virtual-reg

    Value type: 
    Description:
        表示映射到reg第一个物理地址对应的effective address。使bootloader能够提供给内
    
    核它所建立的virtual-to-physical mappings。
    
  • ranges

    Value type:  or  encoded as an arbitrary number of (child-bus-address,parent-bus-
    
    address, length) triplets.
    Description:
        提供了子地址空间与父地址空间的映射关系,如果值为空则父子地址相等,无需转换。
    Example:
        soc {
            compatible = "simple-bus";
            #address-cells = ;
            #size-cells = ;
            ranges = ;
    
            serial {
                compatible = "ns16550";
                reg = ;
            };
        };
        将子节点serial的0x0地址映射到父节点soc的0xe0000000,映射长度为0x100000。此时
    
    reg的实际物理地址就为0xe0004600。
    
  • dma-ranges

    Value type:  or  encoded as an arbitrary number of (child-bus-address,parent-bus-address, length) triplets.
    Description:
        提供了dma地址的映射方法。
    

Interrupts

描述中断的属性有4个:

  • interrupt-controller

    一个空的属性用来指示这个节点设备是接收中断信号的控制器。
    
  • #interrupt-cells

    这是上面所说中断控制器中的一个属性,用来描述需要用多少个cell来描述这个中断控制器的
    
    interrupt specifier(类似#address-cells和#size-cells)。
    
  • interrupt-parent

    常出现在根节点的一个属性,它的属性值是指向interrupt-controller的一个phandle。可从
    
    parent继承。
    
  • interrupts

    包含interrupt specifiers列表,每一个specifier表示一个中断输出信号。
    

Example

/ {
    interrupt-parent = ;

    intc: interrupt-controller@10140000 {        
              compatible = "arm,pl190";        
              reg = ;        
              interrupt-controller;        
                #interrupt-cells = ;    
    };
    serial@101f0000 {        
        interrupts = ;    
    };
};

Base Device Node Types

所有的设备树都必须有一个root节点,且root节点下必须包含一个cpus节点和至少一个memory节点。

  • root node

    root节点须包含 #address-cells、#size-cells、model、compatible等属性。
    
  • /cpus node

    是cpu子节点的父节点容器。须包含 #address-cells、#size-cells属性。
    
  • /cpus/cpu* node

    是描述系统cpu的节点。
    
  • /memory node

    描述系统物理内存的layout。须包含reg节点。
    Example:
    假如一个64位系统有如下两块物理内存:
    - RAM: starting address 0x0, length 0x80000000 (2GB)
    - RAM: starting address 0x100000000, length 0x100000000 (4GB)
    则我们可以有下面两种描述方法(#address-cells =  and #size-cells =):
    Example #1
        memory@0 {
            reg = ;
        };
    Example #2
        memory@0 {
            reg = ;
        };
        memory@100000000 {
            reg = ;
        };
    
  • /chosen node

    根节点下的一个子节点,不是描述设备而是描述运行时参数。常用来给内核传递bootargs:
    chosen {
        bootargs = "root=/dev/nfs rw nfsroot=192.168.1.1 console=ttyS0,115200";
    };
    
  • /aliases node

    由1-31个字母、数字或下划线组成的设备节点full path的别名。它的值是节点的全路径,因此最终会被编码成字符串。
    aliases {
        serial0 = "/simple-bus@fe000000/serial@llc500";
    }
    

Device Bindings

更多具体设备具体类别的描述信息:内核源代码/Documentation/devicetree/bindings。


DTS是描述devicetree的源文本文件,它通过内核中的DTC(Devicetree Compiler)编译后生成相应平台可烧写的二进制DTB。

Devicetree Blob (DTB) Structure

DTB又称Flattened Devicetree(FDT),在内存中的结构如下图所示:
Linux裝置驅動之devicetree:一種描述和管理硬體設備的高效方法

Header

大端字节序结构体:

struct fdt_header {
    uint32_t magic; /* contain the value 0xd00dfeed (big-endian) */
    uint32_t totalsize; /* the total size of the devicetree data structure */
    uint32_t off_dt_struct; /* offset in bytes of the structure block */
    uint32_t off_dt_strings; /* offset in bytes of the strings block */
    uint32_t off_mem_rsvmap; /* offset in bytes of the memory reservation block */
    uint32_t version; /* the version of the devicetree data structure */
    uint32_t last_comp_version; /* the lowest version used is backwards compatible */
    uint32_t boot_cpuid_phys; /* the physical ID of the system’s boot CPU */
    uint32_t size_dt_strings; /* the length in bytes of the strings block */
    uint32_t size_dt_struct; /* the length in bytes of the structure block */
};

Memory Reservation Block

  • Purpose

    为系统保留一些特殊用途的memory。这些保留内存不会进入内存管理系统。
    
  • Format

    struct fdt_reserve_entry {
        uint64_t address;
        uint64_t size;
    };
    

Structure Block

Devicetree结构体存放的位置。由一行行“token+内容”片段线性组成。

  • token
    每一行内容都由一个32位的整形token起始。token指明了其后内容的属性及格式。共有以下5种token:
token Description
FDT_BEGIN_NODE (0x00000001) 节点起始,其后内容为节点名
FDT_END_NODE (0x00000002) 节点结束
FDT_PROP (0x00000003) 描述属性
FDT_NOP (0x00000004) nothing,devicetree解析器忽略它
FDT_END (0x00000009) block结束
  • tree structure
    • (optionally) any number of FDT_NOP tokens
    • FDT_BEGIN_NODE
      • The node’s name as a null-terminated string
      • [zeroed padding bytes to align to a 4-byte boundary]
    • For each property of the node:
      • (optionally) any number of FDT_NOP tokens
      • FDT_PROP token
        • property information
        • [zeroed padding bytes to align to a 4-byte boundary]
    • Representations of all child nodes in this format
    • (optionally) any number of FDT_NOP tokens
    • FDT_END_NODE token

Devicetree Source (DTS) Format

Node and property definitions

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

File layout

Version 1 DTS files have the overall layout:

/dts-v1/; /* dts 版本1 */
[memory reservations] /* DTB中内存保留表的入口 */
    / {
        [property definitions]
        [child nodes]
    };

通过本文,我们了解了devicetree在Linux设备驱动中的应用和作用,学习了如何编写、编译、加载、修改和调试devicetree。我们发现,devicetree是一种非常适合嵌入式系统开发的方法,它可以让我们方便地描述和管理硬件设备,实现硬件无关性和高级功能。当然,devicetree也有一些注意事项和限制,比如需要遵循语法规范、需要注意兼容性问题、需要注意内存占用和性能影响等。因此,在使用devicetree时,我们需要有一定的硬件知识和经验,以及良好的编程习惯和调试技巧。希望本文能够为你提供一个入门级的指导,让你对devicetree有一个初步的认识和理解。如果你想深入学习devicetree,建议你参考更多的资料和示例,以及自己动手实践和探索。

以上是Linux裝置驅動之devicetree:一種描述和管理硬體設備的高效方法的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文轉載於:lxlinux.net。如有侵權,請聯絡admin@php.cn刪除