omap2.c 38 KB

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