ICode9

精准搜索请尝试: 精确搜索
首页 > 系统相关> 文章详细

Linux设备树<转载>

2021-09-14 16:05:25  阅读:189  来源: 互联网

标签:node property struct Linux device dt 转载 节点 设备


转载地址:http://blog.csdn.net/smcdef/article/details/77387975

1. Device Tree简介

Linus Torvalds在2011年3月17日的ARM Linux邮件列表宣称“this whole ARM thing is a fucking pain in the ass”,引发ARM Linux社区的地震,随后ARM社区进行了一系列的重大修正。在过去的ARM Linux中,arch/arm/plat-xxx和arch/arm/mach-xxx中充斥着大量的垃圾代码,相当多数的代码只是在描述板级细节,而这些板级细节对于内核来讲,不过是垃圾,如板上的platform设备、resource、i2c_board_info、spi_board_info以及各种硬件的platform_data。 社区必须改变这种局面,于是PowerPC等其他体系架构下已经使用的Flattened Device Tree(FDT)进入ARM社区的视野。Device Tree是一种描述硬件的数据结构,它起源于OpenFirmware(OF)。在Linux2.6中,ARM架构的板极硬件细节过多地被硬编码在arch/arm/plat-xxx和arch/arm/mach-xxx,采用Device Tree后,许多硬件的细节可以直接透过它传递给Linux,而不再需要在kernel中进行大量的冗余编码。

Device Tree由一系列被命名的结点(node)和属性(property)组成,而结点本身可包含子结点。所谓属性,其实就是成对出现的name和value。在Device Tree中,可描述的信息包括(原先这些信息大多被hard code到kernel中):

  • CPU的数量和类别
  • 内存基地址和大小
  • 总线和桥
  • 外设连接
  • 中断控制器和中断使用情况
  • GPIO控制器和GPIO使用情况

它基本上就是画一棵电路板上CPU、总线、设备组成的树,Bootloader会将这棵树传递给内核,然后内核可以识别这棵树,并根据它展开出Linux内核中的platform_device、i2c_client、spi_device等设备。这些设备用到的内存、IRQ等资源,也被传递给了kernel,kernel会将这些资源绑定给展开的相应的设备。

2. Device Tree编译

Device Tree文件的格式为dts,包含的头文件格式为dtsi,dts文件是一种人可以看懂的编码格式。但是uboot和linux不能直接识别,他们只能识别二进制文件,所以需要把dts文件编译成dtb文件。dtb文件是一种可以被kernel和uboot识别的二进制文件。把dts编译成dtb文件的工具是dtc。Linux源码目录下scripts/dtc目录包含dtc工具的源码。在Linux的scripts/dtc目录下除了提供dtc工具外,也可以自己安装dtc工具,linux下执行:sudo apt-get install device-tree-compiler安装dtc工具。其中还提供了一个fdtdump的工具,可以反编译dtb文件。dts和dtb文件的转换如图1所示。 
dtc工具的使用方法是:dtc –I dts –O dtb –o xxx.dtb xxx.dts,即可生成dts文件对应的dtb文件了。 
图1 dts和dtb文件转换

3. Device Tree头信息

fdtdump工具使用,Linux终端执行ftddump –h,输出以下信息: 
fdtdump -h 
Usage: fdtdump [options] 
Options: -[dshV] 
-d, –debug Dump debug information while decoding the file 
-s, –scan Scan for an embedded fdt in file 
-h, –help Print this help and exit 
-V, –version Print version and exit 
本文采用s5pv21_smc.dtb文件为例说明fdtdump工具的使用。Linux终端执行fdtdump –sd s5pv21_smc.dtb > s5pv21_smc.txt,打开s5pv21_smc.txt文件,部分输出信息如下所示: 
// magic: 0xd00dfeed 
// totalsize: 0xce4 (3300) 
// off_dt_struct: 0x38 
// off_dt_strings: 0xc34 
// off_mem_rsvmap: 0x28 
// version: 17 
// last_comp_version: 16 
// boot_cpuid_phys: 0x0 
// size_dt_strings: 0xb0 
// size_dt_struct: 0xbfc 
以上信息便是Device Tree文件头信息,存储在dtb文件的开头部分。在Linux内核中使用struct fdt_header结构体描述。struct fdt_header结构体定义在scripts\dtc\libfdt\fdt.h文件中。


   
  1. struct fdt_header {
  2. fdt32_t magic; /* magic word FDT_MAGIC */
  3. fdt32_t totalsize; /* total size of DT block */
  4. fdt32_t off_dt_struct; /* offset to structure */
  5. fdt32_t off_dt_strings; /* offset to strings */
  6. fdt32_t off_mem_rsvmap; /* offset to memory reserve map */
  7. fdt32_t version; /* format version */
  8. fdt32_t last_comp_version; /* last compatible version */
  9. /* version 2 fields below */
  10. fdt32_t boot_cpuid_phys; /* Which physical CPU id we're booting on */
  11. /* version 3 fields below */
  12. fdt32_t size_dt_strings; /* size of the strings block */
  13. /* version 17 fields below */
  14. fdt32_t size_dt_struct; /* size of the structure block */
  15. };

fdtdump工具的输出信息即是以上结构中每一个成员的值,struct fdt_header结构体包含了Device Tree的私有信息。例如: fdt_header.magic是fdt的魔数,固定值为0xd00dfeed,fdt_header.totalsize是fdt文件的大小。使用二进制工具打开s5pv21_smc.dtb验证。s5pv21_smc.dtb二进制文件头信息如图2所示。从图2中可以得到Device Tree的文件是以大端模式储存。并且,头部信息和fdtdump的输出信息一致。 
图2 头信息

Device Tree中的节点信息举例如图3所示。 
图3 设备树全景试图

4. Device Tree文件结构

通过以上分析,可以得到Device Tree文件结构如图5所示。dtb的头部首先存放的是fdt_header的结构体信息,接着是填充区域,填充大小为off_dt_struct – sizeof(struct fdt_header),填充的值为0。接着就是struct fdt_property结构体的相关信息。最后是dt_string部分。 
图5 Device Tree文件结构
Device Tree源文件的结构分为header、fill_area、dt_struct及dt_string四个区域。fill_area区域填充数值0。节点(node)信息使用struct fdt_node_header结构体描述。属性信息使用struct fdt_property结构体描述。各个结构体信息如下:


   
  1. struct fdt_node_header {
  2. fdt32_t tag;
  3. char name[ 0];
  4. };
  5. struct fdt_property {
  6. fdt32_t tag;
  7. fdt32_t len;
  8. fdt32_t nameoff;
  9. char data[ 0];
  10. };

struct fdt_node_header描述节点信息,tag是标识node的起始结束等信息的标志位,name指向node名称的首地址。tag的取值如下:


   
  1. #define FDT_BEGIN_NODE 0x1 /* Start node: full name */
  2. #define FDT_END_NODE 0x2 /* End node */
  3. #define FDT_PROP 0x3 /* Property: name off, size, content */
  4. #define FDT_NOP 0x4 /* nop */
  5. #define FDT_END 0x9

FDT_BEGIN_NODE和FDT_END_NODE标识node节点的起始和结束,FDT_PROP标识node节点下面的属性起始符,FDT_END标识Device Tree的结束标识符。因此,对于每个node节点的tag标识符一般为FDT_BEGIN_NODE,对于每个node节点下面的属性的tag标识符一般是FDT_PROP。 
描述属性采用struct fdt_property描述,tag标识是属性,取值为FDT_PROP;len为属性值的长度(包括‘\0’,单位:字节);nameoff为属性名称存储位置相对于off_dt_strings的偏移地址。 
例如:compatible = “samsung,goni”, “samsung,s5pv210”;compatible是属性名称,”samsung,goni”, “samsung,s5pv210”是属性值。compatible属性名称字符串存放的区域是dt_string。”samsung,goni”, “samsung,s5pv210”存放的位置是fdt_property.data后面。因此fdt_property.data指向该属性值。fdt_property.tag的值为属性标识,len为属性值的长度(包括‘\0’,单位:字节),此处len = 29。nameoff为compatible字符串的位置相对于off_dt_strings的偏移地址,即&compatible = nameoff + off_dt_strings。 
dt_struct在Device Tree中的结构如图6所示。节点的嵌套也带来tag标识符的嵌套。 
图6 dt_struct结构图

5. kernel解析Device Tree

Device Tree文件结构描述就以上struct fdt_header、struct fdt_node_header及struct fdt_property三个结构体描述。kernel会根据Device Tree的结构解析出kernel能够使用的struct property结构体。kernel根据Device Tree中所有的属性解析出数据填充struct property结构体。struct property结构体描述如下:


   
  1. struct property {
  2. char *name; /* property full name */
  3. int length; /* property value length */
  4. void *value; /* property value */
  5. struct property *next; /* next property under the same node */
  6. unsigned long _flags;
  7. unsigned int unique_id;
  8. struct bin_attribute attr; /* 属性文件,与sysfs文件系统挂接 */
  9. };

总的来说,kernel根据Device Tree的文件结构信息转换成struct property结构体,并将同一个node节点下面的所有属性通过property.next指针进行链接,形成一个单链表。 
kernel中究竟是如何解析Device Tree的呢?下面分析函数解析过程。函数调用过程如图7所示。kernel的C语言阶段的入口函数是init/main.c/stsrt_kernel()函数,在early_init_dt_scan_nodes()中会做以下三件事:

  • 扫描/chosen或者/chose@0节点下面的bootargs属性值到boot_command_line,此外,还处理initrd相关的property,并保存在initrd_start和initrd_end这两个全局变量中;
  • 扫描根节点下面,获取{size,address}-cells信息,并保存在dt_root_size_cells和dt_root_addr_cells全局变量中;
  • 扫描具有device_type =“memory”属性的/memory或者/memory@0节点下面的reg属性值,并把相关信息保存在meminfo中,全局变量meminfo保存了系统内存相关的信息。 
    图7 函数调用过程
    Device Tree中的每一个node节点经过kernel处理都会生成一个struct device_node的结构体,struct device_node最终一般会被挂接到具体的struct device结构体。struct device_node结构体描述如下:

   
  1. struct device_node {
  2. const char *name; /* node的名称,取最后一次“/”和“@”之间子串 */
  3. const char *type; /* device_type的属性名称,没有为<NULL> */
  4. phandle phandle; /* phandle属性值 */
  5. const char *full_name; /* 指向该结构体结束的位置,存放node的路径全名,例如:/chosen */
  6. struct fwnode_handle fwnode;
  7. struct property *properties; /* 指向该节点下的第一个属性,其他属性与该属性链表相接 */
  8. struct property *deadprops; /* removed properties */
  9. struct device_node *parent; /* 父节点 */
  10. struct device_node *child; /* 子节点 */
  11. struct device_node *sibling; /* 姊妹节点,与自己同等级的node */
  12. struct kobject kobj; /* sysfs文件系统目录体现 */
  13. unsigned long _flags; /* 当前node状态标志位,见/include/linux/of.h line124-127 */
  14. void *data;
  15. };
  16. /* flag descriptions (need to be visible even when !CONFIG_OF) */
  17. #define OF_DYNAMIC 1 /* node and properties were allocated via kmalloc */
  18. #define OF_DETACHED 2 /* node has been detached from the device tree*/
  19. #define OF_POPULATED 3 /* device already created for the node */
  20. #define OF_POPULATED_BUS 4 /* of_platform_populate recursed to children of this node */

struct device_node结构体中的每个成员作用已经备注了注释信息,下面分析以上信息是如何得来的。Device Tree的解析首先从unflatten_device_tree()开始,代码列出如下:


   
  1. /**
  2. * unflatten_device_tree - create tree of device_nodes from flat blob
  3. *
  4. * unflattens the device-tree passed by the firmware, creating the
  5. * tree of struct device_node. It also fills the "name" and "type"
  6. * pointers of the nodes so the normal device-tree walking functions
  7. * can be used.
  8. */
  9. void __init unflatten_device_tree( void )
  10. {
  11. __unflatten_device_tree(initial_boot_params, &of_root,
  12. early_init_dt_alloc_memory_arch);
  13. /* Get pointer to "/chosen" and "/aliases" nodes for use everywhere */
  14. of_alias_scan(early_init_dt_alloc_memory_arch);
  15. }
  16. /**
  17. * __unflatten_device_tree - create tree of device_nodes from flat blob
  18. *
  19. * unflattens a device-tree, creating the
  20. * tree of struct device_node. It also fills the "name" and "type"
  21. * pointers of the nodes so the normal device-tree walking functions
  22. * can be used.
  23. * @blob: The blob to expand
  24. * @mynodes: The device_node tree created by the call
  25. * @dt_alloc: An allocator that provides a virtual address to memory
  26. * for the resulting tree
  27. */
  28. static void __unflatten_device_tree( const void *blob,
  29. struct device_node **mynodes,
  30. void * (*dt_alloc)(u64 size, u64 align))
  31. {
  32. unsigned long size;
  33. int start;
  34. void *mem;
  35. /* 省略部分不重要部分 */
  36. /* First pass, scan for size */
  37. start = 0;
  38. size = ( unsigned long)unflatten_dt_node(blob, NULL, &start, NULL, NULL, 0, true);
  39. size = ALIGN(size, 4);
  40. /* Allocate memory for the expanded device tree */
  41. mem = dt_alloc(size + 4, __alignof__( struct device_node));
  42. memset(mem, 0, size);
  43. /* Second pass, do actual unflattening */
  44. start = 0;
  45. unflatten_dt_node(blob, mem, &start, NULL, mynodes, 0, false);
  46. }

分析以上代码,在unflatten_device_tree()中,调用函数__unflatten_device_tree(),参数initial_boot_params指向Device Tree在内存中的首地址,of_root在经过该函数处理之后,会指向根节点,early_init_dt_alloc_memory_arch是一个函数指针,为struct device_node和struct property结构体分配内存的回调函数(callback)。在__unflatten_device_tree()函数中,两次调用unflatten_dt_node()函数,第一次是为了得到Device Tree转换成struct device_node和struct property结构体需要分配的内存大小,第二次调用才是具体填充每一个struct device_node和struct property结构体。__unflatten_device_tree()代码列出如下:


   
  1. /**
  2. * unflatten_dt_node - Alloc and populate a device_node from the flat tree
  3. * @blob: The parent device tree blob
  4. * @mem: Memory chunk to use for allocating device nodes and properties
  5. * @poffset: pointer to node in flat tree
  6. * @dad: Parent struct device_node
  7. * @nodepp: The device_node tree created by the call
  8. * @fpsize: Size of the node path up at the current depth.
  9. * @dryrun: If true, do not allocate device nodes but still calculate needed
  10. * memory size
  11. */
  12. static void * unflatten_dt_node( const void *blob,
  13. void *mem,
  14. int *poffset,
  15. struct device_node *dad,
  16. struct device_node **nodepp,
  17. unsigned long fpsize,
  18. bool dryrun)
  19. {
  20. const __be32 *p;
  21. struct device_node *np;
  22. struct property *pp, **prev_pp = NULL;
  23. const char *pathp;
  24. unsigned int l, allocl;
  25. static int depth;
  26. int old_depth;
  27. int offset;
  28. int has_name = 0;
  29. int new_format = 0;
  30. /* 获取node节点的name指针到pathp中 */
  31. pathp = fdt_get_name(blob, *poffset, &l);
  32. if (!pathp)
  33. return mem;
  34. allocl = ++l;
  35. /* version 0x10 has a more compact unit name here instead of the full
  36. * path. we accumulate the full path size using "fpsize", we'll rebuild
  37. * it later. We detect this because the first character of the name is
  38. * not '/'.
  39. */
  40. if ((*pathp) != '/') {
  41. new_format = 1;
  42. if (fpsize == 0) {
  43. /* root node: special case. fpsize accounts for path
  44. * plus terminating zero. root node only has '/', so
  45. * fpsize should be 2, but we want to avoid the first
  46. * level nodes to have two '/' so we use fpsize 1 here
  47. */
  48. fpsize = 1;
  49. allocl = 2;
  50. l = 1;
  51. pathp = "";
  52. } else {
  53. /* account for '/' and path size minus terminal 0
  54. * already in 'l'
  55. */
  56. fpsize += l;
  57. allocl = fpsize;
  58. }
  59. }
  60. /* 分配struct device_node内存,包括路径全称大小 */
  61. np = unflatten_dt_alloc(&mem, sizeof( struct device_node) + allocl,
  62. __alignof__( struct device_node));
  63. if (!dryrun) {
  64. char *fn;
  65. of_node_init(np);
  66. /* 填充full_name,full_name指向该node节点的全路径名称字符串 */
  67. np->full_name = fn = (( char *)np) + sizeof(*np);
  68. if (new_format) {
  69. /* rebuild full path for new format */
  70. if (dad && dad->parent) {
  71. strcpy(fn, dad->full_name);
  72. fn += strlen(fn);
  73. }
  74. *(fn++) = '/';
  75. }
  76. memcpy(fn, pathp, l);
  77. /* 节点挂接到相应的父节点、子节点和姊妹节点 */
  78. prev_pp = &np->properties;
  79. if (dad != NULL) {
  80. np->parent = dad;
  81. np->sibling = dad->child;
  82. dad->child = np;
  83. }
  84. }
  85. /* 处理该node节点下面所有的property */
  86. for (offset = fdt_first_property_offset(blob, *poffset);
  87. (offset >= 0);
  88. (offset = fdt_next_property_offset(blob, offset))) {
  89. const char *pname;
  90. u32 sz;
  91. if (!(p = fdt_getprop_by_offset(blob, offset, &pname, &sz))) {
  92. offset = -FDT_ERR_INTERNAL;
  93. break;
  94. }
  95. if (pname == NULL) {
  96. pr_info( "Can't find property name in list !\n");
  97. break;
  98. }
  99. if ( strcmp(pname, "name") == 0)
  100. has_name = 1;
  101. pp = unflatten_dt_alloc(&mem, sizeof( struct property),
  102. __alignof__( struct property));
  103. if (!dryrun) {
  104. /* We accept flattened tree phandles either in
  105. * ePAPR-style "phandle" properties, or the
  106. * legacy "linux,phandle" properties. If both
  107. * appear and have different values, things
  108. * will get weird. Don't do that. */
  109. /* 处理phandle,得到phandle值 */
  110. if (( strcmp(pname, "phandle") == 0) ||
  111. ( strcmp(pname, "linux,phandle") == 0)) {
  112. if (np->phandle == 0)
  113. np->phandle = be32_to_cpup(p);
  114. }
  115. /* And we process the "ibm,phandle" property
  116. * used in pSeries dynamic device tree
  117. * stuff */
  118. if ( strcmp(pname, "ibm,phandle") == 0)
  119. np->phandle = be32_to_cpup(p);
  120. pp->name = ( char *)pname;
  121. pp->length = sz;
  122. pp->value = (__be32 *)p;
  123. *prev_pp = pp;
  124. prev_pp = &pp->next;
  125. }
  126. }
  127. /* with version 0x10 we may not have the name property, recreate
  128. * it here from the unit name if absent
  129. */
  130. /* 为每个node节点添加一个name的属性 */
  131. if (!has_name) {
  132. const char *p1 = pathp, *ps = pathp, *pa = NULL;
  133. int sz;
  134. /* 属性name的value值为node节点的名称,取“/”和“@”之间的子串 */
  135. while (*p1) {
  136. if ((*p1) == '@')
  137. pa = p1;
  138. if ((*p1) == '/')
  139. ps = p1 + 1;
  140. p1++;
  141. }
  142. if (pa < ps)
  143. pa = p1;
  144. sz = (pa - ps) + 1;
  145. pp = unflatten_dt_alloc(&mem, sizeof( struct property) + sz,
  146. __alignof__( struct property));
  147. if (!dryrun) {
  148. pp->name = "name";
  149. pp->length = sz;
  150. pp->value = pp + 1;
  151. *prev_pp = pp;
  152. prev_pp = &pp->next;
  153. memcpy(pp->value, ps, sz - 1);
  154. (( char *)pp->value)[sz - 1] = 0;
  155. }
  156. }
  157. /* 填充device_node结构体中的name和type成员 */
  158. if (!dryrun) {
  159. *prev_pp = NULL;
  160. np->name = of_get_property(np, "name", NULL);
  161. np->type = of_get_property(np, "device_type", NULL);
  162. if (!np->name)
  163. np->name = "<NULL>";
  164. if (!np->type)
  165. np->type = "<NULL>";
  166. }
  167. old_depth = depth;
  168. *poffset = fdt_next_node(blob, *poffset, &depth);
  169. if (depth < 0)
  170. depth = 0;
  171. /* 递归调用node节点下面的子节点 */
  172. while (*poffset > 0 && depth > old_depth)
  173. mem = unflatten_dt_node(blob, mem, poffset, np, NULL,
  174. fpsize, dryrun);
  175. if (*poffset < 0 && *poffset != -FDT_ERR_NOTFOUND)
  176. pr_err( "unflatten: error %d processing FDT\n", *poffset);
  177. /*
  178. * Reverse the child list. Some drivers assumes node order matches .dts
  179. * node order
  180. */
  181. if (!dryrun && np->child) {
  182. struct device_node *child = np->child;
  183. np->child = NULL;
  184. while (child) {
  185. struct device_node *next = child->sibling;
  186. child->sibling = np->child;
  187. np->child = child;
  188. child = next;
  189. }
  190. }
  191. if (nodepp)
  192. *nodepp = np;
  193. return mem;
  194. }

通过以上函数处理就得到了所有的struct device_node结构体,为每一个node都会自动添加一个名称为“name”的property,property.length的值为当前node的名称取最后一个“/”和“@”之间的子串(包括‘\0’)。例如:/serial@e2900800,则length = 7,property.value = device_node.name = “serial”。

6. platform_device和device_node绑定

经过以上解析,Device Tree的数据已经全部解析出具体的struct device_node和struct property结构体,下面需要和具体的device进行绑定。首先讲解platform_device和device_node的绑定过程。在arch/arm/kernel/setup.c文件中,customize_machine()函数负责填充struct platform_device结构体。函数调用过程如图8所示。 
图8 platform_device生成流程图
代码分析如下:


   
  1. const struct of_device_id of_default_bus_match_table[] = {
  2. { .compatible = "simple-bus", },
  3. { .compatible = "simple-mfd", },
  4. #ifdef CONFIG_ARM_AMBA
  5. { .compatible = "arm,amba-bus", },
  6. #endif /* CONFIG_ARM_AMBA */
  7. {} /* Empty terminated list */
  8. };
  9. int of_platform_populate( struct device_node *root,
  10. const struct of_device_id *matches,
  11. const struct of_dev_auxdata *lookup,
  12. struct device *parent)
  13. {
  14. struct device_node *child;
  15. int rc = 0;
  16. /* 获取根节点 */
  17. root = root ? of_node_get(root) : of_find_node_by_path( "/");
  18. if (!root)
  19. return -EINVAL;
  20. /* 为根节点下面的每一个节点创建platform_device结构体 */
  21. for_each_child_of_node(root, child) {
  22. rc = of_platform_bus_create(child, matches, lookup, parent, true);
  23. if (rc) {
  24. of_node_put(child);
  25. break;
  26. }
  27. }
  28. /* 更新device_node flag标志位 */
  29. of_node_set_flag(root, OF_POPULATED_BUS);
  30. of_node_put(root);
  31. return rc;
  32. }
  33. static int of_platform_bus_create( struct device_node *bus,
  34. const struct of_device_id *matches,
  35. const struct of_dev_auxdata *lookup,
  36. struct device *parent, bool strict)
  37. {
  38. const struct of_dev_auxdata *auxdata;
  39. struct device_node *child;
  40. struct platform_device *dev;
  41. const char *bus_id = NULL;
  42. void *platform_data = NULL;
  43. int rc = 0;
  44. /* 只有包含"compatible"属性的node节点才会生成相应的platform_device结构体 */
  45. /* Make sure it has a compatible property */
  46. if (strict && (!of_get_property(bus, "compatible", NULL))) {
  47. return 0;
  48. }
  49. /* 省略部分代码 */
  50. /*
  51. * 针对节点下面得到status = "ok" 或者status = "okay"或者不存在status属性的
  52. * 节点分配内存并填充platform_device结构体
  53. */
  54. dev = of_platform_device_create_pdata(bus, bus_id, platform_data, parent);
  55. if (!dev || !of_match_node(matches, bus))
  56. return 0;
  57. /* 递归调用节点解析函数,为子节点继续生成platform_device结构体,前提是父节点
  58. * 的“compatible” = “simple-bus”,也就是匹配of_default_bus_match_table结构体中的数据
  59. */
  60. for_each_child_of_node(bus, child) {
  61. rc = of_platform_bus_create(child, matches, lookup, &dev->dev, strict);
  62. if (rc) {
  63. of_node_put(child);
  64. break;
  65. }
  66. }
  67. of_node_set_flag(bus, OF_POPULATED_BUS);
  68. return rc;
  69. }

总的来说,当of_platform_populate()函数执行完毕,kernel就为DTB中所有包含compatible属性名的第一级node创建platform_device结构体,并向平台设备总线注册设备信息。如果第一级node的compatible属性值等于“simple-bus”、“simple-mfd”或者”arm,amba-bus”的话,kernel会继续为当前node的第二级包含compatible属性的node创建platform_device结构体,并注册设备。Linux系统下的设备大多都是挂载在平台总线下的,因此在平台总线被注册后,会根据of_root节点的树结构,去寻找该总线的子节点,所有的子节点将被作为设备注册到该总线上。

7. i2c_client和device_node绑定

经过customize_machine()函数的初始化,DTB已经转换成platform_device结构体,这其中就包含i2c adapter设备,不同的SoC需要通过平台设备总线的方式自己实现i2c adapter设备的驱动。例如:i2c_adapter驱动的probe函数中会调用i2c_add_numbered_adapter()注册adapter驱动,函数流执行如图9所示。 
图9 i2c_client绑定流程
在of_i2c_register_devices()函数内部便利i2c节点下面的每一个子节点,并为子节点(status = “disable”的除外)创建i2c_client结构体,并与子节点的device_node挂接。其中i2c_client的填充是在i2c_new_device()中进行的,最后device_register()。在构建i2c_client的时候,会对node下面的compatible属性名称的厂商名字去除作为i2c_client的name。例如:compatible = “maxim,ds1338”,则i2c_client->name = “ds1338”。

8. Device_Tree与sysfs

kernel启动流程为start_kernel()→rest_init()→kernel_thread():kernel_init()→do_basic_setup()→driver_init()→of_core_init(),在of_core_init()函数中在sys/firmware/devicetree/base目录下面为设备树展开成sysfs的目录和二进制属性文件,所有的node节点就是一个目录,所有的property属性就是一个二进制属性文件。

标签:node,property,struct,Linux,device,dt,转载,节点,设备
来源: https://blog.csdn.net/jansert/article/details/120289635

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有