这两个函数都是实现在mm/ioremap.c文件中。
在将I/O内存资源的物理地址映射成核心虚地址后,理论上讲我们就可以象读写RAM那样直接读写I/O内存资源了。为了保证驱动程序的跨平台的可移植性,我们应该使用Linux中特定的函数来访问I/O内存资源,而不应该通过指向核心虚地址的指针来访问。如在x86平台上,读写I/O的函数如下所示:
#define readb(addr) (*(volatile unsigned char *) __io_virt(addr)) #define readw(addr) (*(volatile unsigned short *) __io_virt(addr)) #define readl(addr) (*(volatile unsigned int *) __io_virt(addr))
#define writeb(b,addr) (*(volatile unsigned char *) __io_virt(addr) = (b)) #define writew(b,addr) (*(volatile unsigned short *) __io_virt(addr) = (b)) #define writel(b,addr) (*(volatile unsigned int *) __io_virt(addr) = (b))
#define memset_io(a,b,c) memset(__io_virt(a),(b),(c)) #define memcpy_fromio(a,b,c) memcpy((a),__io_virt(b),(c)) #define memcpy_toio(a,b,c) memcpy(__io_virt(a),(b),(c)) |
最后,我们要特别强调驱动程序中mmap函数的实现方法。用mmap映射一个设备,意味着使用户空间的一段地址关联到设备内存上,这使得只要程序在分配的地址范围内进行读取或者写入,实际上就是对设备的访问。
笔者在Linux源代码中进行包含"ioremap"文本的搜索,发现真正出现的ioremap的地方相当少。所以笔者追根索源地寻找I/O操作的物理地址转换到虚拟地址的真实所在,发现Linux有替代ioremap的语句,但是这个转换过程却是不可或缺的。
譬如我们再次摘取S3C2410这个ARM芯片RTC(实时钟)驱动中的一小段:
static void get_rtc_time(int alm, struct rtc_time *rtc_tm) { spin_lock_irq(&rtc_lock); if (alm == 1) { rtc_tm->tm_year = (unsigned char)ALMYEAR & Msk_RTCYEAR; rtc_tm->tm_mon = (unsigned char)ALMMON & Msk_RTCMON; rtc_tm->tm_mday = (unsigned char)ALMDAY & Msk_RTCDAY; rtc_tm->tm_hour = (unsigned char)ALMHOUR & Msk_RTCHOUR; rtc_tm->tm_min = (unsigned char)ALMMIN & Msk_RTCMIN; rtc_tm->tm_sec = (unsigned char)ALMSEC & Msk_RTCSEC; } else { read_rtc_bcd_time: rtc_tm->tm_year = (unsigned char)BCDYEAR & Msk_RTCYEAR; rtc_tm->tm_mon = (unsigned char)BCDMON & Msk_RTCMON; rtc_tm->tm_mday = (unsigned char)BCDDAY & Msk_RTCDAY; rtc_tm->tm_hour = (unsigned char)BCDHOUR & Msk_RTCHOUR; rtc_tm->tm_min = (unsigned char)BCDMIN & Msk_RTCMIN; rtc_tm->tm_sec = (unsigned char)BCDSEC & Msk_RTCSEC;
if (rtc_tm->tm_sec == 0) { /* Re-read all BCD registers in case of BCDSEC is 0. See RTC section at the manual for more info. */ goto read_rtc_bcd_time; } } spin_unlock_irq(&rtc_lock);
BCD_TO_BIN(rtc_tm->tm_year); BCD_TO_BIN(rtc_tm->tm_mon); BCD_TO_BIN(rtc_tm->tm_mday); BCD_TO_BIN(rtc_tm->tm_hour); BCD_TO_BIN(rtc_tm->tm_min); BCD_TO_BIN(rtc_tm->tm_sec);
/* The epoch of tm_year is 1900 */ rtc_tm->tm_year += RTC_LEAP_YEAR - 1900;
/* tm_mon starts at 0, but rtc month starts at 1 */ rtc_tm->tm_mon--; } |
I/O操作似乎就是对ALMYEAR、ALMMON、ALMDAY定义的寄存器进行操作,那这些宏究竟定义为什么呢?
|