omap_gpmc.c 30 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036
  1. /*
  2. * (C) Copyright 2004-2008 Texas Instruments, <www.ti.com>
  3. * Rohit Choraria <rohitkc@ti.com>
  4. *
  5. * SPDX-License-Identifier: GPL-2.0+
  6. */
  7. #include <common.h>
  8. #include <asm/io.h>
  9. #include <asm/errno.h>
  10. #include <asm/arch/mem.h>
  11. #include <linux/mtd/omap_gpmc.h>
  12. #include <linux/mtd/nand_ecc.h>
  13. #include <linux/bch.h>
  14. #include <linux/compiler.h>
  15. #include <nand.h>
  16. #include <linux/mtd/omap_elm.h>
  17. #define BADBLOCK_MARKER_LENGTH 2
  18. #define SECTOR_BYTES 512
  19. #define ECCCLEAR (0x1 << 8)
  20. #define ECCRESULTREG1 (0x1 << 0)
  21. /* 4 bit padding to make byte aligned, 56 = 52 + 4 */
  22. #define BCH4_BIT_PAD 4
  23. #ifdef CONFIG_BCH
  24. static u8 bch8_polynomial[] = {0xef, 0x51, 0x2e, 0x09, 0xed, 0x93, 0x9a, 0xc2,
  25. 0x97, 0x79, 0xe5, 0x24, 0xb5};
  26. #endif
  27. static uint8_t cs_next;
  28. static __maybe_unused struct nand_ecclayout omap_ecclayout;
  29. #if defined(CONFIG_NAND_OMAP_GPMC_WSCFG)
  30. static const int8_t wscfg[CONFIG_SYS_MAX_NAND_DEVICE] =
  31. { CONFIG_NAND_OMAP_GPMC_WSCFG };
  32. #else
  33. /* wscfg is preset to zero since its a static variable */
  34. static const int8_t wscfg[CONFIG_SYS_MAX_NAND_DEVICE];
  35. #endif
  36. /*
  37. * Driver configurations
  38. */
  39. struct omap_nand_info {
  40. struct bch_control *control;
  41. enum omap_ecc ecc_scheme;
  42. uint8_t cs;
  43. uint8_t ws; /* wait status pin (0,1) */
  44. };
  45. /* We are wasting a bit of memory but al least we are safe */
  46. static struct omap_nand_info omap_nand_info[GPMC_MAX_CS];
  47. /*
  48. * omap_nand_hwcontrol - Set the address pointers corretly for the
  49. * following address/data/command operation
  50. */
  51. static void omap_nand_hwcontrol(struct mtd_info *mtd, int32_t cmd,
  52. uint32_t ctrl)
  53. {
  54. register struct nand_chip *this = mtd_to_nand(mtd);
  55. struct omap_nand_info *info = nand_get_controller_data(this);
  56. int cs = info->cs;
  57. /*
  58. * Point the IO_ADDR to DATA and ADDRESS registers instead
  59. * of chip address
  60. */
  61. switch (ctrl) {
  62. case NAND_CTRL_CHANGE | NAND_CTRL_CLE:
  63. this->IO_ADDR_W = (void __iomem *)&gpmc_cfg->cs[cs].nand_cmd;
  64. break;
  65. case NAND_CTRL_CHANGE | NAND_CTRL_ALE:
  66. this->IO_ADDR_W = (void __iomem *)&gpmc_cfg->cs[cs].nand_adr;
  67. break;
  68. case NAND_CTRL_CHANGE | NAND_NCE:
  69. this->IO_ADDR_W = (void __iomem *)&gpmc_cfg->cs[cs].nand_dat;
  70. break;
  71. }
  72. if (cmd != NAND_CMD_NONE)
  73. writeb(cmd, this->IO_ADDR_W);
  74. }
  75. /* Check wait pin as dev ready indicator */
  76. static int omap_dev_ready(struct mtd_info *mtd)
  77. {
  78. register struct nand_chip *this = mtd_to_nand(mtd);
  79. struct omap_nand_info *info = nand_get_controller_data(this);
  80. return gpmc_cfg->status & (1 << (8 + info->ws));
  81. }
  82. /*
  83. * gen_true_ecc - This function will generate true ECC value, which
  84. * can be used when correcting data read from NAND flash memory core
  85. *
  86. * @ecc_buf: buffer to store ecc code
  87. *
  88. * @return: re-formatted ECC value
  89. */
  90. static uint32_t gen_true_ecc(uint8_t *ecc_buf)
  91. {
  92. return ecc_buf[0] | (ecc_buf[1] << 16) | ((ecc_buf[2] & 0xF0) << 20) |
  93. ((ecc_buf[2] & 0x0F) << 8);
  94. }
  95. /*
  96. * omap_correct_data - Compares the ecc read from nand spare area with ECC
  97. * registers values and corrects one bit error if it has occurred
  98. * Further details can be had from OMAP TRM and the following selected links:
  99. * http://en.wikipedia.org/wiki/Hamming_code
  100. * http://www.cs.utexas.edu/users/plaxton/c/337/05f/slides/ErrorCorrection-4.pdf
  101. *
  102. * @mtd: MTD device structure
  103. * @dat: page data
  104. * @read_ecc: ecc read from nand flash
  105. * @calc_ecc: ecc read from ECC registers
  106. *
  107. * @return 0 if data is OK or corrected, else returns -1
  108. */
  109. static int __maybe_unused omap_correct_data(struct mtd_info *mtd, uint8_t *dat,
  110. uint8_t *read_ecc, uint8_t *calc_ecc)
  111. {
  112. uint32_t orig_ecc, new_ecc, res, hm;
  113. uint16_t parity_bits, byte;
  114. uint8_t bit;
  115. /* Regenerate the orginal ECC */
  116. orig_ecc = gen_true_ecc(read_ecc);
  117. new_ecc = gen_true_ecc(calc_ecc);
  118. /* Get the XOR of real ecc */
  119. res = orig_ecc ^ new_ecc;
  120. if (res) {
  121. /* Get the hamming width */
  122. hm = hweight32(res);
  123. /* Single bit errors can be corrected! */
  124. if (hm == 12) {
  125. /* Correctable data! */
  126. parity_bits = res >> 16;
  127. bit = (parity_bits & 0x7);
  128. byte = (parity_bits >> 3) & 0x1FF;
  129. /* Flip the bit to correct */
  130. dat[byte] ^= (0x1 << bit);
  131. } else if (hm == 1) {
  132. printf("Error: Ecc is wrong\n");
  133. /* ECC itself is corrupted */
  134. return 2;
  135. } else {
  136. /*
  137. * hm distance != parity pairs OR one, could mean 2 bit
  138. * error OR potentially be on a blank page..
  139. * orig_ecc: contains spare area data from nand flash.
  140. * new_ecc: generated ecc while reading data area.
  141. * Note: if the ecc = 0, all data bits from which it was
  142. * generated are 0xFF.
  143. * The 3 byte(24 bits) ecc is generated per 512byte
  144. * chunk of a page. If orig_ecc(from spare area)
  145. * is 0xFF && new_ecc(computed now from data area)=0x0,
  146. * this means that data area is 0xFF and spare area is
  147. * 0xFF. A sure sign of a erased page!
  148. */
  149. if ((orig_ecc == 0x0FFF0FFF) && (new_ecc == 0x00000000))
  150. return 0;
  151. printf("Error: Bad compare! failed\n");
  152. /* detected 2 bit error */
  153. return -1;
  154. }
  155. }
  156. return 0;
  157. }
  158. /*
  159. * omap_enable_hwecc - configures GPMC as per ECC scheme before read/write
  160. * @mtd: MTD device structure
  161. * @mode: Read/Write mode
  162. */
  163. __maybe_unused
  164. static void omap_enable_hwecc(struct mtd_info *mtd, int32_t mode)
  165. {
  166. struct nand_chip *nand = mtd_to_nand(mtd);
  167. struct omap_nand_info *info = nand_get_controller_data(nand);
  168. unsigned int dev_width = (nand->options & NAND_BUSWIDTH_16) ? 1 : 0;
  169. unsigned int ecc_algo = 0;
  170. unsigned int bch_type = 0;
  171. unsigned int eccsize1 = 0x00, eccsize0 = 0x00, bch_wrapmode = 0x00;
  172. u32 ecc_size_config_val = 0;
  173. u32 ecc_config_val = 0;
  174. int cs = info->cs;
  175. /* configure GPMC for specific ecc-scheme */
  176. switch (info->ecc_scheme) {
  177. case OMAP_ECC_HAM1_CODE_SW:
  178. return;
  179. case OMAP_ECC_HAM1_CODE_HW:
  180. ecc_algo = 0x0;
  181. bch_type = 0x0;
  182. bch_wrapmode = 0x00;
  183. eccsize0 = 0xFF;
  184. eccsize1 = 0xFF;
  185. break;
  186. case OMAP_ECC_BCH8_CODE_HW_DETECTION_SW:
  187. case OMAP_ECC_BCH8_CODE_HW:
  188. ecc_algo = 0x1;
  189. bch_type = 0x1;
  190. if (mode == NAND_ECC_WRITE) {
  191. bch_wrapmode = 0x01;
  192. eccsize0 = 0; /* extra bits in nibbles per sector */
  193. eccsize1 = 28; /* OOB bits in nibbles per sector */
  194. } else {
  195. bch_wrapmode = 0x01;
  196. eccsize0 = 26; /* ECC bits in nibbles per sector */
  197. eccsize1 = 2; /* non-ECC bits in nibbles per sector */
  198. }
  199. break;
  200. case OMAP_ECC_BCH16_CODE_HW:
  201. ecc_algo = 0x1;
  202. bch_type = 0x2;
  203. if (mode == NAND_ECC_WRITE) {
  204. bch_wrapmode = 0x01;
  205. eccsize0 = 0; /* extra bits in nibbles per sector */
  206. eccsize1 = 52; /* OOB bits in nibbles per sector */
  207. } else {
  208. bch_wrapmode = 0x01;
  209. eccsize0 = 52; /* ECC bits in nibbles per sector */
  210. eccsize1 = 0; /* non-ECC bits in nibbles per sector */
  211. }
  212. break;
  213. default:
  214. return;
  215. }
  216. /* Clear ecc and enable bits */
  217. writel(ECCCLEAR | ECCRESULTREG1, &gpmc_cfg->ecc_control);
  218. /* Configure ecc size for BCH */
  219. ecc_size_config_val = (eccsize1 << 22) | (eccsize0 << 12);
  220. writel(ecc_size_config_val, &gpmc_cfg->ecc_size_config);
  221. /* Configure device details for BCH engine */
  222. ecc_config_val = ((ecc_algo << 16) | /* HAM1 | BCHx */
  223. (bch_type << 12) | /* BCH4/BCH8/BCH16 */
  224. (bch_wrapmode << 8) | /* wrap mode */
  225. (dev_width << 7) | /* bus width */
  226. (0x0 << 4) | /* number of sectors */
  227. (cs << 1) | /* ECC CS */
  228. (0x1)); /* enable ECC */
  229. writel(ecc_config_val, &gpmc_cfg->ecc_config);
  230. }
  231. /*
  232. * omap_calculate_ecc - Read ECC result
  233. * @mtd: MTD structure
  234. * @dat: unused
  235. * @ecc_code: ecc_code buffer
  236. * Using noninverted ECC can be considered ugly since writing a blank
  237. * page ie. padding will clear the ECC bytes. This is no problem as
  238. * long nobody is trying to write data on the seemingly unused page.
  239. * Reading an erased page will produce an ECC mismatch between
  240. * generated and read ECC bytes that has to be dealt with separately.
  241. * E.g. if page is 0xFF (fresh erased), and if HW ECC engine within GPMC
  242. * is used, the result of read will be 0x0 while the ECC offsets of the
  243. * spare area will be 0xFF which will result in an ECC mismatch.
  244. */
  245. static int omap_calculate_ecc(struct mtd_info *mtd, const uint8_t *dat,
  246. uint8_t *ecc_code)
  247. {
  248. struct nand_chip *chip = mtd_to_nand(mtd);
  249. struct omap_nand_info *info = nand_get_controller_data(chip);
  250. uint32_t *ptr, val = 0;
  251. int8_t i = 0, j;
  252. switch (info->ecc_scheme) {
  253. case OMAP_ECC_HAM1_CODE_HW:
  254. val = readl(&gpmc_cfg->ecc1_result);
  255. ecc_code[0] = val & 0xFF;
  256. ecc_code[1] = (val >> 16) & 0xFF;
  257. ecc_code[2] = ((val >> 8) & 0x0F) | ((val >> 20) & 0xF0);
  258. break;
  259. #ifdef CONFIG_BCH
  260. case OMAP_ECC_BCH8_CODE_HW_DETECTION_SW:
  261. #endif
  262. case OMAP_ECC_BCH8_CODE_HW:
  263. ptr = &gpmc_cfg->bch_result_0_3[0].bch_result_x[3];
  264. val = readl(ptr);
  265. ecc_code[i++] = (val >> 0) & 0xFF;
  266. ptr--;
  267. for (j = 0; j < 3; j++) {
  268. val = readl(ptr);
  269. ecc_code[i++] = (val >> 24) & 0xFF;
  270. ecc_code[i++] = (val >> 16) & 0xFF;
  271. ecc_code[i++] = (val >> 8) & 0xFF;
  272. ecc_code[i++] = (val >> 0) & 0xFF;
  273. ptr--;
  274. }
  275. break;
  276. case OMAP_ECC_BCH16_CODE_HW:
  277. val = readl(&gpmc_cfg->bch_result_4_6[0].bch_result_x[2]);
  278. ecc_code[i++] = (val >> 8) & 0xFF;
  279. ecc_code[i++] = (val >> 0) & 0xFF;
  280. val = readl(&gpmc_cfg->bch_result_4_6[0].bch_result_x[1]);
  281. ecc_code[i++] = (val >> 24) & 0xFF;
  282. ecc_code[i++] = (val >> 16) & 0xFF;
  283. ecc_code[i++] = (val >> 8) & 0xFF;
  284. ecc_code[i++] = (val >> 0) & 0xFF;
  285. val = readl(&gpmc_cfg->bch_result_4_6[0].bch_result_x[0]);
  286. ecc_code[i++] = (val >> 24) & 0xFF;
  287. ecc_code[i++] = (val >> 16) & 0xFF;
  288. ecc_code[i++] = (val >> 8) & 0xFF;
  289. ecc_code[i++] = (val >> 0) & 0xFF;
  290. for (j = 3; j >= 0; j--) {
  291. val = readl(&gpmc_cfg->bch_result_0_3[0].bch_result_x[j]
  292. );
  293. ecc_code[i++] = (val >> 24) & 0xFF;
  294. ecc_code[i++] = (val >> 16) & 0xFF;
  295. ecc_code[i++] = (val >> 8) & 0xFF;
  296. ecc_code[i++] = (val >> 0) & 0xFF;
  297. }
  298. break;
  299. default:
  300. return -EINVAL;
  301. }
  302. /* ECC scheme specific syndrome customizations */
  303. switch (info->ecc_scheme) {
  304. case OMAP_ECC_HAM1_CODE_HW:
  305. break;
  306. #ifdef CONFIG_BCH
  307. case OMAP_ECC_BCH8_CODE_HW_DETECTION_SW:
  308. for (i = 0; i < chip->ecc.bytes; i++)
  309. *(ecc_code + i) = *(ecc_code + i) ^
  310. bch8_polynomial[i];
  311. break;
  312. #endif
  313. case OMAP_ECC_BCH8_CODE_HW:
  314. ecc_code[chip->ecc.bytes - 1] = 0x00;
  315. break;
  316. case OMAP_ECC_BCH16_CODE_HW:
  317. break;
  318. default:
  319. return -EINVAL;
  320. }
  321. return 0;
  322. }
  323. #ifdef CONFIG_NAND_OMAP_GPMC_PREFETCH
  324. #define PREFETCH_CONFIG1_CS_SHIFT 24
  325. #define PREFETCH_FIFOTHRESHOLD_MAX 0x40
  326. #define PREFETCH_FIFOTHRESHOLD(val) ((val) << 8)
  327. #define PREFETCH_STATUS_COUNT(val) (val & 0x00003fff)
  328. #define PREFETCH_STATUS_FIFO_CNT(val) ((val >> 24) & 0x7F)
  329. #define ENABLE_PREFETCH (1 << 7)
  330. /**
  331. * omap_prefetch_enable - configures and starts prefetch transfer
  332. * @fifo_th: fifo threshold to be used for read/ write
  333. * @count: number of bytes to be transferred
  334. * @is_write: prefetch read(0) or write post(1) mode
  335. * @cs: chip select to use
  336. */
  337. static int omap_prefetch_enable(int fifo_th, unsigned int count, int is_write, int cs)
  338. {
  339. uint32_t val;
  340. if (fifo_th > PREFETCH_FIFOTHRESHOLD_MAX)
  341. return -EINVAL;
  342. if (readl(&gpmc_cfg->prefetch_control))
  343. return -EBUSY;
  344. /* Set the amount of bytes to be prefetched */
  345. writel(count, &gpmc_cfg->prefetch_config2);
  346. val = (cs << PREFETCH_CONFIG1_CS_SHIFT) | (is_write & 1) |
  347. PREFETCH_FIFOTHRESHOLD(fifo_th) | ENABLE_PREFETCH;
  348. writel(val, &gpmc_cfg->prefetch_config1);
  349. /* Start the prefetch engine */
  350. writel(1, &gpmc_cfg->prefetch_control);
  351. return 0;
  352. }
  353. /**
  354. * omap_prefetch_reset - disables and stops the prefetch engine
  355. */
  356. static void omap_prefetch_reset(void)
  357. {
  358. writel(0, &gpmc_cfg->prefetch_control);
  359. writel(0, &gpmc_cfg->prefetch_config1);
  360. }
  361. static int __read_prefetch_aligned(struct nand_chip *chip, uint32_t *buf, int len)
  362. {
  363. int ret;
  364. uint32_t cnt;
  365. struct omap_nand_info *info = nand_get_controller_data(chip);
  366. ret = omap_prefetch_enable(PREFETCH_FIFOTHRESHOLD_MAX, len, 0, info->cs);
  367. if (ret < 0)
  368. return ret;
  369. do {
  370. int i;
  371. cnt = readl(&gpmc_cfg->prefetch_status);
  372. cnt = PREFETCH_STATUS_FIFO_CNT(cnt);
  373. for (i = 0; i < cnt / 4; i++) {
  374. *buf++ = readl(CONFIG_SYS_NAND_BASE);
  375. len -= 4;
  376. }
  377. } while (len);
  378. omap_prefetch_reset();
  379. return 0;
  380. }
  381. static inline void omap_nand_read(struct mtd_info *mtd, uint8_t *buf, int len)
  382. {
  383. struct nand_chip *chip = mtd_to_nand(mtd);
  384. if (chip->options & NAND_BUSWIDTH_16)
  385. nand_read_buf16(mtd, buf, len);
  386. else
  387. nand_read_buf(mtd, buf, len);
  388. }
  389. static void omap_nand_read_prefetch(struct mtd_info *mtd, uint8_t *buf, int len)
  390. {
  391. int ret;
  392. uint32_t head, tail;
  393. struct nand_chip *chip = mtd_to_nand(mtd);
  394. /*
  395. * If the destination buffer is unaligned, start with reading
  396. * the overlap byte-wise.
  397. */
  398. head = ((uint32_t) buf) % 4;
  399. if (head) {
  400. omap_nand_read(mtd, buf, head);
  401. buf += head;
  402. len -= head;
  403. }
  404. /*
  405. * Only transfer multiples of 4 bytes in a pre-fetched fashion.
  406. * If there's a residue, care for it byte-wise afterwards.
  407. */
  408. tail = len % 4;
  409. ret = __read_prefetch_aligned(chip, (uint32_t *)buf, len - tail);
  410. if (ret < 0) {
  411. /* fallback in case the prefetch engine is busy */
  412. omap_nand_read(mtd, buf, len);
  413. } else if (tail) {
  414. buf += len - tail;
  415. omap_nand_read(mtd, buf, tail);
  416. }
  417. }
  418. #endif /* CONFIG_NAND_OMAP_GPMC_PREFETCH */
  419. #ifdef CONFIG_NAND_OMAP_ELM
  420. /*
  421. * omap_reverse_list - re-orders list elements in reverse order [internal]
  422. * @list: pointer to start of list
  423. * @length: length of list
  424. */
  425. static void omap_reverse_list(u8 *list, unsigned int length)
  426. {
  427. unsigned int i, j;
  428. unsigned int half_length = length / 2;
  429. u8 tmp;
  430. for (i = 0, j = length - 1; i < half_length; i++, j--) {
  431. tmp = list[i];
  432. list[i] = list[j];
  433. list[j] = tmp;
  434. }
  435. }
  436. /*
  437. * omap_correct_data_bch - Compares the ecc read from nand spare area
  438. * with ECC registers values and corrects one bit error if it has occurred
  439. *
  440. * @mtd: MTD device structure
  441. * @dat: page data
  442. * @read_ecc: ecc read from nand flash (ignored)
  443. * @calc_ecc: ecc read from ECC registers
  444. *
  445. * @return 0 if data is OK or corrected, else returns -1
  446. */
  447. static int omap_correct_data_bch(struct mtd_info *mtd, uint8_t *dat,
  448. uint8_t *read_ecc, uint8_t *calc_ecc)
  449. {
  450. struct nand_chip *chip = mtd_to_nand(mtd);
  451. struct omap_nand_info *info = nand_get_controller_data(chip);
  452. struct nand_ecc_ctrl *ecc = &chip->ecc;
  453. uint32_t error_count = 0, error_max;
  454. uint32_t error_loc[ELM_MAX_ERROR_COUNT];
  455. enum bch_level bch_type;
  456. uint32_t i, ecc_flag = 0;
  457. uint8_t count;
  458. uint32_t byte_pos, bit_pos;
  459. int err = 0;
  460. /* check calculated ecc */
  461. for (i = 0; i < ecc->bytes && !ecc_flag; i++) {
  462. if (calc_ecc[i] != 0x00)
  463. ecc_flag = 1;
  464. }
  465. if (!ecc_flag)
  466. return 0;
  467. /* check for whether its a erased-page */
  468. ecc_flag = 0;
  469. for (i = 0; i < ecc->bytes && !ecc_flag; i++) {
  470. if (read_ecc[i] != 0xff)
  471. ecc_flag = 1;
  472. }
  473. if (!ecc_flag)
  474. return 0;
  475. /*
  476. * while reading ECC result we read it in big endian.
  477. * Hence while loading to ELM we have rotate to get the right endian.
  478. */
  479. switch (info->ecc_scheme) {
  480. case OMAP_ECC_BCH8_CODE_HW:
  481. bch_type = BCH_8_BIT;
  482. omap_reverse_list(calc_ecc, ecc->bytes - 1);
  483. break;
  484. case OMAP_ECC_BCH16_CODE_HW:
  485. bch_type = BCH_16_BIT;
  486. omap_reverse_list(calc_ecc, ecc->bytes);
  487. break;
  488. default:
  489. return -EINVAL;
  490. }
  491. /* use elm module to check for errors */
  492. elm_config(bch_type);
  493. err = elm_check_error(calc_ecc, bch_type, &error_count, error_loc);
  494. if (err)
  495. return err;
  496. /* correct bch error */
  497. for (count = 0; count < error_count; count++) {
  498. switch (info->ecc_scheme) {
  499. case OMAP_ECC_BCH8_CODE_HW:
  500. /* 14th byte in ECC is reserved to match ROM layout */
  501. error_max = SECTOR_BYTES + (ecc->bytes - 1);
  502. break;
  503. case OMAP_ECC_BCH16_CODE_HW:
  504. error_max = SECTOR_BYTES + ecc->bytes;
  505. break;
  506. default:
  507. return -EINVAL;
  508. }
  509. byte_pos = error_max - (error_loc[count] / 8) - 1;
  510. bit_pos = error_loc[count] % 8;
  511. if (byte_pos < SECTOR_BYTES) {
  512. dat[byte_pos] ^= 1 << bit_pos;
  513. debug("nand: bit-flip corrected @data=%d\n", byte_pos);
  514. } else if (byte_pos < error_max) {
  515. read_ecc[byte_pos - SECTOR_BYTES] ^= 1 << bit_pos;
  516. debug("nand: bit-flip corrected @oob=%d\n", byte_pos -
  517. SECTOR_BYTES);
  518. } else {
  519. err = -EBADMSG;
  520. printf("nand: error: invalid bit-flip location\n");
  521. }
  522. }
  523. return (err) ? err : error_count;
  524. }
  525. /**
  526. * omap_read_page_bch - hardware ecc based page read function
  527. * @mtd: mtd info structure
  528. * @chip: nand chip info structure
  529. * @buf: buffer to store read data
  530. * @oob_required: caller expects OOB data read to chip->oob_poi
  531. * @page: page number to read
  532. *
  533. */
  534. static int omap_read_page_bch(struct mtd_info *mtd, struct nand_chip *chip,
  535. uint8_t *buf, int oob_required, int page)
  536. {
  537. int i, eccsize = chip->ecc.size;
  538. int eccbytes = chip->ecc.bytes;
  539. int eccsteps = chip->ecc.steps;
  540. uint8_t *p = buf;
  541. uint8_t *ecc_calc = chip->buffers->ecccalc;
  542. uint8_t *ecc_code = chip->buffers->ecccode;
  543. uint32_t *eccpos = chip->ecc.layout->eccpos;
  544. uint8_t *oob = chip->oob_poi;
  545. uint32_t data_pos;
  546. uint32_t oob_pos;
  547. data_pos = 0;
  548. /* oob area start */
  549. oob_pos = (eccsize * eccsteps) + chip->ecc.layout->eccpos[0];
  550. oob += chip->ecc.layout->eccpos[0];
  551. for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize,
  552. oob += eccbytes) {
  553. chip->ecc.hwctl(mtd, NAND_ECC_READ);
  554. /* read data */
  555. chip->cmdfunc(mtd, NAND_CMD_RNDOUT, data_pos, -1);
  556. chip->read_buf(mtd, p, eccsize);
  557. /* read respective ecc from oob area */
  558. chip->cmdfunc(mtd, NAND_CMD_RNDOUT, oob_pos, -1);
  559. chip->read_buf(mtd, oob, eccbytes);
  560. /* read syndrome */
  561. chip->ecc.calculate(mtd, p, &ecc_calc[i]);
  562. data_pos += eccsize;
  563. oob_pos += eccbytes;
  564. }
  565. for (i = 0; i < chip->ecc.total; i++)
  566. ecc_code[i] = chip->oob_poi[eccpos[i]];
  567. eccsteps = chip->ecc.steps;
  568. p = buf;
  569. for (i = 0 ; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
  570. int stat;
  571. stat = chip->ecc.correct(mtd, p, &ecc_code[i], &ecc_calc[i]);
  572. if (stat < 0)
  573. mtd->ecc_stats.failed++;
  574. else
  575. mtd->ecc_stats.corrected += stat;
  576. }
  577. return 0;
  578. }
  579. #endif /* CONFIG_NAND_OMAP_ELM */
  580. /*
  581. * OMAP3 BCH8 support (with BCH library)
  582. */
  583. #ifdef CONFIG_BCH
  584. /**
  585. * omap_correct_data_bch_sw - Decode received data and correct errors
  586. * @mtd: MTD device structure
  587. * @data: page data
  588. * @read_ecc: ecc read from nand flash
  589. * @calc_ecc: ecc read from HW ECC registers
  590. */
  591. static int omap_correct_data_bch_sw(struct mtd_info *mtd, u_char *data,
  592. u_char *read_ecc, u_char *calc_ecc)
  593. {
  594. int i, count;
  595. /* cannot correct more than 8 errors */
  596. unsigned int errloc[8];
  597. struct nand_chip *chip = mtd_to_nand(mtd);
  598. struct omap_nand_info *info = nand_get_controller_data(chip);
  599. count = decode_bch(info->control, NULL, 512, read_ecc, calc_ecc,
  600. NULL, errloc);
  601. if (count > 0) {
  602. /* correct errors */
  603. for (i = 0; i < count; i++) {
  604. /* correct data only, not ecc bytes */
  605. if (errloc[i] < 8*512)
  606. data[errloc[i]/8] ^= 1 << (errloc[i] & 7);
  607. debug("corrected bitflip %u\n", errloc[i]);
  608. #ifdef DEBUG
  609. puts("read_ecc: ");
  610. /*
  611. * BCH8 have 13 bytes of ECC; BCH4 needs adoption
  612. * here!
  613. */
  614. for (i = 0; i < 13; i++)
  615. printf("%02x ", read_ecc[i]);
  616. puts("\n");
  617. puts("calc_ecc: ");
  618. for (i = 0; i < 13; i++)
  619. printf("%02x ", calc_ecc[i]);
  620. puts("\n");
  621. #endif
  622. }
  623. } else if (count < 0) {
  624. puts("ecc unrecoverable error\n");
  625. }
  626. return count;
  627. }
  628. /**
  629. * omap_free_bch - Release BCH ecc resources
  630. * @mtd: MTD device structure
  631. */
  632. static void __maybe_unused omap_free_bch(struct mtd_info *mtd)
  633. {
  634. struct nand_chip *chip = mtd_to_nand(mtd);
  635. struct omap_nand_info *info = nand_get_controller_data(chip);
  636. if (info->control) {
  637. free_bch(info->control);
  638. info->control = NULL;
  639. }
  640. }
  641. #endif /* CONFIG_BCH */
  642. /**
  643. * omap_select_ecc_scheme - configures driver for particular ecc-scheme
  644. * @nand: NAND chip device structure
  645. * @ecc_scheme: ecc scheme to configure
  646. * @pagesize: number of main-area bytes per page of NAND device
  647. * @oobsize: number of OOB/spare bytes per page of NAND device
  648. */
  649. static int omap_select_ecc_scheme(struct nand_chip *nand,
  650. enum omap_ecc ecc_scheme, unsigned int pagesize, unsigned int oobsize) {
  651. struct omap_nand_info *info = nand_get_controller_data(nand);
  652. struct nand_ecclayout *ecclayout = &omap_ecclayout;
  653. int eccsteps = pagesize / SECTOR_BYTES;
  654. int i;
  655. switch (ecc_scheme) {
  656. case OMAP_ECC_HAM1_CODE_SW:
  657. debug("nand: selected OMAP_ECC_HAM1_CODE_SW\n");
  658. /* For this ecc-scheme, ecc.bytes, ecc.layout, ... are
  659. * initialized in nand_scan_tail(), so just set ecc.mode */
  660. info->control = NULL;
  661. nand->ecc.mode = NAND_ECC_SOFT;
  662. nand->ecc.layout = NULL;
  663. nand->ecc.size = 0;
  664. break;
  665. case OMAP_ECC_HAM1_CODE_HW:
  666. debug("nand: selected OMAP_ECC_HAM1_CODE_HW\n");
  667. /* check ecc-scheme requirements before updating ecc info */
  668. if ((3 * eccsteps) + BADBLOCK_MARKER_LENGTH > oobsize) {
  669. printf("nand: error: insufficient OOB: require=%d\n", (
  670. (3 * eccsteps) + BADBLOCK_MARKER_LENGTH));
  671. return -EINVAL;
  672. }
  673. info->control = NULL;
  674. /* populate ecc specific fields */
  675. memset(&nand->ecc, 0, sizeof(struct nand_ecc_ctrl));
  676. nand->ecc.mode = NAND_ECC_HW;
  677. nand->ecc.strength = 1;
  678. nand->ecc.size = SECTOR_BYTES;
  679. nand->ecc.bytes = 3;
  680. nand->ecc.hwctl = omap_enable_hwecc;
  681. nand->ecc.correct = omap_correct_data;
  682. nand->ecc.calculate = omap_calculate_ecc;
  683. /* define ecc-layout */
  684. ecclayout->eccbytes = nand->ecc.bytes * eccsteps;
  685. for (i = 0; i < ecclayout->eccbytes; i++) {
  686. if (nand->options & NAND_BUSWIDTH_16)
  687. ecclayout->eccpos[i] = i + 2;
  688. else
  689. ecclayout->eccpos[i] = i + 1;
  690. }
  691. ecclayout->oobfree[0].offset = i + BADBLOCK_MARKER_LENGTH;
  692. ecclayout->oobfree[0].length = oobsize - ecclayout->eccbytes -
  693. BADBLOCK_MARKER_LENGTH;
  694. break;
  695. case OMAP_ECC_BCH8_CODE_HW_DETECTION_SW:
  696. #ifdef CONFIG_BCH
  697. debug("nand: selected OMAP_ECC_BCH8_CODE_HW_DETECTION_SW\n");
  698. /* check ecc-scheme requirements before updating ecc info */
  699. if ((13 * eccsteps) + BADBLOCK_MARKER_LENGTH > oobsize) {
  700. printf("nand: error: insufficient OOB: require=%d\n", (
  701. (13 * eccsteps) + BADBLOCK_MARKER_LENGTH));
  702. return -EINVAL;
  703. }
  704. /* check if BCH S/W library can be used for error detection */
  705. info->control = init_bch(13, 8, 0x201b);
  706. if (!info->control) {
  707. printf("nand: error: could not init_bch()\n");
  708. return -ENODEV;
  709. }
  710. /* populate ecc specific fields */
  711. memset(&nand->ecc, 0, sizeof(struct nand_ecc_ctrl));
  712. nand->ecc.mode = NAND_ECC_HW;
  713. nand->ecc.strength = 8;
  714. nand->ecc.size = SECTOR_BYTES;
  715. nand->ecc.bytes = 13;
  716. nand->ecc.hwctl = omap_enable_hwecc;
  717. nand->ecc.correct = omap_correct_data_bch_sw;
  718. nand->ecc.calculate = omap_calculate_ecc;
  719. /* define ecc-layout */
  720. ecclayout->eccbytes = nand->ecc.bytes * eccsteps;
  721. ecclayout->eccpos[0] = BADBLOCK_MARKER_LENGTH;
  722. for (i = 1; i < ecclayout->eccbytes; i++) {
  723. if (i % nand->ecc.bytes)
  724. ecclayout->eccpos[i] =
  725. ecclayout->eccpos[i - 1] + 1;
  726. else
  727. ecclayout->eccpos[i] =
  728. ecclayout->eccpos[i - 1] + 2;
  729. }
  730. ecclayout->oobfree[0].offset = i + BADBLOCK_MARKER_LENGTH;
  731. ecclayout->oobfree[0].length = oobsize - ecclayout->eccbytes -
  732. BADBLOCK_MARKER_LENGTH;
  733. break;
  734. #else
  735. printf("nand: error: CONFIG_BCH required for ECC\n");
  736. return -EINVAL;
  737. #endif
  738. case OMAP_ECC_BCH8_CODE_HW:
  739. #ifdef CONFIG_NAND_OMAP_ELM
  740. debug("nand: selected OMAP_ECC_BCH8_CODE_HW\n");
  741. /* check ecc-scheme requirements before updating ecc info */
  742. if ((14 * eccsteps) + BADBLOCK_MARKER_LENGTH > oobsize) {
  743. printf("nand: error: insufficient OOB: require=%d\n", (
  744. (14 * eccsteps) + BADBLOCK_MARKER_LENGTH));
  745. return -EINVAL;
  746. }
  747. /* intialize ELM for ECC error detection */
  748. elm_init();
  749. info->control = NULL;
  750. /* populate ecc specific fields */
  751. memset(&nand->ecc, 0, sizeof(struct nand_ecc_ctrl));
  752. nand->ecc.mode = NAND_ECC_HW;
  753. nand->ecc.strength = 8;
  754. nand->ecc.size = SECTOR_BYTES;
  755. nand->ecc.bytes = 14;
  756. nand->ecc.hwctl = omap_enable_hwecc;
  757. nand->ecc.correct = omap_correct_data_bch;
  758. nand->ecc.calculate = omap_calculate_ecc;
  759. nand->ecc.read_page = omap_read_page_bch;
  760. /* define ecc-layout */
  761. ecclayout->eccbytes = nand->ecc.bytes * eccsteps;
  762. for (i = 0; i < ecclayout->eccbytes; i++)
  763. ecclayout->eccpos[i] = i + BADBLOCK_MARKER_LENGTH;
  764. ecclayout->oobfree[0].offset = i + BADBLOCK_MARKER_LENGTH;
  765. ecclayout->oobfree[0].length = oobsize - ecclayout->eccbytes -
  766. BADBLOCK_MARKER_LENGTH;
  767. break;
  768. #else
  769. printf("nand: error: CONFIG_NAND_OMAP_ELM required for ECC\n");
  770. return -EINVAL;
  771. #endif
  772. case OMAP_ECC_BCH16_CODE_HW:
  773. #ifdef CONFIG_NAND_OMAP_ELM
  774. debug("nand: using OMAP_ECC_BCH16_CODE_HW\n");
  775. /* check ecc-scheme requirements before updating ecc info */
  776. if ((26 * eccsteps) + BADBLOCK_MARKER_LENGTH > oobsize) {
  777. printf("nand: error: insufficient OOB: require=%d\n", (
  778. (26 * eccsteps) + BADBLOCK_MARKER_LENGTH));
  779. return -EINVAL;
  780. }
  781. /* intialize ELM for ECC error detection */
  782. elm_init();
  783. /* populate ecc specific fields */
  784. nand->ecc.mode = NAND_ECC_HW;
  785. nand->ecc.size = SECTOR_BYTES;
  786. nand->ecc.bytes = 26;
  787. nand->ecc.strength = 16;
  788. nand->ecc.hwctl = omap_enable_hwecc;
  789. nand->ecc.correct = omap_correct_data_bch;
  790. nand->ecc.calculate = omap_calculate_ecc;
  791. nand->ecc.read_page = omap_read_page_bch;
  792. /* define ecc-layout */
  793. ecclayout->eccbytes = nand->ecc.bytes * eccsteps;
  794. for (i = 0; i < ecclayout->eccbytes; i++)
  795. ecclayout->eccpos[i] = i + BADBLOCK_MARKER_LENGTH;
  796. ecclayout->oobfree[0].offset = i + BADBLOCK_MARKER_LENGTH;
  797. ecclayout->oobfree[0].length = oobsize - nand->ecc.bytes -
  798. BADBLOCK_MARKER_LENGTH;
  799. break;
  800. #else
  801. printf("nand: error: CONFIG_NAND_OMAP_ELM required for ECC\n");
  802. return -EINVAL;
  803. #endif
  804. default:
  805. debug("nand: error: ecc scheme not enabled or supported\n");
  806. return -EINVAL;
  807. }
  808. /* nand_scan_tail() sets ham1 sw ecc; hw ecc layout is set by driver */
  809. if (ecc_scheme != OMAP_ECC_HAM1_CODE_SW)
  810. nand->ecc.layout = ecclayout;
  811. info->ecc_scheme = ecc_scheme;
  812. return 0;
  813. }
  814. #ifndef CONFIG_SPL_BUILD
  815. /*
  816. * omap_nand_switch_ecc - switch the ECC operation between different engines
  817. * (h/w and s/w) and different algorithms (hamming and BCHx)
  818. *
  819. * @hardware - true if one of the HW engines should be used
  820. * @eccstrength - the number of bits that could be corrected
  821. * (1 - hamming, 4 - BCH4, 8 - BCH8, 16 - BCH16)
  822. */
  823. int __maybe_unused omap_nand_switch_ecc(uint32_t hardware, uint32_t eccstrength)
  824. {
  825. struct nand_chip *nand;
  826. struct mtd_info *mtd;
  827. int err = 0;
  828. if (nand_curr_device < 0 ||
  829. nand_curr_device >= CONFIG_SYS_MAX_NAND_DEVICE ||
  830. !nand_info[nand_curr_device]->name) {
  831. printf("nand: error: no NAND devices found\n");
  832. return -ENODEV;
  833. }
  834. mtd = nand_info[nand_curr_device];
  835. nand = mtd_to_nand(mtd);
  836. nand->options |= NAND_OWN_BUFFERS;
  837. nand->options &= ~NAND_SUBPAGE_READ;
  838. /* Setup the ecc configurations again */
  839. if (hardware) {
  840. if (eccstrength == 1) {
  841. err = omap_select_ecc_scheme(nand,
  842. OMAP_ECC_HAM1_CODE_HW,
  843. mtd->writesize, mtd->oobsize);
  844. } else if (eccstrength == 8) {
  845. err = omap_select_ecc_scheme(nand,
  846. OMAP_ECC_BCH8_CODE_HW,
  847. mtd->writesize, mtd->oobsize);
  848. } else {
  849. printf("nand: error: unsupported ECC scheme\n");
  850. return -EINVAL;
  851. }
  852. } else {
  853. if (eccstrength == 1) {
  854. err = omap_select_ecc_scheme(nand,
  855. OMAP_ECC_HAM1_CODE_SW,
  856. mtd->writesize, mtd->oobsize);
  857. } else if (eccstrength == 8) {
  858. err = omap_select_ecc_scheme(nand,
  859. OMAP_ECC_BCH8_CODE_HW_DETECTION_SW,
  860. mtd->writesize, mtd->oobsize);
  861. } else {
  862. printf("nand: error: unsupported ECC scheme\n");
  863. return -EINVAL;
  864. }
  865. }
  866. /* Update NAND handling after ECC mode switch */
  867. if (!err)
  868. err = nand_scan_tail(mtd);
  869. return err;
  870. }
  871. #endif /* CONFIG_SPL_BUILD */
  872. /*
  873. * Board-specific NAND initialization. The following members of the
  874. * argument are board-specific:
  875. * - IO_ADDR_R: address to read the 8 I/O lines of the flash device
  876. * - IO_ADDR_W: address to write the 8 I/O lines of the flash device
  877. * - cmd_ctrl: hardwarespecific function for accesing control-lines
  878. * - waitfunc: hardwarespecific function for accesing device ready/busy line
  879. * - ecc.hwctl: function to enable (reset) hardware ecc generator
  880. * - ecc.mode: mode of ecc, see defines
  881. * - chip_delay: chip dependent delay for transfering data from array to
  882. * read regs (tR)
  883. * - options: various chip options. They can partly be set to inform
  884. * nand_scan about special functionality. See the defines for further
  885. * explanation
  886. */
  887. int board_nand_init(struct nand_chip *nand)
  888. {
  889. int32_t gpmc_config = 0;
  890. int cs = cs_next++;
  891. int err = 0;
  892. /*
  893. * xloader/Uboot's gpmc configuration would have configured GPMC for
  894. * nand type of memory. The following logic scans and latches on to the
  895. * first CS with NAND type memory.
  896. * TBD: need to make this logic generic to handle multiple CS NAND
  897. * devices.
  898. */
  899. while (cs < GPMC_MAX_CS) {
  900. /* Check if NAND type is set */
  901. if ((readl(&gpmc_cfg->cs[cs].config1) & 0xC00) == 0x800) {
  902. /* Found it!! */
  903. break;
  904. }
  905. cs++;
  906. }
  907. if (cs >= GPMC_MAX_CS) {
  908. printf("nand: error: Unable to find NAND settings in "
  909. "GPMC Configuration - quitting\n");
  910. return -ENODEV;
  911. }
  912. gpmc_config = readl(&gpmc_cfg->config);
  913. /* Disable Write protect */
  914. gpmc_config |= 0x10;
  915. writel(gpmc_config, &gpmc_cfg->config);
  916. nand->IO_ADDR_R = (void __iomem *)&gpmc_cfg->cs[cs].nand_dat;
  917. nand->IO_ADDR_W = (void __iomem *)&gpmc_cfg->cs[cs].nand_cmd;
  918. omap_nand_info[cs].control = NULL;
  919. omap_nand_info[cs].cs = cs;
  920. omap_nand_info[cs].ws = wscfg[cs];
  921. nand_set_controller_data(nand, &omap_nand_info[cs]);
  922. nand->cmd_ctrl = omap_nand_hwcontrol;
  923. nand->options |= NAND_NO_PADDING | NAND_CACHEPRG;
  924. nand->chip_delay = 100;
  925. nand->ecc.layout = &omap_ecclayout;
  926. /* configure driver and controller based on NAND device bus-width */
  927. gpmc_config = readl(&gpmc_cfg->cs[cs].config1);
  928. #if defined(CONFIG_SYS_NAND_BUSWIDTH_16BIT)
  929. nand->options |= NAND_BUSWIDTH_16;
  930. writel(gpmc_config | (0x1 << 12), &gpmc_cfg->cs[cs].config1);
  931. #else
  932. nand->options &= ~NAND_BUSWIDTH_16;
  933. writel(gpmc_config & ~(0x1 << 12), &gpmc_cfg->cs[cs].config1);
  934. #endif
  935. /* select ECC scheme */
  936. #if defined(CONFIG_NAND_OMAP_ECCSCHEME)
  937. err = omap_select_ecc_scheme(nand, CONFIG_NAND_OMAP_ECCSCHEME,
  938. CONFIG_SYS_NAND_PAGE_SIZE, CONFIG_SYS_NAND_OOBSIZE);
  939. #else
  940. /* pagesize and oobsize are not required to configure sw ecc-scheme */
  941. err = omap_select_ecc_scheme(nand, OMAP_ECC_HAM1_CODE_SW,
  942. 0, 0);
  943. #endif
  944. if (err)
  945. return err;
  946. #ifdef CONFIG_NAND_OMAP_GPMC_PREFETCH
  947. nand->read_buf = omap_nand_read_prefetch;
  948. #else
  949. if (nand->options & NAND_BUSWIDTH_16)
  950. nand->read_buf = nand_read_buf16;
  951. else
  952. nand->read_buf = nand_read_buf;
  953. #endif
  954. nand->dev_ready = omap_dev_ready;
  955. return 0;
  956. }