atmel_nand.c 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343
  1. /*
  2. * (C) Copyright 2007-2008
  3. * Stelian Pop <stelian.pop@leadtechdesign.com>
  4. * Lead Tech Design <www.leadtechdesign.com>
  5. *
  6. * (C) Copyright 2006 ATMEL Rousset, Lacressonniere Nicolas
  7. *
  8. * See file CREDITS for list of people who contributed to this
  9. * project.
  10. *
  11. * This program is free software; you can redistribute it and/or
  12. * modify it under the terms of the GNU General Public License as
  13. * published by the Free Software Foundation; either version 2 of
  14. * the License, or (at your option) any later version.
  15. *
  16. * This program is distributed in the hope that it will be useful,
  17. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. * GNU General Public License for more details.
  20. *
  21. * You should have received a copy of the GNU General Public License
  22. * along with this program; if not, write to the Free Software
  23. * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
  24. * MA 02111-1307 USA
  25. */
  26. #include <common.h>
  27. #include <asm/arch/hardware.h>
  28. #include <asm/arch/gpio.h>
  29. #include <asm/arch/at91_pio.h>
  30. #include <nand.h>
  31. #ifdef CONFIG_ATMEL_NAND_HWECC
  32. /* Register access macros */
  33. #define ecc_readl(add, reg) \
  34. readl(AT91_BASE_SYS + add + ATMEL_ECC_##reg)
  35. #define ecc_writel(add, reg, value) \
  36. writel((value), AT91_BASE_SYS + add + ATMEL_ECC_##reg)
  37. #include "atmel_nand_ecc.h" /* Hardware ECC registers */
  38. /* oob layout for large page size
  39. * bad block info is on bytes 0 and 1
  40. * the bytes have to be consecutives to avoid
  41. * several NAND_CMD_RNDOUT during read
  42. */
  43. static struct nand_ecclayout atmel_oobinfo_large = {
  44. .eccbytes = 4,
  45. .eccpos = {60, 61, 62, 63},
  46. .oobfree = {
  47. {2, 58}
  48. },
  49. };
  50. /* oob layout for small page size
  51. * bad block info is on bytes 4 and 5
  52. * the bytes have to be consecutives to avoid
  53. * several NAND_CMD_RNDOUT during read
  54. */
  55. static struct nand_ecclayout atmel_oobinfo_small = {
  56. .eccbytes = 4,
  57. .eccpos = {0, 1, 2, 3},
  58. .oobfree = {
  59. {6, 10}
  60. },
  61. };
  62. /*
  63. * Calculate HW ECC
  64. *
  65. * function called after a write
  66. *
  67. * mtd: MTD block structure
  68. * dat: raw data (unused)
  69. * ecc_code: buffer for ECC
  70. */
  71. static int atmel_nand_calculate(struct mtd_info *mtd,
  72. const u_char *dat, unsigned char *ecc_code)
  73. {
  74. struct nand_chip *nand_chip = mtd->priv;
  75. unsigned int ecc_value;
  76. /* get the first 2 ECC bytes */
  77. ecc_value = ecc_readl(CONFIG_SYS_NAND_ECC_BASE, PR);
  78. ecc_code[0] = ecc_value & 0xFF;
  79. ecc_code[1] = (ecc_value >> 8) & 0xFF;
  80. /* get the last 2 ECC bytes */
  81. ecc_value = ecc_readl(CONFIG_SYS_NAND_ECC_BASE, NPR) & ATMEL_ECC_NPARITY;
  82. ecc_code[2] = ecc_value & 0xFF;
  83. ecc_code[3] = (ecc_value >> 8) & 0xFF;
  84. return 0;
  85. }
  86. /*
  87. * HW ECC read page function
  88. *
  89. * mtd: mtd info structure
  90. * chip: nand chip info structure
  91. * buf: buffer to store read data
  92. */
  93. static int atmel_nand_read_page(struct mtd_info *mtd,
  94. struct nand_chip *chip, uint8_t *buf, int page)
  95. {
  96. int eccsize = chip->ecc.size;
  97. int eccbytes = chip->ecc.bytes;
  98. uint32_t *eccpos = chip->ecc.layout->eccpos;
  99. uint8_t *p = buf;
  100. uint8_t *oob = chip->oob_poi;
  101. uint8_t *ecc_pos;
  102. int stat;
  103. /* read the page */
  104. chip->read_buf(mtd, p, eccsize);
  105. /* move to ECC position if needed */
  106. if (eccpos[0] != 0) {
  107. /* This only works on large pages
  108. * because the ECC controller waits for
  109. * NAND_CMD_RNDOUTSTART after the
  110. * NAND_CMD_RNDOUT.
  111. * anyway, for small pages, the eccpos[0] == 0
  112. */
  113. chip->cmdfunc(mtd, NAND_CMD_RNDOUT,
  114. mtd->writesize + eccpos[0], -1);
  115. }
  116. /* the ECC controller needs to read the ECC just after the data */
  117. ecc_pos = oob + eccpos[0];
  118. chip->read_buf(mtd, ecc_pos, eccbytes);
  119. /* check if there's an error */
  120. stat = chip->ecc.correct(mtd, p, oob, NULL);
  121. if (stat < 0)
  122. mtd->ecc_stats.failed++;
  123. else
  124. mtd->ecc_stats.corrected += stat;
  125. /* get back to oob start (end of page) */
  126. chip->cmdfunc(mtd, NAND_CMD_RNDOUT, mtd->writesize, -1);
  127. /* read the oob */
  128. chip->read_buf(mtd, oob, mtd->oobsize);
  129. return 0;
  130. }
  131. /*
  132. * HW ECC Correction
  133. *
  134. * function called after a read
  135. *
  136. * mtd: MTD block structure
  137. * dat: raw data read from the chip
  138. * read_ecc: ECC from the chip (unused)
  139. * isnull: unused
  140. *
  141. * Detect and correct a 1 bit error for a page
  142. */
  143. static int atmel_nand_correct(struct mtd_info *mtd, u_char *dat,
  144. u_char *read_ecc, u_char *isnull)
  145. {
  146. struct nand_chip *nand_chip = mtd->priv;
  147. unsigned int ecc_status, ecc_parity, ecc_mode;
  148. unsigned int ecc_word, ecc_bit;
  149. /* get the status from the Status Register */
  150. ecc_status = ecc_readl(CONFIG_SYS_NAND_ECC_BASE, SR);
  151. /* if there's no error */
  152. if (likely(!(ecc_status & ATMEL_ECC_RECERR)))
  153. return 0;
  154. /* get error bit offset (4 bits) */
  155. ecc_bit = ecc_readl(CONFIG_SYS_NAND_ECC_BASE, PR) & ATMEL_ECC_BITADDR;
  156. /* get word address (12 bits) */
  157. ecc_word = ecc_readl(CONFIG_SYS_NAND_ECC_BASE, PR) & ATMEL_ECC_WORDADDR;
  158. ecc_word >>= 4;
  159. /* if there are multiple errors */
  160. if (ecc_status & ATMEL_ECC_MULERR) {
  161. /* check if it is a freshly erased block
  162. * (filled with 0xff) */
  163. if ((ecc_bit == ATMEL_ECC_BITADDR)
  164. && (ecc_word == (ATMEL_ECC_WORDADDR >> 4))) {
  165. /* the block has just been erased, return OK */
  166. return 0;
  167. }
  168. /* it doesn't seems to be a freshly
  169. * erased block.
  170. * We can't correct so many errors */
  171. printk(KERN_WARNING "atmel_nand : multiple errors detected."
  172. " Unable to correct.\n");
  173. return -EIO;
  174. }
  175. /* if there's a single bit error : we can correct it */
  176. if (ecc_status & ATMEL_ECC_ECCERR) {
  177. /* there's nothing much to do here.
  178. * the bit error is on the ECC itself.
  179. */
  180. printk(KERN_WARNING "atmel_nand : one bit error on ECC code."
  181. " Nothing to correct\n");
  182. return 0;
  183. }
  184. printk(KERN_WARNING "atmel_nand : one bit error on data."
  185. " (word offset in the page :"
  186. " 0x%x bit offset : 0x%x)\n",
  187. ecc_word, ecc_bit);
  188. /* correct the error */
  189. if (nand_chip->options & NAND_BUSWIDTH_16) {
  190. /* 16 bits words */
  191. ((unsigned short *) dat)[ecc_word] ^= (1 << ecc_bit);
  192. } else {
  193. /* 8 bits words */
  194. dat[ecc_word] ^= (1 << ecc_bit);
  195. }
  196. printk(KERN_WARNING "atmel_nand : error corrected\n");
  197. return 1;
  198. }
  199. /*
  200. * Enable HW ECC : unused on most chips
  201. */
  202. static void atmel_nand_hwctl(struct mtd_info *mtd, int mode)
  203. {
  204. }
  205. #endif
  206. static void at91_nand_hwcontrol(struct mtd_info *mtd,
  207. int cmd, unsigned int ctrl)
  208. {
  209. struct nand_chip *this = mtd->priv;
  210. if (ctrl & NAND_CTRL_CHANGE) {
  211. ulong IO_ADDR_W = (ulong) this->IO_ADDR_W;
  212. IO_ADDR_W &= ~(CONFIG_SYS_NAND_MASK_ALE
  213. | CONFIG_SYS_NAND_MASK_CLE);
  214. if (ctrl & NAND_CLE)
  215. IO_ADDR_W |= CONFIG_SYS_NAND_MASK_CLE;
  216. if (ctrl & NAND_ALE)
  217. IO_ADDR_W |= CONFIG_SYS_NAND_MASK_ALE;
  218. at91_set_gpio_value(CONFIG_SYS_NAND_ENABLE_PIN,
  219. !(ctrl & NAND_NCE));
  220. this->IO_ADDR_W = (void *) IO_ADDR_W;
  221. }
  222. if (cmd != NAND_CMD_NONE)
  223. writeb(cmd, this->IO_ADDR_W);
  224. }
  225. #ifdef CONFIG_SYS_NAND_READY_PIN
  226. static int at91_nand_ready(struct mtd_info *mtd)
  227. {
  228. return at91_get_gpio_value(CONFIG_SYS_NAND_READY_PIN);
  229. }
  230. #endif
  231. int board_nand_init(struct nand_chip *nand)
  232. {
  233. #ifdef CONFIG_ATMEL_NAND_HWECC
  234. static int chip_nr = 0;
  235. struct mtd_info *mtd;
  236. #endif
  237. nand->ecc.mode = NAND_ECC_SOFT;
  238. #ifdef CONFIG_SYS_NAND_DBW_16
  239. nand->options = NAND_BUSWIDTH_16;
  240. #endif
  241. nand->cmd_ctrl = at91_nand_hwcontrol;
  242. #ifdef CONFIG_SYS_NAND_READY_PIN
  243. nand->dev_ready = at91_nand_ready;
  244. #endif
  245. nand->chip_delay = 20;
  246. #ifdef CONFIG_ATMEL_NAND_HWECC
  247. nand->ecc.mode = NAND_ECC_HW;
  248. nand->ecc.calculate = atmel_nand_calculate;
  249. nand->ecc.correct = atmel_nand_correct;
  250. nand->ecc.hwctl = atmel_nand_hwctl;
  251. nand->ecc.read_page = atmel_nand_read_page;
  252. nand->ecc.bytes = 4;
  253. #endif
  254. #ifdef CONFIG_ATMEL_NAND_HWECC
  255. mtd = &nand_info[chip_nr++];
  256. mtd->priv = nand;
  257. /* Detect NAND chips */
  258. if (nand_scan_ident(mtd, 1)) {
  259. printk(KERN_WARNING "NAND Flash not found !\n");
  260. return -ENXIO;
  261. }
  262. if (nand->ecc.mode == NAND_ECC_HW) {
  263. /* ECC is calculated for the whole page (1 step) */
  264. nand->ecc.size = mtd->writesize;
  265. /* set ECC page size and oob layout */
  266. switch (mtd->writesize) {
  267. case 512:
  268. nand->ecc.layout = &atmel_oobinfo_small;
  269. ecc_writel(CONFIG_SYS_NAND_ECC_BASE, MR, ATMEL_ECC_PAGESIZE_528);
  270. break;
  271. case 1024:
  272. nand->ecc.layout = &atmel_oobinfo_large;
  273. ecc_writel(CONFIG_SYS_NAND_ECC_BASE, MR, ATMEL_ECC_PAGESIZE_1056);
  274. break;
  275. case 2048:
  276. nand->ecc.layout = &atmel_oobinfo_large;
  277. ecc_writel(CONFIG_SYS_NAND_ECC_BASE, MR, ATMEL_ECC_PAGESIZE_2112);
  278. break;
  279. case 4096:
  280. nand->ecc.layout = &atmel_oobinfo_large;
  281. ecc_writel(CONFIG_SYS_NAND_ECC_BASE, MR, ATMEL_ECC_PAGESIZE_4224);
  282. break;
  283. default:
  284. /* page size not handled by HW ECC */
  285. /* switching back to soft ECC */
  286. nand->ecc.mode = NAND_ECC_SOFT;
  287. nand->ecc.calculate = NULL;
  288. nand->ecc.correct = NULL;
  289. nand->ecc.hwctl = NULL;
  290. nand->ecc.read_page = NULL;
  291. nand->ecc.postpad = 0;
  292. nand->ecc.prepad = 0;
  293. nand->ecc.bytes = 0;
  294. break;
  295. }
  296. }
  297. #endif
  298. return 0;
  299. }