omap2.c 38 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408
  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/dmaengine.h>
  12. #include <linux/dma-mapping.h>
  13. #include <linux/delay.h>
  14. #include <linux/module.h>
  15. #include <linux/interrupt.h>
  16. #include <linux/jiffies.h>
  17. #include <linux/sched.h>
  18. #include <linux/mtd/mtd.h>
  19. #include <linux/mtd/nand.h>
  20. #include <linux/mtd/partitions.h>
  21. #include <linux/omap-dma.h>
  22. #include <linux/io.h>
  23. #include <linux/slab.h>
  24. #ifdef CONFIG_MTD_NAND_OMAP_BCH
  25. #include <linux/bch.h>
  26. #endif
  27. #include <plat/dma.h>
  28. #include <plat/gpmc.h>
  29. #include <plat/nand.h>
  30. #define DRIVER_NAME "omap2-nand"
  31. #define OMAP_NAND_TIMEOUT_MS 5000
  32. #define NAND_Ecc_P1e (1 << 0)
  33. #define NAND_Ecc_P2e (1 << 1)
  34. #define NAND_Ecc_P4e (1 << 2)
  35. #define NAND_Ecc_P8e (1 << 3)
  36. #define NAND_Ecc_P16e (1 << 4)
  37. #define NAND_Ecc_P32e (1 << 5)
  38. #define NAND_Ecc_P64e (1 << 6)
  39. #define NAND_Ecc_P128e (1 << 7)
  40. #define NAND_Ecc_P256e (1 << 8)
  41. #define NAND_Ecc_P512e (1 << 9)
  42. #define NAND_Ecc_P1024e (1 << 10)
  43. #define NAND_Ecc_P2048e (1 << 11)
  44. #define NAND_Ecc_P1o (1 << 16)
  45. #define NAND_Ecc_P2o (1 << 17)
  46. #define NAND_Ecc_P4o (1 << 18)
  47. #define NAND_Ecc_P8o (1 << 19)
  48. #define NAND_Ecc_P16o (1 << 20)
  49. #define NAND_Ecc_P32o (1 << 21)
  50. #define NAND_Ecc_P64o (1 << 22)
  51. #define NAND_Ecc_P128o (1 << 23)
  52. #define NAND_Ecc_P256o (1 << 24)
  53. #define NAND_Ecc_P512o (1 << 25)
  54. #define NAND_Ecc_P1024o (1 << 26)
  55. #define NAND_Ecc_P2048o (1 << 27)
  56. #define TF(value) (value ? 1 : 0)
  57. #define P2048e(a) (TF(a & NAND_Ecc_P2048e) << 0)
  58. #define P2048o(a) (TF(a & NAND_Ecc_P2048o) << 1)
  59. #define P1e(a) (TF(a & NAND_Ecc_P1e) << 2)
  60. #define P1o(a) (TF(a & NAND_Ecc_P1o) << 3)
  61. #define P2e(a) (TF(a & NAND_Ecc_P2e) << 4)
  62. #define P2o(a) (TF(a & NAND_Ecc_P2o) << 5)
  63. #define P4e(a) (TF(a & NAND_Ecc_P4e) << 6)
  64. #define P4o(a) (TF(a & NAND_Ecc_P4o) << 7)
  65. #define P8e(a) (TF(a & NAND_Ecc_P8e) << 0)
  66. #define P8o(a) (TF(a & NAND_Ecc_P8o) << 1)
  67. #define P16e(a) (TF(a & NAND_Ecc_P16e) << 2)
  68. #define P16o(a) (TF(a & NAND_Ecc_P16o) << 3)
  69. #define P32e(a) (TF(a & NAND_Ecc_P32e) << 4)
  70. #define P32o(a) (TF(a & NAND_Ecc_P32o) << 5)
  71. #define P64e(a) (TF(a & NAND_Ecc_P64e) << 6)
  72. #define P64o(a) (TF(a & NAND_Ecc_P64o) << 7)
  73. #define P128e(a) (TF(a & NAND_Ecc_P128e) << 0)
  74. #define P128o(a) (TF(a & NAND_Ecc_P128o) << 1)
  75. #define P256e(a) (TF(a & NAND_Ecc_P256e) << 2)
  76. #define P256o(a) (TF(a & NAND_Ecc_P256o) << 3)
  77. #define P512e(a) (TF(a & NAND_Ecc_P512e) << 4)
  78. #define P512o(a) (TF(a & NAND_Ecc_P512o) << 5)
  79. #define P1024e(a) (TF(a & NAND_Ecc_P1024e) << 6)
  80. #define P1024o(a) (TF(a & NAND_Ecc_P1024o) << 7)
  81. #define P8e_s(a) (TF(a & NAND_Ecc_P8e) << 0)
  82. #define P8o_s(a) (TF(a & NAND_Ecc_P8o) << 1)
  83. #define P16e_s(a) (TF(a & NAND_Ecc_P16e) << 2)
  84. #define P16o_s(a) (TF(a & NAND_Ecc_P16o) << 3)
  85. #define P1e_s(a) (TF(a & NAND_Ecc_P1e) << 4)
  86. #define P1o_s(a) (TF(a & NAND_Ecc_P1o) << 5)
  87. #define P2e_s(a) (TF(a & NAND_Ecc_P2e) << 6)
  88. #define P2o_s(a) (TF(a & NAND_Ecc_P2o) << 7)
  89. #define P4e_s(a) (TF(a & NAND_Ecc_P4e) << 0)
  90. #define P4o_s(a) (TF(a & NAND_Ecc_P4o) << 1)
  91. /* oob info generated runtime depending on ecc algorithm and layout selected */
  92. static struct nand_ecclayout omap_oobinfo;
  93. /* Define some generic bad / good block scan pattern which are used
  94. * while scanning a device for factory marked good / bad blocks
  95. */
  96. static uint8_t scan_ff_pattern[] = { 0xff };
  97. static struct nand_bbt_descr bb_descrip_flashbased = {
  98. .options = NAND_BBT_SCANEMPTY | NAND_BBT_SCANALLPAGES,
  99. .offs = 0,
  100. .len = 1,
  101. .pattern = scan_ff_pattern,
  102. };
  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 nand_chip nand;
  108. struct platform_device *pdev;
  109. int gpmc_cs;
  110. unsigned long phys_base;
  111. struct completion comp;
  112. struct dma_chan *dma;
  113. int gpmc_irq;
  114. enum {
  115. OMAP_NAND_IO_READ = 0, /* read */
  116. OMAP_NAND_IO_WRITE, /* write */
  117. } iomode;
  118. u_char *buf;
  119. int buf_len;
  120. #ifdef CONFIG_MTD_NAND_OMAP_BCH
  121. struct bch_control *bch;
  122. struct nand_ecclayout ecclayout;
  123. #endif
  124. };
  125. /**
  126. * omap_hwcontrol - hardware specific access to control-lines
  127. * @mtd: MTD device structure
  128. * @cmd: command to device
  129. * @ctrl:
  130. * NAND_NCE: bit 0 -> don't care
  131. * NAND_CLE: bit 1 -> Command Latch
  132. * NAND_ALE: bit 2 -> Address Latch
  133. *
  134. * NOTE: boards may use different bits for these!!
  135. */
  136. static void omap_hwcontrol(struct mtd_info *mtd, int cmd, unsigned int ctrl)
  137. {
  138. struct omap_nand_info *info = container_of(mtd,
  139. struct omap_nand_info, mtd);
  140. if (cmd != NAND_CMD_NONE) {
  141. if (ctrl & NAND_CLE)
  142. gpmc_nand_write(info->gpmc_cs, GPMC_NAND_COMMAND, cmd);
  143. else if (ctrl & NAND_ALE)
  144. gpmc_nand_write(info->gpmc_cs, GPMC_NAND_ADDRESS, cmd);
  145. else /* NAND_NCE */
  146. gpmc_nand_write(info->gpmc_cs, GPMC_NAND_DATA, cmd);
  147. }
  148. }
  149. /**
  150. * omap_read_buf8 - read data from NAND controller into buffer
  151. * @mtd: MTD device structure
  152. * @buf: buffer to store date
  153. * @len: number of bytes to read
  154. */
  155. static void omap_read_buf8(struct mtd_info *mtd, u_char *buf, int len)
  156. {
  157. struct nand_chip *nand = mtd->priv;
  158. ioread8_rep(nand->IO_ADDR_R, buf, len);
  159. }
  160. /**
  161. * omap_write_buf8 - write buffer to NAND controller
  162. * @mtd: MTD device structure
  163. * @buf: data buffer
  164. * @len: number of bytes to write
  165. */
  166. static void omap_write_buf8(struct mtd_info *mtd, const u_char *buf, int len)
  167. {
  168. struct omap_nand_info *info = container_of(mtd,
  169. struct omap_nand_info, mtd);
  170. u_char *p = (u_char *)buf;
  171. u32 status = 0;
  172. while (len--) {
  173. iowrite8(*p++, info->nand.IO_ADDR_W);
  174. /* wait until buffer is available for write */
  175. do {
  176. status = gpmc_read_status(GPMC_STATUS_BUFFER);
  177. } while (!status);
  178. }
  179. }
  180. /**
  181. * omap_read_buf16 - read data from NAND controller into buffer
  182. * @mtd: MTD device structure
  183. * @buf: buffer to store date
  184. * @len: number of bytes to read
  185. */
  186. static void omap_read_buf16(struct mtd_info *mtd, u_char *buf, int len)
  187. {
  188. struct nand_chip *nand = mtd->priv;
  189. ioread16_rep(nand->IO_ADDR_R, buf, len / 2);
  190. }
  191. /**
  192. * omap_write_buf16 - write buffer to NAND controller
  193. * @mtd: MTD device structure
  194. * @buf: data buffer
  195. * @len: number of bytes to write
  196. */
  197. static void omap_write_buf16(struct mtd_info *mtd, const u_char * buf, int len)
  198. {
  199. struct omap_nand_info *info = container_of(mtd,
  200. struct omap_nand_info, mtd);
  201. u16 *p = (u16 *) buf;
  202. u32 status = 0;
  203. /* FIXME try bursts of writesw() or DMA ... */
  204. len >>= 1;
  205. while (len--) {
  206. iowrite16(*p++, info->nand.IO_ADDR_W);
  207. /* wait until buffer is available for write */
  208. do {
  209. status = gpmc_read_status(GPMC_STATUS_BUFFER);
  210. } while (!status);
  211. }
  212. }
  213. /**
  214. * omap_read_buf_pref - read data from NAND controller into buffer
  215. * @mtd: MTD device structure
  216. * @buf: buffer to store date
  217. * @len: number of bytes to read
  218. */
  219. static void omap_read_buf_pref(struct mtd_info *mtd, u_char *buf, int len)
  220. {
  221. struct omap_nand_info *info = container_of(mtd,
  222. struct omap_nand_info, mtd);
  223. uint32_t r_count = 0;
  224. int ret = 0;
  225. u32 *p = (u32 *)buf;
  226. /* take care of subpage reads */
  227. if (len % 4) {
  228. if (info->nand.options & NAND_BUSWIDTH_16)
  229. omap_read_buf16(mtd, buf, len % 4);
  230. else
  231. omap_read_buf8(mtd, buf, len % 4);
  232. p = (u32 *) (buf + len % 4);
  233. len -= len % 4;
  234. }
  235. /* configure and start prefetch transfer */
  236. ret = gpmc_prefetch_enable(info->gpmc_cs,
  237. PREFETCH_FIFOTHRESHOLD_MAX, 0x0, len, 0x0);
  238. if (ret) {
  239. /* PFPW engine is busy, use cpu copy method */
  240. if (info->nand.options & NAND_BUSWIDTH_16)
  241. omap_read_buf16(mtd, (u_char *)p, len);
  242. else
  243. omap_read_buf8(mtd, (u_char *)p, len);
  244. } else {
  245. do {
  246. r_count = gpmc_read_status(GPMC_PREFETCH_FIFO_CNT);
  247. r_count = r_count >> 2;
  248. ioread32_rep(info->nand.IO_ADDR_R, p, r_count);
  249. p += r_count;
  250. len -= r_count << 2;
  251. } while (len);
  252. /* disable and stop the PFPW engine */
  253. gpmc_prefetch_reset(info->gpmc_cs);
  254. }
  255. }
  256. /**
  257. * omap_write_buf_pref - write buffer to NAND controller
  258. * @mtd: MTD device structure
  259. * @buf: data buffer
  260. * @len: number of bytes to write
  261. */
  262. static void omap_write_buf_pref(struct mtd_info *mtd,
  263. const u_char *buf, int len)
  264. {
  265. struct omap_nand_info *info = container_of(mtd,
  266. struct omap_nand_info, mtd);
  267. uint32_t w_count = 0;
  268. int i = 0, ret = 0;
  269. u16 *p = (u16 *)buf;
  270. unsigned long tim, limit;
  271. /* take care of subpage writes */
  272. if (len % 2 != 0) {
  273. writeb(*buf, info->nand.IO_ADDR_W);
  274. p = (u16 *)(buf + 1);
  275. len--;
  276. }
  277. /* configure and start prefetch transfer */
  278. ret = gpmc_prefetch_enable(info->gpmc_cs,
  279. PREFETCH_FIFOTHRESHOLD_MAX, 0x0, len, 0x1);
  280. if (ret) {
  281. /* PFPW engine is busy, use cpu copy method */
  282. if (info->nand.options & NAND_BUSWIDTH_16)
  283. omap_write_buf16(mtd, (u_char *)p, len);
  284. else
  285. omap_write_buf8(mtd, (u_char *)p, len);
  286. } else {
  287. while (len) {
  288. w_count = gpmc_read_status(GPMC_PREFETCH_FIFO_CNT);
  289. w_count = w_count >> 1;
  290. for (i = 0; (i < w_count) && len; i++, len -= 2)
  291. iowrite16(*p++, info->nand.IO_ADDR_W);
  292. }
  293. /* wait for data to flushed-out before reset the prefetch */
  294. tim = 0;
  295. limit = (loops_per_jiffy *
  296. msecs_to_jiffies(OMAP_NAND_TIMEOUT_MS));
  297. while (gpmc_read_status(GPMC_PREFETCH_COUNT) && (tim++ < limit))
  298. cpu_relax();
  299. /* disable and stop the PFPW engine */
  300. gpmc_prefetch_reset(info->gpmc_cs);
  301. }
  302. }
  303. /*
  304. * omap_nand_dma_callback: callback on the completion of dma transfer
  305. * @data: pointer to completion data structure
  306. */
  307. static void omap_nand_dma_callback(void *data)
  308. {
  309. complete((struct completion *) data);
  310. }
  311. /*
  312. * omap_nand_dma_transfer: configer and start dma transfer
  313. * @mtd: MTD device structure
  314. * @addr: virtual address in RAM of source/destination
  315. * @len: number of data bytes to be transferred
  316. * @is_write: flag for read/write operation
  317. */
  318. static inline int omap_nand_dma_transfer(struct mtd_info *mtd, void *addr,
  319. unsigned int len, int is_write)
  320. {
  321. struct omap_nand_info *info = container_of(mtd,
  322. struct omap_nand_info, mtd);
  323. struct dma_async_tx_descriptor *tx;
  324. enum dma_data_direction dir = is_write ? DMA_TO_DEVICE :
  325. DMA_FROM_DEVICE;
  326. struct scatterlist sg;
  327. unsigned long tim, limit;
  328. unsigned n;
  329. int ret;
  330. if (addr >= high_memory) {
  331. struct page *p1;
  332. if (((size_t)addr & PAGE_MASK) !=
  333. ((size_t)(addr + len - 1) & PAGE_MASK))
  334. goto out_copy;
  335. p1 = vmalloc_to_page(addr);
  336. if (!p1)
  337. goto out_copy;
  338. addr = page_address(p1) + ((size_t)addr & ~PAGE_MASK);
  339. }
  340. sg_init_one(&sg, addr, len);
  341. n = dma_map_sg(info->dma->device->dev, &sg, 1, dir);
  342. if (n == 0) {
  343. dev_err(&info->pdev->dev,
  344. "Couldn't DMA map a %d byte buffer\n", len);
  345. goto out_copy;
  346. }
  347. tx = dmaengine_prep_slave_sg(info->dma, &sg, n,
  348. is_write ? DMA_MEM_TO_DEV : DMA_DEV_TO_MEM,
  349. DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
  350. if (!tx)
  351. goto out_copy_unmap;
  352. tx->callback = omap_nand_dma_callback;
  353. tx->callback_param = &info->comp;
  354. dmaengine_submit(tx);
  355. /* configure and start prefetch transfer */
  356. ret = gpmc_prefetch_enable(info->gpmc_cs,
  357. PREFETCH_FIFOTHRESHOLD_MAX, 0x1, len, is_write);
  358. if (ret)
  359. /* PFPW engine is busy, use cpu copy method */
  360. goto out_copy_unmap;
  361. init_completion(&info->comp);
  362. dma_async_issue_pending(info->dma);
  363. /* setup and start DMA using dma_addr */
  364. wait_for_completion(&info->comp);
  365. tim = 0;
  366. limit = (loops_per_jiffy * msecs_to_jiffies(OMAP_NAND_TIMEOUT_MS));
  367. while (gpmc_read_status(GPMC_PREFETCH_COUNT) && (tim++ < limit))
  368. cpu_relax();
  369. /* disable and stop the PFPW engine */
  370. gpmc_prefetch_reset(info->gpmc_cs);
  371. dma_unmap_sg(info->dma->device->dev, &sg, 1, dir);
  372. return 0;
  373. out_copy_unmap:
  374. dma_unmap_sg(info->dma->device->dev, &sg, 1, dir);
  375. out_copy:
  376. if (info->nand.options & NAND_BUSWIDTH_16)
  377. is_write == 0 ? omap_read_buf16(mtd, (u_char *) addr, len)
  378. : omap_write_buf16(mtd, (u_char *) addr, len);
  379. else
  380. is_write == 0 ? omap_read_buf8(mtd, (u_char *) addr, len)
  381. : omap_write_buf8(mtd, (u_char *) addr, len);
  382. return 0;
  383. }
  384. /**
  385. * omap_read_buf_dma_pref - read data from NAND controller into buffer
  386. * @mtd: MTD device structure
  387. * @buf: buffer to store date
  388. * @len: number of bytes to read
  389. */
  390. static void omap_read_buf_dma_pref(struct mtd_info *mtd, u_char *buf, int len)
  391. {
  392. if (len <= mtd->oobsize)
  393. omap_read_buf_pref(mtd, buf, len);
  394. else
  395. /* start transfer in DMA mode */
  396. omap_nand_dma_transfer(mtd, buf, len, 0x0);
  397. }
  398. /**
  399. * omap_write_buf_dma_pref - write buffer to NAND controller
  400. * @mtd: MTD device structure
  401. * @buf: data buffer
  402. * @len: number of bytes to write
  403. */
  404. static void omap_write_buf_dma_pref(struct mtd_info *mtd,
  405. const u_char *buf, int len)
  406. {
  407. if (len <= mtd->oobsize)
  408. omap_write_buf_pref(mtd, buf, len);
  409. else
  410. /* start transfer in DMA mode */
  411. omap_nand_dma_transfer(mtd, (u_char *) buf, len, 0x1);
  412. }
  413. /*
  414. * omap_nand_irq - GMPC irq handler
  415. * @this_irq: gpmc irq number
  416. * @dev: omap_nand_info structure pointer is passed here
  417. */
  418. static irqreturn_t omap_nand_irq(int this_irq, void *dev)
  419. {
  420. struct omap_nand_info *info = (struct omap_nand_info *) dev;
  421. u32 bytes;
  422. u32 irq_stat;
  423. irq_stat = gpmc_read_status(GPMC_GET_IRQ_STATUS);
  424. bytes = gpmc_read_status(GPMC_PREFETCH_FIFO_CNT);
  425. bytes = bytes & 0xFFFC; /* io in multiple of 4 bytes */
  426. if (info->iomode == OMAP_NAND_IO_WRITE) { /* checks for write io */
  427. if (irq_stat & 0x2)
  428. goto done;
  429. if (info->buf_len && (info->buf_len < bytes))
  430. bytes = info->buf_len;
  431. else if (!info->buf_len)
  432. bytes = 0;
  433. iowrite32_rep(info->nand.IO_ADDR_W,
  434. (u32 *)info->buf, bytes >> 2);
  435. info->buf = info->buf + bytes;
  436. info->buf_len -= bytes;
  437. } else {
  438. ioread32_rep(info->nand.IO_ADDR_R,
  439. (u32 *)info->buf, bytes >> 2);
  440. info->buf = info->buf + bytes;
  441. if (irq_stat & 0x2)
  442. goto done;
  443. }
  444. gpmc_cs_configure(info->gpmc_cs, GPMC_SET_IRQ_STATUS, irq_stat);
  445. return IRQ_HANDLED;
  446. done:
  447. complete(&info->comp);
  448. /* disable irq */
  449. gpmc_cs_configure(info->gpmc_cs, GPMC_ENABLE_IRQ, 0);
  450. /* clear status */
  451. gpmc_cs_configure(info->gpmc_cs, GPMC_SET_IRQ_STATUS, irq_stat);
  452. return IRQ_HANDLED;
  453. }
  454. /*
  455. * omap_read_buf_irq_pref - read data from NAND controller into buffer
  456. * @mtd: MTD device structure
  457. * @buf: buffer to store date
  458. * @len: number of bytes to read
  459. */
  460. static void omap_read_buf_irq_pref(struct mtd_info *mtd, u_char *buf, int len)
  461. {
  462. struct omap_nand_info *info = container_of(mtd,
  463. struct omap_nand_info, mtd);
  464. int ret = 0;
  465. if (len <= mtd->oobsize) {
  466. omap_read_buf_pref(mtd, buf, len);
  467. return;
  468. }
  469. info->iomode = OMAP_NAND_IO_READ;
  470. info->buf = buf;
  471. init_completion(&info->comp);
  472. /* configure and start prefetch transfer */
  473. ret = gpmc_prefetch_enable(info->gpmc_cs,
  474. PREFETCH_FIFOTHRESHOLD_MAX/2, 0x0, len, 0x0);
  475. if (ret)
  476. /* PFPW engine is busy, use cpu copy method */
  477. goto out_copy;
  478. info->buf_len = len;
  479. /* enable irq */
  480. gpmc_cs_configure(info->gpmc_cs, GPMC_ENABLE_IRQ,
  481. (GPMC_IRQ_FIFOEVENTENABLE | GPMC_IRQ_COUNT_EVENT));
  482. /* waiting for read to complete */
  483. wait_for_completion(&info->comp);
  484. /* disable and stop the PFPW engine */
  485. gpmc_prefetch_reset(info->gpmc_cs);
  486. return;
  487. out_copy:
  488. if (info->nand.options & NAND_BUSWIDTH_16)
  489. omap_read_buf16(mtd, buf, len);
  490. else
  491. omap_read_buf8(mtd, buf, len);
  492. }
  493. /*
  494. * omap_write_buf_irq_pref - write buffer to NAND controller
  495. * @mtd: MTD device structure
  496. * @buf: data buffer
  497. * @len: number of bytes to write
  498. */
  499. static void omap_write_buf_irq_pref(struct mtd_info *mtd,
  500. const u_char *buf, int len)
  501. {
  502. struct omap_nand_info *info = container_of(mtd,
  503. struct omap_nand_info, mtd);
  504. int ret = 0;
  505. unsigned long tim, limit;
  506. if (len <= mtd->oobsize) {
  507. omap_write_buf_pref(mtd, buf, len);
  508. return;
  509. }
  510. info->iomode = OMAP_NAND_IO_WRITE;
  511. info->buf = (u_char *) buf;
  512. init_completion(&info->comp);
  513. /* configure and start prefetch transfer : size=24 */
  514. ret = gpmc_prefetch_enable(info->gpmc_cs,
  515. (PREFETCH_FIFOTHRESHOLD_MAX * 3) / 8, 0x0, len, 0x1);
  516. if (ret)
  517. /* PFPW engine is busy, use cpu copy method */
  518. goto out_copy;
  519. info->buf_len = len;
  520. /* enable irq */
  521. gpmc_cs_configure(info->gpmc_cs, GPMC_ENABLE_IRQ,
  522. (GPMC_IRQ_FIFOEVENTENABLE | GPMC_IRQ_COUNT_EVENT));
  523. /* waiting for write to complete */
  524. wait_for_completion(&info->comp);
  525. /* wait for data to flushed-out before reset the prefetch */
  526. tim = 0;
  527. limit = (loops_per_jiffy * msecs_to_jiffies(OMAP_NAND_TIMEOUT_MS));
  528. while (gpmc_read_status(GPMC_PREFETCH_COUNT) && (tim++ < limit))
  529. cpu_relax();
  530. /* disable and stop the PFPW engine */
  531. gpmc_prefetch_reset(info->gpmc_cs);
  532. return;
  533. out_copy:
  534. if (info->nand.options & NAND_BUSWIDTH_16)
  535. omap_write_buf16(mtd, buf, len);
  536. else
  537. omap_write_buf8(mtd, buf, len);
  538. }
  539. /**
  540. * omap_verify_buf - Verify chip data against buffer
  541. * @mtd: MTD device structure
  542. * @buf: buffer containing the data to compare
  543. * @len: number of bytes to compare
  544. */
  545. static int omap_verify_buf(struct mtd_info *mtd, const u_char * buf, int len)
  546. {
  547. struct omap_nand_info *info = container_of(mtd, struct omap_nand_info,
  548. mtd);
  549. u16 *p = (u16 *) buf;
  550. len >>= 1;
  551. while (len--) {
  552. if (*p++ != cpu_to_le16(readw(info->nand.IO_ADDR_R)))
  553. return -EFAULT;
  554. }
  555. return 0;
  556. }
  557. /**
  558. * gen_true_ecc - This function will generate true ECC value
  559. * @ecc_buf: buffer to store ecc code
  560. *
  561. * This generated true ECC value can be used when correcting
  562. * data read from NAND flash memory core
  563. */
  564. static void gen_true_ecc(u8 *ecc_buf)
  565. {
  566. u32 tmp = ecc_buf[0] | (ecc_buf[1] << 16) |
  567. ((ecc_buf[2] & 0xF0) << 20) | ((ecc_buf[2] & 0x0F) << 8);
  568. ecc_buf[0] = ~(P64o(tmp) | P64e(tmp) | P32o(tmp) | P32e(tmp) |
  569. P16o(tmp) | P16e(tmp) | P8o(tmp) | P8e(tmp));
  570. ecc_buf[1] = ~(P1024o(tmp) | P1024e(tmp) | P512o(tmp) | P512e(tmp) |
  571. P256o(tmp) | P256e(tmp) | P128o(tmp) | P128e(tmp));
  572. ecc_buf[2] = ~(P4o(tmp) | P4e(tmp) | P2o(tmp) | P2e(tmp) | P1o(tmp) |
  573. P1e(tmp) | P2048o(tmp) | P2048e(tmp));
  574. }
  575. /**
  576. * omap_compare_ecc - Detect (2 bits) and correct (1 bit) error in data
  577. * @ecc_data1: ecc code from nand spare area
  578. * @ecc_data2: ecc code from hardware register obtained from hardware ecc
  579. * @page_data: page data
  580. *
  581. * This function compares two ECC's and indicates if there is an error.
  582. * If the error can be corrected it will be corrected to the buffer.
  583. * If there is no error, %0 is returned. If there is an error but it
  584. * was corrected, %1 is returned. Otherwise, %-1 is returned.
  585. */
  586. static int omap_compare_ecc(u8 *ecc_data1, /* read from NAND memory */
  587. u8 *ecc_data2, /* read from register */
  588. u8 *page_data)
  589. {
  590. uint i;
  591. u8 tmp0_bit[8], tmp1_bit[8], tmp2_bit[8];
  592. u8 comp0_bit[8], comp1_bit[8], comp2_bit[8];
  593. u8 ecc_bit[24];
  594. u8 ecc_sum = 0;
  595. u8 find_bit = 0;
  596. uint find_byte = 0;
  597. int isEccFF;
  598. isEccFF = ((*(u32 *)ecc_data1 & 0xFFFFFF) == 0xFFFFFF);
  599. gen_true_ecc(ecc_data1);
  600. gen_true_ecc(ecc_data2);
  601. for (i = 0; i <= 2; i++) {
  602. *(ecc_data1 + i) = ~(*(ecc_data1 + i));
  603. *(ecc_data2 + i) = ~(*(ecc_data2 + i));
  604. }
  605. for (i = 0; i < 8; i++) {
  606. tmp0_bit[i] = *ecc_data1 % 2;
  607. *ecc_data1 = *ecc_data1 / 2;
  608. }
  609. for (i = 0; i < 8; i++) {
  610. tmp1_bit[i] = *(ecc_data1 + 1) % 2;
  611. *(ecc_data1 + 1) = *(ecc_data1 + 1) / 2;
  612. }
  613. for (i = 0; i < 8; i++) {
  614. tmp2_bit[i] = *(ecc_data1 + 2) % 2;
  615. *(ecc_data1 + 2) = *(ecc_data1 + 2) / 2;
  616. }
  617. for (i = 0; i < 8; i++) {
  618. comp0_bit[i] = *ecc_data2 % 2;
  619. *ecc_data2 = *ecc_data2 / 2;
  620. }
  621. for (i = 0; i < 8; i++) {
  622. comp1_bit[i] = *(ecc_data2 + 1) % 2;
  623. *(ecc_data2 + 1) = *(ecc_data2 + 1) / 2;
  624. }
  625. for (i = 0; i < 8; i++) {
  626. comp2_bit[i] = *(ecc_data2 + 2) % 2;
  627. *(ecc_data2 + 2) = *(ecc_data2 + 2) / 2;
  628. }
  629. for (i = 0; i < 6; i++)
  630. ecc_bit[i] = tmp2_bit[i + 2] ^ comp2_bit[i + 2];
  631. for (i = 0; i < 8; i++)
  632. ecc_bit[i + 6] = tmp0_bit[i] ^ comp0_bit[i];
  633. for (i = 0; i < 8; i++)
  634. ecc_bit[i + 14] = tmp1_bit[i] ^ comp1_bit[i];
  635. ecc_bit[22] = tmp2_bit[0] ^ comp2_bit[0];
  636. ecc_bit[23] = tmp2_bit[1] ^ comp2_bit[1];
  637. for (i = 0; i < 24; i++)
  638. ecc_sum += ecc_bit[i];
  639. switch (ecc_sum) {
  640. case 0:
  641. /* Not reached because this function is not called if
  642. * ECC values are equal
  643. */
  644. return 0;
  645. case 1:
  646. /* Uncorrectable error */
  647. pr_debug("ECC UNCORRECTED_ERROR 1\n");
  648. return -1;
  649. case 11:
  650. /* UN-Correctable error */
  651. pr_debug("ECC UNCORRECTED_ERROR B\n");
  652. return -1;
  653. case 12:
  654. /* Correctable error */
  655. find_byte = (ecc_bit[23] << 8) +
  656. (ecc_bit[21] << 7) +
  657. (ecc_bit[19] << 6) +
  658. (ecc_bit[17] << 5) +
  659. (ecc_bit[15] << 4) +
  660. (ecc_bit[13] << 3) +
  661. (ecc_bit[11] << 2) +
  662. (ecc_bit[9] << 1) +
  663. ecc_bit[7];
  664. find_bit = (ecc_bit[5] << 2) + (ecc_bit[3] << 1) + ecc_bit[1];
  665. pr_debug("Correcting single bit ECC error at offset: "
  666. "%d, bit: %d\n", find_byte, find_bit);
  667. page_data[find_byte] ^= (1 << find_bit);
  668. return 1;
  669. default:
  670. if (isEccFF) {
  671. if (ecc_data2[0] == 0 &&
  672. ecc_data2[1] == 0 &&
  673. ecc_data2[2] == 0)
  674. return 0;
  675. }
  676. pr_debug("UNCORRECTED_ERROR default\n");
  677. return -1;
  678. }
  679. }
  680. /**
  681. * omap_correct_data - Compares the ECC read with HW generated ECC
  682. * @mtd: MTD device structure
  683. * @dat: page data
  684. * @read_ecc: ecc read from nand flash
  685. * @calc_ecc: ecc read from HW ECC registers
  686. *
  687. * Compares the ecc read from nand spare area with ECC registers values
  688. * and if ECC's mismatched, it will call 'omap_compare_ecc' for error
  689. * detection and correction. If there are no errors, %0 is returned. If
  690. * there were errors and all of the errors were corrected, the number of
  691. * corrected errors is returned. If uncorrectable errors exist, %-1 is
  692. * returned.
  693. */
  694. static int omap_correct_data(struct mtd_info *mtd, u_char *dat,
  695. u_char *read_ecc, u_char *calc_ecc)
  696. {
  697. struct omap_nand_info *info = container_of(mtd, struct omap_nand_info,
  698. mtd);
  699. int blockCnt = 0, i = 0, ret = 0;
  700. int stat = 0;
  701. /* Ex NAND_ECC_HW12_2048 */
  702. if ((info->nand.ecc.mode == NAND_ECC_HW) &&
  703. (info->nand.ecc.size == 2048))
  704. blockCnt = 4;
  705. else
  706. blockCnt = 1;
  707. for (i = 0; i < blockCnt; i++) {
  708. if (memcmp(read_ecc, calc_ecc, 3) != 0) {
  709. ret = omap_compare_ecc(read_ecc, calc_ecc, dat);
  710. if (ret < 0)
  711. return ret;
  712. /* keep track of the number of corrected errors */
  713. stat += ret;
  714. }
  715. read_ecc += 3;
  716. calc_ecc += 3;
  717. dat += 512;
  718. }
  719. return stat;
  720. }
  721. /**
  722. * omap_calcuate_ecc - Generate non-inverted ECC bytes.
  723. * @mtd: MTD device structure
  724. * @dat: The pointer to data on which ecc is computed
  725. * @ecc_code: The ecc_code buffer
  726. *
  727. * Using noninverted ECC can be considered ugly since writing a blank
  728. * page ie. padding will clear the ECC bytes. This is no problem as long
  729. * nobody is trying to write data on the seemingly unused page. Reading
  730. * an erased page will produce an ECC mismatch between generated and read
  731. * ECC bytes that has to be dealt with separately.
  732. */
  733. static int omap_calculate_ecc(struct mtd_info *mtd, const u_char *dat,
  734. u_char *ecc_code)
  735. {
  736. struct omap_nand_info *info = container_of(mtd, struct omap_nand_info,
  737. mtd);
  738. return gpmc_calculate_ecc(info->gpmc_cs, dat, ecc_code);
  739. }
  740. /**
  741. * omap_enable_hwecc - This function enables the hardware ecc functionality
  742. * @mtd: MTD device structure
  743. * @mode: Read/Write mode
  744. */
  745. static void omap_enable_hwecc(struct mtd_info *mtd, int mode)
  746. {
  747. struct omap_nand_info *info = container_of(mtd, struct omap_nand_info,
  748. mtd);
  749. struct nand_chip *chip = mtd->priv;
  750. unsigned int dev_width = (chip->options & NAND_BUSWIDTH_16) ? 1 : 0;
  751. gpmc_enable_hwecc(info->gpmc_cs, mode, dev_width, info->nand.ecc.size);
  752. }
  753. /**
  754. * omap_wait - wait until the command is done
  755. * @mtd: MTD device structure
  756. * @chip: NAND Chip structure
  757. *
  758. * Wait function is called during Program and erase operations and
  759. * the way it is called from MTD layer, we should wait till the NAND
  760. * chip is ready after the programming/erase operation has completed.
  761. *
  762. * Erase can take up to 400ms and program up to 20ms according to
  763. * general NAND and SmartMedia specs
  764. */
  765. static int omap_wait(struct mtd_info *mtd, struct nand_chip *chip)
  766. {
  767. struct nand_chip *this = mtd->priv;
  768. struct omap_nand_info *info = container_of(mtd, struct omap_nand_info,
  769. mtd);
  770. unsigned long timeo = jiffies;
  771. int status, state = this->state;
  772. if (state == FL_ERASING)
  773. timeo += (HZ * 400) / 1000;
  774. else
  775. timeo += (HZ * 20) / 1000;
  776. gpmc_nand_write(info->gpmc_cs,
  777. GPMC_NAND_COMMAND, (NAND_CMD_STATUS & 0xFF));
  778. while (time_before(jiffies, timeo)) {
  779. status = gpmc_nand_read(info->gpmc_cs, GPMC_NAND_DATA);
  780. if (status & NAND_STATUS_READY)
  781. break;
  782. cond_resched();
  783. }
  784. status = gpmc_nand_read(info->gpmc_cs, GPMC_NAND_DATA);
  785. return status;
  786. }
  787. /**
  788. * omap_dev_ready - calls the platform specific dev_ready function
  789. * @mtd: MTD device structure
  790. */
  791. static int omap_dev_ready(struct mtd_info *mtd)
  792. {
  793. unsigned int val = 0;
  794. struct omap_nand_info *info = container_of(mtd, struct omap_nand_info,
  795. mtd);
  796. val = gpmc_read_status(GPMC_GET_IRQ_STATUS);
  797. if ((val & 0x100) == 0x100) {
  798. /* Clear IRQ Interrupt */
  799. val |= 0x100;
  800. val &= ~(0x0);
  801. gpmc_cs_configure(info->gpmc_cs, GPMC_SET_IRQ_STATUS, val);
  802. } else {
  803. unsigned int cnt = 0;
  804. while (cnt++ < 0x1FF) {
  805. if ((val & 0x100) == 0x100)
  806. return 0;
  807. val = gpmc_read_status(GPMC_GET_IRQ_STATUS);
  808. }
  809. }
  810. return 1;
  811. }
  812. #ifdef CONFIG_MTD_NAND_OMAP_BCH
  813. /**
  814. * omap3_enable_hwecc_bch - Program OMAP3 GPMC to perform BCH ECC correction
  815. * @mtd: MTD device structure
  816. * @mode: Read/Write mode
  817. */
  818. static void omap3_enable_hwecc_bch(struct mtd_info *mtd, int mode)
  819. {
  820. int nerrors;
  821. unsigned int dev_width;
  822. struct omap_nand_info *info = container_of(mtd, struct omap_nand_info,
  823. mtd);
  824. struct nand_chip *chip = mtd->priv;
  825. nerrors = (info->nand.ecc.bytes == 13) ? 8 : 4;
  826. dev_width = (chip->options & NAND_BUSWIDTH_16) ? 1 : 0;
  827. /*
  828. * Program GPMC to perform correction on one 512-byte sector at a time.
  829. * Using 4 sectors at a time (i.e. ecc.size = 2048) is also possible and
  830. * gives a slight (5%) performance gain (but requires additional code).
  831. */
  832. (void)gpmc_enable_hwecc_bch(info->gpmc_cs, mode, dev_width, 1, nerrors);
  833. }
  834. /**
  835. * omap3_calculate_ecc_bch4 - Generate 7 bytes of ECC bytes
  836. * @mtd: MTD device structure
  837. * @dat: The pointer to data on which ecc is computed
  838. * @ecc_code: The ecc_code buffer
  839. */
  840. static int omap3_calculate_ecc_bch4(struct mtd_info *mtd, const u_char *dat,
  841. u_char *ecc_code)
  842. {
  843. struct omap_nand_info *info = container_of(mtd, struct omap_nand_info,
  844. mtd);
  845. return gpmc_calculate_ecc_bch4(info->gpmc_cs, dat, ecc_code);
  846. }
  847. /**
  848. * omap3_calculate_ecc_bch8 - Generate 13 bytes of ECC bytes
  849. * @mtd: MTD device structure
  850. * @dat: The pointer to data on which ecc is computed
  851. * @ecc_code: The ecc_code buffer
  852. */
  853. static int omap3_calculate_ecc_bch8(struct mtd_info *mtd, const u_char *dat,
  854. u_char *ecc_code)
  855. {
  856. struct omap_nand_info *info = container_of(mtd, struct omap_nand_info,
  857. mtd);
  858. return gpmc_calculate_ecc_bch8(info->gpmc_cs, dat, ecc_code);
  859. }
  860. /**
  861. * omap3_correct_data_bch - Decode received data and correct errors
  862. * @mtd: MTD device structure
  863. * @data: page data
  864. * @read_ecc: ecc read from nand flash
  865. * @calc_ecc: ecc read from HW ECC registers
  866. */
  867. static int omap3_correct_data_bch(struct mtd_info *mtd, u_char *data,
  868. u_char *read_ecc, u_char *calc_ecc)
  869. {
  870. int i, count;
  871. /* cannot correct more than 8 errors */
  872. unsigned int errloc[8];
  873. struct omap_nand_info *info = container_of(mtd, struct omap_nand_info,
  874. mtd);
  875. count = decode_bch(info->bch, NULL, 512, read_ecc, calc_ecc, NULL,
  876. errloc);
  877. if (count > 0) {
  878. /* correct errors */
  879. for (i = 0; i < count; i++) {
  880. /* correct data only, not ecc bytes */
  881. if (errloc[i] < 8*512)
  882. data[errloc[i]/8] ^= 1 << (errloc[i] & 7);
  883. pr_debug("corrected bitflip %u\n", errloc[i]);
  884. }
  885. } else if (count < 0) {
  886. pr_err("ecc unrecoverable error\n");
  887. }
  888. return count;
  889. }
  890. /**
  891. * omap3_free_bch - Release BCH ecc resources
  892. * @mtd: MTD device structure
  893. */
  894. static void omap3_free_bch(struct mtd_info *mtd)
  895. {
  896. struct omap_nand_info *info = container_of(mtd, struct omap_nand_info,
  897. mtd);
  898. if (info->bch) {
  899. free_bch(info->bch);
  900. info->bch = NULL;
  901. }
  902. }
  903. /**
  904. * omap3_init_bch - Initialize BCH ECC
  905. * @mtd: MTD device structure
  906. * @ecc_opt: OMAP ECC mode (OMAP_ECC_BCH4_CODE_HW or OMAP_ECC_BCH8_CODE_HW)
  907. */
  908. static int omap3_init_bch(struct mtd_info *mtd, int ecc_opt)
  909. {
  910. int ret, max_errors;
  911. struct omap_nand_info *info = container_of(mtd, struct omap_nand_info,
  912. mtd);
  913. #ifdef CONFIG_MTD_NAND_OMAP_BCH8
  914. const int hw_errors = 8;
  915. #else
  916. const int hw_errors = 4;
  917. #endif
  918. info->bch = NULL;
  919. max_errors = (ecc_opt == OMAP_ECC_BCH8_CODE_HW) ? 8 : 4;
  920. if (max_errors != hw_errors) {
  921. pr_err("cannot configure %d-bit BCH ecc, only %d-bit supported",
  922. max_errors, hw_errors);
  923. goto fail;
  924. }
  925. /* initialize GPMC BCH engine */
  926. ret = gpmc_init_hwecc_bch(info->gpmc_cs, 1, max_errors);
  927. if (ret)
  928. goto fail;
  929. /* software bch library is only used to detect and locate errors */
  930. info->bch = init_bch(13, max_errors, 0x201b /* hw polynomial */);
  931. if (!info->bch)
  932. goto fail;
  933. info->nand.ecc.size = 512;
  934. info->nand.ecc.hwctl = omap3_enable_hwecc_bch;
  935. info->nand.ecc.correct = omap3_correct_data_bch;
  936. info->nand.ecc.mode = NAND_ECC_HW;
  937. /*
  938. * The number of corrected errors in an ecc block that will trigger
  939. * block scrubbing defaults to the ecc strength (4 or 8).
  940. * Set mtd->bitflip_threshold here to define a custom threshold.
  941. */
  942. if (max_errors == 8) {
  943. info->nand.ecc.strength = 8;
  944. info->nand.ecc.bytes = 13;
  945. info->nand.ecc.calculate = omap3_calculate_ecc_bch8;
  946. } else {
  947. info->nand.ecc.strength = 4;
  948. info->nand.ecc.bytes = 7;
  949. info->nand.ecc.calculate = omap3_calculate_ecc_bch4;
  950. }
  951. pr_info("enabling NAND BCH ecc with %d-bit correction\n", max_errors);
  952. return 0;
  953. fail:
  954. omap3_free_bch(mtd);
  955. return -1;
  956. }
  957. /**
  958. * omap3_init_bch_tail - Build an oob layout for BCH ECC correction.
  959. * @mtd: MTD device structure
  960. */
  961. static int omap3_init_bch_tail(struct mtd_info *mtd)
  962. {
  963. int i, steps;
  964. struct omap_nand_info *info = container_of(mtd, struct omap_nand_info,
  965. mtd);
  966. struct nand_ecclayout *layout = &info->ecclayout;
  967. /* build oob layout */
  968. steps = mtd->writesize/info->nand.ecc.size;
  969. layout->eccbytes = steps*info->nand.ecc.bytes;
  970. /* do not bother creating special oob layouts for small page devices */
  971. if (mtd->oobsize < 64) {
  972. pr_err("BCH ecc is not supported on small page devices\n");
  973. goto fail;
  974. }
  975. /* reserve 2 bytes for bad block marker */
  976. if (layout->eccbytes+2 > mtd->oobsize) {
  977. pr_err("no oob layout available for oobsize %d eccbytes %u\n",
  978. mtd->oobsize, layout->eccbytes);
  979. goto fail;
  980. }
  981. /* put ecc bytes at oob tail */
  982. for (i = 0; i < layout->eccbytes; i++)
  983. layout->eccpos[i] = mtd->oobsize-layout->eccbytes+i;
  984. layout->oobfree[0].offset = 2;
  985. layout->oobfree[0].length = mtd->oobsize-2-layout->eccbytes;
  986. info->nand.ecc.layout = layout;
  987. if (!(info->nand.options & NAND_BUSWIDTH_16))
  988. info->nand.badblock_pattern = &bb_descrip_flashbased;
  989. return 0;
  990. fail:
  991. omap3_free_bch(mtd);
  992. return -1;
  993. }
  994. #else
  995. static int omap3_init_bch(struct mtd_info *mtd, int ecc_opt)
  996. {
  997. pr_err("CONFIG_MTD_NAND_OMAP_BCH is not enabled\n");
  998. return -1;
  999. }
  1000. static int omap3_init_bch_tail(struct mtd_info *mtd)
  1001. {
  1002. return -1;
  1003. }
  1004. static void omap3_free_bch(struct mtd_info *mtd)
  1005. {
  1006. }
  1007. #endif /* CONFIG_MTD_NAND_OMAP_BCH */
  1008. static int __devinit omap_nand_probe(struct platform_device *pdev)
  1009. {
  1010. struct omap_nand_info *info;
  1011. struct omap_nand_platform_data *pdata;
  1012. int err;
  1013. int i, offset;
  1014. dma_cap_mask_t mask;
  1015. unsigned sig;
  1016. pdata = pdev->dev.platform_data;
  1017. if (pdata == NULL) {
  1018. dev_err(&pdev->dev, "platform data missing\n");
  1019. return -ENODEV;
  1020. }
  1021. info = kzalloc(sizeof(struct omap_nand_info), GFP_KERNEL);
  1022. if (!info)
  1023. return -ENOMEM;
  1024. platform_set_drvdata(pdev, info);
  1025. spin_lock_init(&info->controller.lock);
  1026. init_waitqueue_head(&info->controller.wq);
  1027. info->pdev = pdev;
  1028. info->gpmc_cs = pdata->cs;
  1029. info->phys_base = pdata->phys_base;
  1030. info->mtd.priv = &info->nand;
  1031. info->mtd.name = dev_name(&pdev->dev);
  1032. info->mtd.owner = THIS_MODULE;
  1033. info->nand.options = pdata->devsize;
  1034. info->nand.options |= NAND_SKIP_BBTSCAN;
  1035. /* NAND write protect off */
  1036. gpmc_cs_configure(info->gpmc_cs, GPMC_CONFIG_WP, 0);
  1037. if (!request_mem_region(info->phys_base, NAND_IO_SIZE,
  1038. pdev->dev.driver->name)) {
  1039. err = -EBUSY;
  1040. goto out_free_info;
  1041. }
  1042. info->nand.IO_ADDR_R = ioremap(info->phys_base, NAND_IO_SIZE);
  1043. if (!info->nand.IO_ADDR_R) {
  1044. err = -ENOMEM;
  1045. goto out_release_mem_region;
  1046. }
  1047. info->nand.controller = &info->controller;
  1048. info->nand.IO_ADDR_W = info->nand.IO_ADDR_R;
  1049. info->nand.cmd_ctrl = omap_hwcontrol;
  1050. /*
  1051. * If RDY/BSY line is connected to OMAP then use the omap ready
  1052. * funcrtion and the generic nand_wait function which reads the status
  1053. * register after monitoring the RDY/BSY line.Otherwise use a standard
  1054. * chip delay which is slightly more than tR (AC Timing) of the NAND
  1055. * device and read status register until you get a failure or success
  1056. */
  1057. if (pdata->dev_ready) {
  1058. info->nand.dev_ready = omap_dev_ready;
  1059. info->nand.chip_delay = 0;
  1060. } else {
  1061. info->nand.waitfunc = omap_wait;
  1062. info->nand.chip_delay = 50;
  1063. }
  1064. switch (pdata->xfer_type) {
  1065. case NAND_OMAP_PREFETCH_POLLED:
  1066. info->nand.read_buf = omap_read_buf_pref;
  1067. info->nand.write_buf = omap_write_buf_pref;
  1068. break;
  1069. case NAND_OMAP_POLLED:
  1070. if (info->nand.options & NAND_BUSWIDTH_16) {
  1071. info->nand.read_buf = omap_read_buf16;
  1072. info->nand.write_buf = omap_write_buf16;
  1073. } else {
  1074. info->nand.read_buf = omap_read_buf8;
  1075. info->nand.write_buf = omap_write_buf8;
  1076. }
  1077. break;
  1078. case NAND_OMAP_PREFETCH_DMA:
  1079. dma_cap_zero(mask);
  1080. dma_cap_set(DMA_SLAVE, mask);
  1081. sig = OMAP24XX_DMA_GPMC;
  1082. info->dma = dma_request_channel(mask, omap_dma_filter_fn, &sig);
  1083. if (!info->dma) {
  1084. dev_err(&pdev->dev, "DMA engine request failed\n");
  1085. err = -ENXIO;
  1086. goto out_release_mem_region;
  1087. } else {
  1088. struct dma_slave_config cfg;
  1089. int rc;
  1090. memset(&cfg, 0, sizeof(cfg));
  1091. cfg.src_addr = info->phys_base;
  1092. cfg.dst_addr = info->phys_base;
  1093. cfg.src_addr_width = DMA_SLAVE_BUSWIDTH_4_BYTES;
  1094. cfg.dst_addr_width = DMA_SLAVE_BUSWIDTH_4_BYTES;
  1095. cfg.src_maxburst = 16;
  1096. cfg.dst_maxburst = 16;
  1097. rc = dmaengine_slave_config(info->dma, &cfg);
  1098. if (rc) {
  1099. dev_err(&pdev->dev, "DMA engine slave config failed: %d\n",
  1100. rc);
  1101. goto out_release_mem_region;
  1102. }
  1103. info->nand.read_buf = omap_read_buf_dma_pref;
  1104. info->nand.write_buf = omap_write_buf_dma_pref;
  1105. }
  1106. break;
  1107. case NAND_OMAP_PREFETCH_IRQ:
  1108. err = request_irq(pdata->gpmc_irq,
  1109. omap_nand_irq, IRQF_SHARED, "gpmc-nand", info);
  1110. if (err) {
  1111. dev_err(&pdev->dev, "requesting irq(%d) error:%d",
  1112. pdata->gpmc_irq, err);
  1113. goto out_release_mem_region;
  1114. } else {
  1115. info->gpmc_irq = pdata->gpmc_irq;
  1116. info->nand.read_buf = omap_read_buf_irq_pref;
  1117. info->nand.write_buf = omap_write_buf_irq_pref;
  1118. }
  1119. break;
  1120. default:
  1121. dev_err(&pdev->dev,
  1122. "xfer_type(%d) not supported!\n", pdata->xfer_type);
  1123. err = -EINVAL;
  1124. goto out_release_mem_region;
  1125. }
  1126. info->nand.verify_buf = omap_verify_buf;
  1127. /* selsect the ecc type */
  1128. if (pdata->ecc_opt == OMAP_ECC_HAMMING_CODE_DEFAULT)
  1129. info->nand.ecc.mode = NAND_ECC_SOFT;
  1130. else if ((pdata->ecc_opt == OMAP_ECC_HAMMING_CODE_HW) ||
  1131. (pdata->ecc_opt == OMAP_ECC_HAMMING_CODE_HW_ROMCODE)) {
  1132. info->nand.ecc.bytes = 3;
  1133. info->nand.ecc.size = 512;
  1134. info->nand.ecc.strength = 1;
  1135. info->nand.ecc.calculate = omap_calculate_ecc;
  1136. info->nand.ecc.hwctl = omap_enable_hwecc;
  1137. info->nand.ecc.correct = omap_correct_data;
  1138. info->nand.ecc.mode = NAND_ECC_HW;
  1139. } else if ((pdata->ecc_opt == OMAP_ECC_BCH4_CODE_HW) ||
  1140. (pdata->ecc_opt == OMAP_ECC_BCH8_CODE_HW)) {
  1141. err = omap3_init_bch(&info->mtd, pdata->ecc_opt);
  1142. if (err) {
  1143. err = -EINVAL;
  1144. goto out_release_mem_region;
  1145. }
  1146. }
  1147. /* DIP switches on some boards change between 8 and 16 bit
  1148. * bus widths for flash. Try the other width if the first try fails.
  1149. */
  1150. if (nand_scan_ident(&info->mtd, 1, NULL)) {
  1151. info->nand.options ^= NAND_BUSWIDTH_16;
  1152. if (nand_scan_ident(&info->mtd, 1, NULL)) {
  1153. err = -ENXIO;
  1154. goto out_release_mem_region;
  1155. }
  1156. }
  1157. /* rom code layout */
  1158. if (pdata->ecc_opt == OMAP_ECC_HAMMING_CODE_HW_ROMCODE) {
  1159. if (info->nand.options & NAND_BUSWIDTH_16)
  1160. offset = 2;
  1161. else {
  1162. offset = 1;
  1163. info->nand.badblock_pattern = &bb_descrip_flashbased;
  1164. }
  1165. omap_oobinfo.eccbytes = 3 * (info->mtd.oobsize/16);
  1166. for (i = 0; i < omap_oobinfo.eccbytes; i++)
  1167. omap_oobinfo.eccpos[i] = i+offset;
  1168. omap_oobinfo.oobfree->offset = offset + omap_oobinfo.eccbytes;
  1169. omap_oobinfo.oobfree->length = info->mtd.oobsize -
  1170. (offset + omap_oobinfo.eccbytes);
  1171. info->nand.ecc.layout = &omap_oobinfo;
  1172. } else if ((pdata->ecc_opt == OMAP_ECC_BCH4_CODE_HW) ||
  1173. (pdata->ecc_opt == OMAP_ECC_BCH8_CODE_HW)) {
  1174. /* build OOB layout for BCH ECC correction */
  1175. err = omap3_init_bch_tail(&info->mtd);
  1176. if (err) {
  1177. err = -EINVAL;
  1178. goto out_release_mem_region;
  1179. }
  1180. }
  1181. /* second phase scan */
  1182. if (nand_scan_tail(&info->mtd)) {
  1183. err = -ENXIO;
  1184. goto out_release_mem_region;
  1185. }
  1186. mtd_device_parse_register(&info->mtd, NULL, NULL, pdata->parts,
  1187. pdata->nr_parts);
  1188. platform_set_drvdata(pdev, &info->mtd);
  1189. return 0;
  1190. out_release_mem_region:
  1191. if (info->dma)
  1192. dma_release_channel(info->dma);
  1193. release_mem_region(info->phys_base, NAND_IO_SIZE);
  1194. out_free_info:
  1195. kfree(info);
  1196. return err;
  1197. }
  1198. static int omap_nand_remove(struct platform_device *pdev)
  1199. {
  1200. struct mtd_info *mtd = platform_get_drvdata(pdev);
  1201. struct omap_nand_info *info = container_of(mtd, struct omap_nand_info,
  1202. mtd);
  1203. omap3_free_bch(&info->mtd);
  1204. platform_set_drvdata(pdev, NULL);
  1205. if (info->dma)
  1206. dma_release_channel(info->dma);
  1207. if (info->gpmc_irq)
  1208. free_irq(info->gpmc_irq, info);
  1209. /* Release NAND device, its internal structures and partitions */
  1210. nand_release(&info->mtd);
  1211. iounmap(info->nand.IO_ADDR_R);
  1212. kfree(&info->mtd);
  1213. return 0;
  1214. }
  1215. static struct platform_driver omap_nand_driver = {
  1216. .probe = omap_nand_probe,
  1217. .remove = omap_nand_remove,
  1218. .driver = {
  1219. .name = DRIVER_NAME,
  1220. .owner = THIS_MODULE,
  1221. },
  1222. };
  1223. module_platform_driver(omap_nand_driver);
  1224. MODULE_ALIAS("platform:" DRIVER_NAME);
  1225. MODULE_LICENSE("GPL");
  1226. MODULE_DESCRIPTION("Glue layer for NAND flash on TI OMAP boards");