omap2.c 41 KB

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