IO空間的靜態(tài)映射基本過程
IO端口與IO內(nèi)存的概念:外設(shè)中的寄存器稱為是IO端口,外設(shè)中的內(nèi)存稱為是IO內(nèi)存。二者統(tǒng)稱為IO空間。Linux內(nèi)核是通過虛擬地址訪問外設(shè)的。所以需要先將虛擬地址映射到相應(yīng)外設(shè)的物理地址上,linux的映射方式有兩種:靜態(tài)映射(map_desc)和動(dòng)態(tài)映射(ioremap),其實(shí)也是內(nèi)核訪問外設(shè)資源的方式。
2、靜態(tài)映射基本過程:在驅(qū)動(dòng)中配置寄存器,可以調(diào)用類似于s3c_gpio_cfgpin、s3c_gpio_setpull、gpio_direction_output等直接配置IO寄存器的函數(shù)。這些函數(shù)訪問的虛擬地址,這些虛擬地址都是已經(jīng)在啟動(dòng)啟動(dòng)的時(shí)候通過靜態(tài)映射方式映射到IO寄存器的物理地址上。
靜態(tài)映射概念:所謂的靜態(tài)映射是指,虛擬地址到物理地址的轉(zhuǎn)換所需要的頁表在操作系統(tǒng)啟動(dòng)時(shí)已經(jīng)配置好,不需要用戶進(jìn)行配置,虛擬地址到物理地址的查表轉(zhuǎn)換可直接完成(fromnet)。
以s5pv210為例講述:
MACHINE_START(SMDKV210,"SMDKV210")
/* Maintainer: Kukjin Kim
.boot_params =S5P_PA_SDRAM + 0x100,
.init_irq =s5pv210_init_irq,
.map_io = smdkv210_map_io,
.init_machine = smdkv210_machine_init,
#ifdefCONFIG_S5P_HIGH_RES_TIMERS
.timer = &s5p_systimer,
#else
.timer =&s5p_timer,
#endif
MACHINE_END
其中IO映射函數(shù)是smdkv210_map_io實(shí)現(xiàn):
staticvoid __init smdkv210_map_io(void)
{
s5p_init_io(NULL,0, S5P_VA_CHIPID);
s3c24xx_init_clocks(24000000);
s5pv210_gpiolib_init();
s3c24xx_init_uarts(smdkv210_uartcfgs,
ARRAY_SIZE(smdkv210_uartcfgs));
#ifndefCONFIG_S5P_HIGH_RES_TIMERS
s5p_set_timer_source(S5P_PWM2, S5P_PWM4);
#endif
s5p_reserve_bootmem(s5pv210_media_devs,
ARRAY_SIZE(s5pv210_media_devs),
S5P_RANGE_MFC);
}
該函數(shù)中調(diào)用s5p_init_io進(jìn)行地址的映射,
/*read cpu identification code */
void__inits5p_init_io(struct map_desc*mach_desc,
int size, void __iomem *cpuid_addr)
{
unsigned long idcode;
/* initialize the io descriptors we need forinitialization */
iotable_init(s5p_iodesc, ARRAY_SIZE(s5p_iodesc));
if (mach_desc)
iotable_init(mach_desc, size);
idcode = __raw_readl(cpuid_addr);
s3c_init_cpu(idcode,cpu_ids, ARRAY_SIZE(cpu_ids));
}
其中最重要是struct map_desc s5p_iodesc[]結(jié)構(gòu)體 和 staticstruct map_desc s5pv210_iodesc[]:
structmap_desc{
unsigned long virtual; // IO空間映射后的虛擬地址
unsigned long pfn; // IO空間的物理地址所在的頁幀號(hào)
unsigned long length; // IO空間的長(zhǎng)度
unsigned int type; // IO空間的類型
};
/*minimal IO mapping這部分是最小系統(tǒng)層次的映射*/
staticstruct map_desc s5p_iodesc[] __initdata = {
{
.virtual =(unsigned long)S5P_VA_CHIPID,
.pfn =__phys_to_pfn(S5P_PA_CHIPID),
.length =SZ_4K,
.type =MT_DEVICE,
}, {
.virtual = (unsigned long)S3C_VA_SYS,
.pfn = __phys_to_pfn(S5P_PA_SYSCON),
.length = SZ_64K,
.type = MT_DEVICE,
},{
.virtual =(unsigned long)S3C_VA_TIMER,
.pfn =__phys_to_pfn(S5P_PA_TIMER),
.length =SZ_16K,
.type =MT_DEVICE,
},{
.virtual =(unsigned long)S3C_VA_WATCHDOG,
.pfn =__phys_to_pfn(S3C_PA_WDT),
.length =SZ_4K,
.type =MT_DEVICE,
}, {
.virtual =(unsigned long)S5P_VA_SROMC,
.pfn =__phys_to_pfn(S5P_PA_SROMC),
.length =SZ_4K,
.type =MT_DEVICE,
},
};
/*Initial IO mappings*/
staticstruct map_desc s5pv210_iodesc[] __initdata = {
{
.virtual =(unsigned long)S5P_VA_SYSTIMER,
.pfn =__phys_to_pfn(S5PV210_PA_SYSTIMER),
.length =SZ_4K,
.type =MT_DEVICE,
}, {
.virtual =(unsigned long)S5P_VA_GPIO,
.pfn =__phys_to_pfn(S5PV210_PA_GPIO),
.length =SZ_4K,
.type =MT_DEVICE,
}, {
.virtual =(unsigned long)VA_VIC0,
.pfn =__phys_to_pfn(S5PV210_PA_VIC0),
.length =SZ_16K,
.type =MT_DEVICE,
}, {
.virtual =(unsigned long)VA_VIC1,
.pfn =__phys_to_pfn(S5PV210_PA_VIC1),
.length =SZ_16K,
.type =MT_DEVICE,
}, {
.virtual =(unsigned long)VA_VIC2,
.pfn =__phys_to_pfn(S5PV210_PA_VIC2),
.length =SZ_16K,
.type =MT_DEVICE,
}, {
.virtual =(unsigned long)VA_VIC3,
.pfn =__phys_to_pfn(S5PV210_PA_VIC3),
.length =SZ_16K,
.type =MT_DEVICE,
}, {
.virtual =(unsigned long)S3C_VA_UART,
.pfn =__phys_to_pfn(S3C_PA_UART),
.length =SZ_512K,
.type =MT_DEVICE,
}, {
.virtual =(unsigned long)S5P_VA_DMC0,
.pfn =__phys_to_pfn(S5PV210_PA_DMC0),
.length =SZ_4K,
.type =MT_DEVICE,
}, {
.virtual =(unsigned long)S5P_VA_DMC1,
.pfn =__phys_to_pfn(S5PV210_PA_DMC1),
.length =SZ_4K,
.type =MT_DEVICE,
},{
.virtual = (unsigned long)S5P_VA_BUS_AXI_DSYS,
.pfn = __phys_to_pfn(S5PV210_PA_BUS_AXI_DSYS),
.length= SZ_4K,
.type = MT_DEVICE,