ioremap介绍及用法

 
ioremap: map physical address to kernel virtual address, we can use kernel virtual address to access register.
 
 
1. get physical address
rsc_p = platform_get_resource(Platform_Device_p, IORESOURCE_MEM, p->ResId);
p->PhysBaseAddr      = rsc_p->start;

we can use anyother method to get physical.

 
2. map physical address to kernel virtual address
p->MappedBaseAddr_p = ioremap(p->PhysBaseAddr, p->ResByteCount);

 

 
3. read or write
iowrite32(Value, p->MappedBaseAddr_p + (RemappedOffset / 4));

 

 
for example:
#define AP_GLB_REG_BASE_ADDR 0x00510000
#define AP_SYS_USB3_0_PHY_CFG3_ADDR (AP_GLB_REG_BASE_ADDR+0x011)
#define AP_SYS_USB3_0_CTRL_ADDR (AP_GLB_REG_BASE_ADDR+0x022)

#define AP_SYS_USB3_0_CTRL_VBUSVALID_LSB 7
#define AP_SYS_USB3_0_PHY_CFG3_VBUSVLDEXT_LSB 5


static void init_usb_reg(void)
{
    u32 value;
    void __iomem * addr1;
    void __iomem * addr2;
    
    addr1 = ioremap(AP_SYS_USB3_0_PHY_CFG3_ADDR, 4);
    addr2 = ioremap(AP_SYS_USB3_0_CTRL_ADDR, 4);

    value = readl(addr1);
    value |= (0x1 << AP_SYS_USB3_0_PHY_CFG3_VBUSVLDEXT_LSB);
    writel(value, addr1);

    value = readl(addr2);
    value |= (0x1 << AP_SYS_USB3_0_CTRL_VBUSVALID_LSB);
    writel(value, addr2);
}

 

 

你可能感兴趣的:(Linux,Driver,Linux,Kernel)