omap2.c 58 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119
  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. #include <linux/of.h>
  25. #include <linux/of_device.h>
  26. #ifdef CONFIG_MTD_NAND_OMAP_BCH
  27. #include <linux/bch.h>
  28. #include <linux/platform_data/elm.h>
  29. #endif
  30. #include <linux/platform_data/mtd-nand-omap2.h>
  31. #define DRIVER_NAME "omap2-nand"
  32. #define OMAP_NAND_TIMEOUT_MS 5000
  33. #define NAND_Ecc_P1e (1 << 0)
  34. #define NAND_Ecc_P2e (1 << 1)
  35. #define NAND_Ecc_P4e (1 << 2)
  36. #define NAND_Ecc_P8e (1 << 3)
  37. #define NAND_Ecc_P16e (1 << 4)
  38. #define NAND_Ecc_P32e (1 << 5)
  39. #define NAND_Ecc_P64e (1 << 6)
  40. #define NAND_Ecc_P128e (1 << 7)
  41. #define NAND_Ecc_P256e (1 << 8)
  42. #define NAND_Ecc_P512e (1 << 9)
  43. #define NAND_Ecc_P1024e (1 << 10)
  44. #define NAND_Ecc_P2048e (1 << 11)
  45. #define NAND_Ecc_P1o (1 << 16)
  46. #define NAND_Ecc_P2o (1 << 17)
  47. #define NAND_Ecc_P4o (1 << 18)
  48. #define NAND_Ecc_P8o (1 << 19)
  49. #define NAND_Ecc_P16o (1 << 20)
  50. #define NAND_Ecc_P32o (1 << 21)
  51. #define NAND_Ecc_P64o (1 << 22)
  52. #define NAND_Ecc_P128o (1 << 23)
  53. #define NAND_Ecc_P256o (1 << 24)
  54. #define NAND_Ecc_P512o (1 << 25)
  55. #define NAND_Ecc_P1024o (1 << 26)
  56. #define NAND_Ecc_P2048o (1 << 27)
  57. #define TF(value) (value ? 1 : 0)
  58. #define P2048e(a) (TF(a & NAND_Ecc_P2048e) << 0)
  59. #define P2048o(a) (TF(a & NAND_Ecc_P2048o) << 1)
  60. #define P1e(a) (TF(a & NAND_Ecc_P1e) << 2)
  61. #define P1o(a) (TF(a & NAND_Ecc_P1o) << 3)
  62. #define P2e(a) (TF(a & NAND_Ecc_P2e) << 4)
  63. #define P2o(a) (TF(a & NAND_Ecc_P2o) << 5)
  64. #define P4e(a) (TF(a & NAND_Ecc_P4e) << 6)
  65. #define P4o(a) (TF(a & NAND_Ecc_P4o) << 7)
  66. #define P8e(a) (TF(a & NAND_Ecc_P8e) << 0)
  67. #define P8o(a) (TF(a & NAND_Ecc_P8o) << 1)
  68. #define P16e(a) (TF(a & NAND_Ecc_P16e) << 2)
  69. #define P16o(a) (TF(a & NAND_Ecc_P16o) << 3)
  70. #define P32e(a) (TF(a & NAND_Ecc_P32e) << 4)
  71. #define P32o(a) (TF(a & NAND_Ecc_P32o) << 5)
  72. #define P64e(a) (TF(a & NAND_Ecc_P64e) << 6)
  73. #define P64o(a) (TF(a & NAND_Ecc_P64o) << 7)
  74. #define P128e(a) (TF(a & NAND_Ecc_P128e) << 0)
  75. #define P128o(a) (TF(a & NAND_Ecc_P128o) << 1)
  76. #define P256e(a) (TF(a & NAND_Ecc_P256e) << 2)
  77. #define P256o(a) (TF(a & NAND_Ecc_P256o) << 3)
  78. #define P512e(a) (TF(a & NAND_Ecc_P512e) << 4)
  79. #define P512o(a) (TF(a & NAND_Ecc_P512o) << 5)
  80. #define P1024e(a) (TF(a & NAND_Ecc_P1024e) << 6)
  81. #define P1024o(a) (TF(a & NAND_Ecc_P1024o) << 7)
  82. #define P8e_s(a) (TF(a & NAND_Ecc_P8e) << 0)
  83. #define P8o_s(a) (TF(a & NAND_Ecc_P8o) << 1)
  84. #define P16e_s(a) (TF(a & NAND_Ecc_P16e) << 2)
  85. #define P16o_s(a) (TF(a & NAND_Ecc_P16o) << 3)
  86. #define P1e_s(a) (TF(a & NAND_Ecc_P1e) << 4)
  87. #define P1o_s(a) (TF(a & NAND_Ecc_P1o) << 5)
  88. #define P2e_s(a) (TF(a & NAND_Ecc_P2e) << 6)
  89. #define P2o_s(a) (TF(a & NAND_Ecc_P2o) << 7)
  90. #define P4e_s(a) (TF(a & NAND_Ecc_P4e) << 0)
  91. #define P4o_s(a) (TF(a & NAND_Ecc_P4o) << 1)
  92. #define PREFETCH_CONFIG1_CS_SHIFT 24
  93. #define ECC_CONFIG_CS_SHIFT 1
  94. #define CS_MASK 0x7
  95. #define ENABLE_PREFETCH (0x1 << 7)
  96. #define DMA_MPU_MODE_SHIFT 2
  97. #define ECCSIZE0_SHIFT 12
  98. #define ECCSIZE1_SHIFT 22
  99. #define ECC1RESULTSIZE 0x1
  100. #define ECCCLEAR 0x100
  101. #define ECC1 0x1
  102. #define PREFETCH_FIFOTHRESHOLD_MAX 0x40
  103. #define PREFETCH_FIFOTHRESHOLD(val) ((val) << 8)
  104. #define PREFETCH_STATUS_COUNT(val) (val & 0x00003fff)
  105. #define PREFETCH_STATUS_FIFO_CNT(val) ((val >> 24) & 0x7F)
  106. #define STATUS_BUFF_EMPTY 0x00000001
  107. #define OMAP24XX_DMA_GPMC 4
  108. #define BCH8_MAX_ERROR 8 /* upto 8 bit correctable */
  109. #define BCH4_MAX_ERROR 4 /* upto 4 bit correctable */
  110. #define SECTOR_BYTES 512
  111. /* 4 bit padding to make byte aligned, 56 = 52 + 4 */
  112. #define BCH4_BIT_PAD 4
  113. #define BCH8_ECC_MAX ((SECTOR_BYTES + BCH8_ECC_OOB_BYTES) * 8)
  114. #define BCH4_ECC_MAX ((SECTOR_BYTES + BCH4_ECC_OOB_BYTES) * 8)
  115. /* GPMC ecc engine settings for read */
  116. #define BCH_WRAPMODE_1 1 /* BCH wrap mode 1 */
  117. #define BCH8R_ECC_SIZE0 0x1a /* ecc_size0 = 26 */
  118. #define BCH8R_ECC_SIZE1 0x2 /* ecc_size1 = 2 */
  119. #define BCH4R_ECC_SIZE0 0xd /* ecc_size0 = 13 */
  120. #define BCH4R_ECC_SIZE1 0x3 /* ecc_size1 = 3 */
  121. /* GPMC ecc engine settings for write */
  122. #define BCH_WRAPMODE_6 6 /* BCH wrap mode 6 */
  123. #define BCH_ECC_SIZE0 0x0 /* ecc_size0 = 0, no oob protection */
  124. #define BCH_ECC_SIZE1 0x20 /* ecc_size1 = 32 */
  125. #ifdef CONFIG_MTD_NAND_OMAP_BCH
  126. static u_char bch8_vector[] = {0xf3, 0xdb, 0x14, 0x16, 0x8b, 0xd2, 0xbe, 0xcc,
  127. 0xac, 0x6b, 0xff, 0x99, 0x7b};
  128. static u_char bch4_vector[] = {0x00, 0x6b, 0x31, 0xdd, 0x41, 0xbc, 0x10};
  129. #endif
  130. /* oob info generated runtime depending on ecc algorithm and layout selected */
  131. static struct nand_ecclayout omap_oobinfo;
  132. /* Define some generic bad / good block scan pattern which are used
  133. * while scanning a device for factory marked good / bad blocks
  134. */
  135. static uint8_t scan_ff_pattern[] = { 0xff };
  136. static struct nand_bbt_descr bb_descrip_flashbased = {
  137. .options = NAND_BBT_SCANEMPTY | NAND_BBT_SCANALLPAGES,
  138. .offs = 0,
  139. .len = 1,
  140. .pattern = scan_ff_pattern,
  141. };
  142. struct omap_nand_info {
  143. struct nand_hw_control controller;
  144. struct omap_nand_platform_data *pdata;
  145. struct mtd_info mtd;
  146. struct nand_chip nand;
  147. struct platform_device *pdev;
  148. int gpmc_cs;
  149. unsigned long phys_base;
  150. unsigned long mem_size;
  151. struct completion comp;
  152. struct dma_chan *dma;
  153. int gpmc_irq_fifo;
  154. int gpmc_irq_count;
  155. enum {
  156. OMAP_NAND_IO_READ = 0, /* read */
  157. OMAP_NAND_IO_WRITE, /* write */
  158. } iomode;
  159. u_char *buf;
  160. int buf_len;
  161. struct gpmc_nand_regs reg;
  162. #ifdef CONFIG_MTD_NAND_OMAP_BCH
  163. struct bch_control *bch;
  164. struct nand_ecclayout ecclayout;
  165. bool is_elm_used;
  166. struct device *elm_dev;
  167. struct device_node *of_node;
  168. #endif
  169. };
  170. /**
  171. * omap_prefetch_enable - configures and starts prefetch transfer
  172. * @cs: cs (chip select) number
  173. * @fifo_th: fifo threshold to be used for read/ write
  174. * @dma_mode: dma mode enable (1) or disable (0)
  175. * @u32_count: number of bytes to be transferred
  176. * @is_write: prefetch read(0) or write post(1) mode
  177. */
  178. static int omap_prefetch_enable(int cs, int fifo_th, int dma_mode,
  179. unsigned int u32_count, int is_write, struct omap_nand_info *info)
  180. {
  181. u32 val;
  182. if (fifo_th > PREFETCH_FIFOTHRESHOLD_MAX)
  183. return -1;
  184. if (readl(info->reg.gpmc_prefetch_control))
  185. return -EBUSY;
  186. /* Set the amount of bytes to be prefetched */
  187. writel(u32_count, info->reg.gpmc_prefetch_config2);
  188. /* Set dma/mpu mode, the prefetch read / post write and
  189. * enable the engine. Set which cs is has requested for.
  190. */
  191. val = ((cs << PREFETCH_CONFIG1_CS_SHIFT) |
  192. PREFETCH_FIFOTHRESHOLD(fifo_th) | ENABLE_PREFETCH |
  193. (dma_mode << DMA_MPU_MODE_SHIFT) | (0x1 & is_write));
  194. writel(val, info->reg.gpmc_prefetch_config1);
  195. /* Start the prefetch engine */
  196. writel(0x1, info->reg.gpmc_prefetch_control);
  197. return 0;
  198. }
  199. /**
  200. * omap_prefetch_reset - disables and stops the prefetch engine
  201. */
  202. static int omap_prefetch_reset(int cs, struct omap_nand_info *info)
  203. {
  204. u32 config1;
  205. /* check if the same module/cs is trying to reset */
  206. config1 = readl(info->reg.gpmc_prefetch_config1);
  207. if (((config1 >> PREFETCH_CONFIG1_CS_SHIFT) & CS_MASK) != cs)
  208. return -EINVAL;
  209. /* Stop the PFPW engine */
  210. writel(0x0, info->reg.gpmc_prefetch_control);
  211. /* Reset/disable the PFPW engine */
  212. writel(0x0, info->reg.gpmc_prefetch_config1);
  213. return 0;
  214. }
  215. /**
  216. * omap_hwcontrol - hardware specific access to control-lines
  217. * @mtd: MTD device structure
  218. * @cmd: command to device
  219. * @ctrl:
  220. * NAND_NCE: bit 0 -> don't care
  221. * NAND_CLE: bit 1 -> Command Latch
  222. * NAND_ALE: bit 2 -> Address Latch
  223. *
  224. * NOTE: boards may use different bits for these!!
  225. */
  226. static void omap_hwcontrol(struct mtd_info *mtd, int cmd, unsigned int ctrl)
  227. {
  228. struct omap_nand_info *info = container_of(mtd,
  229. struct omap_nand_info, mtd);
  230. if (cmd != NAND_CMD_NONE) {
  231. if (ctrl & NAND_CLE)
  232. writeb(cmd, info->reg.gpmc_nand_command);
  233. else if (ctrl & NAND_ALE)
  234. writeb(cmd, info->reg.gpmc_nand_address);
  235. else /* NAND_NCE */
  236. writeb(cmd, info->reg.gpmc_nand_data);
  237. }
  238. }
  239. /**
  240. * omap_read_buf8 - read data from NAND controller into buffer
  241. * @mtd: MTD device structure
  242. * @buf: buffer to store date
  243. * @len: number of bytes to read
  244. */
  245. static void omap_read_buf8(struct mtd_info *mtd, u_char *buf, int len)
  246. {
  247. struct nand_chip *nand = mtd->priv;
  248. ioread8_rep(nand->IO_ADDR_R, buf, len);
  249. }
  250. /**
  251. * omap_write_buf8 - write buffer to NAND controller
  252. * @mtd: MTD device structure
  253. * @buf: data buffer
  254. * @len: number of bytes to write
  255. */
  256. static void omap_write_buf8(struct mtd_info *mtd, const u_char *buf, int len)
  257. {
  258. struct omap_nand_info *info = container_of(mtd,
  259. struct omap_nand_info, mtd);
  260. u_char *p = (u_char *)buf;
  261. u32 status = 0;
  262. while (len--) {
  263. iowrite8(*p++, info->nand.IO_ADDR_W);
  264. /* wait until buffer is available for write */
  265. do {
  266. status = readl(info->reg.gpmc_status) &
  267. STATUS_BUFF_EMPTY;
  268. } while (!status);
  269. }
  270. }
  271. /**
  272. * omap_read_buf16 - read data from NAND controller into buffer
  273. * @mtd: MTD device structure
  274. * @buf: buffer to store date
  275. * @len: number of bytes to read
  276. */
  277. static void omap_read_buf16(struct mtd_info *mtd, u_char *buf, int len)
  278. {
  279. struct nand_chip *nand = mtd->priv;
  280. ioread16_rep(nand->IO_ADDR_R, buf, len / 2);
  281. }
  282. /**
  283. * omap_write_buf16 - write buffer to NAND controller
  284. * @mtd: MTD device structure
  285. * @buf: data buffer
  286. * @len: number of bytes to write
  287. */
  288. static void omap_write_buf16(struct mtd_info *mtd, const u_char * buf, int len)
  289. {
  290. struct omap_nand_info *info = container_of(mtd,
  291. struct omap_nand_info, mtd);
  292. u16 *p = (u16 *) buf;
  293. u32 status = 0;
  294. /* FIXME try bursts of writesw() or DMA ... */
  295. len >>= 1;
  296. while (len--) {
  297. iowrite16(*p++, info->nand.IO_ADDR_W);
  298. /* wait until buffer is available for write */
  299. do {
  300. status = readl(info->reg.gpmc_status) &
  301. STATUS_BUFF_EMPTY;
  302. } while (!status);
  303. }
  304. }
  305. /**
  306. * omap_read_buf_pref - read data from NAND controller into buffer
  307. * @mtd: MTD device structure
  308. * @buf: buffer to store date
  309. * @len: number of bytes to read
  310. */
  311. static void omap_read_buf_pref(struct mtd_info *mtd, u_char *buf, int len)
  312. {
  313. struct omap_nand_info *info = container_of(mtd,
  314. struct omap_nand_info, mtd);
  315. uint32_t r_count = 0;
  316. int ret = 0;
  317. u32 *p = (u32 *)buf;
  318. /* take care of subpage reads */
  319. if (len % 4) {
  320. if (info->nand.options & NAND_BUSWIDTH_16)
  321. omap_read_buf16(mtd, buf, len % 4);
  322. else
  323. omap_read_buf8(mtd, buf, len % 4);
  324. p = (u32 *) (buf + len % 4);
  325. len -= len % 4;
  326. }
  327. /* configure and start prefetch transfer */
  328. ret = omap_prefetch_enable(info->gpmc_cs,
  329. PREFETCH_FIFOTHRESHOLD_MAX, 0x0, len, 0x0, info);
  330. if (ret) {
  331. /* PFPW engine is busy, use cpu copy method */
  332. if (info->nand.options & NAND_BUSWIDTH_16)
  333. omap_read_buf16(mtd, (u_char *)p, len);
  334. else
  335. omap_read_buf8(mtd, (u_char *)p, len);
  336. } else {
  337. do {
  338. r_count = readl(info->reg.gpmc_prefetch_status);
  339. r_count = PREFETCH_STATUS_FIFO_CNT(r_count);
  340. r_count = r_count >> 2;
  341. ioread32_rep(info->nand.IO_ADDR_R, p, r_count);
  342. p += r_count;
  343. len -= r_count << 2;
  344. } while (len);
  345. /* disable and stop the PFPW engine */
  346. omap_prefetch_reset(info->gpmc_cs, info);
  347. }
  348. }
  349. /**
  350. * omap_write_buf_pref - write buffer to NAND controller
  351. * @mtd: MTD device structure
  352. * @buf: data buffer
  353. * @len: number of bytes to write
  354. */
  355. static void omap_write_buf_pref(struct mtd_info *mtd,
  356. const u_char *buf, int len)
  357. {
  358. struct omap_nand_info *info = container_of(mtd,
  359. struct omap_nand_info, mtd);
  360. uint32_t w_count = 0;
  361. int i = 0, ret = 0;
  362. u16 *p = (u16 *)buf;
  363. unsigned long tim, limit;
  364. u32 val;
  365. /* take care of subpage writes */
  366. if (len % 2 != 0) {
  367. writeb(*buf, info->nand.IO_ADDR_W);
  368. p = (u16 *)(buf + 1);
  369. len--;
  370. }
  371. /* configure and start prefetch transfer */
  372. ret = omap_prefetch_enable(info->gpmc_cs,
  373. PREFETCH_FIFOTHRESHOLD_MAX, 0x0, len, 0x1, info);
  374. if (ret) {
  375. /* PFPW engine is busy, use cpu copy method */
  376. if (info->nand.options & NAND_BUSWIDTH_16)
  377. omap_write_buf16(mtd, (u_char *)p, len);
  378. else
  379. omap_write_buf8(mtd, (u_char *)p, len);
  380. } else {
  381. while (len) {
  382. w_count = readl(info->reg.gpmc_prefetch_status);
  383. w_count = PREFETCH_STATUS_FIFO_CNT(w_count);
  384. w_count = w_count >> 1;
  385. for (i = 0; (i < w_count) && len; i++, len -= 2)
  386. iowrite16(*p++, info->nand.IO_ADDR_W);
  387. }
  388. /* wait for data to flushed-out before reset the prefetch */
  389. tim = 0;
  390. limit = (loops_per_jiffy *
  391. msecs_to_jiffies(OMAP_NAND_TIMEOUT_MS));
  392. do {
  393. cpu_relax();
  394. val = readl(info->reg.gpmc_prefetch_status);
  395. val = PREFETCH_STATUS_COUNT(val);
  396. } while (val && (tim++ < limit));
  397. /* disable and stop the PFPW engine */
  398. omap_prefetch_reset(info->gpmc_cs, info);
  399. }
  400. }
  401. /*
  402. * omap_nand_dma_callback: callback on the completion of dma transfer
  403. * @data: pointer to completion data structure
  404. */
  405. static void omap_nand_dma_callback(void *data)
  406. {
  407. complete((struct completion *) data);
  408. }
  409. /*
  410. * omap_nand_dma_transfer: configure and start dma transfer
  411. * @mtd: MTD device structure
  412. * @addr: virtual address in RAM of source/destination
  413. * @len: number of data bytes to be transferred
  414. * @is_write: flag for read/write operation
  415. */
  416. static inline int omap_nand_dma_transfer(struct mtd_info *mtd, void *addr,
  417. unsigned int len, int is_write)
  418. {
  419. struct omap_nand_info *info = container_of(mtd,
  420. struct omap_nand_info, mtd);
  421. struct dma_async_tx_descriptor *tx;
  422. enum dma_data_direction dir = is_write ? DMA_TO_DEVICE :
  423. DMA_FROM_DEVICE;
  424. struct scatterlist sg;
  425. unsigned long tim, limit;
  426. unsigned n;
  427. int ret;
  428. u32 val;
  429. if (addr >= high_memory) {
  430. struct page *p1;
  431. if (((size_t)addr & PAGE_MASK) !=
  432. ((size_t)(addr + len - 1) & PAGE_MASK))
  433. goto out_copy;
  434. p1 = vmalloc_to_page(addr);
  435. if (!p1)
  436. goto out_copy;
  437. addr = page_address(p1) + ((size_t)addr & ~PAGE_MASK);
  438. }
  439. sg_init_one(&sg, addr, len);
  440. n = dma_map_sg(info->dma->device->dev, &sg, 1, dir);
  441. if (n == 0) {
  442. dev_err(&info->pdev->dev,
  443. "Couldn't DMA map a %d byte buffer\n", len);
  444. goto out_copy;
  445. }
  446. tx = dmaengine_prep_slave_sg(info->dma, &sg, n,
  447. is_write ? DMA_MEM_TO_DEV : DMA_DEV_TO_MEM,
  448. DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
  449. if (!tx)
  450. goto out_copy_unmap;
  451. tx->callback = omap_nand_dma_callback;
  452. tx->callback_param = &info->comp;
  453. dmaengine_submit(tx);
  454. /* configure and start prefetch transfer */
  455. ret = omap_prefetch_enable(info->gpmc_cs,
  456. PREFETCH_FIFOTHRESHOLD_MAX, 0x1, len, is_write, info);
  457. if (ret)
  458. /* PFPW engine is busy, use cpu copy method */
  459. goto out_copy_unmap;
  460. init_completion(&info->comp);
  461. dma_async_issue_pending(info->dma);
  462. /* setup and start DMA using dma_addr */
  463. wait_for_completion(&info->comp);
  464. tim = 0;
  465. limit = (loops_per_jiffy * msecs_to_jiffies(OMAP_NAND_TIMEOUT_MS));
  466. do {
  467. cpu_relax();
  468. val = readl(info->reg.gpmc_prefetch_status);
  469. val = PREFETCH_STATUS_COUNT(val);
  470. } while (val && (tim++ < limit));
  471. /* disable and stop the PFPW engine */
  472. omap_prefetch_reset(info->gpmc_cs, info);
  473. dma_unmap_sg(info->dma->device->dev, &sg, 1, dir);
  474. return 0;
  475. out_copy_unmap:
  476. dma_unmap_sg(info->dma->device->dev, &sg, 1, dir);
  477. out_copy:
  478. if (info->nand.options & NAND_BUSWIDTH_16)
  479. is_write == 0 ? omap_read_buf16(mtd, (u_char *) addr, len)
  480. : omap_write_buf16(mtd, (u_char *) addr, len);
  481. else
  482. is_write == 0 ? omap_read_buf8(mtd, (u_char *) addr, len)
  483. : omap_write_buf8(mtd, (u_char *) addr, len);
  484. return 0;
  485. }
  486. /**
  487. * omap_read_buf_dma_pref - read data from NAND controller into buffer
  488. * @mtd: MTD device structure
  489. * @buf: buffer to store date
  490. * @len: number of bytes to read
  491. */
  492. static void omap_read_buf_dma_pref(struct mtd_info *mtd, u_char *buf, int len)
  493. {
  494. if (len <= mtd->oobsize)
  495. omap_read_buf_pref(mtd, buf, len);
  496. else
  497. /* start transfer in DMA mode */
  498. omap_nand_dma_transfer(mtd, buf, len, 0x0);
  499. }
  500. /**
  501. * omap_write_buf_dma_pref - write buffer to NAND controller
  502. * @mtd: MTD device structure
  503. * @buf: data buffer
  504. * @len: number of bytes to write
  505. */
  506. static void omap_write_buf_dma_pref(struct mtd_info *mtd,
  507. const u_char *buf, int len)
  508. {
  509. if (len <= mtd->oobsize)
  510. omap_write_buf_pref(mtd, buf, len);
  511. else
  512. /* start transfer in DMA mode */
  513. omap_nand_dma_transfer(mtd, (u_char *) buf, len, 0x1);
  514. }
  515. /*
  516. * omap_nand_irq - GPMC irq handler
  517. * @this_irq: gpmc irq number
  518. * @dev: omap_nand_info structure pointer is passed here
  519. */
  520. static irqreturn_t omap_nand_irq(int this_irq, void *dev)
  521. {
  522. struct omap_nand_info *info = (struct omap_nand_info *) dev;
  523. u32 bytes;
  524. bytes = readl(info->reg.gpmc_prefetch_status);
  525. bytes = PREFETCH_STATUS_FIFO_CNT(bytes);
  526. bytes = bytes & 0xFFFC; /* io in multiple of 4 bytes */
  527. if (info->iomode == OMAP_NAND_IO_WRITE) { /* checks for write io */
  528. if (this_irq == info->gpmc_irq_count)
  529. goto done;
  530. if (info->buf_len && (info->buf_len < bytes))
  531. bytes = info->buf_len;
  532. else if (!info->buf_len)
  533. bytes = 0;
  534. iowrite32_rep(info->nand.IO_ADDR_W,
  535. (u32 *)info->buf, bytes >> 2);
  536. info->buf = info->buf + bytes;
  537. info->buf_len -= bytes;
  538. } else {
  539. ioread32_rep(info->nand.IO_ADDR_R,
  540. (u32 *)info->buf, bytes >> 2);
  541. info->buf = info->buf + bytes;
  542. if (this_irq == info->gpmc_irq_count)
  543. goto done;
  544. }
  545. return IRQ_HANDLED;
  546. done:
  547. complete(&info->comp);
  548. disable_irq_nosync(info->gpmc_irq_fifo);
  549. disable_irq_nosync(info->gpmc_irq_count);
  550. return IRQ_HANDLED;
  551. }
  552. /*
  553. * omap_read_buf_irq_pref - read data from NAND controller into buffer
  554. * @mtd: MTD device structure
  555. * @buf: buffer to store date
  556. * @len: number of bytes to read
  557. */
  558. static void omap_read_buf_irq_pref(struct mtd_info *mtd, u_char *buf, int len)
  559. {
  560. struct omap_nand_info *info = container_of(mtd,
  561. struct omap_nand_info, mtd);
  562. int ret = 0;
  563. if (len <= mtd->oobsize) {
  564. omap_read_buf_pref(mtd, buf, len);
  565. return;
  566. }
  567. info->iomode = OMAP_NAND_IO_READ;
  568. info->buf = buf;
  569. init_completion(&info->comp);
  570. /* configure and start prefetch transfer */
  571. ret = omap_prefetch_enable(info->gpmc_cs,
  572. PREFETCH_FIFOTHRESHOLD_MAX/2, 0x0, len, 0x0, info);
  573. if (ret)
  574. /* PFPW engine is busy, use cpu copy method */
  575. goto out_copy;
  576. info->buf_len = len;
  577. enable_irq(info->gpmc_irq_count);
  578. enable_irq(info->gpmc_irq_fifo);
  579. /* waiting for read to complete */
  580. wait_for_completion(&info->comp);
  581. /* disable and stop the PFPW engine */
  582. omap_prefetch_reset(info->gpmc_cs, info);
  583. return;
  584. out_copy:
  585. if (info->nand.options & NAND_BUSWIDTH_16)
  586. omap_read_buf16(mtd, buf, len);
  587. else
  588. omap_read_buf8(mtd, buf, len);
  589. }
  590. /*
  591. * omap_write_buf_irq_pref - write buffer to NAND controller
  592. * @mtd: MTD device structure
  593. * @buf: data buffer
  594. * @len: number of bytes to write
  595. */
  596. static void omap_write_buf_irq_pref(struct mtd_info *mtd,
  597. const u_char *buf, int len)
  598. {
  599. struct omap_nand_info *info = container_of(mtd,
  600. struct omap_nand_info, mtd);
  601. int ret = 0;
  602. unsigned long tim, limit;
  603. u32 val;
  604. if (len <= mtd->oobsize) {
  605. omap_write_buf_pref(mtd, buf, len);
  606. return;
  607. }
  608. info->iomode = OMAP_NAND_IO_WRITE;
  609. info->buf = (u_char *) buf;
  610. init_completion(&info->comp);
  611. /* configure and start prefetch transfer : size=24 */
  612. ret = omap_prefetch_enable(info->gpmc_cs,
  613. (PREFETCH_FIFOTHRESHOLD_MAX * 3) / 8, 0x0, len, 0x1, info);
  614. if (ret)
  615. /* PFPW engine is busy, use cpu copy method */
  616. goto out_copy;
  617. info->buf_len = len;
  618. enable_irq(info->gpmc_irq_count);
  619. enable_irq(info->gpmc_irq_fifo);
  620. /* waiting for write to complete */
  621. wait_for_completion(&info->comp);
  622. /* wait for data to flushed-out before reset the prefetch */
  623. tim = 0;
  624. limit = (loops_per_jiffy * msecs_to_jiffies(OMAP_NAND_TIMEOUT_MS));
  625. do {
  626. val = readl(info->reg.gpmc_prefetch_status);
  627. val = PREFETCH_STATUS_COUNT(val);
  628. cpu_relax();
  629. } while (val && (tim++ < limit));
  630. /* disable and stop the PFPW engine */
  631. omap_prefetch_reset(info->gpmc_cs, info);
  632. return;
  633. out_copy:
  634. if (info->nand.options & NAND_BUSWIDTH_16)
  635. omap_write_buf16(mtd, buf, len);
  636. else
  637. omap_write_buf8(mtd, buf, len);
  638. }
  639. /**
  640. * gen_true_ecc - This function will generate true ECC value
  641. * @ecc_buf: buffer to store ecc code
  642. *
  643. * This generated true ECC value can be used when correcting
  644. * data read from NAND flash memory core
  645. */
  646. static void gen_true_ecc(u8 *ecc_buf)
  647. {
  648. u32 tmp = ecc_buf[0] | (ecc_buf[1] << 16) |
  649. ((ecc_buf[2] & 0xF0) << 20) | ((ecc_buf[2] & 0x0F) << 8);
  650. ecc_buf[0] = ~(P64o(tmp) | P64e(tmp) | P32o(tmp) | P32e(tmp) |
  651. P16o(tmp) | P16e(tmp) | P8o(tmp) | P8e(tmp));
  652. ecc_buf[1] = ~(P1024o(tmp) | P1024e(tmp) | P512o(tmp) | P512e(tmp) |
  653. P256o(tmp) | P256e(tmp) | P128o(tmp) | P128e(tmp));
  654. ecc_buf[2] = ~(P4o(tmp) | P4e(tmp) | P2o(tmp) | P2e(tmp) | P1o(tmp) |
  655. P1e(tmp) | P2048o(tmp) | P2048e(tmp));
  656. }
  657. /**
  658. * omap_compare_ecc - Detect (2 bits) and correct (1 bit) error in data
  659. * @ecc_data1: ecc code from nand spare area
  660. * @ecc_data2: ecc code from hardware register obtained from hardware ecc
  661. * @page_data: page data
  662. *
  663. * This function compares two ECC's and indicates if there is an error.
  664. * If the error can be corrected it will be corrected to the buffer.
  665. * If there is no error, %0 is returned. If there is an error but it
  666. * was corrected, %1 is returned. Otherwise, %-1 is returned.
  667. */
  668. static int omap_compare_ecc(u8 *ecc_data1, /* read from NAND memory */
  669. u8 *ecc_data2, /* read from register */
  670. u8 *page_data)
  671. {
  672. uint i;
  673. u8 tmp0_bit[8], tmp1_bit[8], tmp2_bit[8];
  674. u8 comp0_bit[8], comp1_bit[8], comp2_bit[8];
  675. u8 ecc_bit[24];
  676. u8 ecc_sum = 0;
  677. u8 find_bit = 0;
  678. uint find_byte = 0;
  679. int isEccFF;
  680. isEccFF = ((*(u32 *)ecc_data1 & 0xFFFFFF) == 0xFFFFFF);
  681. gen_true_ecc(ecc_data1);
  682. gen_true_ecc(ecc_data2);
  683. for (i = 0; i <= 2; i++) {
  684. *(ecc_data1 + i) = ~(*(ecc_data1 + i));
  685. *(ecc_data2 + i) = ~(*(ecc_data2 + i));
  686. }
  687. for (i = 0; i < 8; i++) {
  688. tmp0_bit[i] = *ecc_data1 % 2;
  689. *ecc_data1 = *ecc_data1 / 2;
  690. }
  691. for (i = 0; i < 8; i++) {
  692. tmp1_bit[i] = *(ecc_data1 + 1) % 2;
  693. *(ecc_data1 + 1) = *(ecc_data1 + 1) / 2;
  694. }
  695. for (i = 0; i < 8; i++) {
  696. tmp2_bit[i] = *(ecc_data1 + 2) % 2;
  697. *(ecc_data1 + 2) = *(ecc_data1 + 2) / 2;
  698. }
  699. for (i = 0; i < 8; i++) {
  700. comp0_bit[i] = *ecc_data2 % 2;
  701. *ecc_data2 = *ecc_data2 / 2;
  702. }
  703. for (i = 0; i < 8; i++) {
  704. comp1_bit[i] = *(ecc_data2 + 1) % 2;
  705. *(ecc_data2 + 1) = *(ecc_data2 + 1) / 2;
  706. }
  707. for (i = 0; i < 8; i++) {
  708. comp2_bit[i] = *(ecc_data2 + 2) % 2;
  709. *(ecc_data2 + 2) = *(ecc_data2 + 2) / 2;
  710. }
  711. for (i = 0; i < 6; i++)
  712. ecc_bit[i] = tmp2_bit[i + 2] ^ comp2_bit[i + 2];
  713. for (i = 0; i < 8; i++)
  714. ecc_bit[i + 6] = tmp0_bit[i] ^ comp0_bit[i];
  715. for (i = 0; i < 8; i++)
  716. ecc_bit[i + 14] = tmp1_bit[i] ^ comp1_bit[i];
  717. ecc_bit[22] = tmp2_bit[0] ^ comp2_bit[0];
  718. ecc_bit[23] = tmp2_bit[1] ^ comp2_bit[1];
  719. for (i = 0; i < 24; i++)
  720. ecc_sum += ecc_bit[i];
  721. switch (ecc_sum) {
  722. case 0:
  723. /* Not reached because this function is not called if
  724. * ECC values are equal
  725. */
  726. return 0;
  727. case 1:
  728. /* Uncorrectable error */
  729. pr_debug("ECC UNCORRECTED_ERROR 1\n");
  730. return -1;
  731. case 11:
  732. /* UN-Correctable error */
  733. pr_debug("ECC UNCORRECTED_ERROR B\n");
  734. return -1;
  735. case 12:
  736. /* Correctable error */
  737. find_byte = (ecc_bit[23] << 8) +
  738. (ecc_bit[21] << 7) +
  739. (ecc_bit[19] << 6) +
  740. (ecc_bit[17] << 5) +
  741. (ecc_bit[15] << 4) +
  742. (ecc_bit[13] << 3) +
  743. (ecc_bit[11] << 2) +
  744. (ecc_bit[9] << 1) +
  745. ecc_bit[7];
  746. find_bit = (ecc_bit[5] << 2) + (ecc_bit[3] << 1) + ecc_bit[1];
  747. pr_debug("Correcting single bit ECC error at offset: "
  748. "%d, bit: %d\n", find_byte, find_bit);
  749. page_data[find_byte] ^= (1 << find_bit);
  750. return 1;
  751. default:
  752. if (isEccFF) {
  753. if (ecc_data2[0] == 0 &&
  754. ecc_data2[1] == 0 &&
  755. ecc_data2[2] == 0)
  756. return 0;
  757. }
  758. pr_debug("UNCORRECTED_ERROR default\n");
  759. return -1;
  760. }
  761. }
  762. /**
  763. * omap_correct_data - Compares the ECC read with HW generated ECC
  764. * @mtd: MTD device structure
  765. * @dat: page data
  766. * @read_ecc: ecc read from nand flash
  767. * @calc_ecc: ecc read from HW ECC registers
  768. *
  769. * Compares the ecc read from nand spare area with ECC registers values
  770. * and if ECC's mismatched, it will call 'omap_compare_ecc' for error
  771. * detection and correction. If there are no errors, %0 is returned. If
  772. * there were errors and all of the errors were corrected, the number of
  773. * corrected errors is returned. If uncorrectable errors exist, %-1 is
  774. * returned.
  775. */
  776. static int omap_correct_data(struct mtd_info *mtd, u_char *dat,
  777. u_char *read_ecc, u_char *calc_ecc)
  778. {
  779. struct omap_nand_info *info = container_of(mtd, struct omap_nand_info,
  780. mtd);
  781. int blockCnt = 0, i = 0, ret = 0;
  782. int stat = 0;
  783. /* Ex NAND_ECC_HW12_2048 */
  784. if ((info->nand.ecc.mode == NAND_ECC_HW) &&
  785. (info->nand.ecc.size == 2048))
  786. blockCnt = 4;
  787. else
  788. blockCnt = 1;
  789. for (i = 0; i < blockCnt; i++) {
  790. if (memcmp(read_ecc, calc_ecc, 3) != 0) {
  791. ret = omap_compare_ecc(read_ecc, calc_ecc, dat);
  792. if (ret < 0)
  793. return ret;
  794. /* keep track of the number of corrected errors */
  795. stat += ret;
  796. }
  797. read_ecc += 3;
  798. calc_ecc += 3;
  799. dat += 512;
  800. }
  801. return stat;
  802. }
  803. /**
  804. * omap_calcuate_ecc - Generate non-inverted ECC bytes.
  805. * @mtd: MTD device structure
  806. * @dat: The pointer to data on which ecc is computed
  807. * @ecc_code: The ecc_code buffer
  808. *
  809. * Using noninverted ECC can be considered ugly since writing a blank
  810. * page ie. padding will clear the ECC bytes. This is no problem as long
  811. * nobody is trying to write data on the seemingly unused page. Reading
  812. * an erased page will produce an ECC mismatch between generated and read
  813. * ECC bytes that has to be dealt with separately.
  814. */
  815. static int omap_calculate_ecc(struct mtd_info *mtd, const u_char *dat,
  816. u_char *ecc_code)
  817. {
  818. struct omap_nand_info *info = container_of(mtd, struct omap_nand_info,
  819. mtd);
  820. u32 val;
  821. val = readl(info->reg.gpmc_ecc_config);
  822. if (((val >> ECC_CONFIG_CS_SHIFT) & ~CS_MASK) != info->gpmc_cs)
  823. return -EINVAL;
  824. /* read ecc result */
  825. val = readl(info->reg.gpmc_ecc1_result);
  826. *ecc_code++ = val; /* P128e, ..., P1e */
  827. *ecc_code++ = val >> 16; /* P128o, ..., P1o */
  828. /* P2048o, P1024o, P512o, P256o, P2048e, P1024e, P512e, P256e */
  829. *ecc_code++ = ((val >> 8) & 0x0f) | ((val >> 20) & 0xf0);
  830. return 0;
  831. }
  832. /**
  833. * omap_enable_hwecc - This function enables the hardware ecc functionality
  834. * @mtd: MTD device structure
  835. * @mode: Read/Write mode
  836. */
  837. static void omap_enable_hwecc(struct mtd_info *mtd, int mode)
  838. {
  839. struct omap_nand_info *info = container_of(mtd, struct omap_nand_info,
  840. mtd);
  841. struct nand_chip *chip = mtd->priv;
  842. unsigned int dev_width = (chip->options & NAND_BUSWIDTH_16) ? 1 : 0;
  843. u32 val;
  844. /* clear ecc and enable bits */
  845. val = ECCCLEAR | ECC1;
  846. writel(val, info->reg.gpmc_ecc_control);
  847. /* program ecc and result sizes */
  848. val = ((((info->nand.ecc.size >> 1) - 1) << ECCSIZE1_SHIFT) |
  849. ECC1RESULTSIZE);
  850. writel(val, info->reg.gpmc_ecc_size_config);
  851. switch (mode) {
  852. case NAND_ECC_READ:
  853. case NAND_ECC_WRITE:
  854. writel(ECCCLEAR | ECC1, info->reg.gpmc_ecc_control);
  855. break;
  856. case NAND_ECC_READSYN:
  857. writel(ECCCLEAR, info->reg.gpmc_ecc_control);
  858. break;
  859. default:
  860. dev_info(&info->pdev->dev,
  861. "error: unrecognized Mode[%d]!\n", mode);
  862. break;
  863. }
  864. /* (ECC 16 or 8 bit col) | ( CS ) | ECC Enable */
  865. val = (dev_width << 7) | (info->gpmc_cs << 1) | (0x1);
  866. writel(val, info->reg.gpmc_ecc_config);
  867. }
  868. /**
  869. * omap_wait - wait until the command is done
  870. * @mtd: MTD device structure
  871. * @chip: NAND Chip structure
  872. *
  873. * Wait function is called during Program and erase operations and
  874. * the way it is called from MTD layer, we should wait till the NAND
  875. * chip is ready after the programming/erase operation has completed.
  876. *
  877. * Erase can take up to 400ms and program up to 20ms according to
  878. * general NAND and SmartMedia specs
  879. */
  880. static int omap_wait(struct mtd_info *mtd, struct nand_chip *chip)
  881. {
  882. struct nand_chip *this = mtd->priv;
  883. struct omap_nand_info *info = container_of(mtd, struct omap_nand_info,
  884. mtd);
  885. unsigned long timeo = jiffies;
  886. int status, state = this->state;
  887. if (state == FL_ERASING)
  888. timeo += (HZ * 400) / 1000;
  889. else
  890. timeo += (HZ * 20) / 1000;
  891. writeb(NAND_CMD_STATUS & 0xFF, info->reg.gpmc_nand_command);
  892. while (time_before(jiffies, timeo)) {
  893. status = readb(info->reg.gpmc_nand_data);
  894. if (status & NAND_STATUS_READY)
  895. break;
  896. cond_resched();
  897. }
  898. status = readb(info->reg.gpmc_nand_data);
  899. return status;
  900. }
  901. /**
  902. * omap_dev_ready - calls the platform specific dev_ready function
  903. * @mtd: MTD device structure
  904. */
  905. static int omap_dev_ready(struct mtd_info *mtd)
  906. {
  907. unsigned int val = 0;
  908. struct omap_nand_info *info = container_of(mtd, struct omap_nand_info,
  909. mtd);
  910. val = readl(info->reg.gpmc_status);
  911. if ((val & 0x100) == 0x100) {
  912. return 1;
  913. } else {
  914. return 0;
  915. }
  916. }
  917. #ifdef CONFIG_MTD_NAND_OMAP_BCH
  918. /**
  919. * omap3_enable_hwecc_bch - Program OMAP3 GPMC to perform BCH ECC correction
  920. * @mtd: MTD device structure
  921. * @mode: Read/Write mode
  922. *
  923. * When using BCH, sector size is hardcoded to 512 bytes.
  924. * Using wrapping mode 6 both for reading and writing if ELM module not uses
  925. * for error correction.
  926. * On writing,
  927. * eccsize0 = 0 (no additional protected byte in spare area)
  928. * eccsize1 = 32 (skip 32 nibbles = 16 bytes per sector in spare area)
  929. */
  930. static void omap3_enable_hwecc_bch(struct mtd_info *mtd, int mode)
  931. {
  932. int nerrors;
  933. unsigned int dev_width, nsectors;
  934. struct omap_nand_info *info = container_of(mtd, struct omap_nand_info,
  935. mtd);
  936. struct nand_chip *chip = mtd->priv;
  937. u32 val, wr_mode;
  938. unsigned int ecc_size1, ecc_size0;
  939. /* Using wrapping mode 6 for writing */
  940. wr_mode = BCH_WRAPMODE_6;
  941. /*
  942. * ECC engine enabled for valid ecc_size0 nibbles
  943. * and disabled for ecc_size1 nibbles.
  944. */
  945. ecc_size0 = BCH_ECC_SIZE0;
  946. ecc_size1 = BCH_ECC_SIZE1;
  947. /* Perform ecc calculation on 512-byte sector */
  948. nsectors = 1;
  949. /* Update number of error correction */
  950. nerrors = info->nand.ecc.strength;
  951. /* Multi sector reading/writing for NAND flash with page size < 4096 */
  952. if (info->is_elm_used && (mtd->writesize <= 4096)) {
  953. if (mode == NAND_ECC_READ) {
  954. /* Using wrapping mode 1 for reading */
  955. wr_mode = BCH_WRAPMODE_1;
  956. /*
  957. * ECC engine enabled for ecc_size0 nibbles
  958. * and disabled for ecc_size1 nibbles.
  959. */
  960. ecc_size0 = (nerrors == 8) ?
  961. BCH8R_ECC_SIZE0 : BCH4R_ECC_SIZE0;
  962. ecc_size1 = (nerrors == 8) ?
  963. BCH8R_ECC_SIZE1 : BCH4R_ECC_SIZE1;
  964. }
  965. /* Perform ecc calculation for one page (< 4096) */
  966. nsectors = info->nand.ecc.steps;
  967. }
  968. writel(ECC1, info->reg.gpmc_ecc_control);
  969. /* Configure ecc size for BCH */
  970. val = (ecc_size1 << ECCSIZE1_SHIFT) | (ecc_size0 << ECCSIZE0_SHIFT);
  971. writel(val, info->reg.gpmc_ecc_size_config);
  972. dev_width = (chip->options & NAND_BUSWIDTH_16) ? 1 : 0;
  973. /* BCH configuration */
  974. val = ((1 << 16) | /* enable BCH */
  975. (((nerrors == 8) ? 1 : 0) << 12) | /* 8 or 4 bits */
  976. (wr_mode << 8) | /* wrap mode */
  977. (dev_width << 7) | /* bus width */
  978. (((nsectors-1) & 0x7) << 4) | /* number of sectors */
  979. (info->gpmc_cs << 1) | /* ECC CS */
  980. (0x1)); /* enable ECC */
  981. writel(val, info->reg.gpmc_ecc_config);
  982. /* Clear ecc and enable bits */
  983. writel(ECCCLEAR | ECC1, info->reg.gpmc_ecc_control);
  984. }
  985. /**
  986. * omap3_calculate_ecc_bch4 - Generate 7 bytes of ECC bytes
  987. * @mtd: MTD device structure
  988. * @dat: The pointer to data on which ecc is computed
  989. * @ecc_code: The ecc_code buffer
  990. */
  991. static int omap3_calculate_ecc_bch4(struct mtd_info *mtd, const u_char *dat,
  992. u_char *ecc_code)
  993. {
  994. struct omap_nand_info *info = container_of(mtd, struct omap_nand_info,
  995. mtd);
  996. unsigned long nsectors, val1, val2;
  997. int i;
  998. nsectors = ((readl(info->reg.gpmc_ecc_config) >> 4) & 0x7) + 1;
  999. for (i = 0; i < nsectors; i++) {
  1000. /* Read hw-computed remainder */
  1001. val1 = readl(info->reg.gpmc_bch_result0[i]);
  1002. val2 = readl(info->reg.gpmc_bch_result1[i]);
  1003. /*
  1004. * Add constant polynomial to remainder, in order to get an ecc
  1005. * sequence of 0xFFs for a buffer filled with 0xFFs; and
  1006. * left-justify the resulting polynomial.
  1007. */
  1008. *ecc_code++ = 0x28 ^ ((val2 >> 12) & 0xFF);
  1009. *ecc_code++ = 0x13 ^ ((val2 >> 4) & 0xFF);
  1010. *ecc_code++ = 0xcc ^ (((val2 & 0xF) << 4)|((val1 >> 28) & 0xF));
  1011. *ecc_code++ = 0x39 ^ ((val1 >> 20) & 0xFF);
  1012. *ecc_code++ = 0x96 ^ ((val1 >> 12) & 0xFF);
  1013. *ecc_code++ = 0xac ^ ((val1 >> 4) & 0xFF);
  1014. *ecc_code++ = 0x7f ^ ((val1 & 0xF) << 4);
  1015. }
  1016. return 0;
  1017. }
  1018. /**
  1019. * omap3_calculate_ecc_bch8 - Generate 13 bytes of ECC bytes
  1020. * @mtd: MTD device structure
  1021. * @dat: The pointer to data on which ecc is computed
  1022. * @ecc_code: The ecc_code buffer
  1023. */
  1024. static int omap3_calculate_ecc_bch8(struct mtd_info *mtd, const u_char *dat,
  1025. u_char *ecc_code)
  1026. {
  1027. struct omap_nand_info *info = container_of(mtd, struct omap_nand_info,
  1028. mtd);
  1029. unsigned long nsectors, val1, val2, val3, val4;
  1030. int i;
  1031. nsectors = ((readl(info->reg.gpmc_ecc_config) >> 4) & 0x7) + 1;
  1032. for (i = 0; i < nsectors; i++) {
  1033. /* Read hw-computed remainder */
  1034. val1 = readl(info->reg.gpmc_bch_result0[i]);
  1035. val2 = readl(info->reg.gpmc_bch_result1[i]);
  1036. val3 = readl(info->reg.gpmc_bch_result2[i]);
  1037. val4 = readl(info->reg.gpmc_bch_result3[i]);
  1038. /*
  1039. * Add constant polynomial to remainder, in order to get an ecc
  1040. * sequence of 0xFFs for a buffer filled with 0xFFs.
  1041. */
  1042. *ecc_code++ = 0xef ^ (val4 & 0xFF);
  1043. *ecc_code++ = 0x51 ^ ((val3 >> 24) & 0xFF);
  1044. *ecc_code++ = 0x2e ^ ((val3 >> 16) & 0xFF);
  1045. *ecc_code++ = 0x09 ^ ((val3 >> 8) & 0xFF);
  1046. *ecc_code++ = 0xed ^ (val3 & 0xFF);
  1047. *ecc_code++ = 0x93 ^ ((val2 >> 24) & 0xFF);
  1048. *ecc_code++ = 0x9a ^ ((val2 >> 16) & 0xFF);
  1049. *ecc_code++ = 0xc2 ^ ((val2 >> 8) & 0xFF);
  1050. *ecc_code++ = 0x97 ^ (val2 & 0xFF);
  1051. *ecc_code++ = 0x79 ^ ((val1 >> 24) & 0xFF);
  1052. *ecc_code++ = 0xe5 ^ ((val1 >> 16) & 0xFF);
  1053. *ecc_code++ = 0x24 ^ ((val1 >> 8) & 0xFF);
  1054. *ecc_code++ = 0xb5 ^ (val1 & 0xFF);
  1055. }
  1056. return 0;
  1057. }
  1058. /**
  1059. * omap3_calculate_ecc_bch - Generate bytes of ECC bytes
  1060. * @mtd: MTD device structure
  1061. * @dat: The pointer to data on which ecc is computed
  1062. * @ecc_code: The ecc_code buffer
  1063. *
  1064. * Support calculating of BCH4/8 ecc vectors for the page
  1065. */
  1066. static int omap3_calculate_ecc_bch(struct mtd_info *mtd, const u_char *dat,
  1067. u_char *ecc_code)
  1068. {
  1069. struct omap_nand_info *info = container_of(mtd, struct omap_nand_info,
  1070. mtd);
  1071. unsigned long nsectors, bch_val1, bch_val2, bch_val3, bch_val4;
  1072. int i, eccbchtsel;
  1073. nsectors = ((readl(info->reg.gpmc_ecc_config) >> 4) & 0x7) + 1;
  1074. /*
  1075. * find BCH scheme used
  1076. * 0 -> BCH4
  1077. * 1 -> BCH8
  1078. */
  1079. eccbchtsel = ((readl(info->reg.gpmc_ecc_config) >> 12) & 0x3);
  1080. for (i = 0; i < nsectors; i++) {
  1081. /* Read hw-computed remainder */
  1082. bch_val1 = readl(info->reg.gpmc_bch_result0[i]);
  1083. bch_val2 = readl(info->reg.gpmc_bch_result1[i]);
  1084. if (eccbchtsel) {
  1085. bch_val3 = readl(info->reg.gpmc_bch_result2[i]);
  1086. bch_val4 = readl(info->reg.gpmc_bch_result3[i]);
  1087. }
  1088. if (eccbchtsel) {
  1089. /* BCH8 ecc scheme */
  1090. *ecc_code++ = (bch_val4 & 0xFF);
  1091. *ecc_code++ = ((bch_val3 >> 24) & 0xFF);
  1092. *ecc_code++ = ((bch_val3 >> 16) & 0xFF);
  1093. *ecc_code++ = ((bch_val3 >> 8) & 0xFF);
  1094. *ecc_code++ = (bch_val3 & 0xFF);
  1095. *ecc_code++ = ((bch_val2 >> 24) & 0xFF);
  1096. *ecc_code++ = ((bch_val2 >> 16) & 0xFF);
  1097. *ecc_code++ = ((bch_val2 >> 8) & 0xFF);
  1098. *ecc_code++ = (bch_val2 & 0xFF);
  1099. *ecc_code++ = ((bch_val1 >> 24) & 0xFF);
  1100. *ecc_code++ = ((bch_val1 >> 16) & 0xFF);
  1101. *ecc_code++ = ((bch_val1 >> 8) & 0xFF);
  1102. *ecc_code++ = (bch_val1 & 0xFF);
  1103. /*
  1104. * Setting 14th byte to zero to handle
  1105. * erased page & maintain compatibility
  1106. * with RBL
  1107. */
  1108. *ecc_code++ = 0x0;
  1109. } else {
  1110. /* BCH4 ecc scheme */
  1111. *ecc_code++ = ((bch_val2 >> 12) & 0xFF);
  1112. *ecc_code++ = ((bch_val2 >> 4) & 0xFF);
  1113. *ecc_code++ = ((bch_val2 & 0xF) << 4) |
  1114. ((bch_val1 >> 28) & 0xF);
  1115. *ecc_code++ = ((bch_val1 >> 20) & 0xFF);
  1116. *ecc_code++ = ((bch_val1 >> 12) & 0xFF);
  1117. *ecc_code++ = ((bch_val1 >> 4) & 0xFF);
  1118. *ecc_code++ = ((bch_val1 & 0xF) << 4);
  1119. /*
  1120. * Setting 8th byte to zero to handle
  1121. * erased page
  1122. */
  1123. *ecc_code++ = 0x0;
  1124. }
  1125. }
  1126. return 0;
  1127. }
  1128. /**
  1129. * erased_sector_bitflips - count bit flips
  1130. * @data: data sector buffer
  1131. * @oob: oob buffer
  1132. * @info: omap_nand_info
  1133. *
  1134. * Check the bit flips in erased page falls below correctable level.
  1135. * If falls below, report the page as erased with correctable bit
  1136. * flip, else report as uncorrectable page.
  1137. */
  1138. static int erased_sector_bitflips(u_char *data, u_char *oob,
  1139. struct omap_nand_info *info)
  1140. {
  1141. int flip_bits = 0, i;
  1142. for (i = 0; i < info->nand.ecc.size; i++) {
  1143. flip_bits += hweight8(~data[i]);
  1144. if (flip_bits > info->nand.ecc.strength)
  1145. return 0;
  1146. }
  1147. for (i = 0; i < info->nand.ecc.bytes - 1; i++) {
  1148. flip_bits += hweight8(~oob[i]);
  1149. if (flip_bits > info->nand.ecc.strength)
  1150. return 0;
  1151. }
  1152. /*
  1153. * Bit flips falls in correctable level.
  1154. * Fill data area with 0xFF
  1155. */
  1156. if (flip_bits) {
  1157. memset(data, 0xFF, info->nand.ecc.size);
  1158. memset(oob, 0xFF, info->nand.ecc.bytes);
  1159. }
  1160. return flip_bits;
  1161. }
  1162. /**
  1163. * omap_elm_correct_data - corrects page data area in case error reported
  1164. * @mtd: MTD device structure
  1165. * @data: page data
  1166. * @read_ecc: ecc read from nand flash
  1167. * @calc_ecc: ecc read from HW ECC registers
  1168. *
  1169. * Calculated ecc vector reported as zero in case of non-error pages.
  1170. * In case of error/erased pages non-zero error vector is reported.
  1171. * In case of non-zero ecc vector, check read_ecc at fixed offset
  1172. * (x = 13/7 in case of BCH8/4 == 0) to find page programmed or not.
  1173. * To handle bit flips in this data, count the number of 0's in
  1174. * read_ecc[x] and check if it greater than 4. If it is less, it is
  1175. * programmed page, else erased page.
  1176. *
  1177. * 1. If page is erased, check with standard ecc vector (ecc vector
  1178. * for erased page to find any bit flip). If check fails, bit flip
  1179. * is present in erased page. Count the bit flips in erased page and
  1180. * if it falls under correctable level, report page with 0xFF and
  1181. * update the correctable bit information.
  1182. * 2. If error is reported on programmed page, update elm error
  1183. * vector and correct the page with ELM error correction routine.
  1184. *
  1185. */
  1186. static int omap_elm_correct_data(struct mtd_info *mtd, u_char *data,
  1187. u_char *read_ecc, u_char *calc_ecc)
  1188. {
  1189. struct omap_nand_info *info = container_of(mtd, struct omap_nand_info,
  1190. mtd);
  1191. int eccsteps = info->nand.ecc.steps;
  1192. int i , j, stat = 0;
  1193. int eccsize, eccflag, ecc_vector_size;
  1194. struct elm_errorvec err_vec[ERROR_VECTOR_MAX];
  1195. u_char *ecc_vec = calc_ecc;
  1196. u_char *spare_ecc = read_ecc;
  1197. u_char *erased_ecc_vec;
  1198. enum bch_ecc type;
  1199. bool is_error_reported = false;
  1200. /* Initialize elm error vector to zero */
  1201. memset(err_vec, 0, sizeof(err_vec));
  1202. if (info->nand.ecc.strength == BCH8_MAX_ERROR) {
  1203. type = BCH8_ECC;
  1204. erased_ecc_vec = bch8_vector;
  1205. } else {
  1206. type = BCH4_ECC;
  1207. erased_ecc_vec = bch4_vector;
  1208. }
  1209. ecc_vector_size = info->nand.ecc.bytes;
  1210. /*
  1211. * Remove extra byte padding for BCH8 RBL
  1212. * compatibility and erased page handling
  1213. */
  1214. eccsize = ecc_vector_size - 1;
  1215. for (i = 0; i < eccsteps ; i++) {
  1216. eccflag = 0; /* initialize eccflag */
  1217. /*
  1218. * Check any error reported,
  1219. * In case of error, non zero ecc reported.
  1220. */
  1221. for (j = 0; (j < eccsize); j++) {
  1222. if (calc_ecc[j] != 0) {
  1223. eccflag = 1; /* non zero ecc, error present */
  1224. break;
  1225. }
  1226. }
  1227. if (eccflag == 1) {
  1228. /*
  1229. * Set threshold to minimum of 4, half of ecc.strength/2
  1230. * to allow max bit flip in byte to 4
  1231. */
  1232. unsigned int threshold = min_t(unsigned int, 4,
  1233. info->nand.ecc.strength / 2);
  1234. /*
  1235. * Check data area is programmed by counting
  1236. * number of 0's at fixed offset in spare area.
  1237. * Checking count of 0's against threshold.
  1238. * In case programmed page expects at least threshold
  1239. * zeros in byte.
  1240. * If zeros are less than threshold for programmed page/
  1241. * zeros are more than threshold erased page, either
  1242. * case page reported as uncorrectable.
  1243. */
  1244. if (hweight8(~read_ecc[eccsize]) >= threshold) {
  1245. /*
  1246. * Update elm error vector as
  1247. * data area is programmed
  1248. */
  1249. err_vec[i].error_reported = true;
  1250. is_error_reported = true;
  1251. } else {
  1252. /* Error reported in erased page */
  1253. int bitflip_count;
  1254. u_char *buf = &data[info->nand.ecc.size * i];
  1255. if (memcmp(calc_ecc, erased_ecc_vec, eccsize)) {
  1256. bitflip_count = erased_sector_bitflips(
  1257. buf, read_ecc, info);
  1258. if (bitflip_count)
  1259. stat += bitflip_count;
  1260. else
  1261. return -EINVAL;
  1262. }
  1263. }
  1264. }
  1265. /* Update the ecc vector */
  1266. calc_ecc += ecc_vector_size;
  1267. read_ecc += ecc_vector_size;
  1268. }
  1269. /* Check if any error reported */
  1270. if (!is_error_reported)
  1271. return 0;
  1272. /* Decode BCH error using ELM module */
  1273. elm_decode_bch_error_page(info->elm_dev, ecc_vec, err_vec);
  1274. for (i = 0; i < eccsteps; i++) {
  1275. if (err_vec[i].error_reported) {
  1276. for (j = 0; j < err_vec[i].error_count; j++) {
  1277. u32 bit_pos, byte_pos, error_max, pos;
  1278. if (type == BCH8_ECC)
  1279. error_max = BCH8_ECC_MAX;
  1280. else
  1281. error_max = BCH4_ECC_MAX;
  1282. if (info->nand.ecc.strength == BCH8_MAX_ERROR)
  1283. pos = err_vec[i].error_loc[j];
  1284. else
  1285. /* Add 4 to take care 4 bit padding */
  1286. pos = err_vec[i].error_loc[j] +
  1287. BCH4_BIT_PAD;
  1288. /* Calculate bit position of error */
  1289. bit_pos = pos % 8;
  1290. /* Calculate byte position of error */
  1291. byte_pos = (error_max - pos - 1) / 8;
  1292. if (pos < error_max) {
  1293. if (byte_pos < 512)
  1294. data[byte_pos] ^= 1 << bit_pos;
  1295. else
  1296. spare_ecc[byte_pos - 512] ^=
  1297. 1 << bit_pos;
  1298. }
  1299. /* else, not interested to correct ecc */
  1300. }
  1301. }
  1302. /* Update number of correctable errors */
  1303. stat += err_vec[i].error_count;
  1304. /* Update page data with sector size */
  1305. data += info->nand.ecc.size;
  1306. spare_ecc += ecc_vector_size;
  1307. }
  1308. for (i = 0; i < eccsteps; i++)
  1309. /* Return error if uncorrectable error present */
  1310. if (err_vec[i].error_uncorrectable)
  1311. return -EINVAL;
  1312. return stat;
  1313. }
  1314. /**
  1315. * omap3_correct_data_bch - Decode received data and correct errors
  1316. * @mtd: MTD device structure
  1317. * @data: page data
  1318. * @read_ecc: ecc read from nand flash
  1319. * @calc_ecc: ecc read from HW ECC registers
  1320. */
  1321. static int omap3_correct_data_bch(struct mtd_info *mtd, u_char *data,
  1322. u_char *read_ecc, u_char *calc_ecc)
  1323. {
  1324. int i, count;
  1325. /* cannot correct more than 8 errors */
  1326. unsigned int errloc[8];
  1327. struct omap_nand_info *info = container_of(mtd, struct omap_nand_info,
  1328. mtd);
  1329. count = decode_bch(info->bch, NULL, 512, read_ecc, calc_ecc, NULL,
  1330. errloc);
  1331. if (count > 0) {
  1332. /* correct errors */
  1333. for (i = 0; i < count; i++) {
  1334. /* correct data only, not ecc bytes */
  1335. if (errloc[i] < 8*512)
  1336. data[errloc[i]/8] ^= 1 << (errloc[i] & 7);
  1337. pr_debug("corrected bitflip %u\n", errloc[i]);
  1338. }
  1339. } else if (count < 0) {
  1340. pr_err("ecc unrecoverable error\n");
  1341. }
  1342. return count;
  1343. }
  1344. /**
  1345. * omap_write_page_bch - BCH ecc based write page function for entire page
  1346. * @mtd: mtd info structure
  1347. * @chip: nand chip info structure
  1348. * @buf: data buffer
  1349. * @oob_required: must write chip->oob_poi to OOB
  1350. *
  1351. * Custom write page method evolved to support multi sector writing in one shot
  1352. */
  1353. static int omap_write_page_bch(struct mtd_info *mtd, struct nand_chip *chip,
  1354. const uint8_t *buf, int oob_required)
  1355. {
  1356. int i;
  1357. uint8_t *ecc_calc = chip->buffers->ecccalc;
  1358. uint32_t *eccpos = chip->ecc.layout->eccpos;
  1359. /* Enable GPMC ecc engine */
  1360. chip->ecc.hwctl(mtd, NAND_ECC_WRITE);
  1361. /* Write data */
  1362. chip->write_buf(mtd, buf, mtd->writesize);
  1363. /* Update ecc vector from GPMC result registers */
  1364. chip->ecc.calculate(mtd, buf, &ecc_calc[0]);
  1365. for (i = 0; i < chip->ecc.total; i++)
  1366. chip->oob_poi[eccpos[i]] = ecc_calc[i];
  1367. /* Write ecc vector to OOB area */
  1368. chip->write_buf(mtd, chip->oob_poi, mtd->oobsize);
  1369. return 0;
  1370. }
  1371. /**
  1372. * omap_read_page_bch - BCH ecc based page read function for entire page
  1373. * @mtd: mtd info structure
  1374. * @chip: nand chip info structure
  1375. * @buf: buffer to store read data
  1376. * @oob_required: caller requires OOB data read to chip->oob_poi
  1377. * @page: page number to read
  1378. *
  1379. * For BCH ecc scheme, GPMC used for syndrome calculation and ELM module
  1380. * used for error correction.
  1381. * Custom method evolved to support ELM error correction & multi sector
  1382. * reading. On reading page data area is read along with OOB data with
  1383. * ecc engine enabled. ecc vector updated after read of OOB data.
  1384. * For non error pages ecc vector reported as zero.
  1385. */
  1386. static int omap_read_page_bch(struct mtd_info *mtd, struct nand_chip *chip,
  1387. uint8_t *buf, int oob_required, int page)
  1388. {
  1389. uint8_t *ecc_calc = chip->buffers->ecccalc;
  1390. uint8_t *ecc_code = chip->buffers->ecccode;
  1391. uint32_t *eccpos = chip->ecc.layout->eccpos;
  1392. uint8_t *oob = &chip->oob_poi[eccpos[0]];
  1393. uint32_t oob_pos = mtd->writesize + chip->ecc.layout->eccpos[0];
  1394. int stat;
  1395. unsigned int max_bitflips = 0;
  1396. /* Enable GPMC ecc engine */
  1397. chip->ecc.hwctl(mtd, NAND_ECC_READ);
  1398. /* Read data */
  1399. chip->read_buf(mtd, buf, mtd->writesize);
  1400. /* Read oob bytes */
  1401. chip->cmdfunc(mtd, NAND_CMD_RNDOUT, oob_pos, -1);
  1402. chip->read_buf(mtd, oob, chip->ecc.total);
  1403. /* Calculate ecc bytes */
  1404. chip->ecc.calculate(mtd, buf, ecc_calc);
  1405. memcpy(ecc_code, &chip->oob_poi[eccpos[0]], chip->ecc.total);
  1406. stat = chip->ecc.correct(mtd, buf, ecc_code, ecc_calc);
  1407. if (stat < 0) {
  1408. mtd->ecc_stats.failed++;
  1409. } else {
  1410. mtd->ecc_stats.corrected += stat;
  1411. max_bitflips = max_t(unsigned int, max_bitflips, stat);
  1412. }
  1413. return max_bitflips;
  1414. }
  1415. /**
  1416. * omap3_free_bch - Release BCH ecc resources
  1417. * @mtd: MTD device structure
  1418. */
  1419. static void omap3_free_bch(struct mtd_info *mtd)
  1420. {
  1421. struct omap_nand_info *info = container_of(mtd, struct omap_nand_info,
  1422. mtd);
  1423. if (info->bch) {
  1424. free_bch(info->bch);
  1425. info->bch = NULL;
  1426. }
  1427. }
  1428. /**
  1429. * omap3_init_bch - Initialize BCH ECC
  1430. * @mtd: MTD device structure
  1431. * @ecc_opt: OMAP ECC mode (OMAP_ECC_BCH4_CODE_HW or OMAP_ECC_BCH8_CODE_HW)
  1432. */
  1433. static int omap3_init_bch(struct mtd_info *mtd, int ecc_opt)
  1434. {
  1435. int max_errors;
  1436. struct omap_nand_info *info = container_of(mtd, struct omap_nand_info,
  1437. mtd);
  1438. #ifdef CONFIG_MTD_NAND_OMAP_BCH8
  1439. const int hw_errors = BCH8_MAX_ERROR;
  1440. #else
  1441. const int hw_errors = BCH4_MAX_ERROR;
  1442. #endif
  1443. enum bch_ecc bch_type;
  1444. const __be32 *parp;
  1445. int lenp;
  1446. struct device_node *elm_node;
  1447. info->bch = NULL;
  1448. max_errors = (ecc_opt == OMAP_ECC_BCH8_CODE_HW) ?
  1449. BCH8_MAX_ERROR : BCH4_MAX_ERROR;
  1450. if (max_errors != hw_errors) {
  1451. pr_err("cannot configure %d-bit BCH ecc, only %d-bit supported",
  1452. max_errors, hw_errors);
  1453. goto fail;
  1454. }
  1455. info->nand.ecc.size = 512;
  1456. info->nand.ecc.hwctl = omap3_enable_hwecc_bch;
  1457. info->nand.ecc.mode = NAND_ECC_HW;
  1458. info->nand.ecc.strength = max_errors;
  1459. if (hw_errors == BCH8_MAX_ERROR)
  1460. bch_type = BCH8_ECC;
  1461. else
  1462. bch_type = BCH4_ECC;
  1463. /* Detect availability of ELM module */
  1464. parp = of_get_property(info->of_node, "elm_id", &lenp);
  1465. if ((parp == NULL) && (lenp != (sizeof(void *) * 2))) {
  1466. pr_err("Missing elm_id property, fall back to Software BCH\n");
  1467. info->is_elm_used = false;
  1468. } else {
  1469. struct platform_device *pdev;
  1470. elm_node = of_find_node_by_phandle(be32_to_cpup(parp));
  1471. pdev = of_find_device_by_node(elm_node);
  1472. info->elm_dev = &pdev->dev;
  1473. elm_config(info->elm_dev, bch_type);
  1474. info->is_elm_used = true;
  1475. }
  1476. if (info->is_elm_used && (mtd->writesize <= 4096)) {
  1477. if (hw_errors == BCH8_MAX_ERROR)
  1478. info->nand.ecc.bytes = BCH8_SIZE;
  1479. else
  1480. info->nand.ecc.bytes = BCH4_SIZE;
  1481. info->nand.ecc.correct = omap_elm_correct_data;
  1482. info->nand.ecc.calculate = omap3_calculate_ecc_bch;
  1483. info->nand.ecc.read_page = omap_read_page_bch;
  1484. info->nand.ecc.write_page = omap_write_page_bch;
  1485. } else {
  1486. /*
  1487. * software bch library is only used to detect and
  1488. * locate errors
  1489. */
  1490. info->bch = init_bch(13, max_errors,
  1491. 0x201b /* hw polynomial */);
  1492. if (!info->bch)
  1493. goto fail;
  1494. info->nand.ecc.correct = omap3_correct_data_bch;
  1495. /*
  1496. * The number of corrected errors in an ecc block that will
  1497. * trigger block scrubbing defaults to the ecc strength (4 or 8)
  1498. * Set mtd->bitflip_threshold here to define a custom threshold.
  1499. */
  1500. if (max_errors == 8) {
  1501. info->nand.ecc.bytes = 13;
  1502. info->nand.ecc.calculate = omap3_calculate_ecc_bch8;
  1503. } else {
  1504. info->nand.ecc.bytes = 7;
  1505. info->nand.ecc.calculate = omap3_calculate_ecc_bch4;
  1506. }
  1507. }
  1508. pr_info("enabling NAND BCH ecc with %d-bit correction\n", max_errors);
  1509. return 0;
  1510. fail:
  1511. omap3_free_bch(mtd);
  1512. return -1;
  1513. }
  1514. /**
  1515. * omap3_init_bch_tail - Build an oob layout for BCH ECC correction.
  1516. * @mtd: MTD device structure
  1517. */
  1518. static int omap3_init_bch_tail(struct mtd_info *mtd)
  1519. {
  1520. int i, steps, offset;
  1521. struct omap_nand_info *info = container_of(mtd, struct omap_nand_info,
  1522. mtd);
  1523. struct nand_ecclayout *layout = &info->ecclayout;
  1524. /* build oob layout */
  1525. steps = mtd->writesize/info->nand.ecc.size;
  1526. layout->eccbytes = steps*info->nand.ecc.bytes;
  1527. /* do not bother creating special oob layouts for small page devices */
  1528. if (mtd->oobsize < 64) {
  1529. pr_err("BCH ecc is not supported on small page devices\n");
  1530. goto fail;
  1531. }
  1532. /* reserve 2 bytes for bad block marker */
  1533. if (layout->eccbytes+2 > mtd->oobsize) {
  1534. pr_err("no oob layout available for oobsize %d eccbytes %u\n",
  1535. mtd->oobsize, layout->eccbytes);
  1536. goto fail;
  1537. }
  1538. /* ECC layout compatible with RBL for BCH8 */
  1539. if (info->is_elm_used && (info->nand.ecc.bytes == BCH8_SIZE))
  1540. offset = 2;
  1541. else
  1542. offset = mtd->oobsize - layout->eccbytes;
  1543. /* put ecc bytes at oob tail */
  1544. for (i = 0; i < layout->eccbytes; i++)
  1545. layout->eccpos[i] = offset + i;
  1546. if (info->is_elm_used && (info->nand.ecc.bytes == BCH8_SIZE))
  1547. layout->oobfree[0].offset = 2 + layout->eccbytes * steps;
  1548. else
  1549. layout->oobfree[0].offset = 2;
  1550. layout->oobfree[0].length = mtd->oobsize-2-layout->eccbytes;
  1551. info->nand.ecc.layout = layout;
  1552. if (!(info->nand.options & NAND_BUSWIDTH_16))
  1553. info->nand.badblock_pattern = &bb_descrip_flashbased;
  1554. return 0;
  1555. fail:
  1556. omap3_free_bch(mtd);
  1557. return -1;
  1558. }
  1559. #else
  1560. static int omap3_init_bch(struct mtd_info *mtd, int ecc_opt)
  1561. {
  1562. pr_err("CONFIG_MTD_NAND_OMAP_BCH is not enabled\n");
  1563. return -1;
  1564. }
  1565. static int omap3_init_bch_tail(struct mtd_info *mtd)
  1566. {
  1567. return -1;
  1568. }
  1569. static void omap3_free_bch(struct mtd_info *mtd)
  1570. {
  1571. }
  1572. #endif /* CONFIG_MTD_NAND_OMAP_BCH */
  1573. static int omap_nand_probe(struct platform_device *pdev)
  1574. {
  1575. struct omap_nand_info *info;
  1576. struct omap_nand_platform_data *pdata;
  1577. int err;
  1578. int i, offset;
  1579. dma_cap_mask_t mask;
  1580. unsigned sig;
  1581. struct resource *res;
  1582. struct mtd_part_parser_data ppdata = {};
  1583. pdata = pdev->dev.platform_data;
  1584. if (pdata == NULL) {
  1585. dev_err(&pdev->dev, "platform data missing\n");
  1586. return -ENODEV;
  1587. }
  1588. info = kzalloc(sizeof(struct omap_nand_info), GFP_KERNEL);
  1589. if (!info)
  1590. return -ENOMEM;
  1591. platform_set_drvdata(pdev, info);
  1592. spin_lock_init(&info->controller.lock);
  1593. init_waitqueue_head(&info->controller.wq);
  1594. info->pdev = pdev;
  1595. info->gpmc_cs = pdata->cs;
  1596. info->reg = pdata->reg;
  1597. info->mtd.priv = &info->nand;
  1598. info->mtd.name = dev_name(&pdev->dev);
  1599. info->mtd.owner = THIS_MODULE;
  1600. info->nand.options = pdata->devsize;
  1601. info->nand.options |= NAND_SKIP_BBTSCAN;
  1602. #ifdef CONFIG_MTD_NAND_OMAP_BCH
  1603. info->of_node = pdata->of_node;
  1604. #endif
  1605. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  1606. if (res == NULL) {
  1607. err = -EINVAL;
  1608. dev_err(&pdev->dev, "error getting memory resource\n");
  1609. goto out_free_info;
  1610. }
  1611. info->phys_base = res->start;
  1612. info->mem_size = resource_size(res);
  1613. if (!request_mem_region(info->phys_base, info->mem_size,
  1614. pdev->dev.driver->name)) {
  1615. err = -EBUSY;
  1616. goto out_free_info;
  1617. }
  1618. info->nand.IO_ADDR_R = ioremap(info->phys_base, info->mem_size);
  1619. if (!info->nand.IO_ADDR_R) {
  1620. err = -ENOMEM;
  1621. goto out_release_mem_region;
  1622. }
  1623. info->nand.controller = &info->controller;
  1624. info->nand.IO_ADDR_W = info->nand.IO_ADDR_R;
  1625. info->nand.cmd_ctrl = omap_hwcontrol;
  1626. /*
  1627. * If RDY/BSY line is connected to OMAP then use the omap ready
  1628. * function and the generic nand_wait function which reads the status
  1629. * register after monitoring the RDY/BSY line. Otherwise use a standard
  1630. * chip delay which is slightly more than tR (AC Timing) of the NAND
  1631. * device and read status register until you get a failure or success
  1632. */
  1633. if (pdata->dev_ready) {
  1634. info->nand.dev_ready = omap_dev_ready;
  1635. info->nand.chip_delay = 0;
  1636. } else {
  1637. info->nand.waitfunc = omap_wait;
  1638. info->nand.chip_delay = 50;
  1639. }
  1640. switch (pdata->xfer_type) {
  1641. case NAND_OMAP_PREFETCH_POLLED:
  1642. info->nand.read_buf = omap_read_buf_pref;
  1643. info->nand.write_buf = omap_write_buf_pref;
  1644. break;
  1645. case NAND_OMAP_POLLED:
  1646. if (info->nand.options & NAND_BUSWIDTH_16) {
  1647. info->nand.read_buf = omap_read_buf16;
  1648. info->nand.write_buf = omap_write_buf16;
  1649. } else {
  1650. info->nand.read_buf = omap_read_buf8;
  1651. info->nand.write_buf = omap_write_buf8;
  1652. }
  1653. break;
  1654. case NAND_OMAP_PREFETCH_DMA:
  1655. dma_cap_zero(mask);
  1656. dma_cap_set(DMA_SLAVE, mask);
  1657. sig = OMAP24XX_DMA_GPMC;
  1658. info->dma = dma_request_channel(mask, omap_dma_filter_fn, &sig);
  1659. if (!info->dma) {
  1660. dev_err(&pdev->dev, "DMA engine request failed\n");
  1661. err = -ENXIO;
  1662. goto out_release_mem_region;
  1663. } else {
  1664. struct dma_slave_config cfg;
  1665. memset(&cfg, 0, sizeof(cfg));
  1666. cfg.src_addr = info->phys_base;
  1667. cfg.dst_addr = info->phys_base;
  1668. cfg.src_addr_width = DMA_SLAVE_BUSWIDTH_4_BYTES;
  1669. cfg.dst_addr_width = DMA_SLAVE_BUSWIDTH_4_BYTES;
  1670. cfg.src_maxburst = 16;
  1671. cfg.dst_maxburst = 16;
  1672. err = dmaengine_slave_config(info->dma, &cfg);
  1673. if (err) {
  1674. dev_err(&pdev->dev, "DMA engine slave config failed: %d\n",
  1675. err);
  1676. goto out_release_mem_region;
  1677. }
  1678. info->nand.read_buf = omap_read_buf_dma_pref;
  1679. info->nand.write_buf = omap_write_buf_dma_pref;
  1680. }
  1681. break;
  1682. case NAND_OMAP_PREFETCH_IRQ:
  1683. info->gpmc_irq_fifo = platform_get_irq(pdev, 0);
  1684. if (info->gpmc_irq_fifo <= 0) {
  1685. dev_err(&pdev->dev, "error getting fifo irq\n");
  1686. err = -ENODEV;
  1687. goto out_release_mem_region;
  1688. }
  1689. err = request_irq(info->gpmc_irq_fifo, omap_nand_irq,
  1690. IRQF_SHARED, "gpmc-nand-fifo", info);
  1691. if (err) {
  1692. dev_err(&pdev->dev, "requesting irq(%d) error:%d",
  1693. info->gpmc_irq_fifo, err);
  1694. info->gpmc_irq_fifo = 0;
  1695. goto out_release_mem_region;
  1696. }
  1697. info->gpmc_irq_count = platform_get_irq(pdev, 1);
  1698. if (info->gpmc_irq_count <= 0) {
  1699. dev_err(&pdev->dev, "error getting count irq\n");
  1700. err = -ENODEV;
  1701. goto out_release_mem_region;
  1702. }
  1703. err = request_irq(info->gpmc_irq_count, omap_nand_irq,
  1704. IRQF_SHARED, "gpmc-nand-count", info);
  1705. if (err) {
  1706. dev_err(&pdev->dev, "requesting irq(%d) error:%d",
  1707. info->gpmc_irq_count, err);
  1708. info->gpmc_irq_count = 0;
  1709. goto out_release_mem_region;
  1710. }
  1711. info->nand.read_buf = omap_read_buf_irq_pref;
  1712. info->nand.write_buf = omap_write_buf_irq_pref;
  1713. break;
  1714. default:
  1715. dev_err(&pdev->dev,
  1716. "xfer_type(%d) not supported!\n", pdata->xfer_type);
  1717. err = -EINVAL;
  1718. goto out_release_mem_region;
  1719. }
  1720. /* select the ecc type */
  1721. if (pdata->ecc_opt == OMAP_ECC_HAMMING_CODE_DEFAULT)
  1722. info->nand.ecc.mode = NAND_ECC_SOFT;
  1723. else if ((pdata->ecc_opt == OMAP_ECC_HAMMING_CODE_HW) ||
  1724. (pdata->ecc_opt == OMAP_ECC_HAMMING_CODE_HW_ROMCODE)) {
  1725. info->nand.ecc.bytes = 3;
  1726. info->nand.ecc.size = 512;
  1727. info->nand.ecc.strength = 1;
  1728. info->nand.ecc.calculate = omap_calculate_ecc;
  1729. info->nand.ecc.hwctl = omap_enable_hwecc;
  1730. info->nand.ecc.correct = omap_correct_data;
  1731. info->nand.ecc.mode = NAND_ECC_HW;
  1732. } else if ((pdata->ecc_opt == OMAP_ECC_BCH4_CODE_HW) ||
  1733. (pdata->ecc_opt == OMAP_ECC_BCH8_CODE_HW)) {
  1734. err = omap3_init_bch(&info->mtd, pdata->ecc_opt);
  1735. if (err) {
  1736. err = -EINVAL;
  1737. goto out_release_mem_region;
  1738. }
  1739. }
  1740. /* DIP switches on some boards change between 8 and 16 bit
  1741. * bus widths for flash. Try the other width if the first try fails.
  1742. */
  1743. if (nand_scan_ident(&info->mtd, 1, NULL)) {
  1744. info->nand.options ^= NAND_BUSWIDTH_16;
  1745. if (nand_scan_ident(&info->mtd, 1, NULL)) {
  1746. err = -ENXIO;
  1747. goto out_release_mem_region;
  1748. }
  1749. }
  1750. /* rom code layout */
  1751. if (pdata->ecc_opt == OMAP_ECC_HAMMING_CODE_HW_ROMCODE) {
  1752. if (info->nand.options & NAND_BUSWIDTH_16)
  1753. offset = 2;
  1754. else {
  1755. offset = 1;
  1756. info->nand.badblock_pattern = &bb_descrip_flashbased;
  1757. }
  1758. omap_oobinfo.eccbytes = 3 * (info->mtd.oobsize/16);
  1759. for (i = 0; i < omap_oobinfo.eccbytes; i++)
  1760. omap_oobinfo.eccpos[i] = i+offset;
  1761. omap_oobinfo.oobfree->offset = offset + omap_oobinfo.eccbytes;
  1762. omap_oobinfo.oobfree->length = info->mtd.oobsize -
  1763. (offset + omap_oobinfo.eccbytes);
  1764. info->nand.ecc.layout = &omap_oobinfo;
  1765. } else if ((pdata->ecc_opt == OMAP_ECC_BCH4_CODE_HW) ||
  1766. (pdata->ecc_opt == OMAP_ECC_BCH8_CODE_HW)) {
  1767. /* build OOB layout for BCH ECC correction */
  1768. err = omap3_init_bch_tail(&info->mtd);
  1769. if (err) {
  1770. err = -EINVAL;
  1771. goto out_release_mem_region;
  1772. }
  1773. }
  1774. /* second phase scan */
  1775. if (nand_scan_tail(&info->mtd)) {
  1776. err = -ENXIO;
  1777. goto out_release_mem_region;
  1778. }
  1779. ppdata.of_node = pdata->of_node;
  1780. mtd_device_parse_register(&info->mtd, NULL, &ppdata, pdata->parts,
  1781. pdata->nr_parts);
  1782. platform_set_drvdata(pdev, &info->mtd);
  1783. return 0;
  1784. out_release_mem_region:
  1785. if (info->dma)
  1786. dma_release_channel(info->dma);
  1787. if (info->gpmc_irq_count > 0)
  1788. free_irq(info->gpmc_irq_count, info);
  1789. if (info->gpmc_irq_fifo > 0)
  1790. free_irq(info->gpmc_irq_fifo, info);
  1791. release_mem_region(info->phys_base, info->mem_size);
  1792. out_free_info:
  1793. kfree(info);
  1794. return err;
  1795. }
  1796. static int omap_nand_remove(struct platform_device *pdev)
  1797. {
  1798. struct mtd_info *mtd = platform_get_drvdata(pdev);
  1799. struct omap_nand_info *info = container_of(mtd, struct omap_nand_info,
  1800. mtd);
  1801. omap3_free_bch(&info->mtd);
  1802. platform_set_drvdata(pdev, NULL);
  1803. if (info->dma)
  1804. dma_release_channel(info->dma);
  1805. if (info->gpmc_irq_count > 0)
  1806. free_irq(info->gpmc_irq_count, info);
  1807. if (info->gpmc_irq_fifo > 0)
  1808. free_irq(info->gpmc_irq_fifo, info);
  1809. /* Release NAND device, its internal structures and partitions */
  1810. nand_release(&info->mtd);
  1811. iounmap(info->nand.IO_ADDR_R);
  1812. release_mem_region(info->phys_base, info->mem_size);
  1813. kfree(info);
  1814. return 0;
  1815. }
  1816. static struct platform_driver omap_nand_driver = {
  1817. .probe = omap_nand_probe,
  1818. .remove = omap_nand_remove,
  1819. .driver = {
  1820. .name = DRIVER_NAME,
  1821. .owner = THIS_MODULE,
  1822. },
  1823. };
  1824. module_platform_driver(omap_nand_driver);
  1825. MODULE_ALIAS("platform:" DRIVER_NAME);
  1826. MODULE_LICENSE("GPL");
  1827. MODULE_DESCRIPTION("Glue layer for NAND flash on TI OMAP boards");