Linuxデバイスドライバーモデルのdevicetree
現在の Linux ドライバーの開発は、デバイスツリーの構成と切り離せません。デバイス数を簡単に把握するためのボード構成情報記述ファイルです。当初、カーネルにはデバイス ツリーがなく、ボードに関するデバイス情報がコードに記述されていたため、カーネルの Arch ディレクトリに多数のボード レベルのソース ファイルが作成され、すべて .c または .h 形式で定義されました。カーネルの他の部分と比較すると、これらの構成は非常に反復的です。この問題を解決するために、カーネルは、特にボード レベルの構成情報を記述するために使用されるデバイス ツリーを導入しました。カーネル ドライバーは、デバイス ツリーを解析してボード レベルを取得します。ドライバー ドライバーの作業を実行すると、ドライバーとボードレベルの構成の分離が完了します。では、デバイス ツリーとは一体何でしょうか? 以下フォローアップしてください。
デバイスツリー
デバイス ツリーは、ボード レベルのハードウェア デバイスを説明するデータ構造テキストです。デバイス ツリー ファイルは通常、.dts または .dtsi 接尾辞が付いたファイルで保存され、ボード レベルのリソースはノードと属性を通じて記述されます。以下は dts ファイルの概要です。
#include <dt-bindings/interrupt-controller/arm-gic.h>
#include <dt-bindings/gpio/gpio.h> /* dts中可以通过include引用其他的头文件,使用它们的定义 */
#include "sun8iw18p1-clk.dtsi" /* 可以引用其它的dts文件 */
#include "sun8iw18p1-pinctrl.dtsi"
/ {
/* device tree,树总会有根,而/则是设备树的根,其他的都是节点,节点可以包含子节点 */
cpus {
/* cpus作为根节点下的一个子节点,它也包含了cpu0、1两个子节点以及enable-method等属性信息 */
enable-method = "allwinner,sun8iw18p1";
#address-cells = <1>;
#size-cells = <0>;
cpu@0 {
device_type = "cpu";
compatible = "arm,cortex-a7","arm,armv7";
reg = <0x0>;
enable-method = "psci";
/* 描述设备的时钟依赖信息,后续驱动可获取该信息 */
clocks = <&clk_pll_cpu>,<&clk_cpu>,<&clk_pll_periph0>;
clock-names = "pll","cpu","periph0";
operating-points-v2 = <&cpu_opp_l_table0>;
cpu-idle-states = <&CPU_SLEEP_0 &CLUSTER_SLEEP_0 &SYS_SLEEP_0>;
};
}
}
デバイスツリーをオンにすると、システムが dts を 1 つずつ解析してデバイスを登録し、その後ドライバーを作成し、ドライバーがデバイスと一致すると、デバイスが正常に使用できるようになります。デバイス ツリーに関しては、構文ルールに準拠している限り、それ以外はすべてドライバーによって解析される必要があるということを覚えておいてください。したがって、ドライバーはデバイス ツリー情報に加えて、デバイス ツリーも解析する必要があります。解析インターフェイスは以下に紹介されています。
一般的に使用されるデバイスツリー API
デバイスツリーから整数変数を取得します。
int of_property_read_u32(const struct device_node *np,
const char *propname,
u32 *out_value);
int of_property_read_u16(const struct device_node *np,
const char *propname,
u16 *out_value);
上記のインターフェイスは、デバイス ツリーまたは次のような整形変数から取得できます。
// dts
dao: dao-test {
compatible = "dao-test";
num = <12>;
};
u32 value;
np指向dao节点,此时通过 of_property_read_u32(np, "num", &value)获取“num”指向的内容,此时value=12。
デバイス ツリーから文字列変数を取得します。
int of_property_read_string(const struct device_node *np,
const char *propname,
const char **out_string);
使用方法は次のとおりです。
// dts
dao: dao-test {
compatible = "dao-test";
num = <12>;
str_info = "test_info";
};
const char *info;
np指向dao节点,此时通过 of_property_read_string(np, "str_info", &info)获取“str_info”指向的内容,此时info指向的内容是“test_info”。
include/linux/of.h
カーネル内のインターフェースを参照することで、より多くの API を使用できます。
デバイスツリーの変換
デバイス ツリーはカーネル段階で使用されます。編集段階では、dts のテキスト状態です。カーネルで使用される場合、dtb バイナリ ファイルに変換されます。システムの起動プロセス中に、uboot がそれをロードして渡します。それをカーネルに渡して使用します。このデバイス ツリーの変換は、dtc ツールを通じて実行されます。dtc ツールは通常、カーネル ソース コードに含まれています。dtc は次のコマンドをサポートします。
root@root#./scripts/dtc/dtc --help
Usage: dtc [options] <input file>
Options: -[qI:O:o:V:d:R:S:p:a:fb:i:H:sW:E:@AThv]
-q, --quiet
Quiet: -q suppress warnings, -qq errors, -qqq all
-I, --in-format <arg>
Input formats are:
dts - device tree source text
dtb - device tree blob
fs - /proc/device-tree style directory
-o, --out <arg>
Output file
-O, --out-format <arg>
Output formats are:
dts - device tree source text
dtb - device tree blob
asm - assembler source
-V, --out-version <arg>
Blob version to produce, defaults to 17 (for dtb and asm output)
-d, --out-dependency <arg>
Output dependency file
-R, --reserve <arg>
Make space for <number> reserve map entries (for dtb and asm output)
-S, --space <arg>
Make the blob at least <bytes> long (extra space)
-p, --pad <arg>
Add padding to the blob of <bytes> long (extra space)
-a, --align <arg>
Make the blob align to the <bytes> (extra space)
-b, --boot-cpu <arg>
Set the physical boot cpu
-f, --force
Try to produce output even if the input tree has errors
-i, --include <arg>
Add a path to search for include files
-s, --sort
Sort nodes and properties before outputting (useful for comparing trees)
-H, --phandle <arg>
Valid phandle formats are:
legacy - "linux,phandle" properties only
epapr - "phandle" properties only
both - Both "linux,phandle" and "phandle" properties
-W, --warning <arg>
Enable/disable warnings (prefix with "no-")
-E, --error <arg>
Enable/disable errors (prefix with "no-")
-@, --symbols
Enable generation of symbols
-A, --auto-alias
Enable auto-alias of labels
-T, --annotate
Annotate output .dts with input source file and line (-T -T for more details)
-h, --help
Print this help and exit
-v, --version
Print version and exit
dtc は多くのパラメータをサポートしていますが、-I -i -O -o
これらは一般的に使用されるパラメータであり、それらを通じて入出力ファイルと形式を指定し、dtb ファイルをカーネル イメージに添付します。