Menu Close

Linux Device Tree Other Common of Functions

This article talks about the other commonly used of functions.

For related subjects, please refer to the SOC Table of Contents.

 

of_device_is_compatible

This function is used mainly to see if the node’s compatible property includes the string specified by compat, which serves to check the device node’s compatibility. The function has the following format:

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

device is the device node.

compat is the string to check.

The function will return 0 if the node’s compatible property doesn’t include the string specified by compat, and will return a positive integer if it includes the specified string.

 

of_get_address

This function is used to obtain properties related to addresses, mainly being “reg” and “assigned-addresses” property values. The function has the following format:

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.

The function will return the read address data’s initial address and will return 0 if reading failed.

 

of_translate_address

This function is used to convert addresses read from the device tree into physical addresses.

u64 of_translate_address(struct device_node *dev,
                         const __be32       *in_addr);

dev is the device node.

in_addr is the address to be converted.

The function will return the converted physical address, or will return 0F_BAD_ADDR if it failed.

 

of_address_to_resource

Peripherals such as IIC, SPI, and GPIO all have corresponding registers, which is a group of memory space. The Linux kernel uses the resource structure to describe memory spaces; the resource structure is defined in include/linux/ioport.h from line 18 to 24. Note that for 32-bit SoCs, resource_size_t is unsigned 32-bit.

struct resource{
        resource_size_t start;
        resource_size_t end;
        const char *name;
        unsigned long flags;
        struct resource *parent, *sibling, *child;
};

start is the starting address.

end is the ending address.

name is the name of the resource.

flags is the resource flag, which typically represents the resource type. The resource flag definitions that can be chosen are located in include/linux/ioport.h, as shown below:

The most commonly used is IORESOURCE_MEM, IORESOURCE_REG, and IORESOURCE_IRQ.

The of_address_to_resource function takes the reg property value and converts it to resource structure types. The formatting is as follows:

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

dev is the device node.

index is the address resource index.

r is the obtained resource value of the resource type.

The function will return 0 if the process was successful and will return a negative value otherwise.

 

of_iomap

This function mainly deals with direct memory mapping. It takes the memory information in reg and converts it to a virtual address. If there are multiple segments in the reg property, we can use the index parameter to appoint the segment to map. The formatting is as follows:

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

np is the device node.

index is the segment to map in the reg property. If reg only has one segment, index is set as 0.

The function will return the starting address of the mapped memory and will return NULL if it failed.

Posted in Textbook and Training Project

Related Articles

Leave a Reply

Your email address will not be published.

Leave the field below empty!