dts --> dtb
------------------------------------
dtc -I dts -O dtb -S 0x3000 -o obj_name.dtb source_name.dts
-S 指定的是生成的dtb文件的大小,需要適當地擴大,以供u-boot創(chuàng)建/choose節(jié)點時使用
查看dts設備節(jié)點
------------------------------------
# ls -al /proc/device-tree
內核如何解析設備樹
------------------------------------
1) 首先將從u-boot 傳遞過來的映像基地址和dtb 文件映像基地址保存通用寄存器r30,r31;
2) 通過調用machine_init() --> early_init_devtree()函數來獲
取內核前期初始化所需的bootargs,cmd_line等系統引導參數;
3) 調用start_kernel() --> setup_arch() -->
unflatten_device_tree()函數來解析dtb文件,構建一個由device_node結構連接而成的單項鏈表,并使用全局變量allnodes指針來保存這個鏈表的頭指針;
4) 內核調用OF提供的API函數獲取allnodes鏈表信息來初始化內核其他子系統、設備等。
dispAllnodes() -- 顯示DTS設備節(jié)點
--------------------------------------
./arch/powerpc/kernel/prom.c
void dispAllnodes()
{
?? struct device_node *dn;
?? dn = allnodes;
?? while (dn)
?? {
?????? printk("dn->name = %s
", dn->name);
?????? printk("dn->type = %s
", dn->type);
?????? printk("dn->full_name = %s
", dn->full_name);
?????? printk("
");
?????? dn = dn->next;
?? }
}
EXPORT_SYMBOL(dispAllnodes);
根據DTS文件的設備節(jié)點,添加設備
linux-2.6.28archpowerpcootdtsmpc8548cds.dts
-------------------------------------------------------
??? soc8548@e0000000 {
??? ??? #address-cells = <1>;
#size-cells = <1>;
#interrupt-cells = <2>;
??? ??? device_type = "soc";
??? ??? ranges = <0 e0000000 00100000>;
??? ??? reg = ;??? // CCSRBAR 1M
??? ??? bus-frequency = <0>;
??? ??? mdio@24520 {
??? ??? ??? #address-cells = <1>;
??? ??? ??? #size-cells = <0>;
??? ??? ??? device_type = "mdio";
??? ??? ??? compatible = "gianfar";
??? ??? ??? reg = <24520 20>;
??? ??? ??? phy0: ethernet-phy@0 {
??? ??? ??? ??? interrupt-parent = <&mpic>;
??? ??? ??? ??? interrupts = <35 0>;
??? ??? ??? ??? reg = <0>;
??? ??? ??? ??? device_type = "ethernet-phy";
??? ??? ??? };
??? ??? };
linux-2.6.21.7
gfar_mdio_of_init() -->
??? platform_device_register_simple() -->
????????? platform_device_add() -->
?????????????? device_add()????
將mdio設備節(jié)點添加到platform總線上,這樣添加方式(單獨添加某個節(jié)點)
比較麻煩,抽象度不夠,每個節(jié)點都需要自己的添加代碼,不科學
還是of_platform_bus_probe()比較好,統一添加allnodes下的所有節(jié)點
-------------------------------------------------------
static int?__init?gfar_mdio_of_init(void)
{
??? ...????
??? // 獲取device_type = "mdio" && compatible = "gianfar"的設備節(jié)點
??? np = of_find_compatible_node(np, "mdio", "gianfar"));
??? of_address_to_resource(np, 0, &res);
??? // 添加平臺設備res.start = e0024520
??? platform_device_register_simple("fsl-gianfar_mdio", res.start, &res, 1);
??? ...?
}
arch_initcall(gfar_mdio_of_init);
struct platform_device *platform_device_register_simple(char *name,?
?????????????????????????? unsigned int id,
?? ??? ??? ??? ??? ??? ??? struct resource *res, unsigned int num)
{
?? ...
?? // 生成platform_device
?? // platform_device->name = name; "fsl-gianfar_mdio"
?? // platform_device->id = id; e0024520
?? pdev = platform_device_alloc(name, id);
?? ...
?? platform_device_add(pdev);
?? ...
}
int platform_device_add(struct platform_device *pdev)
{
?? ...
?? // 設置pdev->dev.bus_id = fsl-gianfar_mdio.e0024520
?? // bus_id長度限制為BUS_ID_SIZE
?? // #define BUS_ID_SIZE??? KOBJ_NAME_LEN
?? // 如果"%s.%x"字符串的長度超過KOBJ_NAME_LEN,將會造成設備節(jié)點名信息丟失
?? // 信息的丟失有可能導致設備添加失敗,所以需要將KOBJ_NAME_LEN改大(20 --> 64)
?? snprintf(pdev->dev.bus_id, BUS_ID_SIZE, "%s.%x", pdev->name, pdev->id);
?? ...
?? device_add(&pdev->dev);
?? ...
}
int device_add(struct device *dev)
{
?? ...
?? // 設置kobject->name = fsl-gianfar_mdio.e0024520
?? // #define KOBJ_NAME_LEN 20
?? kobject_set_name(&dev->kobj, "%s", dev->bus_id);
?? ...
?? // 在/sys/devices/platform/目錄下生成設備節(jié)點
?? // 設備節(jié)點名稱為fsl-gianfar_mdio.e0024520
?? device_create_file(dev, &dev->uevent_attr);
?? ...
}
設備與驅動的對應關系
-----------------------------------------------------
static struct device_driver gianfar_mdio_driver = {
?? .name = "fsl-gianfar_mdio",
?? .bus = &platform_bus_type, //該驅動所歸屬的總線
?? .probe = gfar_mdio_probe,
?? .remove = gfar_mdio_remove,
};
cat /sys/devices/platform/fsl-gianfar_mdio.e0024520/modalias
fsl-gianfar_mdio
static struct platform_driver gfar_driver = {
?? .probe = gfar_probe,
?? .remove = gfar_remove,
?? .driver??? = {
?? ??? .name = "fsl-gianfar",
?? },
};
cat /sys/devices/platform/fsl-gianfar.0/modalias
fsl-gianfar
?
評論
查看更多