ioremap exmaple
What is ioremap?
ioremap is used to map the I/O address space to kernel virtual address space, so we can access the I/O device.
Usage of ioremap
ioremap is macro definition in asm/io.h.
#define ioremap(cookie,size) __ioremap(cookie,size,0)
__ioremap is defined in arm/mm/ioremap.c.
void __iomem * __ioremap(unsigned long phys_addr, size_t size, unsigned long flags);
iounmap is used to cancel the map what the ioremap did.
void iounmap(void * addr);
Related function: readl() and writel()
unsigned char readl (unsigned int addr )
void writel (unsigned char data , unsigned short addr )
So now let me take a simple example.
#define BASE 0x12345678
#define GPIO1_MASK 0x40000000
static void __iomem *base_gpio;
int main()
{
base_gpio = ioremap(BASE, SZ_512)
//SZ_512 is a macro definition in linux/size.h
u32 gpioVal = readl(base_gpio + 1)
//1 needs to be modified to the real offset
pgioVal &= ~GPIO1_MASK
//This operation doesn't make sense. It's just an example
writel(gpioVal, base_gpio + 1)
//Write the value.
iounmap(BASE);
}
Have fun.
Share this article to your social media