收藏 分销(赏)

linux内核MTD分区.docx

上传人:pc****0 文档编号:9009197 上传时间:2025-03-11 格式:DOCX 页数:30 大小:64.95KB
下载 相关 举报
linux内核MTD分区.docx_第1页
第1页 / 共30页
linux内核MTD分区.docx_第2页
第2页 / 共30页
点击查看更多>>
资源描述
MTD 设备驱动 和 NAND Flash 驱动程序分析 硬件环境: 飞凌OK6410,256MB DDR,2GB NAND Flash、   NAND Flash 型号:K9G8G08U9A   、     分析源码:Linux 2.6.36.2 内核源码。 一、 MTD 设备驱动。 1、先来简单介绍一下MTD 在Linux 系统中, 提供了MTD(Memory Technology Device , 内存技术设备)系统来建立 Flash 针对 Linux 的系统、抽象的接口, MTD 将文件系统 与 底层的Flash  存储器进行了隔离, 使 Flash 驱动工程师 无需关心Flash 作为字符设备和 块 设备与 LInux内核的接口。 2、在引入MTD 后Linux 系统中的Flash  设备及接口可分为4层, 从上到下依次是:设备节点、MTD 设备层、MTD原始设备层 和 硬件驱动层。  这 4 层的作用定义如下: 1-> 硬件驱动层: Flash 硬件驱动层负责 Flash 硬件设备的读、写、擦除, LInux MTD 设备的 NOR Flash 芯片驱动位于 drivers/mtd/chips 子目录下,  NAND Flash的驱动程序则 位于 drivers/mtd/nand 子目录下。 2->MTD 原始设备层: MTD原始设备层由两部分组成, 一部分是MTD 原始设备的通用代码, 另一部分是各个特定 Flash 的数据,例如分区。 3->MTD设备层: 基于MTD 原始设备,Linux 系统可以定义出 MTD 的块设备的结构(主设备号 31) 和 字符设备 (设备号 90) ,构成MTD 设备层, MTD 字符设备定义       在mtdchar.c 中实现,MTD 块设备则是定义在一个描述MTD 块设备的结构 mtdblk_dev ,并声明了一个名为 mtdblks 的指针数组,这个数组 中的每个mtdblk_dev 和 mtd_table 中的每一个mtd_info 一一对应。 4->设备节点: 通过mknod 在/dev 子目录下建立MTD字符设备节点 和 块设备节点,用户通过访问此此设备节点即可访问 MTD 字符设备和块设备。 3、分析Linux MTD 系统接口 mtd_info 结构体代码分析  此结构体定义在 ./include/linux/mtd/mtd.h 中 关键词词解析: XIP :XIP eXecute In Place,即芯片内执行,指应用程序可以直接在flash闪存内运行,不必再把代码读到系统RAM中。flash内执行 是指nor flash 不需要初始化,可以直接在flash内执行代码。但往往只执行部分代码,比如初始化RAM. OOB :Out Of Brower 传输层协议使用带外数据(out-of-band,OOB)来发送一些重要的数据,如果通信一方有重要的数据需要通知对方时,协议能够将这些数据 快速地发送到对方.为了发送这些数据 iovec-base : iovec 结构体基础。struct iovec定义了一个向量元素。通常,这个结构用作一个多元素的数组。对于每一个传输的元素,指针成员iov_base指向 一个缓冲区,这个缓冲区是存放的是readv所接收的数据或是writev将要发送的数据。成员iov_len在各种情况下分别确定了接收的最大长度以及实际写入的长度。 Sync : 函数, 函数说明:此函数负责将系统缓冲区的内容写回磁盘,以确保数据同步。 [cpp] view plaincopy 1. struct mtd_info {   2.     u_char type;     // 内存技术的类型   3.     uint32_t flags;  // 标志位   4.     uint64_t size;   // Total size of the MTD 、mtd 设备的大小   5.    6.     /* "Major" erase size for the device. Na茂ve users may take this  7.      * to be the only erase size available, or may use the more detailed  8.      * information below if they desire  9.      */   10.     uint32_t erasesize;    // 主要的擦除块大小 erase size of main block   11.     /* Minimal writable flash unit size. In case of NOR flash it is 1 (even  12.      * though individual bits can be cleared), in case of NAND flash it is  13.      * one NAND page (or half, or one-fourths of it), in case of ECC-ed NOR  14.      * it is of ECC block size, etc. It is illegal to have writesize = 0.  15.      * Any driver registering a struct mtd_info must ensure a writesize of  16.      * 1 or larger.  17.      */   18.     uint32_t writesize;           // 最小的可写单元的字节数   19.    20.     uint32_t oobsize;   // Amount of OOB data per block (e.g. 16) OOB 字节数   21.     uint32_t oobavail;  // Available OOB bytes per block   可用OBB 字节数   22.    23.     /*  24.      * If erasesize is a power of 2 then the shift is stored in  25.      * erasesize_shift otherwise erasesize_shift is zero. Ditto writesize.  26.      */   27.     unsigned int erasesize_shift;   28.     unsigned int writesize_shift;   29.     /* Masks based on erasesize_shift and writesize_shift */   30.     unsigned int erasesize_mask;   31.     unsigned int writesize_mask;   32.    33.     // Kernel-only stuff starts here.   34.     const char *name;   35.     int index;   36.    37.     /* ecc layout structure pointer - read only ! */   38.     struct nand_ecclayout *ecclayout;  // ECC 布局结构体指针   39.    40.     /* Data for variable erase regions. If numeraseregions is zero,  41.      * it means that the whole device has erasesize as given above.  42.      */    43.     int numeraseregions;              // 不同的erasesize 的区域   数目通常是1   44.     struct mtd_erase_region_info *eraseregions;   45.    46.     /*  47.      * Erase is an asynchronous operation.  Device drivers are supposed  48.      * to call instr->callback() whenever the operation completes, even  49.      * if it completes with a failure.  50.      * Callers are supposed to pass a callback function and wait for it  51.      * to be called before writing to the block.  52.      */   53.     int (*erase) (struct mtd_info *mtd, struct erase_info *instr);   54.    55.     /* This stuff for eXecute-In-Place */   56.     /* phys is optional and may be set to NULL */   57.     int (*point) (struct mtd_info *mtd, loff_t from, size_t len,            // 针对 eXecute-In- Place   58.             size_t *retlen, void **virt, resource_size_t *phys);   59.    60.     /* We probably shouldn't allow XIP if the unpoint isn't a NULL */   61.     void (*unpoint) (struct mtd_info *mtd, loff_t from, size_t len);        // 如果unpoint 为空,不允许 XIP   62.    63.     /* Allow NOMMU mmap() to directly map the device (if not NULL)  64.      * - return the address to which the offset maps  65.      * - return -ENOSYS to indicate refusal to do the mapping  66.      */   67.     unsigned long (*get_unmapped_area) (struct mtd_info *mtd,   68.                         unsigned long len,   69.                         unsigned long offset,   70.                         unsigned long flags);   71.    72.     /* Backing device capabilities for this device  73.      * - provides mmap capabilities  74.      */   75.     struct backing_dev_info *backing_dev_info;   76.    77.    78.     int (*read) (struct mtd_info *mtd, loff_t from, size_t len, size_t *retlen, u_char *buf);        // 读 flash   79.     int (*write) (struct mtd_info *mtd, loff_t to, size_t len, size_t *retlen, const u_char *buf);   // 写 flash   80.    81.     /* In blackbox flight recorder like scenarios we want to make successful  82.        writes in interrupt context. panic_write() is only intended to be  83.        called when its known the kernel is about to panic and we need the  84.        write to succeed. Since the kernel is not going to be running for much  85.        longer, this function can break locks and delay to ensure the write  86.        succeeds (but not sleep). */   87.    88.     int (*panic_write) (struct mtd_info *mtd, loff_t to, size_t len, size_t *retlen, const u_char *buf);   // Kernel panic 时序读写   89.    90.     int (*read_oob) (struct mtd_info *mtd, loff_t from,           // 读 out-of-band   91.              struct mtd_oob_ops *ops);   92.     int (*write_oob) (struct mtd_info *mtd, loff_t to,            // 写 out-of-band   93.              struct mtd_oob_ops *ops);   94.    95.     /*  96.      * Methods to access the protection register area, present in some  97.      * flash devices. The user data is one time programmable but the  98.      * factory data is read only.  99.      */   100.     int (*get_fact_prot_info) (struct mtd_info *mtd, struct otp_info *buf, size_t len);   101.     int (*read_fact_prot_reg) (struct mtd_info *mtd, loff_t from, size_t len, size_t *retlen, u_char *buf);   102.     int (*get_user_prot_info) (struct mtd_info *mtd, struct otp_info *buf, size_t len);   103.     int (*read_user_prot_reg) (struct mtd_info *mtd, loff_t from, size_t len, size_t *retlen, u_char *buf);   104.     int (*write_user_prot_reg) (struct mtd_info *mtd, loff_t from, size_t len, size_t *retlen, u_char *buf);   105.     int (*lock_user_prot_reg) (struct mtd_info *mtd, loff_t from, size_t len);   106.    107.     /* kvec-based read/write methods.  108.        NB: The 'count' parameter is the number of _vectors_, each of  109.        which contains an (ofs, len) tuple.  110.     */   111.     int (*writev) (struct mtd_info *mtd, const struct kvec *vecs, unsigned long count, loff_t to, size_t *retlen); // iovec-based 读写函数   112.    113.     /* Sync */     114.     void (*sync) (struct mtd_info *mtd);                             // Sync    115.    116.     /* Chip-supported device locking */   117.     int (*lock) (struct mtd_info *mtd, loff_t ofs, uint64_t len);    // 设备锁   118.     int (*unlock) (struct mtd_info *mtd, loff_t ofs, uint64_t len);   119.     int (*is_locked) (struct mtd_info *mtd, loff_t ofs, uint64_t len);   120.    121.     /* Power Management functions */    122.     int (*suspend) (struct mtd_info *mtd);                          // 电源管理函数   123.     void (*resume) (struct mtd_info *mtd);   124.    125.     /* Bad block management functions */   126.     int (*block_isbad) (struct mtd_info *mtd, loff_t ofs);          // 坏块管理函数   127.     int (*block_markbad) (struct mtd_info *mtd, loff_t ofs);   128.    129.     struct notifier_block reboot_notifier;  /* default mode before reboot */   130.    131.     /* ECC status information */   132.     struct mtd_ecc_stats ecc_stats;   133.     /* Subpage shift (NAND) */   134.     int subpage_sft;   135.    136.     void *priv;                                                   // 私有函数   137.    138.     struct module *owner;   139.     struct device dev;   140.     int usecount;   141.    142.     /* If the driver is something smart, like UBI, it may need to maintain  143.      * its own reference counting. The below functions are only for driver.  144.      * The driver may register its callbacks. These callbacks are not  145.      * supposed to be called by MTD users */   146.     int (*get_device) (struct mtd_info *mtd);   147.     void (*put_device) (struct mtd_info *mtd);   148. };   mtd_info 中的 read(). write(). read_oob(). write_oob(). erase() 是 MTD 设备驱动主要实现的函数。在在后面我将要介绍的nand flahs 驱动中几乎看不到mtd_info的成员函数(也就是说这些函数对于Flash 芯片来说是透明的),这是因为在Linux MTD 下层实现了针对 NOR、NAND Flsh 的同游mtd_info 成员函数。 Flash 驱动中使用如下两个函数来注册 和注销MTD 设备: int add_mtd_device(struct mtd_info *mtd); int del_mtd_device (struct mtd_info *mtd) 4、mtd_part 结构体分析     此结构定义在 ../drivers/mtd/mtdpart.c 中 [cpp] view plaincopy 1. struct mtd_part {   2.     struct mtd_info mtd;      // 分区的信息(大部分由其 master 决定)   3.     struct mtd_info *master;  // 该分区的主分区   4.     uint64_t offset;          // 分区的偏移地址   5.     struct list_head list;    // 分区号   6. };   mtd_part 结构体由于表示分区,它在mtd_info 结构体成员用于描述该分区, 它会被加入到 mtd_table (定义为 struct mtd_info *mtd_table[MAX_MTD_DEVICES])中, 其大部分成员有 mtd_part—> 决定,各种函数也指向主分区的相应函数。 5、mtd_partition 结构体分析    该结构体定义在 include/linux/mtd/partitions.h 中 [cpp] view plaincopy 1. struct mtd_partition {   2.     char *name;         /* identifier string */     // 标识字符串   3.     uint64_t size;          /* partition size */        // 分区大小   4.     uint64_t offset;        /* offset within the master MTD space */                              // 主MTD空间内的偏移量                         5.     uint32_t mask_flags;        /* master MTD flags to mask out for this partition */                 // 掩码标志   6.     struct nand_ecclayout *ecclayout;   /* out of band layout for this partition (NAND only)*/   7. };   mtd_partttion 会在MTD 原始设备层调用 add_mtd_partitons() 时传递分区信息,并通过下面两个函数进行注册和注销 分区: [cpp] view plaincopy 1. int add_mtd_partitions( struct mtd_info *master, struct mtd_partition *parts, int nbparts);   2. int del_mtd_partitions (struct mtd_info *master);   add_mtd_partitions() 会对每一个新建立的分区建立一个新的 mtd_part 结构体,将其加入 mtd_partitions 中,并调用 add_mtd_device () 将此分区作为 MTD 设备加入 mtd_tabe。 第二、NAND Flash 驱动程序分析: Linux 内核在 MTD的下层实现了通用NAND 驱动(主要通过 drivers/mtd/nand/nand_base.c 文件实现),因此芯片级的驱动不再需要实现 mtd_info 中的: read(), write() ,read_oob(), write_oob 等成员函数,而主体 转移到了 nand_chip 数据结构。 关键词解析: ALE:地址锁存使能(Address Latch Enable, ALE):当ALE为高时,在WE#信号的上升沿,地址被锁存到NAND地址寄存器中。 CLE:指令锁存使能(Command Latch Enable, CLE): 当CLE为高时,在WE#信号的上升沿,指令被锁存到NAND指令寄存器中。 BSP:Board Support Package 1、nand_chip 结构体分析       该结构体定义在 ./include/linux/mtd/nand.h [cpp] view plaincopy 1. struct nand_chip {   2.     void  __iomem   *IO_ADDR_R;               // 读 8 位 I/O 线地址,由主板决定   3.     void  __iomem   *IO_ADDR_W;               // 写 8 为 I/O 线地址,由主板决定    4.    5.     uint8_t     (*read_byte)(struct mtd_info *mtd);   6.     u16     (*read_word)(struct mtd_info *mtd);   7.     void        (*write_buf)(struct mtd_info *mtd, const uint8_t *buf, int len);   8.     void        (*read_buf)(struct mtd_info *mtd, uint8_t *buf, int len);   9.     int     (*verify_buf)(struct mtd_info *mtd, const uint8_t *buf, int len);   10.     void        (*select_chip)(struct mtd_info *mtd, int chip);                    // 片选芯片   11.     int     (*block_bad)(struct mtd_info *mtd, loff_t ofs, int getchip);       // 是否为坏块   12.     int     (*block_markbad)(struct mtd_info *mtd, loff_t ofs);                // 标记坏块   13.     void        (*cmd_ctrl)(struct mtd_info *mtd, int dat,   14.                     unsigned int ctrl);                                    // 控制 AEL/CLE/nCE, 也用于写命令 和 地址   15.     int     (*dev_ready)(struct mtd_info *mtd);                                // 设备就绪   16.     void        (*cmdfunc)(struct mtd_info *mtd, unsigned command, int column, int page_addr);   17.     int     (*waitfunc)(struct mtd_info *mtd, struct nand_chip *this);   18.     void        (*erase_cmd)(struct mtd_info *mtd, int page);   19.     int     (*scan_bbt)(struct mtd_info *mtd);   20.     int     (*errstat)(struct mtd_info *mtd, struct nand_chip *this, int state, int status, int page);   21.     int     (*write_page)(struct mtd_info *mtd, struct nand_chip *chip,   22.                       const uint8_t *buf, int page, int cached, int raw);   23.    24.     int     chip_delay;   25.     unsigned int    options;   26.    27.     int     page_shift;   28.     int     phys_erase_shift;   29.     int     bbt_erase_shift;   30.     int     chip_shift;   31.     int     numchips;   32.     uint64_t    chipsize;   33.     int     pagemask;   34.     int     pagebuf;   35.     int     subpagesize;   36.     uint8_t     cellinfo;   37.     int     badblockpos;   38.     int     badblockbits;   39.    40.     flstate_t   state;   41.    42.     uint8_t     *oob_poi;   43.     struct nand_hw_control  *controller;   44.     struct nand_ecclayout   *ecclayout;   45.    46.     struct nand_ecc_ctrl ecc;   47.     struct nand_buffers *buffers;   48.     struct nand_hw_control hwcontrol;   49.    50.     struct mtd_oob_ops ops;   51.    52.     uint8_t     *bbt;   53.     struct nand_bbt_descr   *bbt_td;   54.     struct nand_bbt_descr   *bbt_md;   55.    56.     struct nand_bbt_descr   *badblock_pattern;   57.    58.     void        *priv;   59. };   MTD 使用 nand_chip 数据结构表示一个NAND Flash 芯片,这个结构体中包含了关于 NAND Flash 的地址信息、读写方法、ECC模式、硬件控制等一系列底层机制。 2、S3C6410 nand_chip 初始化 与 NAND 探测 S3C6410 的 NAND 驱动以 platform 驱动的形式存在,在执行probe() 时,初始化nand_chip 实例并运行 nand_scan 扫描 NAND 设备, 最后调用 add_mtd_partitions() 添加主板中定义的分区表,nand_chip 是nanf flash 驱动的核心数据结构,这个结构体重的成员直接对应这 NAND Flash 的底层操作,针对具体情况的NAND 控制器情况分析 s3c_nand_probe 函数体对 S3C6410 nand_chip  的初始化和注册: 该文件定义在 drivers/mtd/nand/s3c_nand.c 中,而在基础内核2.6.36.2 中的这个文件为s3c2410.c 所以如果你想在 Linux 2.6.36.2 的内核对 S3C6410 nand flash 控制器进行支持的话,s3c_nand.c 这个文件需要自己添加。 [cpp] view plaincopy 1. static int s3c_nand_probe(struct platform_device *pdev, enum s3c_cpu_type cpu_type)   2. {      3.     struct s3c2410_platform_nand *plat = pdev->dev.platform_data;   4.     struct s3c2410_nand_set *sets;   5.     struct nand_chip *nand;   6.     struct resource *res; 
展开阅读全文

开通  VIP会员、SVIP会员  优惠大
下载10份以上建议开通VIP会员
下载20份以上建议开通SVIP会员


开通VIP      成为共赢上传
相似文档                                   自信AI助手自信AI助手

当前位置:首页 > 包罗万象 > 大杂烩

移动网页_全站_页脚广告1

关于我们      便捷服务       自信AI       AI导航        抽奖活动

©2010-2025 宁波自信网络信息技术有限公司  版权所有

客服电话:4009-655-100  投诉/维权电话:18658249818

gongan.png浙公网安备33021202000488号   

icp.png浙ICP备2021020529号-1  |  浙B2-20240490  

关注我们 :微信公众号    抖音    微博    LOFTER 

客服