omap_gpmc.c 34 KB

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