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