fsmc_nand.c 12 KB

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