omap2.c 58 KB

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