nand_boot.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. /*
  2. * (C) Copyright 2006
  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. extern void board_nand_init(struct nand_chip *nand);
  25. extern void ndfc_hwcontrol(struct mtd_info *mtdinfo, int cmd);
  26. extern void ndfc_write_byte(struct mtd_info *mtdinfo, u_char byte);
  27. extern u_char ndfc_read_byte(struct mtd_info *mtdinfo);
  28. extern int ndfc_dev_ready(struct mtd_info *mtdinfo);
  29. extern int jump_to_ram(ulong delta);
  30. extern int jump_to_uboot(ulong addr);
  31. static int nand_is_bad_block(struct mtd_info *mtd, int block)
  32. {
  33. struct nand_chip *this = mtd->priv;
  34. int page_addr = block * CFG_NAND_PAGE_COUNT;
  35. /* Begin command latch cycle */
  36. this->hwcontrol(mtd, NAND_CTL_SETCLE);
  37. this->write_byte(mtd, NAND_CMD_READOOB);
  38. /* Set ALE and clear CLE to start address cycle */
  39. this->hwcontrol(mtd, NAND_CTL_CLRCLE);
  40. this->hwcontrol(mtd, NAND_CTL_SETALE);
  41. /* Column address */
  42. this->write_byte(mtd, CFG_NAND_BAD_BLOCK_POS); /* A[7:0] */
  43. this->write_byte(mtd, (uchar)(page_addr & 0xff)); /* A[16:9] */
  44. this->write_byte(mtd, (uchar)((page_addr >> 8) & 0xff)); /* A[24:17] */
  45. #ifdef CFG_NAND_4_ADDR_CYCLE
  46. /* One more address cycle for devices > 32MiB */
  47. this->write_byte(mtd, (uchar)((page_addr >> 16) & 0x0f)); /* A[xx:25] */
  48. #endif
  49. /* Latch in address */
  50. this->hwcontrol(mtd, NAND_CTL_CLRALE);
  51. /*
  52. * Wait a while for the data to be ready
  53. */
  54. if (this->dev_ready)
  55. this->dev_ready(mtd);
  56. else
  57. CFG_NAND_READ_DELAY;
  58. /*
  59. * Read on byte
  60. */
  61. if (this->read_byte(mtd) != 0xff)
  62. return 1;
  63. return 0;
  64. }
  65. static int nand_read_page(struct mtd_info *mtd, int block, int page, uchar *dst)
  66. {
  67. struct nand_chip *this = mtd->priv;
  68. int page_addr = page + block * CFG_NAND_PAGE_COUNT;
  69. int i;
  70. /* Begin command latch cycle */
  71. this->hwcontrol(mtd, NAND_CTL_SETCLE);
  72. this->write_byte(mtd, NAND_CMD_READ0);
  73. /* Set ALE and clear CLE to start address cycle */
  74. this->hwcontrol(mtd, NAND_CTL_CLRCLE);
  75. this->hwcontrol(mtd, NAND_CTL_SETALE);
  76. /* Column address */
  77. this->write_byte(mtd, 0); /* A[7:0] */
  78. this->write_byte(mtd, (uchar)(page_addr & 0xff)); /* A[16:9] */
  79. this->write_byte(mtd, (uchar)((page_addr >> 8) & 0xff)); /* A[24:17] */
  80. #ifdef CFG_NAND_4_ADDR_CYCLE
  81. /* One more address cycle for devices > 32MiB */
  82. this->write_byte(mtd, (uchar)((page_addr >> 16) & 0x0f)); /* A[xx:25] */
  83. #endif
  84. /* Latch in address */
  85. this->hwcontrol(mtd, NAND_CTL_CLRALE);
  86. /*
  87. * Wait a while for the data to be ready
  88. */
  89. if (this->dev_ready)
  90. this->dev_ready(mtd);
  91. else
  92. CFG_NAND_READ_DELAY;
  93. /*
  94. * Read page into buffer
  95. */
  96. for (i=0; i<CFG_NAND_PAGE_SIZE; i++)
  97. *dst++ = this->read_byte(mtd);
  98. return 0;
  99. }
  100. static int nand_load(struct mtd_info *mtd, int offs, int uboot_size, uchar *dst)
  101. {
  102. int block;
  103. int blockcopy_count;
  104. int page;
  105. /*
  106. * offs has to be aligned to a block address!
  107. */
  108. block = offs / CFG_NAND_BLOCK_SIZE;
  109. blockcopy_count = 0;
  110. while (blockcopy_count < (uboot_size / CFG_NAND_BLOCK_SIZE)) {
  111. if (!nand_is_bad_block(mtd, block)) {
  112. /*
  113. * Skip bad blocks
  114. */
  115. for (page = 0; page < CFG_NAND_PAGE_COUNT; page++) {
  116. nand_read_page(mtd, block, page, dst);
  117. dst += CFG_NAND_PAGE_SIZE;
  118. }
  119. blockcopy_count++;
  120. }
  121. block++;
  122. }
  123. return 0;
  124. }
  125. void nand_boot(void)
  126. {
  127. ulong mem_size;
  128. struct nand_chip nand_chip;
  129. nand_info_t nand_info;
  130. int ret;
  131. void (*uboot)(void);
  132. /*
  133. * Init sdram, so we have access to memory
  134. */
  135. mem_size = initdram(0);
  136. /*
  137. * Init board specific nand support
  138. */
  139. nand_info.priv = &nand_chip;
  140. nand_chip.IO_ADDR_R = nand_chip.IO_ADDR_W = (void __iomem *)CFG_NAND_BASE;
  141. nand_chip.dev_ready = NULL; /* preset to NULL */
  142. board_nand_init(&nand_chip);
  143. /*
  144. * Load U-Boot image from NAND into RAM
  145. */
  146. ret = nand_load(&nand_info, CFG_NAND_U_BOOT_OFFS,
  147. CFG_NAND_U_BOOT_SIZE,
  148. (uchar *)CFG_NAND_U_BOOT_DST);
  149. /*
  150. * Jump to U-Boot image
  151. */
  152. uboot = (void (*)(void))CFG_NAND_U_BOOT_START;
  153. (*uboot)();
  154. }