nand_boot.c 6.7 KB

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