linux下ioremap的使用

一、头文件

#include <asm/io.h>

二、ioremap函数

/*
 * I/O memory mapping functions.
 */
extern void __iomem *__ioremap(phys_addr_t phys_addr, size_t size, pgprot_t prot);
extern void __iounmap(volatile void __iomem *addr);
extern void __iomem *ioremap_cache(phys_addr_t phys_addr, size_t size);

#define ioremap(addr, size)            __ioremap((addr), (size), __pgprot(PROT_DEVICE_nGnRE))
#define ioremap_nocache(addr, size)    __ioremap((addr), (size), __pgprot(PROT_DEVICE_nGnRE))
#define ioremap_wc(addr, size)         __ioremap((addr), (size), __pgprot(PROT_NORMAL_NC))
#define ioremap_wt(addr, size)         __ioremap((addr), (size), __pgprot(PROT_DEVICE_nGnRE))
#define iounmap                        __iounmap

1、函数功能:将I/O内存资源的物理地址映射到核心虚地址空间(3GB-4GB)中。

2、addr:要映射的起始的IO地址

     size:要映射的空间的大小

三、应用示例

#include <asm/io.h>


#define SYS_WRITEL(Addr, Value) ((*(volatile unsigned int *)(Addr)) = (Value))
#define SYS_READ(Addr)          (*((volatile int *)(Addr)))


static void  *g_reg_iocfg_base = NULL;


static inline void reg_write32(unsigned long value, unsigned long mask, unsigned long addr)
{
    unsigned long t;

    t = SYS_READ((const volatile void *)(uintptr_t)addr);
    t &= ~mask;
    t |= value & mask;
    SYS_WRITEL((volatile void *)(uintptr_t)addr, t);
}

static void i2c0_pin_mux(void)
{
    /* SDA SCL */
    SYS_WRITEL(g_reg_iocfg_base + 0x0014, 0x1501);
    SYS_WRITEL(g_reg_iocfg_base + 0x0018, 0x1501);
}

static int sysconfig_init(void)
{
    g_reg_iocfg_base = (void*)ioremap(0x170F0000, 0x10000);
    if (g_reg_iocfg_base == NULL) {
        return 1;
    }

    return 0;
}

static void sysconfig_exit(void)
{
    if (g_reg_iocfg_base != NULL) {
        iounmap(g_reg_iocfg_base);
        g_reg_iocfg_base = NULL;
    }
}

 

posted @ 2020-08-24 19:22  wmate  阅读(4276)  评论(0编辑  收藏  举报