omap_gpmc.c 29 KB

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