davinci_nand.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652
  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->priv;
  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->priv;
  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->priv;
  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->priv;
  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 -1;
  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 -1;
  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. static void nand_davinci_4bit_enable_hwecc(struct mtd_info *mtd, int mode)
  271. {
  272. u32 val;
  273. switch (mode) {
  274. case NAND_ECC_WRITE:
  275. case NAND_ECC_READ:
  276. /*
  277. * Start a new ECC calculation for reading or writing 512 bytes
  278. * of data.
  279. */
  280. val = __raw_readl(&davinci_emif_regs->nandfcr);
  281. val &= ~DAVINCI_NANDFCR_4BIT_ECC_SEL_MASK;
  282. val |= DAVINCI_NANDFCR_NAND_ENABLE(CONFIG_SYS_NAND_CS);
  283. val |= DAVINCI_NANDFCR_4BIT_ECC_SEL(CONFIG_SYS_NAND_CS);
  284. val |= DAVINCI_NANDFCR_4BIT_ECC_START;
  285. __raw_writel(val, &davinci_emif_regs->nandfcr);
  286. break;
  287. case NAND_ECC_READSYN:
  288. val = __raw_readl(&davinci_emif_regs->nand4bitecc[0]);
  289. break;
  290. default:
  291. break;
  292. }
  293. }
  294. static u32 nand_davinci_4bit_readecc(struct mtd_info *mtd, unsigned int ecc[4])
  295. {
  296. int i;
  297. for (i = 0; i < 4; i++) {
  298. ecc[i] = __raw_readl(&davinci_emif_regs->nand4bitecc[i]) &
  299. NAND_4BITECC_MASK;
  300. }
  301. return 0;
  302. }
  303. static int nand_davinci_4bit_calculate_ecc(struct mtd_info *mtd,
  304. const uint8_t *dat,
  305. uint8_t *ecc_code)
  306. {
  307. unsigned int hw_4ecc[4];
  308. unsigned int i;
  309. nand_davinci_4bit_readecc(mtd, hw_4ecc);
  310. /*Convert 10 bit ecc value to 8 bit */
  311. for (i = 0; i < 2; i++) {
  312. unsigned int hw_ecc_low = hw_4ecc[i * 2];
  313. unsigned int hw_ecc_hi = hw_4ecc[(i * 2) + 1];
  314. /* Take first 8 bits from val1 (count1=0) or val5 (count1=1) */
  315. *ecc_code++ = hw_ecc_low & 0xFF;
  316. /*
  317. * Take 2 bits as LSB bits from val1 (count1=0) or val5
  318. * (count1=1) and 6 bits from val2 (count1=0) or
  319. * val5 (count1=1)
  320. */
  321. *ecc_code++ =
  322. ((hw_ecc_low >> 8) & 0x3) | ((hw_ecc_low >> 14) & 0xFC);
  323. /*
  324. * Take 4 bits from val2 (count1=0) or val5 (count1=1) and
  325. * 4 bits from val3 (count1=0) or val6 (count1=1)
  326. */
  327. *ecc_code++ =
  328. ((hw_ecc_low >> 22) & 0xF) | ((hw_ecc_hi << 4) & 0xF0);
  329. /*
  330. * Take 6 bits from val3(count1=0) or val6 (count1=1) and
  331. * 2 bits from val4 (count1=0) or val7 (count1=1)
  332. */
  333. *ecc_code++ =
  334. ((hw_ecc_hi >> 4) & 0x3F) | ((hw_ecc_hi >> 10) & 0xC0);
  335. /* Take 8 bits from val4 (count1=0) or val7 (count1=1) */
  336. *ecc_code++ = (hw_ecc_hi >> 18) & 0xFF;
  337. }
  338. return 0;
  339. }
  340. static int nand_davinci_4bit_correct_data(struct mtd_info *mtd, uint8_t *dat,
  341. uint8_t *read_ecc, uint8_t *calc_ecc)
  342. {
  343. int i;
  344. unsigned int hw_4ecc[4];
  345. unsigned int iserror;
  346. unsigned short *ecc16;
  347. unsigned int numerrors, erroraddress, errorvalue;
  348. u32 val;
  349. /*
  350. * Check for an ECC where all bytes are 0xFF. If this is the case, we
  351. * will assume we are looking at an erased page and we should ignore
  352. * the ECC.
  353. */
  354. for (i = 0; i < 10; i++) {
  355. if (read_ecc[i] != 0xFF)
  356. break;
  357. }
  358. if (i == 10)
  359. return 0;
  360. /* Convert 8 bit in to 10 bit */
  361. ecc16 = (unsigned short *)&read_ecc[0];
  362. /*
  363. * Write the parity values in the NAND Flash 4-bit ECC Load register.
  364. * Write each parity value one at a time starting from 4bit_ecc_val8
  365. * to 4bit_ecc_val1.
  366. */
  367. /*Take 2 bits from 8th byte and 8 bits from 9th byte */
  368. __raw_writel(((ecc16[4]) >> 6) & 0x3FF,
  369. &davinci_emif_regs->nand4biteccload);
  370. /* Take 4 bits from 7th byte and 6 bits from 8th byte */
  371. __raw_writel((((ecc16[3]) >> 12) & 0xF) | ((((ecc16[4])) << 4) & 0x3F0),
  372. &davinci_emif_regs->nand4biteccload);
  373. /* Take 6 bits from 6th byte and 4 bits from 7th byte */
  374. __raw_writel((ecc16[3] >> 2) & 0x3FF,
  375. &davinci_emif_regs->nand4biteccload);
  376. /* Take 8 bits from 5th byte and 2 bits from 6th byte */
  377. __raw_writel(((ecc16[2]) >> 8) | ((((ecc16[3])) << 8) & 0x300),
  378. &davinci_emif_regs->nand4biteccload);
  379. /*Take 2 bits from 3rd byte and 8 bits from 4th byte */
  380. __raw_writel((((ecc16[1]) >> 14) & 0x3) | ((((ecc16[2])) << 2) & 0x3FC),
  381. &davinci_emif_regs->nand4biteccload);
  382. /* Take 4 bits form 2nd bytes and 6 bits from 3rd bytes */
  383. __raw_writel(((ecc16[1]) >> 4) & 0x3FF,
  384. &davinci_emif_regs->nand4biteccload);
  385. /* Take 6 bits from 1st byte and 4 bits from 2nd byte */
  386. __raw_writel((((ecc16[0]) >> 10) & 0x3F) | (((ecc16[1]) << 6) & 0x3C0),
  387. &davinci_emif_regs->nand4biteccload);
  388. /* Take 10 bits from 0th and 1st bytes */
  389. __raw_writel((ecc16[0]) & 0x3FF,
  390. &davinci_emif_regs->nand4biteccload);
  391. /*
  392. * Perform a dummy read to the EMIF Revision Code and Status register.
  393. * This is required to ensure time for syndrome calculation after
  394. * writing the ECC values in previous step.
  395. */
  396. val = __raw_readl(&davinci_emif_regs->nandfsr);
  397. /*
  398. * Read the syndrome from the NAND Flash 4-Bit ECC 1-4 registers.
  399. * A syndrome value of 0 means no bit errors. If the syndrome is
  400. * non-zero then go further otherwise return.
  401. */
  402. nand_davinci_4bit_readecc(mtd, hw_4ecc);
  403. if (!(hw_4ecc[0] | hw_4ecc[1] | hw_4ecc[2] | hw_4ecc[3]))
  404. return 0;
  405. /*
  406. * Clear any previous address calculation by doing a dummy read of an
  407. * error address register.
  408. */
  409. val = __raw_readl(&davinci_emif_regs->nanderradd1);
  410. /*
  411. * Set the addr_calc_st bit(bit no 13) in the NAND Flash Control
  412. * register to 1.
  413. */
  414. __raw_writel(DAVINCI_NANDFCR_4BIT_CALC_START,
  415. &davinci_emif_regs->nandfcr);
  416. /*
  417. * Wait for the corr_state field (bits 8 to 11) in the
  418. * NAND Flash Status register to be not equal to 0x0, 0x1, 0x2, or 0x3.
  419. * Otherwise ECC calculation has not even begun and the next loop might
  420. * fail because of a false positive!
  421. */
  422. i = NAND_TIMEOUT;
  423. do {
  424. val = __raw_readl(&davinci_emif_regs->nandfsr);
  425. val &= 0xc00;
  426. i--;
  427. } while ((i > 0) && !val);
  428. /*
  429. * Wait for the corr_state field (bits 8 to 11) in the
  430. * NAND Flash Status register to be equal to 0x0, 0x1, 0x2, or 0x3.
  431. */
  432. i = NAND_TIMEOUT;
  433. do {
  434. val = __raw_readl(&davinci_emif_regs->nandfsr);
  435. val &= 0xc00;
  436. i--;
  437. } while ((i > 0) && val);
  438. iserror = __raw_readl(&davinci_emif_regs->nandfsr);
  439. iserror &= EMIF_NANDFSR_ECC_STATE_MASK;
  440. iserror = iserror >> 8;
  441. /*
  442. * ECC_STATE_TOO_MANY_ERRS (0x1) means errors cannot be
  443. * corrected (five or more errors). The number of errors
  444. * calculated (err_num field) differs from the number of errors
  445. * searched. ECC_STATE_ERR_CORR_COMP_P (0x2) means error
  446. * correction complete (errors on bit 8 or 9).
  447. * ECC_STATE_ERR_CORR_COMP_N (0x3) means error correction
  448. * complete (error exists).
  449. */
  450. if (iserror == ECC_STATE_NO_ERR) {
  451. val = __raw_readl(&davinci_emif_regs->nanderrval1);
  452. return 0;
  453. } else if (iserror == ECC_STATE_TOO_MANY_ERRS) {
  454. val = __raw_readl(&davinci_emif_regs->nanderrval1);
  455. return -1;
  456. }
  457. numerrors = ((__raw_readl(&davinci_emif_regs->nandfsr) >> 16)
  458. & 0x3) + 1;
  459. /* Read the error address, error value and correct */
  460. for (i = 0; i < numerrors; i++) {
  461. if (i > 1) {
  462. erroraddress =
  463. ((__raw_readl(&davinci_emif_regs->nanderradd2) >>
  464. (16 * (i & 1))) & 0x3FF);
  465. erroraddress = ((512 + 7) - erroraddress);
  466. errorvalue =
  467. ((__raw_readl(&davinci_emif_regs->nanderrval2) >>
  468. (16 * (i & 1))) & 0xFF);
  469. } else {
  470. erroraddress =
  471. ((__raw_readl(&davinci_emif_regs->nanderradd1) >>
  472. (16 * (i & 1))) & 0x3FF);
  473. erroraddress = ((512 + 7) - erroraddress);
  474. errorvalue =
  475. ((__raw_readl(&davinci_emif_regs->nanderrval1) >>
  476. (16 * (i & 1))) & 0xFF);
  477. }
  478. /* xor the corrupt data with error value */
  479. if (erroraddress < 512)
  480. dat[erroraddress] ^= errorvalue;
  481. }
  482. return numerrors;
  483. }
  484. #endif /* CONFIG_SYS_NAND_4BIT_HW_ECC_OOBFIRST */
  485. static int nand_davinci_dev_ready(struct mtd_info *mtd)
  486. {
  487. return __raw_readl(&davinci_emif_regs->nandfsr) & 0x1;
  488. }
  489. static void nand_flash_init(void)
  490. {
  491. /* This is for DM6446 EVM and *very* similar. DO NOT GROW THIS!
  492. * Instead, have your board_init() set EMIF timings, based on its
  493. * knowledge of the clocks and what devices are hooked up ... and
  494. * don't even do that unless no UBL handled it.
  495. */
  496. #ifdef CONFIG_SOC_DM644X
  497. u_int32_t acfg1 = 0x3ffffffc;
  498. /*------------------------------------------------------------------*
  499. * NAND FLASH CHIP TIMEOUT @ 459 MHz *
  500. * *
  501. * AEMIF.CLK freq = PLL1/6 = 459/6 = 76.5 MHz *
  502. * AEMIF.CLK period = 1/76.5 MHz = 13.1 ns *
  503. * *
  504. *------------------------------------------------------------------*/
  505. acfg1 = 0
  506. | (0 << 31) /* selectStrobe */
  507. | (0 << 30) /* extWait */
  508. | (1 << 26) /* writeSetup 10 ns */
  509. | (3 << 20) /* writeStrobe 40 ns */
  510. | (1 << 17) /* writeHold 10 ns */
  511. | (1 << 13) /* readSetup 10 ns */
  512. | (5 << 7) /* readStrobe 60 ns */
  513. | (1 << 4) /* readHold 10 ns */
  514. | (3 << 2) /* turnAround ?? ns */
  515. | (0 << 0) /* asyncSize 8-bit bus */
  516. ;
  517. __raw_writel(acfg1, &davinci_emif_regs->ab1cr); /* CS2 */
  518. /* NAND flash on CS2 */
  519. __raw_writel(0x00000101, &davinci_emif_regs->nandfcr);
  520. #endif
  521. }
  522. void davinci_nand_init(struct nand_chip *nand)
  523. {
  524. nand->chip_delay = 0;
  525. #ifdef CONFIG_SYS_NAND_USE_FLASH_BBT
  526. nand->bbt_options |= NAND_BBT_USE_FLASH;
  527. #endif
  528. #ifdef CONFIG_SYS_NAND_NO_SUBPAGE_WRITE
  529. nand->options |= NAND_NO_SUBPAGE_WRITE;
  530. #endif
  531. #ifdef CONFIG_SYS_NAND_HW_ECC
  532. nand->ecc.mode = NAND_ECC_HW;
  533. nand->ecc.size = 512;
  534. nand->ecc.bytes = 3;
  535. nand->ecc.strength = 1;
  536. nand->ecc.calculate = nand_davinci_calculate_ecc;
  537. nand->ecc.correct = nand_davinci_correct_data;
  538. nand->ecc.hwctl = nand_davinci_enable_hwecc;
  539. #else
  540. nand->ecc.mode = NAND_ECC_SOFT;
  541. #endif /* CONFIG_SYS_NAND_HW_ECC */
  542. #ifdef CONFIG_SYS_NAND_4BIT_HW_ECC_OOBFIRST
  543. nand->ecc.mode = NAND_ECC_HW_OOB_FIRST;
  544. nand->ecc.size = 512;
  545. nand->ecc.bytes = 10;
  546. nand->ecc.strength = 4;
  547. nand->ecc.calculate = nand_davinci_4bit_calculate_ecc;
  548. nand->ecc.correct = nand_davinci_4bit_correct_data;
  549. nand->ecc.hwctl = nand_davinci_4bit_enable_hwecc;
  550. nand->ecc.layout = &nand_davinci_4bit_layout_oobfirst;
  551. #endif
  552. /* Set address of hardware control function */
  553. nand->cmd_ctrl = nand_davinci_hwcontrol;
  554. nand->read_buf = nand_davinci_read_buf;
  555. nand->write_buf = nand_davinci_write_buf;
  556. nand->dev_ready = nand_davinci_dev_ready;
  557. nand_flash_init();
  558. }
  559. int board_nand_init(struct nand_chip *chip) __attribute__((weak));
  560. int board_nand_init(struct nand_chip *chip)
  561. {
  562. davinci_nand_init(chip);
  563. return 0;
  564. }