<meter id="pryje"><nav id="pryje"><delect id="pryje"></delect></nav></meter>
          <label id="pryje"></label>

          新聞中心

          EEPW首頁(yè) > 嵌入式系統(tǒng) > 設(shè)計(jì)應(yīng)用 > ARM Linux 3.x的設(shè)備樹(Device Tree)

          ARM Linux 3.x的設(shè)備樹(Device Tree)

          作者: 時(shí)間:2016-11-09 來(lái)源:網(wǎng)絡(luò) 收藏
          1. ARM Device Tree起源
          Linus Torvalds在2011年3月17日的ARM Linux郵件列表宣稱“this whole ARM thing is a f*cking pain in the ass”,引發(fā)ARM Linux社區(qū)的地震,隨后ARM社區(qū)進(jìn)行了一系列的重大修正。在過(guò)去的ARM Linux中,arch/arm/plat-xxx和arch/arm/mach-xxx中充斥著大量的垃圾代碼,相當(dāng)多數(shù)的代碼只是在描述板級(jí)細(xì)節(jié),而這些板級(jí)細(xì)節(jié)對(duì)于內(nèi)核來(lái)講,不過(guò)是垃圾,如板上的platform設(shè)備、resource、i2c_board_info、spi_board_info以及各種硬件的platform_data。讀者有興趣可以統(tǒng)計(jì)下常見的s3c2410、s3c6410等板級(jí)目錄,代碼量在數(shù)萬(wàn)行。
          社區(qū)必須改變這種局面,于是PowerPC等其他體系架構(gòu)下已經(jīng)使用的Flattened Device Tree(FDT)進(jìn)入ARM社區(qū)的視野。Device Tree是一種描述硬件的數(shù)據(jù)結(jié)構(gòu),它起源于 OpenFirmware (OF)。在Linux 2.6中,ARM架構(gòu)的板極硬件細(xì)節(jié)過(guò)多地被硬編碼在arch/arm/plat-xxx和arch/arm/mach-xxx,采用Device Tree后,許多硬件的細(xì)節(jié)可以直接透過(guò)它傳遞給Linux,而不再需要在kernel中進(jìn)行大量的冗余編碼。Device Tree由一系列被命名的結(jié)點(diǎn)(node)和屬性(property)組成,而結(jié)點(diǎn)本身可包含子結(jié)點(diǎn)。所謂屬性,其實(shí)就是成對(duì)出現(xiàn)的name和value。在Device Tree中,可描述的信息包括(原先這些信息大多被hard code到kernel中):
          • CPU的數(shù)量和類別
          • 內(nèi)存基地址和大小
          • 總線和橋
          • 外設(shè)連接
          • 中斷控制器和中斷使用情況
          • GPIO控制器和GPIO使用情況
          • Clock控制器和Clock使用情況
          它基本上就是畫一棵電路板上CPU、總線、設(shè)備組成的樹,Bootloader會(huì)將這棵樹傳遞給內(nèi)核,然后內(nèi)核可以識(shí)別這棵樹,并根據(jù)它展開出Linux內(nèi)核中的platform_device、i2c_client、spi_device等設(shè)備,而這些設(shè)備用到的內(nèi)存、IRQ等資源,也被傳遞給了內(nèi)核,內(nèi)核會(huì)將這些資源綁定給展開的相應(yīng)的設(shè)備。

          view plaincopy

          本文引用地址:http://www.ex-cimer.com/article/201611/317984.htm
          1. /{
          2. node1{
          3. a-string-property="Astring";
          4. a-string-list-property="firststring","secondstring";
          5. a-byte-data-property=[0x010x230x340x56];
          6. child-node1{
          7. first-child-property;
          8. second-child-property=<1>;
          9. a-string-property="Hello,world";
          10. };
          11. child-node2{
          12. };
          13. };
          14. node2{
          15. an-empty-property;
          16. a-cell-property=<1234>;/*eachnumber(cell)isauint32*/
          17. child-node1{
          18. };
          19. };
          20. };
          上述.dts文件并沒(méi)有什么真實(shí)的用途,但它基本表征了一個(gè)Device Tree源文件的結(jié)構(gòu):1個(gè)root結(jié)點(diǎn)"/";
          root結(jié)點(diǎn)下面含一系列子結(jié)點(diǎn),本例中為"node1" 和 "node2";結(jié)點(diǎn)"node1"下又含有一系列子結(jié)點(diǎn),本例中為"child-node1" 和 "child-node2";各結(jié)點(diǎn)都有一系列屬性。這些屬性可能為空,如" an-empty-property";可能為字符串,如"a-string-property";可能為字符串?dāng)?shù)組,如"a-string-list-property";可能為Cells(由u32整數(shù)組成),如"second-child-property",可能為二進(jìn)制數(shù),如"a-byte-data-property"。下面以一個(gè)最簡(jiǎn)單的machine為例來(lái)看如何寫一個(gè).dts文件。假設(shè)此machine的配置如下:

          1個(gè)雙核ARM Cortex-A9 32位處理器;ARM的local bus上的內(nèi)存映射區(qū)域分布了2個(gè)串口(分別位于0x101F1000 和 0x101F2000)、GPIO控制器(位于0x101F3000)、SPI控制器(位于0x10170000)、中斷控制器(位于0x10140000)和一個(gè)external bus橋;External bus橋上又連接了SMC SMC91111 Ethernet(位于0x10100000)、I2C控制器(位于0x10160000)、64MB NOR Flash(位于0x30000000);External bus橋上連接的I2C控制器所對(duì)應(yīng)的I2C總線上又連接了Maxim DS1338實(shí)時(shí)鐘(I2C地址為0x58)。

          其對(duì)應(yīng)的.dts文件為:
          [plain]view plaincopy
          1. /{
          2. compatible="acme,coyotes-revenge";
          3. #address-cells=<1>;
          4. #size-cells=<1>;
          5. interrupt-parent=<&intc>;
          6. cpus{
          7. #address-cells=<1>;
          8. #size-cells=<0>;
          9. cpu@0{
          10. compatible="arm,cortex-a9";
          11. reg=<0>;
          12. };
          13. cpu@1{
          14. compatible="arm,cortex-a9";
          15. reg=<1>;
          16. };
          17. };
          18. serial@101f0000{
          19. compatible="arm,pl011";
          20. reg=<0x101f00000x1000>;
          21. interrupts=<10>;
          22. };
          23. serial@101f2000{
          24. compatible="arm,pl011";
          25. reg=<0x101f20000x1000>;
          26. interrupts=<20>;
          27. };
          28. gpio@101f3000{
          29. compatible="arm,pl061";
          30. reg=<0x101f30000x1000
          31. 0x101f40000x0010>;
          32. interrupts=<30>;
          33. };
          34. intc:interrupt-controller@10140000{
          35. compatible="arm,pl190";
          36. reg=<0x101400000x1000>;
          37. interrupt-controller;
          38. #interrupt-cells=<2>;
          39. };
          40. spi@10115000{
          41. compatible="arm,pl022";
          42. reg=<0x101150000x1000>;
          43. interrupts=<40>;
          44. };
          45. external-bus{
          46. #address-cells=<2>
          47. #size-cells=<1>;
          48. ranges=<000x101000000x10000//Chipselect1,Ethernet
          49. 100x101600000x10000//Chipselect2,i2ccontroller
          50. 200x300000000x1000000>;//Chipselect3,NORFlash
          51. ethernet@0,0{
          52. compatible="smc,smc91c111";
          53. reg=<000x1000>;
          54. interrupts=<52>;
          55. };
          56. i2c@1,0{
          57. compatible="acme,a1234-i2c-bus";
          58. #address-cells=<1>;
          59. #size-cells=<0>;
          60. reg=<100x1000>;
          61. interrupts=<62>;
          62. rtc@58{
          63. compatible="maxim,ds1338";
          64. reg=<58>;
          65. interrupts=<73>;
          66. };
          67. };
          68. flash@2,0{
          69. compatible="samsung,k8f1315ebm","cfi-flash";
          70. reg=<200x4000000>;
          71. };
          72. };
          73. };
          上述.dts文件中,root結(jié)點(diǎn)"/"的compatible 屬性compatible = "acme,coyotes-revenge";定義了系統(tǒng)的名稱,它的組織形式為:,。Linux內(nèi)核透過(guò)root結(jié)點(diǎn)"/"的compatible 屬性即可判斷它啟動(dòng)的是什么machine。
          在.dts文件的每個(gè)設(shè)備,都有一個(gè)compatible 屬性,compatible屬性用戶驅(qū)動(dòng)和設(shè)備的綁定。compatible 屬性是一個(gè)字符串的列表,列表中的第一個(gè)字符串表征了結(jié)點(diǎn)代表的確切設(shè)備,形式為",",其后的字符串表征可兼容的其他設(shè)備??梢哉f(shuō)前面的是特指,后面的則涵蓋更廣的范圍。如在arch/arm/boot/dts/vexpress-v2m.dtsi中的Flash結(jié)點(diǎn):
          [plain]view plaincopy
          1. flash@0,00000000{
          2. compatible="arm,vexpress-flash","cfi-flash";
          3. reg=<00x000000000x04000000>,
          4. <10x000000000x04000000>;
          5. bank-width=<4>;
          6. };
          compatible屬性的第2個(gè)字符串"cfi-flash"明顯比第1個(gè)字符串"arm,vexpress-flash"涵蓋的范圍更廣。再比如,F(xiàn)reescale MPC8349 SoC含一個(gè)串口設(shè)備,它實(shí)現(xiàn)了國(guó)家半導(dǎo)體(National Semiconductor)的ns16550 寄存器接口。則MPC8349串口設(shè)備的compatible屬性為compatible = "fsl,mpc8349-uart", "ns16550"。其中,fsl,mpc8349-uart指代了確切的設(shè)備, ns16550代表該設(shè)備與National Semiconductor 的16550 UART保持了寄存器兼容。
          接下來(lái)root結(jié)點(diǎn)"/"的cpus子結(jié)點(diǎn)下面又包含2個(gè)cpu子結(jié)點(diǎn),描述了此machine上的2個(gè)CPU,并且二者的compatible 屬性為"arm,cortex-a9"。
          注意cpus和cpus的2個(gè)cpu子結(jié)點(diǎn)的命名,它們遵循的組織形式為:[@],<>中的內(nèi)容是必選項(xiàng),[]中的則為可選項(xiàng)。name是一個(gè)ASCII字符串,用于描述結(jié)點(diǎn)對(duì)應(yīng)的設(shè)備類型,如3com Ethernet適配器對(duì)應(yīng)的結(jié)點(diǎn)name宜為ethernet,而不是3com509。如果一個(gè)結(jié)點(diǎn)描述的設(shè)備有地址,則應(yīng)該給出@unit-address。多個(gè)相同類型設(shè)備結(jié)點(diǎn)的name可以一樣,只要unit-address不同即可,如本例中含有cpu@0、cpu@1以及serial@101f0000與serial@101f2000這樣的同名結(jié)點(diǎn)。設(shè)備的unit-address地址也經(jīng)常在其對(duì)應(yīng)結(jié)點(diǎn)的reg屬性中給出。ePAPR標(biāo)準(zhǔn)給出了結(jié)點(diǎn)命名的規(guī)范。
          可尋址的設(shè)備使用如下信息來(lái)在Device Tree中編碼地址信息:
          • reg
          • #address-cells
          • #size-cells
          其中reg的組織形式為reg = ,其中的每一組address length表明了設(shè)備使用的一個(gè)地址范圍。address為1個(gè)或多個(gè)32位的整型(即cell),而length則為cell的列表或者為空(若#size-cells = 0)。address 和 length 字段是可變長(zhǎng)的,父結(jié)點(diǎn)的#address-cells和#size-cells分別決定了子結(jié)點(diǎn)的reg屬性的address和length字段的長(zhǎng)度。在本例中,root結(jié)點(diǎn)的#address-cells = <1>;和#size-cells = <1>;決定了serial、gpio、spi等結(jié)點(diǎn)的address和length字段的長(zhǎng)度分別為1。cpus 結(jié)點(diǎn)的#address-cells = <1>;和#size-cells = <0>;決定了2個(gè)cpu子結(jié)點(diǎn)的address為1,而length為空,于是形成了2個(gè)cpu的reg = <0>;和reg = <1>;。external-bus結(jié)點(diǎn)的#address-cells = <2>和#size-cells = <1>;決定了其下的ethernet、i2c、flash的reg字段形如reg = <0 0 0x1000>;、reg = <1 0 0x1000>;和reg = <2 0 0x4000000>;。其中,address字段長(zhǎng)度為0,開始的第一個(gè)cell(0、1、2)是對(duì)應(yīng)的片選,第2個(gè)cell(0,0,0)是相對(duì)該片選的基地址,第3個(gè)cell(0x1000、0x1000、0x4000000)為length。特別要留意的是i2c結(jié)點(diǎn)中定義的 #address-cells = <1>;和#size-cells = <0>;又作用到了I2C總線上連接的RTC,它的address字段為0x58,是設(shè)備的I2C地址。
          root結(jié)點(diǎn)的子結(jié)點(diǎn)描述的是CPU的視圖,因此root子結(jié)點(diǎn)的address區(qū)域就直接位于CPU的memory區(qū)域。但是,經(jīng)過(guò)總線橋后的address往往需要經(jīng)過(guò)轉(zhuǎn)換才能對(duì)應(yīng)的CPU的memory映射。external-bus的ranges屬性定義了經(jīng)過(guò)external-bus橋后的地址范圍如何映射到CPU的memory區(qū)域。
          [plain]view plaincopy
          1. ranges=<000x101000000x10000//Chipselect1,Ethernet
          2. 100x101600000x10000//Chipselect2,i2ccontroller
          3. 200x300000000x1000000>;//Chipselect3,NORFlash
          ranges是地址轉(zhuǎn)換表,其中的每個(gè)項(xiàng)目是一個(gè)子地址、父地址以及在子地址空間的大小的映射。映射表中的子地址、父地址分別采用子地址空間的#address-cells和父地址空間的#address-cells大小。對(duì)于本例而言,子地址空間的#address-cells為2,父地址空間的#address-cells值為1,因此0 0 0x10100000 0x10000的前2個(gè)cell為external-bus后片選0上偏移0,第3個(gè)cell表示external-bus后片選0上偏移0的地址空間被映射到CPU的0x10100000位置,第4個(gè)cell表示映射的大小為0x10000。ranges的后面2個(gè)項(xiàng)目的含義可以類推。
          Device Tree中還可以中斷連接信息,對(duì)于中斷控制器而言,它提供如下屬性:
          interrupt-controller – 這個(gè)屬性為空,中斷控制器應(yīng)該加上此屬性表明自己的身份;
          #interrupt-cells – 與#address-cells 和 #size-cells相似,它表明連接此中斷控制器的設(shè)備的interrupts屬性的cell大??;
          在整個(gè)Device Tree中,與中斷相關(guān)的屬性還包括:
          interrupt-parent – 設(shè)備結(jié)點(diǎn)透過(guò)它來(lái)指定它所依附的中斷控制器的phandle,當(dāng)結(jié)點(diǎn)沒(méi)有指定interrupt-parent 時(shí),則從父級(jí)結(jié)點(diǎn)繼承。對(duì)于本例而言,root結(jié)點(diǎn)指定了interrupt-parent = <&intc>;其對(duì)應(yīng)于intc:interrupt-controller@10140000,而root結(jié)點(diǎn)的子結(jié)點(diǎn)并未指定interrupt-parent,因此它們都繼承了intc,即位于0x10140000的中斷控制器。
          interrupts – 用到了中斷的設(shè)備結(jié)點(diǎn)透過(guò)它指定中斷號(hào)、觸發(fā)方法等,具體這個(gè)屬性含有多少個(gè)cell,由它依附的中斷控制器結(jié)點(diǎn)的#interrupt-cells屬性決定。而具體每個(gè)cell又是什么含義,一般由驅(qū)動(dòng)的實(shí)現(xiàn)決定,而且也會(huì)在Device Tree的binding文檔中說(shuō)明。譬如,對(duì)于ARM GIC中斷控制器而言,#interrupt-cells為3,它3個(gè)cell的具體含義Documentation/devicetree/bindings/arm/gic.txt就有如下文字說(shuō)明:
          [plain]view plaincopy
          1. 01The1stcellistheinterrupttype;0forSPIinterrupts,1forPPI
          2. 02interrupts.
          3. 03
          4. 04The2ndcellcontainstheinterruptnumberfortheinterrupttype.
          5. 05SPIinterruptsareintherange[0-987].PPIinterruptsareinthe
          6. 06range[0-15].
          7. 07
          8. 08The3rdcellistheflags,encodedasfollows:
          9. 09bits[3:0]triggertypeandlevelflags.
          10. 101=low-to-highedgetriggered
          11. 112=high-to-lowedgetriggered
          12. 124=activehighlevel-sensitive
          13. 138=activelowlevel-sensitive
          14. 14bits[15:8]PPIinterruptcpumask.Eachbitcorrespondstoeachof
          15. 15the8possiblecpusattachedtotheGIC.Abitsetto1indicated
          16. 16theinterruptiswiredtothatCPU.OnlyvalidforPPIinterrupts.
          另外,值得注意的是,一個(gè)設(shè)備還可能用到多個(gè)中斷號(hào)。對(duì)于ARM GIC而言,若某設(shè)備使用了SPI的168、169號(hào)2個(gè)中斷,而言都是高電平觸發(fā),則該設(shè)備結(jié)點(diǎn)的interrupts屬性可定義為:interrupts = <0 168 4>, <0 169 4>;
          除了中斷以外,在ARM Linux中clock、GPIO、pinmux都可以透過(guò).dts中的結(jié)點(diǎn)和屬性進(jìn)行描述。

          DTC (device tree compiler)

          將.dts編譯為.dtb的工具。DTC的源代碼位于內(nèi)核的scripts/dtc目錄,在Linux內(nèi)核使能了Device Tree的情況下,編譯內(nèi)核的時(shí)候主機(jī)工具dtc會(huì)被編譯出來(lái),對(duì)應(yīng)scripts/dtc/Makefile中的“hostprogs-y := dtc”這一hostprogs編譯target。
          在Linux內(nèi)核的arch/arm/boot/dts/Makefile中,描述了當(dāng)某種SoC被選中后,哪些.dtb文件會(huì)被編譯出來(lái),如與VEXPRESS對(duì)應(yīng)的.dtb包括:
          [plain]view plaincopy
          1. dtb-$(CONFIG_ARCH_VEXPRESS)+=vexpress-v2p-ca5s.dtb
          2. vexpress-v2p-ca9.dtb
          3. vexpress-v2p-ca15-tc1.dtb
          4. vexpress-v2p-ca15_a7.dtb
          5. xenvm-4.2.dtb
          在Linux下,我們可以單獨(dú)編譯Device Tree文件。當(dāng)我們?cè)贚inux內(nèi)核下運(yùn)行make dtbs時(shí),若我們之前選擇了ARCH_VEXPRESS,上述.dtb都會(huì)由對(duì)應(yīng)的.dts編譯出來(lái)。因?yàn)閍rch/arm/Makefile中含有一個(gè)dtbs編譯target項(xiàng)目。

          Device Tree Blob (.dtb)

          .dtb是.dts被DTC編譯后的二進(jìn)制格式的Device Tree描述,可由Linux內(nèi)核解析。通常在我們?yōu)殡娐钒逯谱鱊AND、SD啟動(dòng)image時(shí),會(huì)為.dtb文件單獨(dú)留下一個(gè)很小的區(qū)域以存放之,之后bootloader在引導(dǎo)kernel的過(guò)程中,會(huì)先讀取該.dtb到內(nèi)存。

          Binding

          對(duì)于Device Tree中的結(jié)點(diǎn)和屬性具體是如何來(lái)描述設(shè)備的硬件細(xì)節(jié)的,一般需要文檔來(lái)進(jìn)行講解,文檔的后綴名一般為.txt。這些文檔位于內(nèi)核的Documentation/devicetree/bindings目錄,其下又分為很多子目錄。

          Bootloader

          Uboot mainline 從 v1.1.3開始支持Device Tree,其對(duì)ARM的支持則是和ARM內(nèi)核支持Device Tree同期完成。為了使能Device Tree,需要編譯Uboot的時(shí)候在config文件中加入
          #define CONFIG_OF_LIBFDT
          在Uboot中,可以從NAND、SD或者TFTP等任意介質(zhì)將.dtb讀入內(nèi)存,假設(shè).dtb放入的內(nèi)存地址為0x71000000,之后可在Uboot運(yùn)行命令fdt addr命令設(shè)置.dtb的地址,如:
          U-Boot> fdt addr 0x71000000
          fdt的其他命令就變地可以使用,如fdt resize、fdt print等。對(duì)于ARM來(lái)講,可以透過(guò)bootz kernel_addr initrd_address dtb_address的命令來(lái)啟動(dòng)內(nèi)核,即dtb_address作為bootz或者bootm的最后一次參數(shù),第一個(gè)參數(shù)為內(nèi)核映像的地址,第二個(gè)參數(shù)為initrd的地址,若不存在initrd,可以用 -代替。

          3. Device Tree引發(fā)的BSP和驅(qū)動(dòng)變更

          有了Device Tree后,大量的板級(jí)信息都不再需要,譬如過(guò)去經(jīng)常在arch/arm/plat-xxx和arch/arm/mach-xxx實(shí)施的如下事情:
          1. 注冊(cè)platform_device,綁定resource,即內(nèi)存、IRQ等板級(jí)信息。

          透過(guò)Device Tree后,形如

          [cpp]view plaincopy
          1. 90staticstructresourcexxx_resources[]={
          2. 91[0]={
          3. 92.start=…,
          4. 93.end=…,
          5. 94.flags=IORESOURCE_MEM,
          6. 95},
          7. 96[1]={
          8. 97.start=…,
          9. 98.end=…,
          10. 99.flags=IORESOURCE_IRQ,
          11. 100},
          12. 101};
          13. 102
          14. 103staticstructplatform_devicexxx_device={
          15. 104.name="xxx",
          16. 105.id=-1,
          17. 106.dev={
          18. 107.platform_data=&xxx_data,
          19. 108},
          20. 109.resource=xxx_resources,
          21. 110.num_resources=ARRAY_SIZE(xxx_resources),
          22. 111};
          之類的platform_device代碼都不再需要,其中platform_device會(huì)由kernel自動(dòng)展開。而這些resource實(shí)際來(lái)源于.dts中設(shè)備結(jié)點(diǎn)的reg、interrupts屬性。典型地,大多數(shù)總線都與“simple_bus”兼容,而在SoC對(duì)應(yīng)的machine的.init_machine成員函數(shù)中,調(diào)用of_platform_bus_probe(NULL, xxx_of_bus_ids, NULL);即可自動(dòng)展開所有的platform_device。譬如,假設(shè)我們有個(gè)XXX SoC,則可在arch/arm/mach-xxx/的板文件中透過(guò)如下方式展開.dts中的設(shè)備結(jié)點(diǎn)對(duì)應(yīng)的platform_device:

          [cpp]view plaincopy
          1. 18staticstructof_device_idxxx_of_bus_ids[]__initdata={
          2. 19{.compatible="simple-bus",},
          3. 20{},
          4. 21};
          5. 22
          6. 23void__initxxx_mach_init(void)
          7. 24{
          8. 25of_platform_bus_probe(NULL,xxx_of_bus_ids,NULL);
          9. 26}
          10. 32
          11. 33#ifdefCONFIG_ARCH_XXX
          12. 38
          13. 39DT_MACHINE_START(XXX_DT,"GenericXXX(FlattenedDeviceTree)")
          14. 41…
          15. 45.init_machine=xxx_mach_init,
          16. 46…
          17. 49MACHINE_END
          18. 50#endif

          2.注冊(cè)i2c_board_info,指定IRQ等板級(jí)信息。

          形如

          [cpp]view plaincopy
          1. 145staticstructi2c_board_info__initdataafeb9260_i2c_devices[]={
          2. 146{
          3. 147I2C_BOARD_INFO("tlv320aic23",0x1a),
          4. 148},{
          5. 149I2C_BOARD_INFO("fm3130",0x68),
          6. 150},{
          7. 151I2C_BOARD_INFO("24c64",0x50),
          8. 152},
          9. 153};
          之類的i2c_board_info代碼,目前不再需要出現(xiàn),現(xiàn)在只需要把tlv320aic23、fm3130、24c64這些設(shè)備結(jié)點(diǎn)填充作為相應(yīng)的I2C controller結(jié)點(diǎn)的子結(jié)點(diǎn)即可,類似于前面的

          [cpp]view plaincopy
          1. i2c@1,0{
          2. compatible="acme,a1234-i2c-bus";
          3. rtc@58{
          4. compatible="maxim,ds1338";
          5. reg=<58>;
          6. interrupts=<73>;
          7. };
          8. };
          Device Tree中的I2C client會(huì)透過(guò)I2C host驅(qū)動(dòng)的probe()函數(shù)中調(diào)用of_i2c_register_devices(&i2c_dev->adapter);被自動(dòng)展開。

          3. 注冊(cè)spi_board_info,指定IRQ等板級(jí)信息。

          形如

          [cpp]view plaincopy
          1. 79staticstructspi_board_infoafeb9260_spi_devices[]={
          2. 80{/*DataFlashchip*/
          3. 81.modalias="mtd_dataflash",
          4. 82.chip_select=1,
          5. 83.max_speed_hz=15*1000*1000,
          6. 84.bus_num=0,
          7. 85},
          8. 86};
          之類的spi_board_info代碼,目前不再需要出現(xiàn),與I2C類似,現(xiàn)在只需要把mtd_dataflash之類的結(jié)點(diǎn),作為SPI控制器的子結(jié)點(diǎn)即可,SPI host驅(qū)動(dòng)的probe函數(shù)透過(guò)spi_register_master()注冊(cè)master的時(shí)候,會(huì)自動(dòng)展開依附于它的slave。

          4.多個(gè)針對(duì)不同電路板的machine,以及相關(guān)的callback。

          過(guò)去,ARM Linux針對(duì)不同的電路板會(huì)建立由MACHINE_START和MACHINE_END包圍起來(lái)的針對(duì)這個(gè)machine的一系列callback,譬如:

          [cpp]view plaincopy
          1. 373MACHINE_START(VEXPRESS,"ARM-VersatileExpress")
          2. 374.atag_offset=0x100,
          3. 375.smp=smp_ops(vexpress_smp_ops),
          4. 376.map_io=v2m_map_io,
          5. 377.init_early=v2m_init_early,
          6. 378.init_irq=v2m_init_irq,
          7. 379.timer=&v2m_timer,
          8. 380.handle_irq=gic_handle_irq,
          9. 381.init_machine=v2m_init,
          10. 382.restart=vexpress_restart,
          11. 383MACHINE_END
          這些不同的machine會(huì)有不同的MACHINE ID,Uboot在啟動(dòng)Linux內(nèi)核時(shí)會(huì)將MACHINE ID存放在r1寄存器,Linux啟動(dòng)時(shí)會(huì)匹配Bootloader傳遞的MACHINE ID和MACHINE_START聲明的MACHINE ID,然后執(zhí)行相應(yīng)machine的一系列初始化函數(shù)。

          引入Device Tree之后,MACHINE_START變更為DT_MACHINE_START,其中含有一個(gè).dt_compat成員,用于表明相關(guān)的machine與.dts中root結(jié)點(diǎn)的compatible屬性兼容關(guān)系。如果Bootloader傳遞給內(nèi)核的Device Tree中root結(jié)點(diǎn)的compatible屬性出現(xiàn)在某machine的.dt_compat表中,相關(guān)的machine就與對(duì)應(yīng)的Device Tree匹配,從而引發(fā)這一machine的一系列初始化函數(shù)被執(zhí)行。

          [cpp]view plaincopy
          1. 489staticconstchar*constv2m_dt_match[]__initconst={
          2. 490"arm,vexpress",
          3. 491"xen,xenvm",
          4. 492NULL,
          5. 493};
          6. 495DT_MACHINE_START(VEXPRESS_DT,"ARM-VersatileExpress")
          7. 496.dt_compat=v2m_dt_match,
          8. 497.smp=smp_ops(vexpress_smp_ops),
          9. 498.map_io=v2m_dt_map_io,
          10. 499.init_early=v2m_dt_init_early,
          11. 500.init_irq=v2m_dt_init_irq,
          12. 501.timer=&v2m_dt_timer,
          13. 502.init_machine=v2m_dt_init,
          14. 503.handle_irq=gic_handle_irq,
          15. 504.restart=vexpress_restart,
          16. 505MACHINE_END
          Linux倡導(dǎo)針對(duì)多個(gè)SoC、多個(gè)電路板的通用DT machine,即一個(gè)DT machine的.dt_compat表含多個(gè)電路板.dts文件的root結(jié)點(diǎn)compatible屬性字符串。之后,如果的電路板的初始化序列不一樣,可以透過(guò)int of_machine_is_compatible(const char *compat) API判斷具體的電路板是什么。

          譬如arch/arm/mach-exynos/mach-exynos5-dt.c的EXYNOS5_DT machine同時(shí)兼容"samsung,exynos5250"和"samsung,exynos5440":

          [cpp]view plaincopy
          1. 158staticcharconst*exynos5_dt_compat[]__initdata={
          2. 159"samsung,exynos5250",
          3. 160"samsung,exynos5440",
          4. 161NULL
          5. 162};
          6. 163
          7. 177DT_MACHINE_START(EXYNOS5_DT,"SAMSUNGEXYNOS5(FlattenedDeviceTree)")
          8. 178/*Maintainer:KukjinKim*/
          9. 179.init_irq=exynos5_init_irq,
          10. 180.smp=smp_ops(exynos_smp_ops),
          11. 181.map_io=exynos5_dt_map_io,
          12. 182.handle_irq=gic_handle_irq,
          13. 183.init_machine=exynos5_dt_machine_init,
          14. 184.init_late=exynos_init_late,
          15. 185.timer=&exynos4_timer,
          16. 186.dt_compat=exynos5_dt_compat,
          17. 187.restart=exynos5_restart,
          18. 188.reserve=exynos5_reserve,
          19. 189MACHINE_END

          它的.init_machine成員函數(shù)就針對(duì)不同的machine進(jìn)行了不同的分支處理:

          [cpp]view plaincopy
          1. 126staticvoid__initexynos5_dt_machine_init(void)
          2. 127{
          3. 128…
          4. 149
          5. 150if(of_machine_is_compatible("samsung,exynos5250"))
          6. 151of_platform_populate(NULL,of_default_bus_match_table,
          7. 152exynos5250_auxdata_lookup,NULL);
          8. 153elseif(of_machine_is_compatible("samsung,exynos5440"))
          9. 154of_platform_populate(NULL,of_default_bus_match_table,
          10. 155exynos5440_auxdata_lookup,NULL);
          11. 156}

          使用Device Tree后,驅(qū)動(dòng)需要與.dts中描述的設(shè)備結(jié)點(diǎn)進(jìn)行匹配,從而引發(fā)驅(qū)動(dòng)的probe()函數(shù)執(zhí)行。對(duì)于platform_driver而言,需要添加一個(gè)OF匹配表,如前文的.dts文件的"acme,a1234-i2c-bus"兼容I2C控制器結(jié)點(diǎn)的OF匹配表可以是:

          [cpp]view plaincopy
          1. 436staticconststructof_device_ida1234_i2c_of_match[]={
          2. 437{.compatible="acme,a1234-i2c-bus",},
          3. 438{},
          4. 439};
          5. 440MODULE_DEVICE_TABLE(of,a1234_i2c_of_match);
          6. 441
          7. 442staticstructplatform_driveri2c_a1234_driver={
          8. 443.driver={
          9. 444.name="a1234-i2c-bus",
          10. 445.owner=THIS_MODULE,
          11. 449.of_match_table=a1234_i2c_of_match,
          12. 450},
          13. 451.probe=i2c_a1234_probe,
          14. 452.remove=i2c_a1234_remove,
          15. 453};
          16. 454module_platform_driver(i2c_a1234_driver);

          對(duì)于I2C和SPI從設(shè)備而言,同樣也可以透過(guò)of_match_table添加匹配的.dts中的相關(guān)結(jié)點(diǎn)的compatible屬性,如sound/soc/codecs/wm8753.c中的:

          [cpp]view plaincopy
          1. 1533staticconststructof_device_idwm8753_of_match[]={
          2. 1534{.compatible="wlf,wm8753",},
          3. 1535{}
          4. 1536};
          5. 1537MODULE_DEVICE_TABLE(of,wm8753_of_match);
          6. 1587staticstructspi_driverwm8753_spi_driver={
          7. 1588.driver={
          8. 1589.name="wm8753",
          9. 1590.owner=THIS_MODULE,
          10. 1591.of_match_table=wm8753_of_match,
          11. 1592},
          12. 1593.probe=wm8753_spi_probe,
          13. 1594.remove=wm8753_spi_remove,
          14. 1595};
          15. 1640staticstructi2c_driverwm8753_i2c_driver={
          16. 1641.driver={
          17. 1642.name="wm8753",
          18. 1643.owner=THIS_MODULE,
          19. 1644.of_match_table=wm8753_of_match,
          20. 1645},
          21. 1646.probe=wm8753_i2c_probe,
          22. 1647.remove=wm8753_i2c_remove,
          23. 1648.id_table=wm8753_i2c_id,
          24. 1649};
          不過(guò)這邊有一點(diǎn)需要提醒的是,I2C和SPI外設(shè)驅(qū)動(dòng)和Device Tree中設(shè)備結(jié)點(diǎn)的compatible 屬性還有一種弱式匹配方法,就是別名匹配。compatible 屬性的組織形式為,,別名其實(shí)就是去掉compatible 屬性中逗號(hào)前的manufacturer前綴。關(guān)于這一點(diǎn),可查看drivers/spi/spi.c的源代碼,函數(shù)spi_match_device()暴露了更多的細(xì)節(jié),如果別名出現(xiàn)在設(shè)備spi_driver的id_table里面,或者別名與spi_driver的name字段相同,SPI設(shè)備和驅(qū)動(dòng)都可以匹配上:

          [cpp]view plaincopy
          1. 90staticintspi_match_device(structdevice*dev,structdevice_driver*drv)
          2. 91{
          3. 92conststructspi_device*spi=to_spi_device(dev);
          4. 93conststructspi_driver*sdrv=to_spi_driver(drv);
          5. 94
          6. 95/*AttemptanOFstylematch*/
          7. 96if(of_driver_match_device(dev,drv))
          8. 97return1;
          9. 98
          10. 99/*ThentryACPI*/
          11. 100if(acpi_driver_match_device(dev,drv))
          12. 101return1;
          13. 102
          14. 103if(sdrv->id_table)
          15. 104return!!spi_match_id(sdrv->id_table,spi);
          16. 105
          17. 106returnstrcmp(spi->modalias,drv->name)==0;
          18. 107}
          19. 71staticconststructspi_device_id*spi_match_id(conststructspi_device_id*id,
          20. 72conststructspi_device*sdev)
          21. 73{
          22. 74while(id->name[0]){
          23. 75if(!strcmp(sdev->modalias,id->name))
          24. 76returnid;
          25. 77id++;
          26. 78}
          27. 79returnNULL;
          28. 80}



          關(guān)鍵詞: ARMLinux設(shè)備

          評(píng)論


          技術(shù)專區(qū)

          關(guān)閉
          看屁屁www成人影院,亚洲人妻成人图片,亚洲精品成人午夜在线,日韩在线 欧美成人 (function(){ var bp = document.createElement('script'); var curProtocol = window.location.protocol.split(':')[0]; if (curProtocol === 'https') { bp.src = 'https://zz.bdstatic.com/linksubmit/push.js'; } else { bp.src = 'http://push.zhanzhang.baidu.com/push.js'; } var s = document.getElementsByTagName("script")[0]; s.parentNode.insertBefore(bp, s); })();