欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  网络运营

linux nand flash驱动编写

程序员文章站 2023-10-13 09:45:11
很长一段时间,nand flash都是嵌入式的标配产品。nand flash价格便宜,存储量大,适用于很多的场景。现在很普及的ssd,上面的存储模块其实也是由一块一块nan...

很长一段时间,nand flash都是嵌入式的标配产品。nand flash价格便宜,存储量大,适用于很多的场景。现在很普及的ssd,上面的存储模块其实也是由一块一块nand flash构成的。对于linux嵌入式来说,开始uboot的加载是硬件完成的,中期的kernel加载是由uboot中的nand flash驱动完成的,而后期的rootfs加载,这就要靠kernel自己来完成了。当然,这次还是以三星s3c芯片为例进行说明。

1、nand flash驱动在什么地方,可以从drviers/mtd/makefile来看

obj-y    += chips/ lpddr/ maps/ devices/ nand/ onenand/ tests/ 

2、nand在mtd下面,是作为一个单独目录保存的,这时应该查看nand下的kconfig

config mtd_nand_s3c2410 
  tristate "nand flash support for samsung s3c socs" 
  depends on arch_s3c24xx || arch_s3c64xx 
  help 
   this enables the nand flash controller on the s3c24xx and s3c64xx 
   socs 
 
   no board specific support is done by this driver, each board 
   must advertise a platform_device for the driver to attach. 
 
config mtd_nand_s3c2410_debug 
  bool "samsung s3c nand driver debug" 
  depends on mtd_nand_s3c2410 
  help 
   enable debugging of the s3c nand driver 
 
config mtd_nand_s3c2410_clkstop 
  bool "samsung s3c nand idle clock stop" 
  depends on mtd_nand_s3c2410 
  default n 
  help 
   stop the clock to the nand controller when there is no chip 
   selected to save power. this will mean there is a small delay 
   when the is nand chip selected or released, but will save 
   approximately 5ma of power when there is nothing happening. 

3、不难发现,mtd_nand_s3c2410才是那个真正的macro,尝试在makefile找文件

obj-$(config_mtd_nand_s3c2410)   += s3c2410.o 

4、查看s3c2410.c文件,看看基本结构构成

static struct platform_driver s3c24xx_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) { 
case type_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; 
 
case type_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;  
case type_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, "system booted from nand\n");  
  break; 
} 

7、抓住了函数接口,就找到了基本逻辑。

对于框架来说,它不关心你的代码如何实现。只要你按照它的接口写,就能让上层正常获得数据。platform、usb、pci这都是一种接口形式,具体实现还要按照各个具体功能模块来实现才行。

8、为什么我们都用s3c芯片进行举例

因为它用的场景最多,学习资料最全,对于新手来说,这会少很多麻烦。

9、这个驱动依赖的kernel版本是什么

这里最有的代码都是按照最新4.16的版本进行分析的,大家可以直接查看这里的。

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。