ram_nor.c源码:
//参考: drivers\mtd\chips\map_ram.c
#include "linux/module.h"
#include "linux/types.h"
#include "linux/kernel.h"
#include "linux/sched.h"
#include "linux/init.h"
#include "asm/io.h"
#include "asm/byteorder.h"
#include "linux/errno.h"
#include "linux/slab.h"
#include "linux/delay.h"
#include "linux/interrupt.h"
#include "linux/reboot.h"
#include "linux/bitmap.h"
#include "linux/mtd/xip.h"
#include "linux/mtd/map.h"
#include "linux/mtd/mtd.h"
#include "linux/mtd/compatmac.h"
#include "linux/mtd/cfi.h"
#define RAM_BLOCK_SIZE (512*1024)
static unsigned char *ramnor_buf;
static struct mtd_info *ram_nor_mtd;
static int ram_nor_erase(struct mtd_info *mtd, struct erase_info *instr)
{
int i;
unsigned char *buf = ramnor_buf + instr->addr;
static int cnt = 0;
printk("%s : %d\n", __FUNCTION__, cnt++);
for (i = 0; i < instr->len; i++)
{
buf[i] = 0xff;
}
instr->state = MTD_ERASE_DONE;
mtd_erase_callback(instr);
return 0;
}
static int ram_nor_read(struct mtd_info *mtd, loff_t from, size_t len, size_t *retlen, u_char *buf)
{
static int cnt = 0;
printk("%s : %d\n", __FUNCTION__, cnt++);
printk("thread: pid = %d, name = %s\n", current->pid, current->comm);
memcpy(buf, ramnor_buf+from, len);
*retlen = len;
return 0;
}
static int ram_nor_write(struct mtd_info *mtd, loff_t to, size_t len, size_t *retlen, const u_char *buf)
{
static int cnt = 0;
printk("%s : %d\n", __FUNCTION__, cnt++);
memcpy(ramnor_buf+to, buf, len);
*retlen = len;
return 0;
}
static void ram_nor_sync_nop(struct mtd_info *mtd)
{
}
static int ram_nor_init(void)
{
ramnor_buf = kmalloc(RAM_BLOCK_SIZE, GFP_KERNEL);
memset(ramnor_buf, 0xff, RAM_BLOCK_SIZE);
// alloc/setup mtd_info
ram_nor_mtd = kzalloc(sizeof(*ram_nor_mtd), GFP_KERNEL);;
ram_nor_mtd->type = MTD_NORFLASH;
// Fill in the default mtd operations
ram_nor_mtd->erase = ram_nor_erase;
ram_nor_mtd->read = ram_nor_read;
ram_nor_mtd->write = ram_nor_write;
ram_nor_mtd->name = "ram_nor";
ram_nor_mtd->size = RAM_BLOCK_SIZE;
ram_nor_mtd->sync = ram_nor_sync_nop;
ram_nor_mtd->flags = MTD_CAP_RAM;
ram_nor_mtd->writesize = 1;
ram_nor_mtd->erasesize = 512;
add_mtd_device(ram_nor_mtd); // add_mtd_partitions
return 0;
}
static void ram_nor_exit(void)
{
del_mtd_device(ram_nor_mtd);
kfree(ram_nor_mtd);
kfree(ramnor_buf);
}
module_init(ram_nor_init);
module_exit(ram_nor_exit);
MODULE_LICENSE("GPL");
=====================================================================
Makefile文件:
all:
make -C /media/sda8/wei/linux-2.6.28_smdk6410 M=`pwd` modules
clean:
make -C /media/sda8/wei/linux-2.6.28_smdk6410 M=`pwd` clean
obj-m += ram_nor.o
=====================================================================
测试详情可以参考:“JZ2440之NOR FLASH驱动程序”
上一篇:OK6410默认u-boot启动参数
下一篇:OK6410块设备驱动之用内存模拟磁盘
推荐阅读最新更新时间:2024-03-16 16:15