am335x_spl_bch.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. /*
  2. * (C) Copyright 2012
  3. * Konstantin Kozhevnikov, Cogent Embedded
  4. *
  5. * based on nand_spl_simple code
  6. *
  7. * (C) Copyright 2006-2008
  8. * Stefan Roese, DENX Software Engineering, sr@denx.de.
  9. *
  10. * SPDX-License-Identifier: GPL-2.0+
  11. */
  12. #include <common.h>
  13. #include <nand.h>
  14. #include <asm/io.h>
  15. #include <linux/mtd/nand_ecc.h>
  16. static int nand_ecc_pos[] = CONFIG_SYS_NAND_ECCPOS;
  17. static struct mtd_info *mtd;
  18. static struct nand_chip nand_chip;
  19. #define ECCSTEPS (CONFIG_SYS_NAND_PAGE_SIZE / \
  20. CONFIG_SYS_NAND_ECCSIZE)
  21. #define ECCTOTAL (ECCSTEPS * CONFIG_SYS_NAND_ECCBYTES)
  22. /*
  23. * NAND command for large page NAND devices (2k)
  24. */
  25. static int nand_command(int block, int page, uint32_t offs,
  26. u8 cmd)
  27. {
  28. struct nand_chip *this = mtd_to_nand(mtd);
  29. int page_addr = page + block * CONFIG_SYS_NAND_PAGE_COUNT;
  30. void (*hwctrl)(struct mtd_info *mtd, int cmd,
  31. unsigned int ctrl) = this->cmd_ctrl;
  32. while (!this->dev_ready(mtd))
  33. ;
  34. /* Emulate NAND_CMD_READOOB */
  35. if (cmd == NAND_CMD_READOOB) {
  36. offs += CONFIG_SYS_NAND_PAGE_SIZE;
  37. cmd = NAND_CMD_READ0;
  38. }
  39. /* Begin command latch cycle */
  40. hwctrl(mtd, cmd, NAND_CTRL_CLE | NAND_CTRL_CHANGE);
  41. if (cmd == NAND_CMD_RESET) {
  42. hwctrl(mtd, NAND_CMD_NONE, NAND_NCE | NAND_CTRL_CHANGE);
  43. /*
  44. * Apply this short delay always to ensure that we do wait
  45. * tWB in any case on any machine.
  46. */
  47. ndelay(150);
  48. while (!this->dev_ready(mtd))
  49. ;
  50. return 0;
  51. }
  52. /* Shift the offset from byte addressing to word addressing. */
  53. if ((this->options & NAND_BUSWIDTH_16) && !nand_opcode_8bits(cmd))
  54. offs >>= 1;
  55. /* Set ALE and clear CLE to start address cycle */
  56. /* Column address */
  57. hwctrl(mtd, offs & 0xff,
  58. NAND_CTRL_ALE | NAND_CTRL_CHANGE); /* A[7:0] */
  59. hwctrl(mtd, (offs >> 8) & 0xff, NAND_CTRL_ALE); /* A[11:9] */
  60. /* Row address */
  61. if (cmd != NAND_CMD_RNDOUT) {
  62. hwctrl(mtd, (page_addr & 0xff),
  63. NAND_CTRL_ALE); /* A[19:12] */
  64. hwctrl(mtd, ((page_addr >> 8) & 0xff),
  65. NAND_CTRL_ALE); /* A[27:20] */
  66. #ifdef CONFIG_SYS_NAND_5_ADDR_CYCLE
  67. /* One more address cycle for devices > 128MiB */
  68. hwctrl(mtd, (page_addr >> 16) & 0x0f,
  69. NAND_CTRL_ALE); /* A[31:28] */
  70. #endif
  71. }
  72. hwctrl(mtd, NAND_CMD_NONE, NAND_NCE | NAND_CTRL_CHANGE);
  73. /*
  74. * Program and erase have their own busy handlers status, sequential
  75. * in and status need no delay.
  76. */
  77. switch (cmd) {
  78. case NAND_CMD_CACHEDPROG:
  79. case NAND_CMD_PAGEPROG:
  80. case NAND_CMD_ERASE1:
  81. case NAND_CMD_ERASE2:
  82. case NAND_CMD_SEQIN:
  83. case NAND_CMD_RNDIN:
  84. case NAND_CMD_STATUS:
  85. return 0;
  86. case NAND_CMD_RNDOUT:
  87. /* No ready / busy check necessary */
  88. hwctrl(mtd, NAND_CMD_RNDOUTSTART, NAND_CTRL_CLE |
  89. NAND_CTRL_CHANGE);
  90. hwctrl(mtd, NAND_CMD_NONE, NAND_NCE | NAND_CTRL_CHANGE);
  91. return 0;
  92. case NAND_CMD_READ0:
  93. /* Latch in address */
  94. hwctrl(mtd, NAND_CMD_READSTART,
  95. NAND_CTRL_CLE | NAND_CTRL_CHANGE);
  96. hwctrl(mtd, NAND_CMD_NONE, NAND_NCE | NAND_CTRL_CHANGE);
  97. }
  98. /*
  99. * Apply this short delay always to ensure that we do wait tWB in
  100. * any case on any machine.
  101. */
  102. ndelay(150);
  103. while (!this->dev_ready(mtd))
  104. ;
  105. return 0;
  106. }
  107. static int nand_is_bad_block(int block)
  108. {
  109. struct nand_chip *this = mtd_to_nand(mtd);
  110. nand_command(block, 0, CONFIG_SYS_NAND_BAD_BLOCK_POS,
  111. NAND_CMD_READOOB);
  112. /*
  113. * Read one byte (or two if it's a 16 bit chip).
  114. */
  115. if (this->options & NAND_BUSWIDTH_16) {
  116. if (readw(this->IO_ADDR_R) != 0xffff)
  117. return 1;
  118. } else {
  119. if (readb(this->IO_ADDR_R) != 0xff)
  120. return 1;
  121. }
  122. return 0;
  123. }
  124. static int nand_read_page(int block, int page, void *dst)
  125. {
  126. struct nand_chip *this = mtd_to_nand(mtd);
  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. uint32_t data_pos = 0;
  136. uint8_t *oob = &oob_data[0] + nand_ecc_pos[0];
  137. uint32_t oob_pos = eccsize * eccsteps + nand_ecc_pos[0];
  138. nand_command(block, page, 0, NAND_CMD_READ0);
  139. for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
  140. this->ecc.hwctl(mtd, NAND_ECC_READ);
  141. nand_command(block, page, data_pos, NAND_CMD_RNDOUT);
  142. this->read_buf(mtd, p, eccsize);
  143. nand_command(block, page, oob_pos, NAND_CMD_RNDOUT);
  144. this->read_buf(mtd, oob, eccbytes);
  145. this->ecc.calculate(mtd, p, &ecc_calc[i]);
  146. data_pos += eccsize;
  147. oob_pos += eccbytes;
  148. oob += eccbytes;
  149. }
  150. /* Pick the ECC bytes out of the oob data */
  151. for (i = 0; i < ECCTOTAL; i++)
  152. ecc_code[i] = oob_data[nand_ecc_pos[i]];
  153. eccsteps = ECCSTEPS;
  154. p = dst;
  155. for (i = 0 ; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
  156. /* No chance to do something with the possible error message
  157. * from correct_data(). We just hope that all possible errors
  158. * are corrected by this routine.
  159. */
  160. this->ecc.correct(mtd, p, &ecc_code[i], &ecc_calc[i]);
  161. }
  162. return 0;
  163. }
  164. /* nand_init() - initialize data to make nand usable by SPL */
  165. void nand_init(void)
  166. {
  167. /*
  168. * Init board specific nand support
  169. */
  170. mtd = nand_to_mtd(&nand_chip);
  171. nand_chip.IO_ADDR_R = nand_chip.IO_ADDR_W =
  172. (void __iomem *)CONFIG_SYS_NAND_BASE;
  173. board_nand_init(&nand_chip);
  174. if (nand_chip.select_chip)
  175. nand_chip.select_chip(mtd, 0);
  176. /* NAND chip may require reset after power-on */
  177. nand_command(0, 0, 0, NAND_CMD_RESET);
  178. }
  179. /* Unselect after operation */
  180. void nand_deselect(void)
  181. {
  182. if (nand_chip.select_chip)
  183. nand_chip.select_chip(mtd, -1);
  184. }
  185. #include "nand_spl_loaders.c"