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