基于tiny4412的Linux内核移植 --- aliases节点解析
作者信息
作者: 彭东林
QQ:405728433
平台简介
开发板:tiny4412ADK + S700 + 4GB Flash
要移植的内核版本:Linux-4.4.0 (支持device tree)
u-boot版本:友善之臂自带的 U-Boot 2010.12 (为支持uImage启动,做了少许改动)
busybox版本:busybox 1.25
交叉编译工具链: arm-none-linux-gnueabi-gcc
(gcc version 4.8.3 20140320 (prerelease) (Sourcery CodeBench Lite 2014.05-29))
正文
在设备树中有一个叫做aliases的节点:
1: / {
2: ... ...
3:
4: chosen {
5: stdout-path = "/serial@13800000";
6: bootargs = "root=/dev/ram0 rw rootfstype=ext4 console=ttySAC0,115200 ethmac=1C:6F:65:34:51:7E init=/linuxrc";
7: };
8:
9: aliases {
10: spi0 = "/spi@13920000";
11: spi1 = "/spi@13930000";
12: spi2 = "/spi@13940000";
13: i2c0 = "/i2c@13860000";
14: i2c1 = "/i2c@13870000";
15: i2c2 = "/i2c@13880000";
16: i2c3 = "/i2c@13890000";
17: ... ...
18: };
19: ... ...
20: };
在Linux内核启动的时候会解析这个节点:
start_kernel
---> setup_arch
---> unflatten_device_tree
---> of_alias_scan
在of_alias_scan中会扫描这个节点:
of_alias_scan:
1: void of_alias_scan(void * (*dt_alloc)(u64 size, u64 align))
2: {
3: struct property *pp;
4:
5: of_aliases = of_find_node_by_path("/aliases"); // 找到/aliases节点对应的device_node
6: of_chosen = of_find_node_by_path("/chosen"); // 找到/chosen节点对应的device_node
7: if (of_chosen == NULL) // 如果没有/chosen的话,就找/chosen@0节点
8: of_chosen = of_find_node_by_path("/chosen@0");
9:
10: if (of_chosen) {
11: /* linux,stdout-path and /aliases/stdout are for legacy compatibility */
12: const char *name = of_get_property(of_chosen, "stdout-path", NULL);
13: if (!name)
14: name = of_get_property(of_chosen, "linux,stdout-path", NULL);
15: if (IS_ENABLED(CONFIG_PPC) && !name)
16: name = of_get_property(of_aliases, "stdout", NULL);
17: if (name)
18: of_stdout = of_find_node_opts_by_path(name, &of_stdout_options);
19: }
20:
21: if (!of_aliases)
22: return;
23:
24: for_each_property_of_node(of_aliases, pp) { // 遍历/aliases节点的属性,以属性i2c2 = "/i2c@13880000";为例
25: const char *start = pp->name; // 属性的名字,如"i2c2"
26: const char *end = start + strlen(start); // 名字的结尾,*end是'\0'
27: struct device_node *np;
28: struct alias_prop *ap;
29: int id, len;
30:
31: /* 不处理名字是name、phandle、linux,phandle的属性 */
32: if (!strcmp(pp->name, "name") ||
33: !strcmp(pp->name, "phandle") ||
34: !strcmp(pp->name, "linux,phandle"))
35: continue;
36:
37: np = of_find_node_by_path(pp->value);
38: /*
39: 根据属性的值(如"/i2c@13880000")获得这个值对应的节点
40: i2c@13880000 {
41: #address-cells = <0x1>;
42: #size-cells = <0x0>;
43: compatible = "samsung,s3c2440-i2c";
44: reg = <0x13880000 0x100>;
45: interrupts = <0x0 0x3c 0x0>;
46: clocks = <0x7 0x13f>;
47: clock-names = "i2c";
48: pinctrl-names = "default";
49: pinctrl-0 = <0x22>;
50: status = "disabled";
51: };
52: */
53: if (!np)
54: continue;
55:
56: /* walk the alias backwards to extract the id and work out
57: * the 'stem' string */
58: while (isdigit(*(end-1)) && end > start) //对于"i2c2",end最终会指向字符'2'的地址
59: end--;
60: len = end - start; // 获得"i2c"的长度(不包含结尾的数字2),就是3
61:
62: if (kstrtoint(end, 10, &id) < 0) // 将end指向的字符'2'转化为数字2,赋值给id
63: continue;
64:
65: /* Allocate an alias_prop with enough space for the stem */
66: ap = dt_alloc(sizeof(*ap) + len + 1, 4); // 分配内存,多分配的"len+1"用于存放stem的名字
67: if (!ap)
68: continue;
69: memset(ap, 0, sizeof(*ap) + len + 1);
70: ap->alias = start; // ap->alias指向字符串"i2c2"
71: of_alias_add(ap, np, id, start, len);
72: }
73: }
of_alias_add:
1: static void of_alias_add(struct alias_prop *ap, struct device_node *np,
2: int id, const char *stem, int stem_len)
3: {
4: ap->np = np; // np是"/i2c@13880000"对应的节点device_node
5: ap->id = id; // id的值是2
6: strncpy(ap->stem, stem, stem_len); // 由于stem_len是3,所以ap->stem被赋值为"i2c"
7: ap->stem[stem_len] = 0;
8: list_add_tail(&ap->link, &aliases_lookup); // 将这个ap加入到全局aliases_lookup链表中
9: pr_debug("adding DT alias:%s: stem=%s id=%i node=%s\n",
10: ap->alias, ap->stem, ap->id, of_node_full_name(np));
11: }
使用:
在drivers/i2c/i2c-core.c中:
1: int i2c_add_adapter(struct i2c_adapter *adapter)
2: {
3: struct device *dev = &adapter->dev;
4: int id;
5:
6: if (dev->of_node) {
7: id = of_alias_get_id(dev->of_node, "i2c");
8: if (id >= 0) {
9: adapter->nr = id;
10: return __i2c_add_numbered_adapter(adapter);
11: }
12: }
13: ... ...
14: }
第7行调用of_alias_get_id获得与这个device_node(即/i2c@13880000节点)对应的alias_prop的id,如果以/i2c@13880000节点为例,这里得到的id就是2。
of_alias_get_id:
1: int of_alias_get_id(struct device_node *np, const char *stem)
2: {
3: struct alias_prop *app;
4: int id = -ENODEV;
5:
6: mutex_lock(&of_mutex);
7: list_for_each_entry(app, &aliases_lookup, link) { // 遍历全局链表aliases_lookup
8: if (strcmp(app->stem, stem) != 0) // 找到 stem 是 "i2c" 的alias_prop
9: continue;
10:
11: if (np == app->np) { // 判断这个alias_prop指向的device_node是不是跟传入的匹配
12: id = app->id; // 获得 id,2
13: break;
14: }
15: }
16: mutex_unlock(&of_mutex);
17:
18: return id;
19: }
從上面的分析就可以知道alias節點的作用了:
比如SoC上有如果多個i2c控制器,alias的相當於給每個i2c控制器分配一個唯一的編號,如上面的i2c@13880000對應的alias是i2c,那麼這個編號就是2,將來就可以在/dev下看到名爲i2c-2的設備節點。
在內核中可以看到很多地方都會調用of_alias_get_id,他的作用就是根據傳入的device node,在alias中找到對應的唯一編號,如:
of_alias_get_id(pdev->dev.of_node, "spi")
of_alias_get_id(node, "fimc")
of_alias_get_id(pdev->dev.of_node, "serial")
of_alias_get_id(pdev->dev.of_node, "uart")
of_alias_get_id(dev->of_node, "gpio")
... ...
完。
基于tiny4412的Linux内核移植 --- aliases节点解析的更多相关文章
- 基于tiny4412的Linux内核移植 --- aliases节点解析【转】
转自:https://www.cnblogs.com/pengdonglin137/p/5252348.html 阅读目录(Content) 作者信息 平台简介 正文 回到顶部(go to top) ...
- 基于tiny4412的Linux内核移植 -- 设备树的展开
作者信息 作者: 彭东林 邮箱:pengdonglin137@163.com QQ:405728433 平台简介 开发板:tiny4412ADK + S700 + 4GB Flash 要移植的内核版本 ...
- 基于tiny4412的Linux内核移植 -- 设备树的展开【转】
转自:https://www.cnblogs.com/pengdonglin137/p/5248114.html#_lab2_3_1 阅读目录(Content) 作者信息 平台简介 摘要 正文 一.根 ...
- 基于tiny4412的Linux内核移植(支持device tree)(三)
作者信息 作者: 彭东林 邮箱:pengdonglin137@163.com QQ:405728433 平台简介 开发板:tiny4412ADK + S700 + 4GB Flash 要移植的内核版本 ...
- 基于tiny4412的Linux内核移植 -- MMA7660驱动移植(九-2)
作者信息 作者: 彭东林 邮箱:pengdonglin137@163.com QQ:405728433 平台简介 开发板:tiny4412ADK + S700 + 4GB Flash 要移植的内核版本 ...
- 基于tiny4412的Linux内核移植 -- MMA7660驱动移植(九)
作者信息 作者: 彭东林 邮箱:pengdonglin137@163.com QQ:405728433 平台简介 开发板:tiny4412ADK + S700 + 4GB Flash 要移植的内核版本 ...
- 基于tiny4412的Linux内核移植(支持device tree)(一)
作者信息 作者: 彭东林 邮箱:pengdonglin137@163.com QQ:405728433 平台简介 开发板:tiny4412ADK + S700 + 4GB Flash 要移植的内核版本 ...
- 基于tiny4412的Linux内核移植 -- PWM子系统学习(八)
作者信息 作者: 彭东林 邮箱:pengdonglin137@163.com QQ:405728433 平台简介 开发板:tiny4412ADK + S700 + 4GB Flash 要移植的内核版本 ...
- 基于tiny4412的Linux内核移植 -- SD卡驱动移植(五)
作者信息 作者: 彭东林 邮箱:pengdonglin137@163.com QQ:405728433 平台简介 开发板:tiny4412ADK + S700 + 4GB Flash 要移植的内核版本 ...
随机推荐
- C# 标签(条码)的打印与设计(一)
C# 标签(条码)的打印与设计(一) C# 标签(条码)的打印与设计(二) 总结:自定义设计条码器.
- solr课程学习系列-solr的概念与结构(1)
Solr是基于Lucene的采用Java5开发的一个高性能全文搜索服务器.源于lucene,却更比Lucene更为丰富更为强大的查询语言.同时实现了可配置.可扩展并对查询性能进行了优化,并且提供了一个 ...
- ListView实现Item局部刷新
对于ListView数据的刷新大家都知道,改变Adapter的数据源,然后调用Adapter的notifyDateSetChanged()方法即可. 但是博主在做公司项目的时候,有个下载模块,因为可 ...
- iOS:OC Lib:MagicalRecord
# MagicalRecord 2.1 ## 前言 CoreData是iOS开发中经常使用的数据持久化的技术.但其操作过程稍微繁琐,即使你只是实现简单的存取,不涉及请求优化,也要进行许多配置工作,代码 ...
- Scala 深入浅出实战经典 第44讲: scala中view bounds代码实例
王家林亲授<DT大数据梦工厂>大数据实战视频 Scala 深入浅出实战经典(1-64讲)完整视频.PPT.代码下载:百度云盘:http://pan.baidu.com/s/1c0noOt6 ...
- 纯CSS实现图片抖动
实现方法: 1.将上文提到的以下JS内容删除: $(".imagelogo").mouseover(function() { obj = $(this); i = 5; timer ...
- A Simple MVVM Example[Forward]
In my opinion, if you are using WPF or Silverlight you should be using the MVVM design pattern. It i ...
- jqPlot插件绘制柱状图
每天都在这里看别人写的东西,确发现自己好久没写文章了,可能是因为确实很忙,或许这也是在给自己找的一种借口. 不过这也是我人生中加入得第一个创业公司,来到这里才知道创业公司其实真的很辛苦,产品的萌芽才开 ...
- NVelocity-0.4.2.8580 的修改记录[发个vs2008能用的版本] -- "It appears that no class was specified as the ResourceManager..." bug 修正等
因为另有开发记录工具最新没怎么在 cnblog 写开发备忘.不过我觉得这个是个比较严重的问题,觉得有必要让更多的人知道处理方法,所以在 cnblog 也放上一篇希望广为传播. 因为现在网络上vs200 ...
- 菜鸟调错(八)—— Maven编译错误:不兼容的类型的解决方案
泛型在实际的工作中应用非常广泛,关于泛型就不在这里赘述了,感兴趣请戳<重新认识泛型>.项目中用到了如下的泛型: public <T> T query(String sql, R ...