mxc_nand_spl.c 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * (C) Copyright 2009
  4. * Magnus Lilja <lilja.magnus@gmail.com>
  5. *
  6. * (C) Copyright 2008
  7. * Maxim Artamonov, <scn1874 at yandex.ru>
  8. *
  9. * (C) Copyright 2006-2008
  10. * Stefan Roese, DENX Software Engineering, sr at denx.de.
  11. */
  12. #include <common.h>
  13. #include <hang.h>
  14. #include <nand.h>
  15. #include <asm/arch/imx-regs.h>
  16. #include <asm/io.h>
  17. #include "mxc_nand.h"
  18. #if defined(MXC_NFC_V1) || defined(MXC_NFC_V2_1)
  19. static struct mxc_nand_regs *const nfc = (void *)NFC_BASE_ADDR;
  20. #elif defined(MXC_NFC_V3_2)
  21. static struct mxc_nand_regs *const nfc = (void *)NFC_BASE_ADDR_AXI;
  22. static struct mxc_nand_ip_regs *const nfc_ip = (void *)NFC_BASE_ADDR;
  23. #endif
  24. static void nfc_wait_ready(void)
  25. {
  26. uint32_t tmp;
  27. #if defined(MXC_NFC_V1) || defined(MXC_NFC_V2_1)
  28. while (!(readnfc(&nfc->config2) & NFC_V1_V2_CONFIG2_INT))
  29. ;
  30. /* Reset interrupt flag */
  31. tmp = readnfc(&nfc->config2);
  32. tmp &= ~NFC_V1_V2_CONFIG2_INT;
  33. writenfc(tmp, &nfc->config2);
  34. #elif defined(MXC_NFC_V3_2)
  35. while (!(readnfc(&nfc_ip->ipc) & NFC_V3_IPC_INT))
  36. ;
  37. /* Reset interrupt flag */
  38. tmp = readnfc(&nfc_ip->ipc);
  39. tmp &= ~NFC_V3_IPC_INT;
  40. writenfc(tmp, &nfc_ip->ipc);
  41. #endif
  42. }
  43. static void nfc_nand_init(void)
  44. {
  45. #if defined(MXC_NFC_V3_2)
  46. int ecc_per_page = CONFIG_SYS_NAND_PAGE_SIZE / 512;
  47. int tmp;
  48. tmp = (readnfc(&nfc_ip->config2) & ~(NFC_V3_CONFIG2_SPAS_MASK |
  49. NFC_V3_CONFIG2_EDC_MASK | NFC_V3_CONFIG2_PS_MASK)) |
  50. NFC_V3_CONFIG2_SPAS(CONFIG_SYS_NAND_OOBSIZE / 2) |
  51. NFC_V3_CONFIG2_INT_MSK | NFC_V3_CONFIG2_ECC_EN |
  52. NFC_V3_CONFIG2_ONE_CYCLE;
  53. if (CONFIG_SYS_NAND_PAGE_SIZE == 4096)
  54. tmp |= NFC_V3_CONFIG2_PS_4096;
  55. else if (CONFIG_SYS_NAND_PAGE_SIZE == 2048)
  56. tmp |= NFC_V3_CONFIG2_PS_2048;
  57. else if (CONFIG_SYS_NAND_PAGE_SIZE == 512)
  58. tmp |= NFC_V3_CONFIG2_PS_512;
  59. /*
  60. * if spare size is larger that 16 bytes per 512 byte hunk
  61. * then use 8 symbol correction instead of 4
  62. */
  63. if (CONFIG_SYS_NAND_OOBSIZE / ecc_per_page > 16)
  64. tmp |= NFC_V3_CONFIG2_ECC_MODE_8;
  65. else
  66. tmp &= ~NFC_V3_CONFIG2_ECC_MODE_8;
  67. writenfc(tmp, &nfc_ip->config2);
  68. tmp = NFC_V3_CONFIG3_NUM_OF_DEVS(0) |
  69. NFC_V3_CONFIG3_NO_SDMA |
  70. NFC_V3_CONFIG3_RBB_MODE |
  71. NFC_V3_CONFIG3_SBB(6) | /* Reset default */
  72. NFC_V3_CONFIG3_ADD_OP(0);
  73. #ifndef CONFIG_SYS_NAND_BUSWIDTH_16
  74. tmp |= NFC_V3_CONFIG3_FW8;
  75. #endif
  76. writenfc(tmp, &nfc_ip->config3);
  77. writenfc(0, &nfc_ip->delay_line);
  78. #elif defined(MXC_NFC_V2_1)
  79. int ecc_per_page = CONFIG_SYS_NAND_PAGE_SIZE / 512;
  80. int config1;
  81. writenfc(CONFIG_SYS_NAND_OOBSIZE / 2, &nfc->spare_area_size);
  82. /* unlocking RAM Buff */
  83. writenfc(0x2, &nfc->config);
  84. /* hardware ECC checking and correct */
  85. config1 = readnfc(&nfc->config1) | NFC_V1_V2_CONFIG1_ECC_EN |
  86. NFC_V1_V2_CONFIG1_INT_MSK | NFC_V2_CONFIG1_ONE_CYCLE |
  87. NFC_V2_CONFIG1_FP_INT;
  88. /*
  89. * if spare size is larger that 16 bytes per 512 byte hunk
  90. * then use 8 symbol correction instead of 4
  91. */
  92. if (CONFIG_SYS_NAND_OOBSIZE / ecc_per_page > 16)
  93. config1 &= ~NFC_V2_CONFIG1_ECC_MODE_4;
  94. else
  95. config1 |= NFC_V2_CONFIG1_ECC_MODE_4;
  96. writenfc(config1, &nfc->config1);
  97. #elif defined(MXC_NFC_V1)
  98. /* unlocking RAM Buff */
  99. writenfc(0x2, &nfc->config);
  100. /* hardware ECC checking and correct */
  101. writenfc(NFC_V1_V2_CONFIG1_ECC_EN | NFC_V1_V2_CONFIG1_INT_MSK,
  102. &nfc->config1);
  103. #endif
  104. }
  105. static void nfc_nand_command(unsigned short command)
  106. {
  107. writenfc(command, &nfc->flash_cmd);
  108. writenfc(NFC_CMD, &nfc->operation);
  109. nfc_wait_ready();
  110. }
  111. static void nfc_nand_address(unsigned short address)
  112. {
  113. writenfc(address, &nfc->flash_addr);
  114. writenfc(NFC_ADDR, &nfc->operation);
  115. nfc_wait_ready();
  116. }
  117. static void nfc_nand_page_address(unsigned int page_address)
  118. {
  119. unsigned int page_count;
  120. nfc_nand_address(0x00);
  121. /* code only for large page flash */
  122. if (CONFIG_SYS_NAND_PAGE_SIZE > 512)
  123. nfc_nand_address(0x00);
  124. page_count = CONFIG_SYS_NAND_SIZE / CONFIG_SYS_NAND_PAGE_SIZE;
  125. if (page_address <= page_count) {
  126. page_count--; /* transform 0x01000000 to 0x00ffffff */
  127. do {
  128. nfc_nand_address(page_address & 0xff);
  129. page_address = page_address >> 8;
  130. page_count = page_count >> 8;
  131. } while (page_count);
  132. }
  133. nfc_nand_address(0x00);
  134. }
  135. static void nfc_nand_data_output(void)
  136. {
  137. #ifdef NAND_MXC_2K_MULTI_CYCLE
  138. int i;
  139. #endif
  140. #if defined(MXC_NFC_V1) || defined(MXC_NFC_V2_1)
  141. writenfc(0, &nfc->buf_addr);
  142. #elif defined(MXC_NFC_V3_2)
  143. int config1 = readnfc(&nfc->config1);
  144. config1 &= ~NFC_V3_CONFIG1_RBA_MASK;
  145. writenfc(config1, &nfc->config1);
  146. #endif
  147. writenfc(NFC_OUTPUT, &nfc->operation);
  148. nfc_wait_ready();
  149. #ifdef NAND_MXC_2K_MULTI_CYCLE
  150. /*
  151. * This NAND controller requires multiple input commands
  152. * for pages larger than 512 bytes.
  153. */
  154. for (i = 1; i < CONFIG_SYS_NAND_PAGE_SIZE / 512; i++) {
  155. writenfc(i, &nfc->buf_addr);
  156. writenfc(NFC_OUTPUT, &nfc->operation);
  157. nfc_wait_ready();
  158. }
  159. #endif
  160. }
  161. static int nfc_nand_check_ecc(void)
  162. {
  163. #if defined(MXC_NFC_V1)
  164. u16 ecc_status = readw(&nfc->ecc_status_result);
  165. return (ecc_status & 0x3) == 2 || (ecc_status >> 2) == 2;
  166. #elif defined(MXC_NFC_V2_1) || defined(MXC_NFC_V3_2)
  167. u32 ecc_status = readl(&nfc->ecc_status_result);
  168. int ecc_per_page = CONFIG_SYS_NAND_PAGE_SIZE / 512;
  169. int err_limit = CONFIG_SYS_NAND_OOBSIZE / ecc_per_page > 16 ? 8 : 4;
  170. int subpages = CONFIG_SYS_NAND_PAGE_SIZE / 512;
  171. do {
  172. if ((ecc_status & 0xf) > err_limit)
  173. return 1;
  174. ecc_status >>= 4;
  175. } while (--subpages);
  176. return 0;
  177. #endif
  178. }
  179. static void nfc_nand_read_page(unsigned int page_address)
  180. {
  181. /* read in first 0 buffer */
  182. #if defined(MXC_NFC_V1) || defined(MXC_NFC_V2_1)
  183. writenfc(0, &nfc->buf_addr);
  184. #elif defined(MXC_NFC_V3_2)
  185. int config1 = readnfc(&nfc->config1);
  186. config1 &= ~NFC_V3_CONFIG1_RBA_MASK;
  187. writenfc(config1, &nfc->config1);
  188. #endif
  189. nfc_nand_command(NAND_CMD_READ0);
  190. nfc_nand_page_address(page_address);
  191. if (CONFIG_SYS_NAND_PAGE_SIZE > 512)
  192. nfc_nand_command(NAND_CMD_READSTART);
  193. nfc_nand_data_output(); /* fill the main buffer 0 */
  194. }
  195. static int nfc_read_page(unsigned int page_address, unsigned char *buf)
  196. {
  197. int i;
  198. u32 *src;
  199. u32 *dst;
  200. nfc_nand_read_page(page_address);
  201. if (nfc_nand_check_ecc())
  202. return -EBADMSG;
  203. src = (u32 *)&nfc->main_area[0][0];
  204. dst = (u32 *)buf;
  205. /* main copy loop from NAND-buffer to SDRAM memory */
  206. for (i = 0; i < CONFIG_SYS_NAND_PAGE_SIZE / 4; i++) {
  207. writel(readl(src), dst);
  208. src++;
  209. dst++;
  210. }
  211. return 0;
  212. }
  213. static int is_badblock(int pagenumber)
  214. {
  215. int page = pagenumber;
  216. u32 badblock;
  217. u32 *src;
  218. /* Check the first two pages for bad block markers */
  219. for (page = pagenumber; page < pagenumber + 2; page++) {
  220. nfc_nand_read_page(page);
  221. src = (u32 *)&nfc->spare_area[0][0];
  222. /*
  223. * IMPORTANT NOTE: The nand flash controller uses a non-
  224. * standard layout for large page devices. This can
  225. * affect the position of the bad block marker.
  226. */
  227. /* Get the bad block marker */
  228. badblock = readl(&src[CONFIG_SYS_NAND_BAD_BLOCK_POS / 4]);
  229. badblock >>= 8 * (CONFIG_SYS_NAND_BAD_BLOCK_POS % 4);
  230. badblock &= 0xff;
  231. /* bad block marker verify */
  232. if (badblock != 0xff)
  233. return 1; /* potential bad block */
  234. }
  235. return 0;
  236. }
  237. int nand_spl_load_image(uint32_t from, unsigned int size, void *buf)
  238. {
  239. int i;
  240. unsigned int page;
  241. unsigned int maxpages = CONFIG_SYS_NAND_SIZE /
  242. CONFIG_SYS_NAND_PAGE_SIZE;
  243. nfc_nand_init();
  244. /* Convert to page number */
  245. page = from / CONFIG_SYS_NAND_PAGE_SIZE;
  246. i = 0;
  247. size = roundup(size, CONFIG_SYS_NAND_PAGE_SIZE);
  248. while (i < size / CONFIG_SYS_NAND_PAGE_SIZE) {
  249. if (nfc_read_page(page, buf) < 0)
  250. return -1;
  251. page++;
  252. i++;
  253. buf = buf + CONFIG_SYS_NAND_PAGE_SIZE;
  254. /*
  255. * Check if we have crossed a block boundary, and if so
  256. * check for bad block.
  257. */
  258. if (!(page % CONFIG_SYS_NAND_PAGE_COUNT)) {
  259. /*
  260. * Yes, new block. See if this block is good. If not,
  261. * loop until we find a good block.
  262. */
  263. while (is_badblock(page)) {
  264. page = page + CONFIG_SYS_NAND_PAGE_COUNT;
  265. /* Check i we've reached the end of flash. */
  266. if (page >= maxpages)
  267. return -1;
  268. }
  269. }
  270. }
  271. return 0;
  272. }
  273. #ifndef CONFIG_SPL_FRAMEWORK
  274. /*
  275. * The main entry for NAND booting. It's necessary that SDRAM is already
  276. * configured and available since this code loads the main U-Boot image
  277. * from NAND into SDRAM and starts it from there.
  278. */
  279. __used void nand_boot(void)
  280. {
  281. __attribute__((noreturn)) void (*uboot)(void);
  282. /*
  283. * CONFIG_SYS_NAND_U_BOOT_OFFS and CONFIG_SYS_NAND_U_BOOT_SIZE must
  284. * be aligned to full pages
  285. */
  286. if (!nand_spl_load_image(CONFIG_SYS_NAND_U_BOOT_OFFS,
  287. CONFIG_SYS_NAND_U_BOOT_SIZE,
  288. (uchar *)CONFIG_SYS_NAND_U_BOOT_DST)) {
  289. /* Copy from NAND successful, start U-Boot */
  290. uboot = (void *)CONFIG_SYS_NAND_U_BOOT_START;
  291. uboot();
  292. } else {
  293. /* Unrecoverable error when copying from NAND */
  294. hang();
  295. }
  296. }
  297. #endif
  298. void nand_init(void) {}
  299. void nand_deselect(void) {}