nand_boot.c 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  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 CONFIG_SYS_NAND_READ_DELAY \
  24. { volatile int dummy; int i; for (i=0; i<10000; i++) dummy = i; }
  25. static int nand_ecc_pos[] = CONFIG_SYS_NAND_ECCPOS;
  26. #if (CONFIG_SYS_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 * CONFIG_SYS_NAND_PAGE_COUNT;
  34. if (this->dev_ready)
  35. while (!this->dev_ready(mtd))
  36. ;
  37. else
  38. CONFIG_SYS_NAND_READ_DELAY;
  39. /* Begin command latch cycle */
  40. this->cmd_ctrl(mtd, cmd, NAND_CTRL_CLE | NAND_CTRL_CHANGE);
  41. /* Set ALE and clear CLE to start address cycle */
  42. /* Column address */
  43. this->cmd_ctrl(mtd, offs, NAND_CTRL_ALE | NAND_CTRL_CHANGE);
  44. this->cmd_ctrl(mtd, page_addr & 0xff, NAND_CTRL_ALE); /* A[16:9] */
  45. this->cmd_ctrl(mtd, (page_addr >> 8) & 0xff,
  46. NAND_CTRL_ALE); /* A[24:17] */
  47. #ifdef CONFIG_SYS_NAND_4_ADDR_CYCLE
  48. /* One more address cycle for devices > 32MiB */
  49. this->cmd_ctrl(mtd, (page_addr >> 16) & 0x0f,
  50. NAND_CTRL_ALE); /* A[28:25] */
  51. #endif
  52. /* Latch in address */
  53. this->cmd_ctrl(mtd, NAND_CMD_NONE, NAND_NCE | NAND_CTRL_CHANGE);
  54. /*
  55. * Wait a while for the data to be ready
  56. */
  57. if (this->dev_ready)
  58. while (!this->dev_ready(mtd))
  59. ;
  60. else
  61. CONFIG_SYS_NAND_READ_DELAY;
  62. return 0;
  63. }
  64. #else
  65. /*
  66. * NAND command for large page NAND devices (2k)
  67. */
  68. static int nand_command(struct mtd_info *mtd, int block, int page, int offs, u8 cmd)
  69. {
  70. struct nand_chip *this = mtd->priv;
  71. int page_addr = page + block * CONFIG_SYS_NAND_PAGE_COUNT;
  72. if (this->dev_ready)
  73. while (!this->dev_ready(mtd))
  74. ;
  75. else
  76. CONFIG_SYS_NAND_READ_DELAY;
  77. /* Emulate NAND_CMD_READOOB */
  78. if (cmd == NAND_CMD_READOOB) {
  79. offs += CONFIG_SYS_NAND_PAGE_SIZE;
  80. cmd = NAND_CMD_READ0;
  81. }
  82. /* Shift the offset from byte addressing to word addressing. */
  83. if (this->options & NAND_BUSWIDTH_16)
  84. offs >>= 1;
  85. /* Begin command latch cycle */
  86. this->cmd_ctrl(mtd, cmd, NAND_CTRL_CLE | NAND_CTRL_CHANGE);
  87. /* Set ALE and clear CLE to start address cycle */
  88. /* Column address */
  89. this->cmd_ctrl(mtd, offs & 0xff,
  90. NAND_CTRL_ALE | NAND_CTRL_CHANGE); /* A[7:0] */
  91. this->cmd_ctrl(mtd, (offs >> 8) & 0xff, NAND_CTRL_ALE); /* A[11:9] */
  92. /* Row address */
  93. this->cmd_ctrl(mtd, (page_addr & 0xff), NAND_CTRL_ALE); /* A[19:12] */
  94. this->cmd_ctrl(mtd, ((page_addr >> 8) & 0xff),
  95. NAND_CTRL_ALE); /* A[27:20] */
  96. #ifdef CONFIG_SYS_NAND_5_ADDR_CYCLE
  97. /* One more address cycle for devices > 128MiB */
  98. this->cmd_ctrl(mtd, (page_addr >> 16) & 0x0f,
  99. NAND_CTRL_ALE); /* A[31:28] */
  100. #endif
  101. /* Latch in address */
  102. this->cmd_ctrl(mtd, NAND_CMD_READSTART,
  103. NAND_CTRL_CLE | NAND_CTRL_CHANGE);
  104. this->cmd_ctrl(mtd, NAND_CMD_NONE, NAND_NCE | NAND_CTRL_CHANGE);
  105. /*
  106. * Wait a while for the data to be ready
  107. */
  108. if (this->dev_ready)
  109. while (!this->dev_ready(mtd))
  110. ;
  111. else
  112. CONFIG_SYS_NAND_READ_DELAY;
  113. return 0;
  114. }
  115. #endif
  116. static int nand_is_bad_block(struct mtd_info *mtd, int block)
  117. {
  118. struct nand_chip *this = mtd->priv;
  119. nand_command(mtd, block, 0, CONFIG_SYS_NAND_BAD_BLOCK_POS, NAND_CMD_READOOB);
  120. /*
  121. * Read one byte
  122. */
  123. if (readb(this->IO_ADDR_R) != 0xff)
  124. return 1;
  125. return 0;
  126. }
  127. static int nand_read_page(struct mtd_info *mtd, 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. int stat;
  139. nand_command(mtd, block, page, 0, NAND_CMD_READ0);
  140. /* No malloc available for now, just use some temporary locations
  141. * in SDRAM
  142. */
  143. ecc_calc = (u_char *)(CONFIG_SYS_SDRAM_BASE + 0x10000);
  144. ecc_code = ecc_calc + 0x100;
  145. oob_data = ecc_calc + 0x200;
  146. for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
  147. this->ecc.hwctl(mtd, NAND_ECC_READ);
  148. this->read_buf(mtd, p, eccsize);
  149. this->ecc.calculate(mtd, p, &ecc_calc[i]);
  150. }
  151. this->read_buf(mtd, oob_data, CONFIG_SYS_NAND_OOBSIZE);
  152. /* Pick the ECC bytes out of the oob data */
  153. for (i = 0; i < CONFIG_SYS_NAND_ECCTOTAL; i++)
  154. ecc_code[i] = oob_data[nand_ecc_pos[i]];
  155. eccsteps = CONFIG_SYS_NAND_ECCSTEPS;
  156. p = dst;
  157. for (i = 0 ; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
  158. /* No chance to do something with the possible error message
  159. * from correct_data(). We just hope that all possible errors
  160. * are corrected by this routine.
  161. */
  162. stat = this->ecc.correct(mtd, p, &ecc_code[i], &ecc_calc[i]);
  163. }
  164. return 0;
  165. }
  166. static int nand_load(struct mtd_info *mtd, unsigned int offs,
  167. unsigned int uboot_size, uchar *dst)
  168. {
  169. unsigned int block, lastblock;
  170. unsigned int page;
  171. /*
  172. * offs has to be aligned to a page address!
  173. */
  174. block = offs / CONFIG_SYS_NAND_BLOCK_SIZE;
  175. lastblock = (offs + uboot_size - 1) / CONFIG_SYS_NAND_BLOCK_SIZE;
  176. page = (offs % CONFIG_SYS_NAND_BLOCK_SIZE) / CONFIG_SYS_NAND_PAGE_SIZE;
  177. while (block <= lastblock) {
  178. if (!nand_is_bad_block(mtd, block)) {
  179. /*
  180. * Skip bad blocks
  181. */
  182. while (page < CONFIG_SYS_NAND_PAGE_COUNT) {
  183. nand_read_page(mtd, block, page, dst);
  184. dst += CONFIG_SYS_NAND_PAGE_SIZE;
  185. page++;
  186. }
  187. page = 0;
  188. } else {
  189. lastblock++;
  190. }
  191. block++;
  192. }
  193. return 0;
  194. }
  195. /*
  196. * The main entry for NAND booting. It's necessary that SDRAM is already
  197. * configured and available since this code loads the main U-Boot image
  198. * from NAND into SDRAM and starts it from there.
  199. */
  200. void nand_boot(void)
  201. {
  202. struct nand_chip nand_chip;
  203. nand_info_t nand_info;
  204. int ret;
  205. __attribute__((noreturn)) void (*uboot)(void);
  206. /*
  207. * Init board specific nand support
  208. */
  209. nand_chip.select_chip = NULL;
  210. nand_info.priv = &nand_chip;
  211. nand_chip.IO_ADDR_R = nand_chip.IO_ADDR_W = (void __iomem *)CONFIG_SYS_NAND_BASE;
  212. nand_chip.dev_ready = NULL; /* preset to NULL */
  213. board_nand_init(&nand_chip);
  214. if (nand_chip.select_chip)
  215. nand_chip.select_chip(&nand_info, 0);
  216. /*
  217. * Load U-Boot image from NAND into RAM
  218. */
  219. ret = nand_load(&nand_info, CONFIG_SYS_NAND_U_BOOT_OFFS, CONFIG_SYS_NAND_U_BOOT_SIZE,
  220. (uchar *)CONFIG_SYS_NAND_U_BOOT_DST);
  221. #ifdef CONFIG_NAND_ENV_DST
  222. nand_load(&nand_info, CONFIG_ENV_OFFSET, CONFIG_ENV_SIZE,
  223. (uchar *)CONFIG_NAND_ENV_DST);
  224. #ifdef CONFIG_ENV_OFFSET_REDUND
  225. nand_load(&nand_info, CONFIG_ENV_OFFSET_REDUND, CONFIG_ENV_SIZE,
  226. (uchar *)CONFIG_NAND_ENV_DST + CONFIG_ENV_SIZE);
  227. #endif
  228. #endif
  229. if (nand_chip.select_chip)
  230. nand_chip.select_chip(&nand_info, -1);
  231. /*
  232. * Jump to U-Boot image
  233. */
  234. uboot = (void *)CONFIG_SYS_NAND_U_BOOT_START;
  235. (*uboot)();
  236. }