首页 > 代码库 > 驱动笔记 - IO端口和IO内存

驱动笔记 - IO端口和IO内存

访问IO端口

(#include <asm/io.h>)


设备资源
struct resource
{
resource_size_t start; //资源起始物理地址
resource_size_t end; //资源结束物理地址
const char *name;
unsigned long flags; //资源类型,如IORESOURCE_MEM,IORESOURCE_IO,IORESOURCE_IRQ
struct resource *parent, *sibling, *child; //资源链表指针
}

 

申请IO端口
struct resource *request_region(unsigned long first, unsigned long n, const char *name)
申请使用从first开始的n个端口,name为设备名字,失败返回NULL

 

释放IO端口
void release_region(unsigned long start, unsigned long n);

 

IO端口分配情况/proc/ioports

unsigned inb(unsigned port) 读字节端口
void outb(unsigned char byte, unsigned port) 写字节端口

unsigned inw(unsigned port) 16位
void outw(unsigned short word, unsigned port)

unsigned inl(unsigned port) 32位
void outl(unsigned longword, unsigned port)


访问IO内存(比访问IO端口多一步映射)
申请IO内存
struct resource *request_mem_region(unsigned long start, unsigned long len, char *name)(返回为物理地址)

void release_mem_region(unsigned long start, unsigned long len)


IO内存分配情况

/proc/iomem

 

访问IO内存前需进行物理地址到虚拟地址的映射
void *ioremap(unsigned long phys_addr, unsigned long size)
void iounmap(void *addr);

 

访问IO内存
读:
unsigned ioread8(void *addr)
unsigned ioread16(void *addr)
unsigned ioread32(void *addr)

unsigned readb(address)

unsigned readw(address)

unsigned readl(address)

 

写:
void iowrite8(u8 value, void *addr)
void iowrite16(u16 value, void *addr)
void iowrite32(u32 value, void *addr)

void writeb(unsigned value, address)

void writew(unsigned value, address)

void writel(unsigned value, address)

驱动笔记 - IO端口和IO内存