nand_spl_simple.c 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  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. static int nand_read_page(int block, int page, void *dst)
  126. {
  127. struct nand_chip *this = mtd.priv;
  128. u_char *ecc_calc;
  129. u_char *ecc_code;
  130. u_char *oob_data;
  131. int i;
  132. int eccsize = CONFIG_SYS_NAND_ECCSIZE;
  133. int eccbytes = CONFIG_SYS_NAND_ECCBYTES;
  134. int eccsteps = CONFIG_SYS_NAND_ECCSTEPS;
  135. uint8_t *p = dst;
  136. int stat;
  137. nand_command(block, page, 0, NAND_CMD_READ0);
  138. /* No malloc available for now, just use some temporary locations
  139. * in SDRAM
  140. */
  141. ecc_calc = (u_char *)(CONFIG_SYS_SDRAM_BASE + 0x10000);
  142. ecc_code = ecc_calc + 0x100;
  143. oob_data = ecc_calc + 0x200;
  144. for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
  145. this->ecc.hwctl(&mtd, NAND_ECC_READ);
  146. this->read_buf(&mtd, p, eccsize);
  147. this->ecc.calculate(&mtd, p, &ecc_calc[i]);
  148. }
  149. this->read_buf(&mtd, oob_data, CONFIG_SYS_NAND_OOBSIZE);
  150. /* Pick the ECC bytes out of the oob data */
  151. for (i = 0; i < CONFIG_SYS_NAND_ECCTOTAL; i++)
  152. ecc_code[i] = oob_data[nand_ecc_pos[i]];
  153. eccsteps = CONFIG_SYS_NAND_ECCSTEPS;
  154. p = dst;
  155. for (i = 0 ; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
  156. /* No chance to do something with the possible error message
  157. * from correct_data(). We just hope that all possible errors
  158. * are corrected by this routine.
  159. */
  160. stat = this->ecc.correct(&mtd, p, &ecc_code[i], &ecc_calc[i]);
  161. }
  162. return 0;
  163. }
  164. int nand_spl_load_image(uint32_t offs, unsigned int size, void *dst)
  165. {
  166. unsigned int block, lastblock;
  167. unsigned int page;
  168. /*
  169. * offs has to be aligned to a page address!
  170. */
  171. block = offs / CONFIG_SYS_NAND_BLOCK_SIZE;
  172. lastblock = (offs + size - 1) / CONFIG_SYS_NAND_BLOCK_SIZE;
  173. page = (offs % CONFIG_SYS_NAND_BLOCK_SIZE) / CONFIG_SYS_NAND_PAGE_SIZE;
  174. while (block <= lastblock) {
  175. if (!nand_is_bad_block(block)) {
  176. /*
  177. * Skip bad blocks
  178. */
  179. while (page < CONFIG_SYS_NAND_PAGE_COUNT) {
  180. nand_read_page(block, page, dst);
  181. dst += CONFIG_SYS_NAND_PAGE_SIZE;
  182. page++;
  183. }
  184. page = 0;
  185. } else {
  186. lastblock++;
  187. }
  188. block++;
  189. }
  190. return 0;
  191. }
  192. /* nand_init() - initialize data to make nand usable by SPL */
  193. void nand_init(void)
  194. {
  195. /*
  196. * Init board specific nand support
  197. */
  198. mtd.priv = &nand_chip;
  199. nand_chip.IO_ADDR_R = nand_chip.IO_ADDR_W =
  200. (void __iomem *)CONFIG_SYS_NAND_BASE;
  201. nand_chip.options = 0;
  202. board_nand_init(&nand_chip);
  203. if (nand_chip.select_chip)
  204. nand_chip.select_chip(&mtd, 0);
  205. }
  206. /* Unselect after operation */
  207. void nand_deselect(void)
  208. {
  209. if (nand_chip.select_chip)
  210. nand_chip.select_chip(&mtd, -1);
  211. }