nand_boot_fsl_nfc.c 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301
  1. /*
  2. * (C) Copyright 2009
  3. * Magnus Lilja <lilja.magnus@gmail.com>
  4. *
  5. * (C) Copyright 2008
  6. * Maxim Artamonov, <scn1874 at yandex.ru>
  7. *
  8. * (C) Copyright 2006-2008
  9. * Stefan Roese, DENX Software Engineering, sr at denx.de.
  10. *
  11. * This program is free software; you can redistribute it and/or
  12. * modify it under the terms of the GNU General Public License as
  13. * published by the Free Software Foundation; either version 2 of
  14. * the License, or (at your option) any later version.
  15. *
  16. * This program is distributed in the hope that it will be useful,
  17. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. * GNU General Public License for more details.
  20. *
  21. * You should have received a copy of the GNU General Public License
  22. * along with this program; if not, write to the Free Software
  23. * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
  24. * MA 02111-1307 USA
  25. */
  26. #include <common.h>
  27. #include <nand.h>
  28. #include <asm/arch/imx-regs.h>
  29. #include <asm/io.h>
  30. #include <fsl_nfc.h>
  31. static struct fsl_nfc_regs *const nfc = (void *)NFC_BASE_ADDR;
  32. static void nfc_wait_ready(void)
  33. {
  34. uint32_t tmp;
  35. while (!(readw(&nfc->nand_flash_config2) & NFC_INT))
  36. ;
  37. /* Reset interrupt flag */
  38. tmp = readw(&nfc->nand_flash_config2);
  39. tmp &= ~NFC_INT;
  40. writew(tmp, &nfc->nand_flash_config2);
  41. }
  42. void nfc_nand_init(void)
  43. {
  44. #if defined(MXC_NFC_V1_1)
  45. int ecc_per_page = CONFIG_SYS_NAND_PAGE_SIZE / 512;
  46. int config1;
  47. writew(CONFIG_SYS_NAND_SPARE_SIZE / 2, &nfc->spare_area_size);
  48. /* unlocking RAM Buff */
  49. writew(0x2, &nfc->configuration);
  50. /* hardware ECC checking and correct */
  51. config1 = readw(&nfc->nand_flash_config1) | NFC_ECC_EN | 0x800;
  52. /*
  53. * if spare size is larger that 16 bytes per 512 byte hunk
  54. * then use 8 symbol correction instead of 4
  55. */
  56. if ((CONFIG_SYS_NAND_SPARE_SIZE / ecc_per_page) > 16)
  57. config1 &= ~NFC_4_8N_ECC;
  58. else
  59. config1 |= NFC_4_8N_ECC;
  60. writew(config1, &nfc->nand_flash_config1);
  61. #elif defined(MXC_NFC_V1)
  62. /* unlocking RAM Buff */
  63. writew(0x2, &nfc->configuration);
  64. /* hardware ECC checking and correct */
  65. writew(NFC_ECC_EN, &nfc->nand_flash_config1);
  66. #endif
  67. }
  68. static void nfc_nand_command(unsigned short command)
  69. {
  70. writew(command, &nfc->flash_cmd);
  71. writew(NFC_CMD, &nfc->nand_flash_config2);
  72. nfc_wait_ready();
  73. }
  74. static void nfc_nand_page_address(unsigned int page_address)
  75. {
  76. unsigned int page_count;
  77. writew(0x00, &nfc->flash_add);
  78. writew(NFC_ADDR, &nfc->nand_flash_config2);
  79. nfc_wait_ready();
  80. /* code only for large page flash */
  81. if (CONFIG_SYS_NAND_PAGE_SIZE > 512) {
  82. writew(0x00, &nfc->flash_add);
  83. writew(NFC_ADDR, &nfc->nand_flash_config2);
  84. nfc_wait_ready();
  85. }
  86. page_count = CONFIG_SYS_NAND_SIZE / CONFIG_SYS_NAND_PAGE_SIZE;
  87. if (page_address <= page_count) {
  88. page_count--; /* transform 0x01000000 to 0x00ffffff */
  89. do {
  90. writew(page_address & 0xff, &nfc->flash_add);
  91. writew(NFC_ADDR, &nfc->nand_flash_config2);
  92. nfc_wait_ready();
  93. page_address = page_address >> 8;
  94. page_count = page_count >> 8;
  95. } while (page_count);
  96. }
  97. writew(0x00, &nfc->flash_add);
  98. writew(NFC_ADDR, &nfc->nand_flash_config2);
  99. nfc_wait_ready();
  100. }
  101. static void nfc_nand_data_output(void)
  102. {
  103. int config1 = readw(&nfc->nand_flash_config1);
  104. #ifdef NAND_MXC_2K_MULTI_CYCLE
  105. int i;
  106. #endif
  107. config1 |= NFC_ECC_EN | NFC_INT_MSK;
  108. writew(config1, &nfc->nand_flash_config1);
  109. writew(0, &nfc->buffer_address);
  110. writew(NFC_OUTPUT, &nfc->nand_flash_config2);
  111. nfc_wait_ready();
  112. #ifdef NAND_MXC_2K_MULTI_CYCLE
  113. /*
  114. * This NAND controller requires multiple input commands
  115. * for pages larger than 512 bytes.
  116. */
  117. for (i = 1; i < (CONFIG_SYS_NAND_PAGE_SIZE / 512); i++) {
  118. config1 = readw(&nfc->nand_flash_config1);
  119. config1 |= NFC_ECC_EN | NFC_INT_MSK;
  120. writew(config1, &nfc->nand_flash_config1);
  121. writew(i, &nfc->buffer_address);
  122. writew(NFC_OUTPUT, &nfc->nand_flash_config2);
  123. nfc_wait_ready();
  124. }
  125. #endif
  126. }
  127. static int nfc_nand_check_ecc(void)
  128. {
  129. return readw(&nfc->ecc_status_result);
  130. }
  131. static int nfc_read_page(unsigned int page_address, unsigned char *buf)
  132. {
  133. int i;
  134. u32 *src;
  135. u32 *dst;
  136. writew(0, &nfc->buffer_address); /* read in first 0 buffer */
  137. nfc_nand_command(NAND_CMD_READ0);
  138. nfc_nand_page_address(page_address);
  139. if (CONFIG_SYS_NAND_PAGE_SIZE > 512)
  140. nfc_nand_command(NAND_CMD_READSTART);
  141. nfc_nand_data_output(); /* fill the main buffer 0 */
  142. if (nfc_nand_check_ecc())
  143. return -1;
  144. src = &nfc->main_area[0][0];
  145. dst = (u32 *)buf;
  146. /* main copy loop from NAND-buffer to SDRAM memory */
  147. for (i = 0; i < (CONFIG_SYS_NAND_PAGE_SIZE / 4); i++) {
  148. writel(readl(src), dst);
  149. src++;
  150. dst++;
  151. }
  152. return 0;
  153. }
  154. static int is_badblock(int pagenumber)
  155. {
  156. int page = pagenumber;
  157. u32 badblock;
  158. u32 *src;
  159. /* Check the first two pages for bad block markers */
  160. for (page = pagenumber; page < pagenumber + 2; page++) {
  161. writew(0, &nfc->buffer_address); /* read in first 0 buffer */
  162. nfc_nand_command(NAND_CMD_READ0);
  163. nfc_nand_page_address(page);
  164. if (CONFIG_SYS_NAND_PAGE_SIZE > 512)
  165. nfc_nand_command(NAND_CMD_READSTART);
  166. nfc_nand_data_output(); /* fill the main buffer 0 */
  167. src = &nfc->spare_area[0][0];
  168. /*
  169. * IMPORTANT NOTE: The nand flash controller uses a non-
  170. * standard layout for large page devices. This can
  171. * affect the position of the bad block marker.
  172. */
  173. /* Get the bad block marker */
  174. badblock = readl(&src[CONFIG_SYS_NAND_BAD_BLOCK_POS / 4]);
  175. badblock >>= 8 * (CONFIG_SYS_NAND_BAD_BLOCK_POS % 4);
  176. badblock &= 0xff;
  177. /* bad block marker verify */
  178. if (badblock != 0xff)
  179. return 1; /* potential bad block */
  180. }
  181. return 0;
  182. }
  183. static int nand_load(unsigned int from, unsigned int size, unsigned char *buf)
  184. {
  185. int i;
  186. unsigned int page;
  187. unsigned int maxpages = CONFIG_SYS_NAND_SIZE /
  188. CONFIG_SYS_NAND_PAGE_SIZE;
  189. nfc_nand_init();
  190. /* Convert to page number */
  191. page = from / CONFIG_SYS_NAND_PAGE_SIZE;
  192. i = 0;
  193. while (i < (size / CONFIG_SYS_NAND_PAGE_SIZE)) {
  194. if (nfc_read_page(page, buf) < 0)
  195. return -1;
  196. page++;
  197. i++;
  198. buf = buf + CONFIG_SYS_NAND_PAGE_SIZE;
  199. /*
  200. * Check if we have crossed a block boundary, and if so
  201. * check for bad block.
  202. */
  203. if (!(page % CONFIG_SYS_NAND_PAGE_COUNT)) {
  204. /*
  205. * Yes, new block. See if this block is good. If not,
  206. * loop until we find a good block.
  207. */
  208. while (is_badblock(page)) {
  209. page = page + CONFIG_SYS_NAND_PAGE_COUNT;
  210. /* Check i we've reached the end of flash. */
  211. if (page >= maxpages)
  212. return -1;
  213. }
  214. }
  215. }
  216. return 0;
  217. }
  218. #if defined(CONFIG_ARM)
  219. void board_init_f (ulong bootflag)
  220. {
  221. relocate_code (CONFIG_SYS_TEXT_BASE - TOTAL_MALLOC_LEN, NULL,
  222. CONFIG_SYS_TEXT_BASE);
  223. }
  224. #endif
  225. /*
  226. * The main entry for NAND booting. It's necessary that SDRAM is already
  227. * configured and available since this code loads the main U-Boot image
  228. * from NAND into SDRAM and starts it from there.
  229. */
  230. void nand_boot(void)
  231. {
  232. __attribute__((noreturn)) void (*uboot)(void);
  233. /*
  234. * CONFIG_SYS_NAND_U_BOOT_OFFS and CONFIG_SYS_NAND_U_BOOT_SIZE must
  235. * be aligned to full pages
  236. */
  237. if (!nand_load(CONFIG_SYS_NAND_U_BOOT_OFFS, CONFIG_SYS_NAND_U_BOOT_SIZE,
  238. (uchar *)CONFIG_SYS_NAND_U_BOOT_DST)) {
  239. /* Copy from NAND successful, start U-boot */
  240. uboot = (void *)CONFIG_SYS_NAND_U_BOOT_START;
  241. uboot();
  242. } else {
  243. /* Unrecoverable error when copying from NAND */
  244. hang();
  245. }
  246. }
  247. /*
  248. * Called in case of an exception.
  249. */
  250. void hang(void)
  251. {
  252. /* Loop forever */
  253. while (1) ;
  254. }