nand_boot_fsl_nfc.c 6.9 KB

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