davinci_nand.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634
  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. * This program is free software; you can redistribute it and/or modify
  19. * it under the terms of the GNU General Public License as published by
  20. * the Free Software Foundation; either version 2 of the License, or
  21. * (at your option) any later version.
  22. *
  23. * This program is distributed in the hope that it will be useful,
  24. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  25. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  26. * GNU General Public License for more details.
  27. *
  28. * You should have received a copy of the GNU General Public License
  29. * along with this program; if not, write to the Free Software
  30. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  31. * ----------------------------------------------------------------------------
  32. *
  33. * Overview:
  34. * This is a device driver for the NAND flash device found on the
  35. * DaVinci board which utilizes the Samsung k9k2g08 part.
  36. *
  37. Modifications:
  38. ver. 1.0: Feb 2005, Vinod/Sudhakar
  39. -
  40. *
  41. */
  42. #include <common.h>
  43. #include <asm/io.h>
  44. #include <nand.h>
  45. #include <asm/arch/nand_defs.h>
  46. #include <asm/arch/emif_defs.h>
  47. /* Definitions for 4-bit hardware ECC */
  48. #define NAND_TIMEOUT 10240
  49. #define NAND_ECC_BUSY 0xC
  50. #define NAND_4BITECC_MASK 0x03FF03FF
  51. #define EMIF_NANDFSR_ECC_STATE_MASK 0x00000F00
  52. #define ECC_STATE_NO_ERR 0x0
  53. #define ECC_STATE_TOO_MANY_ERRS 0x1
  54. #define ECC_STATE_ERR_CORR_COMP_P 0x2
  55. #define ECC_STATE_ERR_CORR_COMP_N 0x3
  56. /*
  57. * Exploit the little endianness of the ARM to do multi-byte transfers
  58. * per device read. This can perform over twice as quickly as individual
  59. * byte transfers when buffer alignment is conducive.
  60. *
  61. * NOTE: This only works if the NAND is not connected to the 2 LSBs of
  62. * the address bus. On Davinci EVM platforms this has always been true.
  63. */
  64. static void nand_davinci_read_buf(struct mtd_info *mtd, uint8_t *buf, int len)
  65. {
  66. struct nand_chip *chip = mtd->priv;
  67. const u32 *nand = chip->IO_ADDR_R;
  68. /* Make sure that buf is 32 bit aligned */
  69. if (((int)buf & 0x3) != 0) {
  70. if (((int)buf & 0x1) != 0) {
  71. if (len) {
  72. *buf = readb(nand);
  73. buf += 1;
  74. len--;
  75. }
  76. }
  77. if (((int)buf & 0x3) != 0) {
  78. if (len >= 2) {
  79. *(u16 *)buf = readw(nand);
  80. buf += 2;
  81. len -= 2;
  82. }
  83. }
  84. }
  85. /* copy aligned data */
  86. while (len >= 4) {
  87. *(u32 *)buf = __raw_readl(nand);
  88. buf += 4;
  89. len -= 4;
  90. }
  91. /* mop up any remaining bytes */
  92. if (len) {
  93. if (len >= 2) {
  94. *(u16 *)buf = readw(nand);
  95. buf += 2;
  96. len -= 2;
  97. }
  98. if (len)
  99. *buf = readb(nand);
  100. }
  101. }
  102. static void nand_davinci_write_buf(struct mtd_info *mtd, const uint8_t *buf,
  103. int len)
  104. {
  105. struct nand_chip *chip = mtd->priv;
  106. const u32 *nand = chip->IO_ADDR_W;
  107. /* Make sure that buf is 32 bit aligned */
  108. if (((int)buf & 0x3) != 0) {
  109. if (((int)buf & 0x1) != 0) {
  110. if (len) {
  111. writeb(*buf, nand);
  112. buf += 1;
  113. len--;
  114. }
  115. }
  116. if (((int)buf & 0x3) != 0) {
  117. if (len >= 2) {
  118. writew(*(u16 *)buf, nand);
  119. buf += 2;
  120. len -= 2;
  121. }
  122. }
  123. }
  124. /* copy aligned data */
  125. while (len >= 4) {
  126. __raw_writel(*(u32 *)buf, nand);
  127. buf += 4;
  128. len -= 4;
  129. }
  130. /* mop up any remaining bytes */
  131. if (len) {
  132. if (len >= 2) {
  133. writew(*(u16 *)buf, nand);
  134. buf += 2;
  135. len -= 2;
  136. }
  137. if (len)
  138. writeb(*buf, nand);
  139. }
  140. }
  141. static void nand_davinci_hwcontrol(struct mtd_info *mtd, int cmd,
  142. unsigned int ctrl)
  143. {
  144. struct nand_chip *this = mtd->priv;
  145. u_int32_t IO_ADDR_W = (u_int32_t)this->IO_ADDR_W;
  146. if (ctrl & NAND_CTRL_CHANGE) {
  147. IO_ADDR_W &= ~(MASK_ALE|MASK_CLE);
  148. if (ctrl & NAND_CLE)
  149. IO_ADDR_W |= MASK_CLE;
  150. if (ctrl & NAND_ALE)
  151. IO_ADDR_W |= MASK_ALE;
  152. this->IO_ADDR_W = (void __iomem *) IO_ADDR_W;
  153. }
  154. if (cmd != NAND_CMD_NONE)
  155. writeb(cmd, IO_ADDR_W);
  156. }
  157. #ifdef CONFIG_SYS_NAND_HW_ECC
  158. static void nand_davinci_enable_hwecc(struct mtd_info *mtd, int mode)
  159. {
  160. u_int32_t val;
  161. (void)__raw_readl(&(davinci_emif_regs->nandfecc[
  162. CONFIG_SYS_NAND_CS - 2]));
  163. val = __raw_readl(&davinci_emif_regs->nandfcr);
  164. val |= DAVINCI_NANDFCR_NAND_ENABLE(CONFIG_SYS_NAND_CS);
  165. val |= DAVINCI_NANDFCR_1BIT_ECC_START(CONFIG_SYS_NAND_CS);
  166. __raw_writel(val, &davinci_emif_regs->nandfcr);
  167. }
  168. static u_int32_t nand_davinci_readecc(struct mtd_info *mtd, u_int32_t region)
  169. {
  170. u_int32_t ecc = 0;
  171. ecc = __raw_readl(&(davinci_emif_regs->nandfecc[region - 1]));
  172. return ecc;
  173. }
  174. static int nand_davinci_calculate_ecc(struct mtd_info *mtd, const u_char *dat,
  175. u_char *ecc_code)
  176. {
  177. u_int32_t tmp;
  178. const int region = 1;
  179. tmp = nand_davinci_readecc(mtd, region);
  180. /* Squeeze 4 bytes ECC into 3 bytes by removing RESERVED bits
  181. * and shifting. RESERVED bits are 31 to 28 and 15 to 12. */
  182. tmp = (tmp & 0x00000fff) | ((tmp & 0x0fff0000) >> 4);
  183. /* Invert so that erased block ECC is correct */
  184. tmp = ~tmp;
  185. *ecc_code++ = tmp;
  186. *ecc_code++ = tmp >> 8;
  187. *ecc_code++ = tmp >> 16;
  188. /* NOTE: the above code matches mainline Linux:
  189. * .PQR.stu ==> ~PQRstu
  190. *
  191. * MontaVista/TI kernels encode those bytes differently, use
  192. * complicated (and allegedly sometimes-wrong) correction code,
  193. * and usually shipped with U-Boot that uses software ECC:
  194. * .PQR.stu ==> PsQRtu
  195. *
  196. * If you need MV/TI compatible NAND I/O in U-Boot, it should
  197. * be possible to (a) change the mangling above, (b) reverse
  198. * that mangling in nand_davinci_correct_data() below.
  199. */
  200. return 0;
  201. }
  202. static int nand_davinci_correct_data(struct mtd_info *mtd, u_char *dat,
  203. u_char *read_ecc, u_char *calc_ecc)
  204. {
  205. struct nand_chip *this = mtd->priv;
  206. u_int32_t ecc_nand = read_ecc[0] | (read_ecc[1] << 8) |
  207. (read_ecc[2] << 16);
  208. u_int32_t ecc_calc = calc_ecc[0] | (calc_ecc[1] << 8) |
  209. (calc_ecc[2] << 16);
  210. u_int32_t diff = ecc_calc ^ ecc_nand;
  211. if (diff) {
  212. if ((((diff >> 12) ^ diff) & 0xfff) == 0xfff) {
  213. /* Correctable error */
  214. if ((diff >> (12 + 3)) < this->ecc.size) {
  215. uint8_t find_bit = 1 << ((diff >> 12) & 7);
  216. uint32_t find_byte = diff >> (12 + 3);
  217. dat[find_byte] ^= find_bit;
  218. MTDDEBUG(MTD_DEBUG_LEVEL0, "Correcting single "
  219. "bit ECC error at offset: %d, bit: "
  220. "%d\n", find_byte, find_bit);
  221. return 1;
  222. } else {
  223. return -1;
  224. }
  225. } else if (!(diff & (diff - 1))) {
  226. /* Single bit ECC error in the ECC itself,
  227. nothing to fix */
  228. MTDDEBUG(MTD_DEBUG_LEVEL0, "Single bit ECC error in "
  229. "ECC.\n");
  230. return 1;
  231. } else {
  232. /* Uncorrectable error */
  233. MTDDEBUG(MTD_DEBUG_LEVEL0, "ECC UNCORRECTED_ERROR 1\n");
  234. return -1;
  235. }
  236. }
  237. return 0;
  238. }
  239. #endif /* CONFIG_SYS_NAND_HW_ECC */
  240. #ifdef CONFIG_SYS_NAND_4BIT_HW_ECC_OOBFIRST
  241. static struct nand_ecclayout nand_davinci_4bit_layout_oobfirst = {
  242. #if defined(CONFIG_SYS_NAND_PAGE_2K)
  243. .eccbytes = 40,
  244. .eccpos = {
  245. 24, 25, 26, 27, 28,
  246. 29, 30, 31, 32, 33, 34, 35, 36, 37, 38,
  247. 39, 40, 41, 42, 43, 44, 45, 46, 47, 48,
  248. 49, 50, 51, 52, 53, 54, 55, 56, 57, 58,
  249. 59, 60, 61, 62, 63,
  250. },
  251. .oobfree = {
  252. {.offset = 2, .length = 22, },
  253. },
  254. #elif defined(CONFIG_SYS_NAND_PAGE_4K)
  255. .eccbytes = 80,
  256. .eccpos = {
  257. 48, 49, 50, 51, 52, 53, 54, 55, 56, 57,
  258. 58, 59, 60, 61, 62, 63, 64, 65, 66, 67,
  259. 68, 69, 70, 71, 72, 73, 74, 75, 76, 77,
  260. 78, 79, 80, 81, 82, 83, 84, 85, 86, 87,
  261. 88, 89, 90, 91, 92, 93, 94, 95, 96, 97,
  262. 98, 99, 100, 101, 102, 103, 104, 105, 106, 107,
  263. 108, 109, 110, 111, 112, 113, 114, 115, 116, 117,
  264. 118, 119, 120, 121, 122, 123, 124, 125, 126, 127,
  265. },
  266. .oobfree = {
  267. {.offset = 2, .length = 46, },
  268. },
  269. #endif
  270. };
  271. static void nand_davinci_4bit_enable_hwecc(struct mtd_info *mtd, int mode)
  272. {
  273. u32 val;
  274. switch (mode) {
  275. case NAND_ECC_WRITE:
  276. case NAND_ECC_READ:
  277. /*
  278. * Start a new ECC calculation for reading or writing 512 bytes
  279. * of data.
  280. */
  281. val = __raw_readl(&davinci_emif_regs->nandfcr);
  282. val &= ~DAVINCI_NANDFCR_4BIT_ECC_SEL_MASK;
  283. val |= DAVINCI_NANDFCR_NAND_ENABLE(CONFIG_SYS_NAND_CS);
  284. val |= DAVINCI_NANDFCR_4BIT_ECC_SEL(CONFIG_SYS_NAND_CS);
  285. val |= DAVINCI_NANDFCR_4BIT_ECC_START;
  286. __raw_writel(val, &davinci_emif_regs->nandfcr);
  287. break;
  288. case NAND_ECC_READSYN:
  289. val = __raw_readl(&davinci_emif_regs->nand4bitecc[0]);
  290. break;
  291. default:
  292. break;
  293. }
  294. }
  295. static u32 nand_davinci_4bit_readecc(struct mtd_info *mtd, unsigned int ecc[4])
  296. {
  297. int i;
  298. for (i = 0; i < 4; i++) {
  299. ecc[i] = __raw_readl(&davinci_emif_regs->nand4bitecc[i]) &
  300. NAND_4BITECC_MASK;
  301. }
  302. return 0;
  303. }
  304. static int nand_davinci_4bit_calculate_ecc(struct mtd_info *mtd,
  305. const uint8_t *dat,
  306. uint8_t *ecc_code)
  307. {
  308. unsigned int hw_4ecc[4];
  309. unsigned int i;
  310. nand_davinci_4bit_readecc(mtd, hw_4ecc);
  311. /*Convert 10 bit ecc value to 8 bit */
  312. for (i = 0; i < 2; i++) {
  313. unsigned int hw_ecc_low = hw_4ecc[i * 2];
  314. unsigned int hw_ecc_hi = hw_4ecc[(i * 2) + 1];
  315. /* Take first 8 bits from val1 (count1=0) or val5 (count1=1) */
  316. *ecc_code++ = hw_ecc_low & 0xFF;
  317. /*
  318. * Take 2 bits as LSB bits from val1 (count1=0) or val5
  319. * (count1=1) and 6 bits from val2 (count1=0) or
  320. * val5 (count1=1)
  321. */
  322. *ecc_code++ =
  323. ((hw_ecc_low >> 8) & 0x3) | ((hw_ecc_low >> 14) & 0xFC);
  324. /*
  325. * Take 4 bits from val2 (count1=0) or val5 (count1=1) and
  326. * 4 bits from val3 (count1=0) or val6 (count1=1)
  327. */
  328. *ecc_code++ =
  329. ((hw_ecc_low >> 22) & 0xF) | ((hw_ecc_hi << 4) & 0xF0);
  330. /*
  331. * Take 6 bits from val3(count1=0) or val6 (count1=1) and
  332. * 2 bits from val4 (count1=0) or val7 (count1=1)
  333. */
  334. *ecc_code++ =
  335. ((hw_ecc_hi >> 4) & 0x3F) | ((hw_ecc_hi >> 10) & 0xC0);
  336. /* Take 8 bits from val4 (count1=0) or val7 (count1=1) */
  337. *ecc_code++ = (hw_ecc_hi >> 18) & 0xFF;
  338. }
  339. return 0;
  340. }
  341. static int nand_davinci_4bit_correct_data(struct mtd_info *mtd, uint8_t *dat,
  342. uint8_t *read_ecc, uint8_t *calc_ecc)
  343. {
  344. int i;
  345. unsigned int hw_4ecc[4];
  346. unsigned int iserror;
  347. unsigned short *ecc16;
  348. unsigned int numerrors, erroraddress, errorvalue;
  349. u32 val;
  350. /*
  351. * Check for an ECC where all bytes are 0xFF. If this is the case, we
  352. * will assume we are looking at an erased page and we should ignore
  353. * the ECC.
  354. */
  355. for (i = 0; i < 10; i++) {
  356. if (read_ecc[i] != 0xFF)
  357. break;
  358. }
  359. if (i == 10)
  360. return 0;
  361. /* Convert 8 bit in to 10 bit */
  362. ecc16 = (unsigned short *)&read_ecc[0];
  363. /*
  364. * Write the parity values in the NAND Flash 4-bit ECC Load register.
  365. * Write each parity value one at a time starting from 4bit_ecc_val8
  366. * to 4bit_ecc_val1.
  367. */
  368. /*Take 2 bits from 8th byte and 8 bits from 9th byte */
  369. __raw_writel(((ecc16[4]) >> 6) & 0x3FF,
  370. &davinci_emif_regs->nand4biteccload);
  371. /* Take 4 bits from 7th byte and 6 bits from 8th byte */
  372. __raw_writel((((ecc16[3]) >> 12) & 0xF) | ((((ecc16[4])) << 4) & 0x3F0),
  373. &davinci_emif_regs->nand4biteccload);
  374. /* Take 6 bits from 6th byte and 4 bits from 7th byte */
  375. __raw_writel((ecc16[3] >> 2) & 0x3FF,
  376. &davinci_emif_regs->nand4biteccload);
  377. /* Take 8 bits from 5th byte and 2 bits from 6th byte */
  378. __raw_writel(((ecc16[2]) >> 8) | ((((ecc16[3])) << 8) & 0x300),
  379. &davinci_emif_regs->nand4biteccload);
  380. /*Take 2 bits from 3rd byte and 8 bits from 4th byte */
  381. __raw_writel((((ecc16[1]) >> 14) & 0x3) | ((((ecc16[2])) << 2) & 0x3FC),
  382. &davinci_emif_regs->nand4biteccload);
  383. /* Take 4 bits form 2nd bytes and 6 bits from 3rd bytes */
  384. __raw_writel(((ecc16[1]) >> 4) & 0x3FF,
  385. &davinci_emif_regs->nand4biteccload);
  386. /* Take 6 bits from 1st byte and 4 bits from 2nd byte */
  387. __raw_writel((((ecc16[0]) >> 10) & 0x3F) | (((ecc16[1]) << 6) & 0x3C0),
  388. &davinci_emif_regs->nand4biteccload);
  389. /* Take 10 bits from 0th and 1st bytes */
  390. __raw_writel((ecc16[0]) & 0x3FF,
  391. &davinci_emif_regs->nand4biteccload);
  392. /*
  393. * Perform a dummy read to the EMIF Revision Code and Status register.
  394. * This is required to ensure time for syndrome calculation after
  395. * writing the ECC values in previous step.
  396. */
  397. val = __raw_readl(&davinci_emif_regs->nandfsr);
  398. /*
  399. * Read the syndrome from the NAND Flash 4-Bit ECC 1-4 registers.
  400. * A syndrome value of 0 means no bit errors. If the syndrome is
  401. * non-zero then go further otherwise return.
  402. */
  403. nand_davinci_4bit_readecc(mtd, hw_4ecc);
  404. if (!(hw_4ecc[0] | hw_4ecc[1] | hw_4ecc[2] | hw_4ecc[3]))
  405. return 0;
  406. /*
  407. * Clear any previous address calculation by doing a dummy read of an
  408. * error address register.
  409. */
  410. val = __raw_readl(&davinci_emif_regs->nanderradd1);
  411. /*
  412. * Set the addr_calc_st bit(bit no 13) in the NAND Flash Control
  413. * register to 1.
  414. */
  415. __raw_writel(1 << 13, &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 equal to 0x0, 0x1, 0x2, or 0x3.
  419. */
  420. i = NAND_TIMEOUT;
  421. do {
  422. val = __raw_readl(&davinci_emif_regs->nandfsr);
  423. val &= 0xc00;
  424. i--;
  425. } while ((i > 0) && val);
  426. iserror = __raw_readl(&davinci_emif_regs->nandfsr);
  427. iserror &= EMIF_NANDFSR_ECC_STATE_MASK;
  428. iserror = iserror >> 8;
  429. /*
  430. * ECC_STATE_TOO_MANY_ERRS (0x1) means errors cannot be
  431. * corrected (five or more errors). The number of errors
  432. * calculated (err_num field) differs from the number of errors
  433. * searched. ECC_STATE_ERR_CORR_COMP_P (0x2) means error
  434. * correction complete (errors on bit 8 or 9).
  435. * ECC_STATE_ERR_CORR_COMP_N (0x3) means error correction
  436. * complete (error exists).
  437. */
  438. if (iserror == ECC_STATE_NO_ERR) {
  439. val = __raw_readl(&davinci_emif_regs->nanderrval1);
  440. return 0;
  441. } else if (iserror == ECC_STATE_TOO_MANY_ERRS) {
  442. val = __raw_readl(&davinci_emif_regs->nanderrval1);
  443. return -1;
  444. }
  445. numerrors = ((__raw_readl(&davinci_emif_regs->nandfsr) >> 16)
  446. & 0x3) + 1;
  447. /* Read the error address, error value and correct */
  448. for (i = 0; i < numerrors; i++) {
  449. if (i > 1) {
  450. erroraddress =
  451. ((__raw_readl(&davinci_emif_regs->nanderradd2) >>
  452. (16 * (i & 1))) & 0x3FF);
  453. erroraddress = ((512 + 7) - erroraddress);
  454. errorvalue =
  455. ((__raw_readl(&davinci_emif_regs->nanderrval2) >>
  456. (16 * (i & 1))) & 0xFF);
  457. } else {
  458. erroraddress =
  459. ((__raw_readl(&davinci_emif_regs->nanderradd1) >>
  460. (16 * (i & 1))) & 0x3FF);
  461. erroraddress = ((512 + 7) - erroraddress);
  462. errorvalue =
  463. ((__raw_readl(&davinci_emif_regs->nanderrval1) >>
  464. (16 * (i & 1))) & 0xFF);
  465. }
  466. /* xor the corrupt data with error value */
  467. if (erroraddress < 512)
  468. dat[erroraddress] ^= errorvalue;
  469. }
  470. return numerrors;
  471. }
  472. #endif /* CONFIG_SYS_NAND_4BIT_HW_ECC_OOBFIRST */
  473. static int nand_davinci_dev_ready(struct mtd_info *mtd)
  474. {
  475. return __raw_readl(&davinci_emif_regs->nandfsr) & 0x1;
  476. }
  477. static void nand_flash_init(void)
  478. {
  479. /* This is for DM6446 EVM and *very* similar. DO NOT GROW THIS!
  480. * Instead, have your board_init() set EMIF timings, based on its
  481. * knowledge of the clocks and what devices are hooked up ... and
  482. * don't even do that unless no UBL handled it.
  483. */
  484. #ifdef CONFIG_SOC_DM644X
  485. u_int32_t acfg1 = 0x3ffffffc;
  486. /*------------------------------------------------------------------*
  487. * NAND FLASH CHIP TIMEOUT @ 459 MHz *
  488. * *
  489. * AEMIF.CLK freq = PLL1/6 = 459/6 = 76.5 MHz *
  490. * AEMIF.CLK period = 1/76.5 MHz = 13.1 ns *
  491. * *
  492. *------------------------------------------------------------------*/
  493. acfg1 = 0
  494. | (0 << 31) /* selectStrobe */
  495. | (0 << 30) /* extWait */
  496. | (1 << 26) /* writeSetup 10 ns */
  497. | (3 << 20) /* writeStrobe 40 ns */
  498. | (1 << 17) /* writeHold 10 ns */
  499. | (1 << 13) /* readSetup 10 ns */
  500. | (5 << 7) /* readStrobe 60 ns */
  501. | (1 << 4) /* readHold 10 ns */
  502. | (3 << 2) /* turnAround ?? ns */
  503. | (0 << 0) /* asyncSize 8-bit bus */
  504. ;
  505. __raw_writel(acfg1, &davinci_emif_regs->ab1cr); /* CS2 */
  506. /* NAND flash on CS2 */
  507. __raw_writel(0x00000101, &davinci_emif_regs->nandfcr);
  508. #endif
  509. }
  510. void davinci_nand_init(struct nand_chip *nand)
  511. {
  512. nand->chip_delay = 0;
  513. #ifdef CONFIG_SYS_NAND_USE_FLASH_BBT
  514. nand->options |= NAND_USE_FLASH_BBT;
  515. #endif
  516. #ifdef CONFIG_SYS_NAND_HW_ECC
  517. nand->ecc.mode = NAND_ECC_HW;
  518. nand->ecc.size = 512;
  519. nand->ecc.bytes = 3;
  520. nand->ecc.calculate = nand_davinci_calculate_ecc;
  521. nand->ecc.correct = nand_davinci_correct_data;
  522. nand->ecc.hwctl = nand_davinci_enable_hwecc;
  523. #else
  524. nand->ecc.mode = NAND_ECC_SOFT;
  525. #endif /* CONFIG_SYS_NAND_HW_ECC */
  526. #ifdef CONFIG_SYS_NAND_4BIT_HW_ECC_OOBFIRST
  527. nand->ecc.mode = NAND_ECC_HW_OOB_FIRST;
  528. nand->ecc.size = 512;
  529. nand->ecc.bytes = 10;
  530. nand->ecc.calculate = nand_davinci_4bit_calculate_ecc;
  531. nand->ecc.correct = nand_davinci_4bit_correct_data;
  532. nand->ecc.hwctl = nand_davinci_4bit_enable_hwecc;
  533. nand->ecc.layout = &nand_davinci_4bit_layout_oobfirst;
  534. #endif
  535. /* Set address of hardware control function */
  536. nand->cmd_ctrl = nand_davinci_hwcontrol;
  537. nand->read_buf = nand_davinci_read_buf;
  538. nand->write_buf = nand_davinci_write_buf;
  539. nand->dev_ready = nand_davinci_dev_ready;
  540. nand_flash_init();
  541. }
  542. int board_nand_init(struct nand_chip *chip) __attribute__((weak));
  543. int board_nand_init(struct nand_chip *chip)
  544. {
  545. davinci_nand_init(chip);
  546. return 0;
  547. }