Nandflash底层驱动编写与验证

平台:MINI2440

在目录drivers\mtd\nand\下创建空的C文件mynand.c

mynand.c:

#include <linux/module.h>
#include <linux/types.h>
#include <linux/init.h>
#include <linux/kernel.h>
#include <linux/string.h>
#include <linux/ioport.h>
#include <linux/platform_device.h>
#include <linux/delay.h>
#include <linux/err.h>
#include <linux/slab.h>
#include <linux/clk.h>
#include <linux/mtd/mtd.h>
#include <linux/mtd/nand.h>
#include <linux/mtd/nand_ecc.h>
#include <linux/mtd/partitions.h>
#include <asm/io.h>
#include <asm/arch/regs-nand.h>
#include <asm/arch/nand.h>

struct s3c_nand_regs {
	unsigned long nfconf  ;
	unsigned long nfcont  ;
	unsigned long nfcmd   ;
	unsigned long nfaddr  ;
	unsigned long nfdata  ;
	unsigned long nfeccd0 ;
	unsigned long nfeccd1 ;
	unsigned long nfeccd  ;           //Nandflash寄存器
	unsigned long nfstat  ;
	unsigned long nfestat0;
	unsigned long nfestat1;
	unsigned long nfmecc0 ;
	unsigned long nfmecc1 ;
	unsigned long nfsecc  ;
	unsigned long nfsblk  ;
	unsigned long nfeblk  ;
};

static struct nand_chip      *s3c_nand;
static struct mtd_info       *s3c_mtd;
static struct s3c_nand_regs  *s3c_nand_regs;

static struct mtd_partition s3c_nand_parts[] = {   
	[0] = {
		.name	= "bootloader",       //<span style="font-family: Arial, Helvetica, sans-serif;">mtd_partition </span>记录当前Nandflash的分区有几个,名字,大小,偏移量
		.size	= 0x00040000,
		.offset	= 0,
	},
	[1] = {
		.name	= "param",            //启动参数
		.offset = 0x00040000,
		.size	= 0x00020000,
	},
	[2] = {
		.name	= "Kernel",           //内核
		.offset = 0x00060000,
		.size	= 0x00500000,
	},
	[3] = {    //noinitrd root=/dev/mtdblock3 init=/linuxrc console=ttySAC0
		.name	= "root",    //文件系统的mtd分区一定要和它的实际位置一致,这里对应mtdblock3
		.offset = 0x00560000,
		.size	= 1024 * 1024 * 1024, 
	},
	[4] = {
		.name	= "nand",             //可供用户使用
		.offset = 0x00000000,
		.size	= 1024 * 1024 * 1024,
	}
};

static void s3c2440_select_chip(struct mtd_info *mtd, int chipnr)
{
	if (chipnr == -1)
	{
		//不选中
		s3c_nand_regs->nfcont |= (1<<1);		
	}
	else
	{
		//选中
		s3c_nand_regs->nfcont &= ~(1<<1);
	}
}

static void s3c2440_cmd_ctrl(struct mtd_info *mtd, int dat, unsigned int ctrl)
{
	if (ctrl & NAND_CLE)
	{
		//发送命令
		s3c_nand_regs->nfcmd = dat;
	}
	else
	{
		//发送地址
		s3c_nand_regs->nfaddr = dat;
	}
}

static int s3c2440_dev_ready(struct mtd_info *mtd)
{
	return (s3c_nand_regs->nfstat & (1<<0));
}

static int s3c_nand_init(void)
{
	struct clk *clk;
	
	//分配nand_chip结构体 
	s3c_nand = kzalloc(sizeof(struct nand_chip), GFP_KERNEL);

	s3c_nand_regs = ioremap(0x4E000000, sizeof(struct s3c_nand_regs));
	
	//设置nand_chip
	s3c_nand->select_chip = s3c2440_select_chip;
	s3c_nand->cmd_ctrl    = s3c2440_cmd_ctrl;
	s3c_nand->IO_ADDR_R   = &s3c_nand_regs->nfdata;
	s3c_nand->IO_ADDR_W   = &s3c_nand_regs->nfdata;
	s3c_nand->dev_ready   = s3c2440_dev_ready;
	s3c_nand->ecc.mode    = NAND_ECC_SOFT;
	
	
	//使能NAND控制器时钟
	clk = clk_get(NULL, "nand");
	clk_enable(clk);              
	
    //查看nandflash手册
    #define TACLS    0
    #define TWRPH0   1
    #define TWRPH1   0
	s3c_nand_regs->nfconf = (TACLS<<12) | (TWRPH0<<8) | (TWRPH1<<4);
	s3c_nand_regs->nfcont = (1<<1) | (1<<0);
	
	s3c_mtd = kzalloc(sizeof(struct mtd_info), GFP_KERNEL);
	s3c_mtd->owner = THIS_MODULE;
	s3c_mtd->priv  = s3c_nand;
	
	nand_scan(s3c_mtd, 1);  //nand_scan会将我们写的对于芯片的读写函数重载到nand_chip结构中去,并把mtd_info结构体中的函数用nand的函数来重载,实现mtd到 底层驱动之间的联系,并且在nand_scan函数中通过读取芯片的设备号和厂家号自动在芯片列表中寻找相应的型号和参数,并将它注册
	add_mtd_partitions(s3c_mtd, s3c_nand_parts, 4);//注册设备函数。如果不分区,使用add_mtd_device函数,在mtdcore.c里实现
	return 0;
}

static void s3c_nand_exit(void)
{
	del_mtd_partitions(s3c_mtd);
	kfree(s3c_mtd);
	iounmap(s3c_nand_regs);
	kfree(s3c_nand);
}

module_init(s3c_nand_init);
module_exit(s3c_nand_exit);
MODULE_LICENSE("GPL");

将该驱动编译到内核,在drivers\mtd\nand\下编辑Makefile

添加:

obj-$(CONFIG_MTD_NAND_TEST) += mynand.o

编辑drivers\mtd\nand\下Kconfig:

添加:

config MTD_NAND_TEST
tristate "NAND Flash TEST"
depends on ARCH_S3C2410 || ARCH_S3C64XX

配置内核:

Nandflash底层驱动编写与验证_第1张图片


Nandflash底层驱动编写与验证_第2张图片

Nandflash底层驱动编写与验证_第3张图片

注释掉默认配置:NAND Flash support for Samsung S3C SoCs

选上NAND Flash TEST,保存。

编译生成zImage后烧写到板子,如果系统启动过程中,挂载文件系统不正常,就要检查mtd分区“root”分区与文件系统实际烧写位置是否一致。

你可能感兴趣的:(linux,验证,编写,驱动,NANDflash)