nand_spl_simple.c 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  1. /*
  2. * (C) Copyright 2006-2008
  3. * Stefan Roese, DENX Software Engineering, sr@denx.de.
  4. *
  5. * This program is free software; you can redistribute it and/or
  6. * modify it under the terms of the GNU General Public License as
  7. * published by the Free Software Foundation; either version 2 of
  8. * the License, or (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program; if not, write to the Free Software
  17. * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
  18. * MA 02111-1307 USA
  19. */
  20. #include <common.h>
  21. #include <nand.h>
  22. #include <asm/io.h>
  23. #include <linux/mtd/nand_ecc.h>
  24. static int nand_ecc_pos[] = CONFIG_SYS_NAND_ECCPOS;
  25. static nand_info_t mtd;
  26. static struct nand_chip nand_chip;
  27. #if (CONFIG_SYS_NAND_PAGE_SIZE <= 512)
  28. /*
  29. * NAND command for small page NAND devices (512)
  30. */
  31. static int nand_command(int block, int page, uint32_t offs,
  32. u8 cmd)
  33. {
  34. struct nand_chip *this = mtd.priv;
  35. int page_addr = page + block * CONFIG_SYS_NAND_PAGE_COUNT;
  36. while (!this->dev_ready(&mtd))
  37. ;
  38. /* Begin command latch cycle */
  39. this->cmd_ctrl(&mtd, cmd, NAND_CTRL_CLE | NAND_CTRL_CHANGE);
  40. /* Set ALE and clear CLE to start address cycle */
  41. /* Column address */
  42. this->cmd_ctrl(&mtd, offs, NAND_CTRL_ALE | NAND_CTRL_CHANGE);
  43. this->cmd_ctrl(&mtd, page_addr & 0xff, NAND_CTRL_ALE); /* A[16:9] */
  44. this->cmd_ctrl(&mtd, (page_addr >> 8) & 0xff,
  45. NAND_CTRL_ALE); /* A[24:17] */
  46. #ifdef CONFIG_SYS_NAND_4_ADDR_CYCLE
  47. /* One more address cycle for devices > 32MiB */
  48. this->cmd_ctrl(&mtd, (page_addr >> 16) & 0x0f,
  49. NAND_CTRL_ALE); /* A[28:25] */
  50. #endif
  51. /* Latch in address */
  52. this->cmd_ctrl(&mtd, NAND_CMD_NONE, NAND_NCE | NAND_CTRL_CHANGE);
  53. /*
  54. * Wait a while for the data to be ready
  55. */
  56. while (!this->dev_ready(&mtd))
  57. ;
  58. return 0;
  59. }
  60. #else
  61. /*
  62. * NAND command for large page NAND devices (2k)
  63. */
  64. static int nand_command(int block, int page, uint32_t offs,
  65. u8 cmd)
  66. {
  67. struct nand_chip *this = mtd.priv;
  68. int page_addr = page + block * CONFIG_SYS_NAND_PAGE_COUNT;
  69. void (*hwctrl)(struct mtd_info *mtd, int cmd,
  70. unsigned int ctrl) = this->cmd_ctrl;
  71. while (!this->dev_ready(&mtd))
  72. ;
  73. /* Emulate NAND_CMD_READOOB */
  74. if (cmd == NAND_CMD_READOOB) {
  75. offs += CONFIG_SYS_NAND_PAGE_SIZE;
  76. cmd = NAND_CMD_READ0;
  77. }
  78. /* Shift the offset from byte addressing to word addressing. */
  79. if (this->options & NAND_BUSWIDTH_16)
  80. offs >>= 1;
  81. /* Begin command latch cycle */
  82. hwctrl(&mtd, cmd, NAND_CTRL_CLE | NAND_CTRL_CHANGE);
  83. /* Set ALE and clear CLE to start address cycle */
  84. /* Column address */
  85. hwctrl(&mtd, offs & 0xff,
  86. NAND_CTRL_ALE | NAND_CTRL_CHANGE); /* A[7:0] */
  87. hwctrl(&mtd, (offs >> 8) & 0xff, NAND_CTRL_ALE); /* A[11:9] */
  88. /* Row address */
  89. hwctrl(&mtd, (page_addr & 0xff), NAND_CTRL_ALE); /* A[19:12] */
  90. hwctrl(&mtd, ((page_addr >> 8) & 0xff),
  91. NAND_CTRL_ALE); /* A[27:20] */
  92. #ifdef CONFIG_SYS_NAND_5_ADDR_CYCLE
  93. /* One more address cycle for devices > 128MiB */
  94. hwctrl(&mtd, (page_addr >> 16) & 0x0f,
  95. NAND_CTRL_ALE); /* A[31:28] */
  96. #endif
  97. /* Latch in address */
  98. hwctrl(&mtd, NAND_CMD_READSTART,
  99. NAND_CTRL_CLE | NAND_CTRL_CHANGE);
  100. hwctrl(&mtd, NAND_CMD_NONE, NAND_NCE | NAND_CTRL_CHANGE);
  101. /*
  102. * Wait a while for the data to be ready
  103. */
  104. while (!this->dev_ready(&mtd))
  105. ;
  106. return 0;
  107. }
  108. #endif
  109. static int nand_is_bad_block(int block)
  110. {
  111. struct nand_chip *this = mtd.priv;
  112. nand_command(block, 0, CONFIG_SYS_NAND_BAD_BLOCK_POS,
  113. NAND_CMD_READOOB);
  114. /*
  115. * Read one byte (or two if it's a 16 bit chip).
  116. */
  117. if (this->options & NAND_BUSWIDTH_16) {
  118. if (readw(this->IO_ADDR_R) != 0xffff)
  119. return 1;
  120. } else {
  121. if (readb(this->IO_ADDR_R) != 0xff)
  122. return 1;
  123. }
  124. return 0;
  125. }
  126. #if defined(CONFIG_SYS_NAND_HW_ECC_OOBFIRST)
  127. static int nand_read_page(int block, int page, uchar *dst)
  128. {
  129. struct nand_chip *this = mtd.priv;
  130. u_char *ecc_calc;
  131. u_char *ecc_code;
  132. u_char *oob_data;
  133. int i;
  134. int eccsize = CONFIG_SYS_NAND_ECCSIZE;
  135. int eccbytes = CONFIG_SYS_NAND_ECCBYTES;
  136. int eccsteps = CONFIG_SYS_NAND_ECCSTEPS;
  137. uint8_t *p = dst;
  138. /*
  139. * No malloc available for now, just use some temporary locations
  140. * in SDRAM
  141. */
  142. ecc_calc = (u_char *)(CONFIG_SYS_SDRAM_BASE + 0x10000);
  143. ecc_code = ecc_calc + 0x100;
  144. oob_data = ecc_calc + 0x200;
  145. nand_command(block, page, 0, NAND_CMD_READOOB);
  146. this->read_buf(&mtd, oob_data, CONFIG_SYS_NAND_OOBSIZE);
  147. nand_command(block, page, 0, NAND_CMD_READ0);
  148. /* Pick the ECC bytes out of the oob data */
  149. for (i = 0; i < CONFIG_SYS_NAND_ECCTOTAL; i++)
  150. ecc_code[i] = oob_data[nand_ecc_pos[i]];
  151. for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
  152. this->ecc.hwctl(&mtd, NAND_ECC_READ);
  153. this->read_buf(&mtd, p, eccsize);
  154. this->ecc.calculate(&mtd, p, &ecc_calc[i]);
  155. this->ecc.correct(&mtd, p, &ecc_code[i], &ecc_calc[i]);
  156. }
  157. return 0;
  158. }
  159. #else
  160. static int nand_read_page(int block, int page, void *dst)
  161. {
  162. struct nand_chip *this = mtd.priv;
  163. u_char *ecc_calc;
  164. u_char *ecc_code;
  165. u_char *oob_data;
  166. int i;
  167. int eccsize = CONFIG_SYS_NAND_ECCSIZE;
  168. int eccbytes = CONFIG_SYS_NAND_ECCBYTES;
  169. int eccsteps = CONFIG_SYS_NAND_ECCSTEPS;
  170. uint8_t *p = dst;
  171. nand_command(block, page, 0, NAND_CMD_READ0);
  172. /* No malloc available for now, just use some temporary locations
  173. * in SDRAM
  174. */
  175. ecc_calc = (u_char *)(CONFIG_SYS_SDRAM_BASE + 0x10000);
  176. ecc_code = ecc_calc + 0x100;
  177. oob_data = ecc_calc + 0x200;
  178. for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
  179. if (this->ecc.mode != NAND_ECC_SOFT)
  180. this->ecc.hwctl(&mtd, NAND_ECC_READ);
  181. this->read_buf(&mtd, p, eccsize);
  182. this->ecc.calculate(&mtd, p, &ecc_calc[i]);
  183. }
  184. this->read_buf(&mtd, oob_data, CONFIG_SYS_NAND_OOBSIZE);
  185. /* Pick the ECC bytes out of the oob data */
  186. for (i = 0; i < CONFIG_SYS_NAND_ECCTOTAL; i++)
  187. ecc_code[i] = oob_data[nand_ecc_pos[i]];
  188. eccsteps = CONFIG_SYS_NAND_ECCSTEPS;
  189. p = dst;
  190. for (i = 0 ; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
  191. /* No chance to do something with the possible error message
  192. * from correct_data(). We just hope that all possible errors
  193. * are corrected by this routine.
  194. */
  195. this->ecc.correct(&mtd, p, &ecc_code[i], &ecc_calc[i]);
  196. }
  197. return 0;
  198. }
  199. #endif
  200. int nand_spl_load_image(uint32_t offs, unsigned int size, void *dst)
  201. {
  202. unsigned int block, lastblock;
  203. unsigned int page;
  204. /*
  205. * offs has to be aligned to a page address!
  206. */
  207. block = offs / CONFIG_SYS_NAND_BLOCK_SIZE;
  208. lastblock = (offs + size - 1) / CONFIG_SYS_NAND_BLOCK_SIZE;
  209. page = (offs % CONFIG_SYS_NAND_BLOCK_SIZE) / CONFIG_SYS_NAND_PAGE_SIZE;
  210. while (block <= lastblock) {
  211. if (!nand_is_bad_block(block)) {
  212. /*
  213. * Skip bad blocks
  214. */
  215. while (page < CONFIG_SYS_NAND_PAGE_COUNT) {
  216. nand_read_page(block, page, dst);
  217. dst += CONFIG_SYS_NAND_PAGE_SIZE;
  218. page++;
  219. }
  220. page = 0;
  221. } else {
  222. lastblock++;
  223. }
  224. block++;
  225. }
  226. return 0;
  227. }
  228. /* nand_init() - initialize data to make nand usable by SPL */
  229. void nand_init(void)
  230. {
  231. /*
  232. * Init board specific nand support
  233. */
  234. mtd.priv = &nand_chip;
  235. nand_chip.IO_ADDR_R = nand_chip.IO_ADDR_W =
  236. (void __iomem *)CONFIG_SYS_NAND_BASE;
  237. board_nand_init(&nand_chip);
  238. #ifdef CONFIG_SPL_NAND_SOFTECC
  239. if (nand_chip.ecc.mode == NAND_ECC_SOFT) {
  240. nand_chip.ecc.calculate = nand_calculate_ecc;
  241. nand_chip.ecc.correct = nand_correct_data;
  242. }
  243. #endif
  244. if (nand_chip.select_chip)
  245. nand_chip.select_chip(&mtd, 0);
  246. }
  247. /* Unselect after operation */
  248. void nand_deselect(void)
  249. {
  250. if (nand_chip.select_chip)
  251. nand_chip.select_chip(&mtd, -1);
  252. }