omap2.c 44 KB

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