fsmc_nand.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489
  1. /*
  2. * (C) Copyright 2010
  3. * Vipin Kumar, ST Microelectronics, vipin.kumar@st.com.
  4. *
  5. * (C) Copyright 2012
  6. * Amit Virdi, ST Microelectronics, amit.virdi@st.com.
  7. *
  8. * See file CREDITS for list of people who contributed to this
  9. * project.
  10. *
  11. * This program is free software; you can redistribute it and/or
  12. * modify it under the terms of the GNU General Public License as
  13. * published by the Free Software Foundation; either version 2 of
  14. * the License, or (at your option) any later version.
  15. *
  16. * This program is distributed in the hope that it will be useful,
  17. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. * GNU General Public License for more details.
  20. *
  21. * You should have received a copy of the GNU General Public License
  22. * along with this program; if not, write to the Free Software
  23. * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
  24. * MA 02111-1307 USA
  25. */
  26. #include <common.h>
  27. #include <nand.h>
  28. #include <asm/io.h>
  29. #include <linux/bitops.h>
  30. #include <linux/err.h>
  31. #include <linux/mtd/nand_ecc.h>
  32. #include <linux/mtd/fsmc_nand.h>
  33. #include <asm/arch/hardware.h>
  34. static u32 fsmc_version;
  35. static struct fsmc_regs *const fsmc_regs_p = (struct fsmc_regs *)
  36. CONFIG_SYS_FSMC_BASE;
  37. /*
  38. * ECC4 and ECC1 have 13 bytes and 3 bytes of ecc respectively for 512 bytes of
  39. * data. ECC4 can correct up to 8 bits in 512 bytes of data while ECC1 can
  40. * correct 1 bit in 512 bytes
  41. */
  42. static struct nand_ecclayout fsmc_ecc4_lp_layout = {
  43. .eccbytes = 104,
  44. .eccpos = { 2, 3, 4, 5, 6, 7, 8,
  45. 9, 10, 11, 12, 13, 14,
  46. 18, 19, 20, 21, 22, 23, 24,
  47. 25, 26, 27, 28, 29, 30,
  48. 34, 35, 36, 37, 38, 39, 40,
  49. 41, 42, 43, 44, 45, 46,
  50. 50, 51, 52, 53, 54, 55, 56,
  51. 57, 58, 59, 60, 61, 62,
  52. 66, 67, 68, 69, 70, 71, 72,
  53. 73, 74, 75, 76, 77, 78,
  54. 82, 83, 84, 85, 86, 87, 88,
  55. 89, 90, 91, 92, 93, 94,
  56. 98, 99, 100, 101, 102, 103, 104,
  57. 105, 106, 107, 108, 109, 110,
  58. 114, 115, 116, 117, 118, 119, 120,
  59. 121, 122, 123, 124, 125, 126
  60. },
  61. .oobfree = {
  62. {.offset = 15, .length = 3},
  63. {.offset = 31, .length = 3},
  64. {.offset = 47, .length = 3},
  65. {.offset = 63, .length = 3},
  66. {.offset = 79, .length = 3},
  67. {.offset = 95, .length = 3},
  68. {.offset = 111, .length = 3},
  69. {.offset = 127, .length = 1}
  70. }
  71. };
  72. /*
  73. * ECC4 layout for NAND of pagesize 4096 bytes & OOBsize 224 bytes. 13*8 bytes
  74. * of OOB size is reserved for ECC, Byte no. 0 & 1 reserved for bad block & 118
  75. * bytes are free for use.
  76. */
  77. static struct nand_ecclayout fsmc_ecc4_224_layout = {
  78. .eccbytes = 104,
  79. .eccpos = { 2, 3, 4, 5, 6, 7, 8,
  80. 9, 10, 11, 12, 13, 14,
  81. 18, 19, 20, 21, 22, 23, 24,
  82. 25, 26, 27, 28, 29, 30,
  83. 34, 35, 36, 37, 38, 39, 40,
  84. 41, 42, 43, 44, 45, 46,
  85. 50, 51, 52, 53, 54, 55, 56,
  86. 57, 58, 59, 60, 61, 62,
  87. 66, 67, 68, 69, 70, 71, 72,
  88. 73, 74, 75, 76, 77, 78,
  89. 82, 83, 84, 85, 86, 87, 88,
  90. 89, 90, 91, 92, 93, 94,
  91. 98, 99, 100, 101, 102, 103, 104,
  92. 105, 106, 107, 108, 109, 110,
  93. 114, 115, 116, 117, 118, 119, 120,
  94. 121, 122, 123, 124, 125, 126
  95. },
  96. .oobfree = {
  97. {.offset = 15, .length = 3},
  98. {.offset = 31, .length = 3},
  99. {.offset = 47, .length = 3},
  100. {.offset = 63, .length = 3},
  101. {.offset = 79, .length = 3},
  102. {.offset = 95, .length = 3},
  103. {.offset = 111, .length = 3},
  104. {.offset = 127, .length = 97}
  105. }
  106. };
  107. /*
  108. * ECC placement definitions in oobfree type format
  109. * There are 13 bytes of ecc for every 512 byte block and it has to be read
  110. * consecutively and immediately after the 512 byte data block for hardware to
  111. * generate the error bit offsets in 512 byte data
  112. * Managing the ecc bytes in the following way makes it easier for software to
  113. * read ecc bytes consecutive to data bytes. This way is similar to
  114. * oobfree structure maintained already in u-boot nand driver
  115. */
  116. static struct fsmc_eccplace fsmc_eccpl_lp = {
  117. .eccplace = {
  118. {.offset = 2, .length = 13},
  119. {.offset = 18, .length = 13},
  120. {.offset = 34, .length = 13},
  121. {.offset = 50, .length = 13},
  122. {.offset = 66, .length = 13},
  123. {.offset = 82, .length = 13},
  124. {.offset = 98, .length = 13},
  125. {.offset = 114, .length = 13}
  126. }
  127. };
  128. static struct nand_ecclayout fsmc_ecc4_sp_layout = {
  129. .eccbytes = 13,
  130. .eccpos = { 0, 1, 2, 3, 6, 7, 8,
  131. 9, 10, 11, 12, 13, 14
  132. },
  133. .oobfree = {
  134. {.offset = 15, .length = 1},
  135. }
  136. };
  137. static struct fsmc_eccplace fsmc_eccpl_sp = {
  138. .eccplace = {
  139. {.offset = 0, .length = 4},
  140. {.offset = 6, .length = 9}
  141. }
  142. };
  143. static struct nand_ecclayout fsmc_ecc1_layout = {
  144. .eccbytes = 24,
  145. .eccpos = {2, 3, 4, 18, 19, 20, 34, 35, 36, 50, 51, 52,
  146. 66, 67, 68, 82, 83, 84, 98, 99, 100, 114, 115, 116},
  147. .oobfree = {
  148. {.offset = 8, .length = 8},
  149. {.offset = 24, .length = 8},
  150. {.offset = 40, .length = 8},
  151. {.offset = 56, .length = 8},
  152. {.offset = 72, .length = 8},
  153. {.offset = 88, .length = 8},
  154. {.offset = 104, .length = 8},
  155. {.offset = 120, .length = 8}
  156. }
  157. };
  158. /* Count the number of 0's in buff upto a max of max_bits */
  159. static int count_written_bits(uint8_t *buff, int size, int max_bits)
  160. {
  161. int k, written_bits = 0;
  162. for (k = 0; k < size; k++) {
  163. written_bits += hweight8(~buff[k]);
  164. if (written_bits > max_bits)
  165. break;
  166. }
  167. return written_bits;
  168. }
  169. static void fsmc_nand_hwcontrol(struct mtd_info *mtd, int cmd, uint ctrl)
  170. {
  171. struct nand_chip *this = mtd->priv;
  172. ulong IO_ADDR_W;
  173. if (ctrl & NAND_CTRL_CHANGE) {
  174. IO_ADDR_W = (ulong)this->IO_ADDR_W;
  175. IO_ADDR_W &= ~(CONFIG_SYS_NAND_CLE | CONFIG_SYS_NAND_ALE);
  176. if (ctrl & NAND_CLE)
  177. IO_ADDR_W |= CONFIG_SYS_NAND_CLE;
  178. if (ctrl & NAND_ALE)
  179. IO_ADDR_W |= CONFIG_SYS_NAND_ALE;
  180. if (ctrl & NAND_NCE) {
  181. writel(readl(&fsmc_regs_p->pc) |
  182. FSMC_ENABLE, &fsmc_regs_p->pc);
  183. } else {
  184. writel(readl(&fsmc_regs_p->pc) &
  185. ~FSMC_ENABLE, &fsmc_regs_p->pc);
  186. }
  187. this->IO_ADDR_W = (void *)IO_ADDR_W;
  188. }
  189. if (cmd != NAND_CMD_NONE)
  190. writeb(cmd, this->IO_ADDR_W);
  191. }
  192. static int fsmc_bch8_correct_data(struct mtd_info *mtd, u_char *dat,
  193. u_char *read_ecc, u_char *calc_ecc)
  194. {
  195. /* The calculated ecc is actually the correction index in data */
  196. u32 err_idx[8];
  197. u32 num_err, i;
  198. u32 ecc1, ecc2, ecc3, ecc4;
  199. num_err = (readl(&fsmc_regs_p->sts) >> 10) & 0xF;
  200. if (likely(num_err == 0))
  201. return 0;
  202. if (unlikely(num_err > 8)) {
  203. /*
  204. * This is a temporary erase check. A newly erased page read
  205. * would result in an ecc error because the oob data is also
  206. * erased to FF and the calculated ecc for an FF data is not
  207. * FF..FF.
  208. * This is a workaround to skip performing correction in case
  209. * data is FF..FF
  210. *
  211. * Logic:
  212. * For every page, each bit written as 0 is counted until these
  213. * number of bits are greater than 8 (the maximum correction
  214. * capability of FSMC for each 512 + 13 bytes)
  215. */
  216. int bits_ecc = count_written_bits(read_ecc, 13, 8);
  217. int bits_data = count_written_bits(dat, 512, 8);
  218. if ((bits_ecc + bits_data) <= 8) {
  219. if (bits_data)
  220. memset(dat, 0xff, 512);
  221. return bits_data + bits_ecc;
  222. }
  223. return -EBADMSG;
  224. }
  225. ecc1 = readl(&fsmc_regs_p->ecc1);
  226. ecc2 = readl(&fsmc_regs_p->ecc2);
  227. ecc3 = readl(&fsmc_regs_p->ecc3);
  228. ecc4 = readl(&fsmc_regs_p->sts);
  229. err_idx[0] = (ecc1 >> 0) & 0x1FFF;
  230. err_idx[1] = (ecc1 >> 13) & 0x1FFF;
  231. err_idx[2] = (((ecc2 >> 0) & 0x7F) << 6) | ((ecc1 >> 26) & 0x3F);
  232. err_idx[3] = (ecc2 >> 7) & 0x1FFF;
  233. err_idx[4] = (((ecc3 >> 0) & 0x1) << 12) | ((ecc2 >> 20) & 0xFFF);
  234. err_idx[5] = (ecc3 >> 1) & 0x1FFF;
  235. err_idx[6] = (ecc3 >> 14) & 0x1FFF;
  236. err_idx[7] = (((ecc4 >> 16) & 0xFF) << 5) | ((ecc3 >> 27) & 0x1F);
  237. i = 0;
  238. while (i < num_err) {
  239. err_idx[i] ^= 3;
  240. if (err_idx[i] < 512 * 8)
  241. __change_bit(err_idx[i], dat);
  242. i++;
  243. }
  244. return num_err;
  245. }
  246. static int fsmc_read_hwecc(struct mtd_info *mtd,
  247. const u_char *data, u_char *ecc)
  248. {
  249. u_int ecc_tmp;
  250. int timeout = CONFIG_SYS_HZ;
  251. ulong start;
  252. switch (fsmc_version) {
  253. case FSMC_VER8:
  254. start = get_timer(0);
  255. while (get_timer(start) < timeout) {
  256. /*
  257. * Busy waiting for ecc computation
  258. * to finish for 512 bytes
  259. */
  260. if (readl(&fsmc_regs_p->sts) & FSMC_CODE_RDY)
  261. break;
  262. }
  263. ecc_tmp = readl(&fsmc_regs_p->ecc1);
  264. ecc[0] = (u_char) (ecc_tmp >> 0);
  265. ecc[1] = (u_char) (ecc_tmp >> 8);
  266. ecc[2] = (u_char) (ecc_tmp >> 16);
  267. ecc[3] = (u_char) (ecc_tmp >> 24);
  268. ecc_tmp = readl(&fsmc_regs_p->ecc2);
  269. ecc[4] = (u_char) (ecc_tmp >> 0);
  270. ecc[5] = (u_char) (ecc_tmp >> 8);
  271. ecc[6] = (u_char) (ecc_tmp >> 16);
  272. ecc[7] = (u_char) (ecc_tmp >> 24);
  273. ecc_tmp = readl(&fsmc_regs_p->ecc3);
  274. ecc[8] = (u_char) (ecc_tmp >> 0);
  275. ecc[9] = (u_char) (ecc_tmp >> 8);
  276. ecc[10] = (u_char) (ecc_tmp >> 16);
  277. ecc[11] = (u_char) (ecc_tmp >> 24);
  278. ecc_tmp = readl(&fsmc_regs_p->sts);
  279. ecc[12] = (u_char) (ecc_tmp >> 16);
  280. break;
  281. default:
  282. ecc_tmp = readl(&fsmc_regs_p->ecc1);
  283. ecc[0] = (u_char) (ecc_tmp >> 0);
  284. ecc[1] = (u_char) (ecc_tmp >> 8);
  285. ecc[2] = (u_char) (ecc_tmp >> 16);
  286. break;
  287. }
  288. return 0;
  289. }
  290. void fsmc_enable_hwecc(struct mtd_info *mtd, int mode)
  291. {
  292. writel(readl(&fsmc_regs_p->pc) & ~FSMC_ECCPLEN_256,
  293. &fsmc_regs_p->pc);
  294. writel(readl(&fsmc_regs_p->pc) & ~FSMC_ECCEN,
  295. &fsmc_regs_p->pc);
  296. writel(readl(&fsmc_regs_p->pc) | FSMC_ECCEN,
  297. &fsmc_regs_p->pc);
  298. }
  299. /*
  300. * fsmc_read_page_hwecc
  301. * @mtd: mtd info structure
  302. * @chip: nand chip info structure
  303. * @buf: buffer to store read data
  304. * @oob_required: caller expects OOB data read to chip->oob_poi
  305. * @page: page number to read
  306. *
  307. * This routine is needed for fsmc verison 8 as reading from NAND chip has to be
  308. * performed in a strict sequence as follows:
  309. * data(512 byte) -> ecc(13 byte)
  310. * After this read, fsmc hardware generates and reports error data bits(upto a
  311. * max of 8 bits)
  312. */
  313. static int fsmc_read_page_hwecc(struct mtd_info *mtd, struct nand_chip *chip,
  314. uint8_t *buf, int oob_required, int page)
  315. {
  316. struct fsmc_eccplace *fsmc_eccpl;
  317. int i, j, s, stat, eccsize = chip->ecc.size;
  318. int eccbytes = chip->ecc.bytes;
  319. int eccsteps = chip->ecc.steps;
  320. uint8_t *p = buf;
  321. uint8_t *ecc_calc = chip->buffers->ecccalc;
  322. uint8_t *ecc_code = chip->buffers->ecccode;
  323. int off, len, group = 0;
  324. uint8_t oob[13] __attribute__ ((aligned (2)));
  325. /* Differentiate between small and large page ecc place definitions */
  326. if (mtd->writesize == 512)
  327. fsmc_eccpl = &fsmc_eccpl_sp;
  328. else
  329. fsmc_eccpl = &fsmc_eccpl_lp;
  330. for (i = 0, s = 0; s < eccsteps; s++, i += eccbytes, p += eccsize) {
  331. chip->cmdfunc(mtd, NAND_CMD_READ0, s * eccsize, page);
  332. chip->ecc.hwctl(mtd, NAND_ECC_READ);
  333. chip->read_buf(mtd, p, eccsize);
  334. for (j = 0; j < eccbytes;) {
  335. off = fsmc_eccpl->eccplace[group].offset;
  336. len = fsmc_eccpl->eccplace[group].length;
  337. group++;
  338. /*
  339. * length is intentionally kept a higher multiple of 2
  340. * to read at least 13 bytes even in case of 16 bit NAND
  341. * devices
  342. */
  343. if (chip->options & NAND_BUSWIDTH_16)
  344. len = roundup(len, 2);
  345. chip->cmdfunc(mtd, NAND_CMD_READOOB, off, page);
  346. chip->read_buf(mtd, oob + j, len);
  347. j += len;
  348. }
  349. memcpy(&ecc_code[i], oob, 13);
  350. chip->ecc.calculate(mtd, p, &ecc_calc[i]);
  351. stat = chip->ecc.correct(mtd, p, &ecc_code[i],
  352. &ecc_calc[i]);
  353. if (stat < 0)
  354. mtd->ecc_stats.failed++;
  355. else
  356. mtd->ecc_stats.corrected += stat;
  357. }
  358. return 0;
  359. }
  360. int fsmc_nand_init(struct nand_chip *nand)
  361. {
  362. static int chip_nr;
  363. struct mtd_info *mtd;
  364. int i;
  365. u32 peripid2 = readl(&fsmc_regs_p->peripid2);
  366. fsmc_version = (peripid2 >> FSMC_REVISION_SHFT) &
  367. FSMC_REVISION_MSK;
  368. writel(readl(&fsmc_regs_p->ctrl) | FSMC_WP, &fsmc_regs_p->ctrl);
  369. #if defined(CONFIG_SYS_FSMC_NAND_16BIT)
  370. writel(FSMC_DEVWID_16 | FSMC_DEVTYPE_NAND | FSMC_ENABLE | FSMC_WAITON,
  371. &fsmc_regs_p->pc);
  372. #elif defined(CONFIG_SYS_FSMC_NAND_8BIT)
  373. writel(FSMC_DEVWID_8 | FSMC_DEVTYPE_NAND | FSMC_ENABLE | FSMC_WAITON,
  374. &fsmc_regs_p->pc);
  375. #else
  376. #error Please define CONFIG_SYS_FSMC_NAND_16BIT or CONFIG_SYS_FSMC_NAND_8BIT
  377. #endif
  378. writel(readl(&fsmc_regs_p->pc) | FSMC_TCLR_1 | FSMC_TAR_1,
  379. &fsmc_regs_p->pc);
  380. writel(FSMC_THIZ_1 | FSMC_THOLD_4 | FSMC_TWAIT_6 | FSMC_TSET_0,
  381. &fsmc_regs_p->comm);
  382. writel(FSMC_THIZ_1 | FSMC_THOLD_4 | FSMC_TWAIT_6 | FSMC_TSET_0,
  383. &fsmc_regs_p->attrib);
  384. nand->options = 0;
  385. #if defined(CONFIG_SYS_FSMC_NAND_16BIT)
  386. nand->options |= NAND_BUSWIDTH_16;
  387. #endif
  388. nand->ecc.mode = NAND_ECC_HW;
  389. nand->ecc.size = 512;
  390. nand->ecc.calculate = fsmc_read_hwecc;
  391. nand->ecc.hwctl = fsmc_enable_hwecc;
  392. nand->cmd_ctrl = fsmc_nand_hwcontrol;
  393. nand->IO_ADDR_R = nand->IO_ADDR_W =
  394. (void __iomem *)CONFIG_SYS_NAND_BASE;
  395. nand->badblockbits = 7;
  396. mtd = &nand_info[chip_nr++];
  397. mtd->priv = nand;
  398. switch (fsmc_version) {
  399. case FSMC_VER8:
  400. nand->ecc.bytes = 13;
  401. nand->ecc.strength = 8;
  402. nand->ecc.correct = fsmc_bch8_correct_data;
  403. nand->ecc.read_page = fsmc_read_page_hwecc;
  404. if (mtd->writesize == 512)
  405. nand->ecc.layout = &fsmc_ecc4_sp_layout;
  406. else {
  407. if (mtd->oobsize == 224)
  408. nand->ecc.layout = &fsmc_ecc4_224_layout;
  409. else
  410. nand->ecc.layout = &fsmc_ecc4_lp_layout;
  411. }
  412. break;
  413. default:
  414. nand->ecc.bytes = 3;
  415. nand->ecc.strength = 1;
  416. nand->ecc.layout = &fsmc_ecc1_layout;
  417. nand->ecc.correct = nand_correct_data;
  418. break;
  419. }
  420. /* Detect NAND chips */
  421. if (nand_scan_ident(mtd, CONFIG_SYS_MAX_NAND_DEVICE, NULL))
  422. return -ENXIO;
  423. if (nand_scan_tail(mtd))
  424. return -ENXIO;
  425. for (i = 0; i < CONFIG_SYS_MAX_NAND_DEVICE; i++)
  426. if (nand_register(i))
  427. return -ENXIO;
  428. return 0;
  429. }