nand_boot_jz4740.c 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429
  1. /*
  2. * Copyright (C) 2007 Ingenic Semiconductor Inc.
  3. * Author: Peter <[email protected]>
  4. *
  5. * This program is free software; you can redistribute it and/or
  6. * modify it under the terms of the GNU General Public License as
  7. * published by the Free Software Foundation; either version 2 of
  8. * the License, or (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program; if not, write to the Free Software
  17. * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
  18. * MA 02111-1307 USA
  19. */
  20. #include <common.h>
  21. #include <nand.h>
  22. #include <asm/io.h>
  23. #include <asm/jz4740.h>
  24. #define KEY_U_OUT (32 * 2 + 16)
  25. #define KEY_U_IN (32 * 3 + 19)
  26. /*
  27. * NAND flash definitions
  28. */
  29. #define NAND_DATAPORT 0xb8000000
  30. #define NAND_ADDRPORT 0xb8010000
  31. #define NAND_COMMPORT 0xb8008000
  32. #define ECC_BLOCK 512
  33. #define ECC_POS 6
  34. #define PAR_SIZE 9
  35. #define __nand_enable() (REG_EMC_NFCSR |= EMC_NFCSR_NFE1 | EMC_NFCSR_NFCE1)
  36. #define __nand_disable() (REG_EMC_NFCSR &= ~(EMC_NFCSR_NFCE1))
  37. #define __nand_ecc_rs_encoding() \
  38. (REG_EMC_NFECR = EMC_NFECR_ECCE | EMC_NFECR_ERST | EMC_NFECR_RS | EMC_NFECR_RS_ENCODING)
  39. #define __nand_ecc_rs_decoding() \
  40. (REG_EMC_NFECR = EMC_NFECR_ECCE | EMC_NFECR_ERST | EMC_NFECR_RS | EMC_NFECR_RS_DECODING)
  41. #define __nand_ecc_disable() (REG_EMC_NFECR &= ~EMC_NFECR_ECCE)
  42. #define __nand_ecc_encode_sync() while (!(REG_EMC_NFINTS & EMC_NFINTS_ENCF))
  43. #define __nand_ecc_decode_sync() while (!(REG_EMC_NFINTS & EMC_NFINTS_DECF))
  44. static inline void __nand_dev_ready(void)
  45. {
  46. unsigned int timeout = 10000;
  47. while ((REG_GPIO_PXPIN(2) & 0x40000000) && timeout--);
  48. while (!(REG_GPIO_PXPIN(2) & 0x40000000));
  49. }
  50. #define __nand_cmd(n) (REG8(NAND_COMMPORT) = (n))
  51. #define __nand_addr(n) (REG8(NAND_ADDRPORT) = (n))
  52. #define __nand_data8() REG8(NAND_DATAPORT)
  53. #define __nand_data16() REG16(NAND_DATAPORT)
  54. #if (JZ4740_NANDBOOT_CFG == JZ4740_NANDBOOT_B8R3)
  55. #define NAND_BUS_WIDTH 8
  56. #define NAND_ROW_CYCLE 3
  57. #elif (JZ4740_NANDBOOT_CFG == JZ4740_NANDBOOT_B8R2)
  58. #define NAND_BUS_WIDTH 8
  59. #define NAND_ROW_CYCLE 2
  60. #elif (JZ4740_NANDBOOT_CFG == JZ4740_NANDBOOT_B16R3)
  61. #define NAND_BUS_WIDTH 16
  62. #define NAND_ROW_CYCLE 3
  63. #elif (JZ4740_NANDBOOT_CFG == JZ4740_NANDBOOT_B16R2)
  64. #define NAND_BUS_WIDTH 16
  65. #define NAND_ROW_CYCLE 2
  66. #endif
  67. /*
  68. * NAND flash parameters
  69. */
  70. static int page_size = 2048;
  71. static int oob_size = 64;
  72. static int ecc_count = 4;
  73. static int page_per_block = 64;
  74. static int bad_block_pos = 0;
  75. static int block_size = 131072;
  76. static unsigned char oob_buf[128] = {0};
  77. /*
  78. * External routines
  79. */
  80. extern void flush_cache_all(void);
  81. extern int serial_init(void);
  82. extern void serial_puts(const char *s);
  83. extern void sdram_init(void);
  84. extern void pll_init(void);
  85. extern void usb_boot();
  86. /*
  87. * NAND flash routines
  88. */
  89. #if NAND_BUS_WIDTH == 16
  90. static inline void nand_read_buf16(void *buf, int count)
  91. {
  92. int i;
  93. u16 *p = (u16 *)buf;
  94. for (i = 0; i < count; i += 2)
  95. *p++ = __nand_data16();
  96. }
  97. #define nand_read_buf nand_read_buf16
  98. #elif NAND_BUS_WIDTH == 8
  99. static inline void nand_read_buf8(void *buf, int count)
  100. {
  101. int i;
  102. u8 *p = (u8 *)buf;
  103. for (i = 0; i < count; i++)
  104. *p++ = __nand_data8();
  105. }
  106. #define nand_read_buf nand_read_buf8
  107. #endif
  108. /* Correct 1~9-bit errors in 512-bytes data */
  109. static void rs_correct(unsigned char *dat, int idx, int mask)
  110. {
  111. int i;
  112. idx--;
  113. i = idx + (idx >> 3);
  114. if (i >= 512)
  115. return;
  116. mask <<= (idx & 0x7);
  117. dat[i] ^= mask & 0xff;
  118. if (i < 511)
  119. dat[i+1] ^= (mask >> 8) & 0xff;
  120. }
  121. static int nand_read_oob(int page_addr, uchar *buf, int size)
  122. {
  123. int col_addr;
  124. if (page_size != 512)
  125. col_addr = page_size;
  126. else {
  127. col_addr = 0;
  128. __nand_dev_ready();
  129. }
  130. if (page_size != 512)
  131. /* Send READ0 command */
  132. __nand_cmd(NAND_CMD_READ0);
  133. else
  134. /* Send READOOB command */
  135. __nand_cmd(NAND_CMD_READOOB);
  136. /* Send column address */
  137. __nand_addr(col_addr & 0xff);
  138. if (page_size != 512)
  139. __nand_addr((col_addr >> 8) & 0xff);
  140. /* Send page address */
  141. __nand_addr(page_addr & 0xff);
  142. __nand_addr((page_addr >> 8) & 0xff);
  143. #ifdef NAND_ROW_CYCLE == 3
  144. __nand_addr((page_addr >> 16) & 0xff);
  145. #endif
  146. /* Send READSTART command for 2048 or 4096 ps NAND */
  147. if (page_size != 512)
  148. __nand_cmd(NAND_CMD_READSTART);
  149. /* Wait for device ready */
  150. __nand_dev_ready();
  151. /* Read oob data */
  152. nand_read_buf(buf, size);
  153. if (page_size == 512)
  154. __nand_dev_ready();
  155. return 0;
  156. }
  157. static int nand_read_page(int page_addr, uchar *dst, uchar *oobbuf)
  158. {
  159. uchar *databuf = dst, *tmpbuf;
  160. int i, j;
  161. /*
  162. * Read oob data
  163. */
  164. nand_read_oob(page_addr, oobbuf, oob_size);
  165. /*
  166. * Read page data
  167. */
  168. /* Send READ0 command */
  169. __nand_cmd(NAND_CMD_READ0);
  170. /* Send column address */
  171. __nand_addr(0);
  172. if (page_size != 512)
  173. __nand_addr(0);
  174. /* Send page address */
  175. __nand_addr(page_addr & 0xff);
  176. __nand_addr((page_addr >> 8) & 0xff);
  177. #if NAND_ROW_CYCLE == 3
  178. __nand_addr((page_addr >> 16) & 0xff);
  179. #endif
  180. /* Send READSTART command for 2048 or 4096 ps NAND */
  181. if (page_size != 512)
  182. __nand_cmd(NAND_CMD_READSTART);
  183. /* Wait for device ready */
  184. __nand_dev_ready();
  185. /* Read page data */
  186. tmpbuf = databuf;
  187. for (i = 0; i < ecc_count; i++) {
  188. volatile unsigned char *paraddr = (volatile unsigned char *)EMC_NFPAR0;
  189. unsigned int stat;
  190. /* Enable RS decoding */
  191. REG_EMC_NFINTS = 0x0;
  192. __nand_ecc_rs_decoding();
  193. /* Read data */
  194. nand_read_buf((void *)tmpbuf, ECC_BLOCK);
  195. /* Set PAR values */
  196. for (j = 0; j < PAR_SIZE; j++) {
  197. #if defined(CONFIG_SYS_NAND_ECC_POS)
  198. *paraddr++ = oobbuf[CONFIG_SYS_NAND_ECC_POS + i*PAR_SIZE + j];
  199. #else
  200. *paraddr++ = oobbuf[ECC_POS + i*PAR_SIZE + j];
  201. #endif
  202. }
  203. /* Set PRDY */
  204. REG_EMC_NFECR |= EMC_NFECR_PRDY;
  205. /* Wait for completion */
  206. __nand_ecc_decode_sync();
  207. /* Disable decoding */
  208. __nand_ecc_disable();
  209. /* Check result of decoding */
  210. stat = REG_EMC_NFINTS;
  211. if (stat & EMC_NFINTS_ERR) {
  212. /* Error occurred */
  213. /* serial_puts("\n Error occurred\n"); */
  214. if (stat & EMC_NFINTS_UNCOR) {
  215. /* Uncorrectable error occurred */
  216. /* serial_puts("\nUncorrectable error occurred\n"); */
  217. }
  218. else {
  219. unsigned int errcnt, index, mask;
  220. errcnt = (stat & EMC_NFINTS_ERRCNT_MASK) >> EMC_NFINTS_ERRCNT_BIT;
  221. switch (errcnt) {
  222. case 4:
  223. index = (REG_EMC_NFERR3 & EMC_NFERR_INDEX_MASK) >> EMC_NFERR_INDEX_BIT;
  224. mask = (REG_EMC_NFERR3 & EMC_NFERR_MASK_MASK) >> EMC_NFERR_MASK_BIT;
  225. rs_correct(tmpbuf, index, mask);
  226. /* FALL-THROUGH */
  227. case 3:
  228. index = (REG_EMC_NFERR2 & EMC_NFERR_INDEX_MASK) >> EMC_NFERR_INDEX_BIT;
  229. mask = (REG_EMC_NFERR2 & EMC_NFERR_MASK_MASK) >> EMC_NFERR_MASK_BIT;
  230. rs_correct(tmpbuf, index, mask);
  231. /* FALL-THROUGH */
  232. case 2:
  233. index = (REG_EMC_NFERR1 & EMC_NFERR_INDEX_MASK) >> EMC_NFERR_INDEX_BIT;
  234. mask = (REG_EMC_NFERR1 & EMC_NFERR_MASK_MASK) >> EMC_NFERR_MASK_BIT;
  235. rs_correct(tmpbuf, index, mask);
  236. /* FALL-THROUGH */
  237. case 1:
  238. index = (REG_EMC_NFERR0 & EMC_NFERR_INDEX_MASK) >> EMC_NFERR_INDEX_BIT;
  239. mask = (REG_EMC_NFERR0 & EMC_NFERR_MASK_MASK) >> EMC_NFERR_MASK_BIT;
  240. rs_correct(tmpbuf, index, mask);
  241. break;
  242. default:
  243. break;
  244. }
  245. }
  246. }
  247. tmpbuf += ECC_BLOCK;
  248. }
  249. return 0;
  250. }
  251. #ifndef CONFIG_SYS_NAND_BADBLOCK_PAGE
  252. #define CONFIG_SYS_NAND_BADBLOCK_PAGE 0 /* NAND bad block was marked at this page in a block, starting from 0 */
  253. #endif
  254. static void nand_load(int offs, int uboot_size, uchar *dst)
  255. {
  256. int page;
  257. int pagecopy_count;
  258. __nand_enable();
  259. page = offs / page_size;
  260. pagecopy_count = 0;
  261. while (pagecopy_count < (uboot_size / page_size)) {
  262. if (page % page_per_block == 0) {
  263. nand_read_oob(page + CONFIG_SYS_NAND_BADBLOCK_PAGE, oob_buf, oob_size);
  264. if (oob_buf[bad_block_pos] != 0xff) {
  265. page += page_per_block;
  266. /* Skip bad block */
  267. continue;
  268. }
  269. }
  270. /* Load this page to dst, do the ECC */
  271. nand_read_page(page, dst, oob_buf);
  272. dst += page_size;
  273. page++;
  274. pagecopy_count++;
  275. }
  276. __nand_disable();
  277. }
  278. static void jz_nand_init(void) {
  279. /* Optimize the timing of nand */
  280. REG_EMC_SMCR1 = 0x094c4400;
  281. }
  282. static void gpio_init(void)
  283. {
  284. /*
  285. * Initialize SDRAM pins
  286. */
  287. #if defined(CONFIG_JZ4720)
  288. __gpio_as_sdram_16bit_4720();
  289. #elif defined(CONFIG_JZ4725)
  290. __gpio_as_sdram_16bit_4725();
  291. #else
  292. __gpio_as_sdram_32bit();
  293. #endif
  294. /*
  295. * Initialize UART0 pins
  296. */
  297. __gpio_as_uart0();
  298. }
  299. static int is_usb_boot()
  300. {
  301. int keyU = 0;
  302. __gpio_as_input(KEY_U_IN);
  303. __gpio_enable_pull(KEY_U_IN);
  304. __gpio_as_output(KEY_U_OUT);
  305. __gpio_clear_pin(KEY_U_OUT);
  306. keyU = __gpio_get_pin(KEY_U_IN);
  307. if (keyU)
  308. serial_puts("[U] not pressed\n");
  309. else
  310. serial_puts("[U] pressed\n");
  311. return !keyU;
  312. }
  313. void nand_boot(void)
  314. {
  315. void (*uboot)(void);
  316. /*
  317. * Init hardware
  318. */
  319. jz_nand_init();
  320. gpio_init();
  321. serial_init();
  322. serial_puts("\n\nNAND Secondary Program Loader\n\n");
  323. pll_init();
  324. sdram_init();
  325. #if defined(CONFIG_NANONOTE)
  326. if(is_usb_boot()) {
  327. serial_puts("enter USB BOOT mode\n");
  328. usb_boot();
  329. }
  330. #endif
  331. page_size = CONFIG_SYS_NAND_PAGE_SIZE;
  332. block_size = CONFIG_SYS_NAND_BLOCK_SIZE;
  333. page_per_block = CONFIG_SYS_NAND_BLOCK_SIZE / CONFIG_SYS_NAND_PAGE_SIZE;
  334. bad_block_pos = (page_size == 512) ? 5 : 0;
  335. oob_size = page_size / 32;
  336. ecc_count = page_size / ECC_BLOCK;
  337. /*
  338. * Load U-Boot image from NAND into RAM
  339. */
  340. nand_load(CONFIG_SYS_NAND_U_BOOT_OFFS, CONFIG_SYS_NAND_U_BOOT_SIZE,
  341. (uchar *)CONFIG_SYS_NAND_U_BOOT_DST);
  342. uboot = (void (*)(void))CONFIG_SYS_NAND_U_BOOT_START;
  343. serial_puts("Starting U-Boot ...\n");
  344. /*
  345. * Flush caches
  346. */
  347. flush_cache_all();
  348. /*
  349. * Jump to U-Boot image
  350. */
  351. (*uboot)();
  352. }