OK6410块设备驱动内存模拟nor_flash

发布者:Joyful222Life最新更新时间:2018-10-11 来源: eefocus关键字:OK6410  块设备  驱动内存  模拟nor_flash 手机看文章 扫描二维码
随时随地手机看文章

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  块设备  驱动内存  模拟nor_flash 引用地址:OK6410块设备驱动内存模拟nor_flash

上一篇:OK6410默认u-boot启动参数
下一篇:OK6410块设备驱动之用内存模拟磁盘

推荐阅读最新更新时间:2024-03-16 16:15

OK6410之LCD驱动
lcd.c驱动源码: #include linux/module.h #include linux/kernel.h #include linux/errno.h #include linux/string.h #include linux/mm.h #include linux/slab.h #include linux/vmalloc.h #include linux/delay.h #include linux/interrupt.h #include asm/uaccess.h #include linux/fb.h #include linux/init.h #includ
[单片机]
小广播
添点儿料...
无论热点新闻、行业分析、技术干货……
设计资源 培训 开发板 精华推荐

最新单片机文章
何立民专栏 单片机及嵌入式宝典

北京航空航天大学教授,20余年来致力于单片机与嵌入式系统推广工作。

换一换 更多 相关热搜器件
电子工程世界版权所有 京B2-20211791 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号 Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved