omap_gpmc.c 26 KB

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