nand_boot_fsl_nfc.c 7.1 KB

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