首先我们来看代码:
/*
*参考drivers\mtd\maps\physmap.c
*/
#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 map_info *s3c_nor_map;
static struct mtd_info *s3c_nor_mtd;
static struct mtd_partition s3c_nor_parts[] = {
[0] = {
.name = "bootloader_nor",
.size = 0x00040000,
.offset = 0,
},
[1] = {
.name = "root_nor",
.offset = MTDPART_OFS_APPEND,
.size = MTDPART_SIZ_FULL,
}
};
static int __init nor_init(void)
{
/* 分配一个map_info结构体 */
s3c_nor_map = kzalloc(sizeof(struct map_info), GFP_KERNEL);
/* 设置上面分配的map_info结构体 */
s3c_nor_map->name = "s3c_nor";//设置名字
s3c_nor_map->phys = 0; //设置物理地址
s3c_nor_map->size = 0x1000000;//设置norflash大小
s3c_nor_map->bankwidth = 2;//设置位宽,我们还记得是16位的
s3c_nor_map->virt = ioremap(s3c_nor_map->phys, s3c_nor_map->size);//将物理地址映射为虚拟地址
simple_map_init(s3c_nor_map);//这里面设置了读写和擦除等函数
printk("use cfi_probe\n");
/* 调用NOR FLASH协议层提供的函数来识别,详见注释1 */
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;
}
/* 添加分区 */
add_mtd_partitions(s3c_nor_mtd, s3c_nor_parts, 2);
return 0;
}
static void __exit nor_exit(void)
{
del_mtd_partitions(s3c_nor_mtd);
iounmap(s3c_nor_map->virt);
kfree(s3c_nor_map);
}
module_init(nor_init);
module_exit(nor_exit);
MODULE_LICENSE("GPL");
注释1:
NOR FLASH识别过程:
do_map_probe("cfi_probe", s3c_nor_map);
drv = get_mtd_chip_driver(name)
ret = drv->probe(map); // cfi_probe.c
cfi_probe
mtd_do_chip_probe(map, &cfi_chip_probe);
cfi = genprobe_ident_chips(map, cp);
genprobe_new_chip(map, cp, &cfi)
cp->probe_chip(map, 0, NULL, cfi)
cfi_probe_chip
// 进入CFI模式
cfi_send_gen_cmd(0x98, 0x55, base, map, cfi, cfi->device_type, NULL);
// 看是否能读出"QRY"
qry_present(map,base,cfi)
.....
do_map_probe("jedec_probe", s3c_nor_map);
drv = get_mtd_chip_driver(name)
ret = drv->probe(map); // jedec_probe
jedec_probe
mtd_do_chip_probe(map, &jedec_chip_probe);
genprobe_ident_chips(map, cp);
genprobe_new_chip(map, cp, &cfi)
cp->probe_chip(map, 0, NULL, cfi)
jedec_probe_chip
// 解锁
cfi_send_gen_cmd(0xaa, cfi->addr_unlock1, base, map, cfi, cfi->device_type, NULL);
cfi_send_gen_cmd(0x55, cfi->addr_unlock2, base, map, cfi, cfi->device_type, NULL);
// 读ID命令
cfi_send_gen_cmd(0x90, cfi->addr_unlock1, base, map, cfi, cfi->device_type, NULL);
// 得到厂家ID,设备ID
cfi->mfr = jedec_read_mfr(map, base, cfi);
cfi->id = jedec_read_id(map, base, cfi);
// 和数组比较
jedec_table
分析:
我们先来看一下norflash块设备的整体框架图

块设备驱动主要是做一些优化工作
norflash协议层知道往某地址写某数据来实现识别、擦除和烧写等
norflash硬件操作就是用来实现一些硬件信息的设置,比如设置要地址、位宽什么的?以及设置读写函数。而我们写驱动程序要做的,就是实现这些最小的硬件差异,我们总结出来写norflash驱动程序的步骤:
(1)分配map_info结构体,并设置:物理地址,位宽,虚拟地址等
(1)分配map_info结构体,并设置:物理地址,位宽,虚拟地址等
(2)设置读写函数,用默认函数即可
(3)调用NOR FLASH协议层提供的函数来识别:do_map_probe
(4)添加分区:add_mtd_partitions
顺便我们也来说一下nandflash的情况:
nandflash协议知道发什么来读写擦除,读写,识别
nandflash硬件操作知道怎么发地址命令/地址
也就是说在硬件层里面我们把协议层里面的读写擦除的条件准备好。而我们要做的就是在硬件操作里面准备好这些条件:
(1)分配nand_chip结构体,并设置它:这里面就是设置一些芯片参数
(2)硬件相关设置:这里主要就是设备芯片,使其可以正常被读写擦除等
(3)使用nand_scan:来扫描并且设置读,写函数,这些函数是内核实现好的,属于协议层
(4)add_mtd_partitions:添加分区