树莓派4b设备树理解

(因为没有找到对树莓派设备树详解的,所以下面完全是由自己跟据别人写的有关设备树解释来写的设备树详解,有错的或者漏的话,敬请看到的同学指出并告诉我,可以私信我,感谢!!!)

这里推荐我学习设备树的相关链接,讲解的真的很不错:

1、Linux设备树1-6
2、设备树语法详解
3、树莓派设备树官网

均以bcm2711-rpi-4-b.dts文件为例:

1、第一行不能少代表的是设备树版本
#include".dtsi"可以理解为C语言中的引用的头文件

  1 /dts-v1/;
  2
  3 #include "bcm2711.dtsi"
  4 #include "bcm2711-rpi.dtsi"
  5 #include "bcm283x-rpi-csi1-2lane.dtsi"

2、一个最简单的设备树必须包含根节点,cpus节点,memory节点。根节点的名字及全路径都是“/”,至少需要包含model和compatible两个属性。model属性是用来描述产品型号的,类型为字符串,推荐的格式为“manufacturer,model-number”(非强制的)。根节点的model属性描述的是板子的型号或者芯片平台的型号

“/”表示根节点,根节点表示的是整个板子或者芯片平台。
在每个.dsti和.dts中都会存在一个“/”根节点,编译器DTC在对.dts进行编译生成dtb时,会对node 进行合并操作,最终生成的dtb只有一个root node。

(即.dts和引用的.dtsi文件中的’/'根节点是同一个)

"compatible ="“model = "是根节点下面的属性,用来描述此设备树文件。
从软件的层面讲model属性仅仅表示一个名字而已,没有更多的作用。compatible属性则不同,该属性决定软件如何匹配硬件对硬件进行初始化,Linux 内核会通过根节点的 compoatible 属性查看是否支持此设备,如果支持的话设备就会启动 Linux 内核

chosen节点描述的不是一个真实设备,而是用于firmware传递一些数据给OS,比如bootloader传递内核启动参数给内核**(在linux 内核中会将u-boot中的bootargs参数添加到此节点下作为其子节点)**

  7 / {
  8     compatible = "raspberrypi,4-model-b", "brcm,bcm2711";
  9     model = "Raspberry Pi 4 Model B";    
 10
 11     memory@0 {                  
 12         device_type = "memory"; 
 13         reg = <0x0 0x0 0x0>;
 14     };
 15
 16     chosen {   
 17         bootargs = "coherent_pool=1M 8250.nr_uarts=1 cma=64M";
 18     };

3、很明显,aliases指的意思是别名

标签

就像C中的标签为代码中的位置提供名称一样,DT标签也为层次结构中的节点分配名称。编译器获取对标签的引用,并将其在字符串上下文(&node)中使用时转换为路径,而在整数上下文(<&node>)中使用处理。原始标签不会出现在编译输出中。请注意,标签不包含任何结构。它们只是一个统一的全局命名空间中的令牌。

别名

别名与标签类似,不同的是别名确实以索引形式出现在FDT输出中。它们存储为/aliases节点的属性,每个属性将别名映射到路径字符串。尽管别名节点出现在源中,但路径字符串通常以对标签(&node)的引用的形式出现,而不是全部写出来。DT API会解析到节点的路径字符串,通常会查看路径的第一个字符,将不以斜杠开头的路径作为别名,必须先使用/aliases表将其转换为路径。

aliases {
 21         serial0 = &uart1;
 22         serial1 = &uart0;
 23         mmc0 = &emmc2;
 24         mmc1 = &mmcnr;
 25         mmc2 = &sdhost;
 26         i2c3 = &i2c3;
 27         i2c4 = &i2c4;
 28         i2c5 = &i2c5;
 29         i2c6 = &i2c6;
 30         /delete-property/ ethernet;
 31         /delete-property/ intc;
 32         ethernet0 = &genet;
 33     };

(aliases和chosen属于与内核相关的节点,一般我们不需要去管)

  1 /dts-v1/;
  2
  3 #include "bcm2711.dtsi"
  4 #include "bcm2711-rpi.dtsi"
  5 #include "bcm283x-rpi-csi1-2lane.dtsi"
  6
  7 / {        /*在每个.dsti和.dts中都会存在一个“/”根节点,编译器DTC在对.dts进行编译生成dtb时,会对node                   进行合并操作,最终生成的dtb只有一个root node。*/
  8     compatible = "raspberrypi,4-model-b", "brcm,bcm2711";
  9     model = "Raspberry Pi 4 Model B";     //根节点下的描述公司与型号
 10
 11     memory@0 {                  
 12         device_type = "memory"; 
 13         reg = <0x0 0x0 0x0>;
 14     };
 15
 16     chosen {   //chosen节点不描述一个真实设备,而是用于firmware传递一些数据给OS,比如bootloader传递内核启动参数给内核
 17         bootargs = "coherent_pool=1M 8250.nr_uarts=1 cma=64M";
 18     };
 19
 20     aliases {
 21         serial0 = &uart1;
 22         serial1 = &uart0;
 23         mmc0 = &emmc2;
 24         mmc1 = &mmcnr;
 25         mmc2 = &sdhost;
 26         i2c3 = &i2c3;
 27         i2c4 = &i2c4;
 28         i2c5 = &i2c5;
 29         i2c6 = &i2c6;
 30         /delete-property/ ethernet;
 31         /delete-property/ intc;
 32         ethernet0 = &genet;
 33     };
 34 };
 35
 36 &soc {  //&可以理解为追加,&label表示在label对应的节点中追加属性
 37     virtgpio: virtgpio {
 38         compatible = "brcm,bcm2835-virtgpio";
 39         gpio-controller;
 40         #gpio-cells = <2>;
 41         firmware = <&firmware>;
 42         status = "okay";
 43     };
 44 };  //根节点下结束
 45
 46 &mmcnr {  //在标签为mmcnr对应的节点下追加内容
 47     pinctrl-names = "default";
 48     pinctrl-0 = <&sdio_pins>;
 49     bus-width = <4>;
 50     status = "okay";
 51 };
 52
 53 &firmware { //在标签为firmware 对应的节点下追加内容
 54     expgpio: gpio {
 55         compatible = "raspberrypi,firmware-gpio";
 56         gpio-controller;
 57         #gpio-cells = <2>;
 58         gpio-line-names = "BT_ON",
 59                   "WL_ON",
 60                   "PWR_LED_OFF",
 61                   "GLOBAL_RESET",
 62                   "VDD_SD_IO_SEL",
 63                   "CAM_GPIO",
 64                   "",
 65                   "";
 66         status = "okay";
 67     };
 68 };
 69
 70 &uart0 {
 71     pinctrl-names = "default";
 72     pinctrl-0 = <&uart0_pins &bt_pins>;
 73     status = "okay";
 74 };
 75
 76 &uart1 {
 77     pinctrl-names = "default";
 78     pinctrl-0 = <&uart1_pins>;
 79     status = "okay";
 80 };
 81
 82 &spi0 {
 83     pinctrl-names = "default";
 84     pinctrl-0 = <&spi0_pins &spi0_cs_pins>;
 85     cs-gpios = <&gpio 8 1>, <&gpio 7 1>;
 86
 87     spidev0: spidev@0{
 88         compatible = "spidev";
 89         reg = <0>;  /* CE0 */
 90         #address-cells = <1>;
 91         #size-cells = <0>;
 92         spi-max-frequency = <125000000>;
 93     };
 94
 95     spidev1: spidev@1{
 96         compatible = "spidev";
 97         reg = <1>;  /* CE1 */
 98         #address-cells = <1>;
 99         #size-cells = <0>;
100         spi-max-frequency = <125000000>;
101     };
102 };
103
104 // =============================================
105 // Board specific stuff here
106
107 / {
108
109     sd_io_1v8_reg: sd_io_1v8_reg {
110         status = "okay";
111         compatible = "regulator-gpio";
112         vin-supply = <&vdd_5v0_reg>;
113         regulator-name = "vdd-sd-io";
114         regulator-min-microvolt = <1800000>;
115         regulator-max-microvolt = <3300000>;
116         regulator-boot-on;
117         regulator-always-on;
118         regulator-settling-time-us = <5000>;
119
120         gpios = <&expgpio 4 GPIO_ACTIVE_HIGH>;
121         states = <1800000 0x1
122               3300000 0x0>;
123     };
124 };
125
126 &sdhost {
127     status = "disabled";
128 };
129
130 &emmc2 {
131     status = "okay";
132     broken-cd;
133     vqmmc-supply = <&sd_io_1v8_reg>;
134 };
135
136 &leds {
137     act_led: act {
138         label = "led0";
139         linux,default-trigger = "mmc0";
140         gpios = <&gpio 42 GPIO_ACTIVE_HIGH>;
141     };
142
143     pwr_led: pwr {
144         label = "led1";
145         linux,default-trigger = "default-on";
146         gpios = <&expgpio 2 GPIO_ACTIVE_LOW>;
147     };
148 };
149
150 &audio {
151     pinctrl-names = "default";
152     pinctrl-0 = <&audio_pins>;
153 };
154
155 &sdhost_gpio48 {
156     brcm,pins = <22 23 24 25 26 27>;
157     brcm,function = <BCM2835_FSEL_ALT0>;
158 };
159
160 &gpio {
161     spi0_pins: spi0_pins {
162         brcm,pins = <9 10 11>;
163         brcm,function = <BCM2835_FSEL_ALT0>;
164     };
165
166     spi0_cs_pins: spi0_cs_pins {
167         brcm,pins = <8 7>;
168         brcm,function = <BCM2835_FSEL_GPIO_OUT>;
169     };
170
171     spi3_pins: spi3_pins {
172         brcm,pins = <1 2 3>;
173         brcm,function = <BCM2835_FSEL_ALT3>;
174     };
175
176     spi3_cs_pins: spi3_cs_pins {
177         brcm,pins = <0 24>;
178         brcm,function = <BCM2835_FSEL_GPIO_OUT>;
179     };
180
181     spi4_pins: spi4_pins {
182         brcm,pins = <5 6 7>;
183         brcm,function = <BCM2835_FSEL_ALT3>;
184     };
185
186     spi4_cs_pins: spi4_cs_pins {
187         brcm,pins = <4 25>;
188         brcm,function = <BCM2835_FSEL_GPIO_OUT>;
189     };
190
191     spi5_pins: spi5_pins {
192         brcm,pins = <13 14 15>;
193         brcm,function = <BCM2835_FSEL_ALT3>;
194     };
195
196     spi5_cs_pins: spi5_cs_pins {
197         brcm,pins = <12 26>;
198         brcm,function = <BCM2835_FSEL_GPIO_OUT>;
199     };
200
201     spi6_pins: spi6_pins {
202         brcm,pins = <19 20 21>;
203         brcm,function = <BCM2835_FSEL_ALT3>;
204     };
205
206     spi6_cs_pins: spi6_cs_pins {
207         brcm,pins = <18 27>;
208         brcm,function = <BCM2835_FSEL_GPIO_OUT>;
209     };
210
211     i2c0_pins: i2c0 {
212         brcm,pins = <0 1>;
213         brcm,function = <BCM2835_FSEL_ALT0>;
214         brcm,pull = <BCM2835_PUD_UP>;
215     };
216
217     i2c1_pins: i2c1 {
218         brcm,pins = <2 3>;
219         brcm,function = <BCM2835_FSEL_ALT0>;
220         brcm,pull = <BCM2835_PUD_UP>;
221     };
222
223     i2c3_pins: i2c3 {
224         brcm,pins = <4 5>;
225         brcm,function = <BCM2835_FSEL_ALT5>;
226         brcm,pull = <BCM2835_PUD_UP>;
227     };
228
229     i2c4_pins: i2c4 {
230         brcm,pins = <8 9>;
231         brcm,function = <BCM2835_FSEL_ALT5>;
232         brcm,pull = <BCM2835_PUD_UP>;
233     };
234
235     i2c5_pins: i2c5 {
236         brcm,pins = <12 13>;
237         brcm,function = <BCM2835_FSEL_ALT5>;
238         brcm,pull = <BCM2835_PUD_UP>;
239     };
240
241     i2c6_pins: i2c6 {
242         brcm,pins = <22 23>;
243         brcm,function = <BCM2835_FSEL_ALT5>;
244         brcm,pull = <BCM2835_PUD_UP>;
245     };
246
247     i2s_pins: i2s {
248         brcm,pins = <18 19 20 21>;
249         brcm,function = <BCM2835_FSEL_ALT0>;
250     };
251
252     sdio_pins: sdio_pins {
253         brcm,pins =     <34 35 36 37 38 39>;
254         brcm,function = <BCM2835_FSEL_ALT3>; // alt3 = SD1
255         brcm,pull =     <0 2 2 2 2 2>;
256     };
257
258     bt_pins: bt_pins {
259         brcm,pins = "-"; // non-empty to keep btuart happy, //4 = 0
260                  // to fool pinctrl
261         brcm,function = <0>;
262         brcm,pull = <2>;
263     };
264
265     uart0_pins: uart0_pins {
266         brcm,pins = <32 33>;
267         brcm,function = <BCM2835_FSEL_ALT3>;
268         brcm,pull = <0 2>;
269     };
270
271     uart1_pins: uart1_pins {
272         brcm,pins;
273         brcm,function;
274         brcm,pull;
275     };
276
277     uart2_pins: uart2_pins {
278         brcm,pins = <0 1>;
279         brcm,function = <BCM2835_FSEL_ALT4>;
280         brcm,pull = <0 2>;
281     };
282
283     uart3_pins: uart3_pins {
284         brcm,pins = <4 5>;
285         brcm,function = <BCM2835_FSEL_ALT4>;
286         brcm,pull = <0 2>;
287     };
288
289     uart4_pins: uart4_pins {
290         brcm,pins = <8 9>;
291         brcm,function = <BCM2835_FSEL_ALT4>;
292         brcm,pull = <0 2>;
293     };
294
295     uart5_pins: uart5_pins {
296         brcm,pins = <12 13>;
297         brcm,function = <BCM2835_FSEL_ALT4>;
298         brcm,pull = <0 2>;
299     };
300
301     audio_pins: audio_pins {
302         brcm,pins = <40 41>;
303         brcm,function = <4>;
303         brcm,function = <4>;
304     };
305 };
306
307 &i2c0 {
308     pinctrl-names = "default";
309     pinctrl-0 = <&i2c0_pins>;
310     clock-frequency = <100000>;
311 };
312
313 &i2c1 {
314     pinctrl-names = "default";
315     pinctrl-0 = <&i2c1_pins>;
316     clock-frequency = <100000>;
317 };
318
319 &i2c2 {
320     clock-frequency = <100000>;
321 };
322
323 &i2s {
324     pinctrl-names = "default";
325     pinctrl-0 = <&i2s_pins>;
326 };
327
328 / {
329     __overrides__ {
330         act_led_gpio = <&act_led>,"gpios:4";
331         act_led_activelow = <&act_led>,"gpios:8";
332         act_led_trigger = <&act_led>,"linux,default-trigger";
333
334         pwr_led_gpio = <&pwr_led>,"gpios:4";
335         pwr_led_activelow = <&pwr_led>,"gpios:8";
336         pwr_led_trigger = <&pwr_led>,"linux,default-trigger";
337
338         eth_led0 = <&phy1>,"led-modes:0";
339         eth_led1 = <&phy1>,"led-modes:4";
340     };
341 };
Logo

更多推荐