omap2.c 59 KB

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