块设备驱动——Nor FLash

转载Nor Flash驱动详解

同nand-flash驱动框架,只需实现底层硬件相关的操作。
nor-flash驱动实现:

  1. 分配map_info结构体;
  2. 设置map_info结构体:物理基地址(phys)、大小(size)、位宽(bankwidth)、虚拟基地址(virt);
  3. 使用,调用do_map_probe(“cfi_probe”,s3c_nor_map)识别(cfi_probe/jedec_probe);
  4. 设置分区add_mtd_partitions。

程序源码


#include <linux/module.h>
#include <linux/types.h>
#include <linux/kernel.h>
#include <linux/init.h>
#include <linux/slab.h>
#include <linux/device.h>
#include <linux/platform_device.h>
#include <linux/mtd/mtd.h>
#include <linux/mtd/map.h>
#include <linux/mtd/partitions.h>
#include <linux/mtd/physmap.h>
#include <asm/io.h>


static struct mtd_info *s3c_nor_mtd;
static struct map_info *s3c_nor_map;

/*设置分区*/
static struct mtd_partition s3c_nor_parts[] = {
	[0] = {
        .name   = "bootloader",
        .size   = 0x00040000,//256k
		.offset	= 0,
	},
	[1] = {
        .name   = "root-nor",
        .offset = MTDPART_OFS_APPEND,
        .size   = MTDPART_SIZ_FULL,//剩下所有
	}
};

static int s3c_nor_init(void)
{	
	 /*1分配一个map_info结构体*/
	s3c_nor_map=kzalloc(sizeof(struct map_info), GFP_KERNEL);
	 
	 /*2设置:物理基地址(phys);大小(size);位宽(bankwidth);虚拟基地址(virt)*/
	s3c_nor_map->name ="s3c_nor";
	s3c_nor_map->phys = 0;
	s3c_nor_map->size = 0x1000000;	//>=norflash的真正大小
	s3c_nor_map->bankwidth=2;		//16/8
	s3c_nor_map->virt = ioremap(s3c_nor_map->phys, s3c_nor_map->size);
	
	simple_map_init(s3c_nor_map);//初始化
	
	 /*3调用norflash协议层提供的函数识别*/
	s3c_nor_mtd=do_map_probe("cfi_probe",s3c_nor_map);
	 if (!s3c_nor_mtd)
	{
		printk("use jedec_probe\n");
		s3c_nor_mtd = do_map_probe("jedec_probe", s3c_nor_map);
	}
	if (!s3c_nor_mtd)
	{		
		iounmap(s3c_nor_map->virt);
		kfree(s3c_nor_map);
		return -EIO;
	}
	
	/*4添加分区,add_mtd_partitions*/
	//add_mtd_partitions(s3c_nor_mtd,s3c_nor_parts,2);
	mtd_device_register(s3c_nor_mtd, s3c_nor_parts, 2);
	
	 return 0;
}

static void s3c_nor_exit(void)
{  
	//del_mtd_partitions(s3c_nor_mtd);
	mtd_device_unregister(s3c_nor_mtd);
	iounmap(s3c_nor_map->virt);
	kfree(s3c_nor_map);
}

module_init(s3c_nor_init);//入口函数
module_exit(s3c_nor_exit);//出口函数


MODULE_LICENSE("GPL");


数据存放
在这里插入图片描述

内存地址线错开原因:
在这里插入图片描述
32位的SDRAM错开两位;16位的nor-flash错开一位。

猜你喜欢

转载自blog.csdn.net/weixin_43542305/article/details/86375362
今日推荐