nand_boot_fsl_nfc.c 7.4 KB

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