omap_gpmc.c 24 KB

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