davinci_nand.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843
  1. /*
  2. * NAND driver for TI DaVinci based boards.
  3. *
  4. * Copyright (C) 2007 Sergey Kubushyn <ksi@koi8.net>
  5. *
  6. * Based on Linux DaVinci NAND driver by TI. Original copyright follows:
  7. */
  8. /*
  9. *
  10. * linux/drivers/mtd/nand/nand_davinci.c
  11. *
  12. * NAND Flash Driver
  13. *
  14. * Copyright (C) 2006 Texas Instruments.
  15. *
  16. * ----------------------------------------------------------------------------
  17. *
  18. * SPDX-License-Identifier: GPL-2.0+
  19. *
  20. * ----------------------------------------------------------------------------
  21. *
  22. * Overview:
  23. * This is a device driver for the NAND flash device found on the
  24. * DaVinci board which utilizes the Samsung k9k2g08 part.
  25. *
  26. Modifications:
  27. ver. 1.0: Feb 2005, Vinod/Sudhakar
  28. -
  29. */
  30. #include <common.h>
  31. #include <asm/io.h>
  32. #include <nand.h>
  33. #include <asm/ti-common/davinci_nand.h>
  34. /* Definitions for 4-bit hardware ECC */
  35. #define NAND_TIMEOUT 10240
  36. #define NAND_ECC_BUSY 0xC
  37. #define NAND_4BITECC_MASK 0x03FF03FF
  38. #define EMIF_NANDFSR_ECC_STATE_MASK 0x00000F00
  39. #define ECC_STATE_NO_ERR 0x0
  40. #define ECC_STATE_TOO_MANY_ERRS 0x1
  41. #define ECC_STATE_ERR_CORR_COMP_P 0x2
  42. #define ECC_STATE_ERR_CORR_COMP_N 0x3
  43. /*
  44. * Exploit the little endianness of the ARM to do multi-byte transfers
  45. * per device read. This can perform over twice as quickly as individual
  46. * byte transfers when buffer alignment is conducive.
  47. *
  48. * NOTE: This only works if the NAND is not connected to the 2 LSBs of
  49. * the address bus. On Davinci EVM platforms this has always been true.
  50. */
  51. static void nand_davinci_read_buf(struct mtd_info *mtd, uint8_t *buf, int len)
  52. {
  53. struct nand_chip *chip = mtd_to_nand(mtd);
  54. const u32 *nand = chip->IO_ADDR_R;
  55. /* Make sure that buf is 32 bit aligned */
  56. if (((int)buf & 0x3) != 0) {
  57. if (((int)buf & 0x1) != 0) {
  58. if (len) {
  59. *buf = readb(nand);
  60. buf += 1;
  61. len--;
  62. }
  63. }
  64. if (((int)buf & 0x3) != 0) {
  65. if (len >= 2) {
  66. *(u16 *)buf = readw(nand);
  67. buf += 2;
  68. len -= 2;
  69. }
  70. }
  71. }
  72. /* copy aligned data */
  73. while (len >= 4) {
  74. *(u32 *)buf = __raw_readl(nand);
  75. buf += 4;
  76. len -= 4;
  77. }
  78. /* mop up any remaining bytes */
  79. if (len) {
  80. if (len >= 2) {
  81. *(u16 *)buf = readw(nand);
  82. buf += 2;
  83. len -= 2;
  84. }
  85. if (len)
  86. *buf = readb(nand);
  87. }
  88. }
  89. static void nand_davinci_write_buf(struct mtd_info *mtd, const uint8_t *buf,
  90. int len)
  91. {
  92. struct nand_chip *chip = mtd_to_nand(mtd);
  93. const u32 *nand = chip->IO_ADDR_W;
  94. /* Make sure that buf is 32 bit aligned */
  95. if (((int)buf & 0x3) != 0) {
  96. if (((int)buf & 0x1) != 0) {
  97. if (len) {
  98. writeb(*buf, nand);
  99. buf += 1;
  100. len--;
  101. }
  102. }
  103. if (((int)buf & 0x3) != 0) {
  104. if (len >= 2) {
  105. writew(*(u16 *)buf, nand);
  106. buf += 2;
  107. len -= 2;
  108. }
  109. }
  110. }
  111. /* copy aligned data */
  112. while (len >= 4) {
  113. __raw_writel(*(u32 *)buf, nand);
  114. buf += 4;
  115. len -= 4;
  116. }
  117. /* mop up any remaining bytes */
  118. if (len) {
  119. if (len >= 2) {
  120. writew(*(u16 *)buf, nand);
  121. buf += 2;
  122. len -= 2;
  123. }
  124. if (len)
  125. writeb(*buf, nand);
  126. }
  127. }
  128. static void nand_davinci_hwcontrol(struct mtd_info *mtd, int cmd,
  129. unsigned int ctrl)
  130. {
  131. struct nand_chip *this = mtd_to_nand(mtd);
  132. u_int32_t IO_ADDR_W = (u_int32_t)this->IO_ADDR_W;
  133. if (ctrl & NAND_CTRL_CHANGE) {
  134. IO_ADDR_W &= ~(MASK_ALE|MASK_CLE);
  135. if (ctrl & NAND_CLE)
  136. IO_ADDR_W |= MASK_CLE;
  137. if (ctrl & NAND_ALE)
  138. IO_ADDR_W |= MASK_ALE;
  139. this->IO_ADDR_W = (void __iomem *) IO_ADDR_W;
  140. }
  141. if (cmd != NAND_CMD_NONE)
  142. writeb(cmd, IO_ADDR_W);
  143. }
  144. #ifdef CONFIG_SYS_NAND_HW_ECC
  145. static u_int32_t nand_davinci_readecc(struct mtd_info *mtd)
  146. {
  147. u_int32_t ecc = 0;
  148. ecc = __raw_readl(&(davinci_emif_regs->nandfecc[
  149. CONFIG_SYS_NAND_CS - 2]));
  150. return ecc;
  151. }
  152. static void nand_davinci_enable_hwecc(struct mtd_info *mtd, int mode)
  153. {
  154. u_int32_t val;
  155. /* reading the ECC result register resets the ECC calculation */
  156. nand_davinci_readecc(mtd);
  157. val = __raw_readl(&davinci_emif_regs->nandfcr);
  158. val |= DAVINCI_NANDFCR_NAND_ENABLE(CONFIG_SYS_NAND_CS);
  159. val |= DAVINCI_NANDFCR_1BIT_ECC_START(CONFIG_SYS_NAND_CS);
  160. __raw_writel(val, &davinci_emif_regs->nandfcr);
  161. }
  162. static int nand_davinci_calculate_ecc(struct mtd_info *mtd, const u_char *dat,
  163. u_char *ecc_code)
  164. {
  165. u_int32_t tmp;
  166. tmp = nand_davinci_readecc(mtd);
  167. /* Squeeze 4 bytes ECC into 3 bytes by removing RESERVED bits
  168. * and shifting. RESERVED bits are 31 to 28 and 15 to 12. */
  169. tmp = (tmp & 0x00000fff) | ((tmp & 0x0fff0000) >> 4);
  170. /* Invert so that erased block ECC is correct */
  171. tmp = ~tmp;
  172. *ecc_code++ = tmp;
  173. *ecc_code++ = tmp >> 8;
  174. *ecc_code++ = tmp >> 16;
  175. /* NOTE: the above code matches mainline Linux:
  176. * .PQR.stu ==> ~PQRstu
  177. *
  178. * MontaVista/TI kernels encode those bytes differently, use
  179. * complicated (and allegedly sometimes-wrong) correction code,
  180. * and usually shipped with U-Boot that uses software ECC:
  181. * .PQR.stu ==> PsQRtu
  182. *
  183. * If you need MV/TI compatible NAND I/O in U-Boot, it should
  184. * be possible to (a) change the mangling above, (b) reverse
  185. * that mangling in nand_davinci_correct_data() below.
  186. */
  187. return 0;
  188. }
  189. static int nand_davinci_correct_data(struct mtd_info *mtd, u_char *dat,
  190. u_char *read_ecc, u_char *calc_ecc)
  191. {
  192. struct nand_chip *this = mtd_to_nand(mtd);
  193. u_int32_t ecc_nand = read_ecc[0] | (read_ecc[1] << 8) |
  194. (read_ecc[2] << 16);
  195. u_int32_t ecc_calc = calc_ecc[0] | (calc_ecc[1] << 8) |
  196. (calc_ecc[2] << 16);
  197. u_int32_t diff = ecc_calc ^ ecc_nand;
  198. if (diff) {
  199. if ((((diff >> 12) ^ diff) & 0xfff) == 0xfff) {
  200. /* Correctable error */
  201. if ((diff >> (12 + 3)) < this->ecc.size) {
  202. uint8_t find_bit = 1 << ((diff >> 12) & 7);
  203. uint32_t find_byte = diff >> (12 + 3);
  204. dat[find_byte] ^= find_bit;
  205. MTDDEBUG(MTD_DEBUG_LEVEL0, "Correcting single "
  206. "bit ECC error at offset: %d, bit: "
  207. "%d\n", find_byte, find_bit);
  208. return 1;
  209. } else {
  210. return -EBADMSG;
  211. }
  212. } else if (!(diff & (diff - 1))) {
  213. /* Single bit ECC error in the ECC itself,
  214. nothing to fix */
  215. MTDDEBUG(MTD_DEBUG_LEVEL0, "Single bit ECC error in "
  216. "ECC.\n");
  217. return 1;
  218. } else {
  219. /* Uncorrectable error */
  220. MTDDEBUG(MTD_DEBUG_LEVEL0, "ECC UNCORRECTED_ERROR 1\n");
  221. return -EBADMSG;
  222. }
  223. }
  224. return 0;
  225. }
  226. #endif /* CONFIG_SYS_NAND_HW_ECC */
  227. #ifdef CONFIG_SYS_NAND_4BIT_HW_ECC_OOBFIRST
  228. static struct nand_ecclayout nand_davinci_4bit_layout_oobfirst = {
  229. #if defined(CONFIG_SYS_NAND_PAGE_2K)
  230. .eccbytes = 40,
  231. #ifdef CONFIG_NAND_6BYTES_OOB_FREE_10BYTES_ECC
  232. .eccpos = {
  233. 6, 7, 8, 9, 10, 11, 12, 13, 14, 15,
  234. 22, 23, 24, 25, 26, 27, 28, 29, 30, 31,
  235. 38, 39, 40, 41, 42, 43, 44, 45, 46, 47,
  236. 54, 55, 56, 57, 58, 59, 60, 61, 62, 63,
  237. },
  238. .oobfree = {
  239. {2, 4}, {16, 6}, {32, 6}, {48, 6},
  240. },
  241. #else
  242. .eccpos = {
  243. 24, 25, 26, 27, 28,
  244. 29, 30, 31, 32, 33, 34, 35, 36, 37, 38,
  245. 39, 40, 41, 42, 43, 44, 45, 46, 47, 48,
  246. 49, 50, 51, 52, 53, 54, 55, 56, 57, 58,
  247. 59, 60, 61, 62, 63,
  248. },
  249. .oobfree = {
  250. {.offset = 2, .length = 22, },
  251. },
  252. #endif /* #ifdef CONFIG_NAND_6BYTES_OOB_FREE_10BYTES_ECC */
  253. #elif defined(CONFIG_SYS_NAND_PAGE_4K)
  254. .eccbytes = 80,
  255. .eccpos = {
  256. 48, 49, 50, 51, 52, 53, 54, 55, 56, 57,
  257. 58, 59, 60, 61, 62, 63, 64, 65, 66, 67,
  258. 68, 69, 70, 71, 72, 73, 74, 75, 76, 77,
  259. 78, 79, 80, 81, 82, 83, 84, 85, 86, 87,
  260. 88, 89, 90, 91, 92, 93, 94, 95, 96, 97,
  261. 98, 99, 100, 101, 102, 103, 104, 105, 106, 107,
  262. 108, 109, 110, 111, 112, 113, 114, 115, 116, 117,
  263. 118, 119, 120, 121, 122, 123, 124, 125, 126, 127,
  264. },
  265. .oobfree = {
  266. {.offset = 2, .length = 46, },
  267. },
  268. #endif
  269. };
  270. #if defined CONFIG_KEYSTONE_RBL_NAND
  271. static struct nand_ecclayout nand_keystone_rbl_4bit_layout_oobfirst = {
  272. #if defined(CONFIG_SYS_NAND_PAGE_2K)
  273. .eccbytes = 40,
  274. .eccpos = {
  275. 6, 7, 8, 9, 10, 11, 12, 13, 14, 15,
  276. 22, 23, 24, 25, 26, 27, 28, 29, 30, 31,
  277. 38, 39, 40, 41, 42, 43, 44, 45, 46, 47,
  278. 54, 55, 56, 57, 58, 59, 60, 61, 62, 63,
  279. },
  280. .oobfree = {
  281. {.offset = 2, .length = 4, },
  282. {.offset = 16, .length = 6, },
  283. {.offset = 32, .length = 6, },
  284. {.offset = 48, .length = 6, },
  285. },
  286. #elif defined(CONFIG_SYS_NAND_PAGE_4K)
  287. .eccbytes = 80,
  288. .eccpos = {
  289. 6, 7, 8, 9, 10, 11, 12, 13, 14, 15,
  290. 22, 23, 24, 25, 26, 27, 28, 29, 30, 31,
  291. 38, 39, 40, 41, 42, 43, 44, 45, 46, 47,
  292. 54, 55, 56, 57, 58, 59, 60, 61, 62, 63,
  293. 70, 71, 72, 73, 74, 75, 76, 77, 78, 79,
  294. 86, 87, 88, 89, 90, 91, 92, 93, 94, 95,
  295. 102, 103, 104, 105, 106, 107, 108, 109, 110, 111,
  296. 118, 119, 120, 121, 122, 123, 124, 125, 126, 127,
  297. },
  298. .oobfree = {
  299. {.offset = 2, .length = 4, },
  300. {.offset = 16, .length = 6, },
  301. {.offset = 32, .length = 6, },
  302. {.offset = 48, .length = 6, },
  303. {.offset = 64, .length = 6, },
  304. {.offset = 80, .length = 6, },
  305. {.offset = 96, .length = 6, },
  306. {.offset = 112, .length = 6, },
  307. },
  308. #endif
  309. };
  310. #ifdef CONFIG_SYS_NAND_PAGE_2K
  311. #define CONFIG_KEYSTONE_NAND_MAX_RBL_PAGE CONFIG_KEYSTONE_NAND_MAX_RBL_SIZE >> 11
  312. #elif defined(CONFIG_SYS_NAND_PAGE_4K)
  313. #define CONFIG_KEYSTONE_NAND_MAX_RBL_PAGE CONFIG_KEYSTONE_NAND_MAX_RBL_SIZE >> 12
  314. #endif
  315. /**
  316. * nand_davinci_write_page - write one page
  317. * @mtd: MTD device structure
  318. * @chip: NAND chip descriptor
  319. * @buf: the data to write
  320. * @oob_required: must write chip->oob_poi to OOB
  321. * @page: page number to write
  322. * @cached: cached programming
  323. * @raw: use _raw version of write_page
  324. */
  325. static int nand_davinci_write_page(struct mtd_info *mtd, struct nand_chip *chip,
  326. uint32_t offset, int data_len,
  327. const uint8_t *buf, int oob_required,
  328. int page, int cached, int raw)
  329. {
  330. int status;
  331. int ret = 0;
  332. struct nand_ecclayout *saved_ecc_layout;
  333. /* save current ECC layout and assign Keystone RBL ECC layout */
  334. if (page < CONFIG_KEYSTONE_NAND_MAX_RBL_PAGE) {
  335. saved_ecc_layout = chip->ecc.layout;
  336. chip->ecc.layout = &nand_keystone_rbl_4bit_layout_oobfirst;
  337. mtd->oobavail = chip->ecc.layout->oobavail;
  338. }
  339. chip->cmdfunc(mtd, NAND_CMD_SEQIN, 0x00, page);
  340. if (unlikely(raw)) {
  341. status = chip->ecc.write_page_raw(mtd, chip, buf,
  342. oob_required, page);
  343. } else {
  344. status = chip->ecc.write_page(mtd, chip, buf,
  345. oob_required, page);
  346. }
  347. if (status < 0) {
  348. ret = status;
  349. goto err;
  350. }
  351. chip->cmdfunc(mtd, NAND_CMD_PAGEPROG, -1, -1);
  352. status = chip->waitfunc(mtd, chip);
  353. /*
  354. * See if operation failed and additional status checks are
  355. * available.
  356. */
  357. if ((status & NAND_STATUS_FAIL) && (chip->errstat))
  358. status = chip->errstat(mtd, chip, FL_WRITING, status, page);
  359. if (status & NAND_STATUS_FAIL) {
  360. ret = -EIO;
  361. goto err;
  362. }
  363. err:
  364. /* restore ECC layout */
  365. if (page < CONFIG_KEYSTONE_NAND_MAX_RBL_PAGE) {
  366. chip->ecc.layout = saved_ecc_layout;
  367. mtd->oobavail = saved_ecc_layout->oobavail;
  368. }
  369. return ret;
  370. }
  371. /**
  372. * nand_davinci_read_page_hwecc - hardware ECC based page read function
  373. * @mtd: mtd info structure
  374. * @chip: nand chip info structure
  375. * @buf: buffer to store read data
  376. * @oob_required: caller requires OOB data read to chip->oob_poi
  377. * @page: page number to read
  378. *
  379. * Not for syndrome calculating ECC controllers which need a special oob layout.
  380. */
  381. static int nand_davinci_read_page_hwecc(struct mtd_info *mtd, struct nand_chip *chip,
  382. uint8_t *buf, int oob_required, int page)
  383. {
  384. int i, eccsize = chip->ecc.size;
  385. int eccbytes = chip->ecc.bytes;
  386. int eccsteps = chip->ecc.steps;
  387. uint32_t *eccpos;
  388. uint8_t *p = buf;
  389. uint8_t *ecc_code = chip->buffers->ecccode;
  390. uint8_t *ecc_calc = chip->buffers->ecccalc;
  391. struct nand_ecclayout *saved_ecc_layout = chip->ecc.layout;
  392. /* save current ECC layout and assign Keystone RBL ECC layout */
  393. if (page < CONFIG_KEYSTONE_NAND_MAX_RBL_PAGE) {
  394. chip->ecc.layout = &nand_keystone_rbl_4bit_layout_oobfirst;
  395. mtd->oobavail = chip->ecc.layout->oobavail;
  396. }
  397. eccpos = chip->ecc.layout->eccpos;
  398. /* Read the OOB area first */
  399. chip->cmdfunc(mtd, NAND_CMD_READOOB, 0, page);
  400. chip->read_buf(mtd, chip->oob_poi, mtd->oobsize);
  401. chip->cmdfunc(mtd, NAND_CMD_READ0, 0, page);
  402. for (i = 0; i < chip->ecc.total; i++)
  403. ecc_code[i] = chip->oob_poi[eccpos[i]];
  404. for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
  405. int stat;
  406. chip->ecc.hwctl(mtd, NAND_ECC_READ);
  407. chip->read_buf(mtd, p, eccsize);
  408. chip->ecc.calculate(mtd, p, &ecc_calc[i]);
  409. stat = chip->ecc.correct(mtd, p, &ecc_code[i], NULL);
  410. if (stat < 0)
  411. mtd->ecc_stats.failed++;
  412. else
  413. mtd->ecc_stats.corrected += stat;
  414. }
  415. /* restore ECC layout */
  416. if (page < CONFIG_KEYSTONE_NAND_MAX_RBL_PAGE) {
  417. chip->ecc.layout = saved_ecc_layout;
  418. mtd->oobavail = saved_ecc_layout->oobavail;
  419. }
  420. return 0;
  421. }
  422. #endif /* CONFIG_KEYSTONE_RBL_NAND */
  423. static void nand_davinci_4bit_enable_hwecc(struct mtd_info *mtd, int mode)
  424. {
  425. u32 val;
  426. switch (mode) {
  427. case NAND_ECC_WRITE:
  428. case NAND_ECC_READ:
  429. /*
  430. * Start a new ECC calculation for reading or writing 512 bytes
  431. * of data.
  432. */
  433. val = __raw_readl(&davinci_emif_regs->nandfcr);
  434. val &= ~DAVINCI_NANDFCR_4BIT_ECC_SEL_MASK;
  435. val |= DAVINCI_NANDFCR_NAND_ENABLE(CONFIG_SYS_NAND_CS);
  436. val |= DAVINCI_NANDFCR_4BIT_ECC_SEL(CONFIG_SYS_NAND_CS);
  437. val |= DAVINCI_NANDFCR_4BIT_ECC_START;
  438. __raw_writel(val, &davinci_emif_regs->nandfcr);
  439. break;
  440. case NAND_ECC_READSYN:
  441. val = __raw_readl(&davinci_emif_regs->nand4bitecc[0]);
  442. break;
  443. default:
  444. break;
  445. }
  446. }
  447. static u32 nand_davinci_4bit_readecc(struct mtd_info *mtd, unsigned int ecc[4])
  448. {
  449. int i;
  450. for (i = 0; i < 4; i++) {
  451. ecc[i] = __raw_readl(&davinci_emif_regs->nand4bitecc[i]) &
  452. NAND_4BITECC_MASK;
  453. }
  454. return 0;
  455. }
  456. static int nand_davinci_4bit_calculate_ecc(struct mtd_info *mtd,
  457. const uint8_t *dat,
  458. uint8_t *ecc_code)
  459. {
  460. unsigned int hw_4ecc[4];
  461. unsigned int i;
  462. nand_davinci_4bit_readecc(mtd, hw_4ecc);
  463. /*Convert 10 bit ecc value to 8 bit */
  464. for (i = 0; i < 2; i++) {
  465. unsigned int hw_ecc_low = hw_4ecc[i * 2];
  466. unsigned int hw_ecc_hi = hw_4ecc[(i * 2) + 1];
  467. /* Take first 8 bits from val1 (count1=0) or val5 (count1=1) */
  468. *ecc_code++ = hw_ecc_low & 0xFF;
  469. /*
  470. * Take 2 bits as LSB bits from val1 (count1=0) or val5
  471. * (count1=1) and 6 bits from val2 (count1=0) or
  472. * val5 (count1=1)
  473. */
  474. *ecc_code++ =
  475. ((hw_ecc_low >> 8) & 0x3) | ((hw_ecc_low >> 14) & 0xFC);
  476. /*
  477. * Take 4 bits from val2 (count1=0) or val5 (count1=1) and
  478. * 4 bits from val3 (count1=0) or val6 (count1=1)
  479. */
  480. *ecc_code++ =
  481. ((hw_ecc_low >> 22) & 0xF) | ((hw_ecc_hi << 4) & 0xF0);
  482. /*
  483. * Take 6 bits from val3(count1=0) or val6 (count1=1) and
  484. * 2 bits from val4 (count1=0) or val7 (count1=1)
  485. */
  486. *ecc_code++ =
  487. ((hw_ecc_hi >> 4) & 0x3F) | ((hw_ecc_hi >> 10) & 0xC0);
  488. /* Take 8 bits from val4 (count1=0) or val7 (count1=1) */
  489. *ecc_code++ = (hw_ecc_hi >> 18) & 0xFF;
  490. }
  491. return 0;
  492. }
  493. static int nand_davinci_4bit_correct_data(struct mtd_info *mtd, uint8_t *dat,
  494. uint8_t *read_ecc, uint8_t *calc_ecc)
  495. {
  496. int i;
  497. unsigned int hw_4ecc[4];
  498. unsigned int iserror;
  499. unsigned short *ecc16;
  500. unsigned int numerrors, erroraddress, errorvalue;
  501. u32 val;
  502. /*
  503. * Check for an ECC where all bytes are 0xFF. If this is the case, we
  504. * will assume we are looking at an erased page and we should ignore
  505. * the ECC.
  506. */
  507. for (i = 0; i < 10; i++) {
  508. if (read_ecc[i] != 0xFF)
  509. break;
  510. }
  511. if (i == 10)
  512. return 0;
  513. /* Convert 8 bit in to 10 bit */
  514. ecc16 = (unsigned short *)&read_ecc[0];
  515. /*
  516. * Write the parity values in the NAND Flash 4-bit ECC Load register.
  517. * Write each parity value one at a time starting from 4bit_ecc_val8
  518. * to 4bit_ecc_val1.
  519. */
  520. /*Take 2 bits from 8th byte and 8 bits from 9th byte */
  521. __raw_writel(((ecc16[4]) >> 6) & 0x3FF,
  522. &davinci_emif_regs->nand4biteccload);
  523. /* Take 4 bits from 7th byte and 6 bits from 8th byte */
  524. __raw_writel((((ecc16[3]) >> 12) & 0xF) | ((((ecc16[4])) << 4) & 0x3F0),
  525. &davinci_emif_regs->nand4biteccload);
  526. /* Take 6 bits from 6th byte and 4 bits from 7th byte */
  527. __raw_writel((ecc16[3] >> 2) & 0x3FF,
  528. &davinci_emif_regs->nand4biteccload);
  529. /* Take 8 bits from 5th byte and 2 bits from 6th byte */
  530. __raw_writel(((ecc16[2]) >> 8) | ((((ecc16[3])) << 8) & 0x300),
  531. &davinci_emif_regs->nand4biteccload);
  532. /*Take 2 bits from 3rd byte and 8 bits from 4th byte */
  533. __raw_writel((((ecc16[1]) >> 14) & 0x3) | ((((ecc16[2])) << 2) & 0x3FC),
  534. &davinci_emif_regs->nand4biteccload);
  535. /* Take 4 bits form 2nd bytes and 6 bits from 3rd bytes */
  536. __raw_writel(((ecc16[1]) >> 4) & 0x3FF,
  537. &davinci_emif_regs->nand4biteccload);
  538. /* Take 6 bits from 1st byte and 4 bits from 2nd byte */
  539. __raw_writel((((ecc16[0]) >> 10) & 0x3F) | (((ecc16[1]) << 6) & 0x3C0),
  540. &davinci_emif_regs->nand4biteccload);
  541. /* Take 10 bits from 0th and 1st bytes */
  542. __raw_writel((ecc16[0]) & 0x3FF,
  543. &davinci_emif_regs->nand4biteccload);
  544. /*
  545. * Perform a dummy read to the EMIF Revision Code and Status register.
  546. * This is required to ensure time for syndrome calculation after
  547. * writing the ECC values in previous step.
  548. */
  549. val = __raw_readl(&davinci_emif_regs->nandfsr);
  550. /*
  551. * Read the syndrome from the NAND Flash 4-Bit ECC 1-4 registers.
  552. * A syndrome value of 0 means no bit errors. If the syndrome is
  553. * non-zero then go further otherwise return.
  554. */
  555. nand_davinci_4bit_readecc(mtd, hw_4ecc);
  556. if (!(hw_4ecc[0] | hw_4ecc[1] | hw_4ecc[2] | hw_4ecc[3]))
  557. return 0;
  558. /*
  559. * Clear any previous address calculation by doing a dummy read of an
  560. * error address register.
  561. */
  562. val = __raw_readl(&davinci_emif_regs->nanderradd1);
  563. /*
  564. * Set the addr_calc_st bit(bit no 13) in the NAND Flash Control
  565. * register to 1.
  566. */
  567. __raw_writel(DAVINCI_NANDFCR_4BIT_CALC_START,
  568. &davinci_emif_regs->nandfcr);
  569. /*
  570. * Wait for the corr_state field (bits 8 to 11) in the
  571. * NAND Flash Status register to be not equal to 0x0, 0x1, 0x2, or 0x3.
  572. * Otherwise ECC calculation has not even begun and the next loop might
  573. * fail because of a false positive!
  574. */
  575. i = NAND_TIMEOUT;
  576. do {
  577. val = __raw_readl(&davinci_emif_regs->nandfsr);
  578. val &= 0xc00;
  579. i--;
  580. } while ((i > 0) && !val);
  581. /*
  582. * Wait for the corr_state field (bits 8 to 11) in the
  583. * NAND Flash Status register to be equal to 0x0, 0x1, 0x2, or 0x3.
  584. */
  585. i = NAND_TIMEOUT;
  586. do {
  587. val = __raw_readl(&davinci_emif_regs->nandfsr);
  588. val &= 0xc00;
  589. i--;
  590. } while ((i > 0) && val);
  591. iserror = __raw_readl(&davinci_emif_regs->nandfsr);
  592. iserror &= EMIF_NANDFSR_ECC_STATE_MASK;
  593. iserror = iserror >> 8;
  594. /*
  595. * ECC_STATE_TOO_MANY_ERRS (0x1) means errors cannot be
  596. * corrected (five or more errors). The number of errors
  597. * calculated (err_num field) differs from the number of errors
  598. * searched. ECC_STATE_ERR_CORR_COMP_P (0x2) means error
  599. * correction complete (errors on bit 8 or 9).
  600. * ECC_STATE_ERR_CORR_COMP_N (0x3) means error correction
  601. * complete (error exists).
  602. */
  603. if (iserror == ECC_STATE_NO_ERR) {
  604. val = __raw_readl(&davinci_emif_regs->nanderrval1);
  605. return 0;
  606. } else if (iserror == ECC_STATE_TOO_MANY_ERRS) {
  607. val = __raw_readl(&davinci_emif_regs->nanderrval1);
  608. return -EBADMSG;
  609. }
  610. numerrors = ((__raw_readl(&davinci_emif_regs->nandfsr) >> 16)
  611. & 0x3) + 1;
  612. /* Read the error address, error value and correct */
  613. for (i = 0; i < numerrors; i++) {
  614. if (i > 1) {
  615. erroraddress =
  616. ((__raw_readl(&davinci_emif_regs->nanderradd2) >>
  617. (16 * (i & 1))) & 0x3FF);
  618. erroraddress = ((512 + 7) - erroraddress);
  619. errorvalue =
  620. ((__raw_readl(&davinci_emif_regs->nanderrval2) >>
  621. (16 * (i & 1))) & 0xFF);
  622. } else {
  623. erroraddress =
  624. ((__raw_readl(&davinci_emif_regs->nanderradd1) >>
  625. (16 * (i & 1))) & 0x3FF);
  626. erroraddress = ((512 + 7) - erroraddress);
  627. errorvalue =
  628. ((__raw_readl(&davinci_emif_regs->nanderrval1) >>
  629. (16 * (i & 1))) & 0xFF);
  630. }
  631. /* xor the corrupt data with error value */
  632. if (erroraddress < 512)
  633. dat[erroraddress] ^= errorvalue;
  634. }
  635. return numerrors;
  636. }
  637. #endif /* CONFIG_SYS_NAND_4BIT_HW_ECC_OOBFIRST */
  638. static int nand_davinci_dev_ready(struct mtd_info *mtd)
  639. {
  640. return __raw_readl(&davinci_emif_regs->nandfsr) & 0x1;
  641. }
  642. static void nand_flash_init(void)
  643. {
  644. /* This is for DM6446 EVM and *very* similar. DO NOT GROW THIS!
  645. * Instead, have your board_init() set EMIF timings, based on its
  646. * knowledge of the clocks and what devices are hooked up ... and
  647. * don't even do that unless no UBL handled it.
  648. */
  649. #ifdef CONFIG_SOC_DM644X
  650. u_int32_t acfg1 = 0x3ffffffc;
  651. /*------------------------------------------------------------------*
  652. * NAND FLASH CHIP TIMEOUT @ 459 MHz *
  653. * *
  654. * AEMIF.CLK freq = PLL1/6 = 459/6 = 76.5 MHz *
  655. * AEMIF.CLK period = 1/76.5 MHz = 13.1 ns *
  656. * *
  657. *------------------------------------------------------------------*/
  658. acfg1 = 0
  659. | (0 << 31) /* selectStrobe */
  660. | (0 << 30) /* extWait */
  661. | (1 << 26) /* writeSetup 10 ns */
  662. | (3 << 20) /* writeStrobe 40 ns */
  663. | (1 << 17) /* writeHold 10 ns */
  664. | (1 << 13) /* readSetup 10 ns */
  665. | (5 << 7) /* readStrobe 60 ns */
  666. | (1 << 4) /* readHold 10 ns */
  667. | (3 << 2) /* turnAround ?? ns */
  668. | (0 << 0) /* asyncSize 8-bit bus */
  669. ;
  670. __raw_writel(acfg1, &davinci_emif_regs->ab1cr); /* CS2 */
  671. /* NAND flash on CS2 */
  672. __raw_writel(0x00000101, &davinci_emif_regs->nandfcr);
  673. #endif
  674. }
  675. void davinci_nand_init(struct nand_chip *nand)
  676. {
  677. #if defined CONFIG_KEYSTONE_RBL_NAND
  678. int i;
  679. struct nand_ecclayout *layout;
  680. layout = &nand_keystone_rbl_4bit_layout_oobfirst;
  681. layout->oobavail = 0;
  682. for (i = 0; layout->oobfree[i].length &&
  683. i < ARRAY_SIZE(layout->oobfree); i++)
  684. layout->oobavail += layout->oobfree[i].length;
  685. nand->write_page = nand_davinci_write_page;
  686. nand->ecc.read_page = nand_davinci_read_page_hwecc;
  687. #endif
  688. nand->chip_delay = 0;
  689. #ifdef CONFIG_SYS_NAND_USE_FLASH_BBT
  690. nand->bbt_options |= NAND_BBT_USE_FLASH;
  691. #endif
  692. #ifdef CONFIG_SYS_NAND_NO_SUBPAGE_WRITE
  693. nand->options |= NAND_NO_SUBPAGE_WRITE;
  694. #endif
  695. #ifdef CONFIG_SYS_NAND_BUSWIDTH_16BIT
  696. nand->options |= NAND_BUSWIDTH_16;
  697. #endif
  698. #ifdef CONFIG_SYS_NAND_HW_ECC
  699. nand->ecc.mode = NAND_ECC_HW;
  700. nand->ecc.size = 512;
  701. nand->ecc.bytes = 3;
  702. nand->ecc.strength = 1;
  703. nand->ecc.calculate = nand_davinci_calculate_ecc;
  704. nand->ecc.correct = nand_davinci_correct_data;
  705. nand->ecc.hwctl = nand_davinci_enable_hwecc;
  706. #else
  707. nand->ecc.mode = NAND_ECC_SOFT;
  708. #endif /* CONFIG_SYS_NAND_HW_ECC */
  709. #ifdef CONFIG_SYS_NAND_4BIT_HW_ECC_OOBFIRST
  710. nand->ecc.mode = NAND_ECC_HW_OOB_FIRST;
  711. nand->ecc.size = 512;
  712. nand->ecc.bytes = 10;
  713. nand->ecc.strength = 4;
  714. nand->ecc.calculate = nand_davinci_4bit_calculate_ecc;
  715. nand->ecc.correct = nand_davinci_4bit_correct_data;
  716. nand->ecc.hwctl = nand_davinci_4bit_enable_hwecc;
  717. nand->ecc.layout = &nand_davinci_4bit_layout_oobfirst;
  718. #endif
  719. /* Set address of hardware control function */
  720. nand->cmd_ctrl = nand_davinci_hwcontrol;
  721. nand->read_buf = nand_davinci_read_buf;
  722. nand->write_buf = nand_davinci_write_buf;
  723. nand->dev_ready = nand_davinci_dev_ready;
  724. nand_flash_init();
  725. }
  726. int board_nand_init(struct nand_chip *chip) __attribute__((weak));
  727. int board_nand_init(struct nand_chip *chip)
  728. {
  729. davinci_nand_init(chip);
  730. return 0;
  731. }