nand_boot.c 8.4 KB

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