本代码来自于韦东山老师嵌入式二期驱动视频所讲解,仅供学习参考。如有侵权等行为,可当即撤销本文章。更多信息请关注www.100ask.com!!!
#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 s3c_nor_init(void)
{
/* 1.分配一个map_info结构体 */
s3c_nor_map = kzalloc(sizeof(struct map_info), GFP_KERNEL);
/* 2.设置:物理基地址, 位宽, 大小, 虚拟基地址 */
s3c_nor_map->name = "s3c_nor";
s3c_nor_map->phys = 0;
s3c_nor_map->size = 0x1000000; /* >= NOR_FLASH的真正大小 */
s3c_nor_map->bankwidth = 2;
s3c_nor_map->virt = ioremap(s3c_nor_map->phys, s3c_nor_map->size);
simple_map_init(s3c_nor_map);
/* 3.调用NOR_FLASH函数协议层提供的函数来识别 */
s3c_nor_mtd = do_map_probe("cfi_probe", s3c_nor_map);
if(!s3c_nor_mtd)
{
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);
return 0;
}
static void s3c_nor_exit(void)
{
del_mtd_partitions(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");
测试1:通过配置内核支持NOR FLASH
1. make menuconfig
-> Device Drivers
-> Memory Technology Device (MTD) support
-> Mapping drivers for chip access
<M> CFI Flash device in physical memory map
(0x0) Physical start address of flash mapping // 物理基地址
(0x1000000) Physical length of flash mapping // 长度
(2) Bank width in octets (NEW) // 位宽
2. make modules
cp drivers/mtd/maps/physmap.ko /work/nfs_root/first_fs
3. 启动开发板
ls /dev/mtd*
insmod physmap.ko
ls /dev/mtd*
cat /proc/mtd
测试2: 使用自己写的驱动程序:
1. ls /dev/mtd*
2. insmod s3c_nor.ko
3. ls /dev/mtd*
4. 格式化: flash_eraseall -j /dev/mtd1
5. mount -t jffs2 /dev/mtdblock1 /mnt
在/mnt目录下操作文件
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