nand_boot.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. /*
  2. * (C) Copyright 2006-2007
  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. #define CFG_NAND_READ_DELAY \
  23. { volatile int dummy; int i; for (i=0; i<10000; i++) dummy = i; }
  24. static int nand_ecc_pos[] = CFG_NAND_ECCPOS;
  25. extern void board_nand_init(struct nand_chip *nand);
  26. static int nand_command(struct mtd_info *mtd, int block, int page, int offs, u8 cmd)
  27. {
  28. struct nand_chip *this = mtd->priv;
  29. int page_addr = page + block * CFG_NAND_PAGE_COUNT;
  30. if (this->dev_ready)
  31. this->dev_ready(mtd);
  32. else
  33. CFG_NAND_READ_DELAY;
  34. /* Begin command latch cycle */
  35. this->hwcontrol(mtd, NAND_CTL_SETCLE);
  36. this->write_byte(mtd, cmd);
  37. /* Set ALE and clear CLE to start address cycle */
  38. this->hwcontrol(mtd, NAND_CTL_CLRCLE);
  39. this->hwcontrol(mtd, NAND_CTL_SETALE);
  40. /* Column address */
  41. this->write_byte(mtd, offs); /* A[7:0] */
  42. this->write_byte(mtd, (uchar)(page_addr & 0xff)); /* A[16:9] */
  43. this->write_byte(mtd, (uchar)((page_addr >> 8) & 0xff)); /* A[24:17] */
  44. #ifdef CFG_NAND_4_ADDR_CYCLE
  45. /* One more address cycle for devices > 32MiB */
  46. this->write_byte(mtd, (uchar)((page_addr >> 16) & 0x0f)); /* A[xx:25] */
  47. #endif
  48. /* Latch in address */
  49. this->hwcontrol(mtd, NAND_CTL_CLRALE);
  50. /*
  51. * Wait a while for the data to be ready
  52. */
  53. if (this->dev_ready)
  54. this->dev_ready(mtd);
  55. else
  56. CFG_NAND_READ_DELAY;
  57. return 0;
  58. }
  59. static int nand_is_bad_block(struct mtd_info *mtd, int block)
  60. {
  61. struct nand_chip *this = mtd->priv;
  62. nand_command(mtd, block, 0, CFG_NAND_BAD_BLOCK_POS, NAND_CMD_READOOB);
  63. /*
  64. * Read on byte
  65. */
  66. if (this->read_byte(mtd) != 0xff)
  67. return 1;
  68. return 0;
  69. }
  70. static int nand_read_page(struct mtd_info *mtd, int block, int page, uchar *dst)
  71. {
  72. struct nand_chip *this = mtd->priv;
  73. u_char *ecc_calc;
  74. u_char *ecc_code;
  75. u_char *oob_data;
  76. int i;
  77. int eccsize = CFG_NAND_ECCSIZE;
  78. int eccbytes = CFG_NAND_ECCBYTES;
  79. int eccsteps = CFG_NAND_ECCSTEPS;
  80. uint8_t *p = dst;
  81. int stat;
  82. nand_command(mtd, block, page, 0, NAND_CMD_READ0);
  83. /* No malloc available for now, just use some temporary locations
  84. * in SDRAM
  85. */
  86. ecc_calc = (u_char *)(CFG_SDRAM_BASE + 0x10000);
  87. ecc_code = ecc_calc + 0x100;
  88. oob_data = ecc_calc + 0x200;
  89. for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
  90. this->enable_hwecc(mtd, NAND_ECC_READ);
  91. this->read_buf(mtd, p, eccsize);
  92. this->calculate_ecc(mtd, p, &ecc_calc[i]);
  93. }
  94. this->read_buf(mtd, oob_data, CFG_NAND_OOBSIZE);
  95. /* Pick the ECC bytes out of the oob data */
  96. for (i = 0; i < CFG_NAND_ECCTOTAL; i++)
  97. ecc_code[i] = oob_data[nand_ecc_pos[i]];
  98. eccsteps = CFG_NAND_ECCSTEPS;
  99. p = dst;
  100. for (i = 0 ; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
  101. /* No chance to do something with the possible error message
  102. * from correct_data(). We just hope that all possible errors
  103. * are corrected by this routine.
  104. */
  105. stat = this->correct_data(mtd, p, &ecc_code[i], &ecc_calc[i]);
  106. }
  107. return 0;
  108. }
  109. static int nand_load(struct mtd_info *mtd, int offs, int uboot_size, uchar *dst)
  110. {
  111. int block;
  112. int blockcopy_count;
  113. int page;
  114. /*
  115. * offs has to be aligned to a block address!
  116. */
  117. block = offs / CFG_NAND_BLOCK_SIZE;
  118. blockcopy_count = 0;
  119. while (blockcopy_count < (uboot_size / CFG_NAND_BLOCK_SIZE)) {
  120. if (!nand_is_bad_block(mtd, block)) {
  121. /*
  122. * Skip bad blocks
  123. */
  124. for (page = 0; page < CFG_NAND_PAGE_COUNT; page++) {
  125. nand_read_page(mtd, block, page, dst);
  126. dst += CFG_NAND_PAGE_SIZE;
  127. }
  128. blockcopy_count++;
  129. }
  130. block++;
  131. }
  132. return 0;
  133. }
  134. void nand_boot(void)
  135. {
  136. ulong mem_size;
  137. struct nand_chip nand_chip;
  138. nand_info_t nand_info;
  139. int ret;
  140. void (*uboot)(void);
  141. /*
  142. * Init sdram, so we have access to memory
  143. */
  144. mem_size = initdram(0);
  145. /*
  146. * Init board specific nand support
  147. */
  148. nand_info.priv = &nand_chip;
  149. nand_chip.IO_ADDR_R = nand_chip.IO_ADDR_W = (void __iomem *)CFG_NAND_BASE;
  150. nand_chip.dev_ready = NULL; /* preset to NULL */
  151. board_nand_init(&nand_chip);
  152. /*
  153. * Load U-Boot image from NAND into RAM
  154. */
  155. ret = nand_load(&nand_info, CFG_NAND_U_BOOT_OFFS, CFG_NAND_U_BOOT_SIZE,
  156. (uchar *)CFG_NAND_U_BOOT_DST);
  157. /*
  158. * Jump to U-Boot image
  159. */
  160. uboot = (void (*)(void))CFG_NAND_U_BOOT_START;
  161. (*uboot)();
  162. }