omap2.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974
  1. /*
  2. * Copyright © 2004 Texas Instruments, Jian Zhang <jzhang@ti.com>
  3. * Copyright © 2004 Micron Technology Inc.
  4. * Copyright © 2004 David Brownell
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License version 2 as
  8. * published by the Free Software Foundation.
  9. */
  10. #define CONFIG_MTD_NAND_OMAP_HWECC
  11. #include <linux/platform_device.h>
  12. #include <linux/dma-mapping.h>
  13. #include <linux/delay.h>
  14. #include <linux/jiffies.h>
  15. #include <linux/sched.h>
  16. #include <linux/mtd/mtd.h>
  17. #include <linux/mtd/nand.h>
  18. #include <linux/mtd/partitions.h>
  19. #include <linux/io.h>
  20. #include <linux/slab.h>
  21. #include <plat/dma.h>
  22. #include <plat/gpmc.h>
  23. #include <plat/nand.h>
  24. #define DRIVER_NAME "omap2-nand"
  25. #define NAND_Ecc_P1e (1 << 0)
  26. #define NAND_Ecc_P2e (1 << 1)
  27. #define NAND_Ecc_P4e (1 << 2)
  28. #define NAND_Ecc_P8e (1 << 3)
  29. #define NAND_Ecc_P16e (1 << 4)
  30. #define NAND_Ecc_P32e (1 << 5)
  31. #define NAND_Ecc_P64e (1 << 6)
  32. #define NAND_Ecc_P128e (1 << 7)
  33. #define NAND_Ecc_P256e (1 << 8)
  34. #define NAND_Ecc_P512e (1 << 9)
  35. #define NAND_Ecc_P1024e (1 << 10)
  36. #define NAND_Ecc_P2048e (1 << 11)
  37. #define NAND_Ecc_P1o (1 << 16)
  38. #define NAND_Ecc_P2o (1 << 17)
  39. #define NAND_Ecc_P4o (1 << 18)
  40. #define NAND_Ecc_P8o (1 << 19)
  41. #define NAND_Ecc_P16o (1 << 20)
  42. #define NAND_Ecc_P32o (1 << 21)
  43. #define NAND_Ecc_P64o (1 << 22)
  44. #define NAND_Ecc_P128o (1 << 23)
  45. #define NAND_Ecc_P256o (1 << 24)
  46. #define NAND_Ecc_P512o (1 << 25)
  47. #define NAND_Ecc_P1024o (1 << 26)
  48. #define NAND_Ecc_P2048o (1 << 27)
  49. #define TF(value) (value ? 1 : 0)
  50. #define P2048e(a) (TF(a & NAND_Ecc_P2048e) << 0)
  51. #define P2048o(a) (TF(a & NAND_Ecc_P2048o) << 1)
  52. #define P1e(a) (TF(a & NAND_Ecc_P1e) << 2)
  53. #define P1o(a) (TF(a & NAND_Ecc_P1o) << 3)
  54. #define P2e(a) (TF(a & NAND_Ecc_P2e) << 4)
  55. #define P2o(a) (TF(a & NAND_Ecc_P2o) << 5)
  56. #define P4e(a) (TF(a & NAND_Ecc_P4e) << 6)
  57. #define P4o(a) (TF(a & NAND_Ecc_P4o) << 7)
  58. #define P8e(a) (TF(a & NAND_Ecc_P8e) << 0)
  59. #define P8o(a) (TF(a & NAND_Ecc_P8o) << 1)
  60. #define P16e(a) (TF(a & NAND_Ecc_P16e) << 2)
  61. #define P16o(a) (TF(a & NAND_Ecc_P16o) << 3)
  62. #define P32e(a) (TF(a & NAND_Ecc_P32e) << 4)
  63. #define P32o(a) (TF(a & NAND_Ecc_P32o) << 5)
  64. #define P64e(a) (TF(a & NAND_Ecc_P64e) << 6)
  65. #define P64o(a) (TF(a & NAND_Ecc_P64o) << 7)
  66. #define P128e(a) (TF(a & NAND_Ecc_P128e) << 0)
  67. #define P128o(a) (TF(a & NAND_Ecc_P128o) << 1)
  68. #define P256e(a) (TF(a & NAND_Ecc_P256e) << 2)
  69. #define P256o(a) (TF(a & NAND_Ecc_P256o) << 3)
  70. #define P512e(a) (TF(a & NAND_Ecc_P512e) << 4)
  71. #define P512o(a) (TF(a & NAND_Ecc_P512o) << 5)
  72. #define P1024e(a) (TF(a & NAND_Ecc_P1024e) << 6)
  73. #define P1024o(a) (TF(a & NAND_Ecc_P1024o) << 7)
  74. #define P8e_s(a) (TF(a & NAND_Ecc_P8e) << 0)
  75. #define P8o_s(a) (TF(a & NAND_Ecc_P8o) << 1)
  76. #define P16e_s(a) (TF(a & NAND_Ecc_P16e) << 2)
  77. #define P16o_s(a) (TF(a & NAND_Ecc_P16o) << 3)
  78. #define P1e_s(a) (TF(a & NAND_Ecc_P1e) << 4)
  79. #define P1o_s(a) (TF(a & NAND_Ecc_P1o) << 5)
  80. #define P2e_s(a) (TF(a & NAND_Ecc_P2e) << 6)
  81. #define P2o_s(a) (TF(a & NAND_Ecc_P2o) << 7)
  82. #define P4e_s(a) (TF(a & NAND_Ecc_P4e) << 0)
  83. #define P4o_s(a) (TF(a & NAND_Ecc_P4o) << 1)
  84. #ifdef CONFIG_MTD_PARTITIONS
  85. static const char *part_probes[] = { "cmdlinepart", NULL };
  86. #endif
  87. #ifdef CONFIG_MTD_NAND_OMAP_PREFETCH
  88. static int use_prefetch = 1;
  89. /* "modprobe ... use_prefetch=0" etc */
  90. module_param(use_prefetch, bool, 0);
  91. MODULE_PARM_DESC(use_prefetch, "enable/disable use of PREFETCH");
  92. #ifdef CONFIG_MTD_NAND_OMAP_PREFETCH_DMA
  93. static int use_dma = 1;
  94. /* "modprobe ... use_dma=0" etc */
  95. module_param(use_dma, bool, 0);
  96. MODULE_PARM_DESC(use_dma, "enable/disable use of DMA");
  97. #else
  98. const int use_dma;
  99. #endif
  100. #else
  101. const int use_prefetch;
  102. const int use_dma;
  103. #endif
  104. struct omap_nand_info {
  105. struct nand_hw_control controller;
  106. struct omap_nand_platform_data *pdata;
  107. struct mtd_info mtd;
  108. struct mtd_partition *parts;
  109. struct nand_chip nand;
  110. struct platform_device *pdev;
  111. int gpmc_cs;
  112. unsigned long phys_base;
  113. struct completion comp;
  114. int dma_ch;
  115. };
  116. /**
  117. * omap_hwcontrol - hardware specific access to control-lines
  118. * @mtd: MTD device structure
  119. * @cmd: command to device
  120. * @ctrl:
  121. * NAND_NCE: bit 0 -> don't care
  122. * NAND_CLE: bit 1 -> Command Latch
  123. * NAND_ALE: bit 2 -> Address Latch
  124. *
  125. * NOTE: boards may use different bits for these!!
  126. */
  127. static void omap_hwcontrol(struct mtd_info *mtd, int cmd, unsigned int ctrl)
  128. {
  129. struct omap_nand_info *info = container_of(mtd,
  130. struct omap_nand_info, mtd);
  131. if (cmd != NAND_CMD_NONE) {
  132. if (ctrl & NAND_CLE)
  133. gpmc_nand_write(info->gpmc_cs, GPMC_NAND_COMMAND, cmd);
  134. else if (ctrl & NAND_ALE)
  135. gpmc_nand_write(info->gpmc_cs, GPMC_NAND_ADDRESS, cmd);
  136. else /* NAND_NCE */
  137. gpmc_nand_write(info->gpmc_cs, GPMC_NAND_DATA, cmd);
  138. }
  139. }
  140. /**
  141. * omap_read_buf8 - read data from NAND controller into buffer
  142. * @mtd: MTD device structure
  143. * @buf: buffer to store date
  144. * @len: number of bytes to read
  145. */
  146. static void omap_read_buf8(struct mtd_info *mtd, u_char *buf, int len)
  147. {
  148. struct nand_chip *nand = mtd->priv;
  149. ioread8_rep(nand->IO_ADDR_R, buf, len);
  150. }
  151. /**
  152. * omap_write_buf8 - write buffer to NAND controller
  153. * @mtd: MTD device structure
  154. * @buf: data buffer
  155. * @len: number of bytes to write
  156. */
  157. static void omap_write_buf8(struct mtd_info *mtd, const u_char *buf, int len)
  158. {
  159. struct omap_nand_info *info = container_of(mtd,
  160. struct omap_nand_info, mtd);
  161. u_char *p = (u_char *)buf;
  162. u32 status = 0;
  163. while (len--) {
  164. iowrite8(*p++, info->nand.IO_ADDR_W);
  165. /* wait until buffer is available for write */
  166. do {
  167. status = gpmc_read_status(GPMC_STATUS_BUFFER);
  168. } while (!status);
  169. }
  170. }
  171. /**
  172. * omap_read_buf16 - read data from NAND controller into buffer
  173. * @mtd: MTD device structure
  174. * @buf: buffer to store date
  175. * @len: number of bytes to read
  176. */
  177. static void omap_read_buf16(struct mtd_info *mtd, u_char *buf, int len)
  178. {
  179. struct nand_chip *nand = mtd->priv;
  180. ioread16_rep(nand->IO_ADDR_R, buf, len / 2);
  181. }
  182. /**
  183. * omap_write_buf16 - write buffer to NAND controller
  184. * @mtd: MTD device structure
  185. * @buf: data buffer
  186. * @len: number of bytes to write
  187. */
  188. static void omap_write_buf16(struct mtd_info *mtd, const u_char * buf, int len)
  189. {
  190. struct omap_nand_info *info = container_of(mtd,
  191. struct omap_nand_info, mtd);
  192. u16 *p = (u16 *) buf;
  193. u32 status = 0;
  194. /* FIXME try bursts of writesw() or DMA ... */
  195. len >>= 1;
  196. while (len--) {
  197. iowrite16(*p++, info->nand.IO_ADDR_W);
  198. /* wait until buffer is available for write */
  199. do {
  200. status = gpmc_read_status(GPMC_STATUS_BUFFER);
  201. } while (!status);
  202. }
  203. }
  204. /**
  205. * omap_read_buf_pref - read data from NAND controller into buffer
  206. * @mtd: MTD device structure
  207. * @buf: buffer to store date
  208. * @len: number of bytes to read
  209. */
  210. static void omap_read_buf_pref(struct mtd_info *mtd, u_char *buf, int len)
  211. {
  212. struct omap_nand_info *info = container_of(mtd,
  213. struct omap_nand_info, mtd);
  214. uint32_t r_count = 0;
  215. int ret = 0;
  216. u32 *p = (u32 *)buf;
  217. /* take care of subpage reads */
  218. if (len % 4) {
  219. if (info->nand.options & NAND_BUSWIDTH_16)
  220. omap_read_buf16(mtd, buf, len % 4);
  221. else
  222. omap_read_buf8(mtd, buf, len % 4);
  223. p = (u32 *) (buf + len % 4);
  224. len -= len % 4;
  225. }
  226. /* configure and start prefetch transfer */
  227. ret = gpmc_prefetch_enable(info->gpmc_cs, 0x0, len, 0x0);
  228. if (ret) {
  229. /* PFPW engine is busy, use cpu copy method */
  230. if (info->nand.options & NAND_BUSWIDTH_16)
  231. omap_read_buf16(mtd, buf, len);
  232. else
  233. omap_read_buf8(mtd, buf, len);
  234. } else {
  235. p = (u32 *) buf;
  236. do {
  237. r_count = gpmc_read_status(GPMC_PREFETCH_FIFO_CNT);
  238. r_count = r_count >> 2;
  239. ioread32_rep(info->nand.IO_ADDR_R, p, r_count);
  240. p += r_count;
  241. len -= r_count << 2;
  242. } while (len);
  243. /* disable and stop the PFPW engine */
  244. gpmc_prefetch_reset(info->gpmc_cs);
  245. }
  246. }
  247. /**
  248. * omap_write_buf_pref - write buffer to NAND controller
  249. * @mtd: MTD device structure
  250. * @buf: data buffer
  251. * @len: number of bytes to write
  252. */
  253. static void omap_write_buf_pref(struct mtd_info *mtd,
  254. const u_char *buf, int len)
  255. {
  256. struct omap_nand_info *info = container_of(mtd,
  257. struct omap_nand_info, mtd);
  258. uint32_t pref_count = 0, w_count = 0;
  259. int i = 0, ret = 0;
  260. u16 *p;
  261. /* take care of subpage writes */
  262. if (len % 2 != 0) {
  263. writeb(*buf, info->nand.IO_ADDR_W);
  264. p = (u16 *)(buf + 1);
  265. len--;
  266. }
  267. /* configure and start prefetch transfer */
  268. ret = gpmc_prefetch_enable(info->gpmc_cs, 0x0, len, 0x1);
  269. if (ret) {
  270. /* PFPW engine is busy, use cpu copy method */
  271. if (info->nand.options & NAND_BUSWIDTH_16)
  272. omap_write_buf16(mtd, buf, len);
  273. else
  274. omap_write_buf8(mtd, buf, len);
  275. } else {
  276. p = (u16 *) buf;
  277. while (len) {
  278. w_count = gpmc_read_status(GPMC_PREFETCH_FIFO_CNT);
  279. w_count = w_count >> 1;
  280. for (i = 0; (i < w_count) && len; i++, len -= 2)
  281. iowrite16(*p++, info->nand.IO_ADDR_W);
  282. }
  283. /* wait for data to flushed-out before reset the prefetch */
  284. do {
  285. pref_count = gpmc_read_status(GPMC_PREFETCH_COUNT);
  286. } while (pref_count);
  287. /* disable and stop the PFPW engine */
  288. gpmc_prefetch_reset(info->gpmc_cs);
  289. }
  290. }
  291. #ifdef CONFIG_MTD_NAND_OMAP_PREFETCH_DMA
  292. /*
  293. * omap_nand_dma_cb: callback on the completion of dma transfer
  294. * @lch: logical channel
  295. * @ch_satuts: channel status
  296. * @data: pointer to completion data structure
  297. */
  298. static void omap_nand_dma_cb(int lch, u16 ch_status, void *data)
  299. {
  300. complete((struct completion *) data);
  301. }
  302. /*
  303. * omap_nand_dma_transfer: configer and start dma transfer
  304. * @mtd: MTD device structure
  305. * @addr: virtual address in RAM of source/destination
  306. * @len: number of data bytes to be transferred
  307. * @is_write: flag for read/write operation
  308. */
  309. static inline int omap_nand_dma_transfer(struct mtd_info *mtd, void *addr,
  310. unsigned int len, int is_write)
  311. {
  312. struct omap_nand_info *info = container_of(mtd,
  313. struct omap_nand_info, mtd);
  314. uint32_t prefetch_status = 0;
  315. enum dma_data_direction dir = is_write ? DMA_TO_DEVICE :
  316. DMA_FROM_DEVICE;
  317. dma_addr_t dma_addr;
  318. int ret;
  319. /* The fifo depth is 64 bytes. We have a sync at each frame and frame
  320. * length is 64 bytes.
  321. */
  322. int buf_len = len >> 6;
  323. if (addr >= high_memory) {
  324. struct page *p1;
  325. if (((size_t)addr & PAGE_MASK) !=
  326. ((size_t)(addr + len - 1) & PAGE_MASK))
  327. goto out_copy;
  328. p1 = vmalloc_to_page(addr);
  329. if (!p1)
  330. goto out_copy;
  331. addr = page_address(p1) + ((size_t)addr & ~PAGE_MASK);
  332. }
  333. dma_addr = dma_map_single(&info->pdev->dev, addr, len, dir);
  334. if (dma_mapping_error(&info->pdev->dev, dma_addr)) {
  335. dev_err(&info->pdev->dev,
  336. "Couldn't DMA map a %d byte buffer\n", len);
  337. goto out_copy;
  338. }
  339. if (is_write) {
  340. omap_set_dma_dest_params(info->dma_ch, 0, OMAP_DMA_AMODE_CONSTANT,
  341. info->phys_base, 0, 0);
  342. omap_set_dma_src_params(info->dma_ch, 0, OMAP_DMA_AMODE_POST_INC,
  343. dma_addr, 0, 0);
  344. omap_set_dma_transfer_params(info->dma_ch, OMAP_DMA_DATA_TYPE_S32,
  345. 0x10, buf_len, OMAP_DMA_SYNC_FRAME,
  346. OMAP24XX_DMA_GPMC, OMAP_DMA_DST_SYNC);
  347. } else {
  348. omap_set_dma_src_params(info->dma_ch, 0, OMAP_DMA_AMODE_CONSTANT,
  349. info->phys_base, 0, 0);
  350. omap_set_dma_dest_params(info->dma_ch, 0, OMAP_DMA_AMODE_POST_INC,
  351. dma_addr, 0, 0);
  352. omap_set_dma_transfer_params(info->dma_ch, OMAP_DMA_DATA_TYPE_S32,
  353. 0x10, buf_len, OMAP_DMA_SYNC_FRAME,
  354. OMAP24XX_DMA_GPMC, OMAP_DMA_SRC_SYNC);
  355. }
  356. /* configure and start prefetch transfer */
  357. ret = gpmc_prefetch_enable(info->gpmc_cs, 0x1, len, is_write);
  358. if (ret)
  359. /* PFPW engine is busy, use cpu copy methode */
  360. goto out_copy;
  361. init_completion(&info->comp);
  362. omap_start_dma(info->dma_ch);
  363. /* setup and start DMA using dma_addr */
  364. wait_for_completion(&info->comp);
  365. do {
  366. prefetch_status = gpmc_read_status(GPMC_PREFETCH_COUNT);
  367. } while (prefetch_status);
  368. /* disable and stop the PFPW engine */
  369. gpmc_prefetch_reset();
  370. dma_unmap_single(&info->pdev->dev, dma_addr, len, dir);
  371. return 0;
  372. out_copy:
  373. if (info->nand.options & NAND_BUSWIDTH_16)
  374. is_write == 0 ? omap_read_buf16(mtd, (u_char *) addr, len)
  375. : omap_write_buf16(mtd, (u_char *) addr, len);
  376. else
  377. is_write == 0 ? omap_read_buf8(mtd, (u_char *) addr, len)
  378. : omap_write_buf8(mtd, (u_char *) addr, len);
  379. return 0;
  380. }
  381. #else
  382. static void omap_nand_dma_cb(int lch, u16 ch_status, void *data) {}
  383. static inline int omap_nand_dma_transfer(struct mtd_info *mtd, void *addr,
  384. unsigned int len, int is_write)
  385. {
  386. return 0;
  387. }
  388. #endif
  389. /**
  390. * omap_read_buf_dma_pref - read data from NAND controller into buffer
  391. * @mtd: MTD device structure
  392. * @buf: buffer to store date
  393. * @len: number of bytes to read
  394. */
  395. static void omap_read_buf_dma_pref(struct mtd_info *mtd, u_char *buf, int len)
  396. {
  397. if (len <= mtd->oobsize)
  398. omap_read_buf_pref(mtd, buf, len);
  399. else
  400. /* start transfer in DMA mode */
  401. omap_nand_dma_transfer(mtd, buf, len, 0x0);
  402. }
  403. /**
  404. * omap_write_buf_dma_pref - write buffer to NAND controller
  405. * @mtd: MTD device structure
  406. * @buf: data buffer
  407. * @len: number of bytes to write
  408. */
  409. static void omap_write_buf_dma_pref(struct mtd_info *mtd,
  410. const u_char *buf, int len)
  411. {
  412. if (len <= mtd->oobsize)
  413. omap_write_buf_pref(mtd, buf, len);
  414. else
  415. /* start transfer in DMA mode */
  416. omap_nand_dma_transfer(mtd, (u_char *) buf, len, 0x1);
  417. }
  418. /**
  419. * omap_verify_buf - Verify chip data against buffer
  420. * @mtd: MTD device structure
  421. * @buf: buffer containing the data to compare
  422. * @len: number of bytes to compare
  423. */
  424. static int omap_verify_buf(struct mtd_info *mtd, const u_char * buf, int len)
  425. {
  426. struct omap_nand_info *info = container_of(mtd, struct omap_nand_info,
  427. mtd);
  428. u16 *p = (u16 *) buf;
  429. len >>= 1;
  430. while (len--) {
  431. if (*p++ != cpu_to_le16(readw(info->nand.IO_ADDR_R)))
  432. return -EFAULT;
  433. }
  434. return 0;
  435. }
  436. #ifdef CONFIG_MTD_NAND_OMAP_HWECC
  437. /**
  438. * gen_true_ecc - This function will generate true ECC value
  439. * @ecc_buf: buffer to store ecc code
  440. *
  441. * This generated true ECC value can be used when correcting
  442. * data read from NAND flash memory core
  443. */
  444. static void gen_true_ecc(u8 *ecc_buf)
  445. {
  446. u32 tmp = ecc_buf[0] | (ecc_buf[1] << 16) |
  447. ((ecc_buf[2] & 0xF0) << 20) | ((ecc_buf[2] & 0x0F) << 8);
  448. ecc_buf[0] = ~(P64o(tmp) | P64e(tmp) | P32o(tmp) | P32e(tmp) |
  449. P16o(tmp) | P16e(tmp) | P8o(tmp) | P8e(tmp));
  450. ecc_buf[1] = ~(P1024o(tmp) | P1024e(tmp) | P512o(tmp) | P512e(tmp) |
  451. P256o(tmp) | P256e(tmp) | P128o(tmp) | P128e(tmp));
  452. ecc_buf[2] = ~(P4o(tmp) | P4e(tmp) | P2o(tmp) | P2e(tmp) | P1o(tmp) |
  453. P1e(tmp) | P2048o(tmp) | P2048e(tmp));
  454. }
  455. /**
  456. * omap_compare_ecc - Detect (2 bits) and correct (1 bit) error in data
  457. * @ecc_data1: ecc code from nand spare area
  458. * @ecc_data2: ecc code from hardware register obtained from hardware ecc
  459. * @page_data: page data
  460. *
  461. * This function compares two ECC's and indicates if there is an error.
  462. * If the error can be corrected it will be corrected to the buffer.
  463. */
  464. static int omap_compare_ecc(u8 *ecc_data1, /* read from NAND memory */
  465. u8 *ecc_data2, /* read from register */
  466. u8 *page_data)
  467. {
  468. uint i;
  469. u8 tmp0_bit[8], tmp1_bit[8], tmp2_bit[8];
  470. u8 comp0_bit[8], comp1_bit[8], comp2_bit[8];
  471. u8 ecc_bit[24];
  472. u8 ecc_sum = 0;
  473. u8 find_bit = 0;
  474. uint find_byte = 0;
  475. int isEccFF;
  476. isEccFF = ((*(u32 *)ecc_data1 & 0xFFFFFF) == 0xFFFFFF);
  477. gen_true_ecc(ecc_data1);
  478. gen_true_ecc(ecc_data2);
  479. for (i = 0; i <= 2; i++) {
  480. *(ecc_data1 + i) = ~(*(ecc_data1 + i));
  481. *(ecc_data2 + i) = ~(*(ecc_data2 + i));
  482. }
  483. for (i = 0; i < 8; i++) {
  484. tmp0_bit[i] = *ecc_data1 % 2;
  485. *ecc_data1 = *ecc_data1 / 2;
  486. }
  487. for (i = 0; i < 8; i++) {
  488. tmp1_bit[i] = *(ecc_data1 + 1) % 2;
  489. *(ecc_data1 + 1) = *(ecc_data1 + 1) / 2;
  490. }
  491. for (i = 0; i < 8; i++) {
  492. tmp2_bit[i] = *(ecc_data1 + 2) % 2;
  493. *(ecc_data1 + 2) = *(ecc_data1 + 2) / 2;
  494. }
  495. for (i = 0; i < 8; i++) {
  496. comp0_bit[i] = *ecc_data2 % 2;
  497. *ecc_data2 = *ecc_data2 / 2;
  498. }
  499. for (i = 0; i < 8; i++) {
  500. comp1_bit[i] = *(ecc_data2 + 1) % 2;
  501. *(ecc_data2 + 1) = *(ecc_data2 + 1) / 2;
  502. }
  503. for (i = 0; i < 8; i++) {
  504. comp2_bit[i] = *(ecc_data2 + 2) % 2;
  505. *(ecc_data2 + 2) = *(ecc_data2 + 2) / 2;
  506. }
  507. for (i = 0; i < 6; i++)
  508. ecc_bit[i] = tmp2_bit[i + 2] ^ comp2_bit[i + 2];
  509. for (i = 0; i < 8; i++)
  510. ecc_bit[i + 6] = tmp0_bit[i] ^ comp0_bit[i];
  511. for (i = 0; i < 8; i++)
  512. ecc_bit[i + 14] = tmp1_bit[i] ^ comp1_bit[i];
  513. ecc_bit[22] = tmp2_bit[0] ^ comp2_bit[0];
  514. ecc_bit[23] = tmp2_bit[1] ^ comp2_bit[1];
  515. for (i = 0; i < 24; i++)
  516. ecc_sum += ecc_bit[i];
  517. switch (ecc_sum) {
  518. case 0:
  519. /* Not reached because this function is not called if
  520. * ECC values are equal
  521. */
  522. return 0;
  523. case 1:
  524. /* Uncorrectable error */
  525. DEBUG(MTD_DEBUG_LEVEL0, "ECC UNCORRECTED_ERROR 1\n");
  526. return -1;
  527. case 11:
  528. /* UN-Correctable error */
  529. DEBUG(MTD_DEBUG_LEVEL0, "ECC UNCORRECTED_ERROR B\n");
  530. return -1;
  531. case 12:
  532. /* Correctable error */
  533. find_byte = (ecc_bit[23] << 8) +
  534. (ecc_bit[21] << 7) +
  535. (ecc_bit[19] << 6) +
  536. (ecc_bit[17] << 5) +
  537. (ecc_bit[15] << 4) +
  538. (ecc_bit[13] << 3) +
  539. (ecc_bit[11] << 2) +
  540. (ecc_bit[9] << 1) +
  541. ecc_bit[7];
  542. find_bit = (ecc_bit[5] << 2) + (ecc_bit[3] << 1) + ecc_bit[1];
  543. DEBUG(MTD_DEBUG_LEVEL0, "Correcting single bit ECC error at "
  544. "offset: %d, bit: %d\n", find_byte, find_bit);
  545. page_data[find_byte] ^= (1 << find_bit);
  546. return 0;
  547. default:
  548. if (isEccFF) {
  549. if (ecc_data2[0] == 0 &&
  550. ecc_data2[1] == 0 &&
  551. ecc_data2[2] == 0)
  552. return 0;
  553. }
  554. DEBUG(MTD_DEBUG_LEVEL0, "UNCORRECTED_ERROR default\n");
  555. return -1;
  556. }
  557. }
  558. /**
  559. * omap_correct_data - Compares the ECC read with HW generated ECC
  560. * @mtd: MTD device structure
  561. * @dat: page data
  562. * @read_ecc: ecc read from nand flash
  563. * @calc_ecc: ecc read from HW ECC registers
  564. *
  565. * Compares the ecc read from nand spare area with ECC registers values
  566. * and if ECC's mismached, it will call 'omap_compare_ecc' for error detection
  567. * and correction.
  568. */
  569. static int omap_correct_data(struct mtd_info *mtd, u_char *dat,
  570. u_char *read_ecc, u_char *calc_ecc)
  571. {
  572. struct omap_nand_info *info = container_of(mtd, struct omap_nand_info,
  573. mtd);
  574. int blockCnt = 0, i = 0, ret = 0;
  575. /* Ex NAND_ECC_HW12_2048 */
  576. if ((info->nand.ecc.mode == NAND_ECC_HW) &&
  577. (info->nand.ecc.size == 2048))
  578. blockCnt = 4;
  579. else
  580. blockCnt = 1;
  581. for (i = 0; i < blockCnt; i++) {
  582. if (memcmp(read_ecc, calc_ecc, 3) != 0) {
  583. ret = omap_compare_ecc(read_ecc, calc_ecc, dat);
  584. if (ret < 0)
  585. return ret;
  586. }
  587. read_ecc += 3;
  588. calc_ecc += 3;
  589. dat += 512;
  590. }
  591. return 0;
  592. }
  593. /**
  594. * omap_calcuate_ecc - Generate non-inverted ECC bytes.
  595. * @mtd: MTD device structure
  596. * @dat: The pointer to data on which ecc is computed
  597. * @ecc_code: The ecc_code buffer
  598. *
  599. * Using noninverted ECC can be considered ugly since writing a blank
  600. * page ie. padding will clear the ECC bytes. This is no problem as long
  601. * nobody is trying to write data on the seemingly unused page. Reading
  602. * an erased page will produce an ECC mismatch between generated and read
  603. * ECC bytes that has to be dealt with separately.
  604. */
  605. static int omap_calculate_ecc(struct mtd_info *mtd, const u_char *dat,
  606. u_char *ecc_code)
  607. {
  608. struct omap_nand_info *info = container_of(mtd, struct omap_nand_info,
  609. mtd);
  610. return gpmc_calculate_ecc(info->gpmc_cs, dat, ecc_code);
  611. }
  612. /**
  613. * omap_enable_hwecc - This function enables the hardware ecc functionality
  614. * @mtd: MTD device structure
  615. * @mode: Read/Write mode
  616. */
  617. static void omap_enable_hwecc(struct mtd_info *mtd, int mode)
  618. {
  619. struct omap_nand_info *info = container_of(mtd, struct omap_nand_info,
  620. mtd);
  621. struct nand_chip *chip = mtd->priv;
  622. unsigned int dev_width = (chip->options & NAND_BUSWIDTH_16) ? 1 : 0;
  623. gpmc_enable_hwecc(info->gpmc_cs, mode, dev_width, info->nand.ecc.size);
  624. }
  625. #endif
  626. /**
  627. * omap_wait - wait until the command is done
  628. * @mtd: MTD device structure
  629. * @chip: NAND Chip structure
  630. *
  631. * Wait function is called during Program and erase operations and
  632. * the way it is called from MTD layer, we should wait till the NAND
  633. * chip is ready after the programming/erase operation has completed.
  634. *
  635. * Erase can take up to 400ms and program up to 20ms according to
  636. * general NAND and SmartMedia specs
  637. */
  638. static int omap_wait(struct mtd_info *mtd, struct nand_chip *chip)
  639. {
  640. struct nand_chip *this = mtd->priv;
  641. struct omap_nand_info *info = container_of(mtd, struct omap_nand_info,
  642. mtd);
  643. unsigned long timeo = jiffies;
  644. int status = NAND_STATUS_FAIL, state = this->state;
  645. if (state == FL_ERASING)
  646. timeo += (HZ * 400) / 1000;
  647. else
  648. timeo += (HZ * 20) / 1000;
  649. gpmc_nand_write(info->gpmc_cs,
  650. GPMC_NAND_COMMAND, (NAND_CMD_STATUS & 0xFF));
  651. while (time_before(jiffies, timeo)) {
  652. status = gpmc_nand_read(info->gpmc_cs, GPMC_NAND_DATA);
  653. if (status & NAND_STATUS_READY)
  654. break;
  655. cond_resched();
  656. }
  657. return status;
  658. }
  659. /**
  660. * omap_dev_ready - calls the platform specific dev_ready function
  661. * @mtd: MTD device structure
  662. */
  663. static int omap_dev_ready(struct mtd_info *mtd)
  664. {
  665. unsigned int val = 0;
  666. struct omap_nand_info *info = container_of(mtd, struct omap_nand_info,
  667. mtd);
  668. val = gpmc_read_status(GPMC_GET_IRQ_STATUS);
  669. if ((val & 0x100) == 0x100) {
  670. /* Clear IRQ Interrupt */
  671. val |= 0x100;
  672. val &= ~(0x0);
  673. gpmc_cs_configure(info->gpmc_cs, GPMC_SET_IRQ_STATUS, val);
  674. } else {
  675. unsigned int cnt = 0;
  676. while (cnt++ < 0x1FF) {
  677. if ((val & 0x100) == 0x100)
  678. return 0;
  679. val = gpmc_read_status(GPMC_GET_IRQ_STATUS);
  680. }
  681. }
  682. return 1;
  683. }
  684. static int __devinit omap_nand_probe(struct platform_device *pdev)
  685. {
  686. struct omap_nand_info *info;
  687. struct omap_nand_platform_data *pdata;
  688. int err;
  689. pdata = pdev->dev.platform_data;
  690. if (pdata == NULL) {
  691. dev_err(&pdev->dev, "platform data missing\n");
  692. return -ENODEV;
  693. }
  694. info = kzalloc(sizeof(struct omap_nand_info), GFP_KERNEL);
  695. if (!info)
  696. return -ENOMEM;
  697. platform_set_drvdata(pdev, info);
  698. spin_lock_init(&info->controller.lock);
  699. init_waitqueue_head(&info->controller.wq);
  700. info->pdev = pdev;
  701. info->gpmc_cs = pdata->cs;
  702. info->phys_base = pdata->phys_base;
  703. info->mtd.priv = &info->nand;
  704. info->mtd.name = dev_name(&pdev->dev);
  705. info->mtd.owner = THIS_MODULE;
  706. info->nand.options |= pdata->devsize ? NAND_BUSWIDTH_16 : 0;
  707. info->nand.options |= NAND_SKIP_BBTSCAN;
  708. /* NAND write protect off */
  709. gpmc_cs_configure(info->gpmc_cs, GPMC_CONFIG_WP, 0);
  710. if (!request_mem_region(info->phys_base, NAND_IO_SIZE,
  711. pdev->dev.driver->name)) {
  712. err = -EBUSY;
  713. goto out_free_info;
  714. }
  715. info->nand.IO_ADDR_R = ioremap(info->phys_base, NAND_IO_SIZE);
  716. if (!info->nand.IO_ADDR_R) {
  717. err = -ENOMEM;
  718. goto out_release_mem_region;
  719. }
  720. info->nand.controller = &info->controller;
  721. info->nand.IO_ADDR_W = info->nand.IO_ADDR_R;
  722. info->nand.cmd_ctrl = omap_hwcontrol;
  723. /*
  724. * If RDY/BSY line is connected to OMAP then use the omap ready
  725. * funcrtion and the generic nand_wait function which reads the status
  726. * register after monitoring the RDY/BSY line.Otherwise use a standard
  727. * chip delay which is slightly more than tR (AC Timing) of the NAND
  728. * device and read status register until you get a failure or success
  729. */
  730. if (pdata->dev_ready) {
  731. info->nand.dev_ready = omap_dev_ready;
  732. info->nand.chip_delay = 0;
  733. } else {
  734. info->nand.waitfunc = omap_wait;
  735. info->nand.chip_delay = 50;
  736. }
  737. if (use_prefetch) {
  738. info->nand.read_buf = omap_read_buf_pref;
  739. info->nand.write_buf = omap_write_buf_pref;
  740. if (use_dma) {
  741. err = omap_request_dma(OMAP24XX_DMA_GPMC, "NAND",
  742. omap_nand_dma_cb, &info->comp, &info->dma_ch);
  743. if (err < 0) {
  744. info->dma_ch = -1;
  745. printk(KERN_WARNING "DMA request failed."
  746. " Non-dma data transfer mode\n");
  747. } else {
  748. omap_set_dma_dest_burst_mode(info->dma_ch,
  749. OMAP_DMA_DATA_BURST_16);
  750. omap_set_dma_src_burst_mode(info->dma_ch,
  751. OMAP_DMA_DATA_BURST_16);
  752. info->nand.read_buf = omap_read_buf_dma_pref;
  753. info->nand.write_buf = omap_write_buf_dma_pref;
  754. }
  755. }
  756. } else {
  757. if (info->nand.options & NAND_BUSWIDTH_16) {
  758. info->nand.read_buf = omap_read_buf16;
  759. info->nand.write_buf = omap_write_buf16;
  760. } else {
  761. info->nand.read_buf = omap_read_buf8;
  762. info->nand.write_buf = omap_write_buf8;
  763. }
  764. }
  765. info->nand.verify_buf = omap_verify_buf;
  766. #ifdef CONFIG_MTD_NAND_OMAP_HWECC
  767. info->nand.ecc.bytes = 3;
  768. info->nand.ecc.size = 512;
  769. info->nand.ecc.calculate = omap_calculate_ecc;
  770. info->nand.ecc.hwctl = omap_enable_hwecc;
  771. info->nand.ecc.correct = omap_correct_data;
  772. info->nand.ecc.mode = NAND_ECC_HW;
  773. #else
  774. info->nand.ecc.mode = NAND_ECC_SOFT;
  775. #endif
  776. /* DIP switches on some boards change between 8 and 16 bit
  777. * bus widths for flash. Try the other width if the first try fails.
  778. */
  779. if (nand_scan(&info->mtd, 1)) {
  780. info->nand.options ^= NAND_BUSWIDTH_16;
  781. if (nand_scan(&info->mtd, 1)) {
  782. err = -ENXIO;
  783. goto out_release_mem_region;
  784. }
  785. }
  786. #ifdef CONFIG_MTD_PARTITIONS
  787. err = parse_mtd_partitions(&info->mtd, part_probes, &info->parts, 0);
  788. if (err > 0)
  789. add_mtd_partitions(&info->mtd, info->parts, err);
  790. else if (pdata->parts)
  791. add_mtd_partitions(&info->mtd, pdata->parts, pdata->nr_parts);
  792. else
  793. #endif
  794. add_mtd_device(&info->mtd);
  795. platform_set_drvdata(pdev, &info->mtd);
  796. return 0;
  797. out_release_mem_region:
  798. release_mem_region(info->phys_base, NAND_IO_SIZE);
  799. out_free_info:
  800. kfree(info);
  801. return err;
  802. }
  803. static int omap_nand_remove(struct platform_device *pdev)
  804. {
  805. struct mtd_info *mtd = platform_get_drvdata(pdev);
  806. struct omap_nand_info *info = container_of(mtd, struct omap_nand_info,
  807. mtd);
  808. platform_set_drvdata(pdev, NULL);
  809. if (use_dma)
  810. omap_free_dma(info->dma_ch);
  811. /* Release NAND device, its internal structures and partitions */
  812. nand_release(&info->mtd);
  813. iounmap(info->nand.IO_ADDR_R);
  814. kfree(&info->mtd);
  815. return 0;
  816. }
  817. static struct platform_driver omap_nand_driver = {
  818. .probe = omap_nand_probe,
  819. .remove = omap_nand_remove,
  820. .driver = {
  821. .name = DRIVER_NAME,
  822. .owner = THIS_MODULE,
  823. },
  824. };
  825. static int __init omap_nand_init(void)
  826. {
  827. printk(KERN_INFO "%s driver initializing\n", DRIVER_NAME);
  828. /* This check is required if driver is being
  829. * loaded run time as a module
  830. */
  831. if ((1 == use_dma) && (0 == use_prefetch)) {
  832. printk(KERN_INFO"Wrong parameters: 'use_dma' can not be 1 "
  833. "without use_prefetch'. Prefetch will not be"
  834. " used in either mode (mpu or dma)\n");
  835. }
  836. return platform_driver_register(&omap_nand_driver);
  837. }
  838. static void __exit omap_nand_exit(void)
  839. {
  840. platform_driver_unregister(&omap_nand_driver);
  841. }
  842. module_init(omap_nand_init);
  843. module_exit(omap_nand_exit);
  844. MODULE_ALIAS(DRIVER_NAME);
  845. MODULE_LICENSE("GPL");
  846. MODULE_DESCRIPTION("Glue layer for NAND flash on TI OMAP boards");