omap_gpmc.c 24 KB

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