nand_boot.c 7.4 KB

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