×
单片机 > 单片机程序设计 > 详情

NOR FLASH驱动程序

发布时间:2020-08-28 发布时间:
|
//
// 参考 driversmtdmapsphysmap.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 "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. 设置: 物理基地址(phys), 大小(size), 位宽(bankwidth), 虚拟基地址(virt) //
        s3c_nor_map->name = "s3c_nor";
        s3c_nor_map->phys = 0;                  //nor启动的时候物理地址是0
        s3c_nor_map->size = 0x1000000;    // >= NOR的真正大小 //
        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);      //简单初始化
        // 3. 使用: 调用NOR FLASH协议层提供的函数来识别 //
        printk("use cfi_probe ");
        s3c_nor_mtd = do_map_probe("cfi_probe", s3c_nor_map);
        if (!s3c_nor_mtd)
        {
                printk("use jedec_probe ");
                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");
=============================================================
使用UBOOT体验NOR FLASH的操作(开发板设为NOR启动,进入UBOOT)
先使用OpenJTAG烧写UBOOT到NOR FLASH
 
1. 读数据
md.b 0 
 
2. 读ID
NOR手册上:
往地址555H写AAH
往地址2AAH写55H
往地址555H写90H
读0地址得到厂家ID: C2H
读1地址得到设备ID: 22DAH或225BH
退出读ID状态: 给任意地址写F0H
 
2440的A1接到NOR的A0,所以2440发出(555h<<1), NOR才能收到555h这个地址
UBOOT怎么操作?
 
往地址AAAH写AAH                      mw.w aaa aa
往地址554写55H                       mw.w 554 55
往地址AAAH写90H                      mw.w aaa 90
读0地址得到厂家ID: C2H               md.w 0 1
读2地址得到设备ID: 22DAH或225BH      md.w 2 1
退出读ID状态:                        mw.w 0 f0
 
3. NOR有两种规范, jedec, cfi(common flash interface)
   读取CFI信息
 
NOR手册:   
进入CFI模式    往55H写入98H
读数据:        读10H得到0051
               读11H得到0052
               读12H得到0059
               读27H得到容量
 
2440的A1接到NOR的A0,所以2440发出(555h<<1), NOR才能收到555h这个地址
UBOOT怎么操作?
进入CFI模式    往AAH写入98H            mw.w aa 98
读数据:        读20H得到0051           md.w 20 1
               读22H得到0052           md.w 22 1
               读24H得到0059           md.w 24 1
               读4EH得到容量           md.w 4e 1
               退出CFI模式             mw.w 0 f0
 
4. 写数据: 在地址0x100000写入0x1234
md.w 100000 1     // 得到ffff
mw.w 100000 1234
md.w 100000 1     // 还是ffff
 
NOR手册:
往地址555H写AAH 
往地址2AAH写55H 
往地址555H写A0H 
往地址PA写PD
 
2440的A1接到NOR的A0,所以2440发出(555h<<1), NOR才能收到555h这个地址
UBOOT怎么操作?
往地址AAAH写AAH               mw.w aaa aa
往地址554H写55H               mw.w 554 55
往地址AAAH写A0H               mw.w aaa a0
往地址0x100000写1234h         mw.w 100000 1234
 
 
NOR FLASH驱动程序框架
 
 
测试1:通过配置内核支持NOR FLASH
1. make menuconfig
-> Device Drivers
  -> Memory Technology Device (MTD) support
    -> Mapping drivers for chip access
    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                   //-j:表示格式化成jffs2,一般nor flash用jffs2,nand flash用yaffs
                                                                   //格式化的时候用字符设备mtd1
5. mount -t jffs2 /dev/mtdblock1 /mnt   //挂接的时候用块设备mtdblock1
   在/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     
 


『本文转载自网络,版权归原作者所有,如有侵权请联系删除』

热门文章 更多
浅谈msp430f5529入门(2)----时钟配置.例程分析