mxc_nand_spl.c 9.2 KB

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