nand_spl_simple.c 7.3 KB

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