Chinaunix首页 | 论坛 | 博客
  • 博客访问: 18676
  • 博文数量: 14
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 123
  • 用 户 组: 普通用户
  • 注册时间: 2013-08-16 09:14
文章分类

全部博文(14)

文章存档

2014年(2)

2013年(12)

我的朋友

分类: LINUX

2013-08-29 10:40:11

学过<微机原理及接口技术>的都知道, IO ports的编址方式有种: 统一编址, 如arm;  独立编址, 如x86;
1. 什么是IO ports , IO memory?
IO ports: 
顾名思意, 就是输入输出端口. 而访问输入输出端口都是通过寄存器读写进行的. 所以, 可以简单理解IO ports 就是外设的寄存器. 

在x86上, IO ports有独立的地址空间, 并且有独立的访问指令.
在arm上, IO ports没有独立的地址空间, 访问IO ports和访问内存用的是同样的指令.

linux为了portability问题, 对于没有IO独立地址空间的体系结构, 通过 "fake port I/O by remapping port addresses to memory addresses" 隐藏了细节.

不过个人认为, 在编写驱动时, 自己应该对当前系统的IO编址方式有清楚的认识.
如果独立编址, 就用IO ports的这套接口. 反之, 就用IO MEMORY 那套接口.
IO ports的这套接口
如:
struct resource *request_region(unsigned long start, unsigned long len, char *name);
void release_region(unsigned long start, unsigned long len);
unsigned inb(unsigned port);
void outb(unsigned char byte, unsigned port);

IO memory:
memory-mapped registers and device memory
如:
struct resource *request_mem_region(unsigned long start, unsigned long len, char *name);
void release_mem_region(unsigned long start, unsigned long len);
void *ioremap(unsigned long phys_addr, unsigned long size);
void *ioremap_nocache(unsigned long phys_addr, unsigned long size);
void iounmap(void *virt_addr);

#include
unsigned int ioread8(void *addr);
unsigned int ioread16(void *addr);

2. Linux kernel 中详细编程接口:
2.1 io ports
#include
struct resource *request_region(unsigned long start, unsigned long len, char *name);
void release_region(unsigned long start, unsigned long len);

#include
unsigned inb(unsigned port);
void outb(unsigned char byte, unsigned port);
unsigned inw(unsigned port);
void outw(unsigned short word, unsigned port);
unsigned inl(unsigned port);
void outl(unsigned doubleword, unsigned port);

2.2 io memory
#include
struct resource *request_mem_region(unsigned long start, unsigned long len, char *name);
void release_mem_region(unsigned long start, unsigned long len);
#include
void *ioremap(unsigned long phys_addr, unsigned long size);
void *ioremap_nocache(unsigned long phys_addr, unsigned long size);
void iounmap(void *virt_addr);

#include
unsigned int ioread8(void *addr);
unsigned int ioread16(void *addr);
unsigned int ioread32(void *addr);
void iowrite8(u8 value, void *addr);
void iowrite16(u16 value, void *addr);
void iowrite32(u32 value, void *addr);
            Accessor functions that are used to work with I/O memory.

unsigned readb(address);
unsigned readw(address);
unsigned readl(address);
void writeb(unsigned value, address);
void writew(unsigned value, address);
void writel(unsigned value, address);
memset_io(address, value, count);
memcpy_fromio(dest, source, nbytes);
memcpy_toio(dest, source, nbytes);
        Older, type-unsafe functions for accessing I/O memory.

阅读(211) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~