atmel_nand.c 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372
  1. /*
  2. * (C) Copyright 2007-2008
  3. * Stelian Pop <stelian@popies.net>
  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. unsigned int ecc_value;
  75. /* get the first 2 ECC bytes */
  76. ecc_value = ecc_readl(CONFIG_SYS_NAND_ECC_BASE, PR);
  77. ecc_code[0] = ecc_value & 0xFF;
  78. ecc_code[1] = (ecc_value >> 8) & 0xFF;
  79. /* get the last 2 ECC bytes */
  80. ecc_value = ecc_readl(CONFIG_SYS_NAND_ECC_BASE, NPR) & ATMEL_ECC_NPARITY;
  81. ecc_code[2] = ecc_value & 0xFF;
  82. ecc_code[3] = (ecc_value >> 8) & 0xFF;
  83. return 0;
  84. }
  85. /*
  86. * HW ECC read page function
  87. *
  88. * mtd: mtd info structure
  89. * chip: nand chip info structure
  90. * buf: buffer to store read data
  91. */
  92. static int atmel_nand_read_page(struct mtd_info *mtd,
  93. struct nand_chip *chip, uint8_t *buf, int page)
  94. {
  95. int eccsize = chip->ecc.size;
  96. int eccbytes = chip->ecc.bytes;
  97. uint32_t *eccpos = chip->ecc.layout->eccpos;
  98. uint8_t *p = buf;
  99. uint8_t *oob = chip->oob_poi;
  100. uint8_t *ecc_pos;
  101. int stat;
  102. /* read the page */
  103. chip->read_buf(mtd, p, eccsize);
  104. /* move to ECC position if needed */
  105. if (eccpos[0] != 0) {
  106. /* This only works on large pages
  107. * because the ECC controller waits for
  108. * NAND_CMD_RNDOUTSTART after the
  109. * NAND_CMD_RNDOUT.
  110. * anyway, for small pages, the eccpos[0] == 0
  111. */
  112. chip->cmdfunc(mtd, NAND_CMD_RNDOUT,
  113. mtd->writesize + eccpos[0], -1);
  114. }
  115. /* the ECC controller needs to read the ECC just after the data */
  116. ecc_pos = oob + eccpos[0];
  117. chip->read_buf(mtd, ecc_pos, eccbytes);
  118. /* check if there's an error */
  119. stat = chip->ecc.correct(mtd, p, oob, NULL);
  120. if (stat < 0)
  121. mtd->ecc_stats.failed++;
  122. else
  123. mtd->ecc_stats.corrected += stat;
  124. /* get back to oob start (end of page) */
  125. chip->cmdfunc(mtd, NAND_CMD_RNDOUT, mtd->writesize, -1);
  126. /* read the oob */
  127. chip->read_buf(mtd, oob, mtd->oobsize);
  128. return 0;
  129. }
  130. /*
  131. * HW ECC Correction
  132. *
  133. * function called after a read
  134. *
  135. * mtd: MTD block structure
  136. * dat: raw data read from the chip
  137. * read_ecc: ECC from the chip (unused)
  138. * isnull: unused
  139. *
  140. * Detect and correct a 1 bit error for a page
  141. */
  142. static int atmel_nand_correct(struct mtd_info *mtd, u_char *dat,
  143. u_char *read_ecc, u_char *isnull)
  144. {
  145. struct nand_chip *nand_chip = mtd->priv;
  146. unsigned int ecc_status;
  147. unsigned int ecc_word, ecc_bit;
  148. /* get the status from the Status Register */
  149. ecc_status = ecc_readl(CONFIG_SYS_NAND_ECC_BASE, SR);
  150. /* if there's no error */
  151. if (likely(!(ecc_status & ATMEL_ECC_RECERR)))
  152. return 0;
  153. /* get error bit offset (4 bits) */
  154. ecc_bit = ecc_readl(CONFIG_SYS_NAND_ECC_BASE, PR) & ATMEL_ECC_BITADDR;
  155. /* get word address (12 bits) */
  156. ecc_word = ecc_readl(CONFIG_SYS_NAND_ECC_BASE, PR) & ATMEL_ECC_WORDADDR;
  157. ecc_word >>= 4;
  158. /* if there are multiple errors */
  159. if (ecc_status & ATMEL_ECC_MULERR) {
  160. /* check if it is a freshly erased block
  161. * (filled with 0xff) */
  162. if ((ecc_bit == ATMEL_ECC_BITADDR)
  163. && (ecc_word == (ATMEL_ECC_WORDADDR >> 4))) {
  164. /* the block has just been erased, return OK */
  165. return 0;
  166. }
  167. /* it doesn't seems to be a freshly
  168. * erased block.
  169. * We can't correct so many errors */
  170. printk(KERN_WARNING "atmel_nand : multiple errors detected."
  171. " Unable to correct.\n");
  172. return -EIO;
  173. }
  174. /* if there's a single bit error : we can correct it */
  175. if (ecc_status & ATMEL_ECC_ECCERR) {
  176. /* there's nothing much to do here.
  177. * the bit error is on the ECC itself.
  178. */
  179. printk(KERN_WARNING "atmel_nand : one bit error on ECC code."
  180. " Nothing to correct\n");
  181. return 0;
  182. }
  183. printk(KERN_WARNING "atmel_nand : one bit error on data."
  184. " (word offset in the page :"
  185. " 0x%x bit offset : 0x%x)\n",
  186. ecc_word, ecc_bit);
  187. /* correct the error */
  188. if (nand_chip->options & NAND_BUSWIDTH_16) {
  189. /* 16 bits words */
  190. ((unsigned short *) dat)[ecc_word] ^= (1 << ecc_bit);
  191. } else {
  192. /* 8 bits words */
  193. dat[ecc_word] ^= (1 << ecc_bit);
  194. }
  195. printk(KERN_WARNING "atmel_nand : error corrected\n");
  196. return 1;
  197. }
  198. /*
  199. * Enable HW ECC : unused on most chips
  200. */
  201. static void atmel_nand_hwctl(struct mtd_info *mtd, int mode)
  202. {
  203. }
  204. int atmel_hwecc_nand_init_param(struct nand_chip *nand, struct mtd_info *mtd)
  205. {
  206. nand->ecc.mode = NAND_ECC_HW;
  207. nand->ecc.calculate = atmel_nand_calculate;
  208. nand->ecc.correct = atmel_nand_correct;
  209. nand->ecc.hwctl = atmel_nand_hwctl;
  210. nand->ecc.read_page = atmel_nand_read_page;
  211. nand->ecc.bytes = 4;
  212. if (nand->ecc.mode == NAND_ECC_HW) {
  213. /* ECC is calculated for the whole page (1 step) */
  214. nand->ecc.size = mtd->writesize;
  215. /* set ECC page size and oob layout */
  216. switch (mtd->writesize) {
  217. case 512:
  218. nand->ecc.layout = &atmel_oobinfo_small;
  219. ecc_writel(CONFIG_SYS_NAND_ECC_BASE, MR,
  220. ATMEL_ECC_PAGESIZE_528);
  221. break;
  222. case 1024:
  223. nand->ecc.layout = &atmel_oobinfo_large;
  224. ecc_writel(CONFIG_SYS_NAND_ECC_BASE, MR,
  225. ATMEL_ECC_PAGESIZE_1056);
  226. break;
  227. case 2048:
  228. nand->ecc.layout = &atmel_oobinfo_large;
  229. ecc_writel(CONFIG_SYS_NAND_ECC_BASE, MR,
  230. ATMEL_ECC_PAGESIZE_2112);
  231. break;
  232. case 4096:
  233. nand->ecc.layout = &atmel_oobinfo_large;
  234. ecc_writel(CONFIG_SYS_NAND_ECC_BASE, MR,
  235. ATMEL_ECC_PAGESIZE_4224);
  236. break;
  237. default:
  238. /* page size not handled by HW ECC */
  239. /* switching back to soft ECC */
  240. nand->ecc.mode = NAND_ECC_SOFT;
  241. nand->ecc.calculate = NULL;
  242. nand->ecc.correct = NULL;
  243. nand->ecc.hwctl = NULL;
  244. nand->ecc.read_page = NULL;
  245. nand->ecc.postpad = 0;
  246. nand->ecc.prepad = 0;
  247. nand->ecc.bytes = 0;
  248. break;
  249. }
  250. }
  251. return 0;
  252. }
  253. #endif
  254. static void at91_nand_hwcontrol(struct mtd_info *mtd,
  255. int cmd, unsigned int ctrl)
  256. {
  257. struct nand_chip *this = mtd->priv;
  258. if (ctrl & NAND_CTRL_CHANGE) {
  259. ulong IO_ADDR_W = (ulong) this->IO_ADDR_W;
  260. IO_ADDR_W &= ~(CONFIG_SYS_NAND_MASK_ALE
  261. | CONFIG_SYS_NAND_MASK_CLE);
  262. if (ctrl & NAND_CLE)
  263. IO_ADDR_W |= CONFIG_SYS_NAND_MASK_CLE;
  264. if (ctrl & NAND_ALE)
  265. IO_ADDR_W |= CONFIG_SYS_NAND_MASK_ALE;
  266. #ifdef CONFIG_SYS_NAND_ENABLE_PIN
  267. at91_set_gpio_value(CONFIG_SYS_NAND_ENABLE_PIN,
  268. !(ctrl & NAND_NCE));
  269. #endif
  270. this->IO_ADDR_W = (void *) IO_ADDR_W;
  271. }
  272. if (cmd != NAND_CMD_NONE)
  273. writeb(cmd, this->IO_ADDR_W);
  274. }
  275. #ifdef CONFIG_SYS_NAND_READY_PIN
  276. static int at91_nand_ready(struct mtd_info *mtd)
  277. {
  278. return at91_get_gpio_value(CONFIG_SYS_NAND_READY_PIN);
  279. }
  280. #endif
  281. #ifndef CONFIG_SYS_NAND_BASE_LIST
  282. #define CONFIG_SYS_NAND_BASE_LIST { CONFIG_SYS_NAND_BASE }
  283. #endif
  284. static struct nand_chip nand_chip[CONFIG_SYS_MAX_NAND_DEVICE];
  285. static ulong base_addr[CONFIG_SYS_MAX_NAND_DEVICE] = CONFIG_SYS_NAND_BASE_LIST;
  286. int atmel_nand_chip_init(int devnum, ulong base_addr)
  287. {
  288. int ret;
  289. struct mtd_info *mtd = &nand_info[devnum];
  290. struct nand_chip *nand = &nand_chip[devnum];
  291. mtd->priv = nand;
  292. nand->IO_ADDR_R = nand->IO_ADDR_W = (void __iomem *)base_addr;
  293. nand->ecc.mode = NAND_ECC_SOFT;
  294. #ifdef CONFIG_SYS_NAND_DBW_16
  295. nand->options = NAND_BUSWIDTH_16;
  296. #endif
  297. nand->cmd_ctrl = at91_nand_hwcontrol;
  298. #ifdef CONFIG_SYS_NAND_READY_PIN
  299. nand->dev_ready = at91_nand_ready;
  300. #endif
  301. nand->chip_delay = 20;
  302. ret = nand_scan_ident(mtd, CONFIG_SYS_NAND_MAX_CHIPS, NULL);
  303. if (ret)
  304. return ret;
  305. #ifdef CONFIG_ATMEL_NAND_HWECC
  306. ret = atmel_hwecc_nand_init_param(nand, mtd);
  307. if (ret)
  308. return ret;
  309. #endif
  310. ret = nand_scan_tail(mtd);
  311. if (!ret)
  312. nand_register(devnum);
  313. return ret;
  314. }
  315. void board_nand_init(void)
  316. {
  317. int i;
  318. for (i = 0; i < CONFIG_SYS_MAX_NAND_DEVICE; i++)
  319. if (atmel_nand_chip_init(i, base_addr[i]))
  320. printk(KERN_ERR "atmel_nand: Fail to initialize #%d chip",
  321. i);
  322. }