nand_boot.c 8.1 KB

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