Device Tree¶
WIP
Trang này mới ở mức giới thiệu, sẽ được bổ sung chi tiết sau.
Device Tree là gì¶
Device Tree (DT) là một cấu trúc dữ liệu dạng cây dùng để mô tả phần cứng của board cho kernel. Thay vì hard-code thông tin thiết bị (địa chỉ thanh ghi, IRQ, clock, GPIO...) trong source kernel, thông tin này được khai báo ở file riêng và truyền cho kernel lúc boot. Nhờ đó một kernel image có thể chạy trên nhiều board khác nhau, chỉ cần đổi file device tree.
Device Tree là chuẩn phổ biến trên ARM/ARM64, RISC-V, PowerPC. Trên x86, vai trò tương tự thường do ACPI đảm nhiệm.
Các loại file¶
| File | Vai trò |
|---|---|
.dts |
Device Tree Source, mô tả một board cụ thể |
.dtsi |
Include file, phần dùng chung (SoC, module) |
.dtb |
Device Tree Blob, dạng nhị phân sau khi biên dịch bằng dtc |
.dtbo |
Device Tree Overlay, vá thêm vào DT đang có lúc runtime/boot |
Thông thường .dtsi mô tả SoC, còn .dts của board #include SoC đó rồi bật
hoặc cấu hình các thiết bị cần dùng. Source nằm trong arch/<arch>/boot/dts/,
binding (tài liệu quy định property) nằm trong
Documentation/devicetree/bindings/.
Ví dụ cú pháp¶
/dts-v1/;
/ {
model = "My Board";
compatible = "vendor,my-board";
soc {
#address-cells = <1>;
#size-cells = <1>;
uart0: serial@40001000 {
compatible = "vendor,my-uart";
reg = <0x40001000 0x100>;
interrupts = <0 12 4>;
clocks = <&clk_uart>;
status = "okay";
};
};
};
Một số property hay gặp:
compatible: chuỗi dùng để match node với driver.reg: địa chỉ và kích thước vùng thanh ghi.interrupts: thông tin ngắt.clocks,gpios,pinctrl-*: tham chiếu tới clock, GPIO, pin mux.status:"okay"hoặc"disabled"để bật/tắt thiết bị.- phandle (
&label): tham chiếu tới node khác.
Device Tree và driver¶
- Bootloader (ví dụ U-Boot) nạp
.dtbvào RAM và truyền địa chỉ cho kernel. - Kernel parse DTB, tạo các
platform_device(hoặc i2c/spi device tuỳ bus cha) từ các node. - Driver khai báo bảng
of_match_tablechứa các chuỗicompatible; khi khớp, kernel gọiprobe(). - Trong
probe(), driver đọc property bằng các APIof_*hoặcdevice_property_*.
static const struct of_device_id my_of_match[] = {
{ .compatible = "vendor,my-uart" },
{ }
};
MODULE_DEVICE_TABLE(of, my_of_match);
Xem thêm: Platform Device, Overview about Linux Device Driver.
Công cụ và debug¶
dtc: compile/decompile giữa.dtsvà.dtb(dtc -I dtb -O dts file.dtb)./proc/device-treehoặc/sys/firmware/devicetree/base: xem DT mà kernel đang dùng.make dtbs: build device tree trong kernel source.
Nội dung sẽ bổ sung¶
- Binding và cách viết
compatible. #address-cells,#size-cells,ranges.- Interrupt, clock, GPIO, pinctrl trong DT.
- Overlay (
.dtbo). - Device Tree trong Yocto/Petalinux.