這篇文章主要講解了“Linux內(nèi)核Device Tree怎么創(chuàng)建”,文中的講解內(nèi)容簡(jiǎn)單清晰,易于學(xué)習(xí)與理解,下面請(qǐng)大家跟著小編的思路慢慢深入,一起來研究和學(xué)習(xí)“Linux內(nèi)核Device Tree怎么創(chuàng)建”吧!
公司主營(yíng)業(yè)務(wù):網(wǎng)站制作、成都網(wǎng)站制作、移動(dòng)網(wǎng)站開發(fā)等業(yè)務(wù)。幫助企業(yè)客戶真正實(shí)現(xiàn)互聯(lián)網(wǎng)宣傳,提高企業(yè)的競(jìng)爭(zhēng)能力。創(chuàng)新互聯(lián)建站是一支青春激揚(yáng)、勤奮敬業(yè)、活力青春激揚(yáng)、勤奮敬業(yè)、活力澎湃、和諧高效的團(tuán)隊(duì)。公司秉承以“開放、自由、嚴(yán)謹(jǐn)、自律”為核心的企業(yè)文化,感謝他們對(duì)我們的高要求,感謝他們從不同領(lǐng)域給我們帶來的挑戰(zhàn),讓我們激情的團(tuán)隊(duì)有機(jī)會(huì)用頭腦與智慧不斷的給客戶帶來驚喜。創(chuàng)新互聯(lián)建站推出臺(tái)江免費(fèi)做網(wǎng)站回饋大家。
在Linux
內(nèi)核啟動(dòng)時(shí),內(nèi)核通過of_platform_populate()
函數(shù),將dts
中的device node
創(chuàng)建成platform device
。為后續(xù)和各類驅(qū)動(dòng)的platform driver
匹配做準(zhǔn)備。
of_platform_populate()
函數(shù)在文件drivers/of/platform.c
中實(shí)現(xiàn)。下面基于RockPI 4A單板的內(nèi)核代碼介紹其調(diào)用流程和實(shí)現(xiàn)過程。
在Linux
內(nèi)核中,可以使用dump_stack()
函數(shù)查看函數(shù)的調(diào)用流程。
/** * of_platform_populate() - Populate platform_devices from device tree data... #省略部分注釋 */int of_platform_populate(struct device_node *root, const struct of_device_id *matches, const struct of_dev_auxdata *lookup, struct device *parent){ struct device_node *child; int rc = 0; dump_stack(); ### 打印函數(shù)調(diào)用的堆棧信息 //1.如果root為NULL,則通過of_find_node_by_path()查找 root = root ? of_node_get(root) : of_find_node_by_path("/"); if (!root) return -EINVAL; //2.遍歷dts中的節(jié)點(diǎn) for_each_child_of_node(root, child) { //3.為每個(gè)節(jié)點(diǎn)和子節(jié)點(diǎn)創(chuàng)建platform device rc = of_platform_bus_create(child, matches, lookup, parent, true); ... } ...}EXPORT_SYMBOL_GPL(of_platform_populate);
dump_stack()
堆棧信息如下:
[ 0.311191] CPU: 0 PID: 1 Comm: swapper/0 Not tainted 4.4.154-00036-gcef30e88a9f5-dirty #36[ 0.311198] Hardware name: ROCK PI 4A 2 (DT)[ 0.311206] Call trace:[ 0.311220] [] dump_backtrace+0x0/0x220[ 0.311232] [ ] show_stack+0x24/0x30[ 0.311244] [ ] dump_stack+0x98/0xc0[ 0.311258] [ ] of_platform_populate+0x30/0xb8[ 0.311268] [ ] arm64_device_init+0x30/0x4c[ 0.311278] [ ] do_one_initcall+0x18c/0x194[ 0.311290] [ ] kernel_init_freeable+0x228/0x22c[ 0.311301] [ ] kernel_init+0x18/0x100[ 0.311311] [ ] ret_from_fork+0x10/0x20
從堆棧信息中,可以看出:在arm64_device_init()
函數(shù)中實(shí)現(xiàn)了of_platform_populate()
函數(shù)的調(diào)用。后續(xù)介紹kernel_init()
函數(shù),暫時(shí)先留個(gè)念想。
注:
arm64_device_init()
函數(shù)在arch/arm64/kernel/setup.c
文件中實(shí)現(xiàn)。此時(shí),串口驅(qū)動(dòng)尚未加載,串口日志保存在緩沖區(qū)中。由于RK3399
是多核,在Linux
內(nèi)核啟動(dòng)時(shí),堆棧信息或其它日志有可能會(huì)丟失。在系統(tǒng)啟動(dòng)時(shí),可以增加nosmp
配置,關(guān)閉其他CPU
的加載,保證盡可能多的日志輸出。在配置文件/boot/extlinux/extlinux.conf
最后增加:
label kernel-debug kernel /debug/Image fdt /debug/rk3399-rock-pi-4a.dtb append earlyprintk console=ttyFIQ0,1500000n8 init=/sbin/init root=PARTUUID=b921b045-1d rw rootwait rootfstype=ext4 nosmp
of_platform_populate()
函數(shù)主要通過of_platform_bus_create()
函數(shù)創(chuàng)建platform device
。為了理解其實(shí)現(xiàn)過程,通過printk
增加了部分調(diào)試日志,代碼如下:
/** * of_platform_bus_create() - Create a device for a node and its children. * @bus: device node of the bus to instantiate * @matches: match table for bus nodes * @lookup: auxdata table for matching id and platform_data with device nodes * @parent: parent for new device, or NULL for top level. * @strict: require compatible property * * Creates a platform_device for the provided device_node, and optionally * recursively create devices for all the child nodes. */static int of_platform_bus_create(struct device_node *bus, const struct of_device_id *matches, const struct of_dev_auxdata *lookup, struct device *parent, bool strict){ ... printk(KERN_ERR"--- name %s \n",bus->name); //1.判斷是否有compatible屬性,沒有則返回 /* Make sure it has a compatible property */ if (strict && (!of_get_property(bus, "compatible", NULL))) { printk(KERN_ERR"--- %s() - skipping %s, no compatible prop\n", __func__, bus->full_name); return 0; } ... //2.創(chuàng)建platform device dev = of_platform_device_create_pdata(bus, bus_id, platform_data, parent); if (!dev || !of_match_node(matches, bus)) { printk(KERN_ERR"--- no match node\n"); return 0; } //3.遍歷子節(jié)點(diǎn)。如果存在,則創(chuàng)建platform device for_each_child_of_node(bus, child) { printk(KERN_ERR"--- create child: %s\n", child->full_name); rc = of_platform_bus_create(child, matches, lookup, &dev->dev, strict); if (rc) { of_node_put(child); break; } } of_node_set_flag(bus, OF_POPULATED_BUS); return rc;}
更新內(nèi)核映像后,截取了部分內(nèi)核啟動(dòng)日志,如下:
[ 0.326151] --- name syscon[ 0.326311] --- create child: /syscon@ff770000/io-domains[ 0.326318] --- name io-domains[ 0.326458] --- no match node[ 0.326466] --- create child: /syscon@ff770000/usb2-phy@e450[ 0.326472] --- name usb2-phy[ 0.326627] --- no match node[ 0.326635] --- create child: /syscon@ff770000/usb2-phy@e460[ 0.326641] --- name usb2-phy[ 0.326791] --- no match node[ 0.326798] --- create child: /syscon@ff770000/phy@f780[ 0.326804] --- name phy[ 0.326958] --- no match node[ 0.326965] --- create child: /syscon@ff770000/mipi-dphy-rx0[ 0.326972] --- name mipi-dphy-rx0[ 0.327113] --- no match node[ 0.327120] --- create child: /syscon@ff770000/pvtm[ 0.327126] --- name pvtm[ 0.327291] --- no match node... ## 省略部分log[ 0.330604] --- name display-subsystem ## drm[ 0.330742] --- no match node...
上述日志中的節(jié)點(diǎn)名稱bus->name
和子節(jié)點(diǎn)名稱child->full_name
可在arch/arm64/boot/dts/rockchip/rk3399.dtsi
文件中查到:
grf: syscon@ff770000 { ## syscon對(duì)應(yīng)節(jié)點(diǎn)名 compatible = "rockchip,rk3399-grf", "syscon", "simple-mfd"; reg = <0x0 0xff770000 0x0 0x10000>; #address-cells = <1>; #size-cells = <1>; io_domains: io-domains { compatible = "rockchip,rk3399-io-voltage-domain"; status = "disabled"; }; u2phy0: usb2-phy@e450 { ## usb2-phy@e450對(duì)應(yīng)子節(jié)點(diǎn)名 compatible = "rockchip,rk3399-usb2phy"; reg = <0xe450 0x10>; clocks = <&cru SCLK_USB2PHY0_REF>; clock-names = "phyclk"; #clock-cells = <0>; clock-output-names = "clk_usbphy0_480m"; status = "disabled"; ... } } ... display_subsystem: display-subsystem { ## display-subsystem 對(duì)應(yīng)節(jié)點(diǎn)名 compatible = "rockchip,display-subsystem"; ports = <&vopl_out>, <&vopb_out>; clocks = <&cru PLL_VPLL>, <&cru PLL_CPLL>; clock-names = "hdmi-tmds-pll", "default-vop-pll"; devfreq = <&dmc>; status = "disabled"; };
在系統(tǒng)啟動(dòng)后,可以在/sys/firmware/devicetree/base
路徑下查看dts
文件節(jié)點(diǎn),在/sys/devices/platform
路徑下查看platform device
。
root@linaro-alip:/sys/firmware/devicetree/base# ls syscon@ff770000/#address-cells compatible mipi-dphy-rx0 phandle pvtm usb2-phy@e450#size-cells io-domains name phy@f780 reg usb2-phy@e460root@linaro-alip:/sys/firmware/devicetree/base# ls display-subsystem/clock-names compatible logo-memory-region phandle routeclocks devfreq name ports statusroot@linaro-alip:/sys/firmware/devicetree/base#
root@linaro-alip:/sys/devices/platform# ls ff770000.syscon/driver_override ff770000.syscon:usb2-phy@e460/ff770000.syscon:io-domains/ modaliasff770000.syscon:mipi-dphy-rx0/ of_node/ff770000.syscon:phy@f780/ power/ff770000.syscon:pvtm/ subsystem/ff770000.syscon:usb2-phy@e450/ ueventroot@linaro-alip:/sys/devices/platform# ls display-subsystem/driver drm modalias power ueventdriver_override graphics of_node subsyste
感謝各位的閱讀,以上就是“Linux內(nèi)核Device Tree怎么創(chuàng)建”的內(nèi)容了,經(jīng)過本文的學(xué)習(xí)后,相信大家對(duì)Linux內(nèi)核Device Tree怎么創(chuàng)建這一問題有了更深刻的體會(huì),具體使用情況還需要大家實(shí)踐驗(yàn)證。這里是創(chuàng)新互聯(lián),小編將為大家推送更多相關(guān)知識(shí)點(diǎn)的文章,歡迎關(guān)注!