第二、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
- struct nand_chip {
- void __iomem *IO_ADDR_R; // 读 8 位 I/O 线地址,由主板决定
- void __iomem *IO_ADDR_W; // 写 8 为 I/O 线地址,由主板决定
- uint8_t (*read_byte)(struct mtd_info *mtd);
- u16 (*read_word)(struct mtd_info *mtd);
- void (*write_buf)(struct mtd_info *mtd, const uint8_t *buf, int len);
- void (*read_buf)(struct mtd_info *mtd, uint8_t *buf, int len);
- int (*verify_buf)(struct mtd_info *mtd, const uint8_t *buf, int len);
- void (*select_chip)(struct mtd_info *mtd, int chip); // 片选芯片
- int (*block_bad)(struct mtd_info *mtd, loff_t ofs, int getchip); // 是否为坏块
- int (*block_markbad)(struct mtd_info *mtd, loff_t ofs); // 标记坏块
- void (*cmd_ctrl)(struct mtd_info *mtd, int dat,
- unsigned int ctrl); // 控制 AEL/CLE/nCE, 也用于写命令 和 地址
- int (*dev_ready)(struct mtd_info *mtd); // 设备就绪
- void (*cmdfunc)(struct mtd_info *mtd, unsigned command, int column, int page_addr);
- int (*waitfunc)(struct mtd_info *mtd, struct nand_chip *this);
- void (*erase_cmd)(struct mtd_info *mtd, int page);
- int (*scan_bbt)(struct mtd_info *mtd);
- int (*errstat)(struct mtd_info *mtd, struct nand_chip *this, int state, int status, int page);
- int (*write_page)(struct mtd_info *mtd, struct nand_chip *chip,
- const uint8_t *buf, int page, int cached, int raw);
- int chip_delay;
- unsigned int options;
- int page_shift;
- int phys_erase_shift;
- int bbt_erase_shift;
- int chip_shift;
- int numchips;
- uint64_t chipsize;
- int pagemask;
- int pagebuf;
- int subpagesize;
- uint8_t cellinfo;
- int badblockpos;
- int badblockbits;
- flstate_t state;
- uint8_t *oob_poi;
- struct nand_hw_control *controller;
- struct nand_ecclayout *ecclayout;
- struct nand_ecc_ctrl ecc;
- struct nand_buffers *buffers;
- struct nand_hw_control hwcontrol;
- struct mtd_oob_ops ops;
- uint8_t *bbt;
- struct nand_bbt_descr *bbt_td;
- struct nand_bbt_descr *bbt_md;
- struct nand_bbt_descr *badblock_pattern;
- void *priv;
- };
MTD 使用 nand_chip 数据结构表示一个NAND Flash 芯片,这个结构体中包含了关于 NAND Flash 的地址信息、读写方法、ECC模式、硬件控制等一系列底层机制。
