设备树接口

设备树OF的API接口

① kernel入口处获取到uboot传过来的.dtb镜像的基地址

② 通过early_init_dt_scan()函数来获取kernel初始化时需要的bootargs和cmd_line等系统引导参数。

③ 调用unflatten_device_tree函数来解析dtb文件,构建一个由device_node结构连接而成的单向链表,并使用全局变量of_allnodes保存这个链表的头指针。

④ 内核调用OF的API接口,获取of_allnodes链表信息来初始化内核其他子系统、设备等。
OF的接口

函数在/drivers/of/目录下,有of_i2c.c、of_mdio.c、of_mtd.c、Adress.c等等

这里将列出几个常用的API接口。

  1. 用来查找在dtb中的根节点

unsigned long __init of_get_flat_dt_root(void)

  1. 根据deice_node结构的full_name参数,在全局链表of_allnodes中,查找合适的device_node

struct device_node *of_find_node_by_path(const char *path)

例如:

struct device_node *cpus;

cpus=of_find_node_by_path("/cpus");

  1. 若from=NULL,则在全局链表of_allnodes中根据name查找合适的device_node

struct device_node *of_find_node_by_name(struct device_node *from,const char *name)

例如:

struct device_node *np;

np = of_find_node_by_name(NULL,“firewire”);

  1. 根据设备类型查找相应的device_node

struct device_node *of_find_node_by_type(struct device_node *from,const char *type)

例如:

struct device_node *tsi_pci;

tsi_pci= of_find_node_by_type(NULL,“pci”);

  1. 根据compatible字符串查找device_node

struct device_node *of_find_compatible_node(struct device_node *from,const char *type, const char *compatible)

  1. 根据节点属性的name查找device_node

struct device_node *of_find_node_with_property(struct device_node *from,const char *prop_name)

  1. 根据phandle查找device_node

struct device_node *of_find_node_by_phandle(phandle handle)

  1. 根据alias的name获得设备id号

int of_alias_get_id(struct device_node *np, const char *stem)

  1. device node计数增加/减少

struct device_node *of_node_get(struct device_node *node)

void of_node_put(struct device_node *node)

  1. 根据property结构的name参数,在指定的device node中查找合适的property

struct property *of_find_property(const struct device_node *np,const char *name,int *lenp)

  1. 根据property结构的name参数,返回该属性的属性值

const void *of_get_property(const struct device_node *np, const char *name,int *lenp)

  1. 根据compat参数与device node的compatible匹配,返回匹配度

int of_device_is_compatible(const struct device_node *device,const char *compat)

  1. 获得父节点的device node

struct device_node *of_get_parent(const struct device_node *node)

  1. 将matches数组中of_device_id结构的name和type与device node的compatible和type匹配,返回匹配度最高的of_device_id结构

const struct of_device_id *of_match_node(const struct of_device_id *matches,const struct device_node *node)

  1. 根据属性名propname,读出属性值中的第index个u32数值给out_value

int of_property_read_u32_index(const struct device_node *np,const char *propname,u32 index, u32 *out_value)

  1. 根据属性名propname,读出该属性的数组中sz个属性值给out_values

int of_property_read_u8_array(const struct device_node *np,const char *propname, u8 *out_values, size_t sz)

int of_property_read_u16_array(const struct device_node *np,const char *propname, u16 *out_values, size_t sz)

int of_property_read_u32_array(const struct device_node *np,const char *propname, u32 *out_values,size_t sz)

  1. 根据属性名propname,读出该属性的u64属性值

int of_property_read_u64(const struct device_node *np, const char *propname,u64 *out_value)

  1. 根据属性名propname,读出该属性的字符串属性值

int of_property_read_string(struct device_node *np, const char *propname,const char **out_string)

  1. 根据属性名propname,读出该字符串属性值数组中的第index个字符串

int of_property_read_string_index(struct device_node *np, const char *propname,int index, const char **output)

  1. 读取属性名propname中,字符串属性值的个数

int of_property_count_strings(struct device_node *np, const char *propname)

  1. 读取该设备的第index个irq号

unsigned int irq_of_parse_and_map(struct device_node *dev, int index)

  1. 读取该设备的第index个irq号,并填充一个irq资源结构体

int of_irq_to_resource(struct device_node *dev, int index, struct resource *r)

  1. 获取该设备的irq个数

int of_irq_count(struct device_node *dev)

  1. 获取设备寄存器地址,并填充寄存器资源结构体

int of_address_to_resource(struct device_node *dev, int index,struct resource *r)

const __be32 *of_get_address(struct device_node *dev, int index, u64 *size,unsigned int *flags)

  1. 获取经过映射的寄存器虚拟地址

void __iomem *of_iomap(struct device_node *np, int index)

  1. 根据device_node查找返回该设备对应的platform_device结构

struct platform_device *of_find_device_by_node(struct device_node *np)

  1. 根据device node,bus id以及父节点创建该设备的platform_device结构

struct platform_device *of_device_alloc(struct device_node *np,const char *bus_id,struct device *parent)

static struct platform_device *of_platform_device_create_pdata(struct device_node *np,const char *bus_id,

void *platform_data,struct device *parent)

  1. 遍历of_allnodes中的节点挂接到of_platform_bus_type总线上,由于此时of_platform_bus_type总线上还没有驱动,所以此时不进行匹配

int of_platform_bus_probe(struct device_node *root,const struct of_device_id *matches,struct device *parent)

参考:https://www.cnblogs.com/edver/p/9063526.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值