ark_nand_spl.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * Copyright (C) 2014 Gateworks Corporation
  4. * Author: Tim Harvey <tharvey@gateworks.com>
  5. */
  6. #include <common.h>
  7. #include <malloc.h>
  8. #define NAND_BASE CONFIG_SYS_NAND_BASE
  9. /* NAND Flash Controller */
  10. #define rNAND_CR (*(volatile unsigned int *) (NAND_BASE + 0x00))
  11. #define rNAND_CLE_WR (*(volatile unsigned char *) (NAND_BASE + 0x04))
  12. #define rNAND_ALE_WR (*(volatile unsigned char *) (NAND_BASE + 0x08))
  13. #define rNAND_ID_RD (*(volatile unsigned char *) (NAND_BASE + 0x0c))
  14. #define rNAND_STATUS_RD (*(volatile unsigned char *) (NAND_BASE + 0x10))
  15. #define rNAND_DATA (*(volatile unsigned int *) (NAND_BASE + 0x14))
  16. #define rNAND_TX_FIFO (*(volatile unsigned int *) (NAND_BASE + 0x18))
  17. #define rNAND_RX_FIFO (*(volatile unsigned int *) (NAND_BASE + 0x1c))
  18. #define rNAND_WRBLK_START (*(volatile unsigned int *) (NAND_BASE + 0x20))
  19. #define rNAND_RDBLK_START (*(volatile unsigned int *) (NAND_BASE + 0x24))
  20. #define rEX_BCH_ENCODE_STATUS (*(volatile unsigned int *) (NAND_BASE + 0x274))
  21. #define rEX_BCH_DECODE_STATUS (*(volatile unsigned int *) (NAND_BASE + 0x278))
  22. #define rBCH_CR (*(volatile unsigned int *) (NAND_BASE + 0x27c))
  23. #define rBCH_NAND_STATUS (*(volatile unsigned int *) (NAND_BASE + 0x280))
  24. #define rBCH_DECODE_STATUS (*(volatile unsigned int *) (NAND_BASE + 0x284))
  25. #define rBCH_INT (*(volatile unsigned int *) (NAND_BASE + 0x288))
  26. #define rBCH_INT_MASK (*(volatile unsigned int *) (NAND_BASE + 0x28c))
  27. #define EX_BCH_DECODE_RESULT_ADDR (NAND_BASE + 0x29c)
  28. #define NAND_INT_GLOBAL (1<<3)
  29. #define NAND_INT_DECODE_ERR (1<<2)
  30. #define NAND_INT_DECODE_END (1<<1)
  31. #define NAND_INT_ENCODE_END (1<<0)
  32. //BCH_CR register fields defination
  33. #define BCH_CR_SECTOR_MODE (1<<8)
  34. #define BCH_CR_ENCODER_RESET (1<<3)
  35. #define BCH_CR_DECODER_RESET (1<<2)
  36. #define BCH_CR_SOFT_ECC_ENABLE (1<<1)
  37. #define BCH_CR_BCH_ENABLE (1<<0)
  38. #define CMD_ERASE1 0x60
  39. #define CMD_ERASE2 0xD0
  40. #define CMD_READ1 0x00
  41. #define CMD_READ2 0x30
  42. #define CMD_PAGEPROGRAM1 0x80
  43. #define CMD_PAGEPROGRAM2 0x10
  44. #define CMD_READ_ID 0x90
  45. #define CMD_RESET 0xFF
  46. #define CMD_RADOM_OUTPUT1 0x05
  47. #define CMD_RADOM_OUTPUT2 0xE0
  48. #define CMD_RADOM_INPUT 0x85
  49. #define CMD_READ_STATUS 0x70
  50. #define NAND_MFR_TOSHIBA 0x98
  51. #define NAND_MFR_SAMSUNG 0xec
  52. #define NAND_MFR_FUJITSU 0x04
  53. #define NAND_MFR_NATIONAL 0x8f
  54. #define NAND_MFR_RENESAS 0x07
  55. #define NAND_MFR_STMICRO 0x20
  56. #define NAND_MFR_HYNIX 0xad
  57. #define NAND_MFR_MICRO 0x2c
  58. #define NAND_MFR_AMD 0x01
  59. struct nand_maker{
  60. int id;
  61. const char *name;
  62. struct nand_dev *dev_desc;
  63. int (*match_id)(unsigned char *id);
  64. };
  65. struct nandflash_info {
  66. const char *name;
  67. unsigned int id;
  68. unsigned int id2;
  69. int pagesize;
  70. int chipsize;
  71. int blocksize;
  72. int oobsize;
  73. int options;
  74. };
  75. struct nand_info{
  76. int chipsize;
  77. int blksize;
  78. int blknum;
  79. int pagesize;
  80. int pagesblk;
  81. int oobsize;
  82. int colcycle;
  83. int rowcycle;
  84. int bchecccodesize;
  85. int bchsegsize;
  86. };
  87. struct nand_dev {
  88. const char *name;
  89. int id;
  90. int pagesize;
  91. int chipsize;
  92. int blocksize;
  93. };
  94. enum nand_chip_sel {
  95. NAND_CHIP0 = 0,
  96. NAND_CHIP1,
  97. NAND_CHIP2,
  98. NAND_CHIP3,
  99. };
  100. enum nand_data_width {
  101. NAND_DATA_WIDTH8 = 0,
  102. NAND_DATA_WIDTH16,
  103. };
  104. struct nand_dev nand_flash_ids[] = {
  105. // name id pagesize chipsize blocksize
  106. {"NAND 64MiB 3,3V 8-bit", 0x76, 512, 64, 0x4000},
  107. {"NAND 128MiB 3,3V 8-bit", 0x79, 512, 128, 0x4000},
  108. {"NAND 256MiB 3,3V 8-bit", 0x71, 512, 256, 0x4000},
  109. /*
  110. * These are the new chips with large page size. The pagesize and the
  111. * blocksize is determined from the extended id bytes
  112. */
  113. /*512 Megabit */
  114. {"NAND 64MiB 3,3V 8-bit", 0xF2, 0, 64, 0},
  115. {"NAND 64MiB 3,3V 8-bit", 0xD0, 0, 64, 0},
  116. /* 1 Gigabit */
  117. {"NAND 128MiB 3,3V 8-bit", 0xF1, 0, 128, 0},
  118. {"NAND 128MiB 3,3V 8-bit", 0xD1, 0, 128, 0},
  119. /* 2 Gigabit */
  120. {"NAND 256MiB 3,3V 8-bit", 0xDA, 0, 256, 0},
  121. /* 4 Gigabit */
  122. {"NAND 512MiB 3,3V 8-bit", 0xDC, 0, 512, 0},
  123. /* 8 Gigabit */
  124. {"NAND 1GiB 3,3V 8-bit", 0xD3, 0, 1024, 0},
  125. /* 16 Gigabit */
  126. {"NAND 2GiB 3,3V 8-bit", 0xD5, 0, 2048, 0},
  127. /* 32 Gigabit */
  128. {"NAND 4GiB 3,3V 8-bit", 0xD7, 0, 4096, 0},
  129. /* 64 Gigabit */
  130. {"NAND 8GiB 3,3V 8-bit", 0xDE, 0, 8192, 0},
  131. /*
  132. * Renesas AND 1 Gigabit. Those chips do not support extended id and
  133. * have a strange page/block layout ! The chosen minimum blocksize is
  134. * 4 * 2 * 2048 = 16384 Byte, as those chips have an array of 4 page
  135. * planes 1 block = 2 pages, but due to plane arrangement the blocks
  136. * 0-3 consists of page 0 + 4,1 + 5, 2 + 6, 3 + 7 Anyway JFFS2 would
  137. * increase the eraseblock size so we chose a combined one which can be
  138. * erased in one go There are more speed improvements for reads and
  139. * writes possible, but not implemented now
  140. */
  141. {"AND 128MiB 3,3V 8-bit", 0x01, 2048, 128, 0x4000},
  142. {NULL,},
  143. };
  144. static const struct nandflash_info nandflash_devices[] = {
  145. {"TC58BVG0S3HTAI0", 0x98, 0xF18015F2, 2048, 128, 128, 64,0},
  146. {"K9F1G08U0C", 0xec, 0xf1009540, 2048, 128, 128, 64, 0},
  147. {"K9F1G08U0E", 0xec, 0xf1009541, 2048, 128, 128, 64, 0},
  148. {"HY27UF081G2A", 0xad, 0xf1801dad, 2048, 128, 128, 64, 0},
  149. {"TC58NVG0S3ETA00", 0x98, 0xD1901576, 2048, 128, 128, 64, 0},
  150. {"TC58NVG0S3HTA00", 0x98, 0xF1801572, 2048, 128, 128, 128,0},
  151. {"TC58NVG1S3HTA00", 0x98, 0xDA901576, 2048, 256, 128, 128,0},
  152. {"W29N01GV", 0xEF, 0xF1809500, 2048, 128, 128, 64, 0},
  153. {"FMND1GXXX3D", 0xF8, 0xF1809500, 2048, 128, 128, 64, 0},
  154. {"XT27G02ETSIGA", 0x2C, 0xDA909506, 2048, 256, 128, 128, 0},
  155. //此款flash芯片不支持,在此处强制将OOB128字节
  156. {"9FU1G8F2AMGF", 0xC8, 0xF1801D42, 2048, 128, 128, 128, 0},
  157. };
  158. static struct nand_info nand_info;
  159. static void nand_reset(void)
  160. {
  161. rNAND_CLE_WR = CMD_RESET;
  162. do {
  163. rNAND_CLE_WR = CMD_READ_STATUS;
  164. udelay(10);//do not delete this delay avoiding to elapse the time for tWHR
  165. } while (!(rNAND_STATUS_RD & 0x40));
  166. }
  167. static void nand_readid(unsigned char *id)
  168. {
  169. int i;
  170. rNAND_CLE_WR = CMD_READ_ID;
  171. rNAND_ALE_WR = 0x00;
  172. udelay(100);
  173. for(i = 0; i < 8; i++)
  174. *id++ = rNAND_ID_RD;
  175. }
  176. static int nand_scan_table(unsigned char *flashid)
  177. {
  178. int i;
  179. unsigned int id = flashid[0];
  180. unsigned int id2 = (flashid[1] << 24) | (flashid[2] << 16) | (flashid[3] << 8) | flashid[4];
  181. for(i = 0; i < sizeof(nandflash_devices) / sizeof(nandflash_devices[0]); i++) {
  182. if(id == nandflash_devices[i].id && id2 == nandflash_devices[i].id2) {
  183. nand_info.blksize = nandflash_devices[i].blocksize * 1024;
  184. nand_info.chipsize = nandflash_devices[i].chipsize * 1024 * 1024;
  185. nand_info.pagesize = nandflash_devices[i].pagesize;
  186. nand_info.oobsize = nandflash_devices[i].oobsize;
  187. return 0;
  188. }
  189. }
  190. puts("NO Device in Table\n");
  191. return -1;
  192. }
  193. static int get_cycle(unsigned int addr)
  194. {
  195. int cycle;
  196. for (cycle = 0; cycle < 4; cycle++) {
  197. if(addr & (0xff000000 >> (8 * cycle)))
  198. return 4-cycle;
  199. }
  200. return 0;
  201. }
  202. static void get_samsung_nandinfo(unsigned char *id)
  203. {
  204. int extid = id[3];
  205. nand_info.pagesize = 2048 << (extid & 0x03);
  206. extid >>= 2;
  207. //K9GBG08U0A ID:0xECD794766443
  208. switch ((extid & 0x03) | ((extid >> 2) & 0x04)) {
  209. case 1:
  210. nand_info.oobsize= 128;
  211. break;
  212. case 2:
  213. nand_info.oobsize = 218;
  214. break;
  215. case 3:
  216. nand_info.oobsize = 400;
  217. break;
  218. case 4:
  219. nand_info.oobsize = 436;
  220. break;
  221. default:
  222. nand_info.oobsize = 640;
  223. break;
  224. }
  225. extid >>= 2;
  226. /* Calc blocksize */
  227. nand_info.blksize = (128 * 1024) << (((extid >> 1) & 0x04) | (extid & 0x03));
  228. }
  229. static void get_hynix_nandinfo(unsigned char *id)
  230. {
  231. int extid = id[3];
  232. /* Calc pagesize */
  233. nand_info.pagesize = 2048 << (extid & 0x03);
  234. extid >>= 2;
  235. switch ((extid & 0x03) | ((extid>>2) & 0x04)) {
  236. case 0:
  237. nand_info.oobsize= 128;
  238. break;
  239. case 1:
  240. nand_info.oobsize = 224;
  241. break;
  242. case 2:
  243. nand_info.oobsize = 448;
  244. break;
  245. case 3:
  246. nand_info.oobsize = 64;
  247. break;
  248. case 4:
  249. nand_info.oobsize = 32;
  250. break;
  251. case 5:
  252. nand_info.oobsize = 16;
  253. break;
  254. case 6:
  255. nand_info.oobsize = 640;
  256. break;
  257. }
  258. extid >>= 2;
  259. switch(((extid >> 1) & 0x04) | (extid & 0x03)) {
  260. case 0:
  261. nand_info.blksize = 128 << 10;
  262. break;
  263. case 1:
  264. nand_info.blksize = 256 << 10;
  265. break;
  266. case 2:
  267. nand_info.blksize = 512 << 10;
  268. break;
  269. case 3:
  270. nand_info.blksize = 768 << 10;
  271. break;
  272. case 4:
  273. nand_info.blksize = 1 << 20;
  274. break;
  275. case 5:
  276. nand_info.blksize = 2 << 20;
  277. break;
  278. case 6:
  279. nand_info.blksize = 4 << 20;
  280. break;
  281. }
  282. }
  283. static int nand_readoob(int block, int page, int offset, unsigned int *buf, int size)
  284. {
  285. int i;
  286. unsigned int page_addr;
  287. page_addr = block * nand_info.pagesblk + page;
  288. rBCH_NAND_STATUS = 0x1;
  289. if(nand_info.pagesize > 512) {
  290. rNAND_CLE_WR = CMD_READ1;//0x00;
  291. rNAND_ALE_WR = offset;
  292. rNAND_ALE_WR = nand_info.pagesize >> 8;
  293. } else {
  294. rNAND_CLE_WR = 0x50;
  295. rNAND_ALE_WR = offset;
  296. }
  297. //send address
  298. for (i = 0; i < nand_info.rowcycle; i++)
  299. rNAND_ALE_WR = (page_addr >> (i * 8)) & 0xff;
  300. if (nand_info.pagesize >= 2048)
  301. rNAND_CLE_WR = CMD_READ2;
  302. while(!(rBCH_NAND_STATUS & (1 << 18)));
  303. for ( i = 0; i < size; i ++)
  304. *buf++ = rNAND_DATA;
  305. return 0;
  306. }
  307. static int nand_is_bad_block(int block)
  308. {
  309. unsigned int val;
  310. nand_readoob(block, 0, 0, &val, 1);
  311. if ((val & 0xffff) != 0xffff)
  312. return 1;
  313. return 0;
  314. }
  315. static int nand_read_page(int block, int page, void *dst)
  316. {
  317. unsigned int PhyAddr;
  318. int i,j;
  319. unsigned int *data = (unsigned int *)dst;
  320. unsigned char *buf = (unsigned char*)dst;
  321. unsigned int ECCData;
  322. unsigned int BchDecode;
  323. unsigned char BchDecodeStatus;
  324. unsigned int ErrByteAddr[2];
  325. unsigned int ErrBitAddr[2];
  326. int nBCH_DECODE_REGs = 4, nBCH_ENCODE_REGs = 4;
  327. unsigned int nMaxBCHSectors;
  328. int result = 0;
  329. int nECCCodeSize;
  330. nECCCodeSize = nand_info.bchecccodesize;
  331. PhyAddr = block * nand_info.pagesblk + page;
  332. //clear rb bit status
  333. rBCH_NAND_STATUS = 0x1; //add by frank
  334. //send cmd
  335. rNAND_CLE_WR = CMD_READ1;//0x00;
  336. //send address
  337. for (i = 0; i < nand_info.colcycle; i++)
  338. rNAND_ALE_WR = 0x00;
  339. for(i = 0; i < nand_info.rowcycle; i++)
  340. rNAND_ALE_WR = (PhyAddr >> (i*8)) & 0xff;
  341. if(nand_info.pagesize >= 2048)
  342. rNAND_CLE_WR = CMD_READ2;
  343. nMaxBCHSectors = nand_info.pagesize/nand_info.bchsegsize;
  344. while (!(rBCH_NAND_STATUS & (1 << 18)));
  345. for(i = 0; i < nMaxBCHSectors; i++) {
  346. rBCH_CR |= BCH_CR_SECTOR_MODE;
  347. //enable ecc controller, it will auto caculate the ecc code while data reading in the following code
  348. rBCH_CR |= BCH_CR_DECODER_RESET;//reset
  349. rBCH_CR &= ~BCH_CR_DECODER_RESET;
  350. rBCH_CR |= (BCH_CR_SOFT_ECC_ENABLE|BCH_CR_BCH_ENABLE);
  351. //read data from fifo with block process
  352. rNAND_RDBLK_START = 1;
  353. for (j = 0; j < (nand_info.bchsegsize >> 2); j++)
  354. *data++ = rNAND_RX_FIFO;
  355. //wait for data decode end while useing block process
  356. while(!(rBCH_INT & NAND_INT_DECODE_END));
  357. rBCH_INT = 1;
  358. //read ecc data stored in spare area
  359. rNAND_CLE_WR = CMD_RADOM_OUTPUT1;//0x05;
  360. for (j = 0; j < nand_info.colcycle; j++)
  361. rNAND_ALE_WR = ((nand_info.pagesize + nand_info.oobsize - nMaxBCHSectors * nECCCodeSize
  362. + i * nECCCodeSize) >> (8 * j)) & 0xff;
  363. rNAND_CLE_WR = CMD_RADOM_OUTPUT2;//0xE0;
  364. switch(nECCCodeSize) {
  365. case 13:
  366. nBCH_ENCODE_REGs = 4;
  367. nBCH_DECODE_REGs = 4;//7 BITS
  368. break;
  369. case 23:
  370. nBCH_ENCODE_REGs = 6;
  371. nBCH_DECODE_REGs = 7;//13 BITS
  372. break;
  373. case 42:
  374. nBCH_ENCODE_REGs = 11;//24 BITS
  375. nBCH_DECODE_REGs = 12;
  376. break;
  377. case 53:
  378. nBCH_ENCODE_REGs = 14;//30 BITS
  379. nBCH_DECODE_REGs = 15;
  380. break;
  381. case 84:
  382. nBCH_ENCODE_REGs = 21;//48 BITS
  383. nBCH_DECODE_REGs = 24;
  384. break;
  385. }
  386. for (j=0; j < nBCH_ENCODE_REGs; j++)
  387. ECCData = rNAND_DATA;
  388. ECCData = ECCData; //remove compile warning
  389. //wait for ecc data read end
  390. while (rEX_BCH_DECODE_STATUS & 0x01);
  391. //begin to check data read at the forward step according the ecc decord result just now.
  392. BchDecode = rEX_BCH_DECODE_STATUS;
  393. BchDecodeStatus = (char)(BchDecode&0x7);
  394. if(BchDecodeStatus&0x2) {
  395. //All Data is right
  396. ;
  397. } else if(BchDecodeStatus&0x4) {
  398. //err more than 8 bit
  399. rBCH_CR &= ~BCH_CR_BCH_ENABLE;
  400. puts("uncorrectable ECC error\n");
  401. result = -1;
  402. } else {
  403. for(j=0;j<nBCH_DECODE_REGs;j++) {
  404. unsigned int val;
  405. val = *(volatile unsigned int*)(EX_BCH_DECODE_RESULT_ADDR + 4 * j);
  406. ErrByteAddr[0] = (val&0x3ff8)>>3;
  407. ErrBitAddr[0] = val&0x7;
  408. val = val >> 14;
  409. ErrByteAddr[1] = (val&0x3ff8)>>3;
  410. ErrBitAddr[1] = val&0x7;
  411. if(ErrByteAddr[0] < 1024)
  412. *(buf + nand_info.bchsegsize * i + ErrByteAddr[0]) ^= 1 << ErrBitAddr[0];
  413. if(ErrByteAddr[1] < 1024)
  414. *(buf + nand_info.bchsegsize * i + ErrByteAddr[1]) ^= 1 << ErrBitAddr[1];
  415. }
  416. }
  417. rBCH_INT = 1;
  418. //if there are some other sector in this page to read, then begin to send their row address
  419. if(i<(nMaxBCHSectors-1) && nMaxBCHSectors>1) {
  420. rNAND_CLE_WR = CMD_RADOM_OUTPUT1;//0x85;
  421. rNAND_ALE_WR = 0x00;
  422. rNAND_ALE_WR = ((i + 1) * nand_info.bchsegsize) >> 8;
  423. rNAND_CLE_WR = CMD_RADOM_OUTPUT2;
  424. }
  425. }
  426. rBCH_INT = 1;
  427. rBCH_CR &= ~BCH_CR_BCH_ENABLE;
  428. return result;
  429. }
  430. int nand_spl_load_image(uint32_t offs, unsigned int size, void *dst)
  431. {
  432. unsigned int block, lastblock;
  433. unsigned int page, page_offset;
  434. /* offs has to be aligned to a page address! */
  435. block = offs / nand_info.blksize;
  436. lastblock = (offs + size - 1) / nand_info.blksize;
  437. page = (offs % nand_info.blksize) / nand_info.pagesize;
  438. page_offset = offs % nand_info.pagesize;
  439. while (block <= lastblock) {
  440. if (!nand_is_bad_block(block)) {
  441. /* Skip bad blocks */
  442. while (page < nand_info.pagesblk) {
  443. nand_read_page(block, page, dst);
  444. /*
  445. * When offs is not aligned to page address the
  446. * extra offset is copied to dst as well. Copy
  447. * the image such that its first byte will be
  448. * at the dst.
  449. */
  450. if (unlikely(page_offset)) {
  451. memmove(dst, dst + page_offset,
  452. nand_info.pagesize);
  453. dst = (void *)((int)dst - page_offset);
  454. page_offset = 0;
  455. }
  456. dst += nand_info.pagesize;
  457. page++;
  458. }
  459. page = 0;
  460. } else {
  461. lastblock++;
  462. }
  463. block++;
  464. }
  465. return 0;
  466. }
  467. void nand_init(void)
  468. {
  469. unsigned char flash_id[8];
  470. int i, num;
  471. struct nand_dev nand_dev;
  472. unsigned char deviceid, devicecellid, extid;
  473. unsigned int val;
  474. rNAND_CR = 0;
  475. rNAND_CR = (1<<22)|(1<<21)|(0<<20)|(0x4<<16)|(0x3<<12)|(0x3<<8)|(0x3<<4)|(0x4<<0);
  476. nand_reset();
  477. nand_readid(flash_id);
  478. if(!nand_scan_table(flash_id)) {
  479. nand_info.blknum = nand_info.chipsize / nand_info.blksize;
  480. nand_info.pagesblk = nand_info.blksize / nand_info.pagesize;
  481. if(nand_info.pagesize == 512)
  482. nand_info.colcycle = 1;
  483. else
  484. nand_info.colcycle = get_cycle(nand_info.pagesize);
  485. nand_info.rowcycle = get_cycle(nand_info.chipsize / nand_info.pagesize - 1);
  486. } else {
  487. deviceid = flash_id[1];
  488. num = sizeof(nand_flash_ids) / sizeof(nand_flash_ids[0]);
  489. for(i = 0; i < num; i++) {
  490. if(deviceid ==nand_flash_ids[i].id) {
  491. nand_dev = nand_flash_ids[i];
  492. nand_info.chipsize = nand_flash_ids[i].chipsize << 20;
  493. break;
  494. }
  495. }
  496. /* Unkown Maker */
  497. if(i == num) {
  498. puts("Unknown Maker,Not Support The Chip!!\n");
  499. return;
  500. }
  501. if (!nand_dev.pagesize) {
  502. /* The 3rd id byte holds MLC / multichip data */
  503. devicecellid = flash_id[2];
  504. /* The 4th id byte is the important one */
  505. extid = flash_id[3];
  506. /*
  507. * Field definitions are in the following datasheets:
  508. * Old style (4,5 byte ID): Samsung K9GAG08U0M (p.32)
  509. * New style (6 byte ID): Samsung K9GBG08U0M (p.40)
  510. *
  511. * Check for wraparound + Samsung ID + nonzero 6th byte
  512. * to decide what to do.
  513. */
  514. if ((flash_id[0] == flash_id[6]) && (flash_id[1] == flash_id[7]) &&
  515. // FlashId[0] == NAND_MFR_SAMSUNG &&
  516. (devicecellid & 0x0C) &&
  517. (flash_id[5] != 0x00)) {
  518. if(flash_id[0] == NAND_MFR_SAMSUNG) {
  519. get_samsung_nandinfo(flash_id);
  520. goto nandinfo_over;
  521. } else if(flash_id[0] == NAND_MFR_HYNIX) {
  522. get_hynix_nandinfo(flash_id);
  523. goto nandinfo_over;
  524. }
  525. }
  526. /* Calc pagesize */
  527. nand_info.pagesize = 1024 << (extid & 0x03);
  528. extid >>= 2;
  529. /* Calc oobsize */
  530. nand_info.oobsize = (8 << (extid & 0x01)) *
  531. (nand_info.pagesize >> 9);
  532. extid >>= 2;
  533. /* Calc blocksize. Blocksize is multiples of 64KiB */
  534. nand_info.blksize= (64 * 1024) << (extid & 0x03);
  535. }
  536. else {
  537. /*
  538. * Old devices have chip data hardcoded in the device id table
  539. */
  540. nand_info.blksize = nand_dev.blocksize;
  541. nand_info.pagesize = nand_dev.pagesize;
  542. nand_info.oobsize = nand_info.pagesize / 32;
  543. /*
  544. * Check for Spansion/AMD ID + repeating 5th, 6th byte since
  545. * some Spansion chips have blocksize that conflicts with size
  546. * listed in nand_ids table
  547. * Data sheet (5 byte ID): Spansion S30ML-P ORNAND (p.39)
  548. */
  549. if (flash_id[0] == NAND_MFR_AMD && flash_id[4] != 0x00 &&
  550. flash_id[5] == 0x00 && flash_id[6] == 0x00 &&
  551. flash_id[7] == 0x00 && nand_info.pagesize == 512) {
  552. nand_info.blksize = 128 * 1024;
  553. nand_info.blksize <<= ((flash_id[3] & 0x03) << 1);
  554. }
  555. }
  556. nandinfo_over:
  557. if(nand_info.pagesize == 512)
  558. nand_info.colcycle = 1;
  559. else
  560. nand_info.colcycle = get_cycle(nand_info.pagesize);
  561. nand_info.rowcycle = get_cycle(nand_info.chipsize / nand_info.pagesize - 1);
  562. nand_info.pagesblk = nand_info.blksize / nand_info.pagesize;
  563. nand_info.blknum = nand_info.chipsize / nand_info.blksize;
  564. }
  565. if(nand_info.oobsize == 64) {
  566. val = rNAND_CR;
  567. val &= ~(3 << 25);
  568. rNAND_CR = val;
  569. nand_info.bchsegsize = 1024;
  570. nand_info.bchecccodesize = 23;
  571. val = rBCH_CR;
  572. val &= ~((1 << 7) | (7 << 4));
  573. val |= (1 << 7) | (1 << 4);
  574. rBCH_CR = val;
  575. debug("ECC 13 bit !!\n");
  576. } else if(nand_info.oobsize >= 128) {
  577. val = rNAND_CR;
  578. val &= ~(3<<25);
  579. rNAND_CR = val;
  580. nand_info.bchsegsize = 1024;
  581. nand_info.bchecccodesize = 42;
  582. val = rBCH_CR;
  583. val &= ~((1 << 8) | (1 << 7) | (7 << 4));
  584. val |= (1 << 7) | (2 << 4);
  585. rBCH_CR = val;
  586. debug("ECC 24 bit !!\n");
  587. }
  588. return;
  589. }
  590. void nand_deselect(void)
  591. {
  592. }