omap2.c 26 KB

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