nand_spl_simple.c 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  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 nand_info_t 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. nand_command(block, 0, CONFIG_SYS_NAND_BAD_BLOCK_POS,
  103. NAND_CMD_READOOB);
  104. /*
  105. * Read one byte (or two if it's a 16 bit chip).
  106. */
  107. if (this->options & NAND_BUSWIDTH_16) {
  108. if (readw(this->IO_ADDR_R) != 0xffff)
  109. return 1;
  110. } else {
  111. if (readb(this->IO_ADDR_R) != 0xff)
  112. return 1;
  113. }
  114. return 0;
  115. }
  116. #if defined(CONFIG_SYS_NAND_HW_ECC_OOBFIRST)
  117. static int nand_read_page(int block, int page, uchar *dst)
  118. {
  119. struct nand_chip *this = mtd.priv;
  120. u_char ecc_calc[ECCTOTAL];
  121. u_char ecc_code[ECCTOTAL];
  122. u_char oob_data[CONFIG_SYS_NAND_OOBSIZE];
  123. int i;
  124. int eccsize = CONFIG_SYS_NAND_ECCSIZE;
  125. int eccbytes = CONFIG_SYS_NAND_ECCBYTES;
  126. int eccsteps = ECCSTEPS;
  127. uint8_t *p = dst;
  128. nand_command(block, page, 0, NAND_CMD_READOOB);
  129. this->read_buf(&mtd, oob_data, CONFIG_SYS_NAND_OOBSIZE);
  130. nand_command(block, page, 0, NAND_CMD_READ0);
  131. /* Pick the ECC bytes out of the oob data */
  132. for (i = 0; i < ECCTOTAL; i++)
  133. ecc_code[i] = oob_data[nand_ecc_pos[i]];
  134. for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
  135. this->ecc.hwctl(&mtd, NAND_ECC_READ);
  136. this->read_buf(&mtd, p, eccsize);
  137. this->ecc.calculate(&mtd, p, &ecc_calc[i]);
  138. this->ecc.correct(&mtd, p, &ecc_code[i], &ecc_calc[i]);
  139. }
  140. return 0;
  141. }
  142. #else
  143. static int nand_read_page(int block, int page, void *dst)
  144. {
  145. struct nand_chip *this = mtd.priv;
  146. u_char ecc_calc[ECCTOTAL];
  147. u_char ecc_code[ECCTOTAL];
  148. u_char oob_data[CONFIG_SYS_NAND_OOBSIZE];
  149. int i;
  150. int eccsize = CONFIG_SYS_NAND_ECCSIZE;
  151. int eccbytes = CONFIG_SYS_NAND_ECCBYTES;
  152. int eccsteps = ECCSTEPS;
  153. uint8_t *p = dst;
  154. nand_command(block, page, 0, NAND_CMD_READ0);
  155. for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
  156. if (this->ecc.mode != NAND_ECC_SOFT)
  157. this->ecc.hwctl(&mtd, NAND_ECC_READ);
  158. this->read_buf(&mtd, p, eccsize);
  159. this->ecc.calculate(&mtd, p, &ecc_calc[i]);
  160. }
  161. this->read_buf(&mtd, oob_data, CONFIG_SYS_NAND_OOBSIZE);
  162. /* Pick the ECC bytes out of the oob data */
  163. for (i = 0; i < ECCTOTAL; i++)
  164. ecc_code[i] = oob_data[nand_ecc_pos[i]];
  165. eccsteps = ECCSTEPS;
  166. p = dst;
  167. for (i = 0 ; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
  168. /* No chance to do something with the possible error message
  169. * from correct_data(). We just hope that all possible errors
  170. * are corrected by this routine.
  171. */
  172. this->ecc.correct(&mtd, p, &ecc_code[i], &ecc_calc[i]);
  173. }
  174. return 0;
  175. }
  176. #endif
  177. int nand_spl_load_image(uint32_t offs, unsigned int size, void *dst)
  178. {
  179. unsigned int block, lastblock;
  180. unsigned int page;
  181. /*
  182. * offs has to be aligned to a page address!
  183. */
  184. block = offs / CONFIG_SYS_NAND_BLOCK_SIZE;
  185. lastblock = (offs + size - 1) / CONFIG_SYS_NAND_BLOCK_SIZE;
  186. page = (offs % CONFIG_SYS_NAND_BLOCK_SIZE) / CONFIG_SYS_NAND_PAGE_SIZE;
  187. while (block <= lastblock) {
  188. if (!nand_is_bad_block(block)) {
  189. /*
  190. * Skip bad blocks
  191. */
  192. while (page < CONFIG_SYS_NAND_PAGE_COUNT) {
  193. nand_read_page(block, page, dst);
  194. dst += CONFIG_SYS_NAND_PAGE_SIZE;
  195. page++;
  196. }
  197. page = 0;
  198. } else {
  199. lastblock++;
  200. }
  201. block++;
  202. }
  203. return 0;
  204. }
  205. /* nand_init() - initialize data to make nand usable by SPL */
  206. void nand_init(void)
  207. {
  208. /*
  209. * Init board specific nand support
  210. */
  211. mtd.priv = &nand_chip;
  212. nand_chip.IO_ADDR_R = nand_chip.IO_ADDR_W =
  213. (void __iomem *)CONFIG_SYS_NAND_BASE;
  214. board_nand_init(&nand_chip);
  215. #ifdef CONFIG_SPL_NAND_SOFTECC
  216. if (nand_chip.ecc.mode == NAND_ECC_SOFT) {
  217. nand_chip.ecc.calculate = nand_calculate_ecc;
  218. nand_chip.ecc.correct = nand_correct_data;
  219. }
  220. #endif
  221. if (nand_chip.select_chip)
  222. nand_chip.select_chip(&mtd, 0);
  223. }
  224. /* Unselect after operation */
  225. void nand_deselect(void)
  226. {
  227. if (nand_chip.select_chip)
  228. nand_chip.select_chip(&mtd, -1);
  229. }