Linux内核的Nand驱动流程分析
- s3c_nand_set_platdata(&mini2440_nand_info);
- /*NANDFlashonMINI2440board*/
- staticstructmtd_partitionmini2440_default_nand_part[]__initdata={
- [0]={
- .name="u-boot",
- .size=SZ_256K,
- .offset=0,
- },
- [1]={
- .name="u-boot-env",
- .size=SZ_128K,
- .offset=SZ_256K,
- },
- [2]={
- .name="kernel",
- /*5megabytes,forakernelwithnomodules
- *orauImagewitharamdiskattached*/
- .size=0x00500000,
- .offset=SZ_256K+SZ_128K,
- },
- [3]={
- .name="root",
- .offset=SZ_256K+SZ_128K+0x00500000,
- .size=MTDPART_SIZ_FULL,
- },
- };
- staticstructs3c2410_nand_setmini2440_nand_sets[]__initdata={
- [0]={
- .name="nand",
- .nr_chips=1,
- .nr_partitions=ARRAY_SIZE(mini2440_default_nand_part),
- .partitions=mini2440_default_nand_part,
- .flash_bbt=1,/*weuseu-boottocreateaBBT*/
- },
- };
- staticstructs3c2410_platform_nandmini2440_nand_info__initdata={
- .tacls=0,
- .twrph0=25,
- .twrph1=15,
- .nr_sets=ARRAY_SIZE(mini2440_nand_sets),
- .sets=mini2440_nand_sets,
- .ignore_unset_ecc=1,
- };
(1)初始化了chip中的各种操作函数指针并赋值给了nmtd->mtd.priv。
(2)初始化了info的sel_*成员,显然是Nand片选所用
(3)初始化了nmtd的几个成员
nmtd,info,set是该函数的三个参数,理解了这几个参数也就理解了这个函数的作用。info显然就是s3c24xx_nand_init中的s3c2410_nand_info,nmtd是info->mtds,而info->mtds是kzmalloc开辟的大小为size的内核空间,kzmalloc是kernel zero malloc,也就是开辟了size大小的空间清全部设置为0,也就是nmtds就是空的mtd数组,sets来就前面我定义的mini2440_nand_sets,这样三个参数都知道什么意思了,再去看代码就很简单了。(刚才去打了半小时电话,思路有点乱,不过大体上看了下,这个函数里面没有复杂的操作,相信大家很容易看懂)。
执行完s3c2410_nand_init之后就执行了nand_scan_ident,这是内核函数我就不做分析了,大家自己跟一下就可以知道,这个函数完成了nand_chip其他未指定函数指针的初始化,并获取了Nand的ID信息等,接下来又s3c2410_nand_update_chip,nand_scan_tail,s3c2410_nand_add_partitions,其中nand_scan_tail是通用的内核函数,而s3c2410_nand_update_chip是ecc相关的操作,我们只分析s3c2410_nand_add_partitions,从名字上讲,s3c2410开头的函数肯定不是内核通用函数,也就是说,这实际上是我们需要自行完成的函数,当然,也是可以借鉴的函数,追踪进入s3c2410_nand_add_partitions,看看内核是如何知道分区信息的。
- staticints3c2410_nand_add_partition(structs3c2410_nand_info*info,
- structs3c2410_nand_mtd*mtd,
- structs3c2410_nand_set*set)
- {
- if(set)
- mtd->mtd.name=set->name;
- returnmtd_device_parse_register(&mtd->mtd,NULL,NULL,
- set->partitions,set->nr_partitions);
- }
这个函数也很简单,仅设置了下mtd的nand然后就调用和mtd_core.c中的mtd_device_parse_register函数,从参数可以知道,该函数向内核注册了Nand分区信息。这样我们就基本上看完了Linux内核Nand驱动部分的结构。
在结尾之前我还要提到一个问题,就是内核驱动的匹配问题,在platform_device定义时内核指定的名称是s3c2410-nand
- structplatform_devices3c_device_nand={
- .name="s3c2410-nand",
- .id=-1,
- .num_resources=ARRAY_SIZE(s3c_nand_resource),
- .resource=s3c_nand_resource,
- };
- void__inits3c244x_map_io(void)
- {
- /*registerourio-tables*/
- iotable_init(s3c244x_iodesc,ARRAY_SIZE(s3c244x_iodesc));
- /*renameanyperipheralsuseddifferingfromthes3c2410*/
- s3c_device_sdi.name="s3c2440-sdi";
- s3c_device_i2c0.name="s3c2440-i2c";
- s3c_nand_setname("s3c2440-nand");
- s3c_device_ts.name="s3c2440-ts";
- s3c_device_usbgadget.name="s3c2440-usbgadget";
- }
- staticstructplatform_device_ids3c24xx_driver_ids[]={
- {
- .name="s3c2410-nand",
- .driver_data=TYPE_S3C2410,
- },{
- .name="s3c2440-nand",
- .driver_data=TYPE_S3C2440,
- },{
- .name="s3c2412-nand",
- .driver_data=TYPE_S3C2412,
- },{
- .name="s3c6400-nand",
- .driver_data=TYPE_S3C2412,/*compatiblewith2412*/
- },
- {}
- };
- structbus_typeplatform_bus_type={
- .name="platform",
- .dev_attrs=platform_dev_attrs,
- .match=platform_match,
- .uevent=platform_uevent,
- .pm=&platform_dev_pm_ops,
- };
- intplatform_driver_register(structplatform_driver*drv)
- {
- drv->driver.bus=&platform_bus_type;
- if(drv->probe)
- drv->driver.probe=platform_drv_probe;
- if(drv->remove)
- drv->driver.remove=platform_drv_remove;
- if(drv->shutdown)
- drv->driver.shutdown=platform_drv_shutdown;
- returndriver_register(&drv->driver);
- }
- staticintplatform_match(structdevice*dev,structdevice_driver*drv)
- {
- structplatform_device*pdev=to_platform_device(dev);
- structplatform_driver*pdrv=to_platform_driver(drv);
- /*AttemptanOFstylematchfirst*/
- if(of_driver_match_device(dev,drv))
- return1;
- /*Thentrytomatchagainsttheidtable*/
- if(pdrv->id_table)
- returnplatform_match_id(pdrv->id_table,pdev)!=NULL;
- /*fall-backtodrivernamematch*/
- return(strcmp(pdev->name,drv->name)==0);
- }
- staticconststructplatform_device_id*platform_match_id(
- conststructplatform_device_id*id,
- structplatform_device*pdev)
- {
- while(id->name[0]){
- if(strcmp(pdev->name,id->name)==0){
- pdev->id_entry=id;
- returnid;
- }
- id++;
- }
- returnNULL;
- }
- (1)定义resource,保证可以以物理地址方式正确访问Nand寄存器。(默认有)
- (2)定义platform_device,这是内核记录的硬件信息,要注册到内核设备列表。
- (3)定义mtd_partition,设置Nand分区。
- (4)定义s3c2410_nand_set,枚举所有Nand芯片信息。
- (5)定义s3c2410_platform_nand,这是驱动程序初始化Nand是需要的数据,包括分区信息的和芯片时序等必要信息。
- (6)将s3c2410_platform_nand赋值给mtd_device->dev.platform_data。
- (7)将Nand设备结构注册到设备列表。
评论