nand_spl_simple.c 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  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. #include <linux/mtd/nand_ecc.h>
  11. static int nand_ecc_pos[] = CONFIG_SYS_NAND_ECCPOS;
  12. static struct mtd_info *mtd;
  13. static struct nand_chip nand_chip;
  14. #define ECCSTEPS (CONFIG_SYS_NAND_PAGE_SIZE / \
  15. CONFIG_SYS_NAND_ECCSIZE)
  16. #define ECCTOTAL (ECCSTEPS * CONFIG_SYS_NAND_ECCBYTES)
  17. #if (CONFIG_SYS_NAND_PAGE_SIZE <= 512)
  18. /*
  19. * NAND command for small page NAND devices (512)
  20. */
  21. static int nand_command(int block, int page, uint32_t offs,
  22. u8 cmd)
  23. {
  24. struct nand_chip *this = mtd->priv();
  25. int page_addr = page + block * CONFIG_SYS_NAND_PAGE_COUNT;
  26. while (!this->dev_ready(mtd))
  27. ;
  28. /* Begin command latch cycle */
  29. this->cmd_ctrl(mtd, cmd, NAND_CTRL_CLE | NAND_CTRL_CHANGE);
  30. /* Set ALE and clear CLE to start address cycle */
  31. /* Column address */
  32. this->cmd_ctrl(mtd, offs, NAND_CTRL_ALE | NAND_CTRL_CHANGE);
  33. this->cmd_ctrl(mtd, page_addr & 0xff, NAND_CTRL_ALE); /* A[16:9] */
  34. this->cmd_ctrl(mtd, (page_addr >> 8) & 0xff,
  35. NAND_CTRL_ALE); /* A[24:17] */
  36. #ifdef CONFIG_SYS_NAND_4_ADDR_CYCLE
  37. /* One more address cycle for devices > 32MiB */
  38. this->cmd_ctrl(mtd, (page_addr >> 16) & 0x0f,
  39. NAND_CTRL_ALE); /* A[28:25] */
  40. #endif
  41. /* Latch in address */
  42. this->cmd_ctrl(mtd, NAND_CMD_NONE, NAND_NCE | NAND_CTRL_CHANGE);
  43. /*
  44. * Wait a while for the data to be ready
  45. */
  46. while (!this->dev_ready(mtd))
  47. ;
  48. return 0;
  49. }
  50. #else
  51. /*
  52. * NAND command for large page NAND devices (2k)
  53. */
  54. static int nand_command(int block, int page, uint32_t offs,
  55. u8 cmd)
  56. {
  57. struct nand_chip *this = mtd->priv;
  58. int page_addr = page + block * CONFIG_SYS_NAND_PAGE_COUNT;
  59. void (*hwctrl)(struct mtd_info *mtd, int cmd,
  60. unsigned int ctrl) = this->cmd_ctrl;
  61. while (!this->dev_ready(mtd))
  62. ;
  63. /* Emulate NAND_CMD_READOOB */
  64. if (cmd == NAND_CMD_READOOB) {
  65. offs += CONFIG_SYS_NAND_PAGE_SIZE;
  66. cmd = NAND_CMD_READ0;
  67. }
  68. /* Shift the offset from byte addressing to word addressing. */
  69. if ((this->options & NAND_BUSWIDTH_16) && !nand_opcode_8bits(cmd))
  70. offs >>= 1;
  71. /* Begin command latch cycle */
  72. hwctrl(mtd, cmd, NAND_CTRL_CLE | NAND_CTRL_CHANGE);
  73. /* Set ALE and clear CLE to start address cycle */
  74. /* Column address */
  75. hwctrl(mtd, offs & 0xff,
  76. NAND_CTRL_ALE | NAND_CTRL_CHANGE); /* A[7:0] */
  77. hwctrl(mtd, (offs >> 8) & 0xff, NAND_CTRL_ALE); /* A[11:9] */
  78. /* Row address */
  79. hwctrl(mtd, (page_addr & 0xff), NAND_CTRL_ALE); /* A[19:12] */
  80. hwctrl(mtd, ((page_addr >> 8) & 0xff),
  81. NAND_CTRL_ALE); /* A[27:20] */
  82. #ifdef CONFIG_SYS_NAND_5_ADDR_CYCLE
  83. /* One more address cycle for devices > 128MiB */
  84. hwctrl(mtd, (page_addr >> 16) & 0x0f,
  85. NAND_CTRL_ALE); /* A[31:28] */
  86. #endif
  87. /* Latch in address */
  88. hwctrl(mtd, NAND_CMD_READSTART,
  89. NAND_CTRL_CLE | NAND_CTRL_CHANGE);
  90. hwctrl(mtd, NAND_CMD_NONE, NAND_NCE | NAND_CTRL_CHANGE);
  91. /*
  92. * Wait a while for the data to be ready
  93. */
  94. while (!this->dev_ready(mtd))
  95. ;
  96. return 0;
  97. }
  98. #endif
  99. static int nand_is_bad_block(int block)
  100. {
  101. struct nand_chip *this = mtd->priv;
  102. u_char bb_data[2];
  103. nand_command(block, 0, CONFIG_SYS_NAND_BAD_BLOCK_POS,
  104. NAND_CMD_READOOB);
  105. /*
  106. * Read one byte (or two if it's a 16 bit chip).
  107. */
  108. if (this->options & NAND_BUSWIDTH_16) {
  109. this->read_buf(mtd, bb_data, 2);
  110. if (bb_data[0] != 0xff || bb_data[1] != 0xff)
  111. return 1;
  112. } else {
  113. this->read_buf(mtd, bb_data, 1);
  114. if (bb_data[0] != 0xff)
  115. return 1;
  116. }
  117. return 0;
  118. }
  119. #if defined(CONFIG_SYS_NAND_HW_ECC_OOBFIRST)
  120. static int nand_read_page(int block, int page, uchar *dst)
  121. {
  122. struct nand_chip *this = mtd->priv;
  123. u_char ecc_calc[ECCTOTAL];
  124. u_char ecc_code[ECCTOTAL];
  125. u_char oob_data[CONFIG_SYS_NAND_OOBSIZE];
  126. int i;
  127. int eccsize = CONFIG_SYS_NAND_ECCSIZE;
  128. int eccbytes = CONFIG_SYS_NAND_ECCBYTES;
  129. int eccsteps = ECCSTEPS;
  130. uint8_t *p = dst;
  131. nand_command(block, page, 0, NAND_CMD_READOOB);
  132. this->read_buf(mtd, oob_data, CONFIG_SYS_NAND_OOBSIZE);
  133. nand_command(block, page, 0, NAND_CMD_READ0);
  134. /* Pick the ECC bytes out of the oob data */
  135. for (i = 0; i < ECCTOTAL; i++)
  136. ecc_code[i] = oob_data[nand_ecc_pos[i]];
  137. for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
  138. this->ecc.hwctl(mtd, NAND_ECC_READ);
  139. this->read_buf(mtd, p, eccsize);
  140. this->ecc.calculate(mtd, p, &ecc_calc[i]);
  141. this->ecc.correct(mtd, p, &ecc_code[i], &ecc_calc[i]);
  142. }
  143. return 0;
  144. }
  145. #else
  146. static int nand_read_page(int block, int page, void *dst)
  147. {
  148. struct nand_chip *this = mtd->priv;
  149. u_char ecc_calc[ECCTOTAL];
  150. u_char ecc_code[ECCTOTAL];
  151. u_char oob_data[CONFIG_SYS_NAND_OOBSIZE];
  152. int i;
  153. int eccsize = CONFIG_SYS_NAND_ECCSIZE;
  154. int eccbytes = CONFIG_SYS_NAND_ECCBYTES;
  155. int eccsteps = ECCSTEPS;
  156. uint8_t *p = dst;
  157. nand_command(block, page, 0, NAND_CMD_READ0);
  158. for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
  159. if (this->ecc.mode != NAND_ECC_SOFT)
  160. this->ecc.hwctl(mtd, NAND_ECC_READ);
  161. this->read_buf(mtd, p, eccsize);
  162. this->ecc.calculate(mtd, p, &ecc_calc[i]);
  163. }
  164. this->read_buf(mtd, oob_data, CONFIG_SYS_NAND_OOBSIZE);
  165. /* Pick the ECC bytes out of the oob data */
  166. for (i = 0; i < ECCTOTAL; i++)
  167. ecc_code[i] = oob_data[nand_ecc_pos[i]];
  168. eccsteps = ECCSTEPS;
  169. p = dst;
  170. for (i = 0 ; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
  171. /* No chance to do something with the possible error message
  172. * from correct_data(). We just hope that all possible errors
  173. * are corrected by this routine.
  174. */
  175. this->ecc.correct(mtd, p, &ecc_code[i], &ecc_calc[i]);
  176. }
  177. return 0;
  178. }
  179. #endif
  180. int nand_spl_load_image(uint32_t offs, unsigned int size, void *dst)
  181. {
  182. unsigned int block, lastblock;
  183. unsigned int page;
  184. /*
  185. * offs has to be aligned to a page address!
  186. */
  187. block = offs / CONFIG_SYS_NAND_BLOCK_SIZE;
  188. lastblock = (offs + size - 1) / CONFIG_SYS_NAND_BLOCK_SIZE;
  189. page = (offs % CONFIG_SYS_NAND_BLOCK_SIZE) / CONFIG_SYS_NAND_PAGE_SIZE;
  190. while (block <= lastblock) {
  191. if (!nand_is_bad_block(block)) {
  192. /*
  193. * Skip bad blocks
  194. */
  195. while (page < CONFIG_SYS_NAND_PAGE_COUNT) {
  196. nand_read_page(block, page, dst);
  197. dst += CONFIG_SYS_NAND_PAGE_SIZE;
  198. page++;
  199. }
  200. page = 0;
  201. } else {
  202. lastblock++;
  203. }
  204. block++;
  205. }
  206. return 0;
  207. }
  208. /* nand_init() - initialize data to make nand usable by SPL */
  209. void nand_init(void)
  210. {
  211. /*
  212. * Init board specific nand support
  213. */
  214. mtd = &nand_chip.mtd;
  215. mtd->priv = &nand_chip;
  216. nand_chip.IO_ADDR_R = nand_chip.IO_ADDR_W =
  217. (void __iomem *)CONFIG_SYS_NAND_BASE;
  218. board_nand_init(&nand_chip);
  219. #ifdef CONFIG_SPL_NAND_SOFTECC
  220. if (nand_chip.ecc.mode == NAND_ECC_SOFT) {
  221. nand_chip.ecc.calculate = nand_calculate_ecc;
  222. nand_chip.ecc.correct = nand_correct_data;
  223. }
  224. #endif
  225. if (nand_chip.select_chip)
  226. nand_chip.select_chip(mtd, 0);
  227. }
  228. /* Unselect after operation */
  229. void nand_deselect(void)
  230. {
  231. if (nand_chip.select_chip)
  232. nand_chip.select_chip(mtd, -1);
  233. }