Linux教程網
Linux教程網
Linux教程網
您现在的位置: Linux教程網 >> UnixLinux >  >> Linux基礎 >> Linux技術

Linux 裝置驅動開發:設備樹在 platform 驅動中的使用

設備樹的概念已在 Exynos4412 核心移植(六)——設備樹解析中介紹過,這裡接著看它在驅動開發中究竟扮演什麼角色。

Device Tree 是一種描述硬體的資料結構,設備樹源碼檔(以 .dts 結尾)就是用來描述目標板硬體資訊的。它由一系列具名節點(node)與屬性(property)組成,節點本身還可包含子節點;屬性其實就是成對出現的 name 與 value。原本這些資訊大多被 hardcode 進核心,如今改由設備樹描述。

一、基礎概念

1. 基本資料格式

Linux 裝置驅動開發:設備樹在 platform 驅動中的使用 Linux 裝置驅動開發:設備樹在 platform 驅動中的使用
/ {
    node1 {
        a-string-property = "A string";
        a-string-list-property = "first string", "second string";
        a-byte-data-property = [0x01 0x23 0x34 0x56];
        child-node1 {
            first-child-property;
            second-child-property = <1>;
            a-string-property = "Hello, world";
        };
        child-node2 {
        };
    };
    node2 {
        an-empty-property;
        a-cell-property = <1 2 3 4>;  /* 每個數字(cell)是一個 uint32 */
        child-node1 {
        };
    };
};

這棵樹沒有描述任何具體硬體,但揭示了結構:

a 根節點 / 下有兩個子節點 node1 與 node2,node1 又有 child-node1 與 child-node2,屬性分散其間。

b 屬性就是簡單的鍵值對,值可以為空或包含任意位元組流,資料型別並未編碼進資料結構,而是用幾種基本表示法呈現。

c 文字字串以雙引號表示:string-property = "a string"。

d Cells 是以角括號分隔的 32 位元無符號整數:cell-property = <0xbeef 123 0xabcd1234>。

e 二進位資料以方括號分隔:binary-property = [0x01 0x23 0x45 0x67]。

f 不同格式的資料可用逗號串接:mixed-property = "a string", [0x01 0x23 0x45 0x67], <0x12345678>。

g 逗號也能建立字串列表:string-list = "red fish", "blue fish"。

二、設備在設備樹中的描述

系統中的每個設備由設備樹中的一個節點表示。

1. 命名慣例

每個節點都必須有 <name>[@<unit-address>] 格式的名稱。<name> 是最長 31 字元的 ASCII 字串,命名應反映它代表什麼設備:代表 3com 乙太網路介面卡的節點該叫 ethernet,而不是 3com509。若設備有記憶體位址,則加上 unit-address,該位址通常也是存取設備的主位址,並在節點的 reg 屬性中列出。

2. 加入設備節點

Linux 裝置驅動開發:設備樹在 platform 驅動中的使用 Linux 裝置驅動開發:設備樹在 platform 驅動中的使用
/ {
    compatible = "acme,coyotes-revenge";
    cpus {
        cpu@0 { compatible = "arm,cortex-a9"; };
        cpu@1 { compatible = "arm,cortex-a9"; };
    };
    serial@101F0000 { compatible = "arm,pl011"; };
    serial@101F2000 { compatible = "arm,pl011"; };
    gpio@101F3000 { compatible = "arm,pl061"; };
    interrupt-controller@10140000 { compatible = "arm,pl190"; };
    spi@10115000 { compatible = "arm,pl022"; };
    external-bus {
        ethernet@0,0 { compatible = "smc,smc91c111"; };
        i2c@1,0 {
            compatible = "acme,a1234-i2c-bus";
            rtc@58 { compatible = "maxim,ds1338"; };
        };
        flash@2,0 {
            compatible = "samsung,k8f1315ebm", "cfi-flash";
        };
    };
};

樹的層次結構反映了設備如何連到系統:外部總線上的設備是外部總線節點的子節點,i2c 設備是 i2c 總線控制節點的子節點。整體呈現的是從 CPU 視角看出去的系統視圖。此時這棵樹仍不完整,還缺少設備之間的連接資訊。

要注意的是,每個設備節點都必須有一個 compatible 屬性;flash 節點的 compatible 有兩個字串。另外,命名應反映設備類型而非特定型號,ePAPR 規範 2.2.2 節列出的通用命名值得優先採用。

3. compatible 屬性

樹中每個代表設備的節點都要有 compatible,這是 OS 決定綁定哪個驅動的關鍵。它是字串列表,第一個字串以「<製造商>,<型號>」格式指定精確設備,第二個字串則代表相容的其他設備。例如 Freescale MPC8349 SoC 的序列埠實作了 National Semiconductor ns16550 的暫存器介面,因此其 compatible 寫為 compatible = "fsl,mpc8349-uart", "ns16550":前者指明精確設備,後者表明與 ns16550 UART 暫存器級相容。

由於歷史原因,ns16550 沒有製造商前綴,但所有新的 compatible 值都應加上前綴。這樣既讓既有驅動能綁定新設備,又能唯一準確地識別硬體。

4. 編址

可定址的設備用三個屬性把位址資訊編碼進設備樹:reg、#address-cells、#size-cells。每個可定址設備都有一個 reg 屬性,形式是元組列表:

reg = <address1 length1 [address2 length2] [address3 length3] ... >

每個位址值由一或多個 32 位元整數(cells)組成,長度值同理,也可以為空。既然位址與長度都是變長的,父節點的 #address-cells 與 #size-cells 就是用來說明子節點各有幾個 cell。換句話說,正確解讀一個子節點的 reg,必須先看父節點的這兩個值。

5. 記憶體映射設備

與 CPU 節點中的單一位址不同,記憶體映射設備會被分配一段可回應的位址範圍,#size-cells 用來說明每個子節點 reg 元組的長度大小。下例中每個位址值為 1 cell、長度值也為 1 cell,這在 32 位元系統中很典型;64 位元平台則可把兩者設為 2 來實現 64 位元定址。

Linux 裝置驅動開發:設備樹在 platform 驅動中的使用 Linux 裝置驅動開發:設備樹在 platform 驅動中的使用
serial@101f2000 {
    compatible = "arm,pl011";
    reg = <0x101f2000 0x1000>;
};
gpio@101f3000 {
    compatible = "arm,pl061";
    reg = <0x101f3000 0x1000
           0x101f4000 0x0010>;
};
interrupt-controller@10140000 {
    compatible = "arm,pl190";
    reg = <0x10140000 0x1000>;
};

每個設備都被分配一個基位址與該區域大小。此例中 GPIO 的位址被切成兩段:0x101f3000~0x101f3fff 與 0x101f4000~0x101f400f。

三、設備樹在 platform 驅動中的用法

1. 設備樹取代 platform_device

Device Tree 用來描述設備資訊,每個設備以節點形式呈現;而在傳統 platform 模型中,我們用 platform_device 描述設備。兩者對比如下:

Linux 裝置驅動開發:設備樹在 platform 驅動中的使用 Linux 裝置驅動開發:設備樹在 platform 驅動中的使用
fs4412-beep {
    compatible = "fs4412,beep";
    reg = <0x114000a0 0x4 0x139D0000 0x14>;
};

a fs4412-beep 是節點名,符合命名規範,從名字即可知道描述的是 beep 設備。

b compatible = "fs4412,beep" 中,fs4412 是製造商前綴。

c reg 把位址資訊編碼進設備樹,代表用到的暫存器位址與長度。

Linux 裝置驅動開發:設備樹在 platform 驅動中的使用 Linux 裝置驅動開發:設備樹在 platform 驅動中的使用
static struct resource beep_resource[] = {
    [0] = {
        .start = 0x114000a0,
        .end   = 0x114000a0 + 0x4,
        .flags = IORESOURCE_MEM,
    },
    [1] = {
        .start = 0x139D0000,
        .end   = 0x139D0000 + 0x14,
        .flags = IORESOURCE_MEM,
    },
};

static struct platform_device hello_device = {
    .name = "bigbang",          // 沒用了
    .id = -1,
    .dev.release = hello_release,
    .num_resources = ARRAY_SIZE(beep_resource),
    .resource = beep_resource,
};

可見設備樹中的設備節點完全可以取代 platform_device。

2. 有了設備樹,device 與 driver 如何匹配

原本是靠 .name 匹配,如今設備樹取代了 device,比較的名字存在哪裡?先看原來的 platform_match:

Linux 裝置驅動開發:設備樹在 platform 驅動中的使用 Linux 裝置驅動開發:設備樹在 platform 驅動中的使用
static int platform_match(struct device *dev, struct device_driver *drv)
{
    struct platform_device *pdev = to_platform_device(dev);
    struct platform_driver *pdrv = to_platform_driver(drv);

    /* 先嘗試 OF 風格匹配 */
    if (of_driver_match_device(dev, drv))
        return 1;
    /* 再嘗試 ACPI 風格匹配 */
    if (acpi_driver_match_device(dev, drv))
        return 1;
    /* 然後匹配 id table */
    if (pdrv->id_table)
        return platform_match_id(pdrv->id_table, pdev) != NULL;
    /* 最後回退到名稱比對 */
    return (strcmp(pdev->name, drv->name) == 0);
}
Linux 裝置驅動開發:設備樹在 platform 驅動中的使用 Linux 裝置驅動開發:設備樹在 platform 驅動中的使用
static inline int of_driver_match_device(struct device *dev,
        const struct device_driver *drv)
{
    return of_match_device(drv->of_match_table, dev) != NULL;
}

繼續追下去,注意這裡的參數是 drv->of_match_table:

Linux 裝置驅動開發:設備樹在 platform 驅動中的使用 Linux 裝置驅動開發:設備樹在 platform 驅動中的使用
const struct of_device_id *of_match_device(const struct of_device_id *matches,
        const struct device *dev)
{
    if ((!matches) || (!dev->of_node))
        return NULL;
    return of_match_node(matches, dev->of_node);
}
EXPORT_SYMBOL(of_match_device);
Linux 裝置驅動開發:設備樹在 platform 驅動中的使用 Linux 裝置驅動開發:設備樹在 platform 驅動中的使用
const struct of_device_id *of_match_node(const struct of_device_id *matches,
        const struct device_node *node)
{
    const struct of_device_id *match;
    unsigned long flags;
    raw_spin_lock_irqsave(&devtree_lock, flags);
    match = __of_match_node(matches, node);
    raw_spin_unlock_irqrestore(&devtree_lock, flags);
    return match;
}
EXPORT_SYMBOL(of_match_node);
Linux 裝置驅動開發:設備樹在 platform 驅動中的使用 Linux 裝置驅動開發:設備樹在 platform 驅動中的使用
const struct of_device_id *__of_match_node(const struct of_device_id *matches,
        const struct device_node *node)
{
    const struct of_device_id *best_match = NULL;
    int score, best_score = 0;
    if (!matches)
        return NULL;
    for (; matches->name[0] || matches->type[0] || matches->compatible[0]; matches++) {
        score = __of_device_is_compatible(node, matches->compatible,
                                          matches->type, matches->name);
        if (score > best_score) {
            best_match = matches;
            best_score = score;
        }
    }
    return best_match;
}
Linux 裝置驅動開發:設備樹在 platform 驅動中的使用 Linux 裝置驅動開發:設備樹在 platform 驅動中的使用
static int __of_device_is_compatible(const struct device_node *device,
        const char *compat, const char *type, const char *name)
{
    struct property *prop;
    const char *cp;
    int index = 0, score = 0;

    /* compatible 匹配優先級最高 */
    if (compat && compat[0]) {
        prop = __of_find_property(device, "compatible", NULL);
        for (cp = of_prop_next_string(prop, NULL); cp;
             cp = of_prop_next_string(prop, cp), index++) {
            if (of_compat_cmp(cp, compat, strlen(compat)) == 0) {
                score = INT_MAX / 2 - (index << 2);
                break;
            }
        }
        if (!score)
            return 0;
    }
    /* type 匹配優於 name 匹配 */
    if (type && type[0]) {
        if (!device->type || of_node_cmp(type, device->type))
            return 0;
        score += 2;
    }
    /* name 匹配略優於不匹配 */
    if (name && name[0]) {
        if (!device->name || of_node_cmp(name, device->name))
            return 0;
        score++;
    }
    return score;
}

關鍵那句是 prop = __of_find_property(device, "compatible", NULL);,一路追到底可以確認是用 "compatible" 來匹配:設備樹載入之後,核心會自動把設備樹節點轉換成 platform_device 這種格式,同時把名字放到 of_node 這個地方。

platform_driver 部分。原本是用 struct driver 中的 name 成員匹配:

Linux 裝置驅動開發:設備樹在 platform 驅動中的使用 Linux 裝置驅動開發:設備樹在 platform 驅動中的使用
struct device_driver {
    const char *name;
    struct bus_type *bus;
    struct module *owner;
    const char *mod_name;              /* 內建模組使用 */
    bool suppress_bind_attrs;          /* 停用透過 sysfs 的 bind/unbind */
    const struct of_device_id *of_match_table;
    const struct acpi_device_id *acpi_match_table;
    int (*probe)(struct device *dev);
    int (*remove)(struct device *dev);
    void (*shutdown)(struct device *dev);
    int (*suspend)(struct device *dev, pm_message_t state);
    int (*resume)(struct device *dev);
    const struct attribute_group **groups;
    const struct dev_pm_ops *pm;
    struct driver_private *p;
}

其中 of_match_table 是 struct of_device_id 類型:

Linux 裝置驅動開發:設備樹在 platform 驅動中的使用 Linux 裝置驅動開發:設備樹在 platform 驅動中的使用
/* 用於匹配一個設備的結構 */
struct of_device_id
{
    char name[32];
    char type[32];
    char compatible[128];
    const void *data;
};

我們要做的就是填充 char compatible[128]。

3. 基於設備樹的 platform_driver

Linux 裝置驅動開發:設備樹在 platform 驅動中的使用 Linux 裝置驅動開發:設備樹在 platform 驅動中的使用
static struct of_device_id beep_table[] = {
    { .compatible = "fs4412,beep" },
};

static struct platform_driver beep_driver = {
    .probe = beep_probe,
    .remove = beep_remove,
    .driver = {
        .name = "bigbang",
        .of_match_table = beep_table,
    },
};

對照原來的寫法:

Linux 裝置驅動開發:設備樹在 platform 驅動中的使用 Linux 裝置驅動開發:設備樹在 platform 驅動中的使用
static struct platform_driver beep_driver = {
    .driver.name = "bigbang",
    .probe = beep_probe,
    .remove = beep_remove,
};

4. 編譯設備樹

在 arch/arm/boot/dts/exynos4412-fs4412.dts 中加入節點:

Linux 裝置驅動開發:設備樹在 platform 驅動中的使用 Linux 裝置驅動開發:設備樹在 platform 驅動中的使用
fs4412-beep {
    compatible = "fs4412,beep";
    reg = <0x114000a0 0x4 0x139D0000 0x14>;
};

然後在核心根目錄編譯設備樹:

make dtbs
vim arch/arm/boot/dts/exynos4412-fs4412.dts
sudo cp arch/arm/boot/dts/exynos4412-fs4412.dtb /tftpboot/

接著把設備樹下載到 0x42000000 處,加載驅動 insmod driver.ko,即可測試。此時 device.c 已不再需要,硬體資訊全由設備樹提供,這正是設備樹最大的價值:驅動與板級資訊徹底分家。

Copyright © Linux教程網 All Rights Reserved