此文章列出了设备树中其他常用的 of 函数,并且进行了介绍。
相关参考文章:SOC 教学教案
of_device_is_compatible
此函数主要用于查看节点的compatible属性是否包含compat指定的字符串,用于检查设备节点的兼容性。函数的格式如下:
int of_device_is_compatible(const struct device_node *device, const char *compat);
device 是设备节点。
compat 是将检查的字符串。
如果节点的compatible属性不包含compat指定的字符串,函数将返回0;若包含将返回一个正整数。
of_get_address
此函数用于获取跟地址相关的属性,主要为“reg”和“assigned-addresses”属性值。格式如下:
const __be32 *of_get_address(struct device_node *dev, int index, u64 *size, unsigned int *flags);
dev is the device node.
index is the address label to be read.
size is the address length.
flags are parameters such as IORESOURCE_IO and IORESOURCE_MEM.
函数将返回读取的地址信息的首地址;如果读取失败将返回0。
of_translate_address
此函数用于把设备树中读取的地址转换成物理地址。
u64 of_translate_address(struct device_node *dev, const __be32 *in_addr);
dev 是设备节点。
in_addr 是将转换的地址。
函数将返回转换成功的物理地址;如果转换失败将返回0F_BAD_ADDR 。
of_address_to_resource
IIC,SPI,GPIO等外设都有相应的寄存器,注意这些寄存器是一组内存空间。Linux内核使用资源结构体描述内存空间;其资源结构体定义在include/linux/ioport.h中的第18至24行之间。注意在32位的SoC中resource_size_t是无符号32位。
struct resource{ resource_size_t start; resource_size_t end; const char *name; unsigned long flags; struct resource *parent, *sibling, *child; };
start是起始地址。
end是结束地址。
name是资源的名称。
flags 是资源标志位,通常用于表示资源类型。资源标志位可以选择的定义在include/linux/ioport.h目录下。如下所示:
其中最常用的是IORESOURCE_MEM,IORESOURCE_REG,以及IORESOURCE_IRQ。
of_address_to_resource函数把reg属性值转换为资源结构体,格式如下:
int of_address_to_resource(struct device_node *dev, int index, struct resource *r);
dev是设备节点。
index是地址资源标号。
r是resource类型获取的资源值。
如果函数的整个过程顺利将返回0;如果不顺利将返回一个负数。
of_iomap
此函数主要用于处理之间的内存映射。它将把reg中的内存信息转换为一个虚拟地址。如果reg的属性有许多段,我们可以使用index参数指定映射的段位。格式如下:
void __iomem *of_iomap(struct device_node *np, int index);
np是设备节点。
index是reg属性中将映射的段位。如果reg只有一个段位,index将被设为0。
函数如果成功将返回映射后的虚拟内存的首地址;如果不成功将返回NULL。