mxc_nand_spl.c 8.5 KB

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