linux nand flash驱动编写
很长一段时间,nandflash都是嵌入式的标配产品。nandflash价格便宜,存储量大,适用于很多的场景。现在很普及的ssd,上面的存储模块其实也是由一块一块nandflash构成的。对于linux嵌入式来说,开始uboot的加载是硬件完成的,中期的kernel加载是由uboot中的nandflash驱动完成的,而后期的rootfs加载,这就要靠kernel自己来完成了。当然,这次还是以三星s3c芯片为例进行说明。
1、nandflash驱动在什么地方,可以从drviers/mtd/Makefile来看
obj-y+=chips/lpddr/maps/devices/nand/onenand/tests/
2、nand在mtd下面,是作为一个单独目录保存的,这时应该查看nand下的Kconfig
configMTD_NAND_S3C2410 tristate"NANDFlashsupportforSamsungS3CSoCs" dependsonARCH_S3C24XX||ARCH_S3C64XX help ThisenablestheNANDflashcontrollerontheS3C24xxandS3C64xx SoCs Noboardspecificsupportisdonebythisdriver,eachboard mustadvertiseaplatform_deviceforthedrivertoattach. configMTD_NAND_S3C2410_DEBUG bool"SamsungS3CNANDdriverdebug" dependsonMTD_NAND_S3C2410 help EnabledebuggingoftheS3CNANDdriver configMTD_NAND_S3C2410_CLKSTOP bool"SamsungS3CNANDIDLEclockstop" dependsonMTD_NAND_S3C2410 defaultn help StoptheclocktotheNANDcontrollerwhenthereisnochip selectedtosavepower.Thiswillmeanthereisasmalldelay whentheisNANDchipselectedorreleased,butwillsave approximately5mAofpowerwhenthereisnothinghappening.
3、不难发现,MTD_NAND_S3C2410才是那个真正的macro,尝试在Makefile找文件
obj-$(CONFIG_MTD_NAND_S3C2410)+=s3c2410.o
4、查看s3c2410.c文件,看看基本结构构成
staticstructplatform_drivers3c24xx_nand_driver={ .probe=s3c24xx_nand_probe, .remove=s3c24xx_nand_remove, .suspend=s3c24xx_nand_suspend, .resume=s3c24xx_nand_resume, .id_table=s3c24xx_driver_ids, .driver={ .name="s3c24xx-nand", .of_match_table=s3c24xx_nand_dt_ids, }, }; module_platform_driver(s3c24xx_nand_driver);
5、继续分析s3c24xx_nand_probe函数
s3c2410_nand_init_chip(info,nmtd,sets);
6、之所以从中摘出了s3c2410_nand_init_chip这个函数,是因为里面进行了函数注册
类似的函数还有s3c2410_nand_update_chip函数
chip->write_buf=s3c2410_nand_write_buf; chip->read_buf=s3c2410_nand_read_buf; chip->select_chip=s3c2410_nand_select_chip; chip->chip_delay=50; nand_set_controller_data(chip,nmtd); chip->options=set->options; chip->controller=&info->controller; switch(info->cpu_type){ caseTYPE_S3C2410: chip->IO_ADDR_W=regs+S3C2410_NFDATA; info->sel_reg=regs+S3C2410_NFCONF; info->sel_bit=S3C2410_NFCONF_nFCE; chip->cmd_ctrl=s3c2410_nand_hwcontrol; chip->dev_ready=s3c2410_nand_devready; break; caseTYPE_S3C2440: chip->IO_ADDR_W=regs+S3C2440_NFDATA; info->sel_reg=regs+S3C2440_NFCONT; info->sel_bit=S3C2440_NFCONT_nFCE; chip->cmd_ctrl=s3c2440_nand_hwcontrol; chip->dev_ready=s3c2440_nand_devready; chip->read_buf=s3c2440_nand_read_buf; chip->write_buf=s3c2440_nand_write_buf; break; caseTYPE_S3C2412: chip->IO_ADDR_W=regs+S3C2440_NFDATA; info->sel_reg=regs+S3C2440_NFCONT; info->sel_bit=S3C2412_NFCONT_nFCE0; chip->cmd_ctrl=s3c2440_nand_hwcontrol; chip->dev_ready=s3c2412_nand_devready; if(readl(regs+S3C2410_NFCONF)&S3C2412_NFCONF_NANDBOOT) dev_info(info->device,"SystembootedfromNAND\n"); break; }
7、抓住了函数接口,就找到了基本逻辑。
对于框架来说,它不关心你的代码如何实现。只要你按照它的接口写,就能让上层正常获得数据。platform、usb、pci这都是一种接口形式,具体实现还要按照各个具体功能模块来实现才行。
8、为什么我们都用s3c芯片进行举例
因为它用的场景最多,学习资料最全,对于新手来说,这会少很多麻烦。
9、这个驱动依赖的kernel版本是什么
这里最有的代码都是按照最新4.16的版本进行分析的,大家可以直接查看这里的地址。
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持毛票票。