omap2.c 59 KB

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