nand_spl_simple.c 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  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. static int nand_ecc_pos[] = CONFIG_SYS_NAND_ECCPOS;
  24. static nand_info_t mtd;
  25. static struct nand_chip nand_chip;
  26. #if (CONFIG_SYS_NAND_PAGE_SIZE <= 512)
  27. /*
  28. * NAND command for small page NAND devices (512)
  29. */
  30. static int nand_command(int block, int page, uint32_t offs,
  31. u8 cmd)
  32. {
  33. struct nand_chip *this = mtd.priv;
  34. int page_addr = page + block * CONFIG_SYS_NAND_PAGE_COUNT;
  35. while (!this->dev_ready(&mtd))
  36. ;
  37. /* Begin command latch cycle */
  38. this->cmd_ctrl(&mtd, cmd, NAND_CTRL_CLE | NAND_CTRL_CHANGE);
  39. /* Set ALE and clear CLE to start address cycle */
  40. /* Column address */
  41. this->cmd_ctrl(&mtd, offs, NAND_CTRL_ALE | NAND_CTRL_CHANGE);
  42. this->cmd_ctrl(&mtd, page_addr & 0xff, NAND_CTRL_ALE); /* A[16:9] */
  43. this->cmd_ctrl(&mtd, (page_addr >> 8) & 0xff,
  44. NAND_CTRL_ALE); /* A[24:17] */
  45. #ifdef CONFIG_SYS_NAND_4_ADDR_CYCLE
  46. /* One more address cycle for devices > 32MiB */
  47. this->cmd_ctrl(&mtd, (page_addr >> 16) & 0x0f,
  48. NAND_CTRL_ALE); /* A[28:25] */
  49. #endif
  50. /* Latch in address */
  51. this->cmd_ctrl(&mtd, NAND_CMD_NONE, NAND_NCE | NAND_CTRL_CHANGE);
  52. /*
  53. * Wait a while for the data to be ready
  54. */
  55. while (!this->dev_ready(&mtd))
  56. ;
  57. return 0;
  58. }
  59. #else
  60. /*
  61. * NAND command for large page NAND devices (2k)
  62. */
  63. static int nand_command(int block, int page, uint32_t offs,
  64. u8 cmd)
  65. {
  66. struct nand_chip *this = mtd.priv;
  67. int page_addr = page + block * CONFIG_SYS_NAND_PAGE_COUNT;
  68. void (*hwctrl)(struct mtd_info *mtd, int cmd,
  69. unsigned int ctrl) = this->cmd_ctrl;
  70. while (!this->dev_ready(&mtd))
  71. ;
  72. /* Emulate NAND_CMD_READOOB */
  73. if (cmd == NAND_CMD_READOOB) {
  74. offs += CONFIG_SYS_NAND_PAGE_SIZE;
  75. cmd = NAND_CMD_READ0;
  76. }
  77. /* Shift the offset from byte addressing to word addressing. */
  78. if (this->options & NAND_BUSWIDTH_16)
  79. offs >>= 1;
  80. /* Begin command latch cycle */
  81. hwctrl(&mtd, cmd, NAND_CTRL_CLE | NAND_CTRL_CHANGE);
  82. /* Set ALE and clear CLE to start address cycle */
  83. /* Column address */
  84. hwctrl(&mtd, offs & 0xff,
  85. NAND_CTRL_ALE | NAND_CTRL_CHANGE); /* A[7:0] */
  86. hwctrl(&mtd, (offs >> 8) & 0xff, NAND_CTRL_ALE); /* A[11:9] */
  87. /* Row address */
  88. hwctrl(&mtd, (page_addr & 0xff), NAND_CTRL_ALE); /* A[19:12] */
  89. hwctrl(&mtd, ((page_addr >> 8) & 0xff),
  90. NAND_CTRL_ALE); /* A[27:20] */
  91. #ifdef CONFIG_SYS_NAND_5_ADDR_CYCLE
  92. /* One more address cycle for devices > 128MiB */
  93. hwctrl(&mtd, (page_addr >> 16) & 0x0f,
  94. NAND_CTRL_ALE); /* A[31:28] */
  95. #endif
  96. /* Latch in address */
  97. hwctrl(&mtd, NAND_CMD_READSTART,
  98. NAND_CTRL_CLE | NAND_CTRL_CHANGE);
  99. hwctrl(&mtd, NAND_CMD_NONE, NAND_NCE | NAND_CTRL_CHANGE);
  100. /*
  101. * Wait a while for the data to be ready
  102. */
  103. while (!this->dev_ready(&mtd))
  104. ;
  105. return 0;
  106. }
  107. #endif
  108. static int nand_is_bad_block(int block)
  109. {
  110. struct nand_chip *this = mtd.priv;
  111. nand_command(block, 0, CONFIG_SYS_NAND_BAD_BLOCK_POS,
  112. NAND_CMD_READOOB);
  113. /*
  114. * Read one byte (or two if it's a 16 bit chip).
  115. */
  116. if (this->options & NAND_BUSWIDTH_16) {
  117. if (readw(this->IO_ADDR_R) != 0xffff)
  118. return 1;
  119. } else {
  120. if (readb(this->IO_ADDR_R) != 0xff)
  121. return 1;
  122. }
  123. return 0;
  124. }
  125. #if defined(CONFIG_SYS_NAND_HW_ECC_OOBFIRST)
  126. static int nand_read_page(int block, int page, uchar *dst)
  127. {
  128. struct nand_chip *this = mtd.priv;
  129. u_char *ecc_calc;
  130. u_char *ecc_code;
  131. u_char *oob_data;
  132. int i;
  133. int eccsize = CONFIG_SYS_NAND_ECCSIZE;
  134. int eccbytes = CONFIG_SYS_NAND_ECCBYTES;
  135. int eccsteps = CONFIG_SYS_NAND_ECCSTEPS;
  136. uint8_t *p = dst;
  137. int stat;
  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. stat = 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. int stat;
  172. nand_command(block, page, 0, NAND_CMD_READ0);
  173. /* No malloc available for now, just use some temporary locations
  174. * in SDRAM
  175. */
  176. ecc_calc = (u_char *)(CONFIG_SYS_SDRAM_BASE + 0x10000);
  177. ecc_code = ecc_calc + 0x100;
  178. oob_data = ecc_calc + 0x200;
  179. for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
  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. stat = 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. if (nand_chip.select_chip)
  239. nand_chip.select_chip(&mtd, 0);
  240. }
  241. /* Unselect after operation */
  242. void nand_deselect(void)
  243. {
  244. if (nand_chip.select_chip)
  245. nand_chip.select_chip(&mtd, -1);
  246. }