ioremap 函数映射操作已知的物理地址(寄存器、端口、IO)

时间:2022-10-20 17:06:50

NAME
ioremap - map bus memory into CPU space
SYNOPSIS

  void __iomem * ioremap  (unsigned long  offset, unsigned long size);

ARGUMENTS

offset
bus address of the memory

size
size of the resource to map

DESCRIPTION

ioremap performs a platform specific sequence of operations to make bus memory CPU accessible via the readb/readw/readl/writeb/ writew/writel functions and the other mmio helpers. The returned address is not guaranteed to be usable directly as a virtual address.

ioremap 函数来映射到内核地址空间,然后修改虚拟地址空间达到控制寄存器的状态。
我用如下方法操作一个地址为0x56000020的端口
第一种:

unsigned long port_addr;
(void *)(port_addr) = ioremap(0x56000020,0x8);
*(volatile unsigned int *)(port_addr) |= 0x00008000;

编译时候出现警告warning,左值有问题,建议使用第二种。
第二种:

volatile unsigned int *port_addr = ioremap(0x56000020,0x8);
*port_addr |= 0x00008000;
#define rGPACON (*(volatile unsigned *)(ioremap(GPACON,0x8)))