ark_nand.c 31 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136
  1. /*
  2. * (C) Copyright 2006 DENX Software Engineering
  3. * (C) Copyright 2012 Ho Ka Leung, ASTRI, kalho@astri.org
  4. *
  5. * See file CREDITS for list of people who contributed to this
  6. * project.
  7. *
  8. * This program is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU General Public License as
  10. * published by the Free Software Foundation; either version 2 of
  11. * the License, or (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program; if not, write to the Free Software
  20. * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
  21. * MA 02111-1307 USA
  22. */
  23. #include <common.h>
  24. #include <linux/err.h>
  25. #include <nand.h>
  26. #include <linux/mtd/mtd.h>
  27. #include <linux/mtd/nand_ecc.h>
  28. #include <asm/arch/ark-nand.h>
  29. #include <asm/io.h>
  30. #include <errno.h>
  31. #define BIT_7_ECC_BYTE 13
  32. #define BIT_13_ECC_BYTE 23
  33. #define BIT_24_ECC_BYTE 42
  34. #define BIT_30_ECC_BYTE 53
  35. #define BIT_48_ECC_BYTE 84
  36. #define EC809_NAND_ADDR_UNALIGNED 0x1
  37. #define EC809_NAND_LEN_UNALIGNED 0x2
  38. //#define USE_DATA_INTERFACE 1
  39. //#define DEBUG
  40. #if 0
  41. static struct nand_ecclayout nand_hw_eccoob_16 = { /* small page 512 byte with 16 byte oob */
  42. .eccbytes = BIT_7_ECC_BYTE,
  43. .eccpos = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15},
  44. .oobfree = {}
  45. };
  46. static struct nand_ecclayout nand_hw_eccoob_64 = { /* large page 2k with 64 byte oob */
  47. .eccbytes = BIT_7_ECC_BYTE,
  48. .eccpos = { 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15,
  49. 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28,
  50. },
  51. .oobfree = { {32, 32} }
  52. };
  53. #endif
  54. static struct nand_ecclayout nand_hw_eccoob_bootstrap = { /* large page 2k with 64 byte oob */
  55. .eccbytes = BIT_7_ECC_BYTE,
  56. .eccpos = { 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15,
  57. 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29,
  58. 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43,
  59. 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54
  60. },
  61. .oobfree = { {56, 8} }
  62. };
  63. static struct nand_ecclayout nand_hw_eccoob_64 = { /* large page 2k with 64 byte oob */
  64. .eccbytes = BIT_13_ECC_BYTE,
  65. .eccpos = {18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33,
  66. 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49,
  67. 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63
  68. },
  69. .oobfree = { {2, 16} }
  70. };
  71. //1024--24
  72. static struct nand_ecclayout nand_hw_eccoob_128 = { /* large page 2k with 64 byte oob */
  73. .eccbytes = BIT_24_ECC_BYTE,
  74. .eccpos = {44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59,
  75. 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75,
  76. 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91,
  77. 92, 93, 94, 95, 96, 97, 98, 99, 100, 101, 102, 103, 104, 105, 106, 107,
  78. 108, 109, 110, 111, 112, 113, 114, 115, 116, 117, 118, 119, 120, 121, 122, 123,
  79. 124, 125, 126, 127
  80. },
  81. .oobfree = { {2, 42} }
  82. };
  83. /*static uint8_t bbt_pattern[] = {'B', 'b', 't', '0' };
  84. static uint8_t mirror_pattern[] = {'1', 't', 'b', 'B' };
  85. static struct nand_bbt_descr Ark_OOB128_bbt_main_descr = {
  86. .options = NAND_BBT_LASTBLOCK | NAND_BBT_CREATE | NAND_BBT_WRITE
  87. | NAND_BBT_2BIT | NAND_BBT_VERSION | NAND_BBT_PERCHIP,
  88. .offs = 96,
  89. .len = 4,
  90. .veroffs = 100,
  91. .maxblocks = 4,
  92. .pattern = bbt_pattern
  93. };
  94. static struct nand_bbt_descr Ark_OOB128_bbt_mirror_descr = {
  95. .options = NAND_BBT_LASTBLOCK | NAND_BBT_CREATE | NAND_BBT_WRITE
  96. | NAND_BBT_2BIT | NAND_BBT_VERSION | NAND_BBT_PERCHIP,
  97. .offs = 96,
  98. .len = 4,
  99. .veroffs = 100,
  100. .maxblocks = 4,
  101. .pattern = mirror_pattern
  102. };
  103. static struct nand_bbt_descr Ark_OOB64_bbt_main_descr = {
  104. .options = NAND_BBT_LASTBLOCK | NAND_BBT_CREATE | NAND_BBT_WRITE
  105. | NAND_BBT_2BIT | NAND_BBT_VERSION | NAND_BBT_PERCHIP,
  106. .offs = 55,
  107. .len = 4,
  108. .veroffs = 59,
  109. .maxblocks = 4,
  110. .pattern = bbt_pattern
  111. };
  112. static struct nand_bbt_descr Ark_OOB64_bbt_mirror_descr = {
  113. .options = NAND_BBT_LASTBLOCK | NAND_BBT_CREATE | NAND_BBT_WRITE
  114. | NAND_BBT_2BIT | NAND_BBT_VERSION | NAND_BBT_PERCHIP,
  115. .offs =55,
  116. .len = 4,
  117. .veroffs = 59,
  118. .maxblocks = 4,
  119. .pattern = mirror_pattern
  120. };*/
  121. struct ark_nand {
  122. struct mtd_info mtd;
  123. struct nand_chip *nand;
  124. int max_chip;
  125. int chip_num;
  126. int raw_rw;
  127. };
  128. static struct ark_nand ark_nand_info = {0};
  129. static void ark_nand_select_chip(struct mtd_info *mtd, int chipnr)
  130. {
  131. struct nand_chip *chip = mtd->priv;
  132. struct ark_nand *nand_info = chip->priv;
  133. unsigned int nand_cr;
  134. debug("ark_nand_select_chip\n");
  135. if(chipnr == -1){
  136. chip->cmd_ctrl(mtd, NAND_CMD_NONE, 0 | NAND_CTRL_CHANGE);
  137. return;
  138. }
  139. if(chipnr < nand_info->max_chip){
  140. nand_cr = readl(rNAND_CR);
  141. nand_cr &= ~(0x3<<23);
  142. nand_cr |= (chipnr<<23);
  143. writel(nand_cr, rNAND_CR);
  144. nand_info->chip_num = chipnr;
  145. }
  146. return;
  147. }
  148. static void ark_nand_hwcontrol(struct mtd_info *mtd, int cmd, unsigned int ctrl)
  149. {
  150. struct nand_chip *chip = mtd->priv;
  151. debug("ark_nand_hwcontrol\n");
  152. /*
  153. * Point the IO_ADDR to DATA and ADDRESS registers instead
  154. * of chip address
  155. */
  156. switch (ctrl) {
  157. case NAND_CTRL_CHANGE | NAND_CTRL_CLE:
  158. debug("NAND_CTRL_CLE\n");
  159. chip->IO_ADDR_W = (void __iomem *)rNAND_CLE_WR;
  160. break;
  161. case NAND_CTRL_CHANGE | NAND_CTRL_ALE:
  162. debug("NAND_CTRL_ALE\n");
  163. chip->IO_ADDR_W = (void __iomem *)rNAND_ALE_WR;
  164. break;
  165. case NAND_CTRL_CHANGE:
  166. debug("NAND_CTRL_DATA\n");
  167. chip->IO_ADDR_W = (void __iomem *)rNAND_DATA; /* Must be a 32-bit read at ARK1680 NFC */
  168. break;
  169. }
  170. if(ctrl & NAND_NCE){
  171. ; /* TODO: Chip Enable activate */
  172. }else{
  173. ; /* TODO: Chip Enable de-activate */
  174. }
  175. /*#ifndef USE_DATA_INTERFACE
  176. if ((cmd == NAND_CMD_READ0 || cmd == NAND_CMD_SEQIN) && (ctrl & NAND_CLE)) {
  177. writel(1, rBCH_NAND_STATUS);
  178. }
  179. #endif*/
  180. if (cmd != NAND_CMD_NONE){
  181. debug("cmd 0x%X\n",cmd);
  182. writeb(cmd, chip->IO_ADDR_W);
  183. }
  184. }
  185. /**
  186. * ark_nand_read_buf - read chip data into buffer
  187. * @mtd: MTD device structure
  188. * @buf: buffer to store date
  189. * @len: number of bytes to read
  190. *
  191. * Default read function for 8bit buswith
  192. */
  193. static int rcount = 0;
  194. void ark_nand_read_buf(struct mtd_info *mtd, uint8_t *buf, int len)
  195. {
  196. int i;
  197. unsigned int* tmp_buf32;
  198. uint8_t* tmp_buf8;
  199. unsigned int unaligned_flag = 0;
  200. unsigned int excess_len;
  201. unsigned int addr_aligned;
  202. unsigned int read_addr = rNAND_DATA;
  203. struct nand_chip *chip = (struct nand_chip *)mtd->priv;
  204. struct ark_nand *nand_info = chip->priv;
  205. union{
  206. unsigned int word;
  207. unsigned char byte[4];
  208. } tmp;
  209. debug("ark_nand_read_buf\n");
  210. addr_aligned = ((unsigned int)buf)%4;
  211. if(addr_aligned){ // Address not aligned to 32 bit word
  212. debug("EC809_NAND_ADDR_UNALIGNED\n");
  213. unaligned_flag |= EC809_NAND_ADDR_UNALIGNED;
  214. }
  215. excess_len = len % 0x4;
  216. if(excess_len){ // length not aligned to 32 bit word
  217. debug("EC809_NAND_LEN_UNALIGNED\n");
  218. unaligned_flag |= EC809_NAND_LEN_UNALIGNED;
  219. }
  220. #ifndef USE_DATA_INTERFACE
  221. if (len >= chip->ecc.size && !nand_info->raw_rw)
  222. read_addr = rNAND_RX_FIFO;
  223. #endif
  224. switch(unaligned_flag){
  225. case 0:
  226. tmp_buf32 = (unsigned int*)buf;
  227. for (i = 0; i < len >> 2; i++){
  228. *tmp_buf32++ = readl(read_addr);
  229. debug("0. tmp_buf32 0x%08X\n",*(tmp_buf32-1));
  230. if(rcount)
  231. printf("%d %d 0x%08X\n",len, i, *(tmp_buf32-1));
  232. }
  233. break;
  234. case 1:
  235. tmp_buf8 = buf;
  236. for (i = 0; i < len >> 2; i++){
  237. tmp.word = readl(read_addr);
  238. debug("0. tmp.word 0x%08X\n",tmp.word);
  239. *tmp_buf8++ = tmp.byte[0];
  240. *tmp_buf8++ = tmp.byte[1];
  241. *tmp_buf8++ = tmp.byte[2];
  242. *tmp_buf8++ = tmp.byte[3];
  243. }
  244. break;
  245. case 2:
  246. tmp_buf32 = (unsigned int*)buf;
  247. for (i = 0; i < len >> 2; i++){
  248. *tmp_buf32++ = readl(read_addr);
  249. debug("1. tmp_buf32 0x%08X\n",*(tmp_buf32-1));
  250. }
  251. /* read the excess byte */
  252. tmp_buf8 = (uint8_t*)tmp_buf32;
  253. tmp.word = readl(read_addr);
  254. debug("1. tmp.word 0x%08X\n",tmp.word);
  255. for(i = 0; i < excess_len; i++){
  256. *tmp_buf8++ = tmp.byte[i];
  257. }
  258. break;
  259. case 3:
  260. tmp_buf8 = buf;
  261. for (i = 0; i < len >> 2; i++){
  262. tmp.word = readl(read_addr);
  263. debug("2. tmp.word 0x%08X\n",tmp.word);
  264. *tmp_buf8++ = tmp.byte[0];
  265. *tmp_buf8++ = tmp.byte[1];
  266. *tmp_buf8++ = tmp.byte[2];
  267. *tmp_buf8++ = tmp.byte[3];
  268. }
  269. tmp.word = readl(read_addr);
  270. debug("3. tmp.word 0x%08X\n",tmp.word);
  271. for(i = 0; i < excess_len; i++){
  272. *tmp_buf8++ = tmp.byte[i];
  273. }
  274. break;
  275. }
  276. }
  277. /**
  278. * ark_nand_write_buf - write buffer to chip
  279. * @mtd: MTD device structure
  280. * @buf: data buffer
  281. * @len: number of bytes to write
  282. *
  283. * Default write function for 8bit buswith
  284. */
  285. void ark_nand_write_buf(struct mtd_info *mtd, const uint8_t *buf, int len)
  286. {
  287. int i;
  288. unsigned int* tmp_buf32;
  289. uint8_t* tmp_buf8;
  290. unsigned int unaligned_flag = 0;
  291. unsigned int excess_len;
  292. unsigned int addr_aligned;
  293. unsigned int write_addr = rNAND_DATA;
  294. struct nand_chip *chip = (struct nand_chip *)mtd->priv;
  295. struct ark_nand *nand_info = chip->priv;
  296. union{
  297. unsigned int word;
  298. unsigned char byte[4];
  299. } tmp;
  300. debug("ark_nand_write_buf\n");
  301. tmp.word = 0xFFFFFFFF;
  302. addr_aligned = (unsigned int)buf % 4;
  303. if(addr_aligned){ // Address not aligned to 32 bit word
  304. unaligned_flag |= EC809_NAND_ADDR_UNALIGNED;
  305. }
  306. excess_len = len % 0x4;
  307. if(excess_len){ // length not aligned to 32 bit word
  308. unaligned_flag |= EC809_NAND_LEN_UNALIGNED;
  309. }
  310. #ifndef USE_DATA_INTERFACE
  311. if (len >= chip->ecc.size && !nand_info->raw_rw)
  312. write_addr = rNAND_TX_FIFO;
  313. #endif
  314. switch(unaligned_flag){
  315. case 0:
  316. tmp_buf32 = (unsigned int*)buf;
  317. for (i = 0; i < len >> 2; i++){
  318. debug("0 rNAND_DATA = 0x%08X\n",*tmp_buf32);
  319. writel(*tmp_buf32++, write_addr);
  320. }
  321. break;
  322. case 1:
  323. tmp_buf8 = (uint8_t*)buf;
  324. for (i = 0; i < len >> 2; i++){
  325. tmp.byte[0] = *tmp_buf8++;
  326. tmp.byte[1] = *tmp_buf8++;
  327. tmp.byte[2] = *tmp_buf8++;
  328. tmp.byte[3] = *tmp_buf8++;
  329. debug("1 rNAND_DATA = 0x%08X\n",tmp.word);
  330. writel(tmp.word, write_addr);
  331. }
  332. break;
  333. case 2:
  334. tmp_buf32 = (unsigned int*)buf;
  335. for (i = 0; i < len >> 2; i++){
  336. debug("2 rNAND_DATA = 0x%08X\n",*tmp_buf32);
  337. writel(*tmp_buf32++, write_addr);
  338. }
  339. /* write the excess byte */
  340. tmp_buf8 = (uint8_t*)tmp_buf32;
  341. tmp.word = 0xFFFFFFFF;
  342. for(i = 0; i < excess_len; i++){
  343. *tmp_buf8++ = tmp.byte[i];
  344. }
  345. debug("2.1 rNAND_DATA = 0x%08X\n",tmp.word);
  346. writel(tmp.word, write_addr);
  347. break;
  348. case 3:
  349. tmp_buf8 = (uint8_t*)buf;
  350. for (i = 0; i < len >> 2; i++){
  351. tmp.byte[0] = *tmp_buf8++;
  352. tmp.byte[1] = *tmp_buf8++;
  353. tmp.byte[2] = *tmp_buf8++;
  354. tmp.byte[3] = *tmp_buf8++;
  355. debug("3 rNAND_DATA = 0x%08X\n",tmp.word);
  356. writel(tmp.word, write_addr);
  357. }
  358. tmp.word = 0xFFFFFFFF;
  359. for(i = 0; i < excess_len; i++){
  360. *tmp_buf8++ = tmp.byte[i];
  361. }
  362. debug("3.1 rNAND_DATA = 0x%08X\n",tmp.word);
  363. writel(tmp.word, write_addr);
  364. break;
  365. }
  366. }
  367. /**
  368. * ark_nand_verify_buf - Verify chip data against buffer
  369. * @mtd: MTD device structure
  370. * @buf: buffer containing the data to compare
  371. * @len: number of bytes to compare
  372. *
  373. * Default verify function for 8bit buswith
  374. */
  375. /*static int ark_nand_verify_buf(struct mtd_info *mtd, const uint8_t *buf, int len)
  376. {
  377. int i,j;
  378. const uint8_t* tmp_buf8;
  379. unsigned int excess_len;
  380. union{
  381. unsigned int word;
  382. unsigned char byte[4];
  383. } tmp;
  384. debug("ark_nand_verify_buf\n");
  385. tmp_buf8 = buf;
  386. excess_len = len % 0x4;
  387. for (i = 0; i < len >> 2; i++){
  388. tmp.word = readl(rNAND_DATA);
  389. for (j = 0; j < 4; j++){
  390. if(tmp.byte[j] != *tmp_buf8++)
  391. return -EFAULT;
  392. }
  393. }
  394. if(excess_len){
  395. tmp.word = readl(rNAND_DATA);
  396. for(i = 0; i < excess_len; i++){
  397. if(tmp.byte[i] != *tmp_buf8++)
  398. return -EFAULT;
  399. }
  400. }
  401. return 0;
  402. }*/
  403. static void ark_nand_enable_hwecc(struct mtd_info *mtd, int mode)
  404. {
  405. debug("ark_nand_enable_hwecc\n");
  406. #if 0
  407. switch(mode){
  408. case NAND_ECC_READ:
  409. writel(readl(rBCH_CR) | BCH_CR_SECTOR_MODE,
  410. rBCH_CR);
  411. writel(readl(rBCH_CR) | BCH_CR_DECODER_RESET,
  412. rBCH_CR);
  413. writel(readl(rBCH_CR) & ~BCH_CR_DECODER_RESET,
  414. rBCH_CR);
  415. writel(readl(rBCH_CR) | BCH_CR_BCH_ENABLE,
  416. rBCH_CR);
  417. printf("\r\n>>>>>NAND_ECC_READ rBCH_CR 0x%08X\n",readl(rBCH_CR));
  418. break;
  419. case NAND_ECC_WRITE:
  420. writel(readl(rBCH_CR) | BCH_CR_SECTOR_MODE,
  421. rBCH_CR);
  422. writel(readl(rBCH_CR) | BCH_CR_ENCODER_RESET,
  423. rBCH_CR);
  424. writel(readl(rBCH_CR) & ~BCH_CR_ENCODER_RESET,
  425. rBCH_CR);
  426. writel(readl(rBCH_CR) | (BCH_CR_SOFT_ECC_ENABLE|BCH_CR_BCH_ENABLE),
  427. rBCH_CR);
  428. printf("\r\n>>>>>sNAND_ECC_WRITE rBCH_CR 0x%08X\n",readl(rBCH_CR));
  429. break;
  430. #else
  431. switch(mode){
  432. case NAND_ECC_READ:
  433. writel(readl(rBCH_CR) | BCH_CR_SECTOR_MODE,
  434. rBCH_CR);
  435. writel(readl(rBCH_CR) | BCH_CR_DECODER_RESET,
  436. rBCH_CR);
  437. writel(readl(rBCH_CR) & ~BCH_CR_DECODER_RESET,
  438. rBCH_CR);
  439. writel(readl(rBCH_CR) | (BCH_CR_SOFT_ECC_ENABLE|BCH_CR_BCH_ENABLE),
  440. rBCH_CR);
  441. debug("\r\n>>>>>NAND_ECC_READ rBCH_CR 0x%08X\n",readl(rBCH_CR));
  442. #ifndef USE_DATA_INTERFACE
  443. writel(1, rNAND_RDBLK_START);
  444. #endif
  445. break;
  446. case NAND_ECC_WRITE:
  447. writel(readl(rBCH_CR) | BCH_CR_SECTOR_MODE,
  448. rBCH_CR);
  449. writel(readl(rBCH_CR) | BCH_CR_ENCODER_RESET,
  450. rBCH_CR);
  451. writel(readl(rBCH_CR) & ~BCH_CR_ENCODER_RESET,
  452. rBCH_CR);
  453. writel(readl(rBCH_CR) | (BCH_CR_SOFT_ECC_ENABLE|BCH_CR_BCH_ENABLE),
  454. rBCH_CR);
  455. debug("\r\n>>>>>sNAND_ECC_WRITE rBCH_CR 0x%08X\n",readl(rBCH_CR));
  456. #ifndef USE_DATA_INTERFACE
  457. writel(1, rNAND_WRBLK_START);
  458. #endif
  459. break;
  460. #endif
  461. case NAND_ECC_READSYN:
  462. #ifndef USE_DATA_INTERFACE
  463. //wait for data decode end while useing block process
  464. while(!(readl(rBCH_INT) & NAND_INT_DECODE_END));
  465. debug("NAND_ECC_READSYN rBCH_INT 0x%08X\n",readl(rBCH_INT));
  466. writel(1, rBCH_INT);
  467. #endif
  468. debug("\r\n>>>>NAND_ECC_READSYN rBCH_CR 0x%08X\n",readl(rBCH_CR));
  469. break;
  470. }
  471. }
  472. #if 0 //512---7bit
  473. #else //1024---7bit
  474. static int ark_nand_calculate_ecc(struct mtd_info *mtd, const u_char *dat,
  475. u_char *ecc_code)
  476. {
  477. struct nand_chip *chip = mtd->priv;
  478. unsigned int bch_encode_no = 0;
  479. int i;
  480. union{
  481. unsigned int word;
  482. unsigned char byte[4];
  483. } tmp;
  484. debug("ark_nand_calculate_ecc\n");
  485. #ifndef USE_DATA_INTERFACE
  486. //wait for data encode end with block process
  487. while(!(readl(rBCH_INT) & NAND_INT_ENCODE_END));
  488. debug("rBCH_INT 0x%08X\n",readl(rBCH_INT));
  489. writel(1, rBCH_INT);
  490. #endif
  491. writel(readl(rBCH_CR) & ~BCH_CR_BCH_ENABLE,
  492. rBCH_CR); //disable BCH for write ECC Code
  493. #if 1
  494. debug("rBCH_NAND_STATUS 0x%08X : 0x%x\n", rBCH_NAND_STATUS, readl(rBCH_NAND_STATUS));
  495. #ifdef USE_DATA_INTERFACE
  496. while((readl(rBCH_NAND_STATUS) & 0x3F) != 0 ); //wait for fsm to idle state
  497. #else
  498. while((readl(rBCH_NAND_STATUS) >> 14) & 0x0F); //wait for all data in fifo to be transfered over.
  499. while((readl(rBCH_NAND_STATUS) & 0x3F) != 0 );//wait for fsm to idle state
  500. #endif
  501. debug("wait rBCH_NAND_STATUS done\n");
  502. #endif
  503. switch(chip->ecc.bytes){
  504. case BIT_7_ECC_BYTE:
  505. bch_encode_no = 4;
  506. break;
  507. case BIT_13_ECC_BYTE:
  508. bch_encode_no = 6;
  509. break;
  510. case BIT_24_ECC_BYTE:
  511. bch_encode_no = 11;
  512. break;
  513. case BIT_30_ECC_BYTE:
  514. bch_encode_no = 14;
  515. break;
  516. case BIT_48_ECC_BYTE:
  517. bch_encode_no = 21;
  518. break;
  519. }
  520. for(i = 0; i < bch_encode_no; i++){
  521. tmp.word = readl(EX_BCH_ENCODE_RESULT_ADDR + i*4);
  522. *ecc_code++ = tmp.byte[0];
  523. *ecc_code++ = tmp.byte[1];
  524. *ecc_code++ = tmp.byte[2];
  525. *ecc_code++ = tmp.byte[3];
  526. debug("EX_BCH_ENCODE_RESULT_ADDR %d: 0x%08X\n", i, tmp.word);
  527. }
  528. return 0;
  529. }
  530. static int ark_nand_correct_data(struct mtd_info *mtd, u_char *dat,
  531. u_char *read_ecc, u_char *calc_ecc)
  532. {
  533. struct nand_chip *chip = mtd->priv;
  534. unsigned int bch_decode_no = 0;
  535. unsigned char bch_decode_status;
  536. unsigned int err_byte_addr[2];
  537. unsigned int err_bit_addr[2];
  538. int i = 0, ret = 0;
  539. debug("ark_nand_correct_data\n");
  540. switch(chip->ecc.bytes){
  541. case BIT_7_ECC_BYTE:
  542. bch_decode_no = 4; /* no. of loops to correct 2 bits per loop */
  543. break;
  544. case BIT_13_ECC_BYTE:
  545. bch_decode_no = 7;
  546. break;
  547. case BIT_24_ECC_BYTE:
  548. bch_decode_no = 12;
  549. break;
  550. case BIT_30_ECC_BYTE:
  551. bch_decode_no = 15;
  552. break;
  553. case BIT_48_ECC_BYTE:
  554. bch_decode_no = 24;
  555. break;
  556. }
  557. //wait for ecc data read end
  558. while(readl(rEX_BCH_DECODE_STATUS)&0x01);
  559. #ifdef DEBUG
  560. debug("ECC code:");
  561. for(i = 0; i<chip->ecc.bytes; i++)
  562. debug("0x%X ",*read_ecc++);
  563. debug("\n");
  564. #endif
  565. /*
  566. printf("\r\nECC code:");
  567. for(i = 0; i<chip->ecc.bytes; i++)
  568. printf("0x%x ",*read_ecc++);
  569. temp=readl(rEX_BCH_DECODE_STATUS);
  570. printf("\n!! status:0x%x\n",temp);
  571. temp=readl(rBCH_CR);
  572. printf("\n!!rBCH_CR status:0x%x\n",temp);
  573. */
  574. bch_decode_status = (unsigned char)(readl(rEX_BCH_DECODE_STATUS) & 0x7);
  575. if(bch_decode_status & 0x2)
  576. { //All Data is right
  577. debug("\nAll data are correct\n");
  578. //continue;
  579. }
  580. else if(bch_decode_status & 0x4)
  581. { //err more than 8 bit
  582. writel(readl(rBCH_CR) & ~BCH_CR_BCH_ENABLE,
  583. rBCH_CR);
  584. printf("\n!!Read Data err more than 8 bit and Group = %d status:0x%x\n",i,bch_decode_status);
  585. return -EBADMSG;
  586. }
  587. else
  588. {
  589. for(i=0; i<bch_decode_no; i++){
  590. unsigned int val;
  591. val = readl(EX_BCH_DECODE_RESULT_ADDR+4*i);
  592. err_byte_addr[0] = (val&0x3ff8)>>3;
  593. err_bit_addr[0] = val&0x7;
  594. val = val >> 14;
  595. err_byte_addr[1] = (val&0x3ff8)>>3;
  596. err_bit_addr[1] = val&0x7;
  597. if(err_byte_addr[0] < 1024){
  598. (*(dat+err_byte_addr[0]) )^= (1<<err_bit_addr[0]);
  599. ret++;
  600. }
  601. if(err_byte_addr[1] < 1024){
  602. (*(dat+err_byte_addr[1]) )^= (1<<err_bit_addr[1]);
  603. ret++;
  604. }
  605. }
  606. }
  607. debug("Bit error no.: %d\n",ret);
  608. return ret;
  609. }
  610. #endif
  611. /**
  612. * ark_nand_read_page_raw_syndrome - [Intern] read raw page data without ecc
  613. * @mtd: mtd info structure
  614. * @chip: nand chip info structure
  615. * @buf: buffer to store read data
  616. * @page: page number to read
  617. *
  618. * We need a special oob layout and handling even when OOB isn't used.
  619. */
  620. static int ark_nand_read_page_raw_syndrome(struct mtd_info *mtd,
  621. struct nand_chip *chip,
  622. uint8_t *buf, int oob_required, int page)
  623. {
  624. struct ark_nand *nand_info = chip->priv;
  625. debug("ark_nand_read_page_raw_syndrome\n");
  626. debug("page %d, mtd->writesize %d\n",page, mtd->writesize);
  627. nand_info->raw_rw = 1;
  628. chip->read_buf(mtd, buf, mtd->writesize);
  629. chip->read_buf(mtd, chip->oob_poi, mtd->oobsize);
  630. nand_info->raw_rw = 0;
  631. return 0;
  632. }
  633. /**
  634. * ark_nand_read_page_syndrome - hardware ecc syndrom based page read
  635. * @mtd: mtd info structure
  636. * @chip: nand chip info structure
  637. * @buf: buffer to store read data
  638. * @page: page number to read
  639. *
  640. */
  641. static int ark_nand_read_page_syndrome(struct mtd_info *mtd, struct nand_chip *chip,
  642. uint8_t *buf, int oob_required, int page)
  643. {
  644. int i, eccsize = chip->ecc.size;
  645. int eccbytes = chip->ecc.bytes;
  646. int eccsteps = chip->ecc.steps;
  647. uint8_t *p = buf;
  648. uint8_t *oob = chip->oob_poi;
  649. int oob_pos = 0, pos = 0;
  650. debug("ark_nand_read_page_syndrome\n");
  651. oob_pos += mtd->writesize;
  652. if(chip->ecc.prepad){
  653. oob_pos += chip->ecc.prepad;
  654. oob += chip->ecc.prepad;
  655. }
  656. for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
  657. int stat;
  658. chip->ecc.hwctl(mtd, NAND_ECC_READ);
  659. chip->read_buf(mtd, p, eccsize);
  660. chip->ecc.hwctl(mtd, NAND_ECC_READSYN);
  661. if (mtd->writesize > 512){
  662. chip->cmdfunc(mtd, NAND_CMD_RNDOUT, oob_pos, -1);
  663. }else{
  664. chip->cmdfunc(mtd, NAND_CMD_READ0, oob_pos, page);
  665. }
  666. oob_pos += eccbytes;
  667. chip->read_buf(mtd, oob, eccbytes);
  668. stat = chip->ecc.correct(mtd, p, oob, NULL);
  669. oob += eccbytes;
  670. if (stat < 0) {
  671. mtd->ecc_stats.failed++;
  672. } else {
  673. mtd->ecc_stats.corrected += stat;
  674. }
  675. if(eccsteps > 1)
  676. {
  677. pos += eccsize;
  678. if (mtd->writesize > 512)
  679. {
  680. chip->cmdfunc(mtd, NAND_CMD_RNDOUT, pos, -1);
  681. }
  682. else
  683. {
  684. chip->cmdfunc(mtd, NAND_CMD_READ0, pos, page);
  685. }
  686. }
  687. }
  688. writel(1,rBCH_INT);
  689. writel(readl(rBCH_CR) & ~BCH_CR_BCH_ENABLE, rBCH_CR);
  690. /* the whole oob area if there is prepad or postpad area */
  691. if (chip->ecc.prepad){
  692. oob_pos = mtd->writesize;
  693. if (mtd->writesize > 512){
  694. chip->cmdfunc(mtd, NAND_CMD_RNDOUT, oob_pos, -1);
  695. }else{
  696. chip->cmdfunc(mtd, NAND_CMD_READ0, oob_pos, page);
  697. }
  698. chip->read_buf(mtd, chip->oob_poi, chip->ecc.prepad);
  699. }
  700. if (chip->ecc.postpad){
  701. oob_pos = mtd->writesize + chip->ecc.prepad + (chip->ecc.steps * chip->ecc.bytes);
  702. if (mtd->writesize > 512){
  703. chip->cmdfunc(mtd, NAND_CMD_RNDOUT, oob_pos, -1);
  704. }else{
  705. chip->cmdfunc(mtd, NAND_CMD_READ0, oob_pos, page);
  706. }
  707. chip->read_buf(mtd, (chip->oob_poi + chip->ecc.prepad + (chip->ecc.steps * chip->ecc.bytes)),
  708. chip->ecc.postpad);
  709. }
  710. return 0;
  711. }
  712. /**
  713. * ark_nand_read_oob_syndrome - OOB data read function
  714. * @mtd: mtd info structure
  715. * @chip: nand chip info structure
  716. * @page: page number to read
  717. * @sndcmd: flag whether to issue read command or not
  718. */
  719. static int ark_nand_read_oob_syndrome(struct mtd_info *mtd, struct nand_chip *chip,
  720. int page)
  721. {
  722. debug("ark_nand_read_oob_syndrome\n");
  723. chip->cmdfunc(mtd, NAND_CMD_READOOB, 0, page);
  724. chip->read_buf(mtd, chip->oob_poi, mtd->oobsize);
  725. return 0;
  726. }
  727. /**
  728. * ark_nand_write_oob_syndrome - OOB data write function for HW ECC
  729. * with syndrome - only for large page flash !
  730. * @mtd: mtd info structure
  731. * @chip: nand chip info structure
  732. * @page: page number to write
  733. */
  734. static int ark_nand_write_oob_syndrome(struct mtd_info *mtd,
  735. struct nand_chip *chip, int page)
  736. {
  737. int status = 0;
  738. const uint8_t *buf = chip->oob_poi;
  739. int length = mtd->oobsize;
  740. debug("ark_nand_write_oob_syndrome\n");
  741. chip->cmdfunc(mtd, NAND_CMD_SEQIN, mtd->writesize, page);
  742. chip->write_buf(mtd, buf, length);
  743. /* Send command to program the OOB data */
  744. chip->cmdfunc(mtd, NAND_CMD_PAGEPROG, -1, -1);
  745. status = chip->waitfunc(mtd, chip);
  746. return status & NAND_STATUS_FAIL ? -EIO : 0;
  747. }
  748. /**
  749. * ark_nand_write_page_raw_syndrome - raw page write function
  750. * @mtd: mtd info structure
  751. * @chip: nand chip info structure
  752. * @buf: data buffer
  753. *
  754. * We need a special oob layout and handling even when ECC isn't checked.
  755. */
  756. static int ark_nand_write_page_raw_syndrome(struct mtd_info *mtd,
  757. struct nand_chip *chip,
  758. const uint8_t *buf,
  759. int oob_required, int page)
  760. {
  761. struct ark_nand *nand_info = chip->priv;
  762. debug("ark_nand_write_page_raw_syndrome\n");
  763. nand_info->raw_rw = 1;
  764. chip->write_buf(mtd, buf, mtd->writesize);
  765. chip->write_buf(mtd, chip->oob_poi, mtd->oobsize);
  766. nand_info->raw_rw = 0;
  767. return 0;
  768. }
  769. /**
  770. * ark_nand_write_page_syndrome - hardware ecc syndrom based page write
  771. * @mtd: mtd info structure
  772. * @chip: nand chip info structure
  773. * @buf: data buffer
  774. *
  775. * The hw generator calculates the error syndrome automatically. Therefor
  776. * we need a special oob layout and handling.
  777. */
  778. static int ark_nand_write_page_syndrome(struct mtd_info *mtd,
  779. struct nand_chip *chip, const uint8_t *buf,
  780. int oob_required, int page)
  781. {
  782. int i, eccsize = chip->ecc.size;
  783. int eccbytes = chip->ecc.bytes;
  784. int eccsteps = chip->ecc.steps;
  785. const uint8_t *p = buf;
  786. uint8_t *oob = chip->oob_poi;
  787. debug("ark_nand_write_page_syndrome\n");
  788. debug("eccsize %d, eccbytes %d, eccsteps %d\n", eccsize, eccbytes, eccsteps);
  789. debug("rBCH_NAND_STATUS 0x%x\n", readl(rBCH_NAND_STATUS));
  790. if(chip->ecc.prepad){
  791. oob += chip->ecc.prepad;
  792. }
  793. for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
  794. chip->ecc.hwctl(mtd, NAND_ECC_WRITE);
  795. chip->write_buf(mtd, p, eccsize);
  796. chip->ecc.calculate(mtd, p, oob);
  797. oob += eccbytes;
  798. }
  799. debug("ark_nand: write ecc\n");
  800. chip->write_buf(mtd, chip->oob_poi, mtd->oobsize);
  801. return 0;
  802. }
  803. static int ark_nand_hw_init(struct nand_chip *chip)
  804. {
  805. unsigned int val;
  806. debug("ark_nand_hw_init\n");
  807. writel(0, rNAND_CR);
  808. val =0;
  809. #ifdef CONFIG_ARK1668EFAMILY
  810. val =(NAND_PAGE_TYPE(0) )|NAND_SEL_CHIP(0)| NAND_PRO_WRITE|NAND_CE_ENABLE |
  811. NAND_WR_SETPASS |NAND_RD_TRP_NUM(4) |NAND_RD_TREH_NUM(3)|
  812. NAND_WR_HOLD_NUM(3)|NAND_WR_WP_NUM(3)|NAND_WR_SET_NUM(4);
  813. #else
  814. val =(NAND_PAGE_TYPE(0) )|NAND_SEL_CHIP(0)| NAND_PRO_WRITE|NAND_CE_ENABLE |
  815. NAND_WR_SETPASS |NAND_RD_TRP_NUM(2) |NAND_RD_TREH_NUM(1)|
  816. NAND_WR_HOLD_NUM(3)|NAND_WR_WP_NUM(3)|NAND_WR_SET_NUM(4);
  817. #endif
  818. /* val =(NAND_PAGE_TYPE(0) )|NAND_SEL_CHIP(0)| NAND_PRO_WRITE|NAND_CE_ENABLE |
  819. NAND_WR_SETPASS |NAND_RD_TRP_NUM(2) |NAND_RD_TREH_NUM(2)|
  820. NAND_WR_HOLD_NUM(2)|NAND_WR_WP_NUM(2)|NAND_WR_SET_NUM(2); */
  821. writel(val,rNAND_CR);
  822. return 0;
  823. }
  824. static int ark_dev_ready(struct mtd_info *mtd)
  825. {
  826. struct nand_chip *chip = mtd->priv;
  827. struct ark_nand *nand_info = chip->priv;
  828. //return readl(rBCH_NAND_STATUS) & (1 << (18 + nand_info->chip_num));
  829. return readl(rBCH_NAND_STATUS) & (1 << (6 + nand_info->chip_num));
  830. }
  831. static int ark_hwecc_nand_init_param(struct nand_chip *chip, struct mtd_info *mtd)
  832. {
  833. u32 val;
  834. chip->ecc.mode = NAND_ECC_HW_SYNDROME;
  835. chip->ecc.calculate = ark_nand_calculate_ecc;
  836. chip->ecc.hwctl = ark_nand_enable_hwecc;
  837. chip->ecc.correct = ark_nand_correct_data;
  838. chip->ecc.read_page = ark_nand_read_page_syndrome;
  839. chip->ecc.read_page_raw = ark_nand_read_page_raw_syndrome;
  840. chip->ecc.read_oob = ark_nand_read_oob_syndrome;
  841. chip->ecc.write_page = ark_nand_write_page_syndrome;
  842. chip->ecc.write_page_raw = ark_nand_write_page_raw_syndrome;
  843. chip->ecc.write_oob = ark_nand_write_oob_syndrome;
  844. chip->ecc.size = 1024;
  845. if (mtd->oobsize == 64) {
  846. chip->ecc.bytes = BIT_13_ECC_BYTE; // should be 13 bytes but the ECC encoder register is in 32 bit word
  847. chip->ecc.strength = 13;
  848. chip->ecc.layout = &nand_hw_eccoob_64;
  849. chip->ecc.prepad = nand_hw_eccoob_64.eccpos[0];
  850. chip->ecc.postpad = nand_hw_eccoob_64.oobfree[0].offset;
  851. val = readl(rBCH_CR);
  852. val &= ~(0x7 << 4);
  853. val |= (1 << 8) | (1 << 7) | (1 << 4) | (1 << 0);
  854. writel(val, rBCH_CR);
  855. } else if(mtd->oobsize >= 128) {
  856. chip->ecc.bytes = BIT_24_ECC_BYTE; // should be 13 bytes but the ECC encoder register is in 32 bit word
  857. chip->ecc.strength = 24;
  858. chip->ecc.layout = &nand_hw_eccoob_128;
  859. chip->ecc.prepad = nand_hw_eccoob_128.eccpos[0];
  860. chip->ecc.postpad = nand_hw_eccoob_128.oobfree[0].offset;
  861. val = readl(rBCH_CR);
  862. val &= ~(0x7 << 4);
  863. val |= (1 << 8) | (1 << 7) | (2 << 4) | (1 << 0);
  864. writel(val, rBCH_CR);
  865. }
  866. return 0;
  867. }
  868. static int ark_nand_init(struct nand_chip *chip, int devnum)
  869. {
  870. struct mtd_info *mtd;
  871. struct ark_nand *nand_info = &ark_nand_info;
  872. int ret;
  873. ark_nand_hw_init(chip);
  874. debug("board_nand_init\n");
  875. mtd = nand_to_mtd(chip);
  876. mtd->priv = chip;
  877. chip->priv = nand_info;
  878. nand_info->max_chip = 4;
  879. nand_info->chip_num = 0;
  880. /* 5 us command delay time */
  881. chip->chip_delay = 100;
  882. chip->IO_ADDR_R = (void __iomem *)rNAND_STATUS_RD;
  883. chip->IO_ADDR_W = (void __iomem *)rNAND_DATA;
  884. #ifdef CONFIG_SYS_NAND_USE_FLASH_BBT
  885. chip->bbt_options |= NAND_BBT_USE_FLASH | NAND_BBT_NO_OOB;
  886. #endif
  887. chip->options |= NAND_NO_SUBPAGE_WRITE;//refer kernel
  888. chip->cmd_ctrl = ark_nand_hwcontrol;
  889. chip->select_chip = ark_nand_select_chip;
  890. chip->write_buf = ark_nand_write_buf;
  891. chip->read_buf = ark_nand_read_buf;
  892. chip->dev_ready = ark_dev_ready;
  893. #if 0 /* TODO: it needs to be fixed due to the 32 bit width of the data read write register */
  894. nand->verify_buf = ark_nand_verify_buf;
  895. #endif
  896. ret = nand_scan_ident(mtd, CONFIG_SYS_NAND_MAX_CHIPS, NULL);
  897. if (ret)
  898. return ret;
  899. ret = ark_hwecc_nand_init_param(chip, mtd);
  900. if (ret)
  901. return ret;
  902. ret = nand_scan_tail(mtd);
  903. if (!ret)
  904. nand_register(devnum, mtd);
  905. return ret;
  906. }
  907. static struct nand_chip nand_chip[CONFIG_SYS_MAX_NAND_DEVICE];
  908. void board_nand_init(void)
  909. {
  910. struct nand_chip *nand = &nand_chip[0];
  911. if (ark_nand_init(nand, 0))
  912. puts("ARK NAND init failed\n");
  913. }
  914. static int do_switchecc(cmd_tbl_t *cmdtp, int flag, int argc,
  915. char * const argv[])
  916. {
  917. struct nand_chip *chip = &nand_chip[0];
  918. struct mtd_info *mtd = nand_to_mtd(chip);
  919. ulong bootecc;
  920. unsigned int val;
  921. if (argc >= 2) {
  922. bootecc = simple_strtoul(argv[1], NULL, 16);
  923. if (bootecc) {
  924. chip->ecc.size = 512;
  925. chip->ecc.steps = mtd->writesize / chip->ecc.size;
  926. chip->ecc.bytes = BIT_7_ECC_BYTE; // should be 13 bytes but the ECC encoder register is in 32 bit word
  927. chip->ecc.strength = 7;
  928. chip->ecc.layout = &nand_hw_eccoob_bootstrap;
  929. chip->ecc.prepad = nand_hw_eccoob_bootstrap.eccpos[0];
  930. chip->ecc.postpad = nand_hw_eccoob_bootstrap.oobfree[0].offset;
  931. val = readl(rBCH_CR);
  932. val &= ~((1 << 7) | (0x7 << 4));
  933. val |= (1 << 8) | (1 << 0);
  934. writel(val, rBCH_CR);
  935. } else {
  936. chip->ecc.size = 1024;
  937. chip->ecc.steps = mtd->writesize / chip->ecc.size;
  938. if (mtd->oobsize == 64) {
  939. chip->ecc.bytes = BIT_13_ECC_BYTE; // should be 13 bytes but the ECC encoder register is in 32 bit word
  940. chip->ecc.strength = 13;
  941. chip->ecc.layout = &nand_hw_eccoob_64;
  942. chip->ecc.prepad = nand_hw_eccoob_64.eccpos[0];
  943. chip->ecc.postpad = nand_hw_eccoob_64.oobfree[0].offset;
  944. val = readl(rBCH_CR);
  945. val &= ~(0x7 << 4);
  946. val |= (1 << 8) | (1 << 7) | (1 << 4) | (1 << 0);
  947. writel(val, rBCH_CR);
  948. } else if(mtd->oobsize >= 128) {
  949. chip->ecc.bytes = BIT_24_ECC_BYTE; // should be 13 bytes but the ECC encoder register is in 32 bit word
  950. chip->ecc.strength = 24;
  951. chip->ecc.layout = &nand_hw_eccoob_128;
  952. chip->ecc.prepad = nand_hw_eccoob_128.eccpos[0];
  953. chip->ecc.postpad = nand_hw_eccoob_128.oobfree[0].offset;
  954. val = readl(rBCH_CR);
  955. val &= ~(0x7 << 4);
  956. val |= (1 << 8) | (1 << 7) | (2 << 4) | (1 << 0);
  957. writel(val, rBCH_CR);
  958. }
  959. }
  960. }
  961. return 0;
  962. }
  963. U_BOOT_CMD(switchecc, 4, 1, do_switchecc,
  964. "switch or restore nand ecc to/from bootstrap setting",
  965. "1:to bootstrap ecc; 0:restore to normal ecc"
  966. );