CubieBoard中文论坛

 找回密码
 立即注册
搜索
热搜: unable
查看: 7369|回复: 7

请问有没有人开发并启动成功过yaffs2文件系统呢?

[复制链接]
发表于 2014-2-17 11:58:05 | 显示全部楼层 |阅读模式

我最近在移植yaffs遇到了问题,没能跑起来。

yaffs2文件系统我从http://www.aleph1.co.uk/gitweb?p=yaffs2.git;a=summary上下载与内核对应的版本,编译通过了。只是基于MTD的nand驱动不会移植。

yaffs2系统基于CONFIG_MTD_BLOCK,但cubieboard2好像没有提供基于MTD_BOLCK的nand驱动。

麻烦各位大神帮忙和指点一下!!!谢谢。。。

回复

使用道具 举报

发表于 2014-2-18 12:10:05 | 显示全部楼层
sunxi的基于mtd的nand驱动在下面的链接中可以找到patch,但是需要clk,dma,dt等的依赖,不过都能找到,你可以试试
http://patchwork.ozlabs.org/project/linux-mtd/list/
回复 支持 反对

使用道具 举报

 楼主| 发表于 2014-2-19 19:01:28 | 显示全部楼层
trickwong,你好
回复 支持 反对

使用道具 举报

 楼主| 发表于 2014-2-19 19:15:46 | 显示全部楼层
trickwong,你好,
      我在你提供的网址上找到了sunxi基于mtd的nand驱动的patch,但发现patch里有几个函数和结构体在我从cubieboard2官网下载的3.3.0版本内核里找不到。请问那里可以下载这个patch对应的内核的完整代码呢?(以下是patch代码)
  1. /*
  2. * Copyright (C) 2013 Boris BREZILLON <b.brezillon.dev@gmail.com>
  3. *
  4. * Derived from:
  5. *        https://github.com/yuq/sunxi-nfc-mtd
  6. *        Copyright (C) 2013 Qiang Yu <yuq825@gmail.com>
  7. *
  8. *        https://github.com/hno/Allwinner-Info
  9. *        Copyright (C) 2013 Henrik Nordstr枚m <Henrik Nordstr枚m>
  10. *
  11. *        Copyright (C) 2013 Dmitriy B. <rzk333@gmail.com>
  12. *        Copyright (C) 2013 Sergey Lapin <slapin@ossfans.org>
  13. *
  14. * This program is free software; you can redistribute it and/or modify
  15. * it under the terms of the GNU General Public License as published by
  16. * the Free Software Foundation; either version 2 of the License, or
  17. * (at your option) any later version.
  18. *
  19. * This program is distributed in the hope that it will be useful,
  20. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  21. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  22. * GNU General Public License for more details.
  23. */

  24. #include <linux/dma-mapping.h>
  25. #include <linux/slab.h>
  26. #include <linux/module.h>
  27. #include <linux/moduleparam.h>
  28. #include <linux/platform_device.h>
  29. #include <linux/of.h>
  30. #include <linux/of_device.h>
  31. #include <linux/of_gpio.h>
  32. #include <linux/of_mtd.h>
  33. #include <linux/mtd/mtd.h>
  34. #include <linux/mtd/nand.h>
  35. #include <linux/mtd/partitions.h>
  36. #include <linux/clk.h>
  37. #include <linux/delay.h>
  38. #include <linux/dmaengine.h>
  39. #include <linux/gpio.h>
  40. #include <linux/interrupt.h>
  41. #include <linux/io.h>

  42. #define NFC_REG_CTL                0x0000
  43. #define NFC_REG_ST                0x0004
  44. #define NFC_REG_INT                0x0008
  45. #define NFC_REG_TIMING_CTL        0x000C
  46. #define NFC_REG_TIMING_CFG        0x0010
  47. #define NFC_REG_ADDR_LOW        0x0014
  48. #define NFC_REG_ADDR_HIGH        0x0018
  49. #define NFC_REG_SECTOR_NUM        0x001C
  50. #define NFC_REG_CNT                0x0020
  51. #define NFC_REG_CMD                0x0024
  52. #define NFC_REG_RCMD_SET        0x0028
  53. #define NFC_REG_WCMD_SET        0x002C
  54. #define NFC_REG_IO_DATA                0x0030
  55. #define NFC_REG_ECC_CTL                0x0034
  56. #define NFC_REG_ECC_ST                0x0038
  57. #define NFC_REG_DEBUG                0x003C
  58. #define NFC_REG_ECC_CNT0        0x0040
  59. #define NFC_REG_ECC_CNT1        0x0044
  60. #define NFC_REG_ECC_CNT2        0x0048
  61. #define NFC_REG_ECC_CNT3        0x004c
  62. #define NFC_REG_USER_DATA_BASE        0x0050
  63. #define NFC_REG_SPARE_AREA        0x00A0
  64. #define NFC_RAM0_BASE                0x0400
  65. #define NFC_RAM1_BASE                0x0800

  66. /*define bit use in NFC_CTL*/
  67. #define NFC_EN                                (1 << 0)
  68. #define NFC_RESET                        (1 << 1)
  69. #define NFC_BUS_WIDYH                        (1 << 2)
  70. #define NFC_RB_SEL                        (1 << 3)
  71. #define NFC_CE_SEL                        (7 << 24)
  72. #define NFC_CE_CTL                        (1 << 6)
  73. #define NFC_CE_CTL1                        (1 << 7)
  74. #define NFC_PAGE_SIZE                        (0xf << 8)
  75. #define NFC_SAM                                (1 << 12)
  76. #define NFC_RAM_METHOD                        (1 << 14)
  77. #define NFC_DEBUG_CTL                        (1 << 31)

  78. /*define bit use in NFC_ST*/
  79. #define NFC_RB_B2R                        (1 << 0)
  80. #define NFC_CMD_INT_FLAG                (1 << 1)
  81. #define NFC_DMA_INT_FLAG                (1 << 2)
  82. #define NFC_CMD_FIFO_STATUS                (1 << 3)
  83. #define NFC_STA                                (1 << 4)
  84. #define NFC_NATCH_INT_FLAG                (1 << 5)
  85. #define NFC_RB_STATE0                        (1 << 8)
  86. #define NFC_RB_STATE1                        (1 << 9)
  87. #define NFC_RB_STATE2                        (1 << 10)
  88. #define NFC_RB_STATE3                        (1 << 11)

  89. /*define bit use in NFC_INT*/
  90. #define NFC_B2R_INT_ENABLE                (1 << 0)
  91. #define NFC_CMD_INT_ENABLE                (1 << 1)
  92. #define NFC_DMA_INT_ENABLE                (1 << 2)
  93. #define NFC_INT_MASK                        (NFC_B2R_INT_ENABLE | \
  94.                                          NFC_CMD_INT_ENABLE | \
  95.                                          NFC_DMA_INT_ENABLE)


  96. /*define bit use in NFC_CMD*/
  97. #define NFC_CMD_LOW_BYTE                (0xff << 0)
  98. #define NFC_CMD_HIGH_BYTE                (0xff << 8)
  99. #define NFC_ADR_NUM                        (0x7 << 16)
  100. #define NFC_SEND_ADR                        (1 << 19)
  101. #define NFC_ACCESS_DIR                        (1 << 20)
  102. #define NFC_DATA_TRANS                        (1 << 21)
  103. #define NFC_SEND_CMD1                        (1 << 22)
  104. #define NFC_WAIT_FLAG                        (1 << 23)
  105. #define NFC_SEND_CMD2                        (1 << 24)
  106. #define NFC_SEQ                                (1 << 25)
  107. #define NFC_DATA_SWAP_METHOD                (1 << 26)
  108. #define NFC_ROW_AUTO_INC                (1 << 27)
  109. #define NFC_SEND_CMD3                        (1 << 28)
  110. #define NFC_SEND_CMD4                        (1 << 29)
  111. #define NFC_CMD_TYPE                        (3 << 30)

  112. /* define bit use in NFC_RCMD_SET*/
  113. #define NFC_READ_CMD                        (0xff << 0)
  114. #define NFC_RANDOM_READ_CMD0                (0xff << 8)
  115. #define NFC_RANDOM_READ_CMD1                (0xff << 16)

  116. /*define bit use in NFC_WCMD_SET*/
  117. #define NFC_PROGRAM_CMD                        (0xff << 0)
  118. #define NFC_RANDOM_WRITE_CMD                (0xff << 8)
  119. #define NFC_READ_CMD0                        (0xff << 16)
  120. #define NFC_READ_CMD1                        (0xff << 24)

  121. /*define bit use in NFC_ECC_CTL*/
  122. #define NFC_ECC_EN                        (1 << 0)
  123. #define NFC_ECC_PIPELINE                (1 << 3)
  124. #define NFC_ECC_EXCEPTION                (1 << 4)
  125. #define NFC_ECC_BLOCK_SIZE                (1 << 5)
  126. #define NFC_RANDOM_EN                        (1 << 9)
  127. #define NFC_RANDOM_DIRECTION                (1 << 10)
  128. #define NFC_ECC_MODE_SHIFT                12
  129. #define NFC_ECC_MODE                        (0xf << NFC_ECC_MODE_SHIFT)
  130. #define NFC_RANDOM_SEED                        (0x7fff << 16)



  131. enum sunxi_nand_rb_type {
  132.         RB_NONE,
  133.         RB_NATIVE,
  134.         RB_GPIO,
  135. };

  136. struct sunxi_nand_rb {
  137.         enum sunxi_nand_rb_type type;
  138.         union {
  139.                 int gpio;
  140.                 int nativeid;
  141.         } info;
  142. };

  143. struct sunxi_nand_chip_sel {
  144.         u8 cs;
  145.         struct sunxi_nand_rb rb;
  146. };

  147. #define DEFAULT_NAME_FORMAT        "nand@%d"
  148. #define MAX_NAME_SIZE                (sizeof("nand@") + 2)

  149. struct sunxi_nand_hw_ecc {
  150.         int mode;
  151.         struct nand_ecclayout layout;
  152. };

  153. struct sunxi_nand_chip {
  154.         struct list_head node;
  155.         struct nand_chip nand;
  156.         struct mtd_info mtd;
  157.         char default_name[MAX_NAME_SIZE];
  158.         unsigned long clk_rate;
  159.         int selected;
  160.         int nsels;
  161.         struct sunxi_nand_chip_sel sels[0];
  162. };

  163. static inline struct sunxi_nand_chip *to_sunxi_nand(struct mtd_info *mtd)
  164. {
  165.         return container_of(mtd, struct sunxi_nand_chip, mtd);
  166. }

  167. struct sunxi_nfc {
  168.         struct nand_hw_control controller;
  169.         void __iomem *regs;
  170.         int irq;
  171.         struct clk *ahb_clk;
  172.         struct clk *sclk;
  173.         unsigned long assigned_cs;
  174.         unsigned long clk_rate;
  175.         struct list_head chips;
  176.         struct completion complete;
  177. };
复制代码
回复 支持 反对

使用道具 举报

 楼主| 发表于 2014-2-19 19:16:12 | 显示全部楼层
  1. static inline struct sunxi_nfc *to_sunxi_nfc(struct nand_hw_control *ctrl)
  2. {
  3.         return container_of(ctrl, struct sunxi_nfc, controller);
  4. }

  5. static irqreturn_t sunxi_nfc_interrupt(int irq, void *dev_id)
  6. {
  7.         struct sunxi_nfc *nfc = dev_id;
  8.         u32 st = readl(nfc->regs + NFC_REG_ST);
  9.         u32 ien = readl(nfc->regs + NFC_REG_INT);

  10.         if (!(ien & st))
  11.                 return IRQ_NONE;

  12.         if ((ien & st) == ien)
  13.                 complete(&nfc->complete);

  14.         writel(st & NFC_INT_MASK, nfc->regs + NFC_REG_ST);
  15.         writel(~st & ien & NFC_INT_MASK, nfc->regs + NFC_REG_INT);

  16.         return IRQ_HANDLED;
  17. }

  18. static int sunxi_nfc_wait_int(struct sunxi_nfc *nfc, u32 flags,
  19.                               unsigned int timeout_ms)
  20. {
  21.         init_completion(&nfc->complete);

  22.         writel(flags, nfc->regs + NFC_REG_INT);
  23.         if (!timeout_ms)
  24.                 wait_for_completion(&nfc->complete);
  25.         else if (!wait_for_completion_timeout(&nfc->complete,
  26.                                               msecs_to_jiffies(timeout_ms)))
  27.                 return -ETIMEDOUT;

  28.         return 0;
  29. }

  30. static int sunxi_nfc_dev_ready(struct mtd_info *mtd)
  31. {
  32.         struct sunxi_nand_chip *sunxi_nand = to_sunxi_nand(mtd);
  33.         struct sunxi_nfc *nfc = to_sunxi_nfc(sunxi_nand->nand.controller);
  34.         struct sunxi_nand_rb *rb;
  35.         unsigned long timeo = (sunxi_nand->nand.state == FL_ERASING ? 400 : 20);
  36.         int ret;

  37.         if (sunxi_nand->selected < 0)
  38.                 return 0;

  39.         rb = &sunxi_nand->sels[sunxi_nand->selected].rb;

  40.         switch (rb->type) {
  41.         case RB_NATIVE:
  42.                 ret = !!(readl(nfc->regs + NFC_REG_ST) &
  43.                          (NFC_RB_STATE0 << rb->info.nativeid));
  44.                 if (ret)
  45.                         break;

  46.                 sunxi_nfc_wait_int(nfc, NFC_RB_B2R, timeo);
  47.                 ret = !!(readl(nfc->regs + NFC_REG_ST) &
  48.                          (NFC_RB_STATE0 << rb->info.nativeid));
  49.                 break;
  50.         case RB_GPIO:
  51.                 ret = gpio_get_value(rb->info.gpio);
  52.                 break;
  53.         case RB_NONE:
  54.         default:
  55.                 ret = 0;
  56.                 dev_err(&mtd->dev, "cannot check R/B NAND status!");
  57.                 break;
  58.         }

  59.         return ret;
  60. }

  61. static void sunxi_nfc_select_chip(struct mtd_info *mtd, int chip)
  62. {
  63.         struct sunxi_nand_chip *sunxi_nand = to_sunxi_nand(mtd);
  64.         struct nand_chip *nand = &sunxi_nand->nand;
  65.         struct sunxi_nfc *nfc = to_sunxi_nfc(sunxi_nand->nand.controller);
  66.         struct sunxi_nand_chip_sel *sel;
  67.         u32 ctl;

  68.         if (chip > 0 && chip >= sunxi_nand->nsels)
  69.                 return;

  70.         if (chip == sunxi_nand->selected)
  71.                 return;

  72.         ctl = readl(nfc->regs + NFC_REG_CTL) &
  73.               ~(NFC_CE_SEL | NFC_RB_SEL | NFC_EN);

  74.         if (chip >= 0) {
  75.                 sel = &sunxi_nand->sels[chip];

  76.                 ctl |= (sel->cs << 24) | NFC_EN |
  77.                        (((nand->page_shift - 10) & 0xf) << 8);
  78.                 if (sel->rb.type == RB_NONE) {
  79.                         nand->dev_ready = NULL;
  80.                 } else {
  81.                         nand->dev_ready = sunxi_nfc_dev_ready;
  82.                         if (sel->rb.type == RB_NATIVE)
  83.                                 ctl |= (sel->rb.info.nativeid << 3);
  84.                 }

  85.                 writel(mtd->writesize, nfc->regs + NFC_REG_SPARE_AREA);

  86.                 if (nfc->clk_rate != sunxi_nand->clk_rate) {
  87.                         clk_set_rate(nfc->sclk, sunxi_nand->clk_rate);
  88.                         nfc->clk_rate = sunxi_nand->clk_rate;
  89.                 }
  90.         }

  91.         writel(ctl, nfc->regs + NFC_REG_CTL);

  92.         sunxi_nand->selected = chip;
  93. }

  94. static void sunxi_nfc_read_buf(struct mtd_info *mtd, uint8_t *buf, int len)
  95. {
  96.         struct sunxi_nand_chip *sunxi_nand = to_sunxi_nand(mtd);
  97.         struct sunxi_nfc *nfc = to_sunxi_nfc(sunxi_nand->nand.controller);
  98.         int cnt;
  99.         int offs = 0;
  100.         u32 tmp;

  101.         while (len > offs) {
  102.                 cnt = len - offs;
  103.                 if (cnt > 1024)
  104.                         cnt = 1024;

  105.                 while ((readl(nfc->regs + NFC_REG_ST) & NFC_CMD_FIFO_STATUS))
  106.                         ;
  107.                 writel(cnt, nfc->regs + NFC_REG_CNT);
  108.                 tmp = NFC_DATA_TRANS | NFC_DATA_SWAP_METHOD;
  109.                 writel(tmp, nfc->regs + NFC_REG_CMD);
  110.                 sunxi_nfc_wait_int(nfc, NFC_CMD_INT_FLAG, 0);
  111.                 if (buf)
  112.                         memcpy_fromio(buf + offs, nfc->regs + NFC_RAM0_BASE,
  113.                                       cnt);
  114.                 offs += cnt;
  115.         }
  116. }

  117. static void sunxi_nfc_write_buf(struct mtd_info *mtd, const uint8_t *buf,
  118.                                 int len)
  119. {
  120.         struct sunxi_nand_chip *sunxi_nand = to_sunxi_nand(mtd);
  121.         struct sunxi_nfc *nfc = to_sunxi_nfc(sunxi_nand->nand.controller);
  122.         int cnt;
  123.         int offs = 0;
  124.         u32 tmp;

  125.         while (len > offs) {
  126.                 cnt = len - offs;
  127.                 if (cnt > 1024)
  128.                         cnt = 1024;

  129.                 while ((readl(nfc->regs + NFC_REG_ST) & NFC_CMD_FIFO_STATUS))
  130.                         ;
  131.                 writel(cnt, nfc->regs + NFC_REG_CNT);
  132.                 memcpy_toio(nfc->regs + NFC_RAM0_BASE, buf + offs, cnt);
  133.                 tmp = NFC_DATA_TRANS | NFC_DATA_SWAP_METHOD |
  134.                       NFC_ACCESS_DIR;
  135.                 writel(tmp, nfc->regs + NFC_REG_CMD);
  136.                 sunxi_nfc_wait_int(nfc, NFC_CMD_INT_FLAG, 0);
  137.                 offs += cnt;
  138.         }
  139. }

  140. static uint8_t sunxi_nfc_read_byte(struct mtd_info *mtd)
  141. {
  142.         uint8_t ret;

  143.         sunxi_nfc_read_buf(mtd, &ret, 1);

  144.         return ret;
  145. }

  146. static void sunxi_nfc_cmd_ctrl(struct mtd_info *mtd, int dat,
  147.                                unsigned int ctrl)
  148. {
  149.         struct sunxi_nand_chip *sunxi_nand = to_sunxi_nand(mtd);
  150.         struct sunxi_nfc *nfc = to_sunxi_nfc(sunxi_nand->nand.controller);
  151.         u32 tmp;

  152.         while ((readl(nfc->regs + NFC_REG_ST) & NFC_CMD_FIFO_STATUS))
  153.                 ;

  154.         if (ctrl & NAND_CTRL_CHANGE) {
  155.                 tmp = readl(nfc->regs + NFC_REG_CTL);
  156.                 if (ctrl & NAND_NCE)
  157.                         tmp |= NFC_CE_CTL;
  158.                 else
  159.                         tmp &= ~NFC_CE_CTL;
  160.                 writel(tmp, nfc->regs + NFC_REG_CTL);
  161.         }

  162.         if (dat == NAND_CMD_NONE)
  163.                 return;

  164.         if (ctrl & NAND_CLE) {
  165.                 writel(NFC_SEND_CMD1 | dat, nfc->regs + NFC_REG_CMD);
  166.         } else {
  167.                 writel(dat, nfc->regs + NFC_REG_ADDR_LOW);
  168.                 writel(NFC_SEND_ADR, nfc->regs + NFC_REG_CMD);
  169.         }

  170.         sunxi_nfc_wait_int(nfc, NFC_CMD_INT_FLAG, 0);
  171. }

  172. static int sunxi_nfc_hwecc_read_page(struct mtd_info *mtd,
  173.                                      struct nand_chip *chip, uint8_t *buf,
  174.                                      int oob_required, int page)
  175. {
  176.         struct sunxi_nand_chip *sunxi_nand = to_sunxi_nand(mtd);
  177.         struct sunxi_nfc *nfc = to_sunxi_nfc(sunxi_nand->nand.controller);
  178.         struct nand_ecc_ctrl *ecc = &chip->ecc;
  179.         struct nand_ecclayout *layout = ecc->layout;
  180.         struct sunxi_nand_hw_ecc *data = ecc->priv;
  181.         unsigned int max_bitflips = 0;
  182.         int offset;
  183.         u32 tmp;
  184.         int i;

  185.         tmp = readl(nfc->regs + NFC_REG_ECC_CTL);
  186.         tmp &= ~(NFC_ECC_MODE | NFC_ECC_PIPELINE | NFC_ECC_BLOCK_SIZE |
  187.                  NFC_ECC_BLOCK_SIZE);
  188.         tmp |= NFC_ECC_EN | (data->mode << NFC_ECC_MODE_SHIFT);
  189.         writel(tmp, nfc->regs + NFC_REG_ECC_CTL);

  190.         for (i = 0; i < mtd->writesize / ecc->size; i++) {
  191.                 if (i)
  192.                         chip->cmdfunc(mtd, NAND_CMD_RNDOUT, i * ecc->size, -1);
  193.                 chip->read_buf(mtd, NULL, chip->ecc.size);
  194.                 offset = mtd->writesize + layout->eccpos[i * ecc->bytes] - 4;
  195.                 chip->cmdfunc(mtd, NAND_CMD_RNDOUT, offset, -1);
  196.                 while ((readl(nfc->regs + NFC_REG_ST) & NFC_CMD_FIFO_STATUS))
  197.                         ;
  198.                 tmp = NFC_DATA_TRANS | NFC_DATA_SWAP_METHOD | (1 << 30);
  199.                 writel(tmp, nfc->regs + NFC_REG_CMD);
  200.                 sunxi_nfc_wait_int(nfc, NFC_CMD_INT_FLAG, 0);
  201.                 memcpy_fromio(buf + (i * ecc->size), nfc->regs + NFC_RAM0_BASE,
  202.                               chip->ecc.size);

  203.                 if (readl(nfc->regs + NFC_REG_ECC_ST) & 0x1) {
  204.                         mtd->ecc_stats.failed++;
  205.                 } else {
  206.                         tmp = readl(nfc->regs + NFC_REG_ECC_CNT0) & 0xff;
  207.                         mtd->ecc_stats.corrected += tmp;
  208.                         max_bitflips = max_t(unsigned int, max_bitflips, tmp);
  209.                 }
  210.         }

  211.         if (oob_required) {
  212.                 chip->cmdfunc(mtd, NAND_CMD_RNDOUT, mtd->writesize, -1);
  213.                 chip->read_buf(mtd, chip->oob_poi, mtd->oobsize);
  214.         }

  215.         tmp = readl(nfc->regs + NFC_REG_ECC_CTL);
  216.         tmp &= ~NFC_ECC_EN;

  217.         writel(tmp, nfc->regs + NFC_REG_ECC_CTL);

  218.         return max_bitflips;
  219. }
复制代码
回复 支持 反对

使用道具 举报

 楼主| 发表于 2014-2-19 19:17:15 | 显示全部楼层

  1. static int sunxi_nfc_hwecc_write_page(struct mtd_info *mtd,
  2.                                       struct nand_chip *chip,
  3.                                       const uint8_t *buf,
  4.                                       int oob_required)
  5. {
  6.         struct sunxi_nand_chip *sunxi_nand = to_sunxi_nand(mtd);
  7.         struct sunxi_nfc *nfc = to_sunxi_nfc(sunxi_nand->nand.controller);
  8.         struct nand_ecc_ctrl *ecc = &chip->ecc;
  9.         struct nand_ecclayout *layout = ecc->layout;
  10.         struct sunxi_nand_hw_ecc *data = ecc->priv;
  11.         int offset;
  12.         u32 tmp;
  13.         int i;
  14.         int j;

  15.         tmp = readl(nfc->regs + NFC_REG_ECC_CTL);
  16.         tmp &= ~(NFC_ECC_MODE | NFC_ECC_PIPELINE | NFC_ECC_BLOCK_SIZE |
  17.                  NFC_ECC_BLOCK_SIZE);
  18.         tmp |= NFC_ECC_EN | (data->mode << NFC_ECC_MODE_SHIFT);

  19.         writel(tmp, nfc->regs + NFC_REG_ECC_CTL);

  20.         for (i = 0; i < mtd->writesize / ecc->size; i++) {
  21.                 if (i)
  22.                         chip->cmdfunc(mtd, NAND_CMD_RNDIN, i * ecc->size, -1);

  23.                 chip->write_buf(mtd, buf + (i * ecc->size), ecc->size);
  24.                 offset = mtd->writesize + layout->eccpos[i * ecc->bytes] - 4;
  25.                 chip->cmdfunc(mtd, NAND_CMD_RNDIN, offset, -1);
  26.                 while ((readl(nfc->regs + NFC_REG_ST) & NFC_CMD_FIFO_STATUS))
  27.                         ;

  28.                 /* Fill OOB data in */
  29.                 for (j = 0; j < 4; j++) {
  30.                         if (oob_required) {
  31.                                 offset = layout->eccpos[i * ecc->size] - 4;
  32.                                 writeb(chip->oob_poi[offset + j],
  33.                                        nfc->regs + NFC_REG_USER_DATA_BASE + j);
  34.                         } else {
  35.                                 writeb(0xff,
  36.                                        nfc->regs + NFC_REG_USER_DATA_BASE + j);
  37.                         }
  38.                 }

  39.                 tmp = NFC_DATA_TRANS | NFC_DATA_SWAP_METHOD |
  40.                       NFC_ACCESS_DIR | (1 << 30);
  41.                 writel(tmp, nfc->regs + NFC_REG_CMD);
  42.                 sunxi_nfc_wait_int(nfc, NFC_CMD_INT_FLAG, 0);
  43.         }

  44.         if (oob_required && chip->ecc.layout->oobfree[0].length > 2) {
  45.                 chip->cmdfunc(mtd, NAND_CMD_RNDIN, mtd->writesize, -1);
  46.                 chip->write_buf(mtd, chip->oob_poi,
  47.                                 chip->ecc.layout->oobfree[0].length - 2);
  48.         }

  49.         tmp = readl(nfc->regs + NFC_REG_ECC_CTL);
  50.         tmp &= ~(NFC_ECC_EN | NFC_ECC_PIPELINE);

  51.         writel(tmp, nfc->regs + NFC_REG_ECC_CTL);

  52.         return 0;
  53. }

  54. static int sunxi_nand_chip_set_timings(struct sunxi_nand_chip *chip,
  55.                                        const struct nand_sdr_timings *timings)
  56. {
  57.         u32 min_clk_period = 0;

  58.         /* T1 <=> tCLS */
  59.         if (timings->tCLS_min > min_clk_period)
  60.                 min_clk_period = timings->tCLS_min;

  61.         /* T2 <=> tCLH */
  62.         if (timings->tCLH_min > min_clk_period)
  63.                 min_clk_period = timings->tCLH_min;

  64.         /* T3 <=> tCS */
  65.         if (timings->tCS_min > min_clk_period)
  66.                 min_clk_period = timings->tCS_min;

  67.         /* T4 <=> tCH */
  68.         if (timings->tCH_min > min_clk_period)
  69.                 min_clk_period = timings->tCH_min;

  70.         /* T5 <=> tWP */
  71.         if (timings->tWP_min > min_clk_period)
  72.                 min_clk_period = timings->tWP_min;

  73.         /* T6 <=> tWH */
  74.         if (timings->tWH_min > min_clk_period)
  75.                 min_clk_period = timings->tWH_min;

  76.         /* T7 <=> tALS */
  77.         if (timings->tALS_min > min_clk_period)
  78.                 min_clk_period = timings->tALS_min;

  79.         /* T8 <=> tDS */
  80.         if (timings->tDS_min > min_clk_period)
  81.                 min_clk_period = timings->tDS_min;

  82.         /* T9 <=> tDH */
  83.         if (timings->tDH_min > min_clk_period)
  84.                 min_clk_period = timings->tDH_min;

  85.         /* T10 <=> tRR */
  86.         if (timings->tRR_min > (min_clk_period * 3))
  87.                 min_clk_period = (timings->tRR_min + 2) / 3;

  88.         /* T11 <=> tALH */
  89.         if (timings->tALH_min > min_clk_period)
  90.                 min_clk_period = timings->tALH_min;

  91.         /* T12 <=> tRP */
  92.         if (timings->tRP_min > min_clk_period)
  93.                 min_clk_period = timings->tRP_min;

  94.         /* T13 <=> tREH */
  95.         if (timings->tREH_min > min_clk_period)
  96.                 min_clk_period = timings->tREH_min;

  97.         /* T14 <=> tRC */
  98.         if (timings->tRC_min > (min_clk_period * 2))
  99.                 min_clk_period = (timings->tRC_min + 1) / 2;

  100.         /* T15 <=> tWC */
  101.         if (timings->tWC_min > (min_clk_period * 2))
  102.                 min_clk_period = (timings->tWC_min + 1) / 2;


  103.         /* min_clk_period = (NAND-clk-period * 2) */
  104.         if (min_clk_period < 1000)
  105.                 min_clk_period = 1000;

  106.         min_clk_period /= 1000;
  107.         chip->clk_rate = (2 * 1000000000) / min_clk_period;

  108.         /* TODO: configure T16-T19 */

  109.         return 0;
  110. }

  111. static int sunxi_nand_chip_init_timings(struct sunxi_nand_chip *chip,
  112.                                         struct device_node *np)
  113. {
  114.         const struct nand_sdr_timings *timings;
  115.         int ret;

  116.         ret = onfi_get_async_timing_mode(&chip->nand);
  117.         if (ret == ONFI_TIMING_MODE_UNKNOWN) {
  118.                 ret = of_get_nand_onfi_timing_mode(np);
  119.                 if (ret < 0)
  120.                         return ret;
  121.         }

  122.         ret = fls(ret);
  123.         if (!ret)
  124.                 return -EINVAL;

  125.         timings = onfi_async_timing_mode_to_sdr_timings(ret - 1);
  126.         if (IS_ERR(timings))
  127.                 return PTR_ERR(timings);

  128.         return sunxi_nand_chip_set_timings(chip, timings);
  129. }

  130. static int sunxi_nand_chip_hwecc_init(struct device *dev,
  131.                                       struct sunxi_nand_chip *chip,
  132.                                       struct mtd_info *mtd,
  133.                                       struct device_node *np)
  134. {
  135.         struct nand_chip *nand = &chip->nand;
  136.         struct nand_ecc_ctrl *ecc = &nand->ecc;
  137.         struct sunxi_nand_hw_ecc *data;
  138.         struct nand_ecclayout *layout;
  139.         int nsectors;
  140.         int i;
  141.         int j;

  142.         data = devm_kzalloc(dev, sizeof(*data), GFP_KERNEL);
  143.         if (!data)
  144.                 return -ENOMEM;

  145.         ecc->read_page = sunxi_nfc_hwecc_read_page;
  146.         ecc->write_page = sunxi_nfc_hwecc_write_page;

  147.         if (nand->ecc_strength_ds <= 16) {
  148.                 nand->ecc_strength_ds = 16;
  149.                 data->mode = 0;
  150.         } else if (nand->ecc_strength_ds <= 24) {
  151.                 nand->ecc_strength_ds = 24;
  152.                 data->mode = 1;
  153.         } else if (nand->ecc_strength_ds <= 28) {
  154.                 nand->ecc_strength_ds = 28;
  155.                 data->mode = 2;
  156.         } else if (nand->ecc_strength_ds <= 32) {
  157.                 nand->ecc_strength_ds = 32;
  158.                 data->mode = 3;
  159.         } else if (nand->ecc_strength_ds <= 40) {
  160.                 nand->ecc_strength_ds = 40;
  161.                 data->mode = 4;
  162.         } else if (nand->ecc_strength_ds <= 48) {
  163.                 nand->ecc_strength_ds = 48;
  164.                 data->mode = 5;
  165.         } else if (nand->ecc_strength_ds <= 56) {
  166.                 nand->ecc_strength_ds = 56;
  167.                 data->mode = 6;
  168.         } else if (nand->ecc_strength_ds <= 60) {
  169.                 nand->ecc_strength_ds = 60;
  170.                 data->mode = 7;
  171.         } else if (nand->ecc_strength_ds <= 64) {
  172.                 nand->ecc_strength_ds = 64;
  173.                 data->mode = 8;
  174.         } else {
  175.                 dev_err(dev, "unsupported strength\n");
  176.                 return -ENOTSUPP;
  177.         }

  178.         /* HW ECC always request ECC bytes for 1024 bytes blocks */
  179.         ecc->bytes = ((nand->ecc_strength_ds * fls(8 * 1024)) + 7) / 8;

  180.         /* HW ECC always work with even numbers of ECC bytes */
  181.         if (ecc->bytes % 2)
  182.                 ecc->bytes++;
  183.         ecc->strength = nand->ecc_strength_ds;
  184.         ecc->size = nand->ecc_step_ds;

  185.         layout = &data->layout;
  186.         nsectors = mtd->writesize / ecc->size;

  187.         if (mtd->oobsize < ((ecc->bytes + 4) * nsectors))
  188.                 return -EINVAL;

  189.         layout->eccbytes = (ecc->bytes * nsectors);

  190.         /*
  191.          * The first 2 bytes are used for BB markers.
  192.          * We merge the 4 user available bytes from HW ECC with this
  193.          * first section, hence why the + 2 operation (- 2 + 4).
  194.          */
  195.         layout->oobfree[0].length = mtd->oobsize + 2 -
  196.                                     ((ecc->bytes + 4) * nsectors);
  197.         layout->oobfree[0].offset = 2;
  198.         for (i = 0; i < nsectors; i++) {
  199.                 /*
  200.                  * The first 4 ECC block bytes are already counted in the first
  201.                  * obbfree entry.
  202.                  */
  203.                 if (i) {
  204.                         layout->oobfree[i].offset =
  205.                                 layout->oobfree[i - 1].offset +
  206.                                 layout->oobfree[i - 1].length +
  207.                                 ecc->bytes;
  208.                         layout->oobfree[i].length = 4;
  209.                 }

  210.                 for (j = 0; j < ecc->bytes; j++)
  211.                         layout->eccpos[(ecc->bytes * i) + j] =
  212.                                         layout->oobfree[i].offset +
  213.                                         layout->oobfree[i].length + j;
  214.         }

  215.         ecc->layout = layout;
  216.         ecc->priv = data;

  217.         return 0;
  218. }
复制代码
回复 支持 反对

使用道具 举报

 楼主| 发表于 2014-2-19 19:17:44 | 显示全部楼层

  1. static int sunxi_nand_chip_ecc_init(struct device *dev,
  2.                                     struct sunxi_nand_chip *chip,
  3.                                     struct mtd_info *mtd,
  4.                                     struct device_node *np)
  5. {
  6.         struct nand_chip *nand = &chip->nand;
  7.         u32 strength;
  8.         u32 blk_size;
  9.         int ret;

  10.         nand->ecc.mode = of_get_nand_ecc_mode(np);

  11.         if (!of_get_nand_ecc_level(np, &strength, &blk_size)) {
  12.                 nand->ecc_step_ds = blk_size;
  13.                 nand->ecc_strength_ds = strength;
  14.         }

  15.         switch (nand->ecc.mode) {
  16.         case NAND_ECC_SOFT_BCH:
  17.                 nand->ecc.size = nand->ecc_step_ds;
  18.                 nand->ecc.bytes = ((nand->ecc_strength_ds *
  19.                                     fls(8 * nand->ecc_step_ds)) + 7) / 8;
  20.                 break;
  21.         case NAND_ECC_HW:
  22.                 ret = sunxi_nand_chip_hwecc_init(dev, chip, mtd, np);
  23.                 if (ret)
  24.                         return ret;
  25.                 break;
  26.         case NAND_ECC_NONE:
  27.         default:
  28.                 break;
  29.         }

  30.         return 0;
  31. }

  32. static int sunxi_nand_chip_init(struct device *dev, struct sunxi_nfc *nfc,
  33.                                 struct device_node *np)
  34. {
  35.         const struct nand_sdr_timings *timings;
  36.         struct sunxi_nand_chip *chip;
  37.         struct mtd_part_parser_data ppdata;
  38.         struct mtd_info *mtd;
  39.         struct nand_chip *nand;
  40.         int nsels;
  41.         int ret;
  42.         int i;
  43.         u32 tmp;

  44.         if (!of_get_property(np, "reg", &nsels))
  45.                 return -EINVAL;

  46.         nsels /= sizeof(u32);
  47.         if (!nsels)
  48.                 return -EINVAL;

  49.         chip = devm_kzalloc(dev,
  50.                             sizeof(*chip) +
  51.                             (nsels * sizeof(struct sunxi_nand_chip_sel)),
  52.                             GFP_KERNEL);
  53.         if (!chip)
  54.                 return -ENOMEM;

  55.         chip->nsels = nsels;
  56.         chip->selected = -1;

  57.         for (i = 0; i < nsels; i++) {
  58.                 ret = of_property_read_u32_index(np, "reg", i, &tmp);
  59.                 if (ret)
  60.                         return ret;

  61.                 if (tmp > 7)
  62.                         return -EINVAL;

  63.                 if (test_and_set_bit(tmp, &nfc->assigned_cs))
  64.                         return -EINVAL;

  65.                 chip->sels[i].cs = tmp;

  66.                 if (!of_property_read_u32_index(np, "allwinner,rb", i, &tmp) &&
  67.                     tmp < 2) {
  68.                         chip->sels[i].rb.type = RB_NATIVE;
  69.                         chip->sels[i].rb.info.nativeid = tmp;
  70.                 } else {
  71.                         ret = of_get_named_gpio(np, "rb-gpios", i);
  72.                         if (ret >= 0) {
  73.                                 tmp = ret;
  74.                                 chip->sels[i].rb.type = RB_GPIO;
  75.                                 chip->sels[i].rb.info.gpio = tmp;
  76.                                 ret = devm_gpio_request(dev, tmp, "nand-rb");
  77.                                 if (ret)
  78.                                         return ret;

  79.                                 ret = gpio_direction_input(tmp);
  80.                                 if (ret)
  81.                                         return ret;
  82.                         } else {
  83.                                 chip->sels[i].rb.type = RB_NONE;
  84.                         }
  85.                 }
  86.         }

  87.         timings = onfi_async_timing_mode_to_sdr_timings(0);
  88.         if (IS_ERR(timings))
  89.                 return PTR_ERR(timings);

  90.         ret = sunxi_nand_chip_set_timings(chip, timings);

  91.         nand = &chip->nand;
  92.         nand->controller = &nfc->controller;
  93.         nand->select_chip = sunxi_nfc_select_chip;
  94.         nand->cmd_ctrl = sunxi_nfc_cmd_ctrl;
  95.         nand->read_buf = sunxi_nfc_read_buf;
  96.         nand->write_buf = sunxi_nfc_write_buf;
  97.         nand->read_byte = sunxi_nfc_read_byte;

  98.         if (of_get_nand_on_flash_bbt(np))
  99.                 nand->bbt_options |= NAND_BBT_USE_FLASH;

  100.         mtd = &chip->mtd;
  101.         mtd->priv = nand;
  102.         mtd->owner = THIS_MODULE;

  103.         ret = nand_scan_ident(mtd, nsels, NULL);
  104.         if (ret)
  105.                 return ret;

  106.         ret = sunxi_nand_chip_init_timings(chip, np);
  107.         if (ret)
  108.                 return ret;

  109.         ret = sunxi_nand_chip_ecc_init(dev, chip, mtd, np);
  110.         if (ret)
  111.                 return ret;

  112.         ret = nand_scan_tail(mtd);
  113.         if (ret)
  114.                 return ret;

  115.         if (of_property_read_string(np, "nand-name", &mtd->name)) {
  116.                 snprintf(chip->default_name, MAX_NAME_SIZE,
  117.                          DEFAULT_NAME_FORMAT, chip->sels[i].cs);
  118.                 mtd->name = chip->default_name;
  119.         }

  120.         ppdata.of_node = np;
  121.         ret = mtd_device_parse_register(mtd, NULL, &ppdata, NULL, 0);
  122.         if (!ret)
  123.                 return ret;

  124.         list_add_tail(&chip->node, &nfc->chips);

  125.         return 0;
  126. }

  127. static int sunxi_nand_chips_init(struct device *dev, struct sunxi_nfc *nfc)
  128. {
  129.         struct device_node *np = dev->of_node;
  130.         struct device_node *nand_np;
  131.         int nchips = of_get_child_count(np);
  132.         int ret;

  133.         if (nchips > 8)
  134.                 return -EINVAL;

  135.         for_each_child_of_node(np, nand_np) {
  136.                 ret = sunxi_nand_chip_init(dev, nfc, nand_np);
  137.                 if (ret)
  138.                         return ret;
  139.         }

  140.         return 0;
  141. }

  142. static int sunxi_nfc_probe(struct platform_device *pdev)
  143. {
  144.         struct device *dev = &pdev->dev;
  145.         struct resource *r;
  146.         struct sunxi_nfc *nfc;
  147.         int ret;

  148.         nfc = devm_kzalloc(dev, sizeof(*nfc), GFP_KERNEL);
  149.         if (!nfc) {
  150.                 dev_err(dev, "failed to allocate NFC struct\n");
  151.                 return -ENOMEM;
  152.         }

  153.         spin_lock_init(&nfc->controller.lock);
  154.         init_waitqueue_head(&nfc->controller.wq);
  155.         INIT_LIST_HEAD(&nfc->chips);

  156.         r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  157.         nfc->regs = devm_ioremap_resource(dev, r);
  158.         if (IS_ERR(nfc->regs)) {
  159.                 dev_err(dev, "failed to remap iomem\n");
  160.                 return PTR_ERR(nfc->regs);
  161.         }

  162.         nfc->irq = platform_get_irq(pdev, 0);
  163.         if (nfc->irq < 0) {
  164.                 dev_err(dev, "failed to retrieve irq\n");
  165.                 return nfc->irq;
  166.         }

  167.         nfc->ahb_clk = devm_clk_get(dev, "ahb_clk");
  168.         if (IS_ERR(nfc->ahb_clk)) {
  169.                 dev_err(dev, "failed to retrieve ahb_clk\n");
  170.                 return PTR_ERR(nfc->ahb_clk);
  171.         }

  172.         ret = clk_prepare_enable(nfc->ahb_clk);
  173.         if (ret)
  174.                 return ret;

  175.         nfc->sclk = devm_clk_get(dev, "sclk");
  176.         if (IS_ERR(nfc->sclk)) {
  177.                 dev_err(dev, "failed to retrieve nand_clk\n");
  178.                 ret = PTR_ERR(nfc->sclk);
  179.                 goto out_ahb_clk_unprepare;
  180.         }

  181.         ret = clk_prepare_enable(nfc->sclk);
  182.         if (ret)
  183.                 goto out_ahb_clk_unprepare;

  184.         /* Reset NFC */
  185.         writel(readl(nfc->regs + NFC_REG_CTL) | NFC_RESET,
  186.                nfc->regs + NFC_REG_CTL);
  187.         while (readl(nfc->regs + NFC_REG_CTL) & NFC_RESET)
  188.                 ;

  189.         writel(0, nfc->regs + NFC_REG_INT);
  190.         ret = devm_request_irq(dev, nfc->irq, sunxi_nfc_interrupt,
  191.                                0, "sunxi-nand", nfc);
  192.         if (ret)
  193.                 goto out_sclk_unprepare;

  194.         platform_set_drvdata(pdev, nfc);

  195.         writel(0x100, nfc->regs + NFC_REG_TIMING_CTL);
  196.         writel(0x7ff, nfc->regs + NFC_REG_TIMING_CFG);

  197.         ret = sunxi_nand_chips_init(dev, nfc);
  198.         if (ret) {
  199.                 dev_err(dev, "failed to init nand chips\n");
  200.                 goto out_sclk_unprepare;
  201.         }

  202.         return 0;

  203. out_sclk_unprepare:
  204.         clk_disable_unprepare(nfc->sclk);
  205. out_ahb_clk_unprepare:
  206.         clk_disable_unprepare(nfc->ahb_clk);

  207.         return ret;
  208. }

  209. static const struct of_device_id sunxi_nfc_ids[] = {
  210.         { .compatible = "allwinner,sun4i-nand" },
  211.         { /* sentinel */ }
  212. };
  213. MODULE_DEVICE_TABLE(of, sunxi_nfc_ids);

  214. static struct platform_driver sunxi_nfc_driver = {
  215.         .driver = {
  216.                 .name = "sunxi_nand",
  217.                 .owner = THIS_MODULE,
  218.                 .of_match_table = of_match_ptr(sunxi_nfc_ids),
  219.         },
  220.         .probe = sunxi_nfc_probe,
  221. };
  222. module_platform_driver(sunxi_nfc_driver);

  223. MODULE_LICENSE("GPL v2");
  224. MODULE_AUTHOR("Boris BREZILLON");
  225. MODULE_DESCRIPTION("Allwinner NAND Flash Controller driver");
  226. MODULE_ALIAS("platform:sunxi_nfc");
复制代码
回复 支持 反对

使用道具 举报

发表于 2014-2-22 17:23:26 | 显示全部楼层
liyunkebeyond 发表于 2014-2-19 19:17

你好,我也没事仔细看过,只是在邮件列表里面有过一些印象,你google一下sunxi-clk应该能找到想要的patch
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

QQ|Archiver|手机版|粤ICP备13051116号|cubie.cc---深刻的嵌入式技术讨论社区

GMT+8, 2024-4-20 17:23 , Processed in 0.026998 second(s), 15 queries .

Powered by Discuz! X3.4

© 2001-2012 Comsenz Inc. | Style by Coxxs

返回顶部