nand_boot.c 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  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. #define CFG_NAND_READ_DELAY \
  23. { volatile int dummy; int i; for (i=0; i<10000; i++) dummy = i; }
  24. static int nand_ecc_pos[] = CFG_NAND_ECCPOS;
  25. extern void board_nand_init(struct nand_chip *nand);
  26. #if (CFG_NAND_PAGE_SIZE <= 512)
  27. /*
  28. * NAND command for small page NAND devices (512)
  29. */
  30. static int nand_command(struct mtd_info *mtd, int block, int page, int offs, u8 cmd)
  31. {
  32. struct nand_chip *this = mtd->priv;
  33. int page_addr = page + block * CFG_NAND_PAGE_COUNT;
  34. if (this->dev_ready)
  35. this->dev_ready(mtd);
  36. else
  37. CFG_NAND_READ_DELAY;
  38. /* Begin command latch cycle */
  39. this->hwcontrol(mtd, NAND_CTL_SETCLE);
  40. this->write_byte(mtd, cmd);
  41. /* Set ALE and clear CLE to start address cycle */
  42. this->hwcontrol(mtd, NAND_CTL_CLRCLE);
  43. this->hwcontrol(mtd, NAND_CTL_SETALE);
  44. /* Column address */
  45. this->write_byte(mtd, offs); /* A[7:0] */
  46. this->write_byte(mtd, (uchar)(page_addr & 0xff)); /* A[16:9] */
  47. this->write_byte(mtd, (uchar)((page_addr >> 8) & 0xff)); /* A[24:17] */
  48. #ifdef CFG_NAND_4_ADDR_CYCLE
  49. /* One more address cycle for devices > 32MiB */
  50. this->write_byte(mtd, (uchar)((page_addr >> 16) & 0x0f)); /* A[xx:25] */
  51. #endif
  52. /* Latch in address */
  53. this->hwcontrol(mtd, NAND_CTL_CLRALE);
  54. /*
  55. * Wait a while for the data to be ready
  56. */
  57. if (this->dev_ready)
  58. this->dev_ready(mtd);
  59. else
  60. CFG_NAND_READ_DELAY;
  61. return 0;
  62. }
  63. #else
  64. /*
  65. * NAND command for large page NAND devices (2k)
  66. */
  67. static int nand_command(struct mtd_info *mtd, int block, int page, int offs, u8 cmd)
  68. {
  69. struct nand_chip *this = mtd->priv;
  70. int page_offs = offs;
  71. int page_addr = page + block * CFG_NAND_PAGE_COUNT;
  72. if (this->dev_ready)
  73. this->dev_ready(mtd);
  74. else
  75. CFG_NAND_READ_DELAY;
  76. /* Emulate NAND_CMD_READOOB */
  77. if (cmd == NAND_CMD_READOOB) {
  78. page_offs += CFG_NAND_PAGE_SIZE;
  79. cmd = NAND_CMD_READ0;
  80. }
  81. /* Begin command latch cycle */
  82. this->hwcontrol(mtd, NAND_CTL_SETCLE);
  83. this->write_byte(mtd, cmd);
  84. /* Set ALE and clear CLE to start address cycle */
  85. this->hwcontrol(mtd, NAND_CTL_CLRCLE);
  86. this->hwcontrol(mtd, NAND_CTL_SETALE);
  87. /* Column address */
  88. this->write_byte(mtd, page_offs & 0xff); /* A[7:0] */
  89. this->write_byte(mtd, (uchar)((page_offs >> 8) & 0xff)); /* A[11:9] */
  90. /* Row address */
  91. this->write_byte(mtd, (uchar)(page_addr & 0xff)); /* A[19:12] */
  92. this->write_byte(mtd, (uchar)((page_addr >> 8) & 0xff)); /* A[27:20] */
  93. #ifdef CFG_NAND_5_ADDR_CYCLE
  94. /* One more address cycle for devices > 128MiB */
  95. this->write_byte(mtd, (uchar)((page_addr >> 16) & 0x0f)); /* A[xx:28] */
  96. #endif
  97. /* Latch in address */
  98. this->hwcontrol(mtd, NAND_CTL_CLRALE);
  99. /* Begin command latch cycle */
  100. this->hwcontrol(mtd, NAND_CTL_SETCLE);
  101. /* Write out the start read command */
  102. this->write_byte(mtd, NAND_CMD_READSTART);
  103. /* End command latch cycle */
  104. this->hwcontrol(mtd, NAND_CTL_CLRCLE);
  105. /*
  106. * Wait a while for the data to be ready
  107. */
  108. if (this->dev_ready)
  109. this->dev_ready(mtd);
  110. else
  111. CFG_NAND_READ_DELAY;
  112. return 0;
  113. }
  114. #endif
  115. static int nand_is_bad_block(struct mtd_info *mtd, int block)
  116. {
  117. struct nand_chip *this = mtd->priv;
  118. nand_command(mtd, block, 0, CFG_NAND_BAD_BLOCK_POS, NAND_CMD_READOOB);
  119. /*
  120. * Read one byte
  121. */
  122. if (this->read_byte(mtd) != 0xff)
  123. return 1;
  124. return 0;
  125. }
  126. static int nand_read_page(struct mtd_info *mtd, 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 = CFG_NAND_ECCSIZE;
  134. int eccbytes = CFG_NAND_ECCBYTES;
  135. int eccsteps = CFG_NAND_ECCSTEPS;
  136. uint8_t *p = dst;
  137. int stat;
  138. nand_command(mtd, block, page, 0, NAND_CMD_READ0);
  139. /* No malloc available for now, just use some temporary locations
  140. * in SDRAM
  141. */
  142. ecc_calc = (u_char *)(CFG_SDRAM_BASE + 0x10000);
  143. ecc_code = ecc_calc + 0x100;
  144. oob_data = ecc_calc + 0x200;
  145. for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
  146. this->enable_hwecc(mtd, NAND_ECC_READ);
  147. this->read_buf(mtd, p, eccsize);
  148. this->calculate_ecc(mtd, p, &ecc_calc[i]);
  149. }
  150. this->read_buf(mtd, oob_data, CFG_NAND_OOBSIZE);
  151. /* Pick the ECC bytes out of the oob data */
  152. for (i = 0; i < CFG_NAND_ECCTOTAL; i++)
  153. ecc_code[i] = oob_data[nand_ecc_pos[i]];
  154. eccsteps = CFG_NAND_ECCSTEPS;
  155. p = dst;
  156. for (i = 0 ; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
  157. /* No chance to do something with the possible error message
  158. * from correct_data(). We just hope that all possible errors
  159. * are corrected by this routine.
  160. */
  161. stat = this->correct_data(mtd, p, &ecc_code[i], &ecc_calc[i]);
  162. }
  163. return 0;
  164. }
  165. static int nand_load(struct mtd_info *mtd, int offs, int uboot_size, uchar *dst)
  166. {
  167. int block;
  168. int blockcopy_count;
  169. int page;
  170. /*
  171. * offs has to be aligned to a block address!
  172. */
  173. block = offs / CFG_NAND_BLOCK_SIZE;
  174. blockcopy_count = 0;
  175. while (blockcopy_count < (uboot_size / CFG_NAND_BLOCK_SIZE)) {
  176. if (!nand_is_bad_block(mtd, block)) {
  177. /*
  178. * Skip bad blocks
  179. */
  180. for (page = 0; page < CFG_NAND_PAGE_COUNT; page++) {
  181. nand_read_page(mtd, block, page, dst);
  182. dst += CFG_NAND_PAGE_SIZE;
  183. }
  184. blockcopy_count++;
  185. }
  186. block++;
  187. }
  188. return 0;
  189. }
  190. void nand_boot(void)
  191. {
  192. ulong mem_size;
  193. struct nand_chip nand_chip;
  194. nand_info_t nand_info;
  195. int ret;
  196. void (*uboot)(void);
  197. /*
  198. * Init sdram, so we have access to memory
  199. */
  200. mem_size = initdram(0);
  201. /*
  202. * Init board specific nand support
  203. */
  204. nand_info.priv = &nand_chip;
  205. nand_chip.IO_ADDR_R = nand_chip.IO_ADDR_W = (void __iomem *)CFG_NAND_BASE;
  206. nand_chip.dev_ready = NULL; /* preset to NULL */
  207. board_nand_init(&nand_chip);
  208. /*
  209. * Load U-Boot image from NAND into RAM
  210. */
  211. ret = nand_load(&nand_info, CFG_NAND_U_BOOT_OFFS, CFG_NAND_U_BOOT_SIZE,
  212. (uchar *)CFG_NAND_U_BOOT_DST);
  213. /*
  214. * Jump to U-Boot image
  215. */
  216. uboot = (void (*)(void))CFG_NAND_U_BOOT_START;
  217. (*uboot)();
  218. }