[Linux]nor驱动程序

[Linux]nor驱动程序

个人博客https://aeneag.xyz/

艾恩凝

2021/7/3

introduction

nor flash也是mtd设备的一种,驱动和nand flash差不多,内核帮我们完成了nor操作协议层,内核提供的协议层接口是map_info结构体,新的nor驱动就只需要填充这个协议接口就可以了。

nor framework

nor.PNG

code

/*
* @author aen
* @date 2021/7/3
*/
#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 <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){
	/* map_info struct */
	s3c_nor_map = kzalloc(sizeof(struct map_info), GFP_KERNEL);;
	/* setting phys address size bankwidth virtual address*/
	s3c_nor_map->name = "s3c_nor";
	s3c_nor_map->phys = 0;
	s3c_nor_map->size = 0x1000000; /* >= NOR's size*/
	s3c_nor_map->bankwidth = 2;
	s3c_nor_map->virt = ioremap(s3c_nor_map->phys, s3c_nor_map->size);

	simple_map_init(s3c_nor_map);
	/* using nor flash protocol function*/
	printk("use cfi_probe\n");
	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*/
	add_mtd_partitions(s3c_nor_mtd, s3c_nor_parts, 2);
}


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_AUTHOR("https://aeneag.xyz"); 
MODULE_VERSION("0.1");
MODULE_DESCRIPTION("S3C2440 Mouse Driver");
MODULE_LICENSE("GPL");

end

懒到不想详细的写,大多数笔记在pad上 = =

艾恩凝

写于大连

2021/7/3

catalogue

[Linux]驱动系列目录


    


公众号'艾恩凝'
个人公众号
个人微信
个人微信
    吾心信其可行,
          则移山填海之难,
                  终有成功之日!
                                  ——孙文
    评论
    0 评论
avatar

取消