一、MTD系统架构

1.MTD设备体验

FLASH在嵌入式系统中是必不可少的,它是bootloader、linux内核和文件系统的最佳载体。

在Linux内核中引入了MTD子系统为NORFLASH和NAND FLASH设备提供统一的接口,从而使得FLASH驱动的设计大为简化。

  1. cat /proc/mtd

每个分区对应一个块设备

  1. ls -l /dev/mtd*
  2. crw-rw---- 1 0 0 90, 0 Jan 1 00:00 /dev/mtd0
  3. crw-rw---- 1 0 0 90, 1 Jan 1 00:00 /dev/mtd0ro
  4. crw-rw---- 1 0 0 90, 2 Jan 1 00:00 /dev/mtd1
  5. crw-rw---- 1 0 0 90, 3 Jan 1 00:00 /dev/mtd1ro
  6. crw-rw---- 1 0 0 90, 4 Jan 1 00:00 /dev/mtd2
  7. crw-rw---- 1 0 0 90, 5 Jan 1 00:00 /dev/mtd2ro
  8. brw-rw---- 1 0 0 31, 0 Jan 1 00:00 /dev/mtdblock0
  9. brw-rw---- 1 0 0 31, 1 Jan 1 00:00 /dev/mtdblock1
  10. brw-rw---- 1 0 0 31, 2 Jan 1 00:00 /dev/mtdblock2

2.块设备驱动系统架构

二、YAFFS2文件系统应用

1.MTD分区设置

配置linux内核支持mtd,找到mtd接口文件,设置空间大小。

2.Yaffs2文件系统制作

将rootfs格式化生成yaffs文件系统。

  1. /home/win/mkyaffs2image ./rootfs/ rootfs.img

3.Uboot参数设置

在uboot_tq2440\include\configs\TQ2440.h中有uboot的启动配置选项

  1. #define CONFIG_BZIP2
  2. #define CONFIG_LZO
  3. #define CONFIG_LZMA
  4. #define CONFIG_CMD_NAND_YAFFS
  5. #define CONFIG_BOOTARGS "console=ttySAC0 root=/dev/mtdblock3"
  6. #define CONFIG_BOOTCOMMAND "nand read 0x30000000 kernel;bootm 0x30000000"

4.下载烧写与启动

在uboot中用dnw下载



三、Nandflash驱动设计

s3c2410.c/s3c24xx_nand_probe:

  1. static int s3c24xx_nand_probe(struct platform_device *pdev,
  2. enum s3c_cpu_type cpu_type)
  3. {
  4. struct s3c2410_platform_nand *plat = to_nand_plat(pdev);
  5. struct s3c2410_nand_info *info;
  6. struct s3c2410_nand_mtd *nmtd;
  7. struct s3c2410_nand_set *sets;
  8. struct resource *res;
  9. int err = 0;
  10. int size;
  11. int nr_sets;
  12. int setno;
  13. pr_debug("s3c2410_nand_probe(%p)\n", pdev);
  14. info = kmalloc(sizeof(*info), GFP_KERNEL);
  15. if (info == NULL) {
  16. dev_err(&pdev->dev, "no memory for flash info\n");
  17. err = -ENOMEM;
  18. goto exit_error;
  19. }
  20. memset(info, 0, sizeof(*info));
  21. platform_set_drvdata(pdev, info);
  22. spin_lock_init(&info->controller.lock);
  23. init_waitqueue_head(&info->controller.wq);
  24. /* get the clock source and enable it */
  25. info->clk = clk_get(&pdev->dev, "nand");                                  //获取时钟,并使能
  26. if (IS_ERR(info->clk)) {
  27. dev_err(&pdev->dev, "failed to get clock\n");
  28. err = -ENOENT;
  29. goto exit_error;
  30. }
  31. clk_enable(info->clk);
  32. /* allocate and map the resource */
  33. /* currently we assume we have the one resource */
  34. res = pdev->resource;
  35. size = res->end - res->start + 1;
  36. info->area = request_mem_region(res->start, size, pdev->name);                         //地址转换
  37. if (info->area == NULL) {
  38. dev_err(&pdev->dev, "cannot reserve register region\n");
  39. err = -ENOENT;
  40. goto exit_error;
  41. }
  42. info->device = &pdev->dev;
  43. info->platform = plat;
  44. info->regs = ioremap(res->start, size);
  45. info->cpu_type = cpu_type;
  46. if (info->regs == NULL) {
  47. dev_err(&pdev->dev, "cannot reserve register region\n");
  48. err = -EIO;
  49. goto exit_error;
  50. }
  51. dev_dbg(&pdev->dev, "mapped registers at %p\n", info->regs);
  52. /* initialise the hardware */
  53. err = s3c2410_nand_inithw(info);                                                        //初始化硬件
  54. if (err != 0)
  55. goto exit_error;
  56. sets = (plat != NULL) ? plat->sets : NULL;
  57. nr_sets = (plat != NULL) ? plat->nr_sets : 1;
  58. info->mtd_count = nr_sets;
  59. /* allocate our information */
  60. size = nr_sets * sizeof(*info->mtds);
  61. info->mtds = kmalloc(size, GFP_KERNEL);
  62. if (info->mtds == NULL) {
  63. dev_err(&pdev->dev, "failed to allocate mtd storage\n");
  64. err = -ENOMEM;
  65. goto exit_error;
  66. }
  67. memset(info->mtds, 0, size);
  68. /* initialise all possible chips */
  69. nmtd = info->mtds;
  70. for (setno = 0; setno < nr_sets; setno++, nmtd++) {
  71. pr_debug("initialising set %d (%p, info %p)\n", setno, nmtd, info);
  72. s3c2410_nand_init_chip(info, nmtd, sets);                                               //里面有校验nandflash
  73. nmtd->scan_res = nand_scan_ident(&nmtd->mtd,                                            //搜索nandflash
  74. (sets) ? sets->nr_chips : 1);
  75. if (nmtd->scan_res == 0) {
  76. s3c2410_nand_update_chip(info, nmtd);
  77. nand_scan_tail(&nmtd->mtd);
  78. s3c2410_nand_add_partition(info, nmtd, sets);                                        //注册分区信息
  79. }
  80. if (sets != NULL)
  81. sets++;
  82. }
  83. err = s3c2410_nand_cpufreq_register(info);
  84. if (err < 0) {
  85. dev_err(&pdev->dev, "failed to init cpufreq support\n");
  86. goto exit_error;
  87. }
  88. if (allow_clk_stop(info)) {
  89. dev_info(&pdev->dev, "clock idle support enabled\n");
  90. clk_disable(info->clk);
  91. }
  92. pr_debug("initialised ok\n");
  93. return 0;
  94. exit_error:
  95. s3c2410_nand_remove(pdev);
  96. if (err == 0)
  97. err = -EINVAL;
  98. return err;
  99. }

MTD通用驱动部分nand_base.c(nand_read:

  1. static int nand_read(struct mtd_info *mtd, loff_t from, size_t len,
  2. size_t *retlen, uint8_t *buf)
  3. {
  4. struct nand_chip *chip = mtd->priv;
  5. int ret;
  6. /* Do not allow reads past end of device */
  7. if ((from + len) > mtd->size)
  8. return -EINVAL;
  9. if (!len)
  10. return 0;
  11. nand_get_device(chip, mtd, FL_READING);
  12. chip->ops.len = len;
  13. chip->ops.datbuf = buf;
  14. chip->ops.oobbuf = NULL;
  15. ret = nand_do_read_ops(mtd, from, &chip->ops);                                       //进行读操作的代码
  16. *retlen = chip->ops.retlen;
  17. nand_release_device(mtd);
  18. return ret;
  19. }

nand_do_read_ops:

  1. /**
  2. * nand_do_read_ops - [Internal] Read data with ECC
  3. *
  4. * @mtd:    MTD device structure
  5. * @from:    offset to read from
  6. * @ops:    oob ops structure
  7. *
  8. * Internal function. Called with chip held.
  9. */
  10. static int nand_do_read_ops(struct mtd_info *mtd, loff_t from,
  11. struct mtd_oob_ops *ops)
  12. {
  13. int chipnr, page, realpage, col, bytes, aligned;
  14. struct nand_chip *chip = mtd->priv;
  15. struct mtd_ecc_stats stats;
  16. int blkcheck = (1 << (chip->phys_erase_shift - chip->page_shift)) - 1;
  17. int sndcmd = 1;
  18. int ret = 0;
  19. uint32_t readlen = ops->len;
  20. uint32_t oobreadlen = ops->ooblen;
  21. uint8_t *bufpoi, *oob, *buf;
  22. stats = mtd->ecc_stats;
  23. chipnr = (int)(from >> chip->chip_shift);
  24. chip->select_chip(mtd, chipnr);
  25. realpage = (int)(from >> chip->page_shift);
  26. page = realpage & chip->pagemask;
  27. col = (int)(from & (mtd->writesize - 1));
  28. buf = ops->datbuf;
  29. oob = ops->oobbuf;
  30. while(1) {
  31. bytes = min(mtd->writesize - col, readlen);
  32. aligned = (bytes == mtd->writesize);
  33. /* Is the current page in the buffer ? */
  34. if (realpage != chip->pagebuf || oob) {
  35. bufpoi = aligned ? buf : chip->buffers->databuf;
  36. if (likely(sndcmd)) {
  37. chip->cmdfunc(mtd, NAND_CMD_READ0, 0x00, page);                                              //实际对应了nand_command_lp,cmd命令是0
  38. sndcmd = 0;
  39. }
  40. .........
  41. }

nand_command_lp:

  1. static void nand_command_lp(struct mtd_info *mtd, unsigned int command,
  2. int column, int page_addr)
  3. {
  4. register struct nand_chip *chip = mtd->priv;
  5. /* Emulate NAND_CMD_READOOB */
  6. if (command == NAND_CMD_READOOB) {
  7. column += mtd->writesize;
  8. command = NAND_CMD_READ0;
  9. }
  10. /* Command latch cycle */
  11. chip->cmd_ctrl(mtd, command & 0xff,
  12. NAND_NCE | NAND_CLE | NAND_CTRL_CHANGE);                                   //cmd_ctrl来源于底层驱动,在s3c2410_nand_init_chip中赋值了。
  13. .......
  14. }

s3c2410_nand_hwcontrol:

  1. /* s3c2410_nand_hwcontrol
  2. *
  3. * Issue command and address cycles to the chip
  4. */
  5. static void s3c2410_nand_hwcontrol(struct mtd_info *mtd, int cmd,
  6. unsigned int ctrl)
  7. {
  8. struct s3c2410_nand_info *info = s3c2410_nand_mtd_toinfo(mtd);
  9. if (cmd == NAND_CMD_NONE)
  10. return;
  11. if (ctrl & NAND_CLE)
  12. writeb(cmd, info->regs + S3C2410_NFCMD);                                     //往NFCONT寄存器中写入cmd,cmd来自于nand_command,往上回溯为nand_read.其实就是发送了命令0x00
  13. else
  14. writeb(cmd, info->regs + S3C2410_NFADDR);
  15. }

继续回到nand_command_lp:

  1. ...............
  2. if (column != -1 || page_addr != -1) {
  3. int ctrl = NAND_CTRL_CHANGE | NAND_NCE | NAND_ALE;
  4. /* Serially input address */
  5. if (column != -1) {
  6. /* Adjust columns for 16 bit buswidth */
  7. if (chip->options & NAND_BUSWIDTH_16)
  8. column >>= 1;
  9. chip->cmd_ctrl(mtd, column, ctrl);                                   //紧接着发送列地址
  10. ctrl &= ~NAND_CTRL_CHANGE;
  11. chip->cmd_ctrl(mtd, column >> 8, ctrl);
  12. }
  13. if (page_addr != -1) {
  14. chip->cmd_ctrl(mtd, page_addr, ctrl);                                //发送行地址
  15. chip->cmd_ctrl(mtd, page_addr >> 8,
  16. NAND_NCE | NAND_ALE);
  17. /* One more address cycle for devices > 128MiB */
  18. if (chip->chipsize > (128 << 20))
  19. chip->cmd_ctrl(mtd, page_addr >> 16,
  20. NAND_NCE | NAND_ALE);
  21. }
  22. }
  23. chip->cmd_ctrl(mtd, NAND_CMD_NONE, NAND_NCE | NAND_CTRL_CHANGE);
  24. /*
  25. * program and erase have their own busy handlers
  26. * status, sequential in, and deplete1 need no delay
  27. */
  28. switch (command) {
  29. case NAND_CMD_CACHEDPROG:
  30. case NAND_CMD_PAGEPROG:
  31. case NAND_CMD_ERASE1:
  32. case NAND_CMD_ERASE2:
  33. case NAND_CMD_SEQIN:
  34. case NAND_CMD_RNDIN:
  35. case NAND_CMD_STATUS:
  36. case NAND_CMD_DEPLETE1:
  37. return;
  38. /*
  39. * read error status commands require only a short delay
  40. */
  41. case NAND_CMD_STATUS_ERROR:
  42. case NAND_CMD_STATUS_ERROR0:
  43. case NAND_CMD_STATUS_ERROR1:
  44. case NAND_CMD_STATUS_ERROR2:
  45. case NAND_CMD_STATUS_ERROR3:
  46. udelay(chip->chip_delay);
  47. return;
  48. case NAND_CMD_RESET:
  49. if (chip->dev_ready)
  50. break;
  51. udelay(chip->chip_delay);
  52. chip->cmd_ctrl(mtd, NAND_CMD_STATUS,
  53. NAND_NCE | NAND_CLE | NAND_CTRL_CHANGE);
  54. chip->cmd_ctrl(mtd, NAND_CMD_NONE,
  55. NAND_NCE | NAND_CTRL_CHANGE);
  56. while (!(chip->read_byte(mtd) & NAND_STATUS_READY)) ;
  57. return;
  58. case NAND_CMD_RNDOUT:
  59. /* No ready / busy check necessary */
  60. chip->cmd_ctrl(mtd, NAND_CMD_RNDOUTSTART,
  61. NAND_NCE | NAND_CLE | NAND_CTRL_CHANGE);
  62. chip->cmd_ctrl(mtd, NAND_CMD_NONE,
  63. NAND_NCE | NAND_CTRL_CHANGE);
  64. return;
  65. case NAND_CMD_READ0:
  66. chip->cmd_ctrl(mtd, NAND_CMD_READSTART,                                                    //这里发送了0x30命令
  67. NAND_NCE | NAND_CLE | NAND_CTRL_CHANGE);
  68. chip->cmd_ctrl(mtd, NAND_CMD_NONE,
  69. NAND_NCE | NAND_CTRL_CHANGE);
  70. /* This applies to read commands */
  71. default:
  72. /*
  73. * If we don't have access to the busy pin, we apply the given
  74. * command delay
  75. */
  76. if (!chip->dev_ready) {
  77. udelay(chip->chip_delay);
  78. return;
  79. }
  80. }
  81. /* Apply this short delay always to ensure that we do wait tWB in
  82. * any case on any machine. */
  83. ndelay(100);
  84. nand_wait_ready(mtd);                                                                          //wait等待
  85. }

MTD系统架构和yaffs2使用、Nandflash驱动设计的更多相关文章

  1. [国嵌攻略][148][MTD系统架构]

    MTD设备概述 Flash在嵌入式系统中是必不可少的,它是bootloader.Linux内核和文件系统的最佳载体.在Linux内核中引入了MTD子系统为NOR Flash和Nand FLash设备提 ...

  2. Java开发架构篇《初识领域驱动设计DDD落地》

    作者:小傅哥 博客:https://bugstack.cn 沉淀.分享.成长,让自己和他人都能有所收获! 一.前言 DDD(Domain-Driven Design 领域驱动设计)是由Eric Eva ...

  3. [国嵌攻略][052][NandFlash驱动设计_读]

    NandFlash读数据方式 1.页读,读出页中主数据区的所有数据,提供页地址(行地址) 2.随机读,读出页中指定的存储单元的数据,提供页地址(行地址)和页内偏移(行地址) 代码编写 1.根据Nand ...

  4. [国嵌攻略][054][NandFlash驱动设计_写]

    Nand Flash支持按页写和随机写两种方式,在下面实现的是按页写.闪存在写数据时,只能写入1,不能写入0,所以写函数必须和擦除函数一起使用,并且擦除函数是按块擦除. /************** ...

  5. [.NET领域驱动设计实战系列]专题二:结合领域驱动设计的面向服务架构来搭建网上书店

    一.前言 在前面专题一中,我已经介绍了我写这系列文章的初衷了.由于dax.net中的DDD框架和Byteart Retail案例并没有对其形成过程做一步步分析,而是把整个DDD的实现案例展现给我们,这 ...

  6. IDDD 实现领域驱动设计-SOA、REST 和六边形架构

    上一篇:<IDDD 实现领域驱动设计-架构之经典分层> 阅读目录: SOA-面向服务架构 REST 与 RESTful 资源(Resources) 状态(State) 六边形架构 DDD ...

  7. 领域驱动设计(Domain Driven Design)参考架构详解

    摘要 本文将介绍领域驱动设计(Domain Driven Design)的官方参考架构,该架构分成了Interfaces.Applications和Domain三层以及包含各类基础设施的Infrast ...

  8. [转载]领域驱动设计(Domain Driven Design)参考架构详解

    摘要 本文将介绍领域驱动设计(Domain Driven Design)的官方参考架构,该架构分成了Interfaces.Applications和Domain三层以及包含各类基础设施的Infrast ...

  9. saas系统架构经验总结

    2B Saas系统最近几年都很火.很多创业公司都在尝试创建企业级别的应用 cRM, HR,销售, Desk Saas系统.很多Saas创业公司也拿了大额风投.毕竟Saas相对传统软件的优势非常明显. ...

随机推荐

  1. Juint test Case 的2种使用方式

    通常情况下,我们去测试一个类中的方法,首先是建立一个包,包中建立一个测试类,在建立测试类文件时,选择JUnit Test Case,如下: 建好之后写测试用例: 但是如果偏就想在编写方法的那个java ...

  2. ssm框架错误展示-1

    1.xmlParse错误+找不到bean ,一般会出现在导入别的地方拷来的项目时发生,报了以下错误: 查看自己的项目配置文件,发现spring的配置文件和sql映射xml文件都有个黄颜色的感叹号,鼠标 ...

  3. Flink State 有可能代替数据库吗?

    有状态的计算作为容错以及数据一致性的保证,是当今实时计算必不可少的特性之一,流行的实时计算引擎包括 Google Dataflow.Flink.Spark (Structure) Streaming. ...

  4. FPGA学习的一些误区

    转载自网络,作者不详. 我常年担任多个有关FPGA学习研讨的QQ群管理员,长期以来很多新入群的菜鸟们总是在重复的问一些非常简单但是又让新手困惑不解的问题.作为管理员经常要给这些菜鸟们普及基础知识,但是 ...

  5. oracle11g笔记

    安装 #!/bin/bash #安装oracle110203 pageDir="/opt/tools/oracle" bdFile="/tmp/bdFile.txt&qu ...

  6. poj-1021--2D-Nim--点阵图同构

    2D-Nim Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 4136   Accepted: 1882 Descriptio ...

  7. 转载:Linux下启动和关闭Weblogic(管理服务器+被管服务器)

    转载自:http://www.cnblogs.com/nick-huang/p/3834134.html  感谢! Weblogic的管理服务器和被管服务器的启动.关闭,偶尔会用到,却又不常用,导致需 ...

  8. PHP常用

    php.ini文件配置 时间配置PRC中国 date.timezone = PRC 文件上传file_uploads = On //支持http上传upload_tmp_dir = //临时文件保存路 ...

  9. 使用Bochs学习硬件原理

    什么是Bochs? 简单地说,Bochs是一款仿真软件,可以用软件的方式模拟硬件的工作.同类软件有Qemu,仿真软件与虚拟机(hypervisor)还不完全相同,仿真软件是完全软件模拟硬件,而虚拟机软 ...

  10. 在学react时候找不到static/js/bundle.js

    看如图上面bundle.js,我在项目中和配置文件中都没有找到这个JS文件,然后我就觉得很诧异,然后各种查找,终于找到一篇文章,在此记录一下 第一步:npm run start            ...